Skip to content

staged commits#477

Draft
junrushao wants to merge 11 commits intoapache:mainfrom
junrushao:2026-02-26/split-commit
Draft

staged commits#477
junrushao wants to merge 11 commits intoapache:mainfrom
junrushao:2026-02-26/split-commit

Conversation

junrushao and others added 11 commits February 26, 2026 18:27
Remove the Python-side field descriptor system (_utils.py, field.py,
complex c_class.py) that is being replaced by C++ reflection-driven
metadata. The c_class decorator now delegates directly to
register_object.

- Delete python/tvm_ffi/dataclasses/_utils.py
- Delete python/tvm_ffi/dataclasses/field.py
- Simplify c_class.py to a pass-through to register_object
- Simplify dataclasses/__init__.py to export only c_class
- Delete tests/python/test_dataclasses_c_class.py
- Update testing.py: add Object base, remove field() usage
- Fix _add_class_attrs to always override __c_ffi_init__
  (prevents inherited base-class init from masking derived-class init)
- Update test_repr.py: use positional args for derived constructors

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Split Cython Object into CObject (extension type) + Object (Python
class with metaclass that auto-adds __slots__=()). This prevents
per-instance __dict__ on all Object subclasses by default.

- object.pxi: CObject base, _ObjectSlotsMeta, Object(CObject),
  object_repr(), OpaquePyObject(CObject), simplified fallback cls
- function.pxi: all Object → CObject casts
- error.pxi: Error(CObject) + __slots__
- tensor.pxi: Tensor(CObject) + __slots__
- device.pxi: __slots__, kDLMAIA=17, kDLTrn=18
- dtype.pxi: __slots__
- base.pxi: __slots__ on ByteArrayArg
- string.pxi: Object → CObject
- type_info.pxi: Object → CObject in FieldGetter/FieldSetter
- core.pyi: export CObject, Object(CObject), object_repr
- container.py: remove redundant __repr__ (now on CObject)
- access_path.py: remove super().__init__() call
- module.py: __slots__ + _tvm_ffi_attr_cache pattern
- registry.py: add ffi.Shape to deepcopy-supported containers,
  use _ffi_api.DeepCopy directly, remove _get_deep_copy_func
- test_object.py: slots enforcement + direct-init guard tests

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…w C++ APIs

Merge deep_copy.cc and repr_print.cc into a unified dataclass.cc using shared
ObjectGraphDFS infrastructure. Add new C++ reflection capabilities:
- RecursiveEq/Hash/Compare for structural comparison and hashing
- MakeInit/RegisterAutoInit for auto-generated __ffi_init__ constructors
- GetKwargsObject for kwargs-based initialization
- New reflection traits: compare, hash, kw_only, init
- New ObjectDef destructor auto-init registration
- Rename test_repr.py → test_dataclass_repr.py

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Expose RecursiveEq/Lt/Le/Gt/Ge to Python tests. Add TestCompare,
TestCustomCompare, and TestEqWithoutHash test classes and comprehensive
test_dataclass_compare.py covering primitives, containers, reflected
objects, CompareOff flags, custom hooks, cycles, and ordering laws.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Expose RecursiveHash to Python tests. Add TestHash and TestCustomHash
test classes and comprehensive test_dataclass_hash.py covering
primitives, containers, reflected objects, HashOff flags, custom hooks,
cycle detection, consistency laws, and aliasing invariants.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Wire up C++ auto-generated __ffi_init__ to Python __init__ via reflection
metadata. TypeField now carries c_init, c_kw_only, c_has_default flags
from the C++ bitmask. _make_init builds a kwargs-aware __init__ with
proper inspect.Signature. _install_init dispatches between auto-init,
plain __ffi_init__, and a guard TypeError for types without constructors.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Rewrite the `@c_class` decorator to combine `register_object` with
`_install_dataclass_dunders`, which installs structural `__init__`,
`__repr__`, `__eq__`/`__ne__`, `__hash__`, and ordering operators from
C++ reflection metadata.

Migrate all test objects from `@register_object` to `@c_class`.
Add test_dataclass_c_class.py and rename test_copy.py to
test_dataclass_copy.py with additional cycle/Shape coverage.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Allow creating `tvm_ffi.Function` directly from a Python callable
or from an existing Function instance, with proper ref-counting
and error handling for non-callable and moved-from objects.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Update tvm-ffi-stubgen to emit `__init__` stubs from C++ reflection
metadata. For auto-init types, walks the parent chain to collect
init-eligible fields with proper kw_only/has_default handling. For
non-auto-init types with `__c_ffi_init__`, generates positional-only
`__init__` from the factory signature.

Regenerate inline stubs and remove now-unnecessary ty:ignore comments
from test files.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add tvm-ffi-stubgen(begin/end) marker blocks and TYPE_CHECKING stubs
  (__ffi_shallow_copy__, __c_ffi_init__) for ~13 test classes in testing.py
- Reorder classes (SchemaAllTypes, create_object, etc.) to match final state
- Use keyword args in test_dataclass_compare.py and test_dataclass_hash.py
  for _TestCxxClassDerived/_TestCxxClassDerivedDerived constructors

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
- Add docs/guides/dataclass_reflection.rst (new guide)
- Update docs/conf.py with intersphinx and autodoc settings
- Update docs/index.rst to include new guide
- Minor updates to object_and_class.rst and export_func_cls.rst

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a new reflection-driven dataclass system for TVM FFI objects. It centralizes deep copy, string representation, structural hashing, and comparison logic within a dedicated C++ module. The core enhancement lies in extending the FFI reflection mechanism with new field-level flags that dictate how fields behave in these structural operations and how constructors are automatically generated. On the Python side, the c_class decorator now leverages this enhanced reflection to automatically install appropriate dunder methods, simplifying the definition of FFI-backed Python classes and ensuring consistent structural behavior. This comprehensive overhaul aims to provide a more intuitive and robust experience for defining and interacting with complex FFI data structures.

Highlights

  • Consolidated Dataclass Operations: Deep copy, repr printing, structural hashing, and comparison functionalities are now unified under a new dataclass.cc module in C++.
  • Enhanced FFI Reflection Flags: New C++ field flags (CompareOff, HashOff, InitOff, KwOnly) provide fine-grained control over how fields participate in structural operations and constructor generation.
  • Automatic ffi_init Generation: The FFI system can now automatically generate __ffi_init__ constructors for reflected types, handling positional/keyword arguments, default values, and new field traits.
  • Simplified Python c_class Decorator: The Python c_class decorator is streamlined to automatically install structural dunder methods (__init__, __repr__, __eq__, __hash__, __lt__, etc.) based on the C++ reflection metadata.
  • Refactored Python Object Hierarchy: The Python FFI object model has been refactored with the introduction of CObject (Cython base) and Object (Python base with __slots__ enforcement) to improve internal management and Python-side behavior.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • CMakeLists.txt
    • Updated build configuration to include new dataclass module and remove old deep copy/repr print modules.
  • include/tvm/ffi/c_api.h
    • Added new field flag bitmasks for comparison, hashing, initialization control, and keyword-only arguments.
  • include/tvm/ffi/extra/dataclass.h
    • New header declaring deep copy, repr print, and structural comparison/hashing APIs.
  • include/tvm/ffi/reflection/init.h
    • New header defining auto-generated __ffi_init__ constructor logic.
  • include/tvm/ffi/reflection/overload.h
    • Modified ObjectDef to track explicit __ffi_init__ registration.
  • include/tvm/ffi/reflection/registry.h
    • Integrated new auto-init logic, added aliases for default traits, and introduced new field traits (compare, hash, kw_only).
  • pyproject.toml
    • Added D107 to pydocstyle ignore list.
  • python/tvm_ffi/_ffi_api.py
    • Added new FFI API functions for dataclass operations and auto-init.
  • python/tvm_ffi/access_path.py
    • Minor Python __init__ adjustment.
  • python/tvm_ffi/container.py
    • Removed Python-side __repr__ implementations, delegating to C++ ReprPrint.
  • python/tvm_ffi/core.pyi
    • Refactored Object hierarchy into CObject and Object, added KWARGS sentinel.
  • python/tvm_ffi/cython/base.pxi
    • Updated Cython FFI field flags and added __slots__ to ByteArrayArg.
  • python/tvm_ffi/cython/core.pyx
    • Added KWARGS global sentinel.
  • python/tvm_ffi/cython/device.pxi
    • Added kDLMAIA device type and __slots__ to Device.
  • python/tvm_ffi/cython/dtype.pxi
    • Added __slots__ to DataType.
  • python/tvm_ffi/cython/error.pxi
    • Refactored Error to inherit from CObject and added __slots__.
  • python/tvm_ffi/cython/function.pxi
    • Refactored Function to inherit from CObject and added Python __init__.
  • python/tvm_ffi/cython/object.pxi
    • Implemented CObject/Object hierarchy, _ObjectSlotsMeta, and updated TypeField with new C++ flags.
  • python/tvm_ffi/cython/string.pxi
    • Updated object type casts.
  • python/tvm_ffi/cython/tensor.pxi
    • Updated object type casts and added __slots__ to Tensor and DLTensorTestWrapper.
  • python/tvm_ffi/cython/type_info.pxi
    • Added new C++ field flags to TypeField.
  • python/tvm_ffi/dataclasses/init.py
    • Simplified exports, removing Field, field, KW_ONLY.
  • python/tvm_ffi/dataclasses/_utils.py
    • Removed, logic moved to registry.py.
  • python/tvm_ffi/dataclasses/c_class.py
    • Simplified decorator, delegating dunder method installation to registry.py.
  • python/tvm_ffi/dataclasses/field.py
    • Removed, functionality replaced by C++ reflection.
  • python/tvm_ffi/module.py
    • Added __slots__ and caching to Module.__getattr__.
  • python/tvm_ffi/registry.py
    • Added _make_init, _make_init_signature, _install_dataclass_dunders for Python-side dunder method generation.
  • python/tvm_ffi/structural.py
    • Updated StructuralKey type hints for auto-init.
  • python/tvm_ffi/stub/codegen.py
    • Updated stub generation to include auto-generated __init__.
  • python/tvm_ffi/stub/utils.py
    • Enhanced ObjectInfo to extract and generate __init__ stubs from reflection metadata.
  • python/tvm_ffi/testing/init.py
    • Added new test classes for dataclass features.
  • python/tvm_ffi/testing/testing.py
    • Updated test classes with @c_class decorator and new auto-init/comparison/hashing test cases.
  • src/ffi/container.cc
    • Removed GetMissingObject and ffi.GetInvalidObject registration.
  • src/ffi/extra/dataclass.cc
    • New C++ implementation for deep copy, repr print, and structural comparison/hashing.
  • src/ffi/extra/deep_copy.cc
    • Removed.
  • src/ffi/extra/reflection_extra.cc
    • Updated AccessStepObj and AccessPathObj to use refl::init(false).
  • src/ffi/extra/repr_print.cc
    • Removed.
  • src/ffi/object.cc
    • Added GetMissingObject and GetKwargsObject and their registrations.
  • src/ffi/object_internal.h
    • Renamed from include/tvm/ffi/extra/deep_copy.h and updated content.
  • src/ffi/testing/testing.cc
    • Updated test object definitions to use new auto-init features and structural traits.
  • tests/cpp/test_reflection.cc
    • Updated test cases for DefaultFactory and DefaultValue aliases, and added auto-init tests.
  • tests/cpp/testing_object.h
    • Updated DefaultValue to default_.
  • tests/python/test_dataclass_c_class.py
    • New Python tests for c_class decorator.
  • tests/python/test_dataclass_compare.py
    • New Python tests for structural comparison.
  • tests/python/test_dataclass_copy.py
    • Renamed from test_copy.py, updated tests for deep copy.
  • tests/python/test_dataclass_hash.py
    • New Python tests for structural hashing.
  • tests/python/test_dataclass_init.py
    • New Python tests for auto-generated __ffi_init__.
  • tests/python/test_dataclass_repr.py
    • Renamed from test_repr.py, added new repr tests.
  • tests/python/test_function.py
    • Added test_pyfunc_init.
  • tests/python/test_object.py
    • Added tests for Object direct initialization and __slots__.
Activity
  • The pull request is titled "staged commits" and marked "DO NOT MERGE", indicating it's a work in progress or a draft for internal review.
  • The changes involve significant refactoring and new feature development, suggesting a substantial amount of work has been committed.
  • There are no explicit comments or reviews provided in the context.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@junrushao junrushao mentioned this pull request Feb 27, 2026
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a major refactoring and enhancement of the FFI reflection system, focusing on providing a more Python-native, dataclass-like experience for FFI objects. Key changes include:

  1. Auto-Generated __init__: A powerful mechanism to automatically generate __init__ methods for C++ objects from their reflection metadata. This supports default values, keyword-only arguments, and inheritance, significantly simplifying the Python-side boilerplate.
  2. Consolidated Dataclass Operations: The logic for deep copy, repr printing, recursive hashing, and recursive comparison has been consolidated into a new dataclass.cc file. The implementations now use an iterative DFS approach, making them robust against deep object graphs and stack overflows.
  3. Structural Comparison and Hashing: New FFI functions (RecursiveEq, RecursiveHash, etc.) provide comprehensive structural comparison and hashing capabilities, with support for custom hooks and fine-grained field exclusion.
  4. Improved Python API: The @c_class decorator is now a clean, declarative way to register FFI objects and opt-in to these new dataclass features. The old, complex Python-side dataclass emulation has been removed in favor of the more robust C++ implementation.
  5. Architectural Improvements: The Object base class has been refactored to use a metaclass that enforces __slots__, improving memory efficiency and preventing accidental attribute setting. The Module.__getattr__ has also been optimized with caching.

Overall, this is an exceptionally well-designed and implemented set of changes that greatly improves the usability and power of the FFI layer. The code is clean, the new APIs are intuitive, and the features are backed by extensive tests. I have no concerns with the proposed changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant