src/kernels/mir_fa_aux.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_fa_aux.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | #include "mir_math.h" | ||
| 5 | |||
| 6 | #include <stdexcept> | ||
| 7 | #include <string> | ||
| 8 | #include <utility> | ||
| 9 | #include <vector> | ||
| 10 | |||
| 11 | namespace brotensor::kernels { | ||
| 12 | |||
| 13 | namespace { | ||
| 14 | |||
| 15 | constexpr float kPScale = 16384.0f; | ||
| 16 | |||
| 17 | 8 | Value* decode_half_raw(Ctx& c, Dtype dt, Value* raw16) { | |
| 18 | 8 | return (dt == Dtype::FP16) ? c.kb.f16_to_f32(raw16) : c.kb.bf16_to_f32(raw16); | |
| 19 | } | ||
| 20 | |||
| 21 | 20 | Value* encode_half_raw(Ctx& c, Dtype dt, Value* v) { | |
| 22 | 20 | return (dt == Dtype::FP16) ? c.kb.f32_to_f16(v) : c.kb.f32_to_bf16(v); | |
| 23 | } | ||
| 24 | |||
| 25 | // Compute power-of-two scale and inverse scale from *sbits: | ||
| 26 | // frexpf(b, &e) where b > 0 && isfinite(b) => e = exp_raw - 126, | ||
| 27 | // 14 - e = 140 - exp_raw clamped to [-120, 120]. | ||
| 28 | 3 | std::pair<Value*, Value*> emit_bw_ds_scale_and_inv(Ctx& c, Value* sbits) { | |
| 29 | 3 | Value* u = c.kb.load_i32(sbits); | |
| 30 | 3 | Value* exp_raw = c.b.build_and(c.b.build_lshr(u, c.i32(23)), c.i32(0xff)); | |
| 31 | 3 | Value* positive = c.b.build_sgt(u, c.i32(0)); | |
| 32 | 3 | Value* finite = c.b.build_slt(exp_raw, c.i32(255)); | |
| 33 | 3 | Value* valid = c.b.build_and(positive, finite); | |
| 34 | 3 | Value* shift = clamp_i32(c, c.b.build_sub(c.i32(140), exp_raw), c.i32(-120), c.i32(120)); | |
| 35 | 3 | Value* scale_bits = c.b.build_shl(c.b.build_add(shift, c.i32(127)), c.i32(23)); | |
| 36 | 3 | Value* inv_bits = c.b.build_shl(c.b.build_sub(c.i32(127), shift), c.i32(23)); | |
| 37 | 3 | Value* scale = c.b.build_select(valid, c.b.build_bitcast_f32_i32(scale_bits), c.f32(1.0f)); | |
| 38 | 3 | Value* inv = c.b.build_select(valid, c.b.build_bitcast_f32_i32(inv_bits), c.f32(1.0f)); | |
| 39 | 3 | return {scale, inv}; | |
| 40 | } | ||
| 41 | |||
| 42 | 1 | brass::Function* build_split_combine(brass::Module& mod, Dtype dt) { | |
| 43 | 1 | const std::string name = std::string("mir_fa_split_combine_") + (dt == Dtype::FP16 ? "f16" : "bf16"); | |
| 44 | 8 | std::vector<Type> params = { | |
| 45 | 3 | Type::ptr(), Type::ptr(), Type::ptr(), | |
| 46 | 5 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 47 | }; | ||
| 48 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 49 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 50 | 1 | Ctx c(kb); | |
| 51 | 1 | auto p = entry_params(c, fn); | |
| 52 | // p: 0:part_o 1:part_ml 2:O | 3:Lq 4:D 5:heads 6:hd 7:splits | ||
| 53 | 1 | Value* part_o = p[0]; | |
| 54 | 1 | Value* part_ml = p[1]; | |
| 55 | 1 | Value* O = p[2]; | |
| 56 | 1 | Value* Lq = p[3]; | |
| 57 | 1 | Value* D = p[4]; | |
| 58 | 1 | Value* heads = p[5]; | |
| 59 | 1 | Value* hd = p[6]; | |
| 60 | 1 | Value* splits = p[7]; | |
| 61 | |||
| 62 | 1 | Value* row = kb.ctaid_x(); | |
| 63 | 1 | Value* head = kb.ctaid_y(); | |
| 64 | 1 | Value* b = kb.ctaid_z(); | |
| 65 | 1 | Value* bs_base = c.b.build_mul(b, splits); | |
| 66 | 1 | Value* head2 = c.b.build_shl(head, c.i32(1)); | |
| 67 | 1 | Value* head_col = c.b.build_mul(head, hd); | |
| 68 | |||
| 69 | 1 | Value* m = kb.for_range_reduce( | |
| 70 | 1 | c.i32(0), splits, c.i32(1), c.f32(-1e30f), | |
| 71 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 72 | 1 | Value* pr = c.b.build_add(c.b.build_mul(c.b.build_add(bs_base, s), Lq), row); | |
| 73 | 1 | Value* idx = c.b.build_add(c.b.build_mul(c.b.build_mul(pr, heads), c.i32(2)), head2); | |
| 74 | 1 | Value* mv = kb.load_f32(ptr_at(c, part_ml, idx, 4)); | |
| 75 | 1 | Value* lv = kb.load_f32(ptr_at(c, part_ml, c.b.build_add(idx, c.i32(1)), 4)); | |
| 76 | 1 | return c.b.build_select(c.b.build_sgt(lv, c.f32(0.0f)), kb.fmax(cur, mv), cur); | |
| 77 | }); | ||
| 78 | 1 | Value* l = kb.for_range_reduce( | |
| 79 | 1 | c.i32(0), splits, c.i32(1), c.f32(0.0f), | |
| 80 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 81 | 1 | Value* pr = c.b.build_add(c.b.build_mul(c.b.build_add(bs_base, s), Lq), row); | |
| 82 | 1 | Value* idx = c.b.build_add(c.b.build_mul(c.b.build_mul(pr, heads), c.i32(2)), head2); | |
| 83 | 1 | Value* mv = kb.load_f32(ptr_at(c, part_ml, idx, 4)); | |
| 84 | 1 | Value* lv = kb.load_f32(ptr_at(c, part_ml, c.b.build_add(idx, c.i32(1)), 4)); | |
| 85 | 1 | Value* ok = c.b.build_sgt(lv, c.f32(0.0f)); | |
| 86 | 1 | Value* term = c.b.build_mul(kb.ex2_approx(c.b.build_sub(mv, m)), lv); | |
| 87 | 1 | return c.b.build_add(cur, c.b.build_select(ok, term, c.f32(0.0f))); | |
| 88 | }); | ||
| 89 | 2 | Value* inv = c.b.build_select( | |
| 90 | 1 | c.b.build_sgt(l, c.f32(0.0f)), kb.div_approx(c.f32(1.0f), l), c.f32(0.0f)); | |
| 91 | 1 | Value* o_base = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(b, Lq), row), D), head_col); | |
| 92 | |||
| 93 | 2 | kb.for_range(kb.tid_x(), hd, c.i32(128), [&](Value* col) { | |
| 94 | 2 | Value* acc = kb.for_range_reduce( | |
| 95 | 1 | c.i32(0), splits, c.i32(1), c.f32(0.0f), | |
| 96 | 2 | [&](Value* s, Value* cur) -> Value* { | |
| 97 | 1 | Value* pr = c.b.build_add(c.b.build_mul(c.b.build_add(bs_base, s), Lq), row); | |
| 98 | 1 | Value* ml_idx = c.b.build_add(c.b.build_mul(c.b.build_mul(pr, heads), c.i32(2)), head2); | |
| 99 | 1 | Value* mv = kb.load_f32(ptr_at(c, part_ml, ml_idx, 4)); | |
| 100 | 1 | Value* lv = kb.load_f32(ptr_at(c, part_ml, c.b.build_add(ml_idx, c.i32(1)), 4)); | |
| 101 | 1 | Value* ok = c.b.build_sgt(lv, c.f32(0.0f)); | |
| 102 | 1 | Value* w = c.b.build_select(ok, kb.ex2_approx(c.b.build_sub(mv, m)), c.f32(0.0f)); | |
| 103 | 1 | Value* pv = kb.load_f32(ptr_at(c, part_o, c.b.build_add(c.b.build_add(c.b.build_mul(pr, D), head_col), col), 4)); | |
| 104 | 1 | return c.b.build_fma_f32(w, pv, cur); | |
| 105 | }); | ||
| 106 | 1 | store_elem_dt(c, dt, O, c.b.build_add(o_base, col), c.b.build_mul(acc, inv)); | |
| 107 | 1 | }); | |
| 108 | |||
| 109 | 1 | c.b.build_ret_void(); | |
| 110 | 1 | return finalize_fn(fn); | |
| 111 | 1 | } | |
| 112 | |||
| 113 | 1 | brass::Function* build_gather(brass::Module& mod, Dtype dt) { | |
| 114 | 1 | const std::string name = std::string("mir_fa_gather_") + (dt == Dtype::FP16 ? "f16" : "bf16"); | |
| 115 | 8 | std::vector<Type> params = { | |
| 116 | 2 | Type::ptr(), Type::ptr(), | |
| 117 | 6 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 118 | }; | ||
| 119 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 120 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 121 | 1 | Ctx c(kb); | |
| 122 | 1 | auto p = entry_params(c, fn); | |
| 123 | // p: 0:X 1:Y | 2:ldX 3:col0 4:rows 5:hd 6:rows_pad 7:ldY | ||
| 124 | 1 | Value* total = c.b.build_mul(p[6], p[7]); | |
| 125 | 1 | Value* idx = kb.global_tid_x(); | |
| 126 | 2 | kb.if_then(c.b.build_slt(idx, total), [&] { | |
| 127 | 1 | Value* r = c.b.build_udiv(idx, p[7]); | |
| 128 | 1 | Value* d = c.b.build_umod(idx, p[7]); | |
| 129 | 1 | Value* ok = c.b.build_and(c.b.build_slt(r, p[4]), c.b.build_slt(d, p[5])); | |
| 130 | 1 | Value* x_idx = c.b.build_add(c.b.build_add(c.b.build_mul(r, p[2]), p[3]), d); | |
| 131 | 1 | Value* raw = kb.load_u16(c.b.build_select(ok, ptr_at(c, p[0], x_idx, 2), p[0])); | |
| 132 | 1 | kb.store_u16(ptr_at(c, p[1], idx, 2), c.b.build_select(ok, raw, c.i32(0))); | |
| 133 | 1 | }); | |
| 134 | 1 | c.b.build_ret_void(); | |
| 135 | 1 | return finalize_fn(fn); | |
| 136 | 1 | } | |
| 137 | |||
| 138 | 1 | brass::Function* build_gather_T(brass::Module& mod, Dtype dt, bool dup) { | |
| 139 | 1 | const std::string name = std::string("mir_fa_gather_T_") + (dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 140 | 1 | (dup ? "_dup" : ""); | |
| 141 | 9 | std::vector<Type> params = { | |
| 142 | 2 | Type::ptr(), Type::ptr(), | |
| 143 | 7 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 144 | }; | ||
| 145 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 146 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 147 | 1 | Ctx c(kb); | |
| 148 | 1 | auto p = entry_params(c, fn); | |
| 149 | // p: 0:X 1:Y | 2:ldX 3:col0 4:rows 5:hd 6:rows_pad 7:hdp 8:ldY | ||
| 150 | 1 | Value* total = c.b.build_mul(p[7], p[6]); | |
| 151 | 1 | Value* idx = kb.global_tid_x(); | |
| 152 | 2 | kb.if_then(c.b.build_slt(idx, total), [&] { | |
| 153 | 1 | Value* d = c.b.build_udiv(idx, p[6]); | |
| 154 | 1 | Value* r = c.b.build_umod(idx, p[6]); | |
| 155 | 1 | Value* ok = c.b.build_and(c.b.build_slt(r, p[4]), c.b.build_slt(d, p[5])); | |
| 156 | 1 | Value* x_idx = c.b.build_add(c.b.build_add(c.b.build_mul(r, p[2]), p[3]), d); | |
| 157 | 1 | Value* raw = kb.load_u16(c.b.build_select(ok, ptr_at(c, p[0], x_idx, 2), p[0])); | |
| 158 | 1 | Value* v = c.b.build_select(ok, raw, c.i32(0)); | |
| 159 | 1 | Value* y_idx = c.b.build_add(c.b.build_mul(d, p[8]), r); | |
| 160 | 1 | kb.store_u16(ptr_at(c, p[1], y_idx, 2), v); | |
| 161 | 1 | if (dup) { | |
| 162 | 1 | kb.store_u16(ptr_at(c, p[1], c.b.build_add(y_idx, p[6]), 2), v); | |
| 163 | 1 | } | |
| 164 | 1 | }); | |
| 165 | 1 | c.b.build_ret_void(); | |
| 166 | 1 | return finalize_fn(fn); | |
| 167 | 1 | } | |
| 168 | |||
| 169 | 1 | brass::Function* build_scatter_head(brass::Module& mod, Dtype dt) { | |
| 170 | 1 | const std::string name = std::string("mir_fa_scatter_head_") + (dt == Dtype::FP16 ? "f16" : "bf16"); | |
| 171 | 7 | std::vector<Type> params = { | |
| 172 | 2 | Type::ptr(), Type::ptr(), | |
| 173 | 5 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 174 | }; | ||
| 175 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 176 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 177 | 1 | Ctx c(kb); | |
| 178 | 1 | auto p = entry_params(c, fn); | |
| 179 | // p: 0:Y 1:Out | 2:L 3:D 4:col0 5:hd 6:ldY | ||
| 180 | 1 | Value* total = c.b.build_mul(p[2], p[5]); | |
| 181 | 1 | Value* idx = kb.global_tid_x(); | |
| 182 | 2 | kb.if_then(c.b.build_slt(idx, total), [&] { | |
| 183 | 1 | Value* l = c.b.build_udiv(idx, p[5]); | |
| 184 | 1 | Value* d = c.b.build_umod(idx, p[5]); | |
| 185 | 1 | Value* y_idx = c.b.build_add(c.b.build_mul(l, p[6]), d); | |
| 186 | 1 | Value* o_idx = c.b.build_add(c.b.build_add(c.b.build_mul(l, p[3]), p[4]), d); | |
| 187 | 1 | kb.store_u16(ptr_at(c, p[1], o_idx, 2), kb.load_u16(ptr_at(c, p[0], y_idx, 2))); | |
| 188 | 1 | }); | |
| 189 | 1 | c.b.build_ret_void(); | |
| 190 | 1 | return finalize_fn(fn); | |
| 191 | 1 | } | |
| 192 | |||
| 193 | 1 | brass::Function* build_softmax_rows_f32(brass::Module& mod, Dtype dt, bool has_mask) { | |
| 194 | 3 | const std::string name = std::string("mir_fa_softmax_rows_") + | |
| 195 | 1 | (dt == Dtype::FP16 ? "f16" : "bf16") + (has_mask ? "_m" : ""); | |
| 196 | 6 | std::vector<Type> params = { | |
| 197 | 3 | Type::ptr(), Type::ptr(), Type::ptr(), | |
| 198 | 3 | Type::i32(), Type::i32(), Type::f32(), | |
| 199 | }; | ||
| 200 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 201 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 202 | 1 | Ctx c(kb); | |
| 203 | 1 | auto p = entry_params(c, fn); | |
| 204 | // p: 0:S 1:P 2:mask | 3:Lk 4:ld | 5:scale | ||
| 205 | 1 | Value* s8 = kb.shared_alloc_f32(8); | |
| 206 | 1 | Value* row_off = c.b.build_mul(kb.ctaid_x(), p[4]); | |
| 207 | 1 | Value* srow = ptr_at(c, p[0], row_off, 4); | |
| 208 | 1 | Value* prow = ptr_at(c, p[1], row_off, 2); | |
| 209 | 1 | Value* tid = kb.tid_x(); | |
| 210 | |||
| 211 | 3 | auto key_ok = [&](Value* k) -> Value* { | |
| 212 | 2 | if (!has_mask) return c.kb.const_i32(1); | |
| 213 | 2 | return c.b.build_sgt(kb.load_f32(ptr_at(c, p[2], k, 4)), c.f32(0.5f)); | |
| 214 | 2 | }; | |
| 215 | |||
| 216 | 1 | Value* local_max = kb.for_range_reduce( | |
| 217 | 1 | tid, p[3], c.i32(256), c.f32(-1e30f), | |
| 218 | 2 | [&](Value* k, Value* cur) -> Value* { | |
| 219 | 1 | Value* sv = c.b.build_mul(kb.load_f32(ptr_at(c, srow, k, 4)), p[5]); | |
| 220 | 1 | return has_mask ? c.b.build_select(key_ok(k), kb.fmax(cur, sv), cur) : kb.fmax(cur, sv); | |
| 221 | }); | ||
| 222 | 1 | Value* mx = emit_block_max_f32(c, s8, local_max); | |
| 223 | 1 | Value* empty = c.b.build_sle(mx, c.f32(-1e29f)); | |
| 224 | |||
| 225 | 1 | Value* local_sum = kb.for_range_reduce( | |
| 226 | 1 | tid, p[3], c.i32(256), c.f32(0.0f), | |
| 227 | 2 | [&](Value* k, Value* cur) -> Value* { | |
| 228 | 1 | Value* ev = emit_exp(c, c.b.build_sub(c.b.build_mul(kb.load_f32(ptr_at(c, srow, k, 4)), p[5]), mx)); | |
| 229 | 1 | Value* ok = has_mask ? key_ok(k) : c.i32(1); | |
| 230 | 1 | return c.b.build_add(cur, c.b.build_select(c.b.build_ne(ok, c.i32(0)), ev, c.f32(0.0f))); | |
| 231 | }); | ||
| 232 | 1 | Value* sum = emit_block_sum_f32(c, s8, local_sum); | |
| 233 | 1 | Value* valid_row = c.b.build_and(c.b.build_not(empty), c.b.build_sgt(sum, c.f32(0.0f))); | |
| 234 | 1 | Value* inv = c.b.build_select(valid_row, emit_div_rn(c, c.f32(1.0f), sum), c.f32(0.0f)); | |
| 235 | |||
| 236 | 2 | kb.for_range(tid, p[4], c.i32(256), [&](Value* k) { | |
| 237 | 1 | Value* in_k = c.b.build_slt(k, p[3]); | |
| 238 | 1 | Value* safe_k = c.b.build_select(in_k, k, c.i32(0)); | |
| 239 | 1 | Value* ev = emit_exp(c, c.b.build_sub(c.b.build_mul(kb.load_f32(ptr_at(c, srow, safe_k, 4)), p[5]), mx)); | |
| 240 | 1 | Value* ok = has_mask ? c.b.build_and(in_k, c.b.build_sgt(kb.load_f32(ptr_at(c, p[2], safe_k, 4)), c.f32(0.5f))) | |
| 241 | ✗ | : in_k; | |
| 242 | 1 | Value* val = c.b.build_select(ok, c.b.build_mul(ev, inv), c.f32(0.0f)); | |
| 243 | 1 | store_elem_dt(c, dt, prow, k, val); | |
| 244 | 1 | }); | |
| 245 | |||
| 246 | 1 | c.b.build_ret_void(); | |
| 247 | 1 | return finalize_fn(fn); | |
| 248 | 1 | } | |
| 249 | |||
| 250 | 1 | brass::Function* build_bw_scatter(brass::Module& mod, Dtype dt, bool has_sbits) { | |
| 251 | 1 | const std::string name = std::string("mir_fa_bw_scatter_") + (dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 252 | 1 | (has_sbits ? "_sb" : ""); | |
| 253 | 8 | std::vector<Type> params = { | |
| 254 | 3 | Type::ptr(), Type::ptr(), Type::ptr(), | |
| 255 | 5 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 256 | }; | ||
| 257 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 258 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 259 | 1 | Ctx c(kb); | |
| 260 | 1 | auto p = entry_params(c, fn); | |
| 261 | // p: 0:Y 1:Out 2:sbits | 3:ldY 4:rows 5:hd 6:ldO 7:col0 | ||
| 262 | 1 | Value* total = c.b.build_mul(p[4], p[5]); | |
| 263 | 1 | Value* idx = kb.global_tid_x(); | |
| 264 | 1 | Value* inv = has_sbits ? emit_bw_ds_scale_and_inv(c, p[2]).second : c.f32(1.0f); | |
| 265 | 2 | kb.if_then(c.b.build_slt(idx, total), [&] { | |
| 266 | 1 | Value* r = c.b.build_udiv(idx, p[5]); | |
| 267 | 1 | Value* d = c.b.build_umod(idx, p[5]); | |
| 268 | 1 | Value* yv = kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_mul(r, p[3]), d), 4)); | |
| 269 | 1 | Value* o_idx = c.b.build_add(c.b.build_add(c.b.build_mul(r, p[6]), p[7]), d); | |
| 270 | 1 | store_elem_dt(c, dt, p[1], o_idx, c.b.build_mul(yv, inv)); | |
| 271 | 1 | }); | |
| 272 | 1 | c.b.build_ret_void(); | |
| 273 | 1 | return finalize_fn(fn); | |
| 274 | 1 | } | |
| 275 | |||
| 276 | 1 | brass::Function* build_bw_acc(brass::Module& mod, bool has_sbits, bool first) { | |
| 277 | 1 | const std::string name = std::string("mir_fa_bw_acc") + (has_sbits ? "_sb" : "") + (first ? "_1st" : ""); | |
| 278 | 5 | std::vector<Type> params = { | |
| 279 | 3 | Type::ptr(), Type::ptr(), Type::ptr(), | |
| 280 | 1 | Type::i32(), | |
| 281 | 1 | Type::f32(), | |
| 282 | }; | ||
| 283 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 284 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 285 | 1 | Ctx c(kb); | |
| 286 | 1 | auto p = entry_params(c, fn); | |
| 287 | // p: 0:acc 1:src 2:sbits | 3:n | 4:inv_const | ||
| 288 | 1 | Value* inv = has_sbits ? emit_bw_ds_scale_and_inv(c, p[2]).second : p[4]; | |
| 289 | 1 | Value* idx = kb.global_tid_x(); | |
| 290 | 2 | kb.if_then(c.b.build_slt(idx, p[3]), [&] { | |
| 291 | 1 | Value* ptr_acc = ptr_at(c, p[0], idx, 4); | |
| 292 | 1 | Value* v = c.b.build_mul(kb.load_f32(ptr_at(c, p[1], idx, 4)), inv); | |
| 293 | 1 | kb.store_f32(ptr_acc, first ? v : c.b.build_add(kb.load_f32(ptr_acc), v)); | |
| 294 | 1 | }); | |
| 295 | 1 | c.b.build_ret_void(); | |
| 296 | 1 | return finalize_fn(fn); | |
| 297 | 1 | } | |
| 298 | |||
| 299 | 1 | brass::Function* build_bw_row_stats(brass::Module& mod, bool causal, bool has_mask) { | |
| 300 | 1 | const std::string name = std::string("mir_fa_bw_row_stats") + (causal ? "_c" : "") + (has_mask ? "_m" : ""); | |
| 301 | 10 | std::vector<Type> params = { | |
| 302 | 5 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 303 | 3 | Type::i32(), Type::i32(), Type::i32(), | |
| 304 | 2 | Type::f32(), Type::f32(), | |
| 305 | }; | ||
| 306 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 307 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 308 | 1 | Ctx c(kb); | |
| 309 | 1 | auto p = entry_params(c, fn); | |
| 310 | // p: 0:S 1:dP 2:mask 3:stats 4:bound_bits | 5:ld 6:Lk 7:q0 | 8:scale_log2 9:scale | ||
| 311 | 1 | Value* s8 = kb.shared_alloc_f32(8); | |
| 312 | 1 | Value* i = kb.ctaid_x(); | |
| 313 | 1 | Value* row_off = c.b.build_mul(i, p[5]); | |
| 314 | 1 | Value* s = ptr_at(c, p[0], row_off, 4); | |
| 315 | 1 | Value* dp = ptr_at(c, p[1], row_off, 4); | |
| 316 | 1 | Value* qa = c.b.build_add(p[7], i); | |
| 317 | 1 | Value* kend = causal ? smin_i32(c, p[6], c.b.build_add(qa, c.i32(1))) : p[6]; | |
| 318 | 1 | Value* tid = kb.tid_x(); | |
| 319 | |||
| 320 | 1 | Value* local_mx = kb.for_range_reduce( | |
| 321 | 1 | tid, kend, c.i32(256), c.f32(-1e30f), | |
| 322 | 2 | [&](Value* k, Value* cur) -> Value* { | |
| 323 | 1 | Value* sv = c.b.build_mul(kb.load_f32(ptr_at(c, s, k, 4)), p[8]); | |
| 324 | 1 | if (!has_mask) return kb.fmax(cur, sv); | |
| 325 | ✗ | Value* ok = c.b.build_sgt(kb.load_f32(ptr_at(c, p[2], k, 4)), c.f32(0.5f)); | |
| 326 | ✗ | return c.b.build_select(ok, kb.fmax(cur, sv), cur); | |
| 327 | 1 | }); | |
| 328 | 1 | Value* mx = emit_block_max_f32(c, s8, local_mx); | |
| 329 | 1 | Value* has_mx = c.b.build_sgt(mx, c.f32(-1e29f)); | |
| 330 | |||
| 331 | 1 | auto acc3 = emit_if_else_values( | |
| 332 | 1 | c, has_mx, {Type::f32(), Type::f32(), Type::f32()}, | |
| 333 | 2 | [&]() -> std::vector<Value*> { | |
| 334 | 1 | return kb.for_range_reduce_n( | |
| 335 | 1 | tid, kend, c.i32(256), {c.f32(0.0f), c.f32(0.0f), c.f32(0.0f)}, | |
| 336 | 2 | [&](Value* k, const std::vector<Value*>& cur) -> std::vector<Value*> { | |
| 337 | 1 | Value* ok = has_mask ? c.b.build_sgt(kb.load_f32(ptr_at(c, p[2], k, 4)), c.f32(0.5f)) | |
| 338 | 1 | : c.i32(1); | |
| 339 | 1 | Value* sv = kb.load_f32(ptr_at(c, s, k, 4)); | |
| 340 | 1 | Value* dpv = kb.load_f32(ptr_at(c, dp, k, 4)); | |
| 341 | 1 | Value* ev = kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[8]), mx)); | |
| 342 | 1 | if (!has_mask) { | |
| 343 | 3 | return { | |
| 344 | 1 | c.b.build_add(cur[0], ev), | |
| 345 | 1 | c.b.build_fma_f32(ev, dpv, cur[1]), | |
| 346 | 1 | kb.fmax(cur[2], kb.fabs(dpv)), | |
| 347 | }; | ||
| 348 | } | ||
| 349 | ✗ | Value* b = c.b.build_ne(ok, c.i32(0)); | |
| 350 | ✗ | return { | |
| 351 | ✗ | c.b.build_select(b, c.b.build_add(cur[0], ev), cur[0]), | |
| 352 | ✗ | c.b.build_select(b, c.b.build_fma_f32(ev, dpv, cur[1]), cur[1]), | |
| 353 | ✗ | c.b.build_select(b, kb.fmax(cur[2], kb.fabs(dpv)), cur[2]), | |
| 354 | }; | ||
| 355 | 1 | }); | |
| 356 | ✗ | }, | |
| 357 | 2 | [&]() -> std::vector<Value*> { return {c.f32(0.0f), c.f32(0.0f), c.f32(0.0f)}; }); | |
| 358 | |||
| 359 | 1 | Value* l = emit_block_sum_f32(c, s8, acc3[0]); | |
| 360 | 1 | Value* a = emit_block_sum_f32(c, s8, acc3[1]); | |
| 361 | 1 | Value* g = emit_block_max_f32(c, s8, acc3[2]); | |
| 362 | |||
| 363 | 2 | kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { | |
| 364 | 1 | Value* any = c.b.build_sgt(l, c.f32(0.0f)); | |
| 365 | 1 | Value* dq = c.b.build_select(any, emit_div_rn(c, a, l), c.f32(0.0f)); | |
| 366 | 1 | Value* inv_l = c.b.build_select(any, emit_div_rn(c, c.f32(1.0f), l), c.f32(0.0f)); | |
| 367 | 1 | Value* i3 = c.b.build_mul(i, c.i32(3)); | |
| 368 | 1 | kb.store_f32(ptr_at(c, p[3], i3, 4), c.b.build_select(any, mx, c.f32(0.0f))); | |
| 369 | 1 | kb.store_f32(ptr_at(c, p[3], c.b.build_add(i3, c.i32(1)), 4), inv_l); | |
| 370 | 1 | kb.store_f32(ptr_at(c, p[3], c.b.build_add(i3, c.i32(2)), 4), dq); | |
| 371 | |||
| 372 | 1 | Value* bval = c.b.build_select(any, c.b.build_mul(p[9], c.b.build_add(g, kb.fabs(dq))), c.f32(0.0f)); | |
| 373 | 1 | Value* bu = c.b.build_bitcast_i32_f32(bval); | |
| 374 | 1 | Value* fin = c.b.build_ult(c.b.build_and(bu, c.i32(0x7f800000)), c.i32(0x7f800000)); | |
| 375 | 2 | kb.if_then(fin, [&] { kb.atom_max_u32(p[4], bu); }); | |
| 376 | 1 | }); | |
| 377 | |||
| 378 | 1 | c.b.build_ret_void(); | |
| 379 | 1 | return finalize_fn(fn); | |
| 380 | 1 | } | |
| 381 | |||
| 382 | 1 | brass::Function* build_bw_ds_tile(brass::Module& mod, Dtype dt, bool causal, bool has_mask) { | |
| 383 | 2 | const std::string name = std::string("mir_fa_bw_ds_") + (dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 384 | 1 | (causal ? "_c" : "") + (has_mask ? "_m" : ""); | |
| 385 | 16 | std::vector<Type> params = { | |
| 386 | 5 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 387 | 3 | Type::ptr(), Type::ptr(), Type::ptr(), | |
| 388 | 6 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 389 | 2 | Type::f32(), Type::f32(), | |
| 390 | }; | ||
| 391 | 1 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 392 | 1 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 393 | 1 | Ctx c(kb); | |
| 394 | 1 | auto p = entry_params(c, fn); | |
| 395 | // p: 0:S 1:dP 2:stats 3:sbits 4:mask 5:dS 6:dST 7:PT | | ||
| 396 | // 8:ldS 9:m 10:m_pad 11:Lk 12:Lk_pad 13:q0 | 14:scale_log2 15:scale | ||
| 397 | 1 | Value* tp = kb.shared_alloc_f32(32 * 33); | |
| 398 | 1 | Value* tds = kb.shared_alloc_f32(32 * 33); | |
| 399 | |||
| 400 | 1 | Value* j0 = c.b.build_shl(kb.ctaid_x(), c.i32(5)); | |
| 401 | 1 | Value* i0 = c.b.build_shl(kb.ctaid_y(), c.i32(5)); | |
| 402 | 1 | Value* tid = kb.tid_x(); | |
| 403 | 1 | Value* tx = c.b.build_and(tid, c.i32(31)); | |
| 404 | 1 | Value* ty = c.b.build_lshr(tid, c.i32(5)); | |
| 405 | |||
| 406 | 1 | Value* j = c.b.build_add(j0, tx); | |
| 407 | 1 | Value* in_lk = c.b.build_slt(j, p[11]); | |
| 408 | 1 | Value* jon = in_lk; | |
| 409 | 1 | if (has_mask) { | |
| 410 | ✗ | Value* safe_j = c.b.build_select(in_lk, j, c.i32(0)); | |
| 411 | ✗ | jon = c.b.build_and(in_lk, c.b.build_sgt(kb.load_f32(ptr_at(c, p[4], safe_j, 4)), c.f32(0.5f))); | |
| 412 | ✗ | } | |
| 413 | 1 | Value* dsc = c.b.build_mul(emit_bw_ds_scale_and_inv(c, p[3]).first, p[15]); | |
| 414 | 1 | Value* lkp2 = c.b.build_shl(p[12], c.i32(1)); | |
| 415 | 1 | Value* mp2 = c.b.build_shl(p[10], c.i32(1)); | |
| 416 | |||
| 417 | 5 | for (int step = 0; step < 4; ++step) { | |
| 418 | 4 | Value* r = c.b.build_add(ty, c.i32(step * 8)); | |
| 419 | 4 | Value* i = c.b.build_add(i0, r); | |
| 420 | 4 | Value* in_m = c.b.build_slt(i, p[9]); | |
| 421 | 4 | Value* attend = c.b.build_and(in_m, jon); | |
| 422 | 4 | if (causal) { | |
| 423 | 4 | attend = c.b.build_and(attend, c.b.build_sle(j, c.b.build_add(p[13], i))); | |
| 424 | 4 | } | |
| 425 | 4 | auto pds = emit_if_else_values( | |
| 426 | 4 | c, attend, {Type::f32(), Type::f32()}, | |
| 427 | 8 | [&]() -> std::vector<Value*> { | |
| 428 | 4 | Value* i3 = c.b.build_mul(i, c.i32(3)); | |
| 429 | 4 | Value* st0 = kb.load_f32(ptr_at(c, p[2], i3, 4)); | |
| 430 | 4 | Value* st1 = kb.load_f32(ptr_at(c, p[2], c.b.build_add(i3, c.i32(1)), 4)); | |
| 431 | 4 | Value* st2 = kb.load_f32(ptr_at(c, p[2], c.b.build_add(i3, c.i32(2)), 4)); | |
| 432 | 4 | Value* pos = c.b.build_sgt(st1, c.f32(0.0f)); | |
| 433 | 4 | Value* o = c.b.build_add(c.b.build_mul(i, p[8]), j); | |
| 434 | 4 | Value* sv = kb.load_f32(ptr_at(c, p[0], o, 4)); | |
| 435 | 4 | Value* dpv = kb.load_f32(ptr_at(c, p[1], o, 4)); | |
| 436 | 4 | Value* pv = c.b.build_mul(kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[14]), st0)), st1); | |
| 437 | 4 | Value* dsv = c.b.build_mul(c.b.build_mul(pv, c.b.build_sub(dpv, st2)), dsc); | |
| 438 | 8 | return { | |
| 439 | 4 | c.b.build_select(pos, pv, c.f32(0.0f)), | |
| 440 | 4 | c.b.build_select(pos, dsv, c.f32(0.0f)), | |
| 441 | }; | ||
| 442 | }, | ||
| 443 | 8 | [&]() -> std::vector<Value*> { return {c.f32(0.0f), c.f32(0.0f)}; }); | |
| 444 | |||
| 445 | 8 | kb.if_then(c.b.build_and(in_m, c.b.build_slt(j, p[12])), [&] { | |
| 446 | 4 | Value* ds = pds[1]; | |
| 447 | 4 | Value* hi_raw = encode_half_raw(c, dt, ds); | |
| 448 | 4 | Value* hi_f = decode_half_raw(c, dt, hi_raw); | |
| 449 | 4 | Value* lo_raw = encode_half_raw(c, dt, c.b.build_sub(ds, hi_f)); | |
| 450 | 4 | Value* ds_idx = c.b.build_add(c.b.build_mul(i, lkp2), j); | |
| 451 | 4 | kb.store_u16(ptr_at(c, p[5], ds_idx, 2), hi_raw); | |
| 452 | 4 | kb.store_u16(ptr_at(c, p[5], c.b.build_add(ds_idx, p[12]), 2), lo_raw); | |
| 453 | 4 | }); | |
| 454 | 4 | Value* sm_idx = c.b.build_add(c.b.build_mul(r, c.i32(33)), tx); | |
| 455 | 4 | kb.shared_store_f32_indexed(tp, sm_idx, pds[0]); | |
| 456 | 4 | kb.shared_store_f32_indexed(tds, sm_idx, pds[1]); | |
| 457 | 4 | } | |
| 458 | 1 | kb.sync(); | |
| 459 | |||
| 460 | 5 | for (int step = 0; step < 4; ++step) { | |
| 461 | 4 | Value* r = c.b.build_add(ty, c.i32(step * 8)); | |
| 462 | 4 | Value* jj = c.b.build_add(j0, r); | |
| 463 | 4 | Value* ii = c.b.build_add(i0, tx); | |
| 464 | 8 | kb.if_then(c.b.build_and(c.b.build_slt(jj, p[12]), c.b.build_slt(ii, p[10])), [&] { | |
| 465 | 4 | Value* sm_idx = c.b.build_add(c.b.build_mul(tx, c.i32(33)), r); | |
| 466 | 4 | Value* pv = kb.shared_load_f32_indexed(tp, sm_idx); | |
| 467 | 4 | Value* ds = kb.shared_load_f32_indexed(tds, sm_idx); | |
| 468 | 8 | kb.store_u16( | |
| 469 | 4 | ptr_at(c, p[7], c.b.build_add(c.b.build_mul(jj, p[10]), ii), 2), | |
| 470 | 4 | encode_half_raw(c, dt, c.b.build_mul(pv, c.f32(kPScale)))); | |
| 471 | 4 | Value* hi_raw = encode_half_raw(c, dt, ds); | |
| 472 | 4 | Value* hi_f = decode_half_raw(c, dt, hi_raw); | |
| 473 | 4 | Value* lo_raw = encode_half_raw(c, dt, c.b.build_sub(ds, hi_f)); | |
| 474 | 4 | Value* dst_idx = c.b.build_add(c.b.build_mul(jj, mp2), ii); | |
| 475 | 4 | kb.store_u16(ptr_at(c, p[6], dst_idx, 2), hi_raw); | |
| 476 | 4 | kb.store_u16(ptr_at(c, p[6], c.b.build_add(dst_idx, p[10]), 2), lo_raw); | |
| 477 | 4 | }); | |
| 478 | 4 | } | |
| 479 | |||
| 480 | 1 | c.b.build_ret_void(); | |
| 481 | 1 | return finalize_fn(fn); | |
| 482 | 1 | } | |
| 483 | |||
| 484 | } // namespace | ||
| 485 | |||
| 486 | 9 | brass::Function* build_fa_aux_mir(brass::Module& mod, std::uint32_t op, Dtype dt, std::uint8_t flags) { | |
| 487 | 9 | switch (op) { | |
| 488 | 1 | case 0u: return build_split_combine(mod, dt); | |
| 489 | 1 | case 1u: return build_gather(mod, dt); | |
| 490 | 1 | case 2u: return build_gather_T(mod, dt, (flags & 1u) != 0); | |
| 491 | 1 | case 3u: return build_scatter_head(mod, dt); | |
| 492 | 1 | case 4u: return build_softmax_rows_f32(mod, dt, (flags & 1u) != 0); | |
| 493 | 1 | case 5u: return build_bw_scatter(mod, dt, (flags & 1u) != 0); | |
| 494 | 1 | case 6u: return build_bw_acc(mod, (flags & 1u) != 0, (flags & 2u) != 0); | |
| 495 | 1 | case 7u: return build_bw_row_stats(mod, (flags & 1u) != 0, (flags & 2u) != 0); | |
| 496 | 1 | case 8u: return build_bw_ds_tile(mod, dt, (flags & 1u) != 0, (flags & 2u) != 0); | |
| 497 | ✗ | default: throw std::runtime_error("build_fa_aux_mir: unsupported op"); | |
| 498 | } | ||
| 499 | 9 | } | |
| 500 | |||
| 501 | } // namespace brotensor::kernels | ||
| 502 |