Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / layernorm.comp
layernorm.comp105 lines · 4.0 KiBRaw
| 1 | #version 450 |
| 2 | //! Layer normalisation over the **channel** axis, with a per-channel affine. |
| 3 | //! |
| 4 | //! Five uses, all in PP-OCRv5 recognition's two transformer blocks. |
| 5 | //! |
| 6 | //! # One invocation per position, reducing over channels |
| 7 | //! |
| 8 | //! This runtime keeps a transformer's sequence as `[d_model, 1, T]` — features in |
| 9 | //! channels, the sequence along the width — because that is the layout the CNN backbone |
| 10 | //! in front of it already produces, and because splitting `d_model` into attention heads |
| 11 | //! is then free: `[120, 1, T]` read as `[8, 15, T]` is the same bytes in the same order. |
| 12 | //! See `nets::Kind::LayerNorm` for the whole argument. |
| 13 | //! |
| 14 | //! The consequence here is that the reduction is strided: `d_model` loads `in_h * in_w` |
| 15 | //! apart. At `d_model` 120 that is 120 loads per position, and there are `T` positions — |
| 16 | //! a few thousand loads for the whole layer. A contiguous layout would make this pass |
| 17 | //! faster and cost two `Permute`s per attention, which is the wrong trade. |
| 18 | //! |
| 19 | //! # Two passes, not Welford |
| 20 | //! |
| 21 | //! Mean then variance, reading the input twice. `d_model` is 120, so both passes sit in |
| 22 | //! cache; a single-pass algorithm would save bandwidth that is not being spent. |
| 23 | //! |
| 24 | //! Accumulation is `float` like everywhere else here, and `epsilon` arrives in the push |
| 25 | //! block because the recogniser uses 1e-5 for four of these and 1e-6 for the last. |
| 26 | //! |
| 27 | //! # Row mode |
| 28 | //! |
| 29 | //! With `p.kw == 1` one workgroup normalises one position and its lanes split the channels. |
| 30 | //! See `reduce.glsl` for why: a decode step has a single position. |
| 31 | |
| 32 | #extension GL_KHR_shader_subgroup_basic : require |
| 33 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 34 | #include "common.glsl" |
| 35 | #include "reduce.glsl" |
| 36 | |
| 37 | void row_mode() { |
| 38 | uint position = reduce_row(); |
| 39 | if (position >= p.count) { |
| 40 | return; |
| 41 | } |
| 42 | uint lane = gl_LocalInvocationID.x; |
| 43 | uint lanes = gl_WorkGroupSize.x; |
| 44 | uint stride = p.in_h * p.in_w; |
| 45 | float epsilon = uintBitsToFloat(p.param1_bits); |
| 46 | |
| 47 | float total = 0.0; |
| 48 | for (uint c = lane; c < p.in_c; c += lanes) { |
| 49 | total += float(arena[p.in0 + c * stride + position]); |
| 50 | } |
| 51 | float mean = workgroup_sum(total) / float(p.in_c); |
| 52 | |
| 53 | float variance = 0.0; |
| 54 | for (uint c = lane; c < p.in_c; c += lanes) { |
| 55 | float centred = float(arena[p.in0 + c * stride + position]) - mean; |
| 56 | variance += centred * centred; |
| 57 | } |
| 58 | float inverse = 1.0 / sqrt(workgroup_sum(variance) / float(p.in_c) + epsilon); |
| 59 | |
| 60 | for (uint c = lane; c < p.in_c; c += lanes) { |
| 61 | uint at = c * stride + position; |
| 62 | float centred = float(arena[p.in0 + at]) - mean; |
| 63 | float gamma = float(weights[p.weight + c]); |
| 64 | float beta = float(weights[p.bias + c]); |
| 65 | arena[p.out_at + at] = float16_t(centred * inverse * gamma + beta); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | void main() { |
| 70 | if (reduce_row_mode()) { |
| 71 | row_mode(); |
| 72 | return; |
| 73 | } |
| 74 | uint position = global_index(); |
| 75 | if (position >= p.count) { |
| 76 | return; |
| 77 | } |
| 78 | // `count` is `in_h * in_w`, so `position` walks the spatial extent and the channel |
| 79 | // stride is that same extent. |
| 80 | uint stride = p.in_h * p.in_w; |
| 81 | float epsilon = uintBitsToFloat(p.param1_bits); |
| 82 | |
| 83 | float total = 0.0; |
| 84 | for (uint c = 0u; c < p.in_c; c++) { |
| 85 | total += float(arena[p.in0 + c * stride + position]); |
| 86 | } |
| 87 | float mean = total / float(p.in_c); |
| 88 | |
| 89 | float variance = 0.0; |
| 90 | for (uint c = 0u; c < p.in_c; c++) { |
| 91 | float centred = float(arena[p.in0 + c * stride + position]) - mean; |
| 92 | variance += centred * centred; |
| 93 | } |
| 94 | // Biased variance, dividing by N rather than N-1, which is what every framework's |
| 95 | // layer norm does at inference. |
| 96 | float inverse = 1.0 / sqrt(variance / float(p.in_c) + epsilon); |
| 97 | |
| 98 | for (uint c = 0u; c < p.in_c; c++) { |
| 99 | uint at = c * stride + position; |
| 100 | float centred = float(arena[p.in0 + at]) - mean; |
| 101 | float gamma = float(weights[p.weight + c]); |
| 102 | float beta = float(weights[p.bias + c]); |
| 103 | arena[p.out_at + at] = float16_t(centred * inverse * gamma + beta); |
| 104 | } |
| 105 | } |