| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include "mir_gemv.h" |
| 2 |
|
|
|
| 3 |
|
|
#include "mir_common.h" |
| 4 |
|
|
#include "mir_math.h" |
| 5 |
|
|
#include "../vulkan/shaders/op_codes.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <string> |
| 8 |
|
|
#include <vector> |
| 9 |
|
|
|
| 10 |
|
|
namespace brotensor::kernels { |
| 11 |
|
|
|
| 12 |
|
|
namespace { |
| 13 |
|
|
|
| 14 |
|
1472 |
std::vector<Value*> load8_vec(Ctx& c, Dtype dt, Value* base, Value* elem_idx) { |
| 15 |
|
1472 |
Value* p = ptr_at(c, base, elem_idx, elem_bytes(dt)); |
| 16 |
2/2
✓ Branch 0 taken 988 times.
✓ Branch 1 taken 484 times.
|
1472 |
if (dt == Dtype::FP32) { |
| 17 |
|
484 |
auto lo = load_lanes(c, Dtype::FP32, 4, p); |
| 18 |
4/8
✓ Branch 0 taken 484 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 484 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 484 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 484 times.
✗ Branch 7 not taken.
|
484 |
auto hi = load_lanes(c, Dtype::FP32, 4, ptr_at(c, base, c.b.build_add(elem_idx, c.i32(4)), 4)); |
| 19 |
1/2
✓ Branch 0 taken 484 times.
✗ Branch 1 not taken.
|
484 |
lo.insert(lo.end(), hi.begin(), hi.end()); |
| 20 |
|
484 |
return lo; |
| 21 |
|
484 |
} |
| 22 |
|
988 |
return load_lanes(c, dt, 8, p); |
| 23 |
|
1472 |
} |
| 24 |
|
|
|
| 25 |
|
331 |
std::vector<Value*> load8_scalar(Ctx& c, Dtype dt, Value* base, Value* idx, Value* col, Value* ncols) { |
| 26 |
|
331 |
std::vector<Value*> out(8); |
| 27 |
2/2
✓ Branch 0 taken 2648 times.
✓ Branch 1 taken 331 times.
|
2979 |
for (int e = 0; e < 8; ++e) { |
| 28 |
3/6
✓ Branch 0 taken 2648 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2648 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2648 times.
✗ Branch 5 not taken.
|
2648 |
Value* ok = c.b.build_ult(c.b.build_add(col, c.i32(e)), ncols); |
| 29 |
1/2
✓ Branch 0 taken 2648 times.
✗ Branch 1 not taken.
|
2648 |
out[static_cast<std::size_t>(e)] = emit_if_else_values( |
| 30 |
1/2
✓ Branch 0 taken 2648 times.
✗ Branch 1 not taken.
|
2648 |
c, ok, {Type::f32()}, |
| 31 |
|
5296 |
[&]() -> std::vector<Value*> { return {load_elem_dt(c, dt, base, c.b.build_add(idx, c.i32(e)))}; }, |
| 32 |
|
5296 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 33 |
|
2648 |
} |
| 34 |
|
331 |
return out; |
| 35 |
|
331 |
} |
| 36 |
|
|
|
| 37 |
|
|
} // namespace |
| 38 |
|
|
|
| 39 |
|
142 |
brass::Function* build_gemv_mir(brass::Module& mod, Dtype dt_x, Dtype dt_w, Dtype dt_y, |
| 40 |
|
|
std::uint32_t mb, std::uint32_t epi, std::uint32_t act, |
| 41 |
|
|
bool vec, bool has_bias) { |
| 42 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 133 times.
|
142 |
const bool glu = (epi == EPI_GEGLU || epi == EPI_SWIGLU); |
| 43 |
|
142 |
const std::uint32_t nw = glu ? 2u : 1u; |
| 44 |
|
142 |
const std::uint32_t unr = (mb * nw <= 8u) ? 4u : 2u; |
| 45 |
3/6
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
|
284 |
const std::string name = std::string("mir_gemv_m") + std::to_string(mb) + |
| 46 |
6/12
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 142 times.
✗ Branch 11 not taken.
|
284 |
"_e" + std::to_string(epi) + "_a" + std::to_string(act) + |
| 47 |
3/4
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 63 times.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
|
142 |
(vec ? "_v" : "_s") + (has_bias ? "_b" : ""); |
| 48 |
|
|
|
| 49 |
11/22
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 142 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 142 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 142 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 142 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 142 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 142 times.
✗ Branch 21 not taken.
|
1562 |
std::vector<Type> params = { |
| 50 |
|
568 |
Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), |
| 51 |
|
994 |
Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), |
| 52 |
|
|
}; |
| 53 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 54 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 55 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
Ctx c(kb); |
| 56 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
auto p = entry_params(c, fn); |
| 57 |
|
|
// p: a(0) b(1) c(2) bias(3) m(4) n(5) k(6) lda(7) ldb(8) ldc(9) half_n(10) |
| 58 |
|
|
|
| 59 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
Value* s_part = kb.shared_alloc_f32(8 * 16); |
| 60 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
Value* tid = kb.tid_x(); |
| 61 |
2/4
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
|
142 |
Value* unit = c.b.build_udiv(tid, c.i32(64)); |
| 62 |
2/4
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
|
142 |
Value* t = c.b.build_umod(tid, c.i32(64)); |
| 63 |
5/10
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
|
142 |
Value* g = c.b.build_add(kb.ctaid_x(), c.b.build_mul(kb.ctaid_y(), kb.nctaid_x())); |
| 64 |
3/6
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
|
142 |
Value* j = c.b.build_add(c.b.build_shl(g, c.i32(2)), unit); |
| 65 |
6/8
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 136 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 127 times.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
|
142 |
Value* nout = (epi == EPI_SWIGLU) ? p[10] : (epi == EPI_GEGLU ? c.b.build_lshr(p[5], c.i32(1)) : p[5]); |
| 66 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
Value* live = c.b.build_ult(j, nout); |
| 67 |
|
|
|
| 68 |
|
|
Value* wrow[2]; |
| 69 |
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 133 times.
|
142 |
if (epi == EPI_GEGLU) { |
| 70 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 |
wrow[0] = c.b.build_shl(j, c.i32(1)); |
| 71 |
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
|
9 |
wrow[1] = c.b.build_add(wrow[0], c.i32(1)); |
| 72 |
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 127 times.
|
142 |
} else if (epi == EPI_SWIGLU) { |
| 73 |
|
6 |
wrow[0] = j; |
| 74 |
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
|
6 |
wrow[1] = c.b.build_add(p[10], j); |
| 75 |
|
6 |
} else { |
| 76 |
|
127 |
wrow[0] = j; |
| 77 |
|
127 |
wrow[1] = j; |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
2/4
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
|
142 |
std::vector<Value*> acc_init(mb * nw, c.f32(0.0f)); |
| 81 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
std::vector<Value*> acc = emit_if_else_values( |
| 82 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
c, live, std::vector<Type>(mb * nw, Type::f32()), |
| 83 |
|
284 |
[&]() -> std::vector<Value*> { |
| 84 |
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 79 times.
|
142 |
if (vec) { |
| 85 |
|
79 |
Value* nch = c.b.build_lshr(p[6], c.i32(3)); |
| 86 |
2/4
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 79 times.
✗ Branch 3 not taken.
|
158 |
return kb.for_range_reduce_n( |
| 87 |
|
79 |
t, nch, c.i32(static_cast<int>(64u * unr)), acc_init, |
| 88 |
|
158 |
[&](Value* c0, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 89 |
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 |
std::vector<Value*> ok(unr), cc(unr); |
| 90 |
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 |
std::vector<std::vector<std::vector<Value*>>> wv( |
| 91 |
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 |
unr, std::vector<std::vector<Value*>>(nw)); |
| 92 |
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 314 times.
|
393 |
for (std::uint32_t u = 0; u < unr; ++u) { |
| 93 |
2/4
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
|
314 |
Value* col_u = c.b.build_add(c0, c.i32(static_cast<int>(u * 64u))); |
| 94 |
1/2
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
|
314 |
Value* in_k = c.b.build_ult(col_u, nch); |
| 95 |
3/6
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 314 times.
✗ Branch 5 not taken.
|
314 |
ok[u] = c.b.build_select(in_k, c.f32(1.0f), c.f32(0.0f)); |
| 96 |
1/2
✓ Branch 0 taken 314 times.
✗ Branch 1 not taken.
|
314 |
cc[u] = c.b.build_select(in_k, col_u, c0); |
| 97 |
2/2
✓ Branch 0 taken 352 times.
✓ Branch 1 taken 314 times.
|
666 |
for (std::uint32_t w = 0; w < nw; ++w) { |
| 98 |
2/4
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
|
704 |
Value* idx = c.b.build_add(c.b.build_mul(wrow[w], p[8]), |
| 99 |
2/4
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
|
352 |
c.b.build_shl(cc[u], c.i32(3))); |
| 100 |
1/2
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
|
352 |
wv[u][w] = load8_vec(c, dt_w, p[1], idx); |
| 101 |
|
352 |
} |
| 102 |
|
314 |
} |
| 103 |
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 |
std::vector<Value*> next = cur; |
| 104 |
2/2
✓ Branch 0 taken 79 times.
✓ Branch 1 taken 284 times.
|
363 |
for (std::uint32_t r = 0; r < mb; ++r) { |
| 105 |
2/4
✓ Branch 0 taken 284 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 284 times.
✗ Branch 3 not taken.
|
284 |
Value* row_off = c.b.build_mul(c.i32(static_cast<int>(r)), p[7]); |
| 106 |
2/2
✓ Branch 0 taken 284 times.
✓ Branch 1 taken 1120 times.
|
1404 |
for (std::uint32_t u = 0; u < unr; ++u) { |
| 107 |
3/6
✓ Branch 0 taken 1120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1120 times.
✗ Branch 5 not taken.
|
1120 |
Value* aidx = c.b.build_add(row_off, c.b.build_shl(cc[u], c.i32(3))); |
| 108 |
1/2
✓ Branch 0 taken 1120 times.
✗ Branch 1 not taken.
|
1120 |
auto xv = load8_vec(c, dt_x, p[0], aidx); |
| 109 |
2/2
✓ Branch 0 taken 1120 times.
✓ Branch 1 taken 1220 times.
|
2340 |
for (std::uint32_t w = 0; w < nw; ++w) { |
| 110 |
1/2
✓ Branch 0 taken 1220 times.
✗ Branch 1 not taken.
|
1220 |
Value* s = c.f32(0.0f); |
| 111 |
2/2
✓ Branch 0 taken 1220 times.
✓ Branch 1 taken 9760 times.
|
10980 |
for (int e = 0; e < 8; ++e) { |
| 112 |
1/2
✓ Branch 0 taken 9760 times.
✗ Branch 1 not taken.
|
9760 |
s = c.b.build_fma_f32(xv[e], wv[u][w][e], s); |
| 113 |
|
9760 |
} |
| 114 |
|
1220 |
std::size_t q = r * nw + w; |
| 115 |
1/2
✓ Branch 0 taken 1220 times.
✗ Branch 1 not taken.
|
1220 |
next[q] = c.b.build_fma_f32(s, ok[u], next[q]); |
| 116 |
|
1220 |
} |
| 117 |
|
1120 |
} |
| 118 |
|
284 |
} |
| 119 |
|
79 |
return next; |
| 120 |
1/2
✓ Branch 0 taken 79 times.
✗ Branch 1 not taken.
|
79 |
}); |
| 121 |
|
|
} |
| 122 |
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
|
126 |
return kb.for_range_reduce_n( |
| 123 |
|
63 |
c.b.build_shl(t, c.i32(3)), p[6], c.i32(64 * 8), acc_init, |
| 124 |
|
126 |
[&](Value* col, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 125 |
|
63 |
std::vector<std::vector<Value*>> wv(nw); |
| 126 |
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 68 times.
|
131 |
for (std::uint32_t w = 0; w < nw; ++w) { |
| 127 |
3/6
✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 68 times.
✗ Branch 5 not taken.
|
68 |
wv[w] = load8_scalar(c, dt_w, p[1], c.b.build_add(c.b.build_mul(wrow[w], p[8]), col), col, p[6]); |
| 128 |
|
68 |
} |
| 129 |
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 |
std::vector<Value*> next = cur; |
| 130 |
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 263 times.
|
326 |
for (std::uint32_t r = 0; r < mb; ++r) { |
| 131 |
3/6
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 263 times.
✗ Branch 5 not taken.
|
263 |
Value* aidx = c.b.build_add(c.b.build_mul(c.i32(static_cast<int>(r)), p[7]), col); |
| 132 |
1/2
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
|
263 |
auto xv = load8_scalar(c, dt_x, p[0], aidx, col, p[6]); |
| 133 |
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 286 times.
|
549 |
for (std::uint32_t w = 0; w < nw; ++w) { |
| 134 |
1/2
✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
|
286 |
Value* s = c.f32(0.0f); |
| 135 |
2/2
✓ Branch 0 taken 286 times.
✓ Branch 1 taken 2288 times.
|
2574 |
for (int e = 0; e < 8; ++e) { |
| 136 |
1/2
✓ Branch 0 taken 2288 times.
✗ Branch 1 not taken.
|
2288 |
s = c.b.build_fma_f32(xv[e], wv[w][e], s); |
| 137 |
|
2288 |
} |
| 138 |
|
286 |
std::size_t q = r * nw + w; |
| 139 |
1/2
✓ Branch 0 taken 286 times.
✗ Branch 1 not taken.
|
286 |
next[q] = c.b.build_add(next[q], s); |
| 140 |
|
286 |
} |
| 141 |
|
263 |
} |
| 142 |
|
63 |
return next; |
| 143 |
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
|
63 |
}); |
| 144 |
|
142 |
}, |
| 145 |
|
284 |
[&]() -> std::vector<Value*> { return acc_init; }); |
| 146 |
|
|
|
| 147 |
2/4
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
|
142 |
Value* lane = c.b.build_and(tid, c.i32(31)); |
| 148 |
4/8
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
|
142 |
Value* w32 = c.b.build_and(c.b.build_lshr(tid, c.i32(5)), c.i32(1)); |
| 149 |
5/10
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
|
142 |
Value* sg_base = c.b.build_shl(c.b.build_add(c.b.build_shl(unit, c.i32(1)), w32), c.i32(4)); |
| 150 |
2/2
✓ Branch 0 taken 142 times.
✓ Branch 1 taken 599 times.
|
741 |
for (std::uint32_t q = 0; q < mb * nw; ++q) { |
| 151 |
|
599 |
Value* s = acc[q]; |
| 152 |
2/2
✓ Branch 0 taken 599 times.
✓ Branch 1 taken 2995 times.
|
3594 |
for (std::uint32_t m = 16; m >= 1; m >>= 1) { |
| 153 |
2/4
✓ Branch 0 taken 2995 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2995 times.
✗ Branch 3 not taken.
|
2995 |
s = c.b.build_add(s, kb.shfl_bfly_f32(s, m)); |
| 154 |
|
2995 |
} |
| 155 |
4/8
✓ Branch 0 taken 599 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 599 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 599 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 599 times.
✗ Branch 7 not taken.
|
1198 |
kb.if_then(c.b.build_eq(lane, c.i32(0)), [&] { |
| 156 |
|
599 |
kb.shared_store_f32_indexed(s_part, c.b.build_add(sg_base, c.i32(static_cast<int>(q))), s); |
| 157 |
|
599 |
}); |
| 158 |
|
599 |
} |
| 159 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
kb.sync(); |
| 160 |
|
|
|
| 161 |
5/10
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 142 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 142 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 142 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 142 times.
✗ Branch 9 not taken.
|
284 |
kb.if_then(c.b.build_and(c.b.build_eq(t, c.i32(0)), live), [&] { |
| 162 |
|
142 |
Value* u0_base = c.b.build_shl(unit, c.i32(5)); |
| 163 |
|
142 |
Value* u1_base = c.b.build_add(u0_base, c.i32(16)); |
| 164 |
2/2
✓ Branch 0 taken 547 times.
✓ Branch 1 taken 142 times.
|
689 |
for (std::uint32_t r = 0; r < mb; ++r) { |
| 165 |
|
547 |
Value* v[2] = {c.f32(0.0f), c.f32(0.0f)}; |
| 166 |
2/2
✓ Branch 0 taken 599 times.
✓ Branch 1 taken 547 times.
|
1146 |
for (std::uint32_t w = 0; w < nw; ++w) { |
| 167 |
|
599 |
Value* qv = c.i32(static_cast<int>(r * nw + w)); |
| 168 |
|
1198 |
Value* s = c.b.build_add(kb.shared_load_f32_indexed(s_part, c.b.build_add(u0_base, qv)), |
| 169 |
|
599 |
kb.shared_load_f32_indexed(s_part, c.b.build_add(u1_base, qv))); |
| 170 |
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 359 times.
|
599 |
if (has_bias) { |
| 171 |
|
359 |
s = c.b.build_add(s, load_elem_dt(c, dt_y, p[3], wrow[w])); |
| 172 |
|
359 |
} |
| 173 |
|
599 |
v[w] = emit_gemm_act(c, act, s); |
| 174 |
|
599 |
} |
| 175 |
|
547 |
Value* o = c.b.build_add(c.b.build_mul(c.i32(static_cast<int>(r)), p[9]), j); |
| 176 |
|
547 |
Value* out = v[0]; |
| 177 |
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 514 times.
|
547 |
if (epi == EPI_GEGLU) { |
| 178 |
|
33 |
out = c.b.build_mul(v[0], emit_gelu_exact(c, v[1])); |
| 179 |
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 495 times.
|
547 |
} else if (epi == EPI_SWIGLU) { |
| 180 |
|
19 |
out = c.b.build_mul(c.b.build_mul(v[0], emit_sigmoid(c, v[0])), v[1]); |
| 181 |
2/2
✓ Branch 0 taken 374 times.
✓ Branch 1 taken 121 times.
|
514 |
} else if (epi == EPI_ACCUM) { |
| 182 |
|
121 |
out = c.b.build_add(v[0], load_elem_dt(c, dt_y, p[2], o)); |
| 183 |
|
121 |
} |
| 184 |
|
547 |
store_elem_dt(c, dt_y, p[2], o, out); |
| 185 |
|
547 |
} |
| 186 |
|
142 |
}); |
| 187 |
|
|
|
| 188 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
c.b.build_ret_void(); |
| 189 |
1/2
✓ Branch 0 taken 142 times.
✗ Branch 1 not taken.
|
142 |
return finalize_fn(fn); |
| 190 |
|
142 |
} |
| 191 |
|
|
|
| 192 |
|
✗ |
brass::Function* build_gemv_nn_mir(brass::Module& mod, Dtype dt_x, Dtype dt_w, Dtype dt_y, |
| 193 |
|
|
std::uint32_t mb, std::uint32_t act, bool vec, bool has_bias) { |
| 194 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
const std::string name = std::string("mir_gemv_nn_m") + std::to_string(mb) + |
| 195 |
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
✗ |
"_a" + std::to_string(act) + (vec ? "_v" : "_s") + (has_bias ? "_b" : ""); |
| 196 |
0/22
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
✗ |
std::vector<Type> params = { |
| 197 |
|
✗ |
Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), |
| 198 |
|
✗ |
Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), |
| 199 |
|
|
}; |
| 200 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 201 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
brass::codegen::KernelBuilder kb(mod, fn); |
| 202 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Ctx c(kb); |
| 203 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
auto p = entry_params(c, fn); |
| 204 |
|
|
// p: a(0) b(1) c(2) bias(3) m(4) n(5) k(6) lda(7) ldb(8) ldc(9) (10 unused) |
| 205 |
|
|
|
| 206 |
|
|
// A thread owns 4 adjacent output columns of every row: B is read a row |
| 207 |
|
|
// at a time, coalesced across the block, and A's (mb, k) panel is |
| 208 |
|
|
// broadcast (every thread reads the same element). |
| 209 |
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
|
✗ |
Value* g = c.b.build_add(kb.ctaid_x(), c.b.build_mul(kb.ctaid_y(), kb.nctaid_x())); |
| 210 |
0/12
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
|
✗ |
Value* col0 = c.b.build_shl(c.b.build_add(c.b.build_mul(g, kb.ntid_x()), kb.tid_x()), c.i32(2)); |
| 211 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
kb.if_then(c.b.build_ult(col0, p[5]), [&] { |
| 212 |
|
✗ |
std::vector<Value*> acc_init(mb * 4u, c.f32(0.0f)); |
| 213 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
std::vector<Value*> acc = kb.for_range_reduce_n( |
| 214 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
c.i32(0), p[6], c.i32(1), acc_init, |
| 215 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
[&](Value* kk, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 216 |
|
✗ |
Value* bidx = c.b.build_add(c.b.build_mul(kk, p[8]), col0); |
| 217 |
|
✗ |
std::vector<Value*> bv(4); |
| 218 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (vec) { |
| 219 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
bv = load_lanes(c, dt_w, 4, ptr_at(c, p[1], bidx, elem_bytes(dt_w))); |
| 220 |
|
✗ |
} else { |
| 221 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (int e = 0; e < 4; ++e) { |
| 222 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
Value* ok = c.b.build_ult(c.b.build_add(col0, c.i32(e)), p[5]); |
| 223 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
bv[static_cast<std::size_t>(e)] = emit_if_else_values( |
| 224 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
c, ok, {Type::f32()}, |
| 225 |
|
✗ |
[&]() -> std::vector<Value*> { |
| 226 |
|
✗ |
return {load_elem_dt(c, dt_w, p[1], c.b.build_add(bidx, c.i32(e)))}; |
| 227 |
|
|
}, |
| 228 |
|
✗ |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 229 |
|
✗ |
} |
| 230 |
|
|
} |
| 231 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
std::vector<Value*> next = cur; |
| 232 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (std::uint32_t r = 0; r < mb; ++r) { |
| 233 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Value* a = load_elem_dt( |
| 234 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
c, dt_x, p[0], c.b.build_add(c.b.build_mul(c.i32(static_cast<int>(r)), p[7]), kk)); |
| 235 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (std::uint32_t e = 0; e < 4u; ++e) { |
| 236 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
next[r * 4u + e] = c.b.build_fma_f32(a, bv[e], next[r * 4u + e]); |
| 237 |
|
✗ |
} |
| 238 |
|
✗ |
} |
| 239 |
|
✗ |
return next; |
| 240 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
}); |
| 241 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (std::uint32_t e = 0; e < 4u; ++e) { |
| 242 |
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
✗ |
Value* col = c.b.build_add(col0, c.i32(static_cast<int>(e))); |
| 243 |
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
|
✗ |
kb.if_then(c.b.build_ult(col, p[5]), [&] { |
| 244 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
Value* bias = has_bias ? load_elem_dt(c, dt_y, p[3], col) : nullptr; |
| 245 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
for (std::uint32_t r = 0; r < mb; ++r) { |
| 246 |
|
✗ |
Value* s = acc[r * 4u + e]; |
| 247 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
if (bias) s = c.b.build_add(s, bias); |
| 248 |
|
✗ |
s = emit_gemm_act(c, act, s); |
| 249 |
|
✗ |
Value* o = c.b.build_add(c.b.build_mul(c.i32(static_cast<int>(r)), p[9]), col); |
| 250 |
|
✗ |
store_elem_dt(c, dt_y, p[2], o, s); |
| 251 |
|
✗ |
} |
| 252 |
|
✗ |
}); |
| 253 |
|
✗ |
} |
| 254 |
|
✗ |
}); |
| 255 |
|
|
|
| 256 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
c.b.build_ret_void(); |
| 257 |
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
✗ |
return finalize_fn(fn); |
| 258 |
|
✗ |
} |
| 259 |
|
|
|
| 260 |
|
|
} // namespace brotensor::kernels |
| 261 |
|
|
|