src/kernels/mir_elementwise.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_elementwise.h" | ||
| 2 | #include "mir_common.h" | ||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <stdexcept> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | namespace brotensor::kernels { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | using jit::gpu_mir::bits_to_f32; | ||
| 15 | using jit::gpu_mir::f32_to_bits; | ||
| 16 | |||
| 17 | 192 | void emit_vec_grid_stride_ew(Ctx& c, Value* n_elems, int lanes, | |
| 18 | const std::function<void(Value* vec_idx)>& body) { | ||
| 19 | 192 | emit_vec_grid_stride(c, n_elems, lanes, kEwBlock, body); | |
| 20 | 192 | } | |
| 21 | |||
| 22 | 245 | Value* unary_lane(Ctx& c, std::uint32_t op, Value* v, Value* a, Value* b_val) { | |
| 23 | 245 | switch (op) { | |
| 24 | ✗ | case UOP_COPY: return v; | |
| 25 | 13 | case UOP_RELU: return c.kb.fmax(v, c.f32(0.0f)); | |
| 26 | 13 | case UOP_TANH: return emit_tanh(c, v); | |
| 27 | 13 | case UOP_SIGMOID: return emit_sigmoid(c, v); | |
| 28 | 30 | case UOP_SILU: return c.b.build_mul(v, emit_sigmoid(c, v)); | |
| 29 | 14 | case UOP_GELU_TANH: return emit_gelu_tanh(c, v); | |
| 30 | 14 | case UOP_GELU_EXACT: return emit_gelu_exact(c, v); | |
| 31 | 14 | case UOP_QUICK_GELU: return c.b.build_mul(v, emit_sigmoid(c, c.b.build_mul(c.f32(1.702f), v))); | |
| 32 | 5 | case UOP_EXP: return emit_exp(c, v); | |
| 33 | 5 | case UOP_LOG: return emit_log(c, v); | |
| 34 | 13 | case UOP_SIN: return emit_sin(c, v); | |
| 35 | 13 | case UOP_COS: return emit_cos(c, v); | |
| 36 | 9 | case UOP_RSQRT: return emit_rsqrt(c, v); | |
| 37 | 5 | case UOP_ROUND: return emit_round_even(c, v); | |
| 38 | case UOP_ELU: { | ||
| 39 | 5 | Value* neg = c.b.build_mul(a, c.b.build_sub(emit_exp(c, v), c.f32(1.0f))); | |
| 40 | 5 | return c.b.build_select(c.b.build_sgt(v, c.f32(0.0f)), v, neg); | |
| 41 | } | ||
| 42 | case UOP_LEAKY_RELU: | ||
| 43 | 22 | return c.b.build_select(c.b.build_sgt(v, c.f32(0.0f)), v, c.b.build_mul(a, v)); | |
| 44 | 21 | case UOP_ADD_SCALAR: return c.b.build_add(v, a); | |
| 45 | 15 | case UOP_SCALE: return c.b.build_mul(v, a); | |
| 46 | 21 | case UOP_CLAMP: return c.kb.clamp_f32(v, a, b_val); | |
| 47 | ✗ | default: throw std::runtime_error("brotensor: unknown unary op"); | |
| 48 | } | ||
| 49 | 245 | } | |
| 50 | |||
| 51 | 54 | Value* binary_lane(Ctx& c, std::uint32_t op, Value* yv, Value* xv, Value* a, Value* b_val, Value* zero_mask) { | |
| 52 | 54 | switch (op) { | |
| 53 | 30 | case BOP_ADD: return c.b.build_add(yv, xv); | |
| 54 | 23 | case BOP_MUL: return c.b.build_mul(yv, xv); | |
| 55 | ✗ | case BOP_DIV: return c.b.build_sdiv(yv, xv); | |
| 56 | case BOP_AXPBY: { | ||
| 57 | // Pin both products so neither MIR nor driver FMA-contracts (a*y + b*x), matching CPU FP32. | ||
| 58 | 3 | auto pin = [&](Value* v) { | |
| 59 | 2 | return bits_to_f32(c, c.b.build_xor(f32_to_bits(c, v), zero_mask)); | |
| 60 | }; | ||
| 61 | 1 | return c.b.build_add(pin(c.b.build_mul(a, yv)), pin(c.b.build_mul(b_val, xv))); | |
| 62 | } | ||
| 63 | ✗ | default: throw std::runtime_error("brotensor: unknown binary op"); | |
| 64 | } | ||
| 65 | 54 | } | |
| 66 | |||
| 67 | 165 | Value* act_bwd_lane(Ctx& c, std::uint32_t op, Value* v, Value* g, Value* a) { | |
| 68 | 165 | switch (op) { | |
| 69 | case GOP_RELU: | ||
| 70 | 28 | return c.b.build_select(c.b.build_sgt(v, c.f32(0.0f)), g, c.f32(0.0f)); | |
| 71 | case GOP_TANH: { | ||
| 72 | 5 | Value* vc = c.kb.clamp_f32(v, c.f32(-1.0f), c.f32(1.0f)); | |
| 73 | 5 | return c.b.build_mul(g, c.b.build_sub(c.f32(1.0f), c.b.build_mul(vc, vc))); | |
| 74 | } | ||
| 75 | case GOP_SIGMOID: { | ||
| 76 | 5 | Value* vc = c.kb.clamp_f32(v, c.f32(0.0f), c.f32(1.0f)); | |
| 77 | 5 | return c.b.build_mul(g, c.b.build_mul(vc, c.b.build_sub(c.f32(1.0f), vc))); | |
| 78 | } | ||
| 79 | case GOP_SILU: { | ||
| 80 | 42 | Value* vc = c.kb.clamp_f32(v, c.f32(-88.0f), c.f32(88.0f)); | |
| 81 | 42 | Value* s = emit_sigmoid(c, vc); | |
| 82 | 42 | Value* inner = c.b.build_fma_f32(vc, c.b.build_sub(c.f32(1.0f), s), c.f32(1.0f)); | |
| 83 | 42 | return c.b.build_mul(g, c.b.build_mul(s, inner)); | |
| 84 | } | ||
| 85 | case GOP_GELU_TANH: | ||
| 86 | 14 | return c.b.build_mul(g, emit_gelu_tanh_grad(c, v)); | |
| 87 | case GOP_GELU_EXACT: { | ||
| 88 | 14 | Value* vc = c.kb.clamp_f32(v, c.f32(-20.0f), c.f32(20.0f)); | |
| 89 | 28 | Value* cdf = c.b.build_fma_f32(emit_erf(c, c.b.build_mul(vc, c.f32(0.7071067811865475f))), | |
| 90 | 14 | c.f32(0.5f), c.f32(0.5f)); | |
| 91 | 14 | Value* pdf = emit_exp(c, c.b.build_mul(c.f32(-0.5f), c.b.build_mul(vc, vc))); | |
| 92 | 14 | Value* scale = c.b.build_fma_f32(vc, c.b.build_mul(c.f32(0.3989422804014327f), pdf), cdf); | |
| 93 | 14 | return c.b.build_mul(g, scale); | |
| 94 | } | ||
| 95 | case GOP_QUICK_GELU: { | ||
| 96 | 14 | Value* vc = c.kb.clamp_f32(v, c.f32(-50.0f), c.f32(50.0f)); | |
| 97 | 14 | Value* kx = c.b.build_mul(c.f32(1.702f), vc); | |
| 98 | 14 | Value* s = emit_sigmoid(c, kx); | |
| 99 | 14 | Value* scale = c.b.build_fma_f32(kx, c.b.build_mul(s, c.b.build_sub(c.f32(1.0f), s)), s); | |
| 100 | 14 | return c.b.build_mul(g, scale); | |
| 101 | } | ||
| 102 | case GOP_EXP: | ||
| 103 | 5 | return c.b.build_mul(g, emit_exp(c, c.kb.clamp_f32(v, c.f32(-88.0f), c.f32(88.0f)))); | |
| 104 | case GOP_LOG: | ||
| 105 | 5 | return c.b.build_sdiv(g, c.kb.fmax(v, c.f32(1e-37f))); | |
| 106 | case GOP_SIN: | ||
| 107 | 5 | return c.b.build_mul(g, emit_cos(c, v)); | |
| 108 | case GOP_COS: | ||
| 109 | 5 | return c.b.build_mul(c.b.build_neg(g), emit_sin(c, v)); | |
| 110 | case GOP_RSQRT: { | ||
| 111 | 9 | Value* v3 = c.b.build_mul(c.b.build_mul(v, v), v); | |
| 112 | 9 | return c.b.build_mul(c.f32(-0.5f), c.b.build_mul(g, v3)); | |
| 113 | } | ||
| 114 | case GOP_ELU: { | ||
| 115 | 5 | Value* neg = c.b.build_mul(a, emit_exp(c, c.kb.clamp_f32(v, c.f32(-88.0f), c.f32(0.0f)))); | |
| 116 | 5 | return c.b.build_mul(g, c.b.build_select(c.b.build_sgt(v, c.f32(0.0f)), c.f32(1.0f), neg)); | |
| 117 | } | ||
| 118 | case GOP_LEAKY_RELU: | ||
| 119 | 5 | return c.b.build_mul(g, c.b.build_select(c.b.build_sgt(v, c.f32(0.0f)), c.f32(1.0f), a)); | |
| 120 | case GOP_PASS: | ||
| 121 | 4 | return g; | |
| 122 | default: | ||
| 123 | ✗ | throw std::runtime_error("brotensor: unknown act_bwd op"); | |
| 124 | } | ||
| 125 | 165 | } | |
| 126 | |||
| 127 | } // namespace | ||
| 128 | |||
| 129 | 64 | Function* build_unary_mir(Module& mod, std::uint32_t op_code, Dtype dt, int lanes) { | |
| 130 | 128 | Function* fn = mod.create_function( | |
| 131 | 64 | "mir_unary", Type::void_type(), | |
| 132 | 64 | {Type::ptr(), Type::ptr(), Type::i32(), Type::f32(), Type::f32()}); | |
| 133 | 64 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 134 | 64 | Ctx c(kb); | |
| 135 | 64 | auto p = entry_params(c, fn); | |
| 136 | 64 | const int esz = elem_bytes(dt); | |
| 137 | 128 | emit_vec_grid_stride_ew(c, p[2], lanes, [&](Value* k) { | |
| 138 | 64 | Value* x_ptr = vec_ptr_at(c, p[0], k, lanes, esz); | |
| 139 | 64 | Value* y_ptr = vec_ptr_at(c, p[1], k, lanes, esz); | |
| 140 | 64 | auto xs = load_lanes(c, dt, lanes, x_ptr); | |
| 141 | 64 | std::vector<Value*> ys(static_cast<std::size_t>(lanes)); | |
| 142 | 309 | for (int j = 0; j < lanes; ++j) { | |
| 143 | 245 | ys[static_cast<std::size_t>(j)] = unary_lane(c, op_code, xs[static_cast<std::size_t>(j)], p[3], p[4]); | |
| 144 | 245 | } | |
| 145 | 64 | store_lanes(c, dt, lanes, y_ptr, ys); | |
| 146 | 64 | }); | |
| 147 | 64 | c.b.build_ret_void(); | |
| 148 | 64 | return finalize_fn(fn); | |
| 149 | 64 | } | |
| 150 | |||
| 151 | 14 | Function* build_binary_mir(Module& mod, std::uint32_t op_code, Dtype dt_y, Dtype dt_x, int lanes) { | |
| 152 | 28 | Function* fn = mod.create_function( | |
| 153 | 14 | "mir_binary", Type::void_type(), | |
| 154 | 14 | {Type::ptr(), Type::ptr(), Type::i32(), Type::f32(), Type::f32()}); | |
| 155 | 14 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 156 | 14 | Ctx c(kb); | |
| 157 | 14 | auto p = entry_params(c, fn); | |
| 158 | 14 | const int esz_y = elem_bytes(dt_y); | |
| 159 | 14 | const int esz_x = elem_bytes(dt_x); | |
| 160 | 15 | Value* zero_mask = (op_code == BOP_AXPBY) | |
| 161 | 1 | ? c.b.build_select(c.b.build_eq(p[2], c.i32(0)), c.i32(1), c.i32(0)) | |
| 162 | : nullptr; | ||
| 163 | 28 | emit_vec_grid_stride_ew(c, p[2], lanes, [&](Value* k) { | |
| 164 | 14 | Value* y_ptr = vec_ptr_at(c, p[0], k, lanes, esz_y); | |
| 165 | 14 | Value* x_ptr = vec_ptr_at(c, p[1], k, lanes, esz_x); | |
| 166 | 14 | auto yv = load_lanes(c, dt_y, lanes, y_ptr); | |
| 167 | 14 | auto xv = load_lanes(c, dt_x, lanes, x_ptr); | |
| 168 | 14 | std::vector<Value*> out(static_cast<std::size_t>(lanes)); | |
| 169 | 68 | for (int j = 0; j < lanes; ++j) { | |
| 170 | 54 | const auto js = static_cast<std::size_t>(j); | |
| 171 | 54 | out[js] = binary_lane(c, op_code, yv[js], xv[js], p[3], p[4], zero_mask); | |
| 172 | 54 | } | |
| 173 | 14 | store_lanes(c, dt_y, lanes, y_ptr, out); | |
| 174 | 14 | }); | |
| 175 | 14 | c.b.build_ret_void(); | |
| 176 | 14 | return finalize_fn(fn); | |
| 177 | 14 | } | |
| 178 | |||
| 179 | 48 | Function* build_act_bwd_mir(Module& mod, std::uint32_t op_code, Dtype dt, int lanes) { | |
| 180 | 96 | Function* fn = mod.create_function( | |
| 181 | 48 | "mir_act_bwd", Type::void_type(), | |
| 182 | 48 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::f32()}); | |
| 183 | 48 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 184 | 48 | Ctx c(kb); | |
| 185 | 48 | auto p = entry_params(c, fn); | |
| 186 | 48 | const int esz = elem_bytes(dt); | |
| 187 | 96 | emit_vec_grid_stride_ew(c, p[3], lanes, [&](Value* k) { | |
| 188 | 48 | Value* dy_ptr = vec_ptr_at(c, p[1], k, lanes, esz); | |
| 189 | 48 | Value* dx_ptr = vec_ptr_at(c, p[2], k, lanes, esz); | |
| 190 | 48 | auto g = load_lanes(c, dt, lanes, dy_ptr); | |
| 191 | 48 | std::vector<Value*> v; | |
| 192 | 48 | if (op_code != GOP_PASS) { | |
| 193 | 47 | v = load_lanes(c, dt, lanes, vec_ptr_at(c, p[0], k, lanes, esz)); | |
| 194 | 47 | } | |
| 195 | 48 | std::vector<Value*> out(static_cast<std::size_t>(lanes)); | |
| 196 | 213 | for (int j = 0; j < lanes; ++j) { | |
| 197 | 165 | const auto js = static_cast<std::size_t>(j); | |
| 198 | 165 | out[js] = act_bwd_lane(c, op_code, op_code == GOP_PASS ? c.f32(0.0f) : v[js], g[js], p[4]); | |
| 199 | 165 | } | |
| 200 | 48 | store_lanes(c, dt, lanes, dx_ptr, out); | |
| 201 | 48 | }); | |
| 202 | 48 | c.b.build_ret_void(); | |
| 203 | 48 | return finalize_fn(fn); | |
| 204 | 48 | } | |
| 205 | |||
| 206 | 9 | Function* build_cast_mir(Module& mod, Dtype src_dt, Dtype dst_dt, int lanes) { | |
| 207 | 18 | Function* fn = mod.create_function( | |
| 208 | 9 | "mir_cast", Type::void_type(), | |
| 209 | 9 | {Type::ptr(), Type::ptr(), Type::i32()}); | |
| 210 | 9 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 211 | 9 | Ctx c(kb); | |
| 212 | 9 | auto p = entry_params(c, fn); | |
| 213 | 9 | const int esz_s = elem_bytes(src_dt); | |
| 214 | 9 | const int esz_d = elem_bytes(dst_dt); | |
| 215 | 18 | emit_vec_grid_stride_ew(c, p[2], lanes, [&](Value* k) { | |
| 216 | 9 | auto vals = load_lanes(c, src_dt, lanes, vec_ptr_at(c, p[0], k, lanes, esz_s)); | |
| 217 | 9 | store_lanes(c, dst_dt, lanes, vec_ptr_at(c, p[1], k, lanes, esz_d), vals); | |
| 218 | 9 | }); | |
| 219 | 9 | c.b.build_ret_void(); | |
| 220 | 9 | return finalize_fn(fn); | |
| 221 | 9 | } | |
| 222 | |||
| 223 | 7 | Function* build_bias_mir(Module& mod, std::uint32_t mode, Dtype dt, int lanes) { | |
| 224 | 14 | Function* fn = mod.create_function( | |
| 225 | 7 | "mir_bias", Type::void_type(), | |
| 226 | 7 | {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()}); | |
| 227 | 7 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 228 | 7 | Ctx c(kb); | |
| 229 | 7 | auto p = entry_params(c, fn); | |
| 230 | 7 | const int esz = elem_bytes(dt); | |
| 231 | 7 | const int l = (mode == BIAS_ROW) ? lanes : 1; | |
| 232 | 14 | emit_vec_grid_stride_ew(c, p[2], l, [&](Value* k) { | |
| 233 | 7 | Value* elem_i = (l == 1) ? k : c.b.build_shl(k, c.i32(log2_exact(l))); | |
| 234 | 7 | Value* y_ptr = ptr_at(c, p[0], elem_i, esz); | |
| 235 | 7 | Value* j_idx = (mode == BIAS_ROW) ? c.b.build_umod(elem_i, p[3]) | |
| 236 | 3 | : c.b.build_umod(c.b.build_udiv(elem_i, p[4]), p[3]); | |
| 237 | 7 | Value* b_ptr = ptr_at(c, p[1], j_idx, esz); | |
| 238 | 7 | auto yv = load_lanes(c, dt, l, y_ptr); | |
| 239 | 7 | auto bv = load_lanes(c, dt, l, b_ptr); | |
| 240 | 31 | for (int q = 0; q < l; ++q) { | |
| 241 | 24 | const auto qs = static_cast<std::size_t>(q); | |
| 242 | 24 | yv[qs] = c.b.build_add(yv[qs], bv[qs]); | |
| 243 | 24 | } | |
| 244 | 7 | store_lanes(c, dt, l, y_ptr, yv); | |
| 245 | 7 | }); | |
| 246 | 7 | c.b.build_ret_void(); | |
| 247 | 7 | return finalize_fn(fn); | |
| 248 | 7 | } | |
| 249 | |||
| 250 | 39 | Function* build_glu_mir(Module& mod, std::uint32_t op_code, Dtype dt_in, Dtype dt_out, int lanes) { | |
| 251 | 78 | Function* fn = mod.create_function( | |
| 252 | 39 | "mir_glu", Type::void_type(), | |
| 253 | 39 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()}); | |
| 254 | 39 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 255 | 39 | Ctx c(kb); | |
| 256 | 39 | auto p = entry_params(c, fn); | |
| 257 | 39 | const int esz_in = elem_bytes(dt_in); | |
| 258 | 39 | const int esz_out = elem_bytes(dt_out); | |
| 259 | 39 | const int l = (op_code == GLU_GEGLU_PAIRS) ? 1 : lanes; | |
| 260 | |||
| 261 | 78 | emit_vec_grid_stride_ew(c, p[3], l, [&](Value* k) { | |
| 262 | 39 | Value* elem_i = (l == 1) ? k : c.b.build_shl(k, c.i32(log2_exact(l))); | |
| 263 | 39 | Value* d = p[4]; | |
| 264 | 39 | Value* r = c.b.build_udiv(elem_i, d); | |
| 265 | 39 | Value* col = c.b.build_umod(elem_i, d); | |
| 266 | 39 | Value* row_base = c.b.build_shl(c.b.build_mul(r, d), c.i32(1)); // 2 * r * d | |
| 267 | |||
| 268 | 39 | if (op_code == GLU_GEGLU_PAIRS) { | |
| 269 | 2 | Value* idx = c.b.build_add(row_base, c.b.build_shl(col, c.i32(1))); | |
| 270 | 2 | auto av = load_lanes(c, dt_in, 1, ptr_at(c, p[0], idx, esz_in)); | |
| 271 | 2 | auto bv = load_lanes(c, dt_in, 1, ptr_at(c, p[0], c.b.build_add(idx, c.i32(1)), esz_in)); | |
| 272 | 2 | Value* out = c.b.build_mul(av[0], emit_gelu_exact(c, bv[0])); | |
| 273 | 2 | store_lanes(c, dt_out, 1, ptr_at(c, p[2], elem_i, esz_out), {out}); | |
| 274 | return; | ||
| 275 | 2 | } | |
| 276 | |||
| 277 | 37 | Value* ia = c.b.build_add(row_base, col); | |
| 278 | 37 | Value* ib = c.b.build_add(ia, d); | |
| 279 | 37 | auto a = load_lanes(c, dt_in, l, ptr_at(c, p[0], ia, esz_in)); | |
| 280 | 37 | auto bv = load_lanes(c, dt_in, l, ptr_at(c, p[0], ib, esz_in)); | |
| 281 | |||
| 282 | 37 | if (op_code <= GLU_GEGLU_EXACT) { | |
| 283 | 20 | std::vector<Value*> out(static_cast<std::size_t>(l)); | |
| 284 | 94 | for (int j = 0; j < l; ++j) { | |
| 285 | 74 | const auto js = static_cast<std::size_t>(j); | |
| 286 | 74 | if (op_code == GLU_SWIGLU) { | |
| 287 | 29 | out[js] = c.b.build_mul(c.b.build_mul(a[js], emit_sigmoid(c, a[js])), bv[js]); | |
| 288 | 74 | } else if (op_code == GLU_GEGLU_TANH) { | |
| 289 | 22 | out[js] = c.b.build_mul(a[js], emit_gelu_tanh(c, bv[js])); | |
| 290 | 22 | } else { | |
| 291 | 23 | out[js] = c.b.build_mul(a[js], emit_gelu_exact(c, bv[js])); | |
| 292 | } | ||
| 293 | 74 | } | |
| 294 | 20 | store_lanes(c, dt_out, l, ptr_at(c, p[2], elem_i, esz_out), out); | |
| 295 | 20 | } else { | |
| 296 | 17 | auto g = load_lanes(c, dt_in, l, ptr_at(c, p[1], elem_i, esz_in)); | |
| 297 | 17 | std::vector<Value*> da(static_cast<std::size_t>(l)); | |
| 298 | 17 | std::vector<Value*> db(static_cast<std::size_t>(l)); | |
| 299 | 78 | for (int j = 0; j < l; ++j) { | |
| 300 | 61 | const auto js = static_cast<std::size_t>(j); | |
| 301 | 61 | Value* aj = a[js]; | |
| 302 | 61 | Value* bj = bv[js]; | |
| 303 | 61 | Value* gj = g[js]; | |
| 304 | 61 | if (op_code == GLU_SWIGLU_BWD) { | |
| 305 | 25 | Value* ac = c.kb.clamp_f32(aj, c.f32(-88.0f), c.f32(88.0f)); | |
| 306 | 25 | Value* s = emit_sigmoid(c, ac); | |
| 307 | 25 | Value* silu_grad = c.b.build_mul(s, c.b.build_fma_f32(ac, c.b.build_sub(c.f32(1.0f), s), c.f32(1.0f))); | |
| 308 | 25 | da[js] = c.b.build_mul(gj, c.b.build_mul(bj, silu_grad)); | |
| 309 | 25 | db[js] = c.b.build_mul(gj, c.b.build_mul(aj, s)); | |
| 310 | 61 | } else if (op_code == GLU_GEGLU_TANH_BWD) { | |
| 311 | 14 | da[js] = c.b.build_mul(gj, emit_gelu_tanh(c, bj)); | |
| 312 | 14 | db[js] = c.b.build_mul(gj, c.b.build_mul(aj, emit_gelu_tanh_grad(c, bj))); | |
| 313 | 14 | } else { | |
| 314 | 22 | Value* bc = c.kb.clamp_f32(bj, c.f32(-20.0f), c.f32(20.0f)); | |
| 315 | 44 | Value* cdf = c.b.build_fma_f32( | |
| 316 | 22 | emit_erf(c, c.b.build_mul(bc, c.f32(0.70710678118654752440f))), | |
| 317 | 22 | c.f32(0.5f), c.f32(0.5f)); | |
| 318 | 44 | Value* pdf = c.b.build_mul( | |
| 319 | 22 | c.f32(0.39894228040143267794f), | |
| 320 | 22 | emit_exp(c, c.b.build_mul(c.f32(-0.5f), c.b.build_mul(bc, bc)))); | |
| 321 | 22 | da[js] = c.b.build_mul(gj, c.b.build_mul(bj, cdf)); | |
| 322 | 22 | db[js] = c.b.build_mul(gj, c.b.build_mul(aj, c.b.build_fma_f32(bc, pdf, cdf))); | |
| 323 | } | ||
| 324 | 61 | } | |
| 325 | 17 | store_lanes(c, dt_out, l, ptr_at(c, p[2], ia, esz_out), da); | |
| 326 | 17 | store_lanes(c, dt_out, l, ptr_at(c, p[2], ib, esz_out), db); | |
| 327 | 17 | } | |
| 328 | 39 | }); | |
| 329 | 39 | c.b.build_ret_void(); | |
| 330 | 39 | return finalize_fn(fn); | |
| 331 | 39 | } | |
| 332 | |||
| 333 | 11 | Function* build_rowvec_mir(Module& mod, std::uint32_t op_code, Dtype dt, int lanes) { | |
| 334 | 22 | Function* fn = mod.create_function( | |
| 335 | 11 | "mir_rowvec", Type::void_type(), | |
| 336 | 11 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()}); | |
| 337 | 11 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 338 | 11 | Ctx c(kb); | |
| 339 | 11 | auto p = entry_params(c, fn); | |
| 340 | 11 | const int esz = elem_bytes(dt); | |
| 341 | 22 | emit_vec_grid_stride_ew(c, p[4], lanes, [&](Value* k) { | |
| 342 | 11 | Value* elem_i = col_elem_idx(c, k, lanes); | |
| 343 | 11 | Value* col = c.b.build_umod(elem_i, p[5]); | |
| 344 | 11 | auto xv = load_lanes(c, dt, lanes, vec_ptr_at(c, p[0], k, lanes, esz)); | |
| 345 | 11 | auto vv = load_lanes(c, dt, lanes, ptr_at(c, p[1], col, esz)); | |
| 346 | 11 | std::vector<Value*> wv; | |
| 347 | 11 | if (op_code == ROWVEC_MODULATE) { | |
| 348 | 7 | wv = load_lanes(c, dt, lanes, ptr_at(c, p[2], col, esz)); | |
| 349 | 7 | } | |
| 350 | 11 | std::vector<Value*> out(static_cast<std::size_t>(lanes)); | |
| 351 | 42 | for (int j = 0; j < lanes; ++j) { | |
| 352 | 31 | const auto js = static_cast<std::size_t>(j); | |
| 353 | 31 | if (op_code == ROWVEC_MODULATE) { | |
| 354 | 17 | Value* scale = c.b.build_add(c.f32(1.0f), vv[js]); | |
| 355 | 17 | out[js] = c.b.build_fma_f32(xv[js], scale, wv[js]); | |
| 356 | 17 | } else { | |
| 357 | 14 | out[js] = c.b.build_mul(xv[js], vv[js]); | |
| 358 | } | ||
| 359 | 31 | } | |
| 360 | 11 | store_lanes(c, dt, lanes, vec_ptr_at(c, p[3], k, lanes, esz), out); | |
| 361 | 11 | }); | |
| 362 | 11 | c.b.build_ret_void(); | |
| 363 | 11 | return finalize_fn(fn); | |
| 364 | 11 | } | |
| 365 | |||
| 366 | } // namespace brotensor::kernels | ||
| 367 |