Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / clamp.comp
clamp.comp30 lines · 1.2 KiBRaw
| 1 | #version 450 |
| 2 | //! Clamp a tensor to a range held in the weights. |
| 3 | //! |
| 4 | //! # Why the vision tower needs this and the decoder does not |
| 5 | //! |
| 6 | //! Gemma 4's vision config sets `use_clipped_linears`, and the export puts a `Clip` around every |
| 7 | //! linear in the tower - 177 of them, each with its own calibrated `(min, max)`. They are not |
| 8 | //! decoration: they bound activations that would otherwise leave fp16's range on some inputs, |
| 9 | //! and dropping them changes the numbers everywhere downstream. |
| 10 | //! |
| 11 | //! The bounds live in the weights rather than in a push constant because they differ per clip and |
| 12 | //! `Builder` sees only shapes and offsets - it cannot read a number at build time. One `[2]` |
| 13 | //! tensor per clip is four bytes against a tower of 337 MB. |
| 14 | //! |
| 15 | //! # Push fields |
| 16 | //! |
| 17 | //! `p.in0`, `p.out_at` and `p.count` as usual; `p.act_weight` is the fp16 offset of the pair, |
| 18 | //! minimum first. |
| 19 | |
| 20 | #include "common.glsl" |
| 21 | |
| 22 | void main() { |
| 23 | uint index = global_index(); |
| 24 | if (index >= p.count) { |
| 25 | return; |
| 26 | } |
| 27 | float low = float(weights[p.act_weight]); |
| 28 | float high = float(weights[p.act_weight + 1u]); |
| 29 | arena[p.out_at + index] = float16_t(clamp(float(arena[p.in0 + index]), low, high)); |
| 30 | } |