Skip to content

Commit

Permalink
Sync with main
Browse files Browse the repository at this point in the history
  • Loading branch information
potatomashed committed Aug 31, 2024
1 parent a1bb7a5 commit a8aa03c
Show file tree
Hide file tree
Showing 60 changed files with 1,197 additions and 1,125 deletions.
47 changes: 30 additions & 17 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,19 @@ on:

env:
CIBW_BUILD_VERBOSITY: 3
CIBW_TEST_REQUIRES: "pytest"
CIBW_TEST_COMMAND: "pytest -svv {project}/tests/python/"
CIBW_TEST_COMMAND: "python -c \"import mlc\""
CIBW_SKIP: "cp313-win_amd64" # Python 3.13 is not quite ready yet
MLC_CIBW_VERSION: "2.20.0"
MLC_PYTHON_VERSION: "3.9"
MLC_CIBW_WIN_BUILD: "cp3*-win_amd64"
MLC_CIBW_MAC_BUILD: "cp3*-macosx_arm64"
MLC_CIBW_MAC_X86_BUILD: "cp3*-macosx_x86_64"
MLC_CIBW_LINUX_BUILD: "cp3*-manylinux_x86_64"

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: ${{ env.MLC_PYTHON_VERSION }}
- uses: pre-commit/[email protected]
windows:
name: Windows
runs-on: windows-latest
needs: pre-commit
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -36,7 +27,7 @@ jobs:
with:
python-version: ${{ env.MLC_PYTHON_VERSION }}
- name: Install cibuildwheel
run: python -m pip install twine cibuildwheel=="${{ env.MLC_CIBW_VERSION }}"
run: python -m pip install cibuildwheel=="${{ env.MLC_CIBW_VERSION }}"
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
env:
Expand All @@ -51,7 +42,6 @@ jobs:
macos:
name: MacOS
runs-on: macos-latest
needs: pre-commit
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -60,7 +50,7 @@ jobs:
with:
python-version: ${{ env.MLC_PYTHON_VERSION }}
- name: Install cibuildwheel
run: python -m pip install twine cibuildwheel==${{ env.MLC_CIBW_VERSION }}
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
env:
Expand All @@ -72,10 +62,32 @@ jobs:
with:
name: wheels-macos
path: ./wheelhouse/*.whl
macos-x86:
name: MacOS-x86
runs-on: macos-13
steps:
- uses: actions/checkout@v4
with:
submodules: "recursive"
- uses: actions/setup-python@v5
with:
python-version: ${{ env.MLC_PYTHON_VERSION }}
- name: Install cibuildwheel
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
env:
CIBW_BUILD: ${{ env.MLC_CIBW_MAC_X86_BUILD }}
- name: Show package contents
run: python scripts/show_wheel_content.py wheelhouse
- name: Upload wheels
uses: actions/upload-artifact@v3
with:
name: wheels-macos-x86
path: ./wheelhouse/*.whl
linux:
name: Linux
runs-on: ubuntu-latest
needs: pre-commit
steps:
- uses: actions/checkout@v4
with:
Expand All @@ -84,7 +96,7 @@ jobs:
with:
python-version: ${{ env.MLC_PYTHON_VERSION }}
- name: Install cibuildwheel
run: python -m pip install twine cibuildwheel==${{ env.MLC_CIBW_VERSION }}
run: python -m pip install cibuildwheel==${{ env.MLC_CIBW_VERSION }}
- name: Build wheels
run: python -m cibuildwheel --output-dir wheelhouse
env:
Expand All @@ -99,7 +111,7 @@ jobs:
publish:
name: Publish
runs-on: ubuntu-latest
needs: [windows, macos, linux]
needs: [windows, macos, linux, macos-x86]
environment:
name: pypi
url: https://pypi.org/p/mlc-python
Expand All @@ -114,6 +126,7 @@ jobs:
- name: Prepare distribution files
run: |
mkdir -p dist
mv wheelhouse/wheels-macos-x86/*.whl dist/
mv wheelhouse/wheels-macos/*.whl dist/
mv wheelhouse/wheels-linux/*.whl dist/
mv wheelhouse/wheels-windows/*.whl dist/
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.15)

project(
mlc
VERSION 1.0
VERSION 0.0.2
DESCRIPTION "MLC's FFI system"
LANGUAGES C CXX
)
Expand Down
29 changes: 13 additions & 16 deletions cpp/c_api.cc
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
#include "./registry.h"
#include "mlc/ffi/ext/func.h"
#include <mlc/ffi/ffi.hpp>
#if defined(__APPLE__)
#include <iostream>
#endif

namespace mlc {
namespace ffi {
namespace registry {
TypeTable *TypeTable::Global() {
static TypeTable *instance = TypeTable::New();
return instance;
}
} // namespace registry
} // namespace ffi
} // namespace mlc

using ::mlc::ffi::Any;
using ::mlc::ffi::AnyView;
using ::mlc::ffi::ErrorObj;
using ::mlc::ffi::FuncObj;
using ::mlc::ffi::Ref;
using ::mlc::ffi::registry::TypeTable;
using ::mlc::Any;
using ::mlc::AnyView;
using ::mlc::ErrorObj;
using ::mlc::FuncObj;
using ::mlc::Ref;
using ::mlc::registry::TypeTable;

namespace {
thread_local Any last_error;
Expand All @@ -35,9 +31,10 @@ MLC_API MLCAny MLCGetLastError() {
}

MLC_API int32_t MLCTypeRegister(MLCTypeTableHandle _self, int32_t parent_type_index, const char *type_key,
int32_t type_index, MLCTypeInfo **out_type_info) {
int32_t type_index, MLCAttrGetterSetter getter, MLCAttrGetterSetter setter,
MLCTypeInfo **out_type_info) {
MLC_SAFE_CALL_BEGIN();
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key);
*out_type_info = TypeTable::Get(_self)->TypeRegister(parent_type_index, type_index, type_key, getter, setter);
MLC_SAFE_CALL_END(&last_error);
}

Expand Down Expand Up @@ -86,16 +83,16 @@ MLC_API int32_t MLCDynTypeTypeTableDestroy(MLCTypeTableHandle handle) {

MLC_API int32_t MLCAnyIncRef(MLCAny *any) {
MLC_SAFE_CALL_BEGIN();
if (!::mlc::ffi::details::IsTypeIndexPOD(any->type_index)) {
::mlc::ffi::details::IncRef(any->v_obj);
if (!::mlc::base::IsTypeIndexPOD(any->type_index)) {
::mlc::base::IncRef(any->v_obj);
}
MLC_SAFE_CALL_END(&last_error);
}

MLC_API int32_t MLCAnyDecRef(MLCAny *any) {
MLC_SAFE_CALL_BEGIN();
if (!::mlc::ffi::details::IsTypeIndexPOD(any->type_index)) {
::mlc::ffi::details::DecRef(any->v_obj);
if (!::mlc::base::IsTypeIndexPOD(any->type_index)) {
::mlc::base::DecRef(any->v_obj);
}
MLC_SAFE_CALL_END(&last_error);
}
Expand Down
6 changes: 3 additions & 3 deletions cpp/c_api_tests.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <mlc/ffi/ffi.hpp>
#include <mlc/all.h>

namespace mlc {
namespace ffi {
namespace {

/**************** FFI ****************/

Expand Down Expand Up @@ -95,5 +95,5 @@ MLC_REGISTER_FUNC("mlc.testing.nested_type_checking_list").set_body([](Str name)
MLC_UNREACHABLE();
});

} // namespace ffi
} // namespace
} // namespace mlc
5 changes: 2 additions & 3 deletions cpp/dso_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
#else
#include <dlfcn.h>
#endif
#include <mlc/ffi/core/core.h>
#include <mlc/all.h>
#include <string>

namespace mlc {
namespace ffi {

struct DSOLibrary {
~DSOLibrary() { Unload(); }
Expand Down Expand Up @@ -56,7 +56,6 @@ struct DSOLibrary {
#endif
};

} // namespace ffi
} // namespace mlc

#endif // MLC_DSO_LIBRARY_H_
73 changes: 50 additions & 23 deletions cpp/registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@
#include <cstring>
#include <iostream>
#include <memory>
#include <mlc/ffi/ffi.hpp>
#include <string>
#include <unordered_map>
#include <vector>

namespace mlc {
namespace ffi {
namespace registry {

struct TypeTable;
Expand All @@ -31,6 +29,32 @@ struct TypeInfoWrapper {
~TypeInfoWrapper() { this->Reset(); }
};

template <typename T> struct PODGetterSetter {
static int32_t Getter(void *addr, MLCAny *ret) {
MLC_SAFE_CALL_BEGIN();
*static_cast<Any *>(ret) = *static_cast<T *>(addr);
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
}
static int32_t Setter(void *addr, MLCAny *src) {
MLC_SAFE_CALL_BEGIN();
*static_cast<T *>(addr) = (static_cast<Any *>(src))->operator T();
MLC_SAFE_CALL_END(static_cast<Any *>(src));
}
};

template <> struct PODGetterSetter<std::nullptr_t> {
static int32_t Getter(void *, MLCAny *ret) {
MLC_SAFE_CALL_BEGIN();
*static_cast<Any *>(ret) = nullptr;
MLC_SAFE_CALL_END(static_cast<Any *>(ret));
}
static int32_t Setter(void *addr, MLCAny *src) {
MLC_SAFE_CALL_BEGIN();
*static_cast<void **>(addr) = nullptr;
MLC_SAFE_CALL_END(static_cast<Any *>(src));
}
};

struct TypeTable {
using ObjPtr = std::unique_ptr<MLCObject, void (*)(MLCObject *)>;

Expand All @@ -39,15 +63,15 @@ struct TypeTable {
std::unordered_map<std::string, MLCTypeInfo *> type_key_to_info;
std::unordered_map<std::string, std::unordered_map<int32_t, FuncObj *>> vtable;
std::unordered_map<std::string, FuncObj *> global_funcs;
std::unordered_map<const void *, details::PODArray> pool_pod_array;
std::unordered_map<const void *, ::mlc::base::PODArray> pool_pod_array;
std::unordered_map<const void *, ObjPtr> pool_obj_ptr;
std::unordered_map<std::string, std::unique_ptr<DSOLibrary>> dso_library;

template <typename PODType> inline PODType *NewArray(int64_t size) {
if (size == 0) {
return nullptr;
}
details::PODArray owned(static_cast<void *>(std::malloc(size * sizeof(PODType))), std::free);
::mlc::base::PODArray owned(static_cast<void *>(std::malloc(size * sizeof(PODType))), std::free);
PODType *ptr = reinterpret_cast<PODType *>(owned.get());
auto [it, success] = this->pool_pod_array.emplace(ptr, std::move(owned));
if (!success) {
Expand Down Expand Up @@ -80,8 +104,8 @@ struct TypeTable {
std::abort();
}
MLCObject *source_casted = reinterpret_cast<MLCObject *>(source);
::mlc::ffi::details::IncRef(source_casted);
it->second = ObjPtr(source_casted, ::mlc::ffi::details::DecRef);
::mlc::base::IncRef(source_casted);
it->second = ObjPtr(source_casted, ::mlc::base::DecRef);
}
}

Expand Down Expand Up @@ -131,7 +155,8 @@ struct TypeTable {
return (self == nullptr) ? TypeTable::Global() : static_cast<TypeTable *>(self);
}

MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key) {
MLCTypeInfo *TypeRegister(int32_t parent_type_index, int32_t type_index, const char *type_key,
MLCAttrGetterSetter getter, MLCAttrGetterSetter setter) {
// Step 1.Check if the type is already registered
if (auto it = this->type_key_to_info.find(type_key); it != this->type_key_to_info.end()) {
MLCTypeInfo *ret = it->second;
Expand All @@ -156,6 +181,8 @@ struct TypeTable {
info->type_index = type_index;
info->type_key = this->NewArray(type_key);
info->type_depth = (parent == nullptr) ? 0 : (parent->type_depth + 1);
info->getter = getter;
info->setter = setter;
info->fields = nullptr;
info->methods = nullptr;
info->type_ancestors = this->NewArray<int32_t>(info->type_depth);
Expand Down Expand Up @@ -209,24 +236,24 @@ struct TypeTable {
};

struct _POD_REG {
inline static const int32_t _none = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCNone))
.Method("__str__", &PODTraits<std::nullptr_t>::__str__);
inline static const int32_t _int = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCInt))
.Method("__str__", &PODTraits<int64_t>::__str__);
inline static const int32_t _float = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCFloat))
.Method("__str__", &PODTraits<double>::__str__);
inline static const int32_t _ptr = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCPtr))
.Method("__str__", &PODTraits<void *>::__str__);
inline static const int32_t _none = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCNone))
.Method("__str__", &base::PODTraits<std::nullptr_t>::__str__);
inline static const int32_t _int = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCInt))
.Method("__str__", &base::PODTraits<int64_t>::__str__);
inline static const int32_t _float = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCFloat))
.Method("__str__", &base::PODTraits<double>::__str__);
inline static const int32_t _ptr = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCPtr))
.Method("__str__", &base::PODTraits<void *>::__str__);
inline static const int32_t _device =
details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDevice))
.Method("__str__", &PODTraits<DLDevice>::__str__)
base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDevice))
.Method("__str__", &base::PODTraits<DLDevice>::__str__)
.Method("__init__", [](AnyView device) { return device.operator DLDevice(); });
inline static const int32_t _dtype =
details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDataType))
.Method("__str__", &PODTraits<DLDataType>::__str__)
base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCDataType))
.Method("__str__", &base::PODTraits<DLDataType>::__str__)
.Method("__init__", [](AnyView dtype) { return dtype.operator DLDataType(); });
inline static const int32_t _str = details::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCRawStr))
.Method("__str__", &PODTraits<const char *>::__str__);
inline static const int32_t _str = base::ReflectionHelper(static_cast<int32_t>(MLCTypeIndex::kMLCRawStr))
.Method("__str__", &base::PODTraits<const char *>::__str__);
};

inline TypeTable *TypeTable::New() {
Expand All @@ -235,7 +262,8 @@ inline TypeTable *TypeTable::New() {
self->type_key_to_info.reserve(1024);
self->num_types = static_cast<int32_t>(MLCTypeIndex::kMLCDynObjectBegin);
#define MLC_TYPE_TABLE_INIT_TYPE(TypeIndex, UnderlyingType, Self) \
Self->TypeRegister(-1, static_cast<int32_t>(TypeIndex), PODTraits<UnderlyingType>::Type2Str());
Self->TypeRegister(-1, static_cast<int32_t>(TypeIndex), ::mlc::base::PODTraits<UnderlyingType>::Type2Str(), \
PODGetterSetter<UnderlyingType>::Getter, PODGetterSetter<UnderlyingType>::Setter);

MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCNone, std::nullptr_t, self);
MLC_TYPE_TABLE_INIT_TYPE(MLCTypeIndex::kMLCInt, int64_t, self);
Expand Down Expand Up @@ -315,7 +343,6 @@ inline void TypeInfoWrapper::SetMethods(int64_t new_num_methods, MLCTypeMethod *
}

} // namespace registry
} // namespace ffi
} // namespace mlc

#endif // MLC_REGISTRY_H_
2 changes: 1 addition & 1 deletion cpp/traceback.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <backtrace.h>
#include <cxxabi.h>
#include <iostream>
#include <mlc/ffi/c_api.h>
#include <mlc/c_api.h>

namespace mlc {
namespace ffi {
Expand Down
Loading

0 comments on commit a8aa03c

Please sign in to comment.