src/cpu/pad2d.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU 2D padding ───────────────────────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementation of pad2d_forward / pad2d_backward — the | ||
| 4 | // image (NCHW) analogue of pad1d in conv1d.cpp. Same mode convention: | ||
| 5 | // 0 = zero, 1 = reflect (no edge repeat; requires pad < H/W on that axis), | ||
| 6 | // 2 = replicate (clamp to edge sample). | ||
| 7 | // | ||
| 8 | // Memory layout (NCHW flat — matches resample.cpp / interp2d.cpp): | ||
| 9 | // X / dX : ((n*C + c)*H + h)*W + w | ||
| 10 | // Y / dY : ((n*C + c)*H_pad + h)*W_pad + w | ||
| 11 | // with H_pad = H + pad_top + pad_bottom, W_pad = W + pad_left + pad_right. | ||
| 12 | // | ||
| 13 | // ── ACCUMULATION ──────────────────────────────────────────────────────────── | ||
| 14 | // pad2d_forward — Y OVERWRITTEN. | ||
| 15 | // pad2d_backward — dX OVERWRITTEN. Adjoint = scatter each output gradient | ||
| 16 | // onto the input sample it read; for reflect / replicate | ||
| 17 | // several output positions may collapse onto the same | ||
| 18 | // input position and those gradients sum. | ||
| 19 | |||
| 20 | #include <brotensor/tensor.h> | ||
| 21 | |||
| 22 | #include <stdexcept> | ||
| 23 | #include <string> | ||
| 24 | |||
| 25 | namespace brotensor::detail::cpu { | ||
| 26 | |||
| 27 | namespace { | ||
| 28 | |||
| 29 | 2 | [[noreturn]] inline void fail(const char* op, const std::string& reason) { | |
| 30 |
7/14✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
|
2 | throw std::runtime_error(std::string("brotensor: ") + op + ": " + reason); |
| 31 | 2 | } | |
| 32 | |||
| 33 | 61 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 34 | const char* op, const char* name) { | ||
| 35 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (t.dtype != Dtype::FP32) { |
| 36 | ✗ | fail(op, std::string(name) + | |
| 37 | " must be FP32 (CPU backend is FP32-only)"); | ||
| 38 | } | ||
| 39 | 61 | } | |
| 40 | |||
| 41 | // Map an output position p in [0, L_pad) along one axis to a source index in | ||
| 42 | // [0, L) for the given mode, or -1 for a zero-padded slot. Verbatim copy of | ||
| 43 | // the pad1d_src helper in conv1d.cpp — the per-axis logic is identical. | ||
| 44 | 7909 | inline int pad_src(int p, int L, int pad_left, int mode) { | |
| 45 | 7909 | const int rel = p - pad_left; | |
| 46 |
4/4✓ Branch 0 taken 6159 times.
✓ Branch 1 taken 1750 times.
✓ Branch 2 taken 1606 times.
✓ Branch 3 taken 4553 times.
|
7909 | if (rel >= 0 && rel < L) return rel; // interior |
| 47 |
2/2✓ Branch 0 taken 2751 times.
✓ Branch 1 taken 605 times.
|
3356 | if (mode == 0) return -1; // zero |
| 48 |
4/4✓ Branch 0 taken 810 times.
✓ Branch 1 taken 1941 times.
✓ Branch 2 taken 371 times.
✓ Branch 3 taken 439 times.
|
2751 | if (mode == 2) return rel < 0 ? 0 : L - 1; // replicate (clamp) |
| 49 | // mode == 1: reflect without repeating the edge sample (numpy 'reflect'). | ||
| 50 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1941 times.
|
1941 | if (L == 1) return 0; |
| 51 | 1941 | int q = rel; | |
| 52 | 1941 | const int period = 2 * (L - 1); | |
| 53 | 1941 | q %= period; | |
| 54 |
2/2✓ Branch 0 taken 856 times.
✓ Branch 1 taken 1085 times.
|
1941 | if (q < 0) q += period; |
| 55 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1900 times.
|
1941 | return q < L ? q : period - q; |
| 56 | 7909 | } | |
| 57 | |||
| 58 | 61 | inline void check_args(const char* op, | |
| 59 | int N, int C, int H, int W, | ||
| 60 | int pad_top, int pad_bottom, | ||
| 61 | int pad_left, int pad_right, int mode) { | ||
| 62 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (N < 0 || C < 1 || H < 1 || W < 1) { |
| 63 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
2 | fail(op, "C/H/W must be >=1 and N >=0"); |
| 64 | } | ||
| 65 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
122 | if (pad_top < 0 || pad_bottom < 0 || |
| 66 | 61 | pad_left < 0 || pad_right < 0) { | |
| 67 | ✗ | fail(op, "pad counts must be >=0"); | |
| 68 | } | ||
| 69 |
1/2✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
|
61 | if (mode < 0 || mode > 2) { |
| 70 | ✗ | fail(op, "mode must be 0 (zero), 1 (reflect) or 2 (replicate)"); | |
| 71 | } | ||
| 72 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 48 times.
|
61 | if (mode == 1) { |
| 73 |
2/2✓ Branch 0 taken 47 times.
✓ Branch 1 taken 1 time.
|
48 | if (pad_top >= H || pad_bottom >= H) { |
| 74 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | fail(op, "reflect padding requires pad_top and pad_bottom < H"); |
| 75 | } | ||
| 76 |
2/2✓ Branch 0 taken 46 times.
✓ Branch 1 taken 1 time.
|
47 | if (pad_left >= W || pad_right >= W) { |
| 77 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
|
1 | fail(op, "reflect padding requires pad_left and pad_right < W"); |
| 78 | } | ||
| 79 | 46 | } | |
| 80 | 61 | } | |
| 81 | |||
| 82 | } // namespace | ||
| 83 | |||
| 84 | // ─── pad2d_forward ───────────────────────────────────────────────────────── | ||
| 85 | |||
| 86 | 52 | void pad2d_forward(const ::brotensor::Tensor& X, | |
| 87 | int N, int C, int H, int W, | ||
| 88 | int pad_top, int pad_bottom, | ||
| 89 | int pad_left, int pad_right, int mode, | ||
| 90 | ::brotensor::Tensor& Y) { | ||
| 91 | 52 | const char* op = "pad2d_forward"; | |
| 92 | 52 | check_fp32(X, op, "X"); | |
| 93 | 52 | check_args(op, N, C, H, W, pad_top, pad_bottom, pad_left, pad_right, mode); | |
| 94 |
1/2✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
|
52 | if (X.rows != N || X.cols != C * H * W) { |
| 95 | ✗ | fail(op, "X shape must be (N, C*H*W)"); | |
| 96 | } | ||
| 97 | 52 | const int H_pad = H + pad_top + pad_bottom; | |
| 98 | 52 | const int W_pad = W + pad_left + pad_right; | |
| 99 | 52 | const int cols_out = C * H_pad * W_pad; | |
| 100 |
4/6✓ Branch 0 taken 39 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 39 times.
|
52 | if (Y.rows != N || Y.cols != cols_out || Y.dtype != Dtype::FP32) { |
| 101 | 13 | Y.resize(N, cols_out, Dtype::FP32); | |
| 102 | 13 | } | |
| 103 |
2/4✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 52 times.
|
52 | if (N == 0 || C == 0) return; |
| 104 | |||
| 105 | 52 | const float* Xp = X.host_f32(); | |
| 106 | 52 | float* Yp = Y.host_f32_mut(); | |
| 107 | |||
| 108 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 59 times.
|
111 | for (int n = 0; n < N; ++n) { |
| 109 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 59 times.
|
149 | for (int c = 0; c < C; ++c) { |
| 110 | 90 | const float* x_chan = | |
| 111 | 90 | Xp + (static_cast<long>(n) * C + c) * H * W; | |
| 112 | 90 | float* y_chan = | |
| 113 | 90 | Yp + (static_cast<long>(n) * C + c) * H_pad * W_pad; | |
| 114 |
2/2✓ Branch 0 taken 640 times.
✓ Branch 1 taken 90 times.
|
730 | for (int p = 0; p < H_pad; ++p) { |
| 115 | 640 | const int src_h = pad_src(p, H, pad_top, mode); | |
| 116 | 640 | float* y_row = y_chan + static_cast<long>(p) * W_pad; | |
| 117 |
2/2✓ Branch 0 taken 556 times.
✓ Branch 1 taken 84 times.
|
640 | if (src_h < 0) { |
| 118 | // Whole row is zero-padded. | ||
| 119 |
2/2✓ Branch 0 taken 789 times.
✓ Branch 1 taken 84 times.
|
873 | for (int q = 0; q < W_pad; ++q) y_row[q] = 0.0f; |
| 120 | 84 | continue; | |
| 121 | } | ||
| 122 | 556 | const float* x_row = x_chan + static_cast<long>(src_h) * W; | |
| 123 |
2/2✓ Branch 0 taken 5141 times.
✓ Branch 1 taken 556 times.
|
5697 | for (int q = 0; q < W_pad; ++q) { |
| 124 | 5141 | const int src_w = pad_src(q, W, pad_left, mode); | |
| 125 |
2/2✓ Branch 0 taken 4760 times.
✓ Branch 1 taken 381 times.
|
5141 | y_row[q] = src_w < 0 ? 0.0f : x_row[src_w]; |
| 126 | 5141 | } | |
| 127 | 556 | } | |
| 128 | 90 | } | |
| 129 | 59 | } | |
| 130 | 52 | } | |
| 131 | |||
| 132 | // ─── pad2d_backward ──────────────────────────────────────────────────────── | ||
| 133 | |||
| 134 | 7 | void pad2d_backward(const ::brotensor::Tensor& dY, | |
| 135 | int N, int C, int H, int W, | ||
| 136 | int pad_top, int pad_bottom, | ||
| 137 | int pad_left, int pad_right, int mode, | ||
| 138 | ::brotensor::Tensor& dX) { | ||
| 139 | 7 | const char* op = "pad2d_backward"; | |
| 140 | 7 | check_fp32(dY, op, "dY"); | |
| 141 | 7 | check_args(op, N, C, H, W, pad_top, pad_bottom, pad_left, pad_right, mode); | |
| 142 | 7 | const int H_pad = H + pad_top + pad_bottom; | |
| 143 | 7 | const int W_pad = W + pad_left + pad_right; | |
| 144 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (dY.rows != N || dY.cols != C * H_pad * W_pad) { |
| 145 | ✗ | fail(op, "dY shape must be (N, C*(H+pt+pb)*(W+pl+pr))"); | |
| 146 | } | ||
| 147 | 7 | const int cols_in = C * H * W; | |
| 148 |
1/6✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
7 | if (dX.rows != N || dX.cols != cols_in || dX.dtype != Dtype::FP32) { |
| 149 | 7 | dX.resize(N, cols_in, Dtype::FP32); | |
| 150 | 7 | } | |
| 151 |
2/4✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
|
7 | if (N == 0 || C == 0) return; |
| 152 | |||
| 153 | 7 | const float* dYp = dY.host_f32(); | |
| 154 | 7 | float* dXp = dX.host_f32_mut(); | |
| 155 | |||
| 156 | // Zero dX, then scatter each output gradient onto its source input pixel. | ||
| 157 | 7 | const long total_in = static_cast<long>(N) * cols_in; | |
| 158 |
2/2✓ Branch 0 taken 657 times.
✓ Branch 1 taken 7 times.
|
664 | for (long i = 0; i < total_in; ++i) dXp[i] = 0.0f; |
| 159 | |||
| 160 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
|
17 | for (int n = 0; n < N; ++n) { |
| 161 |
2/2✓ Branch 0 taken 23 times.
✓ Branch 1 taken 10 times.
|
33 | for (int c = 0; c < C; ++c) { |
| 162 | 23 | const float* dy_chan = | |
| 163 | 23 | dYp + (static_cast<long>(n) * C + c) * H_pad * W_pad; | |
| 164 | 23 | float* dx_chan = | |
| 165 | 23 | dXp + (static_cast<long>(n) * C + c) * H * W; | |
| 166 |
2/2✓ Branch 0 taken 177 times.
✓ Branch 1 taken 23 times.
|
200 | for (int p = 0; p < H_pad; ++p) { |
| 167 | 177 | const int src_h = pad_src(p, H, pad_top, mode); | |
| 168 |
2/2✓ Branch 0 taken 163 times.
✓ Branch 1 taken 14 times.
|
177 | if (src_h < 0) continue; |
| 169 | 163 | const float* dy_row = dy_chan + static_cast<long>(p) * W_pad; | |
| 170 | 163 | float* dx_row = dx_chan + static_cast<long>(src_h) * W; | |
| 171 |
2/2✓ Branch 0 taken 1951 times.
✓ Branch 1 taken 163 times.
|
2114 | for (int q = 0; q < W_pad; ++q) { |
| 172 | 1951 | const int src_w = pad_src(q, W, pad_left, mode); | |
| 173 |
2/2✓ Branch 0 taken 126 times.
✓ Branch 1 taken 1825 times.
|
1951 | if (src_w >= 0) dx_row[src_w] += dy_row[q]; |
| 174 | 1951 | } | |
| 175 | 163 | } | |
| 176 | 23 | } | |
| 177 | 10 | } | |
| 178 | 7 | } | |
| 179 | |||
| 180 | } // namespace brotensor::detail::cpu | ||
| 181 |