brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
97 / 97
100.0%
Branches:
71 / 132
53.8%
Functions:
11 / 11
100.0%

src/kernels/mir_reduce.cpp
Line Branch Exec Source
1 #include "mir_reduce.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include <vector>
6
7 namespace brotensor::kernels {
8
9 8 Function* build_sum_rows_mir(Module& mod, Dtype dt, int lanes, bool mean) {
10 16 Function* fn = mod.create_function(
11 8 mean ? "mir_mean_rows" : "mir_sum_rows", Type::void_type(),
12 8 {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()});
13 8 brass::codegen::KernelBuilder kb(mod, fn);
14
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Ctx c(kb);
15
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 auto p = entry_params(c, fn);
16
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 const int esz = elem_bytes(dt);
17
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* s_val = kb.shared_alloc_f32(8);
18
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* tid = kb.tid_x();
19
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* n_vecs = vec_count(c, p[3], lanes);
20
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* block = c.i32(static_cast<int>(kReduceBlock));
21
22
4/8
✓ 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.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
16 kb.for_range(kb.ctaid_x(), p[2], kb.nctaid_x(), [&](Value* row) {
23 8 Value* base = c.b.build_mul(row, p[3]);
24
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 Value* acc = kb.for_range_reduce(tid, n_vecs, block, c.f32(0.0f), [&](Value* k, Value* cur) {
25 8 Value* col = col_elem_idx(c, k, lanes);
26 8 auto xs = load_lanes(c, dt, lanes, ptr_at(c, p[0], c.b.build_add(base, col), esz));
27
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 8 times.
26 for (int j = 0; j < lanes; ++j) {
28
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 cur = c.b.build_add(cur, xs[static_cast<std::size_t>(j)]);
29 18 }
30 8 return cur;
31 8 });
32 8 Value* total = emit_block_sum_f32(c, s_val, acc);
33
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 if (mean) total = c.b.build_mul(total, c.b.build_sdiv(c.f32(1.0f), kb.i32_to_f32(p[3])));
34
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
35
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 store_lanes(c, dt, 1, ptr_at(c, p[1], row, esz), {total});
36 8 });
37 8 });
38
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 c.b.build_ret_void();
39
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return finalize_fn(fn);
40 8 }
41
42 11 Function* build_argmax_rows_mir(Module& mod, Dtype dt, bool out_i32, int lanes) {
43 22 Function* fn = mod.create_function(
44 11 "mir_argmax_rows", Type::void_type(), {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()});
45 11 brass::codegen::KernelBuilder kb(mod, fn);
46
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Ctx c(kb);
47
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 auto p = entry_params(c, fn);
48
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 const int esz = elem_bytes(dt);
49
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* s_val = kb.shared_alloc_f32(8);
50
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* s_idx = kb.shared_alloc_i32(8);
51
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* tid = kb.tid_x();
52
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* n_vecs = vec_count(c, p[3], lanes);
53
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* block = c.i32(static_cast<int>(kReduceBlock));
54
55
4/8
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
22 kb.for_range(kb.ctaid_x(), p[2], kb.nctaid_x(), [&](Value* row) {
56 11 Value* base = c.b.build_mul(row, p[3]);
57 11 const std::vector<Value*> inits = {c.f32(-3.4028234663852886e38f), c.i32(0)};
58
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
22 auto res = kb.for_range_reduce_n(tid, n_vecs, block, inits, [&](Value* k, const std::vector<Value*>& cur) {
59 11 Value* bv = cur[0];
60 11 Value* bi = cur[1];
61 11 Value* col0 = col_elem_idx(c, k, lanes);
62 11 auto xs = load_lanes(c, dt, lanes, ptr_at(c, p[0], c.b.build_add(base, col0), esz));
63
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 27 times.
38 for (int j = 0; j < lanes; ++j) {
64 27 Value* vj = xs[static_cast<std::size_t>(j)];
65
4/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
27 Value* col = (j == 0) ? col0 : c.b.build_add(col0, c.i32(j));
66
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 Value* better = c.b.build_sgt(vj, bv);
67
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 bv = c.b.build_select(better, vj, bv);
68
1/2
✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
27 bi = c.b.build_select(better, col, bi);
69 27 }
70
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 return std::vector<Value*>{bv, bi};
71 11 });
72
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Value* win = emit_block_argmax_f32(c, s_val, s_idx, res[0], res[1]);
73
4/8
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
22 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
74 11 Value* out_ptr = ptr_at(c, p[1], row, 4);
75
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 9 times.
11 if (out_i32) {
76 2 c.b.build_store(Type::i32(), out_ptr, 0, win);
77 2 } else {
78 9 c.b.build_store(Type::f32(), out_ptr, 0, kb.i32_to_f32(win));
79 }
80 11 });
81 11 });
82
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 c.b.build_ret_void();
83
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 return finalize_fn(fn);
84 11 }
85
86 7 Function* build_sum_cols_mir(Module& mod, Dtype dt, int lanes) {
87 14 Function* fn = mod.create_function(
88 7 "mir_sum_cols", Type::void_type(), {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()});
89 7 brass::codegen::KernelBuilder kb(mod, fn);
90
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Ctx c(kb);
91
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto p = entry_params(c, fn);
92
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 const int esz = elem_bytes(dt);
93
94
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 emit_vec_grid_stride(c, p[3], lanes, kReduceBlock, [&](Value* k) {
95 7 Value* col = col_elem_idx(c, k, lanes);
96 7 std::vector<Value*> zeros(static_cast<std::size_t>(lanes), c.f32(0.0f));
97
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
14 auto sums = kb.for_range_reduce_n(c.i32(0), p[2], c.i32(1), zeros, [&](Value* r, const std::vector<Value*>& cur) {
98 7 Value* idx = c.b.build_add(c.b.build_mul(r, p[3]), col);
99 7 auto xs = load_lanes(c, dt, lanes, ptr_at(c, p[0], idx, esz));
100
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 std::vector<Value*> next(static_cast<std::size_t>(lanes));
101
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 17 times.
24 for (int j = 0; j < lanes; ++j) {
102 17 const auto js = static_cast<std::size_t>(j);
103
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 next[js] = c.b.build_add(cur[js], xs[js]);
104 17 }
105 7 return next;
106
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 });
107
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 store_lanes(c, dt, lanes, ptr_at(c, p[1], col, esz), sums);
108 7 });
109
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 c.b.build_ret_void();
110
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 return finalize_fn(fn);
111 7 }
112
113 } // namespace brotensor::kernels
114