Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / mul_scalar.comp

mul_scalar.comp28 lines · 1.1 KiBRaw
1#version 450
2//! Multiply a tensor by a **scalar held in the weights**.
3//!
4//! # Why this is not `Kind::Affine`
5//!
6//! `Affine` takes its scale as a push constant, decided when the plan is built. This one reads a
7//! one-element weight tensor, because the value is in the `.maml` and `Builder` sees only shapes
8//! and offsets - it cannot read a number at build time without giving every net the ability to
9//! bake weights into a recording, which is exactly what `Reshaped` exists to avoid.
10//!
11//! Gemma 4's `layer_scalar` is the caller: one trained number per layer, multiplying that layer's
12//! whole residual output. At 0.0178 on layer 0 it is not a rounding detail - leaving it out lets
13//! the residual stream grow by a factor of fifty per layer.
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 scalar.
18
19#include "common.glsl"
20
21void main() {
22 uint index = global_index();
23 if (index >= p.count) {
24 return;
25 }
26 float scale = float(weights[p.act_weight]);
27 arena[p.out_at + index] = float16_t(float(arena[p.in0 + index]) * scale);
28}