src/kernels/mir_fa_bwd_cm.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_fa_bwd_cm.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | #include "mir_coop_common.h" | ||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <array> | ||
| 9 | #include <stdexcept> | ||
| 10 | #include <string> | ||
| 11 | #include <utility> | ||
| 12 | #include <vector> | ||
| 13 | |||
| 14 | namespace brotensor::kernels { | ||
| 15 | |||
| 16 | namespace { | ||
| 17 | |||
| 18 | 9 | std::pair<Value*, Value*> emit_bw_cm_interval(Ctx& c, std::uint32_t mode, Value* qi, | |
| 19 | Value* lq, Value* lk, Value* window, Value* causal, | ||
| 20 | Value* nseq, Value* aux, Value* aux2) { | ||
| 21 | 9 | auto res = emit_if_else_values( | |
| 22 | 9 | c, c.b.build_ult(qi, lq), {Type::i32(), Type::i32()}, | |
| 23 | 18 | [&]() -> std::vector<Value*> { | |
| 24 | 9 | Value* a = c.i32(0); | |
| 25 | 9 | Value* b = lk; | |
| 26 | 9 | if (mode == FA_MODE_ROWS) { | |
| 27 | 9 | Value* is_c = c.b.build_ne(causal, c.i32(0)); | |
| 28 | 9 | Value* has_w = c.b.build_ugt(window, c.i32(0)); | |
| 29 | 9 | Value* b_c = smin_i32(c, lk, c.b.build_add(qi, c.i32(1))); | |
| 30 | 9 | Value* a_cw = smax_i32(c, c.i32(0), c.b.build_add(c.b.build_sub(qi, window), c.i32(1))); | |
| 31 | 9 | Value* a_c = c.b.build_select(has_w, a_cw, c.i32(0)); | |
| 32 | 9 | Value* w2 = c.b.build_lshr(window, c.i32(1)); | |
| 33 | 9 | Value* a_nc = c.b.build_select(has_w, smax_i32(c, c.i32(0), c.b.build_sub(qi, w2)), c.i32(0)); | |
| 34 | 9 | Value* b_nc = c.b.build_select(has_w, smin_i32(c, lk, c.b.build_add(c.b.build_add(qi, w2), c.i32(1))), lk); | |
| 35 | 9 | a = c.b.build_select(is_c, a_c, a_nc); | |
| 36 | 9 | b = c.b.build_select(is_c, b_c, b_nc); | |
| 37 | 9 | } else if (mode == FA_MODE_VARLEN) { | |
| 38 | ✗ | Value* valid = emit_if_else_values( | |
| 39 | ✗ | c, c.b.build_ugt(nseq, c.i32(0)), {Type::i32()}, | |
| 40 | ✗ | [&]() -> std::vector<Value*> { | |
| 41 | ✗ | return {c.b.build_slt(qi, c.kb.load_i32(ptr_at(c, aux, nseq, 4)))}; | |
| 42 | }, | ||
| 43 | ✗ | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 44 | ✗ | auto ab = emit_if_else_values( | |
| 45 | ✗ | c, valid, {Type::i32(), Type::i32()}, | |
| 46 | ✗ | [&]() -> std::vector<Value*> { | |
| 47 | ✗ | auto bs = c.kb.for_range_reduce_n( | |
| 48 | ✗ | c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq}, | |
| 49 | ✗ | [&](Value*, const std::vector<Value*>& st) -> std::vector<Value*> { | |
| 50 | ✗ | Value* lo = st[0]; | |
| 51 | ✗ | Value* hi = st[1]; | |
| 52 | ✗ | Value* gt1 = c.b.build_ugt(c.b.build_sub(hi, lo), c.i32(1)); | |
| 53 | ✗ | Value* mid = c.b.build_lshr(c.b.build_add(lo, hi), c.i32(1)); | |
| 54 | ✗ | Value* le = c.b.build_sle(c.kb.load_i32(ptr_at(c, aux, mid, 4)), qi); | |
| 55 | ✗ | Value* nlo = c.b.build_select(c.b.build_and(gt1, le), mid, lo); | |
| 56 | ✗ | Value* nhi = c.b.build_select(c.b.build_and(gt1, c.b.build_xor(le, c.i32(1))), mid, hi); | |
| 57 | ✗ | return {nlo, nhi}; | |
| 58 | }); | ||
| 59 | ✗ | Value* s0 = bs[0]; | |
| 60 | ✗ | Value* ka = c.kb.load_i32(ptr_at(c, aux2, s0, 4)); | |
| 61 | ✗ | Value* kb_v = c.kb.load_i32(ptr_at(c, aux2, c.b.build_add(s0, c.i32(1)), 4)); | |
| 62 | ✗ | Value* qstart = c.kb.load_i32(ptr_at(c, aux, s0, 4)); | |
| 63 | ✗ | Value* kb_c = smin_i32(c, kb_v, c.b.build_add(c.b.build_add(ka, c.b.build_sub(qi, qstart)), c.i32(1))); | |
| 64 | ✗ | return {ka, c.b.build_select(c.b.build_ne(causal, c.i32(0)), kb_c, kb_v)}; | |
| 65 | ✗ | }, | |
| 66 | ✗ | [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; }); | |
| 67 | ✗ | a = ab[0]; | |
| 68 | ✗ | b = ab[1]; | |
| 69 | ✗ | } else if (mode == FA_MODE_PACKED) { | |
| 70 | ✗ | Value* q2 = c.b.build_shl(qi, c.i32(1)); | |
| 71 | ✗ | a = c.kb.load_i32(ptr_at(c, aux, q2, 4)); | |
| 72 | ✗ | b = c.kb.load_i32(ptr_at(c, aux, c.b.build_add(q2, c.i32(1)), 4)); | |
| 73 | ✗ | Value* has_w = c.b.build_ugt(window, c.i32(0)); | |
| 74 | ✗ | Value* w2 = c.b.build_lshr(window, c.i32(1)); | |
| 75 | ✗ | a = c.b.build_select(has_w, smax_i32(c, a, c.b.build_sub(qi, w2)), a); | |
| 76 | ✗ | b = c.b.build_select(has_w, smin_i32(c, b, c.b.build_add(c.b.build_add(qi, w2), c.i32(1))), b); | |
| 77 | ✗ | } | |
| 78 | 9 | a = clamp_i32(c, a, c.i32(0), lk); | |
| 79 | 9 | b = clamp_i32(c, b, a, lk); | |
| 80 | 9 | return {a, b}; | |
| 81 | ✗ | }, | |
| 82 | 18 | [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; }); | |
| 83 | 9 | return {res[0], res[1]}; | |
| 84 | 9 | } | |
| 85 | |||
| 86 | 3 | std::pair<Value*, Value*> emit_bw_cm_qrange(Ctx& c, std::uint32_t mode, Value* k0, Value* k1, | |
| 87 | Value* lq, Value* window, Value* causal, Value* nseq, | ||
| 88 | Value* aux, Value* aux2) { | ||
| 89 | 3 | if (mode == FA_MODE_ROWS) { | |
| 90 | 3 | Value* is_c = c.b.build_ne(causal, c.i32(0)); | |
| 91 | 3 | Value* has_w = c.b.build_ugt(window, c.i32(0)); | |
| 92 | 3 | Value* qhi_cw = smin_i32(c, lq, c.b.build_add(k1, window)); | |
| 93 | 3 | Value* w2 = c.b.build_lshr(window, c.i32(1)); | |
| 94 | 3 | Value* qlo_nc = c.b.build_select(has_w, smax_i32(c, c.i32(0), c.b.build_sub(k0, w2)), c.i32(0)); | |
| 95 | 3 | Value* qhi_nc = c.b.build_select(has_w, smin_i32(c, lq, c.b.build_add(c.b.build_add(k1, w2), c.i32(1))), lq); | |
| 96 | 6 | return {c.b.build_select(is_c, k0, qlo_nc), | |
| 97 | 3 | c.b.build_select(is_c, c.b.build_select(has_w, qhi_cw, lq), qhi_nc)}; | |
| 98 | } | ||
| 99 | ✗ | if (mode == FA_MODE_VARLEN) { | |
| 100 | ✗ | auto find_seq = [&](Value* key) -> Value* { | |
| 101 | ✗ | auto bs = c.kb.for_range_reduce_n( | |
| 102 | ✗ | c.i32(0), c.i32(24), c.i32(1), {c.i32(0), nseq}, | |
| 103 | ✗ | [&](Value*, const std::vector<Value*>& st) -> std::vector<Value*> { | |
| 104 | ✗ | Value* lo = st[0]; | |
| 105 | ✗ | Value* hi = st[1]; | |
| 106 | ✗ | Value* gt1 = c.b.build_ugt(c.b.build_sub(hi, lo), c.i32(1)); | |
| 107 | ✗ | Value* mid = c.b.build_lshr(c.b.build_add(lo, hi), c.i32(1)); | |
| 108 | ✗ | Value* le = c.b.build_sle(c.kb.load_i32(ptr_at(c, aux2, mid, 4)), key); | |
| 109 | ✗ | Value* nlo = c.b.build_select(c.b.build_and(gt1, le), mid, lo); | |
| 110 | ✗ | Value* nhi = c.b.build_select(c.b.build_and(gt1, c.b.build_xor(le, c.i32(1))), mid, hi); | |
| 111 | ✗ | return {nlo, nhi}; | |
| 112 | }); | ||
| 113 | ✗ | return bs[0]; | |
| 114 | ✗ | }; | |
| 115 | ✗ | auto res = emit_if_else_values( | |
| 116 | ✗ | c, c.b.build_ugt(nseq, c.i32(0)), {Type::i32(), Type::i32()}, | |
| 117 | ✗ | [&]() -> std::vector<Value*> { | |
| 118 | ✗ | Value* sa = find_seq(k0); | |
| 119 | ✗ | Value* sb = find_seq(k1); | |
| 120 | ✗ | Value* qlo = clamp_i32(c, c.kb.load_i32(ptr_at(c, aux, sa, 4)), c.i32(0), lq); | |
| 121 | ✗ | Value* qhi = clamp_i32(c, c.kb.load_i32(ptr_at(c, aux, c.b.build_add(sb, c.i32(1)), 4)), qlo, lq); | |
| 122 | ✗ | return {qlo, qhi}; | |
| 123 | }, | ||
| 124 | ✗ | [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; }); | |
| 125 | ✗ | return {res[0], res[1]}; | |
| 126 | ✗ | } | |
| 127 | ✗ | return {c.i32(0), lq}; | |
| 128 | 3 | } | |
| 129 | |||
| 130 | 108 | Value* load8_bwd(Ctx& c, Dtype dt, std::uint32_t hd, bool vec, Value* base, Value* off, Value* ok, Value* col_v4) { | |
| 131 | 108 | if (vec) { | |
| 132 | 108 | Value* idx = c.b.build_select(ok, c.b.build_add(off, c.b.build_shl(col_v4, c.i32(3))), c.i32(0)); | |
| 133 | 108 | Value* w = c.kb.vload(Type::i32x4(), ptr_at(c, base, idx, 2)); | |
| 134 | 108 | if (dt == Dtype::BF16) w = emit_bf16x8_to_f16x8(c, w); | |
| 135 | 108 | return emit_v4_select(c, ok, w, emit_v4_zero(c)); | |
| 136 | } | ||
| 137 | ✗ | return emit_if_else_values( | |
| 138 | ✗ | c, ok, {Type::i32x4()}, | |
| 139 | ✗ | [&]() -> std::vector<Value*> { | |
| 140 | ✗ | Value* c0 = c.b.build_shl(col_v4, c.i32(3)); | |
| 141 | ✗ | std::array<Value*, 8> h{}; | |
| 142 | ✗ | for (int e = 0; e < 8; ++e) { | |
| 143 | ✗ | Value* ce = c.b.build_add(c0, c.i32(e)); | |
| 144 | ✗ | h[static_cast<std::size_t>(e)] = emit_if_else_values( | |
| 145 | ✗ | c, c.b.build_ult(ce, c.i32(static_cast<int>(hd))), {Type::f32()}, | |
| 146 | ✗ | [&]() -> std::vector<Value*> { return {load_elem_dt(c, dt, base, c.b.build_add(off, ce))}; }, | |
| 147 | ✗ | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 148 | ✗ | } | |
| 149 | ✗ | return {emit_pack_f32x8_to_f16_v4(c, h)}; | |
| 150 | ✗ | }, | |
| 151 | ✗ | [&]() -> std::vector<Value*> { return {emit_v4_zero(c)}; })[0]; | |
| 152 | 108 | } | |
| 153 | |||
| 154 | } // namespace | ||
| 155 | |||
| 156 | 9 | brass::Function* build_fa_bwd_cm_mir(brass::Module& mod, const FaBwdCmSpec& s) { | |
| 157 | 9 | if ((s.nsg != 2u && s.nsg != 4u) || (s.bn != 16u && s.bn != 32u) || s.pass > 2u) { | |
| 158 | ✗ | throw std::runtime_error("brotensor: mir_fa_bwd_cm: invalid geometry/pass"); | |
| 159 | } | ||
| 160 | 9 | const std::uint32_t HD = s.hd; | |
| 161 | 9 | const std::uint32_t DP = ((HD + 15u) / 16u) * 16u; | |
| 162 | 9 | const std::uint32_t ND = DP / 16u; | |
| 163 | 9 | const std::uint32_t DV = DP / 8u; | |
| 164 | 9 | const std::uint32_t DVR = (HD + 7u) / 8u; | |
| 165 | 9 | const std::uint32_t SD = DV + 1u; | |
| 166 | 9 | const std::uint32_t BO = s.nsg * 16u; | |
| 167 | 9 | const std::uint32_t WG = s.nsg * 32u; | |
| 168 | 9 | const std::uint32_t NJ = s.bn / 16u; | |
| 169 | 9 | const std::uint32_t HS = s.bn + 8u; | |
| 170 | 9 | const std::uint32_t HS_V4 = HS / 8u; | |
| 171 | 9 | const std::uint32_t FS = s.bn + 4u; | |
| 172 | 9 | const std::uint32_t KVO = s.areg ? 0u : (2u * BO * SD); | |
| 173 | 9 | const std::uint32_t NSM = 2u * (s.areg ? std::max(BO, s.bn) : (BO + s.bn)) * SD; | |
| 174 | 9 | const std::uint32_t NSF = (s.pass == 0u) ? std::max(256u * s.nsg, 2u * BO * FS) : (256u * s.nsg); | |
| 175 | 9 | const std::uint32_t NSH_V4 = (s.pass == 0u) ? 1u : ((s.pass == 1u ? 2u : 1u) * BO * HS_V4); | |
| 176 | |||
| 177 | 18 | const std::string name = std::string("mir_fa_bwd_cm_") + (s.dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 178 | 9 | "_p" + std::to_string(s.pass) + "_hd" + std::to_string(HD) + | |
| 179 | 9 | "_g" + std::to_string(s.nsg) + "_bn" + std::to_string(s.bn) + | |
| 180 | 18 | "_m" + std::to_string(s.mode) + (s.has_mask ? "_mk" : "") + | |
| 181 | 9 | (s.vec ? "_v" : "_s") + (s.areg ? "_ar" : "") + (s.agnostic ? "_ag" : ""); | |
| 182 | |||
| 183 | 9 | std::vector<Type> params = { | |
| 184 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | ||
| 185 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | ||
| 186 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | ||
| 187 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), | ||
| 188 | Type::f32(), Type::f32(), | ||
| 189 | }; | ||
| 190 | 9 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 191 | 9 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 192 | 9 | Ctx c(kb); | |
| 193 | 9 | auto p = entry_params(c, fn); | |
| 194 | // ptrs: 0:q 1:k 2:v 3:g 4:mask 5:aux 6:aux2 7:stats 8:o0 9:o1 | ||
| 195 | // ints: 10:lq 11:lk 12:ldq 13:ldk 14:ldg 15:ldo 16:window 17:causal 18:nseq | ||
| 196 | // flts: 19:scale 20:oscale | ||
| 197 | |||
| 198 | 9 | Value* SM = kb.shared_alloc_i32x4(NSM); | |
| 199 | 9 | Value* SF = kb.shared_alloc_f32(NSF); | |
| 200 | 9 | Value* SH = (s.pass != 0u) ? kb.shared_alloc_i32x4(NSH_V4) : nullptr; | |
| 201 | 9 | Value* ilo = kb.shared_alloc_i32(BO + s.bn); | |
| 202 | 9 | Value* ihi = kb.shared_alloc_i32(BO + s.bn); | |
| 203 | 9 | Value* ist_x = kb.shared_alloc_f32(BO + s.bn); | |
| 204 | 9 | Value* ist_y = kb.shared_alloc_f32(BO + s.bn); | |
| 205 | 9 | Value* kok = kb.shared_alloc_f32(BO + s.bn); | |
| 206 | 9 | Value* s_lo = (s.pass != 1u) ? kb.shared_alloc_i32(WG) : nullptr; | |
| 207 | 9 | Value* s_hi = (s.pass != 1u) ? kb.shared_alloc_i32(WG) : nullptr; | |
| 208 | 9 | Value* sflag = (s.pass == 1u) ? kb.shared_alloc_i32(s.nsg) : nullptr; | |
| 209 | 9 | Value* s_fast = (!s.agnostic) ? kb.shared_alloc_i32(1) : nullptr; | |
| 210 | |||
| 211 | 9 | Value* tid = c.b.build_and(kb.tid_x(), c.i32(static_cast<int>(WG - 1u))); | |
| 212 | 9 | Value* sg = c.b.build_and(kb.subgroup_id(), c.i32(static_cast<int>(s.nsg - 1u))); | |
| 213 | 9 | Value* lane = c.b.build_and(kb.lane_id(), c.i32(31)); | |
| 214 | 9 | Value* cl = c.b.build_and(lane, c.i32(15)); | |
| 215 | 9 | Value* h = kb.ctaid_y(); | |
| 216 | 9 | Value* col_h = c.b.build_mul(h, c.i32(static_cast<int>(HD))); | |
| 217 | 9 | Value* stride_sd = c.i32(static_cast<int>(SD * 8u)); | |
| 218 | 9 | Value* stride_hs = c.i32(static_cast<int>(HS)); | |
| 219 | |||
| 220 | 45 | auto stage = [&](std::uint32_t dst_v4, std::uint32_t nrows, Value* base, Value* ld, Value* row0, Value* rowend) { | |
| 221 | 36 | const std::uint32_t tot = nrows * DV; | |
| 222 | 144 | for (std::uint32_t i0 = 0; i0 < tot; i0 += WG) { | |
| 223 | 108 | Value* i = (i0 == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(i0))); | |
| 224 | 216 | auto body = [&] { | |
| 225 | 108 | Value* rr = c.b.build_udiv(i, c.i32(static_cast<int>(DV))); | |
| 226 | 108 | Value* cc = c.b.build_umod(i, c.i32(static_cast<int>(DV))); | |
| 227 | 108 | Value* row = c.b.build_add(row0, rr); | |
| 228 | 108 | Value* ok = c.b.build_and(c.b.build_ult(row, rowend), c.b.build_ult(cc, c.i32(static_cast<int>(DVR)))); | |
| 229 | 108 | Value* v = load8_bwd(c, s.dt, HD, s.vec, base, c.b.build_add(c.b.build_mul(row, ld), col_h), ok, cc); | |
| 230 | 216 | Value* at = c.b.build_add(c.i32(static_cast<int>(dst_v4)), | |
| 231 | 108 | c.b.build_add(c.b.build_mul(rr, c.i32(static_cast<int>(SD))), cc)); | |
| 232 | 108 | kb.shared_store_i32x4_indexed(SM, at, v); | |
| 233 | 108 | }; | |
| 234 | 108 | if (i0 + WG <= tot) body(); | |
| 235 | ✗ | else kb.if_then(c.b.build_ult(i, c.i32(static_cast<int>(tot))), body); | |
| 236 | 108 | } | |
| 237 | 36 | }; | |
| 238 | |||
| 239 | 18 | auto key_ok_f32 = [&](Value* key) -> Value* { | |
| 240 | 9 | if (!s.has_mask) return c.b.build_select(c.b.build_ult(key, p[11]), c.f32(1.0f), c.f32(0.0f)); | |
| 241 | 9 | return emit_if_else_values( | |
| 242 | 9 | c, c.b.build_ult(key, p[11]), {Type::f32()}, | |
| 243 | 18 | [&]() -> std::vector<Value*> { | |
| 244 | 9 | Value* mv = kb.load_f32(ptr_at(c, p[4], key, 4)); | |
| 245 | 9 | return {c.b.build_select(c.b.build_sgt(mv, c.f32(0.5f)), c.f32(1.0f), c.f32(0.0f))}; | |
| 246 | }, | ||
| 247 | 18 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 248 | 9 | }; | |
| 249 | |||
| 250 | 18 | auto stat_at = [&](Value* q) -> Value* { | |
| 251 | 9 | return c.b.build_shl(c.b.build_add(c.b.build_mul(h, p[10]), q), c.i32(1)); | |
| 252 | }; | ||
| 253 | |||
| 254 | 45 | auto store_tile = [&](Value* a_frag, Value* sbase_f, Value* out_base, Value* row0, Value* rowend, | |
| 255 | std::uint32_t c0, Value* fac) { | ||
| 256 | 36 | kb.coop_store_acc(a_frag, c.b.build_add(SF, c.b.build_shl(sbase_f, c.i32(2))), c.i32(16), false, false); | |
| 257 | 36 | kb.subgroup_barrier(); | |
| 258 | 324 | for (int e0 = 0; e0 < 256; e0 += 32) { | |
| 259 | 288 | Value* e = (e0 == 0) ? lane : c.b.build_add(lane, c.i32(e0)); | |
| 260 | 288 | Value* row = c.b.build_add(row0, c.b.build_lshr(e, c.i32(4))); | |
| 261 | 288 | Value* col = c.b.build_add(c.i32(static_cast<int>(c0)), c.b.build_and(e, c.i32(15))); | |
| 262 | 576 | kb.if_then(c.b.build_and(c.b.build_ult(row, rowend), c.b.build_ult(col, c.i32(static_cast<int>(HD)))), [&] { | |
| 263 | 288 | Value* val = c.b.build_mul(kb.shared_load_f32_indexed(SF, c.b.build_add(sbase_f, e)), fac); | |
| 264 | 288 | store_elem_dt(c, s.dt, out_base, c.b.build_add(c.b.build_add(c.b.build_mul(row, p[15]), col_h), col), val); | |
| 265 | 288 | }); | |
| 266 | 288 | } | |
| 267 | 36 | kb.subgroup_barrier(); | |
| 268 | 36 | }; | |
| 269 | |||
| 270 | // Populate IX table SF[0..255] = float(0..255). | ||
| 271 | 45 | for (std::uint32_t e0 = 0; e0 < 256u; e0 += WG) { | |
| 272 | 36 | Value* e = (e0 == 0) ? tid : c.b.build_add(tid, c.i32(static_cast<int>(e0))); | |
| 273 | 36 | kb.shared_store_f32_indexed(SF, e, kb.u32_to_f32(e)); | |
| 274 | 36 | } | |
| 275 | |||
| 276 | 18 | auto check_cluster_layout = [&](Value* IX) -> Value* { | |
| 277 | 9 | if (s.agnostic) return c.i32(0); | |
| 278 | 9 | Value* len_ok = c.b.build_eq(kb.coop_acc_len(IX), c.i32(8)); | |
| 279 | 9 | Value* ok = emit_if_else_values( | |
| 280 | 9 | c, len_ok, {Type::i32()}, | |
| 281 | 18 | [&]() -> std::vector<Value*> { | |
| 282 | 9 | Value* all_ok = c.i32(1); | |
| 283 | 81 | for (int i = 0; i < 8; ++i) { | |
| 284 | 72 | Value* ev = kb.f32_to_u32(kb.coop_get_acc(IX, c.i32(i))); | |
| 285 | 72 | Value* rw = c.b.build_lshr(ev, c.i32(4)); | |
| 286 | 72 | Value* col_ok = c.b.build_eq(c.b.build_and(ev, c.i32(15)), cl); | |
| 287 | 72 | Value* row_eq = c.b.build_eq(emit_cluster16_min_i32(c, rw), emit_cluster16_max_i32(c, rw)); | |
| 288 | 72 | all_ok = c.b.build_and(all_ok, c.b.build_and(col_ok, row_eq)); | |
| 289 | 72 | } | |
| 290 | 9 | return {all_ok}; | |
| 291 | }, | ||
| 292 | 18 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 293 | 9 | Value* raw_fast = emit_subgroup32_all_i32(c, ok); | |
| 294 | 18 | kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { | |
| 295 | 9 | kb.shared_store_i32_indexed(s_fast, c.i32(0), c.b.build_select(raw_fast, c.i32(1), c.i32(0))); | |
| 296 | 9 | }); | |
| 297 | 9 | kb.sync(); | |
| 298 | 9 | return c.b.build_ne(kb.shared_load_i32_indexed(s_fast, c.i32(0)), c.i32(0)); | |
| 299 | 9 | }; | |
| 300 | |||
| 301 | 9 | Value* IX = kb.coop_alloc_acc(); | |
| 302 | 9 | Value* sbase = c.b.build_shl(sg, c.i32(8)); // sg * 256 | |
| 303 | |||
| 304 | 9 | if (s.pass != 1u) { | |
| 305 | // ── Query-major: PASS 0 (stats) or PASS 2 (dQ) ── | ||
| 306 | 12 | Value* r0 = c.b.build_mul(c.b.build_sub(c.b.build_sub(kb.nctaid_x(), c.i32(1)), kb.ctaid_x()), | |
| 307 | 6 | c.i32(static_cast<int>(BO))); | |
| 308 | 6 | auto row_int = emit_if_else_values( | |
| 309 | 6 | c, c.b.build_ult(tid, c.i32(static_cast<int>(BO))), {Type::i32(), Type::i32()}, | |
| 310 | 12 | [&]() -> std::vector<Value*> { | |
| 311 | 6 | Value* q = c.b.build_add(r0, tid); | |
| 312 | 24 | auto [lo_v, hi_v] = emit_bw_cm_interval(c, s.mode, q, p[10], p[11], p[16], p[17], p[18], p[5], p[6]); | |
| 313 | 12 | kb.shared_store_i32_indexed(ilo, tid, lo_v); | |
| 314 | 12 | kb.shared_store_i32_indexed(ihi, tid, hi_v); | |
| 315 | 6 | if (s.pass == 2u) { | |
| 316 | 3 | auto st_v = emit_if_else_values( | |
| 317 | 3 | c, c.b.build_ult(q, p[10]), {Type::f32(), Type::f32()}, | |
| 318 | 6 | [&]() -> std::vector<Value*> { | |
| 319 | 3 | Value* se = stat_at(q); | |
| 320 | 6 | return {kb.load_f32(ptr_at(c, p[7], se, 4)), | |
| 321 | 3 | kb.load_f32(ptr_at(c, p[7], c.b.build_add(se, c.i32(1)), 4))}; | |
| 322 | }, | ||
| 323 | 6 | [&]() -> std::vector<Value*> { return {c.f32(0.0f), c.f32(0.0f)}; }); | |
| 324 | 3 | kb.shared_store_f32_indexed(ist_x, tid, st_v[0]); | |
| 325 | 3 | kb.shared_store_f32_indexed(ist_y, tid, st_v[1]); | |
| 326 | 3 | } | |
| 327 | 12 | return {lo_v, hi_v}; | |
| 328 | ✗ | }, | |
| 329 | 12 | [&]() -> std::vector<Value*> { return {c.i32(0), c.i32(0)}; }); | |
| 330 | 6 | stage(0u, BO, p[0], p[12], r0, p[10]); | |
| 331 | 6 | stage(BO * SD, BO, p[3], p[14], r0, p[10]); | |
| 332 | |||
| 333 | 6 | Value* has_k = c.b.build_slt(row_int[0], row_int[1]); | |
| 334 | 6 | kb.shared_store_i32_indexed(s_lo, tid, c.b.build_select(has_k, row_int[0], c.i32(static_cast<int>(0x7fffffff)))); | |
| 335 | 6 | kb.shared_store_i32_indexed(s_hi, tid, c.b.build_select(has_k, row_int[1], c.i32(0))); | |
| 336 | 6 | kb.sync(); | |
| 337 | 42 | for (std::uint32_t st = WG / 2u; st >= 1u; st >>= 1) { | |
| 338 | 72 | kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(st))), [&] { | |
| 339 | 36 | Value* t2 = c.b.build_add(tid, c.i32(static_cast<int>(st))); | |
| 340 | 72 | kb.shared_store_i32_indexed(s_lo, tid, smin_i32(c, kb.shared_load_i32_indexed(s_lo, tid), | |
| 341 | 36 | kb.shared_load_i32_indexed(s_lo, t2))); | |
| 342 | 72 | kb.shared_store_i32_indexed(s_hi, tid, smax_i32(c, kb.shared_load_i32_indexed(s_hi, tid), | |
| 343 | 36 | kb.shared_load_i32_indexed(s_hi, t2))); | |
| 344 | 36 | }); | |
| 345 | 36 | kb.sync(); | |
| 346 | 36 | } | |
| 347 | |||
| 348 | 6 | kb.coop_load_acc(IX, SF, c.i32(16), false, false); | |
| 349 | 6 | std::vector<Value*> fq(ND), fg(ND); | |
| 350 | 30 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 351 | 24 | fq[dc] = kb.coop_alloc_a(); | |
| 352 | 24 | fg[dc] = kb.coop_alloc_a(); | |
| 353 | 24 | if (s.areg) { | |
| 354 | 48 | Value* q_off = c.b.build_add(c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(SD))), | |
| 355 | 24 | c.i32(static_cast<int>(dc * 2u))); | |
| 356 | 24 | kb.coop_load_a(fq[dc], c.b.build_add(SM, c.b.build_shl(q_off, c.i32(4))), stride_sd, false); | |
| 357 | 24 | kb.coop_load_a(fg[dc], c.b.build_add(SM, c.b.build_shl(c.b.build_add(q_off, c.i32(static_cast<int>(BO * SD))), c.i32(4))), | |
| 358 | 24 | stride_sd, false); | |
| 359 | 24 | } | |
| 360 | 24 | } | |
| 361 | 6 | Value* fast = check_cluster_layout(IX); | |
| 362 | 6 | std::array<Value*, 8> flo{}, fhi{}, fst_x{}, fst_y{}; | |
| 363 | 6 | if (!s.agnostic) { | |
| 364 | 54 | for (int i = 0; i < 8; ++i) { | |
| 365 | 48 | Value* ri = emit_if_else_values( | |
| 366 | 48 | c, fast, {Type::i32()}, | |
| 367 | 96 | [&]() -> std::vector<Value*> { | |
| 368 | 96 | Value* r_local = c.b.build_and(c.b.build_lshr(kb.f32_to_u32(kb.coop_get_acc(IX, c.i32(i))), c.i32(4)), | |
| 369 | 48 | c.i32(15)); | |
| 370 | 48 | return {c.b.build_add(c.b.build_shl(sg, c.i32(4)), r_local)}; | |
| 371 | }, | ||
| 372 | 96 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 373 | 48 | flo[static_cast<std::size_t>(i)] = kb.shared_load_i32_indexed(ilo, ri); | |
| 374 | 48 | fhi[static_cast<std::size_t>(i)] = kb.shared_load_i32_indexed(ihi, ri); | |
| 375 | 48 | fst_x[static_cast<std::size_t>(i)] = (s.pass == 2u) ? kb.shared_load_f32_indexed(ist_x, ri) : c.f32(-1e30f); | |
| 376 | 48 | fst_y[static_cast<std::size_t>(i)] = (s.pass == 2u) ? kb.shared_load_f32_indexed(ist_y, ri) : c.f32(0.0f); | |
| 377 | 48 | } | |
| 378 | 6 | } | |
| 379 | 6 | kb.sync(); | |
| 380 | |||
| 381 | 6 | Value* kbeg = kb.shared_load_i32_indexed(s_lo, c.i32(0)); | |
| 382 | 6 | Value* kend = kb.shared_load_i32_indexed(s_hi, c.i32(0)); | |
| 383 | |||
| 384 | 6 | Value* fk_frag = kb.coop_alloc_b(); | |
| 385 | 6 | Value* fv_frag = kb.coop_alloc_b(); | |
| 386 | 6 | std::vector<Value*> s_fr(NJ), dp_fr(NJ); | |
| 387 | 12 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 388 | 6 | s_fr[j] = kb.coop_alloc_acc(); | |
| 389 | 6 | dp_fr[j] = kb.coop_alloc_acc(); | |
| 390 | 6 | } | |
| 391 | 12 | auto tile_sdp = [&] { | |
| 392 | 12 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 393 | 6 | kb.coop_fill_acc(s_fr[j], c.f32(0.0f)); | |
| 394 | 6 | kb.coop_fill_acc(dp_fr[j], c.f32(0.0f)); | |
| 395 | 30 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 396 | 24 | if (!s.areg) { | |
| 397 | ✗ | Value* q_off = c.b.build_add(c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(SD))), | |
| 398 | ✗ | c.i32(static_cast<int>(dc * 2u))); | |
| 399 | ✗ | kb.coop_load_a(fq[dc], c.b.build_add(SM, c.b.build_shl(q_off, c.i32(4))), stride_sd, false); | |
| 400 | ✗ | kb.coop_load_a(fg[dc], c.b.build_add(SM, c.b.build_shl(c.b.build_add(q_off, c.i32(static_cast<int>(BO * SD))), c.i32(4))), | |
| 401 | ✗ | stride_sd, false); | |
| 402 | ✗ | } | |
| 403 | 24 | Value* k_off = c.i32(static_cast<int>(KVO + j * 16u * SD + dc * 2u)); | |
| 404 | 24 | Value* v_off = c.i32(static_cast<int>(KVO + s.bn * SD + j * 16u * SD + dc * 2u)); | |
| 405 | 24 | kb.coop_load_b(fk_frag, c.b.build_add(SM, c.b.build_shl(k_off, c.i32(4))), stride_sd, true); | |
| 406 | 24 | kb.coop_load_b(fv_frag, c.b.build_add(SM, c.b.build_shl(v_off, c.i32(4))), stride_sd, true); | |
| 407 | 24 | kb.coop_mma(s_fr[j], fq[dc], fk_frag); | |
| 408 | 24 | kb.coop_mma(dp_fr[j], fg[dc], fv_frag); | |
| 409 | 24 | } | |
| 410 | 6 | } | |
| 411 | 6 | }; | |
| 412 | |||
| 413 | 6 | if (s.pass == 0u) { | |
| 414 | 3 | Value* r = c.b.build_lshr(lane, c.i32(1)); | |
| 415 | 3 | Value* half_ = c.b.build_and(lane, c.i32(1)); | |
| 416 | 3 | Value* rl = c.b.build_add(c.b.build_shl(sg, c.i32(4)), r); | |
| 417 | 3 | Value* c0 = c.b.build_mul(half_, c.i32(static_cast<int>(s.bn / 2u))); | |
| 418 | 3 | Value* alo = kb.shared_load_i32_indexed(ilo, rl); | |
| 419 | 3 | Value* ahi = kb.shared_load_i32_indexed(ihi, rl); | |
| 420 | 3 | Value* fsb = c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(FS))); | |
| 421 | 3 | Value* fdb = c.b.build_add(c.i32(static_cast<int>(BO * FS)), fsb); | |
| 422 | |||
| 423 | // Loop state: agnostic {m, l, dacc} (3) + clustered {m[8], l[8], d[8]} (24 when !agnostic) | ||
| 424 | 3 | std::vector<Value*> init_st = {c.f32(-1e30f), c.f32(0.0f), c.f32(0.0f)}; | |
| 425 | 3 | if (!s.agnostic) { | |
| 426 | 27 | for (int i = 0; i < 8; ++i) init_st.push_back(c.f32(-1e30f)); | |
| 427 | 27 | for (int i = 0; i < 8; ++i) init_st.push_back(c.f32(0.0f)); | |
| 428 | 27 | for (int i = 0; i < 8; ++i) init_st.push_back(c.f32(0.0f)); | |
| 429 | 3 | } | |
| 430 | 3 | auto fin_st = kb.for_range_reduce_n( | |
| 431 | 3 | kbeg, kend, c.i32(static_cast<int>(s.bn)), init_st, | |
| 432 | 6 | [&](Value* kb_v, const std::vector<Value*>& st) -> std::vector<Value*> { | |
| 433 | 3 | kb.sync(); | |
| 434 | 3 | stage(KVO, s.bn, p[1], p[13], kb_v, p[11]); | |
| 435 | 3 | stage(KVO + s.bn * SD, s.bn, p[2], p[13], kb_v, p[11]); | |
| 436 | 6 | kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(s.bn))), [&] { | |
| 437 | 3 | kb.shared_store_f32_indexed(kok, tid, key_ok_f32(c.b.build_add(kb_v, tid))); | |
| 438 | 3 | }); | |
| 439 | 3 | kb.sync(); | |
| 440 | 3 | tile_sdp(); | |
| 441 | |||
| 442 | 3 | if (s.agnostic) { | |
| 443 | ✗ | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 444 | ✗ | kb.coop_store_acc(s_fr[j], c.b.build_add(SF, c.b.build_shl(c.b.build_add(fsb, c.i32(static_cast<int>(j * 16u))), c.i32(2))), | |
| 445 | ✗ | c.i32(static_cast<int>(FS)), false, false); | |
| 446 | ✗ | kb.coop_store_acc(dp_fr[j], c.b.build_add(SF, c.b.build_shl(c.b.build_add(fdb, c.i32(static_cast<int>(j * 16u))), c.i32(2))), | |
| 447 | ✗ | c.i32(static_cast<int>(FS)), false, false); | |
| 448 | ✗ | } | |
| 449 | ✗ | kb.subgroup_barrier(); | |
| 450 | ✗ | Value* row_off = c.b.build_mul(r, c.i32(static_cast<int>(FS))); | |
| 451 | ✗ | Value* mx = c.f32(-1e30f); | |
| 452 | ✗ | for (std::uint32_t ci = 0; ci < s.bn / 2u; ++ci) { | |
| 453 | ✗ | Value* col = c.b.build_add(c0, c.i32(static_cast<int>(ci))); | |
| 454 | ✗ | Value* key = c.b.build_add(kb_v, col); | |
| 455 | ✗ | Value* ok = c.b.build_and(c.b.build_and(c.b.build_sge(key, alo), c.b.build_slt(key, ahi)), | |
| 456 | ✗ | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 457 | ✗ | Value* sv = c.b.build_mul(kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fsb, row_off), col)), p[19]); | |
| 458 | ✗ | mx = c.b.build_select(ok, kb.fmax(mx, sv), mx); | |
| 459 | ✗ | } | |
| 460 | ✗ | mx = kb.fmax(mx, kb.shfl_bfly_f32(mx, 1u)); | |
| 461 | ✗ | Value* mnew = kb.fmax(st[0], mx); | |
| 462 | ✗ | Value* sum = c.f32(0.0f); | |
| 463 | ✗ | Value* ds = c.f32(0.0f); | |
| 464 | ✗ | for (std::uint32_t ci = 0; ci < s.bn / 2u; ++ci) { | |
| 465 | ✗ | Value* col = c.b.build_add(c0, c.i32(static_cast<int>(ci))); | |
| 466 | ✗ | Value* key = c.b.build_add(kb_v, col); | |
| 467 | ✗ | Value* ok = c.b.build_and(c.b.build_and(c.b.build_sge(key, alo), c.b.build_slt(key, ahi)), | |
| 468 | ✗ | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 469 | ✗ | Value* sv = c.b.build_mul(kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fsb, row_off), col)), p[19]); | |
| 470 | ✗ | Value* pv = c.b.build_select(ok, kb.ex2_approx(c.b.build_sub(sv, mnew)), c.f32(0.0f)); | |
| 471 | ✗ | sum = c.b.build_add(sum, pv); | |
| 472 | ✗ | ds = c.b.build_fma_f32(pv, kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fdb, row_off), col)), ds); | |
| 473 | ✗ | } | |
| 474 | ✗ | sum = c.b.build_add(sum, kb.shfl_bfly_f32(sum, 1u)); | |
| 475 | ✗ | ds = c.b.build_add(ds, kb.shfl_bfly_f32(ds, 1u)); | |
| 476 | ✗ | Value* alpha = kb.ex2_approx(c.b.build_sub(st[0], mnew)); | |
| 477 | ✗ | return {mnew, c.b.build_fma_f32(st[1], alpha, sum), c.b.build_fma_f32(st[2], alpha, ds)}; | |
| 478 | } | ||
| 479 | |||
| 480 | // Uniform barrier outside fast/slow split so BVSolver/barrier prover always verifies. | ||
| 481 | 6 | kb.if_then(c.b.build_eq(fast, c.i32(0)), [&] { | |
| 482 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 483 | 6 | kb.coop_store_acc(s_fr[j], c.b.build_add(SF, c.b.build_shl(c.b.build_add(fsb, c.i32(static_cast<int>(j * 16u))), c.i32(2))), | |
| 484 | 3 | c.i32(static_cast<int>(FS)), false, false); | |
| 485 | 6 | kb.coop_store_acc(dp_fr[j], c.b.build_add(SF, c.b.build_shl(c.b.build_add(fdb, c.i32(static_cast<int>(j * 16u))), c.i32(2))), | |
| 486 | 3 | c.i32(static_cast<int>(FS)), false, false); | |
| 487 | 3 | } | |
| 488 | 3 | }); | |
| 489 | 3 | kb.subgroup_barrier(); | |
| 490 | |||
| 491 | 3 | return emit_if_else_values( | |
| 492 | 3 | c, fast, std::vector<Type>(27, Type::f32()), | |
| 493 | 6 | [&]() -> std::vector<Value*> { | |
| 494 | 3 | std::vector<Value*> out = st; | |
| 495 | 27 | for (int i = 0; i < 8; ++i) { | |
| 496 | 24 | Value* mx = c.f32(-1e30f); | |
| 497 | 24 | std::vector<Value*> sv(NJ), okv(NJ); | |
| 498 | 48 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 499 | 24 | Value* col = c.b.build_add(c.i32(static_cast<int>(j * 16u)), cl); | |
| 500 | 24 | Value* key = c.b.build_add(kb_v, col); | |
| 501 | 48 | Value* ok = c.b.build_and( | |
| 502 | 48 | c.b.build_and(c.b.build_sge(key, flo[static_cast<std::size_t>(i)]), | |
| 503 | 24 | c.b.build_slt(key, fhi[static_cast<std::size_t>(i)])), | |
| 504 | 24 | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 505 | 24 | Value* s_sc = c.b.build_mul(kb.coop_get_acc(s_fr[j], c.i32(i)), p[19]); | |
| 506 | 24 | sv[j] = s_sc; | |
| 507 | 24 | okv[j] = ok; | |
| 508 | 24 | mx = c.b.build_select(ok, kb.fmax(mx, s_sc), mx); | |
| 509 | 24 | } | |
| 510 | 24 | Value* mnew = kb.fmax(st[3u + static_cast<std::size_t>(i)], emit_cluster16_max_f32(c, mx)); | |
| 511 | 24 | Value* sum = c.f32(0.0f); | |
| 512 | 24 | Value* ds = c.f32(0.0f); | |
| 513 | 48 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 514 | 24 | Value* pv = c.b.build_select(okv[j], kb.ex2_approx(c.b.build_sub(sv[j], mnew)), c.f32(0.0f)); | |
| 515 | 24 | sum = c.b.build_add(sum, pv); | |
| 516 | 24 | ds = c.b.build_fma_f32(pv, kb.coop_get_acc(dp_fr[j], c.i32(i)), ds); | |
| 517 | 24 | } | |
| 518 | 24 | Value* alpha = kb.ex2_approx(c.b.build_sub(st[3u + static_cast<std::size_t>(i)], mnew)); | |
| 519 | 24 | out[3u + static_cast<std::size_t>(i)] = mnew; | |
| 520 | 24 | out[11u + static_cast<std::size_t>(i)] = c.b.build_fma_f32(st[11u + static_cast<std::size_t>(i)], alpha, emit_cluster16_add_f32(c, sum)); | |
| 521 | 24 | out[19u + static_cast<std::size_t>(i)] = c.b.build_fma_f32(st[19u + static_cast<std::size_t>(i)], alpha, emit_cluster16_add_f32(c, ds)); | |
| 522 | 24 | } | |
| 523 | 3 | return out; | |
| 524 | 3 | }, | |
| 525 | 6 | [&]() -> std::vector<Value*> { | |
| 526 | 3 | std::vector<Value*> out = st; | |
| 527 | 3 | Value* row_off = c.b.build_mul(r, c.i32(static_cast<int>(FS))); | |
| 528 | 3 | Value* mx = c.f32(-1e30f); | |
| 529 | 27 | for (std::uint32_t ci = 0; ci < s.bn / 2u; ++ci) { | |
| 530 | 24 | Value* col = c.b.build_add(c0, c.i32(static_cast<int>(ci))); | |
| 531 | 24 | Value* key = c.b.build_add(kb_v, col); | |
| 532 | 48 | Value* ok = c.b.build_and(c.b.build_and(c.b.build_sge(key, alo), c.b.build_slt(key, ahi)), | |
| 533 | 24 | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 534 | 24 | Value* sv = c.b.build_mul(kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fsb, row_off), col)), p[19]); | |
| 535 | 24 | mx = c.b.build_select(ok, kb.fmax(mx, sv), mx); | |
| 536 | 24 | } | |
| 537 | 3 | mx = kb.fmax(mx, kb.shfl_bfly_f32(mx, 1u)); | |
| 538 | 3 | Value* mnew = kb.fmax(st[0], mx); | |
| 539 | 3 | Value* sum = c.f32(0.0f); | |
| 540 | 3 | Value* ds = c.f32(0.0f); | |
| 541 | 27 | for (std::uint32_t ci = 0; ci < s.bn / 2u; ++ci) { | |
| 542 | 24 | Value* col = c.b.build_add(c0, c.i32(static_cast<int>(ci))); | |
| 543 | 24 | Value* key = c.b.build_add(kb_v, col); | |
| 544 | 48 | Value* ok = c.b.build_and(c.b.build_and(c.b.build_sge(key, alo), c.b.build_slt(key, ahi)), | |
| 545 | 24 | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 546 | 24 | Value* sv = c.b.build_mul(kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fsb, row_off), col)), p[19]); | |
| 547 | 24 | Value* pv = c.b.build_select(ok, kb.ex2_approx(c.b.build_sub(sv, mnew)), c.f32(0.0f)); | |
| 548 | 24 | sum = c.b.build_add(sum, pv); | |
| 549 | 24 | ds = c.b.build_fma_f32(pv, kb.shared_load_f32_indexed(SF, c.b.build_add(c.b.build_add(fdb, row_off), col)), ds); | |
| 550 | 24 | } | |
| 551 | 3 | sum = c.b.build_add(sum, kb.shfl_bfly_f32(sum, 1u)); | |
| 552 | 3 | ds = c.b.build_add(ds, kb.shfl_bfly_f32(ds, 1u)); | |
| 553 | 3 | Value* alpha = kb.ex2_approx(c.b.build_sub(st[0], mnew)); | |
| 554 | 3 | out[0] = mnew; | |
| 555 | 3 | out[1] = c.b.build_fma_f32(st[1], alpha, sum); | |
| 556 | 3 | out[2] = c.b.build_fma_f32(st[2], alpha, ds); | |
| 557 | 3 | return out; | |
| 558 | 3 | }); | |
| 559 | 3 | }); | |
| 560 | |||
| 561 | 3 | if (s.agnostic) { | |
| 562 | ✗ | kb.if_then(c.b.build_eq(half_, c.i32(0)), [&] { | |
| 563 | ✗ | Value* any = c.b.build_sgt(fin_st[1], c.f32(0.0f)); | |
| 564 | ✗ | Value* safe_l = c.b.build_select(any, fin_st[1], c.f32(1.0f)); | |
| 565 | ✗ | kb.shared_store_f32_indexed(ist_x, rl, c.b.build_select(any, c.b.build_add(fin_st[0], kb.lg2_approx(safe_l)), c.f32(0.0f))); | |
| 566 | ✗ | kb.shared_store_f32_indexed(ist_y, rl, c.b.build_select(any, c.b.build_sdiv(fin_st[2], safe_l), c.f32(0.0f))); | |
| 567 | ✗ | }); | |
| 568 | ✗ | } else { | |
| 569 | 6 | kb.if_then(fast, [&] { | |
| 570 | 6 | kb.if_then(c.b.build_eq(cl, c.i32(0)), [&] { | |
| 571 | 27 | for (int i = 0; i < 8; ++i) { | |
| 572 | 24 | Value* fl_i = fin_st[11u + static_cast<std::size_t>(i)]; | |
| 573 | 24 | Value* any = c.b.build_sgt(fl_i, c.f32(0.0f)); | |
| 574 | 24 | Value* safe_l = c.b.build_select(any, fl_i, c.f32(1.0f)); | |
| 575 | 48 | Value* rr = c.b.build_add(c.b.build_shl(sg, c.i32(4)), | |
| 576 | 24 | c.b.build_and(c.b.build_lshr(kb.f32_to_u32(kb.coop_get_acc(IX, c.i32(i))), c.i32(4)), c.i32(15))); | |
| 577 | 24 | kb.shared_store_f32_indexed(ist_x, rr, c.b.build_select(any, c.b.build_add(fin_st[3u + static_cast<std::size_t>(i)], kb.lg2_approx(safe_l)), c.f32(0.0f))); | |
| 578 | 24 | kb.shared_store_f32_indexed(ist_y, rr, c.b.build_select(any, c.b.build_sdiv(fin_st[19u + static_cast<std::size_t>(i)], safe_l), c.f32(0.0f))); | |
| 579 | 24 | } | |
| 580 | 3 | }); | |
| 581 | 3 | }); | |
| 582 | 6 | kb.if_then(c.b.build_and(c.b.build_eq(fast, c.i32(0)), c.b.build_eq(half_, c.i32(0))), [&] { | |
| 583 | 3 | Value* any = c.b.build_sgt(fin_st[1], c.f32(0.0f)); | |
| 584 | 3 | Value* safe_l = c.b.build_select(any, fin_st[1], c.f32(1.0f)); | |
| 585 | 3 | kb.shared_store_f32_indexed(ist_x, rl, c.b.build_select(any, c.b.build_add(fin_st[0], kb.lg2_approx(safe_l)), c.f32(0.0f))); | |
| 586 | 3 | kb.shared_store_f32_indexed(ist_y, rl, c.b.build_select(any, c.b.build_sdiv(fin_st[2], safe_l), c.f32(0.0f))); | |
| 587 | 3 | }); | |
| 588 | } | ||
| 589 | 3 | kb.subgroup_barrier(); | |
| 590 | 6 | kb.if_then(c.b.build_and(c.b.build_eq(half_, c.i32(0)), c.b.build_ult(c.b.build_add(r0, rl), p[10])), [&] { | |
| 591 | 3 | Value* se = stat_at(c.b.build_add(r0, rl)); | |
| 592 | 3 | kb.store_f32(ptr_at(c, p[7], se, 4), kb.shared_load_f32_indexed(ist_x, rl)); | |
| 593 | 3 | kb.store_f32(ptr_at(c, p[7], c.b.build_add(se, c.i32(1)), 4), kb.shared_load_f32_indexed(ist_y, rl)); | |
| 594 | 3 | }); | |
| 595 | 3 | c.b.build_ret_void(); | |
| 596 | 3 | return finalize_fn(fn); | |
| 597 | 3 | } | |
| 598 | |||
| 599 | // ── PASS 2: dQ += dS K ── | ||
| 600 | 3 | Value* hbase_v4 = c.b.build_mul(c.b.build_shl(sg, c.i32(4)), c.i32(static_cast<int>(HS_V4))); | |
| 601 | 3 | std::vector<Value*> acc(ND); | |
| 602 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 603 | 12 | acc[dc] = kb.coop_alloc_acc(); | |
| 604 | 12 | kb.coop_fill_acc(acc[dc], c.f32(0.0f)); | |
| 605 | 12 | } | |
| 606 | 3 | Value* fs_frag = kb.coop_alloc_a(); | |
| 607 | 6 | kb.for_range(kbeg, kend, c.i32(static_cast<int>(s.bn)), [&](Value* kb_v) { | |
| 608 | 3 | kb.sync(); | |
| 609 | 3 | stage(KVO, s.bn, p[1], p[13], kb_v, p[11]); | |
| 610 | 3 | stage(KVO + s.bn * SD, s.bn, p[2], p[13], kb_v, p[11]); | |
| 611 | 6 | kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(s.bn))), [&] { | |
| 612 | 3 | kb.shared_store_f32_indexed(kok, tid, key_ok_f32(c.b.build_add(kb_v, tid))); | |
| 613 | 3 | }); | |
| 614 | 3 | kb.sync(); | |
| 615 | 3 | tile_sdp(); | |
| 616 | |||
| 617 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 618 | 6 | auto do_agnostic = [&] { | |
| 619 | 6 | emit_coop_acc_map(c, s_fr[j], [&](Value* idx, Value* sv) { | |
| 620 | 3 | Value* ev = kb.f32_to_u32(kb.coop_get_acc(IX, idx)); | |
| 621 | 3 | Value* rr = c.b.build_add(c.b.build_shl(sg, c.i32(4)), c.b.build_and(c.b.build_lshr(ev, c.i32(4)), c.i32(15))); | |
| 622 | 3 | Value* col = c.b.build_add(c.i32(static_cast<int>(j * 16u)), c.b.build_and(ev, c.i32(15))); | |
| 623 | 3 | Value* key = c.b.build_add(kb_v, col); | |
| 624 | 6 | Value* ok = c.b.build_and( | |
| 625 | 6 | c.b.build_and(c.b.build_sge(key, kb.shared_load_i32_indexed(ilo, rr)), | |
| 626 | 3 | c.b.build_slt(key, kb.shared_load_i32_indexed(ihi, rr))), | |
| 627 | 3 | c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f))); | |
| 628 | 6 | Value* pv = c.b.build_select( | |
| 629 | 3 | ok, kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[19]), kb.shared_load_f32_indexed(ist_x, rr))), | |
| 630 | 3 | c.f32(0.0f)); | |
| 631 | 3 | return c.b.build_mul(pv, c.b.build_sub(kb.coop_get_acc(dp_fr[j], idx), kb.shared_load_f32_indexed(ist_y, rr))); | |
| 632 | }); | ||
| 633 | 3 | }; | |
| 634 | 3 | if (s.agnostic) { | |
| 635 | ✗ | do_agnostic(); | |
| 636 | ✗ | } else { | |
| 637 | 6 | kb.if_then(fast, [&] { | |
| 638 | 3 | Value* col = c.b.build_add(c.i32(static_cast<int>(j * 16u)), cl); | |
| 639 | 3 | Value* key = c.b.build_add(kb_v, col); | |
| 640 | 3 | Value* kv = c.b.build_sgt(kb.shared_load_f32_indexed(kok, col), c.f32(0.5f)); | |
| 641 | 27 | for (int i = 0; i < 8; ++i) { | |
| 642 | 48 | Value* ok = c.b.build_and( | |
| 643 | 48 | kv, c.b.build_and(c.b.build_sge(key, flo[static_cast<std::size_t>(i)]), | |
| 644 | 24 | c.b.build_slt(key, fhi[static_cast<std::size_t>(i)]))); | |
| 645 | 24 | Value* sv = kb.coop_get_acc(s_fr[j], c.i32(i)); | |
| 646 | 48 | Value* pv = c.b.build_select( | |
| 647 | 24 | ok, kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[19]), fst_x[static_cast<std::size_t>(i)])), | |
| 648 | 24 | c.f32(0.0f)); | |
| 649 | 24 | Value* ds = c.b.build_mul(pv, c.b.build_sub(kb.coop_get_acc(dp_fr[j], c.i32(i)), fst_y[static_cast<std::size_t>(i)])); | |
| 650 | 24 | kb.coop_set_acc(s_fr[j], c.i32(i), ds); | |
| 651 | 24 | } | |
| 652 | 3 | }); | |
| 653 | 3 | kb.if_then(c.b.build_eq(fast, c.i32(0)), do_agnostic); | |
| 654 | } | ||
| 655 | 3 | Value* dst_v4 = c.b.build_add(hbase_v4, c.i32(static_cast<int>(j * 2u))); | |
| 656 | 3 | kb.coop_store_acc(s_fr[j], c.b.build_add(SH, c.b.build_shl(dst_v4, c.i32(4))), stride_hs, false, true); | |
| 657 | 3 | } | |
| 658 | 3 | kb.subgroup_barrier(); | |
| 659 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 660 | 3 | Value* src_v4 = c.b.build_add(hbase_v4, c.i32(static_cast<int>(j * 2u))); | |
| 661 | 3 | kb.coop_load_a(fs_frag, c.b.build_add(SH, c.b.build_shl(src_v4, c.i32(4))), stride_hs, false); | |
| 662 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 663 | 12 | Value* k_off = c.i32(static_cast<int>(KVO + j * 16u * SD + dc * 2u)); | |
| 664 | 12 | kb.coop_load_b(fk_frag, c.b.build_add(SM, c.b.build_shl(k_off, c.i32(4))), stride_sd, false); | |
| 665 | 12 | kb.coop_mma(acc[dc], fs_frag, fk_frag); | |
| 666 | 12 | } | |
| 667 | 3 | } | |
| 668 | 3 | }); | |
| 669 | 3 | kb.sync(); | |
| 670 | 3 | Value* row0 = c.b.build_add(r0, c.b.build_shl(sg, c.i32(4))); | |
| 671 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 672 | 12 | store_tile(acc[dc], sbase, p[8], row0, p[10], dc * 16u, p[20]); | |
| 673 | 12 | } | |
| 674 | 3 | c.b.build_ret_void(); | |
| 675 | 3 | return finalize_fn(fn); | |
| 676 | 6 | } | |
| 677 | |||
| 678 | // ── PASS 1, key-major: dK, dV ── | ||
| 679 | 3 | Value* k0 = c.b.build_mul(kb.ctaid_x(), c.i32(static_cast<int>(BO))); | |
| 680 | 3 | Value* kr = c.b.build_shl(sg, c.i32(4)); | |
| 681 | 3 | const std::uint32_t QB = KVO, GB = QB + s.bn * SD; | |
| 682 | 3 | stage(0u, BO, p[1], p[13], k0, p[11]); | |
| 683 | 3 | stage(BO * SD, BO, p[2], p[13], k0, p[11]); | |
| 684 | 6 | kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(BO))), [&] { | |
| 685 | 3 | kb.shared_store_f32_indexed(kok, c.b.build_add(c.i32(static_cast<int>(s.bn)), tid), key_ok_f32(c.b.build_add(k0, tid))); | |
| 686 | 3 | }); | |
| 687 | 3 | Value* k1 = c.b.build_sub(smin_i32(c, c.b.build_add(k0, c.i32(static_cast<int>(BO))), p[11]), c.i32(1)); | |
| 688 | 9 | auto [qlo, qhi] = emit_bw_cm_qrange(c, s.mode, k0, k1, p[10], p[16], p[17], p[18], p[5], p[6]); | |
| 689 | 3 | kb.sync(); | |
| 690 | |||
| 691 | 3 | kb.coop_load_acc(IX, SF, c.i32(16), false, false); | |
| 692 | 3 | Value* fast = check_cluster_layout(IX); | |
| 693 | 3 | std::array<Value*, 8> k_ok_i{}, k_row_i{}; | |
| 694 | 3 | if (!s.agnostic) { | |
| 695 | 27 | for (int i = 0; i < 8; ++i) { | |
| 696 | 24 | Value* r_local = emit_if_else_values( | |
| 697 | 24 | c, fast, {Type::i32()}, | |
| 698 | 48 | [&]() -> std::vector<Value*> { | |
| 699 | 24 | return {c.b.build_and(c.b.build_lshr(kb.f32_to_u32(kb.coop_get_acc(IX, c.i32(i))), c.i32(4)), c.i32(15))}; | |
| 700 | }, | ||
| 701 | 48 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 702 | 24 | k_row_i[static_cast<std::size_t>(i)] = c.b.build_add(c.b.build_add(k0, kr), r_local); | |
| 703 | 24 | Value* kok_idx = c.b.build_add(c.i32(static_cast<int>(s.bn)), c.b.build_add(kr, r_local)); | |
| 704 | 24 | k_ok_i[static_cast<std::size_t>(i)] = c.b.build_sgt(kb.shared_load_f32_indexed(kok, kok_idx), c.f32(0.5f)); | |
| 705 | 24 | } | |
| 706 | 3 | } | |
| 707 | 3 | kb.sync(); | |
| 708 | |||
| 709 | 3 | std::vector<Value*> ak(ND), av(ND), dk(ND), dv(ND); | |
| 710 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 711 | 12 | ak[dc] = kb.coop_alloc_a(); | |
| 712 | 12 | av[dc] = kb.coop_alloc_a(); | |
| 713 | 12 | if (s.areg) { | |
| 714 | 12 | Value* off = c.b.build_add(c.b.build_mul(kr, c.i32(static_cast<int>(SD))), c.i32(static_cast<int>(dc * 2u))); | |
| 715 | 12 | kb.coop_load_a(ak[dc], c.b.build_add(SM, c.b.build_shl(off, c.i32(4))), stride_sd, false); | |
| 716 | 12 | kb.coop_load_a(av[dc], c.b.build_add(SM, c.b.build_shl(c.b.build_add(off, c.i32(static_cast<int>(BO * SD))), c.i32(4))), | |
| 717 | 12 | stride_sd, false); | |
| 718 | 12 | } | |
| 719 | 12 | dk[dc] = kb.coop_alloc_acc(); | |
| 720 | 12 | dv[dc] = kb.coop_alloc_acc(); | |
| 721 | 12 | kb.coop_fill_acc(dk[dc], c.f32(0.0f)); | |
| 722 | 12 | kb.coop_fill_acc(dv[dc], c.f32(0.0f)); | |
| 723 | 12 | } | |
| 724 | 3 | Value* fq_b = kb.coop_alloc_b(); | |
| 725 | 3 | Value* fg_b = kb.coop_alloc_b(); | |
| 726 | 3 | Value* fp_a = kb.coop_alloc_a(); | |
| 727 | 3 | Value* fd_a = kb.coop_alloc_a(); | |
| 728 | 3 | std::vector<Value*> s_fr(NJ), dp_fr(NJ); | |
| 729 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 730 | 3 | s_fr[j] = kb.coop_alloc_acc(); | |
| 731 | 3 | dp_fr[j] = kb.coop_alloc_acc(); | |
| 732 | 3 | } | |
| 733 | 3 | Value* pbase_v4 = c.b.build_mul(kr, c.i32(static_cast<int>(HS_V4))); | |
| 734 | 3 | Value* dbase_v4 = c.b.build_add(c.i32(static_cast<int>(BO * HS_V4)), pbase_v4); | |
| 735 | 6 | Value* qb_start = c.b.build_mul(c.b.build_sdiv(qlo, c.i32(static_cast<int>(s.bn))), c.i32(static_cast<int>(s.bn))); | |
| 736 | |||
| 737 | 9 | kb.for_range(qb_start, qhi, c.i32(static_cast<int>(s.bn)), [&](Value* qb) { | |
| 738 | 3 | kb.sync(); | |
| 739 | 3 | stage(QB, s.bn, p[0], p[12], qb, p[10]); | |
| 740 | 3 | stage(GB, s.bn, p[3], p[14], qb, p[10]); | |
| 741 | 3 | Value* mine = emit_if_else_values( | |
| 742 | 3 | c, c.b.build_ult(tid, c.i32(static_cast<int>(s.bn))), {Type::i32()}, | |
| 743 | 6 | [&]() -> std::vector<Value*> { | |
| 744 | 3 | Value* q = c.b.build_add(qb, tid); | |
| 745 | 21 | auto [rlo, rhi] = emit_bw_cm_interval(c, s.mode, q, p[10], p[11], p[16], p[17], p[18], p[5], p[6]); | |
| 746 | 6 | kb.shared_store_i32_indexed(ilo, tid, rlo); | |
| 747 | 6 | kb.shared_store_i32_indexed(ihi, tid, rhi); | |
| 748 | 3 | Value* sq = smin_i32(c, q, c.b.build_sub(p[10], c.i32(1))); | |
| 749 | 3 | Value* se = stat_at(sq); | |
| 750 | 3 | kb.shared_store_f32_indexed(ist_x, tid, kb.load_f32(ptr_at(c, p[7], se, 4))); | |
| 751 | 3 | kb.shared_store_f32_indexed(ist_y, tid, kb.load_f32(ptr_at(c, p[7], c.b.build_add(se, c.i32(1)), 4))); | |
| 752 | 12 | Value* hit = c.b.build_and(c.b.build_slt(rlo, rhi), | |
| 753 | 9 | c.b.build_and(c.b.build_sle(rlo, k1), c.b.build_sgt(rhi, k0))); | |
| 754 | 3 | return {hit}; | |
| 755 | }, | ||
| 756 | 6 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 757 | 3 | Value* sg_any = emit_subgroup32_any_i32(c, mine); | |
| 758 | 6 | kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { | |
| 759 | 3 | kb.shared_store_i32_indexed(sflag, sg, c.b.build_select(sg_any, c.i32(1), c.i32(0))); | |
| 760 | 3 | }); | |
| 761 | 3 | kb.sync(); | |
| 762 | 3 | Value* any = c.i32(0); | |
| 763 | 9 | for (std::uint32_t ss = 0; ss < s.nsg; ++ss) { | |
| 764 | 6 | any = c.b.build_or(any, kb.shared_load_i32_indexed(sflag, c.i32(static_cast<int>(ss)))); | |
| 765 | 6 | } | |
| 766 | |||
| 767 | // Compute S^T, dP^T, P^T, dS^T only when any query in this block attends [k0, k1]. | ||
| 768 | // Keep subgroup_barrier outside the non-trivial branch or note `any` is workgroup-uniform | ||
| 769 | // (every thread reads the same sflag[0..nsg-1] after kb.sync()). | ||
| 770 | 6 | kb.if_then(c.b.build_ne(any, c.i32(0)), [&] { | |
| 771 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 772 | 3 | kb.coop_fill_acc(s_fr[j], c.f32(0.0f)); | |
| 773 | 3 | kb.coop_fill_acc(dp_fr[j], c.f32(0.0f)); | |
| 774 | 3 | } | |
| 775 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 776 | 12 | if (!s.areg) { | |
| 777 | ✗ | Value* off = c.b.build_add(c.b.build_mul(kr, c.i32(static_cast<int>(SD))), c.i32(static_cast<int>(dc * 2u))); | |
| 778 | ✗ | kb.coop_load_a(ak[dc], c.b.build_add(SM, c.b.build_shl(off, c.i32(4))), stride_sd, false); | |
| 779 | ✗ | kb.coop_load_a(av[dc], c.b.build_add(SM, c.b.build_shl(c.b.build_add(off, c.i32(static_cast<int>(BO * SD))), c.i32(4))), | |
| 780 | ✗ | stride_sd, false); | |
| 781 | ✗ | } | |
| 782 | 24 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 783 | 12 | Value* q_off = c.i32(static_cast<int>(QB + j * 16u * SD + dc * 2u)); | |
| 784 | 12 | Value* g_off = c.i32(static_cast<int>(GB + j * 16u * SD + dc * 2u)); | |
| 785 | 12 | kb.coop_load_b(fq_b, c.b.build_add(SM, c.b.build_shl(q_off, c.i32(4))), stride_sd, true); | |
| 786 | 12 | kb.coop_load_b(fg_b, c.b.build_add(SM, c.b.build_shl(g_off, c.i32(4))), stride_sd, true); | |
| 787 | 12 | kb.coop_mma(s_fr[j], ak[dc], fq_b); | |
| 788 | 12 | kb.coop_mma(dp_fr[j], av[dc], fg_b); | |
| 789 | 12 | } | |
| 790 | 12 | } | |
| 791 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 792 | 6 | auto do_agnostic = [&] { | |
| 793 | 3 | Value* len = kb.coop_acc_len(s_fr[j]); | |
| 794 | 6 | kb.for_range(c.i32(0), len, c.i32(1), [&](Value* idx) { | |
| 795 | 3 | Value* ev = kb.f32_to_u32(kb.coop_get_acc(IX, idx)); | |
| 796 | 3 | Value* rr = c.b.build_and(c.b.build_lshr(ev, c.i32(4)), c.i32(15)); | |
| 797 | 3 | Value* col = c.b.build_add(c.i32(static_cast<int>(j * 16u)), c.b.build_and(ev, c.i32(15))); | |
| 798 | 3 | Value* key = c.b.build_add(c.b.build_add(k0, kr), rr); | |
| 799 | 3 | Value* kok_v = kb.shared_load_f32_indexed(kok, c.b.build_add(c.i32(static_cast<int>(s.bn)), c.b.build_add(kr, rr))); | |
| 800 | 6 | Value* ok = c.b.build_and( | |
| 801 | 3 | c.b.build_sgt(kok_v, c.f32(0.5f)), | |
| 802 | 6 | c.b.build_and(c.b.build_sge(key, kb.shared_load_i32_indexed(ilo, col)), | |
| 803 | 3 | c.b.build_slt(key, kb.shared_load_i32_indexed(ihi, col)))); | |
| 804 | 3 | Value* sv = kb.coop_get_acc(s_fr[j], idx); | |
| 805 | 6 | Value* pv = c.b.build_select( | |
| 806 | 3 | ok, kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[19]), kb.shared_load_f32_indexed(ist_x, col))), | |
| 807 | 3 | c.f32(0.0f)); | |
| 808 | 3 | Value* dsv = c.b.build_mul(pv, c.b.build_sub(kb.coop_get_acc(dp_fr[j], idx), kb.shared_load_f32_indexed(ist_y, col))); | |
| 809 | 3 | kb.coop_set_acc(s_fr[j], idx, pv); | |
| 810 | 3 | kb.coop_set_acc(dp_fr[j], idx, dsv); | |
| 811 | 3 | }); | |
| 812 | 3 | }; | |
| 813 | 3 | if (s.agnostic) { | |
| 814 | ✗ | do_agnostic(); | |
| 815 | ✗ | } else { | |
| 816 | 6 | kb.if_then(fast, [&] { | |
| 817 | 3 | Value* col = c.b.build_add(c.i32(static_cast<int>(j * 16u)), cl); | |
| 818 | 3 | Value* qx = kb.shared_load_f32_indexed(ist_x, col); | |
| 819 | 3 | Value* qy = kb.shared_load_f32_indexed(ist_y, col); | |
| 820 | 3 | Value* lo_c = kb.shared_load_i32_indexed(ilo, col); | |
| 821 | 3 | Value* hi_c = kb.shared_load_i32_indexed(ihi, col); | |
| 822 | 27 | for (int i = 0; i < 8; ++i) { | |
| 823 | 24 | Value* key = k_row_i[static_cast<std::size_t>(i)]; | |
| 824 | 48 | Value* ok = c.b.build_and(k_ok_i[static_cast<std::size_t>(i)], | |
| 825 | 24 | c.b.build_and(c.b.build_sge(key, lo_c), c.b.build_slt(key, hi_c))); | |
| 826 | 24 | Value* sv = kb.coop_get_acc(s_fr[j], c.i32(i)); | |
| 827 | 24 | Value* pv = c.b.build_select(ok, kb.ex2_approx(c.b.build_sub(c.b.build_mul(sv, p[19]), qx)), c.f32(0.0f)); | |
| 828 | 24 | Value* dsv = c.b.build_mul(pv, c.b.build_sub(kb.coop_get_acc(dp_fr[j], c.i32(i)), qy)); | |
| 829 | 24 | kb.coop_set_acc(s_fr[j], c.i32(i), pv); | |
| 830 | 24 | kb.coop_set_acc(dp_fr[j], c.i32(i), dsv); | |
| 831 | 24 | } | |
| 832 | 3 | }); | |
| 833 | 3 | kb.if_then(c.b.build_eq(fast, c.i32(0)), do_agnostic); | |
| 834 | } | ||
| 835 | 6 | kb.coop_store_acc(s_fr[j], c.b.build_add(SH, c.b.build_shl(c.b.build_add(pbase_v4, c.i32(static_cast<int>(j * 2u))), c.i32(4))), | |
| 836 | 3 | stride_hs, false, true); | |
| 837 | 6 | kb.coop_store_acc(dp_fr[j], c.b.build_add(SH, c.b.build_shl(c.b.build_add(dbase_v4, c.i32(static_cast<int>(j * 2u))), c.i32(4))), | |
| 838 | 3 | stride_hs, false, true); | |
| 839 | 3 | } | |
| 840 | 3 | }); | |
| 841 | 3 | kb.subgroup_barrier(); | |
| 842 | 6 | kb.if_then(c.b.build_ne(any, c.i32(0)), [&] { | |
| 843 | 6 | for (std::uint32_t j = 0; j < NJ; ++j) { | |
| 844 | 6 | kb.coop_load_a(fp_a, c.b.build_add(SH, c.b.build_shl(c.b.build_add(pbase_v4, c.i32(static_cast<int>(j * 2u))), c.i32(4))), | |
| 845 | 3 | stride_hs, false); | |
| 846 | 6 | kb.coop_load_a(fd_a, c.b.build_add(SH, c.b.build_shl(c.b.build_add(dbase_v4, c.i32(static_cast<int>(j * 2u))), c.i32(4))), | |
| 847 | 3 | stride_hs, false); | |
| 848 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 849 | 12 | Value* g_off = c.i32(static_cast<int>(GB + j * 16u * SD + dc * 2u)); | |
| 850 | 12 | Value* q_off = c.i32(static_cast<int>(QB + j * 16u * SD + dc * 2u)); | |
| 851 | 12 | kb.coop_load_b(fg_b, c.b.build_add(SM, c.b.build_shl(g_off, c.i32(4))), stride_sd, false); | |
| 852 | 12 | kb.coop_load_b(fq_b, c.b.build_add(SM, c.b.build_shl(q_off, c.i32(4))), stride_sd, false); | |
| 853 | 12 | kb.coop_mma(dv[dc], fp_a, fg_b); | |
| 854 | 12 | kb.coop_mma(dk[dc], fd_a, fq_b); | |
| 855 | 12 | } | |
| 856 | 3 | } | |
| 857 | 3 | }); | |
| 858 | 3 | }); | |
| 859 | |||
| 860 | 3 | kb.sync(); | |
| 861 | 3 | Value* krow0 = c.b.build_add(k0, kr); | |
| 862 | 15 | for (std::uint32_t dc = 0; dc < ND; ++dc) { | |
| 863 | 12 | store_tile(dk[dc], sbase, p[8], krow0, p[11], dc * 16u, p[20]); | |
| 864 | 12 | store_tile(dv[dc], sbase, p[9], krow0, p[11], dc * 16u, c.f32(1.0f)); | |
| 865 | 12 | } | |
| 866 | |||
| 867 | 3 | c.b.build_ret_void(); | |
| 868 | 3 | return finalize_fn(fn); | |
| 869 | 9 | } | |
| 870 | |||
| 871 | } // namespace brotensor::kernels | ||
| 872 |