Skip to content

[inductor] Fix loop-local load CSE lifetime - #194786

Closed
eellison wants to merge 1 commit into
gh/eellison/1093/basefrom
gh/eellison/1093/head
Closed

eellison wants to merge 1 commit into
gh/eellison/1093/basefrom
gh/eellison/1093/head

Conversation

@eellison

@eellison eellison commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

  • (to be filled)

Human context: fix for #194490) exposed by #184287 although the underlying cause was prior CSE / loop hoisting logic (and ultimately the fact that this is all tied to one very complicated file)..

Triton load placement and CSE lifetime used separate heuristics. An indirect load emitted inside a loop could consequently remain cached after loop closure and be reused by an epilogue even though its temporary did not dominate that use.

Use the selected load buffer as the source of truth for whether a CSE value survives loop closure. Also prevent loads carrying reduction masks from being hoisted before those masks are defined.

Test Plan:

python test/inductor/test_indexing.py ReductionInvariantIndexingTests
python test/inductor/test_codegen_triton.py -k reduction_invariant_load_indexing
git diff --check
lintrunner -a test/inductor/test_indexing.py torch/_inductor/codegen/triton.py

The two test commands and git diff check pass. Lintrunner was invoked, but Pyrefly failed on unrelated repository-wide missing-attribute errors because this detached worktree has no initialized lintrunner data.

AI-Assisted: Author reviewed and validated all AI-generated content.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 dba3349 with merge base 9286a16 (image):

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

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

@eellison eellison added the topic: not user facing topic category label Aug 25, 2026

@atalman atalman left a comment

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.

lgtm

@eellison

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 Aug 25, 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

@atalman

atalman commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

@pytorchmergebot cherry-pick --onto release/2.14 -c critical

@pytorchbot

Copy link
Copy Markdown
Collaborator

Cherry picking #194786

Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 60598ed3c8773875c0923101d54f206303b2f59f returned non-zero exit code 1

Auto-merging test/inductor/test_indexing.py
CONFLICT (content): Merge conflict in test/inductor/test_indexing.py
Auto-merging torch/_inductor/codegen/triton.py
CONFLICT (content): Merge conflict in torch/_inductor/codegen/triton.py
error: could not apply 60598ed3c87... [inductor] Fix loop-local load CSE lifetime (#194786)
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

atalman added a commit that referenced this pull request Aug 26, 2026
…me (#194786) (#194833)

* [inductor] Fix loop-local load CSE lifetime (#194786)

Manual cherry-pick of 60598ed (#194786) onto release/2.14.

Triton load placement and CSE lifetime used separate heuristics. An indirect
load emitted inside a loop could remain cached after loop closure and be
reused by an epilogue even though its temporary did not dominate that use. Use
the selected load buffer as the source of truth for whether a CSE value
survives loop closure, and stop hoisting loads that carry reduction masks
before those masks are defined.

Conflict resolution:

* torch/_inductor/codegen/triton.py: upstream also deletes the
  reduction_axes_omitted bookkeeping added by #184287, which is not on this
  branch, so that deletion is already satisfied and the surrounding code is
  left as-is. The two substantive changes (the has_rmask() guard on lifting,
  and load_buffer is self.body as the CSE-lifetime test) apply unchanged.
* test/inductor/test_indexing.py: upstream adds the regression test to
  ReductionInvariantIndexingTests, a class that #184287 introduced and which
  does not exist here. The regression test is ported verbatim into a new
  TestLoopLocalLoadCSELifetime class with the imports it needs
  (run_and_get_kernels, FileCheck, HAS_CUDA_AND_TRITON, config), all of which
  exist on this branch. The unrelated @parametrize("dense_indexing") hunk is
  dropped, since it belongs to a #184287 test.

Fixes #194490 on the release branch.

* Fix PYFMT import ordering

---------

Co-authored-by: eellison <[email protected]>
pytorchmergebot pushed a commit that referenced this pull request Aug 28, 2026
…5024)

Adds a wheel-level regression check for gh-194490.

## What it covers that the existing test doesn't

gh-194490 was a 2.13 Inductor regression — a gather/scatter pair around a loop reduction produced Triton whose epilogue read a temporary defined only inside the reduction body:

```
tmp41 = tl.where(tmp38, tmp40, tmp19)
                               ^
NameError: tmp19 is not defined
```

It was found by a user training Dion3 through vllm-project/speculators, not by CI.

The fix (gh-194786) added `test_loop_epilogue_indirect_load_scope` to `test/inductor/test_indexing.py`, and **that remains the authoritative test** — it asserts codegen structure via FileCheck, which this cannot. But it compiles with `dynamic=True`, whereas the reported failure only appeared on the **second** compile, once a differing shape triggered *automatic* dynamic shapes. Those are different paths.

So this check does two trials with different row counts and no `dynamic=` flag: the first compiles statically, the second generalizes. That's the route users actually hit.

## Fidelity to the reproducer

The `torch.where` over a tensor predicate is kept deliberately — the failure surfaced *inside* a `tl.where`, so simplifying it to a plain lerp risks not regenerating the same codegen. I had simplified it initially and put it back for that reason.

Shapes shrink from the reproducer's `(6, 4096, 4096)` to `2 x k x 4096`, but the **4096-wide reduction dim is preserved on purpose** so the reduction stays non-persistent and the loop path is actually emitted. Smaller and it becomes a persistent reduction and stops covering the bug.

## Blast radius

Runs under the existing torch-compile gate (already skipped on Python 3.15+ and non-Linux/macOS), and returns early without CUDA — so CPU and Windows wheels are unaffected. Roughly two `torch.compile` invocations on ~128K-element tensors.

Tolerances are loose (`rtol/atol 1e-4`). The assertion that matters is that codegen succeeds at all; a 4096-wide reduction over bf16 inputs differs between eager and Inductor by reduction order alone, and a wheel check must not fail on that.

## Caveats, stated plainly

**I could not run this** — no GPU on the machine I authored it on. Syntax and wiring are verified; the codegen behaviour is not. It needs one real run on a CUDA wheel before it's trusted. If it turns out the reduction goes persistent at these shapes, the test passes while covering nothing, and the reduction dim needs raising.

**Coverage is heuristic-dependent.** If Inductor's persistent-reduction threshold changes, this silently stops exercising the bug rather than failing. That's noted in the docstring with a pointer to the inductor test, which has no such dependency.

**On placement:** my own view is that compiler-codegen regressions belong in `test/inductor/`, not in the release smoke test — the smoke test validates that the binary works, and this adds a GPU-only, Triton-dependent, config-sensitive path to every wheel check. Adding it here anyway at @atalman's direction, since the escape happened on a release binary and the release gate is where he wants the net. Recording the tradeoff so the next person editing this file knows why it's here.

---

Filed with the help of Claude Code.
Pull Request resolved: #195024
Approved by: https://github.com/eellison, https://github.com/karthickai
pytorchmergebot pushed a commit that referenced this pull request Aug 31, 2026
test-infra's Release Binaries Validations checks out smoke_test.py from pytorch main but installs the published stable wheel. The regression check added in #195024 covers gh-194490, whose fix (#194786) landed after the 2.13 branch cut, so it reds all six linux CUDA 12.6 legs against the immutable 2.13.0 artifact with NameError('tmp19 is not defined').

Gate on the first release series containing the fix: 2.14, which has it via cherry-pick #194833. Reverting #195024 was the alternative, but it drops nightly coverage and leaves the next regression test added here to hit the same trap.

Test Plan:

2.13.0 skips; 2.14.0, main and nightly run.

```
python -c "
from torch.torch_version import TorchVersion
for v in ['2.13.0+cu126','2.14.0+cu130','2.15.0a0+gitabc','2.15.0.dev20260829+cu126']:
    print(v, TorchVersion(v) < (2, 14))
"
lintrunner -a --paths-cmd 'echo .ci/pytorch/smoke_test/smoke_test.py'
```

Authored with assistance from Claude Code.
Pull Request resolved: #195306
Approved by: https://github.com/pytorchgreenlight, https://github.com/atalman
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
Human context: fix for pytorch#194490) exposed by pytorch#184287 although the underlying cause was prior CSE / loop hoisting logic (and ultimately the fact that this is all tied to one very complicated file)..

Triton load placement and CSE lifetime used separate heuristics. An indirect load emitted inside a loop could consequently remain cached after loop closure and be reused by an epilogue even though its temporary did not dominate that use.

Use the selected load buffer as the source of truth for whether a CSE value survives loop closure. Also prevent loads carrying reduction masks from being hoisted before those masks are defined.

Test Plan:

```
python test/inductor/test_indexing.py ReductionInvariantIndexingTests
python test/inductor/test_codegen_triton.py -k reduction_invariant_load_indexing
git diff --check
lintrunner -a test/inductor/test_indexing.py torch/_inductor/codegen/triton.py
```

The two test commands and git diff check pass. Lintrunner was invoked, but Pyrefly failed on unrelated repository-wide missing-attribute errors because this detached worktree has no initialized lintrunner data.

AI-Assisted: Author reviewed and validated all AI-generated content.

Pull Request resolved: pytorch#194786
Approved by: https://github.com/atalman, https://github.com/karthickai
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
… (pytorch#195024)

Adds a wheel-level regression check for pytorchgh-194490.

## What it covers that the existing test doesn't

pytorchgh-194490 was a 2.13 Inductor regression — a gather/scatter pair around a loop reduction produced Triton whose epilogue read a temporary defined only inside the reduction body:

```
tmp41 = tl.where(tmp38, tmp40, tmp19)
                               ^
NameError: tmp19 is not defined
```

It was found by a user training Dion3 through vllm-project/speculators, not by CI.

The fix (pytorchgh-194786) added `test_loop_epilogue_indirect_load_scope` to `test/inductor/test_indexing.py`, and **that remains the authoritative test** — it asserts codegen structure via FileCheck, which this cannot. But it compiles with `dynamic=True`, whereas the reported failure only appeared on the **second** compile, once a differing shape triggered *automatic* dynamic shapes. Those are different paths.

So this check does two trials with different row counts and no `dynamic=` flag: the first compiles statically, the second generalizes. That's the route users actually hit.

## Fidelity to the reproducer

The `torch.where` over a tensor predicate is kept deliberately — the failure surfaced *inside* a `tl.where`, so simplifying it to a plain lerp risks not regenerating the same codegen. I had simplified it initially and put it back for that reason.

Shapes shrink from the reproducer's `(6, 4096, 4096)` to `2 x k x 4096`, but the **4096-wide reduction dim is preserved on purpose** so the reduction stays non-persistent and the loop path is actually emitted. Smaller and it becomes a persistent reduction and stops covering the bug.

## Blast radius

Runs under the existing torch-compile gate (already skipped on Python 3.15+ and non-Linux/macOS), and returns early without CUDA — so CPU and Windows wheels are unaffected. Roughly two `torch.compile` invocations on ~128K-element tensors.

Tolerances are loose (`rtol/atol 1e-4`). The assertion that matters is that codegen succeeds at all; a 4096-wide reduction over bf16 inputs differs between eager and Inductor by reduction order alone, and a wheel check must not fail on that.

## Caveats, stated plainly

**I could not run this** — no GPU on the machine I authored it on. Syntax and wiring are verified; the codegen behaviour is not. It needs one real run on a CUDA wheel before it's trusted. If it turns out the reduction goes persistent at these shapes, the test passes while covering nothing, and the reduction dim needs raising.

**Coverage is heuristic-dependent.** If Inductor's persistent-reduction threshold changes, this silently stops exercising the bug rather than failing. That's noted in the docstring with a pointer to the inductor test, which has no such dependency.

**On placement:** my own view is that compiler-codegen regressions belong in `test/inductor/`, not in the release smoke test — the smoke test validates that the binary works, and this adds a GPU-only, Triton-dependent, config-sensitive path to every wheel check. Adding it here anyway at @atalman's direction, since the escape happened on a release binary and the release gate is where he wants the net. Recording the tradeoff so the next person editing this file knows why it's here.

---

Filed with the help of Claude Code.
Pull Request resolved: pytorch#195024
Approved by: https://github.com/eellison, https://github.com/karthickai
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…ch#195306)

test-infra's Release Binaries Validations checks out smoke_test.py from pytorch main but installs the published stable wheel. The regression check added in pytorch#195024 covers pytorchgh-194490, whose fix (pytorch#194786) landed after the 2.13 branch cut, so it reds all six linux CUDA 12.6 legs against the immutable 2.13.0 artifact with NameError('tmp19 is not defined').

Gate on the first release series containing the fix: 2.14, which has it via cherry-pick pytorch#194833. Reverting pytorch#195024 was the alternative, but it drops nightly coverage and leaves the next regression test added here to hit the same trap.

Test Plan:

2.13.0 skips; 2.14.0, main and nightly run.

```
python -c "
from torch.torch_version import TorchVersion
for v in ['2.13.0+cu126','2.14.0+cu130','2.15.0a0+gitabc','2.15.0.dev20260829+cu126']:
    print(v, TorchVersion(v) < (2, 14))
"
lintrunner -a --paths-cmd 'echo .ci/pytorch/smoke_test/smoke_test.py'
```

Authored with assistance from Claude Code.
Pull Request resolved: pytorch#195306
Approved by: https://github.com/pytorchgreenlight, https://github.com/atalman
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
… (pytorch#195024)

Adds a wheel-level regression check for pytorchgh-194490.

## What it covers that the existing test doesn't

pytorchgh-194490 was a 2.13 Inductor regression — a gather/scatter pair around a loop reduction produced Triton whose epilogue read a temporary defined only inside the reduction body:

```
tmp41 = tl.where(tmp38, tmp40, tmp19)
                               ^
NameError: tmp19 is not defined
```

It was found by a user training Dion3 through vllm-project/speculators, not by CI.

The fix (pytorchgh-194786) added `test_loop_epilogue_indirect_load_scope` to `test/inductor/test_indexing.py`, and **that remains the authoritative test** — it asserts codegen structure via FileCheck, which this cannot. But it compiles with `dynamic=True`, whereas the reported failure only appeared on the **second** compile, once a differing shape triggered *automatic* dynamic shapes. Those are different paths.

So this check does two trials with different row counts and no `dynamic=` flag: the first compiles statically, the second generalizes. That's the route users actually hit.

## Fidelity to the reproducer

The `torch.where` over a tensor predicate is kept deliberately — the failure surfaced *inside* a `tl.where`, so simplifying it to a plain lerp risks not regenerating the same codegen. I had simplified it initially and put it back for that reason.

Shapes shrink from the reproducer's `(6, 4096, 4096)` to `2 x k x 4096`, but the **4096-wide reduction dim is preserved on purpose** so the reduction stays non-persistent and the loop path is actually emitted. Smaller and it becomes a persistent reduction and stops covering the bug.

## Blast radius

Runs under the existing torch-compile gate (already skipped on Python 3.15+ and non-Linux/macOS), and returns early without CUDA — so CPU and Windows wheels are unaffected. Roughly two `torch.compile` invocations on ~128K-element tensors.

Tolerances are loose (`rtol/atol 1e-4`). The assertion that matters is that codegen succeeds at all; a 4096-wide reduction over bf16 inputs differs between eager and Inductor by reduction order alone, and a wheel check must not fail on that.

## Caveats, stated plainly

**I could not run this** — no GPU on the machine I authored it on. Syntax and wiring are verified; the codegen behaviour is not. It needs one real run on a CUDA wheel before it's trusted. If it turns out the reduction goes persistent at these shapes, the test passes while covering nothing, and the reduction dim needs raising.

**Coverage is heuristic-dependent.** If Inductor's persistent-reduction threshold changes, this silently stops exercising the bug rather than failing. That's noted in the docstring with a pointer to the inductor test, which has no such dependency.

**On placement:** my own view is that compiler-codegen regressions belong in `test/inductor/`, not in the release smoke test — the smoke test validates that the binary works, and this adds a GPU-only, Triton-dependent, config-sensitive path to every wheel check. Adding it here anyway at @atalman's direction, since the escape happened on a release binary and the release gate is where he wants the net. Recording the tradeoff so the next person editing this file knows why it's here.

---

Filed with the help of Claude Code.
Pull Request resolved: pytorch#195024
Approved by: https://github.com/eellison, https://github.com/karthickai
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…ch#195306)

test-infra's Release Binaries Validations checks out smoke_test.py from pytorch main but installs the published stable wheel. The regression check added in pytorch#195024 covers pytorchgh-194490, whose fix (pytorch#194786) landed after the 2.13 branch cut, so it reds all six linux CUDA 12.6 legs against the immutable 2.13.0 artifact with NameError('tmp19 is not defined').

Gate on the first release series containing the fix: 2.14, which has it via cherry-pick pytorch#194833. Reverting pytorch#195024 was the alternative, but it drops nightly coverage and leaves the next regression test added here to hit the same trap.

Test Plan:

2.13.0 skips; 2.14.0, main and nightly run.

```
python -c "
from torch.torch_version import TorchVersion
for v in ['2.13.0+cu126','2.14.0+cu130','2.15.0a0+gitabc','2.15.0.dev20260829+cu126']:
    print(v, TorchVersion(v) < (2, 14))
"
lintrunner -a --paths-cmd 'echo .ci/pytorch/smoke_test/smoke_test.py'
```

Authored with assistance from Claude Code.
Pull Request resolved: pytorch#195306
Approved by: https://github.com/pytorchgreenlight, https://github.com/atalman
@github-actions
github-actions Bot deleted the gh/eellison/1093/head branch November 9, 2026 01:49
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: inductor topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants