src/cpu/conv3d.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU conv3d ops (Qwen3-VL patch embed) ──────────────────────────────── | ||
| 2 | // | ||
| 3 | // FP32 scalar host implementation of conv3d_forward. Mirrors src/cpu/conv2d.cpp | ||
| 4 | // — direct convolution loop, FP32 accumulator. CPU is FP32-only per the | ||
| 5 | // brotensor convention (CLAUDE.md); conv3d_int8w_fp16_forward is GPU-only and | ||
| 6 | // has no CPU registration (its vtable slot stays null and the dispatcher | ||
| 7 | // throws "not implemented on CPU"). | ||
| 8 | // | ||
| 9 | // Memory layout (matches the GPU exactly): | ||
| 10 | // X : NCTHW — (((n*C_in + c_in) * T + t) * H + h) * W + w | ||
| 11 | // Y : NCTHW — (((n*C_out + c_out) * T_out + ot) * H_out + oh) * W_out + ow | ||
| 12 | // Wt : OICTHW (grouped) — (((c_out*Cg_in + c_in_local) * kT + kt) * kH + kh) * kW + kw | ||
| 13 | // where Cg_in = C_in/groups is the per-group input-channel count. | ||
| 14 | // bias: (C_out, 1), optional (may be null) | ||
| 15 | // | ||
| 16 | // Groups convention: output channel c_out belongs to group g = c_out/Cg_out; | ||
| 17 | // that group's absolute input channels start at g*Cg_in (Cg_in channels wide). | ||
| 18 | // | ||
| 19 | // Output-size formula (identical to conv2d, applied per-axis): | ||
| 20 | // T_out = (T + 2*pad_t - dil_t*(kT-1) - 1) / stride_t + 1 | ||
| 21 | // H_out = (H + 2*pad_h - dil_h*(kH-1) - 1) / stride_h + 1 | ||
| 22 | // W_out = (W + 2*pad_w - dil_w*(kW-1) - 1) / stride_w + 1 | ||
| 23 | // | ||
| 24 | // ACCUMULATION: conv3d_forward — Y OVERWRITTEN (kernel stores acc directly). | ||
| 25 | |||
| 26 | #include <brotensor/tensor.h> | ||
| 27 | #include <brotensor/detail/cpu/thread_pool.h> | ||
| 28 | |||
| 29 | #include <cstddef> | ||
| 30 | #include <stdexcept> | ||
| 31 | #include <string> | ||
| 32 | |||
| 33 | namespace brotensor::detail::cpu { | ||
| 34 | |||
| 35 | namespace { | ||
| 36 | |||
| 37 | 25 | inline void check_groups(const char* op, int C_in, int C_out, int groups) { | |
| 38 | 25 | if (groups < 1 || C_in % groups != 0 || C_out % groups != 0) { | |
| 39 | ✗ | throw std::runtime_error( | |
| 40 | ✗ | std::string(op) + | |
| 41 | ": groups must be >=1 and divide both C_in and C_out"); | ||
| 42 | } | ||
| 43 | 25 | } | |
| 44 | |||
| 45 | 75 | inline int out_dim(int in, int pad, int dil, int k, int stride) { | |
| 46 | 75 | return (in + 2 * pad - dil * (k - 1) - 1) / stride + 1; | |
| 47 | } | ||
| 48 | |||
| 49 | } // namespace | ||
| 50 | |||
| 51 | 25 | void conv3d_forward(const ::brotensor::Tensor& X, | |
| 52 | const ::brotensor::Tensor& Wt, | ||
| 53 | const ::brotensor::Tensor* bias, | ||
| 54 | int N, int C_in, int T, int H, int W, | ||
| 55 | int C_out, int kT, int kH, int kW, | ||
| 56 | int stride_t, int stride_h, int stride_w, | ||
| 57 | int pad_t, int pad_h, int pad_w, | ||
| 58 | int dil_t, int dil_h, int dil_w, | ||
| 59 | int groups, | ||
| 60 | ::brotensor::Tensor& Y) { | ||
| 61 | 25 | if (Wt.dtype != X.dtype) { | |
| 62 | ✗ | throw std::runtime_error("conv3d_forward: Wt dtype must match X"); | |
| 63 | } | ||
| 64 | 25 | if (bias && bias->dtype != X.dtype) { | |
| 65 | ✗ | throw std::runtime_error("conv3d_forward: bias dtype must match X"); | |
| 66 | } | ||
| 67 | 25 | check_groups("conv3d_forward", C_in, C_out, groups); | |
| 68 | 25 | const int Cg_in = C_in / groups; | |
| 69 | 25 | const int Cg_out = C_out / groups; | |
| 70 | 25 | const int T_out = out_dim(T, pad_t, dil_t, kT, stride_t); | |
| 71 | 25 | const int H_out = out_dim(H, pad_h, dil_h, kH, stride_h); | |
| 72 | 25 | const int W_out = out_dim(W, pad_w, dil_w, kW, stride_w); | |
| 73 | 25 | if (T_out <= 0 || H_out <= 0 || W_out <= 0) { | |
| 74 | 1 | throw std::runtime_error("conv3d_forward: non-positive output shape"); | |
| 75 | } | ||
| 76 | 24 | const int out_cols = C_out * T_out * H_out * W_out; | |
| 77 | 24 | if (Y.rows != N || Y.cols != out_cols || Y.dtype != X.dtype) { | |
| 78 | 24 | Y.resize(N, out_cols, X.dtype); | |
| 79 | 24 | } | |
| 80 | 24 | if (N == 0 || out_cols == 0) return; | |
| 81 | |||
| 82 | 24 | const float* Xp = X.host_f32(); | |
| 83 | 24 | const float* Wp = Wt.host_f32(); | |
| 84 | 24 | const float* Bp = bias ? bias->host_f32() : nullptr; | |
| 85 | 24 | float* Yp = Y.host_f32_mut(); | |
| 86 | |||
| 87 | // Interior region: the (ot, oh, ow) box for which every kernel tap is | ||
| 88 | // guaranteed in-bounds on every axis (mirrors conv2d.cpp's split, | ||
| 89 | // extended to 3 axes), computed once — independent of n/oc. Only the | ||
| 90 | // thin border shell outside that box needs the per-tap bounds check; | ||
| 91 | // the interior runs a branch-free kt/kh/kw loop. | ||
| 92 | 24 | int ot_lo = (pad_t + stride_t - 1) / stride_t; | |
| 93 | 24 | int ot_hi = T - 1 + pad_t - (kT - 1) * dil_t; | |
| 94 | 24 | ot_hi = (ot_hi >= 0) ? (ot_hi / stride_t) : -1; | |
| 95 | 24 | if (ot_lo < 0) ot_lo = 0; | |
| 96 | 24 | if (ot_hi >= T_out) ot_hi = T_out - 1; | |
| 97 | |||
| 98 | 24 | int oh_lo = (pad_h + stride_h - 1) / stride_h; | |
| 99 | 24 | int oh_hi = H - 1 + pad_h - (kH - 1) * dil_h; | |
| 100 | 24 | oh_hi = (oh_hi >= 0) ? (oh_hi / stride_h) : -1; | |
| 101 | 24 | if (oh_lo < 0) oh_lo = 0; | |
| 102 | 24 | if (oh_hi >= H_out) oh_hi = H_out - 1; | |
| 103 | |||
| 104 | 24 | int ow_lo = (pad_w + stride_w - 1) / stride_w; | |
| 105 | 24 | int ow_hi = W - 1 + pad_w - (kW - 1) * dil_w; | |
| 106 | 24 | ow_hi = (ow_hi >= 0) ? (ow_hi / stride_w) : -1; | |
| 107 | 24 | if (ow_lo < 0) ow_lo = 0; | |
| 108 | 24 | if (ow_hi >= W_out) ow_hi = W_out - 1; | |
| 109 | |||
| 110 | 24 | const bool has_interior = | |
| 111 | 24 | (ot_lo <= ot_hi) && (oh_lo <= oh_hi) && (ow_lo <= ow_hi); | |
| 112 | |||
| 113 | // Each n exclusively owns Y's batch slice n (X/Wt/bias are read-only), so | ||
| 114 | // this parallelizes across n with no cross-thread writes. | ||
| 115 | 59 | parallel_for(static_cast<std::size_t>(N), [&](std::size_t ni) { | |
| 116 | 35 | const int n = static_cast<int>(ni); | |
| 117 | 212 | for (int oc = 0; oc < C_out; ++oc) { | |
| 118 | 177 | const int g = oc / Cg_out; | |
| 119 | 177 | const int ic_base = g * Cg_in; | |
| 120 | 177 | const int w_oc_base = oc * Cg_in * kT * kH * kW; | |
| 121 | 177 | const float bias_v = Bp ? Bp[oc] : 0.0f; | |
| 122 | |||
| 123 | // Border voxel: same bounds-checked accumulation as before. | ||
| 124 | 6205 | auto compute_bordered = [&](int ot, int oh, int ow) -> float { | |
| 125 | 6028 | const int in_t_origin = ot * stride_t - pad_t; | |
| 126 | 6028 | const int in_h_origin = oh * stride_h - pad_h; | |
| 127 | 6028 | const int in_w_origin = ow * stride_w - pad_w; | |
| 128 | 6028 | float acc = 0.0f; | |
| 129 | 20166 | for (int ic_local = 0; ic_local < Cg_in; ++ic_local) { | |
| 130 | 14138 | const int ic = ic_base + ic_local; | |
| 131 | 14138 | const int w_ic_base = w_oc_base + ic_local * kT * kH * kW; | |
| 132 | 14138 | const int x_ic_base = (n * C_in + ic) * T * H * W; | |
| 133 | 56504 | for (int kt = 0; kt < kT; ++kt) { | |
| 134 | 42366 | const int in_t = in_t_origin + kt * dil_t; | |
| 135 | 42366 | if (in_t < 0 || in_t >= T) continue; | |
| 136 | 130704 | for (int kh = 0; kh < kH; ++kh) { | |
| 137 | 97921 | const int in_h = in_h_origin + kh * dil_h; | |
| 138 | 97921 | if (in_h < 0 || in_h >= H) continue; | |
| 139 | 316584 | for (int kw = 0; kw < kW; ++kw) { | |
| 140 | 237352 | const int in_w = in_w_origin + kw * dil_w; | |
| 141 | 237352 | if (in_w < 0 || in_w >= W) continue; | |
| 142 | 191083 | acc += | |
| 143 | 573249 | Xp[x_ic_base + | |
| 144 | 382166 | (in_t * H + in_h) * W + in_w] * | |
| 145 | 573249 | Wp[w_ic_base + | |
| 146 | 382166 | (kt * kH + kh) * kW + kw]; | |
| 147 | 191083 | } | |
| 148 | 79232 | } | |
| 149 | 32783 | } | |
| 150 | 14138 | } | |
| 151 | 6028 | return acc; | |
| 152 | }; | ||
| 153 | |||
| 154 | // Interior voxel: every tap is guaranteed in-bounds — no checks. | ||
| 155 | 4966 | auto compute_interior = [&](int ot, int oh, int ow) -> float { | |
| 156 | 4789 | const int in_t_origin = ot * stride_t - pad_t; | |
| 157 | 4789 | const int in_h_origin = oh * stride_h - pad_h; | |
| 158 | 4789 | const int in_w_origin = ow * stride_w - pad_w; | |
| 159 | 4789 | float acc = 0.0f; | |
| 160 | 22275 | for (int ic_local = 0; ic_local < Cg_in; ++ic_local) { | |
| 161 | 17486 | const int ic = ic_base + ic_local; | |
| 162 | 17486 | const int w_ic_base = w_oc_base + ic_local * kT * kH * kW; | |
| 163 | 17486 | const int x_ic_base = (n * C_in + ic) * T * H * W; | |
| 164 | 40223 | for (int kt = 0; kt < kT; ++kt) { | |
| 165 | 22737 | const int in_t = in_t_origin + kt * dil_t; | |
| 166 | 22737 | const int x_t_base = x_ic_base + in_t * H * W; | |
| 167 | 22737 | const int w_t_base = w_ic_base + kt * kH * kW; | |
| 168 | 62178 | for (int kh = 0; kh < kH; ++kh) { | |
| 169 | 39441 | const int in_h = in_h_origin + kh * dil_h; | |
| 170 | 39441 | const int x_row_base = x_t_base + in_h * W; | |
| 171 | 39441 | const int w_row_base = w_t_base + kh * kW; | |
| 172 | 148148 | for (int kw = 0; kw < kW; ++kw) { | |
| 173 | 108707 | const int in_w = in_w_origin + kw * dil_w; | |
| 174 | 108707 | acc += Xp[x_row_base + in_w] * Wp[w_row_base + kw]; | |
| 175 | 108707 | } | |
| 176 | 39441 | } | |
| 177 | 22737 | } | |
| 178 | 17486 | } | |
| 179 | 4789 | return acc; | |
| 180 | }; | ||
| 181 | |||
| 182 | 688 | for (int ot = 0; ot < T_out; ++ot) { | |
| 183 | 511 | const bool ot_interior = has_interior && ot >= ot_lo && ot <= ot_hi; | |
| 184 | 511 | const int t_base = ((n * C_out + oc) * T_out + ot) * H_out; | |
| 185 | 511 | if (!ot_interior) { | |
| 186 | 1060 | for (int oh = 0; oh < H_out; ++oh) { | |
| 187 | 872 | const int y_row_base = (t_base + oh) * W_out; | |
| 188 | 5071 | for (int ow = 0; ow < W_out; ++ow) { | |
| 189 | 4199 | Yp[y_row_base + ow] = compute_bordered(ot, oh, ow) + bias_v; | |
| 190 | 4199 | } | |
| 191 | 872 | } | |
| 192 | 188 | continue; | |
| 193 | } | ||
| 194 | 1734 | for (int oh = 0; oh < H_out; ++oh) { | |
| 195 | 1411 | const int y_row_base = (t_base + oh) * W_out; | |
| 196 | 1411 | const bool oh_interior = oh >= oh_lo && oh <= oh_hi; | |
| 197 | 1411 | if (!oh_interior) { | |
| 198 | 1428 | for (int ow = 0; ow < W_out; ++ow) { | |
| 199 | 1180 | Yp[y_row_base + ow] = compute_bordered(ot, oh, ow) + bias_v; | |
| 200 | 1180 | } | |
| 201 | 248 | continue; | |
| 202 | } | ||
| 203 | 1495 | for (int ow = 0; ow < ow_lo; ++ow) { | |
| 204 | 332 | Yp[y_row_base + ow] = compute_bordered(ot, oh, ow) + bias_v; | |
| 205 | 332 | } | |
| 206 | 5952 | for (int ow = ow_lo; ow <= ow_hi; ++ow) { | |
| 207 | 4789 | Yp[y_row_base + ow] = compute_interior(ot, oh, ow) + bias_v; | |
| 208 | 4789 | } | |
| 209 | 1487 | for (int ow = ow_hi + 1; ow < W_out; ++ow) { | |
| 210 | 324 | Yp[y_row_base + ow] = compute_bordered(ot, oh, ow) + bias_v; | |
| 211 | 324 | } | |
| 212 | 1163 | } | |
| 213 | 323 | } | |
| 214 | 177 | } | |
| 215 | 35 | }); | |
| 216 | 24 | } | |
| 217 | |||
| 218 | } // namespace brotensor::detail::cpu | ||
| 219 |