src/kernels/mir_decode_gqa.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_decode_gqa.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | |||
| 5 | #include <array> | ||
| 6 | #include <stdexcept> | ||
| 7 | #include <string> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace brotensor::kernels { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | constexpr int kBlock = 128; | ||
| 16 | constexpr int kWarps = kBlock / 32; | ||
| 17 | constexpr int kMaxSplits = 64; | ||
| 18 | |||
| 19 | 20 | std::pair<Value*, Value*> unpack_word_dt(Ctx& c, Dtype dt, Value* w) { | |
| 20 | 20 | Value* lo_bits = c.b.build_and(w, c.i32(0xffff)); | |
| 21 | 20 | Value* hi_bits = c.b.build_lshr(w, c.i32(16)); | |
| 22 | 20 | if (dt == Dtype::FP16) { | |
| 23 | 20 | return {c.kb.f16_to_f32(lo_bits), c.kb.f16_to_f32(hi_bits)}; | |
| 24 | } | ||
| 25 | ✗ | return {c.kb.bf16_to_f32(lo_bits), c.kb.bf16_to_f32(hi_bits)}; | |
| 26 | 20 | } | |
| 27 | |||
| 28 | 5 | std::array<Value*, 8> load8_f32_safe(Ctx& c, Dtype dt, Value* ptr, Value* safe_ptr, Value* valid) { | |
| 29 | 5 | Value* p = valid ? c.b.build_select(valid, ptr, safe_ptr) : ptr; | |
| 30 | 5 | Value* v4 = c.b.build_call("ptx_ldg_i32x4", Type::i32x4(), {p}); | |
| 31 | 5 | std::array<Value*, 8> out{}; | |
| 32 | 25 | for (int i = 0; i < 4; ++i) { | |
| 33 | 40 | auto [a, b] = unpack_word_dt(c, dt, c.b.build_vextract_lane(v4, static_cast<std::uint32_t>(i))); | |
| 34 | 20 | out[static_cast<std::size_t>(2 * i)] = a; | |
| 35 | 20 | out[static_cast<std::size_t>(2 * i + 1)] = b; | |
| 36 | 20 | } | |
| 37 | 5 | return out; | |
| 38 | } | ||
| 39 | |||
| 40 | Value* pack_pair_i32(Ctx& c, Dtype dt, Value* v0, Value* v1) { | ||
| 41 | if (dt == Dtype::FP16) return c.kb.pack_f16x2(v0, v1); | ||
| 42 | Value* b0 = c.b.build_and(c.kb.f32_to_bf16(v0), c.i32(0xffff)); | ||
| 43 | Value* b1 = c.b.build_shl(c.kb.f32_to_bf16(v1), c.i32(16)); | ||
| 44 | return c.b.build_or(b0, b1); | ||
| 45 | } | ||
| 46 | |||
| 47 | 11 | Value* ptr_idx(Ctx& c, Value* base, Value* elem_idx, int esz) { | |
| 48 | 11 | return ptr_at(c, base, elem_idx, esz); | |
| 49 | } | ||
| 50 | |||
| 51 | 1 | brass::Function* build_partials(brass::Module& mod, Dtype dt, std::uint8_t flags) { | |
| 52 | static constexpr int kHds[3] = {64, 128, 256}; | ||
| 53 | static constexpr int kGs[4] = {1, 2, 4, 8}; | ||
| 54 | 1 | const int hd_code = static_cast<int>(flags & 3u); | |
| 55 | 1 | const int g_code = static_cast<int>((flags >> 2) & 3u); | |
| 56 | 1 | const int rc_code = static_cast<int>((flags >> 4) & 1u); | |
| 57 | 1 | if (hd_code > 2) throw std::runtime_error("mir_decode_gqa: invalid hd_code"); | |
| 58 | |||
| 59 | 1 | const int HD = kHds[hd_code]; | |
| 60 | 1 | const int G = kGs[g_code]; | |
| 61 | 1 | const int RC = (rc_code != 0) ? (8 / G) : 1; | |
| 62 | 1 | const int NG = G * RC; | |
| 63 | 1 | const int LPK = HD / 8; | |
| 64 | 1 | const int KPS = 32 / LPK; | |
| 65 | 1 | const int STRIDE = kWarps * KPS; | |
| 66 | |||
| 67 | 3 | const std::string name = std::string("mir_decode_gqa_p_") + | |
| 68 | 1 | (dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 69 | 1 | "_hd" + std::to_string(HD) + "_g" + std::to_string(G) + | |
| 70 | 1 | "_rc" + std::to_string(RC); | |
| 71 | 12 | std::vector<Type> params = { | |
| 72 | 5 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 73 | 6 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 74 | 1 | Type::f32(), | |
| 75 | }; | ||
| 76 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 77 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 78 | 1 | Ctx c(kb); | |
| 79 | 1 | auto p = entry_params(c, fn); | |
| 80 | // p: 0:Q 1:K 2:V 3:d_len 4:ws | 5:cap 6:Dkv 7:n_splits 8:nq 9:R 10:r0 | 11:inv_sqrt_log2 | ||
| 81 | 1 | Value* Q = p[0]; | |
| 82 | 1 | Value* K = p[1]; | |
| 83 | 1 | Value* V = p[2]; | |
| 84 | 1 | Value* ws = p[4]; | |
| 85 | 1 | Value* cap = p[5]; | |
| 86 | 1 | Value* Dkv = p[6]; | |
| 87 | 1 | Value* n_splits = p[7]; | |
| 88 | 1 | Value* nq = p[8]; | |
| 89 | 1 | Value* R = p[9]; | |
| 90 | 1 | Value* r0 = p[10]; | |
| 91 | 1 | Value* inv_sqrt_log2 = p[11]; | |
| 92 | |||
| 93 | 1 | Value* spl = kb.ctaid_x(); | |
| 94 | 1 | Value* kvh = kb.ctaid_y(); | |
| 95 | 1 | Value* tid = kb.tid_x(); | |
| 96 | 1 | Value* warp = c.b.build_lshr(tid, c.i32(5)); | |
| 97 | 1 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 98 | 1 | Value* sub = c.b.build_udiv(lane, c.i32(LPK)); | |
| 99 | 1 | Value* sl = c.b.build_umod(lane, c.i32(LPK)); | |
| 100 | |||
| 101 | 1 | Value* len_raw = kb.load_i32(p[3]); | |
| 102 | 1 | Value* len = clamp_i32(c, len_raw, c.i32(0), cap); | |
| 103 | 1 | Value* chunk = c.b.build_udiv(c.b.build_sub(c.b.build_add(len, n_splits), c.i32(1)), n_splits); | |
| 104 | 1 | Value* k_begin = c.b.build_mul(spl, chunk); | |
| 105 | 1 | Value* k_end = smin_i32(c, len, c.b.build_add(k_begin, chunk)); | |
| 106 | |||
| 107 | 1 | Value* kvh_g = c.b.build_mul(kvh, c.i32(G)); | |
| 108 | 1 | Value* sl8 = c.b.build_shl(sl, c.i32(3)); | |
| 109 | 1 | Value* nq_hd = c.b.build_mul(nq, c.i32(HD)); | |
| 110 | |||
| 111 | 1 | std::vector<std::array<Value*, 8>> q_vecs(static_cast<std::size_t>(NG)); | |
| 112 | 1 | std::vector<Value*> lim(static_cast<std::size_t>(NG)); | |
| 113 | 2 | for (int gi = 0; gi < NG; ++gi) { | |
| 114 | 1 | const auto giu = static_cast<std::size_t>(gi); | |
| 115 | 1 | Value* rr = c.b.build_add(r0, c.i32(gi / G)); | |
| 116 | 1 | Value* valid_r = c.b.build_slt(rr, R); | |
| 117 | 1 | Value* lim_v = c.b.build_add(c.b.build_add(c.b.build_sub(len, R), c.i32(1)), rr); | |
| 118 | 1 | lim[giu] = c.b.build_select(valid_r, lim_v, c.i32(0)); | |
| 119 | 1 | Value* h = c.b.build_add(kvh_g, c.i32(gi % G)); | |
| 120 | 2 | Value* q_off = c.b.build_add( | |
| 121 | 1 | c.b.build_mul(rr, nq_hd), | |
| 122 | 1 | c.b.build_add(c.b.build_mul(h, c.i32(HD)), sl8)); | |
| 123 | 1 | auto q_raw = load8_f32_safe(c, dt, ptr_idx(c, Q, q_off, 2), Q, valid_r); | |
| 124 | 9 | for (int i = 0; i < 8; ++i) { | |
| 125 | 8 | Value* scaled = c.b.build_mul(q_raw[static_cast<std::size_t>(i)], inv_sqrt_log2); | |
| 126 | 8 | q_vecs[giu][static_cast<std::size_t>(i)] = c.b.build_select(valid_r, scaled, c.f32(0.0f)); | |
| 127 | 8 | } | |
| 128 | 1 | } | |
| 129 | |||
| 130 | 1 | Value* col = c.b.build_add(c.b.build_mul(kvh, c.i32(HD)), sl8); | |
| 131 | 1 | Value* init_base = c.b.build_add(k_begin, c.b.build_mul(warp, c.i32(KPS))); | |
| 132 | |||
| 133 | 1 | std::vector<Value*> init_st; | |
| 134 | 1 | init_st.reserve(static_cast<std::size_t>(NG * 10)); | |
| 135 | 2 | for (int g = 0; g < NG; ++g) { | |
| 136 | 1 | init_st.push_back(c.f32(-1e30f)); // m[g] | |
| 137 | 1 | init_st.push_back(c.f32(0.0f)); // l[g] | |
| 138 | 9 | for (int i = 0; i < 8; ++i) init_st.push_back(c.f32(0.0f)); | |
| 139 | 1 | } | |
| 140 | |||
| 141 | 1 | auto st_out = kb.for_range_reduce_n( | |
| 142 | 1 | init_base, k_end, c.i32(2 * STRIDE), init_st, | |
| 143 | 2 | [&](Value* base, const std::vector<Value*>& cur) -> std::vector<Value*> { | |
| 144 | 1 | Value* k0 = c.b.build_add(base, sub); | |
| 145 | 1 | Value* k1 = c.b.build_add(c.b.build_add(base, c.i32(STRIDE)), sub); | |
| 146 | 1 | Value* v0 = c.b.build_slt(k0, k_end); | |
| 147 | 1 | Value* v1 = c.b.build_slt(k1, k_end); | |
| 148 | |||
| 149 | 1 | Value* off0 = c.b.build_add(c.b.build_mul(k0, Dkv), col); | |
| 150 | 1 | Value* off1 = c.b.build_add(c.b.build_mul(k1, Dkv), col); | |
| 151 | 1 | auto ka = load8_f32_safe(c, dt, ptr_idx(c, K, off0, 2), K, v0); | |
| 152 | 1 | auto va = load8_f32_safe(c, dt, ptr_idx(c, V, off0, 2), V, v0); | |
| 153 | 1 | auto kb_v = load8_f32_safe(c, dt, ptr_idx(c, K, off1, 2), K, v1); | |
| 154 | 1 | auto vb = load8_f32_safe(c, dt, ptr_idx(c, V, off1, 2), V, v1); | |
| 155 | |||
| 156 | 1 | std::vector<Value*> nxt = cur; | |
| 157 | 3 | for (int step = 0; step < 2; ++step) { | |
| 158 | 2 | Value* valid = (step == 0) ? v0 : v1; | |
| 159 | 2 | const auto& kk = (step == 0) ? ka : kb_v; | |
| 160 | 2 | const auto& vv = (step == 0) ? va : vb; | |
| 161 | 2 | Value* kidx = (step == 0) ? k0 : k1; | |
| 162 | 4 | for (int g = 0; g < NG; ++g) { | |
| 163 | 2 | const std::size_t off = static_cast<std::size_t>(g * 10); | |
| 164 | 2 | Value* s = c.f32(0.0f); | |
| 165 | 18 | for (int i = 0; i < 8; ++i) { | |
| 166 | 32 | s = c.b.build_fma_f32( | |
| 167 | 16 | q_vecs[static_cast<std::size_t>(g)][static_cast<std::size_t>(i)], | |
| 168 | 16 | kk[static_cast<std::size_t>(i)], s); | |
| 169 | 16 | } | |
| 170 | 2 | s = c.b.build_select(valid, s, c.f32(0.0f)); | |
| 171 | 8 | for (int red_off = LPK / 2; red_off > 0; red_off >>= 1) { | |
| 172 | 6 | s = c.b.build_add(s, kb.shfl_bfly_f32(s, static_cast<std::uint32_t>(red_off))); | |
| 173 | 6 | } | |
| 174 | 2 | Value* attend = c.b.build_and(valid, c.b.build_slt(kidx, lim[static_cast<std::size_t>(g)])); | |
| 175 | 2 | Value* m_cur = nxt[off + 0]; | |
| 176 | 2 | Value* l_cur = nxt[off + 1]; | |
| 177 | 2 | Value* mn = kb.fmax(m_cur, s); | |
| 178 | 2 | Value* c_scale = kb.ex2_approx(c.b.build_sub(m_cur, mn)); | |
| 179 | 2 | Value* p_val = kb.ex2_approx(c.b.build_sub(s, mn)); | |
| 180 | 2 | Value* l_new = c.b.build_fma_f32(l_cur, c_scale, p_val); | |
| 181 | 2 | nxt[off + 0] = c.b.build_select(attend, mn, m_cur); | |
| 182 | 2 | nxt[off + 1] = c.b.build_select(attend, l_new, l_cur); | |
| 183 | 18 | for (int i = 0; i < 8; ++i) { | |
| 184 | 16 | Value* ai = nxt[off + 2 + static_cast<std::size_t>(i)]; | |
| 185 | 16 | Value* a_new = c.b.build_fma_f32( | |
| 186 | 16 | p_val, vv[static_cast<std::size_t>(i)], c.b.build_mul(ai, c_scale)); | |
| 187 | 16 | nxt[off + 2 + static_cast<std::size_t>(i)] = c.b.build_select(attend, a_new, ai); | |
| 188 | 16 | } | |
| 189 | 2 | } | |
| 190 | 2 | } | |
| 191 | 1 | return nxt; | |
| 192 | 1 | }); | |
| 193 | |||
| 194 | // In-warp merge across key slots (lanes sharing `sl`). | ||
| 195 | 3 | for (int off = LPK; off < 32; off <<= 1) { | |
| 196 | 2 | const auto mask = static_cast<std::uint32_t>(off); | |
| 197 | 4 | for (int g = 0; g < NG; ++g) { | |
| 198 | 2 | const std::size_t base = static_cast<std::size_t>(g * 10); | |
| 199 | 2 | Value* m1 = st_out[base + 0]; | |
| 200 | 2 | Value* l1 = st_out[base + 1]; | |
| 201 | 2 | Value* m2 = kb.shfl_bfly_f32(m1, mask); | |
| 202 | 2 | Value* l2 = kb.shfl_bfly_f32(l1, mask); | |
| 203 | 2 | Value* mn = kb.fmax(m1, m2); | |
| 204 | 2 | Value* ca = kb.ex2_approx(c.b.build_sub(m1, mn)); | |
| 205 | 2 | Value* cb = kb.ex2_approx(c.b.build_sub(m2, mn)); | |
| 206 | 2 | st_out[base + 0] = mn; | |
| 207 | 2 | st_out[base + 1] = c.b.build_fma_f32(l2, cb, c.b.build_mul(l1, ca)); | |
| 208 | 18 | for (int i = 0; i < 8; ++i) { | |
| 209 | 16 | Value* a1 = st_out[base + 2 + static_cast<std::size_t>(i)]; | |
| 210 | 16 | Value* a2 = kb.shfl_bfly_f32(a1, mask); | |
| 211 | 16 | st_out[base + 2 + static_cast<std::size_t>(i)] = | |
| 212 | 16 | c.b.build_fma_f32(a2, cb, c.b.build_mul(a1, ca)); | |
| 213 | 16 | } | |
| 214 | 2 | } | |
| 215 | 2 | } | |
| 216 | |||
| 217 | // Cross-warp merge via shared memory, writing [m, l, acc[HD]] to ws. | ||
| 218 | 1 | Value* s_ml = kb.shared_alloc_f32(static_cast<std::uint32_t>(kWarps * NG * 2)); | |
| 219 | 1 | Value* s_acc = kb.shared_alloc_f32(static_cast<std::uint32_t>(kWarps * NG * HD)); | |
| 220 | 2 | kb.if_then(c.b.build_eq(sub, c.i32(0)), [&] { | |
| 221 | 1 | Value* ml_base = c.b.build_mul(warp, c.i32(NG * 2)); | |
| 222 | 1 | Value* acc_base = c.b.build_mul(warp, c.i32(NG * HD)); | |
| 223 | 2 | for (int g = 0; g < NG; ++g) { | |
| 224 | 1 | const std::size_t sb = static_cast<std::size_t>(g * 10); | |
| 225 | 1 | Value* gml = c.b.build_add(ml_base, c.i32(g * 2)); | |
| 226 | 2 | kb.if_then(c.b.build_eq(sl, c.i32(0)), [&] { | |
| 227 | 1 | kb.shared_store_f32_indexed(s_ml, gml, st_out[sb + 0]); | |
| 228 | 1 | kb.shared_store_f32_indexed(s_ml, c.b.build_add(gml, c.i32(1)), st_out[sb + 1]); | |
| 229 | 1 | }); | |
| 230 | 1 | Value* gacc = c.b.build_add(c.b.build_add(acc_base, c.i32(g * HD)), sl8); | |
| 231 | 9 | for (int i = 0; i < 8; ++i) { | |
| 232 | 16 | kb.shared_store_f32_indexed( | |
| 233 | 8 | s_acc, c.b.build_add(gacc, c.i32(i)), | |
| 234 | 8 | st_out[sb + 2 + static_cast<std::size_t>(i)]); | |
| 235 | 8 | } | |
| 236 | 1 | } | |
| 237 | 1 | }); | |
| 238 | 1 | kb.sync(); | |
| 239 | |||
| 240 | 1 | const int rec_w = HD + 2; | |
| 241 | 1 | const int total = NG * rec_w; | |
| 242 | 2 | kb.for_range(tid, c.i32(total), c.i32(kBlock), [&](Value* idx) { | |
| 243 | 1 | Value* g = c.b.build_udiv(idx, c.i32(rec_w)); | |
| 244 | 1 | Value* e = c.b.build_umod(idx, c.i32(rec_w)); | |
| 245 | 1 | Value* rr = c.b.build_add(r0, c.b.build_udiv(g, c.i32(G))); | |
| 246 | 2 | kb.if_then(c.b.build_slt(rr, R), [&] { | |
| 247 | 1 | Value* g_ml = c.b.build_shl(g, c.i32(1)); | |
| 248 | 1 | Value* m = c.f32(-1e30f); | |
| 249 | 1 | std::array<Value*, kWarps> wm{}; | |
| 250 | 5 | for (int w = 0; w < kWarps; ++w) { | |
| 251 | 8 | Value* v = kb.shared_load_f32_indexed( | |
| 252 | 4 | s_ml, c.b.build_add(c.i32(w * NG * 2), g_ml)); | |
| 253 | 4 | wm[static_cast<std::size_t>(w)] = v; | |
| 254 | 4 | m = kb.fmax(m, v); | |
| 255 | 4 | } | |
| 256 | 1 | Value* g_acc_e = c.b.build_add(c.b.build_mul(g, c.i32(HD)), c.b.build_sub(e, c.i32(2))); | |
| 257 | 1 | Value* is_e0 = c.b.build_eq(e, c.i32(0)); | |
| 258 | 1 | Value* is_e1 = c.b.build_eq(e, c.i32(1)); | |
| 259 | 1 | Value* safe_acc_e = c.b.build_select(c.b.build_ult(e, c.i32(2)), c.i32(0), g_acc_e); | |
| 260 | 1 | Value* sum = c.f32(0.0f); | |
| 261 | 5 | for (int w = 0; w < kWarps; ++w) { | |
| 262 | 4 | Value* cw = kb.ex2_approx(c.b.build_sub(wm[static_cast<std::size_t>(w)], m)); | |
| 263 | 8 | Value* lv = kb.shared_load_f32_indexed( | |
| 264 | 4 | s_ml, c.b.build_add(c.i32(w * NG * 2 + 1), g_ml)); | |
| 265 | 8 | Value* av = kb.shared_load_f32_indexed( | |
| 266 | 4 | s_acc, c.b.build_add(c.i32(w * NG * HD), safe_acc_e)); | |
| 267 | 4 | Value* val = c.b.build_select(is_e0, c.f32(0.0f), c.b.build_select(is_e1, lv, av)); | |
| 268 | 4 | sum = c.b.build_fma_f32(cw, val, sum); | |
| 269 | 4 | } | |
| 270 | 1 | Value* out_v = c.b.build_select(is_e0, m, sum); | |
| 271 | 2 | Value* h = c.b.build_add( | |
| 272 | 1 | c.b.build_add(c.b.build_mul(rr, nq), kvh_g), | |
| 273 | 1 | c.b.build_umod(g, c.i32(G))); | |
| 274 | 2 | Value* ws_idx = c.b.build_add( | |
| 275 | 1 | c.b.build_mul(c.b.build_add(c.b.build_mul(h, n_splits), spl), c.i32(rec_w)), e); | |
| 276 | 1 | kb.store_f32(ptr_idx(c, ws, ws_idx, 4), out_v); | |
| 277 | 1 | }); | |
| 278 | 1 | }); | |
| 279 | |||
| 280 | 1 | c.b.build_ret_void(); | |
| 281 | 1 | return finalize_fn(fn); | |
| 282 | 1 | } | |
| 283 | |||
| 284 | 1 | brass::Function* build_reduce(brass::Module& mod, Dtype dt) { | |
| 285 | 1 | const std::string name = std::string("mir_decode_gqa_r_") + (dt == Dtype::FP16 ? "f16" : "bf16"); | |
| 286 | 1 | std::vector<Type> params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()}; | |
| 287 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 288 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 289 | 1 | Ctx c(kb); | |
| 290 | 1 | auto p = entry_params(c, fn); | |
| 291 | // p: 0:ws 1:O 2:hd 3:n_splits | ||
| 292 | 1 | Value* ws = p[0]; | |
| 293 | 1 | Value* O = p[1]; | |
| 294 | 1 | Value* hd = p[2]; | |
| 295 | 1 | Value* n_splits = p[3]; | |
| 296 | |||
| 297 | 1 | Value* sm = kb.shared_alloc_f32(kMaxSplits); | |
| 298 | 1 | Value* sl = kb.shared_alloc_f32(kMaxSplits); | |
| 299 | 1 | Value* sw = kb.shared_alloc_f32(kMaxSplits); | |
| 300 | |||
| 301 | 1 | Value* h = kb.ctaid_x(); | |
| 302 | 1 | Value* tid = kb.tid_x(); | |
| 303 | 1 | Value* rec = c.b.build_add(hd, c.i32(2)); | |
| 304 | 1 | Value* h_off = c.b.build_mul(c.b.build_mul(h, n_splits), rec); | |
| 305 | 1 | Value* base = ptr_idx(c, ws, h_off, 4); | |
| 306 | |||
| 307 | 2 | kb.for_range(tid, n_splits, c.i32(kBlock), [&](Value* s) { | |
| 308 | 1 | Value* sr = c.b.build_mul(s, rec); | |
| 309 | 1 | kb.shared_store_f32_indexed(sm, s, kb.load_f32(ptr_idx(c, base, sr, 4))); | |
| 310 | 1 | kb.shared_store_f32_indexed(sl, s, kb.load_f32(ptr_idx(c, base, c.b.build_add(sr, c.i32(1)), 4))); | |
| 311 | 1 | }); | |
| 312 | 1 | kb.sync(); | |
| 313 | |||
| 314 | 1 | Value* m = kb.for_range_reduce( | |
| 315 | 1 | c.i32(0), n_splits, c.i32(1), c.f32(-1e30f), | |
| 316 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 317 | 1 | return kb.fmax(cur, kb.shared_load_f32_indexed(sm, s)); | |
| 318 | }); | ||
| 319 | 2 | kb.for_range(tid, n_splits, c.i32(kBlock), [&](Value* s) { | |
| 320 | 1 | Value* mv = kb.shared_load_f32_indexed(sm, s); | |
| 321 | 1 | kb.shared_store_f32_indexed(sw, s, kb.ex2_approx(c.b.build_sub(mv, m))); | |
| 322 | 1 | }); | |
| 323 | 1 | kb.sync(); | |
| 324 | |||
| 325 | 1 | Value* den = kb.for_range_reduce( | |
| 326 | 1 | c.i32(0), n_splits, c.i32(1), c.f32(0.0f), | |
| 327 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 328 | 2 | return c.b.build_fma_f32( | |
| 329 | 1 | kb.shared_load_f32_indexed(sw, s), | |
| 330 | 1 | kb.shared_load_f32_indexed(sl, s), cur); | |
| 331 | }); | ||
| 332 | 2 | Value* inv = c.b.build_select( | |
| 333 | 1 | c.b.build_sgt(den, c.f32(0.0f)), | |
| 334 | 1 | kb.div_approx(c.f32(1.0f), den), | |
| 335 | 1 | c.f32(0.0f)); | |
| 336 | |||
| 337 | 1 | Value* o_row = ptr_idx(c, O, c.b.build_mul(h, hd), 2); | |
| 338 | 2 | kb.for_range(tid, hd, c.i32(kBlock), [&](Value* d) { | |
| 339 | 2 | Value* num = kb.for_range_reduce( | |
| 340 | 1 | c.i32(0), n_splits, c.i32(1), c.f32(0.0f), | |
| 341 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 342 | 1 | Value* w = kb.shared_load_f32_indexed(sw, s); | |
| 343 | 2 | Value* val = kb.load_f32( | |
| 344 | 1 | ptr_idx(c, base, c.b.build_add(c.b.build_add(c.b.build_mul(s, rec), c.i32(2)), d), 4)); | |
| 345 | 1 | return c.b.build_fma_f32(w, val, cur); | |
| 346 | }); | ||
| 347 | 1 | Value* res = c.b.build_mul(num, inv); | |
| 348 | 1 | store_elem_dt(c, dt, o_row, d, res); | |
| 349 | 1 | }); | |
| 350 | |||
| 351 | 1 | c.b.build_ret_void(); | |
| 352 | 1 | return finalize_fn(fn); | |
| 353 | 1 | } | |
| 354 | |||
| 355 | } // namespace | ||
| 356 | |||
| 357 | 2 | brass::Function* build_decode_gqa_mir(brass::Module& mod, std::uint32_t op, Dtype dt, std::uint8_t flags) { | |
| 358 | 2 | if (dt != Dtype::FP16 && dt != Dtype::BF16) { | |
| 359 | ✗ | throw std::runtime_error("build_decode_gqa_mir: only FP16/BF16 supported"); | |
| 360 | } | ||
| 361 | 2 | if (op == 0u) return build_partials(mod, dt, flags); | |
| 362 | 1 | if (op == 1u) return build_reduce(mod, dt); | |
| 363 | ✗ | throw std::runtime_error("build_decode_gqa_mir: unsupported op"); | |
| 364 | 2 | } | |
| 365 | |||
| 366 | } // namespace brotensor::kernels | ||
| 367 |