brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
563 / 583
96.6%
Branches:
233 / 384
60.7%
Functions:
82 / 82
100.0%

src/kernels/mir_audio.cpp
Line Branch Exec Source
1 #include "mir_audio.h"
2 #include "mir_common.h"
3 #include "mir_diffusion.h"
4 #include "mir_math.h"
5
6 #include "../vulkan/shaders/op_codes.h"
7
8 #include <string>
9 #include <utility>
10 #include <vector>
11
12 namespace brotensor::kernels {
13
14 namespace {
15
16 3 Value* clamp_i32(Ctx& c, Value* v, Value* lo, Value* hi) {
17 3 Value* a = c.b.build_select(c.b.build_slt(v, lo), lo, v);
18 3 return c.b.build_select(c.b.build_sgt(a, hi), hi, a);
19 }
20
21 7 Value* guard_denom(Ctx& c, Value* d) {
22 7 Value* pos_min = c.f32(1e-9f);
23 7 Value* neg_min = c.f32(-1e-9f);
24 7 Value* is_pos = c.b.build_sge(d, c.f32(0.0f));
25 7 Value* p_clamp = c.b.build_select(c.b.build_slt(d, pos_min), pos_min, d);
26 7 Value* n_clamp = c.b.build_select(c.b.build_sgt(d, neg_min), neg_min, d);
27 7 return c.b.build_select(is_pos, p_clamp, n_clamp);
28 }
29
30 3 Value* pad1d_src(Ctx& c, Value* p, Value* L, Value* left, std::uint32_t mode) {
31 3 Value* rel = c.b.build_sub(p, left);
32 3 Value* in_mid = c.b.build_and(c.b.build_sge(rel, c.i32(0)), c.b.build_slt(rel, L));
33
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (mode == 0u) {
34 1 return c.b.build_select(in_mid, rel, c.i32(-1));
35 }
36
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (mode == 2u) {
37 1 Value* cl = c.b.build_select(c.b.build_slt(rel, c.i32(0)), c.i32(0), c.b.build_sub(L, c.i32(1)));
38 1 return c.b.build_select(in_mid, rel, cl);
39 }
40 // Reflect (mode == 1)
41
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
42 1 c, in_mid, {Type::i32()},
43 2 [&]() -> std::vector<Value*> { return {rel}; },
44 2 [&]() -> std::vector<Value*> {
45 1 Value* l_one = c.b.build_eq(L, c.i32(1));
46
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
47 1 c, l_one, {Type::i32()},
48 2 [&]() -> std::vector<Value*> { return {c.i32(0)}; },
49 2 [&]() -> std::vector<Value*> {
50 1 Value* period = c.b.build_shl(c.b.build_sub(L, c.i32(1)), c.i32(1));
51 1 Value* q0 = c.b.build_smod(rel, period);
52 1 Value* q = c.b.build_select(c.b.build_slt(q0, c.i32(0)), c.b.build_add(q0, period), q0);
53 1 return {c.b.build_select(c.b.build_slt(q, L), q, c.b.build_sub(period, q))};
54 });
55 ✗ })[0];
56 3 }
57
58 struct ResampleTap {
59 Value* x0;
60 Value* x1;
61 Value* f;
62 };
63
64 4 ResampleTap resample1d_src(Ctx& c, Value* dst, Value* lin, Value* lout, std::uint32_t mode) {
65 4 Value* lin64 = c.b.build_zext_i64(lin);
66 4 Value* lout64 = c.b.build_zext_i64(lout);
67 4 Value* dst64 = c.b.build_zext_i64(dst);
68 4 Value* two_d_p1 = c.b.build_add(c.b.build_shl(dst64, c.kb.const_i64(1)), c.kb.const_i64(1));
69 4 Value* num = c.b.build_sub(c.b.build_mul(two_d_p1, lin64), lout64);
70 4 Value* den = c.b.build_shl(lout64, c.kb.const_i64(1));
71
72
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (mode == 0u) {
73 2 Value* is_neg = c.b.build_slt(num, c.kb.const_i64(0));
74 2 Value* abs_n = c.b.build_select(is_neg, c.b.build_neg(num), num);
75 4 Value* q_neg = c.b.build_neg(c.b.build_udiv(
76 2 c.b.build_sub(c.b.build_add(abs_n, den), c.kb.const_i64(1)), den));
77 2 Value* q_pos = c.b.build_udiv(c.b.build_select(is_neg, c.kb.const_i64(0), num), den);
78 2 Value* q = c.b.build_select(is_neg, q_neg, q_pos);
79 2 Value* r2 = c.b.build_shl(c.b.build_sub(num, c.b.build_mul(q, den)), c.kb.const_i64(1));
80 2 Value* gt = c.b.build_sgt(r2, den);
81 4 Value* tie_odd = c.b.build_and(c.b.build_eq(r2, den),
82 2 c.b.build_ne(c.b.build_and(q, c.kb.const_i64(1)), c.kb.const_i64(0)));
83 2 Value* q_rnd = c.b.build_select(c.b.build_or(gt, tie_odd), c.b.build_add(q, c.kb.const_i64(1)), q);
84 2 Value* x0 = clamp_i32(c, c.b.build_trunc_i32(q_rnd), c.i32(0), c.b.build_sub(lin, c.i32(1)));
85 2 return {x0, x0, c.f32(0.0f)};
86 }
87
88 2 Value* le_zero = c.b.build_sle(num, c.kb.const_i64(0));
89 2 Value* lin_m1 = c.b.build_sub(lin, c.i32(1));
90 2 Value* x1_zero = c.b.build_select(c.b.build_slt(c.i32(1), lin_m1), c.i32(1), lin_m1);
91
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto res = emit_if_else_values(
92 2 c, le_zero, {Type::i32(), Type::i32(), Type::f32()},
93 4 [&]() -> std::vector<Value*> { return {c.i32(0), x1_zero, c.f32(0.0f)}; },
94 4 [&]() -> std::vector<Value*> {
95 2 Value* q = c.b.build_udiv(num, den);
96 2 Value* past = c.b.build_sge(q, c.b.build_sub(lin64, c.kb.const_i64(1)));
97
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return emit_if_else_values(
98 2 c, past, {Type::i32(), Type::i32(), Type::f32()},
99 4 [&]() -> std::vector<Value*> { return {lin_m1, lin_m1, c.f32(0.0f)}; },
100 4 [&]() -> std::vector<Value*> {
101 2 Value* x0 = c.b.build_trunc_i32(q);
102 2 Value* rem = c.b.build_sub(num, c.b.build_mul(q, den));
103 2 Value* f = emit_div_rn(c, c.b.build_sitofp_f32_i64(rem), c.b.build_sitofp_f32_i64(den));
104 2 return {x0, c.b.build_add(x0, c.i32(1)), f};
105 });
106 ✗ });
107 2 return {res[0], res[1], res[2]};
108 4 }
109
110 7 void build_snake(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_beta, bool bwd) {
111
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (!bwd) {
112 // p: X(0) alpha(1) beta(2) Y(3) C(4) L(5) total(6)
113 2 Value* C = p[4];
114 2 Value* L = p[5];
115
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 emit_vec_grid_stride(c, p[6], 1, kAudioBlock, [&](Value* i) {
116 2 Value* ch = c.b.build_umod(c.b.build_udiv(i, L), C);
117 2 Value* al = c.kb.load_f32(ptr_at(c, p[1], ch, 4));
118
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* d_raw = has_beta ? c.kb.load_f32(ptr_at(c, p[2], ch, 4)) : al;
119 2 Value* r = c.b.build_sdiv(c.f32(1.0f), guard_denom(c, d_raw));
120 2 Value* x = load_elem_dt(c, dt, p[0], i);
121 6 auto [s, co] = emit_sincos_acc(c, c.b.build_mul(al, x));
122 (void)co;
123 6 Value* y = c.b.build_add(x, c.b.build_mul(r, c.b.build_mul(s, s)));
124 2 store_elem_dt(c, dt, p[3], i, y);
125 2 });
126 2 return;
127 }
128 // bwd: X(0) alpha(1) beta(2) dY(3) dX(4) dAlpha(5) dBeta(6) C(7) L(8) N(9)
129 5 Value* C = p[7];
130 5 Value* L = p[8];
131 5 Value* per = c.b.build_mul(p[9], L);
132 5 Value* tid = c.kb.tid_x();
133 5 Value* block = c.i32(static_cast<int>(kAudioBlock));
134 5 Value* s_red = c.kb.shared_alloc_f32(8);
135
136
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 c.kb.for_range(c.kb.ctaid_x(), C, c.kb.nctaid_x(), [&](Value* ch) {
137 5 Value* al = c.kb.load_f32(ptr_at(c, p[1], ch, 4));
138
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 Value* d_raw = has_beta ? c.kb.load_f32(ptr_at(c, p[2], ch, 4)) : al;
139 5 Value* r = c.b.build_sdiv(c.f32(1.0f), guard_denom(c, d_raw));
140
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto sums = c.kb.for_range_reduce_n(
141
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
10 tid, per, block, {c.f32(0.0f), c.f32(0.0f)}, [&](Value* j, const std::vector<Value*>& cur) {
142 5 Value* n = c.b.build_udiv(j, L);
143 5 Value* l = c.b.build_sub(j, c.b.build_mul(n, L));
144 5 Value* idx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, C), ch), L), l);
145 5 Value* x = load_elem_dt(c, dt, p[0], idx);
146 5 Value* dy = load_elem_dt(c, dt, p[3], idx);
147 25 auto [s, co] = emit_sincos_acc(c, c.b.build_mul(al, x));
148 15 Value* sc = c.b.build_mul(s, co);
149 5 Value* two_al_r_sc = c.b.build_mul(c.b.build_mul(c.f32(2.0f), al), c.b.build_mul(r, sc));
150 5 store_elem_dt(c, dt, p[4], idx, c.b.build_mul(dy, c.b.build_add(c.f32(1.0f), two_al_r_sc)));
151 5 Value* da_inc = c.b.build_mul(dy, c.b.build_mul(c.b.build_mul(c.f32(2.0f), r), c.b.build_mul(x, sc)));
152 15 Value* t = c.b.build_mul(dy, c.b.build_mul(c.b.build_neg(c.b.build_mul(r, r)), c.b.build_mul(s, s)));
153
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 Value* n_da = c.b.build_add(cur[0], has_beta ? da_inc : c.b.build_add(da_inc, t));
154
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
5 Value* n_db = has_beta ? c.b.build_add(cur[1], t) : cur[1];
155 5 return std::vector<Value*>{n_da, n_db};
156 });
157
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* tot_a = emit_block_sum_f32(c, s_red, sums[0]);
158
3/4
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
5 Value* tot_b = has_beta ? emit_block_sum_f32(c, s_red, sums[1]) : nullptr;
159
4/8
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
10 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
160 5 Value* ap = ptr_at(c, p[5], ch, 4);
161 5 c.kb.store_f32(ap, c.b.build_add(c.kb.load_f32(ap), tot_a));
162
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 4 times.
5 if (has_beta) {
163 4 Value* bp = ptr_at(c, p[6], ch, 4);
164 4 c.kb.store_f32(bp, c.b.build_add(c.kb.load_f32(bp), tot_b));
165 4 }
166 5 });
167 5 });
168 7 }
169
170 6 void build_pad1d(Ctx& c, const std::vector<Value*>& p, Dtype dt, std::uint32_t mode, bool bwd) {
171 // p: src(0) dst(1) L(2) left(3) L_pad(4) rows(5)
172 6 Value* L = p[2];
173 6 Value* left = p[3];
174 6 Value* L_pad = p[4];
175 6 Value* rows = p[5];
176
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (!bwd) {
177
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, c.b.build_mul(rows, L_pad), 1, kAudioBlock, [&](Value* i) {
178 3 Value* row = c.b.build_udiv(i, L_pad);
179 3 Value* pos = c.b.build_sub(i, c.b.build_mul(row, L_pad));
180 3 Value* s = pad1d_src(c, pos, L, left, mode);
181
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* val = emit_if_else_values(
182 3 c, c.b.build_sge(s, c.i32(0)), {Type::f32()},
183 6 [&]() -> std::vector<Value*> {
184 3 return {load_elem_dt(c, dt, p[0], c.b.build_add(c.b.build_mul(row, L), s))};
185 },
186 6 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
187 3 store_elem_dt(c, dt, p[1], i, val);
188 3 });
189 3 return;
190 }
191
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, c.b.build_mul(rows, L), 1, kAudioBlock, [&](Value* i) {
192 3 Value* row = c.b.build_udiv(i, L);
193 3 Value* l = c.b.build_sub(i, c.b.build_mul(row, L));
194 3 Value* base = c.b.build_mul(row, L_pad);
195
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 if (mode == 1u) {
196 1 Value* has_l = c.b.build_and(c.b.build_sge(l, c.i32(1)), c.b.build_sle(l, left));
197
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* a0 = emit_if_else_values(
198 1 c, has_l, {Type::f32()},
199 2 [&]() -> std::vector<Value*> {
200 1 return {load_elem_dt(c, dt, p[0], c.b.build_add(base, c.b.build_sub(left, l)))};
201 },
202 2 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
203 1 Value* a1 = c.b.build_add(a0, load_elem_dt(c, dt, p[0], c.b.build_add(base, c.b.build_add(left, l))));
204 1 Value* pr = c.b.build_sub(c.b.build_add(left, c.b.build_shl(c.b.build_sub(L, c.i32(1)), c.i32(1))), l);
205 2 Value* has_r = c.b.build_and(
206 1 c.b.build_sgt(L, c.i32(1)),
207 1 c.b.build_and(c.b.build_sge(pr, c.b.build_add(left, L)), c.b.build_slt(pr, L_pad)));
208
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* a2 = emit_if_else_values(
209 1 c, has_r, {Type::f32()},
210 2 [&]() -> std::vector<Value*> {
211 1 return {c.b.build_add(a1, load_elem_dt(c, dt, p[0], c.b.build_add(base, pr)))};
212 },
213 2 [&]() -> std::vector<Value*> { return {a1}; })[0];
214 1 store_elem_dt(c, dt, p[1], i, a2);
215
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
3 } else if (mode == 2u) {
216
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* a0 = emit_if_else_values(
217 1 c, c.b.build_eq(l, c.i32(0)), {Type::f32()},
218 2 [&]() -> std::vector<Value*> {
219
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 return {c.kb.for_range_reduce(c.i32(0), left, c.i32(1), c.f32(0.0f), [&](Value* q, Value* cur) {
220 1 return c.b.build_add(cur, load_elem_dt(c, dt, p[0], c.b.build_add(base, q)));
221 })};
222 ✗ },
223 2 [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0];
224 1 Value* a1 = c.b.build_add(a0, load_elem_dt(c, dt, p[0], c.b.build_add(base, c.b.build_add(left, l))));
225
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* a2 = emit_if_else_values(
226 1 c, c.b.build_eq(l, c.b.build_sub(L, c.i32(1))), {Type::f32()},
227 2 [&]() -> std::vector<Value*> {
228
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 return {c.kb.for_range_reduce(
229 2 c.b.build_add(left, L), L_pad, c.i32(1), a1, [&](Value* q, Value* cur) {
230 1 return c.b.build_add(cur, load_elem_dt(c, dt, p[0], c.b.build_add(base, q)));
231 })};
232 ✗ },
233 2 [&]() -> std::vector<Value*> { return {a1}; })[0];
234 1 store_elem_dt(c, dt, p[1], i, a2);
235 1 } else {
236 1 store_elem_dt(c, dt, p[1], i, load_elem_dt(c, dt, p[0], c.b.build_add(base, c.b.build_add(left, l))));
237 }
238 3 });
239 6 }
240
241 4 void build_resample1d(Ctx& c, const std::vector<Value*>& p, Dtype dt, std::uint32_t mode, bool bwd) {
242 // p: src(0) dst(1) L_in(2) L_out(3) rows(4)
243 4 Value* lin = p[2];
244 4 Value* lout = p[3];
245 4 Value* rows = p[4];
246
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (!bwd) {
247
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 emit_vec_grid_stride(c, c.b.build_mul(rows, lout), 1, kAudioBlock, [&](Value* i) {
248 2 Value* row = c.b.build_udiv(i, lout);
249 2 Value* dst = c.b.build_sub(i, c.b.build_mul(row, lout));
250 2 ResampleTap t = resample1d_src(c, dst, lin, lout, mode);
251 2 Value* xb = c.b.build_mul(row, lin);
252 2 Value* v0 = load_elem_dt(c, dt, p[0], c.b.build_add(xb, t.x0));
253 2 Value* y = v0;
254
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (mode != 0u) {
255 1 Value* v1 = load_elem_dt(c, dt, p[0], c.b.build_add(xb, t.x1));
256 1 y = c.b.build_add(c.b.build_mul(c.b.build_sub(c.f32(1.0f), t.f), v0), c.b.build_mul(t.f, v1));
257 1 }
258 2 store_elem_dt(c, dt, p[1], i, y);
259 2 });
260 2 return;
261 }
262 2 Value* two_lout64 = c.b.build_shl(c.b.build_zext_i64(lout), c.kb.const_i64(1));
263 2 Value* two_lin64 = c.b.build_shl(c.b.build_zext_i64(lin), c.kb.const_i64(1));
264 2 Value* ldiff64 = c.b.build_sub(c.b.build_zext_i64(lout), c.b.build_zext_i64(lin));
265
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 emit_vec_grid_stride(c, c.b.build_mul(rows, lin), 1, kAudioBlock, [&](Value* t) {
266 2 Value* row = c.b.build_udiv(t, lin);
267 2 Value* idx = c.b.build_sub(t, c.b.build_mul(row, lin));
268 2 Value* idx64 = c.b.build_zext_i64(idx);
269 2 Value* lo_num = c.b.build_add(c.b.build_mul(two_lout64, c.b.build_sub(idx64, c.kb.const_i64(1))), ldiff64);
270 2 Value* hi_num = c.b.build_add(c.b.build_mul(two_lout64, c.b.build_add(idx64, c.kb.const_i64(1))), ldiff64);
271 2 Value* dlo0 = c.b.build_sub(c.b.build_trunc_i32(c.b.build_sdiv(lo_num, two_lin64)), c.i32(1));
272 2 Value* dhi0 = c.b.build_add(c.b.build_trunc_i32(c.b.build_sdiv(hi_num, two_lin64)), c.i32(1));
273 2 Value* dlo1 = c.b.build_select(c.b.build_eq(idx, c.i32(0)), c.i32(0), dlo0);
274 4 Value* dhi1 = c.b.build_select(c.b.build_eq(idx, c.b.build_sub(lin, c.i32(1))),
275 2 c.b.build_sub(lout, c.i32(1)), dhi0);
276 2 Value* dlo = c.b.build_select(c.b.build_sgt(dlo1, c.i32(0)), dlo1, c.i32(0));
277 2 Value* lout_m1 = c.b.build_sub(lout, c.i32(1));
278 2 Value* dhi = c.b.build_select(c.b.build_slt(dhi1, lout_m1), dhi1, lout_m1);
279 2 Value* yb = c.b.build_mul(row, lout);
280
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* acc = c.kb.for_range_reduce(
281 4 dlo, c.b.build_add(dhi, c.i32(1)), c.i32(1), c.f32(0.0f), [&](Value* dst, Value* cur) {
282 2 ResampleTap tap = resample1d_src(c, dst, lin, lout, mode);
283 2 Value* g = load_elem_dt(c, dt, p[0], c.b.build_add(yb, dst));
284
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (mode == 0u) {
285 1 return c.b.build_select(c.b.build_eq(tap.x0, idx), c.b.build_add(cur, g), cur);
286 }
287 2 Value* c1 = c.b.build_select(
288 1 c.b.build_eq(tap.x0, idx),
289 1 c.b.build_add(cur, c.b.build_mul(c.b.build_sub(c.f32(1.0f), tap.f), g)), cur);
290 2 return c.b.build_select(
291 1 c.b.build_eq(tap.x1, idx),
292 1 c.b.build_add(c1, c.b.build_mul(tap.f, g)), c1);
293 2 });
294 2 store_elem_dt(c, dt, p[1], t, acc);
295 2 });
296 4 }
297
298 3 void build_causal_conv1d(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_bias) {
299 // p: X(0) W(1) bias(2) state(3) Y(4) C(5) L(6) kL(7) dil(8) rows(9)
300 3 Value* C = p[5];
301 3 Value* L = p[6];
302 3 Value* kl = p[7];
303 3 Value* dil = p[8];
304 3 Value* hist = c.b.build_mul(c.b.build_sub(kl, c.i32(1)), dil);
305
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, p[9], 1, kAudioBlock, [&](Value* row) {
306 3 Value* ch = c.b.build_umod(row, C);
307 3 Value* sb = c.b.build_mul(row, hist);
308 3 Value* xb = c.b.build_mul(row, L);
309
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 time.
3 Value* bv = has_bias ? load_elem_dt(c, dt, p[2], ch) : c.f32(0.0f);
310 3 Value* w_base = c.b.build_mul(ch, kl);
311 9 auto read_src = [&](Value* idx) {
312
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 return emit_if_else_values(
313 6 c, c.b.build_ult(idx, hist), {Type::f32()},
314 12 [&]() -> std::vector<Value*> { return {load_elem_dt(c, dt, p[3], c.b.build_add(sb, idx))}; },
315 12 [&]() -> std::vector<Value*> {
316 6 return {load_elem_dt(c, dt, p[0], c.b.build_add(xb, c.b.build_sub(idx, hist)))};
317 })[0];
318 ✗ };
319
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.for_range(c.i32(0), L, c.i32(1), [&](Value* t) {
320
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 Value* acc = c.kb.for_range_reduce(c.i32(0), kl, c.i32(1), bv, [&](Value* k, Value* cur) {
321 3 Value* v = read_src(c.b.build_add(t, c.b.build_mul(k, dil)));
322 3 Value* w = load_elem_dt(c, dt, p[1], c.b.build_add(w_base, k));
323 3 return c.b.build_add(cur, c.b.build_mul(w, v));
324 });
325 3 store_elem_dt(c, dt, p[4], c.b.build_add(xb, t), acc);
326 3 });
327
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.for_range(c.i32(0), hist, c.i32(1), [&](Value* s) {
328 3 store_elem_dt(c, dt, p[3], c.b.build_add(sb, s), read_src(c.b.build_add(L, s)));
329 3 });
330 3 });
331 3 }
332
333 2 void build_convt1d_bwd_w(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
334 // p: X(0) dY(1) dW(2) N(3) Cin(4) L(5) Cout(6) kL(7) st(8) pad(9) dil(10) Cgin(11) Cgout(12) Lout(13)
335 2 Value* Cin = p[4];
336 2 Value* L = p[5];
337 2 Value* Cout = p[6];
338 2 Value* kL = p[7];
339 2 Value* st = p[8];
340 2 Value* pad = p[9];
341 2 Value* dil = p[10];
342 2 Value* Cgin = p[11];
343 2 Value* Cgout = p[12];
344 2 Value* Lout = p[13];
345 2 Value* total = c.b.build_mul(c.b.build_mul(Cin, Cgout), kL);
346 2 Value* per = c.b.build_mul(p[3], L);
347 2 Value* tid = c.kb.tid_x();
348 2 Value* block = c.i32(static_cast<int>(kAudioBlock));
349 2 Value* s_red = c.kb.shared_alloc_f32(8);
350
351
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 c.kb.for_range(c.kb.ctaid_x(), total, c.kb.nctaid_x(), [&](Value* w) {
352 2 Value* tmp = c.b.build_udiv(w, kL);
353 2 Value* k = c.b.build_sub(w, c.b.build_mul(tmp, kL));
354 2 Value* cin = c.b.build_udiv(tmp, Cgout);
355 2 Value* ocl = c.b.build_sub(tmp, c.b.build_mul(cin, Cgout));
356 2 Value* g = c.b.build_udiv(cin, Cgin);
357 2 Value* oc = c.b.build_add(c.b.build_mul(g, Cgout), ocl);
358 2 Value* k_dil = c.b.build_mul(k, dil);
359
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* acc = c.kb.for_range_reduce(tid, per, block, c.f32(0.0f), [&](Value* j, Value* cur) {
360 2 Value* n = c.b.build_udiv(j, L);
361 2 Value* l = c.b.build_sub(j, c.b.build_mul(n, L));
362 2 Value* lo = c.b.build_add(c.b.build_sub(c.b.build_mul(l, st), pad), k_dil);
363 2 Value* inb = c.b.build_and(c.b.build_sge(lo, c.i32(0)), c.b.build_slt(lo, Lout));
364
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return emit_if_else_values(
365 2 c, inb, {Type::f32()},
366 4 [&]() -> std::vector<Value*> {
367 2 Value* xi = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, Cin), cin), L), l);
368 2 Value* yi = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, Cout), oc), Lout), lo);
369 4 return {c.b.build_add(cur, c.b.build_mul(load_elem_dt(c, dt, p[0], xi),
370 2 load_elem_dt(c, dt, p[1], yi)))};
371 },
372 4 [&]() -> std::vector<Value*> { return {cur}; })[0];
373 ✗ });
374 2 Value* sum = emit_block_sum_f32(c, s_red, acc);
375
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
376 2 store_elem_dt(c, dt, p[2], w, c.b.build_add(load_elem_dt(c, dt, p[2], w), sum));
377 2 });
378 2 });
379 2 }
380
381 1 void build_fsq(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
382 // p: x(0) levels(1) quant(2) packed(3) N(4) D(5)
383 1 Value* D = p[5];
384
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 emit_vec_grid_stride(c, p[4], 1, kAudioBlock, [&](Value* n) {
385 1 Value* base = c.b.build_mul(n, D);
386
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
2 Value* packed = c.kb.for_range_reduce(
387 2 c.i32(0), D, c.i32(1), c.kb.const_i64(0), [&](Value* it, Value* cur) {
388 1 Value* dd = c.b.build_sub(c.b.build_sub(D, c.i32(1)), it);
389 1 Value* i = c.b.build_add(base, dd);
390 1 Value* L = c.kb.load_i32(ptr_at(c, p[1], dd, 4));
391 1 Value* lm1 = c.b.build_sub(L, c.i32(1));
392 1 Value* flm1 = c.kb.i32_to_f32(lm1);
393 1 Value* h = c.b.build_mul(flm1, c.f32(0.5f));
394 1 Value* v = c.kb.clamp_f32(load_elem_dt(c, dt, p[0], i), c.f32(-1.0f), c.f32(1.0f));
395 1 Value* t = c.b.build_mul(c.b.build_mul(c.b.build_add(v, c.f32(1.0f)), c.f32(0.5f)), flm1);
396 1 Value* fl = c.b.build_floor_f32(t);
397 1 Value* rnd_up = c.b.build_sge(c.b.build_sub(t, fl), c.f32(0.5f));
398 2 Value* idx0 = c.b.build_add(c.b.build_fptosi_i32_f32(fl),
399 1 c.b.build_select(rnd_up, c.i32(1), c.i32(0)));
400 1 Value* idx = clamp_i32(c, idx0, c.i32(0), lm1);
401 1 Value* idx_f = c.kb.i32_to_f32(idx);
402 1 Value* zero_mask = c.b.build_select(c.b.build_eq(L, c.i32(0)), c.i32(1), c.i32(0));
403 4 auto pin_f32 = [&](Value* v) {
404 3 return jit::gpu_mir::bits_to_f32(
405 3 c, c.b.build_xor(jit::gpu_mir::f32_to_bits(c, v), zero_mask));
406 };
407 // Keep NIR from cancelling -(a * (1/h)) * h + a or folding - 1.0 into the NR correction.
408 1 Value* r = pin_f32(c.b.build_sdiv(c.f32(1.0f), h));
409 1 Value* q = pin_f32(c.b.build_mul(idx_f, r));
410 1 Value* e = c.b.build_fma_f32(c.b.build_neg(q), h, idx_f);
411 1 Value* qv = pin_f32(c.b.build_fma_f32(e, r, q));
412 1 Value* qo = c.b.build_sub(qv, c.f32(1.0f));
413 1 store_elem_dt(c, dt, p[2], i, qo);
414 1 return c.b.build_add(c.b.build_mul(cur, c.b.build_sext_i64(L)), c.b.build_sext_i64(idx));
415 });
416 1 c.kb.store_i32(ptr_at(c, p[3], n, 4), c.b.build_trunc_i32(packed));
417 1 });
418 1 }
419
420 1 void build_col2im1d(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_bias, bool cols_f32) {
421 // p: cols(0) bias(1) Y(2) N(3) Cout(4) L(5) kL(6) st(7) pad(8) dil(9) Cgout(10) Lout(11)
422 1 Value* Cout = p[4];
423 1 Value* L = p[5];
424 1 Value* kL = p[6];
425 1 Value* st = p[7];
426 1 Value* pad = p[8];
427 1 Value* dil = p[9];
428 1 Value* Cgout = p[10];
429 1 Value* Lout = p[11];
430 1 Value* groups = c.b.build_udiv(Cout, Cgout);
431 1 Value* total = c.b.build_mul(c.b.build_mul(p[3], Cout), Lout);
432
433
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 emit_vec_grid_stride(c, total, 1, kAudioBlock, [&](Value* i) {
434 1 Value* noc = c.b.build_udiv(i, Lout);
435 1 Value* lo = c.b.build_sub(i, c.b.build_mul(noc, Lout));
436 1 Value* n = c.b.build_udiv(noc, Cout);
437 1 Value* oc = c.b.build_sub(noc, c.b.build_mul(n, Cout));
438 1 Value* g = c.b.build_udiv(oc, Cgout);
439 1 Value* ocl = c.b.build_sub(oc, c.b.build_mul(g, Cgout));
440 2 Value* rowb = c.b.build_mul(c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, groups), g), Cgout), ocl),
441 1 kL);
442
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* init = has_bias ? load_elem_dt(c, dt, p[1], oc) : c.f32(0.0f);
443
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* acc = c.kb.for_range_reduce(c.i32(0), kL, c.i32(1), init, [&](Value* k, Value* cur) {
444 1 Value* t = c.b.build_sub(c.b.build_add(lo, pad), c.b.build_mul(k, dil));
445
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
446 1 c, c.b.build_sge(t, c.i32(0)), {Type::f32()},
447 2 [&]() -> std::vector<Value*> {
448 1 Value* l = c.b.build_udiv(t, st);
449 1 Value* div_ok = c.b.build_eq(c.b.build_sub(t, c.b.build_mul(l, st)), c.i32(0));
450 1 Value* in_l = c.b.build_and(div_ok, c.b.build_slt(l, L));
451
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
452 1 c, in_l, {Type::f32()},
453 2 [&]() -> std::vector<Value*> {
454 1 Value* ci = c.b.build_add(c.b.build_mul(c.b.build_add(rowb, k), L), l);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* v = cols_f32 ? c.kb.load_f32(ptr_at(c, p[0], ci, 4))
456 1 : load_elem_dt(c, dt, p[0], ci);
457 1 return {c.b.build_add(cur, v)};
458 },
459 2 [&]() -> std::vector<Value*> { return {cur}; });
460 ✗ },
461 2 [&]() -> std::vector<Value*> { return {cur}; })[0];
462 ✗ });
463 1 store_elem_dt(c, dt, p[2], i, acc);
464 1 });
465 1 }
466
467 // conv_transpose1d as a direct gather: one thread per output (n, oc, lo)
468 // sums Wt over the taps whose input position l = (lo + pad - k*dil) / st is
469 // whole and in range, and the group's Cgin input channels. Wt is (C_in,
470 // Cgout*kL). One launch for any groups, where the GEMM + col2im path costs a
471 // transpose and a matmul per group and image: a depthwise upsample (groups =
472 // channels, Cgin = Cgout = 1) is all launches and no math that way.
473 2 void build_convt1d_fwd(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool has_bias) {
474 // p: X(0) Wt(1) bias(2) Y(3) N(4) Cin(5) L(6) Cout(7) kL(8) st(9) pad(10) dil(11)
475 // Cgin(12) Cgout(13) Lout(14)
476 2 Value* Cin = p[5];
477 2 Value* L = p[6];
478 2 Value* Cout = p[7];
479 2 Value* kL = p[8];
480 2 Value* st = p[9];
481 2 Value* pad = p[10];
482 2 Value* dil = p[11];
483 2 Value* Cgin = p[12];
484 2 Value* Cgout = p[13];
485 2 Value* Lout = p[14];
486 2 Value* total = c.b.build_mul(c.b.build_mul(p[4], Cout), Lout);
487
488
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 emit_vec_grid_stride(c, total, 1, kAudioBlock, [&](Value* i) {
489 2 Value* noc = c.b.build_udiv(i, Lout);
490 2 Value* lo = c.b.build_sub(i, c.b.build_mul(noc, Lout));
491 2 Value* n = c.b.build_udiv(noc, Cout);
492 2 Value* oc = c.b.build_sub(noc, c.b.build_mul(n, Cout));
493 2 Value* g = c.b.build_udiv(oc, Cgout);
494 2 Value* ocl = c.b.build_sub(oc, c.b.build_mul(g, Cgout));
495 2 Value* cin0 = c.b.build_mul(g, Cgin);
496 2 Value* x_n = c.b.build_mul(n, Cin);
497
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 Value* init = has_bias ? load_elem_dt(c, dt, p[2], oc) : c.f32(0.0f);
498
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* acc = c.kb.for_range_reduce(c.i32(0), kL, c.i32(1), init, [&](Value* k, Value* cur) {
499 2 Value* t = c.b.build_sub(c.b.build_add(lo, pad), c.b.build_mul(k, dil));
500
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return emit_if_else_values(
501 2 c, c.b.build_sge(t, c.i32(0)), {Type::f32()},
502 4 [&]() -> std::vector<Value*> {
503 2 Value* l = c.b.build_udiv(t, st);
504 2 Value* div_ok = c.b.build_eq(c.b.build_sub(t, c.b.build_mul(l, st)), c.i32(0));
505 2 Value* in_l = c.b.build_and(div_ok, c.b.build_slt(l, L));
506
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return emit_if_else_values(
507 2 c, in_l, {Type::f32()},
508 4 [&]() -> std::vector<Value*> {
509
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 Value* sum = c.kb.for_range_reduce(
510 4 c.i32(0), Cgin, c.i32(1), cur, [&](Value* ci, Value* a) {
511 2 Value* cin = c.b.build_add(cin0, ci);
512 2 Value* xi = c.b.build_add(c.b.build_mul(c.b.build_add(x_n, cin), L), l);
513 4 Value* wi = c.b.build_add(
514 2 c.b.build_mul(c.b.build_add(c.b.build_mul(cin, Cgout), ocl), kL), k);
515 4 return c.b.build_fma_f32(load_elem_dt(c, dt, p[0], xi),
516 2 load_elem_dt(c, dt, p[1], wi), a);
517 });
518 2 return {sum};
519 ✗ },
520 4 [&]() -> std::vector<Value*> { return {cur}; });
521 ✗ },
522 4 [&]() -> std::vector<Value*> { return {cur}; })[0];
523 ✗ });
524 2 store_elem_dt(c, dt, p[3], i, acc);
525 2 });
526 2 }
527
528 // sine_source_forward. p: f0(0) cyc(1) phase0(2) w(3) b(4) y(5)
529 // F(6) U(7) D(8) k0(9) k10(10) sr(11) amp(12) noise_std(13) uv_std(14)
530 // AU_SINE_CYC, one thread: cyc[f] = the fundamental's cycles before frame f,
531 // over voiced frames, kept in [0, 1) (harmonic h's phase is frac((h+1) cyc),
532 // h+1 being whole, so the fraction is all a harmonic needs).
533 1 void build_sine_cycles(Ctx& c, const std::vector<Value*>& p) {
534 1 Value* per_frame = emit_div_rn(c, c.kb.i32_to_f32(p[7]), p[11]); // U / sr
535
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.if_then(c.b.build_eq(c.kb.global_tid_x(), c.i32(0)), [&] {
536
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.for_range_reduce(c.i32(0), p[6], c.i32(1), c.f32(0.0f), [&](Value* f, Value* cur) {
537 1 c.kb.store_f32(ptr_at(c, p[1], f, 4), cur);
538 1 Value* f0 = c.kb.load_f32(ptr_at(c, p[0], f, 4));
539 1 Value* nx = c.b.build_add(cur, c.b.build_mul(f0, per_frame));
540 1 nx = c.b.build_sub(nx, c.b.build_floor_f32(nx));
541 1 return c.b.build_select(c.b.build_sgt(f0, c.f32(0.0f)), nx, cur);
542 });
543 1 });
544 1 }
545
546 // AU_SINE_SRC, a thread a sample t (frame f = t / U, j = t mod U):
547 // y = tanh(b + sum_h w_h s_h),
548 // s_h = voiced ? amp sin(phase0_h + 2pi frac((h+1)(cyc[f] + (j+1) f0/sr))) + noise_std n
549 // : uv_std n, n = Philox normal at counter t D + h.
550 1 void build_sine_source(Ctx& c, const std::vector<Value*>& p) {
551 1 Value* U = p[7];
552 1 Value* D = p[8];
553 1 Value* total = c.b.build_mul(p[6], U);
554 1 Value* inv_sr = emit_div_rn(c, c.f32(1.0f), p[11]);
555 1 Value* two_pi = c.f32(6.28318530717958647692f);
556
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 emit_vec_grid_stride(c, total, 1, kAudioBlock, [&](Value* t) {
557 1 Value* f = c.b.build_udiv(t, U);
558 1 Value* j1 = c.b.build_add(c.b.build_sub(t, c.b.build_mul(f, U)), c.i32(1));
559 1 Value* f0 = c.kb.load_f32(ptr_at(c, p[0], f, 4));
560 1 Value* voiced = c.b.build_sgt(f0, c.f32(0.0f));
561 2 Value* cyc = c.b.build_add(c.kb.load_f32(ptr_at(c, p[1], f, 4)),
562 1 c.b.build_mul(c.kb.i32_to_f32(j1), c.b.build_mul(f0, inv_sr)));
563 1 Value* ctr0 = c.b.build_mul(t, D);
564
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* acc = c.kb.for_range_reduce(c.i32(0), D, c.i32(1), c.kb.load_f32(p[4]), [&](Value* h, Value* a) {
565 1 Value* n = philox_normal(c, p[9], p[10], c.b.build_zext_i64(c.b.build_add(ctr0, h)));
566 1 Value* x = c.b.build_mul(c.kb.i32_to_f32(c.b.build_add(h, c.i32(1))), cyc);
567 1 x = c.b.build_sub(x, c.b.build_floor_f32(x));
568 1 Value* arg = c.b.build_fma_f32(x, two_pi, c.kb.load_f32(ptr_at(c, p[2], h, 4)));
569 1 Value* sv = c.b.build_fma_f32(emit_sincos_acc(c, arg).first, p[12], c.b.build_mul(n, p[13]));
570 1 Value* s = c.b.build_select(voiced, sv, c.b.build_mul(n, p[14]));
571 1 return c.b.build_fma_f32(c.kb.load_f32(ptr_at(c, p[3], h, 4)), s, a);
572 });
573 1 c.kb.store_f32(ptr_at(c, p[5], t, 4), emit_tanh(c, acc));
574 1 });
575 1 }
576
577 2 void build_vq(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
578 // p: x(0) codebook(1) quantized(2) indices(3) N(4) D(5) K(6)
579 2 Value* N = p[4];
580 2 Value* D = p[5];
581 2 Value* K = p[6];
582 2 Value* tid = c.kb.tid_x();
583 2 Value* block = c.i32(static_cast<int>(kAudioBlock));
584 2 Value* s_val = c.kb.shared_alloc_f32(8);
585 2 Value* s_idx = c.kb.shared_alloc_i32(8);
586 2 Value* inv_bi = c.i32(-1);
587
588
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 c.kb.for_range(c.kb.ctaid_x(), N, c.kb.nctaid_x(), [&](Value* n) {
589 2 Value* x_base = c.b.build_mul(n, D);
590
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto best = c.kb.for_range_reduce_n(
591
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 tid, K, block, {c.f32(-3.4028235e38f), inv_bi}, [&](Value* k, const std::vector<Value*>& st) {
592 2 Value* c_base = c.b.build_mul(k, D);
593
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* d2 = c.kb.for_range_reduce(c.i32(0), D, c.i32(1), c.f32(0.0f), [&](Value* j, Value* acc) {
594 4 Value* df = c.b.build_sub(load_elem_dt(c, dt, p[0], c.b.build_add(x_base, j)),
595 2 load_elem_dt(c, dt, p[1], c.b.build_add(c_base, j)));
596 2 return c.b.build_add(acc, c.b.build_mul(df, df));
597 });
598 2 Value* neg_d2 = c.b.build_neg(d2);
599 2 Value* win = c.b.build_or(c.b.build_eq(st[1], inv_bi), c.b.build_sgt(neg_d2, st[0]));
600 2 return std::vector<Value*>{c.b.build_select(win, neg_d2, st[0]), c.b.build_select(win, k, st[1])};
601 ✗ });
602
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 Value* bi_safe = c.b.build_select(c.b.build_eq(best[1], inv_bi), c.i32(-2), best[1]);
603
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* win = emit_block_argmax_f32(c, s_val, s_idx, best[0], bi_safe);
604
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 Value* kb = c.b.build_select(c.b.build_uge(win, K), c.i32(0), win);
605
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(tid, c.i32(0)), [&] { c.kb.store_i32(ptr_at(c, p[3], n, 4), kb); });
606
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* kb_base = c.b.build_mul(kb, D);
607
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 c.kb.for_range(tid, D, block, [&](Value* j) {
608 4 store_elem_dt(c, dt, p[2], c.b.build_add(x_base, j),
609 2 load_elem_dt(c, dt, p[1], c.b.build_add(kb_base, j)));
610 2 });
611
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.kb.sync();
612 2 });
613 2 }
614
615 } // namespace
616
617 30 brass::Function* build_audio_mir(brass::Module& mod, std::uint32_t op, Dtype dt, std::uint32_t aux0, bool aux1) {
618 30 std::vector<Type> params;
619
9/10
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 12 times.
✗ Branch 9 not taken.
30 switch (op) {
620 case AU_SNAKE:
621
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
622 2 break;
623 case AU_SNAKE_BWD:
624
4/8
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
20 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
625 15 Type::i32(), Type::i32(), Type::i32()};
626 5 break;
627 case AU_PAD:
628 case AU_PAD_BWD:
629
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
630 6 break;
631 case AU_RESAMPLE:
632 case AU_RESAMPLE_BWD:
633
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
634 4 break;
635 case AU_CAUSAL:
636
6/12
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 3 times.
✗ Branch 11 not taken.
18 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
637 15 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
638 3 break;
639 case AU_CONVT_BWD_W:
640
12/24
✓ 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.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 2 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 2 times.
✗ Branch 23 not taken.
24 params = {Type::ptr(), Type::ptr(), Type::ptr(),
641 10 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
642 12 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
643 2 break;
644 case AU_FSQ:
645
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32()};
646 1 break;
647 case AU_VQ:
648
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()};
649 2 break;
650 case AU_COL2IM:
651
10/20
✓ 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.
✓ Branch 10 taken 1 time.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 time.
✗ Branch 13 not taken.
✓ Branch 14 taken 1 time.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 time.
✗ Branch 17 not taken.
✓ Branch 18 taken 1 time.
✗ Branch 19 not taken.
10 params = {Type::ptr(), Type::ptr(), Type::ptr(),
652 5 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
653 4 Type::i32(), Type::i32(), Type::i32(), Type::i32()};
654 1 break;
655 case AU_CONVT_FWD:
656
12/24
✓ 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.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 2 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 2 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 2 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 2 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 2 times.
✗ Branch 23 not taken.
24 params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
657 12 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
658 10 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()};
659 2 break;
660 case AU_SINE_CYC:
661 case AU_SINE_SRC:
662
20/20
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 2 times.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 2 times.
✓ Branch 11 taken 2 times.
✓ Branch 12 taken 2 times.
✓ Branch 13 taken 2 times.
✓ Branch 14 taken 2 times.
✓ Branch 15 taken 2 times.
✓ Branch 16 taken 2 times.
✓ Branch 17 taken 2 times.
✓ Branch 18 taken 2 times.
✓ Branch 19 taken 2 times.
✗ params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
663 ✗ Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
664 ✗ Type::f32(), Type::f32(), Type::f32(), Type::f32()};
665 2 break;
666 default:
667 ✗ return nullptr;
668 }
669
670
7/14
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 30 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 30 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 30 times.
✗ Branch 13 not taken.
90 std::string name = "mir_audio_" + std::to_string(op) + "_" + std::to_string(static_cast<int>(dt)) + "_" +
671
4/8
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 30 times.
✗ Branch 7 not taken.
60 std::to_string(aux0) + "_" + std::to_string(aux1 ? 1 : 0);
672
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 Function* fn = mod.create_function(name, Type::void_type(), params);
673
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 brass::codegen::KernelBuilder kb(mod, fn);
674
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 Ctx c(kb);
675
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 auto p = entry_params(c, fn);
676
677
9/10
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 time.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 time.
✓ Branch 7 taken 1 time.
✓ Branch 8 taken 17 times.
✗ Branch 9 not taken.
30 switch (op) {
678 case AU_SNAKE:
679 case AU_SNAKE_BWD:
680
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 10 times.
17 build_snake(c, p, dt, aux0 != 0u, op == AU_SNAKE_BWD);
681 7 break;
682 case AU_PAD:
683 case AU_PAD_BWD:
684
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
✗ build_pad1d(c, p, dt, aux0, op == AU_PAD_BWD);
685 6 break;
686 case AU_RESAMPLE:
687 case AU_RESAMPLE_BWD:
688
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
✗ build_resample1d(c, p, dt, aux0, op == AU_RESAMPLE_BWD);
689 4 break;
690 case AU_CAUSAL:
691
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 build_causal_conv1d(c, p, dt, aux0 != 0u);
692 3 break;
693 case AU_CONVT_BWD_W:
694
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 build_convt1d_bwd_w(c, p, dt);
695 2 break;
696 case AU_FSQ:
697
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 build_fsq(c, p, dt);
698 1 break;
699 case AU_VQ:
700
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 build_vq(c, p, dt);
701 2 break;
702 case AU_COL2IM:
703
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 build_col2im1d(c, p, dt, aux0 != 0u, aux1);
704 1 break;
705 case AU_CONVT_FWD:
706
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 build_convt1d_fwd(c, p, dt, aux0 != 0u);
707 2 break;
708 case AU_SINE_CYC:
709
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 build_sine_cycles(c, p);
710 1 break;
711 case AU_SINE_SRC:
712
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 build_sine_source(c, p);
713 1 break;
714 default:
715 ✗ break;
716 }
717
718
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 c.b.build_ret_void();
719
1/2
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
30 return finalize_fn(fn);
720 62 }
721
722 } // namespace brotensor::kernels
723