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..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,13 +60,17 @@ $if OUTPUT_IS_INDICES: #define OUTPUT_IS_INDICES void main() { - const uint out_idx = 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; + } const TensorIndex out_tidx = contiguous_idx_to_tensor_idx(outp, out_idx); 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 3b1fbcb3494..815f3bea2a3 100644 --- a/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp +++ b/backends/vulkan/runtime/graph/ops/impl/Reduce.cpp @@ -342,11 +342,16 @@ GlobalWorkGrid reduce_per_row_gwg( (void)shader; (void)resize_args; + // 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); - return GlobalWorkGrid( - {1u, utils::safe_downcast(graph->numel_of(out)), 1u}, - 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( diff --git a/backends/vulkan/test/op_tests/cases.py b/backends/vulkan/test/op_tests/cases.py index 28774dd7261..ace438c0d36 100644 --- a/backends/vulkan/test/op_tests/cases.py +++ b/backends/vulkan/test/op_tests/cases.py @@ -1903,6 +1903,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