[CORE] Add TypeConverter system for runtime type validation/conversion#455
[CORE] Add TypeConverter system for runtime type validation/conversion#455junrushao wants to merge 1 commit intoapache:mainfrom
Conversation
Summary of ChangesHello @junrushao, 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 significantly enhances the TVM FFI system by introducing robust runtime type checking and dynamic type conversion capabilities. It provides a Highlights
🧠 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
Activity
Using Gemini Code AssistThe 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
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 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
|
There was a problem hiding this comment.
Code Review
This pull request introduces a powerful and well-designed TypeSchema and TypeChecker for runtime type checking and conversion in the FFI layer. The approach of pre-compiling schemas into an efficient representation with a Kind enum is a great optimization. The implementation is comprehensive, covering a wide range of FFI types and their conversion semantics, including container conversions with fast-paths. The test suite is also extensive, providing good coverage for the new functionality. I have a couple of suggestions to improve robustness and code clarity, but overall this is a solid contribution.
e679834 to
9023f0c
Compare
|
Addressed Gemini's review comments:
|
b28074e to
8bc9abc
Compare
8bc9abc to
faca787
Compare
Add a TypeConverter system to the Cython FFI module that takes a TypeSchema and returns a Function wrapping a C++ conversion function (Any) -> Any. The converter validates/converts input values to match the target type, with fast-path (zero-copy) for containers when elements already match and slow-path (element-by-element conversion) otherwise. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
faca787 to
b623dd9
Compare
Summary
TypeConvertersystem to the Cython FFI module that takes aTypeSchemaand returns atvm_ffi.Functionwrapping a C++ conversion function(Any) -> AnyTypeErrormessages are provided on type mismatch (e.g.,"expected int, got float","expected list[int], but element at index 2 has type float")New Files
python/tvm_ffi/cython/tvm_ffi_type_converter.h— C++ header implementingTVMFFIPyTypeConverterNodetree withCheckStrict(zero-copy eligibility) andConvertmethods for all supported kindspython/tvm_ffi/cython/type_converter.pxi— Cython bridge mappingTypeSchemaorigins to C++ converter nodes, exposescreate_type_converter(schema)tests/python/test_type_converter.py— 34 comprehensive tests covering POD types, strings/bytes, special types, Optional, Union, container fast/slow paths, tuples, nested containers, object hierarchy, and error messagesSupported Type Kinds
Test plan
uv run pytest tests/python/test_type_converter.py -v— 34/34 passuv run pytest tests/python/ -v— 425 passed, 0 failures (no regressions)🤖 Generated with Claude Code