Skip to content

[AOTAutograd] don't save assert_scalar sympy.Exprs for BWD - #179315

Closed
pianpwk wants to merge 2 commits into
gh/pianpwk/130/basefrom
gh/pianpwk/130/head
Closed

pianpwk wants to merge 2 commits into
gh/pianpwk/130/basefrom
gh/pianpwk/130/head

Conversation

@pianpwk

@pianpwk pianpwk commented Apr 3, 2026 •

Copy link
Copy Markdown
Contributor

For issue at pytorch/torchtitan#2741 (comment)

The min-cut partitioner avoids saving intermediate runtime assertion nodes for the BWD graph, but the assertion expressions are not skipped, and inductor can fail when they're passed as BWD graph inputs.

This skips the expression node as well.

Stack from ghstack (oldest at bottom):

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @kadeng @chauhang @amjames @Lucaskabela @jataylo @azahed98

@pytorch-bot

pytorch-bot Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit d189d9e with merge base 44a01f7 (image):
💚 Looks good so far! There are no failures yet. 💚

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

@pytorch-bot pytorch-bot Bot added ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests labels Apr 3, 2026
pianpwk added a commit that referenced this pull request Apr 3, 2026
@pytorch-bot

pytorch-bot Bot commented Apr 3, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

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

@pianpwk pianpwk added the topic: not user facing topic category label Apr 3, 2026
@pianpwk
pianpwk marked this pull request as ready for review April 3, 2026 21:21
return (
isinstance(n.meta.get("val"), torch.SymBool)
and len(n.users) > 0
and all(u.target is torch.ops.aten._assert_scalar.default for u in n.users)

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.

@claude what happens to the _assert_scalar downstream op if we don't save the symbool? is it still partitioned into the backward? and how will it receive its inputs

@xmfan

xmfan commented Apr 3, 2026

Copy link
Copy Markdown
Contributor

@claude what happens to the _assert_scalar downstream op if we don't save the symbool? is it still partitioned into the backward? and how will it receive its inputs

@claude

claude Bot commented Apr 3, 2026 •

Copy link
Copy Markdown

Claude finished @xmfan's task in 4m 16s —— View job


  • Read the changed code and surrounding context
  • Trace the partitioning logic for _assert_scalar and its inputs
  • Post answer

Answer

The _assert_scalar node is already excluded from the backward graph — the existing min-cut logic already handles this. Here's the full picture:

1. _assert_scalar is typically a forward-only node. In classify_nodes (line 3470-3477), required_bw_nodes is built by starting from tangent placeholders and _must_be_in_backward-tagged nodes, then transitively including all users. Runtime assertion _assert_scalar nodes from the forward pass are not downstream of tangent inputs, so they land in required_fw_nodes (or unclaimed_nodes).

2. When the backward graph is extracted, _extract_graph_with_inputs_outputs (line 281) iterates through the joint graph. For call_function nodes, if any argument maps to InvalidNode (line 335-344), the node itself becomes InvalidNode. Since _assert_scalar is forward-only and not in the backward inputs, its args would be InvalidNode in the backward subgraph extraction, making the _assert_scalar itself InvalidNode — effectively excluded from the backward graph via DCE.

3. What this PR actually fixes: The issue is not about the _assert_scalar op appearing in the backward graph. It's about the SymBool expression node that feeds into _assert_scalar being saved as a backward input. Here's what happens without this fix:

  • The min-cut choose_saved_values_set can select the SymBool comparison node (e.g., sym_size_int_1 <= 2147483647) as a value to save.
  • is_sym_node() returns True for SymBool nodes (they are py_sym_types), so they'd get added to saved_sym_nodes.
  • These saved_sym_nodes become inputs (placeholders) to the backward graph via _extract_fwd_bwd_modules (line 1021-1028).
  • Normally, unused placeholders in the backward graph get cleaned up at lines 1036-1041 (removing placeholders with no users). But the SymBool placeholder can have users if _extract_graph_with_inputs_outputs copies the _assert_scalar node into the backward graph because its SymBool input is now a valid placeholder (not InvalidNode).
  • Even if it does get cleaned up, the issue is that these SymBool expressions being passed as graph inputs can cause problems in inductor.

So this PR filters these SymBool nodes out of saved_sym_nodes before they're passed to _extract_fwd_bwd_modules, which prevents them from becoming backward graph inputs in the first place. The _assert_scalar ops that consume them would then have InvalidNode inputs during backward graph extraction and be excluded via the existing DCE logic at line 342-344.

To directly answer your questions:

  • Is _assert_scalar still partitioned into the backward? Without this PR, it could be, if the SymBool is saved as a backward input (making it a valid node in the backward graph extraction). With this PR, no — the SymBool is not saved, so _assert_scalar's input is InvalidNode and the assertion is DCE'd from the backward graph.
  • How will it receive its inputs? It won't need to, because it won't be in the backward graph. The assertion only serves to validate symbolic constraints during the forward pass.

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

Can we get a test?

@weifengpy

Copy link
Copy Markdown
Contributor

bumping this - per-layer compile with moe is this PR away from landing 🙏

For issue at pytorch/torchtitan#2741 (comment)

The min-cut partitioner avoids saving intermediate runtime assertion nodes for the BWD graph, but the assertion expressions are not skipped, and inductor can fail when they're passed as BWD graph inputs.

This skips the expression node as well.




[ghstack-poisoned]
pianpwk added a commit that referenced this pull request Apr 9, 2026

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

thanks for unblocking titan!

@pianpwk

pianpwk commented Apr 9, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 9, 2026
@pytorchmergebot

Copy link
Copy Markdown
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

pytorchmergebot pushed a commit that referenced this pull request May 6, 2026
…#182597)

Fixes #182413
Extension of #179315

AOTAutograd's min-cut partitioner already filters SymBool nodes whose only users are aten._assert_scalar, because these are runtime assertion intermediates and not real backward dependencies. default_partition could still save the same assert-only SymBool nodes into the forward/backward saved-symbol ABI. When a nested AOTAutograd function is executed under make_fx/ProxyTensor with unbacked symbols, RuntimeWrapper sees those saved values as torch.SymBool and rejects them.

This change shares the assert-only SymBool predicate between min-cut and default partitioning, and filters default_partition's saved_sym_nodes before extracting the forward/backward graphs.

The issue was reproduced with the standalone repro in #182413 and also in the TorchTitan/AutoParallel nested AOT GraphTrainer path.

Test Plan:
python test/dynamo/test_aot_autograd.py -k test_partitioner_filters_symbool_saved_for_bw
lintrunner --config=.lintrunner.toml torch/_functorch/partitioners.py test/dynamo/test_aot_autograd.py

Pull Request resolved: #182597
Approved by: https://github.com/pianpwk
@github-actions
github-actions Bot deleted the gh/pianpwk/130/head branch May 10, 2026 02:27
Alokksinha00 pushed a commit to Alokksinha00/pytorch that referenced this pull request May 15, 2026
…pytorch#182597)

Fixes pytorch#182413
Extension of pytorch#179315

AOTAutograd's min-cut partitioner already filters SymBool nodes whose only users are aten._assert_scalar, because these are runtime assertion intermediates and not real backward dependencies. default_partition could still save the same assert-only SymBool nodes into the forward/backward saved-symbol ABI. When a nested AOTAutograd function is executed under make_fx/ProxyTensor with unbacked symbols, RuntimeWrapper sees those saved values as torch.SymBool and rejects them.

This change shares the assert-only SymBool predicate between min-cut and default partitioning, and filters default_partition's saved_sym_nodes before extracting the forward/backward graphs.

The issue was reproduced with the standalone repro in pytorch#182413 and also in the TorchTitan/AutoParallel nested AOT GraphTrainer path.

Test Plan:
python test/dynamo/test_aot_autograd.py -k test_partitioner_filters_symbool_saved_for_bw
lintrunner --config=.lintrunner.toml torch/_functorch/partitioners.py test/dynamo/test_aot_autograd.py

Pull Request resolved: pytorch#182597
Approved by: https://github.com/pianpwk
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…pytorch#182597)

Fixes pytorch#182413
Extension of pytorch#179315

AOTAutograd's min-cut partitioner already filters SymBool nodes whose only users are aten._assert_scalar, because these are runtime assertion intermediates and not real backward dependencies. default_partition could still save the same assert-only SymBool nodes into the forward/backward saved-symbol ABI. When a nested AOTAutograd function is executed under make_fx/ProxyTensor with unbacked symbols, RuntimeWrapper sees those saved values as torch.SymBool and rejects them.

This change shares the assert-only SymBool predicate between min-cut and default partitioning, and filters default_partition's saved_sym_nodes before extracting the forward/backward graphs.

The issue was reproduced with the standalone repro in pytorch#182413 and also in the TorchTitan/AutoParallel nested AOT GraphTrainer path.

Test Plan:
python test/dynamo/test_aot_autograd.py -k test_partitioner_filters_symbool_saved_for_bw
lintrunner --config=.lintrunner.toml torch/_functorch/partitioners.py test/dynamo/test_aot_autograd.py

Pull Request resolved: pytorch#182597
Approved by: https://github.com/pianpwk
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…79315)

For issue at pytorch/torchtitan#2741 (comment)

The min-cut partitioner avoids saving intermediate runtime assertion nodes for the BWD graph, but the assertion expressions are not skipped, and inductor can fail when they're passed as BWD graph inputs.

This skips the expression node as well.

Pull Request resolved: pytorch#179315
Approved by: https://github.com/weifengpy
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…pytorch#182597)

Fixes pytorch#182413
Extension of pytorch#179315

AOTAutograd's min-cut partitioner already filters SymBool nodes whose only users are aten._assert_scalar, because these are runtime assertion intermediates and not real backward dependencies. default_partition could still save the same assert-only SymBool nodes into the forward/backward saved-symbol ABI. When a nested AOTAutograd function is executed under make_fx/ProxyTensor with unbacked symbols, RuntimeWrapper sees those saved values as torch.SymBool and rejects them.

This change shares the assert-only SymBool predicate between min-cut and default partitioning, and filters default_partition's saved_sym_nodes before extracting the forward/backward graphs.

The issue was reproduced with the standalone repro in pytorch#182413 and also in the TorchTitan/AutoParallel nested AOT GraphTrainer path.

Test Plan:
python test/dynamo/test_aot_autograd.py -k test_partitioner_filters_symbool_saved_for_bw
lintrunner --config=.lintrunner.toml torch/_functorch/partitioners.py test/dynamo/test_aot_autograd.py

Pull Request resolved: pytorch#182597
Approved by: https://github.com/pianpwk
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…79315)

For issue at pytorch/torchtitan#2741 (comment)

The min-cut partitioner avoids saving intermediate runtime assertion nodes for the BWD graph, but the assertion expressions are not skipped, and inductor can fail when they're passed as BWD graph inputs.

This skips the expression node as well.

Pull Request resolved: pytorch#179315
Approved by: https://github.com/weifengpy
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…pytorch#182597)

Fixes pytorch#182413
Extension of pytorch#179315

AOTAutograd's min-cut partitioner already filters SymBool nodes whose only users are aten._assert_scalar, because these are runtime assertion intermediates and not real backward dependencies. default_partition could still save the same assert-only SymBool nodes into the forward/backward saved-symbol ABI. When a nested AOTAutograd function is executed under make_fx/ProxyTensor with unbacked symbols, RuntimeWrapper sees those saved values as torch.SymBool and rejects them.

This change shares the assert-only SymBool predicate between min-cut and default partitioning, and filters default_partition's saved_sym_nodes before extracting the forward/backward graphs.

The issue was reproduced with the standalone repro in pytorch#182413 and also in the TorchTitan/AutoParallel nested AOT GraphTrainer path.

Test Plan:
python test/dynamo/test_aot_autograd.py -k test_partitioner_filters_symbool_saved_for_bw
lintrunner --config=.lintrunner.toml torch/_functorch/partitioners.py test/dynamo/test_aot_autograd.py

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

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: dynamo topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants