src/api/native_tensor_extra_conv.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // native_tensor_extra_conv.cpp — image / convolution extra bindings: | ||
| 2 | // StyleGAN3-R ops (bias_act, upfirdn2d, modulated_conv2d, filtered_lrelu), | ||
| 3 | // deformable conv2d, 2x pixel-shuffle upsample and DiT unpatchify. | ||
| 4 | |||
| 5 | #include "native_tensor_extra_util.h" | ||
| 6 | #include "api_internal.h" | ||
| 7 | |||
| 8 | #include <cmath> | ||
| 9 | #include <string> | ||
| 10 | |||
| 11 | namespace brotensor::api { | ||
| 12 | |||
| 13 | using namespace brotensor::api::extra; | ||
| 14 | |||
| 15 | namespace { | ||
| 16 | |||
| 17 | namespace ev = bronze::embed; | ||
| 18 | using brotensor::Tensor; | ||
| 19 | |||
| 20 | constexpr double kSqrt2 = 1.41421356237309504880; | ||
| 21 | |||
| 22 | // upfirdn2d's output extent along one axis: | ||
| 23 | // ((in*up + pad0 + pad1) - f) / down + 1, which needs the padded input to | ||
| 24 | // cover the filter. | ||
| 25 | 44 | inline void firExtent(const char* lb, const char* axis, int64_t in, int64_t up, int64_t down, | |
| 26 | int64_t pad0, int64_t pad1, int64_t f, int64_t& out) { | ||
| 27 | 44 | const int64_t padded = in * up + pad0 + pad1; | |
| 28 | 44 | if (padded < f) { | |
| 29 | ✗ | setError(std::string(lb) + ": the padded " + axis + " extent " + std::to_string(padded) + | |
| 30 | ✗ | " is smaller than the filter (" + std::to_string(f) + ")"); | |
| 31 | } | ||
| 32 | 44 | out = (padded - f) / down + 1; | |
| 33 | 44 | needExtent(lb, axis, out); | |
| 34 | 44 | } | |
| 35 | |||
| 36 | // The upfirdn2d argument block shared by the forward and backward. | ||
| 37 | 10 | inline void firArgs(const char* lb, const Tensor* X, const char* xn, const Tensor* f, | |
| 38 | int32_t N, int32_t C, int32_t H, int32_t W, int32_t fH, int32_t fW, | ||
| 39 | int32_t upX, int32_t upY, int32_t downX, int32_t downY, | ||
| 40 | int32_t padX0, int32_t padX1, int32_t padY0, int32_t padY1, | ||
| 41 | int64_t& Ho, int64_t& Wo) { | ||
| 42 | 10 | needFloat(lb, xn, X); | |
| 43 | 10 | needSameDtype(lb, "f", f, X); | |
| 44 | 10 | if (N < 1 || C < 1 || H < 1 || W < 1 || fH < 1 || fW < 1) { | |
| 45 | ✗ | setError(std::string(lb) + ": N, C, H, W, fH and fW must be >= 1"); | |
| 46 | } | ||
| 47 | 10 | if (upX < 1 || upY < 1 || downX < 1 || downY < 1) { | |
| 48 | ✗ | setError(std::string(lb) + ": up / down factors must be >= 1"); | |
| 49 | } | ||
| 50 | 10 | if (f->rows != fH || f->cols != fW) { | |
| 51 | ✗ | setError(std::string(lb) + ": f must be (fH, fW) = (" + std::to_string(fH) + ", " + | |
| 52 | ✗ | std::to_string(fW) + ")"); | |
| 53 | } | ||
| 54 | 10 | firExtent(lb, "H_out", H, upY, downY, padY0, padY1, fH, Ho); | |
| 55 | 10 | firExtent(lb, "W_out", W, upX, downX, padX0, padX1, fW, Wo); | |
| 56 | 10 | needExtent(lb, "C*H_out*W_out", prod({C, Ho, Wo})); | |
| 57 | 10 | } | |
| 58 | |||
| 59 | // modulated_conv2d's shared argument block. | ||
| 60 | 14 | inline void modConvArgs(const char* lb, const Tensor* X, const Tensor* Wt, const Tensor* s, | |
| 61 | int32_t N, int32_t Cin, int32_t H, int32_t Wd, int32_t Cout, | ||
| 62 | int32_t kH, int32_t kW, int32_t padH, int32_t padW, | ||
| 63 | int64_t& Ho, int64_t& Wo) { | ||
| 64 | 14 | needFloat(lb, "X", X); | |
| 65 | 14 | needSameDtype(lb, "W", Wt, X); | |
| 66 | 14 | needSameDtype(lb, "s", s, X); | |
| 67 | 14 | if (N < 1 || Cin < 1 || H < 1 || Wd < 1 || Cout < 1 || kH < 1 || kW < 1 || padH < 0 || padW < 0) { | |
| 68 | ✗ | setError(std::string(lb) + ": dims and kernel must be >= 1, padding >= 0"); | |
| 69 | } | ||
| 70 | 14 | Ho = static_cast<int64_t>(H) + 2 * static_cast<int64_t>(padH) - (kH - 1); | |
| 71 | 14 | Wo = static_cast<int64_t>(Wd) + 2 * static_cast<int64_t>(padW) - (kW - 1); | |
| 72 | 14 | needExtent(lb, "H_out", Ho); | |
| 73 | 14 | needExtent(lb, "W_out", Wo); | |
| 74 | 14 | needExtent(lb, "C_out*H_out*W_out", prod({Cout, Ho, Wo})); | |
| 75 | 14 | needElems(lb, "X", X, prod({N, Cin, H, Wd})); | |
| 76 | 14 | needElems(lb, "W", Wt, prod({Cout, Cin, kH, kW})); | |
| 77 | 14 | needElems(lb, "s", s, prod({N, Cin})); | |
| 78 | 14 | } | |
| 79 | |||
| 80 | // filtered_lrelu's shared argument block. | ||
| 81 | 8 | inline void flreluArgs(const char* lb, const Tensor* X, const Tensor* fu, const Tensor* fd, | |
| 82 | const Tensor* b, int32_t N, int32_t C, int32_t H, int32_t W, | ||
| 83 | int32_t up, int32_t down, int32_t padX0, int32_t padX1, | ||
| 84 | int32_t padY0, int32_t padY1, | ||
| 85 | int64_t& Hu, int64_t& Wu, int64_t& Ho, int64_t& Wo) { | ||
| 86 | 8 | needFloat(lb, "X", X); | |
| 87 | 8 | needSameDtype(lb, "fu", fu, X); | |
| 88 | 8 | needSameDtype(lb, "fd", fd, X); | |
| 89 | 8 | if (N < 1 || C < 1 || H < 1 || W < 1 || up < 1 || down < 1) { | |
| 90 | ✗ | setError(std::string(lb) + ": N, C, H, W, up and down must be >= 1"); | |
| 91 | } | ||
| 92 | 8 | if (fu->empty() || fd->empty()) { | |
| 93 | ✗ | setError(std::string(lb) + ": fu and fd must be non-empty filters"); | |
| 94 | } | ||
| 95 | 8 | needElems(lb, "X", X, prod({N, C, H, W})); | |
| 96 | 8 | optElems(lb, "b", b, C, X); | |
| 97 | 8 | firExtent(lb, "upsampled H", H, up, 1, padY0, padY1, fu->rows, Hu); | |
| 98 | 8 | firExtent(lb, "upsampled W", W, up, 1, padX0, padX1, fu->cols, Wu); | |
| 99 | 8 | needExtent(lb, "C*H_up*W_up", prod({C, Hu, Wu})); | |
| 100 | 8 | firExtent(lb, "H_out", Hu, 1, down, 0, 0, fd->rows, Ho); | |
| 101 | 8 | firExtent(lb, "W_out", Wu, 1, down, 0, 0, fd->cols, Wo); | |
| 102 | 8 | } | |
| 103 | |||
| 104 | } // namespace | ||
| 105 | |||
| 106 | 2 | void installTensorExtraConv(bronze::Value ns) { | |
| 107 | 2 | ev::ObjectBuilder b(ns); | |
| 108 | |||
| 109 | // ---- bias_act ---------------------------------------------------------- | ||
| 110 | 8 | b.def("biasActForward", 10, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 111 | 6 | constexpr const char* lb = "biasActForward"; | |
| 112 | 6 | Tensor* x = requireTensor(argAt(a, 0), lb, "X"); | |
| 113 | 6 | Tensor* bias = optionalTensor(argAt(a, 1), lb, "b"); | |
| 114 | 6 | const int32_t N = intArg(argAt(a, 2), 0); | |
| 115 | 6 | const int32_t C = intArg(argAt(a, 3), 0); | |
| 116 | 6 | const int32_t HW = intArg(argAt(a, 4), 0); | |
| 117 | 6 | const int32_t act = intArg(argAt(a, 5), 0); | |
| 118 | 6 | const double alpha = numArg(argAt(a, 6), 0.2); | |
| 119 | 6 | const double gain = numArg(argAt(a, 7), act == 1 ? kSqrt2 : 1.0); | |
| 120 | 6 | const double clamp = numArg(argAt(a, 8), -1.0); | |
| 121 | 6 | Tensor* Y = requireTensor(argAt(a, 9), lb, "Y"); | |
| 122 | |||
| 123 | 6 | needFloat(lb, "X", x); | |
| 124 | 6 | needRange(lb, "act", act, 0, 1); | |
| 125 | 6 | if (N < 1 || C < 1 || HW < 1) setError(std::string(lb) + ": N, C and HW must be >= 1"); | |
| 126 | 6 | needElems(lb, "X", x, prod({N, C, HW})); | |
| 127 | 6 | optElems(lb, "b", bias, C, x); | |
| 128 | 6 | BROTENSOR_API_TRY | |
| 129 | 12 | brotensor::bias_act_forward(*x, bias, N, C, HW, act, static_cast<float>(alpha), | |
| 130 | 6 | static_cast<float>(gain), static_cast<float>(clamp), *Y); | |
| 131 | 6 | BROTENSOR_API_CATCH(lb) | |
| 132 | 6 | return ev::undefined(); | |
| 133 | ✗ | }); | |
| 134 | |||
| 135 | 6 | b.def("biasActBackward", 12, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 136 | 4 | constexpr const char* lb = "biasActBackward"; | |
| 137 | 4 | Tensor* dy = requireTensor(argAt(a, 0), lb, "dY"); | |
| 138 | 4 | Tensor* x = requireTensor(argAt(a, 1), lb, "X"); | |
| 139 | 4 | Tensor* bias = optionalTensor(argAt(a, 2), lb, "b"); | |
| 140 | 4 | const int32_t N = intArg(argAt(a, 3), 0); | |
| 141 | 4 | const int32_t C = intArg(argAt(a, 4), 0); | |
| 142 | 4 | const int32_t HW = intArg(argAt(a, 5), 0); | |
| 143 | 4 | const int32_t act = intArg(argAt(a, 6), 0); | |
| 144 | 4 | const double alpha = numArg(argAt(a, 7), 0.2); | |
| 145 | 4 | const double gain = numArg(argAt(a, 8), act == 1 ? kSqrt2 : 1.0); | |
| 146 | 4 | const double clamp = numArg(argAt(a, 9), -1.0); | |
| 147 | 4 | Tensor* dX = requireTensor(argAt(a, 10), lb, "dX"); | |
| 148 | 4 | Tensor* dB = optionalTensor(argAt(a, 11), lb, "dB"); | |
| 149 | |||
| 150 | 4 | needFloat(lb, "X", x); | |
| 151 | 4 | needSameDtype(lb, "dY", dy, x); | |
| 152 | 4 | needRange(lb, "act", act, 0, 1); | |
| 153 | 4 | if (N < 1 || C < 1 || HW < 1) setError(std::string(lb) + ": N, C and HW must be >= 1"); | |
| 154 | 4 | const int64_t n = prod({N, C, HW}); | |
| 155 | 4 | needElems(lb, "X", x, n); | |
| 156 | 4 | needElems(lb, "dY", dy, n); | |
| 157 | 4 | optElems(lb, "b", bias, C, x); | |
| 158 | 4 | optElems(lb, "dB", dB, C, x); | |
| 159 | 4 | BROTENSOR_API_TRY | |
| 160 | 8 | brotensor::bias_act_backward(*dy, *x, bias, N, C, HW, act, static_cast<float>(alpha), | |
| 161 | 4 | static_cast<float>(gain), static_cast<float>(clamp), *dX, dB); | |
| 162 | 4 | BROTENSOR_API_CATCH(lb) | |
| 163 | 4 | return ev::undefined(); | |
| 164 | ✗ | }); | |
| 165 | |||
| 166 | // ---- upfirdn2d --------------------------------------------------------- | ||
| 167 | 6 | b.def("upfirdn2dForward", 19, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 168 | 4 | constexpr const char* lb = "upfirdn2dForward"; | |
| 169 | 4 | Tensor* X = requireTensor(argAt(a, 0), lb, "X"); | |
| 170 | 4 | Tensor* f = requireTensor(argAt(a, 1), lb, "f"); | |
| 171 | 4 | const int32_t N = intArg(argAt(a, 2), 0); | |
| 172 | 4 | const int32_t C = intArg(argAt(a, 3), 0); | |
| 173 | 4 | const int32_t H = intArg(argAt(a, 4), 0); | |
| 174 | 4 | const int32_t W = intArg(argAt(a, 5), 0); | |
| 175 | 4 | const int32_t fH = intArg(argAt(a, 6), f->rows); | |
| 176 | 4 | const int32_t fW = intArg(argAt(a, 7), f->cols); | |
| 177 | 4 | const int32_t upX = intArg(argAt(a, 8), 1); | |
| 178 | 4 | const int32_t upY = intArg(argAt(a, 9), 1); | |
| 179 | 4 | const int32_t downX = intArg(argAt(a, 10), 1); | |
| 180 | 4 | const int32_t downY = intArg(argAt(a, 11), 1); | |
| 181 | 4 | const int32_t padX0 = intArg(argAt(a, 12), 0); | |
| 182 | 4 | const int32_t padX1 = intArg(argAt(a, 13), 0); | |
| 183 | 4 | const int32_t padY0 = intArg(argAt(a, 14), 0); | |
| 184 | 4 | const int32_t padY1 = intArg(argAt(a, 15), 0); | |
| 185 | 4 | const bool flipFilter = boolArg(argAt(a, 16), false); | |
| 186 | 4 | const double gain = numArg(argAt(a, 17), 1.0); | |
| 187 | 4 | Tensor* Y = requireTensor(argAt(a, 18), lb, "Y"); | |
| 188 | |||
| 189 | 4 | int64_t Ho = 0, Wo = 0; | |
| 190 | 4 | firArgs(lb, X, "X", f, N, C, H, W, fH, fW, upX, upY, downX, downY, padX0, padX1, padY0, padY1, Ho, Wo); | |
| 191 | 4 | needElems(lb, "X", X, prod({N, C, H, W})); | |
| 192 | 4 | BROTENSOR_API_TRY | |
| 193 | 8 | brotensor::upfirdn2d_forward(*X, *f, N, C, H, W, fH, fW, upX, upY, downX, downY, | |
| 194 | 4 | padX0, padX1, padY0, padY1, flipFilter, | |
| 195 | 4 | static_cast<float>(gain), *Y); | |
| 196 | 4 | BROTENSOR_API_CATCH(lb) | |
| 197 | 4 | return ev::undefined(); | |
| 198 | ✗ | }); | |
| 199 | |||
| 200 | 4 | b.def("upfirdn2dBackward", 19, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 201 | 2 | constexpr const char* lb = "upfirdn2dBackward"; | |
| 202 | 2 | Tensor* dY = requireTensor(argAt(a, 0), lb, "dY"); | |
| 203 | 2 | Tensor* f = requireTensor(argAt(a, 1), lb, "f"); | |
| 204 | 2 | const int32_t N = intArg(argAt(a, 2), 0); | |
| 205 | 2 | const int32_t C = intArg(argAt(a, 3), 0); | |
| 206 | 2 | const int32_t H = intArg(argAt(a, 4), 0); | |
| 207 | 2 | const int32_t W = intArg(argAt(a, 5), 0); | |
| 208 | 2 | const int32_t fH = intArg(argAt(a, 6), f->rows); | |
| 209 | 2 | const int32_t fW = intArg(argAt(a, 7), f->cols); | |
| 210 | 2 | const int32_t upX = intArg(argAt(a, 8), 1); | |
| 211 | 2 | const int32_t upY = intArg(argAt(a, 9), 1); | |
| 212 | 2 | const int32_t downX = intArg(argAt(a, 10), 1); | |
| 213 | 2 | const int32_t downY = intArg(argAt(a, 11), 1); | |
| 214 | 2 | const int32_t padX0 = intArg(argAt(a, 12), 0); | |
| 215 | 2 | const int32_t padX1 = intArg(argAt(a, 13), 0); | |
| 216 | 2 | const int32_t padY0 = intArg(argAt(a, 14), 0); | |
| 217 | 2 | const int32_t padY1 = intArg(argAt(a, 15), 0); | |
| 218 | 2 | const bool flipFilter = boolArg(argAt(a, 16), false); | |
| 219 | 2 | const double gain = numArg(argAt(a, 17), 1.0); | |
| 220 | 2 | Tensor* dX = requireTensor(argAt(a, 18), lb, "dX"); | |
| 221 | |||
| 222 | 2 | int64_t Ho = 0, Wo = 0; | |
| 223 | 2 | firArgs(lb, dY, "dY", f, N, C, H, W, fH, fW, upX, upY, downX, downY, padX0, padX1, padY0, padY1, Ho, Wo); | |
| 224 | 2 | needExtent(lb, "C*H*W", prod({C, H, W})); | |
| 225 | 2 | needElems(lb, "dY", dY, prod({N, C, Ho, Wo})); | |
| 226 | 2 | BROTENSOR_API_TRY | |
| 227 | 4 | brotensor::upfirdn2d_backward(*dY, *f, N, C, H, W, fH, fW, upX, upY, downX, downY, | |
| 228 | 2 | padX0, padX1, padY0, padY1, flipFilter, | |
| 229 | 2 | static_cast<float>(gain), *dX); | |
| 230 | 2 | BROTENSOR_API_CATCH(lb) | |
| 231 | 2 | return ev::undefined(); | |
| 232 | ✗ | }); | |
| 233 | |||
| 234 | // ---- modulated_conv2d -------------------------------------------------- | ||
| 235 | 6 | b.def("modulatedConv2dForward", 16, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 236 | 4 | constexpr const char* lb = "modulatedConv2dForward"; | |
| 237 | 4 | Tensor* X = requireTensor(argAt(a, 0), lb, "X"); | |
| 238 | 4 | Tensor* W = requireTensor(argAt(a, 1), lb, "W"); | |
| 239 | 4 | Tensor* s = requireTensor(argAt(a, 2), lb, "s"); | |
| 240 | 4 | const int32_t N = intArg(argAt(a, 3), 0); | |
| 241 | 4 | const int32_t Cin = intArg(argAt(a, 4), 0); | |
| 242 | 4 | const int32_t H = intArg(argAt(a, 5), 0); | |
| 243 | 4 | const int32_t Wd = intArg(argAt(a, 6), 0); | |
| 244 | 4 | const int32_t Cout = intArg(argAt(a, 7), 0); | |
| 245 | 4 | const int32_t kH = intArg(argAt(a, 8), 1); | |
| 246 | 4 | const int32_t kW = intArg(argAt(a, 9), 1); | |
| 247 | 4 | const int32_t padH = intArg(argAt(a, 10), 0); | |
| 248 | 4 | const int32_t padW = intArg(argAt(a, 11), 0); | |
| 249 | 4 | const bool demodulate = boolArg(argAt(a, 12), true); | |
| 250 | 4 | const double eps = numArg(argAt(a, 13), 1e-8); | |
| 251 | 4 | Tensor* dcoef = requireTensor(argAt(a, 14), lb, "dcoef"); | |
| 252 | 4 | Tensor* Y = requireTensor(argAt(a, 15), lb, "Y"); | |
| 253 | |||
| 254 | 4 | int64_t Ho = 0, Wo = 0; | |
| 255 | 4 | modConvArgs(lb, X, W, s, N, Cin, H, Wd, Cout, kH, kW, padH, padW, Ho, Wo); | |
| 256 | 4 | BROTENSOR_API_TRY | |
| 257 | 8 | brotensor::modulated_conv2d_forward(*X, *W, *s, N, Cin, H, Wd, Cout, kH, kW, | |
| 258 | 4 | padH, padW, demodulate, static_cast<float>(eps), | |
| 259 | 4 | *dcoef, *Y); | |
| 260 | 4 | BROTENSOR_API_CATCH(lb) | |
| 261 | 4 | return ev::undefined(); | |
| 262 | ✗ | }); | |
| 263 | |||
| 264 | 4 | b.def("modulatedConv2dBackward", 19, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 265 | 2 | constexpr const char* lb = "modulatedConv2dBackward"; | |
| 266 | 2 | Tensor* x = requireTensor(argAt(a, 0), lb, "X"); | |
| 267 | 2 | Tensor* W = requireTensor(argAt(a, 1), lb, "W"); | |
| 268 | 2 | Tensor* s = requireTensor(argAt(a, 2), lb, "s"); | |
| 269 | 2 | Tensor* dcoef = requireTensor(argAt(a, 3), lb, "dcoef"); | |
| 270 | 2 | Tensor* dY = requireTensor(argAt(a, 4), lb, "dY"); | |
| 271 | 2 | const int32_t N = intArg(argAt(a, 5), 0); | |
| 272 | 2 | const int32_t Cin = intArg(argAt(a, 6), 0); | |
| 273 | 2 | const int32_t H = intArg(argAt(a, 7), 0); | |
| 274 | 2 | const int32_t Wd = intArg(argAt(a, 8), 0); | |
| 275 | 2 | const int32_t Cout = intArg(argAt(a, 9), 0); | |
| 276 | 2 | const int32_t kH = intArg(argAt(a, 10), 1); | |
| 277 | 2 | const int32_t kW = intArg(argAt(a, 11), 1); | |
| 278 | 2 | const int32_t padH = intArg(argAt(a, 12), 0); | |
| 279 | 2 | const int32_t padW = intArg(argAt(a, 13), 0); | |
| 280 | 2 | const bool demodulate = boolArg(argAt(a, 14), true); | |
| 281 | 2 | const double eps = numArg(argAt(a, 15), 1e-8); | |
| 282 | 2 | Tensor* dX = requireTensor(argAt(a, 16), lb, "dX"); | |
| 283 | 2 | Tensor* dW = optionalTensor(argAt(a, 17), lb, "dW"); | |
| 284 | 2 | Tensor* ds = requireTensor(argAt(a, 18), lb, "ds"); | |
| 285 | |||
| 286 | 2 | int64_t Ho = 0, Wo = 0; | |
| 287 | 2 | modConvArgs(lb, x, W, s, N, Cin, H, Wd, Cout, kH, kW, padH, padW, Ho, Wo); | |
| 288 | 2 | needDtype(lb, "dcoef", dcoef, brotensor::Dtype::FP32); | |
| 289 | 2 | needElems(lb, "dcoef", dcoef, prod({N, Cout})); | |
| 290 | 2 | needSameDtype(lb, "dY", dY, x); | |
| 291 | 2 | needElems(lb, "dY", dY, prod({N, Cout, Ho, Wo})); | |
| 292 | 2 | if (dW && !dW->empty()) { | |
| 293 | 2 | needSameDtype(lb, "dW", dW, x); | |
| 294 | 2 | needElems(lb, "dW", dW, prod({Cout, Cin, kH, kW})); | |
| 295 | 2 | } | |
| 296 | 2 | BROTENSOR_API_TRY | |
| 297 | 2 | Tensor skip; | |
| 298 | 2 | Tensor& dWref = (dW && !dW->empty()) ? *dW : skip; | |
| 299 | 4 | brotensor::modulated_conv2d_backward(*x, *W, *s, *dcoef, *dY, N, Cin, H, Wd, Cout, | |
| 300 | 2 | kH, kW, padH, padW, demodulate, | |
| 301 | 2 | static_cast<float>(eps), *dX, dWref, *ds); | |
| 302 | 2 | BROTENSOR_API_CATCH(lb) | |
| 303 | 2 | return ev::undefined(); | |
| 304 | ✗ | }); | |
| 305 | |||
| 306 | // ---- filtered_lrelu ---------------------------------------------------- | ||
| 307 | 4 | b.def("filteredLreluForward", 20, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 308 | 2 | constexpr const char* lb = "filteredLreluForward"; | |
| 309 | 2 | Tensor* X = requireTensor(argAt(a, 0), lb, "X"); | |
| 310 | 2 | Tensor* fu = requireTensor(argAt(a, 1), lb, "fu"); | |
| 311 | 2 | Tensor* fd = requireTensor(argAt(a, 2), lb, "fd"); | |
| 312 | 2 | Tensor* bias = optionalTensor(argAt(a, 3), lb, "b"); | |
| 313 | 2 | const int32_t N = intArg(argAt(a, 4), 0); | |
| 314 | 2 | const int32_t C = intArg(argAt(a, 5), 0); | |
| 315 | 2 | const int32_t H = intArg(argAt(a, 6), 0); | |
| 316 | 2 | const int32_t W = intArg(argAt(a, 7), 0); | |
| 317 | 2 | const int32_t up = intArg(argAt(a, 8), 1); | |
| 318 | 2 | const int32_t down = intArg(argAt(a, 9), 1); | |
| 319 | 2 | const int32_t padX0 = intArg(argAt(a, 10), 0); | |
| 320 | 2 | const int32_t padX1 = intArg(argAt(a, 11), 0); | |
| 321 | 2 | const int32_t padY0 = intArg(argAt(a, 12), 0); | |
| 322 | 2 | const int32_t padY1 = intArg(argAt(a, 13), 0); | |
| 323 | 2 | const double gain = numArg(argAt(a, 14), kSqrt2); | |
| 324 | 2 | const double slope = numArg(argAt(a, 15), 0.2); | |
| 325 | 2 | const double clamp = numArg(argAt(a, 16), -1.0); | |
| 326 | 2 | Tensor* upBuf = requireTensor(argAt(a, 17), lb, "upBuf"); | |
| 327 | 2 | Tensor* actBuf = requireTensor(argAt(a, 18), lb, "actBuf"); | |
| 328 | 2 | Tensor* Y = requireTensor(argAt(a, 19), lb, "Y"); | |
| 329 | |||
| 330 | 2 | int64_t Hu = 0, Wu = 0, Ho = 0, Wo = 0; | |
| 331 | 2 | flreluArgs(lb, X, fu, fd, bias, N, C, H, W, up, down, padX0, padX1, padY0, padY1, Hu, Wu, Ho, Wo); | |
| 332 | 2 | BROTENSOR_API_TRY | |
| 333 | 4 | brotensor::filtered_lrelu_forward(*X, *fu, *fd, bias, N, C, H, W, up, down, | |
| 334 | 2 | padX0, padX1, padY0, padY1, static_cast<float>(gain), | |
| 335 | 2 | static_cast<float>(slope), static_cast<float>(clamp), | |
| 336 | 2 | *upBuf, *actBuf, *Y); | |
| 337 | 2 | BROTENSOR_API_CATCH(lb) | |
| 338 | 2 | return ev::undefined(); | |
| 339 | ✗ | }); | |
| 340 | |||
| 341 | 4 | b.def("filteredLreluBackward", 21, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 342 | 2 | constexpr const char* lb = "filteredLreluBackward"; | |
| 343 | 2 | Tensor* dY = requireTensor(argAt(a, 0), lb, "dY"); | |
| 344 | 2 | Tensor* x = requireTensor(argAt(a, 1), lb, "X"); | |
| 345 | 2 | Tensor* fu = requireTensor(argAt(a, 2), lb, "fu"); | |
| 346 | 2 | Tensor* fd = requireTensor(argAt(a, 3), lb, "fd"); | |
| 347 | 2 | Tensor* bias = optionalTensor(argAt(a, 4), lb, "b"); | |
| 348 | 2 | const int32_t N = intArg(argAt(a, 5), 0); | |
| 349 | 2 | const int32_t C = intArg(argAt(a, 6), 0); | |
| 350 | 2 | const int32_t H = intArg(argAt(a, 7), 0); | |
| 351 | 2 | const int32_t W = intArg(argAt(a, 8), 0); | |
| 352 | 2 | const int32_t up = intArg(argAt(a, 9), 1); | |
| 353 | 2 | const int32_t down = intArg(argAt(a, 10), 1); | |
| 354 | 2 | const int32_t padX0 = intArg(argAt(a, 11), 0); | |
| 355 | 2 | const int32_t padX1 = intArg(argAt(a, 12), 0); | |
| 356 | 2 | const int32_t padY0 = intArg(argAt(a, 13), 0); | |
| 357 | 2 | const int32_t padY1 = intArg(argAt(a, 14), 0); | |
| 358 | 2 | const double gain = numArg(argAt(a, 15), kSqrt2); | |
| 359 | 2 | const double slope = numArg(argAt(a, 16), 0.2); | |
| 360 | 2 | const double clamp = numArg(argAt(a, 17), -1.0); | |
| 361 | 2 | Tensor* ub = optionalTensor(argAt(a, 18), lb, "upBuf"); | |
| 362 | 2 | Tensor* dX = requireTensor(argAt(a, 19), lb, "dX"); | |
| 363 | 2 | Tensor* dB = optionalTensor(argAt(a, 20), lb, "dB"); | |
| 364 | |||
| 365 | 2 | int64_t Hu = 0, Wu = 0, Ho = 0, Wo = 0; | |
| 366 | 2 | flreluArgs(lb, x, fu, fd, bias, N, C, H, W, up, down, padX0, padX1, padY0, padY1, Hu, Wu, Ho, Wo); | |
| 367 | 2 | needSameDtype(lb, "dY", dY, x); | |
| 368 | 2 | needElems(lb, "dY", dY, prod({N, C, Ho, Wo})); | |
| 369 | 2 | if (ub && !ub->empty()) { | |
| 370 | ✗ | needSameDtype(lb, "upBuf", ub, x); | |
| 371 | ✗ | needElems(lb, "upBuf", ub, prod({N, C, Hu, Wu})); | |
| 372 | ✗ | } | |
| 373 | 2 | optElems(lb, "dB", dB, C, x); | |
| 374 | 2 | BROTENSOR_API_TRY | |
| 375 | 2 | Tensor none; | |
| 376 | 2 | const Tensor& upRef = (ub && !ub->empty()) ? *ub : none; | |
| 377 | 4 | brotensor::filtered_lrelu_backward(*dY, *x, *fu, *fd, bias, N, C, H, W, up, down, | |
| 378 | 2 | padX0, padX1, padY0, padY1, static_cast<float>(gain), | |
| 379 | 2 | static_cast<float>(slope), static_cast<float>(clamp), | |
| 380 | 2 | upRef, *dX, dB); | |
| 381 | 2 | BROTENSOR_API_CATCH(lb) | |
| 382 | 2 | return ev::undefined(); | |
| 383 | ✗ | }); | |
| 384 | |||
| 385 | // ---- deformable conv2d ------------------------------------------------- | ||
| 386 | 12 | b.def("deformConv2dForward", 21, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 387 | 10 | constexpr const char* lb = "deformConv2dForward"; | |
| 388 | 10 | Tensor* x = requireTensor(argAt(a, 0), lb, "X"); | |
| 389 | 10 | Tensor* off = requireTensor(argAt(a, 1), lb, "offset"); | |
| 390 | 10 | Tensor* mask = optionalTensor(argAt(a, 2), lb, "mask"); | |
| 391 | 10 | Tensor* w = requireTensor(argAt(a, 3), lb, "Wt"); | |
| 392 | 10 | Tensor* bias = optionalTensor(argAt(a, 4), lb, "bias"); | |
| 393 | 10 | const int32_t N = intArg(argAt(a, 5), 0); | |
| 394 | 10 | const int32_t Cin = intArg(argAt(a, 6), 0); | |
| 395 | 10 | const int32_t H = intArg(argAt(a, 7), 0); | |
| 396 | 10 | const int32_t W = intArg(argAt(a, 8), 0); | |
| 397 | 10 | const int32_t Cout = intArg(argAt(a, 9), 0); | |
| 398 | 10 | const int32_t kH = intArg(argAt(a, 10), 1); | |
| 399 | 10 | const int32_t kW = intArg(argAt(a, 11), 1); | |
| 400 | 10 | const int32_t sH = intArg(argAt(a, 12), 1); | |
| 401 | 10 | const int32_t sW = intArg(argAt(a, 13), 1); | |
| 402 | 10 | const int32_t pH = intArg(argAt(a, 14), 0); | |
| 403 | 10 | const int32_t pW = intArg(argAt(a, 15), 0); | |
| 404 | 10 | const int32_t dH = intArg(argAt(a, 16), 1); | |
| 405 | 10 | const int32_t dW = intArg(argAt(a, 17), 1); | |
| 406 | 10 | const int32_t groups = intArg(argAt(a, 18), 1); | |
| 407 | 10 | const int32_t deformGroups = intArg(argAt(a, 19), 1); | |
| 408 | 10 | Tensor* Y = requireTensor(argAt(a, 20), lb, "Y"); | |
| 409 | |||
| 410 | 10 | needFloat(lb, "X", x); | |
| 411 | 10 | needSameDtype(lb, "offset", off, x); | |
| 412 | 10 | needSameDtype(lb, "Wt", w, x); | |
| 413 | 20 | if (N < 1 || Cin < 1 || H < 1 || W < 1 || Cout < 1 || kH < 1 || kW < 1 || sH < 1 || sW < 1 || | |
| 414 | 10 | pH < 0 || pW < 0 || dH < 1 || dW < 1) { | |
| 415 | ✗ | setError(std::string(lb) + ": dims, kernel, stride and dilation must be >= 1, padding >= 0"); | |
| 416 | } | ||
| 417 | 10 | if (groups < 1 || Cin % groups != 0 || Cout % groups != 0) { | |
| 418 | ✗ | setError(std::string(lb) + ": groups must divide C_in and C_out"); | |
| 419 | } | ||
| 420 | 10 | if (deformGroups < 1 || Cin % deformGroups != 0) { | |
| 421 | ✗ | setError(std::string(lb) + ": deformGroups must divide C_in"); | |
| 422 | } | ||
| 423 | 10 | const int64_t Ho = (static_cast<int64_t>(H) + 2 * pH - static_cast<int64_t>(dH) * (kH - 1) - 1) / sH + 1; | |
| 424 | 10 | const int64_t Wo = (static_cast<int64_t>(W) + 2 * pW - static_cast<int64_t>(dW) * (kW - 1) - 1) / sW + 1; | |
| 425 | 10 | needExtent(lb, "H_out", Ho); | |
| 426 | 10 | needExtent(lb, "W_out", Wo); | |
| 427 | 10 | needExtent(lb, "C_out*H_out*W_out", prod({Cout, Ho, Wo})); | |
| 428 | 10 | const int64_t taps = prod({deformGroups, kH, kW}); | |
| 429 | 10 | needElems(lb, "X", x, prod({N, Cin, H, W})); | |
| 430 | 10 | needElems(lb, "offset", off, prod({N, 2, taps, Ho, Wo})); | |
| 431 | 10 | optElems(lb, "mask", mask, prod({N, taps, Ho, Wo}), x); | |
| 432 | 10 | needElems(lb, "Wt", w, prod({Cout, Cin / groups, kH, kW})); | |
| 433 | 10 | optElems(lb, "bias", bias, Cout, x); | |
| 434 | 10 | BROTENSOR_API_TRY | |
| 435 | 20 | brotensor::deform_conv2d_forward(*x, *off, mask, *w, bias, N, Cin, H, W, Cout, kH, kW, | |
| 436 | 10 | sH, sW, pH, pW, dH, dW, groups, deformGroups, *Y); | |
| 437 | 10 | BROTENSOR_API_CATCH(lb) | |
| 438 | 10 | return ev::undefined(); | |
| 439 | ✗ | }); | |
| 440 | |||
| 441 | // ---- pixel-shuffle upsample + unpatchify ------------------------------- | ||
| 442 | 8 | b.def("pixelShuffleUpsample2xForward", 7, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 443 | 6 | constexpr const char* lb = "pixelShuffleUpsample2xForward"; | |
| 444 | 6 | Tensor* x = requireTensor(argAt(a, 0), lb, "X"); | |
| 445 | 6 | const int32_t N = intArg(argAt(a, 1), 0); | |
| 446 | 6 | const int32_t Cin = intArg(argAt(a, 2), 0); | |
| 447 | 6 | const int32_t H = intArg(argAt(a, 3), 0); | |
| 448 | 6 | const int32_t W = intArg(argAt(a, 4), 0); | |
| 449 | 6 | const int32_t Cout = intArg(argAt(a, 5), 0); | |
| 450 | 6 | Tensor* Y = requireTensor(argAt(a, 6), lb, "Y"); | |
| 451 | |||
| 452 | 6 | needFloat(lb, "X", x); | |
| 453 | 6 | if (N < 1 || Cin < 1 || H < 1 || W < 1 || Cout < 1 || (4 * static_cast<int64_t>(Cout)) % Cin != 0) { | |
| 454 | ✗ | setError(std::string(lb) + ": dims must be >= 1 and C_in must divide 4*C_out"); | |
| 455 | } | ||
| 456 | 6 | needElems(lb, "X", x, prod({N, Cin, H, W})); | |
| 457 | 6 | needExtent(lb, "C_out*2H*2W", prod({Cout, 2 * int64_t(H), 2 * int64_t(W)})); | |
| 458 | 6 | BROTENSOR_API_TRY | |
| 459 | 6 | brotensor::pixel_shuffle_upsample_2x_forward(*x, N, Cin, H, W, Cout, *Y); | |
| 460 | 6 | BROTENSOR_API_CATCH(lb) | |
| 461 | 6 | return ev::undefined(); | |
| 462 | ✗ | }); | |
| 463 | |||
| 464 | 8 | b.def("patchUnpackForward", 8, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 465 | 6 | constexpr const char* lb = "patchUnpackForward"; | |
| 466 | 6 | Tensor* t = requireTensor(argAt(a, 0), lb, "tokens"); | |
| 467 | 6 | const int32_t hp = intArg(argAt(a, 1), 0); | |
| 468 | 6 | const int32_t wp = intArg(argAt(a, 2), 0); | |
| 469 | 6 | const int32_t P = intArg(argAt(a, 3), 1); | |
| 470 | 6 | const int32_t Ctotal = intArg(argAt(a, 4), 0); | |
| 471 | 6 | const int32_t Ckeep = intArg(argAt(a, 5), Ctotal); | |
| 472 | 6 | const bool channelMajor = boolArg(argAt(a, 6), false); | |
| 473 | 6 | Tensor* Y = requireTensor(argAt(a, 7), lb, "Y"); | |
| 474 | |||
| 475 | 6 | needFloat(lb, "tokens", t); | |
| 476 | 6 | if (hp < 1 || wp < 1 || P < 1 || Ctotal < 1 || Ckeep < 1 || Ckeep > Ctotal) { | |
| 477 | 2 | setError(std::string(lb) + ": hp, wp, P, C_total must be >= 1 and 1 <= C_keep <= C_total"); | |
| 478 | } | ||
| 479 | 6 | const int64_t rowWidth = prod({P, P, Ctotal}); | |
| 480 | 6 | if (t->cols != rowWidth || t->rows < static_cast<int64_t>(hp) * wp) { | |
| 481 | 6 | setError(std::string(lb) + ": tokens must be (hp*wp, P*P*C_total) = (" + | |
| 482 | 4 | std::to_string(int64_t(hp) * wp) + ", " + std::to_string(rowWidth) + ")"); | |
| 483 | } | ||
| 484 | 4 | needExtent(lb, "C_keep*(hp*P)*(wp*P)", prod({Ckeep, hp, P, wp, P})); | |
| 485 | 4 | BROTENSOR_API_TRY | |
| 486 | 4 | brotensor::patch_unpack_forward(*t, hp, wp, P, Ctotal, Ckeep, channelMajor, *Y); | |
| 487 | 4 | BROTENSOR_API_CATCH(lb) | |
| 488 | 4 | return ev::undefined(); | |
| 489 | 2 | }); | |
| 490 | 2 | } | |
| 491 | |||
| 492 | } // namespace brotensor::api | ||
| 493 |