src/kernels/mir_xent.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_xent.h" | ||
| 2 | |||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include <string> | ||
| 6 | |||
| 7 | namespace brotensor::kernels { | ||
| 8 | |||
| 9 | namespace { | ||
| 10 | |||
| 11 | 8 | Value* clamp_i(Ctx& c, Value* v, Value* lo, Value* hi) { | |
| 12 | 8 | return c.b.build_select(c.b.build_slt(v, lo), lo, c.b.build_select(c.b.build_sgt(v, hi), hi, v)); | |
| 13 | } | ||
| 14 | |||
| 15 | 15 | Value* is_valid(Ctx& c, Value* mask, Value* at, bool has_mask) { | |
| 16 | 15 | if (!has_mask) return nullptr; | |
| 17 | 15 | return c.b.build_sge(c.kb.load_f32(ptr_at(c, mask, at, 4)), c.f32(0.5f)); | |
| 18 | 15 | } | |
| 19 | |||
| 20 | 4 | Value* emit_log1p_acc(Ctx& c, Value* x) { | |
| 21 | 4 | Value* u = c.b.build_add(c.f32(1.0f), x); | |
| 22 | 4 | Value* exact1 = c.b.build_eq(u, c.f32(1.0f)); | |
| 23 | 4 | Value* log_u = emit_log_acc(c, c.b.build_select(exact1, c.f32(1.0f), u)); | |
| 24 | 4 | Value* ratio = c.b.build_select(exact1, c.f32(1.0f), emit_div_rn(c, x, c.b.build_sub(u, c.f32(1.0f)))); | |
| 25 | 4 | return c.b.build_select(exact1, x, c.b.build_mul(log_u, ratio)); | |
| 26 | } | ||
| 27 | |||
| 28 | } // namespace | ||
| 29 | |||
| 30 | 10 | brass::Function* build_xent_mir(brass::Module& mod, std::uint32_t mode, bool has_mask, bool has_off) { | |
| 31 | 20 | const std::string name = "mir_xent_" + std::to_string(mode) + "_m" + std::to_string(has_mask ? 1 : 0) + | |
| 32 | 10 | "_o" + std::to_string(has_off ? 1 : 0); | |
| 33 | 20 | auto* fn = mod.create_function( | |
| 34 | 10 | name, Type::void_type(), | |
| 35 | 50 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 36 | 40 | Type::i32(), Type::i32(), Type::i32(), Type::f32()}); | |
| 37 | 10 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 38 | 10 | Ctx c(kb); | |
| 39 | 10 | auto p = entry_params(c, fn); | |
| 40 | 10 | Value *logits = p[0], *target = p[1], *mask = p[2], *off = p[3]; | |
| 41 | 10 | Value *probs = p[4], *dlog = p[5], *loss_buf = p[6]; | |
| 42 | 10 | Value *rows = p[7], *n = p[8], *heads = p[9], *pos_w = p[10]; | |
| 43 | |||
| 44 | 10 | Value* tid = c.kb.tid_x(); | |
| 45 | 10 | Value* scratch8 = c.kb.shared_alloc_f32(8); | |
| 46 | 10 | Value* bx = c.kb.ctaid_x(); | |
| 47 | 10 | Value* nbx = c.kb.nctaid_x(); | |
| 48 | 10 | Value* block_sz = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock)); | |
| 49 | |||
| 50 | 20 | c.kb.for_range(bx, rows, nbx, [&](Value* b) { | |
| 51 | 10 | Value* ro = c.b.build_mul(b, n); | |
| 52 | 10 | Value* row_loss = c.f32(0.0f); | |
| 53 | 10 | if (mode == 1u) { | |
| 54 | 8 | row_loss = c.kb.for_range_reduce(tid, n, block_sz, c.f32(0.0f), [&](Value* i, Value* acc) { | |
| 55 | 4 | Value* at = c.b.build_add(ro, i); | |
| 56 | 4 | Value* p_at = ptr_at(c, probs, at, 4); | |
| 57 | 4 | Value* d_at = ptr_at(c, dlog, at, 4); | |
| 58 | 8 | auto eval = [&]() -> Value* { | |
| 59 | 4 | Value* z = c.kb.load_f32(ptr_at(c, logits, at, 4)); | |
| 60 | 4 | Value* y = c.kb.load_f32(ptr_at(c, target, at, 4)); | |
| 61 | 4 | Value* e = emit_exp_acc(c, c.b.build_neg(c.kb.fabs(z))); | |
| 62 | 4 | Value* l1 = emit_log1p_acc(c, e); | |
| 63 | 8 | Value* sp_neg = c.b.build_add( | |
| 64 | 4 | c.b.build_select(c.b.build_sgt(z, c.f32(0.0f)), c.f32(0.0f), c.b.build_neg(z)), l1); | |
| 65 | 8 | Value* sp_pos = c.b.build_add( | |
| 66 | 4 | c.b.build_select(c.b.build_sgt(z, c.f32(0.0f)), z, c.f32(0.0f)), l1); | |
| 67 | 4 | Value* one_my = c.b.build_sub(c.f32(1.0f), y); | |
| 68 | 4 | Value* wy = c.b.build_mul(pos_w, y); | |
| 69 | 4 | Value* loss_i = c.b.build_add(c.b.build_mul(wy, sp_neg), c.b.build_mul(one_my, sp_pos)); | |
| 70 | 4 | Value* denom = c.b.build_add(c.f32(1.0f), e); | |
| 71 | 8 | Value* sig = c.b.build_select(c.b.build_sge(z, c.f32(0.0f)), | |
| 72 | 4 | emit_div_rn(c, c.f32(1.0f), denom), emit_div_rn(c, e, denom)); | |
| 73 | 4 | c.kb.store_f32(p_at, sig); | |
| 74 | 4 | c.kb.store_f32(d_at, c.b.build_sub(c.b.build_mul(sig, c.b.build_add(wy, one_my)), wy)); | |
| 75 | 4 | return c.b.build_add(acc, loss_i); | |
| 76 | }; | ||
| 77 | 4 | if (!has_mask) return eval(); | |
| 78 | 3 | return emit_if_else_values( | |
| 79 | 3 | c, is_valid(c, mask, at, true), {Type::f32()}, | |
| 80 | 6 | [&]() -> std::vector<Value*> { return {eval()}; }, | |
| 81 | 6 | [&]() -> std::vector<Value*> { | |
| 82 | 3 | c.kb.store_f32(p_at, c.f32(0.0f)); | |
| 83 | 3 | c.kb.store_f32(d_at, c.f32(0.0f)); | |
| 84 | 3 | return {acc}; | |
| 85 | })[0]; | ||
| 86 | 4 | }); | |
| 87 | 4 | } else { | |
| 88 | 12 | row_loss = c.kb.for_range_reduce(c.i32(0), heads, c.i32(1), c.f32(0.0f), [&](Value* h, Value* h_loss) { | |
| 89 | 6 | Value* s = c.i32(0); | |
| 90 | 6 | Value* e = n; | |
| 91 | 6 | if (has_off) { | |
| 92 | 4 | s = clamp_i(c, c.kb.load_i32(ptr_at(c, off, h, 4)), c.i32(0), n); | |
| 93 | 4 | e = clamp_i(c, c.kb.load_i32(ptr_at(c, off, c.b.build_add(h, c.i32(1)), 4)), s, n); | |
| 94 | 4 | } | |
| 95 | 6 | Value* start_i = c.b.build_add(s, tid); | |
| 96 | 12 | Value* m_loc = c.kb.for_range_reduce(start_i, e, block_sz, c.f32(-1e30f), [&](Value* i, Value* mx) { | |
| 97 | 6 | Value* at = c.b.build_add(ro, i); | |
| 98 | 6 | Value* lv = c.kb.load_f32(ptr_at(c, logits, at, 4)); | |
| 99 | 6 | if (!has_mask) return c.kb.fmax(mx, lv); | |
| 100 | 4 | return c.b.build_select(is_valid(c, mask, at, true), c.kb.fmax(mx, lv), mx); | |
| 101 | 6 | }); | |
| 102 | 6 | Value* m = emit_block_max_f32(c, scratch8, m_loc); | |
| 103 | |||
| 104 | 12 | Value* sum_loc = c.kb.for_range_reduce(start_i, e, block_sz, c.f32(0.0f), [&](Value* i, Value* sm) { | |
| 105 | 6 | Value* at = c.b.build_add(ro, i); | |
| 106 | 6 | Value* ev = emit_exp(c, c.b.build_sub(c.kb.load_f32(ptr_at(c, logits, at, 4)), m)); | |
| 107 | 6 | if (!has_mask) return c.b.build_add(sm, ev); | |
| 108 | 4 | return c.b.build_select(is_valid(c, mask, at, true), c.b.build_add(sm, ev), sm); | |
| 109 | 6 | }); | |
| 110 | 6 | Value* sum = emit_block_sum_f32(c, scratch8, sum_loc); | |
| 111 | 12 | Value* inv = c.b.build_select(c.b.build_sgt(sum, c.f32(0.0f)), | |
| 112 | 6 | c.b.build_sdiv(c.f32(1.0f), sum), c.f32(0.0f)); | |
| 113 | |||
| 114 | 12 | return c.kb.for_range_reduce(start_i, e, block_sz, h_loss, [&](Value* i, Value* l_acc) { | |
| 115 | 6 | Value* at = c.b.build_add(ro, i); | |
| 116 | 6 | Value* p_at = ptr_at(c, probs, at, 4); | |
| 117 | 6 | Value* d_at = ptr_at(c, dlog, at, 4); | |
| 118 | 12 | auto write_elem = [&]() -> Value* { | |
| 119 | 12 | Value* pv = c.b.build_mul( | |
| 120 | 6 | emit_exp(c, c.b.build_sub(c.kb.load_f32(ptr_at(c, logits, at, 4)), m)), inv); | |
| 121 | 6 | Value* tv = c.kb.load_f32(ptr_at(c, target, at, 4)); | |
| 122 | 6 | c.kb.store_f32(p_at, pv); | |
| 123 | 6 | c.kb.store_f32(d_at, c.b.build_sub(pv, tv)); | |
| 124 | 6 | Value* term = c.b.build_mul(tv, emit_log(c, c.kb.fmax(pv, c.f32(1e-12f)))); | |
| 125 | 12 | return c.b.build_select(c.b.build_sgt(tv, c.f32(0.0f)), | |
| 126 | 6 | c.b.build_sub(l_acc, term), l_acc); | |
| 127 | }; | ||
| 128 | 6 | if (!has_mask) return write_elem(); | |
| 129 | 4 | return emit_if_else_values( | |
| 130 | 4 | c, is_valid(c, mask, at, true), {Type::f32()}, | |
| 131 | 8 | [&]() -> std::vector<Value*> { return {write_elem()}; }, | |
| 132 | 8 | [&]() -> std::vector<Value*> { | |
| 133 | 4 | c.kb.store_f32(p_at, c.f32(0.0f)); | |
| 134 | 4 | c.kb.store_f32(d_at, c.f32(0.0f)); | |
| 135 | 4 | return {l_acc}; | |
| 136 | })[0]; | ||
| 137 | 6 | }); | |
| 138 | ✗ | }); | |
| 139 | } | ||
| 140 | 10 | Value* total_loss = emit_block_sum_f32(c, scratch8, row_loss); | |
| 141 | 20 | c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { | |
| 142 | 10 | c.kb.store_f32(ptr_at(c, loss_buf, b, 4), total_loss); | |
| 143 | 10 | }); | |
| 144 | 10 | }); | |
| 145 | |||
| 146 | 10 | c.b.build_ret_void(); | |
| 147 | 10 | return finalize_fn(fn); | |
| 148 | 10 | } | |
| 149 | |||
| 150 | } // namespace brotensor::kernels | ||
| 151 |