Repository navigation
Conversation
When a pointwise and reduction operate on the same data but with different loop decompositions (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Authored with Claude. [ghstack-poisoned]
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/176927
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 8f82588 with merge base 58e3fb0 ( This comment was automatically generated by Dr. CI and updates every 15 minutes. |
This PR needs a
|
When a pointwise and reduction operate on the same data but with different loop decompositions (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Authored with Claude. ghstack-source-id: 22a7ca9 Pull Request resolved: pytorch/pytorch#176927
…ductions" When a pointwise and reduction operate on the same data but with different loop decompositions (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). 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]
…ductions" When a pointwise and reduction operate on the same data but with different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). 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]
|
@claude review this PR and keep in mind potential issues and edge cases |
PaulZhang12
left a comment
There was a problem hiding this comment.
Generally LGTM, do we want a dashboard run if we turn this on by default?
|
Claude finished @PaulZhang12's task in 16m 7s —— View job PR Review: Reindex pointwise iteration loops to enable fusion with reductions
SummaryThis PR adds a loop reindexing pass that re-expresses a pointwise node's iteration variables (via The approach is sound and well-motivated. Below are the issues and edge cases I identified, ranging from a real correctness concern to minor suggestions. Issue 1: Irreversible mutation before fusion is confirmed (the big one)
The TODO comment on line 5697 correctly identifies the core problem:
If any of these checks reject the fusion, the pointwise node (and its sub-nodes if a This is not unique to this PR — the existing Recommended mitigation: Consider snapshotting Issue 2:
|
|
I will do a perf run. ive seen a couple patterns where this allows fusions already. but i agree it would be nice to be more certain that when we reindex, we will fuse. |
shunting314
left a comment
There was a problem hiding this comment.
How does it work with an non-contiguous input for the pointwise nodes? It may not be a valid setup since the view op on the non-contiguous input may fail in some cases
|
An interesting alternative is: instead of reindexing, we can try to merge/flatten loops of the pointwise node |
…ductions" When a pointwise and reduction operate on the same data but with different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Motivation is to fuse qknorm. 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]
|
@pytorchbot merge |
…ductions" When a pointwise and reduction operate on the same data but with different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Motivation is to fuse qknorm. 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]
|
@pytorchbot merge |
Merge startedYour 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 |
…79090) _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: #179090 Approved by: https://github.com/shunting314 ghstack dependencies: #176927
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
…uction-broadcast fusion (#183521) Edited by claude. ## Motivation While profiling Llama4-Maverick-17B-128E with TorchAO MXFP8 quantization, every RMSNorm + block-wise MXFP8 quant layer produces **3 kernels instead of 2**. The root cause is a `FloorDiv` index mismatch in vertical fusion: the block-amax reduction writes `buf[256*p0 + p1]` (`p1 ∈ [0, 256)`) but the fp8-quantize pointwise reads `buf[256*p0 + (p1 // 32)]` (`p1 ∈ [0, 8192)`). `fusable_read_and_write` in `can_fuse_vertical` rejects this with `"memory deps did not match"`. The existing `_try_reindex_pointwise_for_reduction` (from #176927) can resolve this mismatch by rewriting the pointwise's iteration domain to match the reduction's. However, it currently only runs inside `shared_data_after_reordering_loop` (the scoring/loop-ordering path) — by then, `can_fuse_vertical` has already rejected the pair. This affects any block-wise quantization pattern (MXFP8, MXFP4, etc.) where a per-block scale is broadcast back to element granularity via `unsqueeze`. Fixes #183542 ## Solution In `can_fuse` (the top-level fusion entry point), when vertical fusion fails, attempt `_try_reindex_pointwise_for_reduction` as a fallback before giving up. If reindexing succeeds, retry the full `can_fuse_vertical` chain: ```python if node1.get_operation_names() & node2.ancestors: # Try vertical fusion normally first if ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ): return True # Vertical fusion failed — try reindexing pointwise to match # reduction's iteration domain (e.g. FloorDiv broadcast) if ( config.loop_reindexing_after_fusion and self._try_reindex_pointwise_for_reduction(node1, node2) ): return ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ) return False ``` `_try_reindex_pointwise_for_reduction` has a built-in rollback — if reindexing doesn't improve shared deps it restores the original node state, so this is safe to call speculatively. Gated behind `config.loop_reindexing_after_fusion` (enabled by default). ## Test plan Two new tests in `test/inductor/test_loop_ordering.py`: - **`test_floordiv_broadcast_vertical_fusion`**: Standalone block reduction + FloorDiv broadcast pointwise → expects **1 kernel** (was 2). - **`test_floordiv_broadcast_with_preceding_reduction`**: RMSNorm + block-wise quantization → expects **2 kernels** (was 3). | Pattern | Before | After | |---------|--------|-------| | Block reduction + broadcast pointwise | 2 kernels | 1 kernel | | RMSNorm + block-wise quantization | 3 kernels | 2 kernels | Pull Request resolved: #183521 Approved by: https://github.com/jansel, https://github.com/eellison
…uction-broadcast fusion (pytorch#183521) Edited by claude. ## Motivation While profiling Llama4-Maverick-17B-128E with TorchAO MXFP8 quantization, every RMSNorm + block-wise MXFP8 quant layer produces **3 kernels instead of 2**. The root cause is a `FloorDiv` index mismatch in vertical fusion: the block-amax reduction writes `buf[256*p0 + p1]` (`p1 ∈ [0, 256)`) but the fp8-quantize pointwise reads `buf[256*p0 + (p1 // 32)]` (`p1 ∈ [0, 8192)`). `fusable_read_and_write` in `can_fuse_vertical` rejects this with `"memory deps did not match"`. The existing `_try_reindex_pointwise_for_reduction` (from pytorch#176927) can resolve this mismatch by rewriting the pointwise's iteration domain to match the reduction's. However, it currently only runs inside `shared_data_after_reordering_loop` (the scoring/loop-ordering path) — by then, `can_fuse_vertical` has already rejected the pair. This affects any block-wise quantization pattern (MXFP8, MXFP4, etc.) where a per-block scale is broadcast back to element granularity via `unsqueeze`. Fixes pytorch#183542 ## Solution In `can_fuse` (the top-level fusion entry point), when vertical fusion fails, attempt `_try_reindex_pointwise_for_reduction` as a fallback before giving up. If reindexing succeeds, retry the full `can_fuse_vertical` chain: ```python if node1.get_operation_names() & node2.ancestors: # Try vertical fusion normally first if ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ): return True # Vertical fusion failed — try reindexing pointwise to match # reduction's iteration domain (e.g. FloorDiv broadcast) if ( config.loop_reindexing_after_fusion and self._try_reindex_pointwise_for_reduction(node1, node2) ): return ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ) return False ``` `_try_reindex_pointwise_for_reduction` has a built-in rollback — if reindexing doesn't improve shared deps it restores the original node state, so this is safe to call speculatively. Gated behind `config.loop_reindexing_after_fusion` (enabled by default). ## Test plan Two new tests in `test/inductor/test_loop_ordering.py`: - **`test_floordiv_broadcast_vertical_fusion`**: Standalone block reduction + FloorDiv broadcast pointwise → expects **1 kernel** (was 2). - **`test_floordiv_broadcast_with_preceding_reduction`**: RMSNorm + block-wise quantization → expects **2 kernels** (was 3). | Pattern | Before | After | |---------|--------|-------| | Block reduction + broadcast pointwise | 2 kernels | 1 kernel | | RMSNorm + block-wise quantization | 3 kernels | 2 kernels | Pull Request resolved: pytorch#183521 Approved by: https://github.com/jansel, https://github.com/eellison
…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
…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
…ytorch#176927) When a pointwise and reduction operate on the same data but with different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Motivation is to fuse qknorm. Authored with Claude. Pull Request resolved: pytorch#176927 Approved by: https://github.com/PaulZhang12, https://github.com/shunting314
…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
…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
…uction-broadcast fusion (pytorch#183521) Edited by claude. ## Motivation While profiling Llama4-Maverick-17B-128E with TorchAO MXFP8 quantization, every RMSNorm + block-wise MXFP8 quant layer produces **3 kernels instead of 2**. The root cause is a `FloorDiv` index mismatch in vertical fusion: the block-amax reduction writes `buf[256*p0 + p1]` (`p1 ∈ [0, 256)`) but the fp8-quantize pointwise reads `buf[256*p0 + (p1 // 32)]` (`p1 ∈ [0, 8192)`). `fusable_read_and_write` in `can_fuse_vertical` rejects this with `"memory deps did not match"`. The existing `_try_reindex_pointwise_for_reduction` (from pytorch#176927) can resolve this mismatch by rewriting the pointwise's iteration domain to match the reduction's. However, it currently only runs inside `shared_data_after_reordering_loop` (the scoring/loop-ordering path) — by then, `can_fuse_vertical` has already rejected the pair. This affects any block-wise quantization pattern (MXFP8, MXFP4, etc.) where a per-block scale is broadcast back to element granularity via `unsqueeze`. Fixes pytorch#183542 ## Solution In `can_fuse` (the top-level fusion entry point), when vertical fusion fails, attempt `_try_reindex_pointwise_for_reduction` as a fallback before giving up. If reindexing succeeds, retry the full `can_fuse_vertical` chain: ```python if node1.get_operation_names() & node2.ancestors: # Try vertical fusion normally first if ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ): return True # Vertical fusion failed — try reindexing pointwise to match # reduction's iteration domain (e.g. FloorDiv broadcast) if ( config.loop_reindexing_after_fusion and self._try_reindex_pointwise_for_reduction(node1, node2) ): return ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ) return False ``` `_try_reindex_pointwise_for_reduction` has a built-in rollback — if reindexing doesn't improve shared deps it restores the original node state, so this is safe to call speculatively. Gated behind `config.loop_reindexing_after_fusion` (enabled by default). ## Test plan Two new tests in `test/inductor/test_loop_ordering.py`: - **`test_floordiv_broadcast_vertical_fusion`**: Standalone block reduction + FloorDiv broadcast pointwise → expects **1 kernel** (was 2). - **`test_floordiv_broadcast_with_preceding_reduction`**: RMSNorm + block-wise quantization → expects **2 kernels** (was 3). | Pattern | Before | After | |---------|--------|-------| | Block reduction + broadcast pointwise | 2 kernels | 1 kernel | | RMSNorm + block-wise quantization | 3 kernels | 2 kernels | Pull Request resolved: pytorch#183521 Approved by: https://github.com/jansel, https://github.com/eellison
…ytorch#176927) When a pointwise and reduction operate on the same data but with different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim] after a reshape), they can't fuse because their MemoryDep index expressions differ. This adds loop reindexing: re-expressing a pointwise's iteration variables via FloorDiv/ModularIndexing of a flat index to match the reduction's factorization. Handles both single SchedulerNode and FusedSchedulerNode (multiple fused pointwise ops). Motivation is to fuse qknorm. Authored with Claude. Pull Request resolved: pytorch#176927 Approved by: https://github.com/PaulZhang12, https://github.com/shunting314
…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
…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
…uction-broadcast fusion (pytorch#183521) Edited by claude. ## Motivation While profiling Llama4-Maverick-17B-128E with TorchAO MXFP8 quantization, every RMSNorm + block-wise MXFP8 quant layer produces **3 kernels instead of 2**. The root cause is a `FloorDiv` index mismatch in vertical fusion: the block-amax reduction writes `buf[256*p0 + p1]` (`p1 ∈ [0, 256)`) but the fp8-quantize pointwise reads `buf[256*p0 + (p1 // 32)]` (`p1 ∈ [0, 8192)`). `fusable_read_and_write` in `can_fuse_vertical` rejects this with `"memory deps did not match"`. The existing `_try_reindex_pointwise_for_reduction` (from pytorch#176927) can resolve this mismatch by rewriting the pointwise's iteration domain to match the reduction's. However, it currently only runs inside `shared_data_after_reordering_loop` (the scoring/loop-ordering path) — by then, `can_fuse_vertical` has already rejected the pair. This affects any block-wise quantization pattern (MXFP8, MXFP4, etc.) where a per-block scale is broadcast back to element granularity via `unsqueeze`. Fixes pytorch#183542 ## Solution In `can_fuse` (the top-level fusion entry point), when vertical fusion fails, attempt `_try_reindex_pointwise_for_reduction` as a fallback before giving up. If reindexing succeeds, retry the full `can_fuse_vertical` chain: ```python if node1.get_operation_names() & node2.ancestors: # Try vertical fusion normally first if ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ): return True # Vertical fusion failed — try reindexing pointwise to match # reduction's iteration domain (e.g. FloorDiv broadcast) if ( config.loop_reindexing_after_fusion and self._try_reindex_pointwise_for_reduction(node1, node2) ): return ( self.can_fuse_vertical(node1, node2) and V.choices.can_fuse_vertical(...) and self.get_backend(device).can_fuse_vertical(...) ) return False ``` `_try_reindex_pointwise_for_reduction` has a built-in rollback — if reindexing doesn't improve shared deps it restores the original node state, so this is safe to call speculatively. Gated behind `config.loop_reindexing_after_fusion` (enabled by default). ## Test plan Two new tests in `test/inductor/test_loop_ordering.py`: - **`test_floordiv_broadcast_vertical_fusion`**: Standalone block reduction + FloorDiv broadcast pointwise → expects **1 kernel** (was 2). - **`test_floordiv_broadcast_with_preceding_reduction`**: RMSNorm + block-wise quantization → expects **2 kernels** (was 3). | Pattern | Before | After | |---------|--------|-------| | Block reduction + broadcast pointwise | 2 kernels | 1 kernel | | RMSNorm + block-wise quantization | 3 kernels | 2 kernels | Pull Request resolved: pytorch#183521 Approved by: https://github.com/jansel, https://github.com/eellison
Stack from ghstack (oldest at bottom):
When a pointwise and reduction operate on the same data but with
different iteration spaces (e.g., [M, N] vs [M*num_heads, head_dim]
after a reshape), they can't fuse because their MemoryDep index
expressions differ. This adds loop reindexing: re-expressing a
pointwise's iteration variables via FloorDiv/ModularIndexing of a flat
index to match the reduction's factorization.
Handles both single SchedulerNode and FusedSchedulerNode (multiple
fused pointwise ops).
Motivation is to fuse qknorm.
Authored with Claude.
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo