[TVMScript][Relax] Use tir.SizeVar for shape variables#18833
[TVMScript][Relax] Use tir.SizeVar for shape variables#18833tqchen wants to merge 2 commits intoapache:mainfrom
Conversation
Summary of ChangesHello @tqchen, 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 enhances the TVMScript parser by introducing a more precise handling of symbolic variables. It ensures that variables representing sizes in 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
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 the use of tir.SizeVar for symbolic variables in shape contexts within the TVMScript parser. This is a great improvement as it aligns the parser's shape inference with later Relax transformation passes, fixing inconsistencies. The changes in entry.py to introduce get_symbolic_size_vars and in parser.py to use it for creating tir.SizeVar are logical and well-implemented. The new tests are comprehensive and validate the distinction between shape variables (tir.SizeVar) and general symbolic variables (tir.Var) from R.Prim.
I have one concern regarding the un-xfailing of test_function_symbolic_variables_are_annotated, which I've detailed in a specific comment. It seems the test might pass for incorrect reasons.
TVMScript parser now uses `tir.SizeVar` (known non-negative) instead of `tir.Var` for symbolic variables defined in size contexts (R.Tensor shapes, R.Shape), while keeping `tir.Var` for R.Prim arguments. This aligns TVMScript parsing with Relax transformations that call `Analyzer::MarkGlobalNonNegValue`, fixing shape inference differences between parsing and transformation passes (Issue apache#16877).
The test_function_symbolic_variables_are_annotated test uses strided_slice(A, [0], [0], [extent-1]). With extent >= 0 (shape variable), extent=0 is valid making extent-1=-1, which triggers the negative-index clamping logic. Using assume_inbound=True avoids this since the test is about shape inference, not boundary checking.
0ce8e8a to
eeaded1
Compare
|
Closing: the SizeVar approach is too fragile — too many passes create new symbolic Var objects (fuse_ops, lambda_lift, canonicalize_bindings, bind_symbolic_vars, etc.) without preserving SizeVar, causing StructuralEqual mismatches. The existing local kernel shape reasoning (MarkGlobalNonNegValue during transformations) is sufficient. |
TVMScript parser now uses
tir.SizeVar(known non-negative) instead oftir.Varfor symbolic variables defined in size contexts (R.Tensor shapes, R.Shape), while keepingtir.Varfor R.Prim arguments. This aligns TVMScript parsing with Relax transformations that callAnalyzer::MarkGlobalNonNegValue, fixing shape inference differences between parsing and transformation passes