brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
498 / 505
98.6%
Branches:
178 / 672
26.5%
Functions:
34 / 34
100.0%

src/api/native_tensor_attention.cpp
Line Branch Exec Source
1 #include "api_internal.h"
2
3 #include <cmath>
4 #include <cstdint>
5 #include <span>
6 #include <string>
7
8 namespace brotensor::api {
9
10 namespace {
11
12 namespace ev = bronze::embed;
13 using Value = bronze::Value;
14 using brotensor::Tensor;
15
16 66 int32_t optInt(Value v, int32_t def) {
17
2/4
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 66 times.
66 if (ev::isUndefined(v) || ev::isNull(v)) return def;
18 66 double d = ev::toDouble(v);
19
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 return std::isfinite(d) ? static_cast<int32_t>(static_cast<int64_t>(d)) : 0;
20 66 }
21
22 6 double optNum(Value v, double def) {
23
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
6 if (ev::isUndefined(v) || ev::isNull(v)) return def;
24 2 return ev::toDouble(v);
25 6 }
26
27 12 bool optBool(Value v) {
28
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 return !ev::isUndefined(v) && ev::toBool(v);
29 }
30
31 68 Tensor* optT(ev::Nullable<GpuTensorHandle> h) {
32
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 62 times.
68 return h.ptr ? &h.ptr->tensor : nullptr;
33 }
34
35 72 Tensor* propReq(ev::Persistent& opts, const char* key, const char* L) {
36 72 return requireTensor(ev::getProperty(opts.get(), key), L, key);
37 }
38
39 54 Tensor* propOpt(ev::Persistent& opts, const char* key, const char* L) {
40 54 return optionalTensor(ev::getProperty(opts.get(), key), L, key);
41 }
42
43 24 void needSquareProj(const char* L, const Tensor* X, const Tensor* Wq, const Tensor* Wk,
44 const Tensor* Wv, const Tensor* Wo) {
45 24 const int64_t DD = elems({X->cols, X->cols});
46 24 needOperands(L, X, {{"Wq", Wq, DD}, {"Wk", Wk, DD}, {"Wv", Wv, DD}, {"Wo", Wo, DD}});
47 24 }
48
49 12 void needCrossProj(const char* L, const Tensor* X, const Tensor* Ctx, const Tensor* Wq,
50 const Tensor* Wk, const Tensor* Wv, const Tensor* Wo) {
51 12 const int64_t DD = elems({X->cols, X->cols});
52 12 const int64_t DC = elems({X->cols, Ctx->cols});
53 12 needSameDtype(L, "Ctx", Ctx, X);
54 12 needOperands(L, X, {{"Wq", Wq, DD}, {"Wk", Wk, DC}, {"Wv", Wv, DC}, {"Wo", Wo, DD}});
55 12 }
56
57 4 void needMhaCaches(const char* L, const Tensor* X, int32_t numHeads,
58 const Tensor* dO, const Tensor* Qh, const Tensor* Kh, const Tensor* Vh,
59 const Tensor* Attnh, const Tensor* Yconcat) {
60 4 const int64_t KD = elems({X->rows, X->cols});
61 12 needOperands(L, X, {{"dO", dO, KD}, {"Qh", Qh, KD}, {"Kh", Kh, KD}, {"Vh", Vh, KD},
62 4 {"Attnh", Attnh, elems({numHeads, X->rows, X->rows})},
63 4 {"Yconcat", Yconcat, KD}});
64 4 }
65
66 6 void needSquareGrads(const char* L, const Tensor* X, const Tensor* dWq, const Tensor* dWk,
67 const Tensor* dWv, const Tensor* dWo) {
68 6 const int64_t DD = elems({X->cols, X->cols});
69 6 needOperands(L, X, {{"dWq", dWq, DD}, {"dWk", dWk, DD}, {"dWv", dWv, DD}, {"dWo", dWo, DD}});
70 6 }
71
72 8 void needFlashQkv(const char* L, const Tensor* Q, const Tensor* K, const Tensor* V,
73 int32_t numHeads, bool causal) {
74 8 needHeads(L, Q->cols, numHeads);
75
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (K->cols != Q->cols || V->cols != Q->cols || V->rows != K->rows) {
76
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ setError(std::string(L) + ": Q is (Lq,D); K and V must both be (Lk,D)");
77 }
78
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
8 if (causal && Q->rows != K->rows) {
79
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ setError(std::string(L) + ": causal attention needs Lq == Lk");
80 }
81 8 needSameDtype(L, "K", K, Q);
82 8 needSameDtype(L, "V", V, Q);
83 8 }
84
85 // ---- single-head & multi-head attention ------------------------------------
86
87 6 void attnForward(GpuTensorHandle* X, GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv,
88 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask, GpuTensorHandle* Q,
89 GpuTensorHandle* K, GpuTensorHandle* V, GpuTensorHandle* Attn,
90 GpuTensorHandle* Y_pre_Wo, GpuTensorHandle* O) {
91 6 const char* L = "attentionForward";
92 6 Tensor* x = &X->tensor;
93 6 Tensor* mt = optT(mask);
94 6 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
95 6 needMask(L, mt, x->rows);
96 6 BROTENSOR_API_TRY
97
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
12 brotensor::attention_forward(*x, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
98 6 maskPtr(mt), Q->tensor, K->tensor, V->tensor,
99 6 Attn->tensor, Y_pre_Wo->tensor, O->tensor);
100
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 6 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
6 BROTENSOR_API_CATCH(L)
101 6 }
102
103 2 void attnBackward(GpuTensorHandle* dO, GpuTensorHandle* X, GpuTensorHandle* Q, GpuTensorHandle* K,
104 GpuTensorHandle* V, GpuTensorHandle* Attn, GpuTensorHandle* Y_pre_Wo,
105 GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv, GpuTensorHandle* Wo,
106 ev::Nullable<GpuTensorHandle> mask, GpuTensorHandle* dX, GpuTensorHandle* dWq,
107 GpuTensorHandle* dWk, GpuTensorHandle* dWv, GpuTensorHandle* dWo) {
108 2 const char* L = "attentionBackward";
109 2 Tensor* x = &X->tensor;
110 2 Tensor* mt = optT(mask);
111 2 const int64_t ND = elems({x->rows, x->cols});
112 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
113 2 needSquareGrads(L, x, &dWq->tensor, &dWk->tensor, &dWv->tensor, &dWo->tensor);
114 8 needOperands(L, x, {{"dO", &dO->tensor, ND}, {"Q", &Q->tensor, ND}, {"K", &K->tensor, ND},
115 4 {"V", &V->tensor, ND}, {"Attn", &Attn->tensor, elems({x->rows, x->rows})},
116 2 {"Y_pre_Wo", &Y_pre_Wo->tensor, ND}});
117 2 needMask(L, mt, x->rows);
118 2 BROTENSOR_API_TRY
119
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::attention_backward(dO->tensor, *x, Q->tensor, K->tensor, V->tensor,
120 2 Attn->tensor, Y_pre_Wo->tensor,
121 2 Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
122 2 maskPtr(mt), dX->tensor, dWq->tensor, dWk->tensor,
123 2 dWv->tensor, dWo->tensor);
124
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
125 2 }
126
127 2 void attnMhaForward(GpuTensorHandle* X, GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv,
128 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask, Value numHeadsV,
129 GpuTensorHandle* Qh, GpuTensorHandle* Kh, GpuTensorHandle* Vh,
130 GpuTensorHandle* Attnh, GpuTensorHandle* Yconcat, GpuTensorHandle* O) {
131 2 const char* L = "mhaForward";
132 2 const int32_t numHeads = optInt(numHeadsV, 1);
133 2 Tensor* x = &X->tensor;
134 2 Tensor* mt = optT(mask);
135 2 needHeads(L, x->cols, numHeads);
136 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
137 2 needMask(L, mt, x->rows);
138 2 BROTENSOR_API_TRY
139
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::mha_forward(*x, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
140 2 maskPtr(mt), numHeads, Qh->tensor, Kh->tensor, Vh->tensor,
141 2 Attnh->tensor, Yconcat->tensor, O->tensor);
142
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
143 2 }
144
145 2 void attnMhaBackward(GpuTensorHandle* dO, GpuTensorHandle* X, GpuTensorHandle* Qh, GpuTensorHandle* Kh,
146 GpuTensorHandle* Vh, GpuTensorHandle* Attnh, GpuTensorHandle* Yconcat,
147 GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv, GpuTensorHandle* Wo,
148 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, GpuTensorHandle* dX,
149 GpuTensorHandle* dWq, GpuTensorHandle* dWk, GpuTensorHandle* dWv, GpuTensorHandle* dWo) {
150 2 const char* L = "mhaBackward";
151 2 const int32_t numHeads = optInt(numHeadsV, 1);
152 2 Tensor* x = &X->tensor;
153 2 Tensor* mt = optT(mask);
154 2 needHeads(L, x->cols, numHeads);
155 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
156 2 needSquareGrads(L, x, &dWq->tensor, &dWk->tensor, &dWv->tensor, &dWo->tensor);
157 4 needMhaCaches(L, x, numHeads, &dO->tensor, &Qh->tensor, &Kh->tensor, &Vh->tensor,
158 2 &Attnh->tensor, &Yconcat->tensor);
159 2 needMask(L, mt, x->rows);
160 2 BROTENSOR_API_TRY
161
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::mha_backward(dO->tensor, *x, Qh->tensor, Kh->tensor, Vh->tensor,
162 2 Attnh->tensor, Yconcat->tensor,
163 2 Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
164 2 maskPtr(mt), numHeads, dX->tensor, dWq->tensor, dWk->tensor,
165 2 dWv->tensor, dWo->tensor);
166
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
167 2 }
168
169 2 void attnSelfForward(GpuTensorHandle* X, GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv,
170 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask, Value numHeadsV,
171 GpuTensorHandle* O) {
172 2 const char* L = "selfAttentionForward";
173 2 const int32_t numHeads = optInt(numHeadsV, 1);
174 2 Tensor* x = &X->tensor;
175 2 Tensor* mt = optT(mask);
176 2 needHeads(L, x->cols, numHeads);
177 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
178 2 needMask(L, mt, x->rows);
179 2 BROTENSOR_API_TRY
180
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::self_attention_forward(*x, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
181 2 maskPtr(mt), numHeads, O->tensor);
182
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
183 2 }
184
185 2 void attnSelfForwardTrain(GpuTensorHandle* X, GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv,
186 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask, Value numHeadsV,
187 GpuTensorHandle* Qh, GpuTensorHandle* Kh, GpuTensorHandle* Vh,
188 GpuTensorHandle* Attnh, GpuTensorHandle* Yconcat, GpuTensorHandle* O) {
189 2 const char* L = "selfAttentionForwardTrain";
190 2 const int32_t numHeads = optInt(numHeadsV, 1);
191 2 Tensor* x = &X->tensor;
192 2 Tensor* mt = optT(mask);
193 2 needHeads(L, x->cols, numHeads);
194 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
195 2 needMask(L, mt, x->rows);
196 2 BROTENSOR_API_TRY
197
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::self_attention_forward_train(*x, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
198 2 maskPtr(mt), numHeads, Qh->tensor, Kh->tensor, Vh->tensor,
199 2 Attnh->tensor, Yconcat->tensor, O->tensor);
200
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
201 2 }
202
203 2 void attnSelfBackward(GpuTensorHandle* dO, GpuTensorHandle* X, GpuTensorHandle* Qh, GpuTensorHandle* Kh,
204 GpuTensorHandle* Vh, GpuTensorHandle* Attnh, GpuTensorHandle* Yconcat,
205 GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv, GpuTensorHandle* Wo,
206 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, GpuTensorHandle* dX,
207 GpuTensorHandle* dWq, GpuTensorHandle* dWk, GpuTensorHandle* dWv, GpuTensorHandle* dWo) {
208 2 const char* L = "selfAttentionBackward";
209 2 const int32_t numHeads = optInt(numHeadsV, 1);
210 2 Tensor* x = &X->tensor;
211 2 Tensor* mt = optT(mask);
212 2 needHeads(L, x->cols, numHeads);
213 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
214 2 needSquareGrads(L, x, &dWq->tensor, &dWk->tensor, &dWv->tensor, &dWo->tensor);
215 4 needMhaCaches(L, x, numHeads, &dO->tensor, &Qh->tensor, &Kh->tensor, &Vh->tensor,
216 2 &Attnh->tensor, &Yconcat->tensor);
217 2 needMask(L, mt, x->rows);
218 2 BROTENSOR_API_TRY
219
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::self_attention_backward(dO->tensor, *x, Qh->tensor, Kh->tensor, Vh->tensor,
220 2 Attnh->tensor, Yconcat->tensor,
221 2 Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
222 2 maskPtr(mt), numHeads, dX->tensor, dWq->tensor, dWk->tensor,
223 2 dWv->tensor, dWo->tensor);
224
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
225 2 }
226
227 2 void attnSelfBiasForward(GpuTensorHandle* X, GpuTensorHandle* Wq, GpuTensorHandle* Wk, GpuTensorHandle* Wv,
228 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask,
229 ev::Nullable<GpuTensorHandle> attnBias, Value numHeadsV, Value scaleV,
230 GpuTensorHandle* O) {
231 2 const char* L = "selfAttentionBiasForward";
232 2 const int32_t numHeads = optInt(numHeadsV, 1);
233 2 const double scale = optNum(scaleV, 1.0);
234 2 Tensor* x = &X->tensor;
235 2 Tensor* mt = optT(mask);
236 2 Tensor* bt = optT(attnBias);
237 2 needHeads(L, x->cols, numHeads);
238 2 needSquareProj(L, x, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
239 2 needMask(L, mt, x->rows);
240 2 needMask(L, bt, elems({numHeads, x->rows, x->rows}), "attnBias");
241 2 BROTENSOR_API_TRY
242
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::self_attention_bias_forward(*x, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
243 nullptr, nullptr, nullptr, nullptr,
244 2 maskPtr(mt), bt, numHeads, static_cast<float>(scale),
245 2 O->tensor);
246
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
247 2 }
248
249 // ---- cross-attention -------------------------------------------------------
250
251 2 void attnCrossForward(GpuTensorHandle* X, GpuTensorHandle* Ctx, GpuTensorHandle* Wq, GpuTensorHandle* Wk,
252 GpuTensorHandle* Wv, GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask,
253 Value numHeadsV, GpuTensorHandle* O) {
254 2 const char* L = "crossAttentionForward";
255 2 const int32_t numHeads = optInt(numHeadsV, 1);
256 2 Tensor* x = &X->tensor;
257 2 Tensor* ctx = &Ctx->tensor;
258 2 Tensor* mt = optT(mask);
259 2 needHeads(L, x->cols, numHeads);
260 2 needCrossProj(L, x, ctx, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
261 2 needMask(L, mt, ctx->rows);
262 2 BROTENSOR_API_TRY
263
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::cross_attention_forward(*x, *ctx, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
264 2 maskPtr(mt), numHeads, O->tensor);
265
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
266 2 }
267
268 2 void attnCrossForwardWithAttn(GpuTensorHandle* X, GpuTensorHandle* Ctx, GpuTensorHandle* Wq,
269 GpuTensorHandle* Wk, GpuTensorHandle* Wv, GpuTensorHandle* Wo,
270 ev::Nullable<GpuTensorHandle> mask, ev::Nullable<GpuTensorHandle> attnLogitBias,
271 Value numHeadsV, GpuTensorHandle* O, GpuTensorHandle* AttnAvg) {
272 2 const char* L = "crossAttentionForwardWithAttn";
273 2 const int32_t numHeads = optInt(numHeadsV, 1);
274 2 Tensor* x = &X->tensor;
275 2 Tensor* ctx = &Ctx->tensor;
276 2 Tensor* mt = optT(mask);
277 2 Tensor* lbt = optT(attnLogitBias);
278 2 needHeads(L, x->cols, numHeads);
279 2 needCrossProj(L, x, ctx, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
280 2 needMask(L, mt, ctx->rows);
281 2 needMask(L, lbt, elems({x->rows, ctx->rows}), "attnLogitBias");
282 2 BROTENSOR_API_TRY
283
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::cross_attention_forward_with_attn(*x, *ctx, Wq->tensor, Wk->tensor, Wv->tensor,
284 2 Wo->tensor, maskPtr(mt), lbt, numHeads,
285 2 O->tensor, AttnAvg->tensor);
286
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
287 2 }
288
289 2 void attnCrossForwardTrain(GpuTensorHandle* X, GpuTensorHandle* Ctx, GpuTensorHandle* Wq, GpuTensorHandle* Wk,
290 GpuTensorHandle* Wv, GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask,
291 Value numHeadsV, GpuTensorHandle* Qh, GpuTensorHandle* Kh, GpuTensorHandle* Vh,
292 GpuTensorHandle* Attnh, GpuTensorHandle* Yconcat, GpuTensorHandle* O) {
293 2 const char* L = "crossAttentionForwardTrain";
294 2 const int32_t numHeads = optInt(numHeadsV, 1);
295 2 Tensor* x = &X->tensor;
296 2 Tensor* ctx = &Ctx->tensor;
297 2 Tensor* mt = optT(mask);
298 2 needHeads(L, x->cols, numHeads);
299 2 needCrossProj(L, x, ctx, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
300 2 needMask(L, mt, ctx->rows);
301 2 BROTENSOR_API_TRY
302
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::cross_attention_forward_train(*x, *ctx, Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
303 2 maskPtr(mt), numHeads, Qh->tensor, Kh->tensor, Vh->tensor,
304 2 Attnh->tensor, Yconcat->tensor, O->tensor);
305
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
306 2 }
307
308 2 void attnCrossBackward(GpuTensorHandle* dO, GpuTensorHandle* X, GpuTensorHandle* Ctx, GpuTensorHandle* Qh,
309 GpuTensorHandle* Kh, GpuTensorHandle* Vh, GpuTensorHandle* Attnh,
310 GpuTensorHandle* Yconcat, GpuTensorHandle* Wq, GpuTensorHandle* Wk,
311 GpuTensorHandle* Wv, GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> mask,
312 Value numHeadsV, GpuTensorHandle* dX, GpuTensorHandle* dCtx, GpuTensorHandle* dWq,
313 GpuTensorHandle* dWk, GpuTensorHandle* dWv, GpuTensorHandle* dWo) {
314 2 const char* L = "crossAttentionBackward";
315 2 const int32_t numHeads = optInt(numHeadsV, 1);
316 2 Tensor* x = &X->tensor;
317 2 Tensor* ctx = &Ctx->tensor;
318 2 Tensor* mt = optT(mask);
319 2 needHeads(L, x->cols, numHeads);
320 2 needCrossProj(L, x, ctx, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
321 2 const int64_t QD = elems({x->rows, x->cols});
322 2 const int64_t KD = elems({ctx->rows, x->cols});
323 2 const int64_t DD = elems({x->cols, x->cols});
324 2 const int64_t DC = elems({x->cols, ctx->cols});
325 16 needOperands(L, x, {{"dO", &dO->tensor, QD}, {"Qh", &Qh->tensor, QD}, {"Kh", &Kh->tensor, KD},
326 4 {"Vh", &Vh->tensor, KD}, {"Attnh", &Attnh->tensor, elems({numHeads, x->rows, ctx->rows})},
327 4 {"Yconcat", &Yconcat->tensor, QD}, {"dWq", &dWq->tensor, DD},
328 6 {"dWk", &dWk->tensor, DC}, {"dWv", &dWv->tensor, DC}, {"dWo", &dWo->tensor, DD}});
329 2 needMask(L, mt, ctx->rows);
330 2 BROTENSOR_API_TRY
331
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::cross_attention_backward(dO->tensor, *x, *ctx, Qh->tensor, Kh->tensor, Vh->tensor,
332 2 Attnh->tensor, Yconcat->tensor,
333 2 Wq->tensor, Wk->tensor, Wv->tensor, Wo->tensor,
334 2 maskPtr(mt), numHeads, dX->tensor, dCtx->tensor,
335 2 dWq->tensor, dWk->tensor, dWv->tensor, dWo->tensor);
336
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
337 2 }
338
339 // ---- flash attention -------------------------------------------------------
340
341 4 void attnFlashForward(GpuTensorHandle* Q, GpuTensorHandle* K, GpuTensorHandle* V,
342 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, Value causalV,
343 GpuTensorHandle* O) {
344 4 const char* L = "flashAttentionForward";
345 4 const int32_t numHeads = optInt(numHeadsV, 1);
346 4 const bool causal = optBool(causalV);
347 4 Tensor* mt = optT(mask);
348 4 needFlashQkv(L, &Q->tensor, &K->tensor, &V->tensor, numHeads, causal);
349 4 needMask(L, mt, K->tensor.rows);
350 4 BROTENSOR_API_TRY
351
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
8 brotensor::flash_attention_forward(Q->tensor, K->tensor, V->tensor, maskPtr(mt),
352 4 numHeads, causal, O->tensor);
353
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
4 BROTENSOR_API_CATCH(L)
354 4 }
355
356 2 void attnFlashWindowedForward(GpuTensorHandle* Q, GpuTensorHandle* K, GpuTensorHandle* V,
357 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, Value windowV,
358 GpuTensorHandle* O) {
359 2 const char* L = "flashAttentionWindowedForward";
360 2 const int32_t numHeads = optInt(numHeadsV, 1);
361 2 const int32_t window = optInt(windowV, 0);
362 2 Tensor* mt = optT(mask);
363 2 needFlashQkv(L, &Q->tensor, &K->tensor, &V->tensor, numHeads, false);
364
1/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (K->tensor.rows < Q->tensor.rows) setError("flashAttentionWindowedForward: needs Lk >= Lq");
365 2 needMask(L, mt, K->tensor.rows);
366 2 BROTENSOR_API_TRY
367
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::flash_attention_windowed_forward(Q->tensor, K->tensor, V->tensor, maskPtr(mt),
368 2 numHeads, window, O->tensor);
369
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
370 2 }
371
372 2 void attnFlashBackward(GpuTensorHandle* Q, GpuTensorHandle* K, GpuTensorHandle* V, GpuTensorHandle* O,
373 GpuTensorHandle* dO, ev::Nullable<GpuTensorHandle> mask, Value numHeadsV,
374 Value causalV, GpuTensorHandle* dQ, GpuTensorHandle* dK, GpuTensorHandle* dV) {
375 2 const char* L = "flashAttentionBackward";
376 2 const int32_t numHeads = optInt(numHeadsV, 1);
377 2 const bool causal = optBool(causalV);
378 2 Tensor* mt = optT(mask);
379 2 needFlashQkv(L, &Q->tensor, &K->tensor, &V->tensor, numHeads, causal);
380 2 needPair(L, "dO", &dO->tensor, &Q->tensor);
381 2 needMask(L, mt, K->tensor.rows);
382 2 BROTENSOR_API_TRY
383
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::flash_attention_backward(Q->tensor, K->tensor, V->tensor, O->tensor, dO->tensor,
384 2 maskPtr(mt), numHeads, causal,
385 2 dQ->tensor, dK->tensor, dV->tensor);
386
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
387 2 }
388
389 2 void attnFlashQkvoForward(GpuTensorHandle* X, ev::Nullable<GpuTensorHandle> Ctx,
390 GpuTensorHandle* Wq, ev::Nullable<GpuTensorHandle> bq,
391 GpuTensorHandle* Wk, ev::Nullable<GpuTensorHandle> bk,
392 GpuTensorHandle* Wv, ev::Nullable<GpuTensorHandle> bv,
393 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> bo,
394 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, Value causalV,
395 GpuTensorHandle* O) {
396 2 const char* L = "flashAttentionQkvoForward";
397 2 const int32_t numHeads = optInt(numHeadsV, 1);
398 2 const bool causal = optBool(causalV);
399 2 Tensor* x = &X->tensor;
400 2 Tensor* ctx = optT(Ctx);
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 Tensor* kv = ctx ? ctx : x;
402 2 Tensor* bqt = optT(bq);
403 2 Tensor* bkt = optT(bk);
404 2 Tensor* bvt = optT(bv);
405 2 Tensor* bot = optT(bo);
406 2 Tensor* mt = optT(mask);
407 2 needHeads(L, x->cols, numHeads);
408 2 needCrossProj(L, x, kv, &Wq->tensor, &Wk->tensor, &Wv->tensor, &Wo->tensor);
409 6 needOperands(L, x, {{"bq", bqt, x->cols}, {"bk", bkt, x->cols},
410 4 {"bv", bvt, x->cols}, {"bo", bot, x->cols}});
411 2 needMask(L, mt, kv->rows);
412 2 BROTENSOR_API_TRY
413
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::flash_attention_qkvo_forward(*x, ctx, Wq->tensor, bqt, Wk->tensor, bkt,
414 2 Wv->tensor, bvt, Wo->tensor, bot,
415 2 maskPtr(mt), numHeads, causal, O->tensor);
416
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
417 2 }
418
419 4 Value attnFlashQkvoBackward(Value, std::span<const Value> args) {
420 4 const char* L = "flashAttentionQkvoBackward(opts)";
421
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (args.empty() || !ev::isObject(arg(args, 0))) {
422
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 ev::throwTypeError(std::string(L) + " — opts is an object; see docs");
423 }
424 4 ev::Persistent opts(arg(args, 0));
425
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Tensor* x = propReq(opts, "X", L);
426
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Tensor* ctx = propOpt(opts, "Ctx", L);
427
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 Tensor* Wq = propReq(opts, "Wq", L);
428
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* bq = propOpt(opts, "bq", L);
429
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* Wk = propReq(opts, "Wk", L);
430
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* bk = propOpt(opts, "bk", L);
431
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* Wv = propReq(opts, "Wv", L);
432
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* bv = propOpt(opts, "bv", L);
433
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* Wo = propReq(opts, "Wo", L);
434
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* bo = propOpt(opts, "bo", L);
435
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* mask = propOpt(opts, "mask", L);
436
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 int32_t numHeads = optInt(ev::getProperty(opts.get(), "numHeads"), 1);
437
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 bool causal = optBool(ev::getProperty(opts.get(), "causal"));
438
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dO = propReq(opts, "dO", L);
439
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dX = propReq(opts, "dX", L);
440
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dCtx = propOpt(opts, "dCtx", L);
441
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dWq = propReq(opts, "dWq", L);
442
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dbq = propOpt(opts, "dbq", L);
443
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dWk = propReq(opts, "dWk", L);
444
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dbk = propOpt(opts, "dbk", L);
445
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dWv = propReq(opts, "dWv", L);
446
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dbv = propOpt(opts, "dbv", L);
447
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dWo = propReq(opts, "dWo", L);
448
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dbo = propOpt(opts, "dbo", L);
449
450 2 const char* fn = "flashAttentionQkvoBackward";
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 const Tensor* kv = ctx ? ctx : x;
452
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 needHeads(fn, x->cols, numHeads);
453
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 needCrossProj(fn, x, kv, Wq, Wk, Wv, Wo);
454 2 const int64_t D = x->cols;
455
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 const int64_t DD = elems({D, D});
456
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 const int64_t DC = elems({D, kv->cols});
457
5/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
10 needOperands(fn, x, {{"bq", bq, D}, {"bk", bk, D}, {"bv", bv, D}, {"bo", bo, D},
458 8 {"dbq", dbq, D}, {"dbk", dbk, D}, {"dbv", dbv, D}, {"dbo", dbo, D},
459
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 {"dO", dO, elems({x->rows, D})},
460 8 {"dWq", dWq, DD}, {"dWk", dWk, DC}, {"dWv", dWv, DC}, {"dWo", dWo, DD}});
461
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 needMask(fn, mask, kv->rows);
462 2 BROTENSOR_API_TRY
463
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 brotensor::flash_attention_qkvo_backward(*x, ctx, *Wq, bq, *Wk, bk, *Wv, bv, *Wo, bo,
464
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 maskPtr(mask), numHeads, causal, *dO,
465 2 *dX, dCtx, *dWq, dbq, *dWk, dbk, *dWv, dbv, *dWo, dbo);
466
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(fn)
467
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return ev::undefined();
468 4 }
469
470 2 void attnFlashProjectKv(GpuTensorHandle* ctx, GpuTensorHandle* Wk, ev::Nullable<GpuTensorHandle> bk,
471 GpuTensorHandle* Wv, ev::Nullable<GpuTensorHandle> bv,
472 GpuTensorHandle* K_out, GpuTensorHandle* V_out) {
473 2 const char* L = "flashAttentionProjectKv";
474 2 Tensor* c = &ctx->tensor;
475 2 Tensor* wk = &Wk->tensor;
476 2 Tensor* bkt = optT(bk);
477 2 Tensor* bvt = optT(bv);
478 2 const int64_t D = wk->rows;
479 6 needOperands(L, c, {{"Wk", wk, elems({D, c->cols})}, {"Wv", &Wv->tensor, elems({D, c->cols})},
480 4 {"bk", bkt, D}, {"bv", bvt, D}});
481
1/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (wk->cols != c->cols) setError("flashAttentionProjectKv: Wk must be (D, ctx.cols)");
482 2 BROTENSOR_API_TRY
483
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 brotensor::flash_attention_project_kv(*c, *wk, bkt, Wv->tensor, bvt, K_out->tensor, V_out->tensor);
484
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
485 2 }
486
487 2 void attnFlashQWithKvCachedForward(GpuTensorHandle* X, GpuTensorHandle* K, GpuTensorHandle* V,
488 GpuTensorHandle* Wq, ev::Nullable<GpuTensorHandle> bq,
489 GpuTensorHandle* Wo, ev::Nullable<GpuTensorHandle> bo,
490 ev::Nullable<GpuTensorHandle> mask, Value numHeadsV, Value causalV,
491 GpuTensorHandle* O) {
492 2 const char* L = "flashAttentionQWithKvCachedForward";
493 2 const int32_t numHeads = optInt(numHeadsV, 1);
494 2 const bool causal = optBool(causalV);
495 2 Tensor* x = &X->tensor;
496 2 Tensor* kt = &K->tensor;
497 2 Tensor* vt = &V->tensor;
498 2 Tensor* bqt = optT(bq);
499 2 Tensor* bot = optT(bo);
500 2 Tensor* mt = optT(mask);
501 2 const int64_t D = x->cols;
502 2 needHeads(L, D, numHeads);
503
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (kt->cols != D || vt->cols != D || vt->rows != kt->rows) {
504
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ setError("flashAttentionQWithKvCachedForward: K and V must both be (Lk, D)");
505 }
506
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2 if (causal && x->rows != kt->rows) setError("flashAttentionQWithKvCachedForward: causal attention needs Lq == Lk");
507 8 needOperands(L, x, {{"K", kt, 0}, {"V", vt, 0}, {"Wq", &Wq->tensor, elems({D, D})},
508 6 {"Wo", &Wo->tensor, elems({D, D})}, {"bq", bqt, D}, {"bo", bot, D}});
509 2 needMask(L, mt, kt->rows);
510 2 BROTENSOR_API_TRY
511
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::flash_attention_q_with_kv_cached_forward(*x, *kt, *vt, Wq->tensor, bqt,
512 2 Wo->tensor, bot, maskPtr(mt),
513 2 numHeads, causal, O->tensor);
514
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH(L)
515 2 }
516
517 // ---- resblock --------------------------------------------------------------
518
519 struct ResblockCommon {
520 Tensor* X;
521 Tensor* gamma1;
522 Tensor* beta1;
523 Tensor* W1;
524 Tensor* b1;
525 Tensor* t_emb_shift;
526 Tensor* gamma2;
527 Tensor* beta2;
528 Tensor* W2;
529 Tensor* b2;
530 Tensor* Wskip;
531 Tensor* bskip;
532 int32_t N;
533 int32_t C_in;
534 int32_t C_out;
535 int32_t H;
536 int32_t W;
537 int32_t numGroups;
538 double eps;
539 };
540
541 4 ResblockCommon readResblockCommon(ev::Persistent& opts, const char* optsLabel, const char* fnLabel) {
542 4 ResblockCommon r{};
543 4 r.X = propReq(opts, "X", optsLabel);
544 4 r.gamma1 = propReq(opts, "gamma1", optsLabel);
545 4 r.beta1 = propReq(opts, "beta1", optsLabel);
546 4 r.W1 = propReq(opts, "W1", optsLabel);
547 4 r.b1 = propOpt(opts, "b1", optsLabel);
548 4 r.t_emb_shift = propOpt(opts, "t_emb_shift", optsLabel);
549 4 r.gamma2 = propReq(opts, "gamma2", optsLabel);
550 4 r.beta2 = propReq(opts, "beta2", optsLabel);
551 4 r.W2 = propReq(opts, "W2", optsLabel);
552 4 r.b2 = propOpt(opts, "b2", optsLabel);
553 4 r.Wskip = propOpt(opts, "Wskip", optsLabel);
554 4 r.bskip = propOpt(opts, "bskip", optsLabel);
555 4 r.N = optInt(ev::getProperty(opts.get(), "N"), 0);
556 4 r.C_in = optInt(ev::getProperty(opts.get(), "C_in"), 0);
557 4 r.C_out = optInt(ev::getProperty(opts.get(), "C_out"), 0);
558 4 r.H = optInt(ev::getProperty(opts.get(), "H"), 0);
559 4 r.W = optInt(ev::getProperty(opts.get(), "W"), 0);
560 4 r.numGroups = optInt(ev::getProperty(opts.get(), "numGroups"), 32);
561 4 r.eps = optNum(ev::getProperty(opts.get(), "eps"), 0.00001);
562
563 4 needPositive(fnLabel, {r.numGroups});
564 4 needNonNegative(fnLabel, {r.N, r.C_in, r.C_out, r.H, r.W});
565
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (r.C_in % r.numGroups != 0 || r.C_out % r.numGroups != 0) {
566
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ setError(std::string(fnLabel) + ": numGroups must divide C_in and C_out");
567 }
568
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if (r.C_in != r.C_out && !r.Wskip) {
569
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ setError(std::string(fnLabel) + ": C_in != C_out needs a Wskip projection");
570 }
571 48 needOperands(fnLabel, r.X, {{"X", r.X, elems({r.N, r.C_in, r.H, r.W})},
572 8 {"gamma1", r.gamma1, r.C_in}, {"beta1", r.beta1, r.C_in},
573 8 {"W1", r.W1, elems({r.C_out, r.C_in, 9})}, {"b1", r.b1, r.C_out},
574 4 {"t_emb_shift", r.t_emb_shift, r.C_out},
575 8 {"gamma2", r.gamma2, r.C_out}, {"beta2", r.beta2, r.C_out},
576 8 {"W2", r.W2, elems({r.C_out, r.C_out, 9})}, {"b2", r.b2, r.C_out},
577 4 {"Wskip", r.Wskip, elems({r.C_out, r.C_in})},
578 4 {"bskip", r.bskip, r.C_out}});
579 4 return r;
580 ✗ }
581
582 6 Value attnResblockForward(Value, std::span<const Value> args) {
583 6 const char* L = "resblockForward(opts)";
584
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 if (args.empty() || !ev::isObject(arg(args, 0))) {
585
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 ev::throwTypeError(std::string(L) + " — see docs");
586 }
587 4 ev::Persistent opts(arg(args, 0));
588
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 ResblockCommon c = readResblockCommon(opts, L, "resblockForward");
589
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* Y = propReq(opts, "Y", L);
590 2 BROTENSOR_API_TRY
591
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::resblock_forward(*c.X, *c.gamma1, *c.beta1, *c.W1, c.b1, c.t_emb_shift,
592 2 *c.gamma2, *c.beta2, *c.W2, c.b2, c.Wskip, c.bskip,
593 2 c.N, c.C_in, c.C_out, c.H, c.W, c.numGroups,
594 2 static_cast<float>(c.eps), *Y);
595
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH("resblockForward")
596
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 return ev::undefined();
597 6 }
598
599 2 Value attnResblockBackward(Value, std::span<const Value> args) {
600 2 const char* L = "resblockBackward(opts)";
601
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (args.empty() || !ev::isObject(arg(args, 0))) {
602
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ ev::throwTypeError(std::string(L) + " — see docs");
603 }
604 2 ev::Persistent opts(arg(args, 0));
605
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 ResblockCommon c = readResblockCommon(opts, L, "resblockBackward");
606
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dY = propReq(opts, "dY", L);
607
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dX = propReq(opts, "dX", L);
608
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dGamma1 = propReq(opts, "dGamma1", L);
609
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dBeta1 = propReq(opts, "dBeta1", L);
610
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dW1 = propReq(opts, "dW1", L);
611
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* db1 = propOpt(opts, "db1", L);
612
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dt_emb_shift = propOpt(opts, "dt_emb_shift", L);
613
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dGamma2 = propReq(opts, "dGamma2", L);
614
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dBeta2 = propReq(opts, "dBeta2", L);
615
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dW2 = propReq(opts, "dW2", L);
616
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* db2 = propOpt(opts, "db2", L);
617
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dWskip = propOpt(opts, "dWskip", L);
618
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Tensor* dbskip = propOpt(opts, "dbskip", L);
619
620
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 needOperands("resblockBackward", c.X,
621 24 {{"dY", dY, elems({c.N, c.C_out, c.H, c.W})},
622 4 {"dGamma1", dGamma1, c.C_in}, {"dBeta1", dBeta1, c.C_in},
623 4 {"dW1", dW1, elems({c.C_out, c.C_in, 9})}, {"db1", db1, c.C_out},
624 2 {"dt_emb_shift", dt_emb_shift, c.C_out},
625 4 {"dGamma2", dGamma2, c.C_out}, {"dBeta2", dBeta2, c.C_out},
626 4 {"dW2", dW2, elems({c.C_out, c.C_out, 9})}, {"db2", db2, c.C_out},
627 4 {"dWskip", dWskip, elems({c.C_out, c.C_in})}, {"dbskip", dbskip, c.C_out}});
628 2 BROTENSOR_API_TRY
629
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 brotensor::resblock_backward(*c.X, *c.gamma1, *c.beta1, *c.W1, c.b1, c.t_emb_shift,
630 2 *c.gamma2, *c.beta2, *c.W2, c.b2, c.Wskip, c.bskip,
631 2 c.N, c.C_in, c.C_out, c.H, c.W, c.numGroups,
632 2 static_cast<float>(c.eps), *dY, *dX,
633 2 *dGamma1, *dBeta1, *dW1, db1, dt_emb_shift,
634 2 *dGamma2, *dBeta2, *dW2, db2, dWskip, dbskip);
635
1/16
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
2 BROTENSOR_API_CATCH("resblockBackward")
636
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return ev::undefined();
637 2 }
638
639 } // namespace
640
641 2 void installTensorAttention(bronze::Value ns) {
642 4 ev::bindNamespace(ns, "bro.tensor")
643
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnForward>("attentionForward")
644
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnBackward>("attentionBackward")
645
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnMhaForward>("mhaForward")
646
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnMhaBackward>("mhaBackward")
647
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnSelfForward>("selfAttentionForward")
648
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnSelfForwardTrain>("selfAttentionForwardTrain")
649
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnSelfBackward>("selfAttentionBackward")
650
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnSelfBiasForward>("selfAttentionBiasForward")
651
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnCrossForward>("crossAttentionForward")
652
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnCrossForwardWithAttn>("crossAttentionForwardWithAttn")
653
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnCrossForwardTrain>("crossAttentionForwardTrain")
654
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnCrossBackward>("crossAttentionBackward")
655
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashForward>("flashAttentionForward")
656
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashWindowedForward>("flashAttentionWindowedForward")
657
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashBackward>("flashAttentionBackward")
658
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashQkvoForward>("flashAttentionQkvoForward")
659
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 .rawFn("flashAttentionQkvoBackward", 1, &attnFlashQkvoBackward)
660
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashProjectKv>("flashAttentionProjectKv")
661
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 .fn<&attnFlashQWithKvCachedForward>("flashAttentionQWithKvCachedForward")
662
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 .rawFn("resblockForward", 1, &attnResblockForward)
663
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 .rawFn("resblockBackward", 1, &attnResblockBackward);
664 2 }
665
666 } // namespace brotensor::api
667