brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
611 / 657
93.0%
Branches:
408 / 548
74.5%
Functions:
28 / 31
90.3%

src/cpu/ops_mha_batched.cpp
Line Branch Exec Source
1 // ─── CPU multi-head attention, batched ops & masked mean-pool ──────────────
2 //
3 // Plain FP32 host loops decomposed from src/cpu/ops_impl.cpp.
4
5 #include <brotensor/tensor.h>
6 #include <brotensor/detail/cpu/thread_pool.h>
7
8 #include <algorithm>
9 #include <cassert>
10 #include <cmath>
11 #include <cstddef>
12 #include <cstdint>
13 #include <cstring>
14 #include <vector>
15
16 namespace brotensor::detail::cpu {
17
18 namespace {
19
20 10 void ensure_match(const ::brotensor::Tensor& src, ::brotensor::Tensor& dst) {
21
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 10 times.
10 if (dst.rows != src.rows || dst.cols != src.cols ||
22 10 dst.dtype != ::brotensor::Dtype::FP32) {
23 ✗ dst.resize(src.rows, src.cols, ::brotensor::Dtype::FP32);
24 ✗ }
25 10 }
26
27 } // namespace
28
29 // ─── Multi-head self-attention ─────────────────────────────────────────────
30
31 26 void mha_forward(const ::brotensor::Tensor& X,
32 const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk,
33 const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo,
34 const ::brotensor::Tensor* bq, const ::brotensor::Tensor* bk,
35 const ::brotensor::Tensor* bv, const ::brotensor::Tensor* bo,
36 const float* d_mask, int num_heads,
37 ::brotensor::Tensor& Qh, ::brotensor::Tensor& Kh,
38 ::brotensor::Tensor& Vh, ::brotensor::Tensor& Attnh,
39 ::brotensor::Tensor& Yconcat, ::brotensor::Tensor& O) {
40 using ::brotensor::Dtype;
41 26 const int K = X.rows;
42 26 const int D = X.cols;
43 26 const int H = num_heads;
44
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 const int dh = (H > 0) ? D / H : 0;
45
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (Qh.rows != H * K || Qh.cols != dh || Qh.dtype != Dtype::FP32) Qh.resize(H * K, dh, Dtype::FP32);
46
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (Kh.rows != H * K || Kh.cols != dh || Kh.dtype != Dtype::FP32) Kh.resize(H * K, dh, Dtype::FP32);
47
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (Vh.rows != H * K || Vh.cols != dh || Vh.dtype != Dtype::FP32) Vh.resize(H * K, dh, Dtype::FP32);
48
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (Attnh.rows != H * K || Attnh.cols != K || Attnh.dtype != Dtype::FP32) Attnh.resize(H * K, K, Dtype::FP32);
49
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (Yconcat.rows != K || Yconcat.cols != D || Yconcat.dtype != Dtype::FP32) Yconcat.resize(K, D, Dtype::FP32);
50
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
26 if (O.rows != K || O.cols != D || O.dtype != Dtype::FP32) O.resize(K, D, Dtype::FP32);
51
3/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 26 times.
26 if (K == 0 || D == 0 || H == 0) return;
52
53 26 const float* Xp = X.host_f32();
54 26 const float* Wqp = Wq.host_f32();
55 26 const float* Wkp = Wk.host_f32();
56 26 const float* Wvp = Wv.host_f32();
57 26 const float* Wop = Wo.host_f32();
58 26 float* Qp = Qh.host_f32_mut();
59 26 float* Kp = Kh.host_f32_mut();
60 26 float* Vp = Vh.host_f32_mut();
61 26 float* Ap = Attnh.host_f32_mut();
62 26 float* Yp = Yconcat.host_f32_mut();
63 26 float* Op = O.host_f32_mut();
64
65
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
26 const float* bqp = bq ? bq->host_f32() : nullptr;
66
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
26 const float* bkp = bk ? bk->host_f32() : nullptr;
67
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
26 const float* bvp = bv ? bv->host_f32() : nullptr;
68
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
26 const float* bop = bo ? bo->host_f32() : nullptr;
69
70 // Per-head Q/K/V projection: out(hh,i,j) = sum_k X(i,k) * W(hh*dh+j, k)
71 // + (b ? b[hh*dh+j] : 0). Biases are flat length-D vectors indexed by
72 // the full output column (hh*dh+j). Each head hh owns rows
73 // [hh*K, hh*K+K) of Qh/Kh/Vh exclusively (X/W*/b* read-only), so the
74 // head axis parallelizes with no cross-thread writes.
75
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
133 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
76 107 const int hh = static_cast<int>(hhi);
77 107 const int row_off = hh * dh;
78
2/2
✓ Branch 0 taken 1148 times.
✓ Branch 1 taken 107 times.
1255 for (int i = 0; i < K; ++i) {
79 1148 const float* xr = Xp + static_cast<std::size_t>(i) * D;
80 1148 const std::size_t out_row =
81 1148 (static_cast<std::size_t>(hh) * K + i) * dh;
82
2/2
✓ Branch 0 taken 12307 times.
✓ Branch 1 taken 1148 times.
13455 for (int j = 0; j < dh; ++j) {
83 12307 const int wrow = row_off + j;
84 12307 const float* wq = Wqp + static_cast<std::size_t>(wrow) * D;
85 12307 const float* wk = Wkp + static_cast<std::size_t>(wrow) * D;
86 12307 const float* wv = Wvp + static_cast<std::size_t>(wrow) * D;
87 12307 float aq = 0.0f, ak = 0.0f, av = 0.0f;
88
2/2
✓ Branch 0 taken 587323 times.
✓ Branch 1 taken 12307 times.
599630 for (int k = 0; k < D; ++k) {
89 587323 aq += xr[k] * wq[k];
90 587323 ak += xr[k] * wk[k];
91 587323 av += xr[k] * wv[k];
92 587323 }
93
2/2
✓ Branch 0 taken 3070 times.
✓ Branch 1 taken 9237 times.
12307 if (bqp) aq += bqp[wrow];
94
2/2
✓ Branch 0 taken 3065 times.
✓ Branch 1 taken 9242 times.
12307 if (bkp) ak += bkp[wrow];
95
2/2
✓ Branch 0 taken 3104 times.
✓ Branch 1 taken 9203 times.
12307 if (bvp) av += bvp[wrow];
96 12307 Qp[out_row + j] = aq;
97 12307 Kp[out_row + j] = ak;
98 12307 Vp[out_row + j] = av;
99 12307 }
100 1148 }
101 107 });
102
103 26 const float inv_sqrtdh = 1.0f / std::sqrt(static_cast<float>(dh));
104
105 // Per-head scores → masked row softmax → Attnh. Each hh owns rows
106 // [hh*K, hh*K+K) of Attnh exclusively, so parallelizes over hh.
107
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
133 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
108 107 const int hh = static_cast<int>(hhi);
109
2/2
✓ Branch 0 taken 1145 times.
✓ Branch 1 taken 107 times.
1252 for (int i = 0; i < K; ++i) {
110 1145 float* arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K;
111
4/4
✓ Branch 0 taken 335 times.
✓ Branch 1 taken 810 times.
✓ Branch 2 taken 151 times.
✓ Branch 3 taken 184 times.
1145 if (d_mask && d_mask[i] < 0.5f) {
112
2/2
✓ Branch 0 taken 2576 times.
✓ Branch 1 taken 184 times.
2760 for (int j = 0; j < K; ++j) arow[j] = 0.0f;
113 184 continue;
114 }
115 961 const float* qr = Qp + (static_cast<std::size_t>(hh) * K + i) * dh;
116 961 float m = -1e30f;
117
2/2
✓ Branch 0 taken 11135 times.
✓ Branch 1 taken 961 times.
12096 for (int j = 0; j < K; ++j) {
118
4/4
✓ Branch 0 taken 2080 times.
✓ Branch 1 taken 9055 times.
✓ Branch 2 taken 952 times.
✓ Branch 3 taken 1128 times.
11135 if (d_mask && d_mask[j] < 0.5f) continue;
119 10007 const float* kr = Kp + (static_cast<std::size_t>(hh) * K + j) * dh;
120 10007 float s = 0.0f;
121
2/2
✓ Branch 0 taken 111252 times.
✓ Branch 1 taken 10007 times.
121259 for (int k = 0; k < dh; ++k) s += qr[k] * kr[k];
122 10007 s *= inv_sqrtdh;
123 10007 arow[j] = s;
124
2/2
✓ Branch 0 taken 7211 times.
✓ Branch 1 taken 2796 times.
10007 if (s > m) m = s;
125 10007 }
126 961 float sum = 0.0f;
127
2/2
✓ Branch 0 taken 11536 times.
✓ Branch 1 taken 961 times.
12497 for (int j = 0; j < K; ++j) {
128
4/4
✓ Branch 0 taken 2083 times.
✓ Branch 1 taken 9453 times.
✓ Branch 2 taken 955 times.
✓ Branch 3 taken 1128 times.
11536 if (d_mask && d_mask[j] < 0.5f) { arow[j] = 0.0f; continue; }
129 10408 const float e = std::exp(arow[j] - m);
130 10408 arow[j] = e;
131 10408 sum += e;
132 10408 }
133
1/2
✓ Branch 0 taken 961 times.
✗ Branch 1 not taken.
961 const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
134
2/2
✓ Branch 0 taken 11680 times.
✓ Branch 1 taken 961 times.
12641 for (int j = 0; j < K; ++j) arow[j] *= inv;
135 961 }
136 107 });
137
138 // Per-head Attn @ V → Yconcat(i, hh*dh+k). Each hh writes only its own
139 // column range [hh*dh, hh*dh+dh) of every row of Yconcat — different hh
140 // never touch the same bytes, so this parallelizes over hh.
141
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
132 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
142 106 const int hh = static_cast<int>(hhi);
143
2/2
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 106 times.
1250 for (int i = 0; i < K; ++i) {
144 1144 const float* arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K;
145
2/2
✓ Branch 0 taken 11866 times.
✓ Branch 1 taken 1144 times.
13010 for (int k = 0; k < dh; ++k) {
146 11866 float acc = 0.0f;
147
2/2
✓ Branch 0 taken 154061 times.
✓ Branch 1 taken 11866 times.
165927 for (int j = 0; j < K; ++j) {
148 154061 const float vv = Vp[(static_cast<std::size_t>(hh) * K + j) * dh + k];
149 154061 acc += arow[j] * vv;
150 154061 }
151 11866 Yp[static_cast<std::size_t>(i) * D + (hh * dh + k)] = acc;
152 11866 }
153 1144 }
154 106 });
155
156 // Output projection O = Yconcat @ Wo^T + bo, zero invalid query rows.
157 // Each i owns row i of O exclusively (Yconcat/Wo/bo read-only), so this
158 // parallelizes over i.
159
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
280 parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) {
160 254 const int i = static_cast<int>(ii);
161 254 float* orow = Op + static_cast<std::size_t>(i) * D;
162
4/4
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 182 times.
✓ Branch 2 taken 32 times.
✓ Branch 3 taken 40 times.
254 if (d_mask && d_mask[i] < 0.5f) {
163
2/2
✓ Branch 0 taken 2133 times.
✓ Branch 1 taken 40 times.
2173 for (int c = 0; c < D; ++c) orow[c] = 0.0f;
164 40 return;
165 }
166 214 const float* yr = Yp + static_cast<std::size_t>(i) * D;
167
2/2
✓ Branch 0 taken 9723 times.
✓ Branch 1 taken 214 times.
9937 for (int c = 0; c < D; ++c) {
168 9723 const float* wr = Wop + static_cast<std::size_t>(c) * D;
169 9723 float acc = 0.0f;
170
2/2
✓ Branch 0 taken 452772 times.
✓ Branch 1 taken 9723 times.
462495 for (int k = 0; k < D; ++k) acc += yr[k] * wr[k];
171
2/2
✓ Branch 0 taken 7373 times.
✓ Branch 1 taken 2350 times.
9723 if (bop) acc += bop[c];
172 9723 orow[c] = acc;
173 9723 }
174 254 });
175 26 }
176
177 19 void mha_backward(const ::brotensor::Tensor& dO,
178 const ::brotensor::Tensor& X,
179 const ::brotensor::Tensor& Qh, const ::brotensor::Tensor& Kh,
180 const ::brotensor::Tensor& Vh, const ::brotensor::Tensor& Attnh,
181 const ::brotensor::Tensor& Yconcat,
182 const ::brotensor::Tensor& Wq, const ::brotensor::Tensor& Wk,
183 const ::brotensor::Tensor& Wv, const ::brotensor::Tensor& Wo,
184 const float* d_mask, int num_heads,
185 ::brotensor::Tensor& dX,
186 ::brotensor::Tensor& dWq, ::brotensor::Tensor& dWk,
187 ::brotensor::Tensor& dWv, ::brotensor::Tensor& dWo,
188 ::brotensor::Tensor* dbq, ::brotensor::Tensor* dbk,
189 ::brotensor::Tensor* dbv, ::brotensor::Tensor* dbo) {
190 using ::brotensor::Dtype;
191 19 const int K = X.rows;
192 19 const int D = X.cols;
193 19 const int H = num_heads;
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 const int dh = (H > 0) ? D / H : 0;
195
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 if (dX.rows != K || dX.cols != D || dX.dtype != Dtype::FP32) {
196 ✗ dX.resize(K, D, Dtype::FP32);
197 ✗ }
198
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 19 times.
19 if (K == 0 || D == 0 || H == 0) return;
199
200 19 const float inv_sqrtdh = 1.0f / std::sqrt(static_cast<float>(dh));
201
202 19 const float* dOp = dO.host_f32();
203 19 const float* Xp = X.host_f32();
204 19 const float* Qp = Qh.host_f32();
205 19 const float* Kp = Kh.host_f32();
206 19 const float* Vp = Vh.host_f32();
207 19 const float* Ap = Attnh.host_f32();
208 19 const float* Yp = Yconcat.host_f32();
209 19 const float* Wqp = Wq.host_f32();
210 19 const float* Wkp = Wk.host_f32();
211 19 const float* Wvp = Wv.host_f32();
212 19 const float* Wop = Wo.host_f32();
213 19 float* dXp = dX.host_f32_mut();
214 19 float* dWqp = dWq.host_f32_mut();
215 19 float* dWkp = dWk.host_f32_mut();
216 19 float* dWvp = dWv.host_f32_mut();
217 19 float* dWop = dWo.host_f32_mut();
218
219 19 std::vector<float> dYc(static_cast<std::size_t>(K) * D, 0.0f);
220
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<float> dAttn(static_cast<std::size_t>(H) * K * K, 0.0f);
221
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<float> dVh(static_cast<std::size_t>(H) * K * dh, 0.0f);
222
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<float> dScores(static_cast<std::size_t>(H) * K * K, 0.0f);
223
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<float> dQh(static_cast<std::size_t>(H) * K * dh, 0.0f);
224
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<float> dKh(static_cast<std::size_t>(H) * K * dh, 0.0f);
225
226 // dYconcat (overwrite, zero on invalid rows). Each i owns row i of dYc
227 // exclusively (Wo/dO read-only), so this parallelizes over i.
228
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
205 parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) {
229 186 const int i = static_cast<int>(ii);
230
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 63 times.
186 const bool valid = !(d_mask && d_mask[i] < 0.5f);
231
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 9298 times.
9484 for (int k = 0; k < D; ++k) {
232 9298 float acc = 0.0f;
233
2/2
✓ Branch 0 taken 7229 times.
✓ Branch 1 taken 2069 times.
9298 if (valid) {
234
2/2
✓ Branch 0 taken 373827 times.
✓ Branch 1 taken 7229 times.
381056 for (int c = 0; c < D; ++c)
235 747654 acc += Wop[static_cast<std::size_t>(c) * D + k] *
236 373827 dOp[static_cast<std::size_t>(i) * D + c];
237 7229 }
238 9298 dYc[static_cast<std::size_t>(i) * D + k] = acc;
239 9298 }
240 186 });
241 // dWo (accumulate).
242
2/2
✓ Branch 0 taken 186 times.
✓ Branch 1 taken 19 times.
205 for (int i = 0; i < K; ++i) {
243
4/4
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 26 times.
✓ Branch 3 taken 38 times.
186 if (d_mask && d_mask[i] < 0.5f) continue;
244 148 const float* dOi = dOp + static_cast<std::size_t>(i) * D;
245 148 const float* Yi = Yp + static_cast<std::size_t>(i) * D;
246
2/2
✓ Branch 0 taken 7360 times.
✓ Branch 1 taken 148 times.
7508 for (int c = 0; c < D; ++c) {
247 7360 const float dov = dOi[c];
248 7360 float* dWrow = dWop + static_cast<std::size_t>(c) * D;
249
2/2
✓ Branch 0 taken 411136 times.
✓ Branch 1 taken 7360 times.
418496 for (int k = 0; k < D; ++k) {
250 411136 dWrow[k] += dov * Yi[k];
251 411136 }
252 7360 }
253 148 }
254
255 // dbo[c] += sum over valid rows of dO[i,c] (caller zeros).
256
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 15 times.
19 if (dbo) {
257
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 float* dbop = dbo->host_f32_mut();
258
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 4 times.
44 for (int i = 0; i < K; ++i) {
259
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 12 times.
40 if (d_mask && d_mask[i] < 0.5f) continue;
260 28 const float* dOi = dOp + static_cast<std::size_t>(i) * D;
261
2/2
✓ Branch 0 taken 1632 times.
✓ Branch 1 taken 28 times.
1660 for (int c = 0; c < D; ++c) {
262 1632 dbop[c] += dOi[c];
263 1632 }
264 28 }
265 4 }
266
267 // Per-head dAttn and dVh.
268
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
90 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
269 71 const int hh = static_cast<int>(hhi);
270
2/2
✓ Branch 0 taken 780 times.
✓ Branch 1 taken 71 times.
851 for (int i = 0; i < K; ++i) {
271
2/2
✓ Branch 0 taken 10127 times.
✓ Branch 1 taken 780 times.
10907 for (int j = 0; j < K; ++j) {
272 10127 float acc = 0.0f;
273
2/2
✓ Branch 0 taken 118159 times.
✓ Branch 1 taken 10127 times.
128286 for (int k = 0; k < dh; ++k) {
274 118159 const float dy = dYc[static_cast<std::size_t>(i) * D + (hh * dh + k)];
275 118159 const float vv = Vp[(static_cast<std::size_t>(hh) * K + j) * dh + k];
276 118159 acc += dy * vv;
277 118159 }
278 10127 dAttn[(static_cast<std::size_t>(hh) * K + i) * K + j] = acc;
279 10127 }
280 780 }
281
2/2
✓ Branch 0 taken 777 times.
✓ Branch 1 taken 71 times.
848 for (int i = 0; i < K; ++i) {
282 777 const float* Arow = Ap + (static_cast<std::size_t>(hh) * K + i) * K;
283 777 const float* dYseg = dYc.data() + static_cast<std::size_t>(i) * D + hh * dh;
284
2/2
✓ Branch 0 taken 10269 times.
✓ Branch 1 taken 777 times.
11046 for (int j = 0; j < K; ++j) {
285 10269 const float av = Arow[j];
286 10269 float* dVrow = dVh.data() + (static_cast<std::size_t>(hh) * K + j) * dh;
287
2/2
✓ Branch 0 taken 128257 times.
✓ Branch 1 taken 10269 times.
138526 for (int k = 0; k < dh; ++k) {
288 128257 dVrow[k] += av * dYseg[k];
289 128257 }
290 10269 }
291 777 }
292 71 });
293
294 // Per-head softmax backward → dScores.
295
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
89 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
296 70 const int hh = static_cast<int>(hhi);
297
2/2
✓ Branch 0 taken 772 times.
✓ Branch 1 taken 70 times.
842 for (int i = 0; i < K; ++i) {
298 772 const float* prow = Ap + (static_cast<std::size_t>(hh) * K + i) * K;
299 772 const float* dprow = dAttn.data() + (static_cast<std::size_t>(hh) * K + i) * K;
300 772 float* drow = dScores.data() + (static_cast<std::size_t>(hh) * K + i) * K;
301
4/4
✓ Branch 0 taken 303 times.
✓ Branch 1 taken 469 times.
✓ Branch 2 taken 127 times.
✓ Branch 3 taken 176 times.
772 if (d_mask && d_mask[i] < 0.5f) {
302
2/2
✓ Branch 0 taken 2500 times.
✓ Branch 1 taken 176 times.
2676 for (int j = 0; j < K; ++j) drow[j] = 0.0f;
303 176 continue;
304 }
305 596 float dot = 0.0f;
306
2/2
✓ Branch 0 taken 7664 times.
✓ Branch 1 taken 596 times.
8260 for (int j = 0; j < K; ++j) dot += dprow[j] * prow[j];
307
2/2
✓ Branch 0 taken 7586 times.
✓ Branch 1 taken 596 times.
8182 for (int j = 0; j < K; ++j) {
308
4/4
✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 5686 times.
✓ Branch 2 taken 823 times.
✓ Branch 3 taken 1077 times.
7586 if (d_mask && d_mask[j] < 0.5f) drow[j] = 0.0f;
309 6509 else drow[j] = prow[j] * (dprow[j] - dot) * inv_sqrtdh;
310 7586 }
311 596 }
312 70 });
313
314 // Per-head dQh and dKh.
315
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
90 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
316 71 const int hh = static_cast<int>(hhi);
317
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 71 times.
849 for (int i = 0; i < K; ++i) {
318
2/2
✓ Branch 0 taken 8967 times.
✓ Branch 1 taken 778 times.
9745 for (int k = 0; k < dh; ++k) {
319 8967 float acc = 0.0f;
320
2/2
✓ Branch 0 taken 108762 times.
✓ Branch 1 taken 8967 times.
117729 for (int j = 0; j < K; ++j) {
321 108762 const float ds = dScores[(static_cast<std::size_t>(hh) * K + i) * K + j];
322 108762 const float kk = Kp[(static_cast<std::size_t>(hh) * K + j) * dh + k];
323 108762 acc += ds * kk;
324 108762 }
325 8967 dQh[(static_cast<std::size_t>(hh) * K + i) * dh + k] = acc;
326 8967 }
327 778 }
328
2/2
✓ Branch 0 taken 779 times.
✓ Branch 1 taken 71 times.
850 for (int i = 0; i < K; ++i) {
329 779 const float* dSrow = dScores.data() + (static_cast<std::size_t>(hh) * K + i) * K;
330 779 const float* Qseg = Qp + (static_cast<std::size_t>(hh) * K + i) * dh;
331
2/2
✓ Branch 0 taken 10214 times.
✓ Branch 1 taken 779 times.
10993 for (int j = 0; j < K; ++j) {
332 10214 const float ds = dSrow[j];
333 10214 float* dKrow = dKh.data() + (static_cast<std::size_t>(hh) * K + j) * dh;
334
2/2
✓ Branch 0 taken 123177 times.
✓ Branch 1 taken 10214 times.
133391 for (int k = 0; k < dh; ++k) {
335 123177 dKrow[k] += ds * Qseg[k];
336 123177 }
337 10214 }
338 779 }
339 71 });
340
341 // dbq/dbk/dbv accumulate: db[hh*dh+j] += sum_i dQh/dKh/dVh(hh,i,j).
342
4/6
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
19 if (dbq || dbk || dbv) {
343
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 float* dbqp = dbq ? dbq->host_f32_mut() : nullptr;
344
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 float* dbkp = dbk ? dbk->host_f32_mut() : nullptr;
345
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 float* dbvp = dbv ? dbv->host_f32_mut() : nullptr;
346
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
16 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
347 12 const int hh = static_cast<int>(hhi);
348
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 float* dbq_row = dbqp ? dbqp + hh * dh : nullptr;
349
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 float* dbk_row = dbkp ? dbkp + hh * dh : nullptr;
350
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 float* dbv_row = dbvp ? dbvp + hh * dh : nullptr;
351
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 16 times.
124 for (int i = 0; i < K; ++i) {
352 140 const std::size_t base = (static_cast<std::size_t>(hh) * K + i) * dh;
353
2/2
✓ Branch 0 taken 1298 times.
✓ Branch 1 taken 112 times.
1410 for (int j = 0; j < dh; ++j) {
354
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1284 times.
1298 if (dbq_row) dbq_row[j] += dQh[base + j];
355
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 987 times.
1298 if (dbk_row) dbk_row[j] += dKh[base + j];
356
2/2
✓ Branch 0 taken 281 times.
✓ Branch 1 taken 989 times.
1270 if (dbv_row) dbv_row[j] += dVh[base + j];
357 1270 }
358 112 }
359 16 });
360 4 }
361
362 // dWq/dWk/dWv accumulate: dW(hh*dh+j, k) += sum_i dQh(hh,i,j) * X(i,k).
363
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
89 parallel_for(static_cast<std::size_t>(H), [&](std::size_t hhi) {
364 70 const int hh = static_cast<int>(hhi);
365
2/2
✓ Branch 0 taken 778 times.
✓ Branch 1 taken 70 times.
848 for (int i = 0; i < K; ++i) {
366 778 const float* Xi = Xp + static_cast<std::size_t>(i) * D;
367 778 const std::size_t base = (static_cast<std::size_t>(hh) * K + i) * dh;
368
2/2
✓ Branch 0 taken 9386 times.
✓ Branch 1 taken 778 times.
10164 for (int j = 0; j < dh; ++j) {
369 9386 const int wrow = hh * dh + j;
370 9386 const float qv = dQh[base + j];
371 9386 const float kv = dKh[base + j];
372 9386 const float vv = dVh[base + j];
373 9386 float* dWqrow = dWqp + static_cast<std::size_t>(wrow) * D;
374 9386 float* dWkrow = dWkp + static_cast<std::size_t>(wrow) * D;
375 9386 float* dWvrow = dWvp + static_cast<std::size_t>(wrow) * D;
376
2/2
✓ Branch 0 taken 457793 times.
✓ Branch 1 taken 9386 times.
467179 for (int k = 0; k < D; ++k) {
377 457793 const float xv = Xi[k];
378 457793 dWqrow[k] += qv * xv;
379 457793 dWkrow[k] += kv * xv;
380 457793 dWvrow[k] += vv * xv;
381 457793 }
382 9386 }
383 778 }
384 70 });
385
386 // dX(i,k) = sum over heads, j of dQh*Wq + dKh*Wk + dVh*Wv at (hh*dh+j, k).
387
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
205 parallel_for(static_cast<std::size_t>(K), [&](std::size_t ii) {
388 186 const int i = static_cast<int>(ii);
389
2/2
✓ Branch 0 taken 9389 times.
✓ Branch 1 taken 186 times.
9575 for (int k = 0; k < D; ++k) {
390 9389 float acc = 0.0f;
391
2/2
✓ Branch 0 taken 40599 times.
✓ Branch 1 taken 9389 times.
49988 for (int hh = 0; hh < H; ++hh) {
392
2/2
✓ Branch 0 taken 393572 times.
✓ Branch 1 taken 40599 times.
434171 for (int j = 0; j < dh; ++j) {
393 393572 const int wrow = hh * dh + j;
394 393572 const std::size_t widx = static_cast<std::size_t>(wrow) * D + k;
395 393572 const float gq = dQh[(static_cast<std::size_t>(hh) * K + i) * dh + j];
396 393572 const float gk = dKh[(static_cast<std::size_t>(hh) * K + i) * dh + j];
397 393572 const float gv = dVh[(static_cast<std::size_t>(hh) * K + i) * dh + j];
398 393572 acc += gq * Wqp[widx] + gk * Wkp[widx] + gv * Wvp[widx];
399 393572 }
400 40599 }
401 9389 dXp[static_cast<std::size_t>(i) * D + k] = acc;
402 9389 }
403 186 });
404 19 }
405
406 // ─── Concat / split ────────────────────────────────────────────────────────
407
408 2 void concat_rows(const std::vector<const ::brotensor::Tensor*>& parts,
409 ::brotensor::Tensor& out) {
410 2 int total = 0;
411
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
8 for (const auto* p : parts) {
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (p) total += p->size();
413 }
414
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 if (out.rows != total || out.cols != 1 ||
415 ✗ out.dtype != ::brotensor::Dtype::FP32) {
416 2 out.resize(total, 1, ::brotensor::Dtype::FP32);
417 2 }
418
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (total == 0) return;
419 2 float* op = out.host_f32_mut();
420 2 int off = 0;
421
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 for (const auto* p : parts) {
422
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!p) continue;
423 6 const int n = p->size();
424
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (n == 0) continue;
425 6 const float* pp = p->host_f32();
426
2/2
✓ Branch 0 taken 71680 times.
✓ Branch 1 taken 6 times.
71686 for (int i = 0; i < n; ++i) op[off + i] = pp[i];
427 6 off += n;
428 }
429 2 }
430
431 ✗ void split_rows(const ::brotensor::Tensor& in,
432 const std::vector<::brotensor::Tensor*>& parts) {
433 ✗ const float* ip = in.host_f32();
434 ✗ int off = 0;
435
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (auto* p : parts) {
436
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!p) continue;
437 ✗ const int n = p->size();
438
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (n == 0) continue;
439 ✗ float* pp = p->host_f32_mut();
440
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < n; ++i) pp[i] = ip[off + i];
441 ✗ off += n;
442 }
443 ✗ }
444
445 // ─── Batched elementwise / linear ──────────────────────────────────────────
446
447 ✗ void add_inplace_batched(::brotensor::Tensor& Y_BD,
448 const ::brotensor::Tensor& X_BD) {
449 ✗ const int n = Y_BD.size();
450 assert(X_BD.size() == n);
451 ✗ float* yp = Y_BD.host_f32_mut();
452 ✗ const float* xp = X_BD.host_f32();
453
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < n; ++i) yp[i] += xp[i];
454 ✗ }
455
456 59 void linear_forward_batched(const ::brotensor::Tensor& W,
457 const ::brotensor::Tensor& bias,
458 const ::brotensor::Tensor& X_BD,
459 ::brotensor::Tensor& Y_BD) {
460 59 const int out_dim = W.rows;
461 59 const int in_dim = W.cols;
462 59 const int B = X_BD.rows;
463
5/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 37 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 9 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 13 times.
59 if (Y_BD.rows != B || Y_BD.cols != out_dim ||
464 13 Y_BD.dtype != ::brotensor::Dtype::FP32) {
465 46 Y_BD.resize(B, out_dim, ::brotensor::Dtype::FP32);
466 46 }
467
2/4
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 59 times.
59 if (B == 0 || out_dim == 0) return;
468
469 59 const float* bp = bias.host_f32();
470 59 const float* Xp = X_BD.host_f32();
471 59 float* Yp = Y_BD.host_f32_mut();
472
473 // 16-bit weights (FP32 activations): widen per element.
474
4/4
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 51 times.
59 if (W.dtype == ::brotensor::Dtype::FP16 ||
475 56 W.dtype == ::brotensor::Dtype::BF16) {
476 8 const bool bf = (W.dtype == ::brotensor::Dtype::BF16);
477 8 const std::uint16_t* Wp = static_cast<const std::uint16_t*>(W.data);
478
2/2
✓ Branch 0 taken 276 times.
✓ Branch 1 taken 8 times.
284 for (int b = 0; b < B; ++b) {
479 276 const float* xr = Xp + static_cast<std::size_t>(b) * in_dim;
480 276 float* yr = Yp + static_cast<std::size_t>(b) * out_dim;
481
2/2
✓ Branch 0 taken 26582 times.
✓ Branch 1 taken 276 times.
26858 for (int i = 0; i < out_dim; ++i) {
482 26582 const std::uint16_t* wr = Wp + static_cast<std::size_t>(i) * in_dim;
483 26582 float acc = bp[i];
484
2/2
✓ Branch 0 taken 2192330 times.
✓ Branch 1 taken 26582 times.
2218912 for (int j = 0; j < in_dim; ++j) {
485
2/2
✓ Branch 0 taken 1097746 times.
✓ Branch 1 taken 1094584 times.
2192330 const float w = bf ? ::brotensor::bf16_bits_to_fp32(wr[j])
486 1094584 : ::brotensor::fp16_bits_to_fp32(wr[j]);
487 2192330 acc += w * xr[j];
488 2192330 }
489 26582 yr[i] = acc;
490 26582 }
491 276 }
492 8 return;
493 }
494
495 51 const float* Wp = W.host_f32();
496
2/2
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 5424 times.
5475 for (int b = 0; b < B; ++b) {
497 5424 const float* xr = Xp + static_cast<std::size_t>(b) * in_dim;
498 5424 float* yr = Yp + static_cast<std::size_t>(b) * out_dim;
499
2/2
✓ Branch 0 taken 4625845 times.
✓ Branch 1 taken 5424 times.
4631269 for (int i = 0; i < out_dim; ++i) {
500 4625845 const float* wr = Wp + static_cast<std::size_t>(i) * in_dim;
501 4625845 float acc = bp[i];
502
2/2
✓ Branch 0 taken 4478111121 times.
✓ Branch 1 taken 4625845 times.
4482736966 for (int j = 0; j < in_dim; ++j) acc += wr[j] * xr[j];
503 4625845 yr[i] = acc;
504 4625845 }
505 5424 }
506 59 }
507
508 26 void linear_backward_batched(const ::brotensor::Tensor& W,
509 const ::brotensor::Tensor& X_BD,
510 const ::brotensor::Tensor& dY_BD,
511 ::brotensor::Tensor& dX_BD,
512 ::brotensor::Tensor& dW,
513 ::brotensor::Tensor& dB) {
514 26 const int out_dim = W.rows;
515 26 const int in_dim = W.cols;
516 26 const int B = X_BD.rows;
517
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
26 if (dX_BD.rows != B || dX_BD.cols != in_dim ||
518 3 dX_BD.dtype != ::brotensor::Dtype::FP32) {
519 23 dX_BD.resize(B, in_dim, ::brotensor::Dtype::FP32);
520 23 }
521
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (B == 0) return;
522
523 26 const float* Wp = W.host_f32();
524 26 const float* Xp = X_BD.host_f32();
525 26 const float* dYp = dY_BD.host_f32();
526 26 float* dXp = dX_BD.host_f32_mut();
527 26 float* dWp = dW.host_f32_mut();
528 26 float* dBp = dB.host_f32_mut();
529
530 // dX[b] = W^T * dY[b] (overwrite).
531
2/2
✓ Branch 0 taken 2572 times.
✓ Branch 1 taken 26 times.
2598 for (int b = 0; b < B; ++b) {
532 2572 const float* dyr = dYp + static_cast<std::size_t>(b) * out_dim;
533 2572 float* dxr = dXp + static_cast<std::size_t>(b) * in_dim;
534
2/2
✓ Branch 0 taken 977569 times.
✓ Branch 1 taken 2572 times.
980141 for (int j = 0; j < in_dim; ++j) dxr[j] = 0.0f;
535
2/2
✓ Branch 0 taken 1219306 times.
✓ Branch 1 taken 2572 times.
1221878 for (int i = 0; i < out_dim; ++i) {
536 1219306 const float g = dyr[i];
537 1219306 const float* wr = Wp + static_cast<std::size_t>(i) * in_dim;
538
2/2
✓ Branch 0 taken 304113406 times.
✓ Branch 1 taken 1219306 times.
305332712 for (int j = 0; j < in_dim; ++j) dxr[j] += wr[j] * g;
539 1219306 }
540 2572 }
541 // dW += sum_b dY[b] * X[b]^T; dB += sum_b dY[b] (accumulate).
542
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2572 times.
2598 for (int b = 0; b < B; ++b) {
543 2572 const float* dyr = dYp + static_cast<std::size_t>(b) * out_dim;
544 2572 const float* xr = Xp + static_cast<std::size_t>(b) * in_dim;
545
2/2
✓ Branch 0 taken 1219306 times.
✓ Branch 1 taken 2572 times.
1221878 for (int i = 0; i < out_dim; ++i) {
546 1219306 const float g = dyr[i];
547 1219306 float* dwr = dWp + static_cast<std::size_t>(i) * in_dim;
548
2/2
✓ Branch 0 taken 304113406 times.
✓ Branch 1 taken 1219306 times.
305332712 for (int j = 0; j < in_dim; ++j) dwr[j] += g * xr[j];
549 1219306 dBp[i] += g;
550 1219306 }
551 2572 }
552 26 }
553
554 ✗ void relu_forward_batched(const ::brotensor::Tensor& X_BD,
555 ::brotensor::Tensor& Y_BD) {
556 ✗ ensure_match(X_BD, Y_BD);
557 ✗ const int n = X_BD.size();
558 ✗ const float* xp = X_BD.host_f32();
559 ✗ float* yp = Y_BD.host_f32_mut();
560
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ for (int i = 0; i < n; ++i) yp[i] = xp[i] > 0.0f ? xp[i] : 0.0f;
561 ✗ }
562
563 3 void relu_backward_batched(const ::brotensor::Tensor& X_BD,
564 const ::brotensor::Tensor& dY_BD,
565 ::brotensor::Tensor& dX_BD) {
566 3 ensure_match(X_BD, dX_BD);
567 3 const int n = X_BD.size();
568 3 const float* xp = X_BD.host_f32();
569 3 const float* dYp = dY_BD.host_f32();
570 3 float* dXp = dX_BD.host_f32_mut();
571
4/4
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1277 times.
✓ Branch 3 taken 1315 times.
2595 for (int i = 0; i < n; ++i) dXp[i] = xp[i] > 0.0f ? dYp[i] : 0.0f;
572 3 }
573
574 3 void tanh_forward_batched(const ::brotensor::Tensor& X_BD,
575 ::brotensor::Tensor& Y_BD) {
576 3 ensure_match(X_BD, Y_BD);
577 3 const int n = X_BD.size();
578 3 const float* xp = X_BD.host_f32();
579 3 float* yp = Y_BD.host_f32_mut();
580
2/2
✓ Branch 0 taken 2592 times.
✓ Branch 1 taken 3 times.
2595 for (int i = 0; i < n; ++i) yp[i] = std::tanh(xp[i]);
581 3 }
582
583 4 void tanh_backward_batched(const ::brotensor::Tensor& Y_BD,
584 const ::brotensor::Tensor& dY_BD,
585 ::brotensor::Tensor& dX_BD) {
586 4 ensure_match(Y_BD, dX_BD);
587 4 const int n = Y_BD.size();
588 4 const float* yp = Y_BD.host_f32();
589 4 const float* dYp = dY_BD.host_f32();
590 4 float* dXp = dX_BD.host_f32_mut();
591
2/2
✓ Branch 0 taken 2622 times.
✓ Branch 1 taken 4 times.
2626 for (int i = 0; i < n; ++i) {
592 2622 const float yc = std::clamp(yp[i], -1.0f, 1.0f);
593 2622 dXp[i] = dYp[i] * (1.0f - yc * yc);
594 2622 }
595 4 }
596
597 // ─── Batched per-sample losses ─────────────────────────────────────────────
598
599 3 void mse_vec_per_sample(const ::brotensor::Tensor& pred,
600 const ::brotensor::Tensor& target,
601 ::brotensor::Tensor& dPred,
602 ::brotensor::Tensor& loss_per_sample) {
603 3 const int B = pred.size();
604
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (dPred.rows != pred.rows || dPred.cols != pred.cols ||
605 3 dPred.dtype != ::brotensor::Dtype::FP32) {
606 ✗ dPred.resize(pred.rows, pred.cols, ::brotensor::Dtype::FP32);
607 ✗ }
608
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 3 times.
3 if (loss_per_sample.rows != B || loss_per_sample.cols != 1 ||
609 3 loss_per_sample.dtype != ::brotensor::Dtype::FP32) {
610 ✗ loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32);
611 ✗ }
612
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (B == 0) return;
613 3 const float* pp = pred.host_f32();
614 3 const float* tp = target.host_f32();
615 3 float* dp = dPred.host_f32_mut();
616 3 float* lp = loss_per_sample.host_f32_mut();
617
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 3 times.
76 for (int b = 0; b < B; ++b) {
618 73 const float d = pp[b] - tp[b];
619 73 dp[b] = d;
620 73 lp[b] = 0.5f * d * d;
621 73 }
622 3 }
623
624 6 void softmax_xent_fused_batched(const ::brotensor::Tensor& logits_BL,
625 const ::brotensor::Tensor& target_BL,
626 const float* d_mask_BL,
627 const int* d_head_offsets,
628 int n_heads,
629 ::brotensor::Tensor& probs_BL,
630 ::brotensor::Tensor& dLogits_BL,
631 ::brotensor::Tensor& loss_per_sample) {
632 6 const int B = logits_BL.rows;
633 6 const int n_act = logits_BL.cols;
634
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (probs_BL.rows != B || probs_BL.cols != n_act ||
635 6 probs_BL.dtype != ::brotensor::Dtype::FP32) {
636 ✗ probs_BL.resize(B, n_act, ::brotensor::Dtype::FP32);
637 ✗ }
638
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (dLogits_BL.rows != B || dLogits_BL.cols != n_act ||
639 6 dLogits_BL.dtype != ::brotensor::Dtype::FP32) {
640 ✗ dLogits_BL.resize(B, n_act, ::brotensor::Dtype::FP32);
641 ✗ }
642
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (loss_per_sample.rows != B || loss_per_sample.cols != 1 ||
643 6 loss_per_sample.dtype != ::brotensor::Dtype::FP32) {
644 ✗ loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32);
645 ✗ }
646
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
6 if (B == 0 || n_act == 0 || n_heads <= 0) return;
647
648 6 const float* lp = logits_BL.host_f32();
649 6 const float* tp = target_BL.host_f32();
650 6 float* pp = probs_BL.host_f32_mut();
651 6 float* dp = dLogits_BL.host_f32_mut();
652 6 float* lossp = loss_per_sample.host_f32_mut();
653
654
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 34 times.
40 for (int b = 0; b < B; ++b) {
655 // Overwrite loss before per-head accumulation.
656 34 float sample_loss = 0.0f;
657 34 const int row_off = b * n_act;
658
2/2
✓ Branch 0 taken 69 times.
✓ Branch 1 taken 34 times.
103 for (int h = 0; h < n_heads; ++h) {
659 69 const int off = d_head_offsets[h];
660 69 const int end = d_head_offsets[h + 1];
661 69 const int len = end - off;
662 69 const float* lr = lp + row_off + off;
663 69 const float* tr = tp + row_off + off;
664
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 33 times.
69 const float* mr = d_mask_BL ? (d_mask_BL + row_off + off) : nullptr;
665 69 float* pr = pp + row_off + off;
666 69 float* dr = dp + row_off + off;
667
668 69 float m = -1e30f;
669
2/2
✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 69 times.
2181 for (int i = 0; i < len; ++i) {
670
4/4
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 1056 times.
✓ Branch 2 taken 528 times.
✓ Branch 3 taken 528 times.
2112 if (mr && mr[i] < 0.5f) continue;
671
2/2
✓ Branch 0 taken 1334 times.
✓ Branch 1 taken 250 times.
1584 if (lr[i] > m) m = lr[i];
672 1584 }
673 69 float sum = 0.0f;
674
2/2
✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 69 times.
2181 for (int i = 0; i < len; ++i) {
675
4/4
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 1056 times.
✓ Branch 2 taken 528 times.
✓ Branch 3 taken 528 times.
2112 if (mr && mr[i] < 0.5f) { pr[i] = 0.0f; continue; }
676 1584 const float e = std::exp(lr[i] - m);
677 1584 pr[i] = e;
678 1584 sum += e;
679 1584 }
680
1/2
✓ Branch 0 taken 69 times.
✗ Branch 1 not taken.
69 const float inv = sum > 0.0f ? 1.0f / sum : 0.0f;
681
2/2
✓ Branch 0 taken 2112 times.
✓ Branch 1 taken 69 times.
2181 for (int i = 0; i < len; ++i) {
682
4/4
✓ Branch 0 taken 1056 times.
✓ Branch 1 taken 1056 times.
✓ Branch 2 taken 528 times.
✓ Branch 3 taken 528 times.
2112 if (mr && mr[i] < 0.5f) { dr[i] = 0.0f; continue; }
683 1584 const float p = pr[i] * inv;
684 1584 pr[i] = p;
685 1584 const float t = tr[i];
686
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1584 times.
1584 if (t > 0.0f) {
687
1/2
✓ Branch 0 taken 1584 times.
✗ Branch 1 not taken.
1584 const float pc = p > 1e-12f ? p : 1e-12f;
688 1584 sample_loss -= t * std::log(pc);
689 1584 }
690 1584 dr[i] = p - t;
691 1584 }
692 69 }
693 34 lossp[b] = sample_loss;
694 34 }
695 6 }
696
697 7 void bce_with_logits_fused_batched(const ::brotensor::Tensor& logits_BL,
698 const ::brotensor::Tensor& target_BL,
699 const float* d_mask_BL,
700 float pos_weight,
701 ::brotensor::Tensor& probs_BL,
702 ::brotensor::Tensor& dLogits_BL,
703 ::brotensor::Tensor& loss_per_sample) {
704 7 const int B = logits_BL.rows;
705 7 const int L = logits_BL.cols;
706
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
7 if (probs_BL.rows != B || probs_BL.cols != L ||
707 7 probs_BL.dtype != ::brotensor::Dtype::FP32) {
708 ✗ probs_BL.resize(B, L, ::brotensor::Dtype::FP32);
709 ✗ }
710
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
7 if (dLogits_BL.rows != B || dLogits_BL.cols != L ||
711 7 dLogits_BL.dtype != ::brotensor::Dtype::FP32) {
712 ✗ dLogits_BL.resize(B, L, ::brotensor::Dtype::FP32);
713 ✗ }
714
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
7 if (loss_per_sample.rows != B || loss_per_sample.cols != 1 ||
715 7 loss_per_sample.dtype != ::brotensor::Dtype::FP32) {
716 ✗ loss_per_sample.resize(B, 1, ::brotensor::Dtype::FP32);
717 ✗ }
718
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 if (B == 0 || L == 0) return;
719
720 7 const float* lp = logits_BL.host_f32();
721 7 const float* tp = target_BL.host_f32();
722 7 float* pp = probs_BL.host_f32_mut();
723 7 float* dp = dLogits_BL.host_f32_mut();
724 7 float* lossp = loss_per_sample.host_f32_mut();
725 7 const float w = pos_weight;
726
727
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 25 times.
32 for (int b = 0; b < B; ++b) {
728 25 float sample_loss = 0.0f;
729 25 const int row_off = b * L;
730 25 const float* lr = lp + row_off;
731 25 const float* tr = tp + row_off;
732
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 16 times.
25 const float* mr = d_mask_BL ? (d_mask_BL + row_off) : nullptr;
733 25 float* pr = pp + row_off;
734 25 float* dr = dp + row_off;
735
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 25 times.
79 for (int i = 0; i < L; ++i) {
736
4/4
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 17 times.
54 if (mr && mr[i] < 0.5f) {
737 17 pr[i] = 0.0f;
738 17 dr[i] = 0.0f;
739 17 continue;
740 }
741 37 const float z = lr[i];
742 37 const float y = tr[i];
743 // softplus(x) = max(x, 0) + log1p(exp(-|x|))
744 37 const float az = std::fabs(z);
745
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 19 times.
37 const float sp_neg = (z > 0.0f ? 0.0f : -z) + std::log1p(std::exp(-az));
746
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 19 times.
37 const float sp_pos = (z > 0.0f ? z : 0.0f) + std::log1p(std::exp(-az));
747 37 sample_loss += w * y * sp_neg + (1.0f - y) * sp_pos;
748 // s = sigmoid(z), numerically stable.
749
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 15 times.
37 const float s = z >= 0.0f
750 22 ? 1.0f / (1.0f + std::exp(-z))
751 15 : std::exp(z) / (1.0f + std::exp(z));
752 37 pr[i] = s;
753 37 dr[i] = s * (w * y + 1.0f - y) - w * y;
754 37 }
755 25 lossp[b] = sample_loss;
756 25 }
757 7 }
758
759 // ─── Masked mean-pool ──────────────────────────────────────────────────────
760
761 4 void build_slot_mask(const ::brotensor::Tensor& x, int offset, int K, int stride,
762 ::brotensor::Tensor& mask) {
763
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (mask.rows != K || mask.cols != 1 ||
764 ✗ mask.dtype != ::brotensor::Dtype::FP32) {
765 4 mask.resize(K, 1, ::brotensor::Dtype::FP32);
766 4 }
767 4 const float* xp = x.host_f32();
768 4 float* mp = mask.host_f32_mut();
769
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 73 times.
77 for (int k = 0; k < K; ++k) {
770 73 mp[k] = (xp[offset + static_cast<std::size_t>(k) * stride] > 0.5f)
771 ? 1.0f : 0.0f;
772 73 }
773 4 }
774
775 4 void copy_d2d(const ::brotensor::Tensor& src, int src_off,
776 ::brotensor::Tensor& dst, int dst_off, int n) {
777 4 const float* sp = src.host_f32();
778 4 float* dp = dst.host_f32_mut();
779 4 std::memcpy(dp + dst_off, sp + src_off, static_cast<std::size_t>(n) * sizeof(float));
780 4 }
781
782 4 void copy_d2d_strided(const ::brotensor::Tensor& src, int src_off, int src_pitch,
783 ::brotensor::Tensor& dst, int dst_off, int dst_pitch,
784 int width, int height) {
785 4 const float* sp = src.host_f32() + src_off;
786 4 float* dp = dst.host_f32_mut() + dst_off;
787
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 4 times.
88 for (int r = 0; r < height; ++r) {
788 168 std::memcpy(dp + static_cast<std::size_t>(r) * dst_pitch,
789 84 sp + static_cast<std::size_t>(r) * src_pitch,
790 84 static_cast<std::size_t>(width) * sizeof(float));
791 84 }
792 4 }
793
794 6 void masked_mean_pool_forward(const ::brotensor::Tensor& X, const float* d_mask,
795 ::brotensor::Tensor& y) {
796 6 const int K = X.rows;
797 6 const int D = X.cols;
798
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
6 if (y.rows != D || y.cols != 1 || y.dtype != ::brotensor::Dtype::FP32) {
799 6 y.resize(D, 1, ::brotensor::Dtype::FP32);
800 6 }
801 6 const float* Xp = X.host_f32();
802 6 float* yp = y.host_f32_mut();
803
2/2
✓ Branch 0 taken 384 times.
✓ Branch 1 taken 6 times.
390 for (int j = 0; j < D; ++j) yp[j] = 0.0f;
804 6 int num_valid = 0;
805
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 162 times.
168 for (int k = 0; k < K; ++k) {
806
4/4
✓ Branch 0 taken 137 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 57 times.
✓ Branch 3 taken 80 times.
162 if (d_mask && d_mask[k] < 0.5f) continue;
807 82 const float* row = Xp + static_cast<std::size_t>(k) * D;
808
2/2
✓ Branch 0 taken 6800 times.
✓ Branch 1 taken 82 times.
6882 for (int j = 0; j < D; ++j) yp[j] += row[j];
809 82 ++num_valid;
810 82 }
811
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 5 times.
6 if (num_valid > 0) {
812 5 const float inv = 1.0f / static_cast<float>(num_valid);
813
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 5 times.
357 for (int j = 0; j < D; ++j) yp[j] *= inv;
814 5 }
815 6 }
816
817 4 void masked_mean_pool_backward(const ::brotensor::Tensor& dY, const float* d_mask,
818 int K, ::brotensor::Tensor& dX) {
819 4 const int D = dY.size();
820
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
4 if (dX.rows != K || dX.cols != D || dX.dtype != ::brotensor::Dtype::FP32) {
821 4 dX.resize(K, D, ::brotensor::Dtype::FP32);
822 4 }
823 4 const float* dYp = dY.host_f32();
824 4 float* dXp = dX.host_f32_mut();
825 4 int num_valid = 0;
826
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4 times.
126 for (int k = 0; k < K; ++k) {
827
4/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 46 times.
122 if (!d_mask || d_mask[k] >= 0.5f) ++num_valid;
828 122 }
829
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 const float inv = num_valid > 0 ? 1.0f / static_cast<float>(num_valid) : 0.0f;
830
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 4 times.
126 for (int k = 0; k < K; ++k) {
831 122 float* row = dXp + static_cast<std::size_t>(k) * D;
832
4/4
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 46 times.
122 const bool valid = (!d_mask || d_mask[k] >= 0.5f) && num_valid > 0;
833
4/4
✓ Branch 0 taken 12064 times.
✓ Branch 1 taken 122 times.
✓ Branch 2 taken 7072 times.
✓ Branch 3 taken 4992 times.
12186 for (int j = 0; j < D; ++j) row[j] = valid ? dYp[j] * inv : 0.0f;
834 122 }
835 4 }
836
837 } // namespace brotensor::detail::cpu
838