-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Introduce tvm_ffi::reflection::init<>
#32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces the InitOf template function to simplify C++ constructor registration and adds experimental dataclass-style support for FFI classes.
- Adds
InitOf<T, Args...>helper to C++ for streamlined constructor registration - Introduces experimental
@c_classdecorator enabling dataclass syntax for Python FFI proxies - Creates test infrastructure with inheritance hierarchy for validating the new features
Reviewed Changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| include/tvm/ffi/reflection/registry.h | Adds InitOf template helper for constructor registration |
| src/ffi/extra/testing.cc | Creates C++ test classes using InitOf for constructor registration |
| python/tvm_ffi/dataclasses/ | New module providing @c_class decorator and field helper for dataclass syntax |
| python/tvm_ffi/testing.py | Python proxy classes demonstrating dataclass-style FFI usage |
| tests/python/test_dataclasses_c_class.py | Test cases validating the dataclass FFI functionality |
| python/tvm_ffi/cython/type_info.pxi | Adds dataclass_field attribute to TypeField |
| python/tvm_ffi/core.pyi | Type stub updates for new dataclass_field attribute |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
0f262c1 to
f4df760
Compare
tvm_ffi::reflection::init<>
f4df760 to
09a3c8a
Compare
tqchen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks great, let us add a testcase to tests/cpp/testing_object.h
tqchen
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm modulo on minor improvement
0c6c2b2 to
6e5ba5b
Compare
|
@tqchen To summarize the latest change:
Please re-review 🙏 |
6e5ba5b to
c3a8c34
Compare
2f30e41 to
5af56e0
Compare
5af56e0 to
8212b88
Compare
|
Please take another look @tqchen! |
Depends on #32. This PR introduces an experimental `@c_class` decorator that enables Python dataclass-like syntax for exposing C++ objects through TVM FFI. The decorator automatically handles field reflection, inheritance, and constructor generation for FFI-backed classes. ## Example On C++ side, register types using `tvm::ffi::reflection::ObjectDef<>`: ```C++ TVM_FFI_STATIC_INIT_BLOCK() { namespace refl = tvm::ffi::reflection; refl::ObjectDef<MyClass>() .def_static("__init__", [](int64_t v_i64, int32_t v_i32, double v_f64, float v_f32) -> Any { return ObjectRef(ffi::make_object<MyClass>(v_i64, v_i32, v_f64, v_f32)); }) .def_rw("v_i64", &MyClass::v_i64) .def_rw("v_i32", &MyClass::v_i32) .def_rw("v_f64", &MyClass::v_f64) .def_rw("v_f32", &MyClass::v_f32); } ``` Mirror the same structure in Python using dataclass-style annotations: ```python from tvm_ffi.dataclasses import c_class, field @c_class("example.MyClass") class MyClass: v_i64: int v_i32: int v_f64: float = field(default=0.0) v_f32: float = field(default_factory=lambda: 1.0) obj = MyClass(v_i64=4, v_i32=8) obj.v_f64 = 3.14 # transparently forwards to the underlying C++ object ``` ## Future work Supporting as many dataclass features as possible, including: `repr`, `init`, `order`, etc.
This PR introduces
tvm_ffi::reflection::init<ObjectType, Args...>, which can be used to simplify registration of__init__method.