src/ops_attention.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // brotensor public op wrappers — attention, matmul, LSTM, RoPE, RMSNorm, SwiGLU, reductions. | ||
| 2 | |||
| 3 | #include <brotensor/ops.h> | ||
| 4 | #include <brotensor/tensor.h> | ||
| 5 | #include <brotensor/detail/dispatch.h> | ||
| 6 | |||
| 7 | #include <cstdint> | ||
| 8 | #include <stdexcept> | ||
| 9 | |||
| 10 | namespace brotensor { | ||
| 11 | |||
| 12 | // ─── FP16 linear + GEGLU ─────────────────────────────────────────────────── | ||
| 13 | |||
| 14 | 49 | void linear_forward_batched_fp16(const Tensor& W, const Tensor* bias, | |
| 15 | const Tensor& X_BD, Tensor& Y_BD) { | ||
| 16 | 49 | const auto& v = detail::dispatch_with_opts(W, X_BD, {bias, &Y_BD}); | |
| 17 | 49 | if (!v.linear_forward_batched_fp16) detail::throw_not_implemented("linear_forward_batched_fp16", W.device); | |
| 18 | 49 | detail::adopt_output(Y_BD, W.device); | |
| 19 | 49 | v.linear_forward_batched_fp16(W, bias, X_BD, Y_BD); | |
| 20 | 49 | } | |
| 21 | |||
| 22 | 29 | void linear_forward_batched_fp16_act(const Tensor& W, const Tensor* bias, | |
| 23 | const Tensor& X_BD, int act, Tensor& Y_BD) { | ||
| 24 | 29 | const auto& v = detail::dispatch_with_opts(W, X_BD, {bias, &Y_BD}); | |
| 25 | 29 | if (!v.linear_forward_batched_fp16_act) | |
| 26 | ✗ | detail::throw_not_implemented("linear_forward_batched_fp16_act", W.device); | |
| 27 | 29 | detail::adopt_output(Y_BD, W.device); | |
| 28 | 29 | v.linear_forward_batched_fp16_act(W, bias, X_BD, act, Y_BD); | |
| 29 | 29 | } | |
| 30 | |||
| 31 | 476 | void linear_forward_batched_ex(const Tensor& W, const Tensor* bias, const Tensor& X_BD, | |
| 32 | int act, int epilogue, Tensor* workspace, Tensor& Y_BD) { | ||
| 33 | 476 | const auto& v = detail::dispatch_with_opts(W, X_BD, {bias, &Y_BD, workspace}); | |
| 34 | 476 | if (!v.linear_forward_batched_ex) | |
| 35 | ✗ | detail::throw_not_implemented("linear_forward_batched_ex", W.device); | |
| 36 | 476 | detail::adopt_output(Y_BD, W.device); | |
| 37 | 476 | if (workspace) detail::adopt_output(*workspace, W.device); | |
| 38 | 476 | v.linear_forward_batched_ex(W, bias, X_BD, act, epilogue, workspace, Y_BD); | |
| 39 | 476 | } | |
| 40 | |||
| 41 | 12 | void geglu_forward(const Tensor& X, Tensor& Y) { | |
| 42 | 12 | const auto& v = detail::dispatch(X, Y); | |
| 43 | 12 | if (!v.geglu_forward) detail::throw_not_implemented("geglu_forward", X.device); | |
| 44 | 12 | detail::adopt_output(Y, X.device); | |
| 45 | 12 | v.geglu_forward(X, Y); | |
| 46 | 12 | } | |
| 47 | 9 | void geglu_backward(const Tensor& X, const Tensor& dY, Tensor& dX) { | |
| 48 | 9 | const auto& v = detail::dispatch(X, dY, dX); | |
| 49 | 9 | if (!v.geglu_backward) detail::throw_not_implemented("geglu_backward", X.device); | |
| 50 | 9 | detail::adopt_output(dX, X.device); | |
| 51 | 9 | v.geglu_backward(X, dY, dX); | |
| 52 | 9 | } | |
| 53 | 11 | void geglu_exact_forward(const Tensor& X, Tensor& Y) { | |
| 54 | 11 | const auto& v = detail::dispatch(X, Y); | |
| 55 | 11 | if (!v.geglu_exact_forward) detail::throw_not_implemented("geglu_exact_forward", X.device); | |
| 56 | 11 | detail::adopt_output(Y, X.device); | |
| 57 | 11 | v.geglu_exact_forward(X, Y); | |
| 58 | 11 | } | |
| 59 | 10 | void geglu_exact_backward(const Tensor& X, const Tensor& dY, Tensor& dX) { | |
| 60 | 10 | const auto& v = detail::dispatch(X, dY, dX); | |
| 61 | 10 | if (!v.geglu_exact_backward) detail::throw_not_implemented("geglu_exact_backward", X.device); | |
| 62 | 10 | detail::adopt_output(dX, X.device); | |
| 63 | 10 | v.geglu_exact_backward(X, dY, dX); | |
| 64 | 10 | } | |
| 65 | |||
| 66 | // ─── Causal mask helper ──────────────────────────────────────────────────── | ||
| 67 | |||
| 68 | 23 | void build_causal_mask_row(int L, int q, Tensor& mask) { | |
| 69 | 23 | const auto& v = detail::dispatch(mask); | |
| 70 | 23 | if (!v.build_causal_mask_row) detail::throw_not_implemented("build_causal_mask_row", mask.device); | |
| 71 | 23 | detail::adopt_output(mask, mask.device); | |
| 72 | 23 | v.build_causal_mask_row(L, q, mask); | |
| 73 | 23 | } | |
| 74 | |||
| 75 | // ─── Cross-attention family ──────────────────────────────────────────────── | ||
| 76 | |||
| 77 | 27 | void cross_attention_forward(const Tensor& X, const Tensor& Ctx, | |
| 78 | const Tensor& Wq, const Tensor& Wk, | ||
| 79 | const Tensor& Wv, const Tensor& Wo, | ||
| 80 | const float* d_mask, int num_heads, Tensor& O) { | ||
| 81 | 27 | const auto& v = detail::dispatch(X, Ctx, Wq, Wk, Wv, Wo, O); | |
| 82 | 27 | if (!v.cross_attention_forward) detail::throw_not_implemented("cross_attention_forward", X.device); | |
| 83 | 27 | detail::adopt_output(O, X.device); | |
| 84 | 27 | v.cross_attention_forward(X, Ctx, Wq, Wk, Wv, Wo, d_mask, num_heads, O); | |
| 85 | 27 | } | |
| 86 | |||
| 87 | 17 | void cross_attention_forward_with_attn(const Tensor& X, const Tensor& Ctx, | |
| 88 | const Tensor& Wq, const Tensor& Wk, | ||
| 89 | const Tensor& Wv, const Tensor& Wo, | ||
| 90 | const float* d_mask, | ||
| 91 | const Tensor* attn_logit_bias, | ||
| 92 | int num_heads, Tensor& O, Tensor& AttnAvg) { | ||
| 93 | 17 | const auto& v = detail::dispatch_with_opts(X, Ctx, {&Wq, &Wk, &Wv, &Wo, attn_logit_bias, &O, &AttnAvg}); | |
| 94 | 17 | if (!v.cross_attention_forward_with_attn) | |
| 95 | ✗ | detail::throw_not_implemented("cross_attention_forward_with_attn", X.device); | |
| 96 | 17 | detail::adopt_output(O, X.device); | |
| 97 | 17 | detail::adopt_output(AttnAvg, X.device); | |
| 98 | 34 | v.cross_attention_forward_with_attn(X, Ctx, Wq, Wk, Wv, Wo, d_mask, | |
| 99 | 17 | attn_logit_bias, num_heads, O, AttnAvg); | |
| 100 | 17 | } | |
| 101 | |||
| 102 | 21 | void self_attention_forward_train(const Tensor& X, | |
| 103 | const Tensor& Wq, const Tensor& Wk, | ||
| 104 | const Tensor& Wv, const Tensor& Wo, | ||
| 105 | const float* d_mask, int num_heads, | ||
| 106 | Tensor& Qh, Tensor& Kh, Tensor& Vh, | ||
| 107 | Tensor& Attnh, Tensor& Yconcat, Tensor& O) { | ||
| 108 | 21 | const auto& v = detail::dispatch_with_opts( | |
| 109 | 21 | X, Wq, {&Wk, &Wv, &Wo, &Qh, &Kh, &Vh, &Attnh, &Yconcat, &O}); | |
| 110 | 21 | if (!v.self_attention_forward_train) | |
| 111 | ✗ | detail::throw_not_implemented("self_attention_forward_train", X.device); | |
| 112 | 21 | detail::adopt_output(Qh, X.device); | |
| 113 | 21 | detail::adopt_output(Kh, X.device); | |
| 114 | 21 | detail::adopt_output(Vh, X.device); | |
| 115 | 21 | detail::adopt_output(Attnh, X.device); | |
| 116 | 21 | detail::adopt_output(Yconcat, X.device); | |
| 117 | 21 | detail::adopt_output(O, X.device); | |
| 118 | 42 | v.self_attention_forward_train(X, Wq, Wk, Wv, Wo, d_mask, num_heads, | |
| 119 | 21 | Qh, Kh, Vh, Attnh, Yconcat, O); | |
| 120 | 21 | } | |
| 121 | |||
| 122 | 19 | void self_attention_backward(const Tensor& dO, const Tensor& X, | |
| 123 | const Tensor& Qh, const Tensor& Kh, | ||
| 124 | const Tensor& Vh, const Tensor& Attnh, | ||
| 125 | const Tensor& Yconcat, | ||
| 126 | const Tensor& Wq, const Tensor& Wk, | ||
| 127 | const Tensor& Wv, const Tensor& Wo, | ||
| 128 | const float* d_mask, int num_heads, | ||
| 129 | Tensor& dX, | ||
| 130 | Tensor& dWq, Tensor& dWk, | ||
| 131 | Tensor& dWv, Tensor& dWo) { | ||
| 132 | 19 | const auto& v = detail::dispatch_with_opts( | |
| 133 | 114 | dO, X, {&Qh, &Kh, &Vh, &Attnh, &Yconcat, &Wq, &Wk, &Wv, &Wo, | |
| 134 | 95 | &dX, &dWq, &dWk, &dWv, &dWo}); | |
| 135 | 19 | if (!v.self_attention_backward) | |
| 136 | ✗ | detail::throw_not_implemented("self_attention_backward", dO.device); | |
| 137 | 19 | detail::adopt_output(dX, dO.device); | |
| 138 | 19 | detail::adopt_output(dWq, dO.device); | |
| 139 | 19 | detail::adopt_output(dWk, dO.device); | |
| 140 | 19 | detail::adopt_output(dWv, dO.device); | |
| 141 | 19 | detail::adopt_output(dWo, dO.device); | |
| 142 | 38 | v.self_attention_backward(dO, X, Qh, Kh, Vh, Attnh, Yconcat, | |
| 143 | 19 | Wq, Wk, Wv, Wo, d_mask, num_heads, | |
| 144 | 19 | dX, dWq, dWk, dWv, dWo); | |
| 145 | 19 | } | |
| 146 | |||
| 147 | 16 | void attention_token_moments(const Tensor& Attn, int h_lat, int w_lat, | |
| 148 | Tensor& mass, Tensor& centroid) { | ||
| 149 | 16 | if (h_lat <= 0 || w_lat <= 0 || Attn.rows != h_lat * w_lat) { | |
| 150 | ✗ | detail::throw_spatial_2d_error("attention_token_moments", "Attn", | |
| 151 | ✗ | h_lat * w_lat, Attn.cols, h_lat, w_lat, | |
| 152 | ✗ | Attn.rows, Attn.cols); | |
| 153 | } | ||
| 154 | 16 | const auto& v = detail::dispatch(Attn, mass, centroid); | |
| 155 | 16 | if (!v.attention_token_moments) detail::throw_not_implemented("attention_token_moments", Attn.device); | |
| 156 | 16 | detail::adopt_output(mass, Attn.device); | |
| 157 | 16 | detail::adopt_output(centroid, Attn.device); | |
| 158 | 16 | v.attention_token_moments(Attn, h_lat, w_lat, mass, centroid); | |
| 159 | 16 | } | |
| 160 | |||
| 161 | 19 | void cross_attention_forward_train(const Tensor& X, const Tensor& Ctx, | |
| 162 | const Tensor& Wq, const Tensor& Wk, | ||
| 163 | const Tensor& Wv, const Tensor& Wo, | ||
| 164 | const float* d_mask, int num_heads, | ||
| 165 | Tensor& Qh, Tensor& Kh, Tensor& Vh, | ||
| 166 | Tensor& Attnh, Tensor& Yconcat, Tensor& O) { | ||
| 167 | 19 | const auto& v = detail::dispatch_with_opts( | |
| 168 | 19 | X, Ctx, {&Wq, &Wk, &Wv, &Wo, &Qh, &Kh, &Vh, &Attnh, &Yconcat, &O}); | |
| 169 | 19 | if (!v.cross_attention_forward_train) | |
| 170 | ✗ | detail::throw_not_implemented("cross_attention_forward_train", X.device); | |
| 171 | 19 | detail::adopt_output(Qh, X.device); | |
| 172 | 19 | detail::adopt_output(Kh, X.device); | |
| 173 | 19 | detail::adopt_output(Vh, X.device); | |
| 174 | 19 | detail::adopt_output(Attnh, X.device); | |
| 175 | 19 | detail::adopt_output(Yconcat, X.device); | |
| 176 | 19 | detail::adopt_output(O, X.device); | |
| 177 | 38 | v.cross_attention_forward_train(X, Ctx, Wq, Wk, Wv, Wo, d_mask, num_heads, | |
| 178 | 19 | Qh, Kh, Vh, Attnh, Yconcat, O); | |
| 179 | 19 | } | |
| 180 | |||
| 181 | 19 | void cross_attention_backward(const Tensor& dO, const Tensor& X, const Tensor& Ctx, | |
| 182 | const Tensor& Qh, const Tensor& Kh, | ||
| 183 | const Tensor& Vh, const Tensor& Attnh, | ||
| 184 | const Tensor& Yconcat, | ||
| 185 | const Tensor& Wq, const Tensor& Wk, | ||
| 186 | const Tensor& Wv, const Tensor& Wo, | ||
| 187 | const float* d_mask, int num_heads, | ||
| 188 | Tensor& dX, Tensor& dCtx, | ||
| 189 | Tensor& dWq, Tensor& dWk, | ||
| 190 | Tensor& dWv, Tensor& dWo) { | ||
| 191 | 19 | const auto& v = detail::dispatch_with_opts( | |
| 192 | 133 | dO, X, {&Ctx, &Qh, &Kh, &Vh, &Attnh, &Yconcat, &Wq, &Wk, &Wv, &Wo, | |
| 193 | 114 | &dX, &dCtx, &dWq, &dWk, &dWv, &dWo}); | |
| 194 | 19 | if (!v.cross_attention_backward) | |
| 195 | ✗ | detail::throw_not_implemented("cross_attention_backward", dO.device); | |
| 196 | 19 | detail::adopt_output(dX, dO.device); | |
| 197 | 19 | detail::adopt_output(dCtx, dO.device); | |
| 198 | 19 | detail::adopt_output(dWq, dO.device); | |
| 199 | 19 | detail::adopt_output(dWk, dO.device); | |
| 200 | 19 | detail::adopt_output(dWv, dO.device); | |
| 201 | 19 | detail::adopt_output(dWo, dO.device); | |
| 202 | 38 | v.cross_attention_backward(dO, X, Ctx, Qh, Kh, Vh, Attnh, Yconcat, | |
| 203 | 19 | Wq, Wk, Wv, Wo, d_mask, num_heads, | |
| 204 | 19 | dX, dCtx, dWq, dWk, dWv, dWo); | |
| 205 | 19 | } | |
| 206 | |||
| 207 | // ─── FP16 LN inference + FP16 self-attention ─────────────────────────────── | ||
| 208 | |||
| 209 | 3 | void layernorm_forward_inference_batched_fp16(const Tensor& X_RD, | |
| 210 | const Tensor& gamma, | ||
| 211 | const Tensor& beta, | ||
| 212 | Tensor& Y_RD, float eps) { | ||
| 213 | 3 | const auto& v = detail::dispatch(X_RD, gamma, beta, Y_RD); | |
| 214 | 3 | if (!v.layernorm_forward_inference_batched_fp16) | |
| 215 | ✗ | detail::throw_not_implemented("layernorm_forward_inference_batched_fp16", X_RD.device); | |
| 216 | 3 | detail::adopt_output(Y_RD, X_RD.device); | |
| 217 | 3 | v.layernorm_forward_inference_batched_fp16(X_RD, gamma, &beta, Y_RD, eps); | |
| 218 | 3 | } | |
| 219 | |||
| 220 | 2 | void layernorm_forward_inference_batched_fp16(const Tensor& X_RD, | |
| 221 | const Tensor& gamma, | ||
| 222 | Tensor& Y_RD, float eps) { | ||
| 223 | 2 | const auto& v = detail::dispatch(X_RD, gamma, Y_RD); | |
| 224 | 2 | if (!v.layernorm_forward_inference_batched_fp16) | |
| 225 | ✗ | detail::throw_not_implemented("layernorm_forward_inference_batched_fp16", X_RD.device); | |
| 226 | 2 | detail::adopt_output(Y_RD, X_RD.device); | |
| 227 | 2 | v.layernorm_forward_inference_batched_fp16(X_RD, gamma, nullptr, Y_RD, eps); | |
| 228 | 2 | } | |
| 229 | |||
| 230 | 14 | void self_attention_forward(const Tensor& X, | |
| 231 | const Tensor& Wq, const Tensor& Wk, | ||
| 232 | const Tensor& Wv, const Tensor& Wo, | ||
| 233 | const float* d_mask, int num_heads, Tensor& O) { | ||
| 234 | 14 | const auto& v = detail::dispatch(X, Wq, Wk, Wv, Wo, O); | |
| 235 | 14 | if (!v.self_attention_forward) detail::throw_not_implemented("self_attention_forward", X.device); | |
| 236 | 14 | detail::adopt_output(O, X.device); | |
| 237 | 14 | v.self_attention_forward(X, Wq, Wk, Wv, Wo, d_mask, num_heads, O); | |
| 238 | 14 | } | |
| 239 | |||
| 240 | 51 | void self_attention_bias_forward(const Tensor& X, | |
| 241 | const Tensor& Wq, const Tensor& Wk, | ||
| 242 | const Tensor& Wv, const Tensor& Wo, | ||
| 243 | const Tensor* bq, const Tensor* bk, | ||
| 244 | const Tensor* bv, const Tensor* bo, | ||
| 245 | const float* d_mask, | ||
| 246 | const Tensor* attn_bias, | ||
| 247 | int num_heads, float scale, Tensor& O) { | ||
| 248 | 51 | const auto& v = detail::dispatch_with_opts(X, Wq, {&Wk, &Wv, &Wo, bq, bk, bv, bo, attn_bias, &O}); | |
| 249 | 51 | if (!v.self_attention_bias_forward) | |
| 250 | ✗ | detail::throw_not_implemented("self_attention_bias_forward", X.device); | |
| 251 | 51 | detail::adopt_output(O, X.device); | |
| 252 | 102 | v.self_attention_bias_forward(X, Wq, Wk, Wv, Wo, bq, bk, bv, bo, d_mask, attn_bias, | |
| 253 | 51 | num_heads, scale, O); | |
| 254 | 51 | } | |
| 255 | |||
| 256 | 8 | void rel_pos_bias_xl_forward(const Tensor& Qv, const Tensor& Pk, | |
| 257 | int num_heads, int head_dim, Tensor& Bias) { | ||
| 258 | 8 | const auto& v = detail::dispatch_with_opts(Qv, Pk, {&Bias}); | |
| 259 | 8 | if (!v.rel_pos_bias_xl_forward) | |
| 260 | ✗ | detail::throw_not_implemented("rel_pos_bias_xl_forward", Qv.device); | |
| 261 | 8 | detail::adopt_output(Bias, Qv.device); | |
| 262 | 8 | v.rel_pos_bias_xl_forward(Qv, Pk, num_heads, head_dim, Bias); | |
| 263 | 8 | } | |
| 264 | |||
| 265 | 27 | void self_attention_decomposed_rel_pos_forward( | |
| 266 | const Tensor& X, | ||
| 267 | const Tensor& Wq, const Tensor* bq, | ||
| 268 | const Tensor& Wk, const Tensor* bk, | ||
| 269 | const Tensor& Wv, const Tensor* bv, | ||
| 270 | const Tensor& Wo, const Tensor* bo, | ||
| 271 | const Tensor& rel_pos_h, const Tensor& rel_pos_w, | ||
| 272 | int num_heads, int grid_h, int grid_w, float scale, Tensor& O) { | ||
| 273 | 27 | if (grid_h <= 0 || grid_w <= 0 || X.rows != grid_h * grid_w) { | |
| 274 | ✗ | detail::throw_spatial_2d_error("self_attention_decomposed_rel_pos_forward", "X", | |
| 275 | ✗ | grid_h * grid_w, X.cols, grid_h, grid_w, | |
| 276 | ✗ | X.rows, X.cols); | |
| 277 | } | ||
| 278 | 27 | const auto& v = detail::dispatch_with_opts( | |
| 279 | 27 | X, Wq, {bq, &Wk, bk, &Wv, bv, &Wo, bo, &rel_pos_h, &rel_pos_w, &O}); | |
| 280 | 27 | if (!v.self_attention_decomposed_rel_pos_forward) | |
| 281 | ✗ | detail::throw_not_implemented("self_attention_decomposed_rel_pos_forward", | |
| 282 | ✗ | X.device); | |
| 283 | 27 | detail::adopt_output(O, X.device); | |
| 284 | 54 | v.self_attention_decomposed_rel_pos_forward( | |
| 285 | 27 | X, Wq, bq, Wk, bk, Wv, bv, Wo, bo, rel_pos_h, rel_pos_w, | |
| 286 | 27 | num_heads, grid_h, grid_w, scale, O); | |
| 287 | 27 | } | |
| 288 | |||
| 289 | 19 | void self_attention_decomposed_rel_pos_windowed_forward( | |
| 290 | const Tensor& X, | ||
| 291 | const Tensor& Wq, const Tensor* bq, | ||
| 292 | const Tensor& Wk, const Tensor* bk, | ||
| 293 | const Tensor& Wv, const Tensor* bv, | ||
| 294 | const Tensor& Wo, const Tensor* bo, | ||
| 295 | const Tensor& rel_pos_h, const Tensor& rel_pos_w, | ||
| 296 | int num_heads, int grid_h, int grid_w, int window, float scale, | ||
| 297 | Tensor& O) { | ||
| 298 | 19 | if (grid_h <= 0 || grid_w <= 0 || X.rows != grid_h * grid_w) { | |
| 299 | ✗ | detail::throw_spatial_2d_error( | |
| 300 | "self_attention_decomposed_rel_pos_windowed_forward", "X", | ||
| 301 | ✗ | grid_h * grid_w, X.cols, grid_h, grid_w, X.rows, X.cols); | |
| 302 | } | ||
| 303 | 19 | const auto& v = detail::dispatch_with_opts( | |
| 304 | 19 | X, Wq, {bq, &Wk, bk, &Wv, bv, &Wo, bo, &rel_pos_h, &rel_pos_w, &O}); | |
| 305 | 19 | if (!v.self_attention_decomposed_rel_pos_windowed_forward) | |
| 306 | ✗ | detail::throw_not_implemented( | |
| 307 | ✗ | "self_attention_decomposed_rel_pos_windowed_forward", X.device); | |
| 308 | 19 | detail::adopt_output(O, X.device); | |
| 309 | 38 | v.self_attention_decomposed_rel_pos_windowed_forward( | |
| 310 | 19 | X, Wq, bq, Wk, bk, Wv, bv, Wo, bo, rel_pos_h, rel_pos_w, | |
| 311 | 19 | num_heads, grid_h, grid_w, window, scale, O); | |
| 312 | 19 | } | |
| 313 | |||
| 314 | 9 | void self_attention_bias_int8w_fp16(const Tensor& X, | |
| 315 | const Tensor& Wq_int8, const Tensor& sq, | ||
| 316 | const Tensor& Wk_int8, const Tensor& sk, | ||
| 317 | const Tensor& Wv_int8, const Tensor& sv, | ||
| 318 | const Tensor& Wo_int8, const Tensor& so, | ||
| 319 | const float* d_mask, | ||
| 320 | const Tensor* attn_bias, | ||
| 321 | int num_heads, float scale, Tensor& O) { | ||
| 322 | 9 | const auto& v = detail::dispatch_with_opts( | |
| 323 | 27 | X, Wq_int8, {&sq, &Wk_int8, &sk, &Wv_int8, &sv, &Wo_int8, &so, | |
| 324 | 18 | attn_bias, &O}); | |
| 325 | 9 | if (!v.self_attention_bias_int8w_fp16) | |
| 326 | ✗ | detail::throw_not_implemented("self_attention_bias_int8w_fp16", X.device); | |
| 327 | 9 | detail::adopt_output(O, X.device); | |
| 328 | 18 | v.self_attention_bias_int8w_fp16(X, Wq_int8, sq, Wk_int8, sk, | |
| 329 | 9 | Wv_int8, sv, Wo_int8, so, | |
| 330 | 9 | d_mask, attn_bias, num_heads, scale, O); | |
| 331 | 9 | } | |
| 332 | |||
| 333 | // ─── Flash attention family ──────────────────────────────────────────────── | ||
| 334 | |||
| 335 | 165 | void flash_attention_forward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 336 | const float* d_mask, int num_heads, bool causal, | ||
| 337 | Tensor& O) { | ||
| 338 | 165 | const auto& v = detail::dispatch(Q, K, V, O); | |
| 339 | 165 | if (!v.flash_attention_forward) detail::throw_not_implemented("flash_attention_forward", Q.device); | |
| 340 | 165 | detail::adopt_output(O, Q.device); | |
| 341 | 165 | v.flash_attention_forward(Q, K, V, d_mask, num_heads, causal, O); | |
| 342 | 165 | } | |
| 343 | |||
| 344 | 30 | void flash_attention_gqa_forward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 345 | const float* d_mask, int num_q_heads, int num_kv_heads, | ||
| 346 | bool causal, Tensor& O) { | ||
| 347 | 30 | const auto& v = detail::dispatch(Q, K, V, O); | |
| 348 | 30 | if (!v.flash_attention_gqa_forward) | |
| 349 | ✗ | detail::throw_not_implemented("flash_attention_gqa_forward", Q.device); | |
| 350 | 30 | detail::adopt_output(O, Q.device); | |
| 351 | 30 | v.flash_attention_gqa_forward(Q, K, V, d_mask, num_q_heads, num_kv_heads, causal, O); | |
| 352 | 30 | } | |
| 353 | |||
| 354 | 51 | void flash_attention_windowed_forward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 355 | const float* d_mask, int num_heads, int window, | ||
| 356 | Tensor& O, bool causal) { | ||
| 357 | 51 | const auto& v = detail::dispatch(Q, K, V, O); | |
| 358 | 51 | if (!v.flash_attention_windowed_forward) | |
| 359 | ✗ | detail::throw_not_implemented("flash_attention_windowed_forward", Q.device); | |
| 360 | 51 | detail::adopt_output(O, Q.device); | |
| 361 | 51 | v.flash_attention_windowed_forward(Q, K, V, d_mask, num_heads, window, O, causal); | |
| 362 | 51 | } | |
| 363 | |||
| 364 | 69 | void flash_attention_varlen_forward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 365 | const int32_t* cu_seqlens_q, | ||
| 366 | const int32_t* cu_seqlens_k, | ||
| 367 | int batch_size, int max_seqlen_q, int max_seqlen_k, | ||
| 368 | int num_heads, int head_dim, bool causal, | ||
| 369 | Tensor& O) { | ||
| 370 | 69 | const auto& v = detail::dispatch(Q, K, V, O); | |
| 371 | 69 | if (!v.flash_attention_varlen_forward) | |
| 372 | ✗ | detail::throw_not_implemented("flash_attention_varlen_forward", Q.device); | |
| 373 | 69 | detail::adopt_output(O, Q.device); | |
| 374 | 138 | v.flash_attention_varlen_forward(Q, K, V, cu_seqlens_q, cu_seqlens_k, | |
| 375 | 69 | batch_size, max_seqlen_q, max_seqlen_k, | |
| 376 | 69 | num_heads, head_dim, causal, O); | |
| 377 | 69 | } | |
| 378 | |||
| 379 | 60 | void flash_attention_varlen_backward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 380 | const Tensor& O, const Tensor& dO, | ||
| 381 | const int32_t* cu_seqlens_q, | ||
| 382 | const int32_t* cu_seqlens_k, | ||
| 383 | int batch_size, int max_seqlen_q, int max_seqlen_k, | ||
| 384 | int num_heads, int head_dim, bool causal, | ||
| 385 | Tensor& dQ, Tensor& dK, Tensor& dV) { | ||
| 386 | 60 | const auto& v = detail::dispatch(Q, K, V, O, dO, dQ, dK, dV); | |
| 387 | 60 | if (!v.flash_attention_varlen_backward) | |
| 388 | ✗ | detail::throw_not_implemented("flash_attention_varlen_backward", Q.device); | |
| 389 | 60 | detail::adopt_output(dQ, Q.device); | |
| 390 | 60 | detail::adopt_output(dK, Q.device); | |
| 391 | 60 | detail::adopt_output(dV, Q.device); | |
| 392 | 120 | v.flash_attention_varlen_backward(Q, K, V, O, dO, | |
| 393 | 60 | cu_seqlens_q, cu_seqlens_k, | |
| 394 | 60 | batch_size, max_seqlen_q, max_seqlen_k, | |
| 395 | 60 | num_heads, head_dim, causal, | |
| 396 | 60 | dQ, dK, dV); | |
| 397 | 60 | } | |
| 398 | |||
| 399 | 39 | void flash_attention_qkvo_forward(const Tensor& X, const Tensor* Ctx, | |
| 400 | const Tensor& Wq, const Tensor* bq, | ||
| 401 | const Tensor& Wk, const Tensor* bk, | ||
| 402 | const Tensor& Wv, const Tensor* bv, | ||
| 403 | const Tensor& Wo, const Tensor* bo, | ||
| 404 | const float* d_mask, int num_heads, | ||
| 405 | bool causal, Tensor& O) { | ||
| 406 | 39 | const auto& v = detail::dispatch_with_opts( | |
| 407 | 39 | X, Wq, {Ctx, bq, &Wk, bk, &Wv, bv, &Wo, bo, &O}); | |
| 408 | 39 | if (!v.flash_attention_qkvo_forward) | |
| 409 | ✗ | detail::throw_not_implemented("flash_attention_qkvo_forward", X.device); | |
| 410 | 39 | detail::adopt_output(O, X.device); | |
| 411 | 78 | v.flash_attention_qkvo_forward(X, Ctx, Wq, bq, Wk, bk, Wv, bv, Wo, bo, | |
| 412 | 39 | d_mask, num_heads, causal, O); | |
| 413 | 39 | } | |
| 414 | |||
| 415 | 39 | void flash_attention_qkvo_backward( | |
| 416 | const Tensor& X, const Tensor* Ctx, | ||
| 417 | const Tensor& Wq, const Tensor* bq, | ||
| 418 | const Tensor& Wk, const Tensor* bk, | ||
| 419 | const Tensor& Wv, const Tensor* bv, | ||
| 420 | const Tensor& Wo, const Tensor* bo, | ||
| 421 | const float* d_mask, int num_heads, bool causal, | ||
| 422 | const Tensor& dO, | ||
| 423 | Tensor& dX, Tensor* dCtx, | ||
| 424 | Tensor& dWq, Tensor* dbq, | ||
| 425 | Tensor& dWk, Tensor* dbk, | ||
| 426 | Tensor& dWv, Tensor* dbv, | ||
| 427 | Tensor& dWo, Tensor* dbo) { | ||
| 428 | 39 | const auto& v = detail::dispatch_with_opts( | |
| 429 | 429 | X, Wq, {Ctx, bq, &Wk, bk, &Wv, bv, &Wo, bo, &dO, | |
| 430 | 390 | &dX, dCtx, &dWq, dbq, &dWk, dbk, &dWv, dbv, &dWo, dbo}); | |
| 431 | 39 | if (!v.flash_attention_qkvo_backward) | |
| 432 | ✗ | detail::throw_not_implemented("flash_attention_qkvo_backward", X.device); | |
| 433 | 39 | detail::adopt_output(dX, X.device); | |
| 434 | 39 | if (dCtx) detail::adopt_output(*dCtx, X.device); | |
| 435 | 39 | detail::adopt_output(dWq, X.device); | |
| 436 | 39 | if (dbq) detail::adopt_output(*dbq, X.device); | |
| 437 | 39 | detail::adopt_output(dWk, X.device); | |
| 438 | 39 | if (dbk) detail::adopt_output(*dbk, X.device); | |
| 439 | 39 | detail::adopt_output(dWv, X.device); | |
| 440 | 39 | if (dbv) detail::adopt_output(*dbv, X.device); | |
| 441 | 39 | detail::adopt_output(dWo, X.device); | |
| 442 | 39 | if (dbo) detail::adopt_output(*dbo, X.device); | |
| 443 | 78 | v.flash_attention_qkvo_backward(X, Ctx, Wq, bq, Wk, bk, Wv, bv, Wo, bo, | |
| 444 | 39 | d_mask, num_heads, causal, dO, | |
| 445 | 39 | dX, dCtx, dWq, dbq, dWk, dbk, | |
| 446 | 39 | dWv, dbv, dWo, dbo); | |
| 447 | 39 | } | |
| 448 | |||
| 449 | 75 | void flash_attention_backward(const Tensor& Q, const Tensor& K, const Tensor& V, | |
| 450 | const Tensor& O, const Tensor& dO, | ||
| 451 | const float* d_mask, int num_heads, bool causal, | ||
| 452 | Tensor& dQ, Tensor& dK, Tensor& dV) { | ||
| 453 | 75 | const auto& v = detail::dispatch(Q, K, V, O, dO, dQ, dK, dV); | |
| 454 | 75 | if (!v.flash_attention_backward) detail::throw_not_implemented("flash_attention_backward", Q.device); | |
| 455 | 75 | detail::adopt_output(dQ, Q.device); | |
| 456 | 75 | detail::adopt_output(dK, Q.device); | |
| 457 | 75 | detail::adopt_output(dV, Q.device); | |
| 458 | 150 | v.flash_attention_backward(Q, K, V, O, dO, d_mask, num_heads, causal, | |
| 459 | 75 | dQ, dK, dV); | |
| 460 | 75 | } | |
| 461 | |||
| 462 | 11 | void flash_attention_project_kv(const Tensor& ctx, | |
| 463 | const Tensor& Wk, const Tensor* bk, | ||
| 464 | const Tensor& Wv, const Tensor* bv, | ||
| 465 | Tensor& K_out, Tensor& V_out) { | ||
| 466 | 11 | const auto& v = detail::dispatch_with_opts(ctx, Wk, {bk, &Wv, bv, &K_out, &V_out}); | |
| 467 | 11 | if (!v.flash_attention_project_kv) | |
| 468 | ✗ | detail::throw_not_implemented("flash_attention_project_kv", ctx.device); | |
| 469 | 11 | detail::adopt_output(K_out, ctx.device); | |
| 470 | 11 | detail::adopt_output(V_out, ctx.device); | |
| 471 | 11 | v.flash_attention_project_kv(ctx, Wk, bk, Wv, bv, K_out, V_out); | |
| 472 | 11 | } | |
| 473 | |||
| 474 | 15 | void flash_attention_q_with_kv_cached_forward(const Tensor& X, | |
| 475 | const Tensor& K, const Tensor& V, | ||
| 476 | const Tensor& Wq, const Tensor* bq, | ||
| 477 | const Tensor& Wo, const Tensor* bo, | ||
| 478 | const float* d_mask, int num_heads, | ||
| 479 | bool causal, Tensor& O) { | ||
| 480 | 15 | const auto& v = detail::dispatch_with_opts(X, K, {&V, &Wq, bq, &Wo, bo, &O}); | |
| 481 | 15 | if (!v.flash_attention_q_with_kv_cached_forward) | |
| 482 | ✗ | detail::throw_not_implemented("flash_attention_q_with_kv_cached_forward", X.device); | |
| 483 | 15 | detail::adopt_output(O, X.device); | |
| 484 | 30 | v.flash_attention_q_with_kv_cached_forward(X, K, V, Wq, bq, Wo, bo, | |
| 485 | 15 | d_mask, num_heads, causal, O); | |
| 486 | 15 | } | |
| 487 | |||
| 488 | // ─── Matmul + RoPE + RMSNorm + SwiGLU + KV-cache + Llama ─────────────────── | ||
| 489 | |||
| 490 | 131 | void matmul(const Tensor& A, const Tensor& B, Tensor& C) { | |
| 491 | 131 | const auto& v = detail::dispatch(A, B, C); | |
| 492 | 131 | if (!v.matmul) detail::throw_not_implemented("matmul", A.device); | |
| 493 | 131 | detail::adopt_output(C, A.device); | |
| 494 | 131 | v.matmul(A, B, C); | |
| 495 | 131 | } | |
| 496 | |||
| 497 | 84 | void matmul_abt(const Tensor& A, const Tensor& B, Tensor& C, | |
| 498 | int batch, int M, int N, int K, | ||
| 499 | long long strideA, long long strideB, long long strideC, | ||
| 500 | const Tensor* bias, int act) { | ||
| 501 | 84 | const auto& v = detail::dispatch_with_opts(A, B, {bias, &C}); | |
| 502 | 84 | if (!v.matmul_abt) detail::throw_not_implemented("matmul_abt", A.device); | |
| 503 | 84 | detail::adopt_output(C, A.device); | |
| 504 | 84 | v.matmul_abt(A, B, C, batch, M, N, K, strideA, strideB, strideC, bias, act); | |
| 505 | 84 | } | |
| 506 | |||
| 507 | 66 | void matmul_backward(const Tensor& A, const Tensor& B, const Tensor& dC, | |
| 508 | Tensor& dA, Tensor& dB) { | ||
| 509 | 66 | const auto& v = detail::dispatch(A, B, dC, dA, dB); | |
| 510 | 66 | if (!v.matmul_backward) detail::throw_not_implemented("matmul_backward", A.device); | |
| 511 | 66 | detail::adopt_output(dA, A.device); | |
| 512 | 66 | detail::adopt_output(dB, A.device); | |
| 513 | 66 | v.matmul_backward(A, B, dC, dA, dB); | |
| 514 | 66 | } | |
| 515 | |||
| 516 | namespace { | ||
| 517 | |||
| 518 | // gates (T, 4H) = X W_ih^T + b_ih + b_hh. | ||
| 519 | 18 | Tensor lstm_input_gates(const char* op, const Tensor& X, const Tensor& W_ih, const Tensor& W_hh, | |
| 520 | const Tensor* b_ih, const Tensor* b_hh) { | ||
| 521 | 18 | const int G = W_hh.rows; | |
| 522 | 18 | if (W_ih.rows != G || W_hh.cols * 4 != G || W_ih.cols != X.cols) { | |
| 523 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + | |
| 524 | ": W_ih must be (4H, I) and W_hh (4H, H) for X (T, I)"); | ||
| 525 | } | ||
| 526 | 54 | for (const Tensor* b : {b_ih, b_hh}) { | |
| 527 | 36 | if (b && (b->rows != G || b->cols != 1 || b->dtype != X.dtype)) { | |
| 528 | ✗ | throw std::runtime_error(std::string("brotensor: ") + op + ": biases must be (4H, 1) in X's dtype"); | |
| 529 | } | ||
| 530 | } | ||
| 531 | 18 | Tensor bias = Tensor::zeros_on(X.device, G, 1, X.dtype); | |
| 532 | 18 | if (b_ih) add_inplace(bias, *b_ih); | |
| 533 | 18 | if (b_hh) add_inplace(bias, *b_hh); | |
| 534 | 18 | Tensor gates = Tensor::empty_on(X.device, X.rows, G, X.dtype); | |
| 535 | 18 | linear_forward_batched(W_ih, bias, X, gates); | |
| 536 | 18 | return gates; | |
| 537 | 18 | } | |
| 538 | |||
| 539 | } // namespace | ||
| 540 | |||
| 541 | 10 | void lstm_forward(const Tensor& X, const Tensor& W_ih, const Tensor& W_hh, | |
| 542 | const Tensor* b_ih, const Tensor* b_hh, | ||
| 543 | const Tensor* W_ih_r, const Tensor* W_hh_r, | ||
| 544 | const Tensor* b_ih_r, const Tensor* b_hh_r, | ||
| 545 | Tensor& Y) { | ||
| 546 | 10 | constexpr const char* op = "lstm_forward"; | |
| 547 | 10 | if ((W_ih_r == nullptr) != (W_hh_r == nullptr)) { | |
| 548 | ✗ | throw std::runtime_error("brotensor: lstm_forward: W_ih_r and W_hh_r go together"); | |
| 549 | } | ||
| 550 | 10 | if (W_hh_r && (W_hh_r->rows != W_hh.rows || W_hh_r->cols != W_hh.cols)) { | |
| 551 | ✗ | throw std::runtime_error("brotensor: lstm_forward: the directions' hidden sizes differ"); | |
| 552 | } | ||
| 553 | 10 | if (X.rows <= 0) throw std::runtime_error("brotensor: lstm_forward: X has no rows"); | |
| 554 | 10 | const Tensor gf = lstm_input_gates(op, X, W_ih, W_hh, b_ih, b_hh); | |
| 555 | 10 | if (!W_hh_r) { | |
| 556 | 2 | lstm_recurrence(gf, W_hh, nullptr, nullptr, Y); | |
| 557 | 2 | return; | |
| 558 | } | ||
| 559 | 8 | const Tensor gr = lstm_input_gates(op, X, *W_ih_r, *W_hh_r, b_ih_r, b_hh_r); | |
| 560 | 8 | lstm_recurrence(gf, W_hh, &gr, W_hh_r, Y); | |
| 561 | 10 | } | |
| 562 | |||
| 563 | 10 | void lstm_recurrence(const Tensor& gates_f, const Tensor& W_hh_f, | |
| 564 | const Tensor* gates_r, const Tensor* W_hh_r, Tensor& Y) { | ||
| 565 | 10 | if ((gates_r == nullptr) != (W_hh_r == nullptr)) { | |
| 566 | ✗ | throw std::runtime_error("brotensor: lstm_recurrence: gates_r and W_hh_r go together"); | |
| 567 | } | ||
| 568 | 10 | const int H = W_hh_f.cols; | |
| 569 | 10 | const int T = gates_f.rows; | |
| 570 | 18 | if (W_hh_f.rows != 4 * H || gates_f.cols != 4 * H || W_hh_f.dtype != gates_f.dtype || | |
| 571 | 10 | (gates_r && (gates_r->rows != T || gates_r->cols != 4 * H || gates_r->dtype != gates_f.dtype || | |
| 572 | 8 | W_hh_r->rows != 4 * H || W_hh_r->cols != H || W_hh_r->dtype != gates_f.dtype))) { | |
| 573 | ✗ | throw std::runtime_error("brotensor: lstm_recurrence: gates must be (T, 4H) and W_hh (4H, H), one dtype"); | |
| 574 | } | ||
| 575 | 10 | const auto& v = detail::dispatch_with_opts(gates_f, W_hh_f, {gates_r, W_hh_r, &Y}); | |
| 576 | 10 | if (!v.lstm_recurrence) detail::throw_not_implemented("lstm_recurrence", gates_f.device); | |
| 577 | 10 | detail::adopt_output(Y, gates_f.device); | |
| 578 | 10 | const int cols = gates_r ? 2 * H : H; | |
| 579 | 10 | if (Y.rows != T || Y.cols != cols || Y.dtype != gates_f.dtype) Y.resize(T, cols, gates_f.dtype); | |
| 580 | 10 | v.lstm_recurrence(gates_f, W_hh_f, gates_r, W_hh_r, Y); | |
| 581 | 10 | } | |
| 582 | |||
| 583 | 918 | void lstm_forward_train(const Tensor& X, const Tensor& W_ih, const Tensor& W_hh, | |
| 584 | const Tensor* b_ih, const Tensor* b_hh, | ||
| 585 | const Tensor* h0, const Tensor* c0, int T, int B, | ||
| 586 | Tensor& Y, Tensor& gates, Tensor& C, | ||
| 587 | Tensor* hT, Tensor* cT) { | ||
| 588 | 918 | const auto& v = detail::dispatch_with_opts( | |
| 589 | 918 | X, W_ih, {&W_hh, b_ih, b_hh, h0, c0, &Y, &gates, &C, hT, cT}); | |
| 590 | 918 | if (!v.lstm_forward_train) detail::throw_not_implemented("lstm_forward_train", X.device); | |
| 591 | 918 | detail::adopt_output(Y, X.device); | |
| 592 | 918 | detail::adopt_output(gates, X.device); | |
| 593 | 918 | detail::adopt_output(C, X.device); | |
| 594 | 918 | if (hT) detail::adopt_output(*hT, X.device); | |
| 595 | 918 | if (cT) detail::adopt_output(*cT, X.device); | |
| 596 | 918 | v.lstm_forward_train(X, W_ih, W_hh, b_ih, b_hh, h0, c0, T, B, Y, gates, C, hT, cT); | |
| 597 | 918 | } | |
| 598 | |||
| 599 | 415 | void lstm_backward(const Tensor& X, const Tensor& W_ih, const Tensor& W_hh, | |
| 600 | const Tensor* h0, const Tensor* c0, | ||
| 601 | const Tensor& Y, const Tensor& gates, const Tensor& C, | ||
| 602 | const Tensor& dY, int T, int B, | ||
| 603 | Tensor& dX, Tensor& dW_ih, Tensor& dW_hh, | ||
| 604 | Tensor* db_ih, Tensor* db_hh, | ||
| 605 | Tensor* dh0, Tensor* dc0) { | ||
| 606 | 415 | const auto& v = detail::dispatch_with_opts( | |
| 607 | 3320 | X, W_ih, {&W_hh, h0, c0, &Y, &gates, &C, &dY, | |
| 608 | 2905 | &dX, &dW_ih, &dW_hh, db_ih, db_hh, dh0, dc0}); | |
| 609 | 415 | if (!v.lstm_backward) detail::throw_not_implemented("lstm_backward", X.device); | |
| 610 | 415 | detail::adopt_output(dX, X.device); | |
| 611 | 415 | detail::adopt_output(dW_ih, X.device); | |
| 612 | 415 | detail::adopt_output(dW_hh, X.device); | |
| 613 | 415 | if (db_ih) detail::adopt_output(*db_ih, X.device); | |
| 614 | 415 | if (db_hh) detail::adopt_output(*db_hh, X.device); | |
| 615 | 415 | if (dh0) detail::adopt_output(*dh0, X.device); | |
| 616 | 415 | if (dc0) detail::adopt_output(*dc0, X.device); | |
| 617 | 830 | v.lstm_backward(X, W_ih, W_hh, h0, c0, Y, gates, C, dY, T, B, | |
| 618 | 415 | dX, dW_ih, dW_hh, db_ih, db_hh, dh0, dc0); | |
| 619 | 415 | } | |
| 620 | |||
| 621 | 28 | void rope_forward(const Tensor& X, int head_dim, int num_heads, | |
| 622 | int seq_offset, float theta_base, Tensor& Y) { | ||
| 623 | 28 | const auto& v = detail::dispatch(X, Y); | |
| 624 | 28 | if (!v.rope_forward) detail::throw_not_implemented("rope_forward", X.device); | |
| 625 | 28 | detail::adopt_output(Y, X.device); | |
| 626 | 28 | v.rope_forward(X, head_dim, num_heads, seq_offset, theta_base, Y); | |
| 627 | 28 | } | |
| 628 | |||
| 629 | 34 | void rope_backward(const Tensor& dY, int head_dim, int num_heads, | |
| 630 | int seq_offset, float theta_base, Tensor& dX) { | ||
| 631 | 34 | const auto& v = detail::dispatch(dY, dX); | |
| 632 | 34 | if (!v.rope_backward) detail::throw_not_implemented("rope_backward", dY.device); | |
| 633 | 34 | detail::adopt_output(dX, dY.device); | |
| 634 | 34 | v.rope_backward(dY, head_dim, num_heads, seq_offset, theta_base, dX); | |
| 635 | 34 | } | |
| 636 | |||
| 637 | 38 | void rope_apply(const Tensor& X, const Tensor& cos_tbl, const Tensor& sin_tbl, | |
| 638 | int head_dim, int num_heads, Tensor& Y) { | ||
| 639 | 38 | const auto& v = detail::dispatch(X, cos_tbl, sin_tbl, Y); | |
| 640 | 38 | if (!v.rope_apply) detail::throw_not_implemented("rope_apply", X.device); | |
| 641 | 38 | detail::adopt_output(Y, X.device); | |
| 642 | 38 | v.rope_apply(X, cos_tbl, sin_tbl, head_dim, num_heads, Y); | |
| 643 | 38 | } | |
| 644 | |||
| 645 | 6 | void rope_apply_perhead(const Tensor& X, const Tensor& cos_tbl, | |
| 646 | const Tensor& sin_tbl, int head_dim, int num_heads, | ||
| 647 | Tensor& Y) { | ||
| 648 | 6 | const auto& v = detail::dispatch(X, cos_tbl, sin_tbl, Y); | |
| 649 | 6 | if (!v.rope_apply_perhead) | |
| 650 | ✗ | detail::throw_not_implemented("rope_apply_perhead", X.device); | |
| 651 | 6 | detail::adopt_output(Y, X.device); | |
| 652 | 6 | v.rope_apply_perhead(X, cos_tbl, sin_tbl, head_dim, num_heads, Y); | |
| 653 | 6 | } | |
| 654 | |||
| 655 | 4 | void rope_apply_backward(const Tensor& dY, const Tensor& cos_tbl, | |
| 656 | const Tensor& sin_tbl, int head_dim, int num_heads, | ||
| 657 | Tensor& dX) { | ||
| 658 | 4 | const auto& v = detail::dispatch(dY, cos_tbl, sin_tbl, dX); | |
| 659 | 4 | if (!v.rope_apply_backward) | |
| 660 | ✗ | detail::throw_not_implemented("rope_apply_backward", dY.device); | |
| 661 | 4 | detail::adopt_output(dX, dY.device); | |
| 662 | 4 | v.rope_apply_backward(dY, cos_tbl, sin_tbl, head_dim, num_heads, dX); | |
| 663 | 4 | } | |
| 664 | |||
| 665 | 15 | void rope_apply_mrope(const Tensor& X, | |
| 666 | const Tensor& cos_t, const Tensor& sin_t, | ||
| 667 | const Tensor& cos_h, const Tensor& sin_h, | ||
| 668 | const Tensor& cos_w, const Tensor& sin_w, | ||
| 669 | const int32_t* pos_t, const int32_t* pos_h, | ||
| 670 | const int32_t* pos_w, | ||
| 671 | int head_dim, int num_heads, | ||
| 672 | int d_t, int d_h, int d_w, | ||
| 673 | Tensor& Y) { | ||
| 674 | 30 | const auto& v = detail::dispatch(X, cos_t, sin_t, cos_h, sin_h, cos_w, | |
| 675 | 15 | sin_w, Y); | |
| 676 | 15 | if (!v.rope_apply_mrope) | |
| 677 | ✗ | detail::throw_not_implemented("rope_apply_mrope", X.device); | |
| 678 | 15 | detail::adopt_output(Y, X.device); | |
| 679 | 30 | v.rope_apply_mrope(X, cos_t, sin_t, cos_h, sin_h, cos_w, sin_w, | |
| 680 | 15 | pos_t, pos_h, pos_w, head_dim, num_heads, | |
| 681 | 15 | d_t, d_h, d_w, Y); | |
| 682 | 15 | } | |
| 683 | |||
| 684 | 1116 | void rms_norm_forward(const Tensor& X, const Tensor& gamma, float eps, Tensor& Y) { | |
| 685 | 1116 | const auto& v = detail::dispatch(X, gamma, Y); | |
| 686 | 1116 | if (!v.rms_norm_forward) detail::throw_not_implemented("rms_norm_forward", X.device); | |
| 687 | 1116 | detail::adopt_output(Y, X.device); | |
| 688 | 1116 | v.rms_norm_forward(X, gamma, eps, Y); | |
| 689 | 1116 | } | |
| 690 | |||
| 691 | 17 | void rms_norm_backward(const Tensor& X, const Tensor& gamma, const Tensor& dY, | |
| 692 | float eps, Tensor& dX, Tensor& dGamma) { | ||
| 693 | 17 | const auto& v = detail::dispatch(X, gamma, dY, dX, dGamma); | |
| 694 | 17 | if (!v.rms_norm_backward) detail::throw_not_implemented("rms_norm_backward", X.device); | |
| 695 | 17 | detail::adopt_output(dX, X.device); | |
| 696 | 17 | detail::adopt_output(dGamma, X.device); | |
| 697 | 17 | v.rms_norm_backward(X, gamma, dY, eps, dX, dGamma); | |
| 698 | 17 | } | |
| 699 | |||
| 700 | 736 | void swiglu_forward(const Tensor& X, Tensor& Y) { | |
| 701 | 736 | const auto& v = detail::dispatch(X, Y); | |
| 702 | 736 | if (!v.swiglu_forward) detail::throw_not_implemented("swiglu_forward", X.device); | |
| 703 | 736 | detail::adopt_output(Y, X.device); | |
| 704 | 736 | v.swiglu_forward(X, Y); | |
| 705 | 736 | } | |
| 706 | 13 | void swiglu_backward(const Tensor& X, const Tensor& dY, Tensor& dX) { | |
| 707 | 13 | const auto& v = detail::dispatch(X, dY, dX); | |
| 708 | 13 | if (!v.swiglu_backward) detail::throw_not_implemented("swiglu_backward", X.device); | |
| 709 | 13 | detail::adopt_output(dX, X.device); | |
| 710 | 13 | v.swiglu_backward(X, dY, dX); | |
| 711 | 13 | } | |
| 712 | |||
| 713 | 36 | void kv_cache_append(const Tensor& K_new, const Tensor& V_new, int cur_len, | |
| 714 | Tensor& K_cache, Tensor& V_cache) { | ||
| 715 | 36 | const auto& v = detail::dispatch(K_new, V_new, K_cache, V_cache); | |
| 716 | 36 | if (!v.kv_cache_append) detail::throw_not_implemented("kv_cache_append", K_new.device); | |
| 717 | 36 | detail::adopt_output(K_cache, K_new.device); | |
| 718 | 36 | detail::adopt_output(V_cache, K_new.device); | |
| 719 | 36 | v.kv_cache_append(K_new, V_new, cur_len, K_cache, V_cache); | |
| 720 | 36 | } | |
| 721 | |||
| 722 | 61 | void flash_attention_decode(const Tensor& Q, | |
| 723 | const Tensor& K_cache, const Tensor& V_cache, | ||
| 724 | int valid_len, int num_q_heads, int num_kv_heads, | ||
| 725 | Tensor& O, float attn_softcap, int window) { | ||
| 726 | 61 | const auto& v = detail::dispatch(Q, K_cache, V_cache, O); | |
| 727 | 61 | if (!v.flash_attention_decode) detail::throw_not_implemented("flash_attention_decode", Q.device); | |
| 728 | 61 | detail::adopt_output(O, Q.device); | |
| 729 | 122 | v.flash_attention_decode(Q, K_cache, V_cache, valid_len, | |
| 730 | 61 | num_q_heads, num_kv_heads, O, attn_softcap, window); | |
| 731 | 61 | } | |
| 732 | |||
| 733 | 16 | void flash_attention_decode_masked(const Tensor& Q, | |
| 734 | const Tensor& K_cache, | ||
| 735 | const Tensor& V_cache, | ||
| 736 | const float* d_mask, | ||
| 737 | int num_q_heads, int num_kv_heads, | ||
| 738 | Tensor& O, float attn_softcap, int window) { | ||
| 739 | 16 | const auto& v = detail::dispatch(Q, K_cache, V_cache, O); | |
| 740 | 16 | if (!v.flash_attention_decode_masked) { | |
| 741 | ✗ | detail::throw_not_implemented("flash_attention_decode_masked", Q.device); | |
| 742 | } | ||
| 743 | 16 | detail::adopt_output(O, Q.device); | |
| 744 | 32 | v.flash_attention_decode_masked(Q, K_cache, V_cache, d_mask, | |
| 745 | 16 | num_q_heads, num_kv_heads, O, | |
| 746 | 16 | attn_softcap, window); | |
| 747 | 16 | } | |
| 748 | |||
| 749 | 52 | void flash_attention_decode_len(const Tensor& Q, const Tensor& K_cache, const Tensor& V_cache, | |
| 750 | const int32_t* d_len, int num_q_heads, int num_kv_heads, Tensor& O) { | ||
| 751 | 52 | const auto& v = detail::dispatch(Q, K_cache, V_cache, O); | |
| 752 | 52 | if (!v.flash_attention_decode_len) detail::throw_not_implemented("flash_attention_decode_len", Q.device); | |
| 753 | 52 | detail::adopt_output(O, Q.device); | |
| 754 | 52 | v.flash_attention_decode_len(Q, K_cache, V_cache, d_len, num_q_heads, num_kv_heads, O); | |
| 755 | 52 | } | |
| 756 | |||
| 757 | // ─── Public reductions ───────────────────────────────────────────────────── | ||
| 758 | |||
| 759 | 21 | void sum_rows(const Tensor& X, Tensor& Y) { | |
| 760 | 21 | const auto& v = detail::dispatch(X, Y); | |
| 761 | 21 | if (!v.sum_rows) detail::throw_not_implemented("sum_rows", X.device); | |
| 762 | 21 | detail::adopt_output(Y, X.device); | |
| 763 | 21 | v.sum_rows(X, Y); | |
| 764 | 21 | } | |
| 765 | 19 | void sum_cols(const Tensor& X, Tensor& Y) { | |
| 766 | 19 | const auto& v = detail::dispatch(X, Y); | |
| 767 | 19 | if (!v.sum_cols) detail::throw_not_implemented("sum_cols", X.device); | |
| 768 | 19 | detail::adopt_output(Y, X.device); | |
| 769 | 19 | v.sum_cols(X, Y); | |
| 770 | 19 | } | |
| 771 | 23 | void argmax_rows(const Tensor& X, Tensor& Idx) { | |
| 772 | 23 | const auto& v = detail::dispatch(X, Idx); | |
| 773 | 23 | if (!v.argmax_rows) detail::throw_not_implemented("argmax_rows", X.device); | |
| 774 | 23 | detail::adopt_output(Idx, X.device); | |
| 775 | 23 | v.argmax_rows(X, Idx); | |
| 776 | 23 | } | |
| 777 | 5 | void segment_softmax_stats(const Tensor& logits, const Tensor& seg_offsets, Tensor& out) { | |
| 778 | 5 | const auto& v = detail::dispatch(logits, seg_offsets, out); | |
| 779 | 5 | if (!v.segment_softmax_stats) | |
| 780 | ✗ | detail::throw_not_implemented("segment_softmax_stats", logits.device); | |
| 781 | 5 | detail::adopt_output(out, logits.device); | |
| 782 | 5 | v.segment_softmax_stats(logits, seg_offsets, out); | |
| 783 | 5 | } | |
| 784 | 1215 | void flash_attention_packed_qkv_forward(const Tensor& QKV, const Tensor& seq_bounds, | |
| 785 | int num_heads, int window, Tensor& O) { | ||
| 786 | 1215 | const auto& v = detail::dispatch(QKV, seq_bounds, O); | |
| 787 | 1215 | if (!v.flash_attention_packed_qkv_forward) | |
| 788 | ✗ | detail::throw_not_implemented("flash_attention_packed_qkv_forward", QKV.device); | |
| 789 | 1215 | detail::adopt_output(O, QKV.device); | |
| 790 | 1215 | v.flash_attention_packed_qkv_forward(QKV, seq_bounds, num_heads, window, O); | |
| 791 | 1215 | } | |
| 792 | 34 | void flash_attention_packed_qkv_backward(const Tensor& QKV, const Tensor& dO, const Tensor& seq_bounds, | |
| 793 | int num_heads, int window, Tensor& dQKV) { | ||
| 794 | 34 | const auto& v = detail::dispatch(QKV, dO, seq_bounds, dQKV); | |
| 795 | 34 | if (!v.flash_attention_packed_qkv_backward) | |
| 796 | ✗ | detail::throw_not_implemented("flash_attention_packed_qkv_backward", QKV.device); | |
| 797 | 34 | detail::adopt_output(dQKV, QKV.device); | |
| 798 | 34 | v.flash_attention_packed_qkv_backward(QKV, dO, seq_bounds, num_heads, window, dQKV); | |
| 799 | 34 | } | |
| 800 | 8 | void rope_qkv_packed_inplace(Tensor& QKV, const Tensor& cos_tbl, const Tensor& sin_tbl, | |
| 801 | const Tensor& pos, int num_heads, int head_dim) { | ||
| 802 | 8 | const auto& v = detail::dispatch(QKV, cos_tbl, sin_tbl, pos); | |
| 803 | 8 | if (!v.rope_qkv_packed_inplace) | |
| 804 | ✗ | detail::throw_not_implemented("rope_qkv_packed_inplace", QKV.device); | |
| 805 | 8 | v.rope_qkv_packed_inplace(QKV, cos_tbl, sin_tbl, pos, num_heads, head_dim); | |
| 806 | 8 | } | |
| 807 | 13 | void rows_count_above(const Tensor& X, float t_lo, float t_hi, Tensor& counts) { | |
| 808 | 13 | const auto& v = detail::dispatch(X, counts); | |
| 809 | 13 | if (!v.rows_count_above) | |
| 810 | ✗ | detail::throw_not_implemented("rows_count_above", X.device); | |
| 811 | 13 | detail::adopt_output(counts, X.device); | |
| 812 | 13 | v.rows_count_above(X, t_lo, t_hi, counts); | |
| 813 | 13 | } | |
| 814 | 11 | void threshold_u8(const Tensor& X, float t, Tensor& Y) { | |
| 815 | 11 | const auto& v = detail::dispatch(X, Y); | |
| 816 | 11 | if (!v.threshold_u8) detail::throw_not_implemented("threshold_u8", X.device); | |
| 817 | 11 | detail::adopt_output(Y, X.device); | |
| 818 | 11 | v.threshold_u8(X, t, Y); | |
| 819 | 11 | } | |
| 820 | |||
| 821 | } // namespace brotensor | ||
| 822 |