src/api/native_tensor_extra_nn.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // native_tensor_extra_nn.cpp — dense, recurrent & attention extra bindings: | ||
| 2 | // linearForwardBatchedEx, linearForwardBatchedFp16Act, matmulAbt, LSTM pair, | ||
| 3 | // GQA flash attention, packed-QKV encoder attention pair, Transformer-XL | ||
| 4 | // relative-position bias and RoPE variants. | ||
| 5 | |||
| 6 | #include "native_tensor_extra_util.h" | ||
| 7 | #include "api_internal.h" | ||
| 8 | |||
| 9 | #include <algorithm> | ||
| 10 | #include <cmath> | ||
| 11 | #include <string> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace brotensor::api { | ||
| 15 | |||
| 16 | using namespace brotensor::api::extra; | ||
| 17 | |||
| 18 | namespace { | ||
| 19 | |||
| 20 | namespace ev = bronze::embed; | ||
| 21 | using brotensor::Tensor; | ||
| 22 | |||
| 23 | // A LinearActivation value (brotensor/ops/linear.h). | ||
| 24 | 20 | inline void needAct(const char* label, int32_t act) { | |
| 25 | 20 | needRange(label, "act", act, brotensor::kLinearActNone, brotensor::kLinearActQuickGelu); | |
| 26 | 20 | } | |
| 27 | |||
| 28 | // A JS number carrying a `long long` element stride: whole and non-negative. | ||
| 29 | 24 | inline int64_t strideArg(const char* label, const char* name, double v) { | |
| 30 | 24 | if (!(v >= 0.0) || v != std::floor(v) || v > 9007199254740991.0) { | |
| 31 | ✗ | setError(std::string(label) + ": " + name + " must be a non-negative whole number"); | |
| 32 | } | ||
| 33 | 24 | return static_cast<int64_t>(v); | |
| 34 | ✗ | } | |
| 35 | |||
| 36 | // Elements a batch of `batch` slices of `slice` elements, `stride` apart, spans. | ||
| 37 | 20 | inline int64_t spanOf(int64_t batch, int64_t stride, int64_t slice) { | |
| 38 | 20 | if (batch <= 0 || slice <= 0) return 0; | |
| 39 | 20 | const int64_t head = prod({batch - 1, stride}); | |
| 40 | 20 | if (head == INT64_MAX || head > INT64_MAX - slice) return INT64_MAX; | |
| 41 | 20 | return head + slice; | |
| 42 | 20 | } | |
| 43 | |||
| 44 | // The shape the LSTM pair shares: FP32 X / W_ih / W_hh, H and I from the | ||
| 45 | // weights, X (T*B, I). Answers H and I. | ||
| 46 | 12 | inline void lstmShapes(const char* lb, const Tensor* X, const Tensor* Wih, const Tensor* Whh, | |
| 47 | int32_t T, int32_t B, int64_t& H, int64_t& I) { | ||
| 48 | 12 | constexpr auto F = brotensor::Dtype::FP32; | |
| 49 | 12 | needDtype(lb, "X", X, F); | |
| 50 | 12 | needDtype(lb, "W_ih", Wih, F); | |
| 51 | 12 | needDtype(lb, "W_hh", Whh, F); | |
| 52 | 12 | if (T < 1 || B < 1) { | |
| 53 | ✗ | setError(std::string(lb) + ": T and B must be >= 1"); | |
| 54 | } | ||
| 55 | 12 | if (Wih->rows < 4 || Wih->rows % 4 != 0) { | |
| 56 | ✗ | setError(std::string(lb) + ": W_ih must have 4*H rows"); | |
| 57 | } | ||
| 58 | 12 | H = Wih->rows / 4; | |
| 59 | 12 | I = Wih->cols; | |
| 60 | 12 | if (Whh->rows != Wih->rows || Whh->cols != H) { | |
| 61 | ✗ | setError(std::string(lb) + ": W_hh must be (4H, H) = (" + std::to_string(4 * H) + ", " + | |
| 62 | ✗ | std::to_string(H) + ")"); | |
| 63 | } | ||
| 64 | 12 | if (X->cols != I) { | |
| 65 | ✗ | setError(std::string(lb) + ": X must be (T*B, I) with I = W_ih.cols = " + std::to_string(I)); | |
| 66 | } | ||
| 67 | 12 | needElems(lb, "X", X, prod({T, B, I})); | |
| 68 | 12 | } | |
| 69 | |||
| 70 | // QKV (L, 3*numHeads*headDim) float storage, seqBounds (L, 2) whose row r is | ||
| 71 | // a sequence [b0, b1) inside [0, L) containing r. Validates and extracts host | ||
| 72 | // ints BEFORE BROTENSOR_API_TRY. | ||
| 73 | 8 | inline void packedBoundsValidate(const char* lb, const Tensor* qkv, int32_t numHeads, | |
| 74 | const Tensor* sb, std::vector<int32_t>& vals) { | ||
| 75 | 8 | needFloat(lb, "QKV", qkv); | |
| 76 | 8 | if (numHeads < 1 || qkv->cols % (3 * static_cast<int64_t>(numHeads)) != 0 || qkv->cols == 0) { | |
| 77 | 4 | setError(std::string(lb) + ": QKV.cols must be 3 * numHeads * headDim"); | |
| 78 | } | ||
| 79 | 8 | const int L = qkv->rows; | |
| 80 | 8 | if (sb->rows != L || sb->cols != 2) { | |
| 81 | ✗ | setError(std::string(lb) + ": seqBounds must be (" + std::to_string(L) + ", 2)"); | |
| 82 | } | ||
| 83 | 8 | hostInt32(lb, "seqBounds", sb, prod({L, 2}), vals); | |
| 84 | 28 | for (int r = 0; r < L; ++r) { | |
| 85 | 24 | const int32_t b0 = vals[2 * static_cast<size_t>(r)]; | |
| 86 | 24 | const int32_t b1 = vals[2 * static_cast<size_t>(r) + 1]; | |
| 87 | 24 | if (b0 < 0 || b0 > r || b1 <= r || b1 > L) { | |
| 88 | 8 | setError(std::string(lb) + ": seqBounds row " + std::to_string(r) + " is [" + std::to_string(b0) + | |
| 89 | 4 | ", " + std::to_string(b1) + "), which must lie in [0, " + std::to_string(L) + | |
| 90 | ") and contain the row"); | ||
| 91 | } | ||
| 92 | 20 | } | |
| 93 | 8 | } | |
| 94 | |||
| 95 | } // namespace | ||
| 96 | |||
| 97 | 2 | void installTensorExtraNn(bronze::Value ns) { | |
| 98 | 2 | ev::ObjectBuilder b(ns); | |
| 99 | |||
| 100 | // ---- batched linear with a fused epilogue ------------------------------ | ||
| 101 | 8 | b.def("linearForwardBatchedEx", 7, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 102 | 6 | constexpr const char* lb = "linearForwardBatchedEx"; | |
| 103 | 6 | Tensor* w = requireTensor(argAt(a, 0), lb, "W"); | |
| 104 | 6 | Tensor* bias = optionalTensor(argAt(a, 1), lb, "bias"); | |
| 105 | 6 | Tensor* x = requireTensor(argAt(a, 2), lb, "X_BD"); | |
| 106 | 6 | const int32_t act = intArg(argAt(a, 3), 0); | |
| 107 | 6 | const int32_t epilogue = intArg(argAt(a, 4), 0); | |
| 108 | 6 | Tensor* ws = optionalTensor(argAt(a, 5), lb, "workspace"); | |
| 109 | 6 | Tensor* y = requireTensor(argAt(a, 6), lb, "Y_BD"); | |
| 110 | |||
| 111 | 6 | needFloat(lb, "W", w); | |
| 112 | 6 | needSameDtype(lb, "X", x, w); | |
| 113 | 6 | needAct(lb, act); | |
| 114 | 8 | if (w->empty()) setError(std::string(lb) + ": W is empty"); | |
| 115 | 6 | if (x->cols != w->cols) { | |
| 116 | 4 | setError(std::string(lb) + ": X has " + std::to_string(x->cols) + " columns, W has " + | |
| 117 | 2 | std::to_string(w->cols)); | |
| 118 | } | ||
| 119 | 4 | optElems(lb, "bias", bias, w->rows, w); | |
| 120 | 4 | const int32_t base = epilogue & ~static_cast<int32_t>(brotensor::kLinearEpiFastAccum); | |
| 121 | 4 | if (base == brotensor::kLinearEpiAccumulate) { | |
| 122 | 2 | needSameDtype(lb, "Y", y, w); | |
| 123 | 2 | if (y->rows != x->rows || y->cols != w->rows) { | |
| 124 | ✗ | setError(std::string(lb) + ": the accumulate epilogue needs Y already (" + | |
| 125 | ✗ | std::to_string(x->rows) + ", " + std::to_string(w->rows) + ")"); | |
| 126 | } | ||
| 127 | 4 | } else if (base == brotensor::kLinearEpiGeglu) { | |
| 128 | ✗ | if (act != 0 || (w->rows & 1)) { | |
| 129 | ✗ | setError(std::string(lb) + ": the GeGLU epilogue needs act 0 and an even W row count"); | |
| 130 | } | ||
| 131 | 2 | } else if (base != brotensor::kLinearEpiStore) { | |
| 132 | ✗ | setError(std::string(lb) + ": unknown epilogue " + std::to_string(epilogue)); | |
| 133 | } | ||
| 134 | 4 | BROTENSOR_API_TRY | |
| 135 | 4 | brotensor::linear_forward_batched_ex(*w, bias, *x, act, epilogue, ws, *y); | |
| 136 | 4 | BROTENSOR_API_CATCH(lb) | |
| 137 | 4 | return ev::undefined(); | |
| 138 | 2 | }); | |
| 139 | |||
| 140 | 4 | b.def("linearForwardBatchedFp16Act", 5, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 141 | 2 | constexpr const char* lb = "linearForwardBatchedFp16Act"; | |
| 142 | 2 | Tensor* w = requireTensor(argAt(a, 0), lb, "W"); | |
| 143 | 2 | Tensor* bias = optionalTensor(argAt(a, 1), lb, "bias"); | |
| 144 | 2 | Tensor* x = requireTensor(argAt(a, 2), lb, "X_BD"); | |
| 145 | 2 | const int32_t act = intArg(argAt(a, 3), 0); | |
| 146 | 2 | Tensor* y = requireTensor(argAt(a, 4), lb, "Y_BD"); | |
| 147 | |||
| 148 | 2 | if (w->dtype != brotensor::Dtype::FP16 && w->dtype != brotensor::Dtype::BF16) { | |
| 149 | ✗ | setError(std::string(lb) + ": W is " + dtypeToString(w->dtype) + ", expected fp16 or bf16"); | |
| 150 | } | ||
| 151 | 2 | needSameDtype(lb, "X", x, w); | |
| 152 | 2 | needAct(lb, act); | |
| 153 | 2 | if (x->cols != w->cols) { | |
| 154 | ✗ | setError(std::string(lb) + ": X has " + std::to_string(x->cols) + " columns, W has " + | |
| 155 | ✗ | std::to_string(w->cols)); | |
| 156 | } | ||
| 157 | 2 | optElems(lb, "bias", bias, w->rows, w); | |
| 158 | 2 | BROTENSOR_API_TRY | |
| 159 | 2 | brotensor::linear_forward_batched_fp16_act(*w, bias, *x, act, *y); | |
| 160 | 2 | BROTENSOR_API_CATCH(lb) | |
| 161 | 2 | return ev::undefined(); | |
| 162 | ✗ | }); | |
| 163 | |||
| 164 | // ---- batched A @ B^T --------------------------------------------------- | ||
| 165 | 6 | b.def("matmulAbt", 12, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 166 | 4 | constexpr const char* lb = "matmulAbt"; | |
| 167 | 4 | Tensor* A = requireTensor(argAt(a, 0), lb, "A"); | |
| 168 | 4 | Tensor* B = requireTensor(argAt(a, 1), lb, "B"); | |
| 169 | 4 | Tensor* C = requireTensor(argAt(a, 2), lb, "C"); | |
| 170 | 4 | const int32_t batch = intArg(argAt(a, 3), 1); | |
| 171 | 4 | const int32_t M = intArg(argAt(a, 4), 0); | |
| 172 | 4 | const int32_t N = intArg(argAt(a, 5), 0); | |
| 173 | 4 | const int32_t K = intArg(argAt(a, 6), 0); | |
| 174 | 4 | const double strideA = numArg(argAt(a, 7), static_cast<double>(M) * K); | |
| 175 | 4 | const double strideB = numArg(argAt(a, 8), static_cast<double>(N) * K); | |
| 176 | 4 | const double strideC = numArg(argAt(a, 9), static_cast<double>(M) * N); | |
| 177 | 4 | Tensor* bias = optionalTensor(argAt(a, 10), lb, "bias"); | |
| 178 | 4 | const int32_t act = intArg(argAt(a, 11), 0); | |
| 179 | |||
| 180 | 4 | if (A->dtype != brotensor::Dtype::FP16 && A->dtype != brotensor::Dtype::BF16) { | |
| 181 | ✗ | setError(std::string(lb) + ": A is " + dtypeToString(A->dtype) + ", expected fp16 or bf16"); | |
| 182 | } | ||
| 183 | 4 | needSameDtype(lb, "B", B, A); | |
| 184 | 4 | needSameDtype(lb, "C", C, A); | |
| 185 | 4 | needAct(lb, act); | |
| 186 | 4 | if (batch < 1 || M < 0 || N < 0 || K < 0) { | |
| 187 | ✗ | setError(std::string(lb) + ": batch must be >= 1 and M, N, K >= 0"); | |
| 188 | } | ||
| 189 | 4 | const int64_t sA = strideArg(lb, "strideA", strideA); | |
| 190 | 4 | const int64_t sB = strideArg(lb, "strideB", strideB); | |
| 191 | 4 | const int64_t sC = strideArg(lb, "strideC", strideC); | |
| 192 | 4 | needElems(lb, "A", A, spanOf(batch, sA, prod({M, K}))); | |
| 193 | 4 | needElems(lb, "B", B, spanOf(batch, sB, prod({N, K}))); | |
| 194 | 4 | needElems(lb, "C", C, spanOf(batch, sC, prod({M, N}))); | |
| 195 | 4 | optElems(lb, "bias", bias, N, A); | |
| 196 | 4 | BROTENSOR_API_TRY | |
| 197 | 4 | brotensor::matmul_abt(*A, *B, *C, batch, M, N, K, sA, sB, sC, bias, act); | |
| 198 | 4 | BROTENSOR_API_CATCH(lb) | |
| 199 | 4 | return ev::undefined(); | |
| 200 | ✗ | }); | |
| 201 | |||
| 202 | // ---- LSTM (training forward + BPTT) ------------------------------------ | ||
| 203 | 4 | b.def("lstmForwardTrain", 14, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 204 | 2 | constexpr const char* lb = "lstmForwardTrain"; | |
| 205 | 2 | Tensor* X = requireTensor(argAt(a, 0), lb, "X"); | |
| 206 | 2 | Tensor* W_ih = requireTensor(argAt(a, 1), lb, "W_ih"); | |
| 207 | 2 | Tensor* W_hh = requireTensor(argAt(a, 2), lb, "W_hh"); | |
| 208 | 2 | Tensor* b_ih = optionalTensor(argAt(a, 3), lb, "b_ih"); | |
| 209 | 2 | Tensor* b_hh = optionalTensor(argAt(a, 4), lb, "b_hh"); | |
| 210 | 2 | Tensor* h0 = optionalTensor(argAt(a, 5), lb, "h0"); | |
| 211 | 2 | Tensor* c0 = optionalTensor(argAt(a, 6), lb, "c0"); | |
| 212 | 2 | const int32_t T = intArg(argAt(a, 7), 0); | |
| 213 | 2 | const int32_t B = intArg(argAt(a, 8), 0); | |
| 214 | 2 | Tensor* Y = requireTensor(argAt(a, 9), lb, "Y"); | |
| 215 | 2 | Tensor* gates = requireTensor(argAt(a, 10), lb, "gates"); | |
| 216 | 2 | Tensor* C = requireTensor(argAt(a, 11), lb, "C"); | |
| 217 | 2 | Tensor* hT = optionalTensor(argAt(a, 12), lb, "hT"); | |
| 218 | 2 | Tensor* cT = optionalTensor(argAt(a, 13), lb, "cT"); | |
| 219 | |||
| 220 | 2 | int64_t H = 0, I = 0; | |
| 221 | 2 | lstmShapes(lb, X, W_ih, W_hh, T, B, H, I); | |
| 222 | 2 | optF32Elems(lb, "b_ih", b_ih, 4 * H); | |
| 223 | 2 | optF32Elems(lb, "b_hh", b_hh, 4 * H); | |
| 224 | 2 | optF32Elems(lb, "h0", h0, prod({B, H})); | |
| 225 | 2 | optF32Elems(lb, "c0", c0, prod({B, H})); | |
| 226 | 2 | BROTENSOR_API_TRY | |
| 227 | 4 | brotensor::lstm_forward_train(*X, *W_ih, *W_hh, b_ih, b_hh, h0, c0, T, B, | |
| 228 | 2 | *Y, *gates, *C, hT, cT); | |
| 229 | 2 | BROTENSOR_API_CATCH(lb) | |
| 230 | 2 | return ev::undefined(); | |
| 231 | ✗ | }); | |
| 232 | |||
| 233 | 4 | b.def("lstmBackward", 18, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 234 | 2 | constexpr const char* lb = "lstmBackward"; | |
| 235 | 2 | Tensor* X = requireTensor(argAt(a, 0), lb, "X"); | |
| 236 | 2 | Tensor* W_ih = requireTensor(argAt(a, 1), lb, "W_ih"); | |
| 237 | 2 | Tensor* W_hh = requireTensor(argAt(a, 2), lb, "W_hh"); | |
| 238 | 2 | Tensor* h0 = optionalTensor(argAt(a, 3), lb, "h0"); | |
| 239 | 2 | Tensor* c0 = optionalTensor(argAt(a, 4), lb, "c0"); | |
| 240 | 2 | Tensor* Y = requireTensor(argAt(a, 5), lb, "Y"); | |
| 241 | 2 | Tensor* gates = requireTensor(argAt(a, 6), lb, "gates"); | |
| 242 | 2 | Tensor* C = requireTensor(argAt(a, 7), lb, "C"); | |
| 243 | 2 | Tensor* dY = requireTensor(argAt(a, 8), lb, "dY"); | |
| 244 | 2 | const int32_t T = intArg(argAt(a, 9), 0); | |
| 245 | 2 | const int32_t B = intArg(argAt(a, 10), 0); | |
| 246 | 2 | Tensor* dX = requireTensor(argAt(a, 11), lb, "dX"); | |
| 247 | 2 | Tensor* dW_ih = requireTensor(argAt(a, 12), lb, "dW_ih"); | |
| 248 | 2 | Tensor* dW_hh = requireTensor(argAt(a, 13), lb, "dW_hh"); | |
| 249 | 2 | Tensor* db_ih = optionalTensor(argAt(a, 14), lb, "db_ih"); | |
| 250 | 2 | Tensor* db_hh = optionalTensor(argAt(a, 15), lb, "db_hh"); | |
| 251 | 2 | Tensor* dh0 = optionalTensor(argAt(a, 16), lb, "dh0"); | |
| 252 | 2 | Tensor* dc0 = optionalTensor(argAt(a, 17), lb, "dc0"); | |
| 253 | |||
| 254 | 2 | int64_t H = 0, I = 0; | |
| 255 | 2 | lstmShapes(lb, X, W_ih, W_hh, T, B, H, I); | |
| 256 | 2 | constexpr auto F = brotensor::Dtype::FP32; | |
| 257 | 2 | const int64_t TB = prod({T, B}); | |
| 258 | 2 | optF32Elems(lb, "h0", h0, prod({B, H})); | |
| 259 | 2 | optF32Elems(lb, "c0", c0, prod({B, H})); | |
| 260 | 2 | needDtype(lb, "Y", Y, F); | |
| 261 | 2 | needElems(lb, "Y", Y, prod({TB, H})); | |
| 262 | 2 | needDtype(lb, "gates", gates, F); | |
| 263 | 2 | needElems(lb, "gates", gates, prod({TB, 4 * H})); | |
| 264 | 2 | needDtype(lb, "C", C, F); | |
| 265 | 2 | needElems(lb, "C", C, prod({TB, H})); | |
| 266 | 2 | needDtype(lb, "dY", dY, F); | |
| 267 | 2 | needElems(lb, "dY", dY, prod({TB, H})); | |
| 268 | 2 | needDtype(lb, "dW_ih", dW_ih, F); | |
| 269 | 2 | needElems(lb, "dW_ih", dW_ih, prod({4 * H, I})); | |
| 270 | 2 | needDtype(lb, "dW_hh", dW_hh, F); | |
| 271 | 2 | needElems(lb, "dW_hh", dW_hh, prod({4 * H, H})); | |
| 272 | 2 | optF32Elems(lb, "db_ih", db_ih, 4 * H); | |
| 273 | 2 | optF32Elems(lb, "db_hh", db_hh, 4 * H); | |
| 274 | 2 | BROTENSOR_API_TRY | |
| 275 | 4 | brotensor::lstm_backward(*X, *W_ih, *W_hh, h0, c0, *Y, *gates, *C, *dY, T, B, | |
| 276 | 2 | *dX, *dW_ih, *dW_hh, db_ih, db_hh, dh0, dc0); | |
| 277 | 2 | BROTENSOR_API_CATCH(lb) | |
| 278 | 2 | return ev::undefined(); | |
| 279 | ✗ | }); | |
| 280 | |||
| 281 | // ---- GQA flash attention ----------------------------------------------- | ||
| 282 | 10 | b.def("flashAttentionGqaForward", 8, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 283 | 8 | constexpr const char* lb = "flashAttentionGqaForward"; | |
| 284 | 8 | Tensor* Q = requireTensor(argAt(a, 0), lb, "Q"); | |
| 285 | 8 | Tensor* K = requireTensor(argAt(a, 1), lb, "K"); | |
| 286 | 8 | Tensor* V = requireTensor(argAt(a, 2), lb, "V"); | |
| 287 | 8 | Tensor* mask = optionalTensor(argAt(a, 3), lb, "mask"); | |
| 288 | 8 | const int32_t numQHeads = intArg(argAt(a, 4), 1); | |
| 289 | 8 | const int32_t numKvHeads = intArg(argAt(a, 5), numQHeads); | |
| 290 | 8 | const bool causal = boolArg(argAt(a, 6), false); | |
| 291 | 8 | Tensor* O = requireTensor(argAt(a, 7), lb, "O"); | |
| 292 | |||
| 293 | 8 | needFloat(lb, "Q", Q); | |
| 294 | 8 | needSameDtype(lb, "K", K, Q); | |
| 295 | 8 | needSameDtype(lb, "V", V, Q); | |
| 296 | 8 | if (numQHeads < 1 || numKvHeads < 1 || numQHeads % numKvHeads != 0) { | |
| 297 | 2 | setError(std::string(lb) + ": numKvHeads must divide numQHeads (both >= 1)"); | |
| 298 | } | ||
| 299 | 8 | if (Q->cols % numQHeads != 0) { | |
| 300 | ✗ | setError(std::string(lb) + ": Q.cols must be numQHeads * headDim"); | |
| 301 | } | ||
| 302 | 8 | const int64_t hd = Q->cols / numQHeads; | |
| 303 | 8 | if (K->cols != numKvHeads * hd || V->cols != K->cols || V->rows != K->rows) { | |
| 304 | 4 | setError(std::string(lb) + ": K and V must both be (Lk, numKvHeads*headDim) with headDim " + | |
| 305 | 2 | std::to_string(hd)); | |
| 306 | } | ||
| 307 | 6 | if (K->rows < Q->rows || (causal && K->rows != Q->rows)) { | |
| 308 | ✗ | setError(std::string(lb) + ": needs Lk >= Lq (and Lk == Lq when causal)"); | |
| 309 | } | ||
| 310 | 6 | optF32Elems(lb, "mask", mask, K->rows); | |
| 311 | 6 | BROTENSOR_API_TRY | |
| 312 | 6 | brotensor::flash_attention_gqa_forward(*Q, *K, *V, maskPtr(mask), | |
| 313 | 4 | numQHeads, numKvHeads, causal, *O); | |
| 314 | 2 | BROTENSOR_API_CATCH(lb) | |
| 315 | 2 | return ev::undefined(); | |
| 316 | 6 | }); | |
| 317 | |||
| 318 | // ---- packed-QKV encoder attention -------------------------------------- | ||
| 319 | 8 | b.def("flashAttentionPackedQkvForward", 5, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 320 | 6 | constexpr const char* lb = "flashAttentionPackedQkvForward"; | |
| 321 | 6 | Tensor* QKV = requireTensor(argAt(a, 0), lb, "QKV"); | |
| 322 | 6 | Tensor* seqBounds = requireTensor(argAt(a, 1), lb, "seqBounds"); | |
| 323 | 6 | const int32_t numHeads = intArg(argAt(a, 2), 1); | |
| 324 | 6 | const int32_t window = intArg(argAt(a, 3), 0); | |
| 325 | 6 | Tensor* O = requireTensor(argAt(a, 4), lb, "O"); | |
| 326 | |||
| 327 | 6 | std::vector<int32_t> vals; | |
| 328 | 6 | packedBoundsValidate(lb, QKV, numHeads, seqBounds, vals); | |
| 329 | 2 | BROTENSOR_API_TRY | |
| 330 | 2 | Tensor scratch; | |
| 331 | 2 | const Tensor& sb = int32Operand(*seqBounds, vals, scratch); | |
| 332 | 2 | brotensor::flash_attention_packed_qkv_forward(*QKV, sb, numHeads, window, *O); | |
| 333 | 2 | BROTENSOR_API_CATCH(lb) | |
| 334 | 2 | return ev::undefined(); | |
| 335 | 6 | }); | |
| 336 | |||
| 337 | 8 | b.def("flashAttentionPackedQkvBackward", 6, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 338 | 6 | constexpr const char* lb = "flashAttentionPackedQkvBackward"; | |
| 339 | 6 | Tensor* QKV = requireTensor(argAt(a, 0), lb, "QKV"); | |
| 340 | 6 | Tensor* dO = requireTensor(argAt(a, 1), lb, "dO"); | |
| 341 | 6 | Tensor* seqBounds = requireTensor(argAt(a, 2), lb, "seqBounds"); | |
| 342 | 6 | const int32_t numHeads = intArg(argAt(a, 3), 1); | |
| 343 | 6 | const int32_t window = intArg(argAt(a, 4), 0); | |
| 344 | 6 | Tensor* dQKV = requireTensor(argAt(a, 5), lb, "dQKV"); | |
| 345 | |||
| 346 | 6 | needSameDtype(lb, "dO", dO, QKV); | |
| 347 | 6 | if (dO->rows != QKV->rows || static_cast<int64_t>(dO->cols) * 3 != QKV->cols) { | |
| 348 | 2 | setError(std::string(lb) + ": dO must be (L, numHeads*headDim), a third of QKV's width"); | |
| 349 | } | ||
| 350 | 4 | std::vector<int32_t> vals; | |
| 351 | 4 | packedBoundsValidate(lb, QKV, numHeads, seqBounds, vals); | |
| 352 | 2 | BROTENSOR_API_TRY | |
| 353 | 2 | Tensor scratch; | |
| 354 | 2 | const Tensor& sb = int32Operand(*seqBounds, vals, scratch); | |
| 355 | 2 | brotensor::flash_attention_packed_qkv_backward(*QKV, *dO, sb, numHeads, window, *dQKV); | |
| 356 | 2 | BROTENSOR_API_CATCH(lb) | |
| 357 | 2 | return ev::undefined(); | |
| 358 | 6 | }); | |
| 359 | |||
| 360 | // ---- Transformer-XL relative-position bias ----------------------------- | ||
| 361 | 6 | b.def("relPosBiasXlForward", 5, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 362 | 4 | constexpr const char* lb = "relPosBiasXlForward"; | |
| 363 | 4 | Tensor* q = requireTensor(argAt(a, 0), lb, "Qv"); | |
| 364 | 4 | Tensor* pk = requireTensor(argAt(a, 1), lb, "Pk"); | |
| 365 | 4 | const int32_t numHeads = intArg(argAt(a, 2), 1); | |
| 366 | 4 | const int32_t headDim = intArg(argAt(a, 3), 0); | |
| 367 | 4 | Tensor* Bias = requireTensor(argAt(a, 4), lb, "Bias"); | |
| 368 | |||
| 369 | 4 | constexpr auto F = brotensor::Dtype::FP32; | |
| 370 | 4 | needDtype(lb, "Qv", q, F); | |
| 371 | 4 | needDtype(lb, "Pk", pk, F); | |
| 372 | 4 | if (numHeads < 1 || headDim < 1 || q->cols != static_cast<int64_t>(numHeads) * headDim) { | |
| 373 | 2 | setError(std::string(lb) + ": Qv must be (T, numHeads*headDim)"); | |
| 374 | } | ||
| 375 | 4 | const int64_t T = q->rows; | |
| 376 | 4 | if (T < 1) setError(std::string(lb) + ": Qv is empty"); | |
| 377 | 4 | if (pk->cols != q->cols || pk->rows < 2 * T - 1) { | |
| 378 | 6 | setError(std::string(lb) + ": Pk must be (2T-1, numHeads*headDim) = (" + | |
| 379 | 4 | std::to_string(2 * T - 1) + ", " + std::to_string(q->cols) + ")"); | |
| 380 | } | ||
| 381 | 2 | needExtent(lb, "numHeads*T*T", prod({numHeads, T, T})); | |
| 382 | 2 | BROTENSOR_API_TRY | |
| 383 | 2 | brotensor::rel_pos_bias_xl_forward(*q, *pk, numHeads, headDim, *Bias); | |
| 384 | 2 | BROTENSOR_API_CATCH(lb) | |
| 385 | 2 | return ev::undefined(); | |
| 386 | 2 | }); | |
| 387 | |||
| 388 | // ---- RoPE: per-head tables, packed QKV in place ------------------------ | ||
| 389 | 6 | b.def("ropeApplyPerhead", 6, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 390 | 4 | constexpr const char* lb = "ropeApplyPerhead"; | |
| 391 | 4 | Tensor* x = requireTensor(argAt(a, 0), lb, "X"); | |
| 392 | 4 | Tensor* cosTbl = requireTensor(argAt(a, 1), lb, "cosTbl"); | |
| 393 | 4 | Tensor* sinTbl = requireTensor(argAt(a, 2), lb, "sinTbl"); | |
| 394 | 4 | const int32_t headDim = intArg(argAt(a, 3), 0); | |
| 395 | 4 | const int32_t numHeads = intArg(argAt(a, 4), 1); | |
| 396 | 4 | Tensor* Y = requireTensor(argAt(a, 5), lb, "Y"); | |
| 397 | |||
| 398 | 4 | needFloat(lb, "X", x); | |
| 399 | 4 | if (headDim < 2 || (headDim & 1) || numHeads < 1 || x->cols != static_cast<int64_t>(numHeads) * headDim) { | |
| 400 | ✗ | setError(std::string(lb) + ": X must be (L, numHeads*headDim) with headDim even"); | |
| 401 | } | ||
| 402 | 4 | const int64_t n = prod({x->rows, numHeads, headDim / 2}); | |
| 403 | 4 | optF32Elems(lb, "cosTbl", cosTbl, n); | |
| 404 | 4 | optF32Elems(lb, "sinTbl", sinTbl, n); | |
| 405 | 4 | BROTENSOR_API_TRY | |
| 406 | 4 | brotensor::rope_apply_perhead(*x, *cosTbl, *sinTbl, headDim, numHeads, *Y); | |
| 407 | 4 | BROTENSOR_API_CATCH(lb) | |
| 408 | 4 | return ev::undefined(); | |
| 409 | ✗ | }); | |
| 410 | |||
| 411 | 8 | b.def("ropeQkvPackedInplace", 6, [](bronze::Value, std::span<const bronze::Value> a) -> bronze::Value { | |
| 412 | 6 | constexpr const char* lb = "ropeQkvPackedInplace"; | |
| 413 | 6 | Tensor* qkv = requireTensor(argAt(a, 0), lb, "QKV"); | |
| 414 | 6 | Tensor* ct = requireTensor(argAt(a, 1), lb, "cosTbl"); | |
| 415 | 6 | Tensor* st = requireTensor(argAt(a, 2), lb, "sinTbl"); | |
| 416 | 6 | Tensor* ps = requireTensor(argAt(a, 3), lb, "pos"); | |
| 417 | 6 | const int32_t numHeads = intArg(argAt(a, 4), 1); | |
| 418 | 6 | const int32_t headDim = intArg(argAt(a, 5), 0); | |
| 419 | |||
| 420 | 6 | needFloat(lb, "QKV", qkv); | |
| 421 | 6 | if (headDim < 2 || (headDim & 1) || numHeads < 1 || | |
| 422 | 6 | qkv->cols != 3 * static_cast<int64_t>(numHeads) * headDim) { | |
| 423 | 4 | setError(std::string(lb) + ": QKV must be (L, 3*numHeads*headDim) with headDim even"); | |
| 424 | } | ||
| 425 | 6 | constexpr auto F = brotensor::Dtype::FP32; | |
| 426 | 6 | needDtype(lb, "cosTbl", ct, F); | |
| 427 | 6 | needDtype(lb, "sinTbl", st, F); | |
| 428 | 6 | if (ct->cols != headDim / 2 || st->cols != headDim / 2) { | |
| 429 | ✗ | setError(std::string(lb) + ": cosTbl / sinTbl must be (P, headDim/2)"); | |
| 430 | } | ||
| 431 | 6 | if (ps->rows != qkv->rows || ps->cols != 1) { | |
| 432 | 2 | setError(std::string(lb) + ": pos must be (" + std::to_string(qkv->rows) + ", 1)"); | |
| 433 | } | ||
| 434 | 4 | std::vector<int32_t> vals; | |
| 435 | 4 | hostInt32(lb, "pos", ps, qkv->rows, vals); | |
| 436 | 4 | needIndicesIn(lb, "pos", vals, qkv->rows, 0, std::min(ct->rows, st->rows)); | |
| 437 | 2 | BROTENSOR_API_TRY | |
| 438 | 2 | Tensor scratch; | |
| 439 | 2 | brotensor::rope_qkv_packed_inplace(*qkv, *ct, *st, int32Operand(*ps, vals, scratch), | |
| 440 | 2 | numHeads, headDim); | |
| 441 | 2 | BROTENSOR_API_CATCH(lb) | |
| 442 | 2 | return ev::undefined(); | |
| 443 | 6 | }); | |
| 444 | 2 | } | |
| 445 | |||
| 446 | } // namespace brotensor::api | ||
| 447 |