brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
456 / 490
93.1%
Branches:
110 / 224
49.1%
Functions:
64 / 66
97.0%

src/ops.cpp
Line Branch Exec Source
1 // brotensor public op wrappers — core dense, elementwise, norm, losses, embedding, concat, optim.
2 //
3 // Spatial/conv/pool/StyleGAN wrappers live in src/ops_spatial.cpp;
4 // attention/matmul/RoPE/RMSNorm/reductions live in src/ops_attention.cpp;
5 // quantisation/diffusion/audio/sampling/noise live in src/ops_quant_audio.cpp.
6
7 #include <brotensor/ops.h>
8 #include <brotensor/tensor.h>
9 #include <brotensor/detail/dispatch.h>
10
11 #include <cstdint>
12 #include <stdexcept>
13 #include <string>
14 #include <vector>
15
16 namespace brotensor {
17
18 namespace {
19
20 ✗ [[noreturn]] void throw_empty_parts(const char* op) {
21 ✗ std::string m = "brotensor: ";
22
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m += op;
23
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m += ": parts must be non-empty";
24
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error(m);
25 ✗ }
26
27 67 const detail::OpsVTable& vtable_from_parts(
28 const std::vector<const Tensor*>& parts, const char* op) {
29
1/2
✓ Branch 0 taken 67 times.
✗ Branch 1 not taken.
67 if (parts.empty() || parts[0] == nullptr) throw_empty_parts(op);
30 67 Device d = parts[0]->device;
31
2/2
✓ Branch 0 taken 99 times.
✓ Branch 1 taken 67 times.
166 for (std::size_t i = 1; i < parts.size(); ++i) {
32
2/4
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 99 times.
✗ Branch 3 not taken.
99 if (parts[i] && parts[i]->device != d) {
33 ✗ std::string m = "brotensor: ";
34
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m += op;
35
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m += ": parts[";
36
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ m += std::to_string(i);
37
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ m += "] on different device";
38
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error(m);
39 ✗ }
40 99 }
41 67 return detail::ops_for(d);
42 ✗ }
43
44 } // namespace
45
46 // ─── Dense layers + elementwise ────────────────────────────────────────────
47
48 1264 void linear_forward(const Tensor& W, const Tensor& b,
49 const Tensor& x, Tensor& y) {
50 1264 const auto& v = detail::dispatch(W, b, x, y);
51
1/2
✓ Branch 0 taken 1264 times.
✗ Branch 1 not taken.
1264 if (!v.linear_forward) detail::throw_not_implemented("linear_forward", W.device);
52 1264 detail::adopt_output(y, W.device);
53 1264 v.linear_forward(W, b, x, y);
54 1264 }
55
56 30 void linear_backward(const Tensor& W, const Tensor& x, const Tensor& dY,
57 Tensor& dX, Tensor& dW, Tensor& dB) {
58 30 const auto& v = detail::dispatch(W, x, dY, dX, dW, dB);
59
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (!v.linear_backward) detail::throw_not_implemented("linear_backward", W.device);
60 30 detail::adopt_output(dX, W.device);
61 30 detail::adopt_output(dW, W.device);
62 30 detail::adopt_output(dB, W.device);
63 30 v.linear_backward(W, x, dY, dX, dW, dB);
64 30 }
65
66 97 void relu_forward(const Tensor& x, Tensor& y) {
67 97 const auto& v = detail::dispatch(x, y);
68
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (!v.relu_forward) detail::throw_not_implemented("relu_forward", x.device);
69 97 detail::adopt_output(y, x.device);
70 97 v.relu_forward(x, y);
71 97 }
72
73 12 void relu_backward(const Tensor& x, const Tensor& dY, Tensor& dX) {
74 12 const auto& v = detail::dispatch(x, dY, dX);
75
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.relu_backward) detail::throw_not_implemented("relu_backward", x.device);
76 12 detail::adopt_output(dX, x.device);
77 12 v.relu_backward(x, dY, dX);
78 12 }
79
80 99 void tanh_forward(const Tensor& x, Tensor& y) {
81 99 const auto& v = detail::dispatch(x, y);
82
1/2
✓ Branch 0 taken 99 times.
✗ Branch 1 not taken.
99 if (!v.tanh_forward) detail::throw_not_implemented("tanh_forward", x.device);
83 99 detail::adopt_output(y, x.device);
84 99 v.tanh_forward(x, y);
85 99 }
86
87 12 void tanh_backward(const Tensor& y, const Tensor& dY, Tensor& dX) {
88 12 const auto& v = detail::dispatch(y, dY, dX);
89
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.tanh_backward) detail::throw_not_implemented("tanh_backward", y.device);
90 12 detail::adopt_output(dX, y.device);
91 12 v.tanh_backward(y, dY, dX);
92 12 }
93
94 26 void sigmoid_forward(const Tensor& x, Tensor& y) {
95 26 const auto& v = detail::dispatch(x, y);
96
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (!v.sigmoid_forward) detail::throw_not_implemented("sigmoid_forward", x.device);
97 26 detail::adopt_output(y, x.device);
98 26 v.sigmoid_forward(x, y);
99 26 }
100
101 12 void sigmoid_backward(const Tensor& y, const Tensor& dY, Tensor& dX) {
102 12 const auto& v = detail::dispatch(y, dY, dX);
103
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.sigmoid_backward) detail::throw_not_implemented("sigmoid_backward", y.device);
104 12 detail::adopt_output(dX, y.device);
105 12 v.sigmoid_backward(y, dY, dX);
106 12 }
107
108 1333 void add_inplace(Tensor& y, const Tensor& x) {
109 1333 const auto& v = detail::dispatch(y, x);
110
1/2
✓ Branch 0 taken 1333 times.
✗ Branch 1 not taken.
1333 if (!v.add_inplace) detail::throw_not_implemented("add_inplace", y.device);
111 1333 detail::adopt_output(y, y.device);
112 1333 v.add_inplace(y, x);
113 1333 }
114
115 4 void axpby_inplace(Tensor& y, const Tensor& x, float a, float b) {
116 4 const auto& v = detail::dispatch(y, x);
117
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (!v.axpby_inplace) detail::throw_not_implemented("axpby_inplace", y.device);
118 4 detail::adopt_output(y, y.device);
119 4 v.axpby_inplace(y, x, a, b);
120 4 }
121
122 17 void add_scalar_inplace(Tensor& y, float s) {
123 17 const auto& v = detail::dispatch(y);
124
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!v.add_scalar_inplace) detail::throw_not_implemented("add_scalar_inplace", y.device);
125 17 detail::adopt_output(y, y.device);
126 17 v.add_scalar_inplace(y, s);
127 17 }
128
129 40 void add_channel_bias_inplace(Tensor& y, const Tensor& bias, int C, int L) {
130 40 const auto& v = detail::dispatch(y, bias);
131
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 if (!v.add_channel_bias_inplace)
132 ✗ detail::throw_not_implemented("add_channel_bias_inplace", y.device);
133 40 detail::adopt_output(y, y.device);
134 40 v.add_channel_bias_inplace(y, bias, C, L);
135 40 }
136
137 15 void add_row_bias_inplace(Tensor& Y, const Tensor& bias) {
138 15 const auto& v = detail::dispatch(Y, bias);
139
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!v.add_row_bias_inplace)
140 ✗ detail::throw_not_implemented("add_row_bias_inplace", Y.device);
141 15 detail::adopt_output(Y, Y.device);
142 15 v.add_row_bias_inplace(Y, bias);
143 15 }
144
145 302 void scale_inplace(Tensor& y, float s) {
146 302 const auto& v = detail::dispatch(y);
147
1/2
✓ Branch 0 taken 302 times.
✗ Branch 1 not taken.
302 if (!v.scale_inplace) detail::throw_not_implemented("scale_inplace", y.device);
148 302 detail::adopt_output(y, y.device);
149 302 v.scale_inplace(y, s);
150 302 }
151
152 17 void clamp(Tensor& y, float lo, float hi) {
153
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
17 if (detail::jit_clamp_inplace_hook && detail::jit_clamp_inplace_hook(y, lo, hi)) {
154 1 return;
155 }
156 16 const auto& v = detail::dispatch(y);
157
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!v.clamp) detail::throw_not_implemented("clamp", y.device);
158 16 detail::adopt_output(y, y.device);
159 16 v.clamp(y, lo, hi);
160 17 }
161
162 1263 void mul_inplace(Tensor& y, const Tensor& x) {
163 1263 const auto& v = detail::dispatch(y, x);
164
1/2
✓ Branch 0 taken 1263 times.
✗ Branch 1 not taken.
1263 if (!v.mul_inplace) detail::throw_not_implemented("mul_inplace", y.device);
165 1263 detail::adopt_output(y, y.device);
166 1263 v.mul_inplace(y, x);
167 1263 }
168
169 ✗ void div_inplace(Tensor& y, const Tensor& x) {
170 ✗ const auto& v = detail::dispatch(y, x);
171
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ if (!v.div_inplace) detail::throw_not_implemented("div_inplace", y.device);
172 ✗ detail::adopt_output(y, y.device);
173 ✗ v.div_inplace(y, x);
174 ✗ }
175
176 1092 void modulate(const Tensor& X, const Tensor& scale, const Tensor& shift,
177 Tensor& Y) {
178 1092 const auto& v = detail::dispatch(X, scale, shift, Y);
179
1/2
✓ Branch 0 taken 1092 times.
✗ Branch 1 not taken.
1092 if (!v.modulate) detail::throw_not_implemented("modulate", X.device);
180 1092 detail::adopt_output(Y, X.device);
181 1092 v.modulate(X, scale, shift, Y);
182 1092 }
183
184 731 void broadcast_mul(const Tensor& X, const Tensor& vv, Tensor& Y) {
185 731 const auto& v = detail::dispatch(X, vv, Y);
186
1/2
✓ Branch 0 taken 731 times.
✗ Branch 1 not taken.
731 if (!v.broadcast_mul) detail::throw_not_implemented("broadcast_mul", X.device);
187 731 detail::adopt_output(Y, X.device);
188 731 v.broadcast_mul(X, vv, Y);
189 731 }
190
191 8 void build_slot_mask(const Tensor& x, int offset, int K, int stride, Tensor& mask) {
192 8 const auto& v = detail::dispatch(x, mask);
193
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!v.build_slot_mask) detail::throw_not_implemented("build_slot_mask", x.device);
194 8 detail::adopt_output(mask, x.device);
195 8 v.build_slot_mask(x, offset, K, stride, mask);
196 8 }
197
198 // ─── Reductions / norm / softmax / attention (training) ────────────────────
199
200 43 void softmax_forward(const Tensor& logits, Tensor& probs, const float* mask) {
201 43 const auto& v = detail::dispatch(logits, probs);
202
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 if (!v.softmax_forward) detail::throw_not_implemented("softmax_forward", logits.device);
203 43 detail::adopt_output(probs, logits.device);
204 43 v.softmax_forward(logits, probs, mask);
205 43 }
206
207 51 void softmax_rows_forward(const Tensor& X, Tensor& Y, int rows, int cols) {
208 51 const auto& v = detail::dispatch(X, Y);
209
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 if (!v.softmax_rows_forward) detail::throw_not_implemented("softmax_rows_forward", X.device);
210 51 detail::adopt_output(Y, X.device);
211 51 v.softmax_rows_forward(X, Y, rows, cols);
212 51 }
213
214 22 void softmax_backward(const Tensor& probs, const Tensor& dProbs, Tensor& dLogits) {
215 22 const auto& v = detail::dispatch(probs, dProbs, dLogits);
216
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!v.softmax_backward) detail::throw_not_implemented("softmax_backward", probs.device);
217 22 detail::adopt_output(dLogits, probs.device);
218 22 v.softmax_backward(probs, dProbs, dLogits);
219 22 }
220
221 9 void layernorm_forward(const Tensor& x, const Tensor& gamma, const Tensor& beta,
222 Tensor& y, Tensor& xhat,
223 float& mean_out, float& rstd_out, float eps) {
224 9 const auto& v = detail::dispatch(x, gamma, beta, y, xhat);
225
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.layernorm_forward) detail::throw_not_implemented("layernorm_forward", x.device);
226 9 detail::adopt_output(y, x.device);
227 9 detail::adopt_output(xhat, x.device);
228 9 v.layernorm_forward(x, gamma, beta, y, xhat, mean_out, rstd_out, eps);
229 9 }
230
231 15 void layernorm_backward(const Tensor& dY, const Tensor& xhat,
232 const Tensor& gamma, float rstd,
233 Tensor& dX, Tensor& dGamma, Tensor& dBeta) {
234 15 const auto& v = detail::dispatch(dY, xhat, gamma, dX, dGamma, dBeta);
235
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 if (!v.layernorm_backward) detail::throw_not_implemented("layernorm_backward", dY.device);
236 15 detail::adopt_output(dX, dY.device);
237 15 detail::adopt_output(dGamma, dY.device);
238 15 detail::adopt_output(dBeta, dY.device);
239 15 v.layernorm_backward(dY, xhat, gamma, rstd, dX, dGamma, dBeta);
240 15 }
241
242 20 void attention_forward(const Tensor& X,
243 const Tensor& Wq, const Tensor& Wk,
244 const Tensor& Wv, const Tensor& Wo,
245 const float* d_mask,
246 Tensor& Q, Tensor& K, Tensor& V,
247 Tensor& Attn, Tensor& Y_pre_Wo, Tensor& O) {
248 20 const auto& v = detail::dispatch_with_opts(
249 20 X, Wq, {&Wk, &Wv, &Wo, &Q, &K, &V, &Attn, &Y_pre_Wo, &O});
250
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!v.attention_forward) detail::throw_not_implemented("attention_forward", X.device);
251 20 detail::adopt_output(Q, X.device);
252 20 detail::adopt_output(K, X.device);
253 20 detail::adopt_output(V, X.device);
254 20 detail::adopt_output(Attn, X.device);
255 20 detail::adopt_output(Y_pre_Wo, X.device);
256 20 detail::adopt_output(O, X.device);
257 20 v.attention_forward(X, Wq, Wk, Wv, Wo, d_mask, Q, K, V, Attn, Y_pre_Wo, O);
258 20 }
259
260 16 void attention_backward(const Tensor& dO, const Tensor& X,
261 const Tensor& Q, const Tensor& K,
262 const Tensor& V, const Tensor& Attn,
263 const Tensor& Y_pre_Wo,
264 const Tensor& Wq, const Tensor& Wk,
265 const Tensor& Wv, const Tensor& Wo,
266 const float* d_mask,
267 Tensor& dX,
268 Tensor& dWq, Tensor& dWk,
269 Tensor& dWv, Tensor& dWo) {
270 16 const auto& v = detail::dispatch_with_opts(
271 96 dO, X, {&Q, &K, &V, &Attn, &Y_pre_Wo, &Wq, &Wk, &Wv, &Wo,
272 80 &dX, &dWq, &dWk, &dWv, &dWo});
273
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!v.attention_backward) detail::throw_not_implemented("attention_backward", dO.device);
274 16 detail::adopt_output(dX, dO.device);
275 16 detail::adopt_output(dWq, dO.device);
276 16 detail::adopt_output(dWk, dO.device);
277 16 detail::adopt_output(dWv, dO.device);
278 16 detail::adopt_output(dWo, dO.device);
279 32 v.attention_backward(dO, X, Q, K, V, Attn, Y_pre_Wo,
280 16 Wq, Wk, Wv, Wo, d_mask,
281 16 dX, dWq, dWk, dWv, dWo);
282 16 }
283
284 32 void mha_forward(const Tensor& X,
285 const Tensor& Wq, const Tensor& Wk,
286 const Tensor& Wv, const Tensor& Wo,
287 const Tensor* bq, const Tensor* bk,
288 const Tensor* bv, const Tensor* bo,
289 const float* d_mask, int num_heads,
290 Tensor& Qh, Tensor& Kh, Tensor& Vh,
291 Tensor& Attnh, Tensor& Yconcat, Tensor& O) {
292 32 const auto& v = detail::dispatch_with_opts(
293 32 X, Wq, {&Wk, &Wv, &Wo, bq, bk, bv, bo, &Qh, &Kh, &Vh, &Attnh, &Yconcat, &O});
294
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (!v.mha_forward) detail::throw_not_implemented("mha_forward", X.device);
295 32 detail::adopt_output(Qh, X.device);
296 32 detail::adopt_output(Kh, X.device);
297 32 detail::adopt_output(Vh, X.device);
298 32 detail::adopt_output(Attnh, X.device);
299 32 detail::adopt_output(Yconcat, X.device);
300 32 detail::adopt_output(O, X.device);
301 64 v.mha_forward(X, Wq, Wk, Wv, Wo, bq, bk, bv, bo, d_mask, num_heads,
302 32 Qh, Kh, Vh, Attnh, Yconcat, O);
303 32 }
304
305 30 void mha_backward(const Tensor& dO, const Tensor& X,
306 const Tensor& Qh, const Tensor& Kh,
307 const Tensor& Vh, const Tensor& Attnh,
308 const Tensor& Yconcat,
309 const Tensor& Wq, const Tensor& Wk,
310 const Tensor& Wv, const Tensor& Wo,
311 const float* d_mask, int num_heads,
312 Tensor& dX,
313 Tensor& dWq, Tensor& dWk,
314 Tensor& dWv, Tensor& dWo,
315 Tensor* dbq, Tensor* dbk,
316 Tensor* dbv, Tensor* dbo) {
317 30 const auto& v = detail::dispatch_with_opts(
318 300 dO, X, {&Qh, &Kh, &Vh, &Attnh, &Yconcat, &Wq, &Wk, &Wv, &Wo,
319 270 &dX, &dWq, &dWk, &dWv, &dWo, dbq, dbk, dbv, dbo});
320
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (!v.mha_backward) detail::throw_not_implemented("mha_backward", dO.device);
321 30 detail::adopt_output(dX, dO.device);
322 30 detail::adopt_output(dWq, dO.device);
323 30 detail::adopt_output(dWk, dO.device);
324 30 detail::adopt_output(dWv, dO.device);
325 30 detail::adopt_output(dWo, dO.device);
326
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 8 times.
30 if (dbq) detail::adopt_output(*dbq, dO.device);
327
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 8 times.
30 if (dbk) detail::adopt_output(*dbk, dO.device);
328
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 8 times.
30 if (dbv) detail::adopt_output(*dbv, dO.device);
329
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 8 times.
30 if (dbo) detail::adopt_output(*dbo, dO.device);
330 60 v.mha_backward(dO, X, Qh, Kh, Vh, Attnh, Yconcat,
331 30 Wq, Wk, Wv, Wo, d_mask, num_heads,
332 30 dX, dWq, dWk, dWv, dWo,
333 30 dbq, dbk, dbv, dbo);
334 30 }
335
336 // ─── Pooling / losses / embedding / concat ─────────────────────────────────
337
338 22 void masked_mean_pool_forward(const Tensor& X, const float* d_mask, Tensor& y) {
339 22 const auto& v = detail::dispatch(X, y);
340
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!v.masked_mean_pool_forward) detail::throw_not_implemented("masked_mean_pool_forward", X.device);
341 22 detail::adopt_output(y, X.device);
342 22 v.masked_mean_pool_forward(X, d_mask, y);
343 22 }
344
345 16 void masked_mean_pool_backward(const Tensor& dY, const float* d_mask,
346 int K, Tensor& dX) {
347 16 const auto& v = detail::dispatch(dY, dX);
348
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!v.masked_mean_pool_backward) detail::throw_not_implemented("masked_mean_pool_backward", dY.device);
349 16 detail::adopt_output(dX, dY.device);
350 16 v.masked_mean_pool_backward(dY, d_mask, K, dX);
351 16 }
352
353 9 float mse_vec_forward(const Tensor& pred, const Tensor& target) {
354 9 const auto& v = detail::dispatch(pred, target);
355
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.mse_vec_forward) detail::throw_not_implemented("mse_vec_forward", pred.device);
356 9 return v.mse_vec_forward(pred, target);
357 }
358
359 9 void mse_vec_backward(const Tensor& pred, const Tensor& target, Tensor& dPred) {
360 9 const auto& v = detail::dispatch(pred, target, dPred);
361
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.mse_vec_backward) detail::throw_not_implemented("mse_vec_backward", pred.device);
362 9 detail::adopt_output(dPred, pred.device);
363 9 v.mse_vec_backward(pred, target, dPred);
364 9 }
365
366 7 float mse_scalar(float pred, float target, float& dPred) {
367 7 const auto& v = detail::ops_for(Device::CPU);
368
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!v.mse_scalar) detail::throw_not_implemented("mse_scalar", Device::CPU);
369 7 return v.mse_scalar(pred, target, dPred);
370 }
371
372 7 float softmax_xent(const Tensor& logits, const Tensor& target,
373 Tensor& probs, Tensor& dLogits, const float* mask) {
374 7 const auto& v = detail::dispatch(logits, target, probs, dLogits);
375
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!v.softmax_xent) detail::throw_not_implemented("softmax_xent", logits.device);
376 7 detail::adopt_output(probs, logits.device);
377 7 detail::adopt_output(dLogits, logits.device);
378 7 return v.softmax_xent(logits, target, probs, dLogits, mask);
379 }
380
381 11 float softmax_xent_segment(const float* logits, const float* target,
382 float* probs, float* dLogits,
383 int n, const float* mask) {
384 11 const auto& v = detail::ops_for(Device::CPU);
385
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!v.softmax_xent_segment) detail::throw_not_implemented("softmax_xent_segment", Device::CPU);
386 11 return v.softmax_xent_segment(logits, target, probs, dLogits, n, mask);
387 }
388
389 19 float softmax_xent_fused(const Tensor& logits, const Tensor& target,
390 const float* d_mask,
391 Tensor& probs, Tensor& dLogits) {
392 19 const auto& v = detail::dispatch(logits, target, probs, dLogits);
393
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (!v.softmax_xent_fused) detail::throw_not_implemented("softmax_xent_fused", logits.device);
394 19 detail::adopt_output(probs, logits.device);
395 19 detail::adopt_output(dLogits, logits.device);
396 19 return v.softmax_xent_fused(logits, target, d_mask, probs, dLogits);
397 }
398
399 39 void embedding_lookup_forward(const Tensor& table, const int32_t* d_idx,
400 int B, Tensor& out) {
401 39 const auto& v = detail::dispatch(table, out);
402
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 if (!v.embedding_lookup_forward) detail::throw_not_implemented("embedding_lookup_forward", table.device);
403 39 detail::adopt_output(out, table.device);
404 39 v.embedding_lookup_forward(table, d_idx, B, out);
405 39 }
406
407 27 void embedding_lookup_backward(const Tensor& dOut, const int32_t* d_idx,
408 int B, Tensor& dTable) {
409 27 const auto& v = detail::dispatch(dOut, dTable);
410
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (!v.embedding_lookup_backward) detail::throw_not_implemented("embedding_lookup_backward", dOut.device);
411 27 detail::adopt_output(dTable, dOut.device);
412 27 v.embedding_lookup_backward(dOut, d_idx, B, dTable);
413 27 }
414
415 17 void concat_rows(const std::vector<const Tensor*>& parts, Tensor& out) {
416 17 const auto& v = vtable_from_parts(parts, "concat_rows");
417
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
17 if (out.data != nullptr && parts[0]->device != out.device) {
418
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: concat_rows: out on different device");
419 }
420 17 detail::adopt_output(out, parts[0]->device);
421
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!v.concat_rows) detail::throw_not_implemented("concat_rows", parts[0]->device);
422 17 v.concat_rows(parts, out);
423 17 }
424
425 12 void split_rows(const Tensor& in, const std::vector<Tensor*>& parts) {
426 12 Device d = in.device;
427
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 12 times.
52 for (std::size_t i = 0; i < parts.size(); ++i) {
428
3/6
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
40 if (parts[i] && parts[i]->data != nullptr && parts[i]->device != d) {
429
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: split_rows: parts on different device");
430 }
431 40 }
432 12 const auto& v = detail::ops_for(d);
433
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.split_rows) detail::throw_not_implemented("split_rows", d);
434
3/4
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
52 for (Tensor* _p : parts) if (_p) detail::adopt_output(*_p, d);
435 12 v.split_rows(in, parts);
436 12 }
437
438 23 void concat_batched_rows(const std::vector<const Tensor*>& parts, Tensor& out) {
439 23 const auto& v = vtable_from_parts(parts, "concat_batched_rows");
440
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
23 if (out.data != nullptr && parts[0]->device != out.device) {
441
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: concat_batched_rows: out on different device");
442 }
443 23 detail::adopt_output(out, parts[0]->device);
444
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (!v.concat_batched_rows) detail::throw_not_implemented("concat_batched_rows", parts[0]->device);
445 23 v.concat_batched_rows(parts, out);
446 23 }
447
448 27 void concat_nchw_channels(const std::vector<const Tensor*>& parts,
449 int N, int H, int W,
450 const std::vector<int>& C_per_part,
451 Tensor& out) {
452 27 const auto& v = vtable_from_parts(parts, "concat_nchw_channels");
453
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 25 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
27 if (out.data != nullptr && parts[0]->device != out.device) {
454
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: concat_nchw_channels: out on different device");
455 }
456 27 detail::adopt_output(out, parts[0]->device);
457
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (!v.concat_nchw_channels) detail::throw_not_implemented("concat_nchw_channels", parts[0]->device);
458 27 v.concat_nchw_channels(parts, N, H, W, C_per_part, out);
459 27 }
460
461 24 void concat_nchw_channels_backward(const Tensor& dY, int N, int H, int W,
462 const std::vector<int>& C_per_part,
463 const std::vector<Tensor*>& parts) {
464 24 Device d = dY.device;
465
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 for (std::size_t i = 0; i < parts.size(); ++i) {
466
4/6
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 44 times.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
48 if (parts[i] && parts[i]->data != nullptr && parts[i]->device != d) {
467
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: concat_nchw_channels_backward: parts on different device");
468 }
469 48 }
470 24 const auto& v = detail::ops_for(d);
471
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (!v.concat_nchw_channels_backward) detail::throw_not_implemented("concat_nchw_channels_backward", d);
472
3/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 48 times.
72 for (Tensor* _p : parts) if (_p) detail::adopt_output(*_p, d);
473 24 v.concat_nchw_channels_backward(dY, N, H, W, C_per_part, parts);
474 24 }
475
476 72 void copy_d2d(const Tensor& src, int src_off, Tensor& dst, int dst_off, int n) {
477 72 const auto& v = detail::dispatch(src, dst);
478
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (!v.copy_d2d) detail::throw_not_implemented("copy_d2d", src.device);
479 72 detail::adopt_output(dst, src.device);
480 72 v.copy_d2d(src, src_off, dst, dst_off, n);
481 72 }
482
483 10 void copy_d2d_strided(const Tensor& src, int src_off, int src_pitch,
484 Tensor& dst, int dst_off, int dst_pitch,
485 int width, int height) {
486
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (width > src_pitch || width > dst_pitch) {
487
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("brotensor: copy_d2d_strided: width exceeds pitch");
488 }
489 10 const auto& v = detail::dispatch(src, dst);
490
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.copy_d2d_strided) detail::throw_not_implemented("copy_d2d_strided", src.device);
491 10 detail::adopt_output(dst, src.device);
492 10 v.copy_d2d_strided(src, src_off, src_pitch, dst, dst_off, dst_pitch, width, height);
493 10 }
494
495 242 void cast(const Tensor& src, Tensor& dst, Dtype out_dtype) {
496
4/4
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 195 times.
✓ Branch 2 taken 46 times.
✓ Branch 3 taken 1 time.
242 if (detail::jit_cast_out_hook && detail::jit_cast_out_hook(src, dst, out_dtype)) {
497 1 return;
498 }
499 241 const auto& v = detail::dispatch(src, dst);
500
1/2
✓ Branch 0 taken 241 times.
✗ Branch 1 not taken.
241 if (!v.cast) detail::throw_not_implemented("cast", src.device);
501 241 detail::adopt_output(dst, src.device);
502 241 v.cast(src, dst, out_dtype);
503 242 }
504
505 // ─── Inference batched + optim ─────────────────────────────────────────────
506
507 1121 void layernorm_forward_inference_batched(const Tensor& X_RD,
508 const Tensor& gamma,
509 const Tensor& beta,
510 Tensor& Y_RD, float eps) {
511 1121 const auto& v = detail::dispatch(X_RD, gamma, beta, Y_RD);
512
1/2
✓ Branch 0 taken 1121 times.
✗ Branch 1 not taken.
1121 if (!v.layernorm_forward_inference_batched)
513 ✗ detail::throw_not_implemented("layernorm_forward_inference_batched", X_RD.device);
514 1121 detail::adopt_output(Y_RD, X_RD.device);
515 1121 v.layernorm_forward_inference_batched(X_RD, gamma, &beta, Y_RD, eps);
516 1121 }
517
518 9 void layernorm_forward_inference_batched(const Tensor& X_RD,
519 const Tensor& gamma,
520 Tensor& Y_RD, float eps) {
521 9 const auto& v = detail::dispatch(X_RD, gamma, Y_RD);
522
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.layernorm_forward_inference_batched)
523 ✗ detail::throw_not_implemented("layernorm_forward_inference_batched", X_RD.device);
524 9 detail::adopt_output(Y_RD, X_RD.device);
525 9 v.layernorm_forward_inference_batched(X_RD, gamma, nullptr, Y_RD, eps);
526 9 }
527
528 19 void layernorm_forward_batched_with_caches(const Tensor& X_RD,
529 const Tensor& gamma,
530 const Tensor& beta,
531 Tensor& Y_RD, Tensor& Xhat_RD,
532 Tensor& Mean_R, Tensor& Rstd_R,
533 float eps) {
534 19 const auto& v = detail::dispatch(X_RD, gamma, beta, Y_RD, Xhat_RD, Mean_R, Rstd_R);
535
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (!v.layernorm_forward_batched_with_caches)
536 ✗ detail::throw_not_implemented("layernorm_forward_batched_with_caches", X_RD.device);
537 19 detail::adopt_output(Y_RD, X_RD.device);
538 19 detail::adopt_output(Xhat_RD, X_RD.device);
539 19 detail::adopt_output(Mean_R, X_RD.device);
540 19 detail::adopt_output(Rstd_R, X_RD.device);
541 38 v.layernorm_forward_batched_with_caches(X_RD, gamma, beta, Y_RD, Xhat_RD,
542 19 Mean_R, Rstd_R, eps);
543 19 }
544
545 19 void layernorm_backward_batched_with_caches(const Tensor& dY_RD,
546 const Tensor& Xhat_RD,
547 const Tensor& gamma,
548 const Tensor& Rstd_R,
549 Tensor& dX_RD,
550 Tensor& dGamma, Tensor& dBeta) {
551 19 const auto& v = detail::dispatch(dY_RD, Xhat_RD, gamma, Rstd_R, dX_RD, dGamma, dBeta);
552
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if (!v.layernorm_backward_batched_with_caches)
553 ✗ detail::throw_not_implemented("layernorm_backward_batched_with_caches", dY_RD.device);
554 19 detail::adopt_output(dX_RD, dY_RD.device);
555 19 detail::adopt_output(dGamma, dY_RD.device);
556 19 detail::adopt_output(dBeta, dY_RD.device);
557 38 v.layernorm_backward_batched_with_caches(dY_RD, Xhat_RD, gamma, Rstd_R,
558 19 dX_RD, dGamma, dBeta);
559 19 }
560
561 5 void sgd_step(Tensor& param, Tensor& grad, Tensor& velocity,
562 float lr, float momentum) {
563 5 const auto& v = detail::dispatch(param, grad, velocity);
564
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!v.sgd_step) detail::throw_not_implemented("sgd_step", param.device);
565 5 detail::adopt_output(param, param.device);
566 5 detail::adopt_output(grad, param.device);
567 5 detail::adopt_output(velocity, param.device);
568 5 v.sgd_step(param, grad, velocity, lr, momentum);
569 5 }
570
571 50 void adam_step(Tensor& param, const Tensor& grad,
572 Tensor& m, Tensor& v_buf,
573 float lr, float beta1, float beta2, float eps, int step) {
574 50 const auto& v = detail::dispatch(param, grad, m, v_buf);
575
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 if (!v.adam_step) detail::throw_not_implemented("adam_step", param.device);
576 50 detail::adopt_output(param, param.device);
577 50 detail::adopt_output(m, param.device);
578 50 detail::adopt_output(v_buf, param.device);
579 50 v.adam_step(param, grad, m, v_buf, lr, beta1, beta2, eps, step);
580 50 }
581
582 3 void xavier_init(Tensor& W, uint64_t& rng_state) {
583 3 const auto& v = detail::dispatch(W);
584
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (!v.xavier_init) detail::throw_not_implemented("xavier_init", W.device);
585 3 detail::adopt_output(W, W.device);
586 3 v.xavier_init(W, rng_state);
587 3 }
588
589 // ─── Batched inference variants ────────────────────────────────────────────
590
591 119 void linear_forward_batched(const Tensor& W, const Tensor& bias,
592 const Tensor& X_BD, Tensor& Y_BD) {
593 119 const auto& v = detail::dispatch(W, bias, X_BD, Y_BD);
594
1/2
✓ Branch 0 taken 119 times.
✗ Branch 1 not taken.
119 if (!v.linear_forward_batched) detail::throw_not_implemented("linear_forward_batched", W.device);
595 119 detail::adopt_output(Y_BD, W.device);
596 119 v.linear_forward_batched(W, bias, X_BD, Y_BD);
597 119 }
598
599 5 void relu_forward_batched(const Tensor& X_BD, Tensor& Y_BD) {
600 5 const auto& v = detail::dispatch(X_BD, Y_BD);
601
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!v.relu_forward_batched) detail::throw_not_implemented("relu_forward_batched", X_BD.device);
602 5 detail::adopt_output(Y_BD, X_BD.device);
603 5 v.relu_forward_batched(X_BD, Y_BD);
604 5 }
605
606 11 void tanh_forward_batched(const Tensor& X_BD, Tensor& Y_BD) {
607 11 const auto& v = detail::dispatch(X_BD, Y_BD);
608
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!v.tanh_forward_batched) detail::throw_not_implemented("tanh_forward_batched", X_BD.device);
609 11 detail::adopt_output(Y_BD, X_BD.device);
610 11 v.tanh_forward_batched(X_BD, Y_BD);
611 11 }
612
613 5 void add_inplace_batched(Tensor& Y_BD, const Tensor& X_BD) {
614 5 const auto& v = detail::dispatch(Y_BD, X_BD);
615
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (!v.add_inplace_batched) detail::throw_not_implemented("add_inplace_batched", Y_BD.device);
616 5 detail::adopt_output(Y_BD, Y_BD.device);
617 5 v.add_inplace_batched(Y_BD, X_BD);
618 5 }
619
620 // ─── Batched backward variants ─────────────────────────────────────────────
621
622 59 void linear_backward_batched(const Tensor& W, const Tensor& X_BD,
623 const Tensor& dY_BD,
624 Tensor& dX_BD, Tensor& dW, Tensor& dB) {
625 59 const auto& v = detail::dispatch(W, X_BD, dY_BD, dX_BD, dW, dB);
626
1/2
✓ Branch 0 taken 59 times.
✗ Branch 1 not taken.
59 if (!v.linear_backward_batched) detail::throw_not_implemented("linear_backward_batched", W.device);
627 59 detail::adopt_output(dX_BD, W.device);
628 59 detail::adopt_output(dW, W.device);
629 59 detail::adopt_output(dB, W.device);
630 59 v.linear_backward_batched(W, X_BD, dY_BD, dX_BD, dW, dB);
631 59 }
632
633 8 void relu_backward_batched(const Tensor& X_BD, const Tensor& dY_BD, Tensor& dX_BD) {
634 8 const auto& v = detail::dispatch(X_BD, dY_BD, dX_BD);
635
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!v.relu_backward_batched) detail::throw_not_implemented("relu_backward_batched", X_BD.device);
636 8 detail::adopt_output(dX_BD, X_BD.device);
637 8 v.relu_backward_batched(X_BD, dY_BD, dX_BD);
638 8 }
639
640 9 void tanh_backward_batched(const Tensor& Y_BD, const Tensor& dY_BD, Tensor& dX_BD) {
641 9 const auto& v = detail::dispatch(Y_BD, dY_BD, dX_BD);
642
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.tanh_backward_batched) detail::throw_not_implemented("tanh_backward_batched", Y_BD.device);
643 9 detail::adopt_output(dX_BD, Y_BD.device);
644 9 v.tanh_backward_batched(Y_BD, dY_BD, dX_BD);
645 9 }
646
647 // ─── Batched per-sample losses ─────────────────────────────────────────────
648
649 6 void mse_vec_per_sample(const Tensor& pred, const Tensor& target,
650 Tensor& dPred, Tensor& loss_per_sample) {
651 6 const auto& v = detail::dispatch(pred, target, dPred, loss_per_sample);
652
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (!v.mse_vec_per_sample) detail::throw_not_implemented("mse_vec_per_sample", pred.device);
653 6 detail::adopt_output(dPred, pred.device);
654 6 detail::adopt_output(loss_per_sample, pred.device);
655 6 v.mse_vec_per_sample(pred, target, dPred, loss_per_sample);
656 6 }
657
658 12 void softmax_xent_fused_batched(const Tensor& logits_BL, const Tensor& target_BL,
659 const float* d_mask_BL,
660 const int* d_head_offsets, int n_heads,
661 Tensor& probs_BL, Tensor& dLogits_BL,
662 Tensor& loss_per_sample) {
663 12 const auto& v = detail::dispatch(logits_BL, target_BL, probs_BL, dLogits_BL, loss_per_sample);
664
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.softmax_xent_fused_batched)
665 ✗ detail::throw_not_implemented("softmax_xent_fused_batched", logits_BL.device);
666 12 detail::adopt_output(probs_BL, logits_BL.device);
667 12 detail::adopt_output(dLogits_BL, logits_BL.device);
668 12 detail::adopt_output(loss_per_sample, logits_BL.device);
669 24 v.softmax_xent_fused_batched(logits_BL, target_BL, d_mask_BL,
670 12 d_head_offsets, n_heads,
671 12 probs_BL, dLogits_BL, loss_per_sample);
672 12 }
673
674 17 void bce_with_logits_fused_batched(const Tensor& logits_BL, const Tensor& target_BL,
675 const float* d_mask_BL,
676 float pos_weight,
677 Tensor& probs_BL, Tensor& dLogits_BL,
678 Tensor& loss_per_sample) {
679 17 const auto& v = detail::dispatch(logits_BL, target_BL, probs_BL, dLogits_BL, loss_per_sample);
680
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!v.bce_with_logits_fused_batched)
681 ✗ detail::throw_not_implemented("bce_with_logits_fused_batched", logits_BL.device);
682 17 detail::adopt_output(probs_BL, logits_BL.device);
683 17 detail::adopt_output(dLogits_BL, logits_BL.device);
684 17 detail::adopt_output(loss_per_sample, logits_BL.device);
685 34 v.bce_with_logits_fused_batched(logits_BL, target_BL, d_mask_BL,
686 17 pos_weight,
687 17 probs_BL, dLogits_BL, loss_per_sample);
688 17 }
689
690 } // namespace brotensor
691