brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
78 / 79
98.7%
Branches:
29 / 54
53.7%
Functions:
5 / 5
100.0%

src/filtered_lrelu.cpp
Line Branch Exec Source
1 // ─── filtered_lrelu (StyleGAN3 alias-free nonlinearity) ─────────────────────
2 //
3 // The public entry points are thin dispatchers: if the resolved backend
4 // registered a fused `filtered_lrelu_forward/backward` vtable slot (CUDA: the
5 // forward; Metal: both), they call it; otherwise they fall back to the
6 // device-agnostic COMPOSITE below — a sequence of the public bias_act +
7 // upfirdn2d ops, which run on whatever backend the operands live on (the only
8 // path on CPU; the fused kernels hand uncovered configs back to it). The
9 // composite mirrors NVlabs `_filtered_lrelu_ref` EXACTLY, including order:
10 //
11 // x = bias_act(x, b) # apply channel bias
12 // x = upfirdn2d(x, fu, up=up, pad=p, gain=up^2) # upsample
13 // x = bias_act(x, act=lrelu, gain, clamp) # bias-free lrelu+clamp
14 // x = upfirdn2d(x, fd, down=down) # downsample
15 //
16 // The bias is applied BEFORE the upsample (a linear bias_act), and the
17 // post-upsample bias_act carries only the lrelu — this is not interchangeable
18 // with biasing after the upsample. The backward reverses the chain.
19 //
20 // up_buf/act_buf are returned as caches: up_buf (the post-upsample tensor) is
21 // the input to the lrelu bias_act and is required by the backward; act_buf is
22 // kept for symmetry with the fused-kernel surface. A fused backend that does
23 // not reproduce these caches must keep whatever its own backward consumes —
24 // the cache contract is per-backend (see the CUDA kernel).
25
26 #include <brotensor/ops.h>
27 #include <brotensor/tensor.h>
28 #include <brotensor/detail/dispatch.h>
29
30 #include <stdexcept>
31
32 namespace brotensor {
33
34 // Composite fallback, also reused by the CUDA backend for configs its fused
35 // kernel does not cover. Declared here (not in the public header) and called
36 // from src/cuda/filtered_lrelu.cu via a matching forward declaration.
37 void filtered_lrelu_forward_composite(const Tensor& X, const Tensor& fu,
38 const Tensor& fd, const Tensor* b,
39 int N, int C, int H, int W, int up, int down,
40 int pad_x0, int pad_x1, int pad_y0, int pad_y1,
41 float gain, float slope, float clamp,
42 Tensor& up_buf, Tensor& act_buf, Tensor& Y);
43 void filtered_lrelu_backward_composite(const Tensor& dY, const Tensor& X,
44 const Tensor& fu, const Tensor& fd,
45 const Tensor* b, int N, int C, int H, int W,
46 int up, int down, int pad_x0, int pad_x1,
47 int pad_y0, int pad_y1, float gain, float slope,
48 float clamp, const Tensor& up_buf,
49 Tensor& dX, Tensor* dB);
50
51 namespace {
52
53 constexpr int ACT_LINEAR = 0;
54 constexpr int ACT_LRELU = 1;
55 constexpr float NO_CLAMP = -1.0f;
56
57 // Up-stage upfirdn2d output extent (down=1): (in*up + pad0 + pad1 - f) + 1.
58 94 inline int up_out(int in, int up, int pad0, int pad1, int fdim) {
59 94 return in * up + pad0 + pad1 - fdim + 1;
60 }
61
62 } // namespace
63
64 // ─── public dispatchers ─────────────────────────────────────────────────────
65
66 26 void filtered_lrelu_forward(const Tensor& X, const Tensor& fu, const Tensor& fd,
67 const Tensor* b, int N, int C, int H, int W,
68 int up, int down, int pad_x0, int pad_x1,
69 int pad_y0, int pad_y1, float gain, float slope,
70 float clamp, Tensor& up_buf, Tensor& act_buf,
71 Tensor& Y) {
72 26 BROTENSOR_CHECK_SPATIAL_2D("filtered_lrelu_forward", X, N, C, H, W);
73 26 const auto& v = detail::dispatch_with_opts(
74 26 X, fu, {&fd, b, &up_buf, &act_buf, &Y});
75
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
26 if (v.filtered_lrelu_forward) {
76 14 detail::adopt_output(up_buf, X.device);
77 14 detail::adopt_output(act_buf, X.device);
78 14 detail::adopt_output(Y, X.device);
79 28 v.filtered_lrelu_forward(X, fu, fd, b, N, C, H, W, up, down,
80 14 pad_x0, pad_x1, pad_y0, pad_y1, gain, slope,
81 14 clamp, up_buf, act_buf, Y);
82 14 return;
83 }
84 24 filtered_lrelu_forward_composite(X, fu, fd, b, N, C, H, W, up, down,
85 12 pad_x0, pad_x1, pad_y0, pad_y1, gain, slope,
86 12 clamp, up_buf, act_buf, Y);
87 26 }
88
89 33 void filtered_lrelu_backward(const Tensor& dY, const Tensor& X,
90 const Tensor& fu, const Tensor& fd,
91 const Tensor* b, int N, int C, int H, int W,
92 int up, int down, int pad_x0, int pad_x1,
93 int pad_y0, int pad_y1, float gain, float slope,
94 float clamp, const Tensor& up_buf,
95 Tensor& dX, Tensor* dB) {
96 33 BROTENSOR_CHECK_SPATIAL_2D("filtered_lrelu_backward", X, N, C, H, W);
97 33 const auto& v = detail::dispatch_with_opts(
98 33 dY, X, {&fu, &fd, b, &up_buf, &dX, dB});
99
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 12 times.
33 if (v.filtered_lrelu_backward) {
100 21 detail::adopt_output(dX, X.device);
101
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 7 times.
21 if (dB) detail::adopt_output(*dB, X.device);
102 42 v.filtered_lrelu_backward(dY, X, fu, fd, b, N, C, H, W, up, down,
103 21 pad_x0, pad_x1, pad_y0, pad_y1, gain, slope,
104 21 clamp, up_buf, dX, dB);
105 21 return;
106 }
107 24 filtered_lrelu_backward_composite(dY, X, fu, fd, b, N, C, H, W, up, down,
108 12 pad_x0, pad_x1, pad_y0, pad_y1, gain, slope,
109 12 clamp, up_buf, dX, dB);
110 33 }
111
112 // ─── composite implementation (fallback / CPU / Metal) ──────────────────────
113
114 14 void filtered_lrelu_forward_composite(const Tensor& X, const Tensor& fu, const Tensor& fd,
115 const Tensor* b, int N, int C, int H, int W,
116 int up, int down, int pad_x0, int pad_x1,
117 int pad_y0, int pad_y1, float gain, float slope,
118 float clamp, Tensor& up_buf, Tensor& act_buf,
119 Tensor& Y) {
120 14 const int fuH = fu.rows, fuW = fu.cols;
121 14 const int fdH = fd.rows, fdW = fd.cols;
122
123 // 1. Channel bias (linear bias_act), at the input rate.
124 14 Tensor pre;
125
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 bias_act_forward(X, b, N, C, H * W, ACT_LINEAR, 0.0f, 1.0f, NO_CLAMP, pre);
126
127 // 2. Upsample with gain = up^2.
128
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
28 upfirdn2d_forward(pre, fu, N, C, H, W, fuH, fuW,
129 14 up, up, 1, 1, pad_x0, pad_x1, pad_y0, pad_y1,
130 14 /*flip=*/false, static_cast<float>(up * up), up_buf);
131
132
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const int Huo = up_out(H, up, pad_y0, pad_y1, fuH);
133
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const int Wuo = up_out(W, up, pad_x0, pad_x1, fuW);
134
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 if (up_buf.cols != C * Huo * Wuo) {
135
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ throw std::runtime_error("filtered_lrelu_forward: upsample dim mismatch");
136 }
137
138 // 3. Bias-free leaky ReLU + clamp at the 2x rate.
139
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
28 bias_act_forward(up_buf, nullptr, N, C, Huo * Wuo, ACT_LRELU, slope,
140 14 gain, clamp, act_buf);
141
142 // 4. Downsample (gain 1, no padding).
143
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
28 upfirdn2d_forward(act_buf, fd, N, C, Huo, Wuo, fdH, fdW,
144 14 1, 1, down, down, 0, 0, 0, 0,
145 14 /*flip=*/false, 1.0f, Y);
146 14 }
147
148 33 void filtered_lrelu_backward_composite(const Tensor& dY, const Tensor& X,
149 const Tensor& fu, const Tensor& fd,
150 const Tensor* b, int N, int C, int H, int W,
151 int up, int down, int pad_x0, int pad_x1,
152 int pad_y0, int pad_y1, float gain, float slope,
153 float clamp, const Tensor& up_buf,
154 Tensor& dX, Tensor* dB) {
155 33 const int fuH = fu.rows, fuW = fu.cols;
156 33 const int fdH = fd.rows, fdW = fd.cols;
157 33 const int Huo = up_out(H, up, pad_y0, pad_y1, fuH);
158 33 const int Wuo = up_out(W, up, pad_x0, pad_x1, fuW);
159
160 // up_buf is the post-upsample (pre-lrelu) tensor — the lrelu backward needs
161 // it. The fused forward skips producing it (it's the buffer fusion avoids),
162 // so when the caller hands us an uncommitted up_buf we recompute it here
163 // from X exactly as the forward did (bias → up-FIR). The cache is thus
164 // optional: a populated up_buf is used directly, an empty one is rebuilt.
165 33 Tensor up_buf_local;
166 33 const Tensor* up_ptr = &up_buf;
167
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 21 times.
33 if (up_buf.data == nullptr) {
168 21 Tensor pre;
169
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 bias_act_forward(X, b, N, C, H * W, ACT_LINEAR, 0.0f, 1.0f, NO_CLAMP, pre);
170
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
42 upfirdn2d_forward(pre, fu, N, C, H, W, fuH, fuW,
171 21 up, up, 1, 1, pad_x0, pad_x1, pad_y0, pad_y1,
172 21 /*flip=*/false, static_cast<float>(up * up), up_buf_local);
173 21 up_ptr = &up_buf_local;
174 21 }
175
176 // 4'. Through the downsample.
177 33 Tensor d_act;
178
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 upfirdn2d_backward(dY, fd, N, C, Huo, Wuo, fdH, fdW,
179 33 1, 1, down, down, 0, 0, 0, 0,
180 /*flip=*/false, 1.0f, d_act);
181
182 // 3'. Through the leaky ReLU (no bias gradient here).
183 33 Tensor d_up;
184
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 bias_act_backward(d_act, *up_ptr, nullptr, N, C, Huo * Wuo, ACT_LRELU, slope,
185 33 gain, clamp, d_up, nullptr);
186
187 // 2'. Through the upsample.
188 33 Tensor d_pre;
189
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 upfirdn2d_backward(d_up, fu, N, C, H, W, fuH, fuW,
190 33 up, up, 1, 1, pad_x0, pad_x1, pad_y0, pad_y1,
191 33 /*flip=*/false, static_cast<float>(up * up), d_pre);
192
193 // 1'. Through the channel bias — dX (overwrite) and dB (accumulate).
194
2/4
✓ Branch 0 taken 33 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
66 bias_act_backward(d_pre, X, b, N, C, H * W, ACT_LINEAR, 0.0f, 1.0f,
195 33 NO_CLAMP, dX, dB);
196 33 }
197
198 } // namespace brotensor
199