brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
166 / 174
95.4%
Branches:
85 / 164
51.8%
Functions:
18 / 18
100.0%

src/kernels/mir_dft64.cpp
Line Branch Exec Source
1 #include "mir_dft64.h"
2
3 #include "mir_common.h"
4 #include "../vulkan/shaders/op_codes.h"
5
6 #include <string>
7 #include <utility>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 constexpr std::uint32_t kMaxL = 2048;
15 constexpr std::uint32_t kMaxIn = 2050;
16
17 51 Value* f64(Ctx& c, double v) { return c.b.build_fconst_f64(v); }
18
19 6 Value* shared_alloc_f64(Ctx& c, std::uint32_t count) {
20 6 return c.b.build_call("ptx_shared_alloc_f64", Type::ptr(), {c.i32(static_cast<int>(count))});
21 }
22
23 7 Value* shared_load_f64_idx(Ctx& c, Value* smem, Value* idx) {
24 7 return c.b.build_call("ptx_shared_load_f64_indexed", Type::f64(), {smem, idx});
25 }
26
27 6 void shared_store_f64_idx(Ctx& c, Value* smem, Value* idx, Value* val) {
28 6 c.b.build_call("ptx_shared_store_f64_indexed", Type::void_type(), {smem, idx, val});
29 6 }
30
31 4 Value* clamp_tw(Ctx& c, Value* idx) { return c.b.build_and(idx, c.i32(static_cast<int>(kMaxL - 1u))); }
32
33 5 Value* clamp_in(Ctx& c, Value* idx) {
34 5 return c.b.build_select(c.b.build_ult(idx, c.i32(static_cast<int>(kMaxIn))), idx, c.i32(0));
35 }
36
37 // sin / cos of x in [0, pi / 4] to double rounding: Taylor through x^21 / x^20.
38 2 std::pair<Value*, Value*> sincos64(Ctx& c, Value* x) {
39 2 Value* z = c.b.build_mul(x, x);
40 2 Value* s = f64(c, 1.0 / 51090942171709440000.0);
41 2 s = c.b.build_fma_f64(s, z, f64(c, -1.0 / 121645100408832000.0));
42 2 s = c.b.build_fma_f64(s, z, f64(c, 1.0 / 355687428096000.0));
43 2 s = c.b.build_fma_f64(s, z, f64(c, -1.0 / 1307674368000.0));
44 2 s = c.b.build_fma_f64(s, z, f64(c, 1.0 / 6227020800.0));
45 2 s = c.b.build_fma_f64(s, z, f64(c, -1.0 / 39916800.0));
46 2 s = c.b.build_fma_f64(s, z, f64(c, 1.0 / 362880.0));
47 2 s = c.b.build_fma_f64(s, z, f64(c, -1.0 / 5040.0));
48 2 s = c.b.build_fma_f64(s, z, f64(c, 1.0 / 120.0));
49 2 s = c.b.build_fma_f64(s, z, f64(c, -1.0 / 6.0));
50 2 s = c.b.build_fma_f64(c.b.build_mul(x, z), s, x);
51
52 2 Value* cs = f64(c, 1.0 / 2432902008176640000.0);
53 2 cs = c.b.build_fma_f64(cs, z, f64(c, -1.0 / 6402373705728000.0));
54 2 cs = c.b.build_fma_f64(cs, z, f64(c, 1.0 / 20922789888000.0));
55 2 cs = c.b.build_fma_f64(cs, z, f64(c, -1.0 / 87178291200.0));
56 2 cs = c.b.build_fma_f64(cs, z, f64(c, 1.0 / 479001600.0));
57 2 cs = c.b.build_fma_f64(cs, z, f64(c, -1.0 / 3628800.0));
58 2 cs = c.b.build_fma_f64(cs, z, f64(c, 1.0 / 40320.0));
59 2 cs = c.b.build_fma_f64(cs, z, f64(c, -1.0 / 720.0));
60 2 cs = c.b.build_fma_f64(cs, z, f64(c, 1.0 / 24.0));
61 2 cs = c.b.build_fma_f64(cs, z, f64(c, -0.5));
62 2 cs = c.b.build_fma_f64(z, cs, f64(c, 1.0));
63 2 return {s, cs};
64 }
65
66 // (cos, sin) of 2 * pi * m / L.
67 2 std::pair<Value*, Value*> twiddle64(Ctx& c, Value* m, Value* L) {
68 2 Value* m4 = c.b.build_shl(m, c.i32(2));
69 2 Value* q = c.b.build_udiv(m4, L);
70 2 Value* r0 = c.b.build_sub(m4, c.b.build_mul(q, L));
71 2 Value* swp = c.b.build_ugt(c.b.build_shl(r0, c.i32(1)), L);
72 2 Value* r = c.b.build_select(swp, c.b.build_sub(L, r0), r0);
73 2 Value* rd = c.b.build_sitofp_f64_i32(r);
74 2 Value* ld = c.b.build_sitofp_f64_i32(L);
75 2 Value* x = c.b.build_mul(rd, c.b.build_sdiv(f64(c, 1.5707963267948966192), ld));
76 10 auto [sx, cx] = sincos64(c, x);
77 6 Value* cq = c.b.build_select(swp, sx, cx);
78 6 Value* sq = c.b.build_select(swp, cx, sx);
79 2 Value* n_cq = c.b.build_neg(cq);
80 2 Value* n_sq = c.b.build_neg(sq);
81 2 Value* q0 = c.b.build_eq(q, c.i32(0));
82 2 Value* q1 = c.b.build_eq(q, c.i32(1));
83 2 Value* q2 = c.b.build_eq(q, c.i32(2));
84 2 Value* re = c.b.build_select(q0, cq, c.b.build_select(q1, n_sq, c.b.build_select(q2, n_cq, sq)));
85 2 Value* im = c.b.build_select(q0, sq, c.b.build_select(q1, cq, c.b.build_select(q2, n_sq, n_cq)));
86 2 return {re, im};
87 }
88
89 2 Value* hermitian_wk(Ctx& c, Value* k, Value* L) {
90 2 Value* is_dc = c.b.build_eq(k, c.i32(0));
91 2 Value* even_L = c.b.build_eq(c.b.build_and(L, c.i32(1)), c.i32(0));
92 2 Value* is_nyq = c.b.build_and(even_L, c.b.build_eq(c.b.build_shl(k, c.i32(1)), L));
93 2 return c.b.build_select(c.b.build_or(is_dc, is_nyq), f64(c, 1.0), f64(c, 2.0));
94 }
95
96 } // namespace
97
98 2 brass::Function* build_dft64_mir(brass::Module& mod, std::uint32_t mode,
99 bool has_win, bool has_weights) {
100
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.
4 const std::string name = std::string("mir_dft64_m") + std::to_string(mode) +
101
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 (has_win ? "_w" : "") + (has_weights ? "_h" : "");
102
15/30
✓ 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.
✓ Branch 24 taken 2 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 2 times.
✗ Branch 27 not taken.
✓ Branch 28 taken 2 times.
✗ Branch 29 not taken.
30 std::vector<Type> params = {
103 6 Type::ptr(), Type::ptr(), Type::ptr(),
104 12 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
105 10 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
106 2 Type::f32(),
107 };
108
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params));
109
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 brass::codegen::KernelBuilder kb(mod, fn);
110
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
111
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
112 // ptrs: 0:x 1:y 2:win
113 // ints: 3:sa 4:sc 5:R 6:batch 7:cols 8:lda 9:rows 10:ldc 11:L 12:tofs 13:sgn
114 // flts: 14:scale
115
116
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s_tw_re = shared_alloc_f64(c, kMaxL);
117
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s_tw_im = shared_alloc_f64(c, kMaxL);
118
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* s_in = shared_alloc_f64(c, kMaxIn);
119
120
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* tid = kb.tid_x();
121
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* wg = c.i32(static_cast<int>(jit::gpu_mir::kEwBlock));
122 2 Value* L = p[11];
123
124
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
4 kb.for_range(tid, L, wg, [&](Value* m) {
125 6 auto [re, im] = twiddle64(c, m, L);
126 2 Value* cm = clamp_tw(c, m);
127 4 shared_store_f64_idx(c, s_tw_re, cm, re);
128 4 shared_store_f64_idx(c, s_tw_im, cm, im);
129 2 });
130
131
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* sg = c.b.build_sitofp_f64_i32(p[13]);
132
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* scale = c.b.build_fpext_f64_f32(p[14]);
133
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* total = c.b.build_mul(p[5], p[6]);
134
135
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 kb.for_range(kb.ctaid_x(), total, kb.nctaid_x(), [&](Value* row) {
136 2 Value* z = c.b.build_udiv(row, p[5]);
137 2 Value* r = c.b.build_umod(row, p[5]);
138 2 Value* xo = c.b.build_add(c.b.build_mul(z, p[3]), c.b.build_mul(r, p[8]));
139 2 Value* yo = c.b.build_add(c.b.build_mul(z, p[4]), c.b.build_mul(r, p[10]));
140 2 kb.sync();
141
142
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 kb.for_range(tid, p[7], wg, [&](Value* i) {
143 2 Value* v = c.b.build_fpext_f64_f32(kb.load_f32(ptr_at(c, p[0], c.b.build_add(xo, i), 4)));
144
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
2 if (mode == BASIS_R2C && has_win) {
145 1 Value* wv = c.b.build_fpext_f64_f32(kb.load_f32(ptr_at(c, p[2], i, 4)));
146 1 v = c.b.build_mul(v, wv);
147 1 }
148
3/4
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 time.
2 if (mode == BASIS_C2R && has_weights) {
149 1 v = c.b.build_mul(v, hermitian_wk(c, c.b.build_lshr(i, c.i32(1)), L));
150 1 }
151 2 shared_store_f64_idx(c, s_in, clamp_in(c, i), v);
152 2 });
153 2 kb.sync();
154
155
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (mode == BASIS_C2R) {
156 1 Value* nb = c.b.build_lshr(p[7], c.i32(1));
157
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.for_range(tid, p[9], wg, [&](Value* o) {
158 1 Value* n = c.b.build_umod(c.b.build_add(o, p[12]), L);
159
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto st = kb.for_range_reduce_n(
160 1 c.i32(0), nb, c.i32(1), {f64(c, 0.0), c.i32(0)},
161
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* k, const std::vector<Value*>& cur) -> std::vector<Value*> {
162 1 Value* cm = clamp_tw(c, cur[1]);
163 1 Value* tr = shared_load_f64_idx(c, s_tw_re, cm);
164 1 Value* ti = shared_load_f64_idx(c, s_tw_im, cm);
165 1 Value* k2 = c.b.build_shl(k, c.i32(1));
166 1 Value* sr = shared_load_f64_idx(c, s_in, clamp_in(c, k2));
167 1 Value* si = shared_load_f64_idx(c, s_in, clamp_in(c, c.b.build_add(k2, c.i32(1))));
168 1 Value* acc = c.b.build_fma_f64(sr, tr, cur[0]);
169 1 acc = c.b.build_sub(acc, c.b.build_mul(sg, c.b.build_mul(si, ti)));
170 1 Value* nm = c.b.build_add(cur[1], n);
171 1 nm = c.b.build_select(c.b.build_uge(nm, L), c.b.build_sub(nm, L), nm);
172 1 return {acc, nm};
173 });
174
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* out = c.b.build_mul(st[0], scale);
175
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (has_win) {
176
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.
1 out = c.b.build_mul(out, c.b.build_fpext_f64_f32(kb.load_f32(ptr_at(c, p[2], o, 4))));
177 1 }
178
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.
1 kb.store_f32(ptr_at(c, p[1], c.b.build_add(yo, o), 4), c.b.build_fptrunc_f32_f64(out));
179 1 });
180 1 } else {
181 1 const bool cx = (mode == BASIS_C2C);
182
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* nin = cx ? c.b.build_lshr(p[7], c.i32(1)) : p[7];
183 1 Value* nk = c.b.build_lshr(c.b.build_add(p[9], c.i32(1)), c.i32(1));
184
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* tofs_m = cx ? c.i32(0) : c.b.build_umod(p[12], L);
185
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 kb.for_range(tid, nk, wg, [&](Value* k) {
186 1 Value* step = c.b.build_umod(k, L);
187
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 Value* m0 = cx ? c.i32(0) : c.b.build_umod(c.b.build_mul(step, tofs_m), L);
188
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto st = kb.for_range_reduce_n(
189 1 c.i32(0), nin, c.i32(1), {f64(c, 0.0), f64(c, 0.0), m0},
190
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* n, const std::vector<Value*>& cur) -> std::vector<Value*> {
191 1 Value* cm = clamp_tw(c, cur[2]);
192 1 Value* tr = shared_load_f64_idx(c, s_tw_re, cm);
193 1 Value* ti = shared_load_f64_idx(c, s_tw_im, cm);
194 1 Value* nm = c.b.build_add(cur[2], step);
195 1 nm = c.b.build_select(c.b.build_uge(nm, L), c.b.build_sub(nm, L), nm);
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
1 if (cx) {
197 ✗ Value* n2 = c.b.build_shl(n, c.i32(1));
198 ✗ Value* xr = shared_load_f64_idx(c, s_in, clamp_in(c, n2));
199 ✗ Value* xi = shared_load_f64_idx(c, s_in, clamp_in(c, c.b.build_add(n2, c.i32(1))));
200 ✗ Value* re = c.b.build_sub(c.b.build_fma_f64(xr, tr, cur[0]),
201 ✗ c.b.build_mul(sg, c.b.build_mul(xi, ti)));
202 ✗ Value* im = c.b.build_fma_f64(xi, tr,
203 ✗ c.b.build_fma_f64(sg, c.b.build_mul(xr, ti), cur[1]));
204 ✗ return {re, im, nm};
205 }
206 1 Value* xv = shared_load_f64_idx(c, s_in, clamp_in(c, n));
207 1 return {c.b.build_fma_f64(xv, tr, cur[0]), c.b.build_fma_f64(xv, ti, cur[1]), nm};
208 1 });
209 1 Value* re = st[0];
210
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* im = cx ? st[1] : c.b.build_mul(st[1], sg);
211
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* f = has_weights ? c.b.build_mul(scale, hermitian_wk(c, k, L)) : scale;
212
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* k2 = c.b.build_shl(k, c.i32(1));
213
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 kb.store_f32(ptr_at(c, p[1], c.b.build_add(yo, k2), 4),
214
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 c.b.build_fptrunc_f32_f64(c.b.build_mul(re, f)));
215
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* k2p1 = c.b.build_add(k2, c.i32(1));
216
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 kb.if_then(c.b.build_ult(k2p1, p[9]), [&] {
217 2 kb.store_f32(ptr_at(c, p[1], c.b.build_add(yo, k2p1), 4),
218 1 c.b.build_fptrunc_f32_f64(c.b.build_mul(im, f)));
219 1 });
220 1 });
221 }
222 2 });
223
224
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
225
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 return finalize_fn(fn);
226 2 }
227
228 } // namespace brotensor::kernels
229