Skip to content

Skip impure ops in constant_prop_pass - #22418

Open
john-rocky wants to merge 1 commit into
pytorch:mainfrom
john-rocky:constant-prop-skip-impure
Open

Skip impure ops in constant_prop_pass#22418
john-rocky wants to merge 1 commit into
pytorch:mainfrom
john-rocky:constant-prop-skip-impure

Conversation

@john-rocky

Copy link
Copy Markdown
Contributor

Summary

constant_prop_pass folds every call_function node whose arguments are all constants. Ops that draw from the RNG take only sizes as arguments, so they qualify: a model returning x + torch.rand(4) came out of the pass with the draw frozen into _prop_tensor_constant0, and returned the same value on every call.

@JakeStevens spotted this while reviewing #22391 (repro there). The pass already runs in the Qualcomm and Samsung backends and in quant_fusion_pass, so the fix stands on its own.

Skip nodes that torch.fx.Node.is_impure() reports as impure. That covers ops tagged nondeterministic_seeded (rand, randn, bernoulli, dropout, ...), mutable schemas and side-effectful functions, and is the same check eliminate_dead_code uses to decide what it must keep.

Test plan

New test_constant_prop_pass_skips_nondeterministic_ops in exir/tests/test_passes.py: after the pass one aten.rand node remains, no constant was added, and two calls give different outputs. It fails on main with 0 != 1.

python -m unittest executorch.exir.tests.test_passes -k constant_prop: 15 tests pass (torch 2.13.0, macOS arm64).

constant_prop_pass folds any call_function node whose arguments are all
constants. Ops that draw from the RNG, such as aten.rand, take only sizes
as arguments, so they qualified and were replaced by a single frozen draw:
a model returning x + torch.rand(4) produced the same output on every call
once the pass had run.

Skip nodes that torch.fx.Node.is_impure() reports as impure. That covers
ops tagged nondeterministic_seeded, mutable schemas and side-effectful
functions, and is the same test eliminate_dead_code uses to decide what it
must keep.
@pytorch-bot

pytorch-bot Bot commented Sep 1, 2026

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/22418

Note: Links to docs will display an error until the docs builds have been completed.

❌ 3 New Failures, 2 Unrelated Failures

As of commit 65ff880 with merge base 448fbfe (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

BROKEN TRUNK - The following job failed but was present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 1, 2026
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

or node.target is memory.alloc
or node.target in all_skip_targets
# Ops with side effects (RNG draws, mutation) have to run at
# runtime. `aten.rand` has no tensor inputs, so without this check

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.

Suggested change
# runtime. `aten.rand` has no tensor inputs, so without this check
# runtime.

or node.target in all_skip_targets
# Ops with side effects (RNG draws, mutation) have to run at
# runtime. `aten.rand` has no tensor inputs, so without this check
# it would be folded into a single frozen draw.

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.

Suggested change
# it would be folded into a single frozen draw.

@JakeStevens JakeStevens 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.

some nits on comment

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants