brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
145 / 235
61.7%
Branches:
90 / 376
23.9%
Functions:
13 / 17
76.5%

src/kernels/mir_lstm.cpp
Line Branch Exec Source
1 #include "mir_lstm.h"
2
3 #include "mir_math.h"
4 #include "../vulkan/shaders/op_codes.h"
5
6 #include <string>
7
8 namespace brotensor::kernels {
9
10 static_assert(kLstmStepOp == LS_STEP);
11
12 10 brass::Function* build_lstm_mir(brass::Module& mod, std::uint32_t mode, bool has_bhh, bool has_c0) {
13
5/10
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 10 times.
✗ Branch 9 not taken.
20 const std::string name = "mir_lstm_" + std::to_string(mode) + "_b" + std::to_string(has_bhh ? 1 : 0) +
14
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 "_c" + std::to_string(has_c0 ? 1 : 0);
15
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20 auto* fn = mod.create_function(
16 10 name, Type::void_type(),
17 70 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
18 60 Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()});
19
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 brass::codegen::KernelBuilder kb(mod, fn);
20
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Ctx c(kb);
21
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 auto p = entry_params(c, fn);
22 10 Value *gates = p[0], *cell = p[1], *y = p[2], *bhh = p[3], *c0 = p[4];
23 10 Value *dy = p[5], *dz = p[6], *dh_buf = p[7], *dc_buf = p[8];
24 10 Value *B = p[9], *H = p[10], *row0 = p[11];
25
26
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Value* G = c.b.build_mul(H, c.i32(4));
27
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Value* total = c.b.build_mul(B, H);
28
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 Value* row0_gt0 = c.b.build_ugt(row0, c.i32(0));
29
30
4/8
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 10 times.
✗ Branch 7 not taken.
20 c.kb.for_range(c.kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* e) {
31 10 Value* b = c.b.build_udiv(e, H);
32 10 Value* n = c.b.build_umod(e, H);
33 10 Value* row = c.b.build_add(row0, b);
34 10 Value* gr = c.b.build_mul(row, G);
35 10 Value* rn_idx = c.b.build_add(c.b.build_mul(row, H), n);
36 10 Value* bn_idx = c.b.build_add(c.b.build_mul(b, H), n);
37
38
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 Value* cprev = emit_if_else_values(
39 10 c, row0_gt0, {Type::f32()},
40 20 [&]() -> std::vector<Value*> {
41 10 Value* prev_idx = c.b.build_add(c.b.build_mul(c.b.build_sub(row, B), H), n);
42 10 return {c.kb.load_f32(ptr_at(c, cell, prev_idx, 4))};
43 },
44 20 [&]() -> std::vector<Value*> {
45
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 return {has_c0 ? c.kb.load_f32(ptr_at(c, c0, bn_idx, 4)) : c.f32(0.0f)};
46 })[0];
47
48 10 Value* h2 = c.b.build_mul(H, c.i32(2));
49 10 Value* h3 = c.b.build_mul(H, c.i32(3));
50 10 Value* gi0 = c.b.build_add(gr, n);
51 10 Value* gi1 = c.b.build_add(gi0, H);
52 10 Value* gi2 = c.b.build_add(gi0, h2);
53 10 Value* gi3 = c.b.build_add(gi0, h3);
54
55
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 6 times.
10 if (mode == LS_FWD) {
56 6 Value* zi = c.kb.load_f32(ptr_at(c, gates, gi0, 4));
57 6 Value* zf = c.kb.load_f32(ptr_at(c, gates, gi1, 4));
58 6 Value* zg = c.kb.load_f32(ptr_at(c, gates, gi2, 4));
59 6 Value* zo = c.kb.load_f32(ptr_at(c, gates, gi3, 4));
60
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 if (has_bhh) {
61 4 zi = c.b.build_add(zi, c.kb.load_f32(ptr_at(c, bhh, n, 4)));
62 4 zf = c.b.build_add(zf, c.kb.load_f32(ptr_at(c, bhh, c.b.build_add(H, n), 4)));
63 4 zg = c.b.build_add(zg, c.kb.load_f32(ptr_at(c, bhh, c.b.build_add(h2, n), 4)));
64 4 zo = c.b.build_add(zo, c.kb.load_f32(ptr_at(c, bhh, c.b.build_add(h3, n), 4)));
65 4 }
66 6 Value* ig = emit_sigmoid(c, zi);
67 6 Value* fg = emit_sigmoid(c, zf);
68 6 Value* gg = emit_tanh(c, zg);
69 6 Value* og = emit_sigmoid(c, zo);
70 6 Value* cn = c.b.build_add(c.b.build_mul(fg, cprev), c.b.build_mul(ig, gg));
71 6 c.kb.store_f32(ptr_at(c, gates, gi0, 4), ig);
72 6 c.kb.store_f32(ptr_at(c, gates, gi1, 4), fg);
73 6 c.kb.store_f32(ptr_at(c, gates, gi2, 4), gg);
74 6 c.kb.store_f32(ptr_at(c, gates, gi3, 4), og);
75 6 c.kb.store_f32(ptr_at(c, cell, rn_idx, 4), cn);
76 6 c.kb.store_f32(ptr_at(c, y, rn_idx, 4), c.b.build_mul(og, emit_tanh(c, cn)));
77 6 } else {
78 4 Value* ig = c.kb.load_f32(ptr_at(c, gates, gi0, 4));
79 4 Value* fg = c.kb.load_f32(ptr_at(c, gates, gi1, 4));
80 4 Value* gg = c.kb.load_f32(ptr_at(c, gates, gi2, 4));
81 4 Value* og = c.kb.load_f32(ptr_at(c, gates, gi3, 4));
82 4 Value* tc = emit_tanh(c, c.kb.load_f32(ptr_at(c, cell, rn_idx, 4)));
83 8 Value* dh = c.b.build_add(c.kb.load_f32(ptr_at(c, dy, rn_idx, 4)),
84 4 c.kb.load_f32(ptr_at(c, dh_buf, bn_idx, 4)));
85 4 Value* one_m_tc2 = c.b.build_sub(c.f32(1.0f), c.b.build_mul(tc, tc));
86 8 Value* dc = c.b.build_add(c.b.build_mul(c.b.build_mul(dh, og), one_m_tc2),
87 4 c.kb.load_f32(ptr_at(c, dc_buf, bn_idx, 4)));
88 8 c.kb.store_f32(ptr_at(c, dz, gi0, 4),
89 4 c.b.build_mul(c.b.build_mul(dc, gg), c.b.build_mul(ig, c.b.build_sub(c.f32(1.0f), ig))));
90 8 c.kb.store_f32(ptr_at(c, dz, gi1, 4),
91 4 c.b.build_mul(c.b.build_mul(dc, cprev), c.b.build_mul(fg, c.b.build_sub(c.f32(1.0f), fg))));
92 8 c.kb.store_f32(ptr_at(c, dz, gi2, 4),
93 4 c.b.build_mul(c.b.build_mul(dc, ig), c.b.build_sub(c.f32(1.0f), c.b.build_mul(gg, gg))));
94 8 c.kb.store_f32(ptr_at(c, dz, gi3, 4),
95 4 c.b.build_mul(c.b.build_mul(dh, tc), c.b.build_mul(og, c.b.build_sub(c.f32(1.0f), og))));
96 4 c.kb.store_f32(ptr_at(c, dc_buf, bn_idx, 4), c.b.build_mul(dc, fg));
97 }
98 10 });
99
100
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 c.b.build_ret_void();
101
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 return finalize_fn(fn);
102 10 }
103
104 1 brass::Function* build_lstm_step_mir(brass::Module& mod, Dtype dt) {
105
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const std::string name = "mir_lstm_step_" + std::to_string(static_cast<int>(dt));
106
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 auto* fn = mod.create_function(
107 1 name, Type::void_type(),
108 5 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
109 4 Type::i32(), Type::i32(), Type::i32(), Type::i32()});
110
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 brass::codegen::KernelBuilder kb(mod, fn);
111
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Ctx c(kb);
112
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto p = entry_params(c, fn);
113 // gates_f(0) whh_f(1) gates_r(2) whh_r(3) y(4) cell(5) T(6) H(7) step(8) ldy(9)
114 1 Value *y = p[4], *cell = p[5], *T = p[6], *H = p[7], *step = p[8], *ldy = p[9];
115 1 constexpr std::uint32_t kWarps = kLstmStepBlock / 32u;
116
117
3/6
✓ 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.
1 Value* lane = c.b.build_and(c.kb.tid_x(), c.i32(31));
118
4/8
✓ 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.
2 Value* unit = c.b.build_add(c.b.build_mul(c.kb.ctaid_x(), c.i32(static_cast<int>(kWarps))),
119
3/6
✓ 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.
1 c.b.build_lshr(c.kb.tid_x(), c.i32(5)));
120
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* dir = c.kb.ctaid_y();
121
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* has_prev = c.b.build_ugt(step, c.i32(0));
122
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* G = c.b.build_mul(H, c.i32(4));
123
124 3 auto body = [&](Value* gates, Value* whh, bool reverse) {
125
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* t = reverse ? c.b.build_sub(c.b.build_sub(T, c.i32(1)), step) : step;
126
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* t_prev = reverse ? c.b.build_add(t, c.i32(1)) : c.b.build_sub(t, c.i32(1));
127
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* col0 = reverse ? H : c.i32(0);
128 2 Value* hp_row = c.b.build_add(c.b.build_mul(t_prev, ldy), col0);
129
130 // z_q = W_hh[q*H + unit, :] . h_{t-1}, lanes striding the row.
131 2 std::vector<Value*> zero(4, c.f32(0.0f));
132
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::vector<Value*> acc = emit_if_else_values(
133
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c, has_prev, std::vector<Type>(4, Type::f32()),
134 4 [&]() -> std::vector<Value*> {
135
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 return c.kb.for_range_reduce_n(
136 4 lane, H, c.i32(32), zero, [&](Value* k, const std::vector<Value*>& a) {
137 2 Value* hk = load_elem_dt(c, dt, y, c.b.build_add(hp_row, k));
138 2 std::vector<Value*> out(4);
139
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 for (int q = 0; q < 4; ++q) {
140
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Value* row = c.b.build_add(c.b.build_mul(c.i32(q), H), unit);
141
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Value* w = load_elem_dt(c, dt, whh, c.b.build_add(c.b.build_mul(row, H), k));
142
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 out[q] = c.b.build_fma_f32(w, hk, a[q]);
143 8 }
144 2 return out;
145
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 });
146 ✗ },
147 4 [&]() -> std::vector<Value*> { return zero; });
148
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 for (int q = 0; q < 4; ++q) {
149
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
48 for (std::uint32_t m = 16; m >= 1; m >>= 1) acc[q] = c.b.build_add(acc[q], c.kb.shfl_bfly_f32(acc[q], m));
150 8 }
151
152
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.
4 c.kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] {
153 2 Value* g_row = c.b.build_add(c.b.build_mul(t, G), unit);
154 Value* z[4];
155
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 for (int q = 0; q < 4; ++q) {
156 8 Value* gi = c.b.build_add(g_row, c.b.build_mul(c.i32(q), H));
157 8 z[q] = c.b.build_add(acc[q], load_elem_dt(c, dt, gates, gi));
158 8 }
159 2 Value* ci = c.b.build_add(c.b.build_mul(dir, H), unit);
160 2 Value* c_prev = c.b.build_select(has_prev, c.kb.load_f32(ptr_at(c, cell, ci, 4)), c.f32(0.0f));
161 4 Value* cn = c.b.build_add(c.b.build_mul(emit_sigmoid(c, z[1]), c_prev),
162 2 c.b.build_mul(emit_sigmoid(c, z[0]), emit_tanh(c, z[2])));
163 2 c.kb.store_f32(ptr_at(c, cell, ci, 4), cn);
164 2 Value* h = c.b.build_mul(emit_sigmoid(c, z[3]), emit_tanh(c, cn));
165 2 store_elem_dt(c, dt, y, c.b.build_add(c.b.build_add(c.b.build_mul(t, ldy), col0), unit), h);
166 2 });
167 2 };
168
169
3/6
✓ 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.
2 c.kb.if_then(c.b.build_ult(unit, H), [&] {
170
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.if_then(c.b.build_eq(dir, c.i32(0)), [&] { body(p[0], p[1], false); });
171
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.if_then(c.b.build_ne(dir, c.i32(0)), [&] { body(p[2], p[3], true); });
172 1 });
173
174
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 c.b.build_ret_void();
175
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return finalize_fn(fn);
176 1 }
177
178 ✗ brass::Function* build_lstm_persist_mir(brass::Module& mod, Dtype dt, std::uint32_t ni) {
179
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ const std::string name = "mir_lstm_persist_" + std::to_string(static_cast<int>(dt)) + "_" + std::to_string(ni);
180
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ auto* fn = mod.create_function(
181 ✗ name, Type::void_type(),
182 ✗ {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
183 ✗ Type::i32(), Type::i32(), Type::i32()});
184
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ brass::codegen::KernelBuilder kb(mod, fn);
185
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Ctx c(kb);
186
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ auto p = entry_params(c, fn);
187 // gates_f(0) whh_f(1) gates_r(2) whh_r(3) y(4) bar(5) T(6) H(7) ldy(8)
188 ✗ Value *y = p[4], *T = p[6], *H = p[7], *ldy = p[8];
189 ✗ constexpr int kWarps = static_cast<int>(kLstmStepBlock / 32u);
190
191
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* tid = kb.tid_x();
192
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* lane = c.b.build_and(tid, c.i32(31));
193
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Value* unit = c.b.build_add(c.b.build_mul(kb.ctaid_x(), c.i32(kWarps)), c.b.build_lshr(tid, c.i32(5)));
194
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* unit_ok = c.b.build_ult(unit, H);
195
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* safe_unit = c.b.build_select(unit_ok, unit, c.i32(0));
196
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* dir = kb.ctaid_y();
197
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* rev = c.b.build_ne(dir, c.i32(0));
198
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* gates = c.b.build_select(rev, p[2], p[0]);
199
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* whh = c.b.build_select(rev, p[3], p[1]);
200
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* col0 = c.b.build_select(rev, H, c.i32(0));
201
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* G = c.b.build_mul(H, c.i32(4));
202
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Value* bar = c.b.build_add(p[5], c.b.build_shl(c.b.build_zext_i64(dir), kb.const_i64(2)));
203
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* blocks = kb.nctaid_x();
204
205 // This lane's slice of the unit's four W_hh rows: k = lane + 32 i.
206
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ std::vector<Value*> k_ok(ni), w(4 * ni);
207
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::uint32_t i = 0; i < ni; ++i) {
208
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* k = c.b.build_add(lane, c.i32(static_cast<int>(32 * i)));
209
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ k_ok[i] = c.b.build_and(unit_ok, c.b.build_ult(k, H));
210
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* sk = c.b.build_select(k_ok[i], k, c.i32(0));
211
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int q = 0; q < 4; ++q) {
212
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* row = c.b.build_add(c.b.build_mul(c.i32(q), H), safe_unit);
213
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* v = load_elem_dt(c, dt, whh, c.b.build_add(c.b.build_mul(row, H), sk));
214
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ w[static_cast<std::size_t>(q) * ni + i] = c.b.build_select(k_ok[i], v, c.f32(0.0f));
215 ✗ }
216 ✗ }
217
218 ✗ auto step_body = [&](Value* step, const std::vector<Value*>& cur) -> std::vector<Value*> {
219 ✗ Value* c_prev = cur[0];
220 ✗ Value* t = c.b.build_select(rev, c.b.build_sub(c.b.build_sub(T, c.i32(1)), step), step);
221 ✗ Value* t_prev = c.b.build_select(rev, c.b.build_add(t, c.i32(1)), c.b.build_sub(t, c.i32(1)));
222 ✗ Value* has_prev = c.b.build_ugt(step, c.i32(0));
223 ✗ Value* hp_row = c.b.build_add(c.b.build_mul(c.b.build_select(has_prev, t_prev, c.i32(0)), ldy), col0);
224
225 ✗ std::vector<Value*> acc(4, c.f32(0.0f));
226
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (std::uint32_t i = 0; i < ni; ++i) {
227
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* k = c.b.build_add(lane, c.i32(static_cast<int>(32 * i)));
228
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Value* ok = c.b.build_and(has_prev, k_ok[i]);
229
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* idx = c.b.build_select(ok, c.b.build_add(hp_row, k), c.i32(0));
230
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* hk = c.b.build_select(ok, load_elem_dt(c, dt, y, idx), c.f32(0.0f));
231
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int q = 0; q < 4; ++q) {
232 ✗ acc[static_cast<std::size_t>(q)] =
233
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_fma_f32(w[static_cast<std::size_t>(q) * ni + i], hk, acc[static_cast<std::size_t>(q)]);
234 ✗ }
235 ✗ }
236
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int q = 0; q < 4; ++q) {
237
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ for (std::uint32_t m = 16; m >= 1; m >>= 1) acc[q] = c.b.build_add(acc[q], kb.shfl_bfly_f32(acc[q], m));
238 ✗ }
239
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Value* g_row = c.b.build_add(c.b.build_mul(t, G), safe_unit);
240 Value* z[4];
241
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int q = 0; q < 4; ++q) {
242
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* gi = c.b.build_add(g_row, c.b.build_mul(c.i32(q), H));
243
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ z[q] = c.b.build_add(acc[q], load_elem_dt(c, dt, gates, gi));
244 ✗ }
245
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* cn = c.b.build_add(c.b.build_mul(emit_sigmoid(c, z[1]), c_prev),
246
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ c.b.build_mul(emit_sigmoid(c, z[0]), emit_tanh(c, z[2])));
247
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Value* h = c.b.build_mul(emit_sigmoid(c, z[3]), emit_tanh(c, cn));
248
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ kb.if_then(c.b.build_and(unit_ok, c.b.build_eq(lane, c.i32(0))), [&] {
249 ✗ store_elem_dt(c, dt, y, c.b.build_add(c.b.build_add(c.b.build_mul(t, ldy), col0), unit), h);
250 ✗ });
251
252 // Grid barrier over this direction's blocks: release the h stores,
253 // arrive, spin until every block has arrived at this step, acquire.
254
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.sync();
255
0/8
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
256 ✗ c.b.build_call("ptx_membar_gl", Type::void_type());
257 ✗ kb.atom_add_u32(bar, c.i32(1));
258 ✗ Value* target = c.b.build_mul(c.b.build_add(step, c.i32(1)), blocks);
259 ✗ BasicBlock* from = c.b.current_block();
260 ✗ BasicBlock* spin = c.b.append_block("bar_spin"); // append_block moves the builder
261 ✗ BasicBlock* done = c.b.append_block("bar_done");
262 ✗ c.b.position_at_end(from);
263 ✗ c.b.build_br(spin, {});
264 ✗ c.b.position_at_end(spin);
265 ✗ Value* arrived = c.b.build_call("ptx_atom_or_u32", Type::i32(), {bar, c.i32(0)});
266 ✗ c.b.build_br_if(c.b.build_ult(arrived, target), spin, done);
267 ✗ c.b.position_at_end(done);
268 ✗ });
269
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ kb.sync();
270
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ c.b.build_call("ptx_membar_gl", Type::void_type());
271
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return {cn};
272 ✗ };
273
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ kb.for_range_reduce_n(c.i32(0), T, c.i32(1), {c.f32(0.0f)}, step_body);
274
275
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ c.b.build_ret_void();
276
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ return finalize_fn(fn);
277 ✗ }
278
279 } // namespace brotensor::kernels
280