Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / instancenorm.comp
instancenorm.comp99 lines · 3.5 KiBRaw
| 1 | #version 450 |
| 2 | //! Instance normalisation over the **spatial** axes, with no affine. |
| 3 | //! |
| 4 | //! Sixteen uses, all MODNet's `IBNorm` second halves |
| 5 | //! (`InstanceNorm2d(affine=False)` over one channel-split half). |
| 6 | //! |
| 7 | //! # One invocation per channel, reducing over its plane |
| 8 | //! |
| 9 | //! This runtime stores a tensor `[C, H, W]` with the width innermost, so one |
| 10 | //! channel's `H x W` plane is contiguous: `plane` loads back to back. That is |
| 11 | //! the transpose of `layernorm.comp`, whose reduction strides across channels |
| 12 | //! — and the reason this is a separate pipeline rather than a flag on it. |
| 13 | //! |
| 14 | //! # Two passes, not Welford |
| 15 | //! |
| 16 | //! Mean then variance, reading the plane twice. The largest plane here is |
| 17 | //! 128x128 (the 16-channel halves at full resolution); both passes sit in |
| 18 | //! cache, and a single-pass algorithm would save bandwidth that is not being |
| 19 | //! spent. |
| 20 | //! |
| 21 | //! Accumulation is `float` like everywhere else here. There is no gamma/beta: |
| 22 | //! the export's scale and bias are constant 1 and 0 (verified by |
| 23 | //! `scripts/ml/modnet_fold.py`), so `epsilon` is the only parameter and it |
| 24 | //! arrives in the push block. |
| 25 | |
| 26 | #extension GL_KHR_shader_subgroup_basic : require |
| 27 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 28 | #include "common.glsl" |
| 29 | #include "reduce.glsl" |
| 30 | |
| 31 | void row_mode() { |
| 32 | uint channel = reduce_row(); |
| 33 | if (channel >= p.count) { |
| 34 | return; |
| 35 | } |
| 36 | // `count` is `in_c`, so `channel` walks the channels and the plane extent |
| 37 | // is that channel's contiguous run. Lanes stride it, so adjacent lanes |
| 38 | // read adjacent elements. See `reduce.glsl` for why: MODNet's planes reach |
| 39 | // 128x128, which one invocation walks serially at 16 ms a channel. |
| 40 | uint plane = p.in_h * p.in_w; |
| 41 | uint base = channel * plane; |
| 42 | uint lane = gl_LocalInvocationID.x; |
| 43 | uint lanes = gl_WorkGroupSize.x; |
| 44 | float epsilon = uintBitsToFloat(p.param1_bits); |
| 45 | |
| 46 | float total = 0.0; |
| 47 | for (uint i = lane; i < plane; i += lanes) { |
| 48 | total += float(arena[p.in0 + base + i]); |
| 49 | } |
| 50 | float mean = workgroup_sum(total) / float(plane); |
| 51 | |
| 52 | float variance = 0.0; |
| 53 | for (uint i = lane; i < plane; i += lanes) { |
| 54 | float centred = float(arena[p.in0 + base + i]) - mean; |
| 55 | variance += centred * centred; |
| 56 | } |
| 57 | float inverse = 1.0 / sqrt(workgroup_sum(variance) / float(plane) + epsilon); |
| 58 | |
| 59 | for (uint i = lane; i < plane; i += lanes) { |
| 60 | float centred = float(arena[p.in0 + base + i]) - mean; |
| 61 | arena[p.out_at + base + i] = float16_t(centred * inverse); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | void main() { |
| 66 | if (reduce_row_mode()) { |
| 67 | row_mode(); |
| 68 | return; |
| 69 | } |
| 70 | uint channel = global_index(); |
| 71 | if (channel >= p.count) { |
| 72 | return; |
| 73 | } |
| 74 | // `count` is `in_c`, so `channel` walks the channels and the plane extent |
| 75 | // is that channel's contiguous run. |
| 76 | uint plane = p.in_h * p.in_w; |
| 77 | uint base = channel * plane; |
| 78 | float epsilon = uintBitsToFloat(p.param1_bits); |
| 79 | |
| 80 | float total = 0.0; |
| 81 | for (uint i = 0u; i < plane; i++) { |
| 82 | total += float(arena[p.in0 + base + i]); |
| 83 | } |
| 84 | float mean = total / float(plane); |
| 85 | |
| 86 | float variance = 0.0; |
| 87 | for (uint i = 0u; i < plane; i++) { |
| 88 | float centred = float(arena[p.in0 + base + i]) - mean; |
| 89 | variance += centred * centred; |
| 90 | } |
| 91 | // Biased variance, dividing by N rather than N-1, which is what every framework's |
| 92 | // instance norm does at inference. |
| 93 | float inverse = 1.0 / sqrt(variance / float(plane) + epsilon); |
| 94 | |
| 95 | for (uint i = 0u; i < plane; i++) { |
| 96 | float centred = float(arena[p.in0 + base + i]) - mean; |
| 97 | arena[p.out_at + base + i] = float16_t(centred * inverse); |
| 98 | } |
| 99 | } |