src/api/native_tensor_batched.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // native_tensor_batched.cpp — the "batched" group of bro.tensor, each op | ||
| 2 | // bound directly onto `bro.tensor` via bronze::embed and delegating to | ||
| 3 | // include/brotensor/ops/. | ||
| 4 | |||
| 5 | #include "api_internal.h" | ||
| 6 | #include "native_tensor_extra_util.h" | ||
| 7 | |||
| 8 | #include <cmath> | ||
| 9 | #include <cstdint> | ||
| 10 | #include <span> | ||
| 11 | #include <string> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace brotensor::api { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | namespace ev = bronze::embed; | ||
| 19 | using brotensor::Tensor; | ||
| 20 | |||
| 21 | 10 | int32_t argInt(ev::Value v, int32_t def = 0) { | |
| 22 | 10 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 23 | 10 | double d = ev::toDouble(v); | |
| 24 | 10 | if (!std::isfinite(d) || d == 0.0) return 0; | |
| 25 | 10 | if (d >= -2147483648.0 && d <= 2147483647.0) { | |
| 26 | 10 | return static_cast<int32_t>(std::trunc(d)); | |
| 27 | } | ||
| 28 | ✗ | double m = std::fmod(std::trunc(d), 4294967296.0); | |
| 29 | ✗ | if (m >= 2147483648.0) m -= 4294967296.0; | |
| 30 | ✗ | else if (m < -2147483648.0) m += 4294967296.0; | |
| 31 | ✗ | return static_cast<int32_t>(m); | |
| 32 | 10 | } | |
| 33 | |||
| 34 | 2 | double argNum(ev::Value v, double def = 0.0) { | |
| 35 | 2 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 36 | 2 | return ev::toDouble(v); | |
| 37 | 2 | } | |
| 38 | |||
| 39 | // X_BD is (B, W.cols). | ||
| 40 | 12 | void needBatchIn(const char* L, const Tensor* W, const Tensor* X) { | |
| 41 | 12 | if (X->cols != W->cols) { | |
| 42 | 2 | setError(std::string(L) + ": X_BD must be (B, " + std::to_string(W->cols) + | |
| 43 | ") to match W's in-dim"); | ||
| 44 | } | ||
| 45 | 12 | } | |
| 46 | |||
| 47 | // ---- batched forwards ------------------------------------------------------ | ||
| 48 | |||
| 49 | 2 | ev::Value jsLinearForwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 50 | 2 | const char* L = "linearForwardBatched"; | |
| 51 | 2 | Tensor* w = requireTensor(arg(args, 0), L, "W"); | |
| 52 | 2 | Tensor* b = requireTensor(arg(args, 1), L, "bias"); | |
| 53 | 2 | Tensor* x = requireTensor(arg(args, 2), L, "X_BD"); | |
| 54 | 2 | Tensor* y = requireTensor(arg(args, 3), L, "Y_BD"); | |
| 55 | 2 | needBatchIn(L, w, x); | |
| 56 | 2 | needMask(L, b, w->rows, "bias"); | |
| 57 | 2 | needMask(L, x, 0, "X_BD"); | |
| 58 | 2 | BROTENSOR_API_TRY | |
| 59 | 2 | brotensor::linear_forward_batched(*w, *b, *x, *y); | |
| 60 | 2 | BROTENSOR_API_CATCH(L) | |
| 61 | 2 | return ev::undefined(); | |
| 62 | ✗ | } | |
| 63 | |||
| 64 | 4 | ev::Value jsLinearForwardBatchedFp16(ev::Value, std::span<const ev::Value> args) { | |
| 65 | 4 | const char* L = "linearForwardBatchedFp16"; | |
| 66 | 4 | Tensor* w = requireTensor(arg(args, 0), L, "W"); | |
| 67 | 4 | Tensor* b = optionalTensor(arg(args, 1), L, "bias"); | |
| 68 | 4 | Tensor* x = requireTensor(arg(args, 2), L, "X_BD"); | |
| 69 | 4 | Tensor* y = requireTensor(arg(args, 3), L, "Y_BD"); | |
| 70 | 4 | needBatchIn(L, w, x); | |
| 71 | 4 | needOperands(L, w, {{"X_BD", x, 0}, {"bias", b, w->rows}}); | |
| 72 | 4 | BROTENSOR_API_TRY | |
| 73 | 4 | brotensor::linear_forward_batched_fp16(*w, b, *x, *y); | |
| 74 | 4 | BROTENSOR_API_CATCH(L) | |
| 75 | 4 | return ev::undefined(); | |
| 76 | ✗ | } | |
| 77 | |||
| 78 | 2 | ev::Value jsReluForwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 79 | 2 | const char* L = "reluForwardBatched"; | |
| 80 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X_BD"); | |
| 81 | 2 | Tensor* y = requireTensor(arg(args, 1), L, "Y_BD"); | |
| 82 | 2 | BROTENSOR_API_TRY | |
| 83 | 2 | brotensor::relu_forward_batched(*x, *y); | |
| 84 | 2 | BROTENSOR_API_CATCH(L) | |
| 85 | 2 | return ev::undefined(); | |
| 86 | ✗ | } | |
| 87 | |||
| 88 | 2 | ev::Value jsTanhForwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 89 | 2 | const char* L = "tanhForwardBatched"; | |
| 90 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X_BD"); | |
| 91 | 2 | Tensor* y = requireTensor(arg(args, 1), L, "Y_BD"); | |
| 92 | 2 | BROTENSOR_API_TRY | |
| 93 | 2 | brotensor::tanh_forward_batched(*x, *y); | |
| 94 | 2 | BROTENSOR_API_CATCH(L) | |
| 95 | 2 | return ev::undefined(); | |
| 96 | ✗ | } | |
| 97 | |||
| 98 | 2 | ev::Value jsAddInplaceBatched(ev::Value, std::span<const ev::Value> args) { | |
| 99 | 2 | const char* L = "addInplaceBatched"; | |
| 100 | 2 | Tensor* y = requireTensor(arg(args, 0), L, "Y_BD"); | |
| 101 | 2 | Tensor* x = requireTensor(arg(args, 1), L, "X_BD"); | |
| 102 | 2 | needPair(L, "X_BD", x, y); | |
| 103 | 2 | BROTENSOR_API_TRY | |
| 104 | 2 | brotensor::add_inplace_batched(*y, *x); | |
| 105 | 2 | BROTENSOR_API_CATCH(L) | |
| 106 | 2 | return ev::undefined(); | |
| 107 | ✗ | } | |
| 108 | |||
| 109 | // ---- batched-training backwards -------------------------------------------- | ||
| 110 | |||
| 111 | 2 | ev::Value jsLinearBackwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 112 | 2 | const char* L = "linearBackwardBatched"; | |
| 113 | 2 | Tensor* w = requireTensor(arg(args, 0), L, "W"); | |
| 114 | 2 | Tensor* x = requireTensor(arg(args, 1), L, "X_BD"); | |
| 115 | 2 | Tensor* dy = requireTensor(arg(args, 2), L, "dY_BD"); | |
| 116 | 2 | Tensor* dx = requireTensor(arg(args, 3), L, "dX_BD"); | |
| 117 | 2 | Tensor* dw = requireTensor(arg(args, 4), L, "dW"); | |
| 118 | 2 | Tensor* db = requireTensor(arg(args, 5), L, "dB"); | |
| 119 | 2 | needBatchIn(L, w, x); | |
| 120 | // dW / dB accumulate — "caller zeros". | ||
| 121 | 10 | needOperands(L, w, { | |
| 122 | 2 | {"X_BD", x, 0}, | |
| 123 | 2 | {"dY_BD", dy, elems({x->rows, w->rows})}, | |
| 124 | 2 | {"dW", dw, elems({w->rows, w->cols})}, | |
| 125 | 2 | {"dB", db, w->rows}, | |
| 126 | }); | ||
| 127 | 2 | BROTENSOR_API_TRY | |
| 128 | 2 | brotensor::linear_backward_batched(*w, *x, *dy, *dx, *dw, *db); | |
| 129 | 2 | BROTENSOR_API_CATCH(L) | |
| 130 | 2 | return ev::undefined(); | |
| 131 | ✗ | } | |
| 132 | |||
| 133 | 2 | ev::Value jsReluBackwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 134 | 2 | const char* L = "reluBackwardBatched"; | |
| 135 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X_BD"); | |
| 136 | 2 | Tensor* dy = requireTensor(arg(args, 1), L, "dY_BD"); | |
| 137 | 2 | Tensor* dx = requireTensor(arg(args, 2), L, "dX_BD"); | |
| 138 | 2 | needPair(L, "dY_BD", dy, x); | |
| 139 | 2 | BROTENSOR_API_TRY | |
| 140 | 2 | brotensor::relu_backward_batched(*x, *dy, *dx); | |
| 141 | 2 | BROTENSOR_API_CATCH(L) | |
| 142 | 2 | return ev::undefined(); | |
| 143 | ✗ | } | |
| 144 | |||
| 145 | 2 | ev::Value jsTanhBackwardBatched(ev::Value, std::span<const ev::Value> args) { | |
| 146 | 2 | const char* L = "tanhBackwardBatched"; | |
| 147 | 2 | Tensor* y = requireTensor(arg(args, 0), L, "Y_BD"); | |
| 148 | 2 | Tensor* dy = requireTensor(arg(args, 1), L, "dY_BD"); | |
| 149 | 2 | Tensor* dx = requireTensor(arg(args, 2), L, "dX_BD"); | |
| 150 | 2 | needPair(L, "dY_BD", dy, y); | |
| 151 | 2 | BROTENSOR_API_TRY | |
| 152 | 2 | brotensor::tanh_backward_batched(*y, *dy, *dx); | |
| 153 | 2 | BROTENSOR_API_CATCH(L) | |
| 154 | 2 | return ev::undefined(); | |
| 155 | ✗ | } | |
| 156 | |||
| 157 | // ---- row gather / scatter / top-k ------------------------------------------ | ||
| 158 | |||
| 159 | 10 | ev::Value jsGatherRows(ev::Value, std::span<const ev::Value> args) { | |
| 160 | 10 | const char* L = "gatherRows"; | |
| 161 | 10 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 162 | 10 | Tensor* idx = requireTensor(arg(args, 1), L, "Idx"); | |
| 163 | 10 | Tensor* y = requireTensor(arg(args, 2), L, "Y"); | |
| 164 | 10 | std::vector<int32_t> vals; | |
| 165 | 10 | extra::hostInt32(L, "Idx", idx, idx->size(), vals); | |
| 166 | 10 | extra::needIndicesIn(L, "Idx", vals, idx->size(), 0, x->rows); | |
| 167 | 6 | BROTENSOR_API_TRY | |
| 168 | 6 | Tensor scratch; | |
| 169 | 6 | brotensor::gather_rows(*x, extra::int32Operand(*idx, vals, scratch), *y); | |
| 170 | 6 | BROTENSOR_API_CATCH(L) | |
| 171 | 6 | return ev::undefined(); | |
| 172 | 10 | } | |
| 173 | |||
| 174 | // dY is (M, C) with M = Idx's length; every index names one of dX's R rows. | ||
| 175 | 4 | ev::Value jsScatterRowsAdd(ev::Value, std::span<const ev::Value> args) { | |
| 176 | 4 | const char* L = "scatterRowsAdd"; | |
| 177 | 4 | Tensor* dy = requireTensor(arg(args, 0), L, "dY"); | |
| 178 | 4 | Tensor* idx = requireTensor(arg(args, 1), L, "Idx"); | |
| 179 | 4 | int32_t R = argInt(arg(args, 2), 0); | |
| 180 | 4 | Tensor* dx = requireTensor(arg(args, 3), L, "dX"); | |
| 181 | 4 | needNonNegative(L, {R}); | |
| 182 | 4 | std::vector<int32_t> vals; | |
| 183 | 4 | extra::hostInt32(L, "Idx", idx, dy->rows, vals); | |
| 184 | 4 | extra::needIndicesIn(L, "Idx", vals, dy->rows, 0, R); | |
| 185 | 2 | BROTENSOR_API_TRY | |
| 186 | 2 | Tensor scratch; | |
| 187 | 2 | brotensor::scatter_rows_add(*dy, extra::int32Operand(*idx, vals, scratch), R, *dx); | |
| 188 | 2 | BROTENSOR_API_CATCH(L) | |
| 189 | 2 | return ev::undefined(); | |
| 190 | 4 | } | |
| 191 | |||
| 192 | 6 | ev::Value jsTopKRows(ev::Value, std::span<const ev::Value> args) { | |
| 193 | 6 | const char* L = "topKRows"; | |
| 194 | 6 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 195 | 6 | int32_t k = argInt(arg(args, 1), 1); | |
| 196 | 6 | Tensor* vals = requireTensor(arg(args, 2), L, "Vals"); | |
| 197 | 6 | Tensor* idx = requireTensor(arg(args, 3), L, "Idx"); | |
| 198 | 6 | BROTENSOR_API_TRY | |
| 199 | 6 | brotensor::top_k_rows(*x, k, *vals, *idx); | |
| 200 | 8 | BROTENSOR_API_CATCH(L) | |
| 201 | 4 | return ev::undefined(); | |
| 202 | 4 | } | |
| 203 | |||
| 204 | // ---- batched LayerNorm with training caches -------------------------------- | ||
| 205 | |||
| 206 | 2 | ev::Value jsLayernormForwardBatchedWithCaches(ev::Value, std::span<const ev::Value> args) { | |
| 207 | 2 | const char* L = "layernormForwardBatchedWithCaches"; | |
| 208 | 2 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 209 | 2 | Tensor* gamma = requireTensor(arg(args, 1), L, "gamma"); | |
| 210 | 2 | Tensor* beta = requireTensor(arg(args, 2), L, "beta"); | |
| 211 | 2 | Tensor* y = requireTensor(arg(args, 3), L, "Y"); | |
| 212 | 2 | Tensor* xhat = requireTensor(arg(args, 4), L, "Xhat"); | |
| 213 | 2 | Tensor* mean = requireTensor(arg(args, 5), L, "Mean"); | |
| 214 | 2 | Tensor* rstd = requireTensor(arg(args, 6), L, "Rstd"); | |
| 215 | 2 | double eps = argNum(arg(args, 7), 0.00001); | |
| 216 | 2 | needOperands(L, x, {{"gamma", gamma, x->cols}, {"beta", beta, x->cols}}); | |
| 217 | 2 | BROTENSOR_API_TRY | |
| 218 | 2 | brotensor::layernorm_forward_batched_with_caches( | |
| 219 | 2 | *x, *gamma, *beta, *y, *xhat, *mean, *rstd, static_cast<float>(eps)); | |
| 220 | 2 | BROTENSOR_API_CATCH(L) | |
| 221 | 2 | return ev::undefined(); | |
| 222 | ✗ | } | |
| 223 | |||
| 224 | 2 | ev::Value jsLayernormBackwardBatchedWithCaches(ev::Value, std::span<const ev::Value> args) { | |
| 225 | 2 | const char* L = "layernormBackwardBatchedWithCaches"; | |
| 226 | 2 | Tensor* dy = requireTensor(arg(args, 0), L, "dY"); | |
| 227 | 2 | Tensor* xhat = requireTensor(arg(args, 1), L, "Xhat"); | |
| 228 | 2 | Tensor* gamma = requireTensor(arg(args, 2), L, "gamma"); | |
| 229 | 2 | Tensor* rstd = requireTensor(arg(args, 3), L, "Rstd"); | |
| 230 | 2 | Tensor* dx = requireTensor(arg(args, 4), L, "dX"); | |
| 231 | 2 | Tensor* dGamma = requireTensor(arg(args, 5), L, "dGamma"); | |
| 232 | 2 | Tensor* dBeta = requireTensor(arg(args, 6), L, "dBeta"); | |
| 233 | // dY/Xhat (R,D); gamma, dGamma, dBeta (D,) — the last two accumulate; Rstd (R,1) FP32. | ||
| 234 | 2 | needPair(L, "Xhat", xhat, dy); | |
| 235 | 8 | needOperands(L, dy, { | |
| 236 | 2 | {"gamma", gamma, dy->cols}, | |
| 237 | 2 | {"dGamma", dGamma, dy->cols}, | |
| 238 | 2 | {"dBeta", dBeta, dy->cols}, | |
| 239 | }); | ||
| 240 | 2 | needMask(L, rstd, dy->rows, "Rstd"); | |
| 241 | 2 | BROTENSOR_API_TRY | |
| 242 | 2 | brotensor::layernorm_backward_batched_with_caches( | |
| 243 | 2 | *dy, *xhat, *gamma, *rstd, *dx, *dGamma, *dBeta); | |
| 244 | 2 | BROTENSOR_API_CATCH(L) | |
| 245 | 2 | return ev::undefined(); | |
| 246 | ✗ | } | |
| 247 | |||
| 248 | } // namespace | ||
| 249 | |||
| 250 | 2 | void installTensorBatched(bronze::Value ns) { | |
| 251 | 2 | ev::ObjectBuilder b(ns); | |
| 252 | 2 | b.def("linearForwardBatched", 4, &jsLinearForwardBatched); | |
| 253 | 2 | b.def("linearForwardBatchedFp16", 4, &jsLinearForwardBatchedFp16); | |
| 254 | 2 | b.def("reluForwardBatched", 2, &jsReluForwardBatched); | |
| 255 | 2 | b.def("tanhForwardBatched", 2, &jsTanhForwardBatched); | |
| 256 | 2 | b.def("addInplaceBatched", 2, &jsAddInplaceBatched); | |
| 257 | 2 | b.def("linearBackwardBatched", 6, &jsLinearBackwardBatched); | |
| 258 | 2 | b.def("reluBackwardBatched", 3, &jsReluBackwardBatched); | |
| 259 | 2 | b.def("tanhBackwardBatched", 3, &jsTanhBackwardBatched); | |
| 260 | 2 | b.def("gatherRows", 3, &jsGatherRows); | |
| 261 | 2 | b.def("scatterRowsAdd", 4, &jsScatterRowsAdd); | |
| 262 | 2 | b.def("topKRows", 4, &jsTopKRows); | |
| 263 | 2 | b.def("layernormForwardBatchedWithCaches", 8, &jsLayernormForwardBatchedWithCaches); | |
| 264 | 2 | b.def("layernormBackwardBatchedWithCaches", 7, &jsLayernormBackwardBatchedWithCaches); | |
| 265 | 2 | } | |
| 266 | |||
| 267 | } // namespace brotensor::api | ||
| 268 |