Skip to content

[inductor] Allow index-equivalence proofs for non-overlapping affine writes - #200462

Closed
eellison wants to merge 14 commits into
gh/eellison/1118/basefrom
gh/eellison/1118/head
Closed

eellison wants to merge 14 commits into
gh/eellison/1118/basefrom
gh/eellison/1118/head

Conversation

@eellison

@eellison eellison commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Author's note: Forwarding a producer's live values required dense writes, so padded producers (row pitch wider than width) couldn't fuse. I reviewed the conservative non-overlap proof, which only declines on unknown sizes; it was also checked against brute-force address enumeration.

The description below was drafted with an AI assistant (Claude):

Fusing a consumer that forwards a producer's live values requires knowing the
producer writes each address from exactly one iteration. The existing check
only accepts dense (contiguous) writes, so a producer whose logical rows are
narrower than their storage pitch (e.g. width 1056, pitch 1088, from a padded
output) is rejected even though its rows never overlap.

Add MemoryDep.is_non_overlapping: the index must equal its affine
reconstruction offset + sum(stride_i * var_i), and with axes ordered by
stride, each stride must exceed the largest displacement the inner axes can
reach. Size hints only choose the order; every inequality is checked with
statically_known_*, so a wrong hint can only make the proof decline. Offsets
containing indirect (TMP) symbols vary per iteration and decline too.

This only replaces the density clause of
_memory_dep_supports_index_equivalence; callers still prove that each read
matches the write of the same iteration.

Test Plan:

python -m pytest test/inductor/test_dependencies.py test/inductor/test_inductor_scheduler.py
python -m pytest test/inductor/test_nested_reduction_lanes.py -k pitched_group_lanes

test_non_overlapping_addresses checks every accepted map against its
enumerated addresses; a separate brute-force sweep over 18k rank-1..3 maps
found no accepted map with repeated addresses.
test_pitched_group_lanes_and_aliases reads a fixed lane of a producer whose
row pitch is wider than its width; its escape and dynamic cases fail
without this change.

This change was prepared with an AI assistant.

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 Oct 9, 2026 •

Copy link
Copy Markdown

PR Status: Approved 🚀

Please fix all CI failures and trigger merge by commenting "@pytorchbot merge".

✅ AUTOMATED REVIEW - OUTDATED (earlier commit) - Ready for human review:

The PR adds MemoryDep.is_non_overlapping(), which proves that an affine write
map is injective even when storage has gaps. The scheduler uses it to accept
pitched producer writes for nested-reduction index equivalence. The proof looks
sound and the read-side matching still rules out reads into gaps. The unit tests
would fail if the change regressed. The only problems are where the new GPU test
lives and how it is set up; the one maintainer comment is a non-actionable
aside.

Findings (2):

[minor] test/inductor/test_nested_reduction_lanes.py:8
New test file duplicates the scope of test/inductor/test_nested_reduction.py,
which already tests metrics.codegen_nested_reduction. Move this test there. It
also uses common_utils.TestCase/run_tests instead of torch._inductor.test_case,
so dynamo and inductor cache state are not reset between tests.

[minor] test/inductor/test_nested_reduction_lanes.py:93
No HAS_GPU guard (test_nested_reduction.py uses __unittest_skip__ = not HAS_GPU
and `if HAS_GPU: run_tests()`). On a CUDA machine without Triton the test is
instantiated and crashes, and it never runs on XPU because allow_xpu is not
passed.

Reviewed commit: 1ea220c (NOT the current head 931392e)

Review run

🔗 Helpful Links

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

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

✅ No Failures

As of commit 931392e with merge base 4fb332d (image):
💚 Looks good so far! There are no failures yet. 💚

🟡 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.

[ghstack-poisoned]
[ghstack-poisoned]
@eellison
eellison marked this pull request as ready for review October 12, 2026 17:25
[ghstack-poisoned]
return True
return isinstance(self.index, sympy.Symbol) and self.index in self.var_names

def is_non_overlapping(self) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should start writng up lean proofs for this could be kinda fun

[ghstack-poisoned]
[ghstack-poisoned]
@eellison eellison added the topic: not user facing topic category label Oct 12, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200465

@pytorchbot pytorchbot added the in progress PR is being worked on by the author, until the automated review accepts it label Oct 16, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Automated review of 1ea220c: nothing blocking; ready for human review.

The PR adds MemoryDep.is_non_overlapping(), which proves that an affine write map is injective even when storage has gaps. The scheduler uses it to accept pitched producer writes for nested-reduction index equivalence. The proof looks sound and the read-side matching still rules out reads into gaps. The unit tests would fail if the change regressed. The only problems are where the new GPU test lives and how it is set up; the one maintainer comment is a non-actionable aside.

⚪ 2 non-blocking findings

from torch.testing._internal.common_utils import parametrize, run_tests, TestCase

New test file duplicates the scope of test/inductor/test_nested_reduction.py, which already tests metrics.codegen_nested_reduction. Move this test there. It also uses common_utils.TestCase/run_tests instead of torch._inductor.test_case, so dynamo and inductor cache state are not reset between tests.


instantiate_device_type_tests(TestPitchedLaneSources, globals(), only_for=GPU_TYPE)

No HAS_GPU guard (test_nested_reduction.py uses unittest_skip = not HAS_GPU and if HAS_GPU: run_tests()). On a CUDA machine without Triton the test is instantiated and crashes, and it never runs on XPU because allow_xpu is not passed.

This review is AI-generated and advisory. Each new automated review replaces the previous one.

@pytorch-bot pytorch-bot Bot added ready for review Automated review passed, ready for a final review by a maintainer. and removed in progress PR is being worked on by the author, until the automated review accepts it labels Oct 16, 2026
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200466

1 similar comment
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #200466

pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…stages (#200463)

Author's note: A grouped reduction over fp16 labeled its fp32 tile as fp16, so a later bitcast emitted an invalid Triton cast and compilation failed; this is an upstream bug found by fuzzing. The reshape now keeps the input's dtype and the result uses the same accumulation type as `TritonKernel.reduction`.

The description below was drafted with an AI assistant (Claude):

> `_GroupedReductionOpsHandler.reduction` reshapes and reduces a grouped stage
> value with the source dtype, but like `TritonKernel.reduction` the tile is
> computed in float32 for float16/bfloat16 inputs. The reduced value is then
> labelled float16 while holding a float32 tile, so a later bitcast emits
> `<fp32 tile>.to(tl.int16, bitcast=True)`, which Triton rejects. Use the upcast
> compute type for both the reshape and the reduce.
>
> Found by fuzzing the nested-reduction lane paths; it reproduces on upstream
> main without them (`amin` over fp16 groups, then `.view(torch.int16) + 1`).
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction.py -k half_grouped_reduction_bitcast
> ```
> `test_half_grouped_reduction_bitcast_epilogue` fails with a Triton compilation
> error without the fix (persistent and looped variants) and passes with it.
>
> This change was prepared with an AI assistant.

Pull Request resolved: #200463
Approved by: https://github.com/drisspg
ghstack dependencies: #200459, #200460, #200461, #200462
pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…200464)

Author's note: Reading fixed lanes of a live tile used to round and bitcast every lane separately after a 31-way split, which is slow on MI350X. Now the bitcast runs once on the whole tile and 16-bit lanes are split as packed 32-bit pairs (16 splits); I verified MI350X performance natively.

The description below was drafted with an AI assistant (Claude):

> Sub-parent epilogues read fixed lanes of a group tile: lane k of a
> `[..., groups, G]` tile is extracted with a reshape and a `tl.split` tree.
> Inductor inlines the pointwise chain between the source and each lane read
> into every lane's consumer. For the MXFP6 pack (`bits(v[2j]) |
> bits(v[2j+1]) << 16` over a group of 32 bf16 values), each lane is split out
> as fp32 and then rounded to bf16 and bitcast on its own. The split lanes have
> replicated layouts, so every thread repeats that work for each lane, and on
> MI350X the 32 separate 16-bit lanes also cost extra layout conversions.
>
> Three changes, all local to the existing split path:
>
> - When every lane of a source is read, a `to_dtype_bitcast` of the source is
>   emitted once at parent width, so the existing parent-twin fold collapses
>   each lane's cast chain into a split of the integer tile.
>   `to_dtype_bitcast` is registered as a pointwise op for that fold.
> - `emit_split_via_reshape` moves 16-bit lanes (int16/fp16/bf16, power-of-two
>   factor > 2) in pairs: split even/odd once, pack each pair into a uint32,
>   run the existing recursive split on the packed tile (16 splits instead of
>   31 at G=32), and unpack each lane to its original name and dtype.
> - In looped kernels the recursive split takes contiguous halves (via
>   `tl.permute`) instead of even/odd lanes, which needs fewer layout
>   conversions there; persistent kernels keep even/odd, which is faster for
>   them.
>
> Sources are no longer split eagerly before the epilogue: `check_sources` only
> checks that required sources are live, and a split is emitted when a
> consumer needs a lane.
>
> This changes how existing sub-parent epilogues are emitted; which graphs fuse
> is unchanged. It matters once the next two commits route the MXFP6 preparation
> graphs through the resolver. On native MI350X, a fused MXFP6 preparation
> kernel (an elementwise producer followed by a per-group pack) is about 1.3x
> slower with only the tile bitcast and no packed split, and falls behind a
> hand-written kernel. With this change it beats that kernel on both the
> elementwise-producer and the padded layer-norm preparation graphs.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction_lanes.py \
>   test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py
> ```
>
> This change was prepared with an AI assistant.

Pull Request resolved: #200464
Approved by: https://github.com/drisspg
ghstack dependencies: #200459, #200460, #200461, #200462, #200463
pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
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: #200465
Approved by: https://github.com/drisspg
ghstack dependencies: #200459, #200460, #200461, #200462, #200463, #200464
pytorchmergebot pushed a commit that referenced this pull request Nov 3, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…writes (pytorch#200462)

Author's note: Forwarding a producer's live values required dense writes, so padded producers (row pitch wider than width) couldn't fuse. I reviewed the conservative non-overlap proof, which only declines on unknown sizes; it was also checked against brute-force address enumeration.

The description below was drafted with an AI assistant (Claude):

> Fusing a consumer that forwards a producer's live values requires knowing the
> producer writes each address from exactly one iteration. The existing check
> only accepts dense (contiguous) writes, so a producer whose logical rows are
> narrower than their storage pitch (e.g. width 1056, pitch 1088, from a padded
> output) is rejected even though its rows never overlap.
>
> Add `MemoryDep.is_non_overlapping`: the index must equal its affine
> reconstruction `offset + sum(stride_i * var_i)`, and with axes ordered by
> stride, each stride must exceed the largest displacement the inner axes can
> reach. Size hints only choose the order; every inequality is checked with
> `statically_known_*`, so a wrong hint can only make the proof decline. Offsets
> containing indirect (TMP) symbols vary per iteration and decline too.
>
> This only replaces the density clause of
> `_memory_dep_supports_index_equivalence`; callers still prove that each read
> matches the write of the same iteration.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_dependencies.py test/inductor/test_inductor_scheduler.py
> python -m pytest test/inductor/test_nested_reduction_lanes.py -k pitched_group_lanes
> ```
> `test_non_overlapping_addresses` checks every accepted map against its
> enumerated addresses; a separate brute-force sweep over 18k rank-1..3 maps
> found no accepted map with repeated addresses.
> `test_pitched_group_lanes_and_aliases` reads a fixed lane of a producer whose
> row pitch is wider than its width; its `escape` and `dynamic` cases fail
> without this change.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200462
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…stages (pytorch#200463)

Author's note: A grouped reduction over fp16 labeled its fp32 tile as fp16, so a later bitcast emitted an invalid Triton cast and compilation failed; this is an upstream bug found by fuzzing. The reshape now keeps the input's dtype and the result uses the same accumulation type as `TritonKernel.reduction`.

The description below was drafted with an AI assistant (Claude):

> `_GroupedReductionOpsHandler.reduction` reshapes and reduces a grouped stage
> value with the source dtype, but like `TritonKernel.reduction` the tile is
> computed in float32 for float16/bfloat16 inputs. The reduced value is then
> labelled float16 while holding a float32 tile, so a later bitcast emits
> `<fp32 tile>.to(tl.int16, bitcast=True)`, which Triton rejects. Use the upcast
> compute type for both the reshape and the reduce.
>
> Found by fuzzing the nested-reduction lane paths; it reproduces on upstream
> main without them (`amin` over fp16 groups, then `.view(torch.int16) + 1`).
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction.py -k half_grouped_reduction_bitcast
> ```
> `test_half_grouped_reduction_bitcast_epilogue` fails with a Triton compilation
> error without the fix (persistent and looped variants) and passes with it.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200463
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…ytorch#200464)

Author's note: Reading fixed lanes of a live tile used to round and bitcast every lane separately after a 31-way split, which is slow on MI350X. Now the bitcast runs once on the whole tile and 16-bit lanes are split as packed 32-bit pairs (16 splits); I verified MI350X performance natively.

The description below was drafted with an AI assistant (Claude):

> Sub-parent epilogues read fixed lanes of a group tile: lane k of a
> `[..., groups, G]` tile is extracted with a reshape and a `tl.split` tree.
> Inductor inlines the pointwise chain between the source and each lane read
> into every lane's consumer. For the MXFP6 pack (`bits(v[2j]) |
> bits(v[2j+1]) << 16` over a group of 32 bf16 values), each lane is split out
> as fp32 and then rounded to bf16 and bitcast on its own. The split lanes have
> replicated layouts, so every thread repeats that work for each lane, and on
> MI350X the 32 separate 16-bit lanes also cost extra layout conversions.
>
> Three changes, all local to the existing split path:
>
> - When every lane of a source is read, a `to_dtype_bitcast` of the source is
>   emitted once at parent width, so the existing parent-twin fold collapses
>   each lane's cast chain into a split of the integer tile.
>   `to_dtype_bitcast` is registered as a pointwise op for that fold.
> - `emit_split_via_reshape` moves 16-bit lanes (int16/fp16/bf16, power-of-two
>   factor > 2) in pairs: split even/odd once, pack each pair into a uint32,
>   run the existing recursive split on the packed tile (16 splits instead of
>   31 at G=32), and unpack each lane to its original name and dtype.
> - In looped kernels the recursive split takes contiguous halves (via
>   `tl.permute`) instead of even/odd lanes, which needs fewer layout
>   conversions there; persistent kernels keep even/odd, which is faster for
>   them.
>
> Sources are no longer split eagerly before the epilogue: `check_sources` only
> checks that required sources are live, and a split is emitted when a
> consumer needs a lane.
>
> This changes how existing sub-parent epilogues are emitted; which graphs fuse
> is unchanged. It matters once the next two commits route the MXFP6 preparation
> graphs through the resolver. On native MI350X, a fused MXFP6 preparation
> kernel (an elementwise producer followed by a per-group pack) is about 1.3x
> slower with only the tile bitcast and no packed split, and falls behind a
> hand-written kernel. With this change it beats that kernel on both the
> elementwise-producer and the padded layer-norm preparation graphs.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction_lanes.py \
>   test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py
> ```
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200464
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
… affine writes (pytorch#200462)"

This reverts commit d132e85.

Reverted pytorch#200462 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)))
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…writes (pytorch#200462)

Author's note: Forwarding a producer's live values required dense writes, so padded producers (row pitch wider than width) couldn't fuse. I reviewed the conservative non-overlap proof, which only declines on unknown sizes; it was also checked against brute-force address enumeration.

The description below was drafted with an AI assistant (Claude):

> Fusing a consumer that forwards a producer's live values requires knowing the
> producer writes each address from exactly one iteration. The existing check
> only accepts dense (contiguous) writes, so a producer whose logical rows are
> narrower than their storage pitch (e.g. width 1056, pitch 1088, from a padded
> output) is rejected even though its rows never overlap.
>
> Add `MemoryDep.is_non_overlapping`: the index must equal its affine
> reconstruction `offset + sum(stride_i * var_i)`, and with axes ordered by
> stride, each stride must exceed the largest displacement the inner axes can
> reach. Size hints only choose the order; every inequality is checked with
> `statically_known_*`, so a wrong hint can only make the proof decline. Offsets
> containing indirect (TMP) symbols vary per iteration and decline too.
>
> This only replaces the density clause of
> `_memory_dep_supports_index_equivalence`; callers still prove that each read
> matches the write of the same iteration.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_dependencies.py test/inductor/test_inductor_scheduler.py
> python -m pytest test/inductor/test_nested_reduction_lanes.py -k pitched_group_lanes
> ```
> `test_non_overlapping_addresses` checks every accepted map against its
> enumerated addresses; a separate brute-force sweep over 18k rank-1..3 maps
> found no accepted map with repeated addresses.
> `test_pitched_group_lanes_and_aliases` reads a fixed lane of a producer whose
> row pitch is wider than its width; its `escape` and `dynamic` cases fail
> without this change.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200462
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…stages (pytorch#200463)

Author's note: A grouped reduction over fp16 labeled its fp32 tile as fp16, so a later bitcast emitted an invalid Triton cast and compilation failed; this is an upstream bug found by fuzzing. The reshape now keeps the input's dtype and the result uses the same accumulation type as `TritonKernel.reduction`.

The description below was drafted with an AI assistant (Claude):

> `_GroupedReductionOpsHandler.reduction` reshapes and reduces a grouped stage
> value with the source dtype, but like `TritonKernel.reduction` the tile is
> computed in float32 for float16/bfloat16 inputs. The reduced value is then
> labelled float16 while holding a float32 tile, so a later bitcast emits
> `<fp32 tile>.to(tl.int16, bitcast=True)`, which Triton rejects. Use the upcast
> compute type for both the reshape and the reduce.
>
> Found by fuzzing the nested-reduction lane paths; it reproduces on upstream
> main without them (`amin` over fp16 groups, then `.view(torch.int16) + 1`).
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction.py -k half_grouped_reduction_bitcast
> ```
> `test_half_grouped_reduction_bitcast_epilogue` fails with a Triton compilation
> error without the fix (persistent and looped variants) and passes with it.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200463
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…ytorch#200464)

Author's note: Reading fixed lanes of a live tile used to round and bitcast every lane separately after a 31-way split, which is slow on MI350X. Now the bitcast runs once on the whole tile and 16-bit lanes are split as packed 32-bit pairs (16 splits); I verified MI350X performance natively.

The description below was drafted with an AI assistant (Claude):

> Sub-parent epilogues read fixed lanes of a group tile: lane k of a
> `[..., groups, G]` tile is extracted with a reshape and a `tl.split` tree.
> Inductor inlines the pointwise chain between the source and each lane read
> into every lane's consumer. For the MXFP6 pack (`bits(v[2j]) |
> bits(v[2j+1]) << 16` over a group of 32 bf16 values), each lane is split out
> as fp32 and then rounded to bf16 and bitcast on its own. The split lanes have
> replicated layouts, so every thread repeats that work for each lane, and on
> MI350X the 32 separate 16-bit lanes also cost extra layout conversions.
>
> Three changes, all local to the existing split path:
>
> - When every lane of a source is read, a `to_dtype_bitcast` of the source is
>   emitted once at parent width, so the existing parent-twin fold collapses
>   each lane's cast chain into a split of the integer tile.
>   `to_dtype_bitcast` is registered as a pointwise op for that fold.
> - `emit_split_via_reshape` moves 16-bit lanes (int16/fp16/bf16, power-of-two
>   factor > 2) in pairs: split even/odd once, pack each pair into a uint32,
>   run the existing recursive split on the packed tile (16 splits instead of
>   31 at G=32), and unpack each lane to its original name and dtype.
> - In looped kernels the recursive split takes contiguous halves (via
>   `tl.permute`) instead of even/odd lanes, which needs fewer layout
>   conversions there; persistent kernels keep even/odd, which is faster for
>   them.
>
> Sources are no longer split eagerly before the epilogue: `check_sources` only
> checks that required sources are live, and a split is emitted when a
> consumer needs a lane.
>
> This changes how existing sub-parent epilogues are emitted; which graphs fuse
> is unchanged. It matters once the next two commits route the MXFP6 preparation
> graphs through the resolver. On native MI350X, a fused MXFP6 preparation
> kernel (an elementwise producer followed by a per-group pack) is about 1.3x
> slower with only the tile bitcast and no packed split, and falls behind a
> hand-written kernel. With this change it beats that kernel on both the
> elementwise-producer and the padded layer-norm preparation graphs.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction_lanes.py \
>   test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py
> ```
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200464
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 5, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…writes (pytorch#200462)

Author's note: Forwarding a producer's live values required dense writes, so padded producers (row pitch wider than width) couldn't fuse. I reviewed the conservative non-overlap proof, which only declines on unknown sizes; it was also checked against brute-force address enumeration.

The description below was drafted with an AI assistant (Claude):

> Fusing a consumer that forwards a producer's live values requires knowing the
> producer writes each address from exactly one iteration. The existing check
> only accepts dense (contiguous) writes, so a producer whose logical rows are
> narrower than their storage pitch (e.g. width 1056, pitch 1088, from a padded
> output) is rejected even though its rows never overlap.
>
> Add `MemoryDep.is_non_overlapping`: the index must equal its affine
> reconstruction `offset + sum(stride_i * var_i)`, and with axes ordered by
> stride, each stride must exceed the largest displacement the inner axes can
> reach. Size hints only choose the order; every inequality is checked with
> `statically_known_*`, so a wrong hint can only make the proof decline. Offsets
> containing indirect (TMP) symbols vary per iteration and decline too.
>
> This only replaces the density clause of
> `_memory_dep_supports_index_equivalence`; callers still prove that each read
> matches the write of the same iteration.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_dependencies.py test/inductor/test_inductor_scheduler.py
> python -m pytest test/inductor/test_nested_reduction_lanes.py -k pitched_group_lanes
> ```
> `test_non_overlapping_addresses` checks every accepted map against its
> enumerated addresses; a separate brute-force sweep over 18k rank-1..3 maps
> found no accepted map with repeated addresses.
> `test_pitched_group_lanes_and_aliases` reads a fixed lane of a producer whose
> row pitch is wider than its width; its `escape` and `dynamic` cases fail
> without this change.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200462
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…stages (pytorch#200463)

Author's note: A grouped reduction over fp16 labeled its fp32 tile as fp16, so a later bitcast emitted an invalid Triton cast and compilation failed; this is an upstream bug found by fuzzing. The reshape now keeps the input's dtype and the result uses the same accumulation type as `TritonKernel.reduction`.

The description below was drafted with an AI assistant (Claude):

> `_GroupedReductionOpsHandler.reduction` reshapes and reduces a grouped stage
> value with the source dtype, but like `TritonKernel.reduction` the tile is
> computed in float32 for float16/bfloat16 inputs. The reduced value is then
> labelled float16 while holding a float32 tile, so a later bitcast emits
> `<fp32 tile>.to(tl.int16, bitcast=True)`, which Triton rejects. Use the upcast
> compute type for both the reshape and the reduce.
>
> Found by fuzzing the nested-reduction lane paths; it reproduces on upstream
> main without them (`amin` over fp16 groups, then `.view(torch.int16) + 1`).
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction.py -k half_grouped_reduction_bitcast
> ```
> `test_half_grouped_reduction_bitcast_epilogue` fails with a Triton compilation
> error without the fix (persistent and looped variants) and passes with it.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200463
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…ytorch#200464)

Author's note: Reading fixed lanes of a live tile used to round and bitcast every lane separately after a 31-way split, which is slow on MI350X. Now the bitcast runs once on the whole tile and 16-bit lanes are split as packed 32-bit pairs (16 splits); I verified MI350X performance natively.

The description below was drafted with an AI assistant (Claude):

> Sub-parent epilogues read fixed lanes of a group tile: lane k of a
> `[..., groups, G]` tile is extracted with a reshape and a `tl.split` tree.
> Inductor inlines the pointwise chain between the source and each lane read
> into every lane's consumer. For the MXFP6 pack (`bits(v[2j]) |
> bits(v[2j+1]) << 16` over a group of 32 bf16 values), each lane is split out
> as fp32 and then rounded to bf16 and bitcast on its own. The split lanes have
> replicated layouts, so every thread repeats that work for each lane, and on
> MI350X the 32 separate 16-bit lanes also cost extra layout conversions.
>
> Three changes, all local to the existing split path:
>
> - When every lane of a source is read, a `to_dtype_bitcast` of the source is
>   emitted once at parent width, so the existing parent-twin fold collapses
>   each lane's cast chain into a split of the integer tile.
>   `to_dtype_bitcast` is registered as a pointwise op for that fold.
> - `emit_split_via_reshape` moves 16-bit lanes (int16/fp16/bf16, power-of-two
>   factor > 2) in pairs: split even/odd once, pack each pair into a uint32,
>   run the existing recursive split on the packed tile (16 splits instead of
>   31 at G=32), and unpack each lane to its original name and dtype.
> - In looped kernels the recursive split takes contiguous halves (via
>   `tl.permute`) instead of even/odd lanes, which needs fewer layout
>   conversions there; persistent kernels keep even/odd, which is faster for
>   them.
>
> Sources are no longer split eagerly before the epilogue: `check_sources` only
> checks that required sources are live, and a split is emitted when a
> consumer needs a lane.
>
> This changes how existing sub-parent epilogues are emitted; which graphs fuse
> is unchanged. It matters once the next two commits route the MXFP6 preparation
> graphs through the resolver. On native MI350X, a fused MXFP6 preparation
> kernel (an elementwise producer followed by a per-group pack) is about 1.3x
> slower with only the tile bitcast and no packed split, and falls behind a
> hand-written kernel. With this change it beats that kernel on both the
> elementwise-producer and the padded layer-norm preparation graphs.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction_lanes.py \
>   test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py
> ```
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200464
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
… affine writes (pytorch#200462)"

This reverts commit d132e85.

Reverted pytorch#200462 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)))
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…writes (pytorch#200462)

Author's note: Forwarding a producer's live values required dense writes, so padded producers (row pitch wider than width) couldn't fuse. I reviewed the conservative non-overlap proof, which only declines on unknown sizes; it was also checked against brute-force address enumeration.

The description below was drafted with an AI assistant (Claude):

> Fusing a consumer that forwards a producer's live values requires knowing the
> producer writes each address from exactly one iteration. The existing check
> only accepts dense (contiguous) writes, so a producer whose logical rows are
> narrower than their storage pitch (e.g. width 1056, pitch 1088, from a padded
> output) is rejected even though its rows never overlap.
>
> Add `MemoryDep.is_non_overlapping`: the index must equal its affine
> reconstruction `offset + sum(stride_i * var_i)`, and with axes ordered by
> stride, each stride must exceed the largest displacement the inner axes can
> reach. Size hints only choose the order; every inequality is checked with
> `statically_known_*`, so a wrong hint can only make the proof decline. Offsets
> containing indirect (TMP) symbols vary per iteration and decline too.
>
> This only replaces the density clause of
> `_memory_dep_supports_index_equivalence`; callers still prove that each read
> matches the write of the same iteration.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_dependencies.py test/inductor/test_inductor_scheduler.py
> python -m pytest test/inductor/test_nested_reduction_lanes.py -k pitched_group_lanes
> ```
> `test_non_overlapping_addresses` checks every accepted map against its
> enumerated addresses; a separate brute-force sweep over 18k rank-1..3 maps
> found no accepted map with repeated addresses.
> `test_pitched_group_lanes_and_aliases` reads a fixed lane of a producer whose
> row pitch is wider than its width; its `escape` and `dynamic` cases fail
> without this change.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200462
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…stages (pytorch#200463)

Author's note: A grouped reduction over fp16 labeled its fp32 tile as fp16, so a later bitcast emitted an invalid Triton cast and compilation failed; this is an upstream bug found by fuzzing. The reshape now keeps the input's dtype and the result uses the same accumulation type as `TritonKernel.reduction`.

The description below was drafted with an AI assistant (Claude):

> `_GroupedReductionOpsHandler.reduction` reshapes and reduces a grouped stage
> value with the source dtype, but like `TritonKernel.reduction` the tile is
> computed in float32 for float16/bfloat16 inputs. The reduced value is then
> labelled float16 while holding a float32 tile, so a later bitcast emits
> `<fp32 tile>.to(tl.int16, bitcast=True)`, which Triton rejects. Use the upcast
> compute type for both the reshape and the reduce.
>
> Found by fuzzing the nested-reduction lane paths; it reproduces on upstream
> main without them (`amin` over fp16 groups, then `.view(torch.int16) + 1`).
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction.py -k half_grouped_reduction_bitcast
> ```
> `test_half_grouped_reduction_bitcast_epilogue` fails with a Triton compilation
> error without the fix (persistent and looped variants) and passes with it.
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200463
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…ytorch#200464)

Author's note: Reading fixed lanes of a live tile used to round and bitcast every lane separately after a 31-way split, which is slow on MI350X. Now the bitcast runs once on the whole tile and 16-bit lanes are split as packed 32-bit pairs (16 splits); I verified MI350X performance natively.

The description below was drafted with an AI assistant (Claude):

> Sub-parent epilogues read fixed lanes of a group tile: lane k of a
> `[..., groups, G]` tile is extracted with a reshape and a `tl.split` tree.
> Inductor inlines the pointwise chain between the source and each lane read
> into every lane's consumer. For the MXFP6 pack (`bits(v[2j]) |
> bits(v[2j+1]) << 16` over a group of 32 bf16 values), each lane is split out
> as fp32 and then rounded to bf16 and bitcast on its own. The split lanes have
> replicated layouts, so every thread repeats that work for each lane, and on
> MI350X the 32 separate 16-bit lanes also cost extra layout conversions.
>
> Three changes, all local to the existing split path:
>
> - When every lane of a source is read, a `to_dtype_bitcast` of the source is
>   emitted once at parent width, so the existing parent-twin fold collapses
>   each lane's cast chain into a split of the integer tile.
>   `to_dtype_bitcast` is registered as a pointwise op for that fold.
> - `emit_split_via_reshape` moves 16-bit lanes (int16/fp16/bf16, power-of-two
>   factor > 2) in pairs: split even/odd once, pack each pair into a uint32,
>   run the existing recursive split on the packed tile (16 splits instead of
>   31 at G=32), and unpack each lane to its original name and dtype.
> - In looped kernels the recursive split takes contiguous halves (via
>   `tl.permute`) instead of even/odd lanes, which needs fewer layout
>   conversions there; persistent kernels keep even/odd, which is faster for
>   them.
>
> Sources are no longer split eagerly before the epilogue: `check_sources` only
> checks that required sources are live, and a split is emitted when a
> consumer needs a lane.
>
> This changes how existing sub-parent epilogues are emitted; which graphs fuse
> is unchanged. It matters once the next two commits route the MXFP6 preparation
> graphs through the resolver. On native MI350X, a fused MXFP6 preparation
> kernel (an elementwise producer followed by a per-group pack) is about 1.3x
> slower with only the tile bitcast and no packed split, and falls behind a
> hand-written kernel. With this change it beats that kernel on both the
> elementwise-producer and the padded layer-norm preparation graphs.
>
> Test Plan:
> ```
> python -m pytest test/inductor/test_nested_reduction_lanes.py \
>   test/inductor/test_inductor_scheduler.py test/inductor/test_nested_reduction.py
> ```
>
> This change was prepared with an AI assistant.

Pull Request resolved: pytorch#200464
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#200459, pytorch#200460, pytorch#200461, pytorch#200462, pytorch#200463
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
dominikjastrzebskix pushed a commit to dominikjastrzebskix/pytorch that referenced this pull request Nov 9, 2026
…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
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 Merged module: inductor ready for review Automated review passed, ready for a final review by a maintainer. Reverted topic: not user facing topic category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants