src/kernels/mir_math.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_math.h" | ||
| 2 | |||
| 3 | #include <cstddef> | ||
| 4 | |||
| 5 | namespace brotensor::kernels { | ||
| 6 | |||
| 7 | namespace { | ||
| 8 | |||
| 9 | template <std::size_t N> | ||
| 10 | 823 | Value* horner(Ctx& c, Value* x, const float (&coeffs)[N]) { | |
| 11 | 823 | Value* acc = c.f32(coeffs[N - 1]); | |
| 12 | 4714 | for (std::size_t i = N - 1; i > 0; --i) { | |
| 13 | 3891 | acc = c.b.build_fma_f32(acc, x, c.f32(coeffs[i - 1])); | |
| 14 | 3891 | } | |
| 15 | 823 | return acc; | |
| 16 | } | ||
| 17 | |||
| 18 | } // namespace | ||
| 19 | |||
| 20 | 1948 | Value* emit_exp(Ctx& c, Value* x) { return c.b.build_call("expf", brass::Type::f32(), {x}); } | |
| 21 | 71 | Value* emit_log(Ctx& c, Value* x) { return c.b.build_call("logf", brass::Type::f32(), {x}); } | |
| 22 | 18 | Value* emit_sin(Ctx& c, Value* x) { return c.b.build_call("sinf", brass::Type::f32(), {x}); } | |
| 23 | 18 | Value* emit_cos(Ctx& c, Value* x) { return c.b.build_call("cosf", brass::Type::f32(), {x}); } | |
| 24 | 5 | Value* emit_sqrt(Ctx& c, Value* x) { return c.b.build_call("sqrtf", brass::Type::f32(), {x}); } | |
| 25 | 78 | Value* emit_rsqrt(Ctx& c, Value* x) { return c.b.build_call("rsqrtf", brass::Type::f32(), {x}); } | |
| 26 | 7 | Value* emit_round_even(Ctx& c, Value* x) { return c.b.build_round_f32(x); } | |
| 27 | |||
| 28 | 991 | Value* emit_sigmoid(Ctx& c, Value* x) { | |
| 29 | 991 | Value* nx = c.kb.clamp_f32(c.b.build_neg(x), c.f32(-88.0f), c.f32(88.0f)); | |
| 30 | 991 | Value* d = c.b.build_add(c.f32(1.0f), emit_exp(c, nx)); | |
| 31 | 991 | return c.b.build_sdiv(c.f32(1.0f), d); | |
| 32 | } | ||
| 33 | |||
| 34 | 68 | Value* emit_tanh(Ctx& c, Value* x) { | |
| 35 | 68 | Value* ax = c.kb.fabs(x); | |
| 36 | 68 | Value* x2 = c.b.build_mul(x, x); | |
| 37 | static constexpr float kMac[] = { | ||
| 38 | 1.0f, -1.0f / 3.0f, 2.0f / 15.0f, -17.0f / 315.0f, 62.0f / 2835.0f, -1382.0f / 155925.0f}; | ||
| 39 | 68 | Value* small = c.b.build_mul(x, horner(c, x2, kMac)); | |
| 40 | |||
| 41 | 68 | Value* neg_2ax = c.kb.fmax(c.b.build_mul(c.f32(-2.0f), ax), c.f32(-88.0f)); | |
| 42 | 68 | Value* t = emit_exp(c, neg_2ax); | |
| 43 | 68 | Value* mag = c.b.build_sdiv(c.b.build_sub(c.f32(1.0f), t), c.b.build_add(c.f32(1.0f), t)); | |
| 44 | 68 | Value* large = c.b.build_select(c.b.build_slt(x, c.f32(0.0f)), c.b.build_neg(mag), mag); | |
| 45 | 68 | return c.b.build_select(c.b.build_slt(ax, c.f32(0.25f)), small, large); | |
| 46 | } | ||
| 47 | |||
| 48 | // Single-precision erf, under 1 ulp: |x| <= 0.927734375 is x + x P(x^2); above, | ||
| 49 | // 1 - exp(Q(|x|)) with the sign restored. No division (an epilogue that runs it | ||
| 50 | // per element, a GEMM's GeGLU, stays cheap); the exp underflows to 0 for large | ||
| 51 | // |x|, giving exactly 1. | ||
| 52 | 615 | Value* emit_erf(Ctx& c, Value* x) { | |
| 53 | 615 | Value* ax = c.kb.fabs(x); | |
| 54 | 615 | Value* s = c.b.build_mul(x, x); | |
| 55 | static constexpr float kSmall[] = { | ||
| 56 | 1.28379166e-1f, -3.76125336e-1f, 1.12819925e-1f, -2.67681349e-2f, | ||
| 57 | 4.99119423e-3f, -5.96761703e-4f}; | ||
| 58 | 615 | Value* small = c.b.build_fma_f32(horner(c, s, kSmall), x, x); | |
| 59 | |||
| 60 | 615 | Value* r = c.b.build_fma_f32(c.f32(-1.72853470e-5f), ax, c.f32(3.83197126e-4f)); | |
| 61 | 615 | Value* u = c.b.build_fma_f32(c.f32(-3.88396438e-3f), ax, c.f32(2.42546219e-2f)); | |
| 62 | 615 | r = c.b.build_fma_f32(r, s, u); | |
| 63 | 615 | r = c.b.build_fma_f32(r, ax, c.f32(-1.06777877e-1f)); | |
| 64 | 615 | r = c.b.build_fma_f32(r, ax, c.f32(-6.34846687e-1f)); | |
| 65 | 615 | r = c.b.build_fma_f32(r, ax, c.f32(-1.28717512e-1f)); | |
| 66 | 615 | r = c.b.build_fma_f32(r, ax, c.b.build_neg(ax)); | |
| 67 | 615 | Value* mag = c.b.build_sub(c.f32(1.0f), emit_exp(c, r)); | |
| 68 | 615 | Value* large = c.b.build_select(c.b.build_slt(x, c.f32(0.0f)), c.b.build_neg(mag), mag); | |
| 69 | 615 | return c.b.build_select(c.b.build_sgt(ax, c.f32(0.927734375f)), large, small); | |
| 70 | } | ||
| 71 | |||
| 72 | 133 | Value* emit_gelu_tanh(Ctx& c, Value* x) { | |
| 73 | 133 | constexpr float kGeluC = 0.7978845608028654f; | |
| 74 | 133 | Value* xc = c.kb.clamp_f32(x, c.f32(-20.0f), c.f32(20.0f)); | |
| 75 | 133 | Value* xc3 = c.b.build_mul(c.b.build_mul(xc, xc), xc); | |
| 76 | 133 | Value* inner = c.b.build_fma_f32(xc3, c.f32(0.044715f), xc); | |
| 77 | 133 | Value* u2 = c.b.build_mul(c.f32(2.0f * kGeluC), inner); | |
| 78 | 133 | return c.b.build_mul(x, emit_sigmoid(c, u2)); | |
| 79 | } | ||
| 80 | |||
| 81 | 28 | Value* emit_gelu_tanh_grad(Ctx& c, Value* x) { | |
| 82 | 28 | constexpr float kGeluC = 0.7978845608028654f; | |
| 83 | 28 | Value* xc = c.kb.clamp_f32(x, c.f32(-20.0f), c.f32(20.0f)); | |
| 84 | 28 | Value* x2 = c.b.build_mul(xc, xc); | |
| 85 | 28 | Value* inner = c.b.build_fma_f32(c.b.build_mul(x2, xc), c.f32(0.044715f), xc); | |
| 86 | 28 | Value* s = emit_sigmoid(c, c.b.build_mul(c.f32(2.0f * kGeluC), inner)); | |
| 87 | 28 | Value* d_inner = c.b.build_mul(c.f32(kGeluC), c.b.build_fma_f32(x2, c.f32(3.0f * 0.044715f), c.f32(1.0f))); | |
| 88 | 28 | Value* s1s = c.b.build_mul(s, c.b.build_sub(c.f32(1.0f), s)); | |
| 89 | 28 | return c.b.build_fma_f32(c.b.build_mul(c.f32(2.0f), xc), c.b.build_mul(s1s, d_inner), s); | |
| 90 | } | ||
| 91 | |||
| 92 | 579 | Value* emit_gelu_exact(Ctx& c, Value* x) { | |
| 93 | 579 | Value* erf_v = emit_erf(c, c.b.build_mul(x, c.f32(0.70710678118654752440f))); | |
| 94 | 579 | return c.b.build_mul(c.b.build_mul(c.f32(0.5f), x), c.b.build_add(c.f32(1.0f), erf_v)); | |
| 95 | } | ||
| 96 | |||
| 97 | 2511 | Value* emit_gemm_act(Ctx& c, std::uint32_t act, Value* v) { | |
| 98 | 2511 | switch (act) { | |
| 99 | 156 | case 1: return c.kb.fmax(v, c.f32(0.0f)); // LACT_RELU | |
| 100 | 83 | case 2: return emit_gelu_tanh(c, v); // LACT_GELU_TANH | |
| 101 | 373 | case 3: return emit_gelu_exact(c, v); // LACT_GELU_EXACT | |
| 102 | 376 | case 4: return c.b.build_mul(v, emit_sigmoid(c, v)); // LACT_SILU | |
| 103 | 20 | case 5: return c.b.build_mul(v, emit_sigmoid(c, c.b.build_mul(v, c.f32(1.702f)))); // LACT_QUICK_GELU | |
| 104 | 1503 | default: return v; | |
| 105 | } | ||
| 106 | 2511 | } | |
| 107 | |||
| 108 | 266 | Value* emit_div_rn(Ctx& c, Value* a, Value* b_val) { | |
| 109 | 266 | Value* r = c.b.build_sdiv(c.f32(1.0f), b_val); | |
| 110 | 266 | Value* q = c.b.build_mul(a, r); | |
| 111 | 266 | return c.b.build_fma_f32(c.b.build_fma_f32(c.b.build_neg(q), b_val, a), r, q); | |
| 112 | } | ||
| 113 | |||
| 114 | // exp(t), accurate to ~1 ulp. The range reduction needs a finite t: at | ||
| 115 | // t = -inf, n = round(t * log2e) = -inf and fma(-n, ln2_hi, t) is NaN, which a | ||
| 116 | // masked softmax or a sampler with suppressed (-inf) logits then sorts or sums | ||
| 117 | // as a real value. Below -104 (exp < 2^-150) the result is 0, above 88.7228 | ||
| 118 | // (exp > FLT_MAX) +inf, and the reduction runs on t clamped into that range so | ||
| 119 | // n stays where ldexp is exact. A NaN t compares false both ways and stays NaN. | ||
| 120 | 28 | Value* emit_exp_acc(Ctx& c, Value* t_in) { | |
| 121 | 28 | Value* under = c.b.build_slt(t_in, c.f32(-104.0f)); | |
| 122 | 28 | Value* over = c.b.build_sgt(t_in, c.f32(88.72283935546875f)); | |
| 123 | 28 | Value* t = c.b.build_select(under, c.f32(-104.0f), c.b.build_select(over, c.f32(88.0f), t_in)); | |
| 124 | 28 | Value* n = c.b.build_round_f32(c.b.build_mul(t, c.f32(1.44269504088896341f))); | |
| 125 | 28 | Value* neg_n = c.b.build_neg(n); | |
| 126 | 28 | Value* r = c.b.build_fma_f32(neg_n, c.f32(0.693145751953125f), t); | |
| 127 | 28 | r = c.b.build_fma_f32(neg_n, c.f32(1.428606765330187045e-06f), r); | |
| 128 | static constexpr float kExpPoly[] = { | ||
| 129 | 1.0f, 1.0f, 0.5f, 1.0f / 6.0f, 1.0f / 24.0f, 1.0f / 120.0f, 1.0f / 720.0f, 1.0f / 5040.0f}; | ||
| 130 | 28 | Value* p = horner(c, r, kExpPoly); | |
| 131 | 28 | Value* e = c.kb.ldexp_f32(p, c.kb.f32_to_i32(n)); | |
| 132 | 28 | Value* inf = jit::gpu_mir::bits_to_f32(c, c.i32(0x7f800000)); | |
| 133 | 28 | return c.b.build_select(under, c.f32(0.0f), c.b.build_select(over, inf, e)); | |
| 134 | } | ||
| 135 | |||
| 136 | 28 | Value* emit_log_acc(Ctx& c, Value* x) { | |
| 137 | 28 | Value* bits = jit::gpu_mir::f32_to_bits(c, x); | |
| 138 | 28 | Value* exp_bits = c.b.build_and(c.b.build_lshr(bits, c.i32(23)), c.i32(0xff)); | |
| 139 | 28 | Value* e_raw = c.b.build_sub(exp_bits, c.i32(126)); | |
| 140 | 28 | Value* m_bits = c.b.build_or(c.b.build_and(bits, c.i32(0x007fffff)), c.i32(0x3f000000)); | |
| 141 | 28 | Value* m_raw = jit::gpu_mir::bits_to_f32(c, m_bits); | |
| 142 | 28 | Value* low = c.b.build_slt(m_raw, c.f32(0.70710678118654752f)); | |
| 143 | 28 | Value* m = c.b.build_select(low, c.b.build_mul(m_raw, c.f32(2.0f)), m_raw); | |
| 144 | 28 | Value* e = c.b.build_select(low, c.b.build_sub(e_raw, c.i32(1)), e_raw); | |
| 145 | 28 | Value* s = emit_div_rn(c, c.b.build_sub(m, c.f32(1.0f)), c.b.build_add(m, c.f32(1.0f))); | |
| 146 | 28 | Value* z = c.b.build_mul(s, s); | |
| 147 | static constexpr float kLogPoly[] = {2.0f / 3.0f, 2.0f / 5.0f, 2.0f / 7.0f, 2.0f / 9.0f, 2.0f / 11.0f}; | ||
| 148 | 28 | Value* lm = c.b.build_fma_f32(c.b.build_mul(s, z), horner(c, z, kLogPoly), c.b.build_mul(c.f32(2.0f), s)); | |
| 149 | 28 | Value* fe = c.kb.i32_to_f32(e); | |
| 150 | 56 | return c.b.build_fma_f32( | |
| 151 | 28 | fe, c.f32(0.693145751953125f), c.b.build_fma_f32(fe, c.f32(1.428606765330187045e-06f), lm)); | |
| 152 | } | ||
| 153 | |||
| 154 | 42 | std::pair<Value*, Value*> emit_sincos_acc(Ctx& c, Value* x) { | |
| 155 | 42 | Value* j = c.b.build_round_f32(c.b.build_mul(x, c.f32(0.636619772367581343f))); | |
| 156 | 42 | Value* neg_j = c.b.build_neg(j); | |
| 157 | 42 | Value* y = c.b.build_fma_f32(neg_j, c.f32(1.57079637050628662f), x); | |
| 158 | 42 | y = c.b.build_fma_f32(neg_j, c.f32(-4.37113882867379e-08f), y); | |
| 159 | 42 | y = c.b.build_fma_f32(neg_j, c.f32(-1.71512451e-15f), y); | |
| 160 | 42 | Value* z = c.b.build_mul(y, y); | |
| 161 | static constexpr float kSinPoly[] = {-1.6666654611e-1f, 8.3321608736e-3f, -1.9515295891e-4f}; | ||
| 162 | static constexpr float kCosPoly[] = {4.166664568298827e-2f, -1.388731625493765e-3f, 2.443315711809948e-5f}; | ||
| 163 | 42 | Value* sp = c.b.build_fma_f32(c.b.build_mul(y, z), horner(c, z, kSinPoly), y); | |
| 164 | 84 | Value* cp = c.b.build_fma_f32(c.b.build_mul(z, z), horner(c, z, kCosPoly), | |
| 165 | 42 | c.b.build_fma_f32(c.f32(-0.5f), z, c.f32(1.0f))); | |
| 166 | 42 | Value* nsp = c.b.build_neg(sp); | |
| 167 | 42 | Value* ncp = c.b.build_neg(cp); | |
| 168 | 42 | Value* q = c.b.build_and(c.kb.f32_to_i32(j), c.i32(3)); | |
| 169 | 42 | Value* is0 = c.b.build_eq(q, c.i32(0)); | |
| 170 | 42 | Value* is1 = c.b.build_eq(q, c.i32(1)); | |
| 171 | 42 | Value* is2 = c.b.build_eq(q, c.i32(2)); | |
| 172 | 42 | Value* sin_v = c.b.build_select(is0, sp, c.b.build_select(is1, cp, c.b.build_select(is2, nsp, ncp))); | |
| 173 | 42 | Value* cos_v = c.b.build_select(is0, cp, c.b.build_select(is1, nsp, c.b.build_select(is2, ncp, sp))); | |
| 174 | 42 | return {sin_v, cos_v}; | |
| 175 | } | ||
| 176 | |||
| 177 | namespace { | ||
| 178 | |||
| 179 | 325 | Value* emit_block_reduce_32x8(Ctx& c, Value* scratch8, Value* v, float ident, bool is_max) { | |
| 180 | 3575 | auto comb = [&](Value* a, Value* b_val) -> Value* { | |
| 181 | 3250 | return is_max ? c.kb.fmax(a, b_val) : c.b.build_add(a, b_val); | |
| 182 | }; | ||
| 183 | 1950 | for (std::uint32_t m = 16; m >= 1; m >>= 1) { | |
| 184 | 1625 | v = comb(v, c.kb.shfl_bfly_f32(v, m)); | |
| 185 | 1625 | } | |
| 186 | 325 | Value* tid = c.kb.tid_x(); | |
| 187 | 325 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 188 | 325 | Value* warp = c.b.build_and(c.b.build_lshr(tid, c.i32(5)), c.i32(7)); | |
| 189 | 650 | c.kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { | |
| 190 | 325 | c.kb.shared_store_f32_indexed(scratch8, warp, v); | |
| 191 | 325 | }); | |
| 192 | 325 | c.kb.sync(); | |
| 193 | 325 | Value* loaded = c.kb.shared_load_f32_indexed(scratch8, c.b.build_and(lane, c.i32(7))); | |
| 194 | 325 | Value* mine = c.b.build_select(c.b.build_ult(lane, c.i32(8)), loaded, c.f32(ident)); | |
| 195 | 1950 | for (std::uint32_t m = 16; m >= 1; m >>= 1) { | |
| 196 | 1625 | mine = comb(mine, c.kb.shfl_bfly_f32(mine, m)); | |
| 197 | 1625 | } | |
| 198 | 325 | c.kb.sync(); | |
| 199 | 325 | return mine; | |
| 200 | ✗ | } | |
| 201 | |||
| 202 | } // namespace | ||
| 203 | |||
| 204 | 278 | Value* emit_block_sum_f32(Ctx& c, Value* scratch8, Value* v) { | |
| 205 | 278 | return emit_block_reduce_32x8(c, scratch8, v, 0.0f, false); | |
| 206 | } | ||
| 207 | |||
| 208 | 47 | Value* emit_block_max_f32(Ctx& c, Value* scratch8, Value* v) { | |
| 209 | 47 | return emit_block_reduce_32x8(c, scratch8, v, -3.4028234663852886e38f, true); | |
| 210 | } | ||
| 211 | |||
| 212 | 13 | Value* emit_block_argmax_f32(Ctx& c, Value* s_val8, Value* s_idx8, Value* best_v, Value* best_i) { | |
| 213 | 169 | auto better = [&](Value* ov, Value* oi, Value* bv, Value* bi) -> Value* { | |
| 214 | 156 | Value* gt = c.b.build_sgt(ov, bv); | |
| 215 | 156 | Value* eq_tie = c.b.build_and(c.b.build_eq(ov, bv), c.b.build_ult(oi, bi)); | |
| 216 | 156 | return c.b.build_or(gt, eq_tie); | |
| 217 | }; | ||
| 218 | 78 | for (std::uint32_t m = 16; m >= 1; m >>= 1) { | |
| 219 | 65 | Value* ov = c.kb.shfl_bfly_f32(best_v, m); | |
| 220 | 65 | Value* oi = c.kb.shfl_bfly_i32(best_i, m); | |
| 221 | 65 | Value* take = better(ov, oi, best_v, best_i); | |
| 222 | 65 | best_v = c.b.build_select(take, ov, best_v); | |
| 223 | 65 | best_i = c.b.build_select(take, oi, best_i); | |
| 224 | 65 | } | |
| 225 | 13 | Value* tid = c.kb.tid_x(); | |
| 226 | 13 | Value* lane = c.b.build_and(tid, c.i32(31)); | |
| 227 | 13 | Value* warp = c.b.build_and(c.b.build_lshr(tid, c.i32(5)), c.i32(7)); | |
| 228 | 26 | c.kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { | |
| 229 | 13 | c.kb.shared_store_f32_indexed(s_val8, warp, best_v); | |
| 230 | 13 | c.kb.shared_store_i32_indexed(s_idx8, warp, best_i); | |
| 231 | 13 | }); | |
| 232 | 13 | c.kb.sync(); | |
| 233 | 13 | Value* bv = c.kb.shared_load_f32_indexed(s_val8, c.i32(0)); | |
| 234 | 13 | Value* bi = c.kb.shared_load_i32_indexed(s_idx8, c.i32(0)); | |
| 235 | 104 | for (int w = 1; w < 8; ++w) { | |
| 236 | 91 | Value* wv = c.kb.shared_load_f32_indexed(s_val8, c.i32(w)); | |
| 237 | 91 | Value* wi = c.kb.shared_load_i32_indexed(s_idx8, c.i32(w)); | |
| 238 | 91 | Value* take = better(wv, wi, bv, bi); | |
| 239 | 91 | bv = c.b.build_select(take, wv, bv); | |
| 240 | 91 | bi = c.b.build_select(take, wi, bi); | |
| 241 | 91 | } | |
| 242 | 13 | c.kb.sync(); | |
| 243 | 13 | return bi; | |
| 244 | ✗ | } | |
| 245 | |||
| 246 | 5 | Value* emit_splitmix64_mix_u01(Ctx& c, Value* z64) { | |
| 247 | 5 | Value* m1 = c.kb.const_i64(static_cast<std::int64_t>(0xBF58476D1CE4E5B9ULL)); | |
| 248 | 5 | Value* m2 = c.kb.const_i64(static_cast<std::int64_t>(0x94D049BB133111EBULL)); | |
| 249 | 5 | Value* z = c.b.build_mul(c.b.build_xor(z64, c.b.build_lshr(z64, c.kb.const_i64(30))), m1); | |
| 250 | 5 | z = c.b.build_mul(c.b.build_xor(z, c.b.build_lshr(z, c.kb.const_i64(27))), m2); | |
| 251 | 5 | z = c.b.build_xor(z, c.b.build_lshr(z, c.kb.const_i64(31))); | |
| 252 | 5 | Value* top24 = c.b.build_trunc_i32(c.b.build_lshr(z, c.kb.const_i64(40))); | |
| 253 | 5 | return c.b.build_mul(c.kb.u32_to_f32(top24), c.f32(1.0f / 16777216.0f)); | |
| 254 | } | ||
| 255 | |||
| 256 | } // namespace brotensor::kernels | ||
| 257 |