src/kernels/mir_filtered_lrelu.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_filtered_lrelu.h" | ||
| 2 | #include "mir_common.h" | ||
| 3 | |||
| 4 | #include <cstdint> | ||
| 5 | #include <vector> | ||
| 6 | |||
| 7 | namespace brotensor::kernels { | ||
| 8 | |||
| 9 | namespace { | ||
| 10 | |||
| 11 | constexpr int kFlTh = 16; | ||
| 12 | constexpr int kFlTw = 16; | ||
| 13 | constexpr int kFlThreads = kFlTh * kFlTw; | ||
| 14 | |||
| 15 | 9 | Value* alloc_fl_stage(Ctx& c, Dtype dt, std::uint32_t count) { | |
| 16 | 9 | return (dt == Dtype::FP32) ? c.kb.shared_alloc_f32(count) : c.kb.shared_alloc_u16(count); | |
| 17 | } | ||
| 18 | |||
| 19 | 70 | Value* load_fl_stage(Ctx& c, Dtype dt, Value* buf, Value* idx) { | |
| 20 | 70 | if (dt == Dtype::FP32) return c.kb.shared_load_f32_indexed(buf, idx); | |
| 21 | 9 | Value* raw = c.kb.shared_load_u16_indexed(buf, idx); | |
| 22 | 9 | return (dt == Dtype::FP16) ? c.kb.f16_to_f32(raw) : c.kb.bf16_to_f32(raw); | |
| 23 | 70 | } | |
| 24 | |||
| 25 | 9 | void store_fl_stage(Ctx& c, Dtype dt, Value* buf, Value* idx, Value* val) { | |
| 26 | 9 | if (dt == Dtype::FP32) { | |
| 27 | 8 | c.kb.shared_store_f32_indexed(buf, idx, val); | |
| 28 | 8 | return; | |
| 29 | } | ||
| 30 | 1 | Value* raw = (dt == Dtype::FP16) ? c.kb.f32_to_f16(val) : c.kb.f32_to_bf16(val); | |
| 31 | 1 | c.kb.shared_store_u16_indexed(buf, idx, raw); | |
| 32 | 9 | } | |
| 33 | |||
| 34 | 3 | Function* build_polyu_fn(Module& mod, Dtype dt) { | |
| 35 | 6 | Function* fn = mod.create_function( | |
| 36 | 3 | "mir_fl_polyu", Type::void_type(), | |
| 37 | 3 | {Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32()}); | |
| 38 | 3 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 39 | 3 | Ctx c(kb); | |
| 40 | 3 | auto p = entry_params(c, fn); | |
| 41 | // p: fu(0) poly(1) fuH(2) fuW(3) up(4) JyMax(5) JxMax(6) | ||
| 42 | 3 | Value* total = c.b.build_mul(c.b.build_mul(p[4], p[4]), c.b.build_mul(p[5], p[6])); | |
| 43 | 3 | Value* idx = kb.global_tid_x(); | |
| 44 | 6 | kb.if_then(c.b.build_ult(idx, total), [&] { | |
| 45 | 3 | Value* i = c.b.build_umod(idx, p[6]); | |
| 46 | 3 | Value* t0 = c.b.build_udiv(idx, p[6]); | |
| 47 | 3 | Value* j = c.b.build_umod(t0, p[5]); | |
| 48 | 3 | Value* t1 = c.b.build_udiv(t0, p[5]); | |
| 49 | 3 | Value* rx = c.b.build_umod(t1, p[4]); | |
| 50 | 3 | Value* ry = c.b.build_udiv(t1, p[4]); | |
| 51 | 3 | Value* fuy = c.b.build_add(ry, c.b.build_mul(j, p[4])); | |
| 52 | 3 | Value* fux = c.b.build_add(rx, c.b.build_mul(i, p[4])); | |
| 53 | 3 | Value* ok = c.b.build_and(c.b.build_slt(fuy, p[2]), c.b.build_slt(fux, p[3])); | |
| 54 | 3 | Value* val = emit_if_else_values( | |
| 55 | 3 | c, ok, {Type::f32()}, | |
| 56 | 6 | [&]() -> std::vector<Value*> { | |
| 57 | 3 | Value* r = c.b.build_sub(c.b.build_sub(p[2], c.i32(1)), fuy); | |
| 58 | 3 | Value* col = c.b.build_sub(c.b.build_sub(p[3], c.i32(1)), fux); | |
| 59 | 3 | Value* off = c.b.build_add(c.b.build_mul(r, p[3]), col); | |
| 60 | 3 | return {load_elem_dt(c, dt, p[0], off)}; | |
| 61 | }, | ||
| 62 | 6 | [&]() -> std::vector<Value*> { return {c.f32(0.0f)}; })[0]; | |
| 63 | 3 | store_elem_dt(c, dt, p[1], idx, val); | |
| 64 | 3 | }); | |
| 65 | 3 | c.b.build_ret_void(); | |
| 66 | 3 | return finalize_fn(fn); | |
| 67 | 3 | } | |
| 68 | |||
| 69 | 3 | Function* build_polyd_fn(Module& mod, Dtype dt) { | |
| 70 | 6 | Function* fn = mod.create_function( | |
| 71 | 3 | "mir_fl_polyd", Type::void_type(), | |
| 72 | 3 | {Type::ptr(), Type::ptr(), Type::i32(), Type::i32()}); | |
| 73 | 3 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 74 | 3 | Ctx c(kb); | |
| 75 | 3 | auto p = entry_params(c, fn); | |
| 76 | // p: fd(0) poly(1) fdH(2) fdW(3) | ||
| 77 | 3 | Value* total = c.b.build_mul(p[2], p[3]); | |
| 78 | 3 | Value* idx = kb.global_tid_x(); | |
| 79 | 6 | kb.if_then(c.b.build_ult(idx, total), [&] { | |
| 80 | 3 | Value* fdx = c.b.build_umod(idx, p[3]); | |
| 81 | 3 | Value* fdy = c.b.build_udiv(idx, p[3]); | |
| 82 | 3 | Value* r = c.b.build_sub(c.b.build_sub(p[2], c.i32(1)), fdy); | |
| 83 | 3 | Value* col = c.b.build_sub(c.b.build_sub(p[3], c.i32(1)), fdx); | |
| 84 | 3 | Value* val = load_elem_dt(c, dt, p[0], c.b.build_add(c.b.build_mul(r, p[3]), col)); | |
| 85 | 3 | store_elem_dt(c, dt, p[1], idx, val); | |
| 86 | 3 | }); | |
| 87 | 3 | c.b.build_ret_void(); | |
| 88 | 3 | return finalize_fn(fn); | |
| 89 | 3 | } | |
| 90 | |||
| 91 | 9 | Function* build_main_fn(Module& mod, Dtype dt, bool has_bias, bool has_clamp, std::uint64_t aux) { | |
| 92 | 9 | const int up = static_cast<int>(aux & 0xffu); | |
| 93 | 9 | const int down = static_cast<int>((aux >> 8) & 0xffu); | |
| 94 | 9 | const int JyMax = static_cast<int>((aux >> 16) & 0xffu); | |
| 95 | 9 | const int JxMax = static_cast<int>((aux >> 24) & 0xffu); | |
| 96 | 9 | const int fdH = static_cast<int>((aux >> 32) & 0xffu); | |
| 97 | 9 | const int fdW = static_cast<int>((aux >> 40) & 0xffu); | |
| 98 | 9 | const int AH = (kFlTh - 1) * down + fdH; | |
| 99 | 9 | const int AW = (kFlTw - 1) * down + fdW; | |
| 100 | 9 | const int acells = AH * AW; | |
| 101 | 9 | const int bank = JyMax * JxMax; | |
| 102 | 9 | const int esz = elem_bytes(dt); | |
| 103 | |||
| 104 | 18 | Function* fn = mod.create_function( | |
| 105 | 9 | "mir_filtered_lrelu", Type::void_type(), | |
| 106 | 108 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 107 | 63 | Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 108 | 36 | Type::f32(), Type::f32(), Type::f32(), Type::f32()}); | |
| 109 | 9 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 110 | 9 | Ctx c(kb); | |
| 111 | 9 | auto p = entry_params(c, fn); | |
| 112 | // ptrs: X(0) poly_u(1) poly_d(2) b(3) Y(4) | ||
| 113 | // ints: C(5) H(6) W(7) Hout(8) Wout(9) px0(10) py0(11) | ||
| 114 | // flts: up_gain(12) gain(13) slope(14) clamp_v(15) | ||
| 115 | |||
| 116 | 9 | Value* act = alloc_fl_stage(c, dt, static_cast<std::uint32_t>(acells)); | |
| 117 | |||
| 118 | 9 | Value* nc = kb.ctaid_z(); | |
| 119 | 9 | Value* oh0 = c.b.build_mul(kb.ctaid_y(), c.i32(kFlTh)); | |
| 120 | 9 | Value* ow0 = c.b.build_mul(kb.ctaid_x(), c.i32(kFlTw)); | |
| 121 | 9 | Value* ay0 = c.b.build_mul(oh0, c.i32(down)); | |
| 122 | 9 | Value* ax0 = c.b.build_mul(ow0, c.i32(down)); | |
| 123 | 9 | Value* tid = kb.tid_x(); | |
| 124 | |||
| 125 | 9 | Value* H = p[6]; | |
| 126 | 9 | Value* W = p[7]; | |
| 127 | 9 | Value* Hout = p[8]; | |
| 128 | 9 | Value* Wout = p[9]; | |
| 129 | 9 | Value* px0 = p[10]; | |
| 130 | 9 | Value* py0 = p[11]; | |
| 131 | |||
| 132 | 9 | Value* nc64 = c.b.build_zext_i64(nc); | |
| 133 | 9 | Value* hw64 = c.b.build_mul(c.b.build_zext_i64(H), c.b.build_zext_i64(W)); | |
| 134 | 9 | Value* x_base = c.b.build_add(p[0], c.b.build_mul(nc64, c.b.build_mul(hw64, kb.const_i64(esz)))); | |
| 135 | 9 | Value* hwo64 = c.b.build_mul(c.b.build_zext_i64(Hout), c.b.build_zext_i64(Wout)); | |
| 136 | 9 | Value* y_base = c.b.build_add(p[4], c.b.build_mul(nc64, c.b.build_mul(hwo64, kb.const_i64(esz)))); | |
| 137 | |||
| 138 | 9 | Value* bias_v = c.f32(0.0f); | |
| 139 | 9 | if (has_bias) { | |
| 140 | 8 | Value* ch = c.b.build_umod(nc, p[5]); | |
| 141 | 8 | bias_v = load_elem_dt(c, dt, p[3], ch); | |
| 142 | 8 | } | |
| 143 | |||
| 144 | 9 | const bool up_pow2 = (up & (up - 1)) == 0; | |
| 145 | 9 | const int up_shift = up_pow2 ? log2_exact(up) : 0; | |
| 146 | |||
| 147 | // Phase A: compute shared activation tile. | ||
| 148 | 18 | kb.for_range(tid, c.i32(acells), c.i32(kFlThreads), [&](Value* a) { | |
| 149 | 9 | Value* ly = c.b.build_udiv(a, c.i32(AW)); | |
| 150 | 9 | Value* lx = c.b.build_umod(a, c.i32(AW)); | |
| 151 | 9 | Value* ay = c.b.build_add(ay0, ly); | |
| 152 | 9 | Value* ax = c.b.build_add(ax0, lx); | |
| 153 | |||
| 154 | 9 | Value* ry = nullptr; | |
| 155 | 9 | Value* rx = nullptr; | |
| 156 | 9 | Value* iyb = nullptr; | |
| 157 | 9 | Value* ixb = nullptr; | |
| 158 | 9 | if (up == 1) { | |
| 159 | 2 | ry = c.i32(0); | |
| 160 | 2 | rx = c.i32(0); | |
| 161 | 2 | iyb = c.b.build_sub(ay, py0); | |
| 162 | 2 | ixb = c.b.build_sub(ax, px0); | |
| 163 | 9 | } else if (up_pow2) { | |
| 164 | 7 | ry = c.b.build_and(c.b.build_sub(py0, ay), c.i32(up - 1)); | |
| 165 | 7 | rx = c.b.build_and(c.b.build_sub(px0, ax), c.i32(up - 1)); | |
| 166 | 7 | iyb = c.b.build_ashr(c.b.build_sub(c.b.build_add(ay, ry), py0), c.i32(up_shift)); | |
| 167 | 7 | ixb = c.b.build_ashr(c.b.build_sub(c.b.build_add(ax, rx), px0), c.i32(up_shift)); | |
| 168 | 7 | } else { | |
| 169 | ✗ | Value* dy = c.b.build_smod(c.b.build_sub(py0, ay), c.i32(up)); | |
| 170 | ✗ | Value* dx = c.b.build_smod(c.b.build_sub(px0, ax), c.i32(up)); | |
| 171 | ✗ | ry = c.b.build_select(c.b.build_slt(dy, c.i32(0)), c.b.build_add(dy, c.i32(up)), dy); | |
| 172 | ✗ | rx = c.b.build_select(c.b.build_slt(dx, c.i32(0)), c.b.build_add(dx, c.i32(up)), dx); | |
| 173 | ✗ | iyb = c.b.build_sdiv(c.b.build_sub(c.b.build_add(ay, ry), py0), c.i32(up)); | |
| 174 | ✗ | ixb = c.b.build_sdiv(c.b.build_sub(c.b.build_add(ax, rx), px0), c.i32(up)); | |
| 175 | } | ||
| 176 | |||
| 177 | 9 | Value* jlo = smax_i32(c, c.i32(0), c.b.build_neg(iyb)); | |
| 178 | 9 | Value* jhi = smin_i32(c, c.i32(JyMax), c.b.build_sub(H, iyb)); | |
| 179 | 9 | Value* ilo = smax_i32(c, c.i32(0), c.b.build_neg(ixb)); | |
| 180 | 9 | Value* ihi = smin_i32(c, c.i32(JxMax), c.b.build_sub(W, ixb)); | |
| 181 | |||
| 182 | 9 | Value* bank_off = c.b.build_mul(c.b.build_add(c.b.build_mul(ry, c.i32(up)), rx), c.i32(bank)); | |
| 183 | 18 | Value* u = kb.for_range_reduce(jlo, jhi, c.i32(1), c.f32(0.0f), [&](Value* j, Value* uj) { | |
| 184 | 9 | Value* x_row_base = c.b.build_add(c.b.build_mul(c.b.build_add(iyb, j), W), ixb); | |
| 185 | 9 | Value* p_row_base = c.b.build_add(bank_off, c.b.build_mul(j, c.i32(JxMax))); | |
| 186 | 18 | return kb.for_range_reduce(ilo, ihi, c.i32(1), uj, [&](Value* i, Value* ui) { | |
| 187 | 9 | Value* xv = load_elem_dt(c, dt, x_base, c.b.build_add(x_row_base, i)); | |
| 188 | 9 | Value* pv = load_elem_dt(c, dt, p[1], c.b.build_add(p_row_base, i)); | |
| 189 | 9 | Value* xvb = has_bias ? c.b.build_add(xv, bias_v) : xv; | |
| 190 | 9 | return c.b.build_fma_f32(pv, xvb, ui); | |
| 191 | }); | ||
| 192 | ✗ | }); | |
| 193 | |||
| 194 | 9 | Value* ug = c.b.build_mul(u, p[12]); | |
| 195 | 9 | Value* act0 = c.b.build_select(c.b.build_sgt(ug, c.f32(0.0f)), ug, c.b.build_mul(ug, p[14])); | |
| 196 | 9 | Value* av = c.b.build_mul(act0, p[13]); | |
| 197 | 9 | if (has_clamp) { | |
| 198 | 5 | av = kb.clamp_f32(av, c.b.build_neg(p[15]), p[15]); | |
| 199 | 5 | } | |
| 200 | 9 | store_fl_stage(c, dt, act, a, av); | |
| 201 | 9 | }); | |
| 202 | 9 | kb.sync(); | |
| 203 | |||
| 204 | // Phase B: one thread per output pixel in the 16x16 tile; downsample from shared. | ||
| 205 | 9 | Value* ly_out = c.b.build_lshr(tid, c.i32(4)); | |
| 206 | 9 | Value* lx_out = c.b.build_and(tid, c.i32(15)); | |
| 207 | 9 | Value* oh = c.b.build_add(oh0, ly_out); | |
| 208 | 9 | Value* ow = c.b.build_add(ow0, lx_out); | |
| 209 | 18 | kb.if_then(c.b.build_and(c.b.build_slt(oh, Hout), c.b.build_slt(ow, Wout)), [&] { | |
| 210 | 9 | Value* ly0 = c.b.build_mul(ly_out, c.i32(down)); | |
| 211 | 9 | Value* lx0 = c.b.build_mul(lx_out, c.i32(down)); | |
| 212 | 9 | Value* act_base = c.b.build_add(c.b.build_mul(ly0, c.i32(AW)), lx0); | |
| 213 | |||
| 214 | 9 | Value* acc = c.f32(0.0f); | |
| 215 | 9 | if (fdH * fdW <= 64) { | |
| 216 | 25 | for (int fdy = 0; fdy < fdH; ++fdy) { | |
| 217 | 86 | for (int fdx = 0; fdx < fdW; ++fdx) { | |
| 218 | 67 | Value* a_val = load_fl_stage(c, dt, act, c.b.build_add(act_base, c.i32(fdy * AW + fdx))); | |
| 219 | 67 | Value* p_val = load_elem_dt(c, dt, p[2], c.i32(fdy * fdW + fdx)); | |
| 220 | 67 | acc = c.b.build_fma_f32(p_val, a_val, acc); | |
| 221 | 67 | } | |
| 222 | 19 | } | |
| 223 | 6 | } else { | |
| 224 | 6 | acc = kb.for_range_reduce(c.i32(0), c.i32(fdH), c.i32(1), c.f32(0.0f), [&](Value* fdy, Value* ay_acc) { | |
| 225 | 3 | Value* ar = c.b.build_add(act_base, c.b.build_mul(fdy, c.i32(AW))); | |
| 226 | 3 | Value* pr = c.b.build_mul(fdy, c.i32(fdW)); | |
| 227 | 6 | return kb.for_range_reduce(c.i32(0), c.i32(fdW), c.i32(1), ay_acc, [&](Value* fdx, Value* ax_acc) { | |
| 228 | 3 | Value* a_val = load_fl_stage(c, dt, act, c.b.build_add(ar, fdx)); | |
| 229 | 3 | Value* p_val = load_elem_dt(c, dt, p[2], c.b.build_add(pr, fdx)); | |
| 230 | 3 | return c.b.build_fma_f32(p_val, a_val, ax_acc); | |
| 231 | }); | ||
| 232 | ✗ | }); | |
| 233 | } | ||
| 234 | 9 | Value* out_idx = c.b.build_add(c.b.build_mul(oh, Wout), ow); | |
| 235 | 9 | store_elem_dt(c, dt, y_base, out_idx, acc); | |
| 236 | 9 | }); | |
| 237 | 9 | c.b.build_ret_void(); | |
| 238 | 9 | return finalize_fn(fn); | |
| 239 | 9 | } | |
| 240 | |||
| 241 | } // namespace | ||
| 242 | |||
| 243 | 15 | Function* build_filtered_lrelu_mir(Module& mod, std::uint32_t mode, Dtype dt, | |
| 244 | bool has_bias, bool has_clamp, std::uint64_t aux) { | ||
| 245 | 15 | if (mode == 0u) return build_polyu_fn(mod, dt); | |
| 246 | 12 | if (mode == 1u) return build_polyd_fn(mod, dt); | |
| 247 | 9 | return build_main_fn(mod, dt, has_bias, has_clamp, aux); | |
| 248 | 15 | } | |
| 249 | |||
| 250 | } // namespace brotensor::kernels | ||
| 251 |