Skip to content

[Relax] Add size heuristic to skip folding large creation ops#18764

Merged
tlopex merged 1 commit into
apache:mainfrom
guan404ming:fold-constant-size-heuristic
Feb 12, 2026
Merged

[Relax] Add size heuristic to skip folding large creation ops#18764
tlopex merged 1 commit into
apache:mainfrom
guan404ming:fold-constant-size-heuristic

Conversation

@guan404ming

Copy link
Copy Markdown
Member

Why

Folding large creation ops (zeros, ones, full, arange) with no tensor inputs materializes large constants in
the binary unnecessarily, since they are cheap to compute at runtime.

How

  • Add ShouldBeFolded heuristic that skips folding when output exceeds 1024 elements and the op has no tensor inputs
  • Check call arguments for tensor inputs, including tuples for call_tir
  • Add tests for large creation ops, small creation ops, and large ops with tensor inputs

@gemini-code-assist

Copy link
Copy Markdown
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 introduces a crucial optimization to the constant folding pass by preventing the folding of large creation operations that lack tensor inputs. This change significantly reduces the size of the compiled binary by avoiding the unnecessary embedding of large, easily computable constants, thereby improving overall efficiency without compromising correctness.

Highlights

  • Constant Folding Heuristic: Implemented a new heuristic in the ShouldBeFolded function to prevent constant folding of large creation operations (e.g., zeros, ones, full, arange) that do not have tensor inputs. This avoids materializing large constants into the binary when they are cheap to compute at runtime.
  • Tensor Input Detection: Enhanced the logic to accurately detect tensor inputs, including checking for tensors nested within tuples, which is relevant for operations like call_tir.
  • New Test Cases: Added comprehensive test cases to validate the new folding behavior, covering scenarios for large creation ops that should not be folded, small creation ops that should be folded, and large ops with tensor inputs that should still be folded.

🧠 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
  • src/relax/transform/fold_constant.cc
    • Implemented a heuristic in ShouldBeFolded to skip folding for creation ops producing more than 1024 elements if they have no tensor inputs.
    • Added logic to check for tensor inputs, including those within tuple arguments, to determine if an operation is a pure creation op.
  • tests/python/relax/test_transform_fold_constant.py
    • Added test_skip_folding_large_creation_op to verify that large creation ops without tensor inputs are not folded.
    • Added test_fold_small_creation_op to confirm that small creation ops are still correctly folded.
    • Added test_fold_large_op_with_tensor_input to ensure that large ops with tensor inputs are folded as expected.
Activity
  • No specific activity (comments, reviews, or progress updates) has been recorded for this pull request yet.
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.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

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 introduces a valuable heuristic to skip constant folding for large creation operations, which helps in reducing the final binary size. The implementation is well-structured and the accompanying tests cover the primary scenarios. I've identified a couple of areas for improvement: a potential integer overflow during the calculation of the number of elements in a tensor, and a bug in the logic for detecting tensor inputs within nested tuples. My review includes suggestions to address these points for a more robust implementation.

Comment thread src/relax/transform/fold_constant.cc Outdated
Comment thread src/relax/transform/fold_constant.cc Outdated
@guan404ming guan404ming force-pushed the fold-constant-size-heuristic branch from 7a35680 to 7680c2d Compare February 12, 2026 03:15
Signed-off-by: Guan-Ming Chiu <guanmingchiu@gmail.com>
@guan404ming guan404ming force-pushed the fold-constant-size-heuristic branch from 7680c2d to 09a68e5 Compare February 12, 2026 03:28
@guan404ming guan404ming marked this pull request as ready for review February 12, 2026 08:36
@tlopex tlopex merged commit c932f2a into apache:main Feb 12, 2026
11 checks passed
@guan404ming guan404ming deleted the fold-constant-size-heuristic branch February 17, 2026 15:50
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