-
Notifications
You must be signed in to change notification settings - Fork 49
feat: Introduce Experimental tvm_ffi.dataclasses.c_class
#8
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
cd052b2 to
c535491
Compare
c535491 to
e67b5eb
Compare
e67b5eb to
363e890
Compare
858ec21 to
9fd39a4
Compare
tvm_ffi.experimental.c_classtvm_ffi.dataclasses.c_class
9fd39a4 to
d268c94
Compare
tvm_ffi.dataclasses.c_classtvm_ffi.dataclasses.c_class
f8dfc67 to
5e336b0
Compare
3ff1f33 to
2e4f901
Compare
Previously, `TypeInfo` stays strictly in C and is glued to Python via Cython. This PR makes it Cython side registry to store Python objects, i.e. `TypeInfo`, which makes it possible to lookup type info in pure Python. This PR is split from #8.
78f3bd2 to
1d66b52
Compare
1c4f153 to
321a32f
Compare
|
@tqchen This PR is finally ready for review! |
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 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.
- Adds
c_classdecorator for creating dataclass-style FFI bindings - Implements field introspection and default value handling
- Provides inheritance support for FFI classes
Reviewed Changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| python/tvm_ffi/dataclasses/init.py | Package initialization exporting the new dataclass functionality |
| python/tvm_ffi/dataclasses/c_class.py | Core c_class decorator implementation with field reflection |
| python/tvm_ffi/dataclasses/field.py | Field specification utilities similar to dataclasses.field |
| python/tvm_ffi/dataclasses/_utils.py | Utility functions for class construction and method generation |
| python/tvm_ffi/core.pyi | Type stub updates for the new dataclass_field attribute |
| python/tvm_ffi/cython/type_info.pxi | Addition of dataclass_field to TypeField class |
| python/tvm_ffi/testing.py | Example usage with test classes demonstrating inheritance |
| src/ffi/extra/testing.cc | C++ test classes for validating the FFI dataclass functionality |
| tests/python/test_dataclasses_c_class.py | Comprehensive tests for the new c_class decorator |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
321a32f to
a6090ba
Compare
1b6d278 to
9402e48
Compare
|
|
||
| @dataclass(kw_only=True) | ||
| class Field: | ||
| """(Experimental) Descriptor placeholder returned by :func:`tvm_ffi.dataclasses.field`. |
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.
I think we only need to put experimental at the docstring for dataclasses __init__.py
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.
It doesn’t hurt :))
9402e48 to
463d9c9
Compare
68590b0 to
b2c1bc6
Compare
b2c1bc6 to
46466b5
Compare
|
@tqchen Rebased against the latest |
This PR addresses a memory corruption issue in Cython. The fix involves
ensuring that the `ByteArrayArg` object, which holds the type key, is
properly destructed after being passed to the `TVMFFITypeKeyToIndex`
function. This prevents a potential read-after-free scenario, as
reported by ASan.
## ASan Report
```
READ of size 9 at 0x604000420a30 thread T0
...
#5 0x7fdb57299506 in __pyx_f_4core__type_info_create_from_type_key /home/dolores/Projects/tvm-ffi/build/core.cpp:17732
...
0x604000420a30 is located 32 bytes inside of 42-byte region [0x604000420a10,0x604000420a3a)
freed by thread T0 here:
...
#4 0x7fdb572994e2 in __pyx_f_4core__type_info_create_from_type_key /home/dolores/Projects/tvm-ffi/build/core.cpp:17731
...
previously allocated by thread T0 here:
...
#8 0x7fdb57299366 in __pyx_f_4core__type_info_create_from_type_key /home/dolores/Projects/tvm-ffi/build/core.cpp:17718
```
<img width="1444" height="904" alt="image"
src="https://github.com/user-attachments/assets/7a80d33d-dedf-41ca-ac77-108e63b8e57b"
/>
## Recommended ASan Options
One will need to preload `libasan` to properly work with CPython, and
`libstdc++` to properly intercept `__cxa_throw`. The path to those two
files can be found using:
```
ASAN="$(gcc -print-file-name=libasan.so)"
STDCXX="$(g++ -print-file-name=libstdc++.so.6)"
LD_PRELOAD="$ASAN $STDCXX"
```
Additionally, it might be helpful to tweak
```
PYTHONMALLOC=malloc
```
and run with ASan options
```
ASAN_OPTIONS="detect_leaks=0:abort_on_error=1:symbolize=1:fast_unwind_on_malloc=0"
```
Notably, turning on `detect_leaks=1` will lead to bunch of irrelevant
noisy reports. Better turning it off.
Depends on #32.
This PR introduces an experimental
@c_classdecorator 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<>:Mirror the same structure in Python using dataclass-style annotations:
Future work
Supporting as many dataclass features as possible, including:
repr,init,order, etc.