Repository navigation
Conversation
PR Status: Approved 🚀Please fix all CI failures and trigger merge by commenting "@pytorchbot merge". 🟡 AUTOMATED REVIEW - Changes requested:
Findings (10): Reviewed commit: 🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/200465
Note: Links to docs will display an error until the docs builds have been completed. ✅ No FailuresAs of commit 6229a86 with merge base 4fb332d ( 🟡 GREEN LIGHT - PR was reverted - re-landing requires human review:
Green Light will not review this PR again, on this or any later commit; re-landing it needs a human approval. This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@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 |
|
The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command |
Blocking findings addressed earlier in this stack (miscompile fix, backend gate, precision fix, test fixes); remaining items are coverage/perf follow-ups. Approved by human reviewer.
|
Starting merge as part of PR stack under #200466 |
There was a problem hiding this comment.
Automated review of 6229a86: changes requested before human review.
The PR lets a REDUCED-domain consumer in an outer-plus-grouped nested reduction read a fixed lane of the live group tile, recording lane relations on NestedReductionStage.lane_accesses, accepting them in the scheduler's dependency proof, and resolving them in codegen via _SubParentValueResolver. Recommendation: Request Changes. Three things block approval: the test/REVIEW.md violations that keep the new tests off XPU, no fbcode-default (loop_ordering_after_fusion off) test that can fail, and the implicit factor == local_reduction_size switch that changes codegen on untested existing paths.
🔴 3 blocking findings are attached to the code below.
⚪ 7 non-blocking findings
pytorch/torch/_inductor/scheduler.py
Line 10464 in 6229a86
Lane relations store a different kind of dep than sub-parent relations, which forces a parallel normalize-and-match path on every nested plan. _try_get_r_grouped_lane_accesses stores deps re-extracted with extract_read_writes(..., normalize=False), while _try_get_sub_parent_access_relations stores the scheduler's own node.read_writes deps, which are matched raw. As a result, MemoryDepMatch(raw_write.normalize(), raw_read.normalize()) in grouped_lane_matches runs uncached MemoryDep.normalize() (sympy.expand, sympy_subs, index_prevent_reordering) inside can_fuse. It does so for every nested-stage read that fails fusable_read_and_write, including in every pre-existing plan, where grouped_lane_matches is empty. Fix: map the relations back to scheduler-visible deps at plan time and fold them into sub_parent_access_pairs(). At minimum, guard the check with grouped_lane_matches and ....
pytorch/torch/_inductor/scheduler.py
Line 2345 in 6229a86
Docs and messages were not updated for the reuse of the sub-parent machinery. SubParentAccessRelation (:2515) still says "used for sub-parent register forwarding", and requires_live_source is undocumented. The sub_parent_access_pairs docstring (:2631) says it yields every proved pair, but it omits nested_stage.lane_accesses. The _try_get_r_grouped_lane_accesses docstring does not say that () means "accept, forward by position" as distinct from None, which matters because () is falsy. This if lane_accesses: return None has no comment but is load-bearing: codegen prefers the sub-parent stage's factor and relations (simd.py:3804-3809), so lane relations would otherwise be dropped silently. The _codegen_nested_grouped_schedule docstring does not mention value_resolver. "sub-parent stage lost required source" (simd.py:2852) can now be raised by lane-only plans that have no sub-parent stage.
pytorch/torch/_inductor/scheduler.py
Line 10841 in 6229a86
The why("nested reduction plan declined") log is misleading. It renders as "cannot fuse A with B: nested reduction plan declined", but control then falls through to ordinary fusion, which may still fuse the pair. Every other why() in can_fuse is followed by return False. Reword it, or log it with fusion_log.debug instead.
The helpers combine_inputs_asm and xor_words are imported from higher_order_ops.test_inline_asm_elementwise via sys.path.append, with no # @ manual=fbcode//... annotation. Other cross-target imports in test/inductor carry one (e.g. test_inductor_freezing.py:27, test_memory_planning.py:39). These helpers now have users in two test directories, so move them to torch/testing/_internal/ (e.g. inductor_utils.py).
pytorch/test/inductor/test_inductor_scheduler.py
Line 2179 in 6229a86
The mock tests do not cover every new condition, despite the description's claim that "each negative case fails if its condition is removed". In test_nested_grouped_lane_access_conditions, no case fails the source-side _sub_parent_access_preserves_x_boundary(source_writes\[0\], ...) check (scheduler.py:1361), since every writer splits cleanly into (4, 64); a (2, 128) writer would exercise it. A negative lane (the 0 <= bound) and an atomic read dep.mode are also untested. In test_nested_dependency_matches_require_planned_lane, no case reaches the new return None when _memory_dep_supports_index_equivalence fails for a planned lane (scheduler.py:10469-10470); a planned lane with an atomic or tmp-index write would. test_nested_plan_restricts_lane_sources\[local_source\] patches _try_get_r_grouped_lane_accesses to return a Mock, so it only checks plumbing, not a lane-source restriction.
test_pitched_layer_norm_lanes does not check what it is said to cover. assertNotIn("in_ptr3", ...) is redundant once len(compute) == 1 is asserted. Nothing checks that comprehensive_padding actually produces a row pitch wider than the width, which is the test's stated purpose, so a change to the padding heuristics would silently drop coverage of pitched producers. Assert the padded stride, or force it with empty_strided as TestPitchedLaneSources does. Also, test_padded_two_pass_layer_norm does not reset or assert metrics.codegen_nested_reduction, although the description says it "locks in the one-kernel structure".
NESTED_CONFIG repeats compare()'s settings (:198-205) and TestPitchedLaneSources's config (:230-239), and the copies already disagree. Only compare() sets split_reductions=False, so the two NESTED_CONFIG tests run with split reductions enabled. TestPitchedLaneSources omits cat_fuse_computed_input_copies. Build the other two from NESTED_CONFIG.
Please address the findings and push; a new commit normally gets a fresh automated review.
This review is AI-generated and advisory. Each new automated review replaces the previous one.
|
|
||
|
|
||
| class TestNestedLaneForwarding(NestedLaneTestCase): | ||
| hw_classification = HardwareClassification.CUDA |
There was a problem hiding this comment.
🔴 test/REVIEW.md Rules 2 and 4: TestNestedLaneForwarding (new), TestPitchedLaneSources and TestNestedLaneSplits are classified CUDA but are accelerator-agnostic. None of them uses inline asm, torch.cuda.* or a hardcoded device. They are instantiated with only_for=GPU_TYPE and no allow_xpu=True, and get_device_type_test_bases() never adds XPUTestBase without allow_xpu, so they silently never run on XPU. Fix: classify them ACCELERATOR, instantiate with except_for=("cpu", "mps") and allow_xpu=True, and drop only_for. Also, the comment "The inline asm strings are PTX or AMDGCN." (:561) sits above TestRecursiveLaneSplit, which has no inline asm. It belongs above TestNestedAsmLaneInputs.
| words = pack_words(groups, maximum.to(torch.int32), 6) | ||
| return words.view(torch.uint8), maximum | ||
|
|
||
| self.compare(fn, lane_input(device), persistent, loop_ordering=False) |
There was a problem hiding this comment.
🔴 Nothing tests the lane plan with loop_ordering_after_fusion off, which is the fbcode default. test_parent_lane_inputs_without_loop_ordering only calls compare(), which checks output equality. If the plan never forms, both sides compile unfused and the test still passes, so the feature could do nothing in fbcode without any test noticing. Also, _InternalsBase.setUp (test_nested_reduction.py:3258) forces the flag on, and its stated reason ("nested reductions assume it") does not hold: NestedReduction._is_enabled_for never reads it, and test_rmsnorm_factor4_three_output_epilogue asserts nested fusion with the flag off. Fix: assert codegen_nested_reduction and generated_kernel_count for whichever outcome is intended. Then either drop the setUp override or document which internals test needs it.
| ) | ||
|
|
||
| def child_block(self, factor: int) -> str: | ||
| if factor == self.local_reduction_size: |
There was a problem hiding this comment.
🔴 factor == self.local_reduction_size is an implicit mode switch, duplicated in child_block and make_sub_parent_family (:1974). It never checks for a lane stage, so it also fires on existing paths when unroll_reductions_threshold <= 4: a nested sub-parent stage at rate (4, 3) with G = 4, which the scheduler.py:1946-1947 guard does not decline, and the standalone _codegen_reduction_with_sub_parent_epilogue, where rnumel is 4 and the layout uses parent_rnumel as local_reduction_size. On those paths is_group_width_shape always returns False, so group-width values skip the broadcast-and-split. The values are equivalent, but the generated code changes and no test covers either path. The PR description says this applies "for nested stages", which does not match the code. Fix: pass lane mode (or the child block) explicitly into make_sub_parent_family and have child_block read it from the family. Otherwise, correct the description and add exact-code tests for both paths.
Blocking findings addressed earlier in this stack (miscompile fix, backend gate, precision fix, test fixes); remaining items are coverage/perf follow-ups. Approved by human reviewer.
|
Starting merge as part of PR stack under #200466 |
3 similar comments
|
Starting merge as part of PR stack under #200466 |
|
Starting merge as part of PR stack under #200466 |
|
Starting merge as part of PR stack under #200466 |
…ilogues (#200466) Author's note: When the reduction itself is the group, a persistent kernel already holds each group's tile, but fixed-lane epilogue reads forced the producer to be materialized. This plans those epilogues against the live tile and forces a persistent kernel only when the backend heuristic agrees, so the producer, reduction, and epilogue compile to one kernel. The description below was drafted with an AI assistant (Claude): > A reduced-width epilogue that reads a fixed element of each reduction group > (e.g. `v.amax(-1) + v[:, 7]` with `v` of shape `[N, G]`) reads a value a > persistent reduction already holds in registers. Today that value is loaded > again, and if `v` is materialized in the same graph the epilogue cannot fuse: > it reads `v` at a different index than the producer writes it. > > For persistent reductions with a static power-of-2 group (at most 128), the > scheduler now plans such epilogues as sub-parent stages whose lane reads are > resolved from the live tile (`requires_persistent` plans). The lanes of a group > must be live together; since the reduction is the group, that means a > persistent kernel, which `SIMDScheduling` forces for these plans (a looped > multi-kernel variant could not serve the lanes). Suggested review order: > > 1. `NestedReduction._persistent_lane_epilogue_candidates`: which reductions > and epilogues qualify. > 2. `sub_parent_epilogue_plan`: instead of the hoisting mutation check, > persistent candidates (which have no mutations) only accept aliased > outputs that are slices of a `ConcatKernel` they feed (e.g. the columns of > `torch.stack(words, -1)`). Concat slices are disjoint and their stores keep > their own indexing; a reader of the concat depends on the concat node and > cannot join the group. > 3. The scheduler side: the existing index-equivalence fallback now also > accepts reads of persistent plans; the sub-parent read proof maps a size-1 > child coordinate; the row-boundary check accepts a flat contiguous source > over the whole frame; and `can_fuse` can prepend reduced epilogues to an > existing `FusedStagedReduction`. > > A reduction that an upstream reduction over the same full domain could own as > an outer + grouped nested pipeline is left to that planner. > > Performance on MI300X, 16M elements, `v` materialized as an output: > `v.abs().amax(-1) + v[:, G // 3]` goes from 2 kernels to 1 and 1.12-1.14x for > G = 8, 32, 64. When the lane sources are graph inputs there is no kernel to > remove; taking the lanes from the tile measured 0.93-1.01x for 1-2 of 32 lanes > and faster beyond that (0.43-0.58x of the load time for all 32). > > `test_nested_reduction.py`'s even/odd consumer test now expects the fixed-lane > side output to fuse while the incompatible consumers keep their own kernel. > > Test Plan: > ``` > python -m pytest test/inductor/test_persistent_reduction_lanes.py \ > test/inductor/test_nested_reduction_lanes.py test/inductor/test_nested_reduction.py \ > test/inductor/test_inductor_scheduler.py > ``` > A randomized sweep of 173 generated lane graphs matched eager. > `test_single_group_is_not_forwarded` and `test_dynamic_group_count` cover a > single group and a symbolic group count, both declined (their epilogue ranges > are empty, or merge differently per node in `merge_loops`). > `test_tiled_group_axes` covers a GEMM scale tile whose first axis has size 1. > > This change was prepared with an AI assistant. Pull Request resolved: #200466 Approved by: https://github.com/drisspg ghstack dependencies: #200459, #200460, #200461, #200462, #200463, #200464, #200465
…0465) Author's note: In outer-plus-grouped nested reductions (layer norm followed by a per-group pack), a consumer reading fixed lanes of each group broke the fusion, so the normalized tensor was written to memory and read back. This proves when such a read is a lane of the live group tile; the MXFP6 layer-norm preparation then compiles to one kernel. The description below was drafted with an AI assistant (Claude): > In an outer + grouped nested reduction, a reduced-width consumer in the grouped > stage may read one fixed element of each group, e.g. the per-group inputs of > a multi-output `inline_asm_elementwise` call. The cross-stage check > (`_r_grouped_stage_accesses_match`) required every internal read to address > the same element its producer writes, so such a consumer broke the plan and > the value was materialized and reloaded in a second kernel. > > `_try_get_r_grouped_lane_accesses` now also accepts a read that selects a > fixed lane of the producer's local group, and returns it as a > `SubParentAccessRelation`. It requires a unique, unit-stride, non-reduction, > non-atomic write, a constant lane in `[0, G)`, a group base that is a multiple > of `G`, and row-preserving indices on both sides. The writer must belong to > the local stage, or compute a full-width value from the grouped reduction's > output; a writer outside the plan's pointwise domains belongs to the parent > stage, which a looped parent flushes before the grouped stage runs. The > relations are stored on `NestedReductionStage.lane_accesses`; the scheduler's > dependency check accepts exactly those read/write pairs, and > codegen resolves them through `_SubParentValueResolver` at the local > reduction factor, whose derived family now sizes its child block from the > reduced block for nested stages. > > Also included: a `why("nested reduction plan declined")` fusion log, and the > nested-reduction internals tests now enable `loop_ordering_after_fusion` in > `setUp` (nested reductions assume it). > > End to end, this makes a whole MXFP6 layer-norm preparation one kernel. The > preparation graphs pad K to a 192-element tile. Written the obvious way (layer > norm over K, then pad the packed payload with zeros and the scales with 127), > the padding tail and the GEMM scale layout need a second kernel. Written with > the input padded instead (two-pass statistics that keep the true K, padded > values masked to zero), the padded groups flow through the same nested > reduction and a small-M or medium-K preparation compiles to one kernel. On > MI350X this form is faster than the hand-written kernel for both layer-norm > shapes (K=1056, M=128: 4.43 vs 5.11 us; K=22592: 28.5 vs 40.1 us). Inductor > cannot rewrite the first form into the second: the payload padding is zero > only because the FP6 conversion maps zero inputs to zero, which is hidden > inside inline assembly. `test_padded_two_pass_layer_norm` locks in the > one-kernel structure for the input-padded form, and the packed 16-split lane > extraction the performance depends on. > > Test Plan: > ``` > python -m pytest test/inductor/test_nested_reduction_lanes.py \ > test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py > ``` > `test_nested_reduction_lanes.py` covers lanes read from the parent stage, source > lifetimes, and the previous commit's shared source bitcast and packed 16-bit > split, with exact split counts (these graphs only reach the resolver once their > lanes are forwarded). `test_pitched_layer_norm_lanes` covers a producer with a > row pitch wider than its width. The acceptance conditions are tested directly in > `test_inductor_scheduler.py` (`test_nested_grouped_lane_access_conditions`, > `test_nested_plan_restricts_lane_sources`, > `test_nested_dependency_matches_require_planned_lane`); each negative case fails > if its condition is removed. `test_parent_full_lane_source` covers lanes of a > value computed from the grouped reduction's output, and > `test_padded_two_pass_layer_norm` the whole padded MXFP6 layer-norm > preparation (one kernel, 16 packed splits). > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200465 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464
…ilogues (pytorch#200466) Author's note: When the reduction itself is the group, a persistent kernel already holds each group's tile, but fixed-lane epilogue reads forced the producer to be materialized. This plans those epilogues against the live tile and forces a persistent kernel only when the backend heuristic agrees, so the producer, reduction, and epilogue compile to one kernel. The description below was drafted with an AI assistant (Claude): > A reduced-width epilogue that reads a fixed element of each reduction group > (e.g. `v.amax(-1) + v[:, 7]` with `v` of shape `[N, G]`) reads a value a > persistent reduction already holds in registers. Today that value is loaded > again, and if `v` is materialized in the same graph the epilogue cannot fuse: > it reads `v` at a different index than the producer writes it. > > For persistent reductions with a static power-of-2 group (at most 128), the > scheduler now plans such epilogues as sub-parent stages whose lane reads are > resolved from the live tile (`requires_persistent` plans). The lanes of a group > must be live together; since the reduction is the group, that means a > persistent kernel, which `SIMDScheduling` forces for these plans (a looped > multi-kernel variant could not serve the lanes). Suggested review order: > > 1. `NestedReduction._persistent_lane_epilogue_candidates`: which reductions > and epilogues qualify. > 2. `sub_parent_epilogue_plan`: instead of the hoisting mutation check, > persistent candidates (which have no mutations) only accept aliased > outputs that are slices of a `ConcatKernel` they feed (e.g. the columns of > `torch.stack(words, -1)`). Concat slices are disjoint and their stores keep > their own indexing; a reader of the concat depends on the concat node and > cannot join the group. > 3. The scheduler side: the existing index-equivalence fallback now also > accepts reads of persistent plans; the sub-parent read proof maps a size-1 > child coordinate; the row-boundary check accepts a flat contiguous source > over the whole frame; and `can_fuse` can prepend reduced epilogues to an > existing `FusedStagedReduction`. > > A reduction that an upstream reduction over the same full domain could own as > an outer + grouped nested pipeline is left to that planner. > > Performance on MI300X, 16M elements, `v` materialized as an output: > `v.abs().amax(-1) + v[:, G // 3]` goes from 2 kernels to 1 and 1.12-1.14x for > G = 8, 32, 64. When the lane sources are graph inputs there is no kernel to > remove; taking the lanes from the tile measured 0.93-1.01x for 1-2 of 32 lanes > and faster beyond that (0.43-0.58x of the load time for all 32). > > `test_nested_reduction.py`'s even/odd consumer test now expects the fixed-lane > side output to fuse while the incompatible consumers keep their own kernel. > > Test Plan: > ``` > python -m pytest test/inductor/test_persistent_reduction_lanes.py \ > test/inductor/test_nested_reduction_lanes.py test/inductor/test_nested_reduction.py \ > test/inductor/test_inductor_scheduler.py > ``` > A randomized sweep of 173 generated lane graphs matched eager. > `test_single_group_is_not_forwarded` and `test_dynamic_group_count` cover a > single group and a symbolic group count, both declined (their epilogue ranges > are empty, or merge differently per node in `merge_loops`). > `test_tiled_group_axes` covers a GEMM scale tile whose first axis has size 1. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200466 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464, pytorch#200465
…torch#200465)" This reverts commit 671e030. Reverted pytorch#200465 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#200459 (comment)))
…0465) Author's note: In outer-plus-grouped nested reductions (layer norm followed by a per-group pack), a consumer reading fixed lanes of each group broke the fusion, so the normalized tensor was written to memory and read back. This proves when such a read is a lane of the live group tile; the MXFP6 layer-norm preparation then compiles to one kernel. The description below was drafted with an AI assistant (Claude): > In an outer + grouped nested reduction, a reduced-width consumer in the grouped > stage may read one fixed element of each group, e.g. the per-group inputs of > a multi-output `inline_asm_elementwise` call. The cross-stage check > (`_r_grouped_stage_accesses_match`) required every internal read to address > the same element its producer writes, so such a consumer broke the plan and > the value was materialized and reloaded in a second kernel. > > `_try_get_r_grouped_lane_accesses` now also accepts a read that selects a > fixed lane of the producer's local group, and returns it as a > `SubParentAccessRelation`. It requires a unique, unit-stride, non-reduction, > non-atomic write, a constant lane in `[0, G)`, a group base that is a multiple > of `G`, and row-preserving indices on both sides. The writer must belong to > the local stage, or compute a full-width value from the grouped reduction's > output; a writer outside the plan's pointwise domains belongs to the parent > stage, which a looped parent flushes before the grouped stage runs. The > relations are stored on `NestedReductionStage.lane_accesses`; the scheduler's > dependency check accepts exactly those read/write pairs, and > codegen resolves them through `_SubParentValueResolver` at the local > reduction factor, whose derived family now sizes its child block from the > reduced block for nested stages. > > Also included: a `why("nested reduction plan declined")` fusion log, and the > nested-reduction internals tests now enable `loop_ordering_after_fusion` in > `setUp` (nested reductions assume it). > > End to end, this makes a whole MXFP6 layer-norm preparation one kernel. The > preparation graphs pad K to a 192-element tile. Written the obvious way (layer > norm over K, then pad the packed payload with zeros and the scales with 127), > the padding tail and the GEMM scale layout need a second kernel. Written with > the input padded instead (two-pass statistics that keep the true K, padded > values masked to zero), the padded groups flow through the same nested > reduction and a small-M or medium-K preparation compiles to one kernel. On > MI350X this form is faster than the hand-written kernel for both layer-norm > shapes (K=1056, M=128: 4.43 vs 5.11 us; K=22592: 28.5 vs 40.1 us). Inductor > cannot rewrite the first form into the second: the payload padding is zero > only because the FP6 conversion maps zero inputs to zero, which is hidden > inside inline assembly. `test_padded_two_pass_layer_norm` locks in the > one-kernel structure for the input-padded form, and the packed 16-split lane > extraction the performance depends on. > > Test Plan: > ``` > python -m pytest test/inductor/test_nested_reduction_lanes.py \ > test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py > ``` > `test_nested_reduction_lanes.py` covers lanes read from the parent stage, source > lifetimes, and the previous commit's shared source bitcast and packed 16-bit > split, with exact split counts (these graphs only reach the resolver once their > lanes are forwarded). `test_pitched_layer_norm_lanes` covers a producer with a > row pitch wider than its width. The acceptance conditions are tested directly in > `test_inductor_scheduler.py` (`test_nested_grouped_lane_access_conditions`, > `test_nested_plan_restricts_lane_sources`, > `test_nested_dependency_matches_require_planned_lane`); each negative case fails > if its condition is removed. `test_parent_full_lane_source` covers lanes of a > value computed from the grouped reduction's output, and > `test_padded_two_pass_layer_norm` the whole padded MXFP6 layer-norm > preparation (one kernel, 16 packed splits). > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200465 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464
…ilogues (pytorch#200466) Author's note: When the reduction itself is the group, a persistent kernel already holds each group's tile, but fixed-lane epilogue reads forced the producer to be materialized. This plans those epilogues against the live tile and forces a persistent kernel only when the backend heuristic agrees, so the producer, reduction, and epilogue compile to one kernel. The description below was drafted with an AI assistant (Claude): > A reduced-width epilogue that reads a fixed element of each reduction group > (e.g. `v.amax(-1) + v[:, 7]` with `v` of shape `[N, G]`) reads a value a > persistent reduction already holds in registers. Today that value is loaded > again, and if `v` is materialized in the same graph the epilogue cannot fuse: > it reads `v` at a different index than the producer writes it. > > For persistent reductions with a static power-of-2 group (at most 128), the > scheduler now plans such epilogues as sub-parent stages whose lane reads are > resolved from the live tile (`requires_persistent` plans). The lanes of a group > must be live together; since the reduction is the group, that means a > persistent kernel, which `SIMDScheduling` forces for these plans (a looped > multi-kernel variant could not serve the lanes). Suggested review order: > > 1. `NestedReduction._persistent_lane_epilogue_candidates`: which reductions > and epilogues qualify. > 2. `sub_parent_epilogue_plan`: instead of the hoisting mutation check, > persistent candidates (which have no mutations) only accept aliased > outputs that are slices of a `ConcatKernel` they feed (e.g. the columns of > `torch.stack(words, -1)`). Concat slices are disjoint and their stores keep > their own indexing; a reader of the concat depends on the concat node and > cannot join the group. > 3. The scheduler side: the existing index-equivalence fallback now also > accepts reads of persistent plans; the sub-parent read proof maps a size-1 > child coordinate; the row-boundary check accepts a flat contiguous source > over the whole frame; and `can_fuse` can prepend reduced epilogues to an > existing `FusedStagedReduction`. > > A reduction that an upstream reduction over the same full domain could own as > an outer + grouped nested pipeline is left to that planner. > > Performance on MI300X, 16M elements, `v` materialized as an output: > `v.abs().amax(-1) + v[:, G // 3]` goes from 2 kernels to 1 and 1.12-1.14x for > G = 8, 32, 64. When the lane sources are graph inputs there is no kernel to > remove; taking the lanes from the tile measured 0.93-1.01x for 1-2 of 32 lanes > and faster beyond that (0.43-0.58x of the load time for all 32). > > `test_nested_reduction.py`'s even/odd consumer test now expects the fixed-lane > side output to fuse while the incompatible consumers keep their own kernel. > > Test Plan: > ``` > python -m pytest test/inductor/test_persistent_reduction_lanes.py \ > test/inductor/test_nested_reduction_lanes.py test/inductor/test_nested_reduction.py \ > test/inductor/test_inductor_scheduler.py > ``` > A randomized sweep of 173 generated lane graphs matched eager. > `test_single_group_is_not_forwarded` and `test_dynamic_group_count` cover a > single group and a symbolic group count, both declined (their epilogue ranges > are empty, or merge differently per node in `merge_loops`). > `test_tiled_group_axes` covers a GEMM scale tile whose first axis has size 1. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200466 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464, pytorch#200465
…0465) Author's note: In outer-plus-grouped nested reductions (layer norm followed by a per-group pack), a consumer reading fixed lanes of each group broke the fusion, so the normalized tensor was written to memory and read back. This proves when such a read is a lane of the live group tile; the MXFP6 layer-norm preparation then compiles to one kernel. The description below was drafted with an AI assistant (Claude): > In an outer + grouped nested reduction, a reduced-width consumer in the grouped > stage may read one fixed element of each group, e.g. the per-group inputs of > a multi-output `inline_asm_elementwise` call. The cross-stage check > (`_r_grouped_stage_accesses_match`) required every internal read to address > the same element its producer writes, so such a consumer broke the plan and > the value was materialized and reloaded in a second kernel. > > `_try_get_r_grouped_lane_accesses` now also accepts a read that selects a > fixed lane of the producer's local group, and returns it as a > `SubParentAccessRelation`. It requires a unique, unit-stride, non-reduction, > non-atomic write, a constant lane in `[0, G)`, a group base that is a multiple > of `G`, and row-preserving indices on both sides. The writer must belong to > the local stage, or compute a full-width value from the grouped reduction's > output; a writer outside the plan's pointwise domains belongs to the parent > stage, which a looped parent flushes before the grouped stage runs. The > relations are stored on `NestedReductionStage.lane_accesses`; the scheduler's > dependency check accepts exactly those read/write pairs, and > codegen resolves them through `_SubParentValueResolver` at the local > reduction factor, whose derived family now sizes its child block from the > reduced block for nested stages. > > Also included: a `why("nested reduction plan declined")` fusion log, and the > nested-reduction internals tests now enable `loop_ordering_after_fusion` in > `setUp` (nested reductions assume it). > > End to end, this makes a whole MXFP6 layer-norm preparation one kernel. The > preparation graphs pad K to a 192-element tile. Written the obvious way (layer > norm over K, then pad the packed payload with zeros and the scales with 127), > the padding tail and the GEMM scale layout need a second kernel. Written with > the input padded instead (two-pass statistics that keep the true K, padded > values masked to zero), the padded groups flow through the same nested > reduction and a small-M or medium-K preparation compiles to one kernel. On > MI350X this form is faster than the hand-written kernel for both layer-norm > shapes (K=1056, M=128: 4.43 vs 5.11 us; K=22592: 28.5 vs 40.1 us). Inductor > cannot rewrite the first form into the second: the payload padding is zero > only because the FP6 conversion maps zero inputs to zero, which is hidden > inside inline assembly. `test_padded_two_pass_layer_norm` locks in the > one-kernel structure for the input-padded form, and the packed 16-split lane > extraction the performance depends on. > > Test Plan: > ``` > python -m pytest test/inductor/test_nested_reduction_lanes.py \ > test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py > ``` > `test_nested_reduction_lanes.py` covers lanes read from the parent stage, source > lifetimes, and the previous commit's shared source bitcast and packed 16-bit > split, with exact split counts (these graphs only reach the resolver once their > lanes are forwarded). `test_pitched_layer_norm_lanes` covers a producer with a > row pitch wider than its width. The acceptance conditions are tested directly in > `test_inductor_scheduler.py` (`test_nested_grouped_lane_access_conditions`, > `test_nested_plan_restricts_lane_sources`, > `test_nested_dependency_matches_require_planned_lane`); each negative case fails > if its condition is removed. `test_parent_full_lane_source` covers lanes of a > value computed from the grouped reduction's output, and > `test_padded_two_pass_layer_norm` the whole padded MXFP6 layer-norm > preparation (one kernel, 16 packed splits). > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200465 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464
…ilogues (pytorch#200466) Author's note: When the reduction itself is the group, a persistent kernel already holds each group's tile, but fixed-lane epilogue reads forced the producer to be materialized. This plans those epilogues against the live tile and forces a persistent kernel only when the backend heuristic agrees, so the producer, reduction, and epilogue compile to one kernel. The description below was drafted with an AI assistant (Claude): > A reduced-width epilogue that reads a fixed element of each reduction group > (e.g. `v.amax(-1) + v[:, 7]` with `v` of shape `[N, G]`) reads a value a > persistent reduction already holds in registers. Today that value is loaded > again, and if `v` is materialized in the same graph the epilogue cannot fuse: > it reads `v` at a different index than the producer writes it. > > For persistent reductions with a static power-of-2 group (at most 128), the > scheduler now plans such epilogues as sub-parent stages whose lane reads are > resolved from the live tile (`requires_persistent` plans). The lanes of a group > must be live together; since the reduction is the group, that means a > persistent kernel, which `SIMDScheduling` forces for these plans (a looped > multi-kernel variant could not serve the lanes). Suggested review order: > > 1. `NestedReduction._persistent_lane_epilogue_candidates`: which reductions > and epilogues qualify. > 2. `sub_parent_epilogue_plan`: instead of the hoisting mutation check, > persistent candidates (which have no mutations) only accept aliased > outputs that are slices of a `ConcatKernel` they feed (e.g. the columns of > `torch.stack(words, -1)`). Concat slices are disjoint and their stores keep > their own indexing; a reader of the concat depends on the concat node and > cannot join the group. > 3. The scheduler side: the existing index-equivalence fallback now also > accepts reads of persistent plans; the sub-parent read proof maps a size-1 > child coordinate; the row-boundary check accepts a flat contiguous source > over the whole frame; and `can_fuse` can prepend reduced epilogues to an > existing `FusedStagedReduction`. > > A reduction that an upstream reduction over the same full domain could own as > an outer + grouped nested pipeline is left to that planner. > > Performance on MI300X, 16M elements, `v` materialized as an output: > `v.abs().amax(-1) + v[:, G // 3]` goes from 2 kernels to 1 and 1.12-1.14x for > G = 8, 32, 64. When the lane sources are graph inputs there is no kernel to > remove; taking the lanes from the tile measured 0.93-1.01x for 1-2 of 32 lanes > and faster beyond that (0.43-0.58x of the load time for all 32). > > `test_nested_reduction.py`'s even/odd consumer test now expects the fixed-lane > side output to fuse while the incompatible consumers keep their own kernel. > > Test Plan: > ``` > python -m pytest test/inductor/test_persistent_reduction_lanes.py \ > test/inductor/test_nested_reduction_lanes.py test/inductor/test_nested_reduction.py \ > test/inductor/test_inductor_scheduler.py > ``` > A randomized sweep of 173 generated lane graphs matched eager. > `test_single_group_is_not_forwarded` and `test_dynamic_group_count` cover a > single group and a symbolic group count, both declined (their epilogue ranges > are empty, or merge differently per node in `merge_loops`). > `test_tiled_group_axes` covers a GEMM scale tile whose first axis has size 1. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200466 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464, pytorch#200465
…torch#200465)" This reverts commit 671e030. Reverted pytorch#200465 on behalf of https://github.com/pytorch-auto-revert due to Reverted automatically by pytorch's autorevert, to avoid this behaviour add the tag autorevert: disable ([comment](pytorch#200459 (comment)))
…0465) Author's note: In outer-plus-grouped nested reductions (layer norm followed by a per-group pack), a consumer reading fixed lanes of each group broke the fusion, so the normalized tensor was written to memory and read back. This proves when such a read is a lane of the live group tile; the MXFP6 layer-norm preparation then compiles to one kernel. The description below was drafted with an AI assistant (Claude): > In an outer + grouped nested reduction, a reduced-width consumer in the grouped > stage may read one fixed element of each group, e.g. the per-group inputs of > a multi-output `inline_asm_elementwise` call. The cross-stage check > (`_r_grouped_stage_accesses_match`) required every internal read to address > the same element its producer writes, so such a consumer broke the plan and > the value was materialized and reloaded in a second kernel. > > `_try_get_r_grouped_lane_accesses` now also accepts a read that selects a > fixed lane of the producer's local group, and returns it as a > `SubParentAccessRelation`. It requires a unique, unit-stride, non-reduction, > non-atomic write, a constant lane in `[0, G)`, a group base that is a multiple > of `G`, and row-preserving indices on both sides. The writer must belong to > the local stage, or compute a full-width value from the grouped reduction's > output; a writer outside the plan's pointwise domains belongs to the parent > stage, which a looped parent flushes before the grouped stage runs. The > relations are stored on `NestedReductionStage.lane_accesses`; the scheduler's > dependency check accepts exactly those read/write pairs, and > codegen resolves them through `_SubParentValueResolver` at the local > reduction factor, whose derived family now sizes its child block from the > reduced block for nested stages. > > Also included: a `why("nested reduction plan declined")` fusion log, and the > nested-reduction internals tests now enable `loop_ordering_after_fusion` in > `setUp` (nested reductions assume it). > > End to end, this makes a whole MXFP6 layer-norm preparation one kernel. The > preparation graphs pad K to a 192-element tile. Written the obvious way (layer > norm over K, then pad the packed payload with zeros and the scales with 127), > the padding tail and the GEMM scale layout need a second kernel. Written with > the input padded instead (two-pass statistics that keep the true K, padded > values masked to zero), the padded groups flow through the same nested > reduction and a small-M or medium-K preparation compiles to one kernel. On > MI350X this form is faster than the hand-written kernel for both layer-norm > shapes (K=1056, M=128: 4.43 vs 5.11 us; K=22592: 28.5 vs 40.1 us). Inductor > cannot rewrite the first form into the second: the payload padding is zero > only because the FP6 conversion maps zero inputs to zero, which is hidden > inside inline assembly. `test_padded_two_pass_layer_norm` locks in the > one-kernel structure for the input-padded form, and the packed 16-split lane > extraction the performance depends on. > > Test Plan: > ``` > python -m pytest test/inductor/test_nested_reduction_lanes.py \ > test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py > ``` > `test_nested_reduction_lanes.py` covers lanes read from the parent stage, source > lifetimes, and the previous commit's shared source bitcast and packed 16-bit > split, with exact split counts (these graphs only reach the resolver once their > lanes are forwarded). `test_pitched_layer_norm_lanes` covers a producer with a > row pitch wider than its width. The acceptance conditions are tested directly in > `test_inductor_scheduler.py` (`test_nested_grouped_lane_access_conditions`, > `test_nested_plan_restricts_lane_sources`, > `test_nested_dependency_matches_require_planned_lane`); each negative case fails > if its condition is removed. `test_parent_full_lane_source` covers lanes of a > value computed from the grouped reduction's output, and > `test_padded_two_pass_layer_norm` the whole padded MXFP6 layer-norm > preparation (one kernel, 16 packed splits). > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200465 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464
…ilogues (pytorch#200466) Author's note: When the reduction itself is the group, a persistent kernel already holds each group's tile, but fixed-lane epilogue reads forced the producer to be materialized. This plans those epilogues against the live tile and forces a persistent kernel only when the backend heuristic agrees, so the producer, reduction, and epilogue compile to one kernel. The description below was drafted with an AI assistant (Claude): > A reduced-width epilogue that reads a fixed element of each reduction group > (e.g. `v.amax(-1) + v[:, 7]` with `v` of shape `[N, G]`) reads a value a > persistent reduction already holds in registers. Today that value is loaded > again, and if `v` is materialized in the same graph the epilogue cannot fuse: > it reads `v` at a different index than the producer writes it. > > For persistent reductions with a static power-of-2 group (at most 128), the > scheduler now plans such epilogues as sub-parent stages whose lane reads are > resolved from the live tile (`requires_persistent` plans). The lanes of a group > must be live together; since the reduction is the group, that means a > persistent kernel, which `SIMDScheduling` forces for these plans (a looped > multi-kernel variant could not serve the lanes). Suggested review order: > > 1. `NestedReduction._persistent_lane_epilogue_candidates`: which reductions > and epilogues qualify. > 2. `sub_parent_epilogue_plan`: instead of the hoisting mutation check, > persistent candidates (which have no mutations) only accept aliased > outputs that are slices of a `ConcatKernel` they feed (e.g. the columns of > `torch.stack(words, -1)`). Concat slices are disjoint and their stores keep > their own indexing; a reader of the concat depends on the concat node and > cannot join the group. > 3. The scheduler side: the existing index-equivalence fallback now also > accepts reads of persistent plans; the sub-parent read proof maps a size-1 > child coordinate; the row-boundary check accepts a flat contiguous source > over the whole frame; and `can_fuse` can prepend reduced epilogues to an > existing `FusedStagedReduction`. > > A reduction that an upstream reduction over the same full domain could own as > an outer + grouped nested pipeline is left to that planner. > > Performance on MI300X, 16M elements, `v` materialized as an output: > `v.abs().amax(-1) + v[:, G // 3]` goes from 2 kernels to 1 and 1.12-1.14x for > G = 8, 32, 64. When the lane sources are graph inputs there is no kernel to > remove; taking the lanes from the tile measured 0.93-1.01x for 1-2 of 32 lanes > and faster beyond that (0.43-0.58x of the load time for all 32). > > `test_nested_reduction.py`'s even/odd consumer test now expects the fixed-lane > side output to fuse while the incompatible consumers keep their own kernel. > > Test Plan: > ``` > python -m pytest test/inductor/test_persistent_reduction_lanes.py \ > test/inductor/test_nested_reduction_lanes.py test/inductor/test_nested_reduction.py \ > test/inductor/test_inductor_scheduler.py > ``` > A randomized sweep of 173 generated lane graphs matched eager. > `test_single_group_is_not_forwarded` and `test_dynamic_group_count` cover a > single group and a symbolic group count, both declined (their epilogue ranges > are empty, or merge differently per node in `merge_loops`). > `test_tiled_group_axes` covers a GEMM scale tile whose first axis has size 1. > > This change was prepared with an AI assistant. Pull Request resolved: pytorch#200466 Approved by: https://github.com/drisspg ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463, pytorch#200464, pytorch#200465
Stack from ghstack (oldest at bottom):
Author's note: In outer-plus-grouped nested reductions (layer norm followed by a per-group pack), a consumer reading fixed lanes of each group broke the fusion, so the normalized tensor was written to memory and read back. This proves when such a read is a lane of the live group tile; the MXFP6 layer-norm preparation then compiles to one kernel.
The description below was drafted with an AI assistant (Claude):
cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo