src/cpu/modulated_conv2d.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ─── CPU modulated_conv2d (StyleGAN3) ─────────────────────────────────────── | ||
| 2 | // | ||
| 3 | // The StyleGAN synthesis-layer core: per-sample style modulation of the conv | ||
| 4 | // weights, optional demodulation, then a standard stride-1 conv per sample. | ||
| 5 | // FP32 reference mirroring NVlabs `modulated_conv2d` (the pure-PyTorch path). | ||
| 6 | // | ||
| 7 | // Realized by looping the batch and reusing the validated CPU conv2d kernels | ||
| 8 | // (groups=1) on a per-sample weight — only the weight construction + demod is | ||
| 9 | // new. Layouts: X (N,C_in*H*W) NCHW; W (C_out, C_in*kH*kW) OIHW; s (N,C_in). | ||
| 10 | // | ||
| 11 | // w'[o,i,kh,kw] = W[o,i,kh,kw] * s[n,i] | ||
| 12 | // dcoef[n,o] = demodulate ? rsqrt(Σ_{i,kh,kw} w'^2 + eps) : 1 | ||
| 13 | // w'' = w' * dcoef[n,o] | ||
| 14 | // Y[n] = conv2d(X[n], w'', pad, stride=1) | ||
| 15 | // | ||
| 16 | // Backward (per n; dw'' = conv2d_backward_weight(X[n],dY[n])): | ||
| 17 | // g[o] = Σ dw''[o,..] * w'[o,..] | ||
| 18 | // dw'[o] = demodulate ? dw''[o]*dcoef - g[o]*dcoef^3*w'[o] : dw''[o] | ||
| 19 | // dW[o] += Σ_n dw'[n,o] * s[n,i] (accumulate — caller zeros dW) | ||
| 20 | // ds[n,i] = Σ_{o,kh,kw} dw'[o,i,kh,kw] * W[o,i,kh,kw] (overwrite) | ||
| 21 | // dX[n] = conv2d_backward_input(w''[n], dY[n]) (overwrite) | ||
| 22 | |||
| 23 | #include <brotensor/tensor.h> | ||
| 24 | |||
| 25 | #include <cmath> | ||
| 26 | #include <stdexcept> | ||
| 27 | #include <string> | ||
| 28 | #include <vector> | ||
| 29 | |||
| 30 | namespace brotensor::detail::cpu { | ||
| 31 | |||
| 32 | // Reused CPU conv2d kernels (defined in conv2d.cpp, same namespace). | ||
| 33 | void conv2d_forward(const ::brotensor::Tensor& X, const ::brotensor::Tensor& Wt, | ||
| 34 | const ::brotensor::Tensor* bias, | ||
| 35 | int N, int C_in, int H, int W, int C_out, int kH, int kW, | ||
| 36 | int stride_h, int stride_w, int pad_h, int pad_w, | ||
| 37 | int dil_h, int dil_w, int groups, ::brotensor::Tensor& Y); | ||
| 38 | void conv2d_backward_input(const ::brotensor::Tensor& Wt, | ||
| 39 | const ::brotensor::Tensor& dY, | ||
| 40 | int N, int C_in, int H, int W, | ||
| 41 | int C_out, int kH, int kW, | ||
| 42 | int stride_h, int stride_w, int pad_h, int pad_w, | ||
| 43 | int dil_h, int dil_w, int groups, | ||
| 44 | ::brotensor::Tensor& dX); | ||
| 45 | void conv2d_backward_weight(const ::brotensor::Tensor& X, | ||
| 46 | const ::brotensor::Tensor& dY, | ||
| 47 | int N, int C_in, int H, int W, | ||
| 48 | int C_out, int kH, int kW, | ||
| 49 | int stride_h, int stride_w, int pad_h, int pad_w, | ||
| 50 | int dil_h, int dil_w, int groups, | ||
| 51 | ::brotensor::Tensor& dWt); | ||
| 52 | |||
| 53 | namespace { | ||
| 54 | |||
| 55 | 261 | inline void check_fp32(const ::brotensor::Tensor& t, | |
| 56 | const char* op, const char* name) { | ||
| 57 | 261 | if (t.dtype != Dtype::FP32) { | |
| 58 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": " + | |
| 59 | ✗ | name + " must be FP32 (CPU backend is " | |
| 60 | "FP32-only)"); | ||
| 61 | } | ||
| 62 | 261 | } | |
| 63 | |||
| 64 | // Non-owning CPU FP32 view over one row of a (N, cols) tensor. | ||
| 65 | 147 | inline ::brotensor::Tensor row_view(const ::brotensor::Tensor& T, int n, int cols) { | |
| 66 | 147 | float* base = const_cast<float*>(T.host_f32()) + static_cast<size_t>(n) * cols; | |
| 67 | 147 | return ::brotensor::Tensor::view(Device::CPU, base, 1, cols, Dtype::FP32); | |
| 68 | } | ||
| 69 | 94 | inline ::brotensor::Tensor row_view_mut(::brotensor::Tensor& T, int n, int cols) { | |
| 70 | 94 | float* base = T.host_f32_mut() + static_cast<size_t>(n) * cols; | |
| 71 | 94 | return ::brotensor::Tensor::view(Device::CPU, base, 1, cols, Dtype::FP32); | |
| 72 | } | ||
| 73 | |||
| 74 | } // namespace | ||
| 75 | |||
| 76 | 24 | void modulated_conv2d_forward(const ::brotensor::Tensor& X, | |
| 77 | const ::brotensor::Tensor& W, | ||
| 78 | const ::brotensor::Tensor& s, | ||
| 79 | int N, int C_in, int H, int Wd, | ||
| 80 | int C_out, int kH, int kW, | ||
| 81 | int pad_h, int pad_w, | ||
| 82 | bool demodulate, float eps, | ||
| 83 | ::brotensor::Tensor& dcoef, | ||
| 84 | ::brotensor::Tensor& Y) { | ||
| 85 | 24 | check_fp32(X, "modulated_conv2d_forward", "X"); | |
| 86 | 24 | check_fp32(W, "modulated_conv2d_forward", "W"); | |
| 87 | 24 | check_fp32(s, "modulated_conv2d_forward", "s"); | |
| 88 | 24 | if (N < 0 || C_in < 1 || H < 1 || Wd < 1 || C_out < 1 || kH < 1 || kW < 1) | |
| 89 | ✗ | throw std::runtime_error("modulated_conv2d_forward: invalid dimensions"); | |
| 90 | 24 | const int wk = C_in * kH * kW; | |
| 91 | 24 | if (X.rows != N || X.cols != C_in * H * Wd) | |
| 92 | ✗ | throw std::runtime_error("modulated_conv2d_forward: X shape mismatch"); | |
| 93 | 24 | if (W.rows != C_out || W.cols != wk) | |
| 94 | ✗ | throw std::runtime_error("modulated_conv2d_forward: W shape mismatch"); | |
| 95 | 24 | if (s.rows != N || s.cols != C_in) | |
| 96 | ✗ | throw std::runtime_error("modulated_conv2d_forward: s shape mismatch"); | |
| 97 | 24 | const int H_out = H + 2 * pad_h - (kH - 1); | |
| 98 | 24 | const int W_out = Wd + 2 * pad_w - (kW - 1); | |
| 99 | 24 | if (H_out <= 0 || W_out <= 0) | |
| 100 | ✗ | throw std::runtime_error("modulated_conv2d_forward: non-positive output shape"); | |
| 101 | 24 | if (dcoef.rows != N || dcoef.cols != C_out || dcoef.dtype != Dtype::FP32) | |
| 102 | 24 | dcoef.resize(N, C_out, Dtype::FP32); | |
| 103 | 24 | const int out_cols = C_out * H_out * W_out; | |
| 104 | 24 | if (Y.rows != N || Y.cols != out_cols || Y.dtype != Dtype::FP32) | |
| 105 | 24 | Y.resize(N, out_cols, Dtype::FP32); | |
| 106 | 24 | if (N == 0 || out_cols == 0) return; | |
| 107 | |||
| 108 | 24 | const float* Wp = W.host_f32(); | |
| 109 | 24 | const float* sp = s.host_f32(); | |
| 110 | 24 | float* dcp = dcoef.host_f32_mut(); | |
| 111 | |||
| 112 | 24 | ::brotensor::Tensor Wn = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk); | |
| 113 | 24 | float* Wnp = Wn.host_f32_mut(); | |
| 114 | |||
| 115 | 65 | for (int n = 0; n < N; ++n) { | |
| 116 | 41 | const float* sn = sp + static_cast<size_t>(n) * C_in; | |
| 117 | 1012 | for (int o = 0; o < C_out; ++o) { | |
| 118 | 971 | const float* Wo = Wp + static_cast<size_t>(o) * wk; | |
| 119 | 971 | float* Wno = Wnp + static_cast<size_t>(o) * wk; | |
| 120 | // w'[o,..] = W[o,..]*s[n,i]; demod coefficient from its norm. | ||
| 121 | 971 | double ss = 0.0; | |
| 122 | 35849 | for (int i = 0; i < C_in; ++i) { | |
| 123 | 34878 | const float si = sn[i]; | |
| 124 | 71544 | for (int t = 0; t < kH * kW; ++t) { | |
| 125 | 36666 | const float wp = Wo[i * kH * kW + t] * si; | |
| 126 | 36666 | Wno[i * kH * kW + t] = wp; | |
| 127 | 36666 | ss += static_cast<double>(wp) * wp; | |
| 128 | 36666 | } | |
| 129 | 34878 | } | |
| 130 | 971 | const float d = demodulate | |
| 131 | 489 | ? 1.0f / std::sqrt(static_cast<float>(ss) + eps) : 1.0f; | |
| 132 | 971 | dcp[static_cast<size_t>(n) * C_out + o] = d; | |
| 133 | 19553 | if (demodulate) for (int t = 0; t < wk; ++t) Wno[t] *= d; | |
| 134 | 971 | } | |
| 135 | 41 | ::brotensor::Tensor Xn = row_view(X, n, C_in * H * Wd); | |
| 136 | 41 | ::brotensor::Tensor Yn = row_view_mut(Y, n, out_cols); | |
| 137 | 82 | conv2d_forward(Xn, Wn, nullptr, 1, C_in, H, Wd, C_out, kH, kW, | |
| 138 | 41 | 1, 1, pad_h, pad_w, 1, 1, 1, Yn); | |
| 139 | 41 | } | |
| 140 | 24 | } | |
| 141 | |||
| 142 | 33 | void modulated_conv2d_backward(const ::brotensor::Tensor& X, | |
| 143 | const ::brotensor::Tensor& W, | ||
| 144 | const ::brotensor::Tensor& s, | ||
| 145 | const ::brotensor::Tensor& dcoef, | ||
| 146 | const ::brotensor::Tensor& dY, | ||
| 147 | int N, int C_in, int H, int Wd, | ||
| 148 | int C_out, int kH, int kW, | ||
| 149 | int pad_h, int pad_w, bool demodulate, float eps, | ||
| 150 | ::brotensor::Tensor& dX, | ||
| 151 | ::brotensor::Tensor& dW, | ||
| 152 | ::brotensor::Tensor& ds) { | ||
| 153 | 33 | check_fp32(X, "modulated_conv2d_backward", "X"); | |
| 154 | 33 | check_fp32(W, "modulated_conv2d_backward", "W"); | |
| 155 | 33 | check_fp32(s, "modulated_conv2d_backward", "s"); | |
| 156 | 33 | check_fp32(dcoef, "modulated_conv2d_backward", "dcoef"); | |
| 157 | 33 | check_fp32(dY, "modulated_conv2d_backward", "dY"); | |
| 158 | (void)eps; // demod coefficient is precomputed (passed in as dcoef) | ||
| 159 | 33 | if (N < 0 || C_in < 1 || H < 1 || Wd < 1 || C_out < 1 || kH < 1 || kW < 1) | |
| 160 | ✗ | throw std::runtime_error("modulated_conv2d_backward: invalid dimensions"); | |
| 161 | 33 | const int wk = C_in * kH * kW; | |
| 162 | 33 | const int H_out = H + 2 * pad_h - (kH - 1); | |
| 163 | 33 | const int W_out = Wd + 2 * pad_w - (kW - 1); | |
| 164 | 33 | if (H_out <= 0 || W_out <= 0) | |
| 165 | 1 | throw std::runtime_error("modulated_conv2d_backward: non-positive output shape"); | |
| 166 | 32 | const int out_cols = C_out * H_out * W_out; | |
| 167 | 32 | if (X.rows != N || X.cols != C_in * H * Wd) | |
| 168 | ✗ | throw std::runtime_error("modulated_conv2d_backward: X shape mismatch"); | |
| 169 | 32 | if (W.rows != C_out || W.cols != wk) | |
| 170 | ✗ | throw std::runtime_error("modulated_conv2d_backward: W shape mismatch"); | |
| 171 | 32 | if (s.rows != N || s.cols != C_in) | |
| 172 | 1 | throw std::runtime_error("modulated_conv2d_backward: s shape mismatch"); | |
| 173 | 31 | if (dcoef.rows != N || dcoef.cols != C_out) | |
| 174 | 1 | throw std::runtime_error("modulated_conv2d_backward: dcoef shape mismatch"); | |
| 175 | 30 | if (dY.rows != N || dY.cols != out_cols) | |
| 176 | ✗ | throw std::runtime_error("modulated_conv2d_backward: dY shape mismatch"); | |
| 177 | // dW is an optional output: an uncommitted (data == nullptr) dW means | ||
| 178 | // "skip the weight gradient" (mirrors the CUDA backend — used by inversion, | ||
| 179 | // which freezes the weights). When committed it must be FP32 and shaped. | ||
| 180 | 30 | const bool want_dW = (dW.data != nullptr); | |
| 181 | 30 | if (want_dW) { | |
| 182 | 24 | check_fp32(dW, "modulated_conv2d_backward", "dW"); | |
| 183 | 24 | if (dW.rows != C_out || dW.cols != wk) | |
| 184 | ✗ | throw std::runtime_error("modulated_conv2d_backward: dW shape mismatch"); | |
| 185 | 24 | } | |
| 186 | 30 | if (dX.rows != N || dX.cols != C_in * H * Wd || dX.dtype != Dtype::FP32) | |
| 187 | 30 | dX.resize(N, C_in * H * Wd, Dtype::FP32); | |
| 188 | 30 | if (ds.rows != N || ds.cols != C_in || ds.dtype != Dtype::FP32) | |
| 189 | 30 | ds.resize(N, C_in, Dtype::FP32); | |
| 190 | 30 | if (N == 0) return; | |
| 191 | |||
| 192 | 30 | const float* Wp = W.host_f32(); | |
| 193 | 30 | const float* sp = s.host_f32(); | |
| 194 | 30 | const float* dcp = dcoef.host_f32(); | |
| 195 | 30 | float* dWp = want_dW ? dW.host_f32_mut() : nullptr; // accumulate (optional) | |
| 196 | 30 | float* dsp = ds.host_f32_mut(); // overwrite | |
| 197 | |||
| 198 | 30 | ::brotensor::Tensor Wpp = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk); // w'' | |
| 199 | 30 | ::brotensor::Tensor Wpr = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk); // w' | |
| 200 | 30 | ::brotensor::Tensor dWpp = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk); | |
| 201 | 30 | std::vector<float> dWpr(static_cast<size_t>(C_out) * wk); // dw' | |
| 202 | 30 | float* Wppp = Wpp.host_f32_mut(); | |
| 203 | 30 | float* Wprp = Wpr.host_f32_mut(); | |
| 204 | 30 | float* dWppp = dWpp.host_f32_mut(); | |
| 205 | |||
| 206 | 83 | for (int n = 0; n < N; ++n) { | |
| 207 | 53 | const float* sn = sp + static_cast<size_t>(n) * C_in; | |
| 208 | 53 | const float* dcn = dcp + static_cast<size_t>(n) * C_out; | |
| 209 | // Rebuild w' and w''. | ||
| 210 | 1072 | for (int o = 0; o < C_out; ++o) { | |
| 211 | 1019 | const float* Wo = Wp + static_cast<size_t>(o) * wk; | |
| 212 | 1019 | const float d = dcn[o]; | |
| 213 | 36041 | for (int i = 0; i < C_in; ++i) { | |
| 214 | 35022 | const float si = sn[i]; | |
| 215 | 72600 | for (int t = 0; t < kH * kW; ++t) { | |
| 216 | 37578 | const int col = i * kH * kW + t; | |
| 217 | 37578 | const float wp = Wo[col] * si; | |
| 218 | 37578 | Wprp[static_cast<size_t>(o) * wk + col] = wp; | |
| 219 | 37578 | Wppp[static_cast<size_t>(o) * wk + col] = wp * d; | |
| 220 | 37578 | } | |
| 221 | 35022 | } | |
| 222 | 1019 | } | |
| 223 | 53 | ::brotensor::Tensor Xn = row_view(X, n, C_in * H * Wd); | |
| 224 | 53 | ::brotensor::Tensor dYn = row_view(dY, n, out_cols); | |
| 225 | // dw'' (conv2d_backward_weight accumulates → zero first), and dX[n]. | ||
| 226 | 53 | dWpp.zero(); | |
| 227 | 106 | conv2d_backward_weight(Xn, dYn, 1, C_in, H, Wd, C_out, kH, kW, | |
| 228 | 53 | 1, 1, pad_h, pad_w, 1, 1, 1, dWpp); | |
| 229 | 53 | ::brotensor::Tensor dXn = row_view_mut(dX, n, C_in * H * Wd); | |
| 230 | 106 | conv2d_backward_input(Wpp, dYn, 1, C_in, H, Wd, C_out, kH, kW, | |
| 231 | 53 | 1, 1, pad_h, pad_w, 1, 1, 1, dXn); | |
| 232 | // Through demod → dw'. | ||
| 233 | 1072 | for (int o = 0; o < C_out; ++o) { | |
| 234 | 1019 | const size_t ob = static_cast<size_t>(o) * wk; | |
| 235 | 1019 | if (demodulate) { | |
| 236 | 513 | const float d = dcn[o]; | |
| 237 | 513 | double g = 0.0; | |
| 238 | 19551 | for (int t = 0; t < wk; ++t) | |
| 239 | 19038 | g += static_cast<double>(dWppp[ob + t]) * Wprp[ob + t]; | |
| 240 | 513 | const float gd3 = static_cast<float>(g) * d * d * d; | |
| 241 | 19551 | for (int t = 0; t < wk; ++t) | |
| 242 | 19038 | dWpr[ob + t] = dWppp[ob + t] * d - gd3 * Wprp[ob + t]; | |
| 243 | 513 | } else { | |
| 244 | 19046 | for (int t = 0; t < wk; ++t) dWpr[ob + t] = dWppp[ob + t]; | |
| 245 | } | ||
| 246 | 1019 | } | |
| 247 | // Accumulate dW and write ds[n]. | ||
| 248 | 53 | float* dsn = dsp + static_cast<size_t>(n) * C_in; | |
| 249 | 656 | for (int i = 0; i < C_in; ++i) dsn[i] = 0.0f; | |
| 250 | 1072 | for (int o = 0; o < C_out; ++o) { | |
| 251 | 1019 | const float* Wo = Wp + static_cast<size_t>(o) * wk; | |
| 252 | 1019 | const size_t ob = static_cast<size_t>(o) * wk; | |
| 253 | 36041 | for (int i = 0; i < C_in; ++i) { | |
| 254 | 35022 | const float si = sn[i]; | |
| 255 | 35022 | double ds_acc = 0.0; | |
| 256 | 72600 | for (int t = 0; t < kH * kW; ++t) { | |
| 257 | 37578 | const int col = i * kH * kW + t; | |
| 258 | 37578 | const float dwp = dWpr[ob + col]; | |
| 259 | 37578 | if (want_dW) dWp[ob + col] += dwp * si; // accumulate dW (optional) | |
| 260 | 37578 | ds_acc += static_cast<double>(dwp) * Wo[col]; | |
| 261 | 37578 | } | |
| 262 | 35022 | dsn[i] += static_cast<float>(ds_acc); // overwrite ds[n,i] | |
| 263 | 35022 | } | |
| 264 | 1019 | } | |
| 265 | 53 | } | |
| 266 | 30 | } | |
| 267 | |||
| 268 | } // namespace brotensor::detail::cpu | ||
| 269 |