Skip to content

[Cherry-pick][release/2.14] [inductor] Fix loop-local load CSE lifetime (#194786) - #194833

Merged
atalman merged 2 commits into
pytorch:release/2.14from
atalman:atalman/cp-194786-2.14
Aug 26, 2026
Merged

atalman merged 2 commits into
pytorch:release/2.14from
atalman:atalman/cp-194786-2.14

Conversation

@atalman

@atalman atalman commented Aug 26, 2026 •

Copy link
Copy Markdown
Collaborator

Manual cherry-pick of #194786 (60598ed3c877) onto release/2.14. Fixes #194490 on the release branch.

What the fix does

Triton load placement and CSE lifetime used separate heuristics. An indirect load emitted inside a reduction loop could stay in the CSE cache after the loop closed and be reused by an epilogue, even though its temporary did not dominate that use. Two changes:

  • get_load_buffer no longer lifts a load out of the loop when its indexing carries a reduction mask, since the mask is not defined outside the loop.
  • load now uses the selected load buffer as the source of truth for CSE lifetime: load_buffer is self.body, rather than re-deriving it from has_rmask() / has_rindex().
             self.inside_reduction
             and self.range_trees[-1].is_loop
             and not indexing.has_rindex()
+            and not indexing.has_rmask()
         ):
-            # can lift a common load outside of reduction loop
-            # One exception is when this is an indirect_load.
+            # Lift loads whose address and mask are available outside the loop.
             return self.body

-        if not self.inside_reduction or (not indexing.has_rmask() and not has_rindex):
+        # Only CSE values emitted outside the loop remain valid after it closes.
+        if not self.inside_reduction or load_buffer is self.body:
             self.outside_loop_vars.add(result_var)

Why it needed manual resolution

Both files conflicted, because release/2.14 predates #184287 — the PR whose narrowed-load work exposed this bug.

torch/_inductor/codegen/triton.py — upstream also deletes the reduction_axes_omitted bookkeeping that #184287 introduced. That code is not on this branch, so the deletion is already satisfied and the surrounding lines are left as they are. Both substantive changes apply unchanged, and load_buffer is already in scope at the fix site (assigned from get_load_buffer earlier in load).

test/inductor/test_indexing.py — upstream adds the regression test to ReductionInvariantIndexingTests, a class #184287 introduced that does not exist here. Rather than importing that whole class (whose other tests cover a feature this branch does not have), the regression test test_loop_epilogue_indirect_load_scope is ported verbatim into a new TestLoopLocalLoadCSELifetime class. Its helpers all exist on this branch: run_and_get_kernels (including the remove_quote kwarg), FileCheck, HAS_CUDA_AND_TRITON and torch._inductor.config, added to the imports. The unrelated @parametrize("dense_indexing") hunk in the same upstream diff is dropped, since it parametrizes a #184287 test.

Net result against release/2.14: triton.py +4/-3, test_indexing.py +48/-2.

Test plan

python test/inductor/test_indexing.py TestLoopLocalLoadCSELifetime

I could not execute it here — this checkout has no built PyTorch and the test requires CUDA and Triton — so it needs a run on CI or a GPU host before merge. Both files were syntax-checked, and the ported test is byte-identical to the upstream one apart from the class it lives in.

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


AI-Assisted: the conflict resolution and the test port were done with Claude Code. Author reviewed and validated all AI-generated content, per AI_POLICY.md.

Manual cherry-pick of 60598ed (pytorch#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 pytorch#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 pytorch#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 pytorch#184287 test.

Fixes pytorch#194490 on the release branch.
@pytorch-bot

pytorch-bot Bot commented Aug 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure

As of commit f02a9f9 with merge base 73db3ed (image):

GREEN LIGHT - PR requires human review:

The triton.py fix itself is sound and conservative in both directions: adding
`not indexing.has_rmask()` at get_load_buffer (triton.py:4516) only narrows
which loads are lifted to self.body, and switching the lifetime guard to
`load_buffer is self.body` (triton.py:5004) only shrinks outside_loop_vars,
whose sole consumer is self.cse.invalidate at triton.py:6714 inside the
non-persistent reduction branch, so over-invalidation costs recomputation rather
than correctness. The blocker is verification of the ported regression test: the
PR body states the bug was exposed by #184287, which is NOT on release/2.14
(`reduction_axes_omitted` is absent from the tree), yet
test_loop_epilogue_indirect_load_scope asserts an exact codegen shape via
FileCheck (indirect `tl.load` inside `for r0_offset in tl.range`, then
`tl.store`, then a second indirect load) — whether that pattern reproduces the
bug on this older codegen path is undetermined, and the author explicitly states
the CUDA+Triton test was never executed and "needs a run on CI or a GPU host
before merge". As a manually conflict-resolved cherry-pick onto a release branch
that re-homes the upstream test into a new TestLoopLocalLoadCSELifetime class
and drops one upstream hunk, this warrants a human release-manager glance; the
earlier flagged usort import-ordering issue is resolved at this head
(test_indexing.py:12,26).

reason: insufficient_tests
Reviewed commit: f02a9f9

Inference job

NEW FAILURE - The following job has failed:

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

@pytorch-bot

pytorch-bot Bot commented Aug 26, 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.

@atalman
atalman merged commit 99ecebc into pytorch:release/2.14 Aug 26, 2026
210 of 211 checks passed
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
…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
…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants