brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
288 / 293
98.3%
Branches:
235 / 399
58.9%
Functions:
19 / 19
100.0%

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
18/20
✗ Branch 0 not taken.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 13 times.
✓ Branch 4 taken 30 times.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 14 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 5 times.
✓ Branch 10 taken 13 times.
✓ Branch 11 taken 13 times.
✓ Branch 12 taken 9 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 5 times.
✓ Branch 15 taken 22 times.
✓ Branch 16 taken 21 times.
✓ Branch 17 taken 15 times.
✓ Branch 18 taken 21 times.
✗ Branch 19 not taken.
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
3/5
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
15/16
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 14 times.
✓ Branch 5 taken 14 times.
✓ Branch 6 taken 14 times.
✓ Branch 7 taken 5 times.
✓ Branch 8 taken 5 times.
✓ Branch 9 taken 5 times.
✓ Branch 10 taken 5 times.
✓ Branch 11 taken 9 times.
✓ Branch 12 taken 5 times.
✓ Branch 13 taken 5 times.
✓ Branch 14 taken 4 times.
✗ Branch 15 not taken.
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
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 Ctx c(kb);
135
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 auto p = entry_params(c, fn);
136
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 const int esz = elem_bytes(dt);
137
2/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 std::vector<Value*> ys(static_cast<std::size_t>(lanes));
142
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 245 times.
309 for (int j = 0; j < lanes; ++j) {
143
1/2
✓ Branch 0 taken 245 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 store_lanes(c, dt, lanes, y_ptr, ys);
146 64 });
147
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 c.b.build_ret_void();
148
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 Ctx c(kb);
157
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto p = entry_params(c, fn);
158
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const int esz_y = elem_bytes(dt_y);
159
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 const int esz_x = elem_bytes(dt_x);
160
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 13 times.
15 Value* zero_mask = (op_code == BOP_AXPBY)
161
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
1 ? c.b.build_select(c.b.build_eq(p[2], c.i32(0)), c.i32(1), c.i32(0))
162 : nullptr;
163
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto xv = load_lanes(c, dt_x, lanes, x_ptr);
168
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 std::vector<Value*> out(static_cast<std::size_t>(lanes));
169
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 54 times.
68 for (int j = 0; j < lanes; ++j) {
170 54 const auto js = static_cast<std::size_t>(j);
171
1/2
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
54 out[js] = binary_lane(c, op_code, yv[js], xv[js], p[3], p[4], zero_mask);
172 54 }
173
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 store_lanes(c, dt_y, lanes, y_ptr, out);
174 14 });
175
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 c.b.build_ret_void();
176
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 Ctx c(kb);
185
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 auto p = entry_params(c, fn);
186
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 const int esz = elem_bytes(dt);
187
2/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
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
2/2
✓ Branch 0 taken 47 times.
✓ Branch 1 taken 1 time.
48 if (op_code != GOP_PASS) {
193
2/4
✓ Branch 0 taken 47 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 47 times.
✗ Branch 3 not taken.
47 v = load_lanes(c, dt, lanes, vec_ptr_at(c, p[0], k, lanes, esz));
194 47 }
195
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 std::vector<Value*> out(static_cast<std::size_t>(lanes));
196
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 165 times.
213 for (int j = 0; j < lanes; ++j) {
197 165 const auto js = static_cast<std::size_t>(j);
198
4/6
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 161 times.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 165 times.
✗ Branch 5 not taken.
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
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 store_lanes(c, dt, lanes, dx_ptr, out);
201 48 });
202
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 c.b.build_ret_void();
203
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Ctx c(kb);
212
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto p = entry_params(c, fn);
213
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const int esz_s = elem_bytes(src_dt);
214
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 const int esz_d = elem_bytes(dst_dt);
215
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
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
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 store_lanes(c, dst_dt, lanes, vec_ptr_at(c, p[1], k, lanes, esz_d), vals);
218 9 });
219
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 c.b.build_ret_void();
220
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Ctx c(kb);
229
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto p = entry_params(c, fn);
230
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 const int esz = elem_bytes(dt);
231
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
7 const int l = (mode == BIAS_ROW) ? lanes : 1;
232
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 emit_vec_grid_stride_ew(c, p[2], l, [&](Value* k) {
233
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 3 times.
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
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
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
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto bv = load_lanes(c, dt, l, b_ptr);
240
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 24 times.
31 for (int q = 0; q < l; ++q) {
241 24 const auto qs = static_cast<std::size_t>(q);
242
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 yv[qs] = c.b.build_add(yv[qs], bv[qs]);
243 24 }
244
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 store_lanes(c, dt, l, y_ptr, yv);
245 7 });
246
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 c.b.build_ret_void();
247
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 Ctx c(kb);
256
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 auto p = entry_params(c, fn);
257
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 const int esz_in = elem_bytes(dt_in);
258
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 const int esz_out = elem_bytes(dt_out);
259
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37 times.
39 const int l = (op_code == GLU_GEGLU_PAIRS) ? 1 : lanes;
260
261
2/4
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 39 times.
✗ Branch 3 not taken.
78 emit_vec_grid_stride_ew(c, p[3], l, [&](Value* k) {
262
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 18 times.
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
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 37 times.
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
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
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/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 Value* out = c.b.build_mul(av[0], emit_gelu_exact(c, bv[0]));
273
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
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
2/4
✓ Branch 0 taken 37 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 37 times.
✗ Branch 3 not taken.
37 auto bv = load_lanes(c, dt_in, l, ptr_at(c, p[0], ib, esz_in));
281
282
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 20 times.
37 if (op_code <= GLU_GEGLU_EXACT) {
283
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 std::vector<Value*> out(static_cast<std::size_t>(l));
284
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 74 times.
94 for (int j = 0; j < l; ++j) {
285 74 const auto js = static_cast<std::size_t>(j);
286
2/2
✓ Branch 0 taken 45 times.
✓ Branch 1 taken 29 times.
74 if (op_code == GLU_SWIGLU) {
287
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
29 out[js] = c.b.build_mul(c.b.build_mul(a[js], emit_sigmoid(c, a[js])), bv[js]);
288
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 23 times.
74 } else if (op_code == GLU_GEGLU_TANH) {
289
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 out[js] = c.b.build_mul(a[js], emit_gelu_tanh(c, bv[js]));
290 22 } else {
291
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 out[js] = c.b.build_mul(a[js], emit_gelu_exact(c, bv[js]));
292 }
293 74 }
294
2/4
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 store_lanes(c, dt_out, l, ptr_at(c, p[2], elem_i, esz_out), out);
295 20 } else {
296
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 auto g = load_lanes(c, dt_in, l, ptr_at(c, p[1], elem_i, esz_in));
297
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 std::vector<Value*> da(static_cast<std::size_t>(l));
298
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 std::vector<Value*> db(static_cast<std::size_t>(l));
299
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 61 times.
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
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 36 times.
61 if (op_code == GLU_SWIGLU_BWD) {
305
3/6
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
25 Value* ac = c.kb.clamp_f32(aj, c.f32(-88.0f), c.f32(88.0f));
306
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 Value* s = emit_sigmoid(c, ac);
307
5/10
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 25 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 25 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 25 times.
✗ Branch 9 not taken.
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
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 da[js] = c.b.build_mul(gj, c.b.build_mul(bj, silu_grad));
309
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 db[js] = c.b.build_mul(gj, c.b.build_mul(aj, s));
310
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 22 times.
61 } else if (op_code == GLU_GEGLU_TANH_BWD) {
311
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 da[js] = c.b.build_mul(gj, emit_gelu_tanh(c, bj));
312
3/6
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
✗ Branch 5 not taken.
14 db[js] = c.b.build_mul(gj, c.b.build_mul(aj, emit_gelu_tanh_grad(c, bj)));
313 14 } else {
314
3/6
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 Value* bc = c.kb.clamp_f32(bj, c.f32(-20.0f), c.f32(20.0f));
315
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
44 Value* cdf = c.b.build_fma_f32(
316
3/6
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
22 emit_erf(c, c.b.build_mul(bc, c.f32(0.70710678118654752440f))),
317
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 c.f32(0.5f), c.f32(0.5f));
318
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
44 Value* pdf = c.b.build_mul(
319
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 c.f32(0.39894228040143267794f),
320
4/8
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 22 times.
✗ Branch 7 not taken.
22 emit_exp(c, c.b.build_mul(c.f32(-0.5f), c.b.build_mul(bc, bc))));
321
2/4
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
22 da[js] = c.b.build_mul(gj, c.b.build_mul(bj, cdf));
322
3/6
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 22 times.
✗ Branch 5 not taken.
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
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 store_lanes(c, dt_out, l, ptr_at(c, p[2], ia, esz_out), da);
326
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 store_lanes(c, dt_out, l, ptr_at(c, p[2], ib, esz_out), db);
327 17 }
328 39 });
329
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 c.b.build_ret_void();
330
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Ctx c(kb);
339
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 auto p = entry_params(c, fn);
340
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 const int esz = elem_bytes(dt);
341
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
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
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 auto vv = load_lanes(c, dt, lanes, ptr_at(c, p[1], col, esz));
346 11 std::vector<Value*> wv;
347
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (op_code == ROWVEC_MODULATE) {
348
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 wv = load_lanes(c, dt, lanes, ptr_at(c, p[2], col, esz));
349 7 }
350
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 std::vector<Value*> out(static_cast<std::size_t>(lanes));
351
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 31 times.
42 for (int j = 0; j < lanes; ++j) {
352 31 const auto js = static_cast<std::size_t>(j);
353
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 14 times.
31 if (op_code == ROWVEC_MODULATE) {
354
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 Value* scale = c.b.build_add(c.f32(1.0f), vv[js]);
355
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 out[js] = c.b.build_fma_f32(xv[js], scale, wv[js]);
356 17 } else {
357
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 out[js] = c.b.build_mul(xv[js], vv[js]);
358 }
359 31 }
360
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 store_lanes(c, dt, lanes, vec_ptr_at(c, p[3], k, lanes, esz), out);
361 11 });
362
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 c.b.build_ret_void();
363
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 return finalize_fn(fn);
364 11 }
365
366 } // namespace brotensor::kernels
367