Skip to content

Commit dd0116c

Browse files
committed
Upload Artifacts to GitHub Release Page
1 parent b9dd8c9 commit dd0116c

File tree

16 files changed

+198
-92
lines changed

16 files changed

+198
-92
lines changed

.github/workflows/release.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,13 @@ jobs:
117117
url: https://pypi.org/p/mlc-python
118118
permissions:
119119
id-token: write
120+
contents: write
120121
steps:
121122
- uses: actions/setup-python@v5
122123
with:
123124
python-version: ${{ env.MLC_PYTHON_VERSION }}
125+
- name: Checkout code
126+
uses: actions/checkout@v4
124127
- name: Download all artifacts
125128
uses: actions/download-artifact@v4
126129
with:
@@ -132,6 +135,33 @@ jobs:
132135
mv wheelhouse/wheels-macos/*.whl dist/
133136
mv wheelhouse/wheels-linux/*.whl dist/
134137
mv wheelhouse/wheels-windows/*.whl dist/
138+
- name: Upload wheels to release
139+
env:
140+
GITHUB_TOKEN: ${{ secrets.PYMLC_GITHUB_TOKEN }}
141+
run: |
142+
# Get the release ID
143+
release_id=$(gh api repos/${{ github.repository }}/releases/tags/${{ github.ref_name }} --jq '.id')
144+
if [ -z "$release_id" ]; then
145+
echo "Error: Could not find release with tag ${{ github.ref_name }}"
146+
exit 1
147+
fi
148+
upload_url=https://uploads.github.com/repos/${{ github.repository }}/releases/$release_id/assets
149+
echo "Uploading to release ID: $release_id. Upload URL: $upload_url"
150+
for wheel in dist/*.whl; do
151+
echo "⏫ Uploading $(basename $wheel)"
152+
response=$(curl -s -w "%{http_code}" -X POST \
153+
-H "Authorization: token $GITHUB_TOKEN" \
154+
-H "Content-Type: application/octet-stream" \
155+
--data-binary @"$wheel" \
156+
"$upload_url?name=$(basename $wheel)")
157+
http_code=${response: -3}
158+
if [ $http_code -eq 201 ]; then
159+
echo "🎉 Successfully uploaded $(basename $wheel)"
160+
else
161+
echo "❌ Failed to upload $(basename $wheel). HTTP status: $http_code. Error response: ${response%???}"
162+
exit 1
163+
fi
164+
done
135165
- name: Publish to PyPI
136166
uses: pypa/gh-action-pypi-publish@release/v1
137167
with:

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)
22

33
project(
44
mlc
5-
VERSION 0.0.3
5+
VERSION 0.0.4
66
DESCRIPTION "PyMLC"
77
LANGUAGES C CXX
88
)

cpp/registry.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -261,17 +261,18 @@ inline TypeTable *TypeTable::New() {
261261
self->type_table.resize(1024);
262262
self->type_key_to_info.reserve(1024);
263263
self->num_types = static_cast<int32_t>(MLCTypeIndex::kMLCDynObjectBegin);
264-
#define MLC_TYPE_TABLE_INIT_TYPE(TypeIndex, UnderlyingType, Self) \
265-
Self->TypeRegister(-1, static_cast<int32_t>(TypeIndex), ::mlc::base::PODTraits<UnderlyingType>::Type2Str(), \
266-
PODGetterSetter<UnderlyingType>::Getter, PODGetterSetter<UnderlyingType>::Setter);
267-
268-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCNone, std::nullptr_t, self);
269-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCInt, int64_t, self);
270-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCFloat, double, self);
271-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCPtr, void *, self);
272-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCDevice, DLDevice, self);
273-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCDataType, DLDataType, self);
274-
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCRawStr, const char *, self);
264+
#define MLC_TYPE_TABLE_INIT_TYPE(UnderlyingType, Self) \
265+
Self->TypeRegister(-1, ::mlc::base::PODTraits<UnderlyingType>::default_type_index, \
266+
::mlc::base::PODTraits<UnderlyingType>::Type2Str(), PODGetterSetter<UnderlyingType>::Getter, \
267+
PODGetterSetter<UnderlyingType>::Setter);
268+
269+
MLC_TYPE_TABLE_INIT_TYPE(std::nullptr_t, self);
270+
MLC_TYPE_TABLE_INIT_TYPE(int64_t, self);
271+
MLC_TYPE_TABLE_INIT_TYPE(double, self);
272+
MLC_TYPE_TABLE_INIT_TYPE(void *, self);
273+
MLC_TYPE_TABLE_INIT_TYPE(DLDevice, self);
274+
MLC_TYPE_TABLE_INIT_TYPE(DLDataType, self);
275+
MLC_TYPE_TABLE_INIT_TYPE(const char *, self);
275276
#undef MLC_TYPE_TABLE_INIT_TYPE
276277
return self;
277278
}

include/mlc/base/common.h

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <mlc/c_api.h>
2020
#include <sstream>
2121
#include <type_traits>
22+
#include <vector>
2223
#if MLC_DEBUG_MODE == 1
2324
#include <iostream>
2425
#endif
@@ -159,6 +160,7 @@ template <typename T> constexpr bool IsPOD = IsPODImpl<T>::value;
159160
template <typename, typename = void> struct IsObjImpl : std::false_type {};
160161
template <typename T> struct IsObjImpl<T, std::void_t<decltype(T::_type_info)>> : std::true_type {};
161162
template <typename T> constexpr static bool IsObj = IsObjImpl<T>::value;
163+
template <typename T> constexpr static bool IsRawObjPtr = std::is_pointer_v<T> && IsObj<std::remove_pointer_t<T>>;
162164
template <typename ObjectType, typename = std::enable_if_t<IsObj<ObjectType>>> struct DefaultObjectAllocator;
163165
template <typename T, typename = void> struct AllocatorOfImplImpl { using Type = DefaultObjectAllocator<T>; };
164166
template <typename T> struct AllocatorOfImplImpl<T, std::void_t<typename T::Allocator>> { using Type = typename T::Allocator; };
@@ -243,39 +245,6 @@ template <typename, typename = void> struct PODTraits {};
243245
template <typename T, typename = void> struct ObjPtrTraits;
244246
template <typename T> struct ObjPtrTraits<ListObj<T>, void>;
245247

246-
template <typename _T> struct Type2Str {
247-
static std::string Run() {
248-
using T = RemoveCR<_T>;
249-
if constexpr (std::is_same_v<T, Any>) {
250-
return "Any";
251-
} else if constexpr (std::is_same_v<T, AnyView>) {
252-
return "AnyView";
253-
} else if constexpr (std::is_same_v<T, void>) {
254-
return "void";
255-
} else if constexpr (IsPOD<T>) {
256-
return PODTraits<T>::Type2Str();
257-
} else if constexpr (std::is_pointer_v<T> && IsObj<std::remove_pointer_t<T>>) {
258-
return std::string(std::remove_pointer_t<T>::_type_key) + " *";
259-
} else if constexpr (std::is_same_v<T, UList>) {
260-
return "list[Any]";
261-
} else if constexpr (std::is_same_v<T, UDict>) {
262-
return "dict[Any, Any]";
263-
} else if constexpr (std::is_base_of_v<UList, T>) {
264-
return "list[" + Type2Str<typename T::TElem>::Run() + "]";
265-
} else if constexpr (std::is_base_of_v<UDict, T>) {
266-
return "dict[" + Type2Str<typename T::TKey>::Run() + ", " + Type2Str<typename T::TValue>::Run() + "]";
267-
} else if constexpr (IsRef<T>) {
268-
return "Ref<" + std::string(T::TObj::_type_key) + ">";
269-
} else if constexpr (IsObjRef<T>) {
270-
return std::string(T::TObj::_type_key);
271-
} else if constexpr (IsObj<T>) {
272-
return std::string(T::_type_key);
273-
} else {
274-
static_assert(std::is_void_v<T>, "Unsupported type");
275-
}
276-
}
277-
};
278-
279248
/********** Section 3. Errors *********/
280249

281250
struct TemporaryTypeError : public std::exception {};
@@ -306,14 +275,27 @@ StrObj *StrCopyFromCharArray(const char *source, size_t length);
306275
void FuncCall(const void *func, int32_t num_args, const MLCAny *args, MLCAny *ret);
307276
template <typename DerivedType, typename SelfType = Object> bool IsInstanceOf(const MLCAny *self);
308277

309-
MLC_INLINE const char *TypeIndex2TypeKey(int32_t type_index) {
278+
MLC_INLINE MLCTypeInfo *TypeIndex2TypeInfo(int32_t type_index) {
310279
MLCTypeInfo *type_info;
311280
MLCTypeIndex2Info(nullptr, type_index, &type_info);
312-
return type_info ? type_info->type_key : "(undefined)";
281+
return type_info;
282+
}
283+
284+
MLC_INLINE const char *TypeIndex2TypeKey(int32_t type_index) {
285+
if (MLCTypeInfo *type_info = TypeIndex2TypeInfo(type_index)) {
286+
return type_info->type_key;
287+
}
288+
return "(undefined)";
313289
}
314290

315291
MLC_INLINE const char *TypeIndex2TypeKey(const MLCAny *self) {
316-
return self == nullptr ? "None" : TypeIndex2TypeKey(self->type_index);
292+
if (self == nullptr) {
293+
return "None";
294+
}
295+
if (MLCTypeInfo *type_info = TypeIndex2TypeInfo(self->type_index)) {
296+
return type_info->type_key;
297+
}
298+
return "(undefined)";
317299
}
318300

319301
MLC_INLINE MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key,
@@ -331,6 +313,77 @@ MLC_INLINE bool IsTypeIndexPOD(int32_t type_index) {
331313
return type_index < static_cast<int32_t>(MLCTypeIndex::kMLCStaticObjectBegin);
332314
}
333315

316+
template <typename _T> struct Type2Str {
317+
using T = RemoveCR<_T>;
318+
static std::string Run() {
319+
if constexpr (std::is_same_v<T, Any>) {
320+
return "Any";
321+
} else if constexpr (std::is_same_v<T, AnyView>) {
322+
return "AnyView";
323+
} else if constexpr (std::is_same_v<T, void>) {
324+
return "void";
325+
} else if constexpr (std::is_same_v<T, Object>) {
326+
return "object.Object";
327+
} else if constexpr (std::is_same_v<T, ObjectRef>) {
328+
return "object.ObjectRef";
329+
} else if constexpr (IsPOD<T>) {
330+
return PODTraits<T>::Type2Str();
331+
} else if constexpr (IsRawObjPtr<T>) {
332+
using U = std::remove_pointer_t<T>;
333+
return Type2Str<U>::Run() + " *";
334+
} else if constexpr (std::is_base_of_v<UListObj, T>) {
335+
using E = typename T::TElem;
336+
return "object.ListObj[" + Type2Str<E>::Run() + "]";
337+
} else if constexpr (std::is_base_of_v<UDictObj, T>) {
338+
using K = typename T::TKey;
339+
using V = typename T::TValue;
340+
return "object.DictObj[" + Type2Str<K>::Run() + ", " + Type2Str<V>::Run() + "]";
341+
} else if constexpr (std::is_base_of_v<UList, T>) {
342+
using E = typename T::TElem;
343+
return "list[" + Type2Str<E>::Run() + "]";
344+
} else if constexpr (std::is_base_of_v<UDict, T>) {
345+
using K = typename T::TKey;
346+
using V = typename T::TValue;
347+
return "dict[" + Type2Str<K>::Run() + ", " + Type2Str<V>::Run() + "]";
348+
} else if constexpr (IsRef<T>) {
349+
return "Ref<" + Type2Str<typename T::TObj>::Run() + ">";
350+
} else if constexpr (IsObjRef<T>) {
351+
return std::string(T::TObj::_type_key);
352+
} else if constexpr (IsObj<T>) {
353+
return std::string(T::_type_key) + "Obj";
354+
} else {
355+
static_assert(std::is_void_v<T>, "Unsupported type");
356+
}
357+
}
358+
359+
static void CollectTypeInfo(std::vector<MLCTypeInfo *> *info) {
360+
if constexpr (std::is_same_v<T, Any> || std::is_same_v<T, AnyView>) {
361+
info->push_back(TypeIndex2TypeInfo(static_cast<int32_t>(MLCTypeIndex::kMLCNone)));
362+
} else if constexpr (std::is_same_v<T, void>) {
363+
MLC_THROW(TypeError) << "`void` is not allowed in type annotation";
364+
} else if constexpr (std::is_same_v<T, char *>) {
365+
info->push_back(TypeIndex2TypeInfo(PODTraits<_T>::default_type_index));
366+
} else if constexpr (IsPOD<T>) {
367+
info->push_back(TypeIndex2TypeInfo(PODTraits<T>::default_type_index));
368+
} else if constexpr (IsRawObjPtr<T>) {
369+
using U = std::remove_pointer_t<T>;
370+
Type2Str<Ref<U>>::CollectTypeInfo(info);
371+
} else if constexpr (std::is_base_of_v<UList, T>) {
372+
info->push_back(TypeIndex2TypeInfo(static_cast<int32_t>(MLCTypeIndex::kMLCList)));
373+
Type2Str<typename T::TElem>::CollectTypeInfo(info);
374+
} else if constexpr (std::is_base_of_v<UDict, T>) {
375+
info->push_back(TypeIndex2TypeInfo(static_cast<int32_t>(MLCTypeIndex::kMLCDict)));
376+
Type2Str<typename T::TKey>::CollectTypeInfo(info);
377+
Type2Str<typename T::TValue>::CollectTypeInfo(info);
378+
} else if constexpr (IsRef<T> || IsObjRef<T>) {
379+
using U = typename T::TObj;
380+
info->push_back(TypeIndex2TypeInfo(U::_type_index));
381+
} else {
382+
static_assert(std::is_void_v<T>, "Unsupported type");
383+
}
384+
}
385+
};
386+
334387
MLC_INLINE void IncRef(MLCObject *obj) {
335388
if (obj != nullptr) {
336389
#ifdef _MSC_VER

include/mlc/base/traits_device.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ DLDevice String2DLDevice(const std::string &source);
1212
inline bool DeviceEqual(DLDevice a, DLDevice b) { return a.device_type == b.device_type && a.device_id == b.device_id; }
1313

1414
template <> struct PODTraits<DLDevice> {
15+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCDevice);
16+
1517
MLC_INLINE static void TypeCopyToAny(DLDevice src, MLCAny *ret) {
1618
ret->type_index = static_cast<int32_t>(MLCTypeIndex::kMLCDevice);
1719
ret->v_device = src;

include/mlc/base/traits_dtype.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ inline bool DataTypeEqual(DLDataType a, DLDataType b) {
1414
}
1515

1616
template <> struct PODTraits<DLDataType> {
17+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCDataType);
18+
1719
MLC_INLINE static void TypeCopyToAny(DLDataType src, MLCAny *ret) {
1820
ret->type_index = static_cast<int32_t>(MLCTypeIndex::kMLCDataType);
1921
ret->v_dtype = src;

include/mlc/base/traits_scalar.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace base {
1010
/********** PODTraits: Integer *********/
1111

1212
template <typename Int> struct PODTraits<Int, std::enable_if_t<std::is_integral_v<Int>>> {
13+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCInt);
14+
1315
MLC_INLINE static void TypeCopyToAny(Int src, MLCAny *ret) {
1416
ret->type_index = static_cast<int32_t>(MLCTypeIndex::kMLCInt);
1517
ret->v_int64 = static_cast<int64_t>(src);
@@ -31,6 +33,8 @@ template <typename Int> struct PODTraits<Int, std::enable_if_t<std::is_integral_
3133
/********** PODTraits: Float *********/
3234

3335
template <typename Float> struct PODTraits<Float, std::enable_if_t<std::is_floating_point_v<Float>>> {
36+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCFloat);
37+
3438
MLC_INLINE static void TypeCopyToAny(Float src, MLCAny *ret) {
3539
ret->type_index = static_cast<int32_t>(MLCTypeIndex::kMLCFloat);
3640
ret->v_float64 = src;
@@ -54,6 +58,7 @@ template <typename Float> struct PODTraits<Float, std::enable_if_t<std::is_float
5458
/********** PODTraits: Opaque Pointer *********/
5559

5660
template <> struct PODTraits<void *> {
61+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCPtr);
5762
using Ptr = void *;
5863

5964
MLC_INLINE static void TypeCopyToAny(Ptr src, MLCAny *ret) {
@@ -86,6 +91,7 @@ template <> struct PODTraits<void *> {
8691

8792
template <> struct PODTraits<std::nullptr_t> : public PODTraits<void *> {
8893
MLC_INLINE static const char *Type2Str() { return "None"; }
94+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCNone);
8995
};
9096
} // namespace base
9197
} // namespace mlc

include/mlc/base/traits_str.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace mlc {
77
namespace base {
88

99
template <> struct PODTraits<const char *> {
10+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCRawStr);
1011
static void TypeCopyToAny(const char *src, MLCAny *ret) {
1112
ret->type_index = static_cast<int32_t>(MLCTypeIndex::kMLCRawStr);
1213
ret->v_str = src;
@@ -25,7 +26,16 @@ template <> struct PODTraits<const char *> {
2526
static std::string __str__(const char *src) { return '"' + std::string(src) + '"'; }
2627
};
2728

29+
template <> struct PODTraits<char *> {
30+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCRawStr);
31+
static void TypeCopyToAny(char *src, MLCAny *ret) { return PODTraits<const char *>::TypeCopyToAny(src, ret); }
32+
static char *AnyCopyToType(const MLCAny *v) { return const_cast<char *>(PODTraits<const char *>::AnyCopyToType(v)); }
33+
static const char *Type2Str() { return "const char *"; }
34+
static std::string __str__(const char *src) { return '"' + std::string(src) + '"'; }
35+
};
36+
2837
template <> struct PODTraits<std::string> {
38+
static constexpr int32_t default_type_index = static_cast<int32_t>(MLCTypeIndex::kMLCRawStr);
2939
static void TypeCopyToAny(const std::string &src, MLCAny *ret) {
3040
return PODTraits<const char *>::TypeCopyToAny(src.data(), ret);
3141
}

include/mlc/core/func_details.h

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -264,29 +264,25 @@ inline Ref<FuncObj> FuncObj::FromForeign(void *self, MLCDeleterType deleter, MLC
264264
namespace mlc {
265265
namespace base {
266266

267-
template <typename FieldType> struct ReflectGetter {
268-
static int32_t Run(void *addr, MLCAny *ret) {
267+
template <typename FieldType> struct ReflectGetterSetter {
268+
static int32_t Getter(void *addr, MLCAny *ret) {
269269
MLC_SAFE_CALL_BEGIN();
270270
*static_cast<Any *>(ret) = *static_cast<FieldType *>(addr);
271271
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
272272
}
273-
};
274-
template <typename FieldType> struct ReflectSetter {
275-
static int32_t Run(void *addr, MLCAny *src) {
273+
static int32_t Setter(void *addr, MLCAny *src) {
276274
MLC_SAFE_CALL_BEGIN();
277275
*static_cast<FieldType *>(addr) = (static_cast<Any *>(src))->operator FieldType();
278276
MLC_SAFE_CALL_END(static_cast<Any *>(src));
279277
}
280278
};
281-
template <> struct ReflectGetter<char *> {
282-
static int32_t Run(void *addr, MLCAny *ret) {
279+
template <> struct ReflectGetterSetter<char *> {
280+
static int32_t Getter(void *addr, MLCAny *ret) {
283281
MLC_SAFE_CALL_BEGIN();
284282
*static_cast<Any *>(ret) = static_cast<const char *>(*static_cast<char **>(addr));
285283
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
286284
}
287-
};
288-
template <> struct ReflectSetter<char *> {
289-
static int32_t Run(void *addr, MLCAny *src) {
285+
static int32_t Setter(void *addr, MLCAny *src) {
290286
MLC_SAFE_CALL_BEGIN();
291287
*static_cast<char **>(addr) = const_cast<char *>((static_cast<Any *>(src))->operator const char *());
292288
MLC_SAFE_CALL_END(static_cast<Any *>(src));
@@ -295,18 +291,17 @@ template <> struct ReflectSetter<char *> {
295291

296292
template <typename Super, typename FieldType>
297293
inline ReflectionHelper &ReflectionHelper::FieldReadOnly(const char *name, FieldType Super::*field) {
298-
using PGet = ReflectGetter<FieldType>;
294+
using P = ReflectGetterSetter<FieldType>;
299295
const int64_t field_offset = static_cast<int64_t>(ReflectOffset(field));
300-
this->fields.emplace_back(MLCTypeField{name, field_offset, &PGet::Run, nullptr});
296+
this->fields.emplace_back(MLCTypeField{name, field_offset, &P::Getter, nullptr});
301297
return *this;
302298
}
303299

304300
template <typename Super, typename FieldType>
305301
inline ReflectionHelper &ReflectionHelper::Field(const char *name, FieldType Super::*field) {
306-
using PGet = ReflectGetter<FieldType>;
307-
using PSet = ReflectSetter<FieldType>;
302+
using P = ReflectGetterSetter<FieldType>;
308303
const int64_t field_offset = static_cast<int64_t>(ReflectOffset(field));
309-
this->fields.emplace_back(MLCTypeField{name, field_offset, &PGet::Run, &PSet::Run});
304+
this->fields.emplace_back(MLCTypeField{name, field_offset, &P::Getter, &P::Setter});
310305
return *this;
311306
}
312307

0 commit comments

Comments
 (0)