Skip to content

[AOTAutograd] Fix static_input_indices not offset when effect tokens are prepended#175904

Closed
wmhst7 wants to merge 3 commits intopytorch:mainfrom
wmhst7:wmhst7-patch-1
Closed

[AOTAutograd] Fix static_input_indices not offset when effect tokens are prepended#175904
wmhst7 wants to merge 3 commits intopytorch:mainfrom
wmhst7:wmhst7-patch-1

Conversation

@wmhst7
Copy link
Contributor

@wmhst7 wmhst7 commented Feb 26, 2026

Summary

When effectful ops (e.g., with_effects) are present, handle_effect_tokens_fn() prepends effect token placeholders to the input args. However, static_input_indices in ViewAndMutationMeta is computed before this prepending and is not adjusted afterwards. This causes indices to point to wrong inputs, leading to issues like unnecessary CUDA graph re-recording.

Problem

In handle_effect_tokens_fn(), effect tokens are prepended to args:

additional_fwd_token_inputs = [torch.tensor([])] * num_tokens
args = [*additional_fwd_token_inputs, *args]  # tokens prepended at index 0

But meta.static_input_indices is not offset by num_tokens. When these indices are later used (e.g., by CUDAGraph's check_invariants), they point to the wrong inputs:

Before tokens: args=[activation, weight], static_input_indices=[1] → weight ✓
After tokens: args=[token, activation, weight], static_input_indices=[1] → activation ✗
Expected: static_input_indices=[2] (offset by num_tokens=1) → weight ✓

Impact

  • Activations get incorrectly marked as static inputs
  • CUDAGraph's check_invariants sees data pointer changes for "static" inputs
  • This triggers unnecessary re-recording, causing performance degradation

Fix

Offset static_input_indices by num_tokens after prepending effect tokens in the forward-only (trace_joint=False) path:

  if num_tokens > 0:
      meta.static_input_indices = [
          idx + num_tokens for idx in meta.static_input_indices
      ]

Unit Test

Added test_static_input_indices_with_effect_tokens in test/functorch/test_aotdispatch.py which:

  1. Registers a custom effectful op via _register_effectful_op
  2. Compiles a function with torch.compile using a metadata-capturing backend
  3. Verifies that all static_input_indices are >= num_tokens after effect tokens are prepended (i.e., no index
    incorrectly points to a token input)

cc @yanboliang

@pytorch-bot
Copy link

pytorch-bot bot commented Feb 26, 2026

🔗 Helpful Links

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

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

✅ You can merge normally! (4 Unrelated Failures)

As of commit 36ac958 with merge base ca7ffb7 (image):

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

BROKEN TRUNK - The following jobs 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.

@pytorch-bot
Copy link

pytorch-bot bot commented Feb 26, 2026

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.

@soulitzer soulitzer added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Feb 27, 2026
@wmhst7 wmhst7 requested review from Chillee and ezyang as code owners March 3, 2026 05:22
@linux-foundation-easycla
Copy link

linux-foundation-easycla bot commented Mar 3, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

@yanboliang
Copy link
Contributor

@zou3519 @angelayi

Copy link
Contributor

@angelayi angelayi left a comment

Choose a reason for hiding this comment

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

thanks!

Fix line-too-long lint error in graph_capture_wrappers.py and add a test
verifying that static_input_indices are correctly offset when effect
tokens are prepended to inputs.
@wmhst7
Copy link
Contributor Author

wmhst7 commented Mar 5, 2026

@pytorchbot merge

@pytorch-bot pytorch-bot bot added the ciflow/trunk Trigger trunk jobs on your pull request label Mar 5, 2026
@pytorchmergebot
Copy link
Collaborator

Merge failed

Reason: 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.

Details for Dev Infra team Raised by workflow job

@pytorch-bot
Copy link

pytorch-bot bot commented Mar 5, 2026

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.

@wmhst7
Copy link
Contributor Author

wmhst7 commented Mar 5, 2026

@pytorchbot label "topic: not user facing"

@pytorch-bot pytorch-bot bot added the topic: not user facing topic category label Mar 5, 2026
@wmhst7
Copy link
Contributor Author

wmhst7 commented Mar 5, 2026

@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

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 Merged open source topic: not user facing topic category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants