Modern Apps git

← Modern-Apps.git

ml: row-mode InstanceNorm + GlobalAvgPool shaders

Commit
c48a351c5f2471a58f7884e477991c4fa2d65fc9
Author
vayun-mathur <[email protected]>
When
2026-10-25 21:44 UTC (1 month ago)
Parents
053fd65

Diff

library/ml/src/main/rust/shaders/gap.comp
diff --git a/library/ml/src/main/rust/shaders/gap.comp b/library/ml/src/main/rust/shaders/gap.compindex ed4564cb34..5382ab26dd 100644--- a/library/ml/src/main/rust/shaders/gap.comp+++ b/library/ml/src/main/rust/shaders/gap.comp@@ -9,10 +9,43 @@ //! 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]);+    }++    if (lane == 0u) {+        arena[p.out_at + channel] = float16_t(workgroup_sum(total) / float(elements));+    }+}  void main() {+    if (reduce_row_mode()) {+        row_mode();+        return;+    }     uint channel = global_index();     if (channel >= p.count) {         return;
library/ml/src/main/rust/shaders/instancenorm.comp
diff --git a/library/ml/src/main/rust/shaders/instancenorm.comp b/library/ml/src/main/rust/shaders/instancenorm.compindex 1ca6c286db..dc7072846d 100644--- a/library/ml/src/main/rust/shaders/instancenorm.comp+++ b/library/ml/src/main/rust/shaders/instancenorm.comp@@ -23,9 +23,50 @@ //! `scripts/ml/modnet_fold.py`), so `epsilon` is the only parameter and it //! arrives in the push block. +#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;+    }+    // `count` is `in_c`, so `channel` walks the channels and the plane extent+    // is that channel's contiguous run. Lanes stride it, so adjacent lanes+    // read adjacent elements. See `reduce.glsl` for why: MODNet's planes reach+    // 128x128, which one invocation walks serially at 16 ms a channel.+    uint plane = p.in_h * p.in_w;+    uint base = channel * plane;+    uint lane = gl_LocalInvocationID.x;+    uint lanes = gl_WorkGroupSize.x;+    float epsilon = uintBitsToFloat(p.param1_bits);++    float total = 0.0;+    for (uint i = lane; i < plane; i += lanes) {+        total += float(arena[p.in0 + base + i]);+    }+    float mean = workgroup_sum(total) / float(plane);++    float variance = 0.0;+    for (uint i = lane; i < plane; i += lanes) {+        float centred = float(arena[p.in0 + base + i]) - mean;+        variance += centred * centred;+    }+    float inverse = 1.0 / sqrt(workgroup_sum(variance) / float(plane) + epsilon);++    for (uint i = lane; i < plane; i += lanes) {+        float centred = float(arena[p.in0 + base + i]) - mean;+        arena[p.out_at + base + i] = float16_t(centred * inverse);+    }+}  void main() {+    if (reduce_row_mode()) {+        row_mode();+        return;+    }     uint channel = global_index();     if (channel >= p.count) {         return;