GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 91.0% 181 / 0 / 199
Functions: 85.7% 6 / 0 / 7
Branches: 47.9% 93 / 0 / 194

src/cpu/pool2d.cpp
Line Branch Exec Source
1 // ─── CPU 2D pooling: adaptive_avg_pool2d + max_pool2d ──────────────────────
2 //
3 // FP32 scalar host implementations for the two pooling primitives needed by
4 // modern vision encoders / detectors:
5 //
6 // * adaptive_avg_pool2d — output (H_out, W_out) is the runtime parameter;
7 // each output pixel averages a variable-size input region defined by
8 // PyTorch's adaptive formula (floor / ceil at the boundaries). Used by
9 // SegFormer / Mask2Former decoder aggregation and detection heads.
10 //
11 // * max_pool2d — standard kernel/stride/pad max pool. Padding pixels are
12 // treated as -inf so they never win. Forward returns Y and a per-output
13 // INT32 flat-spatial Idx into the per-channel HxW plane; backward uses
14 // Idx to scatter dY without rescanning the kernel.
15 //
16 // Memory layout (NCHW flat):
17 // X / dX : ((n*C + c)*H + h)*W + w
18 // Y / dY : ((n*C + c)*H_out + h)*W_out + w
19 // Idx same layout as Y, INT32.
20 //
21 // ── ACCUMULATION ────────────────────────────────────────────────────────────
22 // adaptive_avg_pool2d_forward — Y OVERWRITTEN.
23 // adaptive_avg_pool2d_backward — dX OVERWRITTEN (zero-then-scatter; many
24 // output regions overlap the same input
25 // pixel and their contributions sum).
26 // max_pool2d_forward — Y OVERWRITTEN, Idx OVERWRITTEN.
27 // max_pool2d_backward — dX OVERWRITTEN (zero-then-scatter; with
28 // stride < kernel size overlapping kernels
29 // may pick the same input pixel from
30 // multiple outputs — those dY values sum).
31
32 #include <brotensor/tensor.h>
33
34 #include <cmath>
35 #include <limits>
36 #include <stdexcept>
37 #include <string>
38
39 namespace brotensor::detail::cpu {
40
41 namespace {
42
43 [[noreturn]] inline void fail(const char* op, const std::string& reason) {
44 throw std::runtime_error(std::string("brotensor: ") + op + ": " + reason);
45 }
46
47 60 inline void check_fp32(const ::brotensor::Tensor& t,
48 const char* op, const char* name) {
49
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (t.dtype != Dtype::FP32) {
50 fail(op, std::string(name) +
51 " must be FP32 (CPU backend is FP32-only)");
52 }
53 60 }
54
55 // PyTorch adaptive-pool window endpoints for axis len L -> L_out.
56 1228 inline void adaptive_window(int o, int L, int L_out, int& start, int& end) {
57 // start = floor(o * L / L_out), end = ceil((o+1) * L / L_out)
58 1228 start = (o * L) / L_out;
59 1228 end = ((o + 1) * L + L_out - 1) / L_out;
60
1/2
✓ Branch 0 taken 1228 times.
✗ Branch 1 not taken.
1228 if (end > L) end = L;
61
1/2
✓ Branch 0 taken 1228 times.
✗ Branch 1 not taken.
1228 if (start < 0) start = 0;
62 1228 }
63
64 } // namespace
65
66 // ═══════════════════════════════════════════════════════════════════════════
67 // adaptive_avg_pool2d
68 // ═══════════════════════════════════════════════════════════════════════════
69
70 38 void adaptive_avg_pool2d_forward(const ::brotensor::Tensor& X,
71 int N, int C, int H, int W,
72 int H_out, int W_out,
73 ::brotensor::Tensor& Y) {
74 38 const char* op = "adaptive_avg_pool2d_forward";
75 38 check_fp32(X, op, "X");
76
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (N < 0 || C < 1 || H < 1 || W < 1)
77 fail(op, "C/H/W must be >=1 and N >=0");
78
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (H_out < 1 || W_out < 1)
79 fail(op, "H_out and W_out must be >= 1");
80
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (X.rows != N || X.cols != C * H * W)
81 fail(op, "X shape must be (N, C*H*W)");
82
83 38 const int cols_out = C * H_out * W_out;
84
4/6
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
38 if (Y.rows != N || Y.cols != cols_out || Y.dtype != Dtype::FP32) {
85 9 Y.resize(N, cols_out, Dtype::FP32);
86 9 }
87
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (N == 0) return;
88
89 38 const float* Xp = X.host_f32();
90 38 float* Yp = Y.host_f32_mut();
91
92
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 45 times.
83 for (int n = 0; n < N; ++n) {
93
2/2
✓ Branch 0 taken 78 times.
✓ Branch 1 taken 45 times.
123 for (int c = 0; c < C; ++c) {
94 78 const float* x_chan =
95 78 Xp + (static_cast<long>(n) * C + c) * H * W;
96 78 float* y_chan =
97 78 Yp + (static_cast<long>(n) * C + c) * H_out * W_out;
98
2/2
✓ Branch 0 taken 210 times.
✓ Branch 1 taken 78 times.
288 for (int oh = 0; oh < H_out; ++oh) {
99 int h0, h1;
100 210 adaptive_window(oh, H, H_out, h0, h1);
101
2/2
✓ Branch 0 taken 890 times.
✓ Branch 1 taken 210 times.
1100 for (int ow = 0; ow < W_out; ++ow) {
102 int w0, w1;
103 890 adaptive_window(ow, W, W_out, w0, w1);
104 890 const int area = (h1 - h0) * (w1 - w0);
105 890 double acc = 0.0;
106
2/2
✓ Branch 0 taken 1796 times.
✓ Branch 1 taken 890 times.
2686 for (int h = h0; h < h1; ++h) {
107 1796 const float* row = x_chan + static_cast<long>(h) * W;
108
2/2
✓ Branch 0 taken 5028 times.
✓ Branch 1 taken 1796 times.
6824 for (int w = w0; w < w1; ++w) acc += row[w];
109 1796 }
110 890 y_chan[static_cast<long>(oh) * W_out + ow] =
111 890 static_cast<float>(acc / area);
112 890 }
113 210 }
114 78 }
115 45 }
116 38 }
117
118 4 void adaptive_avg_pool2d_backward(const ::brotensor::Tensor& dY,
119 int N, int C, int H, int W,
120 int H_out, int W_out,
121 ::brotensor::Tensor& dX) {
122 4 const char* op = "adaptive_avg_pool2d_backward";
123 4 check_fp32(dY, op, "dY");
124
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (N < 0 || C < 1 || H < 1 || W < 1)
125 fail(op, "C/H/W must be >=1 and N >=0");
126
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (H_out < 1 || W_out < 1)
127 fail(op, "H_out and W_out must be >= 1");
128
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (dY.rows != N || dY.cols != C * H_out * W_out)
129 fail(op, "dY shape must be (N, C*H_out*W_out)");
130
131 4 const int cols_in = C * H * W;
132
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 != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) {
133 4 dX.resize(N, cols_in, Dtype::FP32);
134 4 }
135
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (N == 0) return;
136
137 4 const float* dYp = dY.host_f32();
138 4 float* dXp = dX.host_f32_mut();
139
140 4 const long total_in = static_cast<long>(N) * cols_in;
141
2/2
✓ Branch 0 taken 1043 times.
✓ Branch 1 taken 4 times.
1047 for (long i = 0; i < total_in; ++i) dXp[i] = 0.0f;
142
143
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 for (int n = 0; n < N; ++n) {
144
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 6 times.
22 for (int c = 0; c < C; ++c) {
145 16 const float* dy_chan =
146 16 dYp + (static_cast<long>(n) * C + c) * H_out * W_out;
147 16 float* dx_chan =
148 16 dXp + (static_cast<long>(n) * C + c) * H * W;
149
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 16 times.
52 for (int oh = 0; oh < H_out; ++oh) {
150 int h0, h1;
151 36 adaptive_window(oh, H, H_out, h0, h1);
152
2/2
✓ Branch 0 taken 92 times.
✓ Branch 1 taken 36 times.
128 for (int ow = 0; ow < W_out; ++ow) {
153 int w0, w1;
154 92 adaptive_window(ow, W, W_out, w0, w1);
155 92 const int area = (h1 - h0) * (w1 - w0);
156 92 const float g =
157 184 dy_chan[static_cast<long>(oh) * W_out + ow] /
158 92 static_cast<float>(area);
159
2/2
✓ Branch 0 taken 302 times.
✓ Branch 1 taken 92 times.
394 for (int h = h0; h < h1; ++h) {
160 302 float* row = dx_chan + static_cast<long>(h) * W;
161
2/2
✓ Branch 0 taken 1330 times.
✓ Branch 1 taken 302 times.
1632 for (int w = w0; w < w1; ++w) row[w] += g;
162 302 }
163 92 }
164 36 }
165 16 }
166 6 }
167 4 }
168
169 // ═══════════════════════════════════════════════════════════════════════════
170 // max_pool2d
171 // ═══════════════════════════════════════════════════════════════════════════
172
173 13 void max_pool2d_forward(const ::brotensor::Tensor& X,
174 int N, int C, int H, int W,
175 int kH, int kW, int stride_h, int stride_w,
176 int pad_h, int pad_w,
177 ::brotensor::Tensor& Y, ::brotensor::Tensor& Idx) {
178 13 const char* op = "max_pool2d_forward";
179 13 check_fp32(X, op, "X");
180
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (N < 0 || C < 1 || H < 1 || W < 1)
181 fail(op, "C/H/W must be >=1 and N >=0");
182
1/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
13 if (kH < 1 || kW < 1) fail(op, "kH and kW must be >= 1");
183
1/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
13 if (stride_h < 1 || stride_w < 1) fail(op, "strides must be >= 1");
184
1/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
13 if (pad_h < 0 || pad_w < 0) fail(op, "pads must be >= 0");
185
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (kH > H + 2 * pad_h || kW > W + 2 * pad_w)
186 fail(op, "kernel larger than padded input");
187
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (X.rows != N || X.cols != C * H * W)
188 fail(op, "X shape must be (N, C*H*W)");
189
190 13 const int H_out = (H + 2 * pad_h - kH) / stride_h + 1;
191 13 const int W_out = (W + 2 * pad_w - kW) / stride_w + 1;
192 13 const int cols_out = C * H_out * W_out;
193
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13 if (Y.rows != N || Y.cols != cols_out || Y.dtype != Dtype::FP32) {
194 13 Y.resize(N, cols_out, Dtype::FP32);
195 13 }
196
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
13 if (Idx.rows != N || Idx.cols != cols_out || Idx.dtype != Dtype::INT32) {
197 13 Idx.resize(N, cols_out, Dtype::INT32);
198 13 }
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
13 if (N == 0) return;
200
201 13 const float NEG_INF = -std::numeric_limits<float>::infinity();
202 13 const float* Xp = X.host_f32();
203 13 float* Yp = Y.host_f32_mut();
204 13 int32_t* Ip = static_cast<int32_t*>(Idx.data);
205
206
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 13 times.
32 for (int n = 0; n < N; ++n) {
207
2/2
✓ Branch 0 taken 46 times.
✓ Branch 1 taken 19 times.
65 for (int c = 0; c < C; ++c) {
208 46 const float* x_chan =
209 46 Xp + (static_cast<long>(n) * C + c) * H * W;
210 46 float* y_chan =
211 46 Yp + (static_cast<long>(n) * C + c) * H_out * W_out;
212 46 int32_t* i_chan =
213 46 Ip + (static_cast<long>(n) * C + c) * H_out * W_out;
214
2/2
✓ Branch 0 taken 201 times.
✓ Branch 1 taken 46 times.
247 for (int oh = 0; oh < H_out; ++oh) {
215 201 const int h_base = oh * stride_h - pad_h;
216
2/2
✓ Branch 0 taken 1009 times.
✓ Branch 1 taken 201 times.
1210 for (int ow = 0; ow < W_out; ++ow) {
217 1009 const int w_base = ow * stride_w - pad_w;
218 1009 float best_v = NEG_INF;
219 1009 int32_t best_i = -1;
220
2/2
✓ Branch 0 taken 2622 times.
✓ Branch 1 taken 1009 times.
3631 for (int kh = 0; kh < kH; ++kh) {
221 2622 const int ih = h_base + kh;
222
4/4
✓ Branch 0 taken 2519 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 2416 times.
✓ Branch 3 taken 103 times.
2622 if (ih < 0 || ih >= H) continue;
223 2416 const float* row =
224 2416 x_chan + static_cast<long>(ih) * W;
225
2/2
✓ Branch 0 taken 6444 times.
✓ Branch 1 taken 2416 times.
8860 for (int kw = 0; kw < kW; ++kw) {
226 6444 const int iw = w_base + kw;
227
4/4
✓ Branch 0 taken 6208 times.
✓ Branch 1 taken 236 times.
✓ Branch 2 taken 236 times.
✓ Branch 3 taken 5972 times.
6444 if (iw < 0 || iw >= W) continue;
228 5972 const float v = row[iw];
229
2/2
✓ Branch 0 taken 3552 times.
✓ Branch 1 taken 2420 times.
5972 if (v > best_v) {
230 2420 best_v = v;
231 2420 best_i = ih * W + iw;
232 2420 }
233 5972 }
234 2416 }
235 1009 const long o = static_cast<long>(oh) * W_out + ow;
236 1009 y_chan[o] = best_v;
237 1009 i_chan[o] = best_i;
238 1009 }
239 201 }
240 46 }
241 19 }
242 13 }
243
244 5 void max_pool2d_backward(const ::brotensor::Tensor& dY,
245 const ::brotensor::Tensor& Idx,
246 int N, int C, int H, int W,
247 int H_out, int W_out,
248 ::brotensor::Tensor& dX) {
249 5 const char* op = "max_pool2d_backward";
250 5 check_fp32(dY, op, "dY");
251
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (Idx.dtype != Dtype::INT32)
252 fail(op, "Idx must be INT32 (as produced by max_pool2d_forward)");
253
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (N < 0 || C < 1 || H < 1 || W < 1)
254 fail(op, "C/H/W must be >=1 and N >=0");
255
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (H_out < 0 || W_out < 0)
256 fail(op, "H_out and W_out must be >= 0");
257
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (dY.rows != N || dY.cols != C * H_out * W_out)
258 fail(op, "dY shape must be (N, C*H_out*W_out)");
259
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (Idx.rows != N || Idx.cols != C * H_out * W_out)
260 fail(op, "Idx shape must be (N, C*H_out*W_out)");
261
262 5 const int cols_in = C * H * W;
263
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
5 if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) {
264 5 dX.resize(N, cols_in, Dtype::FP32);
265 5 }
266
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (N == 0) return;
267
268 5 const float* dYp = dY.host_f32();
269 5 const int32_t* Ip = static_cast<const int32_t*>(Idx.data);
270 5 float* dXp = dX.host_f32_mut();
271
272 5 const long total_in = static_cast<long>(N) * cols_in;
273
2/2
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 5 times.
738 for (long i = 0; i < total_in; ++i) dXp[i] = 0.0f;
274
275
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
5 if (H_out == 0 || W_out == 0) return;
276
277
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 7 times.
12 for (int n = 0; n < N; ++n) {
278
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 7 times.
23 for (int c = 0; c < C; ++c) {
279 16 const float* dy_chan =
280 16 dYp + (static_cast<long>(n) * C + c) * H_out * W_out;
281 16 const int32_t* i_chan =
282 16 Ip + (static_cast<long>(n) * C + c) * H_out * W_out;
283 16 float* dx_chan =
284 16 dXp + (static_cast<long>(n) * C + c) * H * W;
285
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 16 times.
88 for (int oh = 0; oh < H_out; ++oh) {
286
2/2
✓ Branch 0 taken 388 times.
✓ Branch 1 taken 72 times.
460 for (int ow = 0; ow < W_out; ++ow) {
287 388 const long o = static_cast<long>(oh) * W_out + ow;
288 388 const int32_t idx = i_chan[o];
289
1/2
✓ Branch 0 taken 388 times.
✗ Branch 1 not taken.
388 if (idx < 0) continue; // degenerate: no valid pixel.
290 388 dx_chan[idx] += dy_chan[o];
291 388 }
292 72 }
293 16 }
294 7 }
295 5 }
296
297 } // namespace brotensor::detail::cpu
298