#version 450 //! Mean over H and W, to `C x 1 x 1`. The squeeze half of a squeeze-excite block. //! //! Ten uses, all in the selfie net, and all on a small map: the largest is 128x16x16, so //! one invocation per *channel* reducing 256 elements serially is the right shape. A //! tree reduction across a workgroup would be faster per channel and slower overall, //! because there are only 16 to 128 channels to keep the GPU busy with. //! //! ONNX calls this `ReduceMean` with `axes = [2, 3]` and `keepdims = 1`, which is why the //! output keeps its two trailing unit dimensions and `mul_bcast.comp` can index it by //! channel alone. //! //! # Row mode //! //! With `p.kw == 1` one 64-lane workgroup owns one channel and its lanes stride the //! plane, like `instancenorm.comp`. Selected by the emit arm for large planes; small //! maps keep one invocation per channel. See `reduce.glsl`. #extension GL_KHR_shader_subgroup_basic : require #extension GL_KHR_shader_subgroup_arithmetic : require #include "common.glsl" #include "reduce.glsl" void row_mode() { uint channel = reduce_row(); if (channel >= p.count) { return; } uint plane = p.in0 + channel * p.in_h * p.in_w; uint elements = p.in_h * p.in_w; uint lane = gl_LocalInvocationID.x; uint lanes = gl_WorkGroupSize.x; float total = 0.0; for (uint i = lane; i < elements; i += lanes) { total += float(arena[plane + i]); } // Every lane reaches the workgroup reduction: `workgroup_sum` barriers, // which a subset of lanes must never enter alone. float sum = workgroup_sum(total); if (lane == 0u) { arena[p.out_at + channel] = float16_t(sum / float(elements)); } } void main() { if (reduce_row_mode()) { row_mode(); return; } uint channel = global_index(); if (channel >= p.count) { return; } uint plane = p.in0 + channel * p.in_h * p.in_w; uint elements = p.in_h * p.in_w; float total = 0.0; for (uint i = 0u; i < elements; i++) { total += float(arena[plane + i]); } arena[p.out_at + channel] = float16_t(total / float(elements)); }