Skip to content

[inductor][NVGEMM] Define scheduler Loop IR epilogue lowering - #194873

Closed
mlazos wants to merge 5 commits into
gh/mlazos/314/basefrom
gh/mlazos/314/head
Closed

mlazos wants to merge 5 commits into
gh/mlazos/314/basefrom
gh/mlazos/314/head

Conversation

@mlazos

@mlazos mlazos commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Introduce the semantic lowering and CuTeDSL emission layer for scheduler-owned
GEMM epilogues. A candidate sequence of scheduler nodes is captured as Loop IR,
classified from iteration ranges and access strides, and represented as one
NVGemmEpilogueProgram containing pointwise work, grouped reduction regions,
output ownership, and any callbacks required by the kernel.

Architecture

NVGemmEpilogueCapture interprets the candidate nodes without selecting a
named reduction algorithm. reduction_type identifies only the associative
primitive; the captured expressions define source transforms, finalizers,
consumers, and compositions of multiple reductions with pointwise operations.

LoopIRCuteDSLCodegen replays those expressions through the shared CuTeDSL
operation overrides. Fragment-local reductions remain in the generated tensor
epilogue. When a reduction crosses fragments or threads, the generated plan
supplies combine and finalize callbacks while the vendored kernel owns
traversal, synchronization, and storage.

NVGemmEpilogueProgram is the validation boundary used by scheduling. It
records which nodes belong to reduction regions, which outputs remain visible,
whether a reduction feeds the main GEMM output, and whether every reduction in
the candidate prefix has a supported lowering.

Examples

A pointwise epilogue becomes one generated callback:

result = a @ b
return torch.relu(result.float() + bias)

Multiple reductions and pointwise consumers remain one semantic program:

result = a @ b
grouped = result.float().view(m, n // 4, 4)
scale = grouped.sum(-1) + grouped.amax(-1)
return (grouped / scale.unsqueeze(-1)).reshape(m, n), scale

The second example is represented as an N-axis reduction with group size four.
The generated epilogue computes both primitive reductions and the division; a
larger or cross-thread geometry additionally receives generated physical
reduction callbacks.

Test Plan

spin lint torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py
lintrunner -a
python -m py_compile torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py

Description drafted 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 Aug 26, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

✅ You can merge normally! (1 Unrelated Failure)

As of commit 64f85c6 with merge base 96043a9 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

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

@mlazos

mlazos commented Aug 26, 2026 •

Copy link
Copy Markdown
Contributor Author

AI-Review

Automated multi-agent review (2 Claude + 2 Codex + GPT-5.6 verification). This is AI-generated — please verify findings manually.


No verified issues found.

@mlazos
mlazos marked this pull request as ready for review August 28, 2026 20:24
mlazos added 2 commits August 28, 2026 14:08
[ghstack-poisoned]
[ghstack-poisoned]

@drisspg drisspg 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.

stamp

@karthickai karthickai left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

could you add test coverage for this PR? also my agent found three blocking issues and it looks legitimate and also repro in my local machine.

  1. The explicit reduction check validates the index strides but not its constant offset. Since stride_vars() drops that offset, an index such as ... + 1 can be mistaken for an unshifted reduction, causing the generated kernel to reduce the wrong elements. Could we validate the complete affine index or reject non-zero offsets?

  2. source_from_expression() creates a unary source(value) callback, but the accepted expression can contain differently indexed GEMM loads. Because lower() discards the load index, an expression such as a * b becomes value * value. Could we require all loads to reference the same logical lane or reject the fusion?

  3. _output_scale_name() strips to_dtype operations even when the cast changes the value. For example, alpha=1.75 and alpha.to(int32)=1 are not equivalent, but the later native output-scale path receives raw 1.75. Could we only allow direct scalar loads or proven no-op conversions?

[ghstack-poisoned]
[ghstack-poisoned]
@mlazos
mlazos dismissed karthickai’s stale review September 1, 2026 05:31

Resolved Karthick's concerns

@mlazos

mlazos commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge

@pytorch-bot pytorch-bot Bot added the ciflow/trunk Trigger trunk jobs on your pull request label Sep 1, 2026
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged once all checks pass (ETA 0-4 Hours).

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: 1 jobs have failed, first few of them are: torchtitan-test / torchtitan-x-pytorch-test / test (torchtitan_models_integration, 1, 1, mt-l-x86aavx2-189-704-a10g-8)

Details for Dev Infra team Raised by workflow job

@mlazos

mlazos commented Sep 1, 2026

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "unrelated failures"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged immediately since you used the force (-f) flag, bypassing any CI checks (ETA: 1-5 minutes). Please use -f as last resort and instead consider -i/--ignore-current to continue the merge ignoring current failures. This will allow currently pending tests to finish and report signal before the merge.

Learn more about merging in the wiki.

Questions? Feedback? Please reach out to the PyTorch DevX Team

Advanced Debugging
Check the merge workflow status
here

orrangetabby17 pushed a commit to orrangetabby17/pytorch that referenced this pull request Sep 2, 2026
…h#194873)

<human note needed>

> Introduce the semantic lowering and CuTeDSL emission layer for scheduler-owned
> GEMM epilogues. A candidate sequence of scheduler nodes is captured as Loop IR,
> classified from iteration ranges and access strides, and represented as one
> `NVGemmEpilogueProgram` containing pointwise work, grouped reduction regions,
> output ownership, and any callbacks required by the kernel.
>
> ## Architecture
>
> `NVGemmEpilogueCapture` interprets the candidate nodes without selecting a
> named reduction algorithm. `reduction_type` identifies only the associative
> primitive; the captured expressions define source transforms, finalizers,
> consumers, and compositions of multiple reductions with pointwise operations.
>
> `LoopIRCuteDSLCodegen` replays those expressions through the shared CuTeDSL
> operation overrides. Fragment-local reductions remain in the generated tensor
> epilogue. When a reduction crosses fragments or threads, the generated plan
> supplies combine and finalize callbacks while the vendored kernel owns
> traversal, synchronization, and storage.
>
> `NVGemmEpilogueProgram` is the validation boundary used by scheduling. It
> records which nodes belong to reduction regions, which outputs remain visible,
> whether a reduction feeds the main GEMM output, and whether every reduction in
> the candidate prefix has a supported lowering.
>
> ## Examples
>
> A pointwise epilogue becomes one generated callback:
>
> ```python
> result = a @ b
> return torch.relu(result.float() + bias)
> ```
>
> Multiple reductions and pointwise consumers remain one semantic program:
>
> ```python
> result = a @ b
> grouped = result.float().view(m, n // 4, 4)
> scale = grouped.sum(-1) + grouped.amax(-1)
> return (grouped / scale.unsqueeze(-1)).reshape(m, n), scale
> ```
>
> The second example is represented as an N-axis reduction with group size four.
> The generated epilogue computes both primitive reductions and the division; a
> larger or cross-thread geometry additionally receives generated physical
> reduction callbacks.
>
> ## Test Plan
>
> ```bash
> spin lint torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py
> lintrunner -a
> python -m py_compile torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: pytorch#194873
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#194792, pytorch#194793, pytorch#190821, pytorch#195231
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…h#194873)

<human note needed>

> Introduce the semantic lowering and CuTeDSL emission layer for scheduler-owned
> GEMM epilogues. A candidate sequence of scheduler nodes is captured as Loop IR,
> classified from iteration ranges and access strides, and represented as one
> `NVGemmEpilogueProgram` containing pointwise work, grouped reduction regions,
> output ownership, and any callbacks required by the kernel.
>
> ## Architecture
>
> `NVGemmEpilogueCapture` interprets the candidate nodes without selecting a
> named reduction algorithm. `reduction_type` identifies only the associative
> primitive; the captured expressions define source transforms, finalizers,
> consumers, and compositions of multiple reductions with pointwise operations.
>
> `LoopIRCuteDSLCodegen` replays those expressions through the shared CuTeDSL
> operation overrides. Fragment-local reductions remain in the generated tensor
> epilogue. When a reduction crosses fragments or threads, the generated plan
> supplies combine and finalize callbacks while the vendored kernel owns
> traversal, synchronization, and storage.
>
> `NVGemmEpilogueProgram` is the validation boundary used by scheduling. It
> records which nodes belong to reduction regions, which outputs remain visible,
> whether a reduction feeds the main GEMM output, and whether every reduction in
> the candidate prefix has a supported lowering.
>
> ## Examples
>
> A pointwise epilogue becomes one generated callback:
>
> ```python
> result = a @ b
> return torch.relu(result.float() + bias)
> ```
>
> Multiple reductions and pointwise consumers remain one semantic program:
>
> ```python
> result = a @ b
> grouped = result.float().view(m, n // 4, 4)
> scale = grouped.sum(-1) + grouped.amax(-1)
> return (grouped / scale.unsqueeze(-1)).reshape(m, n), scale
> ```
>
> The second example is represented as an N-axis reduction with group size four.
> The generated epilogue computes both primitive reductions and the division; a
> larger or cross-thread geometry additionally receives generated physical
> reduction callbacks.
>
> ## Test Plan
>
> ```bash
> spin lint torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py
> lintrunner -a
> python -m py_compile torch/_inductor/codegen/nv_universal_gemm/epilogue_lowering.py torch/_inductor/kernel/loop_ir_cutedsl_codegen.py
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: pytorch#194873
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#194792, pytorch#194793, pytorch#190821, pytorch#195231
@github-actions
github-actions Bot deleted the gh/mlazos/314/head branch November 13, 2026 01:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants