brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
60 / 69
87.0%
Branches:
26 / 52
50.0%
Functions:
6 / 6
100.0%

src/kernels/mir_gemm_splitk.cpp
Line Branch Exec Source
1 #include "mir_gemm_splitk.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include <stdexcept>
6 #include <utility>
7 #include <vector>
8
9 namespace brotensor::kernels {
10
11 namespace {
12
13 3 Value* load_elem_f32(Ctx& c, Dtype dt, Value* ptr, Value* idx) {
14 3 Value* ep = ptr_at(c, ptr, idx, 2);
15 3 Value* raw = c.kb.load_u16(ep);
16
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 return (dt == Dtype::FP16) ? c.kb.f16_to_f32(raw) : c.kb.bf16_to_f32(raw);
17 }
18
19 2 void store_elem_f32(Ctx& c, Dtype dt, Value* ptr, Value* idx, Value* v) {
20 2 Value* ep = ptr_at(c, ptr, idx, 2);
21
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 store_lanes(c, dt, 1, ep, {v});
22 2 }
23
24 } // namespace
25
26 2 Function* build_gemm_splitk_reduce_mir(Module& mod, const GemmSplitkSpec& spec) {
27
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
2 if (spec.dt != Dtype::FP16 && spec.dt != Dtype::BF16) {
28
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ throw std::runtime_error("build_gemm_splitk_reduce_mir: only FP16/BF16 supported");
29 }
30 4 Function* fn = mod.create_function(
31 2 "mir_gemm_splitk_reduce", Type::void_type(),
32 2 {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32()});
33
34 2 brass::codegen::KernelBuilder kb(mod, fn);
35
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Ctx c(kb);
36
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 auto p = entry_params(c, fn);
37 // p: partial(0), C(1), bias(2), M(3), N(4), splits(5)
38 2 Value* M = p[3];
39 2 Value* N = p[4];
40 2 Value* splits = p[5];
41
42 2 const bool gated = (spec.epi >= 2u);
43
4/6
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 1 time.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 time.
✗ Branch 5 not taken.
2 Value* out_cols = gated ? c.b.build_lshr(N, c.i32(1)) : N;
44
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* total = c.b.build_mul(M, out_cols);
45
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Value* idx = kb.global_tid_x();
46
47
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 kb.if_then(c.b.build_ult(idx, total), [&] {
48 2 Value* m = c.b.build_udiv(idx, out_cols);
49 2 Value* col = c.b.build_umod(idx, out_cols);
50 2 Value* stride = c.b.build_mul(M, N);
51
52 5 auto sum_at = [&](Value* n_col) -> Value* {
53 3 Value* base = c.b.build_add(c.b.build_mul(m, N), n_col);
54 3 Value* p_col = ptr_at(c, p[0], base, 4);
55 3 Value* s_bytes = c.b.build_shl(c.b.build_zext_i64(stride), kb.const_i64(2));
56
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 std::vector<Value*> red = kb.for_range_reduce_n(
57 3 c.i32(0), splits, c.i32(1), {c.f32(0.0f), p_col},
58
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 [&](Value*, const std::vector<Value*>& cur) -> std::vector<Value*> {
59 3 Value* v = kb.load_f32(cur[1], 0);
60 3 return {c.b.build_add(cur[0], v), c.b.build_add(cur[1], s_bytes)};
61 });
62 3 Value* val = red[0];
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (spec.has_bias) {
64
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 val = c.b.build_add(val, load_elem_f32(c, spec.dt, p[2], n_col));
65 3 }
66 3 return val;
67 3 };
68
69 2 Value* out_v = nullptr;
70
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 1 time.
2 if (!gated) {
71 1 Value* v = sum_at(col);
72
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.act != 0u) v = emit_gemm_act(c, spec.act, v);
73
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (spec.epi == 1u) {
74 ✗ v = c.b.build_add(v, load_elem_f32(c, spec.dt, p[1], idx));
75 ✗ }
76 1 out_v = v;
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 time.
2 } else if (spec.epi == 2u) {
78 ✗ Value* n0 = c.b.build_shl(col, c.i32(1));
79 ✗ Value* n1 = c.b.build_add(n0, c.i32(1));
80 ✗ Value* a = sum_at(n0);
81 ✗ Value* b = sum_at(n1);
82 ✗ out_v = c.b.build_mul(a, emit_gelu_exact(c, b));
83 ✗ } else {
84 1 Value* n0 = col;
85 1 Value* n1 = c.b.build_add(out_cols, col);
86 1 Value* a = sum_at(n0);
87 1 Value* b = sum_at(n1);
88 1 out_v = c.b.build_mul(emit_gemm_act(c, 4, a), b); // SiLU(a) * b
89 }
90 2 store_elem_f32(c, spec.dt, p[1], idx, out_v);
91 2 });
92
93
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 c.b.build_ret_void();
94
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 fn->rebuild_cfg_predecessors();
95 2 return fn;
96 2 }
97
98 } // namespace brotensor::kernels
99