brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
220 / 232
94.8%
Branches:
136 / 200
68.0%
Functions:
10 / 10
100.0%

src/cpu/conv2d.cpp
Line Branch Exec Source
1 // ─── CPU conv2d ops (CHUNK 3) ──────────────────────────────────────────────
2 //
3 // FP32 scalar host implementations. Ports src/cuda/conv2d.cu — the plain
4 // direct-conv kernels (NOT the WMMA implicit-GEMM fast path), FP32 path only.
5 //
6 // Memory layout (matches the GPU exactly):
7 // X : NCHW — ((n*C_in + c_in) * H + h) * W + w
8 // Y : NCHW — ((n*C_out + c_out) * H_out + h_out) * W_out + w_out
9 // Wt : OIHW (grouped) — ((c_out*Cg_in + c_in_local) * kH + kh) * kW + kw
10 // where Cg_in = C_in/groups is the per-group input-channel count.
11 // bias: (C_out, 1), optional (may be null)
12 //
13 // Groups convention: output channel c_out belongs to group g = c_out/Cg_out;
14 // that group's absolute input channels start at g*Cg_in (Cg_in channels wide).
15 //
16 // Output-size formula (identical to the GPU):
17 // H_out = (H + 2*pad_h - dil_h*(kH-1) - 1) / stride_h + 1
18 // W_out = (W + 2*pad_w - dil_w*(kW-1) - 1) / stride_w + 1
19 //
20 // ACCUMULATION (matches the GPU kernels):
21 // conv2d_forward — Y OVERWRITTEN (kernel stores acc directly).
22 // conv2d_backward_input — dX OVERWRITTEN.
23 // conv2d_backward_weight — dWt ACCUMULATES (+=); GPU folds an FP32 scratch
24 // into the caller's dWt. Caller zeros dWt first.
25 // conv2d_backward_bias — dB ACCUMULATES (+=); GPU folds an FP32 scratch
26 // into the caller's dB. Caller zeros dB first.
27
28 #include <brotensor/tensor.h>
29 #include <brotensor/detail/cpu/thread_pool.h>
30
31 #include <cstddef>
32 #include <stdexcept>
33 #include <string>
34
35 namespace brotensor::detail::cpu {
36
37 namespace {
38
39 317 inline void check_groups(const char* op, int C_in, int C_out, int groups) {
40
1/2
✓ Branch 0 taken 317 times.
✗ Branch 1 not taken.
317 if (groups < 1 || C_in % groups != 0 || C_out % groups != 0) {
41
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ throw std::runtime_error(
42
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ std::string(op) +
43 ": groups must be >=1 and divide both C_in and C_out");
44 }
45 317 }
46
47 634 inline int out_dim(int in, int pad, int dil, int k, int stride) {
48 634 return (in + 2 * pad - dil * (k - 1) - 1) / stride + 1;
49 }
50
51 } // namespace
52
53 118 void conv2d_forward(const ::brotensor::Tensor& X,
54 const ::brotensor::Tensor& Wt,
55 const ::brotensor::Tensor* bias,
56 int N, int C_in, int H, int W,
57 int C_out, int kH, int kW,
58 int stride_h, int stride_w,
59 int pad_h, int pad_w,
60 int dil_h, int dil_w,
61 int groups,
62 ::brotensor::Tensor& Y) {
63
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (Wt.dtype != X.dtype) {
64
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_forward: Wt dtype must match X");
65 }
66
3/4
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
118 if (bias && bias->dtype != X.dtype) {
67
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_forward: bias dtype must match X");
68 }
69 118 check_groups("conv2d_forward", C_in, C_out, groups);
70 118 const int Cg_in = C_in / groups;
71 118 const int Cg_out = C_out / groups;
72 118 const int H_out = out_dim(H, pad_h, dil_h, kH, stride_h);
73 118 const int W_out = out_dim(W, pad_w, dil_w, kW, stride_w);
74
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (H_out <= 0 || W_out <= 0) {
75
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_forward: non-positive output shape");
76 }
77 118 const int out_cols = C_out * H_out * W_out;
78
4/6
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 69 times.
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 49 times.
118 if (Y.rows != N || Y.cols != out_cols || Y.dtype != X.dtype) {
79 69 Y.resize(N, out_cols, X.dtype);
80 69 }
81
2/4
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 118 times.
118 if (N == 0 || out_cols == 0) return;
82
83 118 const float* Xp = X.host_f32();
84 118 const float* Wp = Wt.host_f32();
85
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 60 times.
118 const float* Bp = bias ? bias->host_f32() : nullptr;
86 118 float* Yp = Y.host_f32_mut();
87
88 // Interior region: the output rows/cols for which every kernel tap is
89 // guaranteed in-bounds (the whole kH*kW*dil window sits inside the padded
90 // input), computed once — independent of n/oc. In-bounds is monotonic in
91 // (oh, ow), so the intersection across all kh (resp. kw) is itself a
92 // single contiguous range. Only the thin border ring outside that range
93 // needs the per-tap bounds check; the interior runs a branch-free loop.
94 118 int oh_lo = (pad_h + stride_h - 1) / stride_h; // ceil(pad_h/stride_h)
95 118 int oh_hi = H - 1 + pad_h - (kH - 1) * dil_h;
96
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 oh_hi = (oh_hi >= 0) ? (oh_hi / stride_h) : -1; // floor(.../stride_h)
97
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (oh_lo < 0) oh_lo = 0;
98
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (oh_hi >= H_out) oh_hi = H_out - 1;
99
100 118 int ow_lo = (pad_w + stride_w - 1) / stride_w;
101 118 int ow_hi = W - 1 + pad_w - (kW - 1) * dil_w;
102
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 ow_hi = (ow_hi >= 0) ? (ow_hi / stride_w) : -1;
103
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (ow_lo < 0) ow_lo = 0;
104
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
118 if (ow_hi >= W_out) ow_hi = W_out - 1;
105
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 118 times.
118 const bool has_interior = (oh_lo <= oh_hi) && (ow_lo <= ow_hi);
107
108 // Each n exclusively owns Y's batch slice n (X/Wt/bias are read-only), so
109 // this parallelizes across n with no cross-thread writes.
110
1/2
✓ Branch 0 taken 118 times.
✗ Branch 1 not taken.
284 parallel_for(static_cast<std::size_t>(N), [&](std::size_t ni) {
111 166 const int n = static_cast<int>(ni);
112
2/2
✓ Branch 0 taken 6240 times.
✓ Branch 1 taken 166 times.
6406 for (int oc = 0; oc < C_out; ++oc) {
113 6240 const int g = oc / Cg_out;
114 6240 const int ic_base = g * Cg_in;
115 6240 const int w_oc_base = oc * Cg_in * kH * kW;
116
2/2
✓ Branch 0 taken 4664 times.
✓ Branch 1 taken 1576 times.
6240 const float bias_v = Bp ? Bp[oc] : 0.0f;
117
118 // Border pixel: same bounds-checked accumulation as before.
119 95563 auto compute_bordered = [&](int oh, int ow) -> float {
120 89323 const int in_h_origin = oh * stride_h - pad_h;
121 89323 const int in_w_origin = ow * stride_w - pad_w;
122 89323 float acc = 0.0f;
123
2/2
✓ Branch 0 taken 3609556 times.
✓ Branch 1 taken 89323 times.
3698879 for (int ic_local = 0; ic_local < Cg_in; ++ic_local) {
124 3609556 const int ic = ic_base + ic_local;
125 3609556 const int w_ic_base = w_oc_base + ic_local * kH * kW;
126 3609556 const int x_ic_base = (n * C_in + ic) * H * W;
127
2/2
✓ Branch 0 taken 12064419 times.
✓ Branch 1 taken 3609556 times.
15673975 for (int kh = 0; kh < kH; ++kh) {
128 12064419 const int in_h = in_h_origin + kh * dil_h;
129
4/4
✓ Branch 0 taken 10777536 times.
✓ Branch 1 taken 1286883 times.
✓ Branch 2 taken 9672411 times.
✓ Branch 3 taken 1105125 times.
12064419 if (in_h < 0 || in_h >= H) continue;
130
2/2
✓ Branch 0 taken 41565463 times.
✓ Branch 1 taken 9672411 times.
51237874 for (int kw = 0; kw < kW; ++kw) {
131 41565463 const int in_w = in_w_origin + kw * dil_w;
132
4/4
✓ Branch 0 taken 37530620 times.
✓ Branch 1 taken 4034843 times.
✓ Branch 2 taken 3841114 times.
✓ Branch 3 taken 33689506 times.
41565463 if (in_w < 0 || in_w >= W) continue;
133 67379012 acc += Xp[x_ic_base + in_h * W + in_w] *
134 33689506 Wp[w_ic_base + kh * kW + kw];
135 33689506 }
136 9672411 }
137 3609556 }
138 89323 return acc;
139 };
140
141 // Interior pixel: every tap is guaranteed in-bounds — no checks.
142 3052112 auto compute_interior = [&](int oh, int ow) -> float {
143 3045872 const int in_h_origin = oh * stride_h - pad_h;
144 3045872 const int in_w_origin = ow * stride_w - pad_w;
145 3045872 float acc = 0.0f;
146
2/2
✓ Branch 0 taken 157698128 times.
✓ Branch 1 taken 3045872 times.
160744000 for (int ic_local = 0; ic_local < Cg_in; ++ic_local) {
147 157698128 const int ic = ic_base + ic_local;
148 157698128 const int w_ic_base = w_oc_base + ic_local * kH * kW;
149 157698128 const int x_ic_base = (n * C_in + ic) * H * W;
150
2/2
✓ Branch 0 taken 187343447 times.
✓ Branch 1 taken 157698128 times.
345041575 for (int kh = 0; kh < kH; ++kh) {
151 187343447 const int in_h = in_h_origin + kh * dil_h;
152 187343447 const int x_row_base = x_ic_base + in_h * W;
153 187343447 const int w_row_base = w_ic_base + kh * kW;
154
2/2
✓ Branch 0 taken 622864624 times.
✓ Branch 1 taken 187343447 times.
810208071 for (int kw = 0; kw < kW; ++kw) {
155 622864624 const int in_w = in_w_origin + kw * dil_w;
156 622864624 acc += Xp[x_row_base + in_w] * Wp[w_row_base + kw];
157 622864624 }
158 187343447 }
159 157698128 }
160 3045872 return acc;
161 };
162
163
2/2
✓ Branch 0 taken 84447 times.
✓ Branch 1 taken 6240 times.
90687 for (int oh = 0; oh < H_out; ++oh) {
164 84447 const int y_row_base = ((n * C_out + oc) * H_out + oh) * W_out;
165
3/4
✓ Branch 0 taken 84447 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2039 times.
✓ Branch 3 taken 82408 times.
84447 const bool oh_interior = has_interior && oh >= oh_lo && oh <= oh_hi;
166
2/2
✓ Branch 0 taken 3948 times.
✓ Branch 1 taken 80499 times.
84447 if (!oh_interior) {
167
2/2
✓ Branch 0 taken 58013 times.
✓ Branch 1 taken 3948 times.
61961 for (int ow = 0; ow < W_out; ++ow) {
168 58013 Yp[y_row_base + ow] = compute_bordered(oh, ow) + bias_v;
169 58013 }
170 3948 continue;
171 }
172
2/2
✓ Branch 0 taken 17647 times.
✓ Branch 1 taken 80499 times.
98146 for (int ow = 0; ow < ow_lo; ++ow) {
173 17647 Yp[y_row_base + ow] = compute_bordered(oh, ow) + bias_v;
174 17647 }
175
2/2
✓ Branch 0 taken 3049407 times.
✓ Branch 1 taken 80499 times.
3129906 for (int ow = ow_lo; ow <= ow_hi; ++ow) {
176 3049407 Yp[y_row_base + ow] = compute_interior(oh, ow) + bias_v;
177 3049407 }
178
2/2
✓ Branch 0 taken 13680 times.
✓ Branch 1 taken 80499 times.
94179 for (int ow = ow_hi + 1; ow < W_out; ++ow) {
179 13680 Yp[y_row_base + ow] = compute_bordered(oh, ow) + bias_v;
180 13680 }
181 80499 }
182 6240 }
183 166 });
184 118 }
185
186 102 void conv2d_backward_input(const ::brotensor::Tensor& Wt,
187 const ::brotensor::Tensor& dY,
188 int N, int C_in, int H, int W,
189 int C_out, int kH, int kW,
190 int stride_h, int stride_w,
191 int pad_h, int pad_w,
192 int dil_h, int dil_w,
193 int groups,
194 ::brotensor::Tensor& dX) {
195
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (dY.dtype != Wt.dtype) {
196
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_input: dY dtype must match Wt");
197 }
198 102 check_groups("conv2d_backward_input", C_in, C_out, groups);
199 102 const int Cg_in = C_in / groups;
200 102 const int Cg_out = C_out / groups;
201 102 const int H_out = out_dim(H, pad_h, dil_h, kH, stride_h);
202 102 const int W_out = out_dim(W, pad_w, dil_w, kW, stride_w);
203
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
102 if (H_out <= 0 || W_out <= 0) {
204
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_input: non-positive output shape");
205 }
206 102 const int in_cols = C_in * H * W;
207
4/6
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 53 times.
102 if (dX.rows != N || dX.cols != in_cols || dX.dtype != Wt.dtype) {
208 49 dX.resize(N, in_cols, Wt.dtype);
209 49 }
210
2/4
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
102 if (N == 0 || in_cols == 0) return;
211
212 102 const float* Wp = Wt.host_f32();
213 102 const float* dYp = dY.host_f32();
214 102 float* dXp = dX.host_f32_mut();
215
216 // Gather form: one accumulation per input pixel, inverting the forward
217 // index relation in_h = stride_h*i_out - pad_h + dil_h*kh.
218 // Each n exclusively owns dX's batch slice n (dY/Wt are read-only), so
219 // this parallelizes across n with no cross-thread writes.
220
1/2
✓ Branch 0 taken 102 times.
✗ Branch 1 not taken.
235 parallel_for(static_cast<std::size_t>(N), [&](std::size_t ni) {
221 133 const int n = static_cast<int>(ni);
222
2/2
✓ Branch 0 taken 1319 times.
✓ Branch 1 taken 27 times.
1346 for (int c_in = 0; c_in < C_in; ++c_in) {
223 1319 const int g = c_in / Cg_in;
224 1319 const int c_in_local = c_in - g * Cg_in;
225 1319 const int oc_lo = g * Cg_out;
226 1319 const int oc_hi = oc_lo + Cg_out;
227
2/2
✓ Branch 0 taken 29022 times.
✓ Branch 1 taken 1213 times.
30235 for (int i = 0; i < H; ++i) {
228
2/2
✓ Branch 0 taken 1199542 times.
✓ Branch 1 taken 28916 times.
1228458 for (int j = 0; j < W; ++j) {
229 1199542 float acc = 0.0f;
230
2/2
✓ Branch 0 taken 1235916 times.
✓ Branch 1 taken 1199436 times.
2435352 for (int kh = 0; kh < kH; ++kh) {
231 1235916 const int num_h = i + pad_h - dil_h * kh;
232
4/4
✓ Branch 0 taken 1232184 times.
✓ Branch 1 taken 3732 times.
✓ Branch 2 taken 11586 times.
✓ Branch 3 taken 1220598 times.
1235916 if (num_h < 0 || num_h % stride_h != 0) continue;
233 1220598 const int i_out = num_h / stride_h;
234
4/4
✓ Branch 0 taken 1220541 times.
✓ Branch 1 taken 57 times.
✓ Branch 2 taken 2695 times.
✓ Branch 3 taken 1217846 times.
1220598 if (i_out < 0 || i_out >= H_out) continue;
235
2/2
✓ Branch 0 taken 1296233 times.
✓ Branch 1 taken 1217740 times.
2513973 for (int kw = 0; kw < kW; ++kw) {
236 1296233 const int num_w = j + pad_w - dil_w * kw;
237
4/4
✓ Branch 0 taken 1288694 times.
✓ Branch 1 taken 7539 times.
✓ Branch 2 taken 9939 times.
✓ Branch 3 taken 1278755 times.
1296233 if (num_w < 0 || num_w % stride_w != 0) continue;
238 1278755 const int j_out = num_w / stride_w;
239
4/4
✓ Branch 0 taken 1278702 times.
✓ Branch 1 taken 53 times.
✓ Branch 2 taken 1271396 times.
✓ Branch 3 taken 7306 times.
1278755 if (j_out < 0 || j_out >= W_out) continue;
240
2/2
✓ Branch 0 taken 84440190 times.
✓ Branch 1 taken 1271396 times.
85711586 for (int c_out = oc_lo; c_out < oc_hi; ++c_out) {
241 84440190 const int dy_idx =
242 168880380 ((n * C_out + c_out) * H_out + i_out) *
243 168880380 W_out + j_out;
244 84440190 const int w_idx =
245 168880380 ((c_out * Cg_in + c_in_local) * kH + kh) *
246 168880380 kW + kw;
247 84440190 acc += dYp[dy_idx] * Wp[w_idx];
248 84440190 }
249 1271396 }
250 1217740 }
251 1199436 const int dx_idx = ((n * C_in + c_in) * H + i) * W + j;
252 1199436 dXp[dx_idx] = acc; // overwrite
253 1199436 }
254 28916 }
255 1213 }
256 27 });
257 102 }
258
259 97 void conv2d_backward_weight(const ::brotensor::Tensor& X,
260 const ::brotensor::Tensor& dY,
261 int N, int C_in, int H, int W,
262 int C_out, int kH, int kW,
263 int stride_h, int stride_w,
264 int pad_h, int pad_w,
265 int dil_h, int dil_w,
266 int groups,
267 ::brotensor::Tensor& dWt) {
268
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (dY.dtype != X.dtype || dWt.dtype != X.dtype) {
269
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error(
270 "conv2d_backward_weight: X, dY, dWt dtype must match");
271 }
272 97 check_groups("conv2d_backward_weight", C_in, C_out, groups);
273 97 const int Cg_in = C_in / groups;
274 97 const int Cg_out = C_out / groups;
275 97 const int H_out = out_dim(H, pad_h, dil_h, kH, stride_h);
276 97 const int W_out = out_dim(W, pad_w, dil_w, kW, stride_w);
277
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (H_out <= 0 || W_out <= 0) {
278
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_weight: non-positive output shape");
279 }
280
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (dWt.rows != C_out || dWt.cols != Cg_in * kH * kW) {
281
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_weight: dWt shape mismatch");
282 }
283 97 const int total = C_out * Cg_in * kH * kW;
284
1/2
✓ Branch 0 taken 97 times.
✗ Branch 1 not taken.
97 if (total == 0) return;
285
286 97 const float* Xp = X.host_f32();
287 97 const float* dYp = dY.host_f32();
288 97 float* dWp = dWt.host_f32_mut();
289
290 // One accumulation per weight element; accumulate (+=) into dWt to match
291 // the GPU's FP32-scratch-fold-into-dWt contract.
292 //
293 // NOT parallelized over n: here n is the innermost reduction axis (every
294 // batch item's contribution sums into the same dWp[w_idx]), not the outer
295 // axis — naively wrapping parallel_for around n would have every thread
296 // racing on the same dWt elements. Parallelizing over c_out instead would
297 // be race-free (each c_out row of dWt is disjoint), but that's a
298 // different axis than what this task scopes to, so left single-threaded.
299
2/2
✓ Branch 0 taken 97 times.
✓ Branch 1 taken 1540 times.
1637 for (int c_out = 0; c_out < C_out; ++c_out) {
300 1540 const int g = c_out / Cg_out;
301
2/2
✓ Branch 0 taken 46155 times.
✓ Branch 1 taken 1540 times.
47695 for (int c_in_local = 0; c_in_local < Cg_in; ++c_in_local) {
302 46155 const int c_in = g * Cg_in + c_in_local;
303
2/2
✓ Branch 0 taken 67574 times.
✓ Branch 1 taken 46155 times.
113729 for (int kh = 0; kh < kH; ++kh) {
304
2/2
✓ Branch 0 taken 132278 times.
✓ Branch 1 taken 67574 times.
199852 for (int kw = 0; kw < kW; ++kw) {
305 132278 float acc = 0.0f;
306
2/2
✓ Branch 0 taken 141248 times.
✓ Branch 1 taken 132278 times.
273526 for (int n = 0; n < N; ++n) {
307
2/2
✓ Branch 0 taken 2291497 times.
✓ Branch 1 taken 141248 times.
2432745 for (int i_out = 0; i_out < H_out; ++i_out) {
308 2291497 const int in_h =
309 2291497 i_out * stride_h - pad_h + kh * dil_h;
310
4/4
✓ Branch 0 taken 2256772 times.
✓ Branch 1 taken 34725 times.
✓ Branch 2 taken 34698 times.
✓ Branch 3 taken 2222074 times.
2291497 if (in_h < 0 || in_h >= H) continue;
311
2/2
✓ Branch 0 taken 86118064 times.
✓ Branch 1 taken 2222074 times.
88340138 for (int j_out = 0; j_out < W_out; ++j_out) {
312 86118064 const int in_w =
313 86118064 j_out * stride_w - pad_w + kw * dil_w;
314
4/4
✓ Branch 0 taken 85943781 times.
✓ Branch 1 taken 174283 times.
✓ Branch 2 taken 173467 times.
✓ Branch 3 taken 85770314 times.
86118064 if (in_w < 0 || in_w >= W) continue;
315 85770314 const int x_idx =
316 85770314 ((n * C_in + c_in) * H + in_h) * W + in_w;
317 85770314 const int dy_idx =
318 171540628 ((n * C_out + c_out) * H_out + i_out) *
319 171540628 W_out + j_out;
320 85770314 acc += dYp[dy_idx] * Xp[x_idx];
321 85770314 }
322 2222074 }
323 141248 }
324 132278 const int w_idx =
325 132278 ((c_out * Cg_in + c_in_local) * kH + kh) * kW + kw;
326 132278 dWp[w_idx] += acc; // accumulate
327 132278 }
328 67574 }
329 46155 }
330 1540 }
331 97 }
332
333 31 void conv2d_backward_bias(const ::brotensor::Tensor& dY,
334 int N, int C_out, int H_out, int W_out,
335 ::brotensor::Tensor& dB) {
336
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (dB.dtype != dY.dtype) {
337
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_bias: dB dtype must match dY");
338 }
339
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (dB.rows != C_out || dB.cols != 1) {
340
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("conv2d_backward_bias: dB shape mismatch");
341 }
342
4/8
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 31 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 31 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 31 times.
31 if (C_out == 0 || N == 0 || H_out == 0 || W_out == 0) return;
343
344 31 const float* dYp = dY.host_f32();
345 31 float* dBp = dB.host_f32_mut();
346
347 31 const int spatial = H_out * W_out;
348 // Per-channel sum over (N, H_out, W_out); accumulate (+=) into dB to match
349 // the GPU's FP32-scratch-fold-into-dB contract.
350 //
351 // NOT parallelized over n: n is the reduction axis here too (summed into
352 // the same dBp[c_out] for every batch item), so it's left single-threaded
353 // for the same reason as conv2d_backward_weight above.
354
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 357 times.
388 for (int c_out = 0; c_out < C_out; ++c_out) {
355 357 float acc = 0.0f;
356
2/2
✓ Branch 0 taken 581 times.
✓ Branch 1 taken 357 times.
938 for (int n = 0; n < N; ++n) {
357 581 const int base = (n * C_out + c_out) * spatial;
358
2/2
✓ Branch 0 taken 13653 times.
✓ Branch 1 taken 581 times.
14234 for (int sp = 0; sp < spatial; ++sp) {
359 13653 acc += dYp[base + sp];
360 13653 }
361 581 }
362 357 dBp[c_out] += acc; // accumulate
363 357 }
364 31 }
365
366 } // namespace brotensor::detail::cpu
367