brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
176 / 177
99.4%
Branches:
111 / 190
58.4%
Functions:
18 / 18
100.0%

src/kernels/mir_diffusion.cpp
Line Branch Exec Source
1 #include "mir_diffusion.h"
2
3 #include "mir_math.h"
4 #include "../vulkan/shaders/op_codes.h"
5
6 #include <functional>
7 #include <string>
8 #include <utility>
9
10 namespace brotensor::kernels {
11
12 9 std::pair<Value*, Value*> philox_words(Ctx& c, Value* k0, Value* k1, Value* sub64) {
13 9 Value* c0 = c.b.build_trunc_i32(sub64);
14 9 Value* c1 = c.b.build_trunc_i32(c.b.build_lshr(sub64, c.kb.const_i64(32)));
15 9 Value* c2 = c.i32(0);
16 9 Value* c3 = c.i32(0);
17 9 Value* m0 = c.i32(static_cast<std::int32_t>(0xD2511F53u));
18 9 Value* m1 = c.i32(static_cast<std::int32_t>(0xCD9E8D57u));
19 9 Value* w0 = c.i32(static_cast<std::int32_t>(0x9E3779B9u));
20 9 Value* w1 = c.i32(static_cast<std::int32_t>(0xBB67AE85u));
21
2/2
✓ Branch 0 taken 90 times.
✓ Branch 1 taken 9 times.
99 for (int r = 0; r < 10; ++r) {
22 90 Value* lo0 = c.b.build_mul(m0, c0);
23 90 Value* hi0 = c.kb.mul_hi_u32(m0, c0);
24 90 Value* lo1 = c.b.build_mul(m1, c2);
25 90 Value* hi1 = c.kb.mul_hi_u32(m1, c2);
26 90 Value* nc0 = c.b.build_xor(c.b.build_xor(hi1, c1), k0);
27 90 Value* nc2 = c.b.build_xor(c.b.build_xor(hi0, c3), k1);
28 90 c0 = nc0;
29 90 c1 = lo1;
30 90 c2 = nc2;
31 90 c3 = lo0;
32
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 81 times.
90 if (r < 9) {
33 81 k0 = c.b.build_add(k0, w0);
34 81 k1 = c.b.build_add(k1, w1);
35 81 }
36 90 }
37 9 return {c0, c1};
38 }
39
40 namespace {
41
42 10 Value* u32_to_u01(Ctx& c, Value* w) {
43 10 Value* top24 = c.kb.u32_to_f32(c.b.build_lshr(w, c.i32(8)));
44 10 return c.b.build_mul(top24, c.f32(1.0f / 16777216.0f));
45 }
46
47 } // namespace
48
49 4 Value* philox_normal(Ctx& c, Value* k0, Value* k1, Value* sub64) {
50 12 auto [c0, c1] = philox_words(c, k0, k1, sub64);
51 8 Value* u1 = c.b.build_sub(c.f32(1.0f), u32_to_u01(c, c0));
52 8 Value* u2 = u32_to_u01(c, c1);
53 4 Value* radius = emit_sqrt(c, c.b.build_mul(c.f32(-2.0f), emit_log_acc(c, u1)));
54 4 Value* h = c.f32(6.28318548202514648f);
55 4 Value* l = c.f32(-1.7484555314695172e-07f);
56 4 Value* ph = c.b.build_mul(u2, h);
57 4 Value* err = c.b.build_fma_f32(u2, h, c.b.build_neg(ph));
58 4 Value* theta = c.b.build_add(ph, c.b.build_add(err, c.b.build_mul(u2, l)));
59 8 auto [s, co] = emit_sincos_acc(c, theta);
60 (void)s;
61 8 return c.b.build_mul(radius, co);
62 }
63
64 namespace {
65
66 12 void emit_ew_sampler(Ctx& c, Value* n, int lanes, const std::function<void(Value* base, int k)>& emit_k) {
67 12 Value* gid = c.kb.global_tid_x();
68 12 Value* stride = grid_stride_ew(c);
69
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9 times.
12 if (lanes > 1) {
70 9 Value* vec_end = c.b.build_and(n, c.i32(-lanes));
71
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 c.kb.for_range(c.b.build_mul(gid, c.i32(lanes)), vec_end, c.b.build_mul(stride, c.i32(lanes)),
72 18 [&](Value* base) { emit_k(base, lanes); });
73
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 c.kb.for_range(c.b.build_add(vec_end, gid), n, stride, [&](Value* idx) { emit_k(idx, 1); });
74 9 } else {
75
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.for_range(gid, n, stride, [&](Value* idx) { emit_k(idx, 1); });
76 }
77 12 }
78
79 } // namespace
80
81 15 brass::Function* build_sampler_mir(brass::Module& mod, std::uint32_t mode, Dtype dt, Dtype dti, int lanes) {
82
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 const std::string name = "mir_sampler_" + std::to_string(mode);
83
2/4
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
30 auto* fn = mod.create_function(
84 15 name, Type::void_type(),
85 105 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(),
86 90 Type::i32(), Type::i32(), Type::f32(), Type::f32(), Type::f32(), Type::f32()});
87
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 brass::codegen::KernelBuilder kb(mod, fn);
88
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 Ctx c(kb);
89
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 auto p = entry_params(c, fn);
90 15 Value *x = p[0], *e = p[1], *pbuf = p[2], *y = p[3], *z = p[4];
91 15 Value *n = p[5], *dim = p[6], *half = p[7], *hw = p[8], *c_dim = p[9];
92 15 Value *k0 = p[10], *k1 = p[11], *k2 = p[12], *k3 = p[13];
93
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 const int es = elem_bytes(dt);
94
95
6/6
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
15 if (mode == SMP_DDIM || mode == SMP_EULER || mode == SMP_DPMPP_2M) {
96
2/4
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
33 emit_ew_sampler(c, n, lanes, [&](Value* base, int k) {
97 21 auto xv = load_lanes(c, dt, k, ptr_at(c, x, base, es));
98
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 auto ev = load_lanes(c, dt, k, ptr_at(c, e, base, es));
99
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 std::vector<Value*> yv(static_cast<std::size_t>(k));
100
1/2
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
21 std::vector<Value*> zv(static_cast<std::size_t>(k));
101
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 9 times.
21 if (mode == SMP_DDIM) {
102
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 9 times.
35 for (int i = 0; i < k; ++i) {
103
3/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
26 Value* x0 = c.b.build_mul(c.b.build_sub(xv[i], c.b.build_mul(k2, ev[i])), k1);
104
3/6
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 26 times.
✗ Branch 5 not taken.
26 yv[i] = c.b.build_add(c.b.build_mul(k0, x0), c.b.build_mul(k3, ev[i]));
105 26 }
106
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
21 } else if (mode == SMP_EULER) {
107
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 23 times.
29 for (int i = 0; i < k; ++i) {
108
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 yv[i] = c.b.build_add(xv[i], c.b.build_mul(k0, ev[i]));
109 23 }
110 6 } else {
111
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 auto pv = load_lanes(c, dt, k, ptr_at(c, pbuf, base, es));
112
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 23 times.
29 for (int i = 0; i < k; ++i) {
113
2/4
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
23 Value* x0 = c.b.build_sub(xv[i], c.b.build_mul(k0, ev[i]));
114
4/8
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 23 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 23 times.
✗ Branch 7 not taken.
46 yv[i] = c.b.build_add(c.b.build_add(c.b.build_mul(k1, xv[i]), c.b.build_mul(k2, x0)),
115
1/2
✓ Branch 0 taken 23 times.
✗ Branch 1 not taken.
23 c.b.build_mul(k3, pv[i]));
116 23 zv[i] = x0;
117 23 }
118
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 store_lanes(c, dt, k, ptr_at(c, z, base, es), zv);
119 6 }
120
2/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 21 times.
✗ Branch 3 not taken.
21 store_lanes(c, dt, k, ptr_at(c, y, base, es), yv);
121 21 });
122
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
15 } else if (mode == SMP_TIMESTEP_EMB) {
123
2/4
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
1 Value* two_half = c.b.build_mul(half, c.i32(2));
124
5/10
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 time.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 time.
✗ Branch 9 not taken.
1 Value* safe_half = c.kb.i32_to_f32(c.b.build_select(c.b.build_eq(half, c.i32(0)), c.i32(1), half));
125
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* neg_k0 = c.b.build_neg(k0);
126
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 c.kb.for_range(c.kb.global_tid_x(), n, grid_stride_ew(c), [&](Value* i) {
127 1 Value* r = c.b.build_udiv(i, dim);
128 1 Value* j = c.b.build_umod(i, dim);
129 1 Value* lt_h = c.b.build_ult(j, half);
130 1 Value* k = c.b.build_select(lt_h, j, c.b.build_sub(j, half));
131 1 Value* freq = emit_exp_acc(c, emit_div_rn(c, c.b.build_mul(neg_k0, c.kb.u32_to_f32(k)), safe_half));
132 1 Value* t = load_elem_dt(c, dti, x, r);
133 3 auto [s, co] = emit_sincos_acc(c, c.b.build_mul(t, freq));
134 3 Value* v = c.b.build_select(c.b.build_ult(j, two_half), c.b.build_select(lt_h, co, s), c.f32(0.0f));
135 1 store_elem_dt(c, dt, y, i, v);
136 1 });
137
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
3 } else if (mode == SMP_IMAGE_NORM) {
138
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 c.kb.for_range(c.kb.global_tid_x(), n, grid_stride_ew(c), [&](Value* i) {
139 1 Value* ch = c.b.build_umod(c.b.build_udiv(i, hw), c_dim);
140 1 Value* m = c.kb.load_f32(ptr_at(c, e, ch, 4));
141 1 Value* inv = emit_div_rn(c, c.f32(1.0f), c.kb.load_f32(ptr_at(c, pbuf, ch, 4)));
142 1 Value* xv = load_elem_dt(c, dt, x, i);
143 1 store_elem_dt(c, dt, y, i, c.b.build_mul(c.b.build_sub(xv, m), inv));
144 1 });
145 1 } else {
146
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 Value* hc = c.b.build_mul(hw, c_dim);
147
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 c.kb.for_range(c.kb.global_tid_x(), n, grid_stride_ew(c), [&](Value* i) {
148 1 Value* img = c.b.build_udiv(i, hc);
149 1 Value* rem = c.b.build_umod(i, hc);
150 1 Value* ch = c.b.build_udiv(rem, hw);
151 1 Value* px = c.b.build_umod(rem, hw);
152 1 Value* src_idx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(img, hw), px), c_dim), ch);
153 1 Value* vf = c.kb.u32_to_f32(c.kb.load_u8(c.b.build_add(x, c.b.build_zext_i64(src_idx))));
154 1 Value* res = c.b.build_add(c.b.build_mul(vf, k0), k1);
155 1 store_elem_dt(c, dt, y, i, res);
156 1 });
157 }
158
159
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 c.b.build_ret_void();
160
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
15 return finalize_fn(fn);
161 15 }
162
163 5 brass::Function* build_philox_mir(brass::Module& mod, std::uint32_t mode) {
164
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto* fn = mod.create_function(
165
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 "mir_philox_" + std::to_string(mode), Type::void_type(),
166 30 {Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
167 25 Type::i32(), Type::i32(), Type::f32(), Type::f32(), Type::f32()});
168 5 brass::codegen::KernelBuilder kb(mod, fn);
169
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Ctx c(kb);
170
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto p = entry_params(c, fn);
171 5 Value *y = p[0], *k_lo = p[1], *k_hi = p[2], *c_lo = p[3], *c_hi = p[4];
172 5 Value *n_lo = p[5], *n_hi = p[6], *prob = p[7], *lo = p[8], *hi = p[9];
173
174 15 auto join_u64 = [&](Value* lo32, Value* hi32) -> Value* {
175 10 return c.b.build_or(c.b.build_zext_i64(lo32), c.b.build_shl(c.b.build_zext_i64(hi32), c.kb.const_i64(32)));
176 };
177
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* ctr_base = join_u64(c_lo, c_hi);
178
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* n64 = join_u64(n_lo, n_hi);
179
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* start64 = c.b.build_zext_i64(c.kb.global_tid_x());
180
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* stride64 = c.b.build_zext_i64(grid_stride_ew(c));
181
182
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
10 c.kb.for_range(start64, n64, stride64, [&](Value* idx64) {
183 5 Value* sub = c.b.build_add(ctr_base, idx64);
184 5 Value* v = nullptr;
185
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (mode == RNG_NORMAL) {
186 2 v = philox_normal(c, k_lo, k_hi, sub);
187
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2 times.
5 } else if (mode == RNG_UNIFORM) {
188 1 v = u32_to_u01(c, philox_words(c, k_lo, k_hi, sub).first);
189
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
3 } else if (mode == RNG_BERNOULLI) {
190 1 Value* u = u32_to_u01(c, philox_words(c, k_lo, k_hi, sub).first);
191 1 v = c.b.build_select(c.b.build_slt(u, prob), c.f32(1.0f), c.f32(0.0f));
192 1 } else {
193
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 auto st = c.kb.for_range_reduce_n(
194 1 c.i32(0), c.i32(64), c.i32(1), {c.i32(0), c.f32(0.0f)},
195
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 [&](Value* r_cur, const std::vector<Value*>& acc) -> std::vector<Value*> {
196
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 return emit_if_else_values(
197 1 c, c.b.build_eq(acc[0], c.i32(0)), {Type::i32(), Type::f32()},
198 2 [&]() -> std::vector<Value*> {
199 1 Value* sub_r = c.b.build_add(sub, c.b.build_mul(c.b.build_zext_i64(r_cur), n64));
200 1 Value* draw = philox_normal(c, k_lo, k_hi, sub_r);
201 1 Value* ok = c.b.build_and(c.b.build_sge(draw, lo), c.b.build_sle(draw, hi));
202 1 return {c.b.build_select(ok, c.i32(1), c.i32(0)), draw};
203 },
204 2 [&]() -> std::vector<Value*> { return acc; });
205 ✗ });
206
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 v = c.kb.clamp_f32(st[1], lo, hi);
207 1 }
208 5 Value* byte_off = c.b.build_shl(idx64, c.kb.const_i64(2));
209 5 c.kb.store_f32(c.b.build_add(y, byte_off), v);
210 5 });
211
212
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c.b.build_ret_void();
213
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return finalize_fn(fn);
214 5 }
215
216 } // namespace brotensor::kernels
217