Skip to content

feat: support configurable constant-fold exclusions - #4450

Open
fs-eire wants to merge 1 commit into
pytorch:mainfrom
fs-eire:constant-fold-exclusion-mechanics
Open

feat: support configurable constant-fold exclusions#4450
fs-eire wants to merge 1 commit into
pytorch:mainfrom
fs-eire:constant-fold-exclusion-mechanics

Conversation

@fs-eire

@fs-eire fs-eire commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Description

This PR introduces a new lowering pass mark_constant_fold_exclusions just before the constant_fold pass. The new pass is used to mark specific nodes so that they will not be constant folded in the next pass.

The feature uses a rule based, configurable approach. There are pre-defined rules, and by default all rules are applied. User can use a disabled_constant_fold_exclusions setting to disable one or more rules by specifying the rule name.

Currently, this PR should not affect the behavior because the rule list is empty. I will work on add a new rule related to attention in the next PR, which depends on the current PR.

Type of change

Please delete options that are not relevant and/or add your own.

  • New feature (non-breaking change which adds functionality)

Checklist:

  • My code follows the style guidelines of this project (You can use the linters)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas and hacks
  • I have made corresponding changes to the documentation
  • I have added tests to verify my fix or my feature
  • New and existing unit tests pass locally with my changes
  • I have added the relevant labels to my PR in so that relevant reviewers are notified

@meta-cla meta-cla Bot added the cla signed label Jul 30, 2026
@github-actions github-actions Bot added component: lowering Issues re: The lowering / preprocessing passes component: core Issues re: The core compiler component: api [Python] Issues re: Python API component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths labels Jul 30, 2026
@github-actions
github-actions Bot requested a review from zewenli98 July 30, 2026 20:33
@fs-eire
fs-eire force-pushed the constant-fold-exclusion-mechanics branch from d688a7c to 7c18031 Compare July 30, 2026 22:27
@github-actions github-actions Bot added the component: tests Issues re: Tests label Jul 30, 2026
mark_constant_fold_exclusions,
)

TEST_RULE_ID = "test_arbitrary_node"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registering test_arbitrary_node at import time permanently adds it to the global rule registry. Can we register/unregister so this test node isnt in the registry for other tests?

incompatible_settings,
)

def test_registered_rule_can_mark_an_arbitrary_node(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test verifies that a node gets marked, but not that the marked node survives constant_fold. Can we also run constant_fold and assert the node wasn’t folded away?

continue
_mark_constant_fold_exclusion(rule(node), rule_id)

if disabled_rule_ids:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marks can also be set earlier, and this pass is supposed to revoke them when the rule is disabled. Can we add a test that pre-seeds node.meta with a rule ID and asserts it’s cleared after running with that rule disabled?


@register_constant_fold_exclusion_rule(TEST_RULE_ID)
def custom_rule(node):
return (node,) if node.target is custom_target else ()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it a must for users to register rules?
From the perspective of users, is there any principle for them to say in which scenarios they need to register rules? Can we predefine some rules?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla signed component: api [Python] Issues re: Python API component: core Issues re: The core compiler component: dynamo Issues relating to the `torch.compile` or `torch._dynamo.export` paths component: lowering Issues re: The lowering / preprocessing passes component: tests Issues re: Tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants