brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
181 / 199
91.0%
Branches:
93 / 194
47.9%
Functions:
6 / 7
85.7%

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
0/14
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ throw std::runtime_error(std::string("brotensor: ") + op + ": " + reason);
45 ✗ }
46
47 72 inline void check_fp32(const ::brotensor::Tensor& t,
48 const char* op, const char* name) {
49
1/2
✓ Branch 0 taken 72 times.
✗ Branch 1 not taken.
72 if (t.dtype != Dtype::FP32) {
50
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ fail(op, std::string(name) +
51 " must be FP32 (CPU backend is FP32-only)");
52 }
53 72 }
54
55 // PyTorch adaptive-pool window endpoints for axis len L -> L_out.
56 1528 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 1528 start = (o * L) / L_out;
59 1528 end = ((o + 1) * L + L_out - 1) / L_out;
60
1/2
✓ Branch 0 taken 1528 times.
✗ Branch 1 not taken.
1528 if (end > L) end = L;
61
1/2
✓ Branch 0 taken 1528 times.
✗ Branch 1 not taken.
1528 if (start < 0) start = 0;
62 1528 }
63
64 } // namespace
65
66 // ═══════════════════════════════════════════════════════════════════════════
67 // adaptive_avg_pool2d
68 // ═══════════════════════════════════════════════════════════════════════════
69
70 44 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 44 const char* op = "adaptive_avg_pool2d_forward";
75 44 check_fp32(X, op, "X");
76
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (N < 0 || C < 1 || H < 1 || W < 1)
77
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "C/H/W must be >=1 and N >=0");
78
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (H_out < 1 || W_out < 1)
79
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "H_out and W_out must be >= 1");
80
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (X.rows != N || X.cols != C * H * W)
81
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "X shape must be (N, C*H*W)");
82
83 44 const int cols_out = C * H_out * W_out;
84
4/6
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 29 times.
44 if (Y.rows != N || Y.cols != cols_out || Y.dtype != Dtype::FP32) {
85 15 Y.resize(N, cols_out, Dtype::FP32);
86 15 }
87
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 if (N == 0) return;
88
89 44 const float* Xp = X.host_f32();
90 44 float* Yp = Y.host_f32_mut();
91
92
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 54 times.
98 for (int n = 0; n < N; ++n) {
93
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 54 times.
150 for (int c = 0; c < C; ++c) {
94 96 const float* x_chan =
95 96 Xp + (static_cast<long>(n) * C + c) * H * W;
96 96 float* y_chan =
97 96 Yp + (static_cast<long>(n) * C + c) * H_out * W_out;
98
2/2
✓ Branch 0 taken 250 times.
✓ Branch 1 taken 96 times.
346 for (int oh = 0; oh < H_out; ++oh) {
99 int h0, h1;
100 250 adaptive_window(oh, H, H_out, h0, h1);
101
2/2
✓ Branch 0 taken 1000 times.
✓ Branch 1 taken 250 times.
1250 for (int ow = 0; ow < W_out; ++ow) {
102 int w0, w1;
103 1000 adaptive_window(ow, W, W_out, w0, w1);
104 1000 const int area = (h1 - h0) * (w1 - w0);
105 1000 double acc = 0.0;
106
2/2
✓ Branch 0 taken 2144 times.
✓ Branch 1 taken 1000 times.
3144 for (int h = h0; h < h1; ++h) {
107 2144 const float* row = x_chan + static_cast<long>(h) * W;
108
2/2
✓ Branch 0 taken 6156 times.
✓ Branch 1 taken 2144 times.
8300 for (int w = w0; w < w1; ++w) acc += row[w];
109 2144 }
110 1000 y_chan[static_cast<long>(oh) * W_out + ow] =
111 1000 static_cast<float>(acc / area);
112 1000 }
113 250 }
114 96 }
115 54 }
116 44 }
117
118 10 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 10 const char* op = "adaptive_avg_pool2d_backward";
123 10 check_fp32(dY, op, "dY");
124
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (N < 0 || C < 1 || H < 1 || W < 1)
125
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "C/H/W must be >=1 and N >=0");
126
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (H_out < 1 || W_out < 1)
127
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "H_out and W_out must be >= 1");
128
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (dY.rows != N || dY.cols != C * H_out * W_out)
129
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ fail(op, "dY shape must be (N, C*H_out*W_out)");
130
131 10 const int cols_in = C * H * W;
132
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
10 if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) {
133 10 dX.resize(N, cols_in, Dtype::FP32);
134 10 }
135
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (N == 0) return;
136
137 10 const float* dYp = dY.host_f32();
138 10 float* dXp = dX.host_f32_mut();
139
140 10 const long total_in = static_cast<long>(N) * cols_in;
141
2/2
✓ Branch 0 taken 1915 times.
✓ Branch 1 taken 10 times.
1925 for (long i = 0; i < total_in; ++i) dXp[i] = 0.0f;
142
143
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 15 times.
25 for (int n = 0; n < N; ++n) {
144
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 15 times.
49 for (int c = 0; c < C; ++c) {
145 34 const float* dy_chan =
146 34 dYp + (static_cast<long>(n) * C + c) * H_out * W_out;
147 34 float* dx_chan =
148 34 dXp + (static_cast<long>(n) * C + c) * H * W;
149
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 34 times.
110 for (int oh = 0; oh < H_out; ++oh) {
150 int h0, h1;
151 76 adaptive_window(oh, H, H_out, h0, h1);
152
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 76 times.
278 for (int ow = 0; ow < W_out; ++ow) {
153 int w0, w1;
154 202 adaptive_window(ow, W, W_out, w0, w1);
155 202 const int area = (h1 - h0) * (w1 - w0);
156 202 const float g =
157 404 dy_chan[static_cast<long>(oh) * W_out + ow] /
158 202 static_cast<float>(area);
159
2/2
✓ Branch 0 taken 650 times.
✓ Branch 1 taken 202 times.
852 for (int h = h0; h < h1; ++h) {
160 650 float* row = dx_chan + static_cast<long>(h) * W;
161
2/2
✓ Branch 0 taken 2458 times.
✓ Branch 1 taken 650 times.
3108 for (int w = w0; w < w1; ++w) row[w] += g;
162 650 }
163 202 }
164 76 }
165 34 }
166 15 }
167 10 }
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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