brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
576 / 627
91.9%
Branches:
124 / 222
55.9%
Functions:
67 / 67
100.0%

src/ops_spatial.cpp
Line Branch Exec Source
1 // brotensor public op wrappers — 2D/3D spatial, conv, norm, pool, StyleGAN.
2
3 #include <brotensor/ops.h>
4 #include <brotensor/tensor.h>
5 #include <brotensor/detail/dispatch.h>
6
7 #include <stdexcept>
8
9 namespace brotensor {
10
11 // ─── Conv2d ────────────────────────────────────────────────────────────────
12
13 128 void conv2d_forward(const Tensor& X, const Tensor& Wt, const Tensor* bias,
14 int N, int C_in, int H, int W,
15 int C_out, int kH, int kW,
16 int stride_h, int stride_w,
17 int pad_h, int pad_w,
18 int dil_h, int dil_w,
19 int groups, Tensor& Y) {
20 128 BROTENSOR_CHECK_SPATIAL_2D("conv2d_forward", X, N, C_in, H, W);
21 128 const auto& v = detail::dispatch_with_opts(X, Wt, {bias, &Y});
22
1/2
✓ Branch 0 taken 128 times.
✗ Branch 1 not taken.
128 if (!v.conv2d_forward) detail::throw_not_implemented("conv2d_forward", X.device);
23 128 detail::adopt_output(Y, X.device);
24 256 v.conv2d_forward(X, Wt, bias, N, C_in, H, W, C_out, kH, kW,
25 128 stride_h, stride_w, pad_h, pad_w, dil_h, dil_w, groups, Y);
26 128 }
27
28 70 void conv2d_backward_input(const Tensor& Wt, const Tensor& dY,
29 int N, int C_in, int H, int W,
30 int C_out, int kH, int kW,
31 int stride_h, int stride_w,
32 int pad_h, int pad_w,
33 int dil_h, int dil_w,
34 int groups, Tensor& dX) {
35
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 if (N <= 0 || C_in <= 0 || H <= 0 || W <= 0 || C_out <= 0) {
36 ✗ detail::throw_spatial_2d_error("conv2d_backward_input", "dY",
37 ✗ N, C_in, H, W, dY.rows, dY.cols);
38 }
39
6/12
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 70 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 70 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 70 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 70 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 70 times.
70 if (kH > 0 && kW > 0 && stride_h > 0 && stride_w > 0 && dil_h > 0 && dil_w > 0) {
40 70 const int H_out = (H + 2 * pad_h - dil_h * (kH - 1) - 1) / stride_h + 1;
41 70 const int W_out = (W + 2 * pad_w - dil_w * (kW - 1) - 1) / stride_w + 1;
42
2/4
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 70 times.
70 if (H_out > 0 && W_out > 0) {
43 70 BROTENSOR_CHECK_SPATIAL_2D("conv2d_backward_input", dY, N, C_out, H_out, W_out);
44 70 }
45 70 }
46 70 const auto& v = detail::dispatch(Wt, dY, dX);
47
1/2
✓ Branch 0 taken 70 times.
✗ Branch 1 not taken.
70 if (!v.conv2d_backward_input) detail::throw_not_implemented("conv2d_backward_input", Wt.device);
48 70 detail::adopt_output(dX, Wt.device);
49 140 v.conv2d_backward_input(Wt, dY, N, C_in, H, W, C_out, kH, kW,
50 70 stride_h, stride_w, pad_h, pad_w, dil_h, dil_w, groups, dX);
51 70 }
52
53 60 void conv2d_backward_weight(const Tensor& X, const Tensor& dY,
54 int N, int C_in, int H, int W,
55 int C_out, int kH, int kW,
56 int stride_h, int stride_w,
57 int pad_h, int pad_w,
58 int dil_h, int dil_w,
59 int groups, Tensor& dWt) {
60 60 BROTENSOR_CHECK_SPATIAL_2D("conv2d_backward_weight", X, N, C_in, H, W);
61 60 const auto& v = detail::dispatch(X, dY, dWt);
62
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (!v.conv2d_backward_weight) detail::throw_not_implemented("conv2d_backward_weight", X.device);
63 60 detail::adopt_output(dWt, X.device);
64 120 v.conv2d_backward_weight(X, dY, N, C_in, H, W, C_out, kH, kW,
65 60 stride_h, stride_w, pad_h, pad_w, dil_h, dil_w, groups, dWt);
66 60 }
67
68 33 void conv2d_backward_bias(const Tensor& dY,
69 int N, int C_out, int H_out, int W_out,
70 Tensor& dB) {
71 33 BROTENSOR_CHECK_SPATIAL_2D("conv2d_backward_bias", dY, N, C_out, H_out, W_out);
72 33 const auto& v = detail::dispatch(dY, dB);
73
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (!v.conv2d_backward_bias) detail::throw_not_implemented("conv2d_backward_bias", dY.device);
74 33 detail::adopt_output(dB, dY.device);
75 33 v.conv2d_backward_bias(dY, N, C_out, H_out, W_out, dB);
76 33 }
77
78 11 void conv2d_int8w_fp16_forward(const Tensor& X,
79 const Tensor& W_int8, const Tensor& scales,
80 const Tensor* bias,
81 int N, int C_in, int H, int W,
82 int C_out, int kH, int kW,
83 int stride_h, int stride_w,
84 int pad_h, int pad_w,
85 int dil_h, int dil_w, int groups,
86 Tensor& Y) {
87 11 BROTENSOR_CHECK_SPATIAL_2D("conv2d_int8w_fp16_forward", X, N, C_in, H, W);
88 11 const auto& v = detail::dispatch_with_opts(X, W_int8, {&scales, bias, &Y});
89
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 time.
11 if (!v.conv2d_int8w_fp16_forward)
90 1 detail::throw_not_implemented("conv2d_int8w_fp16_forward", X.device);
91 10 detail::adopt_output(Y, X.device);
92 20 v.conv2d_int8w_fp16_forward(X, W_int8, scales, bias,
93 10 N, C_in, H, W, C_out, kH, kW,
94 10 stride_h, stride_w, pad_h, pad_w,
95 10 dil_h, dil_w, groups, Y);
96 10 }
97
98 18 void deform_conv2d_forward(const Tensor& X, const Tensor& offset,
99 const Tensor* mask, const Tensor& Wt,
100 const Tensor* bias,
101 int N, int C_in, int H, int W,
102 int C_out, int kH, int kW,
103 int stride_h, int stride_w,
104 int pad_h, int pad_w,
105 int dil_h, int dil_w,
106 int groups, int deform_groups, Tensor& Y) {
107 18 BROTENSOR_CHECK_SPATIAL_2D("deform_conv2d_forward", X, N, C_in, H, W);
108 18 const auto& v = detail::dispatch_with_opts(X, Wt, {&offset, mask, bias, &Y});
109
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (!v.deform_conv2d_forward) detail::throw_not_implemented("deform_conv2d_forward", X.device);
110 18 detail::adopt_output(Y, X.device);
111 36 v.deform_conv2d_forward(X, offset, mask, Wt, bias, N, C_in, H, W,
112 18 C_out, kH, kW, stride_h, stride_w, pad_h, pad_w,
113 18 dil_h, dil_w, groups, deform_groups, Y);
114 18 }
115
116 // ─── Conv3d ────────────────────────────────────────────────────────────────
117
118 45 void conv3d_forward(const Tensor& X, const Tensor& Wt, const Tensor* bias,
119 int N, int C_in, int T, int H, int W,
120 int C_out, int kT, int kH, int kW,
121 int stride_t, int stride_h, int stride_w,
122 int pad_t, int pad_h, int pad_w,
123 int dil_t, int dil_h, int dil_w,
124 int groups, Tensor& Y) {
125 45 BROTENSOR_CHECK_SPATIAL_3D("conv3d_forward", X, N, C_in, T, H, W);
126 45 const auto& v = detail::dispatch_with_opts(X, Wt, {bias, &Y});
127
1/2
✓ Branch 0 taken 45 times.
✗ Branch 1 not taken.
45 if (!v.conv3d_forward) detail::throw_not_implemented("conv3d_forward", X.device);
128 45 detail::adopt_output(Y, X.device);
129 90 v.conv3d_forward(X, Wt, bias, N, C_in, T, H, W, C_out, kT, kH, kW,
130 45 stride_t, stride_h, stride_w,
131 45 pad_t, pad_h, pad_w,
132 45 dil_t, dil_h, dil_w, groups, Y);
133 45 }
134
135 9 void conv3d_int8w_fp16_forward(const Tensor& X, const Tensor& W_int8,
136 const Tensor& scales, const Tensor* bias,
137 int N, int C_in, int T, int H, int W,
138 int C_out, int kT, int kH, int kW,
139 int stride_t, int stride_h, int stride_w,
140 int pad_t, int pad_h, int pad_w,
141 int dil_t, int dil_h, int dil_w,
142 int groups, Tensor& Y) {
143 9 BROTENSOR_CHECK_SPATIAL_3D("conv3d_int8w_fp16_forward", X, N, C_in, T, H, W);
144 9 const auto& v = detail::dispatch_with_opts(X, W_int8, {&scales, bias, &Y});
145
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 time.
9 if (!v.conv3d_int8w_fp16_forward)
146 1 detail::throw_not_implemented("conv3d_int8w_fp16_forward", X.device);
147 8 detail::adopt_output(Y, X.device);
148 16 v.conv3d_int8w_fp16_forward(X, W_int8, scales, bias,
149 8 N, C_in, T, H, W, C_out, kT, kH, kW,
150 8 stride_t, stride_h, stride_w,
151 8 pad_t, pad_h, pad_w,
152 8 dil_t, dil_h, dil_w, groups, Y);
153 8 }
154
155 // ─── GroupNorm ─────────────────────────────────────────────────────────────
156
157 41 void group_norm_forward(const Tensor& X, const Tensor& gamma, const Tensor& beta,
158 int N, int C, int H, int W, int num_groups,
159 float eps, Tensor& Y) {
160 41 BROTENSOR_CHECK_SPATIAL_2D("group_norm_forward", X, N, C, H, W);
161 41 const auto& v = detail::dispatch(X, gamma, beta, Y);
162
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (!v.group_norm_forward) detail::throw_not_implemented("group_norm_forward", X.device);
163 41 detail::adopt_output(Y, X.device);
164 41 v.group_norm_forward(X, gamma, beta, N, C, H, W, num_groups, eps, Y);
165 41 }
166
167 33 void group_norm_backward(const Tensor& X, const Tensor& gamma, const Tensor& dY,
168 int N, int C, int H, int W, int num_groups, float eps,
169 Tensor& dX, Tensor& dGamma, Tensor& dBeta) {
170 33 BROTENSOR_CHECK_SPATIAL_2D("group_norm_backward", X, N, C, H, W);
171 33 BROTENSOR_CHECK_SPATIAL_2D("group_norm_backward", dY, N, C, H, W);
172 33 const auto& v = detail::dispatch(X, gamma, dY, dX, dGamma, dBeta);
173
1/2
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
33 if (!v.group_norm_backward) detail::throw_not_implemented("group_norm_backward", X.device);
174 33 detail::adopt_output(dX, X.device);
175 33 detail::adopt_output(dGamma, X.device);
176 33 detail::adopt_output(dBeta, X.device);
177 66 v.group_norm_backward(X, gamma, dY, N, C, H, W, num_groups, eps,
178 33 dX, dGamma, dBeta);
179 33 }
180
181 // ─── Activations ───────────────────────────────────────────────────────────
182
183 575 void silu_forward(const Tensor& x, Tensor& y) {
184 575 const auto& v = detail::dispatch(x, y);
185
1/2
✓ Branch 0 taken 575 times.
✗ Branch 1 not taken.
575 if (!v.silu_forward) detail::throw_not_implemented("silu_forward", x.device);
186 575 detail::adopt_output(y, x.device);
187 575 v.silu_forward(x, y);
188 575 }
189 13 void silu_backward(const Tensor& x, const Tensor& dY, Tensor& dX) {
190 13 const auto& v = detail::dispatch(x, dY, dX);
191
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 if (!v.silu_backward) detail::throw_not_implemented("silu_backward", x.device);
192 13 detail::adopt_output(dX, x.device);
193 13 v.silu_backward(x, dY, dX);
194 13 }
195 12 void gelu_forward(const Tensor& x, Tensor& y) {
196 12 const auto& v = detail::dispatch(x, y);
197
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.gelu_forward) detail::throw_not_implemented("gelu_forward", x.device);
198 12 detail::adopt_output(y, x.device);
199 12 v.gelu_forward(x, y);
200 12 }
201 9 void gelu_backward(const Tensor& x, const Tensor& dY, Tensor& dX) {
202 9 const auto& v = detail::dispatch(x, dY, dX);
203
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.gelu_backward) detail::throw_not_implemented("gelu_backward", x.device);
204 9 detail::adopt_output(dX, x.device);
205 9 v.gelu_backward(x, dY, dX);
206 9 }
207 10 void gelu_exact_forward(const Tensor& x, Tensor& y) {
208 10 const auto& v = detail::dispatch(x, y);
209
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.gelu_exact_forward) detail::throw_not_implemented("gelu_exact_forward", x.device);
210 10 detail::adopt_output(y, x.device);
211 10 v.gelu_exact_forward(x, y);
212 10 }
213 9 void gelu_exact_backward(const Tensor& x, const Tensor& dY, Tensor& dX) {
214 9 const auto& v = detail::dispatch(x, dY, dX);
215
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.gelu_exact_backward) detail::throw_not_implemented("gelu_exact_backward", x.device);
216 9 detail::adopt_output(dX, x.device);
217 9 v.gelu_exact_backward(x, dY, dX);
218 9 }
219 10 void quick_gelu_forward(const Tensor& x, Tensor& y) {
220 10 const auto& v = detail::dispatch(x, y);
221
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.quick_gelu_forward) detail::throw_not_implemented("quick_gelu_forward", x.device);
222 10 detail::adopt_output(y, x.device);
223 10 v.quick_gelu_forward(x, y);
224 10 }
225 9 void quick_gelu_backward(const Tensor& x, const Tensor& dY, Tensor& dX) {
226 9 const auto& v = detail::dispatch(x, dY, dX);
227
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.quick_gelu_backward) detail::throw_not_implemented("quick_gelu_backward", x.device);
228 9 detail::adopt_output(dX, x.device);
229 9 v.quick_gelu_backward(x, dY, dX);
230 9 }
231
232 // ─── Resample ──────────────────────────────────────────────────────────────
233
234 34 void upsample_nearest_2x(const Tensor& X, int N, int C, int H, int W, Tensor& Y) {
235 34 BROTENSOR_CHECK_SPATIAL_2D("upsample_nearest_2x", X, N, C, H, W);
236 34 const auto& v = detail::dispatch(X, Y);
237
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (!v.upsample_nearest_2x) detail::throw_not_implemented("upsample_nearest_2x", X.device);
238 34 detail::adopt_output(Y, X.device);
239 34 v.upsample_nearest_2x(X, N, C, H, W, Y);
240 34 }
241 31 void upsample_bilinear_2x(const Tensor& X, int N, int C, int H, int W, Tensor& Y) {
242 31 BROTENSOR_CHECK_SPATIAL_2D("upsample_bilinear_2x", X, N, C, H, W);
243 31 const auto& v = detail::dispatch(X, Y);
244
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (!v.upsample_bilinear_2x) detail::throw_not_implemented("upsample_bilinear_2x", X.device);
245 31 detail::adopt_output(Y, X.device);
246 31 v.upsample_bilinear_2x(X, N, C, H, W, Y);
247 31 }
248 31 void downsample_avg_2x(const Tensor& X, int N, int C, int H, int W, Tensor& Y) {
249 31 BROTENSOR_CHECK_SPATIAL_2D("downsample_avg_2x", X, N, C, H, W);
250 31 const auto& v = detail::dispatch(X, Y);
251
1/2
✓ Branch 0 taken 31 times.
✗ Branch 1 not taken.
31 if (!v.downsample_avg_2x) detail::throw_not_implemented("downsample_avg_2x", X.device);
252 31 detail::adopt_output(Y, X.device);
253 31 v.downsample_avg_2x(X, N, C, H, W, Y);
254 31 }
255 28 void upsample_nearest_2x_backward(const Tensor& dY, int N, int C, int H, int W, Tensor& dX) {
256
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
257 ✗ detail::throw_spatial_2d_error("upsample_nearest_2x_backward", "dY",
258 ✗ N, C, H, W, dY.rows, dY.cols);
259 }
260 28 BROTENSOR_CHECK_SPATIAL_2D("upsample_nearest_2x_backward", dY, N, C, 2 * H, 2 * W);
261 28 const auto& v = detail::dispatch(dY, dX);
262
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!v.upsample_nearest_2x_backward) detail::throw_not_implemented("upsample_nearest_2x_backward", dY.device);
263 28 detail::adopt_output(dX, dY.device);
264 28 v.upsample_nearest_2x_backward(dY, N, C, H, W, dX);
265 28 }
266 28 void upsample_bilinear_2x_backward(const Tensor& dY, int N, int C, int H, int W, Tensor& dX) {
267
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
268 ✗ detail::throw_spatial_2d_error("upsample_bilinear_2x_backward", "dY",
269 ✗ N, C, H, W, dY.rows, dY.cols);
270 }
271 28 BROTENSOR_CHECK_SPATIAL_2D("upsample_bilinear_2x_backward", dY, N, C, 2 * H, 2 * W);
272 28 const auto& v = detail::dispatch(dY, dX);
273
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!v.upsample_bilinear_2x_backward) detail::throw_not_implemented("upsample_bilinear_2x_backward", dY.device);
274 28 detail::adopt_output(dX, dY.device);
275 28 v.upsample_bilinear_2x_backward(dY, N, C, H, W, dX);
276 28 }
277 28 void downsample_avg_2x_backward(const Tensor& dY, int N, int C, int H, int W, Tensor& dX) {
278
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
279 ✗ detail::throw_spatial_2d_error("downsample_avg_2x_backward", "dY",
280 ✗ N, C, H, W, dY.rows, dY.cols);
281 }
282
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
28 if (H % 2 == 0 && W % 2 == 0) {
283 28 BROTENSOR_CHECK_SPATIAL_2D("downsample_avg_2x_backward", dY, N, C, H / 2, W / 2);
284 28 }
285 28 const auto& v = detail::dispatch(dY, dX);
286
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 if (!v.downsample_avg_2x_backward) detail::throw_not_implemented("downsample_avg_2x_backward", dY.device);
287 28 detail::adopt_output(dX, dY.device);
288 28 v.downsample_avg_2x_backward(dY, N, C, H, W, dX);
289 28 }
290
291 110 void interp2d_forward(const Tensor& X,
292 int N, int C, int H_in, int W_in, int H_out, int W_out,
293 int mode, Tensor& Y) {
294 110 BROTENSOR_CHECK_SPATIAL_2D("interp2d_forward", X, N, C, H_in, W_in);
295 110 const auto& v = detail::dispatch(X, Y);
296
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
110 if (!v.interp2d_forward) detail::throw_not_implemented("interp2d_forward", X.device);
297 110 detail::adopt_output(Y, X.device);
298 110 v.interp2d_forward(X, N, C, H_in, W_in, H_out, W_out, mode, Y);
299 110 }
300 41 void interp2d_backward(const Tensor& dY,
301 int N, int C, int H_in, int W_in, int H_out, int W_out,
302 int mode, Tensor& dX) {
303
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (N <= 0 || C <= 0 || H_in <= 0 || W_in <= 0) {
304 ✗ detail::throw_spatial_2d_error("interp2d_backward", "dY",
305 ✗ N, C, H_in, W_in, dY.rows, dY.cols);
306 }
307 41 BROTENSOR_CHECK_SPATIAL_2D("interp2d_backward", dY, N, C, H_out, W_out);
308 41 const auto& v = detail::dispatch(dY, dX);
309
1/2
✓ Branch 0 taken 41 times.
✗ Branch 1 not taken.
41 if (!v.interp2d_backward) detail::throw_not_implemented("interp2d_backward", dY.device);
310 41 detail::adopt_output(dX, dY.device);
311 41 v.interp2d_backward(dY, N, C, H_in, W_in, H_out, W_out, mode, dX);
312 41 }
313 22 void interp2d_align_corners_forward(const Tensor& X,
314 int N, int C, int H_in, int W_in,
315 int H_out, int W_out, int mode, Tensor& Y) {
316 22 BROTENSOR_CHECK_SPATIAL_2D("interp2d_align_corners_forward", X, N, C, H_in, W_in);
317 22 const auto& v = detail::dispatch(X, Y);
318
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!v.interp2d_align_corners_forward)
319 ✗ detail::throw_not_implemented("interp2d_align_corners_forward", X.device);
320 22 detail::adopt_output(Y, X.device);
321 22 v.interp2d_align_corners_forward(X, N, C, H_in, W_in, H_out, W_out, mode, Y);
322 22 }
323
324 101 void pad2d_forward(const Tensor& X, int N, int C, int H, int W,
325 int pad_top, int pad_bottom, int pad_left, int pad_right,
326 int mode, Tensor& Y) {
327 101 BROTENSOR_CHECK_SPATIAL_2D("pad2d_forward", X, N, C, H, W);
328 101 const auto& v = detail::dispatch(X, Y);
329
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 if (!v.pad2d_forward) detail::throw_not_implemented("pad2d_forward", X.device);
330 101 detail::adopt_output(Y, X.device);
331 101 v.pad2d_forward(X, N, C, H, W, pad_top, pad_bottom, pad_left, pad_right, mode, Y);
332 101 }
333 49 void pad2d_backward(const Tensor& dY, int N, int C, int H, int W,
334 int pad_top, int pad_bottom, int pad_left, int pad_right,
335 int mode, Tensor& dX) {
336
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
337 ✗ detail::throw_spatial_2d_error("pad2d_backward", "dY",
338 ✗ N, C, H, W, dY.rows, dY.cols);
339 }
340 49 const int H_out = H + pad_top + pad_bottom;
341 49 const int W_out = W + pad_left + pad_right;
342
2/4
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 49 times.
49 if (H_out > 0 && W_out > 0) {
343 49 BROTENSOR_CHECK_SPATIAL_2D("pad2d_backward", dY, N, C, H_out, W_out);
344 49 }
345 49 const auto& v = detail::dispatch(dY, dX);
346
1/2
✓ Branch 0 taken 49 times.
✗ Branch 1 not taken.
49 if (!v.pad2d_backward) detail::throw_not_implemented("pad2d_backward", dY.device);
347 49 detail::adopt_output(dX, dY.device);
348 49 v.pad2d_backward(dY, N, C, H, W, pad_top, pad_bottom, pad_left, pad_right, mode, dX);
349 49 }
350
351 16 void slice2d_forward(const Tensor& X, int N, int C, int H, int W,
352 int h0, int w0, int H_out, int W_out, Tensor& Y) {
353 16 BROTENSOR_CHECK_SPATIAL_2D("slice2d_forward", X, N, C, H, W);
354 16 const auto& v = detail::dispatch(X, Y);
355
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!v.slice2d_forward) detail::throw_not_implemented("slice2d_forward", X.device);
356 16 detail::adopt_output(Y, X.device);
357 16 v.slice2d_forward(X, N, C, H, W, h0, w0, H_out, W_out, Y);
358 16 }
359 8 void slice2d_backward(const Tensor& dY, int N, int C, int H, int W,
360 int h0, int w0, int H_out, int W_out, Tensor& dX) {
361
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
362 ✗ detail::throw_spatial_2d_error("slice2d_backward", "dY",
363 ✗ N, C, H, W, dY.rows, dY.cols);
364 }
365
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (H_out > 0 && W_out > 0) {
366 8 BROTENSOR_CHECK_SPATIAL_2D("slice2d_backward", dY, N, C, H_out, W_out);
367 8 }
368 8 const auto& v = detail::dispatch(dY, dX);
369
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!v.slice2d_backward) detail::throw_not_implemented("slice2d_backward", dY.device);
370 8 detail::adopt_output(dX, dY.device);
371 8 v.slice2d_backward(dY, N, C, H, W, h0, w0, H_out, W_out, dX);
372 8 }
373
374 10 void unfold2d_forward(const Tensor& X, int N, int C, int H, int W,
375 int kH, int kW, int stride_h, int stride_w,
376 int pad_top, int pad_bottom, int pad_left, int pad_right,
377 int mode, Tensor& Y) {
378 10 BROTENSOR_CHECK_SPATIAL_2D("unfold2d_forward", X, N, C, H, W);
379 10 const auto& v = detail::dispatch(X, Y);
380
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.unfold2d_forward) detail::throw_not_implemented("unfold2d_forward", X.device);
381 10 detail::adopt_output(Y, X.device);
382 20 v.unfold2d_forward(X, N, C, H, W, kH, kW, stride_h, stride_w,
383 10 pad_top, pad_bottom, pad_left, pad_right, mode, Y);
384 10 }
385
386 10 void l2_normalize_nchw_forward(const Tensor& X, int N, int C, int H, int W,
387 float eps, Tensor& Y) {
388 10 BROTENSOR_CHECK_SPATIAL_2D("l2_normalize_nchw_forward", X, N, C, H, W);
389 10 const auto& v = detail::dispatch(X, Y);
390
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.l2_normalize_nchw_forward)
391 ✗ detail::throw_not_implemented("l2_normalize_nchw_forward", X.device);
392 10 detail::adopt_output(Y, X.device);
393 10 v.l2_normalize_nchw_forward(X, N, C, H, W, eps, Y);
394 10 }
395
396 10 void convex_upsample_forward(const Tensor& X, const Tensor& Mask,
397 int N, int C, int H, int W, int scale, Tensor& Y) {
398 10 BROTENSOR_CHECK_SPATIAL_2D("convex_upsample_forward", X, N, C, H, W);
399 10 const auto& v = detail::dispatch(X, Mask, Y);
400
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.convex_upsample_forward)
401 ✗ detail::throw_not_implemented("convex_upsample_forward", X.device);
402 10 detail::adopt_output(Y, X.device);
403 10 v.convex_upsample_forward(X, Mask, N, C, H, W, scale, Y);
404 10 }
405
406 71 void top_k_rows(const Tensor& X, int k, Tensor& Vals, Tensor& Idx) {
407 71 const auto& v = detail::dispatch(X, Vals, Idx);
408
1/2
✓ Branch 0 taken 71 times.
✗ Branch 1 not taken.
71 if (!v.top_k_rows) detail::throw_not_implemented("top_k_rows", X.device);
409 71 detail::adopt_output(Vals, X.device);
410 71 detail::adopt_output(Idx, X.device);
411 71 v.top_k_rows(X, k, Vals, Idx);
412 71 }
413
414 58 void adaptive_avg_pool2d_forward(const Tensor& X, int N, int C, int H, int W,
415 int H_out, int W_out, Tensor& Y) {
416 58 BROTENSOR_CHECK_SPATIAL_2D("adaptive_avg_pool2d_forward", X, N, C, H, W);
417 58 const auto& v = detail::dispatch(X, Y);
418
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 if (!v.adaptive_avg_pool2d_forward)
419 ✗ detail::throw_not_implemented("adaptive_avg_pool2d_forward", X.device);
420 58 detail::adopt_output(Y, X.device);
421 58 v.adaptive_avg_pool2d_forward(X, N, C, H, W, H_out, W_out, Y);
422 58 }
423 20 void adaptive_avg_pool2d_backward(const Tensor& dY, int N, int C, int H, int W,
424 int H_out, int W_out, Tensor& dX) {
425
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
426 ✗ detail::throw_spatial_2d_error("adaptive_avg_pool2d_backward", "dY",
427 ✗ N, C, H, W, dY.rows, dY.cols);
428 }
429 20 BROTENSOR_CHECK_SPATIAL_2D("adaptive_avg_pool2d_backward", dY, N, C, H_out, W_out);
430 20 const auto& v = detail::dispatch(dY, dX);
431
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!v.adaptive_avg_pool2d_backward)
432 ✗ detail::throw_not_implemented("adaptive_avg_pool2d_backward", dY.device);
433 20 detail::adopt_output(dX, dY.device);
434 20 v.adaptive_avg_pool2d_backward(dY, N, C, H, W, H_out, W_out, dX);
435 20 }
436
437 23 void max_pool2d_forward(const Tensor& X, int N, int C, int H, int W,
438 int kH, int kW, int stride_h, int stride_w,
439 int pad_h, int pad_w, Tensor& Y, Tensor& Idx) {
440 23 BROTENSOR_CHECK_SPATIAL_2D("max_pool2d_forward", X, N, C, H, W);
441 23 const auto& v = detail::dispatch(X, Y, Idx);
442
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 if (!v.max_pool2d_forward)
443 ✗ detail::throw_not_implemented("max_pool2d_forward", X.device);
444 23 detail::adopt_output(Y, X.device);
445 23 detail::adopt_output(Idx, X.device);
446 46 v.max_pool2d_forward(X, N, C, H, W, kH, kW, stride_h, stride_w,
447 23 pad_h, pad_w, Y, Idx);
448 23 }
449 10 void max_pool2d_backward(const Tensor& dY, const Tensor& Idx,
450 int N, int C, int H, int W, int H_out, int W_out,
451 Tensor& dX) {
452
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
453 ✗ detail::throw_spatial_2d_error("max_pool2d_backward", "dY",
454 ✗ N, C, H, W, dY.rows, dY.cols);
455 }
456 10 BROTENSOR_CHECK_SPATIAL_2D("max_pool2d_backward", dY, N, C, H_out, W_out);
457 10 const auto& v = detail::dispatch(dY, Idx, dX);
458
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (!v.max_pool2d_backward)
459 ✗ detail::throw_not_implemented("max_pool2d_backward", dY.device);
460 10 detail::adopt_output(dX, dY.device);
461 10 v.max_pool2d_backward(dY, Idx, N, C, H, W, H_out, W_out, dX);
462 10 }
463
464 18 void gather_rows(const Tensor& X, const Tensor& Idx, Tensor& Y) {
465 18 const auto& v = detail::dispatch(X, Idx, Y);
466
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (!v.gather_rows) detail::throw_not_implemented("gather_rows", X.device);
467 18 detail::adopt_output(Y, X.device);
468 18 v.gather_rows(X, Idx, Y);
469 18 }
470 11 void scatter_rows_add(const Tensor& dY, const Tensor& Idx, int R, Tensor& dX) {
471 11 const auto& v = detail::dispatch(dY, Idx, dX);
472
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!v.scatter_rows_add)
473 ✗ detail::throw_not_implemented("scatter_rows_add", dY.device);
474 11 detail::adopt_output(dX, dY.device);
475 11 v.scatter_rows_add(dY, Idx, R, dX);
476 11 }
477 8 void scatter_rows(const Tensor& Y, const Tensor& Idx, Tensor& X) {
478 8 const auto& v = detail::dispatch(Y, Idx, X);
479
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!v.scatter_rows) detail::throw_not_implemented("scatter_rows", Y.device);
480 8 detail::adopt_output(X, Y.device);
481 8 v.scatter_rows(Y, Idx, X);
482 8 }
483
484 103 void conv_transpose2d_forward(const Tensor& X, const Tensor& Wt,
485 const Tensor* bias,
486 int N, int C_in, int H, int W,
487 int C_out, int kH, int kW,
488 int stride_h, int stride_w,
489 int pad_h, int pad_w,
490 int output_padding_h, int output_padding_w,
491 int dil_h, int dil_w, int groups,
492 Tensor& Y) {
493 103 BROTENSOR_CHECK_SPATIAL_2D("conv_transpose2d_forward", X, N, C_in, H, W);
494 103 const auto& v = detail::dispatch_with_opts(X, Wt, {bias, &Y});
495
1/2
✓ Branch 0 taken 103 times.
✗ Branch 1 not taken.
103 if (!v.conv_transpose2d_forward)
496 ✗ detail::throw_not_implemented("conv_transpose2d_forward", X.device);
497 103 detail::adopt_output(Y, X.device);
498 206 v.conv_transpose2d_forward(X, Wt, bias, N, C_in, H, W, C_out, kH, kW,
499 103 stride_h, stride_w, pad_h, pad_w,
500 103 output_padding_h, output_padding_w,
501 103 dil_h, dil_w, groups, Y);
502 103 }
503 22 void conv_transpose2d_backward_input(const Tensor& Wt, const Tensor& dY,
504 int N, int C_in, int H, int W,
505 int C_out, int kH, int kW,
506 int stride_h, int stride_w,
507 int pad_h, int pad_w,
508 int output_padding_h, int output_padding_w,
509 int dil_h, int dil_w, int groups,
510 Tensor& dX) {
511
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (N <= 0 || C_in <= 0 || H <= 0 || W <= 0 || C_out <= 0) {
512 ✗ detail::throw_spatial_2d_error("conv_transpose2d_backward_input", "dY",
513 ✗ N, C_in, H, W, dY.rows, dY.cols);
514 }
515 22 const auto& v = detail::dispatch(Wt, dY, dX);
516
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (!v.conv_transpose2d_backward_input)
517 ✗ detail::throw_not_implemented("conv_transpose2d_backward_input", Wt.device);
518 22 detail::adopt_output(dX, Wt.device);
519 44 v.conv_transpose2d_backward_input(Wt, dY, N, C_in, H, W, C_out, kH, kW,
520 22 stride_h, stride_w, pad_h, pad_w,
521 22 output_padding_h, output_padding_w,
522 22 dil_h, dil_w, groups, dX);
523 22 }
524 9 void conv_transpose2d_backward_weight(const Tensor& X, const Tensor& dY,
525 int N, int C_in, int H, int W,
526 int C_out, int kH, int kW,
527 int stride_h, int stride_w,
528 int pad_h, int pad_w,
529 int output_padding_h, int output_padding_w,
530 int dil_h, int dil_w, int groups,
531 Tensor& dWt) {
532 9 BROTENSOR_CHECK_SPATIAL_2D("conv_transpose2d_backward_weight", X, N, C_in, H, W);
533 9 const auto& v = detail::dispatch(X, dY, dWt);
534
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (!v.conv_transpose2d_backward_weight)
535 ✗ detail::throw_not_implemented("conv_transpose2d_backward_weight", X.device);
536 9 detail::adopt_output(dWt, X.device);
537 18 v.conv_transpose2d_backward_weight(X, dY, N, C_in, H, W, C_out, kH, kW,
538 9 stride_h, stride_w, pad_h, pad_w,
539 9 output_padding_h, output_padding_w,
540 9 dil_h, dil_w, groups, dWt);
541 9 }
542 7 void conv_transpose2d_backward_bias(const Tensor& dY, int N, int C_out,
543 int H_out, int W_out, Tensor& dB) {
544 7 BROTENSOR_CHECK_SPATIAL_2D("conv_transpose2d_backward_bias", dY, N, C_out, H_out, W_out);
545 7 const auto& v = detail::dispatch(dY, dB);
546
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!v.conv_transpose2d_backward_bias)
547 ✗ detail::throw_not_implemented("conv_transpose2d_backward_bias", dY.device);
548 7 detail::adopt_output(dB, dY.device);
549 7 v.conv_transpose2d_backward_bias(dY, N, C_out, H_out, W_out, dB);
550 7 }
551
552 18 void window_partition_forward(const Tensor& X, int N, int C, int H, int W,
553 int window, Tensor& Y) {
554 18 BROTENSOR_CHECK_SPATIAL_2D("window_partition_forward", X, N, C, H, W);
555 18 const auto& v = detail::dispatch(X, Y);
556
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (!v.window_partition_forward)
557 ✗ detail::throw_not_implemented("window_partition_forward", X.device);
558 18 detail::adopt_output(Y, X.device);
559 18 v.window_partition_forward(X, N, C, H, W, window, Y);
560 18 }
561 12 void window_reverse_forward(const Tensor& X, int N, int C, int H, int W,
562 int window, Tensor& Y) {
563
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
564 ✗ detail::throw_spatial_2d_error("window_reverse_forward", "X",
565 ✗ N, C, H, W, X.rows, X.cols);
566 }
567
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 12 times.
12 if (window > 0 && H % window == 0 && W % window == 0) {
568 12 BROTENSOR_CHECK_SPATIAL_2D("window_reverse_forward", X,
569 N * (H / window) * (W / window), C, window, window);
570 12 }
571 12 const auto& v = detail::dispatch(X, Y);
572
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 if (!v.window_reverse_forward)
573 ✗ detail::throw_not_implemented("window_reverse_forward", X.device);
574 12 detail::adopt_output(Y, X.device);
575 12 v.window_reverse_forward(X, N, C, H, W, window, Y);
576 12 }
577
578 // ─── NCHW <-> sequence ─────────────────────────────────────────────────────
579
580 34 void nchw_to_sequence(const Tensor& X, int N, int C, int H, int W, Tensor& Y) {
581 34 BROTENSOR_CHECK_SPATIAL_2D("nchw_to_sequence", X, N, C, H, W);
582 34 const auto& v = detail::dispatch(X, Y);
583
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 if (!v.nchw_to_sequence) detail::throw_not_implemented("nchw_to_sequence", X.device);
584 34 detail::adopt_output(Y, X.device);
585 34 v.nchw_to_sequence(X, N, C, H, W, Y);
586 34 }
587
588 27 void sequence_to_nchw(const Tensor& X, int N, int C, int H, int W, Tensor& Y) {
589
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (N <= 0 || C <= 0 || H <= 0 || W <= 0) {
590 ✗ detail::throw_spatial_2d_error("sequence_to_nchw", "X",
591 ✗ N, C, H, W, X.rows, X.cols);
592 }
593 27 BROTENSOR_CHECK_SPATIAL_1D("sequence_to_nchw", X, N * H * W, C, 1);
594 27 const auto& v = detail::dispatch(X, Y);
595
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 if (!v.sequence_to_nchw) detail::throw_not_implemented("sequence_to_nchw", X.device);
596 27 detail::adopt_output(Y, X.device);
597 27 v.sequence_to_nchw(X, N, C, H, W, Y);
598 27 }
599
600 38 void spatial_merge_2x2_forward(const Tensor& X, int N, int C, int H, int W,
601 bool channel_major, Tensor& Y) {
602 38 BROTENSOR_CHECK_SPATIAL_2D("spatial_merge_2x2_forward", X, N, C, H, W);
603 38 const auto& v = detail::dispatch(X, Y);
604
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 if (!v.spatial_merge_2x2_forward)
605 ✗ detail::throw_not_implemented("spatial_merge_2x2_forward", X.device);
606 38 detail::adopt_output(Y, X.device);
607 38 v.spatial_merge_2x2_forward(X, N, C, H, W, channel_major, Y);
608 38 }
609
610 52 void pixel_shuffle_upsample_2x_forward(const Tensor& X, int N, int C_in,
611 int H, int W, int C_out, Tensor& Y) {
612
7/8
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 50 times.
✓ Branch 3 taken 1 time.
✓ Branch 4 taken 48 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 48 times.
52 if (N > 0 && C_in > 0 && H > 0 && W > 0) {
613 48 BROTENSOR_CHECK_SPATIAL_2D("pixel_shuffle_upsample_2x_forward", X, N, C_in, H, W);
614 48 }
615 52 const auto& v = detail::dispatch(X, Y);
616
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 if (!v.pixel_shuffle_upsample_2x_forward)
617 ✗ detail::throw_not_implemented("pixel_shuffle_upsample_2x_forward", X.device);
618 52 detail::adopt_output(Y, X.device);
619 52 v.pixel_shuffle_upsample_2x_forward(X, N, C_in, H, W, C_out, Y);
620 52 }
621
622 16 void patch_unpack_forward(const Tensor& tokens, int hp, int wp, int P,
623 int C_total, int C_keep, bool channel_major,
624 Tensor& Y) {
625 16 const auto& v = detail::dispatch(tokens, Y);
626
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (!v.patch_unpack_forward)
627 ✗ detail::throw_not_implemented("patch_unpack_forward", tokens.device);
628 16 detail::adopt_output(Y, tokens.device);
629 16 v.patch_unpack_forward(tokens, hp, wp, P, C_total, C_keep, channel_major, Y);
630 16 }
631
632 // ─── ResBlock ──────────────────────────────────────────────────────────────
633
634 26 void resblock_forward(const Tensor& X,
635 const Tensor& gamma1, const Tensor& beta1,
636 const Tensor& W1, const Tensor* b1,
637 const Tensor* t_emb_shift,
638 const Tensor& gamma2, const Tensor& beta2,
639 const Tensor& W2, const Tensor* b2,
640 const Tensor* Wskip, const Tensor* bskip,
641 int N, int C_in, int C_out, int H, int W,
642 int num_groups, float eps, Tensor& Y) {
643 26 BROTENSOR_CHECK_SPATIAL_2D("resblock_forward", X, N, C_in, H, W);
644 26 const auto& v = detail::dispatch_with_opts(
645 104 X, gamma1, {&beta1, &W1, b1, t_emb_shift, &gamma2, &beta2, &W2, b2,
646 78 Wskip, bskip, &Y});
647
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (!v.resblock_forward) detail::throw_not_implemented("resblock_forward", X.device);
648 26 detail::adopt_output(Y, X.device);
649 52 v.resblock_forward(X, gamma1, beta1, W1, b1, t_emb_shift,
650 26 gamma2, beta2, W2, b2, Wskip, bskip,
651 26 N, C_in, C_out, H, W, num_groups, eps, Y);
652 26 }
653
654 7 void resblock_forward_int8w_fp16(const Tensor& X,
655 const Tensor& gamma1, const Tensor& beta1,
656 const Tensor& W1_int8, const Tensor& s1,
657 const Tensor* b1,
658 const Tensor* t_emb_shift,
659 const Tensor& gamma2, const Tensor& beta2,
660 const Tensor& W2_int8, const Tensor& s2,
661 const Tensor* b2,
662 const Tensor* Wskip_int8, const Tensor* sskip,
663 const Tensor* bskip,
664 int N, int C_in, int C_out, int H, int W,
665 int num_groups, float eps, Tensor& Y) {
666 7 BROTENSOR_CHECK_SPATIAL_2D("resblock_forward_int8w_fp16", X, N, C_in, H, W);
667 7 const auto& v = detail::dispatch_with_opts(
668 70 X, gamma1, {&beta1, &W1_int8, &s1, b1, t_emb_shift,
669 35 &gamma2, &beta2, &W2_int8, &s2, b2,
670 28 Wskip_int8, sskip, bskip, &Y});
671
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!v.resblock_forward_int8w_fp16)
672 ✗ detail::throw_not_implemented("resblock_forward_int8w_fp16", X.device);
673 7 detail::adopt_output(Y, X.device);
674 14 v.resblock_forward_int8w_fp16(X, gamma1, beta1, W1_int8, s1, b1, t_emb_shift,
675 7 gamma2, beta2, W2_int8, s2, b2,
676 7 Wskip_int8, sskip, bskip,
677 7 N, C_in, C_out, H, W, num_groups, eps, Y);
678 7 }
679
680 20 void resblock_backward(const Tensor& X,
681 const Tensor& gamma1, const Tensor& beta1,
682 const Tensor& W1, const Tensor* b1,
683 const Tensor* t_emb_shift,
684 const Tensor& gamma2, const Tensor& beta2,
685 const Tensor& W2, const Tensor* b2,
686 const Tensor* Wskip, const Tensor* bskip,
687 int N, int C_in, int C_out, int H, int W,
688 int num_groups, float eps,
689 const Tensor& dY,
690 Tensor& dX,
691 Tensor& dGamma1, Tensor& dBeta1,
692 Tensor& dW1, Tensor* db1,
693 Tensor* dt_emb_shift,
694 Tensor& dGamma2, Tensor& dBeta2,
695 Tensor& dW2, Tensor* db2,
696 Tensor* dWskip, Tensor* dbskip) {
697 20 BROTENSOR_CHECK_SPATIAL_2D("resblock_backward", X, N, C_in, H, W);
698 20 BROTENSOR_CHECK_SPATIAL_2D("resblock_backward", dY, N, C_out, H, W);
699 20 const auto& v = detail::dispatch_with_opts(
700 320 X, gamma1, {&beta1, &W1, b1, t_emb_shift, &gamma2, &beta2, &W2, b2,
701 160 Wskip, bskip, &dY, &dX, &dGamma1, &dBeta1, &dW1, db1,
702 140 dt_emb_shift, &dGamma2, &dBeta2, &dW2, db2, dWskip, dbskip});
703
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (!v.resblock_backward) detail::throw_not_implemented("resblock_backward", X.device);
704 20 detail::adopt_output(dX, X.device);
705 20 detail::adopt_output(dGamma1, X.device);
706 20 detail::adopt_output(dBeta1, X.device);
707 20 detail::adopt_output(dW1, X.device);
708
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (db1) detail::adopt_output(*db1, X.device);
709
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 if (dt_emb_shift) detail::adopt_output(*dt_emb_shift, X.device);
710 20 detail::adopt_output(dGamma2, X.device);
711 20 detail::adopt_output(dBeta2, X.device);
712 20 detail::adopt_output(dW2, X.device);
713
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (db2) detail::adopt_output(*db2, X.device);
714
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 if (dWskip) detail::adopt_output(*dWskip, X.device);
715
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 if (dbskip) detail::adopt_output(*dbskip, X.device);
716 40 v.resblock_backward(X, gamma1, beta1, W1, b1, t_emb_shift,
717 20 gamma2, beta2, W2, b2, Wskip, bskip,
718 20 N, C_in, C_out, H, W, num_groups, eps,
719 20 dY, dX, dGamma1, dBeta1, dW1, db1, dt_emb_shift,
720 20 dGamma2, dBeta2, dW2, db2, dWskip, dbskip);
721 20 }
722
723 // ─── BatchNorm ─────────────────────────────────────────────────────────────
724
725 11 void batch_norm_forward(const Tensor& X,
726 const Tensor& gamma, const Tensor& beta,
727 Tensor& running_mean, Tensor& running_var,
728 int N, int C, int H, int W,
729 float eps, float momentum,
730 Tensor& Y,
731 Tensor& saved_mean, Tensor& saved_rstd) {
732 11 BROTENSOR_CHECK_SPATIAL_2D("batch_norm_forward", X, N, C, H, W);
733 22 const auto& v = detail::dispatch(X, gamma, beta, running_mean, running_var,
734 11 Y, saved_mean, saved_rstd);
735
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (!v.batch_norm_forward)
736 ✗ detail::throw_not_implemented("batch_norm_forward", X.device);
737 11 detail::adopt_output(running_mean, X.device);
738 11 detail::adopt_output(running_var, X.device);
739 11 detail::adopt_output(Y, X.device);
740 11 detail::adopt_output(saved_mean, X.device);
741 11 detail::adopt_output(saved_rstd, X.device);
742 22 v.batch_norm_forward(X, gamma, beta, running_mean, running_var,
743 11 N, C, H, W, eps, momentum,
744 11 Y, saved_mean, saved_rstd);
745 11 }
746
747 17 void batch_norm_inference(const Tensor& X,
748 const Tensor& gamma, const Tensor& beta,
749 const Tensor& running_mean,
750 const Tensor& running_var,
751 int N, int C, int H, int W,
752 float eps,
753 Tensor& Y) {
754 17 BROTENSOR_CHECK_SPATIAL_2D("batch_norm_inference", X, N, C, H, W);
755 17 const auto& v = detail::dispatch(X, gamma, beta, running_mean, running_var, Y);
756
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!v.batch_norm_inference)
757 ✗ detail::throw_not_implemented("batch_norm_inference", X.device);
758 17 detail::adopt_output(Y, X.device);
759 34 v.batch_norm_inference(X, gamma, beta, running_mean, running_var,
760 17 N, C, H, W, eps, Y);
761 17 }
762
763 7 void batch_norm_backward(const Tensor& X,
764 const Tensor& gamma,
765 const Tensor& saved_mean,
766 const Tensor& saved_rstd,
767 const Tensor& dY,
768 int N, int C, int H, int W,
769 Tensor& dX,
770 Tensor& dGamma, Tensor& dBeta) {
771 7 BROTENSOR_CHECK_SPATIAL_2D("batch_norm_backward", X, N, C, H, W);
772 7 BROTENSOR_CHECK_SPATIAL_2D("batch_norm_backward", dY, N, C, H, W);
773 14 const auto& v = detail::dispatch(X, gamma, saved_mean, saved_rstd, dY,
774 7 dX, dGamma, dBeta);
775
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 if (!v.batch_norm_backward)
776 ✗ detail::throw_not_implemented("batch_norm_backward", X.device);
777 7 detail::adopt_output(dX, X.device);
778 7 detail::adopt_output(dGamma, X.device);
779 7 detail::adopt_output(dBeta, X.device);
780 14 v.batch_norm_backward(X, gamma, saved_mean, saved_rstd, dY,
781 7 N, C, H, W, dX, dGamma, dBeta);
782 7 }
783
784 // ─── Image preprocessing helpers ───────────────────────────────────────────
785
786 8 void image_normalize(const Tensor& X,
787 const Tensor& mean, const Tensor& std_,
788 int N, int C, int H, int W,
789 Tensor& Y) {
790 8 BROTENSOR_CHECK_SPATIAL_2D("image_normalize", X, N, C, H, W);
791 8 const auto& v = detail::dispatch(X, mean, std_, Y);
792
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (!v.image_normalize)
793 ✗ detail::throw_not_implemented("image_normalize", X.device);
794 8 detail::adopt_output(Y, X.device);
795 8 v.image_normalize(X, mean, std_, N, C, H, W, Y);
796 8 }
797
798 17 void image_u8_to_f32_nhwc_to_nchw(const uint8_t* src,
799 int N, int H, int W, int C,
800 float scale, float bias,
801 Tensor& Y) {
802 17 const auto& v = detail::dispatch(Y);
803
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 if (!v.image_u8_to_f32_nhwc_to_nchw)
804 ✗ detail::throw_not_implemented("image_u8_to_f32_nhwc_to_nchw", Y.device);
805 17 detail::adopt_output(Y, Y.device);
806 17 v.image_u8_to_f32_nhwc_to_nchw(src, N, H, W, C, scale, bias, Y);
807 17 }
808
809 // ─── StyleGAN3 bias_act / upfirdn2d / modulated_conv2d ─────────────────────
810
811 79 void bias_act_forward(const Tensor& X, const Tensor* b,
812 int N, int C, int HW, int act, float alpha,
813 float gain, float clamp, Tensor& Y) {
814 79 BROTENSOR_CHECK_SPATIAL_1D("bias_act_forward", X, N, C, HW);
815 79 const auto& v = detail::dispatch_with_opts(X, {b, &Y});
816
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
79 if (!v.bias_act_forward) detail::throw_not_implemented("bias_act_forward", X.device);
817 79 detail::adopt_output(Y, X.device);
818 79 v.bias_act_forward(X, b, N, C, HW, act, alpha, gain, clamp, Y);
819 79 }
820
821 96 void bias_act_backward(const Tensor& dY, const Tensor& X, const Tensor* b,
822 int N, int C, int HW, int act, float alpha,
823 float gain, float clamp, Tensor& dX, Tensor* dB) {
824 96 BROTENSOR_CHECK_SPATIAL_1D("bias_act_backward", dY, N, C, HW);
825 96 BROTENSOR_CHECK_SPATIAL_1D("bias_act_backward", X, N, C, HW);
826 96 const auto& v = detail::dispatch_with_opts(dY, X, {b, dB, &dX});
827
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (!v.bias_act_backward) detail::throw_not_implemented("bias_act_backward", dY.device);
828 96 detail::adopt_output(dX, dY.device);
829
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 32 times.
96 if (dB) detail::adopt_output(*dB, dY.device);
830 96 v.bias_act_backward(dY, X, b, N, C, HW, act, alpha, gain, clamp, dX, dB);
831 96 }
832
833 96 void upfirdn2d_forward(const Tensor& X, const Tensor& f,
834 int N, int C, int H, int Wd, int fH, int fW,
835 int up_x, int up_y, int down_x, int down_y,
836 int pad_x0, int pad_x1, int pad_y0, int pad_y1,
837 bool flip_filter, float gain, Tensor& Y) {
838 96 BROTENSOR_CHECK_SPATIAL_2D("upfirdn2d_forward", X, N, C, H, Wd);
839 96 const auto& v = detail::dispatch(X, f, Y);
840
1/2
✓ Branch 0 taken 96 times.
✗ Branch 1 not taken.
96 if (!v.upfirdn2d_forward) detail::throw_not_implemented("upfirdn2d_forward", X.device);
841 96 detail::adopt_output(Y, X.device);
842 192 v.upfirdn2d_forward(X, f, N, C, H, Wd, fH, fW, up_x, up_y, down_x, down_y,
843 96 pad_x0, pad_x1, pad_y0, pad_y1, flip_filter, gain, Y);
844 96 }
845
846 108 void upfirdn2d_backward(const Tensor& dY, const Tensor& f,
847 int N, int C, int H, int Wd, int fH, int fW,
848 int up_x, int up_y, int down_x, int down_y,
849 int pad_x0, int pad_x1, int pad_y0, int pad_y1,
850 bool flip_filter, float gain, Tensor& dX) {
851
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 if (N <= 0 || C <= 0 || H <= 0 || Wd <= 0) {
852 ✗ detail::throw_spatial_2d_error("upfirdn2d_backward", "dY",
853 ✗ N, C, H, Wd, dY.rows, dY.cols);
854 }
855 108 const auto& v = detail::dispatch(dY, f, dX);
856
1/2
✓ Branch 0 taken 108 times.
✗ Branch 1 not taken.
108 if (!v.upfirdn2d_backward) detail::throw_not_implemented("upfirdn2d_backward", dY.device);
857 108 detail::adopt_output(dX, dY.device);
858 216 v.upfirdn2d_backward(dY, f, N, C, H, Wd, fH, fW, up_x, up_y, down_x, down_y,
859 108 pad_x0, pad_x1, pad_y0, pad_y1, flip_filter, gain, dX);
860 108 }
861
862 46 void modulated_conv2d_forward(const Tensor& X, const Tensor& W, const Tensor& s,
863 int N, int C_in, int H, int Wd,
864 int C_out, int kH, int kW,
865 int pad_h, int pad_w,
866 bool demodulate, float eps,
867 Tensor& dcoef, Tensor& Y) {
868 46 BROTENSOR_CHECK_SPATIAL_2D("modulated_conv2d_forward", X, N, C_in, H, Wd);
869 46 const auto& v = detail::dispatch(X, W, s, dcoef, Y);
870
1/2
✓ Branch 0 taken 46 times.
✗ Branch 1 not taken.
46 if (!v.modulated_conv2d_forward)
871 ✗ detail::throw_not_implemented("modulated_conv2d_forward", X.device);
872 46 detail::adopt_output(dcoef, X.device);
873 46 detail::adopt_output(Y, X.device);
874 92 v.modulated_conv2d_forward(X, W, s, N, C_in, H, Wd, C_out, kH, kW,
875 46 pad_h, pad_w, demodulate, eps, dcoef, Y);
876 46 }
877
878 63 void modulated_conv2d_backward(const Tensor& X, const Tensor& W, const Tensor& s,
879 const Tensor& dcoef, const Tensor& dY,
880 int N, int C_in, int H, int Wd,
881 int C_out, int kH, int kW,
882 int pad_h, int pad_w, bool demodulate, float eps,
883 Tensor& dX, Tensor& dW, Tensor& ds) {
884 63 BROTENSOR_CHECK_SPATIAL_2D("modulated_conv2d_backward", X, N, C_in, H, Wd);
885
4/4
✓ Branch 0 taken 62 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 61 times.
63 if (kH > 0 && kW > 0) {
886 61 const int H_out = H + 2 * pad_h - (kH - 1);
887 61 const int W_out = Wd + 2 * pad_w - (kW - 1);
888
2/4
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 61 times.
61 if (H_out > 0 && W_out > 0) {
889 61 BROTENSOR_CHECK_SPATIAL_2D("modulated_conv2d_backward", dY, N, C_out, H_out, W_out);
890 61 }
891 61 }
892 63 const auto& v = detail::dispatch(X, W, s, dcoef, dY, dX, dW, ds);
893
1/2
✓ Branch 0 taken 61 times.
✗ Branch 1 not taken.
63 if (!v.modulated_conv2d_backward)
894 ✗ detail::throw_not_implemented("modulated_conv2d_backward", X.device);
895 61 detail::adopt_output(dX, X.device);
896 61 detail::adopt_output(dW, X.device);
897 61 detail::adopt_output(ds, X.device);
898 122 v.modulated_conv2d_backward(X, W, s, dcoef, dY, N, C_in, H, Wd, C_out, kH, kW,
899 61 pad_h, pad_w, demodulate, eps, dX, dW, ds);
900 61 }
901
902 } // namespace brotensor
903