src/kernels/mir_spectral.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_spectral.h" | ||
| 2 | #include "mir_common.h" | ||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <array> | ||
| 8 | #include <string> | ||
| 9 | #include <utility> | ||
| 10 | #include <vector> | ||
| 11 | |||
| 12 | namespace brotensor::kernels { | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | 18 | std::pair<Value*, Value*> emit_twiddle(Ctx& c, Value* m, Value* L) { | |
| 17 | 18 | Value* m64 = c.b.build_zext_i64(m); | |
| 18 | 18 | Value* L64 = c.b.build_zext_i64(L); | |
| 19 | 18 | Value* m4 = c.b.build_shl(m64, c.kb.const_i64(2)); | |
| 20 | 18 | Value* q = c.b.build_trunc_i32(c.b.build_udiv(m4, L64)); | |
| 21 | 18 | Value* r0 = c.b.build_trunc_i32(c.b.build_sub(m4, c.b.build_mul(c.b.build_zext_i64(q), L64))); | |
| 22 | 18 | Value* swp = c.b.build_ugt(c.b.build_shl(r0, c.i32(1)), L); | |
| 23 | 18 | Value* r = c.b.build_select(swp, c.b.build_sub(L, r0), r0); | |
| 24 | 36 | Value* x = c.b.build_mul(c.kb.u32_to_f32(r), | |
| 25 | 18 | c.b.build_sdiv(c.f32(1.57079632679489662f), c.kb.u32_to_f32(L))); | |
| 26 | 90 | auto [sx, cx] = emit_sincos_acc(c, x); | |
| 27 | 54 | Value* cq = c.b.build_select(swp, sx, cx); | |
| 28 | 54 | Value* sq = c.b.build_select(swp, cx, sx); | |
| 29 | 18 | Value* is0 = c.b.build_eq(q, c.i32(0)); | |
| 30 | 18 | Value* is1 = c.b.build_eq(q, c.i32(1)); | |
| 31 | 18 | Value* is2 = c.b.build_eq(q, c.i32(2)); | |
| 32 | 18 | Value* ncq = c.b.build_neg(cq); | |
| 33 | 18 | Value* nsq = c.b.build_neg(sq); | |
| 34 | 18 | Value* cos_v = c.b.build_select(is0, cq, c.b.build_select(is1, nsq, c.b.build_select(is2, ncq, sq))); | |
| 35 | 18 | Value* sin_v = c.b.build_select(is0, sq, c.b.build_select(is1, cq, c.b.build_select(is2, nsq, ncq))); | |
| 36 | 18 | return {cos_v, sin_v}; | |
| 37 | } | ||
| 38 | |||
| 39 | 5 | Value* hermite_wk(Ctx& c, Value* k, Value* L) { | |
| 40 | 5 | Value* k0 = c.b.build_eq(k, c.i32(0)); | |
| 41 | 5 | Value* even = c.b.build_eq(c.b.build_and(L, c.i32(1)), c.i32(0)); | |
| 42 | 5 | Value* nyq = c.b.build_and(even, c.b.build_eq(c.b.build_shl(k, c.i32(1)), L)); | |
| 43 | 5 | return c.b.build_select(c.b.build_or(k0, nyq), c.f32(1.0f), c.f32(2.0f)); | |
| 44 | } | ||
| 45 | |||
| 46 | 1 | Value* emit_atan2_acc(Ctx& c, Value* y, Value* x) { | |
| 47 | 1 | Value* ax = c.b.build_fabs_f32(x); | |
| 48 | 1 | Value* ay = c.b.build_fabs_f32(y); | |
| 49 | 1 | Value* xbits = jit::gpu_mir::f32_to_bits(c, x); | |
| 50 | 1 | Value* ybits = jit::gpu_mir::f32_to_bits(c, y); | |
| 51 | 1 | Value* x_neg = c.b.build_ne(c.b.build_lshr(xbits, c.i32(31)), c.i32(0)); | |
| 52 | 1 | Value* y_neg = c.b.build_ne(c.b.build_lshr(ybits, c.i32(31)), c.i32(0)); | |
| 53 | 1 | Value* both_zero = c.b.build_and(c.b.build_eq(ax, c.f32(0.0f)), c.b.build_eq(ay, c.f32(0.0f))); | |
| 54 | 1 | Value* pi = c.f32(3.14159265358979324f); | |
| 55 | 1 | Value* half_pi = c.f32(1.57079632679489662f); | |
| 56 | |||
| 57 | 1 | return emit_if_else_values( | |
| 58 | 1 | c, both_zero, {Type::f32()}, | |
| 59 | 2 | [&]() -> std::vector<Value*> { | |
| 60 | 1 | Value* r0 = c.b.build_select(x_neg, pi, c.f32(0.0f)); | |
| 61 | 1 | return {c.b.build_select(y_neg, c.b.build_neg(r0), r0)}; | |
| 62 | }, | ||
| 63 | 2 | [&]() -> std::vector<Value*> { | |
| 64 | 1 | Value* sw = c.b.build_sgt(ay, ax); | |
| 65 | 1 | Value* a0 = c.b.build_select(sw, emit_div_rn(c, ax, ay), emit_div_rn(c, ay, ax)); | |
| 66 | 1 | Value* oct = c.b.build_sgt(a0, c.f32(0.414213562373095f)); | |
| 67 | 2 | Value* a = c.b.build_select( | |
| 68 | 1 | oct, emit_div_rn(c, c.b.build_sub(a0, c.f32(1.0f)), c.b.build_add(a0, c.f32(1.0f))), a0); | |
| 69 | 1 | Value* off = c.b.build_select(oct, c.f32(0.785398163397448310f), c.f32(0.0f)); | |
| 70 | 1 | Value* z = c.b.build_mul(a, a); | |
| 71 | 1 | Value* p = c.f32(8.05374449538e-2f); | |
| 72 | 1 | p = c.b.build_sub(c.b.build_mul(p, z), c.f32(1.38776856032e-1f)); | |
| 73 | 1 | p = c.b.build_add(c.b.build_mul(p, z), c.f32(1.99777106478e-1f)); | |
| 74 | 1 | p = c.b.build_sub(c.b.build_mul(p, z), c.f32(3.33329491539e-1f)); | |
| 75 | 1 | Value* r = c.b.build_add(c.b.build_mul(c.b.build_mul(p, z), a), a); | |
| 76 | 1 | r = c.b.build_add(r, off); | |
| 77 | 1 | r = c.b.build_select(sw, c.b.build_sub(half_pi, r), r); | |
| 78 | 1 | r = c.b.build_select(x_neg, c.b.build_sub(pi, r), r); | |
| 79 | 1 | return {c.b.build_select(y_neg, c.b.build_neg(r), r)}; | |
| 80 | })[0]; | ||
| 81 | ✗ | } | |
| 82 | |||
| 83 | 20 | std::pair<Value*, Value*> cover_range(Ctx& c, Value* p, Value* hop, Value* pad_lo, Value* win, Value* frames) { | |
| 84 | 20 | Value* t = c.b.build_sub(p, pad_lo); | |
| 85 | 20 | Value* div_t = c.b.build_sdiv(t, hop); | |
| 86 | 20 | Value* fr_m1 = c.b.build_sub(frames, c.i32(1)); | |
| 87 | 20 | Value* min_f1 = c.b.build_select(c.b.build_slt(div_t, fr_m1), div_t, fr_m1); | |
| 88 | 20 | Value* f1 = c.b.build_select(c.b.build_slt(t, c.i32(0)), c.i32(-1), min_f1); | |
| 89 | 20 | Value* lo = c.b.build_add(c.b.build_sub(t, win), c.i32(1)); | |
| 90 | 20 | Value* lo_div = c.b.build_sdiv(c.b.build_sub(c.b.build_add(lo, hop), c.i32(1)), hop); | |
| 91 | 20 | Value* f0 = c.b.build_select(c.b.build_sle(lo, c.i32(0)), c.i32(0), lo_div); | |
| 92 | 20 | return {f0, f1}; | |
| 93 | } | ||
| 94 | |||
| 95 | 10 | void build_basis(Ctx& c, const std::vector<Value*>& p, std::uint32_t mode, bool weights, bool has_win) { | |
| 96 | // p: win(0) B(1) rows(2) cols(3) L(4) tofs(5) row0(6) sgn(7) scale(8) | ||
| 97 | 10 | Value* cols = p[3]; | |
| 98 | 10 | Value* L = p[4]; | |
| 99 | 10 | Value* tofs = p[5]; | |
| 100 | 10 | Value* row0 = p[6]; | |
| 101 | 10 | Value* sg = c.kb.i32_to_f32(p[7]); | |
| 102 | 10 | Value* scale = p[8]; | |
| 103 | 10 | Value* L64 = c.b.build_zext_i64(L); | |
| 104 | 10 | Value* total = c.b.build_mul(p[2], cols); | |
| 105 | |||
| 106 | 20 | emit_vec_grid_stride(c, total, 1, kSpectralBlock, [&](Value* t) { | |
| 107 | 10 | Value* o0 = c.b.build_udiv(t, cols); | |
| 108 | 10 | Value* i = c.b.build_sub(t, c.b.build_mul(o0, cols)); | |
| 109 | 10 | Value* o = c.b.build_add(o0, row0); | |
| 110 | 10 | Value *k = nullptr, *n = nullptr, *widx = nullptr, *oc = nullptr, *ic = nullptr; | |
| 111 | 10 | if (mode == BASIS_R2C) { | |
| 112 | 5 | k = c.b.build_lshr(o, c.i32(1)); | |
| 113 | 5 | oc = c.b.build_and(o, c.i32(1)); | |
| 114 | 5 | n = c.b.build_add(i, tofs); | |
| 115 | 5 | widx = i; | |
| 116 | 5 | ic = c.i32(0); | |
| 117 | 10 | } else if (mode == BASIS_C2R) { | |
| 118 | 4 | k = c.b.build_lshr(i, c.i32(1)); | |
| 119 | 4 | ic = c.b.build_and(i, c.i32(1)); | |
| 120 | 4 | n = c.b.build_add(o, tofs); | |
| 121 | 4 | widx = o; | |
| 122 | 4 | oc = c.i32(0); | |
| 123 | 4 | } else { | |
| 124 | 1 | k = c.b.build_lshr(o, c.i32(1)); | |
| 125 | 1 | oc = c.b.build_and(o, c.i32(1)); | |
| 126 | 1 | n = c.b.build_lshr(i, c.i32(1)); | |
| 127 | 1 | ic = c.b.build_and(i, c.i32(1)); | |
| 128 | 1 | widx = c.i32(0); | |
| 129 | } | ||
| 130 | 10 | Value* km = c.b.build_umod(k, L); | |
| 131 | 10 | Value* nm = c.b.build_umod(n, L); | |
| 132 | 10 | Value* m = c.b.build_trunc_i32(c.b.build_umod(c.b.build_mul(c.b.build_zext_i64(km), c.b.build_zext_i64(nm)), L64)); | |
| 133 | 40 | auto [cv, sv] = emit_twiddle(c, m, L); | |
| 134 | 20 | Value* s_sv = c.b.build_mul(sg, sv); | |
| 135 | 10 | Value* oc0 = c.b.build_eq(oc, c.i32(0)); | |
| 136 | 10 | Value* ic0 = c.b.build_eq(ic, c.i32(0)); | |
| 137 | 20 | Value* v_oc0 = c.b.build_select(ic0, cv, c.b.build_neg(s_sv)); | |
| 138 | 20 | Value* v_oc1 = c.b.build_select(ic0, s_sv, cv); | |
| 139 | 10 | Value* v = c.b.build_mul(c.b.build_select(oc0, v_oc0, v_oc1), scale); | |
| 140 | 10 | if (weights) v = c.b.build_mul(v, hermite_wk(c, k, L)); | |
| 141 | 10 | if (has_win) v = c.b.build_mul(v, c.kb.load_f32(ptr_at(c, p[0], widx, 4))); | |
| 142 | 10 | c.kb.store_f32(ptr_at(c, p[1], t, 4), v); | |
| 143 | 10 | }); | |
| 144 | 10 | } | |
| 145 | |||
| 146 | 3 | void build_ola_or_istft_adj(Ctx& c, const std::vector<Value*>& p, bool is_ola) { | |
| 147 | // p: a(0) win(1) d(2) N(3) sig_len(4) frames(5) hop(6) pad_lo(7) win_len(8) shift(9) padded_len(10) | ||
| 148 | 3 | Value* sig_len = p[4]; | |
| 149 | 3 | Value* frames = p[5]; | |
| 150 | 3 | Value* hop = p[6]; | |
| 151 | 3 | Value* pad_lo = p[7]; | |
| 152 | 3 | Value* win_len = p[8]; | |
| 153 | 3 | Value* shift = p[9]; | |
| 154 | 3 | Value* per = is_ola ? sig_len : p[10]; | |
| 155 | 3 | Value* total = c.b.build_mul(p[3], per); | |
| 156 | |||
| 157 | 6 | emit_vec_grid_stride(c, total, 1, kSpectralBlock, [&](Value* t) { | |
| 158 | 3 | Value* b = c.b.build_udiv(t, per); | |
| 159 | 3 | Value* rem = c.b.build_sub(t, c.b.build_mul(b, per)); | |
| 160 | 3 | Value* pos = is_ola ? c.b.build_add(rem, shift) : rem; | |
| 161 | 3 | Value* n = c.b.build_sub(pos, shift); | |
| 162 | 3 | Value* oob = is_ola ? c.i32(0) : c.b.build_or(c.b.build_slt(n, c.i32(0)), c.b.build_sge(n, sig_len)); | |
| 163 | |||
| 164 | 3 | Value* out = emit_if_else_values( | |
| 165 | 3 | c, oob, {Type::f32()}, | |
| 166 | 6 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; }, | |
| 167 | 6 | [&]() -> std::vector<Value*> { | |
| 168 | 9 | auto [f0, f1] = cover_range(c, pos, hop, pad_lo, win_len, frames); | |
| 169 | 3 | auto res = c.kb.for_range_reduce_n( | |
| 170 | 6 | f0, c.b.build_add(f1, c.i32(1)), c.i32(1), {c.f32(0.0f), c.f32(0.0f)}, | |
| 171 | 6 | [&](Value* f, const std::vector<Value*>& cur) { | |
| 172 | 3 | Value* j = c.b.build_sub(c.b.build_sub(pos, c.b.build_mul(f, hop)), pad_lo); | |
| 173 | 3 | Value* w = c.kb.load_f32(ptr_at(c, p[1], j, 4)); | |
| 174 | 3 | Value* env = c.b.build_add(cur[0], c.b.build_mul(w, w)); | |
| 175 | 3 | Value* acc = cur[1]; | |
| 176 | 3 | if (is_ola) { | |
| 177 | 4 | Value* a_idx = c.b.build_add( | |
| 178 | 2 | c.b.build_mul(c.b.build_add(c.b.build_mul(b, frames), f), win_len), j); | |
| 179 | 2 | acc = c.b.build_add(acc, c.kb.load_f32(ptr_at(c, p[0], a_idx, 4))); | |
| 180 | 2 | } | |
| 181 | 3 | return std::vector<Value*>{env, acc}; | |
| 182 | }); | ||
| 183 | 3 | Value* env = res[0]; | |
| 184 | 3 | Value* num = is_ola ? res[1] : c.kb.load_f32(ptr_at(c, p[0], c.b.build_add(c.b.build_mul(b, sig_len), n), 4)); | |
| 185 | 3 | return {c.b.build_select(c.b.build_sgt(env, c.f32(1e-10f)), emit_div_rn(c, num, env), c.f32(0.0f))}; | |
| 186 | 3 | })[0]; | |
| 187 | 3 | c.kb.store_f32(ptr_at(c, p[2], t, 4), out); | |
| 188 | 3 | }); | |
| 189 | 3 | } | |
| 190 | |||
| 191 | 3 | void build_stft_adj(Ctx& c, const std::vector<Value*>& p, bool center) { | |
| 192 | // p: G(0) dSignal(1) N(2) sig_len(3) frames(4) hop(5) pad_lo(6) win_len(7) half_nfft(8) padded_len(9) | ||
| 193 | 3 | Value* L = p[3]; | |
| 194 | 3 | Value* frames = p[4]; | |
| 195 | 3 | Value* hop = p[5]; | |
| 196 | 3 | Value* pad_lo = p[6]; | |
| 197 | 3 | Value* win_len = p[7]; | |
| 198 | 3 | Value* half = p[8]; | |
| 199 | 3 | Value* plen = p[9]; | |
| 200 | 3 | Value* total = c.b.build_mul(p[2], L); | |
| 201 | |||
| 202 | 6 | emit_vec_grid_stride(c, total, 1, kSpectralBlock, [&](Value* t) { | |
| 203 | 3 | Value* b = c.b.build_udiv(t, L); | |
| 204 | 3 | Value* s = c.b.build_sub(t, c.b.build_mul(b, L)); | |
| 205 | 20 | auto accum_at = [&](Value* pos, Value* acc_in) { | |
| 206 | 51 | auto [f0, f1] = cover_range(c, pos, hop, pad_lo, win_len, frames); | |
| 207 | 34 | return c.kb.for_range_reduce( | |
| 208 | 51 | f0, c.b.build_add(f1, c.i32(1)), c.i32(1), acc_in, [&](Value* f, Value* cur) { | |
| 209 | 17 | Value* j = c.b.build_sub(c.b.build_sub(pos, c.b.build_mul(f, hop)), pad_lo); | |
| 210 | 17 | Value* idx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(b, frames), f), win_len), j); | |
| 211 | 17 | return c.b.build_add(cur, c.kb.load_f32(ptr_at(c, p[0], idx, 4))); | |
| 212 | }); | ||
| 213 | ✗ | }; | |
| 214 | 3 | if (!center) { | |
| 215 | 1 | c.kb.store_f32(ptr_at(c, p[1], t, 4), accum_at(s, c.f32(0.0f))); | |
| 216 | 1 | return; | |
| 217 | } | ||
| 218 | 2 | Value* per = c.b.build_shl(c.b.build_sub(L, c.i32(1)), c.i32(1)); | |
| 219 | 2 | Value* l_one = c.b.build_eq(L, c.i32(1)); | |
| 220 | 2 | std::array<Value*, 8> p_vals{}; | |
| 221 | 2 | std::array<Value*, 8> v_flags{}; | |
| 222 | 2 | Value* acc = c.f32(0.0f); | |
| 223 | 18 | for (int idx = 0; idx < 8; ++idx) { | |
| 224 | 16 | const int k = (idx >> 1) - 1; | |
| 225 | 16 | const int sg = idx & 1; | |
| 226 | 16 | Value* q = c.b.build_add(sg == 0 ? s : c.b.build_neg(s), c.b.build_mul(c.i32(k), per)); | |
| 227 | 16 | Value* pc = c.b.build_add(q, half); | |
| 228 | 16 | p_vals[static_cast<std::size_t>(idx)] = pc; | |
| 229 | 16 | Value* valid = c.b.build_and(c.b.build_sge(pc, c.i32(0)), c.b.build_slt(pc, plen)); | |
| 230 | 16 | if (k != 0 || sg != 0) valid = c.b.build_and(valid, c.b.build_eq(l_one, c.i32(0))); | |
| 231 | 72 | for (int prev = 0; prev < idx; ++prev) { | |
| 232 | 112 | Value* same = c.b.build_and(v_flags[static_cast<std::size_t>(prev)], | |
| 233 | 56 | c.b.build_eq(p_vals[static_cast<std::size_t>(prev)], pc)); | |
| 234 | 56 | valid = c.b.build_and(valid, c.b.build_eq(same, c.i32(0))); | |
| 235 | 56 | } | |
| 236 | 16 | v_flags[static_cast<std::size_t>(idx)] = valid; | |
| 237 | 16 | acc = emit_if_else_values( | |
| 238 | 16 | c, valid, {Type::f32()}, | |
| 239 | 32 | [&]() -> std::vector<Value*> { return {accum_at(pc, acc)}; }, | |
| 240 | 32 | [&]() -> std::vector<Value*> { return {acc}; })[0]; | |
| 241 | 16 | } | |
| 242 | 2 | c.kb.store_f32(ptr_at(c, p[1], t, 4), acc); | |
| 243 | 3 | }); | |
| 244 | 3 | } | |
| 245 | |||
| 246 | 6 | void build_complex(Ctx& c, const std::vector<Value*>& p, std::uint32_t op) { | |
| 247 | // a(0) b(1) c_dy(2) d(3) e(4) n(5) | ||
| 248 | 12 | emit_vec_grid_stride(c, p[5], 1, kSpectralBlock, [&](Value* i) { | |
| 249 | 6 | Value* i2 = c.b.build_shl(i, c.i32(1)); | |
| 250 | 6 | Value* i2p1 = c.b.build_add(i2, c.i32(1)); | |
| 251 | 6 | if (op == SP_CX_MUL) { | |
| 252 | 1 | Value* ar = c.kb.load_f32(ptr_at(c, p[0], i2, 4)); | |
| 253 | 1 | Value* ai = c.kb.load_f32(ptr_at(c, p[0], i2p1, 4)); | |
| 254 | 1 | Value* br = c.kb.load_f32(ptr_at(c, p[1], i2, 4)); | |
| 255 | 1 | Value* bi = c.kb.load_f32(ptr_at(c, p[1], i2p1, 4)); | |
| 256 | 1 | c.kb.store_f32(ptr_at(c, p[3], i2, 4), c.b.build_sub(c.b.build_mul(ar, br), c.b.build_mul(ai, bi))); | |
| 257 | 1 | c.kb.store_f32(ptr_at(c, p[3], i2p1, 4), c.b.build_add(c.b.build_mul(ar, bi), c.b.build_mul(ai, br))); | |
| 258 | 6 | } else if (op == SP_CX_MUL_BWD) { | |
| 259 | 1 | Value* ar = c.kb.load_f32(ptr_at(c, p[0], i2, 4)); | |
| 260 | 1 | Value* ai = c.kb.load_f32(ptr_at(c, p[0], i2p1, 4)); | |
| 261 | 1 | Value* br = c.kb.load_f32(ptr_at(c, p[1], i2, 4)); | |
| 262 | 1 | Value* bi = c.kb.load_f32(ptr_at(c, p[1], i2p1, 4)); | |
| 263 | 1 | Value* gr = c.kb.load_f32(ptr_at(c, p[2], i2, 4)); | |
| 264 | 1 | Value* gi = c.kb.load_f32(ptr_at(c, p[2], i2p1, 4)); | |
| 265 | 1 | Value* dar_p = ptr_at(c, p[3], i2, 4); | |
| 266 | 1 | Value* dai_p = ptr_at(c, p[3], i2p1, 4); | |
| 267 | 1 | Value* dbr_p = ptr_at(c, p[4], i2, 4); | |
| 268 | 1 | Value* dbi_p = ptr_at(c, p[4], i2p1, 4); | |
| 269 | 2 | c.kb.store_f32(dar_p, c.b.build_add(c.kb.load_f32(dar_p), | |
| 270 | 1 | c.b.build_add(c.b.build_mul(gr, br), c.b.build_mul(gi, bi)))); | |
| 271 | 2 | c.kb.store_f32(dai_p, c.b.build_add(c.kb.load_f32(dai_p), | |
| 272 | 1 | c.b.build_sub(c.b.build_mul(gi, br), c.b.build_mul(gr, bi)))); | |
| 273 | 2 | c.kb.store_f32(dbr_p, c.b.build_add(c.kb.load_f32(dbr_p), | |
| 274 | 1 | c.b.build_add(c.b.build_mul(gr, ar), c.b.build_mul(gi, ai)))); | |
| 275 | 2 | c.kb.store_f32(dbi_p, c.b.build_add(c.kb.load_f32(dbi_p), | |
| 276 | 1 | c.b.build_sub(c.b.build_mul(gi, ar), c.b.build_mul(gr, ai)))); | |
| 277 | 5 | } else if (op == SP_CX_ABS || op == SP_CX_ABS_BWD) { | |
| 278 | 2 | Value* re = c.kb.load_f32(ptr_at(c, p[0], i2, 4)); | |
| 279 | 2 | Value* im = c.kb.load_f32(ptr_at(c, p[0], i2p1, 4)); | |
| 280 | 2 | Value* mag = c.b.build_sqrt_f32(c.b.build_add(c.b.build_mul(re, re), c.b.build_mul(im, im))); | |
| 281 | 2 | if (op == SP_CX_ABS) { | |
| 282 | 1 | c.kb.store_f32(ptr_at(c, p[3], i, 4), mag); | |
| 283 | 1 | } else { | |
| 284 | 1 | Value* pos = c.b.build_sgt(mag, c.f32(0.0f)); | |
| 285 | 1 | Value* inv = emit_if_else_values( | |
| 286 | 1 | c, pos, {Type::f32()}, | |
| 287 | 2 | [&]() -> std::vector<Value*> { return {emit_div_rn(c, c.kb.load_f32(ptr_at(c, p[1], i, 4)), mag)}; }, | |
| 288 | 2 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 289 | 1 | c.kb.store_f32(ptr_at(c, p[3], i2, 4), c.b.build_select(pos, c.b.build_mul(re, inv), c.f32(0.0f))); | |
| 290 | 1 | c.kb.store_f32(ptr_at(c, p[3], i2p1, 4), c.b.build_select(pos, c.b.build_mul(im, inv), c.f32(0.0f))); | |
| 291 | } | ||
| 292 | 4 | } else if (op == SP_CX_ANGLE) { | |
| 293 | 1 | Value* re = c.kb.load_f32(ptr_at(c, p[0], i2, 4)); | |
| 294 | 1 | Value* im = c.kb.load_f32(ptr_at(c, p[0], i2p1, 4)); | |
| 295 | 1 | c.kb.store_f32(ptr_at(c, p[3], i, 4), emit_atan2_acc(c, im, re)); | |
| 296 | 1 | } else { | |
| 297 | 1 | Value* m = c.kb.load_f32(ptr_at(c, p[0], i, 4)); | |
| 298 | 3 | auto [sv, cv] = emit_sincos_acc(c, c.kb.load_f32(ptr_at(c, p[1], i, 4))); | |
| 299 | 2 | c.kb.store_f32(ptr_at(c, p[3], i2, 4), c.b.build_mul(m, cv)); | |
| 300 | 2 | c.kb.store_f32(ptr_at(c, p[3], i2p1, 4), c.b.build_mul(m, sv)); | |
| 301 | } | ||
| 302 | 6 | }); | |
| 303 | 6 | } | |
| 304 | |||
| 305 | // Bit reversal of the low `bits` bits of i. | ||
| 306 | 8 | Value* bitrev(Ctx& c, Value* i, int bits) { | |
| 307 | 8 | Value* v = i; | |
| 308 | 8 | const std::uint32_t masks[5] = {0x55555555u, 0x33333333u, 0x0f0f0f0fu, 0x00ff00ffu, 0x0000ffffu}; | |
| 309 | 48 | for (int s = 0; s < 5; ++s) { | |
| 310 | 40 | const int sh = 1 << s; | |
| 311 | 40 | Value* m = c.i32(static_cast<std::int32_t>(masks[s])); | |
| 312 | 80 | v = c.b.build_or(c.b.build_and(c.b.build_lshr(v, c.i32(sh)), m), | |
| 313 | 40 | c.b.build_shl(c.b.build_and(v, m), c.i32(sh))); | |
| 314 | 40 | } | |
| 315 | 8 | return bits == 0 ? c.i32(0) : c.b.build_lshr(v, c.i32(32 - bits)); | |
| 316 | } | ||
| 317 | |||
| 318 | // One block per frame: a 2^logn point radix-2 complex FFT in shared memory. | ||
| 319 | // STFT (!inverse): frame (batch, f) of the padded signal, windowed over | ||
| 320 | // [pad_lo, pad_lo + win), forward transformed; bins written interleaved. | ||
| 321 | // p: base(0) win(1) spec(2) | hop(3) win_len(4) pad_lo(5) bins(6) frames(7) padded_len(8) rows(9) | norm(10) | ||
| 322 | // iSTFT (inverse): spec row's Hermitian-mirrored spectrum inverse | ||
| 323 | // transformed; F[row][j] = Re x[pad_lo + j] * scale * win[j]. | ||
| 324 | // p: spec(0) win(1) F(2) | win_len(3) pad_lo(4) bins(5) rows(6) | scale(7) | ||
| 325 | // Row = ctaid.x + kFrameFftGridX ctaid.y; a block past rows exits whole. | ||
| 326 | // The twiddles (cos, sin)(2 pi j / n), j < n/2, are a shared table; stage | ||
| 327 | // len reads index j n/len. | ||
| 328 | 8 | void build_frame_fft_body(Ctx& c, const std::vector<Value*>& p, int logn, bool inverse, Value* row, | |
| 329 | Value* s, Value* tw) { | ||
| 330 | 8 | const int n = 1 << logn; | |
| 331 | 8 | const int half_n = n / 2; | |
| 332 | 8 | auto& kb = c.kb; | |
| 333 | 8 | Value* tid = kb.tid_x(); | |
| 334 | 8 | Value* blk = c.i32(static_cast<int>(kSpectralBlock)); | |
| 335 | 8 | Value* nn = c.i32(n); | |
| 336 | 8 | Value* win = p[1]; | |
| 337 | |||
| 338 | 16 | kb.for_range(tid, c.i32(half_n), blk, [&](Value* j) { | |
| 339 | 24 | auto [cv, sv] = emit_twiddle(c, j, nn); | |
| 340 | 8 | Value* j2 = c.b.build_shl(j, c.i32(1)); | |
| 341 | 16 | kb.shared_store_f32_indexed(tw, j2, cv); | |
| 342 | 16 | kb.shared_store_f32_indexed(tw, c.b.build_add(j2, c.i32(1)), sv); | |
| 343 | 8 | }); | |
| 344 | |||
| 345 | 16 | auto store_br = [&](Value* i, Value* re, Value* im) { | |
| 346 | 8 | Value* d = c.b.build_shl(bitrev(c, i, logn), c.i32(1)); | |
| 347 | 8 | kb.shared_store_f32_indexed(s, d, re); | |
| 348 | 8 | kb.shared_store_f32_indexed(s, c.b.build_add(d, c.i32(1)), im); | |
| 349 | 8 | }; | |
| 350 | |||
| 351 | 8 | if (!inverse) { | |
| 352 | 4 | Value* hop = p[3]; | |
| 353 | 4 | Value* win_len = p[4]; | |
| 354 | 4 | Value* pad_lo = p[5]; | |
| 355 | 4 | Value* frames = p[7]; | |
| 356 | 4 | Value* batch = c.b.build_udiv(row, frames); | |
| 357 | 4 | Value* f = c.b.build_sub(row, c.b.build_mul(batch, frames)); | |
| 358 | 8 | Value* src = ptr_at(c, p[0], | |
| 359 | 8 | c.b.build_add(c.b.build_mul(c.b.build_zext_i64(batch), c.b.build_zext_i64(p[8])), | |
| 360 | 4 | c.b.build_zext_i64(c.b.build_mul(f, hop))), | |
| 361 | 4); | ||
| 362 | 8 | kb.for_range(tid, nn, blk, [&](Value* i) { | |
| 363 | 4 | Value* j = c.b.build_sub(i, pad_lo); | |
| 364 | 4 | Value* ok = c.b.build_ult(j, win_len); | |
| 365 | 4 | Value* sj = c.b.build_select(ok, j, c.i32(0)); | |
| 366 | 4 | Value* si = c.b.build_select(ok, i, c.i32(0)); | |
| 367 | 4 | Value* v = c.b.build_mul(kb.load_f32(ptr_at(c, src, si, 4)), kb.load_f32(ptr_at(c, win, sj, 4))); | |
| 368 | 4 | store_br(i, c.b.build_select(ok, v, c.f32(0.0f)), c.f32(0.0f)); | |
| 369 | 4 | }); | |
| 370 | 4 | } else { | |
| 371 | 4 | Value* bins = p[5]; | |
| 372 | 8 | Value* srow = ptr_at(c, p[0], c.b.build_mul(c.b.build_zext_i64(row), | |
| 373 | 4 | c.b.build_zext_i64(c.b.build_shl(bins, c.i32(1)))), 4); | |
| 374 | 8 | kb.for_range(tid, nn, blk, [&](Value* k) { | |
| 375 | 4 | Value* lo = c.b.build_slt(k, bins); | |
| 376 | 4 | Value* kk = c.b.build_select(lo, k, c.b.build_sub(nn, k)); | |
| 377 | 4 | Value* k2 = c.b.build_shl(kk, c.i32(1)); | |
| 378 | 4 | Value* re = kb.load_f32(ptr_at(c, srow, k2, 4)); | |
| 379 | 4 | Value* im = kb.load_f32(ptr_at(c, srow, c.b.build_add(k2, c.i32(1)), 4)); | |
| 380 | 4 | Value* real_bin = c.b.build_or(c.b.build_eq(k, c.i32(0)), c.b.build_eq(k, c.i32(half_n))); | |
| 381 | 4 | im = c.b.build_select(real_bin, c.f32(0.0f), c.b.build_select(lo, im, c.b.build_neg(im))); | |
| 382 | 4 | store_br(k, re, im); | |
| 383 | 4 | }); | |
| 384 | } | ||
| 385 | 8 | kb.sync(); | |
| 386 | |||
| 387 | 8 | const float sign = inverse ? 1.0f : -1.0f; | |
| 388 | 38 | for (int st = 1; st <= logn; ++st) { | |
| 389 | 30 | const int half = 1 << (st - 1); // len / 2 | |
| 390 | 30 | const int tw_shift = logn - st; // log2(n / len) | |
| 391 | 60 | kb.for_range(tid, c.i32(half_n), blk, [&](Value* t) { | |
| 392 | 30 | Value* j = c.b.build_and(t, c.i32(half - 1)); | |
| 393 | 30 | Value* i0 = c.b.build_add(c.b.build_shl(c.b.build_sub(t, j), c.i32(1)), j); | |
| 394 | 30 | Value* i1 = c.b.build_add(i0, c.i32(half)); | |
| 395 | 30 | Value* ti = c.b.build_shl(c.b.build_shl(j, c.i32(tw_shift)), c.i32(1)); | |
| 396 | 30 | Value* cs = kb.shared_load_f32_indexed(tw, ti); | |
| 397 | 30 | Value* sn = c.b.build_mul(kb.shared_load_f32_indexed(tw, c.b.build_add(ti, c.i32(1))), c.f32(sign)); | |
| 398 | 30 | Value* a0 = c.b.build_shl(i0, c.i32(1)); | |
| 399 | 30 | Value* a1 = c.b.build_shl(i1, c.i32(1)); | |
| 400 | 30 | Value* ar = kb.shared_load_f32_indexed(s, a0); | |
| 401 | 30 | Value* ai = kb.shared_load_f32_indexed(s, c.b.build_add(a0, c.i32(1))); | |
| 402 | 30 | Value* br = kb.shared_load_f32_indexed(s, a1); | |
| 403 | 30 | Value* bi = kb.shared_load_f32_indexed(s, c.b.build_add(a1, c.i32(1))); | |
| 404 | 30 | Value* wr = c.b.build_sub(c.b.build_mul(br, cs), c.b.build_mul(bi, sn)); | |
| 405 | 30 | Value* wi = c.b.build_add(c.b.build_mul(br, sn), c.b.build_mul(bi, cs)); | |
| 406 | 30 | kb.shared_store_f32_indexed(s, a0, c.b.build_add(ar, wr)); | |
| 407 | 30 | kb.shared_store_f32_indexed(s, c.b.build_add(a0, c.i32(1)), c.b.build_add(ai, wi)); | |
| 408 | 30 | kb.shared_store_f32_indexed(s, a1, c.b.build_sub(ar, wr)); | |
| 409 | 30 | kb.shared_store_f32_indexed(s, c.b.build_add(a1, c.i32(1)), c.b.build_sub(ai, wi)); | |
| 410 | 30 | }); | |
| 411 | 30 | kb.sync(); | |
| 412 | 30 | } | |
| 413 | |||
| 414 | 8 | if (!inverse) { | |
| 415 | 4 | Value* bins = p[6]; | |
| 416 | 4 | Value* norm = p[10]; | |
| 417 | 8 | Value* dst = ptr_at(c, p[2], c.b.build_mul(c.b.build_zext_i64(row), | |
| 418 | 4 | c.b.build_zext_i64(c.b.build_shl(bins, c.i32(1)))), 4); | |
| 419 | 8 | kb.for_range(tid, c.b.build_shl(bins, c.i32(1)), blk, [&](Value* e) { | |
| 420 | 4 | kb.store_f32(ptr_at(c, dst, e, 4), c.b.build_mul(kb.shared_load_f32_indexed(s, e), norm)); | |
| 421 | 4 | }); | |
| 422 | 4 | } else { | |
| 423 | 4 | Value* win_len = p[3]; | |
| 424 | 4 | Value* pad_lo = p[4]; | |
| 425 | 4 | Value* scale = p[7]; | |
| 426 | 4 | Value* dst = ptr_at(c, p[2], c.b.build_mul(c.b.build_zext_i64(row), c.b.build_zext_i64(win_len)), 4); | |
| 427 | 8 | kb.for_range(tid, win_len, blk, [&](Value* j) { | |
| 428 | 4 | Value* re = kb.shared_load_f32_indexed(s, c.b.build_shl(c.b.build_add(pad_lo, j), c.i32(1))); | |
| 429 | 8 | kb.store_f32(ptr_at(c, dst, j, 4), | |
| 430 | 4 | c.b.build_mul(c.b.build_mul(re, scale), kb.load_f32(ptr_at(c, win, j, 4)))); | |
| 431 | 4 | }); | |
| 432 | } | ||
| 433 | 8 | } | |
| 434 | |||
| 435 | 8 | void build_frame_fft(Ctx& c, const std::vector<Value*>& p, int logn, bool inverse) { | |
| 436 | 8 | auto& kb = c.kb; | |
| 437 | 8 | const int n = 1 << logn; | |
| 438 | 8 | Value* s = kb.shared_alloc_f32(static_cast<std::uint32_t>(2 * n)); | |
| 439 | 8 | Value* tw = kb.shared_alloc_f32(static_cast<std::uint32_t>(n)); | |
| 440 | 8 | Value* row = c.b.build_add(kb.ctaid_x(), c.b.build_mul(kb.ctaid_y(), c.i32(static_cast<int>(kFrameFftGridX)))); | |
| 441 | 16 | kb.if_then(c.b.build_slt(row, p[inverse ? 6 : 9]), | |
| 442 | 16 | [&] { build_frame_fft_body(c, p, logn, inverse, row, s, tw); }); | |
| 443 | 8 | } | |
| 444 | |||
| 445 | } // namespace | ||
| 446 | |||
| 447 | 46 | brass::Function* build_spectral_mir(brass::Module& mod, std::uint32_t op, std::uint32_t aux0, bool aux1, bool aux2) { | |
| 448 | 46 | std::vector<Type> params; | |
| 449 | 46 | switch (op) { | |
| 450 | case SP_BASIS: | ||
| 451 | 50 | params = {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), | |
| 452 | 40 | Type::i32(), Type::i32(), Type::i32(), Type::f32()}; | |
| 453 | 10 | break; | |
| 454 | case SP_OLA: | ||
| 455 | case SP_ISTFT_ADJ: | ||
| 456 | ✗ | params = {Type::ptr(), Type::ptr(), Type::ptr(), | |
| 457 | ✗ | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}; | |
| 458 | 3 | break; | |
| 459 | case SP_STFT_ADJ: | ||
| 460 | 27 | params = {Type::ptr(), Type::ptr(), | |
| 461 | 24 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}; | |
| 462 | 3 | break; | |
| 463 | case SP_CX_MUL: | ||
| 464 | case SP_CX_MUL_BWD: | ||
| 465 | case SP_CX_ABS: | ||
| 466 | case SP_CX_ABS_BWD: | ||
| 467 | case SP_CX_ANGLE: | ||
| 468 | case SP_CX_POLAR: | ||
| 469 | 25 | params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32()}; | |
| 470 | 6 | break; | |
| 471 | case SP_STFT_FFT: | ||
| 472 | 24 | params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), | |
| 473 | 20 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::f32()}; | |
| 474 | 4 | break; | |
| 475 | case SP_ISTFT_FFT: | ||
| 476 | 8 | params = {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 477 | 4 | Type::f32()}; | |
| 478 | 4 | break; | |
| 479 | default: | ||
| 480 | ✗ | return nullptr; | |
| 481 | } | ||
| 482 | 30 | if ((op == SP_STFT_FFT || op == SP_ISTFT_FFT) && (aux0 < 1u || aux0 > kFrameFftMaxLog)) return nullptr; | |
| 483 | |||
| 484 | 74 | std::string name = "mir_spectral_" + std::to_string(op) + "_" + std::to_string(aux0) + "_" + | |
| 485 | 60 | std::to_string(aux1 ? 1 : 0) + "_" + std::to_string(aux2 ? 1 : 0); | |
| 486 | 30 | Function* fn = mod.create_function(name, Type::void_type(), params); | |
| 487 | 30 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 488 | 30 | Ctx c(kb); | |
| 489 | 30 | auto p = entry_params(c, fn); | |
| 490 | |||
| 491 | 30 | switch (op) { | |
| 492 | 10 | case SP_BASIS: build_basis(c, p, aux0, aux1, aux2); break; | |
| 493 | case SP_OLA: | ||
| 494 | 11 | case SP_ISTFT_ADJ: build_ola_or_istft_adj(c, p, op == SP_OLA); break; | |
| 495 | 3 | case SP_STFT_ADJ: build_stft_adj(c, p, aux0 != 0u); break; | |
| 496 | case SP_STFT_FFT: | ||
| 497 | ✗ | case SP_ISTFT_FFT: build_frame_fft(c, p, static_cast<int>(aux0), op == SP_ISTFT_FFT); break; | |
| 498 | 6 | default: build_complex(c, p, op); break; | |
| 499 | } | ||
| 500 | |||
| 501 | 30 | c.b.build_ret_void(); | |
| 502 | 30 | return finalize_fn(fn); | |
| 503 | 84 | } | |
| 504 | |||
| 505 | } // namespace brotensor::kernels | ||
| 506 |