src/cpu/resample.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU resample ops (CHUNK 4) ──────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementations. Ports src/cuda/resample.cu — FP32 path | ||
| 4 | // only (CPU is FP32-only). 2x spatial nearest / bilinear upsample, 2x average | ||
| 5 | // downsample, plus their backward passes. All tensors NCHW row-major. | ||
| 6 | // | ||
| 7 | // Memory layout (matches the GPU exactly): | ||
| 8 | // X / Y / dX / dY : NCHW — ((n*C + c) * H + h) * W + w | ||
| 9 | // Upsample: H_out = 2*H, W_out = 2*W. | ||
| 10 | // Downsample: H_out = H/2, W_out = W/2 (H, W must be even). | ||
| 11 | // | ||
| 12 | // Bilinear sampling convention (matches the GPU kernel verbatim — HALF-PIXEL, | ||
| 13 | // NOT align-corners): | ||
| 14 | // src_y = (oh + 0.5) * 0.5 - 0.5 (scale 0.5 because output is 2x input) | ||
| 15 | // src_x = (ow + 0.5) * 0.5 - 0.5 | ||
| 16 | // y0 = floor(src_y), fy = src_y - y0 (then border-clamped indices) | ||
| 17 | // value = bilinear interp of the 4 clamped neighbours. | ||
| 18 | // | ||
| 19 | // ACCUMULATION (matches the GPU kernels): | ||
| 20 | // upsample_nearest_2x — Y OVERWRITTEN. | ||
| 21 | // upsample_bilinear_2x — Y OVERWRITTEN. | ||
| 22 | // downsample_avg_2x — Y OVERWRITTEN. | ||
| 23 | // upsample_nearest_2x_backward — dX OVERWRITTEN (gather of 4 dY values). | ||
| 24 | // upsample_bilinear_2x_backward— dX OVERWRITTEN (GPU memsets dX to 0 then | ||
| 25 | // atomic-scatters; net effect = overwrite). | ||
| 26 | // downsample_avg_2x_backward — dX OVERWRITTEN. | ||
| 27 | |||
| 28 | #include <brotensor/tensor.h> | ||
| 29 | |||
| 30 | #include <cmath> | ||
| 31 | #include <stdexcept> | ||
| 32 | #include <string> | ||
| 33 | #include <vector> | ||
| 34 | |||
| 35 | namespace brotensor::detail::cpu { | ||
| 36 | |||
| 37 | namespace { | ||
| 38 | |||
| 39 | 77 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 40 | const char* op, const char* name) { | ||
| 41 | 77 | if (t.dtype != Dtype::FP32) { | |
| 42 | ✗ | throw std::runtime_error(std::string(op) + ": " + name + | |
| 43 | " must be FP32 (CPU backend is FP32-only)"); | ||
| 44 | } | ||
| 45 | 77 | } | |
| 46 | |||
| 47 | 1332 | inline int clampi(int v, int lo, int hi) { | |
| 48 | 1332 | return v < lo ? lo : (v > hi ? hi : v); | |
| 49 | } | ||
| 50 | |||
| 51 | } // namespace | ||
| 52 | |||
| 53 | // ─── Forward ─────────────────────────────────────────────────────────────── | ||
| 54 | |||
| 55 | 14 | void upsample_nearest_2x(const ::brotensor::Tensor& X, | |
| 56 | int N, int C, int H, int W, | ||
| 57 | ::brotensor::Tensor& Y) { | ||
| 58 | 14 | check_fp32(X, "upsample_nearest_2x", "X"); | |
| 59 | 14 | const int H_out = 2 * H, W_out = 2 * W; | |
| 60 | 14 | const int cols = C * H_out * W_out; | |
| 61 | 14 | if (Y.rows != N || Y.cols != cols || Y.dtype != Dtype::FP32) { | |
| 62 | 14 | Y.resize(N, cols, Dtype::FP32); | |
| 63 | 14 | } | |
| 64 | 14 | if (N == 0 || cols == 0) return; | |
| 65 | |||
| 66 | 14 | const float* Xp = X.host_f32(); | |
| 67 | 14 | float* Yp = Y.host_f32_mut(); | |
| 68 | |||
| 69 | 36 | for (int n = 0; n < N; ++n) { | |
| 70 | 100 | for (int c = 0; c < C; ++c) { | |
| 71 | 1046 | for (int oh = 0; oh < H_out; ++oh) { | |
| 72 | 968 | const int ih = oh / 2; | |
| 73 | 16328 | for (int ow = 0; ow < W_out; ++ow) { | |
| 74 | 15360 | const int iw = ow / 2; | |
| 75 | 15360 | Yp[((n * C + c) * H_out + oh) * W_out + ow] = | |
| 76 | 15360 | Xp[((n * C + c) * H + ih) * W + iw]; | |
| 77 | 15360 | } | |
| 78 | 968 | } | |
| 79 | 78 | } | |
| 80 | 22 | } | |
| 81 | 14 | } | |
| 82 | |||
| 83 | 14 | void upsample_bilinear_2x(const ::brotensor::Tensor& X, | |
| 84 | int N, int C, int H, int W, | ||
| 85 | ::brotensor::Tensor& Y) { | ||
| 86 | 14 | check_fp32(X, "upsample_bilinear_2x", "X"); | |
| 87 | 14 | const int H_out = 2 * H, W_out = 2 * W; | |
| 88 | 14 | const int cols = C * H_out * W_out; | |
| 89 | 14 | if (Y.rows != N || Y.cols != cols || Y.dtype != Dtype::FP32) { | |
| 90 | 14 | Y.resize(N, cols, Dtype::FP32); | |
| 91 | 14 | } | |
| 92 | 14 | if (N == 0 || cols == 0) return; | |
| 93 | |||
| 94 | 14 | const float* Xp = X.host_f32(); | |
| 95 | 14 | float* Yp = Y.host_f32_mut(); | |
| 96 | |||
| 97 | // src_y/src_x and the derived taps depend only on (oh)/(ow), never on | ||
| 98 | // (n, c) — precompute once instead of redoing it inside the (n, c) loop. | ||
| 99 | struct RowTap { int y0, y1; float fy; }; | ||
| 100 | struct ColTap { int x0, x1; float fx; }; | ||
| 101 | 14 | std::vector<RowTap> row_tap(H_out); | |
| 102 | 178 | for (int oh = 0; oh < H_out; ++oh) { | |
| 103 | 164 | const float src_y = (oh + 0.5f) * 0.5f - 0.5f; | |
| 104 | 164 | const int y0 = static_cast<int>(std::floor(src_y)); | |
| 105 | 164 | row_tap[oh].fy = src_y - y0; | |
| 106 | 164 | row_tap[oh].y0 = clampi(y0, 0, H - 1); | |
| 107 | 164 | row_tap[oh].y1 = clampi(y0 + 1, 0, H - 1); | |
| 108 | 164 | } | |
| 109 | 14 | std::vector<ColTap> col_tap(W_out); | |
| 110 | 204 | for (int ow = 0; ow < W_out; ++ow) { | |
| 111 | 190 | const float src_x = (ow + 0.5f) * 0.5f - 0.5f; | |
| 112 | 190 | const int x0 = static_cast<int>(std::floor(src_x)); | |
| 113 | 190 | col_tap[ow].fx = src_x - x0; | |
| 114 | 190 | col_tap[ow].x0 = clampi(x0, 0, W - 1); | |
| 115 | 190 | col_tap[ow].x1 = clampi(x0 + 1, 0, W - 1); | |
| 116 | 190 | } | |
| 117 | |||
| 118 | 36 | for (int n = 0; n < N; ++n) { | |
| 119 | 100 | for (int c = 0; c < C; ++c) { | |
| 120 | 78 | const int base = (n * C + c) * H; | |
| 121 | 1046 | for (int oh = 0; oh < H_out; ++oh) { | |
| 122 | 968 | const RowTap& r = row_tap[oh]; | |
| 123 | 16328 | for (int ow = 0; ow < W_out; ++ow) { | |
| 124 | 15360 | const ColTap& cx = col_tap[ow]; | |
| 125 | 15360 | const float v00 = Xp[(base + r.y0) * W + cx.x0]; | |
| 126 | 15360 | const float v01 = Xp[(base + r.y0) * W + cx.x1]; | |
| 127 | 15360 | const float v10 = Xp[(base + r.y1) * W + cx.x0]; | |
| 128 | 15360 | const float v11 = Xp[(base + r.y1) * W + cx.x1]; | |
| 129 | 15360 | const float top = v00 + (v01 - v00) * cx.fx; | |
| 130 | 15360 | const float bot = v10 + (v11 - v10) * cx.fx; | |
| 131 | 15360 | Yp[((n * C + c) * H_out + oh) * W_out + ow] = | |
| 132 | 15360 | top + (bot - top) * r.fy; | |
| 133 | 15360 | } | |
| 134 | 968 | } | |
| 135 | 78 | } | |
| 136 | 22 | } | |
| 137 | 14 | } | |
| 138 | |||
| 139 | 13 | void downsample_avg_2x(const ::brotensor::Tensor& X, | |
| 140 | int N, int C, int H, int W, | ||
| 141 | ::brotensor::Tensor& Y) { | ||
| 142 | 13 | check_fp32(X, "downsample_avg_2x", "X"); | |
| 143 | 13 | if ((H & 1) || (W & 1)) { | |
| 144 | ✗ | throw std::runtime_error("downsample_avg_2x: H and W must be even"); | |
| 145 | } | ||
| 146 | 13 | const int H_out = H / 2, W_out = W / 2; | |
| 147 | 13 | const int cols = C * H_out * W_out; | |
| 148 | 13 | if (Y.rows != N || Y.cols != cols || Y.dtype != Dtype::FP32) { | |
| 149 | 13 | Y.resize(N, cols, Dtype::FP32); | |
| 150 | 13 | } | |
| 151 | 13 | if (N == 0 || cols == 0) return; | |
| 152 | |||
| 153 | 13 | const float* Xp = X.host_f32(); | |
| 154 | 13 | float* Yp = Y.host_f32_mut(); | |
| 155 | |||
| 156 | 33 | for (int n = 0; n < N; ++n) { | |
| 157 | 92 | for (int c = 0; c < C; ++c) { | |
| 158 | 302 | for (int oh = 0; oh < H_out; ++oh) { | |
| 159 | 230 | const int ih = oh * 2; | |
| 160 | 1160 | for (int ow = 0; ow < W_out; ++ow) { | |
| 161 | 930 | const int iw = ow * 2; | |
| 162 | 930 | const int b = ((n * C + c) * H + ih) * W + iw; | |
| 163 | 930 | Yp[((n * C + c) * H_out + oh) * W_out + ow] = | |
| 164 | 2790 | 0.25f * (Xp[b] + Xp[b + 1] + | |
| 165 | 1860 | Xp[b + W] + Xp[b + W + 1]); | |
| 166 | 930 | } | |
| 167 | 230 | } | |
| 168 | 72 | } | |
| 169 | 20 | } | |
| 170 | 13 | } | |
| 171 | |||
| 172 | // ─── Backward ────────────────────────────────────────────────────────────── | ||
| 173 | |||
| 174 | 12 | void upsample_nearest_2x_backward(const ::brotensor::Tensor& dY, | |
| 175 | int N, int C, int H, int W, | ||
| 176 | ::brotensor::Tensor& dX) { | ||
| 177 | 12 | check_fp32(dY, "upsample_nearest_2x_backward", "dY"); | |
| 178 | 12 | const int H_out = 2 * H, W_out = 2 * W; | |
| 179 | 12 | const int cols_in = C * H * W; | |
| 180 | 12 | if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) { | |
| 181 | 12 | dX.resize(N, cols_in, Dtype::FP32); | |
| 182 | 12 | } | |
| 183 | 12 | if (N == 0 || cols_in == 0) return; | |
| 184 | |||
| 185 | 12 | const float* dYp = dY.host_f32(); | |
| 186 | 12 | float* dXp = dX.host_f32_mut(); | |
| 187 | |||
| 188 | // One accumulation per input pixel; gather the 4 contributing dY values. | ||
| 189 | 31 | for (int n = 0; n < N; ++n) { | |
| 190 | 83 | for (int c = 0; c < C; ++c) { | |
| 191 | 492 | for (int ih = 0; ih < H; ++ih) { | |
| 192 | 3892 | for (int iw = 0; iw < W; ++iw) { | |
| 193 | 3464 | float s = 0.0f; | |
| 194 | 10392 | for (int a = 0; a < 2; ++a) { | |
| 195 | 20784 | for (int b = 0; b < 2; ++b) { | |
| 196 | 41568 | s += dYp[((n * C + c) * H_out + 2 * ih + a) * | |
| 197 | 27712 | W_out + 2 * iw + b]; | |
| 198 | 13856 | } | |
| 199 | 6928 | } | |
| 200 | 3464 | dXp[((n * C + c) * H + ih) * W + iw] = s; // overwrite | |
| 201 | 3464 | } | |
| 202 | 428 | } | |
| 203 | 64 | } | |
| 204 | 19 | } | |
| 205 | 12 | } | |
| 206 | |||
| 207 | 12 | void upsample_bilinear_2x_backward(const ::brotensor::Tensor& dY, | |
| 208 | int N, int C, int H, int W, | ||
| 209 | ::brotensor::Tensor& dX) { | ||
| 210 | 12 | check_fp32(dY, "upsample_bilinear_2x_backward", "dY"); | |
| 211 | 12 | const int H_out = 2 * H, W_out = 2 * W; | |
| 212 | 12 | const int cols_in = C * H * W; | |
| 213 | 12 | if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) { | |
| 214 | 12 | dX.resize(N, cols_in, Dtype::FP32); | |
| 215 | 12 | } | |
| 216 | 12 | if (N == 0 || cols_in == 0) return; | |
| 217 | |||
| 218 | 12 | const float* dYp = dY.host_f32(); | |
| 219 | 12 | float* dXp = dX.host_f32_mut(); | |
| 220 | |||
| 221 | // GPU memsets dX to 0 then atomic-scatters — net effect is OVERWRITE. | ||
| 222 | 12 | const int total_in = N * cols_in; | |
| 223 | 3476 | for (int i = 0; i < total_in; ++i) dXp[i] = 0.0f; | |
| 224 | |||
| 225 | // Same per-row / per-column tap hoist as the forward pass. | ||
| 226 | struct RowTap { int y0, y1; float fy; }; | ||
| 227 | struct ColTap { int x0, x1; float fx; }; | ||
| 228 | 12 | std::vector<RowTap> row_tap(H_out); | |
| 229 | 160 | for (int oh = 0; oh < H_out; ++oh) { | |
| 230 | 148 | const float src_y = (oh + 0.5f) * 0.5f - 0.5f; | |
| 231 | 148 | const int y0 = static_cast<int>(std::floor(src_y)); | |
| 232 | 148 | row_tap[oh].fy = src_y - y0; | |
| 233 | 148 | row_tap[oh].y0 = clampi(y0, 0, H - 1); | |
| 234 | 148 | row_tap[oh].y1 = clampi(y0 + 1, 0, H - 1); | |
| 235 | 148 | } | |
| 236 | 12 | std::vector<ColTap> col_tap(W_out); | |
| 237 | 176 | for (int ow = 0; ow < W_out; ++ow) { | |
| 238 | 164 | const float src_x = (ow + 0.5f) * 0.5f - 0.5f; | |
| 239 | 164 | const int x0 = static_cast<int>(std::floor(src_x)); | |
| 240 | 164 | col_tap[ow].fx = src_x - x0; | |
| 241 | 164 | col_tap[ow].x0 = clampi(x0, 0, W - 1); | |
| 242 | 164 | col_tap[ow].x1 = clampi(x0 + 1, 0, W - 1); | |
| 243 | 164 | } | |
| 244 | |||
| 245 | 31 | for (int n = 0; n < N; ++n) { | |
| 246 | 83 | for (int c = 0; c < C; ++c) { | |
| 247 | 64 | const int base = (n * C + c) * H; | |
| 248 | 920 | for (int oh = 0; oh < H_out; ++oh) { | |
| 249 | 856 | const RowTap& r = row_tap[oh]; | |
| 250 | 14712 | for (int ow = 0; ow < W_out; ++ow) { | |
| 251 | 13856 | const ColTap& cx = col_tap[ow]; | |
| 252 | 13856 | const float w00 = (1.0f - r.fy) * (1.0f - cx.fx); | |
| 253 | 13856 | const float w01 = (1.0f - r.fy) * cx.fx; | |
| 254 | 13856 | const float w10 = r.fy * (1.0f - cx.fx); | |
| 255 | 13856 | const float w11 = r.fy * cx.fx; | |
| 256 | 13856 | const float g = | |
| 257 | 13856 | dYp[((n * C + c) * H_out + oh) * W_out + ow]; | |
| 258 | 13856 | dXp[(base + r.y0) * W + cx.x0] += w00 * g; | |
| 259 | 13856 | dXp[(base + r.y0) * W + cx.x1] += w01 * g; | |
| 260 | 13856 | dXp[(base + r.y1) * W + cx.x0] += w10 * g; | |
| 261 | 13856 | dXp[(base + r.y1) * W + cx.x1] += w11 * g; | |
| 262 | 13856 | } | |
| 263 | 856 | } | |
| 264 | 64 | } | |
| 265 | 19 | } | |
| 266 | 12 | } | |
| 267 | |||
| 268 | 12 | void downsample_avg_2x_backward(const ::brotensor::Tensor& dY, | |
| 269 | int N, int C, int H, int W, | ||
| 270 | ::brotensor::Tensor& dX) { | ||
| 271 | 12 | check_fp32(dY, "downsample_avg_2x_backward", "dY"); | |
| 272 | 12 | if ((H & 1) || (W & 1)) { | |
| 273 | ✗ | throw std::runtime_error("downsample_avg_2x_backward: H and W must be even"); | |
| 274 | } | ||
| 275 | 12 | const int H_out = H / 2, W_out = W / 2; | |
| 276 | 12 | const int cols_in = C * H * W; | |
| 277 | 12 | if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) { | |
| 278 | 12 | dX.resize(N, cols_in, Dtype::FP32); | |
| 279 | 12 | } | |
| 280 | 12 | if (N == 0 || cols_in == 0) return; | |
| 281 | |||
| 282 | 12 | const float* dYp = dY.host_f32(); | |
| 283 | 12 | float* dXp = dX.host_f32_mut(); | |
| 284 | |||
| 285 | // One read per input pixel; each maps to a single output pixel, scale 1/4. | ||
| 286 | 31 | for (int n = 0; n < N; ++n) { | |
| 287 | 83 | for (int c = 0; c < C; ++c) { | |
| 288 | 492 | for (int ih = 0; ih < H; ++ih) { | |
| 289 | 428 | const int oh = ih / 2; | |
| 290 | 3892 | for (int iw = 0; iw < W; ++iw) { | |
| 291 | 3464 | const int ow = iw / 2; | |
| 292 | 3464 | dXp[((n * C + c) * H + ih) * W + iw] = | |
| 293 | 3464 | 0.25f * dYp[((n * C + c) * H_out + oh) * W_out + ow]; | |
| 294 | 3464 | } | |
| 295 | 428 | } | |
| 296 | 64 | } | |
| 297 | 19 | } | |
| 298 | 12 | } | |
| 299 | |||
| 300 | } // namespace brotensor::detail::cpu | ||
| 301 |