brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
155 / 167
92.8%
Branches:
92 / 184
50.0%
Functions:
5 / 5
100.0%

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
1/2
✓ Branch 0 taken 261 times.
✗ Branch 1 not taken.
261 if (t.dtype != Dtype::FP32) {
58
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 + ": " +
59
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (N < 0 || C_in < 1 || H < 1 || Wd < 1 || C_out < 1 || kH < 1 || kW < 1)
89
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_forward: invalid dimensions");
90 24 const int wk = C_in * kH * kW;
91
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (X.rows != N || X.cols != C_in * H * Wd)
92
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_forward: X shape mismatch");
93
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (W.rows != C_out || W.cols != wk)
94
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_forward: W shape mismatch");
95
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (s.rows != N || s.cols != C_in)
96
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (H_out <= 0 || W_out <= 0)
100
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_forward: non-positive output shape");
101
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
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
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
24 if (Y.rows != N || Y.cols != out_cols || Y.dtype != Dtype::FP32)
105 24 Y.resize(N, out_cols, Dtype::FP32);
106
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 24 times.
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
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 float* Wnp = Wn.host_f32_mut();
114
115
2/2
✓ Branch 0 taken 41 times.
✓ Branch 1 taken 24 times.
65 for (int n = 0; n < N; ++n) {
116 41 const float* sn = sp + static_cast<size_t>(n) * C_in;
117
2/2
✓ Branch 0 taken 971 times.
✓ Branch 1 taken 41 times.
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
2/2
✓ Branch 0 taken 34878 times.
✓ Branch 1 taken 971 times.
35849 for (int i = 0; i < C_in; ++i) {
123 34878 const float si = sn[i];
124
2/2
✓ Branch 0 taken 36666 times.
✓ Branch 1 taken 34878 times.
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
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 482 times.
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
4/4
✓ Branch 0 taken 482 times.
✓ Branch 1 taken 489 times.
✓ Branch 2 taken 18582 times.
✓ Branch 3 taken 489 times.
19553 if (demodulate) for (int t = 0; t < wk; ++t) Wno[t] *= d;
134 971 }
135
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 ::brotensor::Tensor Xn = row_view(X, n, C_in * H * Wd);
136
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 ::brotensor::Tensor Yn = row_view_mut(Y, n, out_cols);
137
2/4
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 41 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (N < 0 || C_in < 1 || H < 1 || Wd < 1 || C_out < 1 || kH < 1 || kW < 1)
160
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 1 time.
33 if (H_out <= 0 || W_out <= 0)
165
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (X.rows != N || X.cols != C_in * H * Wd)
168
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_backward: X shape mismatch");
169
1/2
✓ Branch 0 taken 32 times.
✗ Branch 1 not taken.
32 if (W.rows != C_out || W.cols != wk)
170
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_backward: W shape mismatch");
171
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 1 time.
32 if (s.rows != N || s.cols != C_in)
172
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 throw std::runtime_error("modulated_conv2d_backward: s shape mismatch");
173
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 1 time.
31 if (dcoef.rows != N || dcoef.cols != C_out)
174
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 throw std::runtime_error("modulated_conv2d_backward: dcoef shape mismatch");
175
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 if (dY.rows != N || dY.cols != out_cols)
176
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 24 times.
30 if (want_dW) {
182 24 check_fp32(dW, "modulated_conv2d_backward", "dW");
183
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (dW.rows != C_out || dW.cols != wk)
184
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("modulated_conv2d_backward: dW shape mismatch");
185 24 }
186
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
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
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
30 if (ds.rows != N || ds.cols != C_in || ds.dtype != Dtype::FP32)
189 30 ds.resize(N, C_in, Dtype::FP32);
190
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
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
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 ::brotensor::Tensor Wpr = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk); // w'
200
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 ::brotensor::Tensor dWpp = ::brotensor::Tensor::zeros_on(Device::CPU, C_out, wk);
201
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 std::vector<float> dWpr(static_cast<size_t>(C_out) * wk); // dw'
202
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 float* Wppp = Wpp.host_f32_mut();
203
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 float* Wprp = Wpr.host_f32_mut();
204
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 float* dWppp = dWpp.host_f32_mut();
205
206
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 30 times.
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
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 53 times.
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
2/2
✓ Branch 0 taken 35022 times.
✓ Branch 1 taken 1019 times.
36041 for (int i = 0; i < C_in; ++i) {
214 35022 const float si = sn[i];
215
2/2
✓ Branch 0 taken 37578 times.
✓ Branch 1 taken 35022 times.
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
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 ::brotensor::Tensor Xn = row_view(X, n, C_in * H * Wd);
224
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 ::brotensor::Tensor dYn = row_view(dY, n, out_cols);
225 // dw'' (conv2d_backward_weight accumulates → zero first), and dX[n].
226
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 dWpp.zero();
227
2/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
53 ::brotensor::Tensor dXn = row_view_mut(dX, n, C_in * H * Wd);
230
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 53 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 53 times.
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
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 53 times.
1072 for (int o = 0; o < C_out; ++o) {
234 1019 const size_t ob = static_cast<size_t>(o) * wk;
235
2/2
✓ Branch 0 taken 513 times.
✓ Branch 1 taken 506 times.
1019 if (demodulate) {
236 513 const float d = dcn[o];
237 513 double g = 0.0;
238
2/2
✓ Branch 0 taken 19038 times.
✓ Branch 1 taken 513 times.
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
2/2
✓ Branch 0 taken 19038 times.
✓ Branch 1 taken 513 times.
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
2/2
✓ Branch 0 taken 18540 times.
✓ Branch 1 taken 506 times.
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
2/2
✓ Branch 0 taken 603 times.
✓ Branch 1 taken 53 times.
656 for (int i = 0; i < C_in; ++i) dsn[i] = 0.0f;
250
2/2
✓ Branch 0 taken 1019 times.
✓ Branch 1 taken 53 times.
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
2/2
✓ Branch 0 taken 35022 times.
✓ Branch 1 taken 1019 times.
36041 for (int i = 0; i < C_in; ++i) {
254 35022 const float si = sn[i];
255 35022 double ds_acc = 0.0;
256
2/2
✓ Branch 0 taken 37578 times.
✓ Branch 1 taken 35022 times.
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
2/2
✓ Branch 0 taken 912 times.
✓ Branch 1 taken 36666 times.
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