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 | 8 | Ctx c(kb); | |
| 15 | 8 | auto p = entry_params(c, fn); | |
| 16 | 8 | const int esz = elem_bytes(dt); | |
| 17 | 8 | Value* s_val = kb.shared_alloc_f32(8); | |
| 18 | 8 | Value* tid = kb.tid_x(); | |
| 19 | 8 | Value* n_vecs = vec_count(c, p[3], lanes); | |
| 20 | 8 | Value* block = c.i32(static_cast<int>(kReduceBlock)); | |
| 21 | |||
| 22 | 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 | 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 | 26 | for (int j = 0; j < lanes; ++j) { | |
| 28 | 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 | 8 | if (mean) total = c.b.build_mul(total, c.b.build_sdiv(c.f32(1.0f), kb.i32_to_f32(p[3]))); | |
| 34 | 16 | kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] { | |
| 35 | 8 | store_lanes(c, dt, 1, ptr_at(c, p[1], row, esz), {total}); | |
| 36 | 8 | }); | |
| 37 | 8 | }); | |
| 38 | 8 | c.b.build_ret_void(); | |
| 39 | 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 | 11 | Ctx c(kb); | |
| 47 | 11 | auto p = entry_params(c, fn); | |
| 48 | 11 | const int esz = elem_bytes(dt); | |
| 49 | 11 | Value* s_val = kb.shared_alloc_f32(8); | |
| 50 | 11 | Value* s_idx = kb.shared_alloc_i32(8); | |
| 51 | 11 | Value* tid = kb.tid_x(); | |
| 52 | 11 | Value* n_vecs = vec_count(c, p[3], lanes); | |
| 53 | 11 | Value* block = c.i32(static_cast<int>(kReduceBlock)); | |
| 54 | |||
| 55 | 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 | 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 | 38 | for (int j = 0; j < lanes; ++j) { | |
| 64 | 27 | Value* vj = xs[static_cast<std::size_t>(j)]; | |
| 65 | 27 | Value* col = (j == 0) ? col0 : c.b.build_add(col0, c.i32(j)); | |
| 66 | 27 | Value* better = c.b.build_sgt(vj, bv); | |
| 67 | 27 | bv = c.b.build_select(better, vj, bv); | |
| 68 | 27 | bi = c.b.build_select(better, col, bi); | |
| 69 | 27 | } | |
| 70 | 11 | return std::vector<Value*>{bv, bi}; | |
| 71 | 11 | }); | |
| 72 | 11 | Value* win = emit_block_argmax_f32(c, s_val, s_idx, res[0], res[1]); | |
| 73 | 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 | 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 | 11 | c.b.build_ret_void(); | |
| 83 | 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 | 7 | Ctx c(kb); | |
| 91 | 7 | auto p = entry_params(c, fn); | |
| 92 | 7 | const int esz = elem_bytes(dt); | |
| 93 | |||
| 94 | 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 | 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 | 7 | std::vector<Value*> next(static_cast<std::size_t>(lanes)); | |
| 101 | 24 | for (int j = 0; j < lanes; ++j) { | |
| 102 | 17 | const auto js = static_cast<std::size_t>(j); | |
| 103 | 17 | next[js] = c.b.build_add(cur[js], xs[js]); | |
| 104 | 17 | } | |
| 105 | 7 | return next; | |
| 106 | 7 | }); | |
| 107 | 7 | store_lanes(c, dt, lanes, ptr_at(c, p[1], col, esz), sums); | |
| 108 | 7 | }); | |
| 109 | 7 | c.b.build_ret_void(); | |
| 110 | 7 | return finalize_fn(fn); | |
| 111 | 7 | } | |
| 112 | |||
| 113 | } // namespace brotensor::kernels | ||
| 114 |