From 8fbef9a0791a4fab6f96c369eadf16d69676d466 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Thu, 19 Nov 2026 14:27:33 +0100 Subject: [PATCH 1/2] [ET-VK] Spill per-row reductions past the y workgroup limit into z 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 #22225. Rows past the limit now go to z and the shader recombines the index. --- .../runtime/graph/ops/glsl/reduce_per_row_buffer.glsl | 7 ++++++- backends/vulkan/runtime/graph/ops/impl/Reduce.cpp | 9 ++++++++- backends/vulkan/test/op_tests/cases.py | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl b/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl index 699ad721a51..97a0d11edf5 100644 --- a/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl +++ b/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl @@ -59,7 +59,12 @@ $if OUTPUT_IS_INDICES: #define OUTPUT_IS_INDICES void main() { - 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; + if (out_idx >= numel(outp)) { + return; + } const TensorIndex out_tidx = contiguous_idx_to_tensor_idx(outp, out_idx); const uint out_bufi = tensor_idx_to_linear_idx(outp, out_tidx); diff --git a/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp b/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp index 3b1fbcb3494..d2b8618ec0f 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp @@ -342,9 +342,16 @@ GlobalWorkGrid reduce_per_row_gwg( (void)shader; (void)resize_args; + // One workgroup per output row along y. Rows past the device's y limit + // spill into z; the shader recombines them as z * gl_NumWorkGroups.y + y. const ValueRef out = args.at(0).refs.at(0); + const uint32_t rows = + std::max(utils::safe_downcast(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); return GlobalWorkGrid( - {1u, utils::safe_downcast(graph->numel_of(out)), 1u}, + {1u, y, utils::div_up(rows, y)}, kTiledWorkGrid, LocalWorkGroup(64u, 1u, 1u)); } diff --git a/backends/vulkan/test/op_tests/cases.py b/backends/vulkan/test/op_tests/cases.py index dc0e31399aa..5d2a99d6cd3 100644 --- a/backends/vulkan/test/op_tests/cases.py +++ b/backends/vulkan/test/op_tests/cases.py @@ -1885,6 +1885,8 @@ def get_reduce_per_row_inputs(): ((3, 7, 280), -1, False), ((3, 17, 77), -1, True), ((3, 17, 77), -1, False), + # More rows than the usual 65535 y-workgroup limit + ((70000, 8), -1, False), ] return inputs From 82b7df3f3084d56f8e7f5f27e189acd82703e428 Mon Sep 17 00:00:00 2001 From: Mateusz Sluszniak Date: Thu, 3 Dec 2026 10:28:14 +0100 Subject: [PATCH 2/2] [ET-VK] Use the linear dispatch wrapping for per-row reductions 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. --- .../graph/ops/glsl/reduce_per_row_buffer.glsl | 10 +++++----- .../vulkan/runtime/graph/ops/impl/Reduce.cpp | 20 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl b/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl index 97a0d11edf5..518e749e86e 100644 --- a/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl +++ b/backends/vulkan/runtime/graph/ops/glsl/reduce_per_row_buffer.glsl @@ -24,6 +24,7 @@ ${define_active_storage_type("buffer")} layout(std430) buffer; +#include "dispatch.glslh" #include "indexing.glslh" #include "convert.glslh" #include "reduce_op_defs.glslh" @@ -59,9 +60,8 @@ $if OUTPUT_IS_INDICES: #define OUTPUT_IS_INDICES void main() { - // 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; + // The linear dispatch may launch idle workgroups past the last row. + const uint out_idx = linear_idx_from_gid(); if (out_idx >= numel(outp)) { return; } @@ -69,8 +69,8 @@ void main() { const uint out_bufi = tensor_idx_to_linear_idx(outp, out_tidx); // Local indices - const uint worker_id = gl_LocalInvocationID.x; - const uint output_id = gl_LocalInvocationID.y; + const uint worker_id = gl_LocalInvocationID.z; + const uint output_id = 0; const uint in_bufi_base = out_idx * stride_at(inp, 1); diff --git a/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp b/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp index d2b8618ec0f..815f3bea2a3 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp @@ -342,18 +342,16 @@ GlobalWorkGrid reduce_per_row_gwg( (void)shader; (void)resize_args; - // One workgroup per output row along y. Rows past the device's y limit - // spill into z; the shader recombines them as z * gl_NumWorkGroups.y + y. + // One workgroup per output row, with 64 cooperating workers along z. The + // linear dispatch wraps into x/y when the row count exceeds a device limit. const ValueRef out = args.at(0).refs.at(0); - const uint32_t rows = - std::max(utils::safe_downcast(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); - return GlobalWorkGrid( - {1u, y, utils::div_up(rows, y)}, - kTiledWorkGrid, - LocalWorkGroup(64u, 1u, 1u)); + const LocalWorkGroup lwg(1u, 1u, 64u); + GlobalWorkGrid gwg( + {utils::safe_downcast(graph->numel_of(out)), 1u, 1u}, + kLinearWorkGrid); + gwg.wrap_linear_dispatch( + graph->context()->adapter_ptr()->max_compute_workgroup_count(), lwg); + return gwg; } void add_reduce_per_row_node(