src/kernels/mir_delta.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_delta.h" | ||
| 2 | |||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include <algorithm> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | namespace brotensor::kernels { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | 6 | Value* group_sum(Ctx& c, Value* v, std::uint32_t lpr) { | |
| 14 | 36 | for (std::uint32_t off = lpr >> 1; off > 0; off >>= 1) { | |
| 15 | 30 | v = c.b.build_add(v, c.kb.shfl_bfly_f32(v, off)); | |
| 16 | 30 | } | |
| 17 | 6 | return v; | |
| 18 | } | ||
| 19 | |||
| 20 | 3 | Value* softplus_stable(Ctx& c, Value* x) { | |
| 21 | 3 | Value* e = emit_exp(c, c.b.build_neg(c.kb.fabs(x))); | |
| 22 | 3 | Value* small = c.b.build_mul(e, c.b.build_sub(c.f32(1.0f), c.b.build_mul(c.f32(0.5f), e))); | |
| 23 | 3 | Value* large = emit_log(c, c.b.build_add(c.f32(1.0f), e)); | |
| 24 | 3 | Value* tail = c.b.build_select(c.b.build_slt(e, c.f32(1e-4f)), small, large); | |
| 25 | 3 | return c.b.build_add(c.kb.fmax(x, c.f32(0.0f)), tail); | |
| 26 | } | ||
| 27 | |||
| 28 | 3 | Value* sigmoid_stable(Ctx& c, Value* x) { | |
| 29 | 3 | Value* pos = c.b.build_sdiv(c.f32(1.0f), c.b.build_add(c.f32(1.0f), emit_exp(c, c.b.build_neg(x)))); | |
| 30 | 3 | Value* ep = emit_exp(c, x); | |
| 31 | 3 | Value* neg = c.b.build_sdiv(ep, c.b.build_add(c.f32(1.0f), ep)); | |
| 32 | 3 | return c.b.build_select(c.b.build_sge(x, c.f32(0.0f)), pos, neg); | |
| 33 | } | ||
| 34 | |||
| 35 | } // namespace | ||
| 36 | |||
| 37 | 3 | brass::Function* build_delta_rule_mir(brass::Module& mod, std::uint32_t lpr, std::uint32_t ne) { | |
| 38 | 3 | const std::uint32_t count = std::max<std::uint32_t>(1u, std::min<std::uint32_t>(ne, 16u)); | |
| 39 | 6 | auto* fn = mod.create_function( | |
| 40 | 3 | "mir_delta_" + std::to_string(lpr) + "_" + std::to_string(count), Type::void_type(), | |
| 41 | 21 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 42 | 18 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}); | |
| 43 | 3 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 44 | 3 | Ctx c(kb); | |
| 45 | 3 | auto p = entry_params(c, fn); | |
| 46 | 3 | Value *q = p[0], *k = p[1], *v = p[2], *a = p[3], *b = p[4], *loga = p[5], *s = p[6], *o = p[7]; | |
| 47 | 3 | Value *L = p[8], *H = p[9], *dk = p[10], *dv = p[11], *ldq = p[12], *ldv = p[13]; | |
| 48 | |||
| 49 | 3 | Value* lpr_v = c.i32(static_cast<int>(lpr)); | |
| 50 | 3 | Value* lane = c.b.build_and(c.kb.tid_x(), c.i32(static_cast<int>(lpr - 1u))); | |
| 51 | 3 | Value* row = c.b.build_udiv(c.kb.global_tid_x(), lpr_v); | |
| 52 | 3 | Value* total_rows = c.b.build_mul(H, dv); | |
| 53 | 3 | Value* active = c.b.build_ult(row, total_rows); | |
| 54 | |||
| 55 | 3 | Value* safe_row = c.b.build_select(active, row, c.i32(0)); | |
| 56 | 3 | Value* h = c.b.build_udiv(safe_row, dv); | |
| 57 | 3 | Value* vr = c.b.build_umod(safe_row, dv); | |
| 58 | 3 | Value* sbase = c.b.build_mul(safe_row, dk); | |
| 59 | |||
| 60 | 3 | std::vector<Value*> S_init(count); | |
| 61 | 12 | for (std::uint32_t e = 0; e < count; ++e) { | |
| 62 | 9 | Value* j = c.b.build_add(lane, c.i32(static_cast<int>(e * lpr))); | |
| 63 | 9 | Value* ok = c.b.build_and(active, c.b.build_ult(j, dk)); | |
| 64 | 9 | S_init[e] = emit_if_else_values( | |
| 65 | 9 | c, ok, {Type::f32()}, | |
| 66 | 18 | [&]() -> std::vector<Value*> { return {c.kb.load_f32(ptr_at(c, s, c.b.build_add(sbase, j), 4))}; }, | |
| 67 | 18 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 68 | 9 | } | |
| 69 | |||
| 70 | 3 | Value* expA = emit_exp(c, c.kb.load_f32(ptr_at(c, loga, h, 4))); | |
| 71 | 3 | Value* h_dk = c.b.build_mul(h, dk); | |
| 72 | 3 | Value* h_dv = c.b.build_mul(h, dv); | |
| 73 | 3 | Value* is_lane0 = c.b.build_and(active, c.b.build_eq(lane, c.i32(0))); | |
| 74 | |||
| 75 | 6 | auto S_fin = c.kb.for_range_reduce_n( | |
| 76 | 6 | c.i32(0), L, c.i32(1), S_init, [&](Value* t, const std::vector<Value*>& S_cur) -> std::vector<Value*> { | |
| 77 | 3 | Value* qb = c.b.build_add(c.b.build_mul(t, ldq), h_dk); | |
| 78 | 3 | Value* th_idx = c.b.build_add(c.b.build_mul(t, H), h); | |
| 79 | 3 | Value* v_idx = c.b.build_add(c.b.build_add(c.b.build_mul(t, ldv), h_dv), vr); | |
| 80 | |||
| 81 | 3 | std::vector<Value*> kv(count), qv(count); | |
| 82 | 3 | Value* dot = c.f32(0.0f); | |
| 83 | 12 | for (std::uint32_t e = 0; e < count; ++e) { | |
| 84 | 9 | Value* j = c.b.build_add(lane, c.i32(static_cast<int>(e * lpr))); | |
| 85 | 9 | Value* ok = c.b.build_and(active, c.b.build_ult(j, dk)); | |
| 86 | 9 | auto pair = emit_if_else_values( | |
| 87 | 9 | c, ok, {Type::f32(), Type::f32()}, | |
| 88 | 18 | [&]() -> std::vector<Value*> { | |
| 89 | 9 | Value* idx = c.b.build_add(qb, j); | |
| 90 | 9 | return {c.kb.load_f32(ptr_at(c, k, idx, 4)), c.kb.load_f32(ptr_at(c, q, idx, 4))}; | |
| 91 | }, | ||
| 92 | 18 | [&]() -> std::vector<Value*> { return {c.f32(0.0f), c.f32(0.0f)}; }); | |
| 93 | 9 | kv[e] = pair[0]; | |
| 94 | 9 | qv[e] = pair[1]; | |
| 95 | 9 | dot = c.b.build_add(dot, c.b.build_mul(S_cur[e], kv[e])); | |
| 96 | 9 | } | |
| 97 | |||
| 98 | 3 | Value* ar = c.kb.load_f32(ptr_at(c, a, th_idx, 4)); | |
| 99 | 3 | Value* br = c.kb.load_f32(ptr_at(c, b, th_idx, 4)); | |
| 100 | 3 | Value* alpha = emit_exp(c, c.b.build_neg(c.b.build_mul(softplus_stable(c, ar), expA))); | |
| 101 | 3 | Value* beta = sigmoid_stable(c, br); | |
| 102 | |||
| 103 | 3 | Value* dot_alpha = c.b.build_mul(group_sum(c, dot, lpr), alpha); | |
| 104 | 3 | Value* v_t = emit_if_else_values( | |
| 105 | 3 | c, active, {Type::f32()}, | |
| 106 | 6 | [&]() -> std::vector<Value*> { return {c.kb.load_f32(ptr_at(c, v, v_idx, 4))}; }, | |
| 107 | 6 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 108 | 3 | Value* sc = c.b.build_mul(beta, c.b.build_sub(v_t, dot_alpha)); | |
| 109 | |||
| 110 | 3 | std::vector<Value*> S_next(count); | |
| 111 | 3 | Value* o_acc = c.f32(0.0f); | |
| 112 | 12 | for (std::uint32_t e = 0; e < count; ++e) { | |
| 113 | 9 | S_next[e] = c.b.build_add(c.b.build_mul(alpha, S_cur[e]), c.b.build_mul(sc, kv[e])); | |
| 114 | 9 | o_acc = c.b.build_add(o_acc, c.b.build_mul(S_next[e], qv[e])); | |
| 115 | 9 | } | |
| 116 | 3 | Value* o_sum = group_sum(c, o_acc, lpr); | |
| 117 | 6 | c.kb.if_then(is_lane0, [&] { c.kb.store_f32(ptr_at(c, o, v_idx, 4), o_sum); }); | |
| 118 | 3 | return S_next; | |
| 119 | 3 | }); | |
| 120 | |||
| 121 | 12 | for (std::uint32_t e = 0; e < count; ++e) { | |
| 122 | 9 | Value* j = c.b.build_add(lane, c.i32(static_cast<int>(e * lpr))); | |
| 123 | 9 | Value* ok = c.b.build_and(active, c.b.build_ult(j, dk)); | |
| 124 | 18 | c.kb.if_then(ok, [&] { c.kb.store_f32(ptr_at(c, s, c.b.build_add(sbase, j), 4), S_fin[e]); }); | |
| 125 | 9 | } | |
| 126 | |||
| 127 | 3 | c.b.build_ret_void(); | |
| 128 | 3 | return finalize_fn(fn); | |
| 129 | 3 | } | |
| 130 | |||
| 131 | } // namespace brotensor::kernels | ||
| 132 |