brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
133 / 151
88.1%
Branches:
53 / 92
57.6%
Functions:
22 / 24
91.7%

src/kernels/mir_scatter.cpp
Line Branch Exec Source
1 #include "mir_scatter.h"
2 #include "mir_common.h"
3
4 #include "../vulkan/shaders/op_codes.h"
5
6 #include <cstdint>
7 #include <vector>
8
9 namespace brotensor::kernels {
10
11 namespace {
12
13 constexpr std::int64_t kNoneKey = -1LL; // 0xffffffffffffffff
14
15 11 Value* pack_u32_pair(Ctx& c, Value* hi32, Value* lo32) {
16 11 Value* h64 = c.b.build_shl(c.b.build_zext_i64(hi32), c.kb.const_i64(32));
17 11 return c.b.build_or(h64, c.b.build_zext_i64(lo32));
18 }
19
20 360 Value* pair_gt(Ctx& c, Value* ha, Value* la, Value* hb, Value* lb) {
21 360 Value* hi_gt = c.b.build_ugt(ha, hb);
22 360 Value* hi_eq = c.b.build_eq(ha, hb);
23 360 return c.b.build_or(hi_gt, c.b.build_and(hi_eq, c.b.build_ugt(la, lb)));
24 }
25
26 3 void build_sa_keys(Ctx& c, const std::vector<Value*>& p) {
27 3 Value* m = p[5];
28 3 Value* r = p[6];
29 3 Value* p_cnt = p[8];
30 3 Value* none = c.kb.const_i64(kNoneKey);
31 3 Value* inv_hi = c.i32(static_cast<int>(0xffffffffu));
32
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, p_cnt, 1, kScatterBlock, [&](Value* i) {
33
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto kv = emit_if_else_values(
34 3 c, c.b.build_ult(i, m), {Type::i64()},
35 6 [&]() -> std::vector<Value*> {
36 3 Value* row = c.kb.load_i32(ptr_at(c, p[0], i, 4));
37 3 Value* valid = c.b.build_and(c.b.build_sge(row, c.i32(0)), c.b.build_ult(row, r));
38 3 Value* hi = c.b.build_select(valid, row, inv_hi);
39 3 return {pack_u32_pair(c, hi, i)};
40 },
41 6 [&]() -> std::vector<Value*> { return {none}; });
42
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 c.kb.store_i64(ptr_at(c, p[1], i, 8), kv[0]);
43 3 });
44 3 }
45
46 4 void build_sa_local(Ctx& c, const std::vector<Value*>& p) {
47 4 Value* p_cnt = p[8];
48 4 Value* tid = c.kb.tid_x();
49 4 Value* sk_hi = c.kb.shared_alloc_i32(512);
50 4 Value* sk_lo = c.kb.shared_alloc_i32(512);
51 4 Value* none = c.kb.const_i64(kNoneKey);
52
53
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int s = 0; s < 2; ++s) {
54
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 Value* idx = (s == 0) ? tid : c.b.build_add(tid, c.i32(256));
55
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 auto kv = emit_if_else_values(
56 8 c, c.b.build_ult(idx, p_cnt), {Type::i64()},
57 16 [&]() -> std::vector<Value*> { return {c.kb.load_i64(ptr_at(c, p[1], idx, 8))}; },
58 16 [&]() -> std::vector<Value*> { return {none}; });
59
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 Value* hi = c.b.build_trunc_i32(c.b.build_lshr(kv[0], c.kb.const_i64(32)));
60
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* lo = c.b.build_trunc_i32(kv[0]);
61
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c.kb.shared_store_i32_indexed(sk_hi, idx, hi);
62
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c.kb.shared_store_i32_indexed(sk_lo, idx, lo);
63 8 }
64 4 c.kb.sync();
65
66
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 4 times.
40 for (int k = 2; k <= 512; k <<= 1) {
67
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 180 times.
216 for (int j = k >> 1; j > 0; j >>= 1) {
68
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 360 times.
540 for (int s = 0; s < 2; ++s) {
69
2/2
✓ Branch 0 taken 180 times.
✓ Branch 1 taken 180 times.
360 Value* i = (s == 0) ? tid : c.b.build_add(tid, c.i32(256));
70 360 Value* l = c.b.build_xor(i, c.i32(j));
71
1/2
✓ Branch 0 taken 360 times.
✗ Branch 1 not taken.
720 c.kb.if_then(c.b.build_ugt(l, i), [&] {
72 360 Value* ha = c.kb.shared_load_i32_indexed(sk_hi, i);
73 360 Value* la = c.kb.shared_load_i32_indexed(sk_lo, i);
74 360 Value* hb = c.kb.shared_load_i32_indexed(sk_hi, l);
75 360 Value* lb = c.kb.shared_load_i32_indexed(sk_lo, l);
76 360 Value* up = c.b.build_eq(c.b.build_and(i, c.i32(k)), c.i32(0));
77 360 Value* gt = pair_gt(c, ha, la, hb, lb);
78 360 Value* do_swap = c.b.build_eq(gt, up);
79 360 c.kb.shared_store_i32_indexed(sk_hi, i, c.b.build_select(do_swap, hb, ha));
80 360 c.kb.shared_store_i32_indexed(sk_lo, i, c.b.build_select(do_swap, lb, la));
81 360 c.kb.shared_store_i32_indexed(sk_hi, l, c.b.build_select(do_swap, ha, hb));
82 360 c.kb.shared_store_i32_indexed(sk_lo, l, c.b.build_select(do_swap, la, lb));
83 360 });
84 360 }
85 180 c.kb.sync();
86 180 }
87 36 }
88
89
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 4 times.
12 for (int s = 0; s < 2; ++s) {
90
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 Value* idx = (s == 0) ? tid : c.b.build_add(tid, c.i32(256));
91
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 c.kb.if_then(c.b.build_ult(idx, p_cnt), [&] {
92 8 Value* hi = c.kb.shared_load_i32_indexed(sk_hi, idx);
93 8 Value* lo = c.kb.shared_load_i32_indexed(sk_lo, idx);
94 8 c.kb.store_i64(ptr_at(c, p[1], idx, 8), pack_u32_pair(c, hi, lo));
95 8 });
96 8 }
97 4 }
98
99 ✗ void build_sa_step(Ctx& c, const std::vector<Value*>& p) {
100 ✗ Value* p_cnt = p[8];
101 ✗ Value* k_v = p[9];
102 ✗ Value* j_v = p[10];
103
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ emit_vec_grid_stride(c, p_cnt, 1, kScatterBlock, [&](Value* i) {
104 ✗ Value* l = c.b.build_xor(i, j_v);
105 ✗ Value* a = c.kb.load_i64(ptr_at(c, p[1], i, 8));
106 ✗ Value* b_val = c.kb.load_i64(ptr_at(c, p[1], l, 8));
107 ✗ Value* up = c.b.build_eq(c.b.build_and(i, k_v), c.i32(0));
108 ✗ Value* take_min = c.b.build_eq(c.b.build_ult(i, l), up);
109 ✗ Value* lo = c.b.build_select(c.b.build_ult(a, b_val), a, b_val);
110 ✗ Value* hi = c.b.build_select(c.b.build_ugt(a, b_val), a, b_val);
111 ✗ c.kb.store_i64(ptr_at(c, p[2], i, 8), c.b.build_select(take_min, lo, hi));
112 ✗ });
113 ✗ }
114
115 9 void build_sa_sum(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
116 9 const int esz = elem_bytes(dt);
117 9 Value* m = p[5];
118 9 Value* r = p[6];
119 9 Value* cols = p[7];
120 18 Value* col = c.b.build_add(c.b.build_mul(c.kb.ctaid_x(), c.i32(static_cast<int>(kScatterBlock))),
121 9 c.kb.tid_x());
122 9 Value* col_ok = c.b.build_ult(col, cols);
123
124
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 c.kb.for_range(c.kb.ctaid_y(), m, c.kb.nctaid_y(), [&](Value* pos) {
125 9 Value* key = c.kb.load_i64(ptr_at(c, p[1], pos, 8));
126 9 Value* row = c.b.build_trunc_i32(c.b.build_lshr(key, c.kb.const_i64(32)));
127 9 Value* in_r = c.b.build_ult(row, r);
128
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto first_res = emit_if_else_values(
129 9 c, c.b.build_ugt(pos, c.i32(0)), {Type::i32()},
130 18 [&]() -> std::vector<Value*> {
131 9 Value* pk = c.kb.load_i64(ptr_at(c, p[1], c.b.build_sub(pos, c.i32(1)), 8));
132 9 Value* pr = c.b.build_trunc_i32(c.b.build_lshr(pk, c.kb.const_i64(32)));
133 9 return {c.b.build_select(c.b.build_ne(pr, row), c.i32(1), c.i32(0))};
134 },
135 18 [&]() -> std::vector<Value*> { return {c.i32(1)}; });
136
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 Value* active = c.b.build_and(c.b.build_and(in_r, col_ok), c.b.build_ne(first_res[0], c.i32(0)));
137
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 c.kb.if_then(active, [&] {
138 9 Value* o = c.b.build_add(c.b.build_mul(row, cols), col);
139 9 Value* dst_ptr = ptr_at(c, p[4], o, esz);
140 9 Value* acc0 = load_lanes(c, dt, 1, dst_ptr)[0];
141
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto sum_st = c.kb.for_range_reduce_n(
142
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 pos, m, c.i32(1), {acc0, c.i32(1)}, [&](Value* q, const std::vector<Value*>& cur) {
143
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 return emit_if_else_values(
144 9 c, c.b.build_ne(cur[1], c.i32(0)), {Type::f32(), Type::i32()},
145 18 [&]() -> std::vector<Value*> {
146 9 Value* kq = c.kb.load_i64(ptr_at(c, p[1], q, 8));
147 9 Value* rq = c.b.build_trunc_i32(c.b.build_lshr(kq, c.kb.const_i64(32)));
148
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 return emit_if_else_values(
149 9 c, c.b.build_eq(rq, row), {Type::f32(), Type::i32()},
150 18 [&]() -> std::vector<Value*> {
151 9 Value* mq = c.b.build_trunc_i32(kq);
152 9 Value* si = c.b.build_add(c.b.build_mul(mq, cols), col);
153 9 Value* sv = load_lanes(c, dt, 1, ptr_at(c, p[3], si, esz))[0];
154 9 return {c.b.build_add(cur[0], sv), c.i32(1)};
155 },
156 18 [&]() -> std::vector<Value*> { return {cur[0], c.i32(0)}; });
157 ✗ },
158 18 [&]() -> std::vector<Value*> { return cur; });
159 ✗ });
160
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 store_lanes(c, dt, 1, dst_ptr, {sum_st[0]});
161 9 });
162 9 });
163 9 }
164
165 } // namespace
166
167 16 Function* build_scatter_add_mir(Module& mod, std::uint32_t mode, Dtype dt) {
168 32 Function* fn = mod.create_function(
169 16 "mir_scatter_add", Type::void_type(),
170 112 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
171 96 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()});
172 16 brass::codegen::KernelBuilder kb(mod, fn);
173
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 Ctx c(kb);
174
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 auto p = entry_params(c, fn);
175
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
16 switch (mode) {
176
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 case SA_KEYS: build_sa_keys(c, p); break;
177
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 case SA_LOCAL: build_sa_local(c, p); break;
178
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ case SA_STEP: build_sa_step(c, p); break;
179
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 default: build_sa_sum(c, p, dt); break;
180 }
181
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 c.b.build_ret_void();
182
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 return finalize_fn(fn);
183 16 }
184
185 } // namespace brotensor::kernels
186