src/kernels/mir_rope.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_rope.h" | ||
| 2 | #include "mir_common.h" | ||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <utility> | ||
| 8 | |||
| 9 | namespace brotensor::kernels { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | 23 | void emit_rotate_pair(Ctx& c, Dtype dt, bool bwd, Value* src, Value* dst, Value* o, | |
| 14 | Value* cos_v, Value* sin_v) { | ||
| 15 | 23 | const int esz = elem_bytes(dt); | |
| 16 | 23 | Value* s = bwd ? c.b.build_neg(sin_v) : sin_v; | |
| 17 | 23 | Value* p0_in = ptr_at(c, src, o, esz); | |
| 18 | 23 | Value* p1_in = ptr_at(c, src, c.b.build_add(o, c.i32(1)), esz); | |
| 19 | 23 | Value* x0 = load_lanes(c, dt, 1, p0_in)[0]; | |
| 20 | 23 | Value* x1 = load_lanes(c, dt, 1, p1_in)[0]; | |
| 21 | 23 | Value* y0 = c.b.build_sub(c.b.build_mul(x0, cos_v), c.b.build_mul(x1, s)); | |
| 22 | 23 | Value* y1 = c.b.build_add(c.b.build_mul(x0, s), c.b.build_mul(x1, cos_v)); | |
| 23 | 23 | Value* p0_out = ptr_at(c, dst, o, esz); | |
| 24 | 23 | Value* p1_out = ptr_at(c, dst, c.b.build_add(o, c.i32(1)), esz); | |
| 25 | 23 | store_lanes(c, dt, 1, p0_out, {y0}); | |
| 26 | 23 | store_lanes(c, dt, 1, p1_out, {y1}); | |
| 27 | 23 | } | |
| 28 | |||
| 29 | 12 | std::pair<Value*, Value*> load_cs(Ctx& c, Value* ct, Value* st, Value* idx) { | |
| 30 | 12 | Value* off = byte_offset(c, idx, 4); | |
| 31 | 12 | Value* cv = c.kb.load_f32(c.b.build_add(ct, off)); | |
| 32 | 12 | Value* sv = c.kb.load_f32(c.b.build_add(st, off)); | |
| 33 | 12 | return {cv, sv}; | |
| 34 | } | ||
| 35 | |||
| 36 | } // namespace | ||
| 37 | |||
| 38 | 20 | Function* build_rope_mir(Module& mod, std::uint32_t mode, bool bwd, Dtype dt) { | |
| 39 | 40 | Function* fn = mod.create_function( | |
| 40 | 20 | "mir_rope", Type::void_type(), | |
| 41 | 20 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 42 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | ||
| 43 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | ||
| 44 | Type::i32(), Type::f32()}); | ||
| 45 | 20 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 46 | 20 | Ctx c(kb); | |
| 47 | 20 | auto p = entry_params(c, fn); | |
| 48 | |||
| 49 | 20 | Value* rows = p[11]; | |
| 50 | 20 | Value* heads = p[12]; | |
| 51 | 20 | Value* hd = p[13]; | |
| 52 | 20 | Value* hp = c.b.build_lshr(hd, c.i32(1)); | |
| 53 | 20 | Value* total = c.b.build_mul(c.b.build_mul(rows, heads), hp); | |
| 54 | 20 | Value* stride_d = c.b.build_mul(heads, hd); | |
| 55 | |||
| 56 | 40 | emit_vec_grid_stride(c, total, 1, kRopeBlock, [&](Value* idx) { | |
| 57 | 20 | Value* rest = c.b.build_udiv(idx, hp); | |
| 58 | 20 | Value* i = c.b.build_sub(idx, c.b.build_mul(rest, hp)); | |
| 59 | 20 | Value* row = c.b.build_udiv(rest, heads); | |
| 60 | 20 | Value* h = c.b.build_sub(rest, c.b.build_mul(row, heads)); | |
| 61 | 20 | Value* pair_off = c.b.build_add(c.b.build_mul(h, hd), c.b.build_shl(i, c.i32(1))); | |
| 62 | |||
| 63 | 20 | if (mode == ROPE_PACKED) { | |
| 64 | 3 | Value* pos = kb.load_i32(ptr_at(c, p[8], row, 4)); | |
| 65 | 6 | kb.if_then(c.b.build_ult(pos, p[15]), [&] { | |
| 66 | 3 | Value* tbl_idx = c.b.build_add(c.b.build_mul(pos, hp), i); | |
| 67 | 15 | auto [cv, sv] = load_cs(c, p[2], p[3], tbl_idx); | |
| 68 | 3 | Value* o = c.b.build_add(c.b.build_mul(row, c.b.build_mul(c.i32(3), stride_d)), pair_off); | |
| 69 | 9 | emit_rotate_pair(c, dt, bwd, p[0], p[0], o, cv, sv); | |
| 70 | 9 | emit_rotate_pair(c, dt, bwd, p[0], p[0], c.b.build_add(o, stride_d), cv, sv); | |
| 71 | 3 | }); | |
| 72 | 3 | return; | |
| 73 | } | ||
| 74 | |||
| 75 | 17 | Value* cv = nullptr; | |
| 76 | 17 | Value* sv = nullptr; | |
| 77 | 17 | if (mode == ROPE_THETA) { | |
| 78 | 10 | Value* hd_f = kb.fmax(kb.i32_to_f32(hd), c.f32(2.0f)); | |
| 79 | 10 | Value* neg_2i = c.b.build_neg(kb.i32_to_f32(c.b.build_shl(i, c.i32(1)))); | |
| 80 | 10 | Value* a = emit_div_rn(c, neg_2i, hd_f); | |
| 81 | 10 | Value* pos_f = kb.i32_to_f32(c.b.build_add(row, p[17])); | |
| 82 | 10 | Value* theta = c.b.build_mul(pos_f, emit_exp_acc(c, c.b.build_mul(a, p[18]))); | |
| 83 | 20 | auto [s_v, c_v] = emit_sincos_acc(c, theta); | |
| 84 | 10 | cv = c_v; | |
| 85 | 10 | sv = s_v; | |
| 86 | 17 | } else if (mode == ROPE_TABLE) { | |
| 87 | 8 | auto [c_v, s_v] = load_cs(c, p[2], p[3], c.b.build_add(c.b.build_mul(row, hp), i)); | |
| 88 | 4 | cv = c_v; | |
| 89 | 4 | sv = s_v; | |
| 90 | 7 | } else if (mode == ROPE_PERHEAD) { | |
| 91 | 2 | Value* tidx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(row, heads), h), hp), i); | |
| 92 | 4 | auto [c_v, s_v] = load_cs(c, p[2], p[3], tidx); | |
| 93 | 2 | cv = c_v; | |
| 94 | 2 | sv = s_v; | |
| 95 | 2 | } else { | |
| 96 | 1 | Value* dt_n = p[14]; | |
| 97 | 1 | Value* dh_n = p[15]; | |
| 98 | 1 | Value* dw_n = p[16]; | |
| 99 | 1 | auto cs = emit_if_else_values( | |
| 100 | 1 | c, c.b.build_ult(i, dt_n), {Type::f32(), Type::f32()}, | |
| 101 | 2 | [&]() -> std::vector<Value*> { | |
| 102 | 1 | Value* pos = kb.load_i32(ptr_at(c, p[8], row, 4)); | |
| 103 | 2 | auto [c_v, s_v] = load_cs(c, p[2], p[3], c.b.build_add(c.b.build_mul(pos, dt_n), i)); | |
| 104 | 2 | return {c_v, s_v}; | |
| 105 | }, | ||
| 106 | 2 | [&]() -> std::vector<Value*> { | |
| 107 | 1 | Value* dth = c.b.build_add(dt_n, dh_n); | |
| 108 | 1 | return emit_if_else_values( | |
| 109 | 1 | c, c.b.build_ult(i, dth), {Type::f32(), Type::f32()}, | |
| 110 | 2 | [&]() -> std::vector<Value*> { | |
| 111 | 1 | Value* pos = kb.load_i32(ptr_at(c, p[9], row, 4)); | |
| 112 | 1 | Value* l = c.b.build_sub(i, dt_n); | |
| 113 | 2 | auto [c_v, s_v] = load_cs(c, p[4], p[5], c.b.build_add(c.b.build_mul(pos, dh_n), l)); | |
| 114 | 2 | return {c_v, s_v}; | |
| 115 | }, | ||
| 116 | 2 | [&]() -> std::vector<Value*> { | |
| 117 | 1 | Value* pos = kb.load_i32(ptr_at(c, p[10], row, 4)); | |
| 118 | 1 | Value* l = c.b.build_sub(i, dth); | |
| 119 | 2 | auto [c_v, s_v] = load_cs(c, p[6], p[7], c.b.build_add(c.b.build_mul(pos, dw_n), l)); | |
| 120 | 2 | return {c_v, s_v}; | |
| 121 | }); | ||
| 122 | ✗ | }); | |
| 123 | 1 | cv = cs[0]; | |
| 124 | 1 | sv = cs[1]; | |
| 125 | 1 | } | |
| 126 | 17 | Value* o = c.b.build_add(c.b.build_mul(row, stride_d), pair_off); | |
| 127 | 17 | emit_rotate_pair(c, dt, bwd, p[0], p[1], o, cv, sv); | |
| 128 | 20 | }); | |
| 129 | 20 | c.b.build_ret_void(); | |
| 130 | 20 | return finalize_fn(fn); | |
| 131 | 20 | } | |
| 132 | |||
| 133 | } // namespace brotensor::kernels | ||
| 134 |