Skip to content

[inductor] Support atomic template reduction groups - #190822

Closed
mlazos wants to merge 59 commits into
gh/mlazos/274/basefrom
gh/mlazos/274/head
Closed

mlazos wants to merge 59 commits into
gh/mlazos/274/basefrom
gh/mlazos/274/head

Conversation

@mlazos

@mlazos mlazos commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Add scheduler hooks that let a CUDA template backend keep a validated
reduction region together during fusion. Reduction-pair compatibility and
reduction-epilogue decisions are delegated to the backend before generic
fusion can consume only part of the region.

NVGEMM uses these hooks to prioritize supported reduction chains, reject
conflicting partial fusions, and keep NVGEMM boolean outputs out of unrelated
horizontal fusion. The Loop IR capture and validation logic lives in the
preceding commits; this commit only adds the scheduler coordination needed to
treat its result atomically.

Test Plan

lintrunner -a

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 Jul 22, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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 2937357 with merge base 3ee461f (image):

BROKEN TRUNK - The following job failed but were present on the merge base:

👉 Rebase onto the `viable/strict` branch to avoid these failures

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

This was referenced Jul 22, 2026
@mlazos

mlazos commented Jul 22, 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.

[ghstack-poisoned]
mlazos added a commit that referenced this pull request Jul 23, 2026
Allow template scheduling to claim a complete reduction chain as one fusion
unit and retain the output identity needed by multi-output template kernels.
This prevents partial fusion from consuming layout or finalizer nodes before
NVGEMM can validate the complete grouped pattern.

Test Plan:

```bash
lintrunner -a
```

Authored with an AI assistant.


ghstack-source-id: 1b35327
Pull-Request: #190822
[ghstack-poisoned]
mlazos added 2 commits August 28, 2026 14:08
[ghstack-poisoned]
[ghstack-poisoned]
mlazos added 2 commits August 28, 2026 19:08
[ghstack-poisoned]
[ghstack-poisoned]
mlazos added 2 commits August 31, 2026 21:56
[ghstack-poisoned]
[ghstack-poisoned]
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #192928

1 similar comment
@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Starting merge as part of PR stack under #192928

pytorchmergebot pushed a commit that referenced this pull request Sep 2, 2026
prepares dense kernel for direct cutedsl codegen (instead of EVT)

> This updates the vendored dense matrix-multiply kernel so it can run the
> post-processing function generated directly by Inductor. It maps captured
> tensors such as biases into the layout expected by the kernel, exposes the
> operations used by the generated function, and stores each requested output.
>
> The older metadata format remains supported in this commit so the kernel
> change can land separately from the scheduler change that starts using the
> direct path.
>
> ## Example
>
> The direct epilogue represents a computation such as:
>
> ```python
> def fn(a, b, bias):
>     return torch.relu((a @ b) + bias)
> ```
>
> Given generated source for this epilogue, the adapter maps the bias into the
> dense kernel's broadcast layout, evaluates the addition and ReLU, and stores
> the result. This commit teaches the provider to accept that direct function;
> the following commit switches scheduler selection from the older CUTLASS
> expression-graph representation to this path.
>
> ## Test Plan
>
> ```bash
> CUDA_VISIBLE_DEVICES=0 TORCHINDUCTOR_COMPILE_THREADS=1 python test/inductor/test_nv_universal_gemm.py -k matmul_pointwise_epilogue_fusion -v
> CUDA_VISIBLE_DEVICES=0 TORCHINDUCTOR_COMPILE_THREADS=1 python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_matmul_multi_store_epilogue_fusion -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.
Pull Request resolved: #191514
Approved by: https://github.com/drisspg
ghstack dependencies: #192298, #190822
pytorchmergebot pushed a commit that referenced this pull request Sep 2, 2026
…191515)

> Enable shared Loop IR to CuTeDSL lowering for dense NVGEMM epilogues. The
> scheduler selects generated CuTeDSL source when the complete captured
> expression is supported and retains CUTLASS EVT lowering as the fallback.
>
> Preserve captured input dtypes and broadcast layouts, compose bias through
> the same epilogue plan, reuse common subexpressions, and reject unsupported
> operand swaps or reduction layouts before selecting the direct path.
>
> ## Example
>
> ```python
> result = (a @ b).float()
> return torch.relu(result), result + bias
> ```
>
> Both stores are generated from one captured Loop IR program. Expressions that
> cannot be represented by the direct path continue through EVT.
>
> ## Test Plan
>
> ```bash
> PYTORCH_WORKTREE=$PWD env -u PYTHONPATH python agent_space/run_worktree_python.py test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_matmul_multi_store_epilogue_fusion -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: #191515
Approved by: https://github.com/drisspg
ghstack dependencies: #192298, #190822, #191514
pytorchmergebot pushed a commit that referenced this pull request Sep 2, 2026
> Add focused parity coverage for generated CuTeDSL callbacks across dense and
> block-scaled grouped reductions. The tests cover both reduction axes,
> primitive and pointwise-transformed sources, multiple reductions in one
> epilogue, reduction consumers, and finalization after cross-warp combination.
>
> Keep the transposed output layout explicit when NVGEMM swaps the GEMM operands.
> Let FlexGEMM request the broader tuned NVGEMM configuration while rejecting
> unsupported NVGEMM options.
>
> ## Test Plan
>
> ```bash
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmHeuristics -v
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_m_reduce_finalizes_after_cross_warp_combine TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_n_composite_reduction_fusion TestNVUniversalGemmEpilogueFusion.test_scaled_mm_grouped_n_composite_reduction_fusion TestNVUniversalGemmEpilogueFusion.test_scaled_mm_grouped_m_reduce_finalizes_after_cross_warp_combine -v
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_reduce_epilogue_fusion_axis_1_group_4_sum TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_reduce_epilogue_fusion_axis_0_group_64_abs_amax TestNVUniversalGemmEpilogueFusion.test_flex_gemm_grouped_n_reduce_epilogue_fusion_feeds_main_True -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: #190825
Approved by: https://github.com/karthickai
ghstack dependencies: #192298, #190822, #191514, #191515
pytorchmergebot pushed a commit that referenced this pull request Sep 2, 2026
doesn't run hese tests in fbcode until we add operator API internally

> This skips two low-level tests only in fbcode because fbcode does not include
> the optional CUTLASS package those tests use to construct and run kernel
> objects directly. The tests fail while importing or creating the operator,
> before they exercise the NVGEMM behavior they are intended to check.
>
> The tests still run in the open-source build, and the higher-level
> `torch.compile` tests remain enabled in fbcode.
>
> ## Architecture
>
> This is a test-only environment boundary. The two tests that directly import
> the optional CUTLASS Operator API are skipped individually when `IS_FBCODE` is
> true. No production lowering, provider registration, or generated-kernel
> behavior changes. The tests continue running in open source, while fbcode
> retains the higher-level compiler coverage.
>
> ## Example
>
> The affected tests directly compile a grouped-reduction provider and a
> unit-dimension epilogue on a non-current CUDA stream. In fbcode, the required
> operator-construction API is absent, so skipping those two tests avoids
> reporting an environment limitation as an NVGEMM correctness failure.
>
>
> ```python
> @unittest.skipIf(
>     IS_FBCODE,
>     "CUTLASS Operator API is not available in fbcode",
> )
> def test_scaled_gemm_grouped_n_reduce_provider(self):
>     ...
> ```
>
> ## Test Plan
>
> ```bash
> PYTORCH_TEST_FBCODE=1 python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemm.test_scaled_gemm_grouped_n_reduce_provider TestNVUniversalGemm.test_scaled_gemm_unit_dim_epilogue_non_current_stream -v
> python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemm.test_scaled_gemm_grouped_n_reduce_provider TestNVUniversalGemm.test_scaled_gemm_unit_dim_epilogue_non_current_stream -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: #192928
Approved by: https://github.com/karthickai
ghstack dependencies: #192298, #190822, #191514, #191515, #190825
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
<human note needed>

> Add scheduler hooks that let a CUDA template backend keep a validated
> reduction region together during fusion. Reduction-pair compatibility and
> reduction-epilogue decisions are delegated to the backend before generic
> fusion can consume only part of the region.
>
> NVGEMM uses these hooks to prioritize supported reduction chains, reject
> conflicting partial fusions, and keep NVGEMM boolean outputs out of unrelated
> horizontal fusion. The Loop IR capture and validation logic lives in the
> preceding commits; this commit only adds the scheduler coordination needed to
> treat its result atomically.
>
> ## Test Plan
>
> ```bash
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.
Pull Request resolved: pytorch#190822
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#192298
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…ch#191514)

prepares dense kernel for direct cutedsl codegen (instead of EVT)

> This updates the vendored dense matrix-multiply kernel so it can run the
> post-processing function generated directly by Inductor. It maps captured
> tensors such as biases into the layout expected by the kernel, exposes the
> operations used by the generated function, and stores each requested output.
>
> The older metadata format remains supported in this commit so the kernel
> change can land separately from the scheduler change that starts using the
> direct path.
>
> ## Example
>
> The direct epilogue represents a computation such as:
>
> ```python
> def fn(a, b, bias):
>     return torch.relu((a @ b) + bias)
> ```
>
> Given generated source for this epilogue, the adapter maps the bias into the
> dense kernel's broadcast layout, evaluates the addition and ReLU, and stores
> the result. This commit teaches the provider to accept that direct function;
> the following commit switches scheduler selection from the older CUTLASS
> expression-graph representation to this path.
>
> ## Test Plan
>
> ```bash
> CUDA_VISIBLE_DEVICES=0 TORCHINDUCTOR_COMPILE_THREADS=1 python test/inductor/test_nv_universal_gemm.py -k matmul_pointwise_epilogue_fusion -v
> CUDA_VISIBLE_DEVICES=0 TORCHINDUCTOR_COMPILE_THREADS=1 python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_matmul_multi_store_epilogue_fusion -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.
Pull Request resolved: pytorch#191514
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#192298, pytorch#190822
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
…ytorch#191515)

> Enable shared Loop IR to CuTeDSL lowering for dense NVGEMM epilogues. The
> scheduler selects generated CuTeDSL source when the complete captured
> expression is supported and retains CUTLASS EVT lowering as the fallback.
>
> Preserve captured input dtypes and broadcast layouts, compose bias through
> the same epilogue plan, reuse common subexpressions, and reject unsupported
> operand swaps or reduction layouts before selecting the direct path.
>
> ## Example
>
> ```python
> result = (a @ b).float()
> return torch.relu(result), result + bias
> ```
>
> Both stores are generated from one captured Loop IR program. Expressions that
> cannot be represented by the direct path continue through EVT.
>
> ## Test Plan
>
> ```bash
> PYTORCH_WORKTREE=$PWD env -u PYTHONPATH python agent_space/run_worktree_python.py test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_matmul_multi_store_epilogue_fusion -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: pytorch#191515
Approved by: https://github.com/drisspg
ghstack dependencies: pytorch#192298, pytorch#190822, pytorch#191514
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
> Add focused parity coverage for generated CuTeDSL callbacks across dense and
> block-scaled grouped reductions. The tests cover both reduction axes,
> primitive and pointwise-transformed sources, multiple reductions in one
> epilogue, reduction consumers, and finalization after cross-warp combination.
>
> Keep the transposed output layout explicit when NVGEMM swaps the GEMM operands.
> Let FlexGEMM request the broader tuned NVGEMM configuration while rejecting
> unsupported NVGEMM options.
>
> ## Test Plan
>
> ```bash
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmHeuristics -v
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_m_reduce_finalizes_after_cross_warp_combine TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_n_composite_reduction_fusion TestNVUniversalGemmEpilogueFusion.test_scaled_mm_grouped_n_composite_reduction_fusion TestNVUniversalGemmEpilogueFusion.test_scaled_mm_grouped_m_reduce_finalizes_after_cross_warp_combine -v
> PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_reduce_epilogue_fusion_axis_1_group_4_sum TestNVUniversalGemmEpilogueFusion.test_bf16_grouped_reduce_epilogue_fusion_axis_0_group_64_abs_amax TestNVUniversalGemmEpilogueFusion.test_flex_gemm_grouped_n_reduce_epilogue_fusion_feeds_main_True -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: pytorch#190825
Approved by: https://github.com/karthickai
ghstack dependencies: pytorch#192298, pytorch#190822, pytorch#191514, pytorch#191515
Sanskaaar1 pushed a commit to Sanskaaar1/pytorch that referenced this pull request Sep 8, 2026
doesn't run hese tests in fbcode until we add operator API internally

> This skips two low-level tests only in fbcode because fbcode does not include
> the optional CUTLASS package those tests use to construct and run kernel
> objects directly. The tests fail while importing or creating the operator,
> before they exercise the NVGEMM behavior they are intended to check.
>
> The tests still run in the open-source build, and the higher-level
> `torch.compile` tests remain enabled in fbcode.
>
> ## Architecture
>
> This is a test-only environment boundary. The two tests that directly import
> the optional CUTLASS Operator API are skipped individually when `IS_FBCODE` is
> true. No production lowering, provider registration, or generated-kernel
> behavior changes. The tests continue running in open source, while fbcode
> retains the higher-level compiler coverage.
>
> ## Example
>
> The affected tests directly compile a grouped-reduction provider and a
> unit-dimension epilogue on a non-current CUDA stream. In fbcode, the required
> operator-construction API is absent, so skipping those two tests avoids
> reporting an environment limitation as an NVGEMM correctness failure.
>
>
> ```python
> @unittest.skipIf(
>     IS_FBCODE,
>     "CUTLASS Operator API is not available in fbcode",
> )
> def test_scaled_gemm_grouped_n_reduce_provider(self):
>     ...
> ```
>
> ## Test Plan
>
> ```bash
> PYTORCH_TEST_FBCODE=1 python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemm.test_scaled_gemm_grouped_n_reduce_provider TestNVUniversalGemm.test_scaled_gemm_unit_dim_epilogue_non_current_stream -v
> python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemm.test_scaled_gemm_grouped_n_reduce_provider TestNVUniversalGemm.test_scaled_gemm_unit_dim_epilogue_non_current_stream -v
> lintrunner -a
> ```
>
> Description drafted with an AI assistant.

Pull Request resolved: pytorch#192928
Approved by: https://github.com/karthickai
ghstack dependencies: pytorch#192298, pytorch#190822, pytorch#191514, pytorch#191515, pytorch#190825
pytorchmergebot pushed a commit that referenced this pull request Sep 10, 2026
Orient generated reduction geometry, output layouts, and runtime tensors when NVGEMM swaps GEMM operands. Dense and block-scaled epilogues continue to use the shared generated source, combine, and finalizer callbacks.

Keep the kernel reduction mechanism unchanged. Candidate validation filters physical tile layouts that cross CTA or warp ownership, leaving valid configurations in the normal autotuning set and falling back safely when a layout cannot implement the reduction.

## Test Plan

```bash
PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py -k swap_ab
PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py -k grouped_reduce
spin quicklint
lintrunner -a
```

Authored with an AI assistant.

Pull Request resolved: #195538
Approved by: https://github.com/karthickai
ghstack dependencies: #192298, #190822, #191514, #191515, #190825, #192928
pytorchmergebot pushed a commit that referenced this pull request Sep 10, 2026
Generate vendored dense EFC variants for the validated SM100 N-tile widths through 256, including 160, 192, and 224. This makes the configurations available to nvMatmulHeuristics without retaining them unconditionally.

Keep the existing supplemented benchmark space unchanged. Wider candidates enter autotuning only when nvMatmulHeuristics selects them, avoiding additional profiling by default.

## Test Plan

```bash
PYTHONPATH=/data/users/mlazos/pytorch/agent_space/runtime_overlay PYTORCH_BOUNDARY_ROOT=$PWD python test/inductor/test_nv_universal_gemm.py TestNVUniversalGemm.test_vendored_dense_efc_exposes_wide_n_tiles -v
lintrunner -a
```

Authored with an AI assistant.

Pull Request resolved: #195539
Approved by: https://github.com/karthickai
ghstack dependencies: #192298, #190822, #191514, #191515, #190825, #192928, #195538
@github-actions
github-actions Bot deleted the gh/mlazos/274/head branch November 14, 2026 01:42
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.

3 participants