Skip to content

[inductor] Delete the post-fusion reindex retry - #193158

Closed
anijain2305 wants to merge 18 commits into
gh/anijain2305/1303/basefrom
gh/anijain2305/1303/head
Closed

anijain2305 wants to merge 18 commits into
gh/anijain2305/1303/basefrom
gh/anijain2305/1303/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 12, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

can_fuse had a second chance for vertical fusion: when the checks failed, it
reindexed the pointwise to the reduction's iteration domain and ran them again.
This PR originally kept that retry and added a pre-check to skip it when graph
topology already ruled the fusion out, because most retries were wasted - 560 of
576 on one model's backward graph.

Per review, the retry itself is no longer needed: the case it was added for
(#183521) is handled by the original loop-transform path, and that PR's own
tests, test_floordiv_broadcast_vertical_fusion and
test_floordiv_broadcast_with_preceding_reduction, pass without it. So delete the
retry rather than optimize it, and with it the pre-check this PR had added.

Note what stays. _try_reindex_pointwise_for_reduction is still live: it is called
from shared_data_after_reordering_loop, which is the loop-transform path the
review suggested handling this in. config.loop_reindexing_after_fusion still
gates that call and the score threshold above it. Only the retry in can_fuse and
its pre-check go away.

No compile-time claim this time. The workload the earlier numbers came from is
nondeterministic - it fabricates random inputs, and three runs of identical
code produced 96, 100 and 101 triton kernels in 46.24s, 45.76s and 48.40s - so a
difference of a second or two between the two variants cannot be resolved on it,
and the measurements I took disagreed with each other. The justification here is
that the code path is unnecessary, not that removing it is faster.

Test plan:

python test/inductor/test_loop_ordering.py

124 tests, OK, and the failure set is identical to a run with the retry still in
place. Both tests from #183521 pass individually.

This PR was authored with the assistance of an AI coding agent.

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 12, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❌ 1 New Failure, 5 Unrelated Failures

As of commit 8d79a45 with merge base 557649f (image):

NEW FAILURE - The following job has failed:

BROKEN TRUNK - The following jobs failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

CRCR (non-blocking) - The following CRCR downstream CI jobs failed but are non-blocking:

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

anijain2305 added a commit that referenced this pull request Aug 12, 2026
…es out

When vertical fusion fails, _can_fuse reindexes the pointwise onto the
reduction's iteration domain and retries. Reindexing rewrites index
expressions, so it can only rescue a fusion that failed because reads and
writes of the same buffer were indexed differently. If node2 depends on a
buffer node1 never writes, and that buffer is produced between the two nodes,
the fusion is illegal whatever the indexing, and the retry rejects it again
with "intermediate nodes between node1 & node2".

Check that first. Buffer names and the ancestor graph do not change under loop
mutation, which is what makes the answer stable across a reindex, so the
pre-check is a necessary condition: when it says no, the retried
can_fuse_vertical would have said no too.

Also roll the reindex back when the retried checks still reject the pair.
_try_reindex_pointwise_for_reduction rolls back its own failures, but a
rejection that happens after it returns left the pointwise permanently
reindexed on behalf of a fusion that never happened.

Measured on the backward graph of FairChem UMA, captured as a standalone
fx_graph_runnable:

  reindex attempts        1888 -> 247
  apply_loop_reindexing    613 -> 1
  possible fusions         107 -> 107   (unchanged)
  generated kernels         70 -> 70    (identical content hashes)

Of the 576 reindexes that graph applied and kept, 560 were rejected afterwards
for the topology reason above and none of the 414 distinct pairs involved ever
fused.

Being honest about the payoff: on that standalone graph this is worth ~10% of
the compile, but on the same model compiled end to end it is worth nothing
measurable (49.16s vs 49.34s over two interleaved pairs). The pre-check does
fire there - 2238 calls, 2065 skips - but in situ those attempts were already
exiting early, before the expensive part. fx_graph_runnable compiles the
post-grad graph against a fresh ShapeEnv, without the replacements and ranges
the real compile has, so statically_known_equals inside the reindex path stops
rejecting early and the repro drives the expensive path far more often than
production does. The change is kept because skipping work whose outcome is
already determined is right on its own terms, not because it made this model
faster.

Test plan:

```
python test/inductor/test_perf.py
python test/inductor/test_loop_ordering.py
python test/inductor/test_fused_attention.py
```

test_perf (which asserts memory-traffic expectations and would catch a lost
fusion) matches baseline exactly. test_loop_ordering has one pre-existing
failure with and without the change. test_fused_attention's sdpa_rewriter
failures are order-dependent flakes: a different test fails on each run, and
all of them pass when run individually with and without the change.

This PR was authored with the assistance of an AI coding agent.


ghstack-source-id: 12954a4
Pull-Request: #193158
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@anijain2305

Copy link
Copy Markdown
Contributor Author

@eellison ping for review.

[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 20, 2026
can_fuse had a second chance for vertical fusion: when the checks failed, it
reindexed the pointwise to the reduction's iteration domain and ran them again.
This PR originally kept that retry and added a pre-check to skip it when graph
topology already ruled the fusion out, because most retries were wasted - 560 of
576 on one model's backward graph.

Per review, the retry itself is no longer needed: the case it was added for
(#183521) is handled by the original loop-transform path, and that PR's own
tests, test_floordiv_broadcast_vertical_fusion and
test_floordiv_broadcast_with_preceding_reduction, pass without it. So delete the
retry rather than optimize it, and with it the pre-check this PR had added.

Note what stays. _try_reindex_pointwise_for_reduction is still live: it is called
from shared_data_after_reordering_loop, which is the loop-transform path the
review suggested handling this in. config.loop_reindexing_after_fusion still
gates that call and the score threshold above it. Only the retry in can_fuse and
its pre-check go away.

No compile-time claim this time. The workload the earlier numbers came from is
nondeterministic - it fabricates random inputs, and three runs of *identical*
code produced 96, 100 and 101 triton kernels in 46.24s, 45.76s and 48.40s - so a
difference of a second or two between the two variants cannot be resolved on it,
and the measurements I took disagreed with each other. The justification here is
that the code path is unnecessary, not that removing it is faster.

Test plan:

```
python test/inductor/test_loop_ordering.py
```

124 tests, OK, and the failure set is identical to a run with the retry still in
place. Both tests from #183521 pass individually.

This PR was authored with the assistance of an AI coding agent.

ghstack-source-id: 8b4573a
Pull-Request: #193158
@anijain2305 anijain2305 changed the title [inductor] Don't reindex for a fusion that graph topology already rules out [inductor] Delete the post-fusion reindex retry Aug 20, 2026
anijain2305 added a commit that referenced this pull request Aug 20, 2026
can_fuse had a second chance for vertical fusion: when the checks failed, it
reindexed the pointwise to the reduction's iteration domain and ran them again.
This PR originally kept that retry and added a pre-check to skip it when graph
topology already ruled the fusion out, because most retries were wasted - 560 of
576 on one model's backward graph.

Per review, the retry itself is no longer needed: the case it was added for
(#183521) is handled by the original loop-transform path, and that PR's own
tests, test_floordiv_broadcast_vertical_fusion and
test_floordiv_broadcast_with_preceding_reduction, pass without it. So delete the
retry rather than optimize it, and with it the pre-check this PR had added.

Note what stays. _try_reindex_pointwise_for_reduction is still live: it is called
from shared_data_after_reordering_loop, which is the loop-transform path the
review suggested handling this in. config.loop_reindexing_after_fusion still
gates that call and the score threshold above it. Only the retry in can_fuse and
its pre-check go away.

No compile-time claim this time. The workload the earlier numbers came from is
nondeterministic - it fabricates random inputs, and three runs of *identical*
code produced 96, 100 and 101 triton kernels in 46.24s, 45.76s and 48.40s - so a
difference of a second or two between the two variants cannot be resolved on it,
and the measurements I took disagreed with each other. The justification here is
that the code path is unnecessary, not that removing it is faster.

Test plan:

```
python test/inductor/test_loop_ordering.py
```

124 tests, OK, and the failure set is identical to a run with the retry still in
place. Both tests from #183521 pass individually.

This PR was authored with the assistance of an AI coding agent.

ghstack-source-id: 8b4573a
Pull-Request: #193158
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 20, 2026
can_fuse had a second chance for vertical fusion: when the checks failed, it
reindexed the pointwise to the reduction's iteration domain and ran them again.
This PR originally kept that retry and added a pre-check to skip it when graph
topology already ruled the fusion out, because most retries were wasted - 560 of
576 on one model's backward graph.

Per review, the retry itself is no longer needed: the case it was added for
(#183521) is handled by the original loop-transform path, and that PR's own
tests, test_floordiv_broadcast_vertical_fusion and
test_floordiv_broadcast_with_preceding_reduction, pass without it. So delete the
retry rather than optimize it, and with it the pre-check this PR had added.

Note what stays. _try_reindex_pointwise_for_reduction is still live: it is called
from shared_data_after_reordering_loop, which is the loop-transform path the
review suggested handling this in. config.loop_reindexing_after_fusion still
gates that call and the score threshold above it. Only the retry in can_fuse and
its pre-check go away.

No compile-time claim this time. The workload the earlier numbers came from is
nondeterministic - it fabricates random inputs, and three runs of *identical*
code produced 96, 100 and 101 triton kernels in 46.24s, 45.76s and 48.40s - so a
difference of a second or two between the two variants cannot be resolved on it,
and the measurements I took disagreed with each other. The justification here is
that the code path is unnecessary, not that removing it is faster.

Test plan:

```
python test/inductor/test_loop_ordering.py
```

124 tests, OK, and the failure set is identical to a run with the retry still in
place. Both tests from #183521 pass individually.

This PR was authored with the assistance of an AI coding agent.

ghstack-source-id: f8e9fdc
Pull-Request: #193158
@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@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

@nWEIdia

nWEIdia commented Aug 21, 2026 •

Copy link
Copy Markdown
Collaborator

Hi @anijain2305 ,
FYI our internal setup has detected and bisected a unit test failure to this PR. Please help review the following Claude analysis.

Platform detected: GB200
Unit Test Failure: pytest -v test/inductor/test_cuda_repro.py -k test_persistent_reduction_cse_reindexed_epilogue_use_block_ptr_True_dynamic_batch_False

Summary
  
  The bisect identifies e39e7385c20 — "[inductor] Delete the post-fusion reindex retry (#193158)" (PR #193158
  (https://github.com/pytorch/pytorch/pull/193158)) as the commit that caused the test failure.
  
  What it does: It removes the "post-fusion reindex retry" logic from Scheduler.can_fuse in torch/_inductor/scheduler.py. This retry used to reindex a
  pointwise node to a reduction's iteration domain when vertical fusion initially failed, giving fusion a second chance.
  
  Why it breaks the test: The test test_persistent_reduction_cse_reindexed_epilogue expects that the persistent reduction kernel fuses the epilogue
  pointwise operations with the reduction, resulting in only 2 tl.load(tl.make_block_ptr(in_ptr0 loads (one for gate, one for up). Without the reindex
  retry, the fusion doesn't happen for the epilogue, causing separate loads to be emitted (4 instead of 2) — the CSE deduplication relies on the
  loads being in the same fused kernel.

My manual run seems to confirm the pass/fail observation. Perhaps the test needs to be updated?

Thank you!

cc @atalman @ptrblck @eqy @tinglvv

@anijain2305

anijain2305 commented Aug 25, 2026 •

Copy link
Copy Markdown
Contributor Author

@nWEIdia taking a look. Would CI have caught this issue if we used b200 label to trigger tests on GB200 machines?

cc @ngimel

@anijain2305

Copy link
Copy Markdown
Contributor Author

@pytorchbot revert -m "b200 regression" -c nosignal

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@pytorchbot successfully started a revert job. Check the current status here.
Questions? Feedback? Please reach out to the PyTorch DevX Team

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

@anijain2305 your PR has been successfully reverted.

@pytorchmergebot pytorchmergebot added Reverted ci-no-td Do not run TD on this PR labels Aug 25, 2026
@nWEIdia

nWEIdia commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@nWEIdia taking a look. Would CI have caught this issue if we used b200 label to trigger tests on GB200 machines?

cc @ngimel
Hi @anijain2305 , thanks for taking a look, I was thinking perhaps a test update forward fix would be sufficient.

To give you more information on the status of the B200 and GB200:
In current CI setting, you do not need to add manual labels, as ciflow/trunk would run smoke tests on B200 (x86 CPU + B200). Please look for cu132sm100* smoke test job status before landing. As I said this only runs smoke tests and did not cover the failure exposed in here.
We are still working on the availability of GB200 systems. The way we discovered the new failures was due to simply NVIDIA internal setup, so for a while we may need to continue rely on this one.

We can consider getting other projects who are sharing x86+B200 systems to move away, and try testing full on B200, but since the test runs too long, I am not sure if existing capacity would be enough.

So TLDR: likely the failure is still only available via NVIDIA internal testing post landing.
When it happens, forward fixing is acceptable too, since this is missing signal.

@ngimel

ngimel commented Aug 25, 2026

Copy link
Copy Markdown
Collaborator

@claude is the test failure blackwell-specific? From the description "Why it breaks the test: The test test_persistent_reduction_cse_reindexed_epilogue expects that the persistent reduction kernel fuses the epilogue
pointwise operations with the reduction, resulting in only 2 tl.load(tl.make_block_ptr(in_ptr0 loads (one for gate, one for up). Without the reindex
retry, the fusion doesn't happen for the epilogue, causing separate loads to be emitted (4 instead of 2) — the CSE deduplication relies on the
loads being in the same fused kernel." it doesn't seem like it would depend on the hardware

@claude

claude Bot commented Aug 25, 2026 •

Copy link
Copy Markdown

Claude finished @ngimel's task in 27m 1s —— View job


Short answer: I agree with you — nothing in what this test asserts looks Blackwell-specific.

The assertions are on the generated Triton source (load counts, one persistent_reduction), and I traced every device-conditional knob that could reach that codegen path. None of them fire under this test's config. The more likely reason PyTorch CI stayed green is a Triton-version-gated skip, not the GPU.

Details: why the assertions should be arch-independent, and what I think actually hid the signal

What the test asserts

test/inductor/test_cuda_repro.py:2368-2439, under @config.patch({triton.multi_kernel: 0, triton.tile_reductions: True, triton.prefer_nd_tiling: False, triton.max_tiles: 3, split_reductions: False}):

  • exactly 1 @triton_heuristics.persistent_reduction
  • use_block_ptr=True -> exactly 2 tl.load(tl.make_block_ptr(in_ptr0
  • use_block_ptr=False -> exactly 2 tl.load(in_ptr0 +
  • <= 1 libdevice.exp

All of these are string counts over emitted source, so they depend on scheduling/codegen decisions, not on the runtime launch config.

Device-conditional code in that path, and why each is inert here

Knob Location Why it can't explain an arch difference
SM-count-driven reduction splitting, including the explicit GB200 branch props.major >= 10 -> 32768/40960 torch/_inductor/choices.py:515-536, reduction_split_factor at :538 the test patches split_reductions=False, so inner_reduction_splits is the constant-1 stub (torch/_inductor/ir.py:1540-1548) and reduction_split_factor is never consulted
numel_hint >= num_sm * 2 * 32 -> ReductionHint.DEFAULT torch/_inductor/ir.py:1583-1587 numel_hint here is 8 (x.shape[0]=4, 2 groups); the threshold is in the thousands on every SKU from A10G to GB200, so the comparison has the same answer everywhere
TMA / tensor-descriptor codegen, gated cc >= 9 torch/_inductor/codegen/triton.py:2942-2953 requires config.triton.use_tensor_descriptor, which defaults False (config.py:2227) and the test doesn't set it
PDL codegen, gated cc >= 9 torch/_inductor/codegen/triton.py:4657-4669 requires triton.enable_pdl, default "0" (config.py:2291)
mix-order-reduction split size from multi_processor_count torch/_inductor/codegen/simd.py:2827-2846 different fusion shape (two reductions over opposite dims of one input); also only affects split size, not load count
block-pointer codegen use_block_ptr_enabled(), torch/_inductor/codegen/triton_utils.py:46-58 gated purely on whether the installed Triton exposes the API — no hardware check

should_use_persistent_reduction (choices.py:448-513) is a pure function of SIMDKernelFeatures plus config. And the fusion decision the revert restores — the can_fuse retry that called _try_reindex_pointwise_for_reduction — has no device predicate beyond is_cpu() (scheduler.py:8301-8302).

So the mechanism in the bisect writeup ("without the reindex retry the epilogue doesn't fuse, so loads aren't CSE'd") should reproduce on any CUDA device where the test actually executes.

My hypothesis for the missing PyTorch CI signal

The failing case is use_block_ptr_True, and that subtest carries decorators=[requires_block_ptr] (test_cuda_repro.py:2354-2357), which is unittest.skipUnless(has_triton_block_ptr(), ...) (torch/testing/_internal/inductor_utils.py:183-185). has_triton_block_ptr() requires both tl.make_block_ptr and tl.advance (torch/utils/_triton.py:47-59), because triton-lang/triton#10833 removed block pointers and left make_block_ptr as a raising stub. config.py:2205-2213 says the same and carries TODO(#191012): remove use_block_ptr entirely.

If PyTorch's pinned Triton (.github/ci_commit_pins/triton.txt -> 3f6e4113) has block pointers removed, then use_block_ptr_True is skipped in PyTorch CI and only runs where a Triton build still has the API — which would make this a Triton-configuration difference that happens to correlate with the internal GB200 setup, not an arch difference.

The decisive check (I can't run it — no GPU in this environment)

On the internal GB200 box, with the revert reapplied (i.e. retry deleted), run both parametrizations:

pytest -v test/inductor/test_cuda_repro.py -k test_persistent_reduction_cse_reindexed_epilogue

Then read the skip/fail split:

  • use_block_ptr_False also fails -> the fusion regression is real and generic; the fix belongs in the fusion path (or the test's expectation), not in an arch skip. Worth also checking whether use_block_ptr_True shows up as SKIPPED on an sm86/sm89 CI runner, which would confirm the coverage gap is the Triton pin.
  • use_block_ptr_False passes and only True fails -> the divergence is inside the block-pointer codegen path (which [inductor] Block-pointer codegen breaks on Triton that removed tl.make_block_ptr #191012 is slated to delete), and it's a Triton-API question rather than a hardware one.

Either way, I would not label this Blackwell-specific on the current evidence.

| branch: `gh/anijain2305/1303/head`

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
can_fuse had a second chance for vertical fusion: when the checks failed, it
reindexed the pointwise to the reduction's iteration domain and ran them again.
This PR originally kept that retry and added a pre-check to skip it when graph
topology already ruled the fusion out, because most retries were wasted - 560 of
576 on one model's backward graph.

Per review, the retry itself is no longer needed: the case it was added for
(pytorch#183521) is handled by the original loop-transform path, and that PR's own
tests, test_floordiv_broadcast_vertical_fusion and
test_floordiv_broadcast_with_preceding_reduction, pass without it. So delete the
retry rather than optimize it, and with it the pre-check this PR had added.

Note what stays. _try_reindex_pointwise_for_reduction is still live: it is called
from shared_data_after_reordering_loop, which is the loop-transform path the
review suggested handling this in. config.loop_reindexing_after_fusion still
gates that call and the score threshold above it. Only the retry in can_fuse and
its pre-check go away.

No compile-time claim this time. The workload the earlier numbers came from is
nondeterministic - it fabricates random inputs, and three runs of *identical*
code produced 96, 100 and 101 triton kernels in 46.24s, 45.76s and 48.40s - so a
difference of a second or two between the two variants cannot be resolved on it,
and the measurements I took disagreed with each other. The justification here is
that the code path is unnecessary, not that removing it is faster.

Test plan:

```
python test/inductor/test_loop_ordering.py
```

124 tests, OK, and the failure set is identical to a run with the retry still in
place. Both tests from pytorch#183521 pass individually.

This PR was authored with the assistance of an AI coding agent.

Pull Request resolved: pytorch#193158
Approved by: https://github.com/eellison
orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
@anijain2305

Copy link
Copy Markdown
Contributor Author

Closing because this does prevent one fusion. And the compile time benefits have lowered as other PRs have been merged.

@anijain2305 anijain2305 closed this Sep 2, 2026
@github-actions
github-actions Bot deleted the gh/anijain2305/1303/head branch November 7, 2026 01:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci-no-td Do not run TD on this PR ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor Reverted topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants