[FEAT] Add structural visitor and typed structural walk APIs#601
[FEAT] Add structural visitor and typed structural walk APIs#601Kathryn-cat wants to merge 4 commits into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a structural visit and walk implementation for the TVM FFI, enabling traversal of object graphs with support for early interruption. It adds the StructuralVisitor and StructuralWalk APIs, implements built-in container visitors, and provides unsafe raw-storage helpers for Expected. The review feedback recommends adding a defensive check for undefined ObjectRef values in DefaultVisitExpected to prevent potential crashes, supporting void return types in StructuralWalk callbacks to reduce boilerplate, and adding a static assertion to verify that each callback takes exactly one argument to avoid obscure template compilation errors.
6618b29 to
802b33e
Compare
d1858f6 to
e7da414
Compare
| std::vector<ObjectRef> visited; | ||
| std::vector<TVMFFIDefRegionKind> modes; | ||
| ObjectRef interrupt_on; | ||
| String interrupt_payload = "stop"; |
There was a problem hiding this comment.
This is likely not need atm, focus ontesting StructuralWalk
There was a problem hiding this comment.
I've significantly reduced the test_structural_visit.cc test cases, and moved objects to testing_object.h. I'd like to keep TestVisitorObj in the test file because it is useful for helping to check visited nodes, def region kinds, and interrupt behavior. This is a useful fixture I think.
6869dc1 to
6771810
Compare
3a7e7ac to
eab8843
Compare
7f17093 to
3df7f1f
Compare
61c1dcc to
54e80f0
Compare
92462cf to
efcef39
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a structural walk and visit API to the TVM FFI, allowing users to traverse structured value graphs and invoke typed callbacks in both C++ and Python. It adds core classes and functions such as StructuralVisitor, VisitInterrupt, WalkOrder, WalkResult, and structural_walk, along with comprehensive unit tests and documentation. The review feedback highlights three improvement opportunities: refactoring the C++ macros TVM_FFI_S_VISIT_MAYBE_EARLY_RETURN and TVM_FFI_S_VISIT_MAYBE_EARLY_RETURN_WITH_ERROR_CONTEXT to prevent multiple evaluations of their arguments, and replacing assert statements in the Python structural_walk callback normalization with explicit type checks to ensure robust input validation.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces a new structural_walk API and StructuralVisitor to the TVM FFI, enabling structural traversal of object graphs with support for custom callbacks and definition-region tracking. The implementation includes necessary C++ headers, source files, Python bindings, and comprehensive tests. A review comment suggests improving robustness by catching std::exception in the visitor's callback dispatcher to avoid potential process termination.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
Summary
This PR adds structural traversal APIs for TVM FFI values in C++ and Python.
The main user-facing addition is
StructuralWalk/tvm_ffi.structural_walk, a pytree-style walk that can visit both object-backed nodes and POD leaves (int,float,bool,str,bytes). It supports typed callbacks, pre/post-order traversal, child skipping, early interruption, and def-region-aware callbacks.Key Changes
StructuralVisitor,VisitInterrupt,WalkResult, andStructuralWalk.tvm_ffi.structural_walk.AnyView, allowing callbacks to match objects, containers, and POD values.TVMFFIDefRegionKind.(type, callback)entries, with support for grouped types like((int, float), callback), a single callback catch-all, or a sequence of entries.with_def_region_kind=and receive(value, def_region_kind).Anycatch-all,Objectmatching, and string/bytes storage variants.AnyViewfor the visited value.Examples
Python:
Grouped callback types:
Def-region-aware callbacks:
C++:
C++ def-region-aware walk:
Testing
Any/Objectbehavior, post-order traversal, skip, and interrupts.