src/kernels/mir_remap.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_remap.h" | ||
| 2 | |||
| 3 | #include "../vulkan/shaders/op_codes.h" | ||
| 4 | |||
| 5 | #include <functional> | ||
| 6 | #include <string> | ||
| 7 | |||
| 8 | namespace brotensor::kernels { | ||
| 9 | |||
| 10 | namespace { | ||
| 11 | |||
| 12 | constexpr int kRemapCpt = 16; | ||
| 13 | |||
| 14 | 23 | Value* load_raw(Ctx& c, Value* base, Value* elem_idx, int es) { | |
| 15 | 23 | Value* ptr = ptr_at(c, base, elem_idx, es); | |
| 16 | 23 | return es == 2 ? c.kb.load_u16(ptr) : c.kb.load_i32(ptr); | |
| 17 | } | ||
| 18 | |||
| 19 | 23 | void store_raw(Ctx& c, Value* base, Value* elem_idx, Value* val, int es) { | |
| 20 | 23 | Value* ptr = ptr_at(c, base, elem_idx, es); | |
| 21 | 23 | if (es == 2) { | |
| 22 | 8 | c.kb.store_u16(ptr, val); | |
| 23 | 8 | } else { | |
| 24 | 15 | c.kb.store_i32(ptr, val); | |
| 25 | } | ||
| 26 | 23 | } | |
| 27 | |||
| 28 | 14 | Value* cond_load_raw(Ctx& c, Value* cond, Value* ptr, const std::function<Value*()>& make_idx, int es) { | |
| 29 | 14 | return emit_if_else_values( | |
| 30 | 14 | c, cond, {Type::i32()}, | |
| 31 | 28 | [&]() -> std::vector<Value*> { return {load_raw(c, ptr, make_idx(), es)}; }, | |
| 32 | 28 | [&]() -> std::vector<Value*> { return {c.i32(0)}; })[0]; | |
| 33 | ✗ | } | |
| 34 | |||
| 35 | 18 | Value* emit_pad_src(Ctx& c, Value* rel, Value* len, Value* pad_mode) { | |
| 36 | 18 | Value* in_bounds = c.b.build_and(c.b.build_sge(rel, c.i32(0)), c.b.build_slt(rel, len)); | |
| 37 | 18 | Value* len_m1 = c.b.build_sub(len, c.i32(1)); | |
| 38 | 18 | Value* rep = c.b.build_select(c.b.build_slt(rel, c.i32(0)), c.i32(0), len_m1); | |
| 39 | |||
| 40 | 18 | Value* per_raw = c.b.build_mul(len_m1, c.i32(2)); | |
| 41 | 18 | Value* period = c.b.build_select(c.b.build_sgt(per_raw, c.i32(0)), per_raw, c.i32(1)); | |
| 42 | 18 | Value* q_raw = c.b.build_smod(rel, period); | |
| 43 | 18 | Value* q = c.b.build_select(c.b.build_slt(q_raw, c.i32(0)), c.b.build_add(q_raw, period), q_raw); | |
| 44 | 18 | Value* ref_raw = c.b.build_select(c.b.build_slt(q, len), q, c.b.build_sub(period, q)); | |
| 45 | 18 | Value* refl = c.b.build_select(c.b.build_eq(len, c.i32(1)), c.i32(0), ref_raw); | |
| 46 | |||
| 47 | 36 | Value* oob = c.b.build_select( | |
| 48 | 18 | c.b.build_eq(pad_mode, c.i32(0)), c.i32(-1), | |
| 49 | 18 | c.b.build_select(c.b.build_eq(pad_mode, c.i32(2)), rep, refl)); | |
| 50 | 18 | return c.b.build_select(in_bounds, rel, oob); | |
| 51 | } | ||
| 52 | |||
| 53 | } // namespace | ||
| 54 | |||
| 55 | 23 | brass::Function* build_remap_mir(brass::Module& mod, std::uint32_t mode, int elem_bytes) { | |
| 56 | 46 | auto* fn = mod.create_function( | |
| 57 | 23 | "mir_remap_" + std::to_string(mode) + "_b" + std::to_string(elem_bytes), Type::void_type(), | |
| 58 | 207 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 59 | 184 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}); | |
| 60 | 23 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 61 | 23 | Ctx c(kb); | |
| 62 | 23 | auto p = entry_params(c, fn); | |
| 63 | 23 | Value *x = p[0], *y = p[1], *idx = p[2]; | |
| 64 | 23 | Value *c_out = p[3], *cin = p[4], *hi = p[5], *wi = p[6], *ho = p[7], *wo = p[8]; | |
| 65 | 23 | Value *a0 = p[9], *a1 = p[10], *a2 = p[11], *a3 = p[12], *a4 = p[13], *a5 = p[14], *a6 = p[15]; | |
| 66 | |||
| 67 | 23 | Value* plane_o = c.b.build_mul(ho, wo); | |
| 68 | 23 | Value* plane_i = c.b.build_mul(hi, wi); | |
| 69 | 23 | Value* img = c.kb.ctaid_z(); | |
| 70 | 23 | Value* c0 = c.b.build_mul(c.kb.ctaid_y(), c.i32(kRemapCpt)); | |
| 71 | 46 | Value* c1 = c.b.build_select(c.b.build_ult(c.b.build_add(c0, c.i32(kRemapCpt)), c_out), | |
| 72 | 23 | c.b.build_add(c0, c.i32(kRemapCpt)), c_out); | |
| 73 | |||
| 74 | 46 | c.kb.for_range(c.kb.global_tid_x(), plane_o, grid_stride_ew(c), [&](Value* pos) { | |
| 75 | 23 | Value* oh = c.b.build_udiv(pos, wo); | |
| 76 | 23 | Value* ow = c.b.build_umod(pos, wo); | |
| 77 | 23 | Value* obase = c.b.build_add(c.b.build_mul(c.b.build_mul(img, c_out), plane_o), pos); | |
| 78 | |||
| 79 | 23 | if (mode == RM_PAD || mode == RM_SLICE || mode == RM_UP2 || mode == RM_WIN_PART || mode == RM_WIN_REV) { | |
| 80 | 11 | Value *sh = nullptr, *sw = nullptr, *ibase = nullptr; | |
| 81 | 11 | if (mode == RM_PAD) { | |
| 82 | 6 | sh = emit_pad_src(c, c.b.build_sub(oh, a0), hi, a2); | |
| 83 | 6 | sw = emit_pad_src(c, c.b.build_sub(ow, a1), wi, a2); | |
| 84 | 6 | ibase = c.b.build_mul(c.b.build_mul(img, cin), plane_i); | |
| 85 | 11 | } else if (mode == RM_SLICE) { | |
| 86 | 1 | sh = c.b.build_add(oh, a0); | |
| 87 | 1 | sw = c.b.build_add(ow, a1); | |
| 88 | 1 | ibase = c.b.build_mul(c.b.build_mul(img, cin), plane_i); | |
| 89 | 5 | } else if (mode == RM_UP2) { | |
| 90 | 2 | sh = c.b.build_lshr(oh, c.i32(1)); | |
| 91 | 2 | sw = c.b.build_lshr(ow, c.i32(1)); | |
| 92 | 2 | ibase = c.b.build_mul(c.b.build_mul(img, cin), plane_i); | |
| 93 | 4 | } else if (mode == RM_WIN_PART) { | |
| 94 | 1 | Value* per = c.b.build_mul(a1, a2); | |
| 95 | 1 | Value* n = c.b.build_udiv(img, per); | |
| 96 | 1 | Value* r = c.b.build_umod(img, per); | |
| 97 | 1 | sh = c.b.build_add(c.b.build_mul(c.b.build_udiv(r, a2), a0), oh); | |
| 98 | 1 | sw = c.b.build_add(c.b.build_mul(c.b.build_umod(r, a2), a0), ow); | |
| 99 | 1 | ibase = c.b.build_mul(c.b.build_mul(n, cin), plane_i); | |
| 100 | 1 | } else { | |
| 101 | 1 | Value* win = a0; | |
| 102 | 2 | Value* b_win = c.b.build_add( | |
| 103 | 1 | c.b.build_mul(c.b.build_add(c.b.build_mul(img, a1), c.b.build_udiv(oh, win)), a2), | |
| 104 | 1 | c.b.build_udiv(ow, win)); | |
| 105 | 1 | sh = c.b.build_umod(oh, win); | |
| 106 | 1 | sw = c.b.build_umod(ow, win); | |
| 107 | 1 | ibase = c.b.build_mul(c.b.build_mul(b_win, cin), plane_i); | |
| 108 | } | ||
| 109 | 11 | Value* ok = c.b.build_and(c.b.build_sge(sh, c.i32(0)), c.b.build_sge(sw, c.i32(0))); | |
| 110 | 11 | Value* ip = c.b.build_add(c.b.build_mul(sh, wi), sw); | |
| 111 | 22 | c.kb.for_range(c0, c1, c.i32(1), [&](Value* ch) { | |
| 112 | 33 | Value* v = cond_load_raw(c, ok, x, [&] { | |
| 113 | 11 | return c.b.build_add(c.b.build_add(ibase, c.b.build_mul(ch, plane_i)), ip); | |
| 114 | 11 | }, elem_bytes); | |
| 115 | 11 | store_raw(c, y, c.b.build_add(obase, c.b.build_mul(ch, plane_o)), v, elem_bytes); | |
| 116 | 11 | }); | |
| 117 | 23 | } else if (mode == RM_UNFOLD) { | |
| 118 | 3 | Value* kK = c.b.build_mul(a0, a1); | |
| 119 | 3 | Value* yb = c.b.build_sub(c.b.build_mul(oh, a2), a4); | |
| 120 | 3 | Value* xb = c.b.build_sub(c.b.build_mul(ow, a3), a5); | |
| 121 | 6 | c.kb.for_range(c0, c1, c.i32(1), [&](Value* ch) { | |
| 122 | 3 | Value* ci = c.b.build_udiv(ch, kK); | |
| 123 | 3 | Value* k = c.b.build_umod(ch, kK); | |
| 124 | 3 | Value* sy = emit_pad_src(c, c.b.build_add(yb, c.b.build_udiv(k, a1)), hi, a6); | |
| 125 | 3 | Value* sx = emit_pad_src(c, c.b.build_add(xb, c.b.build_umod(k, a1)), wi, a6); | |
| 126 | 3 | Value* ok = c.b.build_and(c.b.build_sge(sy, c.i32(0)), c.b.build_sge(sx, c.i32(0))); | |
| 127 | 9 | Value* v = cond_load_raw(c, ok, x, [&] { | |
| 128 | 3 | Value* row_b = c.b.build_mul(c.b.build_add(c.b.build_mul(img, cin), ci), plane_i); | |
| 129 | 3 | return c.b.build_add(row_b, c.b.build_add(c.b.build_mul(sy, wi), sx)); | |
| 130 | 3 | }, elem_bytes); | |
| 131 | 3 | store_raw(c, y, c.b.build_add(obase, c.b.build_mul(ch, plane_o)), v, elem_bytes); | |
| 132 | 3 | }); | |
| 133 | 12 | } else if (mode == RM_MERGE2) { | |
| 134 | 2 | c.kb.for_range(c0, c1, c.i32(1), [&](Value* ch) { | |
| 135 | 1 | Value* ch_maj = c.b.build_ne(a0, c.i32(0)); | |
| 136 | 1 | Value* blk = c.b.build_select(ch_maj, c.b.build_and(ch, c.i32(3)), c.b.build_udiv(ch, cin)); | |
| 137 | 1 | Value* ci = c.b.build_select(ch_maj, c.b.build_lshr(ch, c.i32(2)), c.b.build_umod(ch, cin)); | |
| 138 | 1 | Value* ih = c.b.build_add(c.b.build_mul(oh, c.i32(2)), c.b.build_lshr(blk, c.i32(1))); | |
| 139 | 1 | Value* iw = c.b.build_add(c.b.build_mul(ow, c.i32(2)), c.b.build_and(blk, c.i32(1))); | |
| 140 | 2 | Value* src_i = c.b.build_add( | |
| 141 | 1 | c.b.build_mul(c.b.build_add(c.b.build_mul(img, cin), ci), plane_i), | |
| 142 | 1 | c.b.build_add(c.b.build_mul(ih, wi), iw)); | |
| 143 | 2 | store_raw(c, y, c.b.build_add(obase, c.b.build_mul(ch, plane_o)), load_raw(c, x, src_i, elem_bytes), | |
| 144 | 1 | elem_bytes); | |
| 145 | 1 | }); | |
| 146 | 9 | } else if (mode == RM_PSHUF) { | |
| 147 | 2 | Value* i = c.b.build_and(oh, c.i32(1)); | |
| 148 | 2 | Value* j = c.b.build_and(ow, c.i32(1)); | |
| 149 | 2 | Value* ij = c.b.build_add(c.b.build_mul(i, c.i32(2)), j); | |
| 150 | 2 | Value* ip = c.b.build_add(c.b.build_mul(c.b.build_lshr(oh, c.i32(1)), wi), c.b.build_lshr(ow, c.i32(1))); | |
| 151 | 4 | c.kb.for_range(c0, c1, c.i32(1), [&](Value* ch) { | |
| 152 | 2 | Value* sc = c.b.build_udiv(c.b.build_add(c.b.build_mul(ch, c.i32(4)), ij), a0); | |
| 153 | 2 | Value* src_i = c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(img, cin), sc), plane_i), ip); | |
| 154 | 4 | store_raw(c, y, c.b.build_add(obase, c.b.build_mul(ch, plane_o)), load_raw(c, x, src_i, elem_bytes), | |
| 155 | 2 | elem_bytes); | |
| 156 | 2 | }); | |
| 157 | 8 | } else if (mode == RM_GATHER_ROWS) { | |
| 158 | 2 | Value* r = clamp_i32(c, c.kb.load_i32(ptr_at(c, idx, img, 4)), c.i32(0), c.b.build_sub(a0, c.i32(1))); | |
| 159 | 2 | Value* v = load_raw(c, x, c.b.build_add(c.b.build_mul(r, wo), pos), elem_bytes); | |
| 160 | 2 | store_raw(c, y, c.b.build_add(c.b.build_mul(img, wo), pos), v, elem_bytes); | |
| 161 | 6 | } else if (mode == RM_SCATTER_ROWS) { | |
| 162 | 3 | Value* r = c.kb.load_i32(ptr_at(c, idx, img, 4)); | |
| 163 | 6 | c.kb.if_then(c.b.build_and(c.b.build_sge(r, c.i32(0)), c.b.build_slt(r, a0)), [&] { | |
| 164 | 3 | Value* v = load_raw(c, x, c.b.build_add(c.b.build_mul(img, wo), pos), elem_bytes); | |
| 165 | 3 | store_raw(c, y, c.b.build_add(c.b.build_mul(r, wo), pos), v, elem_bytes); | |
| 166 | 3 | }); | |
| 167 | 3 | } else { | |
| 168 | 1 | Value* P = a0; | |
| 169 | 1 | Value* PP = c.b.build_mul(P, P); | |
| 170 | 1 | Value* tok = c.b.build_add(c.b.build_mul(c.b.build_udiv(oh, P), a3), c.b.build_udiv(ow, P)); | |
| 171 | 1 | Value* blk = c.b.build_add(c.b.build_mul(c.b.build_umod(oh, P), P), c.b.build_umod(ow, P)); | |
| 172 | 1 | Value* row = c.b.build_mul(c.b.build_mul(tok, PP), a1); | |
| 173 | 1 | Value* ch_maj = c.b.build_ne(a2, c.i32(0)); | |
| 174 | 2 | c.kb.for_range(c0, c1, c.i32(1), [&](Value* ch) { | |
| 175 | 2 | Value* col = c.b.build_select( | |
| 176 | 1 | ch_maj, c.b.build_add(c.b.build_mul(ch, PP), blk), | |
| 177 | 1 | c.b.build_add(c.b.build_mul(blk, a1), ch)); | |
| 178 | 2 | store_raw(c, y, c.b.build_add(obase, c.b.build_mul(ch, plane_o)), | |
| 179 | 1 | load_raw(c, x, c.b.build_add(row, col), elem_bytes), elem_bytes); | |
| 180 | 1 | }); | |
| 181 | } | ||
| 182 | 23 | }); | |
| 183 | |||
| 184 | 23 | c.b.build_ret_void(); | |
| 185 | 23 | return finalize_fn(fn); | |
| 186 | 23 | } | |
| 187 | |||
| 188 | } // namespace brotensor::kernels | ||
| 189 |