src/api/native_tensor_conv.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // native_tensor_conv.cpp — the `conv` group of bro.tensor: the 2-D spatial | ||
| 2 | // family (interp2d backward, pad2d/slice2d, max-pool/adaptive-avg-pool, | ||
| 3 | // transposed conv2d, conv3d, window partition/reverse, spatial merge, batch | ||
| 4 | // norm, image preprocessing), bound directly onto `bro.tensor` via bronze::embed. | ||
| 5 | |||
| 6 | #include "api_internal.h" | ||
| 7 | #include "native_tensor_extra_util.h" | ||
| 8 | |||
| 9 | #include <climits> | ||
| 10 | #include <cmath> | ||
| 11 | #include <cstdint> | ||
| 12 | #include <cstring> | ||
| 13 | #include <span> | ||
| 14 | #include <string> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | namespace brotensor::api { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | namespace ev = bronze::embed; | ||
| 22 | using brotensor::Tensor; | ||
| 23 | using brotensor::Dtype; | ||
| 24 | |||
| 25 | 688 | int32_t argInt(ev::Value v, int32_t def = 0) { | |
| 26 | 688 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 27 | 688 | double d = ev::toDouble(v); | |
| 28 | 688 | if (!std::isfinite(d) || d == 0.0) return 0; | |
| 29 | 604 | if (d >= -2147483648.0 && d <= 2147483647.0) { | |
| 30 | 604 | return static_cast<int32_t>(std::trunc(d)); | |
| 31 | } | ||
| 32 | ✗ | double m = std::fmod(std::trunc(d), 4294967296.0); | |
| 33 | ✗ | if (m >= 2147483648.0) m -= 4294967296.0; | |
| 34 | ✗ | else if (m < -2147483648.0) m += 4294967296.0; | |
| 35 | ✗ | return static_cast<int32_t>(m); | |
| 36 | 688 | } | |
| 37 | |||
| 38 | 32 | double argNum(ev::Value v, double def = 0.0) { | |
| 39 | 32 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 40 | 32 | return ev::toDouble(v); | |
| 41 | 32 | } | |
| 42 | |||
| 43 | 12 | uint8_t byteFromDouble(double d) { | |
| 44 | 12 | if (!std::isfinite(d) || d == 0.0) return 0; | |
| 45 | 12 | if (d >= -2147483648.0 && d <= 2147483647.0) { | |
| 46 | 12 | return static_cast<uint8_t>(static_cast<int32_t>(std::trunc(d)) & 0xFF); | |
| 47 | } | ||
| 48 | ✗ | double m = std::fmod(std::trunc(d), 256.0); | |
| 49 | ✗ | if (m < 0.0) m += 256.0; | |
| 50 | ✗ | return static_cast<uint8_t>(m); | |
| 51 | 12 | } | |
| 52 | |||
| 53 | // Transposed-conv geometry (torch ConvTranspose2d): positive kernel, stride, | ||
| 54 | // dilation and groups dividing both channel counts; the output extent is | ||
| 55 | // (in-1)*s - 2p + d*(k-1) + op + 1. | ||
| 56 | 10 | void convTGeom(const char* L, int64_t C_in, int64_t C_out, int64_t H, int64_t W, | |
| 57 | int64_t kH, int64_t kW, int64_t sH, int64_t sW, int64_t pH, int64_t pW, | ||
| 58 | int64_t opH, int64_t opW, int64_t dH, int64_t dW, int64_t groups, | ||
| 59 | int64_t& H_out, int64_t& W_out) { | ||
| 60 | 10 | needPositive(L, {kH, kW, sH, sW, dH, dW, groups}); | |
| 61 | 10 | needNonNegative(L, {C_in, C_out, H, W, pH, pW, opH, opW}); | |
| 62 | 10 | if (C_in % groups != 0 || C_out % groups != 0) { | |
| 63 | ✗ | setError(std::string(L) + ": groups must divide C_in and C_out"); | |
| 64 | } | ||
| 65 | 10 | H_out = (H - 1) * sH - 2 * pH + dH * (kH - 1) + opH + 1; | |
| 66 | 10 | W_out = (W - 1) * sW - 2 * pW + dW * (kW - 1) + opW + 1; | |
| 67 | 10 | if (H_out < 0) H_out = 0; | |
| 68 | 10 | if (W_out < 0) W_out = 0; | |
| 69 | 10 | } | |
| 70 | |||
| 71 | // Idx values address the per-channel H*W input plane on the device: read | ||
| 72 | // them back and range-check them (-1 = "all padding" is skipped by the op). | ||
| 73 | 2 | void needPoolIndices(const char* L, const Tensor* idx, int64_t n, int64_t plane) { | |
| 74 | 2 | if (idx->dtype != Dtype::INT32) { | |
| 75 | ✗ | setError(std::string(L) + ": Idx must be INT32 (the forward's output)"); | |
| 76 | } | ||
| 77 | 2 | needElems(L, "Idx", idx, n); | |
| 78 | 2 | if (n == 0) return; | |
| 79 | 2 | Tensor host; | |
| 80 | 2 | BROTENSOR_API_TRY | |
| 81 | 2 | host = idx->to(brotensor::Device::CPU); | |
| 82 | 2 | BROTENSOR_API_CATCH(L) | |
| 83 | 2 | const auto* v = static_cast<const int32_t*>(host.host_raw()); | |
| 84 | 4 | for (int64_t i = 0; i < n; ++i) { | |
| 85 | 2 | if (v[i] < -1 || v[i] >= plane) { | |
| 86 | ✗ | setError(std::string(L) + ": Idx[" + std::to_string(i) + "] = " + | |
| 87 | ✗ | std::to_string(v[i]) + " is outside the " + std::to_string(plane) + | |
| 88 | "-pixel input plane"); | ||
| 89 | } | ||
| 90 | 2 | } | |
| 91 | 2 | } | |
| 92 | |||
| 93 | 8 | void runImageU8ToF32(const char* L, const uint8_t* src, int32_t N, int32_t H, int32_t W, | |
| 94 | int32_t C, int64_t want, double scale, double bias, Tensor* yt) { | ||
| 95 | 8 | if (want > INT_MAX) { | |
| 96 | ✗ | setError(std::string(L) + ": N*H*W*C exceeds INT_MAX"); | |
| 97 | } | ||
| 98 | 8 | BROTENSOR_API_TRY | |
| 99 | 8 | const brotensor::Device dev = yt->data ? yt->device : brotensor::default_device(); | |
| 100 | 8 | if (dev == brotensor::Device::CPU || want == 0) { | |
| 101 | ✗ | brotensor::image_u8_to_f32_nhwc_to_nchw( | |
| 102 | ✗ | src, N, H, W, C, static_cast<float>(scale), static_cast<float>(bias), *yt); | |
| 103 | ✗ | } else { | |
| 104 | 8 | Tensor staged = Tensor::zeros_on( | |
| 105 | 8 | brotensor::Device::CPU, static_cast<int>(want), 1, Dtype::INT8); | |
| 106 | 8 | std::memcpy(staged.host_raw_mut(), src, static_cast<size_t>(want)); | |
| 107 | 8 | staged = staged.to(dev); | |
| 108 | 8 | brotensor::image_u8_to_f32_nhwc_to_nchw( | |
| 109 | 8 | static_cast<const uint8_t*>(staged.data), | |
| 110 | 8 | N, H, W, C, static_cast<float>(scale), static_cast<float>(bias), *yt); | |
| 111 | 8 | } | |
| 112 | 8 | BROTENSOR_API_CATCH(L) | |
| 113 | 8 | } | |
| 114 | |||
| 115 | // ---- interp2d backward ----------------------------------------------------- | ||
| 116 | |||
| 117 | 2 | ev::Value jsInterp2dBackward(ev::Value, std::span<const ev::Value> args) { | |
| 118 | 2 | const char* L = "interp2dBackward"; | |
| 119 | 2 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 120 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 121 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 122 | 2 | int32_t H_in = argInt(arg(args, 3), 0); | |
| 123 | 2 | int32_t W_in = argInt(arg(args, 4), 0); | |
| 124 | 2 | int32_t H_out = argInt(arg(args, 5), 0); | |
| 125 | 2 | int32_t W_out = argInt(arg(args, 6), 0); | |
| 126 | 2 | int32_t mode = argInt(arg(args, 7), 1); | |
| 127 | 2 | Tensor* dX = requireTensor(arg(args, 8), L, "dX"); | |
| 128 | 2 | needNonNegative(L, {N, C, H_in, W_in}); | |
| 129 | 2 | needElems(L, "dY", dY, elems({N, C, H_out, W_out})); | |
| 130 | 2 | BROTENSOR_API_TRY | |
| 131 | 2 | brotensor::interp2d_backward(*dY, N, C, H_in, W_in, H_out, W_out, mode, *dX); | |
| 132 | 2 | BROTENSOR_API_CATCH(L) | |
| 133 | 2 | return ev::undefined(); | |
| 134 | ✗ | } | |
| 135 | |||
| 136 | // ---- pad2d / slice2d ------------------------------------------------------- | ||
| 137 | |||
| 138 | 2 | ev::Value jsPad2dForward(ev::Value, std::span<const ev::Value> args) { | |
| 139 | 2 | const char* L = "pad2dForward"; | |
| 140 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 141 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 142 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 143 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 144 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 145 | 2 | int32_t padT = argInt(arg(args, 5), 0); | |
| 146 | 2 | int32_t padB = argInt(arg(args, 6), 0); | |
| 147 | 2 | int32_t padL = argInt(arg(args, 7), 0); | |
| 148 | 2 | int32_t padR = argInt(arg(args, 8), 0); | |
| 149 | 2 | int32_t mode = argInt(arg(args, 9), 0); | |
| 150 | 2 | Tensor* y = requireTensor(arg(args, 10), L, "Y"); | |
| 151 | 2 | needNonNegative(L, {padT, padB, padL, padR}); | |
| 152 | 2 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 153 | 2 | BROTENSOR_API_TRY | |
| 154 | 2 | brotensor::pad2d_forward(*x, N, C, H, W, padT, padB, padL, padR, mode, *y); | |
| 155 | 2 | BROTENSOR_API_CATCH(L) | |
| 156 | 2 | return ev::undefined(); | |
| 157 | ✗ | } | |
| 158 | |||
| 159 | 2 | ev::Value jsPad2dBackward(ev::Value, std::span<const ev::Value> args) { | |
| 160 | 2 | const char* L = "pad2dBackward"; | |
| 161 | 2 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 162 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 163 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 164 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 165 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 166 | 2 | int32_t padT = argInt(arg(args, 5), 0); | |
| 167 | 2 | int32_t padB = argInt(arg(args, 6), 0); | |
| 168 | 2 | int32_t padL = argInt(arg(args, 7), 0); | |
| 169 | 2 | int32_t padR = argInt(arg(args, 8), 0); | |
| 170 | 2 | int32_t mode = argInt(arg(args, 9), 0); | |
| 171 | 2 | Tensor* dX = requireTensor(arg(args, 10), L, "dX"); | |
| 172 | 2 | needNonNegative(L, {N, C, H, W, padT, padB, padL, padR}); | |
| 173 | 4 | needElems(L, "dY", dY, elems({N, C, static_cast<int64_t>(H) + padT + padB, | |
| 174 | 2 | static_cast<int64_t>(W) + padL + padR})); | |
| 175 | 2 | BROTENSOR_API_TRY | |
| 176 | 2 | brotensor::pad2d_backward(*dY, N, C, H, W, padT, padB, padL, padR, mode, *dX); | |
| 177 | 2 | BROTENSOR_API_CATCH(L) | |
| 178 | 2 | return ev::undefined(); | |
| 179 | ✗ | } | |
| 180 | |||
| 181 | 2 | ev::Value jsSlice2dForward(ev::Value, std::span<const ev::Value> args) { | |
| 182 | 2 | const char* L = "slice2dForward"; | |
| 183 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 184 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 185 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 186 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 187 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 188 | 2 | int32_t h0 = argInt(arg(args, 5), 0); | |
| 189 | 2 | int32_t w0 = argInt(arg(args, 6), 0); | |
| 190 | 2 | int32_t H_out = argInt(arg(args, 7), 0); | |
| 191 | 2 | int32_t W_out = argInt(arg(args, 8), 0); | |
| 192 | 2 | Tensor* y = requireTensor(arg(args, 9), L, "Y"); | |
| 193 | 2 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 194 | 2 | BROTENSOR_API_TRY | |
| 195 | 2 | brotensor::slice2d_forward(*x, N, C, H, W, h0, w0, H_out, W_out, *y); | |
| 196 | 2 | BROTENSOR_API_CATCH(L) | |
| 197 | 2 | return ev::undefined(); | |
| 198 | ✗ | } | |
| 199 | |||
| 200 | 2 | ev::Value jsSlice2dBackward(ev::Value, std::span<const ev::Value> args) { | |
| 201 | 2 | const char* L = "slice2dBackward"; | |
| 202 | 2 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 203 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 204 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 205 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 206 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 207 | 2 | int32_t h0 = argInt(arg(args, 5), 0); | |
| 208 | 2 | int32_t w0 = argInt(arg(args, 6), 0); | |
| 209 | 2 | int32_t H_out = argInt(arg(args, 7), 0); | |
| 210 | 2 | int32_t W_out = argInt(arg(args, 8), 0); | |
| 211 | 2 | Tensor* dX = requireTensor(arg(args, 9), L, "dX"); | |
| 212 | 2 | needElems(L, "dY", dY, elems({N, C, H_out, W_out})); | |
| 213 | 2 | BROTENSOR_API_TRY | |
| 214 | 2 | brotensor::slice2d_backward(*dY, N, C, H, W, h0, w0, H_out, W_out, *dX); | |
| 215 | 2 | BROTENSOR_API_CATCH(L) | |
| 216 | 2 | return ev::undefined(); | |
| 217 | ✗ | } | |
| 218 | |||
| 219 | // ---- max pool / adaptive average pool -------------------------------------- | ||
| 220 | |||
| 221 | 4 | ev::Value jsMaxPool2dForward(ev::Value, std::span<const ev::Value> args) { | |
| 222 | 4 | const char* L = "maxPool2dForward"; | |
| 223 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 224 | 4 | int32_t N = argInt(arg(args, 1), 0); | |
| 225 | 4 | int32_t C = argInt(arg(args, 2), 0); | |
| 226 | 4 | int32_t H = argInt(arg(args, 3), 0); | |
| 227 | 4 | int32_t W = argInt(arg(args, 4), 0); | |
| 228 | 4 | int32_t kH = argInt(arg(args, 5), 0); | |
| 229 | 4 | int32_t kW = argInt(arg(args, 6), 0); | |
| 230 | 4 | int32_t sH = argInt(arg(args, 7), 1); | |
| 231 | 4 | int32_t sW = argInt(arg(args, 8), 1); | |
| 232 | 4 | int32_t padH = argInt(arg(args, 9), 0); | |
| 233 | 4 | int32_t padW = argInt(arg(args, 10), 0); | |
| 234 | 4 | Tensor* y = requireTensor(arg(args, 11), L, "Y"); | |
| 235 | 4 | Tensor* idx = requireTensor(arg(args, 12), L, "Idx"); | |
| 236 | 4 | needPositive(L, {kH, kW, sH, sW}); | |
| 237 | 4 | needNonNegative(L, {padH, padW}); | |
| 238 | 4 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 239 | 4 | BROTENSOR_API_TRY | |
| 240 | 4 | brotensor::max_pool2d_forward(*x, N, C, H, W, kH, kW, sH, sW, padH, padW, *y, *idx); | |
| 241 | 4 | BROTENSOR_API_CATCH(L) | |
| 242 | 4 | return ev::undefined(); | |
| 243 | ✗ | } | |
| 244 | |||
| 245 | 2 | ev::Value jsMaxPool2dBackward(ev::Value, std::span<const ev::Value> args) { | |
| 246 | 2 | const char* L = "maxPool2dBackward"; | |
| 247 | 2 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 248 | 2 | Tensor* idx = requireTensor(arg(args, 1), L, "Idx"); | |
| 249 | 2 | int32_t N = argInt(arg(args, 2), 0); | |
| 250 | 2 | int32_t C = argInt(arg(args, 3), 0); | |
| 251 | 2 | int32_t H = argInt(arg(args, 4), 0); | |
| 252 | 2 | int32_t W = argInt(arg(args, 5), 0); | |
| 253 | 2 | int32_t H_out = argInt(arg(args, 6), 0); | |
| 254 | 2 | int32_t W_out = argInt(arg(args, 7), 0); | |
| 255 | 2 | Tensor* dX = requireTensor(arg(args, 8), L, "dX"); | |
| 256 | 2 | needNonNegative(L, {N, C, H, W}); | |
| 257 | 2 | const int64_t n = elems({N, C, H_out, W_out}); | |
| 258 | 2 | needElems(L, "dY", dY, n); | |
| 259 | 2 | needPoolIndices(L, idx, n, elems({H, W})); | |
| 260 | 2 | BROTENSOR_API_TRY | |
| 261 | 2 | brotensor::max_pool2d_backward(*dY, *idx, N, C, H, W, H_out, W_out, *dX); | |
| 262 | 2 | BROTENSOR_API_CATCH(L) | |
| 263 | 2 | return ev::undefined(); | |
| 264 | ✗ | } | |
| 265 | |||
| 266 | 2 | ev::Value jsAdaptiveAvgPool2dForward(ev::Value, std::span<const ev::Value> args) { | |
| 267 | 2 | const char* L = "adaptiveAvgPool2dForward"; | |
| 268 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 269 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 270 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 271 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 272 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 273 | 2 | int32_t H_out = argInt(arg(args, 5), 0); | |
| 274 | 2 | int32_t W_out = argInt(arg(args, 6), 0); | |
| 275 | 2 | Tensor* y = requireTensor(arg(args, 7), L, "Y"); | |
| 276 | 2 | needPositive(L, {H_out, W_out}); | |
| 277 | 2 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 278 | 2 | BROTENSOR_API_TRY | |
| 279 | 2 | brotensor::adaptive_avg_pool2d_forward(*x, N, C, H, W, H_out, W_out, *y); | |
| 280 | 2 | BROTENSOR_API_CATCH(L) | |
| 281 | 2 | return ev::undefined(); | |
| 282 | ✗ | } | |
| 283 | |||
| 284 | 2 | ev::Value jsAdaptiveAvgPool2dBackward(ev::Value, std::span<const ev::Value> args) { | |
| 285 | 2 | const char* L = "adaptiveAvgPool2dBackward"; | |
| 286 | 2 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 287 | 2 | int32_t N = argInt(arg(args, 1), 0); | |
| 288 | 2 | int32_t C = argInt(arg(args, 2), 0); | |
| 289 | 2 | int32_t H = argInt(arg(args, 3), 0); | |
| 290 | 2 | int32_t W = argInt(arg(args, 4), 0); | |
| 291 | 2 | int32_t H_out = argInt(arg(args, 5), 0); | |
| 292 | 2 | int32_t W_out = argInt(arg(args, 6), 0); | |
| 293 | 2 | Tensor* dX = requireTensor(arg(args, 7), L, "dX"); | |
| 294 | 2 | needPositive(L, {H_out, W_out}); | |
| 295 | 2 | needNonNegative(L, {N, C, H, W}); | |
| 296 | 2 | needElems(L, "dY", dY, elems({N, C, H_out, W_out})); | |
| 297 | 2 | BROTENSOR_API_TRY | |
| 298 | 2 | brotensor::adaptive_avg_pool2d_backward(*dY, N, C, H, W, H_out, W_out, *dX); | |
| 299 | 2 | BROTENSOR_API_CATCH(L) | |
| 300 | 2 | return ev::undefined(); | |
| 301 | ✗ | } | |
| 302 | |||
| 303 | // ---- transposed conv2d ----------------------------------------------------- | ||
| 304 | |||
| 305 | 4 | ev::Value jsConvTranspose2dForward(ev::Value, std::span<const ev::Value> args) { | |
| 306 | 4 | const char* L = "convTranspose2dForward"; | |
| 307 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 308 | 4 | Tensor* wt = requireTensor(arg(args, 1), L, "Wt"); | |
| 309 | 4 | Tensor* bias = optionalTensor(arg(args, 2), L, "bias"); | |
| 310 | 4 | int32_t N = argInt(arg(args, 3), 0); | |
| 311 | 4 | int32_t C_in = argInt(arg(args, 4), 0); | |
| 312 | 4 | int32_t H = argInt(arg(args, 5), 0); | |
| 313 | 4 | int32_t W = argInt(arg(args, 6), 0); | |
| 314 | 4 | int32_t C_out = argInt(arg(args, 7), 0); | |
| 315 | 4 | int32_t kH = argInt(arg(args, 8), 0); | |
| 316 | 4 | int32_t kW = argInt(arg(args, 9), 0); | |
| 317 | 4 | int32_t sH = argInt(arg(args, 10), 1); | |
| 318 | 4 | int32_t sW = argInt(arg(args, 11), 1); | |
| 319 | 4 | int32_t pH = argInt(arg(args, 12), 0); | |
| 320 | 4 | int32_t pW = argInt(arg(args, 13), 0); | |
| 321 | 4 | int32_t opH = argInt(arg(args, 14), 0); | |
| 322 | 4 | int32_t opW = argInt(arg(args, 15), 0); | |
| 323 | 4 | int32_t dH = argInt(arg(args, 16), 1); | |
| 324 | 4 | int32_t dW = argInt(arg(args, 17), 1); | |
| 325 | 4 | int32_t groups = argInt(arg(args, 18), 1); | |
| 326 | 4 | Tensor* y = requireTensor(arg(args, 19), L, "Y"); | |
| 327 | 4 | int64_t H_out = 0, W_out = 0; | |
| 328 | 4 | convTGeom(L, C_in, C_out, H, W, kH, kW, sH, sW, pH, pW, opH, opW, dH, dW, groups, H_out, W_out); | |
| 329 | 16 | needOperands(L, x, { | |
| 330 | 4 | {"X", x, elems({N, C_in, H, W})}, | |
| 331 | 4 | {"Wt", wt, elems({C_in, C_out / groups, kH, kW})}, | |
| 332 | 4 | {"bias", bias, C_out}, | |
| 333 | }); | ||
| 334 | 4 | BROTENSOR_API_TRY | |
| 335 | 4 | brotensor::conv_transpose2d_forward( | |
| 336 | 4 | *x, *wt, bias, N, C_in, H, W, C_out, kH, kW, | |
| 337 | 4 | sH, sW, pH, pW, opH, opW, dH, dW, groups, *y); | |
| 338 | 4 | BROTENSOR_API_CATCH(L) | |
| 339 | 4 | return ev::undefined(); | |
| 340 | ✗ | } | |
| 341 | |||
| 342 | 2 | ev::Value jsConvTranspose2dBackwardInput(ev::Value, std::span<const ev::Value> args) { | |
| 343 | 2 | const char* L = "convTranspose2dBackwardInput"; | |
| 344 | 2 | Tensor* wt = requireTensor(arg(args, 0), L, "Wt"); | |
| 345 | 2 | Tensor* dY = requireTensor(arg(args, 1), L, "dY"); | |
| 346 | 2 | int32_t N = argInt(arg(args, 2), 0); | |
| 347 | 2 | int32_t C_in = argInt(arg(args, 3), 0); | |
| 348 | 2 | int32_t H = argInt(arg(args, 4), 0); | |
| 349 | 2 | int32_t W = argInt(arg(args, 5), 0); | |
| 350 | 2 | int32_t C_out = argInt(arg(args, 6), 0); | |
| 351 | 2 | int32_t kH = argInt(arg(args, 7), 0); | |
| 352 | 2 | int32_t kW = argInt(arg(args, 8), 0); | |
| 353 | 2 | int32_t sH = argInt(arg(args, 9), 1); | |
| 354 | 2 | int32_t sW = argInt(arg(args, 10), 1); | |
| 355 | 2 | int32_t pH = argInt(arg(args, 11), 0); | |
| 356 | 2 | int32_t pW = argInt(arg(args, 12), 0); | |
| 357 | 2 | int32_t opH = argInt(arg(args, 13), 0); | |
| 358 | 2 | int32_t opW = argInt(arg(args, 14), 0); | |
| 359 | 2 | int32_t dH = argInt(arg(args, 15), 1); | |
| 360 | 2 | int32_t dW = argInt(arg(args, 16), 1); | |
| 361 | 2 | int32_t groups = argInt(arg(args, 17), 1); | |
| 362 | 2 | Tensor* dX = requireTensor(arg(args, 18), L, "dX"); | |
| 363 | 2 | int64_t H_out = 0, W_out = 0; | |
| 364 | 2 | convTGeom(L, C_in, C_out, H, W, kH, kW, sH, sW, pH, pW, opH, opW, dH, dW, groups, H_out, W_out); | |
| 365 | 2 | needNonNegative(L, {N}); | |
| 366 | 6 | needOperands(L, wt, { | |
| 367 | 2 | {"Wt", wt, elems({C_in, C_out / groups, kH, kW})}, | |
| 368 | 2 | {"dY", dY, elems({N, C_out, H_out, W_out})}, | |
| 369 | }); | ||
| 370 | 2 | BROTENSOR_API_TRY | |
| 371 | 2 | brotensor::conv_transpose2d_backward_input( | |
| 372 | 2 | *wt, *dY, N, C_in, H, W, C_out, kH, kW, | |
| 373 | 2 | sH, sW, pH, pW, opH, opW, dH, dW, groups, *dX); | |
| 374 | 2 | BROTENSOR_API_CATCH(L) | |
| 375 | 2 | return ev::undefined(); | |
| 376 | ✗ | } | |
| 377 | |||
| 378 | 2 | ev::Value jsConvTranspose2dBackwardWeight(ev::Value, std::span<const ev::Value> args) { | |
| 379 | 2 | const char* L = "convTranspose2dBackwardWeight"; | |
| 380 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 381 | 2 | Tensor* dY = requireTensor(arg(args, 1), L, "dY"); | |
| 382 | 2 | int32_t N = argInt(arg(args, 2), 0); | |
| 383 | 2 | int32_t C_in = argInt(arg(args, 3), 0); | |
| 384 | 2 | int32_t H = argInt(arg(args, 4), 0); | |
| 385 | 2 | int32_t W = argInt(arg(args, 5), 0); | |
| 386 | 2 | int32_t C_out = argInt(arg(args, 6), 0); | |
| 387 | 2 | int32_t kH = argInt(arg(args, 7), 0); | |
| 388 | 2 | int32_t kW = argInt(arg(args, 8), 0); | |
| 389 | 2 | int32_t sH = argInt(arg(args, 9), 1); | |
| 390 | 2 | int32_t sW = argInt(arg(args, 10), 1); | |
| 391 | 2 | int32_t pH = argInt(arg(args, 11), 0); | |
| 392 | 2 | int32_t pW = argInt(arg(args, 12), 0); | |
| 393 | 2 | int32_t opH = argInt(arg(args, 13), 0); | |
| 394 | 2 | int32_t opW = argInt(arg(args, 14), 0); | |
| 395 | 2 | int32_t dH = argInt(arg(args, 15), 1); | |
| 396 | 2 | int32_t dW = argInt(arg(args, 16), 1); | |
| 397 | 2 | int32_t groups = argInt(arg(args, 17), 1); | |
| 398 | 2 | Tensor* dWt = requireTensor(arg(args, 18), L, "dWt"); | |
| 399 | 2 | int64_t H_out = 0, W_out = 0; | |
| 400 | 2 | convTGeom(L, C_in, C_out, H, W, kH, kW, sH, sW, pH, pW, opH, opW, dH, dW, groups, H_out, W_out); | |
| 401 | 2 | needNonNegative(L, {N}); | |
| 402 | // dWt accumulates — "pre-zeroed by caller". | ||
| 403 | 8 | needOperands(L, x, { | |
| 404 | 2 | {"X", x, elems({N, C_in, H, W})}, | |
| 405 | 2 | {"dY", dY, elems({N, C_out, H_out, W_out})}, | |
| 406 | 2 | {"dWt", dWt, elems({C_in, C_out / groups, kH, kW})}, | |
| 407 | }); | ||
| 408 | 2 | BROTENSOR_API_TRY | |
| 409 | 2 | brotensor::conv_transpose2d_backward_weight( | |
| 410 | 2 | *x, *dY, N, C_in, H, W, C_out, kH, kW, | |
| 411 | 2 | sH, sW, pH, pW, opH, opW, dH, dW, groups, *dWt); | |
| 412 | 2 | BROTENSOR_API_CATCH(L) | |
| 413 | 2 | return ev::undefined(); | |
| 414 | ✗ | } | |
| 415 | |||
| 416 | 4 | ev::Value jsConvTranspose2dBackwardBias(ev::Value, std::span<const ev::Value> args) { | |
| 417 | 4 | const char* L = "convTranspose2dBackwardBias"; | |
| 418 | 4 | Tensor* dY = requireTensor(arg(args, 0), L, "dY"); | |
| 419 | 4 | int32_t N = argInt(arg(args, 1), 0); | |
| 420 | 4 | int32_t C_out = argInt(arg(args, 2), 0); | |
| 421 | 4 | int32_t H_out = argInt(arg(args, 3), 0); | |
| 422 | 4 | int32_t W_out = argInt(arg(args, 4), 0); | |
| 423 | 4 | Tensor* dB = requireTensor(arg(args, 5), L, "dB"); | |
| 424 | 4 | needOperands(L, dY, {{"dY", dY, elems({N, C_out, H_out, W_out})}, {"dB", dB, C_out}}); | |
| 425 | 4 | BROTENSOR_API_TRY | |
| 426 | 4 | brotensor::conv_transpose2d_backward_bias(*dY, N, C_out, H_out, W_out, *dB); | |
| 427 | 4 | BROTENSOR_API_CATCH(L) | |
| 428 | 4 | return ev::undefined(); | |
| 429 | ✗ | } | |
| 430 | |||
| 431 | // ---- conv3d (NCTHW) -------------------------------------------------------- | ||
| 432 | |||
| 433 | 6 | ev::Value jsConv3dForward(ev::Value, std::span<const ev::Value> args) { | |
| 434 | 6 | const char* L = "conv3dForward"; | |
| 435 | 6 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 436 | 6 | Tensor* wt = requireTensor(arg(args, 1), L, "Wt"); | |
| 437 | 6 | Tensor* bias = optionalTensor(arg(args, 2), L, "bias"); | |
| 438 | 6 | int32_t N = argInt(arg(args, 3), 0); | |
| 439 | 6 | int32_t C_in = argInt(arg(args, 4), 0); | |
| 440 | 6 | int32_t T_in = argInt(arg(args, 5), 0); | |
| 441 | 6 | int32_t H = argInt(arg(args, 6), 0); | |
| 442 | 6 | int32_t W = argInt(arg(args, 7), 0); | |
| 443 | 6 | int32_t C_out = argInt(arg(args, 8), 0); | |
| 444 | 6 | int32_t kT = argInt(arg(args, 9), 0); | |
| 445 | 6 | int32_t kH = argInt(arg(args, 10), 0); | |
| 446 | 6 | int32_t kW = argInt(arg(args, 11), 0); | |
| 447 | 6 | int32_t sT = argInt(arg(args, 12), 1); | |
| 448 | 6 | int32_t sH = argInt(arg(args, 13), 1); | |
| 449 | 6 | int32_t sW = argInt(arg(args, 14), 1); | |
| 450 | 6 | int32_t pT = argInt(arg(args, 15), 0); | |
| 451 | 6 | int32_t pH = argInt(arg(args, 16), 0); | |
| 452 | 6 | int32_t pW = argInt(arg(args, 17), 0); | |
| 453 | 6 | int32_t dT = argInt(arg(args, 18), 1); | |
| 454 | 6 | int32_t dH = argInt(arg(args, 19), 1); | |
| 455 | 6 | int32_t dW = argInt(arg(args, 20), 1); | |
| 456 | 6 | int32_t groups = argInt(arg(args, 21), 1); | |
| 457 | 6 | Tensor* y = requireTensor(arg(args, 22), L, "Y"); | |
| 458 | // conv2d's geometry on (H, W) plus the same rules on T. | ||
| 459 | 6 | int64_t H_out = 0, W_out = 0; | |
| 460 | 6 | convGeom2d(L, C_in, C_out, H, W, kH, kW, sH, sW, pH, pW, dH, dW, groups, H_out, W_out); | |
| 461 | 6 | needPositive(L, {kT, sT, dT}); | |
| 462 | 6 | needNonNegative(L, {N, T_in, pT}); | |
| 463 | 24 | needOperands(L, x, { | |
| 464 | 6 | {"X", x, elems({N, C_in, T_in, H, W})}, | |
| 465 | 6 | {"Wt", wt, elems({C_out, C_in / groups, kT, kH, kW})}, | |
| 466 | 6 | {"bias", bias, C_out}, | |
| 467 | }); | ||
| 468 | 6 | BROTENSOR_API_TRY | |
| 469 | 6 | brotensor::conv3d_forward( | |
| 470 | 6 | *x, *wt, bias, N, C_in, T_in, H, W, C_out, kT, kH, kW, | |
| 471 | 6 | sT, sH, sW, pT, pH, pW, dT, dH, dW, groups, *y); | |
| 472 | 6 | BROTENSOR_API_CATCH(L) | |
| 473 | 6 | return ev::undefined(); | |
| 474 | ✗ | } | |
| 475 | |||
| 476 | // ---- window partition / reverse / 2x2 spatial merge ------------------------ | ||
| 477 | |||
| 478 | 4 | ev::Value jsWindowPartitionForward(ev::Value, std::span<const ev::Value> args) { | |
| 479 | 4 | const char* L = "windowPartitionForward"; | |
| 480 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 481 | 4 | int32_t N = argInt(arg(args, 1), 0); | |
| 482 | 4 | int32_t C = argInt(arg(args, 2), 0); | |
| 483 | 4 | int32_t H = argInt(arg(args, 3), 0); | |
| 484 | 4 | int32_t W = argInt(arg(args, 4), 0); | |
| 485 | 4 | int32_t window = argInt(arg(args, 5), 0); | |
| 486 | 4 | Tensor* y = requireTensor(arg(args, 6), L, "Y"); | |
| 487 | 4 | needPositive(L, {window}); | |
| 488 | 4 | if (H % window != 0 || W % window != 0) { | |
| 489 | ✗ | setError("windowPartitionForward: H and W must be multiples of window"); | |
| 490 | } | ||
| 491 | 4 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 492 | 4 | BROTENSOR_API_TRY | |
| 493 | 4 | brotensor::window_partition_forward(*x, N, C, H, W, window, *y); | |
| 494 | 4 | BROTENSOR_API_CATCH(L) | |
| 495 | 4 | return ev::undefined(); | |
| 496 | ✗ | } | |
| 497 | |||
| 498 | 4 | ev::Value jsWindowReverseForward(ev::Value, std::span<const ev::Value> args) { | |
| 499 | 4 | const char* L = "windowReverseForward"; | |
| 500 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 501 | 4 | int32_t N = argInt(arg(args, 1), 0); | |
| 502 | 4 | int32_t C = argInt(arg(args, 2), 0); | |
| 503 | 4 | int32_t H = argInt(arg(args, 3), 0); | |
| 504 | 4 | int32_t W = argInt(arg(args, 4), 0); | |
| 505 | 4 | int32_t window = argInt(arg(args, 5), 0); | |
| 506 | 4 | Tensor* y = requireTensor(arg(args, 6), L, "Y"); | |
| 507 | 4 | needPositive(L, {window}); | |
| 508 | 4 | if (H % window != 0 || W % window != 0) { | |
| 509 | ✗ | setError("windowReverseForward: H and W must be multiples of window"); | |
| 510 | } | ||
| 511 | 4 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 512 | 4 | BROTENSOR_API_TRY | |
| 513 | 4 | brotensor::window_reverse_forward(*x, N, C, H, W, window, *y); | |
| 514 | 4 | BROTENSOR_API_CATCH(L) | |
| 515 | 4 | return ev::undefined(); | |
| 516 | ✗ | } | |
| 517 | |||
| 518 | 4 | ev::Value jsSpatialMerge2x2Forward(ev::Value, std::span<const ev::Value> args) { | |
| 519 | 4 | const char* L = "spatialMerge2x2Forward"; | |
| 520 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 521 | 4 | int32_t N = argInt(arg(args, 1), 0); | |
| 522 | 4 | int32_t C = argInt(arg(args, 2), 0); | |
| 523 | 4 | int32_t H = argInt(arg(args, 3), 0); | |
| 524 | 4 | int32_t W = argInt(arg(args, 4), 0); | |
| 525 | 4 | Tensor* y = requireTensor(arg(args, 5), L, "Y"); | |
| 526 | 4 | bool channelMajor = ev::toBool(arg(args, 6)); | |
| 527 | 4 | if (H % 2 != 0 || W % 2 != 0) { | |
| 528 | ✗ | setError("spatialMerge2x2Forward: H and W must be even"); | |
| 529 | } | ||
| 530 | 4 | needElems(L, "X", x, elems({N, C, H, W})); | |
| 531 | 4 | BROTENSOR_API_TRY | |
| 532 | 4 | brotensor::spatial_merge_2x2_forward(*x, N, C, H, W, channelMajor, *y); | |
| 533 | 4 | BROTENSOR_API_CATCH(L) | |
| 534 | 4 | return ev::undefined(); | |
| 535 | ✗ | } | |
| 536 | |||
| 537 | // ---- batch norm ------------------------------------------------------------ | ||
| 538 | |||
| 539 | 2 | ev::Value jsBatchNormForward(ev::Value, std::span<const ev::Value> args) { | |
| 540 | 2 | const char* L = "batchNormForward"; | |
| 541 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 542 | 2 | Tensor* gamma = requireTensor(arg(args, 1), L, "gamma"); | |
| 543 | 2 | Tensor* beta = requireTensor(arg(args, 2), L, "beta"); | |
| 544 | 2 | Tensor* runningMean = requireTensor(arg(args, 3), L, "runningMean"); | |
| 545 | 2 | Tensor* runningVar = requireTensor(arg(args, 4), L, "runningVar"); | |
| 546 | 2 | int32_t N = argInt(arg(args, 5), 0); | |
| 547 | 2 | int32_t C = argInt(arg(args, 6), 0); | |
| 548 | 2 | int32_t H = argInt(arg(args, 7), 0); | |
| 549 | 2 | int32_t W = argInt(arg(args, 8), 0); | |
| 550 | 2 | double eps = argNum(arg(args, 9), 0.00001); | |
| 551 | 2 | double momentum = argNum(arg(args, 10), 0.1); | |
| 552 | 2 | Tensor* y = requireTensor(arg(args, 11), L, "Y"); | |
| 553 | 2 | Tensor* savedMean = requireTensor(arg(args, 12), L, "savedMean"); | |
| 554 | 2 | Tensor* savedRstd = requireTensor(arg(args, 13), L, "savedRstd"); | |
| 555 | 12 | needOperands(L, x, { | |
| 556 | 2 | {"X", x, elems({N, C, H, W})}, | |
| 557 | 2 | {"gamma", gamma, C}, | |
| 558 | 2 | {"beta", beta, C}, | |
| 559 | 2 | {"runningMean", runningMean, C}, | |
| 560 | 2 | {"runningVar", runningVar, C}, | |
| 561 | }); | ||
| 562 | 2 | BROTENSOR_API_TRY | |
| 563 | 2 | brotensor::batch_norm_forward( | |
| 564 | 2 | *x, *gamma, *beta, *runningMean, *runningVar, | |
| 565 | 2 | N, C, H, W, static_cast<float>(eps), static_cast<float>(momentum), | |
| 566 | 2 | *y, *savedMean, *savedRstd); | |
| 567 | 2 | BROTENSOR_API_CATCH(L) | |
| 568 | 2 | return ev::undefined(); | |
| 569 | ✗ | } | |
| 570 | |||
| 571 | 2 | ev::Value jsBatchNormBackward(ev::Value, std::span<const ev::Value> args) { | |
| 572 | 2 | const char* L = "batchNormBackward"; | |
| 573 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 574 | 2 | Tensor* gamma = requireTensor(arg(args, 1), L, "gamma"); | |
| 575 | 2 | Tensor* savedMean = requireTensor(arg(args, 2), L, "savedMean"); | |
| 576 | 2 | Tensor* savedRstd = requireTensor(arg(args, 3), L, "savedRstd"); | |
| 577 | 2 | Tensor* dY = requireTensor(arg(args, 4), L, "dY"); | |
| 578 | 2 | int32_t N = argInt(arg(args, 5), 0); | |
| 579 | 2 | int32_t C = argInt(arg(args, 6), 0); | |
| 580 | 2 | int32_t H = argInt(arg(args, 7), 0); | |
| 581 | 2 | int32_t W = argInt(arg(args, 8), 0); | |
| 582 | 2 | Tensor* dX = requireTensor(arg(args, 9), L, "dX"); | |
| 583 | 2 | Tensor* dGamma = requireTensor(arg(args, 10), L, "dGamma"); | |
| 584 | 2 | Tensor* dBeta = requireTensor(arg(args, 11), L, "dBeta"); | |
| 585 | 2 | const int64_t n = elems({N, C, H, W}); | |
| 586 | // savedMean/savedRstd are the forward's FP32 caches; dGamma/dBeta accumulate. | ||
| 587 | 12 | needOperands(L, x, { | |
| 588 | 2 | {"X", x, n}, | |
| 589 | 2 | {"dY", dY, n}, | |
| 590 | 2 | {"gamma", gamma, C}, | |
| 591 | 2 | {"dGamma", dGamma, C}, | |
| 592 | 2 | {"dBeta", dBeta, C}, | |
| 593 | }); | ||
| 594 | 2 | needMask(L, savedMean, C, "savedMean"); | |
| 595 | 2 | needMask(L, savedRstd, C, "savedRstd"); | |
| 596 | 2 | BROTENSOR_API_TRY | |
| 597 | 2 | brotensor::batch_norm_backward( | |
| 598 | 2 | *x, *gamma, *savedMean, *savedRstd, *dY, N, C, H, W, *dX, *dGamma, *dBeta); | |
| 599 | 2 | BROTENSOR_API_CATCH(L) | |
| 600 | 2 | return ev::undefined(); | |
| 601 | ✗ | } | |
| 602 | |||
| 603 | 4 | ev::Value jsBatchNormInference(ev::Value, std::span<const ev::Value> args) { | |
| 604 | 4 | const char* L = "batchNormInference"; | |
| 605 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 606 | 4 | Tensor* gamma = requireTensor(arg(args, 1), L, "gamma"); | |
| 607 | 4 | Tensor* beta = requireTensor(arg(args, 2), L, "beta"); | |
| 608 | 4 | Tensor* runningMean = requireTensor(arg(args, 3), L, "runningMean"); | |
| 609 | 4 | Tensor* runningVar = requireTensor(arg(args, 4), L, "runningVar"); | |
| 610 | 4 | int32_t N = argInt(arg(args, 5), 0); | |
| 611 | 4 | int32_t C = argInt(arg(args, 6), 0); | |
| 612 | 4 | int32_t H = argInt(arg(args, 7), 0); | |
| 613 | 4 | int32_t W = argInt(arg(args, 8), 0); | |
| 614 | 4 | double eps = argNum(arg(args, 9), 0.00001); | |
| 615 | 4 | Tensor* y = requireTensor(arg(args, 10), L, "Y"); | |
| 616 | 24 | needOperands(L, x, { | |
| 617 | 4 | {"X", x, elems({N, C, H, W})}, | |
| 618 | 4 | {"gamma", gamma, C}, | |
| 619 | 4 | {"beta", beta, C}, | |
| 620 | 4 | {"runningMean", runningMean, C}, | |
| 621 | 4 | {"runningVar", runningVar, C}, | |
| 622 | }); | ||
| 623 | 4 | BROTENSOR_API_TRY | |
| 624 | 4 | brotensor::batch_norm_inference( | |
| 625 | 4 | *x, *gamma, *beta, *runningMean, *runningVar, | |
| 626 | 4 | N, C, H, W, static_cast<float>(eps), *y); | |
| 627 | 4 | BROTENSOR_API_CATCH(L) | |
| 628 | 4 | return ev::undefined(); | |
| 629 | ✗ | } | |
| 630 | |||
| 631 | // ---- image preprocessing --------------------------------------------------- | ||
| 632 | |||
| 633 | 2 | ev::Value jsImageNormalize(ev::Value, std::span<const ev::Value> args) { | |
| 634 | 2 | const char* L = "imageNormalize"; | |
| 635 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 636 | 2 | Tensor* mean = requireTensor(arg(args, 1), L, "mean"); | |
| 637 | 2 | Tensor* std_ = requireTensor(arg(args, 2), L, "std"); | |
| 638 | 2 | int32_t N = argInt(arg(args, 3), 0); | |
| 639 | 2 | int32_t C = argInt(arg(args, 4), 0); | |
| 640 | 2 | int32_t H = argInt(arg(args, 5), 0); | |
| 641 | 2 | int32_t W = argInt(arg(args, 6), 0); | |
| 642 | 2 | Tensor* y = requireTensor(arg(args, 7), L, "Y"); | |
| 643 | 8 | needOperands(L, x, { | |
| 644 | 2 | {"X", x, elems({N, C, H, W})}, | |
| 645 | 2 | {"mean", mean, C}, | |
| 646 | 2 | {"std", std_, C}, | |
| 647 | }); | ||
| 648 | 2 | BROTENSOR_API_TRY | |
| 649 | 2 | brotensor::image_normalize(*x, *mean, *std_, N, C, H, W, *y); | |
| 650 | 2 | BROTENSOR_API_CATCH(L) | |
| 651 | 2 | return ev::undefined(); | |
| 652 | ✗ | } | |
| 653 | |||
| 654 | 12 | ev::Value jsImageU8ToF32NhwcToNchw(ev::Value, std::span<const ev::Value> args) { | |
| 655 | 12 | const char* L = "imageU8ToF32NhwcToNchw"; | |
| 656 | 12 | ev::Value srcVal = arg(args, 0); | |
| 657 | 12 | int32_t N = argInt(arg(args, 1), 0); | |
| 658 | 12 | int32_t H = argInt(arg(args, 2), 0); | |
| 659 | 12 | int32_t W = argInt(arg(args, 3), 0); | |
| 660 | 12 | int32_t C = argInt(arg(args, 4), 0); | |
| 661 | 12 | double scale = argNum(arg(args, 5), 1.0); | |
| 662 | 12 | double bias = argNum(arg(args, 6), 0.0); | |
| 663 | 12 | Tensor* yt = requireTensor(arg(args, 7), L, "Y"); | |
| 664 | 12 | if (N < 0 || H < 0 || W < 0 || C < 0) { | |
| 665 | 4 | setError(std::string(L) + ": negative dimension"); | |
| 666 | } | ||
| 667 | 12 | const int64_t want = extra::prod({N, H, W, C}); | |
| 668 | |||
| 669 | 12 | if (ev::isTypedArray(srcVal)) { | |
| 670 | 8 | ev::TypedArrayInfo ta = ev::typedArrayInfo(srcVal); | |
| 671 | 8 | if (ta.elementKind != ev::elements::Uint8) { | |
| 672 | ✗ | ev::throwTypeError(std::string(L) + ": src must be a Uint8Array"); | |
| 673 | } | ||
| 674 | 8 | if (static_cast<int64_t>(ta.byteLength) < want) { | |
| 675 | 4 | setError(std::string(L) + ": src holds " + std::to_string(ta.byteLength) + | |
| 676 | 2 | " bytes, need " + std::to_string(want)); | |
| 677 | } | ||
| 678 | 6 | runImageU8ToF32(L, reinterpret_cast<const uint8_t*>(ta.data), N, H, W, C, want, scale, bias, yt); | |
| 679 | 6 | return ev::undefined(); | |
| 680 | } | ||
| 681 | |||
| 682 | 4 | if (ev::isObject(srcVal)) { | |
| 683 | 2 | ev::Value lenVal = ev::getProperty(srcVal, "length"); | |
| 684 | 2 | if (!ev::isNumber(lenVal)) { | |
| 685 | ✗ | ev::throwTypeError(std::string(L) + ": src must be a Uint8Array"); | |
| 686 | } | ||
| 687 | 2 | double lenD = ev::toDouble(lenVal); | |
| 688 | 2 | if (!std::isfinite(lenD) || lenD < 0.0 || lenD > 4294967295.0) { | |
| 689 | ✗ | ev::throwRangeError(std::string(L) + ": invalid array length"); | |
| 690 | } | ||
| 691 | 2 | uint32_t len = static_cast<uint32_t>(lenD); | |
| 692 | 2 | if (static_cast<int64_t>(len) < want) { | |
| 693 | ✗ | setError(std::string(L) + ": src holds " + std::to_string(len) + | |
| 694 | ✗ | " bytes, need " + std::to_string(want)); | |
| 695 | } | ||
| 696 | 2 | std::vector<uint8_t> bytes(static_cast<size_t>(want)); | |
| 697 | 14 | for (int64_t i = 0; i < want; ++i) { | |
| 698 | 12 | bytes[static_cast<size_t>(i)] = | |
| 699 | 12 | byteFromDouble(ev::toDouble(ev::getElement(srcVal, static_cast<uint32_t>(i)))); | |
| 700 | 12 | } | |
| 701 | 2 | runImageU8ToF32(L, bytes.data(), N, H, W, C, want, scale, bias, yt); | |
| 702 | 2 | return ev::undefined(); | |
| 703 | 2 | } | |
| 704 | |||
| 705 | 2 | ev::throwTypeError(std::string(L) + ": src must be a Uint8Array"); | |
| 706 | 12 | } | |
| 707 | |||
| 708 | } // namespace | ||
| 709 | |||
| 710 | 2 | void installTensorConv(bronze::Value ns) { | |
| 711 | 2 | ev::ObjectBuilder b(ns); | |
| 712 | 2 | b.def("interp2dBackward", 9, &jsInterp2dBackward); | |
| 713 | 2 | b.def("pad2dForward", 11, &jsPad2dForward); | |
| 714 | 2 | b.def("pad2dBackward", 11, &jsPad2dBackward); | |
| 715 | 2 | b.def("slice2dForward", 10, &jsSlice2dForward); | |
| 716 | 2 | b.def("slice2dBackward", 10, &jsSlice2dBackward); | |
| 717 | 2 | b.def("maxPool2dForward", 13, &jsMaxPool2dForward); | |
| 718 | 2 | b.def("maxPool2dBackward", 9, &jsMaxPool2dBackward); | |
| 719 | 2 | b.def("adaptiveAvgPool2dForward", 8, &jsAdaptiveAvgPool2dForward); | |
| 720 | 2 | b.def("adaptiveAvgPool2dBackward", 8, &jsAdaptiveAvgPool2dBackward); | |
| 721 | 2 | b.def("convTranspose2dForward", 20, &jsConvTranspose2dForward); | |
| 722 | 2 | b.def("convTranspose2dBackwardInput", 19, &jsConvTranspose2dBackwardInput); | |
| 723 | 2 | b.def("convTranspose2dBackwardWeight", 19, &jsConvTranspose2dBackwardWeight); | |
| 724 | 2 | b.def("convTranspose2dBackwardBias", 6, &jsConvTranspose2dBackwardBias); | |
| 725 | 2 | b.def("conv3dForward", 23, &jsConv3dForward); | |
| 726 | 2 | b.def("windowPartitionForward", 7, &jsWindowPartitionForward); | |
| 727 | 2 | b.def("windowReverseForward", 7, &jsWindowReverseForward); | |
| 728 | 2 | b.def("spatialMerge2x2Forward", 7, &jsSpatialMerge2x2Forward); | |
| 729 | 2 | b.def("batchNormForward", 14, &jsBatchNormForward); | |
| 730 | 2 | b.def("batchNormBackward", 12, &jsBatchNormBackward); | |
| 731 | 2 | b.def("batchNormInference", 11, &jsBatchNormInference); | |
| 732 | 2 | b.def("imageNormalize", 8, &jsImageNormalize); | |
| 733 | 2 | b.def("imageU8ToF32NhwcToNchw", 8, &jsImageU8ToF32NhwcToNchw); | |
| 734 | 2 | } | |
| 735 | |||
| 736 | } // namespace brotensor::api | ||
| 737 |