brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
73 / 78
93.6%
Branches:
35 / 100
35.0%
Functions:
4 / 4
100.0%

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
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (t.dtype != Dtype::FP32) {
24
0/14
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ 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
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (head_dim <= 0) {
33
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ throw std::runtime_error(std::string(op) + ": head_dim must be positive");
34 }
35
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (num_heads <= 0) {
36
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ throw std::runtime_error(std::string(op) + ": num_heads must be positive");
37 }
38
1/2
✓ Branch 0 taken 81 times.
✗ Branch 1 not taken.
81 if (t.cols != num_heads * head_dim) {
39
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ 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
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 67 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
67 if (Y.rows != L || Y.cols != D || Y.dtype != Dtype::FP32) {
54 67 Y.resize(L, D, Dtype::FP32);
55 67 }
56
2/4
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
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
2/2
✓ Branch 0 taken 67 times.
✓ Branch 1 taken 213 times.
280 for (int r = 0; r < L; ++r) {
63
2/2
✓ Branch 0 taken 468 times.
✓ Branch 1 taken 213 times.
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
2/2
✓ Branch 0 taken 6730 times.
✓ Branch 1 taken 468 times.
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
1/2
✓ Branch 0 taken 468 times.
✗ Branch 1 not taken.
468 const float inv = std::isfinite(denom) ? (1.0f / std::sqrt(denom)) : 0.0f;
73
2/2
✓ Branch 0 taken 6730 times.
✓ Branch 1 taken 468 times.
7198 for (int d = 0; d < head_dim; ++d) {
74
1/2
✓ Branch 0 taken 6730 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (dY.rows != X.rows) {
89
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 time.
7 if (dX.rows != L || dX.cols != D || dX.dtype != Dtype::FP32) {
94 6 dX.resize(L, D, Dtype::FP32);
95 6 }
96
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
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
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 29 times.
36 for (int r = 0; r < L; ++r) {
106
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 29 times.
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
2/2
✓ Branch 0 taken 4669 times.
✓ Branch 1 taken 90 times.
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
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 const float n2 = std::isfinite(denom) ? (1.0f / denom) : 0.0f; // n^2
118 90 const float n = std::sqrt(n2); // n
119
1/2
✓ Branch 0 taken 90 times.
✗ Branch 1 not taken.
90 const float c = std::isfinite(dot) ? (dot * n2) : 0.0f; // x . dY / (||x||^2 + eps)
120
2/2
✓ Branch 0 taken 4669 times.
✓ Branch 1 taken 90 times.
4759 for (int d = 0; d < head_dim; ++d) {
121
1/2
✓ Branch 0 taken 4669 times.
✗ Branch 1 not taken.
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