Skip to content

[ET-VK] Spill per-row reductions past the y workgroup limit into z - #25290

Open
msluszniak wants to merge 3 commits into
pytorch:mainfrom
msluszniak:ms/vulkan-per-row-reduce-z-spill
Open

msluszniak wants to merge 3 commits into
pytorch:mainfrom
msluszniak:ms/vulkan-per-row-reduce-z-spill

Conversation

@msluszniak

@msluszniak msluszniak commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

reduce_per_row dispatches one workgroup per output row along y, so a reduction with more than maxComputeWorkGroupCount[1] rows (65535 on Adreno) fails the dispatch check from #22225. RF-DETR keypoint hits this with 435200 rows. Rows past the limit now go to z.

Test plan

  • New per_row op test case with 70000 rows.
  • Galaxy S26 Ultra (Adreno), same change on v1.6.0: RF-DETR keypoint runs again, and a 435200-row sum(-1) matches the expected values, while it aborts without the change.

cc @SS-JIA @manuelcandales @digantdesai @cbilgin @nil-is-all

reduce_per_row dispatches one workgroup per output row along y, so more than
maxComputeWorkGroupCount[1] rows (65535 on Adreno) fails the dispatch geometry
check from pytorch#22225. Rows past the limit now go to z and the shader recombines
the index.
@msluszniak
msluszniak requested a review from SS-JIA as a code owner November 19, 2026 13:28
@pytorch-bot

pytorch-bot Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

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

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

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

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 19, 2026
@executorch-triage executorch-triage Bot added the community: contribution PRs coming from community (excluding hardware partners) label Nov 19, 2026
@nil-is-all nil-is-all added module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ release notes: vulkan Changes to the Vulkan backend delegate labels Nov 23, 2026

@SS-JIA SS-JIA 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.

🤖 AI-generated review. These findings come from an independent automated review (Claude, via @SS-JIA) of the head commit dbbb85e9bc. Each finding has a severity, a short ID, and a proposed fix. Please sanity-check them before acting on them.

Recommendation: Requires Changes. The fix works: with a 65535 workgroup-count limit applied through a Vulkan profile layer, the 70000-row case fails on main and passes here. However, it adds a second wrapping scheme where a shared helper already exists (A1, inline).

Findings not tied to a diff line:

A2 (Low): Sibling one-workgroup-per-row ops still hit the same y limit. RmsNorm.cpp ({1u, num_rows, 1u}, buffer and texture), NativeLayerNorm.cpp (layer_norm_buffer_gwg), ChooseQParams.cpp, FusedCe.cpp, and the buffer branches of Softmax.cpp and Var.cpp all put a per-row count on y with no device-limit clamp. On Adreno or Mali they fail with Shader dispatch exceeds device workgroup count limits, e.g. rms_norm over [1, 70000, C]. The title says "per-row reductions", but only reduce_per_row changes.
Proposed fix: not required here. A follow-up can move these ops to the A1 idiom so they all share one wrapping helper.

A3 (Low): GitHub CI doesn't run the new op test. test_op.sh --build runs build-only, and CI doesn't set TORCH_OPS_YAML_PATH, so vulkan_op_correctness_tests isn't built on GitHub. The 70000-row case runs only via the internal compute_graph_op_tests, and only does anything on devices with a y limit ≤ 65535 (it is a no-op on MoltenVK).
Proposed fix: confirm in the test plan which internal Android or GPU-host job runs it. Otherwise add a reduce cases/correctness.json under benchmark_v2/operators/ and list it in the CI loops.

std::max(utils::safe_downcast<uint32_t>(graph->numel_of(out)), 1u);
const uint32_t max_y =
graph->context()->adapter_ptr()->max_compute_workgroup_count()[1u];
const uint32_t y = std::min(rows, max_y);

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.

[AI review] A1 (Medium): Use the existing linear-dispatch wrapping instead of a custom y→z spill.

The backend already has device-safe wrapping for "one workgroup per item": GlobalWorkGrid(..., kLinearWorkGrid) + wrap_linear_dispatch(...) in C++, and linear_idx_from_gid() from dispatch.glslh in GLSL. group_norm_gwg, q8ta_linear_gemv and the q4gsw coop linear use it for the same 64-worker cooperative layout. This PR adds a second scheme instead (z * gl_NumWorkGroups.y + y).

The split is also unbalanced. y is pinned at max_y, so 70000 rows on Adreno launch 65535 × 2 = 131070 workgroups, and 61070 of them (47%) only early-return. wrap_linear_dispatch folds into a near-square grid (265 × 265, 225 idle), and doesn't fold at all on devices with a large x limit.

Proposed fix (verified: 404/404 tests pass natively and with a 65535 limit; 100/100 with a 1000 limit forcing the 2D fold):

// reduce_per_row_gwg
const ValueRef out = args.at(0).refs.at(0);
const LocalWorkGroup lwg(1u, 1u, 64u);
GlobalWorkGrid gwg(
    {utils::safe_downcast<uint32_t>(graph->numel_of(out)), 1u, 1u},
    kLinearWorkGrid);
gwg.wrap_linear_dispatch(
    graph->context()->adapter_ptr()->max_compute_workgroup_count(), lwg);
return gwg;
#include "dispatch.glslh"
const uint out_idx = linear_idx_from_gid();
if (out_idx >= numel(outp)) { return; }
const uint worker_id = gl_LocalInvocationID.z;

If you keep the y/z scheme, at least balance it: z = div_up(rows, max_y); y = div_up(rows, z);.

const uint out_idx = gl_GlobalInvocationID.y;
// Rows beyond the device's y workgroup limit are spilled into z.
const uint out_idx =
gl_GlobalInvocationID.z * gl_NumWorkGroups.y + gl_GlobalInvocationID.y;

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.

[AI review] A1 (Medium), shader side: see the comment on Reduce.cpp. Replace this with linear_idx_from_gid() from dispatch.glslh, and read worker_id from gl_LocalInvocationID.z.

@SS-JIA SS-JIA 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.

Requesting changes based on the findings in the AI review above. Please address the Medium/High findings (or reply if you disagree); Low/Nit items are optional.

Replace the custom y->z spill with kLinearWorkGrid + wrap_linear_dispatch
and linear_idx_from_gid(), with the 64 cooperating workers along z, as
group_norm and the q8ta/q4gsw cooperative linears do.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. community: contribution PRs coming from community (excluding hardware partners) module: vulkan Issues related to the Vulkan delegate and code under backends/vulkan/ release notes: vulkan Changes to the Vulkan backend delegate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants