Skip to content

Conversation

@junrushao
Copy link
Member

@junrushao junrushao commented Sep 19, 2025

This PR introduces tvm_ffi::reflection::init<ObjectType, Args...>, which can be used to simplify registration of __init__ method.

@junrushao junrushao marked this pull request as ready for review September 19, 2025 21:57
@junrushao junrushao requested a review from Copilot September 19, 2025 21:57
Copy link

Copilot AI left a 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_class decorator 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.

@junrushao junrushao changed the title feat: Introduce InitOf to Simplify __init__ method registration feat: Introduce tvm_ffi::reflection::init<> Sep 20, 2025
@junrushao junrushao requested a review from tqchen September 20, 2025 07:10
Copy link
Member

@tqchen tqchen left a 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

Copy link
Member

@tqchen tqchen left a 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

@junrushao junrushao force-pushed the 2025-09-19/init-of branch 2 times, most recently from 0c6c2b2 to 6e5ba5b Compare September 20, 2025 20:57
@junrushao
Copy link
Member Author

@tqchen To summarize the latest change:

  • A unittest is added
  • refl::init<> now supports both Object and ObjectRef

Please re-review 🙏

@junrushao junrushao force-pushed the 2025-09-19/init-of branch 2 times, most recently from 2f30e41 to 5af56e0 Compare September 21, 2025 05:59
@junrushao
Copy link
Member Author

Please take another look @tqchen!

@tqchen tqchen merged commit c01dadf into apache:main Sep 21, 2025
6 checks passed
tqchen pushed a commit that referenced this pull request Sep 22, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants