src/api/native_tensor_attn2.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // native_tensor_attn2.cpp — the second attention family of bro.tensor: | ||
| 2 | // decomposed 2D rel-pos self-attention (global + windowed), packed | ||
| 3 | // variable-length flash attention, the gated delta rule and M-RoPE, bound | ||
| 4 | // directly onto `bro.tensor` via bronze::embed. | ||
| 5 | |||
| 6 | #include "api_internal.h" | ||
| 7 | #include "native_tensor_extra_util.h" | ||
| 8 | |||
| 9 | #include <cmath> | ||
| 10 | #include <cstdint> | ||
| 11 | #include <span> | ||
| 12 | #include <string> | ||
| 13 | #include <vector> | ||
| 14 | |||
| 15 | namespace brotensor::api { | ||
| 16 | |||
| 17 | namespace { | ||
| 18 | |||
| 19 | namespace ev = bronze::embed; | ||
| 20 | using brotensor::Tensor; | ||
| 21 | |||
| 22 | 178 | int32_t argInt(ev::Value v, int32_t def = 0) { | |
| 23 | 178 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 24 | 178 | double d = ev::toDouble(v); | |
| 25 | 178 | if (!std::isfinite(d) || d == 0.0) return 0; | |
| 26 | 160 | if (d >= -2147483648.0 && d <= 2147483647.0) { | |
| 27 | 160 | return static_cast<int32_t>(std::trunc(d)); | |
| 28 | } | ||
| 29 | ✗ | double m = std::fmod(std::trunc(d), 4294967296.0); | |
| 30 | ✗ | if (m >= 2147483648.0) m -= 4294967296.0; | |
| 31 | ✗ | else if (m < -2147483648.0) m += 4294967296.0; | |
| 32 | ✗ | return static_cast<int32_t>(m); | |
| 33 | 178 | } | |
| 34 | |||
| 35 | 8 | double argNum(ev::Value v, double def = 0.0) { | |
| 36 | 8 | if (ev::isUndefined(v) || ev::isNull(v)) return def; | |
| 37 | 8 | return ev::toDouble(v); | |
| 38 | 8 | } | |
| 39 | |||
| 40 | // The varlen / M-RoPE ops take `const int32_t*` streams (device pointers on | ||
| 41 | // CUDA/Metal, host on CPU) whose VALUES address rows of the other operands. | ||
| 42 | // Validation reads the stream back before BROTENSOR_API_TRY; materialize() | ||
| 43 | // produces the INT32 device operand inside BROTENSOR_API_TRY. | ||
| 44 | 116 | struct IdxStream { | |
| 45 | 58 | const Tensor* tensor = nullptr; | |
| 46 | std::vector<int32_t> vals; | ||
| 47 | Tensor scratch; | ||
| 48 | 58 | const int32_t* ptr = nullptr; | |
| 49 | |||
| 50 | 24 | void materialize() { | |
| 51 | 24 | if (!tensor) return; | |
| 52 | 18 | ptr = static_cast<const int32_t*>(extra::int32Operand(*tensor, vals, scratch).data); | |
| 53 | 24 | } | |
| 54 | }; | ||
| 55 | |||
| 56 | // cu_seqlens: batch+1 non-decreasing prefix sums inside [0, total], each | ||
| 57 | // sequence at most maxLen long (the GPU kernels size their tiles from it). | ||
| 58 | 18 | void needCuSeq(const char* L, const char* name, const Tensor* t, | |
| 59 | int32_t batch, int64_t total, int32_t maxLen, IdxStream& s) { | ||
| 60 | 18 | if (batch <= 0) return; | |
| 61 | 18 | if (!t) { | |
| 62 | 6 | setError(std::string(L) + ": " + name + " is required when batch > 0"); | |
| 63 | } | ||
| 64 | 16 | const int64_t n = static_cast<int64_t>(batch) + 1; | |
| 65 | 16 | extra::hostInt32(L, name, t, n, s.vals); | |
| 66 | 16 | extra::needIndicesIn(L, name, s.vals, n, 0, total + 1); | |
| 67 | 42 | for (int32_t b = 0; b < batch; ++b) { | |
| 68 | 30 | const int64_t len = static_cast<int64_t>(s.vals[b + 1]) - s.vals[b]; | |
| 69 | 30 | if (len < 0 || len > maxLen) { | |
| 70 | 8 | setError(std::string(L) + ": " + name + " sequence " + std::to_string(b) + | |
| 71 | 8 | " has length " + std::to_string(len) + " (must be in [0, " + | |
| 72 | 8 | std::to_string(maxLen) + "])"); | |
| 73 | } | ||
| 74 | 26 | } | |
| 75 | 12 | s.tensor = t; | |
| 76 | 18 | } | |
| 77 | |||
| 78 | // An M-RoPE position stream for an axis of width d: length L, every value a | ||
| 79 | // row of that axis's cos/sin tables. | ||
| 80 | 16 | void needPosStream(const char* L, const char* name, const Tensor* t, int32_t d, | |
| 81 | int64_t rows, const Tensor* cosT, const Tensor* sinT, IdxStream& s) { | ||
| 82 | 16 | if (d <= 0) return; | |
| 83 | 10 | if (!t) { | |
| 84 | 2 | setError(std::string(L) + ": " + name + " is required when its axis width is > 0"); | |
| 85 | } | ||
| 86 | 8 | extra::hostInt32(L, name, t, rows, s.vals); | |
| 87 | 8 | const int64_t maxPos = cosT->rows < sinT->rows ? cosT->rows : sinT->rows; | |
| 88 | 8 | extra::needIndicesIn(L, name, s.vals, rows, 0, maxPos); | |
| 89 | 8 | s.tensor = t; | |
| 90 | 16 | } | |
| 91 | |||
| 92 | // ---- SAM / ViTDet decomposed 2D rel-pos self-attention --------------------- | ||
| 93 | |||
| 94 | 4 | ev::Value jsSelfAttentionDecomposedRelPosForward(ev::Value, std::span<const ev::Value> args) { | |
| 95 | 4 | const char* L = "selfAttentionDecomposedRelPosForward"; | |
| 96 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 97 | 4 | Tensor* wq = requireTensor(arg(args, 1), L, "Wq"); | |
| 98 | 4 | Tensor* bq = optionalTensor(arg(args, 2), L, "bq"); | |
| 99 | 4 | Tensor* wk = requireTensor(arg(args, 3), L, "Wk"); | |
| 100 | 4 | Tensor* bk = optionalTensor(arg(args, 4), L, "bk"); | |
| 101 | 4 | Tensor* wv = requireTensor(arg(args, 5), L, "Wv"); | |
| 102 | 4 | Tensor* bv = optionalTensor(arg(args, 6), L, "bv"); | |
| 103 | 4 | Tensor* wo = requireTensor(arg(args, 7), L, "Wo"); | |
| 104 | 4 | Tensor* bo = optionalTensor(arg(args, 8), L, "bo"); | |
| 105 | 4 | Tensor* rph = requireTensor(arg(args, 9), L, "relPosH"); | |
| 106 | 4 | Tensor* rpw = requireTensor(arg(args, 10), L, "relPosW"); | |
| 107 | 4 | int32_t numHeads = argInt(arg(args, 11), 1); | |
| 108 | 4 | int32_t gridH = argInt(arg(args, 12), 0); | |
| 109 | 4 | int32_t gridW = argInt(arg(args, 13), 0); | |
| 110 | 4 | double scale = argNum(arg(args, 14), 1.0); | |
| 111 | 4 | Tensor* o = requireTensor(arg(args, 15), L, "O"); | |
| 112 | 4 | BROTENSOR_API_TRY | |
| 113 | 4 | brotensor::self_attention_decomposed_rel_pos_forward( | |
| 114 | 4 | *x, *wq, bq, *wk, bk, *wv, bv, *wo, bo, *rph, *rpw, | |
| 115 | 4 | numHeads, gridH, gridW, static_cast<float>(scale), *o); | |
| 116 | 4 | BROTENSOR_API_CATCH(L) | |
| 117 | 4 | return ev::undefined(); | |
| 118 | ✗ | } | |
| 119 | |||
| 120 | 4 | ev::Value jsSelfAttentionDecomposedRelPosWindowedForward(ev::Value, std::span<const ev::Value> args) { | |
| 121 | 4 | const char* L = "selfAttentionDecomposedRelPosWindowedForward"; | |
| 122 | 4 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 123 | 4 | Tensor* wq = requireTensor(arg(args, 1), L, "Wq"); | |
| 124 | 4 | Tensor* bq = optionalTensor(arg(args, 2), L, "bq"); | |
| 125 | 4 | Tensor* wk = requireTensor(arg(args, 3), L, "Wk"); | |
| 126 | 4 | Tensor* bk = optionalTensor(arg(args, 4), L, "bk"); | |
| 127 | 4 | Tensor* wv = requireTensor(arg(args, 5), L, "Wv"); | |
| 128 | 4 | Tensor* bv = optionalTensor(arg(args, 6), L, "bv"); | |
| 129 | 4 | Tensor* wo = requireTensor(arg(args, 7), L, "Wo"); | |
| 130 | 4 | Tensor* bo = optionalTensor(arg(args, 8), L, "bo"); | |
| 131 | 4 | Tensor* rph = requireTensor(arg(args, 9), L, "relPosH"); | |
| 132 | 4 | Tensor* rpw = requireTensor(arg(args, 10), L, "relPosW"); | |
| 133 | 4 | int32_t numHeads = argInt(arg(args, 11), 1); | |
| 134 | 4 | int32_t gridH = argInt(arg(args, 12), 0); | |
| 135 | 4 | int32_t gridW = argInt(arg(args, 13), 0); | |
| 136 | 4 | int32_t window = argInt(arg(args, 14), 0); | |
| 137 | 4 | double scale = argNum(arg(args, 15), 1.0); | |
| 138 | 4 | Tensor* o = requireTensor(arg(args, 16), L, "O"); | |
| 139 | 4 | BROTENSOR_API_TRY | |
| 140 | 4 | brotensor::self_attention_decomposed_rel_pos_windowed_forward( | |
| 141 | 4 | *x, *wq, bq, *wk, bk, *wv, bv, *wo, bo, *rph, *rpw, | |
| 142 | 4 | numHeads, gridH, gridW, window, static_cast<float>(scale), *o); | |
| 143 | 4 | BROTENSOR_API_CATCH(L) | |
| 144 | 4 | return ev::undefined(); | |
| 145 | ✗ | } | |
| 146 | |||
| 147 | // ---- packed variable-length flash attention (Qwen-VL window attention) ----- | ||
| 148 | |||
| 149 | 14 | ev::Value jsFlashAttentionVarlenForward(ev::Value, std::span<const ev::Value> args) { | |
| 150 | 14 | const char* L = "flashAttentionVarlenForward"; | |
| 151 | 14 | Tensor* q = requireTensor(arg(args, 0), L, "Q"); | |
| 152 | 14 | Tensor* k = requireTensor(arg(args, 1), L, "K"); | |
| 153 | 14 | Tensor* v = requireTensor(arg(args, 2), L, "V"); | |
| 154 | 14 | Tensor* cuSeqQ = optionalTensor(arg(args, 3), L, "cuSeqQ"); | |
| 155 | 14 | Tensor* cuSeqK = optionalTensor(arg(args, 4), L, "cuSeqK"); | |
| 156 | 14 | int32_t batch = argInt(arg(args, 5), 0); | |
| 157 | 14 | int32_t maxQ = argInt(arg(args, 6), 0); | |
| 158 | 14 | int32_t maxK = argInt(arg(args, 7), 0); | |
| 159 | 14 | int32_t numHeads = argInt(arg(args, 8), 1); | |
| 160 | 14 | int32_t headDim = argInt(arg(args, 9), 0); | |
| 161 | 14 | bool causal = ev::toBool(arg(args, 10)); | |
| 162 | 14 | Tensor* o = requireTensor(arg(args, 11), L, "O"); | |
| 163 | 14 | needNonNegative(L, {batch, maxQ, maxK}); | |
| 164 | 14 | IdxStream cq, ck; | |
| 165 | 14 | needCuSeq(L, "cuSeqQ", cuSeqQ, batch, q->rows, maxQ, cq); | |
| 166 | 4 | needCuSeq(L, "cuSeqK", cuSeqK, batch, k->rows, maxK, ck); | |
| 167 | 4 | BROTENSOR_API_TRY | |
| 168 | 4 | cq.materialize(); | |
| 169 | 4 | ck.materialize(); | |
| 170 | 4 | brotensor::flash_attention_varlen_forward( | |
| 171 | 4 | *q, *k, *v, cq.ptr, ck.ptr, batch, maxQ, maxK, numHeads, headDim, causal, *o); | |
| 172 | 4 | BROTENSOR_API_CATCH(L) | |
| 173 | 4 | return ev::undefined(); | |
| 174 | 14 | } | |
| 175 | |||
| 176 | 2 | ev::Value jsFlashAttentionVarlenBackward(ev::Value, std::span<const ev::Value> args) { | |
| 177 | 2 | const char* L = "flashAttentionVarlenBackward"; | |
| 178 | 2 | Tensor* q = requireTensor(arg(args, 0), L, "Q"); | |
| 179 | 2 | Tensor* k = requireTensor(arg(args, 1), L, "K"); | |
| 180 | 2 | Tensor* v = requireTensor(arg(args, 2), L, "V"); | |
| 181 | 2 | Tensor* o = requireTensor(arg(args, 3), L, "O"); | |
| 182 | 2 | Tensor* dO = requireTensor(arg(args, 4), L, "dO"); | |
| 183 | 2 | Tensor* cuSeqQ = optionalTensor(arg(args, 5), L, "cuSeqQ"); | |
| 184 | 2 | Tensor* cuSeqK = optionalTensor(arg(args, 6), L, "cuSeqK"); | |
| 185 | 2 | int32_t batch = argInt(arg(args, 7), 0); | |
| 186 | 2 | int32_t maxQ = argInt(arg(args, 8), 0); | |
| 187 | 2 | int32_t maxK = argInt(arg(args, 9), 0); | |
| 188 | 2 | int32_t numHeads = argInt(arg(args, 10), 1); | |
| 189 | 2 | int32_t headDim = argInt(arg(args, 11), 0); | |
| 190 | 2 | bool causal = ev::toBool(arg(args, 12)); | |
| 191 | 2 | Tensor* dq = requireTensor(arg(args, 13), L, "dQ"); | |
| 192 | 2 | Tensor* dk = requireTensor(arg(args, 14), L, "dK"); | |
| 193 | 2 | Tensor* dv = requireTensor(arg(args, 15), L, "dV"); | |
| 194 | 2 | needNonNegative(L, {batch, maxQ, maxK}); | |
| 195 | 2 | needPair(L, "O", o, q); | |
| 196 | 2 | needPair(L, "dO", dO, q); | |
| 197 | 2 | IdxStream cq, ck; | |
| 198 | 2 | needCuSeq(L, "cuSeqQ", cuSeqQ, batch, q->rows, maxQ, cq); | |
| 199 | 2 | needCuSeq(L, "cuSeqK", cuSeqK, batch, k->rows, maxK, ck); | |
| 200 | 2 | BROTENSOR_API_TRY | |
| 201 | 2 | cq.materialize(); | |
| 202 | 2 | ck.materialize(); | |
| 203 | 2 | brotensor::flash_attention_varlen_backward( | |
| 204 | 2 | *q, *k, *v, *o, *dO, cq.ptr, ck.ptr, batch, maxQ, maxK, | |
| 205 | 2 | numHeads, headDim, causal, *dq, *dk, *dv); | |
| 206 | 2 | BROTENSOR_API_CATCH(L) | |
| 207 | 2 | return ev::undefined(); | |
| 208 | 2 | } | |
| 209 | |||
| 210 | // ---- gated delta rule (linear attention — Qwen3-Next) --------------------- | ||
| 211 | |||
| 212 | 4 | ev::Value jsGatedDeltaRuleChunked(ev::Value, std::span<const ev::Value> args) { | |
| 213 | 4 | const char* L = "gatedDeltaRuleChunked"; | |
| 214 | 4 | Tensor* q = requireTensor(arg(args, 0), L, "Q"); | |
| 215 | 4 | Tensor* k = requireTensor(arg(args, 1), L, "K"); | |
| 216 | 4 | Tensor* v = requireTensor(arg(args, 2), L, "V"); | |
| 217 | 4 | Tensor* aRaw = requireTensor(arg(args, 3), L, "aRaw"); | |
| 218 | 4 | Tensor* beta = requireTensor(arg(args, 4), L, "beta"); | |
| 219 | 4 | Tensor* logA = requireTensor(arg(args, 5), L, "logA"); | |
| 220 | 4 | int32_t numHeads = argInt(arg(args, 6), 1); | |
| 221 | 4 | int32_t d_k = argInt(arg(args, 7), 0); | |
| 222 | 4 | int32_t d_v = argInt(arg(args, 8), 0); | |
| 223 | 4 | Tensor* state = requireTensor(arg(args, 9), L, "state"); | |
| 224 | 4 | Tensor* o = requireTensor(arg(args, 10), L, "O"); | |
| 225 | 4 | BROTENSOR_API_TRY | |
| 226 | 4 | brotensor::gated_delta_rule_chunked( | |
| 227 | 4 | *q, *k, *v, *aRaw, *beta, *logA, numHeads, d_k, d_v, *state, *o); | |
| 228 | 4 | BROTENSOR_API_CATCH(L) | |
| 229 | 4 | return ev::undefined(); | |
| 230 | ✗ | } | |
| 231 | |||
| 232 | 6 | ev::Value jsGatedDeltaRuleStep(ev::Value, std::span<const ev::Value> args) { | |
| 233 | 6 | const char* L = "gatedDeltaRuleStep"; | |
| 234 | 6 | Tensor* q = requireTensor(arg(args, 0), L, "Q"); | |
| 235 | 6 | Tensor* k = requireTensor(arg(args, 1), L, "K"); | |
| 236 | 6 | Tensor* v = requireTensor(arg(args, 2), L, "V"); | |
| 237 | 6 | Tensor* aRaw = requireTensor(arg(args, 3), L, "aRaw"); | |
| 238 | 6 | Tensor* beta = requireTensor(arg(args, 4), L, "beta"); | |
| 239 | 6 | Tensor* logA = requireTensor(arg(args, 5), L, "logA"); | |
| 240 | 6 | int32_t numHeads = argInt(arg(args, 6), 1); | |
| 241 | 6 | int32_t d_k = argInt(arg(args, 7), 0); | |
| 242 | 6 | int32_t d_v = argInt(arg(args, 8), 0); | |
| 243 | 6 | Tensor* state = requireTensor(arg(args, 9), L, "state"); | |
| 244 | 6 | Tensor* o = requireTensor(arg(args, 10), L, "O"); | |
| 245 | 6 | BROTENSOR_API_TRY | |
| 246 | 6 | brotensor::gated_delta_rule_step( | |
| 247 | 6 | *q, *k, *v, *aRaw, *beta, *logA, numHeads, d_k, d_v, *state, *o); | |
| 248 | 6 | BROTENSOR_API_CATCH(L) | |
| 249 | 6 | return ev::undefined(); | |
| 250 | ✗ | } | |
| 251 | |||
| 252 | // ---- M-RoPE (Qwen-VL multimodal rotary) ----------------------------------- | ||
| 253 | |||
| 254 | 12 | ev::Value jsRopeApplyMrope(ev::Value, std::span<const ev::Value> args) { | |
| 255 | 12 | const char* L = "ropeApplyMrope"; | |
| 256 | 12 | Tensor* x = requireTensor(arg(args, 0), L, "X"); | |
| 257 | 12 | Tensor* cosT = requireTensor(arg(args, 1), L, "cosT"); | |
| 258 | 12 | Tensor* sinT = requireTensor(arg(args, 2), L, "sinT"); | |
| 259 | 12 | Tensor* cosH = requireTensor(arg(args, 3), L, "cosH"); | |
| 260 | 12 | Tensor* sinH = requireTensor(arg(args, 4), L, "sinH"); | |
| 261 | 12 | Tensor* cosW = requireTensor(arg(args, 5), L, "cosW"); | |
| 262 | 12 | Tensor* sinW = requireTensor(arg(args, 6), L, "sinW"); | |
| 263 | 12 | Tensor* posT = optionalTensor(arg(args, 7), L, "posT"); | |
| 264 | 12 | Tensor* posH = optionalTensor(arg(args, 8), L, "posH"); | |
| 265 | 12 | Tensor* posW = optionalTensor(arg(args, 9), L, "posW"); | |
| 266 | 12 | int32_t headDim = argInt(arg(args, 10), 0); | |
| 267 | 12 | int32_t numHeads = argInt(arg(args, 11), 1); | |
| 268 | 12 | int32_t d_t = argInt(arg(args, 12), 0); | |
| 269 | 12 | int32_t d_h = argInt(arg(args, 13), 0); | |
| 270 | 12 | int32_t d_w = argInt(arg(args, 14), 0); | |
| 271 | 12 | Tensor* y = requireTensor(arg(args, 15), L, "Y"); | |
| 272 | 12 | IdxStream pt, ph, pw; | |
| 273 | 12 | needPosStream(L, "posT", posT, d_t, x->rows, cosT, sinT, pt); | |
| 274 | 4 | needPosStream(L, "posH", posH, d_h, x->rows, cosH, sinH, ph); | |
| 275 | 4 | needPosStream(L, "posW", posW, d_w, x->rows, cosW, sinW, pw); | |
| 276 | 4 | BROTENSOR_API_TRY | |
| 277 | 4 | pt.materialize(); | |
| 278 | 4 | ph.materialize(); | |
| 279 | 4 | pw.materialize(); | |
| 280 | 4 | brotensor::rope_apply_mrope( | |
| 281 | 4 | *x, *cosT, *sinT, *cosH, *sinH, *cosW, *sinW, | |
| 282 | 4 | pt.ptr, ph.ptr, pw.ptr, headDim, numHeads, d_t, d_h, d_w, *y); | |
| 283 | 4 | BROTENSOR_API_CATCH(L) | |
| 284 | 4 | return ev::undefined(); | |
| 285 | 12 | } | |
| 286 | |||
| 287 | } // namespace | ||
| 288 | |||
| 289 | 2 | void installTensorAttn2(bronze::Value ns) { | |
| 290 | 2 | ev::ObjectBuilder b(ns); | |
| 291 | 2 | b.def("selfAttentionDecomposedRelPosForward", 16, &jsSelfAttentionDecomposedRelPosForward); | |
| 292 | 2 | b.def("selfAttentionDecomposedRelPosWindowedForward", 17, &jsSelfAttentionDecomposedRelPosWindowedForward); | |
| 293 | 2 | b.def("flashAttentionVarlenForward", 12, &jsFlashAttentionVarlenForward); | |
| 294 | 2 | b.def("flashAttentionVarlenBackward", 16, &jsFlashAttentionVarlenBackward); | |
| 295 | 2 | b.def("gatedDeltaRuleChunked", 11, &jsGatedDeltaRuleChunked); | |
| 296 | 2 | b.def("gatedDeltaRuleStep", 11, &jsGatedDeltaRuleStep); | |
| 297 | 2 | b.def("ropeApplyMrope", 16, &jsRopeApplyMrope); | |
| 298 | 2 | } | |
| 299 | |||
| 300 | } // namespace brotensor::api | ||
| 301 |