src/cpu/l2_norm.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU L2-norm (per-head, last-dim) ────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32-only host implementation. Used by Gated DeltaNet to L2-normalise q and | ||
| 4 | // k per head before the recurrence — distinct from rms_norm (sum vs. mean of | ||
| 5 | // squares, no learnable gamma, no sqrt(d) factor). | ||
| 6 | // | ||
| 7 | // Layout: (L, num_heads * head_dim), row-major; head h occupies columns | ||
| 8 | // [h*head_dim, (h+1)*head_dim). Same convention as rope_forward and rms_norm. | ||
| 9 | |||
| 10 | #include <brotensor/tensor.h> | ||
| 11 | |||
| 12 | #include <algorithm> | ||
| 13 | #include <cmath> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | |||
| 17 | namespace brotensor::detail::cpu { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | 81 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 22 | const char* op, const char* name) { | ||
| 23 | 81 | if (t.dtype != Dtype::FP32) { | |
| 24 | ✗ | throw std::runtime_error(std::string(op) + ": " + name + | |
| 25 | " must be FP32 (CPU backend is FP32-only)"); | ||
| 26 | } | ||
| 27 | 81 | } | |
| 28 | |||
| 29 | 81 | inline void check_shape(const ::brotensor::Tensor& t, | |
| 30 | int head_dim, int num_heads, | ||
| 31 | const char* op, const char* name) { | ||
| 32 | 81 | if (head_dim <= 0) { | |
| 33 | ✗ | throw std::runtime_error(std::string(op) + ": head_dim must be positive"); | |
| 34 | } | ||
| 35 | 81 | if (num_heads <= 0) { | |
| 36 | ✗ | throw std::runtime_error(std::string(op) + ": num_heads must be positive"); | |
| 37 | } | ||
| 38 | 81 | if (t.cols != num_heads * head_dim) { | |
| 39 | ✗ | throw std::runtime_error(std::string(op) + ": " + name + | |
| 40 | ".cols != num_heads * head_dim"); | ||
| 41 | } | ||
| 42 | 81 | } | |
| 43 | |||
| 44 | } // namespace | ||
| 45 | |||
| 46 | 67 | void l2_norm_forward(const ::brotensor::Tensor& X, | |
| 47 | int head_dim, int num_heads, float eps, | ||
| 48 | ::brotensor::Tensor& Y) { | ||
| 49 | 67 | check_fp32(X, "l2_norm_forward", "X"); | |
| 50 | 67 | check_shape(X, head_dim, num_heads, "l2_norm_forward", "X"); | |
| 51 | 67 | const int L = X.rows; | |
| 52 | 67 | const int D = X.cols; | |
| 53 | 67 | if (Y.rows != L || Y.cols != D || Y.dtype != Dtype::FP32) { | |
| 54 | 67 | Y.resize(L, D, Dtype::FP32); | |
| 55 | 67 | } | |
| 56 | 67 | if (L == 0 || D == 0) return; | |
| 57 | |||
| 58 | 67 | const float* xp = X.host_f32(); | |
| 59 | 67 | float* yp = Y.host_f32_mut(); | |
| 60 | 67 | const float eps_safe = std::max(eps, 1e-12f); | |
| 61 | |||
| 62 | 280 | for (int r = 0; r < L; ++r) { | |
| 63 | 681 | for (int h = 0; h < num_heads; ++h) { | |
| 64 | 468 | const int off = r * D + h * head_dim; | |
| 65 | // sum of squares | ||
| 66 | 468 | float sumsq = 0.0f; | |
| 67 | 7198 | for (int d = 0; d < head_dim; ++d) { | |
| 68 | 6730 | const float v = xp[off + d]; | |
| 69 | 6730 | sumsq += v * v; | |
| 70 | 6730 | } | |
| 71 | 468 | const float denom = std::max(sumsq + eps_safe, 1e-12f); | |
| 72 | 468 | const float inv = std::isfinite(denom) ? (1.0f / std::sqrt(denom)) : 0.0f; | |
| 73 | 7198 | for (int d = 0; d < head_dim; ++d) { | |
| 74 | 6730 | yp[off + d] = (inv > 0.0f) ? (xp[off + d] * inv) : 0.0f; | |
| 75 | 6730 | } | |
| 76 | 468 | } | |
| 77 | 213 | } | |
| 78 | 67 | } | |
| 79 | |||
| 80 | 7 | void l2_norm_backward(const ::brotensor::Tensor& X, | |
| 81 | int head_dim, int num_heads, float eps, | ||
| 82 | const ::brotensor::Tensor& dY, | ||
| 83 | ::brotensor::Tensor& dX) { | ||
| 84 | 7 | check_fp32(X, "l2_norm_backward", "X"); | |
| 85 | 7 | check_fp32(dY, "l2_norm_backward", "dY"); | |
| 86 | 7 | check_shape(X, head_dim, num_heads, "l2_norm_backward", "X"); | |
| 87 | 7 | check_shape(dY, head_dim, num_heads, "l2_norm_backward", "dY"); | |
| 88 | 7 | if (dY.rows != X.rows) { | |
| 89 | ✗ | throw std::runtime_error("l2_norm_backward: dY.rows != X.rows"); | |
| 90 | } | ||
| 91 | 7 | const int L = X.rows; | |
| 92 | 7 | const int D = X.cols; | |
| 93 | 7 | if (dX.rows != L || dX.cols != D || dX.dtype != Dtype::FP32) { | |
| 94 | 6 | dX.resize(L, D, Dtype::FP32); | |
| 95 | 6 | } | |
| 96 | 7 | if (L == 0 || D == 0) return; | |
| 97 | |||
| 98 | 7 | const float* xp = X.host_f32(); | |
| 99 | 7 | const float* gp = dY.host_f32(); | |
| 100 | 7 | float* dxp = dX.host_f32_mut(); | |
| 101 | 7 | const float eps_safe = std::max(eps, 1e-12f); | |
| 102 | |||
| 103 | // dX_d = n * dY_d - x_d * n^3 * sum_d' (x_d' * dY_d') | ||
| 104 | // = n * (dY_d - x_d * n^2 * dot(x, dY)) | ||
| 105 | 36 | for (int r = 0; r < L; ++r) { | |
| 106 | 119 | for (int h = 0; h < num_heads; ++h) { | |
| 107 | 90 | const int off = r * D + h * head_dim; | |
| 108 | 90 | float sumsq = 0.0f; | |
| 109 | 90 | float dot = 0.0f; | |
| 110 | 4759 | for (int d = 0; d < head_dim; ++d) { | |
| 111 | 4669 | const float v = xp[off + d]; | |
| 112 | 4669 | const float gv = gp[off + d]; | |
| 113 | 4669 | sumsq += v * v; | |
| 114 | 4669 | dot += v * gv; | |
| 115 | 4669 | } | |
| 116 | 90 | const float denom = std::max(sumsq + eps_safe, 1e-12f); | |
| 117 | 90 | const float n2 = std::isfinite(denom) ? (1.0f / denom) : 0.0f; // n^2 | |
| 118 | 90 | const float n = std::sqrt(n2); // n | |
| 119 | 90 | const float c = std::isfinite(dot) ? (dot * n2) : 0.0f; // x . dY / (||x||^2 + eps) | |
| 120 | 4759 | for (int d = 0; d < head_dim; ++d) { | |
| 121 | 4669 | dxp[off + d] = (n > 0.0f) ? (n * (gp[off + d] - xp[off + d] * c)) : 0.0f; | |
| 122 | 4669 | } | |
| 123 | 90 | } | |
| 124 | 29 | } | |
| 125 | 7 | } | |
| 126 | |||
| 127 | } // namespace brotensor::detail::cpu | ||
| 128 |