Skip to content

move subgraph_has_impure_ops from node.is_impure into const_fold to unblock production#167443

Closed
jazlyn5 wants to merge 1 commit intopytorch:mainfrom
jazlyn5:export-D86641994
Closed

move subgraph_has_impure_ops from node.is_impure into const_fold to unblock production#167443
jazlyn5 wants to merge 1 commit intopytorch:mainfrom
jazlyn5:export-D86641994

Conversation

@jazlyn5
Copy link
Contributor

@jazlyn5 jazlyn5 commented Nov 10, 2025

Summary:
#166609 updates node.is_impure to consider a submodule as impure if submodule contains impure node. This in turn changes graph.eliminate_dead_code() function behavior, which does not eliminate nodes with side effects, see pytorch documentation

Remove all dead code from the graph, based on each node’s number of users, and whether the nodes have any side effects.

While this is correct that a submodule containing side-effectful ops is side-effectful and should not be dead code eliminated, some customers rely on the dead code elimination to eliminate submodules that contain impure ops which is the behavior before #166609 fix.

Due to production environment constraints, we have to revert #166609 and move the side-effectful submodule check logic to const_fold.py, which will correctly not const-fold a submodule that contains impure ops.

NOTE other call sites that use node.is_impure() to make decisions are still incorrectly eliminating side-effectful submodules, but we can't safely change that today.

This pr

  • move _subgraph_has_impure_op into fx/experimental/const_fold.py, check and prevent const-folding an impure submodule
  • added a note in node.is_impure to highlight the incorrect behavior and context in case people go looking in the future.

Test Plan: run test_fx_const_fold and all tests pass

Differential Revision: D86641994

cc @ezyang @EikanWang @jgong5 @wenzhe-nrv

@pytorch-bot
Copy link

pytorch-bot bot commented Nov 10, 2025

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 46aee4d with merge base 3cfbf98 (image):

UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:

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

@pytorch-bot pytorch-bot bot added the release notes: fx release notes category label Nov 10, 2025
@meta-codesync
Copy link

meta-codesync bot commented Nov 10, 2025

@jazlyn5 has exported this pull request. If you are a Meta employee, you can view the originating Diff in D86641994.

jazlyn5 added a commit to jazlyn5/pytorch that referenced this pull request Nov 10, 2025
…nblock production (pytorch#167443)

Summary:

pytorch#166609 updates `node.is_impure` to consider a submodule as impure if submodule contains impure node. Prior to pytorch#166609, we always consider a submodule as pure, regardless of what ops contained inside. 


pytorch#166609 fixed that impurity gap, and in turn changed `graph.eliminate_dead_code()` function behavior, which does not eliminate nodes with side effects, see [pytorch documentation](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.Graph.eliminate_dead_code)
> Remove all dead code from the graph, based on each node’s number of users, and whether the nodes have any side effects. 

As a result, dead code elimination may no longer remove *some* unused submodules if they have impure ops. While this is logically correct, some customers rely on the dead code elimination to eliminate impure submodules.

Due to such production environment constraints, we have to revert pytorch#166609 and move the side-effectful submodule check logic to `const_fold.py`, which will correctly **not** const-fold a submodule that contains impure ops.

NOTE other call sites that use `node.is_impure()` to make decisions are still incorrectly eliminating impure submodules, but we can't safely change that today.


## This pr
- move `_subgraph_has_impure_op` into `fx/experimental/const_fold.py`, check and prevent const-folding an impure submodule 
- added a note in `node.is_impure` to highlight the incorrect behavior and context in case people go looking in the future.

Test Plan: run test_fx_const_fold and all tests pass

Differential Revision: D86641994
@jazlyn5 jazlyn5 force-pushed the export-D86641994 branch 2 times, most recently from 68d60b3 to 6918379 Compare November 10, 2025 05:08
…nblock production (pytorch#167443)

Summary:

pytorch#166609 updates `node.is_impure` to consider a submodule as impure if submodule contains impure node. Prior to pytorch#166609, we mostly always consider a GraphModule submodule as pure, regardless of if it has impure ops. 


pytorch#166609 fixed that impurity gap, and in turn changed `graph.eliminate_dead_code()` function behavior, which does not eliminate nodes with side effects, see [pytorch documentation](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.Graph.eliminate_dead_code)
> Remove all dead code from the graph, based on each node’s number of users, and whether the nodes have any side effects. 

As a result, dead code elimination may no longer remove *some* unused submodules if they have impure ops. While this is logically correct, some customers rely on the dead code elimination to eliminate impure submodules.

Due to such production environment constraints, we have to revert pytorch#166609 and move the side-effectful submodule check logic to `const_fold.py`, which will correctly **not** const-fold a submodule that contains impure ops.

NOTE other call sites that use `node.is_impure()` to make decisions are still incorrectly eliminating impure submodules, but we can't safely change that today.


## This pr
- move `_subgraph_has_impure_op` into `fx/experimental/const_fold.py`, check and prevent const-folding an impure submodule 
- added a note in `node.is_impure` to highlight the incorrect behavior and context in case people go looking in the future.

Test Plan: run test_fx_const_fold and all tests pass

Differential Revision: D86641994
@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Nov 10, 2025
@jazlyn5
Copy link
Contributor Author

jazlyn5 commented Nov 10, 2025

@pytorchbot merge

@pytorchmergebot
Copy link
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

Silv3S pushed a commit to Silv3S/pytorch that referenced this pull request Nov 18, 2025
… unblock production (pytorch#167443)

Summary:
pytorch#166609 updates `node.is_impure` to consider a submodule as impure if submodule contains impure node. This in turn changes `graph.eliminate_dead_code()` function behavior, which does not eliminate nodes with side effects, see [pytorch documentation](https://docs.pytorch.org/docs/stable/fx.html#torch.fx.Graph.eliminate_dead_code)
> Remove all dead code from the graph, based on each node’s number of users, and whether the nodes have any side effects.

While this is correct that a submodule containing side-effectful ops is side-effectful and should not be dead code eliminated, some customers rely on the dead code elimination to eliminate submodules that contain impure ops which is the behavior before pytorch#166609 fix.

Due to production environment constraints, we have to revert pytorch#166609 and move the side-effectful submodule check logic to `const_fold.py`, which will correctly **not** const-fold a submodule that contains impure ops.

NOTE other call sites that use `node.is_impure()` to make decisions are still incorrectly eliminating side-effectful submodules, but we can't safely change that today.

## This pr
- move `_subgraph_has_impure_op` into `fx/experimental/const_fold.py`, check and prevent const-folding an impure submodule
- added a note in `node.is_impure` to highlight the incorrect behavior and context in case people go looking in the future.

Test Plan: run test_fx_const_fold and all tests pass

Differential Revision: D86641994

Pull Request resolved: pytorch#167443
Approved by: https://github.com/jfix71
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/trunk Trigger trunk jobs on your pull request fb-exported fx Merged meta-exported release notes: fx release notes category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants