Skip to content

[inductor] Support XBLOCK nested grouped reductions - #182898

Closed
eellison wants to merge 27 commits into
gh/eellison/1014/basefrom
gh/eellison/1014/head
Closed

eellison wants to merge 27 commits into
gh/eellison/1014/basefrom
gh/eellison/1014/head

Conversation

@eellison

@eellison eellison commented May 7, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

cc @voznesenskym @penguinwu @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @chauhang @aakhundov @coconutruben @jataylo @mlazos

Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented May 7, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ No Failures

As of commit 646fc86 with merge base 333b108 (image):
💚 Looks good so far! There are no failures yet. 💚

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

Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

[ghstack-poisoned]
eellison added a commit that referenced this pull request May 8, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 3848a6f
Pull Request resolved: #182898
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

[ghstack-poisoned]
eellison added a commit that referenced this pull request May 8, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 433728f
Pull Request resolved: #182898
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

[ghstack-poisoned]
eellison added a commit that referenced this pull request May 8, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 138e071
Pull Request resolved: #182898
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

[ghstack-poisoned]
eellison added a commit that referenced this pull request May 8, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 607aa06
Pull Request resolved: #182898
[ghstack-poisoned]
eellison added a commit that referenced this pull request May 11, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: a6013b1
Pull Request resolved: #182898
[ghstack-poisoned]
eellison added a commit that referenced this pull request May 11, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 6a66787
Pull Request resolved: #182898
[ghstack-poisoned]
eellison added a commit that referenced this pull request May 11, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: e344a54
Pull Request resolved: #182898
[ghstack-poisoned]
eellison added a commit that referenced this pull request May 11, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 816c4fc
Pull Request resolved: #182898
[ghstack-poisoned]
eellison added a commit that referenced this pull request May 12, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

ghstack-source-id: 83847c7
Pull Request resolved: #182898
@claude

claude Bot commented May 15, 2026 •

Copy link
Copy Markdown

Claude finished @PaulZhang12's task in 2m 0s —— View job


Review: XBLOCK Nested Grouped Reductions

  • Read PR diff and understand the changes
  • Review torch/_inductor/scheduler.py changes
  • Review torch/_inductor/codegen/simd.py changes
  • Review test changes
  • Summarize findings

This PR extends the nested reduction fusion from only handling the "group-in-R" case (split parent R tile) to also handling "group-in-X" (split parent X tile). The change is well-structured: _GroupedReductionLayout is parameterized by local_reduction_in_r, and the scheduler gains a fallback heuristic (_get_grouped_axis_from_loop_body) for cases where size-based axis inference is ambiguous.

Overall this is a clean extension of an existing pattern. The approval from @jansel and green CI give confidence the approach is sound. A few observations:


1. Potential redundant divisibility check (scheduler.py:890-896)

grouped_parent_extent = (
    outer_rnumel if grouped_axis is cls.GroupedAxis.R else outer_numel
)
if not V.graph.sizevars.statically_known_equals(
    sympy.Mod(grouped_parent_extent, group_size), 0
):
    return False

This variable grouped_parent_extent is computed identically to parent_grouped_axis (line 871-873). The divisibility check at line 893 is conceptually the same constraint validated earlier (line 879-885 for the 2D case, and line 883-885 for the 1D case). In the 2D path, FloorDiv(parent_grouped_axis, group_size) == grouped_axis_groups already implies divisibility assuming integer arithmetic — though FloorDiv in sympy is a floor divide, so it doesn't strictly prove Mod == 0. For the 1D branch, the check is identical. This may be intentionally conservative — just flagging that it could be unified or a comment could clarify the intent.

2. _get_grouped_axis_from_loop_body ambiguity heuristic (scheduler.py:974-1039)

This is the most complex new logic. The approach of matching coefficient patterns between outer and grouped loop bodies is clever but relies on:

  • Load expressions being linear in the loop variables (using coeff)
  • A shared buffer name existing between both bodies
  • The coefficient of the grouped reduce var uniquely matching either an outer reduce var or an outer iter var

Edge case to consider: If a buffer has multiple loads with different indexing patterns (e.g., a strided vs contiguous access), the inner loop iterates all (grouped_read_expr, outer_read_expr) pairs. A mismatch within the same buffer would return None (line 1036-1037). This is correct conservative behavior. However, if buffer A says "R" and buffer B says "X" (different buffers, different candidates), the code would also return None. This seems right for safety but could reject valid fusions where the conflicting buffer isn't actually shared between the kernels.

The unit test (test_nested_reduction_axis_from_loop_body) covers the key cases well, including the ambiguous case (last assertion returning None).

3. _select_full_resolution_pointwise_source priority ordering (simd.py:2791-2808)

def _select_full_resolution_pointwise_source(self, sn, parent_full_source, local_reduction_source):
    if self._ranges_exactly_match_groups(parent_full_source.groups, sn_ranges):
        return parent_full_source
    if self._ranges_exactly_match_groups(local_reduction_source.groups, sn_ranges):
        return local_reduction_source
    if SIMDKernel.is_compatible(parent_full_source.groups, sn_ranges):
        return parent_full_source
    if SIMDKernel.is_compatible(local_reduction_source.groups, sn_ranges):
        return local_reduction_source
    raise AssertionError(...)

The fallback from "exact match" to is_compatible is a sensible tiered approach. Worth noting: the AssertionError here could theoretically be hit if a node is classified as PARENT_FULL by the scheduler but its actual ranges don't match either source. This would be a bug in classification logic rather than a codegen bug — the test test_fullres_x_epilogue_rejects_intermediate_dependency validates the rejection path for problematic consumers.

4. parent_full_iteration_values for the X-split case (simd.py:1752-1773)

For local_reduction_in_r=False, the source returns 3 groups: [num_groups, local_reduction_size, passthrough_tree.numel]. This 3-element iteration space is a different shape than the 2-element one used in the R case. The _map_iteration_values_to_node_sizes path must handle this correctly. Since SIMDKernel.map_kernel_groups_to_node_sizes uses split_values_by_ranges which calls decompose_index, this should work as long as the product of groups equals the product of the target node's ranges — which is guaranteed by the earlier divisibility/equality checks.

5. Minor: operator precedence in list comprehension (simd.py:1675-1679)

iter_remapped = [
    passthrough_var * self.num_groups + group_index_expr
    if self.local_reduction_in_r
    else group_index_expr * self.passthrough_tree.numel + passthrough_var
]

This is a ternary expression inside a single-element list. It reads correctly but could be misread as a list comprehension with an if filter. A set of parentheses around the ternary would improve clarity:

iter_remapped = [
    (passthrough_var * self.num_groups + group_index_expr
     if self.local_reduction_in_r
     else group_index_expr * self.passthrough_tree.numel + passthrough_var)
]

This is purely stylistic — no functional concern.

6. Test coverage

The tests are comprehensive:

  • test_fullres_epilogue_small_dim_in_x: validates the fusion now works with a _post_fusion_custom_pass to inspect FusedNestedReductions internals
  • test_fullres_x_epilogue_rejects_intermediate_dependency: ensures safety when a consumer depends on something produced after the nested reduction
  • test_reject_bad_x_group_size: validates non-power-of-2 and too-large group sizes are rejected
  • test_reject_x_grouped_reduction_with_three_iter_dims: validates the 3D X-axis case is properly rejected
  • test_nested_reduction_axis_from_loop_body: unit test for the coefficient-matching heuristic

Summary: This is a well-executed extension of existing infrastructure. The code is well-factored with the local_reduction_in_r flag cleanly parametrizing the reshape/broadcast logic. The heuristic in _get_grouped_axis_from_loop_body is the most delicate addition — it handles ambiguity conservatively, which is the right default. No blocking issues found.

eellison added 8 commits May 15, 2026 09:10
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@eellison

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

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

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: inductor / unit-test / linux-jammy-cpu-py3.12-gcc11-inductor-triton-cpu / test (inductor-triton-cpu, 1, 1, linux.8xlarge.amx)

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

@eellison

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -f "unrelated failures / taking too long"

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

The merge job was canceled or timed out. This most often happen if two merge requests were issued for the same PR, or if merge job was waiting for more than 6 hours for tests to finish. In later case, please do not hesitate to reissue the merge command
For more information see pytorch-bot wiki.

@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

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge failed

Reason: Command git -C /home/runner/work/pytorch/pytorch cherry-pick -x 0b6462cab49ecf4e597459970ae2a879ec4e367d returned non-zero exit code 1

CONFLICT (modify/delete): test/inductor/test_nested_reduction.py deleted in HEAD and modified in 0b6462cab49 ([inductor] Support XBLOCK nested grouped reductions).  Version 0b6462cab49 ([inductor] Support XBLOCK nested grouped reductions) of test/inductor/test_nested_reduction.py left in tree.
Auto-merging torch/_inductor/codegen/simd.py
CONFLICT (content): Merge conflict in torch/_inductor/codegen/simd.py
Auto-merging torch/_inductor/scheduler.py
CONFLICT (content): Merge conflict in torch/_inductor/scheduler.py
error: could not apply 0b6462cab49... [inductor] Support XBLOCK nested grouped reductions
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Details for Dev Infra team Raised by workflow job

[ghstack-poisoned]
@eellison

Copy link
Copy Markdown
Contributor Author

@pytorchbot merge -i

@pytorchmergebot

Copy link
Copy Markdown
Collaborator

Merge started

Your change will be merged while ignoring the following 1 checks: inductor / unit-test / inductor-triton-cpu-build / build

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 pushed a commit to weishi-deng/pytorch that referenced this pull request May 21, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

Pull Request resolved: pytorch#182898
Approved by: https://github.com/jansel
@github-actions
github-actions Bot deleted the gh/eellison/1014/head branch June 20, 2026 02:28
gplutop7 pushed a commit to gplutop7/pytorch that referenced this pull request Jul 15, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

Pull Request resolved: pytorch#182898
Approved by: https://github.com/jansel
aws-kingrj pushed a commit to amazon-contributing/upstream-to-pytorch that referenced this pull request Jul 29, 2026
Extend nested reduction fusion to the complementary case where the grouped local reduction splits the parent X tile instead of the R tile. The scheduler classifies pointwise nodes against grouped-body and parent-tile domains, then regular fusion legality decides which follow-up consumers are admitted. Codegen maps each admitted full-resolution pointwise node using the domain matching its actual ranges, so degenerate collapsed shapes such as B=1 lower correctly without adding generic broadcast-dependency equivalence.

Authored with Claude.

Pull Request resolved: pytorch#182898
Approved by: https://github.com/jansel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/inductor ciflow/torchtitan Run TorchTitan integration tests ciflow/trunk Trigger trunk jobs on your pull request Merged module: inductor release notes: inductor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants