Skip to content

Prioritize write→read deps in loop reordering candidate selection - #179090

Closed
eellison wants to merge 8 commits into
gh/eellison/990/basefrom
gh/eellison/990/head
Closed

eellison wants to merge 8 commits into
gh/eellison/990/basefrom
gh/eellison/990/head

Conversation

@eellison

@eellison eellison commented Apr 1, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion.

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation.

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.

Authored with Claude.

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

_try_reorder_loops_for_candidates picks the largest common buffer to
guide loop reordering. When a shared read (e.g., the input tensor) is
larger than a write→read dep (e.g., the reduction output), it gets
picked. If the shared read has a num_vars mismatch that normalizes
away, the function returns a positive score that short-circuits the
reindexing fallback in score_fusion_can_reorder_v2 — even though the
write→read dep has a factorization mismatch that only reindexing can
fix.

This manifests in FP8 grouped quantization patterns where reshape
changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The
reduction and pointwise share the input tensor (large, normalizes
to match) but the reduction output (small, different factorization)
blocks can_fuse_vertical.

Two changes to _try_reorder_loops_for_candidates:

1. Prefer write→read deps over shared reads when selecting the
   candidate to guide reordering, since write→read deps determine
   whether can_fuse_vertical will pass.

2. When a write→read dep fails normalize_with_stride_order, check
   whether the write's dimension sizes appear in the read's sizes.
   If not, the mismatch is a factorization issue (not a dimension
   order issue) that reordering cannot fix — return -1 to let
   the reindexing fallback handle it.

Authored with Claude.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Apr 1, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ No Failures

As of commit 13e7be6 with merge base 58e3fb0 (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 commented Apr 1, 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.

eellison added 2 commits April 2, 2026 08:36
…lection"

_try_reorder_loops_for_candidates picks the largest common buffer to
guide loop reordering. When a shared read (e.g., the input tensor) is
larger than a write→read dep (e.g., the reduction output), it gets
picked. If the shared read has a num_vars mismatch that normalizes
away, the function returns a positive score that short-circuits the
reindexing fallback in score_fusion_can_reorder_v2 — even though the
write→read dep has a factorization mismatch that only reindexing can
fix.

This manifests in FP8 grouped quantization patterns where reshape
changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The
reduction and pointwise share the input tensor (large, normalizes
to match) but the reduction output (small, different factorization)
blocks can_fuse_vertical.

Two changes to _try_reorder_loops_for_candidates:

1. Prefer write→read deps over shared reads when selecting the
   candidate to guide reordering, since write→read deps determine
   whether can_fuse_vertical will pass.

2. When a write→read dep fails normalize_with_stride_order, check
   whether the write's dimension sizes appear in the read's sizes.
   If not, the mismatch is a factorization issue (not a dimension
   order issue) that reordering cannot fix — return -1 to let
   the reindexing fallback handle it.

Authored with Claude.

[ghstack-poisoned]
…lection"


_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion. 

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation. 

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.


Authored with Claude.

[ghstack-poisoned]
@eellison
eellison requested a review from shunting314 April 2, 2026 17:32
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
…lection"


_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion. 

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation. 

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.


Authored with Claude.

[ghstack-poisoned]
@eellison eellison added the topic: not user facing topic category label Apr 14, 2026
…lection"


_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion. 

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation. 

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.


Authored with Claude.

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

[ghstack-poisoned]
@eellison eellison added the ciflow/trunk Trigger trunk jobs on your pull request label Apr 15, 2026
@eellison

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

pytorchmergebot pushed a commit that referenced this pull request Apr 15, 2026
Pointwise cat evaluates every input's computation for each output element (masked), so fusing reductions in is normally wasteful. When cat inputs just recombine a reduction's output (e.g. qknorm → RoPE → cat), no computation is duplicated — exclude that specific reduction from the guard so pointwise cat can enable fusion.

Authored with Claude.

Pull Request resolved: #179091
Approved by: https://github.com/PaulZhang12
ghstack dependencies: #176927, #179090
@github-actions
github-actions Bot deleted the gh/eellison/990/head branch May 16, 2026 02:27
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…torch#179090)

_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion.

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation.

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.

Authored with Claude.

Pull Request resolved: pytorch#179090
Approved by: https://github.com/shunting314
ghstack dependencies: pytorch#176927
aperson30 pushed a commit to aperson30/pytorch that referenced this pull request Jun 20, 2026
…9091)

Pointwise cat evaluates every input's computation for each output element (masked), so fusing reductions in is normally wasteful. When cat inputs just recombine a reduction's output (e.g. qknorm → RoPE → cat), no computation is duplicated — exclude that specific reduction from the guard so pointwise cat can enable fusion.

Authored with Claude.

Pull Request resolved: pytorch#179091
Approved by: https://github.com/PaulZhang12
ghstack dependencies: pytorch#176927, pytorch#179090
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…torch#179090)

_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion.

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation.

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.

Authored with Claude.

Pull Request resolved: pytorch#179090
Approved by: https://github.com/shunting314
ghstack dependencies: pytorch#176927
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
…9091)

Pointwise cat evaluates every input's computation for each output element (masked), so fusing reductions in is normally wasteful. When cat inputs just recombine a reduction's output (e.g. qknorm → RoPE → cat), no computation is duplicated — exclude that specific reduction from the guard so pointwise cat can enable fusion.

Authored with Claude.

Pull Request resolved: pytorch#179091
Approved by: https://github.com/PaulZhang12
ghstack dependencies: pytorch#176927, pytorch#179090
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…torch#179090)

_try_reorder_loops_for_candidates picks the largest common buffer to guide loop reordering. When a shared read (e.g., an input to both kernels) is larger than a write→read dep, it gets picked. Instead, we should prioritize based on the dependencies that are blocking fusion.

This pr prioritizes those dependencies, and detects when loop re-ordering will not be able to successfully fix a failed fusion. In this case we do not reorder the node’s loops. Loop ordering after fusion was interfering with loop reindexing. Potential other approaches would be either saving a pre-modified node prior to loop reordering, or, finding a way to reverse the transformation.

This fixes an fp8 grouped quantization fusion where reshape changes the iteration domain (e.g., [8, 7168] vs [448, 128]). The reduction and pointwise share the input tensor (large, normalizes to match) but the reduction output (small, different factorization) blocks can_fuse_vertical.

Authored with Claude.

Pull Request resolved: pytorch#179090
Approved by: https://github.com/shunting314
ghstack dependencies: pytorch#176927
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
…9091)

Pointwise cat evaluates every input's computation for each output element (masked), so fusing reductions in is normally wasteful. When cat inputs just recombine a reduction's output (e.g. qknorm → RoPE → cat), no computation is duplicated — exclude that specific reduction from the guard so pointwise cat can enable fusion.

Authored with Claude.

Pull Request resolved: pytorch#179091
Approved by: https://github.com/PaulZhang12
ghstack dependencies: pytorch#176927, pytorch#179090
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.

3 participants