gemini-code-assist[bot] commented on PR #472: URL: https://github.com/apache/tvm-ffi/pull/472#issuecomment-3948969546
## Summary of Changes Hello @junrushao, I'm Gemini Code Assist[^1]! 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 significantly enhances the FFI object system by introducing robust recursive comparison and hashing capabilities, which are crucial for deep equality checks and deterministic hashing across complex, potentially cyclic, data structures. It also refines the Python binding layer by optimizing object memory usage with `__slots__`, enforcing proper object construction, and improving the flexibility of `tvm_ffi.Function` initialization. Additionally, the changes include support for a new hardware device type and minor but important fixes to object representation for better debugging and consistency. This is a staged set of changes and should not be merged yet. ### Highlights * **Recursive Comparison and Hashing**: Introduced new C++ implementations for deep equality, ordering comparisons (RecursiveEq, RecursiveLt, etc.), and deterministic hashing (RecursiveHash) for FFI objects, supporting primitives, containers, and reflected objects with cycle detection and NaN handling. * **Reflection Flags for Comparison/Hashing**: Added new FFI field flags and corresponding `refl::Compare(false)` and `refl::Hash(false)` traits to allow specific object fields to be excluded from recursive comparison and hashing. * **Python Object System Refinements**: Refactored the Python FFI object hierarchy by introducing `CObject` as a Cython base class, making `Object` inherit from it with `__slots__` for efficiency, and disabling direct instantiation of base FFI objects. Also added an `__init__` method to `tvm_ffi.Function` for direct wrapping of Python callables. * **Deep Copy Cycle Handling**: Enhanced the `ObjectDeepCopier` to correctly manage cyclic references within immutable containers (like Array and Map) that contain mutable children (List and Dict), ensuring proper deep copying. * **New Device Type and Improved Repr**: Added `kDLMAIA` as a new device type and improved the `ReprPrint` utility to correctly escape special characters in strings and represent the 'trn' device name accurately. 🧠**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](https://codeassist.google/code-review/login) to enable Memory in your admin console.** <details> <summary><b>Changelog</b></summary> * **CMakeLists.txt** * Added new source files for recursive comparison and hashing to the build configuration. * **docs/conf.py** * Updated Sphinx documentation generation to correctly handle the `CObject` and `Object` class renaming. * Added new FFI methods to be hidden from the generated documentation. * **include/tvm/ffi/c_api.h** * Defined new bitmask flags (`kTVMFFIFieldFlagBitMaskCompareOff`, `kTVMFFIFieldFlagBitMaskHashOff`) to control field participation in recursive comparison and hashing. * **include/tvm/ffi/reflection/registry.h** * Added `refl::Compare` and `refl::Hash` `InfoTrait` classes to apply the new comparison and hashing flags to field information. * **python/tvm_ffi/_ffi_api.py** * Exposed new FFI functions for recursive comparison (`RecursiveEq`, `RecursiveGe`, `RecursiveGt`, `RecursiveLe`, `RecursiveLt`) and hashing (`RecursiveHash`). * **python/tvm_ffi/access_path.py** * Removed a redundant `super().__init__()` call in `AccessPath.__init__`. * **python/tvm_ffi/core.pyi** * Renamed the `Object` class to `CObject` and introduced a new `Object` class inheriting from `CObject` for type hinting. * Added an `__init__` method to the `Function` class for direct callable wrapping. * **python/tvm_ffi/cython/base.pxi** * Added `__slots__ = ()` to `ByteArrayArg` for memory efficiency. * **python/tvm_ffi/cython/device.pxi** * Added `kDLMAIA` to the `DLDeviceType` enum and updated device type mappings. * Added `__slots__ = ()` to the `Device` class for memory efficiency. * **python/tvm_ffi/cython/dtype.pxi** * Added `__slots__ = ()` to the `DataType` class for memory efficiency. * **python/tvm_ffi/cython/error.pxi** * Changed the base class of `Error` from `Object` to `CObject`. * Added `__slots__ = ()` to the `Error` class. * Updated `chandle` casting to `CObject` in error handling logic. * **python/tvm_ffi/cython/function.pxi** * Updated type casting from `Object` to `CObject` across various FFI argument setters and function creation. * Added an `__init__` method to the `Function` class to allow direct wrapping of Python callables. * **python/tvm_ffi/cython/object.pxi** * Refactored the `Object` class into `CObject` (Cython base) and `Object` (Python base with `__slots__` and metaclass). * Disabled direct instantiation of `CObject` to enforce FFI constructors. * Updated internal object handling to reflect the `CObject` and `Object` split. * **python/tvm_ffi/cython/string.pxi** * Updated `_string_obj_get_py_str` and `_bytes_obj_get_py_bytes` to cast to `CObject`. * **python/tvm_ffi/cython/tensor.pxi** * Updated `_shape_obj_get_py_tuple` and the `Tensor` base class to `CObject`. * Added `__slots__ = ()` to `Tensor` and `DLTensorTestWrapper` for memory efficiency. * **python/tvm_ffi/cython/type_info.pxi** * Updated `FieldGetter` and `FieldSetter` to accept `CObject` for field access. * **python/tvm_ffi/module.py** * Added `_tvm_ffi_attr_cache` to `Module.__slots__`. * Improved `__getattr__` caching logic for module functions. * **python/tvm_ffi/registry.py** * Included `ffi.Shape` in the list of container types for copy methods. * **python/tvm_ffi/testing/__init__.py** * Imported new testing classes `TestCompare` and `TestHash`. * **python/tvm_ffi/testing/testing.py** * Added `__test__ = False` to `TestIntPair`. * Defined new `TestCompare` and `TestHash` object classes with specific fields marked for comparison/hashing exclusion. * **src/ffi/extra/deep_copy.cc** * Added `unordered_set` for `in_progress_` tracking to manage objects currently being copied. * Implemented `FixupDeferredReferences` to handle cyclic references involving immutable containers and mutable children during deep copy. * **src/ffi/extra/recursive_compare.cc** * Added new file implementing `RecursiveComparer` class and FFI functions for deep equality and ordering comparisons, including NaN handling and cycle detection. * **src/ffi/extra/recursive_hash.cc** * Added new file implementing `RecursiveHasher` class and FFI function for deterministic recursive hashing, including NaN handling, cycle detection, and order-independent map hashing. * **src/ffi/extra/repr_print.cc** * Updated `ReprPrinter` to correctly escape strings and prevent duplicate field names in generic object representation. * Ensured correct representation of the 'trn' device name. * **src/ffi/testing/testing.cc** * Defined C++ classes `TestCompareObj` and `TestHashObj` and registered their FFI object info, including `refl::Compare(false)` and `refl::Hash(false)` for specific fields. * **tests/python/test_copy.py** * Added new test cases for deep copy cycle preservation with immutable root containers. * Added tests for Python deepcopy consistency for `Shape` objects. * **tests/python/test_function.py** * Added new test cases for `tvm_ffi.Function` initialization from Python callables and existing `Function` objects. * Included error handling tests for moved-from function objects and non-callable inputs. * **tests/python/test_object.py** * Added new test cases to verify that direct instantiation of `Object` and unregistered subclasses is disabled. * Added tests to check `__slots__` behavior for object subclasses. * **tests/python/test_recursive_compare.py** * Added new file with extensive unit tests for `RecursiveEq`, `RecursiveLt`, `RecursiveLe`, `RecursiveGt`, `RecursiveGe` covering primitives, containers, reflected objects, NaN handling, type mismatches, `CompareOff` flag, and cycle detection. * **tests/python/test_recursive_hash.py** * Added new file with extensive unit tests for `RecursiveHash` covering primitives, containers, reflected objects, NaN handling, `HashOff` flag, order independence for maps, cycle detection, and consistency with `RecursiveEq`. * **tests/python/test_repr.py** * Added new test cases for string literal round-tripping with special characters. * Added tests for container literal round-tripping with special strings. * Verified correct representation of the 'trn' device name. * Ensured inherited fields do not appear twice in generic repr for unregistered objects. </details> <details> <summary><b>Using Gemini Code Assist</b></summary> <br> The full guide for Gemini Code Assist can be found on our [documentation page](https://developers.google.com/gemini-code-assist/docs/review-github-code), here are some quick tips. <b>Invoking Gemini</b> 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. <b>Customization</b> 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](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). <b>Limitations & Feedback</b> Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: 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](https://google.qualtrics.com/jfe/form/SV_2cyuGuTWsEw84yG). <b>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](https://cloud.google.com/products/gemini/code-assist).</b> </details> [^1]: Review the [Privacy Notices](https://policies.google.com/privacy), [Generative AI Prohibited Use Policy](https://policies.google.com/terms/generative-ai/use-policy), [Terms of Service](https://policies.google.com/terms), and learn how to configure Gemini Code Assist in GitHub [here](https://developers.google.com/gemini-code-assist/docs/customize-gemini-behavior-github). Gemini can make mistakes, so double check it and [use code with caution](https://support.google.com/legal/answer/13505487). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
