Skip to content

[Relax] Migrate NN conv/pooling/grad attrs from Array<IntImm> to Array<int64_t>#18733

Draft
guan404ming wants to merge 2 commits intoapache:mainfrom
guan404ming:fix-has-attr-integer-matching
Draft

[Relax] Migrate NN conv/pooling/grad attrs from Array<IntImm> to Array<int64_t>#18733
guan404ming wants to merge 2 commits intoapache:mainfrom
guan404ming:fix-has-attr-integer-matching

Conversation

@guan404ming
Copy link
Member

@guan404ming guan404ming commented Feb 8, 2026

Why

has_attr pattern matching failed for integer attributes because StructuralEqual requires exact dtype match for IntImm,
causing mismatches between Python-specified pattern values and actual operator attributes.

How

  • Migrate NN conv/pooling/grad attrs from Array to Array<int64_t>

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @guan404ming, 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 resolves an issue where has_attr pattern matching failed for integer attributes due to the strict type requirements of StructuralEqual. By introducing a new AttrValueEqual helper, the system now performs value-based integer comparisons and recursively handles arrays, allowing Python-specified pattern values to correctly match actual operator attributes. This enhancement improves the robustness and flexibility of attribute pattern matching within the dataflow matcher, ensuring more reliable pattern recognition.

Highlights

  • Lenient Integer Attribute Matching: Introduced a new helper function, AttrValueEqual, in dataflow_matcher.cc to perform lenient integer comparisons for attribute values, addressing issues where StructuralEqual required exact dtype matches for IntImm.
  • Integration of New Comparison Logic: Replaced all instances of StructuralEqual with the new AttrValueEqual within the MatchAttrs function, ensuring consistent and flexible attribute matching for both dictionary-style and reflection-based attributes.
  • Test Case Re-enablement and Fix: Re-enabled a previously disabled assertion in test_op_attr that validates has_attr pattern matching, and corrected the operator name from nn.conv2d to relax.nn.conv2d.
  • Documentation Update: Updated the docstring for AttrPattern in pattern.py to accurately reflect its support for Op, Call, and Function attributes, expanding on its previously stated limitation.

🧠 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
  • python/tvm/relax/dpl/pattern.py
    • Updated the docstring for AttrPattern to clarify its support for Op, Call, and Function attributes.
  • src/relax/ir/dataflow_matcher.cc
    • Added AttrValueEqual function for lenient integer and recursive array comparison of attribute values.
    • Replaced StructuralEqual with AttrValueEqual in MatchAttrs for both dict-style and reflection-based attribute matching.
  • tests/python/relax/test_dataflow_pattern.py
    • Re-enabled a previously disabled assertion in test_op_attr for has_attr pattern matching.
    • Corrected the operator name in the re-enabled assertion from nn.conv2d to relax.nn.conv2d.
    • Removed a redundant failing assertion.
Activity
  • A previously disabled test assertion related to has_attr pattern matching for nn.conv2d was re-enabled and fixed, indicating a prior issue that prevented its successful execution.
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.

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 effectively addresses the issue of has_attr pattern matching failing for integer attributes with mismatched dtypes. The introduction of the AttrValueEqual helper function in dataflow_matcher.cc is a clean solution. It correctly falls back to value-based comparison for integers and recursively handles arrays, which is necessary for attributes like strides. The changes to MatchAttrs to use this new helper are appropriate. The re-enabling of the test case in test_dataflow_pattern.py confirms the fix. The updated docstring for AttrPattern in python/tvm/relax/dpl/pattern.py accurately reflects its expanded capabilities. Overall, the changes are well-implemented and improve the robustness of the dataflow pattern matcher.

@guan404ming guan404ming marked this pull request as ready for review February 8, 2026 04:47
@guan404ming guan404ming changed the title Fix has_attr pattern matching for integer attributes [Relax] Fix has_attr pattern matching for integer attributes Feb 8, 2026
@tqchen
Copy link
Member

tqchen commented Feb 8, 2026

Thanks for the contribution. I think this is due to one of the legacy issues. Background: previously we used IntImm for constant attribute, and Integer redirects to IntImm.

POD types like int cannot not appear in the nested contained. Since tvm-ffi refactor, now we have dedicated int, so ideally for constant attributes like strides, we should enforce them now to use POD int and such issue will disappear

@guan404ming guan404ming force-pushed the fix-has-attr-integer-matching branch from 2c1729b to bb93d9a Compare February 8, 2026 15:57
@guan404ming guan404ming changed the title [Relax] Fix has_attr pattern matching for integer attributes [Relax] Migrate NN conv/pooling/grad attrs from Array<IntImm> to Array<int64_t> Feb 8, 2026
@guan404ming guan404ming force-pushed the fix-has-attr-integer-matching branch from 019b500 to 1421f0b Compare February 8, 2026 16:39
@guan404ming
Copy link
Member Author

Thanks for the contribution. I think this is due to one of the legacy issues. Background: previously we used IntImm for constant attribute, and Integer redirects to IntImm. POD types like int cannot not appear in the nested contained. Since tvm-ffi refactor, now we have dedicated int, so ideally for constant attributes like strides, we should enforce them now to use POD int and such issue will disappear

Thanks for the suggestion. I adjust the pr to migrate NN conv/pooling/grad attrs from Array to Array<int64_t> which I think to address the issue from real root cause.

@guan404ming guan404ming marked this pull request as draft February 8, 2026 18:21
@guan404ming guan404ming force-pushed the fix-has-attr-integer-matching branch from 1421f0b to c3b28dd Compare February 9, 2026 02:51
Signed-off-by: Guan-Ming Chiu <guanmingchiu@gmail.com>
Signed-off-by: Guan-Ming Chiu <guanmingchiu@gmail.com>
@guan404ming guan404ming force-pushed the fix-has-attr-integer-matching branch from c3b28dd to 4b245c1 Compare February 9, 2026 04:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants