| Line |
Branch |
Exec |
Source |
| 1 |
|
|
#include "mir_gemm_simt.h" |
| 2 |
|
|
|
| 3 |
|
|
#include "mir_common.h" |
| 4 |
|
|
#include "mir_math.h" |
| 5 |
|
|
#include "../vulkan/shaders/op_codes.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <algorithm> |
| 8 |
|
|
#include <string> |
| 9 |
|
|
#include <vector> |
| 10 |
|
|
|
| 11 |
|
|
namespace brotensor::kernels { |
| 12 |
|
|
|
| 13 |
|
|
namespace { |
| 14 |
|
|
|
| 15 |
|
|
constexpr std::uint32_t kSimtWg = 256; |
| 16 |
|
|
constexpr std::uint32_t kSimtBk = 16; |
| 17 |
|
|
|
| 18 |
|
|
struct TileDims { |
| 19 |
|
|
std::uint32_t bm, bn, tm, tn, la, lb, pa, pb, ntx; |
| 20 |
|
|
int bn_shift; |
| 21 |
|
|
}; |
| 22 |
|
|
|
| 23 |
|
84 |
TileDims make_tile_dims(bool big128) { |
| 24 |
|
84 |
TileDims d{}; |
| 25 |
|
84 |
d.bm = big128 ? 128u : 64u; |
| 26 |
|
84 |
d.bn = big128 ? 128u : 64u; |
| 27 |
|
84 |
d.tm = big128 ? 8u : 4u; |
| 28 |
|
84 |
d.tn = big128 ? 8u : 4u; |
| 29 |
|
84 |
d.la = (d.bm * kSimtBk) / kSimtWg; |
| 30 |
|
84 |
d.lb = (d.bn * kSimtBk) / kSimtWg; |
| 31 |
|
84 |
d.pa = d.bm + 4u; |
| 32 |
|
84 |
d.pb = d.bn + 4u; |
| 33 |
|
84 |
d.ntx = d.bn / d.tn; |
| 34 |
|
84 |
d.bn_shift = big128 ? 7 : 6; |
| 35 |
|
84 |
return d; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
255 |
Value* ptr_offset_i64(Ctx& c, Value* base, Value* elem_off_i64, int esz) { |
| 39 |
|
255 |
return c.b.build_add(base, c.b.build_mul(elem_off_i64, c.kb.const_i64(esz))); |
| 40 |
|
|
} |
| 41 |
|
|
|
| 42 |
|
168 |
Value* batch_base_ptr(Ctx& c, Value* base, Value* z, Value* stride, int esz) { |
| 43 |
|
168 |
Value* off = c.b.build_mul(c.b.build_zext_i64(z), c.b.build_zext_i64(stride)); |
| 44 |
|
168 |
return ptr_offset_i64(c, base, off, esz); |
| 45 |
|
|
} |
| 46 |
|
|
|
| 47 |
|
|
template <typename LoadFn, typename StoreFn> |
| 48 |
|
84 |
std::vector<Value*> run_simt_k_loop(Ctx& c, const TileDims& d, Value* k_dim, |
| 49 |
|
|
Value* As, Value* Bs, Value* Bs_up, |
| 50 |
|
|
LoadFn&& load_tiles, StoreFn&& store_tiles) { |
| 51 |
|
84 |
const std::size_t n_acc = static_cast<std::size_t>(d.tm) * d.tn; |
| 52 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 29 times.
|
84 |
const std::size_t k_accs = Bs_up ? (2u * n_acc) : n_acc; |
| 53 |
|
84 |
const std::size_t n_reg = static_cast<std::size_t>(d.la) + d.lb * (Bs_up ? 2u : 1u); |
| 54 |
|
84 |
Value* nk = c.b.build_lshr(c.b.build_add(k_dim, c.i32(static_cast<int>(kSimtBk - 1u))), c.i32(4)); |
| 55 |
|
84 |
Value* tid = c.kb.tid_x(); |
| 56 |
|
84 |
Value* tx = c.b.build_umod(tid, c.i32(static_cast<int>(d.ntx))); |
| 57 |
|
84 |
Value* ty = c.b.build_udiv(tid, c.i32(static_cast<int>(d.ntx))); |
| 58 |
|
84 |
Value* tx_tn = c.b.build_mul(tx, c.i32(static_cast<int>(d.tn))); |
| 59 |
|
84 |
Value* ty_tm = c.b.build_mul(ty, c.i32(static_cast<int>(d.tm))); |
| 60 |
|
|
|
| 61 |
|
84 |
std::vector<Type> reg_types(n_reg, Type::f32()); |
| 62 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
std::vector<Value*> reg0 = emit_if_else_values( |
| 63 |
4/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
84 |
c, c.b.build_sgt(nk, c.i32(0)), reg_types, |
| 64 |
|
168 |
[&]() -> std::vector<Value*> { return load_tiles(c.i32(0)); }, |
| 65 |
|
168 |
[&]() -> std::vector<Value*> { return std::vector<Value*>(n_reg, c.f32(0.0f)); }); |
| 66 |
|
|
|
| 67 |
|
84 |
std::vector<Value*> init = std::move(reg0); |
| 68 |
4/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
84 |
init.resize(n_reg + k_accs, c.f32(0.0f)); |
| 69 |
|
|
|
| 70 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
168 |
std::vector<Value*> fin = c.kb.for_range_reduce_n( |
| 71 |
4/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
84 |
c.i32(0), nk, c.i32(1), init, |
| 72 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
168 |
[&](Value* kt, const std::vector<Value*>& cur) -> std::vector<Value*> { |
| 73 |
|
84 |
store_tiles(cur); |
| 74 |
|
84 |
c.kb.sync(); |
| 75 |
|
|
|
| 76 |
|
84 |
Value* kt1 = c.b.build_add(kt, c.i32(1)); |
| 77 |
|
84 |
std::vector<Value*> next_reg = emit_if_else_values( |
| 78 |
|
84 |
c, c.b.build_ult(kt1, nk), reg_types, |
| 79 |
|
168 |
[&]() -> std::vector<Value*> { return load_tiles(c.b.build_shl(kt1, c.i32(4))); }, |
| 80 |
|
168 |
[&]() -> std::vector<Value*> { return std::vector<Value*>(n_reg, c.f32(0.0f)); }); |
| 81 |
|
|
|
| 82 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
std::vector<Value*> acc(cur.begin() + static_cast<std::ptrdiff_t>(n_reg), cur.end()); |
| 83 |
4/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 29 times.
✓ Branch 3 taken 464 times.
|
1428 |
for (std::uint32_t kk = 0; kk < kSimtBk; ++kk) { |
| 84 |
4/8
✓ Branch 0 taken 880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 880 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 464 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 464 times.
✗ Branch 7 not taken.
|
1344 |
Value* a_base = c.b.build_add(c.i32(static_cast<int>(kk * d.pa)), ty_tm); |
| 85 |
4/8
✓ Branch 0 taken 880 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 880 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 464 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 464 times.
✗ Branch 7 not taken.
|
1344 |
Value* b_base = c.b.build_add(c.i32(static_cast<int>(kk * d.pb)), tx_tn); |
| 86 |
4/8
✓ Branch 0 taken 880 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 880 times.
✓ Branch 4 taken 464 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 464 times.
|
1344 |
std::vector<Value*> av(d.tm), bv(d.tn), buv; |
| 87 |
4/4
✓ Branch 0 taken 3712 times.
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 1920 times.
✓ Branch 3 taken 464 times.
|
6976 |
for (std::uint32_t i = 0; i < d.tm; ++i) { |
| 88 |
6/12
✓ Branch 0 taken 3712 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3712 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3712 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1920 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1920 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1920 times.
✗ Branch 11 not taken.
|
5632 |
av[i] = c.kb.shared_load_f32_indexed(As, c.b.build_add(a_base, c.i32(static_cast<int>(i)))); |
| 89 |
|
5632 |
} |
| 90 |
4/4
✓ Branch 0 taken 3712 times.
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 1920 times.
✓ Branch 3 taken 464 times.
|
6976 |
for (std::uint32_t j = 0; j < d.tn; ++j) { |
| 91 |
6/12
✓ Branch 0 taken 3712 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3712 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3712 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1920 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 1920 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 1920 times.
✗ Branch 11 not taken.
|
5632 |
bv[j] = c.kb.shared_load_f32_indexed(Bs, c.b.build_add(b_base, c.i32(static_cast<int>(j)))); |
| 92 |
|
5632 |
} |
| 93 |
3/4
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 832 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 464 times.
|
1344 |
if (Bs_up) { |
| 94 |
1/4
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
48 |
buv.resize(d.tn); |
| 95 |
2/4
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
240 |
for (std::uint32_t j = 0; j < d.tn; ++j) { |
| 96 |
3/12
✓ Branch 0 taken 192 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 192 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 192 times.
✗ 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.
|
192 |
buv[j] = c.kb.shared_load_f32_indexed(Bs_up, c.b.build_add(b_base, c.i32(static_cast<int>(j)))); |
| 97 |
|
192 |
} |
| 98 |
|
48 |
} |
| 99 |
4/4
✓ Branch 0 taken 3712 times.
✓ Branch 1 taken 880 times.
✓ Branch 2 taken 1920 times.
✓ Branch 3 taken 464 times.
|
6976 |
for (std::uint32_t i = 0; i < d.tm; ++i) { |
| 100 |
4/4
✓ Branch 0 taken 3712 times.
✓ Branch 1 taken 16384 times.
✓ Branch 2 taken 1920 times.
✓ Branch 3 taken 8192 times.
|
30208 |
for (std::uint32_t j = 0; j < d.tn; ++j) { |
| 101 |
|
24576 |
std::size_t idx = static_cast<std::size_t>(i) * d.tn + j; |
| 102 |
2/4
✓ Branch 0 taken 16384 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8192 times.
✗ Branch 3 not taken.
|
24576 |
acc[idx] = c.b.build_fma_f32(av[i], bv[j], acc[idx]); |
| 103 |
3/4
✓ Branch 0 taken 768 times.
✓ Branch 1 taken 15616 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8192 times.
|
24576 |
if (Bs_up) { |
| 104 |
1/4
✓ Branch 0 taken 768 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
768 |
acc[n_acc + idx] = c.b.build_fma_f32(av[i], buv[j], acc[n_acc + idx]); |
| 105 |
|
768 |
} |
| 106 |
|
24576 |
} |
| 107 |
|
5632 |
} |
| 108 |
|
1344 |
} |
| 109 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
c.kb.sync(); |
| 110 |
|
|
|
| 111 |
|
84 |
std::vector<Value*> out = std::move(next_reg); |
| 112 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
out.insert(out.end(), acc.begin(), acc.end()); |
| 113 |
|
84 |
return out; |
| 114 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
}); |
| 115 |
|
|
|
| 116 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
84 |
return std::vector<Value*>(fin.begin() + static_cast<std::ptrdiff_t>(n_reg), fin.end()); |
| 117 |
|
84 |
} |
| 118 |
|
|
|
| 119 |
|
|
} // namespace |
| 120 |
|
|
|
| 121 |
|
55 |
brass::Function* build_gemm_simt_mir(brass::Module& mod, Dtype dt_a, Dtype dt_b, Dtype dt_c, Dtype dt_bias, |
| 122 |
|
|
bool big128, std::uint32_t epi, std::uint32_t act, |
| 123 |
|
|
bool ta, bool nb, bool has_bias) { |
| 124 |
|
55 |
const TileDims d = make_tile_dims(big128); |
| 125 |
|
55 |
const bool has_res = (epi == EPI_RESIDUAL_ADD); |
| 126 |
|
55 |
const bool is_swiglu = (epi == EPI_SWIGLU); |
| 127 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
165 |
const std::string name = std::string("mir_gemm_simt_") + (big128 ? "128_" : "64_") + |
| 128 |
9/14
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 13 times.
✓ Branch 3 taken 42 times.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 55 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 55 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 55 times.
✗ Branch 13 not taken.
|
55 |
(ta ? "t" : "n") + (nb ? "t" : "n") + "_e" + std::to_string(epi) + |
| 129 |
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
|
55 |
"_a" + std::to_string(act) + (has_bias ? "_b" : ""); |
| 130 |
4/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 55 times.
✗ Branch 7 not taken.
|
220 |
std::vector<Type> params = { |
| 131 |
|
220 |
Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), |
| 132 |
|
|
}; |
| 133 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
55 |
if (has_res) params.push_back(Type::ptr()); |
| 134 |
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 495 times.
✓ Branch 2 taken 495 times.
✗ Branch 3 not taken.
|
550 |
for (int i = 0; i < 9; ++i) params.push_back(Type::i32()); |
| 135 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 136 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 137 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Ctx c(kb); |
| 138 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
auto raw_p = entry_params(c, fn); |
| 139 |
|
55 |
std::vector<Value*> p; |
| 140 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
p.reserve(13); |
| 141 |
3/4
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 55 times.
✓ Branch 2 taken 220 times.
✗ Branch 3 not taken.
|
275 |
for (int i = 0; i < 4; ++i) p.push_back(raw_p[static_cast<std::size_t>(i)]); |
| 142 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
|
55 |
Value* residual_ptr = has_res ? raw_p[4] : nullptr; |
| 143 |
3/4
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 495 times.
✓ Branch 2 taken 495 times.
✗ Branch 3 not taken.
|
550 |
for (int i = 0; i < 9; ++i) p.push_back(raw_p[static_cast<std::size_t>(4 + (has_res ? 1 : 0) + i)]); |
| 144 |
|
|
// p: a(0) b(1) c(2) bias(3) m(4) n(5) k(6) lda(7) ldb(8) ldc(9) sa(10) sb(11) sc(12) |
| 145 |
|
|
|
| 146 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Value* As = kb.shared_alloc_f32(kSimtBk * d.pa); |
| 147 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Value* Bs = kb.shared_alloc_f32(kSimtBk * d.pb); |
| 148 |
3/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
55 |
Value* Bs_up = is_swiglu ? kb.shared_alloc_f32(kSimtBk * d.pb) : nullptr; |
| 149 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Value* z = kb.ctaid_z(); |
| 150 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 |
Value* A = batch_base_ptr(c, p[0], z, p[10], elem_bytes(dt_a)); |
| 151 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 |
Value* B = batch_base_ptr(c, p[1], z, p[11], elem_bytes(dt_b)); |
| 152 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 |
Value* C = batch_base_ptr(c, p[2], z, p[12], elem_bytes(dt_c)); |
| 153 |
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
55 |
Value* R = has_res ? batch_base_ptr(c, residual_ptr, z, p[12], elem_bytes(dt_c)) : nullptr; |
| 154 |
4/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
55 |
Value* half_n = is_swiglu ? c.b.build_lshr(p[5], c.i32(1)) : p[5]; |
| 155 |
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 52 times.
|
55 |
Value* n_limit = is_swiglu ? half_n : p[5]; |
| 156 |
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
|
55 |
Value* m0 = c.b.build_mul(kb.ctaid_y(), c.i32(static_cast<int>(d.bm))); |
| 157 |
3/6
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 55 times.
✗ Branch 5 not taken.
|
55 |
Value* n0 = c.b.build_mul(kb.ctaid_x(), c.i32(static_cast<int>(d.bn))); |
| 158 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
Value* tid = kb.tid_x(); |
| 159 |
|
|
|
| 160 |
|
165 |
auto load_tiles = [&](Value* k0) -> std::vector<Value*> { |
| 161 |
|
110 |
std::vector<Value*> regs(d.la + d.lb * (is_swiglu ? 2u : 1u)); |
| 162 |
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 110 times.
|
574 |
for (std::uint32_t j = 0; j < d.la; ++j) { |
| 163 |
2/4
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
✗ Branch 3 not taken.
|
464 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 164 |
4/6
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 416 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
|
880 |
Value* m = ta ? c.b.build_umod(e, c.i32(static_cast<int>(d.bm))) |
| 165 |
2/4
✓ Branch 0 taken 416 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 416 times.
✗ Branch 3 not taken.
|
416 |
: c.b.build_lshr(e, c.i32(4)); |
| 166 |
4/6
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 48 times.
✓ Branch 2 taken 48 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 48 times.
✗ Branch 5 not taken.
|
880 |
Value* k = ta ? c.b.build_udiv(e, c.i32(static_cast<int>(d.bm))) |
| 167 |
2/4
✓ Branch 0 taken 416 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 416 times.
✗ Branch 3 not taken.
|
416 |
: c.b.build_and(e, c.i32(15)); |
| 168 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
Value* gm = c.b.build_add(m0, m); |
| 169 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
Value* gk = c.b.build_add(k0, k); |
| 170 |
3/6
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 464 times.
✗ Branch 5 not taken.
|
464 |
Value* ok = c.b.build_and(c.b.build_ult(gm, p[4]), c.b.build_ult(gk, p[6])); |
| 171 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
|
464 |
regs[j] = emit_if_else_values( |
| 172 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
c, ok, {Type::f32()}, |
| 173 |
|
928 |
[&]() -> std::vector<Value*> { |
| 174 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 416 times.
|
464 |
Value* idx = ta ? c.b.build_add(c.b.build_mul(gk, p[7]), gm) |
| 175 |
|
416 |
: c.b.build_add(c.b.build_mul(gm, p[7]), gk); |
| 176 |
|
464 |
return {load_elem_dt(c, dt_a, A, idx)}; |
| 177 |
|
|
}, |
| 178 |
|
928 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 179 |
|
464 |
} |
| 180 |
2/2
✓ Branch 0 taken 464 times.
✓ Branch 1 taken 110 times.
|
574 |
for (std::uint32_t j = 0; j < d.lb; ++j) { |
| 181 |
2/4
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
✗ Branch 3 not taken.
|
464 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 182 |
4/6
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
|
816 |
Value* n = nb ? c.b.build_umod(e, c.i32(static_cast<int>(d.bn))) |
| 183 |
2/4
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
|
352 |
: c.b.build_lshr(e, c.i32(4)); |
| 184 |
4/6
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 352 times.
✓ Branch 2 taken 112 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 112 times.
✗ Branch 5 not taken.
|
816 |
Value* k = nb ? c.b.build_udiv(e, c.i32(static_cast<int>(d.bn))) |
| 185 |
2/4
✓ Branch 0 taken 352 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 352 times.
✗ Branch 3 not taken.
|
352 |
: c.b.build_and(e, c.i32(15)); |
| 186 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
Value* gn = c.b.build_add(n0, n); |
| 187 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
Value* gk = c.b.build_add(k0, k); |
| 188 |
3/6
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 464 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 464 times.
✗ Branch 5 not taken.
|
464 |
Value* ok = c.b.build_and(c.b.build_ult(gn, n_limit), c.b.build_ult(gk, p[6])); |
| 189 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 464 times.
|
464 |
regs[d.la + j] = emit_if_else_values( |
| 190 |
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
|
464 |
c, ok, {Type::f32()}, |
| 191 |
|
928 |
[&]() -> std::vector<Value*> { |
| 192 |
2/2
✓ Branch 0 taken 112 times.
✓ Branch 1 taken 352 times.
|
464 |
Value* idx = nb ? c.b.build_add(c.b.build_mul(gk, p[8]), gn) |
| 193 |
|
352 |
: c.b.build_add(c.b.build_mul(gn, p[8]), gk); |
| 194 |
|
464 |
return {load_elem_dt(c, dt_b, B, idx)}; |
| 195 |
|
|
}, |
| 196 |
|
928 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 197 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 440 times.
|
464 |
if (is_swiglu) { |
| 198 |
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 |
Value* gn_u = c.b.build_add(half_n, gn); |
| 199 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
|
24 |
regs[d.la + d.lb + j] = emit_if_else_values( |
| 200 |
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
|
24 |
c, ok, {Type::f32()}, |
| 201 |
|
48 |
[&]() -> std::vector<Value*> { |
| 202 |
|
24 |
Value* idx = c.b.build_add(c.b.build_mul(gn_u, p[8]), gk); |
| 203 |
|
24 |
return {load_elem_dt(c, dt_b, B, idx)}; |
| 204 |
|
|
}, |
| 205 |
|
48 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 206 |
|
24 |
} |
| 207 |
|
464 |
} |
| 208 |
|
110 |
return regs; |
| 209 |
1/2
✓ Branch 0 taken 110 times.
✗ Branch 1 not taken.
|
110 |
}; |
| 210 |
|
|
|
| 211 |
|
110 |
auto store_tiles = [&](const std::vector<Value*>& regs) { |
| 212 |
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 55 times.
|
287 |
for (std::uint32_t j = 0; j < d.la; ++j) { |
| 213 |
|
232 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 214 |
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 208 times.
|
232 |
Value* idx = ta ? c.b.build_add(c.b.build_mul(c.b.build_udiv(e, c.i32(static_cast<int>(d.bm))), |
| 215 |
|
24 |
c.i32(static_cast<int>(d.pa))), |
| 216 |
|
24 |
c.b.build_umod(e, c.i32(static_cast<int>(d.bm)))) |
| 217 |
|
416 |
: c.b.build_add(c.b.build_mul(c.b.build_and(e, c.i32(15)), c.i32(static_cast<int>(d.pa))), |
| 218 |
|
208 |
c.b.build_lshr(e, c.i32(4))); |
| 219 |
|
232 |
kb.shared_store_f32_indexed(As, idx, regs[j]); |
| 220 |
|
232 |
} |
| 221 |
2/2
✓ Branch 0 taken 232 times.
✓ Branch 1 taken 55 times.
|
287 |
for (std::uint32_t j = 0; j < d.lb; ++j) { |
| 222 |
|
232 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 223 |
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 176 times.
|
232 |
Value* idx = nb ? c.b.build_add(c.b.build_mul(c.b.build_udiv(e, c.i32(static_cast<int>(d.bn))), |
| 224 |
|
56 |
c.i32(static_cast<int>(d.pb))), |
| 225 |
|
56 |
c.b.build_umod(e, c.i32(static_cast<int>(d.bn)))) |
| 226 |
|
352 |
: c.b.build_add(c.b.build_mul(c.b.build_and(e, c.i32(15)), c.i32(static_cast<int>(d.pb))), |
| 227 |
|
176 |
c.b.build_lshr(e, c.i32(4))); |
| 228 |
|
232 |
kb.shared_store_f32_indexed(Bs, idx, regs[d.la + j]); |
| 229 |
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 12 times.
|
232 |
if (is_swiglu) kb.shared_store_f32_indexed(Bs_up, idx, regs[d.la + d.lb + j]); |
| 230 |
|
232 |
} |
| 231 |
|
55 |
}; |
| 232 |
|
|
|
| 233 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
std::vector<Value*> acc = run_simt_k_loop(c, d, p[6], As, Bs, Bs_up, load_tiles, store_tiles); |
| 234 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 |
Value* tx = c.b.build_umod(tid, c.i32(static_cast<int>(d.ntx))); |
| 235 |
2/4
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
|
55 |
Value* ty = c.b.build_udiv(tid, c.i32(static_cast<int>(d.ntx))); |
| 236 |
|
55 |
const std::size_t n_acc = static_cast<std::size_t>(d.tm) * d.tn; |
| 237 |
|
|
|
| 238 |
2/2
✓ Branch 0 taken 55 times.
✓ Branch 1 taken 232 times.
|
287 |
for (std::uint32_t i = 0; i < d.tm; ++i) { |
| 239 |
4/8
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 232 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 232 times.
✗ Branch 7 not taken.
|
464 |
Value* gm = c.b.build_add(m0, c.b.build_add(c.b.build_mul(ty, c.i32(static_cast<int>(d.tm))), |
| 240 |
1/2
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
|
232 |
c.i32(static_cast<int>(i)))); |
| 241 |
3/6
✓ Branch 0 taken 232 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 232 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 232 times.
|
464 |
kb.if_then(c.b.build_ult(gm, p[4]), [&] { |
| 242 |
2/2
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 232 times.
|
1256 |
for (std::uint32_t j = 0; j < d.tn; ++j) { |
| 243 |
|
2048 |
Value* gn = c.b.build_add(n0, c.b.build_add(c.b.build_mul(tx, c.i32(static_cast<int>(d.tn))), |
| 244 |
|
1024 |
c.i32(static_cast<int>(j)))); |
| 245 |
1/2
✓ Branch 0 taken 1024 times.
✗ Branch 1 not taken.
|
2048 |
kb.if_then(c.b.build_ult(gn, n_limit), [&] { |
| 246 |
|
1024 |
const std::size_t idx = static_cast<std::size_t>(i) * d.tn + j; |
| 247 |
|
1024 |
Value* v = acc[idx]; |
| 248 |
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 976 times.
|
1024 |
if (is_swiglu) { |
| 249 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 |
if (has_bias) v = c.b.build_add(v, load_elem_dt(c, dt_bias, p[3], gn)); |
| 250 |
|
48 |
Value* u = acc[n_acc + idx]; |
| 251 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 48 times.
|
48 |
if (has_bias) u = c.b.build_add(u, load_elem_dt(c, dt_bias, p[3], c.b.build_add(half_n, gn))); |
| 252 |
|
48 |
v = c.b.build_mul(c.b.build_mul(v, emit_sigmoid(c, v)), u); |
| 253 |
|
48 |
} else { |
| 254 |
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 464 times.
|
976 |
if (has_bias) v = c.b.build_add(v, load_elem_dt(c, dt_bias, p[3], gn)); |
| 255 |
|
976 |
v = emit_gemm_act(c, act, v); |
| 256 |
|
|
} |
| 257 |
|
1024 |
Value* o = c.b.build_add(c.b.build_mul(gm, p[9]), gn); |
| 258 |
2/2
✓ Branch 0 taken 224 times.
✓ Branch 1 taken 800 times.
|
1024 |
if (epi == EPI_ACCUM) v = c.b.build_add(v, load_elem_dt(c, dt_c, C, o)); |
| 259 |
2/2
✓ Branch 0 taken 752 times.
✓ Branch 1 taken 48 times.
|
800 |
else if (epi == EPI_RESIDUAL_ADD) v = c.b.build_add(v, load_elem_dt(c, dt_c, R, o)); |
| 260 |
|
1024 |
store_elem_dt(c, dt_c, C, o, v); |
| 261 |
|
1024 |
}); |
| 262 |
|
1024 |
} |
| 263 |
|
232 |
}); |
| 264 |
|
232 |
} |
| 265 |
|
|
|
| 266 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
c.b.build_ret_void(); |
| 267 |
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
|
55 |
return finalize_fn(fn); |
| 268 |
|
55 |
} |
| 269 |
|
|
|
| 270 |
|
29 |
brass::Function* build_conv_simt_mir(brass::Module& mod, Dtype dt, bool big128, bool accum, bool has_bias, |
| 271 |
|
|
int kh, int kw, int sh, int sw, int ph, int pw, int dh, int dw, |
| 272 |
|
|
std::uint32_t act) { |
| 273 |
|
29 |
const TileDims d = make_tile_dims(big128); |
| 274 |
|
29 |
const int khw = kh * kw; |
| 275 |
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 27 times.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
|
87 |
const std::string name = std::string("mir_conv_simt_") + (big128 ? "128_" : "64_") + |
| 276 |
5/10
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
|
58 |
std::to_string(kh) + "x" + std::to_string(kw) + |
| 277 |
6/12
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
|
29 |
"_s" + std::to_string(sh) + "x" + std::to_string(sw) + |
| 278 |
6/12
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
|
29 |
"_p" + std::to_string(ph) + "x" + std::to_string(pw) + |
| 279 |
6/12
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
|
29 |
"_d" + std::to_string(dh) + "x" + std::to_string(dw) + |
| 280 |
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
58 |
(accum ? "_ac" : "") + (has_bias ? "_b" : "") + |
| 281 |
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 27 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
29 |
(act != LACT_NONE ? ("_x" + std::to_string(act)) : std::string()); |
| 282 |
13/26
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 29 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 29 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 29 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 29 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 29 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 29 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 29 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 29 times.
✗ Branch 25 not taken.
|
377 |
std::vector<Type> params = { |
| 283 |
|
116 |
Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), |
| 284 |
|
174 |
Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), |
| 285 |
|
87 |
Type::i32(), Type::i32(), Type::i32(), |
| 286 |
|
|
}; |
| 287 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); |
| 288 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
brass::codegen::KernelBuilder kb(mod, fn); |
| 289 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Ctx c(kb); |
| 290 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
auto p = entry_params(c, fn); |
| 291 |
|
|
// p: x(0) w(1) y(2) bias(3) m(4) n(5) k(6) h(7) wd(8) wo(9) cin(10) cout(11) groups(12) |
| 292 |
|
|
|
| 293 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
const int esz = elem_bytes(dt); |
| 294 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* As = kb.shared_alloc_f32(kSimtBk * d.pa); |
| 295 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* Bs = kb.shared_alloc_f32(kSimtBk * d.pb); |
| 296 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* z = kb.ctaid_z(); |
| 297 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* img = c.b.build_udiv(z, p[12]); |
| 298 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* g = c.b.build_umod(z, p[12]); |
| 299 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* cgi = c.b.build_udiv(p[10], p[12]); |
| 300 |
|
|
|
| 301 |
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
58 |
Value* x_chan = c.b.build_add(c.b.build_mul(c.b.build_zext_i64(img), c.b.build_zext_i64(p[10])), |
| 302 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
c.b.build_mul(c.b.build_zext_i64(g), c.b.build_zext_i64(cgi))); |
| 303 |
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
58 |
Value* X = ptr_offset_i64(c, p[0], c.b.build_mul(x_chan, c.b.build_mul(c.b.build_zext_i64(p[7]), |
| 304 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
58 |
c.b.build_zext_i64(p[8]))), esz); |
| 305 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
58 |
Value* A = ptr_offset_i64(c, p[1], c.b.build_mul(c.b.build_zext_i64(g), |
| 306 |
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
58 |
c.b.build_mul(c.b.build_zext_i64(p[4]), |
| 307 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
58 |
c.b.build_zext_i64(p[6]))), esz); |
| 308 |
4/8
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
|
58 |
Value* c_chan = c.b.build_add(c.b.build_mul(c.b.build_zext_i64(img), c.b.build_zext_i64(p[11])), |
| 309 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
c.b.build_mul(c.b.build_zext_i64(g), c.b.build_zext_i64(p[4]))); |
| 310 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
Value* C = ptr_offset_i64(c, p[2], c.b.build_mul(c_chan, c.b.build_zext_i64(p[5])), esz); |
| 311 |
|
|
|
| 312 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
Value* m0 = c.b.build_mul(kb.ctaid_y(), c.i32(static_cast<int>(d.bm))); |
| 313 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
Value* n0 = c.b.build_mul(kb.ctaid_x(), c.i32(static_cast<int>(d.bn))); |
| 314 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* tid = kb.tid_x(); |
| 315 |
|
|
|
| 316 |
3/6
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
|
29 |
Value* px = c.b.build_add(n0, c.b.build_and(tid, c.i32(static_cast<int>(d.bn - 1u)))); |
| 317 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* px_ok = c.b.build_ult(px, p[5]); |
| 318 |
5/10
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
|
29 |
Value* px_ih = c.b.build_sub(c.b.build_mul(c.b.build_udiv(px, p[9]), c.i32(sh)), c.i32(ph)); |
| 319 |
5/10
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 29 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 29 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 29 times.
✗ Branch 9 not taken.
|
29 |
Value* px_iw = c.b.build_sub(c.b.build_mul(c.b.build_umod(px, p[9]), c.i32(sw)), c.i32(pw)); |
| 320 |
|
|
|
| 321 |
|
87 |
auto load_tiles = [&](Value* k0) -> std::vector<Value*> { |
| 322 |
|
58 |
std::vector<Value*> regs(d.la + d.lb); |
| 323 |
2/2
✓ Branch 0 taken 240 times.
✓ Branch 1 taken 58 times.
|
298 |
for (std::uint32_t j = 0; j < d.la; ++j) { |
| 324 |
2/4
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
240 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 325 |
3/6
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
|
240 |
Value* gm = c.b.build_add(m0, c.b.build_lshr(e, c.i32(4))); |
| 326 |
3/6
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
|
240 |
Value* gk = c.b.build_add(k0, c.b.build_and(e, c.i32(15))); |
| 327 |
3/6
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
|
240 |
Value* ok = c.b.build_and(c.b.build_ult(gm, p[4]), c.b.build_ult(gk, p[6])); |
| 328 |
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 240 times.
|
240 |
regs[j] = emit_if_else_values( |
| 329 |
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 |
c, ok, {Type::f32()}, |
| 330 |
|
480 |
[&]() -> std::vector<Value*> { |
| 331 |
|
240 |
return {load_elem_dt(c, dt, A, c.b.build_add(c.b.build_mul(gm, p[6]), gk))}; |
| 332 |
|
|
}, |
| 333 |
|
480 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 334 |
|
240 |
} |
| 335 |
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 240 times.
|
298 |
for (std::uint32_t j = 0; j < d.lb; ++j) { |
| 336 |
2/4
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
240 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 337 |
3/6
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
|
240 |
Value* gk = c.b.build_add(k0, c.b.build_lshr(e, c.i32(d.bn_shift))); |
| 338 |
2/4
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
240 |
Value* ic = c.b.build_udiv(gk, c.i32(khw)); |
| 339 |
2/4
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
|
240 |
Value* t = c.b.build_umod(gk, c.i32(khw)); |
| 340 |
5/10
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 240 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 240 times.
✗ Branch 9 not taken.
|
240 |
Value* ih = c.b.build_add(px_ih, c.b.build_mul(c.b.build_udiv(t, c.i32(kw)), c.i32(dh))); |
| 341 |
5/10
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 240 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 240 times.
✗ Branch 9 not taken.
|
240 |
Value* iw = c.b.build_add(px_iw, c.b.build_mul(c.b.build_umod(t, c.i32(kw)), c.i32(dw))); |
| 342 |
4/8
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 240 times.
✗ Branch 7 not taken.
|
240 |
Value* in_h = c.b.build_and(c.b.build_sge(ih, c.i32(0)), c.b.build_slt(ih, p[7])); |
| 343 |
4/8
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 240 times.
✗ Branch 7 not taken.
|
240 |
Value* in_w = c.b.build_and(c.b.build_sge(iw, c.i32(0)), c.b.build_slt(iw, p[8])); |
| 344 |
4/8
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 240 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 240 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 240 times.
✗ Branch 7 not taken.
|
240 |
Value* ok = c.b.build_and(c.b.build_and(px_ok, c.b.build_ult(gk, p[6])), c.b.build_and(in_h, in_w)); |
| 345 |
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 |
regs[d.la + j] = emit_if_else_values( |
| 346 |
1/2
✓ Branch 0 taken 240 times.
✗ Branch 1 not taken.
|
240 |
c, ok, {Type::f32()}, |
| 347 |
|
480 |
[&]() -> std::vector<Value*> { |
| 348 |
|
240 |
Value* xidx = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(ic, p[7]), ih), p[8]), iw); |
| 349 |
|
240 |
return {load_elem_dt(c, dt, X, xidx)}; |
| 350 |
|
|
}, |
| 351 |
|
480 |
[&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; |
| 352 |
|
240 |
} |
| 353 |
|
58 |
return regs; |
| 354 |
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
|
58 |
}; |
| 355 |
|
|
|
| 356 |
|
58 |
auto store_tiles = [&](const std::vector<Value*>& regs) { |
| 357 |
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 29 times.
|
149 |
for (std::uint32_t j = 0; j < d.la; ++j) { |
| 358 |
|
120 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 359 |
|
240 |
Value* idx = c.b.build_add(c.b.build_mul(c.b.build_and(e, c.i32(15)), c.i32(static_cast<int>(d.pa))), |
| 360 |
|
120 |
c.b.build_lshr(e, c.i32(4))); |
| 361 |
|
120 |
kb.shared_store_f32_indexed(As, idx, regs[j]); |
| 362 |
|
120 |
} |
| 363 |
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 29 times.
|
149 |
for (std::uint32_t j = 0; j < d.lb; ++j) { |
| 364 |
|
120 |
Value* e = c.b.build_add(tid, c.i32(static_cast<int>(j * kSimtWg))); |
| 365 |
|
360 |
Value* idx = c.b.build_add(c.b.build_mul(c.b.build_lshr(e, c.i32(d.bn_shift)), |
| 366 |
|
120 |
c.i32(static_cast<int>(d.pb))), |
| 367 |
|
120 |
c.b.build_and(e, c.i32(static_cast<int>(d.bn - 1u)))); |
| 368 |
|
120 |
kb.shared_store_f32_indexed(Bs, idx, regs[d.la + j]); |
| 369 |
|
120 |
} |
| 370 |
|
29 |
}; |
| 371 |
|
|
|
| 372 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
std::vector<Value*> acc = run_simt_k_loop(c, d, p[6], As, Bs, nullptr, load_tiles, store_tiles); |
| 373 |
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29 |
Value* tx = c.b.build_umod(tid, c.i32(static_cast<int>(d.ntx))); |
| 374 |
2/4
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 29 times.
✗ Branch 3 not taken.
|
29 |
Value* ty = c.b.build_udiv(tid, c.i32(static_cast<int>(d.ntx))); |
| 375 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
Value* gm_bias = c.b.build_mul(g, p[4]); |
| 376 |
|
|
|
| 377 |
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 120 times.
|
149 |
for (std::uint32_t i = 0; i < d.tm; ++i) { |
| 378 |
4/8
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 120 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 120 times.
✗ Branch 7 not taken.
|
240 |
Value* gm = c.b.build_add(m0, c.b.build_add(c.b.build_mul(ty, c.i32(static_cast<int>(d.tm))), |
| 379 |
1/2
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
|
120 |
c.i32(static_cast<int>(i)))); |
| 380 |
3/6
✓ Branch 0 taken 120 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 120 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 120 times.
✗ Branch 5 not taken.
|
240 |
kb.if_then(c.b.build_ult(gm, p[4]), [&] { |
| 381 |
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 12 times.
|
120 |
Value* bv = has_bias ? load_elem_dt(c, dt, p[3], c.b.build_add(gm_bias, gm)) : c.f32(0.0f); |
| 382 |
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 120 times.
|
632 |
for (std::uint32_t j = 0; j < d.tn; ++j) { |
| 383 |
|
1024 |
Value* gn = c.b.build_add(n0, c.b.build_add(c.b.build_mul(tx, c.i32(static_cast<int>(d.tn))), |
| 384 |
|
512 |
c.i32(static_cast<int>(j)))); |
| 385 |
1/2
✓ Branch 0 taken 512 times.
✗ Branch 1 not taken.
|
1024 |
kb.if_then(c.b.build_ult(gn, p[5]), [&] { |
| 386 |
|
512 |
Value* v = c.b.build_add(acc[static_cast<std::size_t>(i) * d.tn + j], bv); |
| 387 |
2/2
✓ Branch 0 taken 480 times.
✓ Branch 1 taken 32 times.
|
512 |
if (act != LACT_NONE) v = emit_gemm_act(c, act, v); |
| 388 |
|
512 |
Value* o = c.b.build_add(c.b.build_mul(gm, p[5]), gn); |
| 389 |
2/2
✓ Branch 0 taken 416 times.
✓ Branch 1 taken 96 times.
|
512 |
if (accum) v = c.b.build_add(v, load_elem_dt(c, dt, C, o)); |
| 390 |
|
512 |
store_elem_dt(c, dt, C, o, v); |
| 391 |
|
512 |
}); |
| 392 |
|
512 |
} |
| 393 |
|
120 |
}); |
| 394 |
|
120 |
} |
| 395 |
|
|
|
| 396 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
c.b.build_ret_void(); |
| 397 |
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
|
29 |
return finalize_fn(fn); |
| 398 |
|
29 |
} |
| 399 |
|
|
|
| 400 |
|
|
} // namespace brotensor::kernels |
| 401 |
|
|
|