src/kernels/mir_conv_direct.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_conv_direct.h" | ||
| 2 | |||
| 3 | #include "mir_common.h" | ||
| 4 | #include "mir_math.h" | ||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <string> | ||
| 8 | |||
| 9 | namespace brotensor::kernels { | ||
| 10 | |||
| 11 | namespace { | ||
| 12 | |||
| 13 | constexpr std::uint32_t kConvDirectFwd = 0; | ||
| 14 | constexpr std::uint32_t kConvDirectTrans = 1; | ||
| 15 | |||
| 16 | 39 | Value* in_bounds(Ctx& c, Value* idx, Value* bound) { | |
| 17 | 39 | return c.b.build_and(c.b.build_sge(idx, c.i32(0)), c.b.build_slt(idx, bound)); | |
| 18 | } | ||
| 19 | |||
| 20 | 13 | Value* conv_direct_fwd(Ctx& c, Dtype dt, const std::vector<Value*>& p, | |
| 21 | Value* ot, Value* oh, Value* ow, Value* oc, | ||
| 22 | Value* cgi, Value* khw, Value* kvol, Value* ivol, Value* xbase, Value* acc0) { | ||
| 23 | // p: [0..3]=x,w,y,bias; [4..15]=n,cin,t,h,wd,cout,to,ho,wo,kt,kh,kw; [16..25]=st,sh,sw,pt,ph,pw,dt,dh,dw,groups | ||
| 24 | 13 | Value* t0 = c.b.build_sub(c.b.build_mul(ot, p[16]), p[19]); | |
| 25 | 13 | Value* h0 = c.b.build_sub(c.b.build_mul(oh, p[17]), p[20]); | |
| 26 | 13 | Value* w0 = c.b.build_sub(c.b.build_mul(ow, p[18]), p[21]); | |
| 27 | 26 | return c.kb.for_range_reduce( | |
| 28 | 13 | c.i32(0), cgi, c.i32(1), acc0, | |
| 29 | 26 | [&](Value* icl, Value* acc_ic) -> Value* { | |
| 30 | 13 | Value* wb = c.b.build_mul(c.b.build_add(c.b.build_mul(oc, cgi), icl), kvol); | |
| 31 | 13 | Value* xb = c.b.build_add(xbase, c.b.build_mul(icl, ivol)); | |
| 32 | 26 | return c.kb.for_range_reduce( | |
| 33 | 13 | c.i32(0), p[13], c.i32(1), acc_ic, | |
| 34 | 26 | [&](Value* a, Value* acc_t) -> Value* { | |
| 35 | 13 | Value* it = c.b.build_add(t0, c.b.build_mul(a, p[22])); | |
| 36 | 13 | return emit_if_else_values( | |
| 37 | 13 | c, in_bounds(c, it, p[6]), {Type::f32()}, | |
| 38 | 26 | [&]() -> std::vector<Value*> { | |
| 39 | 13 | Value* it_h = c.b.build_mul(it, p[7]); | |
| 40 | 13 | Value* wa = c.b.build_add(wb, c.b.build_mul(a, khw)); | |
| 41 | 26 | Value* res = c.kb.for_range_reduce( | |
| 42 | 13 | c.i32(0), p[14], c.i32(1), acc_t, | |
| 43 | 26 | [&](Value* b, Value* acc_h) -> Value* { | |
| 44 | 13 | Value* ih = c.b.build_add(h0, c.b.build_mul(b, p[23])); | |
| 45 | 13 | return emit_if_else_values( | |
| 46 | 13 | c, in_bounds(c, ih, p[7]), {Type::f32()}, | |
| 47 | 26 | [&]() -> std::vector<Value*> { | |
| 48 | 13 | Value* xr = c.b.build_add(xb, c.b.build_mul(c.b.build_add(it_h, ih), p[8])); | |
| 49 | 13 | Value* wr = c.b.build_add(wa, c.b.build_mul(b, p[15])); | |
| 50 | 26 | Value* r_w = c.kb.for_range_reduce( | |
| 51 | 13 | c.i32(0), p[15], c.i32(1), acc_h, | |
| 52 | 26 | [&](Value* kw_i, Value* acc_w) -> Value* { | |
| 53 | 13 | Value* iw = c.b.build_add(w0, c.b.build_mul(kw_i, p[24])); | |
| 54 | 13 | return emit_if_else_values( | |
| 55 | 13 | c, in_bounds(c, iw, p[8]), {Type::f32()}, | |
| 56 | 26 | [&]() -> std::vector<Value*> { | |
| 57 | 13 | Value* xv = load_elem_dt(c, dt, p[0], c.b.build_add(xr, iw)); | |
| 58 | 13 | Value* wv = load_elem_dt(c, dt, p[1], c.b.build_add(wr, kw_i)); | |
| 59 | 13 | return {c.b.build_fma_f32(xv, wv, acc_w)}; | |
| 60 | }, | ||
| 61 | 26 | [&]() -> std::vector<Value*> { return {acc_w}; })[0]; | |
| 62 | ✗ | }); | |
| 63 | 13 | return {r_w}; | |
| 64 | ✗ | }, | |
| 65 | 26 | [&]() -> std::vector<Value*> { return {acc_h}; })[0]; | |
| 66 | ✗ | }); | |
| 67 | 13 | return {res}; | |
| 68 | ✗ | }, | |
| 69 | 26 | [&]() -> std::vector<Value*> { return {acc_t}; })[0]; | |
| 70 | ✗ | }); | |
| 71 | ✗ | }); | |
| 72 | ✗ | } | |
| 73 | |||
| 74 | 6 | Value* conv_direct_trans(Ctx& c, Dtype dt, const std::vector<Value*>& p, | |
| 75 | Value* oh, Value* ow, Value* g, Value* ocl, | ||
| 76 | Value* cgi, Value* cgo, Value* khw, Value* ivol, Value* xbase, Value* acc0) { | ||
| 77 | 6 | Value* g_base = c.b.build_mul(g, cgi); | |
| 78 | 12 | return c.kb.for_range_reduce( | |
| 79 | 6 | c.i32(0), p[14], c.i32(1), acc0, | |
| 80 | 12 | [&](Value* b, Value* acc_h) -> Value* { | |
| 81 | 6 | Value* nh = c.b.build_sub(c.b.build_add(oh, p[20]), c.b.build_mul(b, p[23])); | |
| 82 | 6 | Value* nh_pos = c.b.build_sge(nh, c.i32(0)); | |
| 83 | 6 | Value* nh_div = c.b.build_eq(c.b.build_umod(nh, p[17]), c.i32(0)); | |
| 84 | 6 | Value* ih = c.b.build_udiv(nh, p[17]); | |
| 85 | 6 | Value* h_ok = c.b.build_and(c.b.build_and(nh_pos, nh_div), c.b.build_slt(ih, p[7])); | |
| 86 | 6 | return emit_if_else_values( | |
| 87 | 6 | c, h_ok, {Type::f32()}, | |
| 88 | 12 | [&]() -> std::vector<Value*> { | |
| 89 | 6 | Value* ih_wd = c.b.build_mul(ih, p[8]); | |
| 90 | 6 | Value* bk = c.b.build_mul(b, p[15]); | |
| 91 | 12 | Value* r_w = c.kb.for_range_reduce( | |
| 92 | 6 | c.i32(0), p[15], c.i32(1), acc_h, | |
| 93 | 12 | [&](Value* kw_i, Value* acc_w) -> Value* { | |
| 94 | 6 | Value* nw = c.b.build_sub(c.b.build_add(ow, p[21]), c.b.build_mul(kw_i, p[24])); | |
| 95 | 6 | Value* nw_pos = c.b.build_sge(nw, c.i32(0)); | |
| 96 | 6 | Value* nw_div = c.b.build_eq(c.b.build_umod(nw, p[18]), c.i32(0)); | |
| 97 | 6 | Value* iw = c.b.build_udiv(nw, p[18]); | |
| 98 | 6 | Value* w_ok = c.b.build_and(c.b.build_and(nw_pos, nw_div), c.b.build_slt(iw, p[8])); | |
| 99 | 6 | return emit_if_else_values( | |
| 100 | 6 | c, w_ok, {Type::f32()}, | |
| 101 | 12 | [&]() -> std::vector<Value*> { | |
| 102 | 6 | Value* xo = c.b.build_add(xbase, c.b.build_add(ih_wd, iw)); | |
| 103 | 6 | Value* wk_off = c.b.build_add(bk, kw_i); | |
| 104 | 12 | Value* r_ic = c.kb.for_range_reduce( | |
| 105 | 6 | c.i32(0), cgi, c.i32(1), acc_w, | |
| 106 | 12 | [&](Value* icl, Value* acc_ic) -> Value* { | |
| 107 | 12 | Value* wi = c.b.build_add( | |
| 108 | 12 | c.b.build_mul( | |
| 109 | 6 | c.b.build_add(c.b.build_mul(c.b.build_add(g_base, icl), cgo), ocl), | |
| 110 | 6 | khw), | |
| 111 | 6 | wk_off); | |
| 112 | 6 | Value* xv = load_elem_dt(c, dt, p[0], c.b.build_add(xo, c.b.build_mul(icl, ivol))); | |
| 113 | 6 | Value* wv = load_elem_dt(c, dt, p[1], wi); | |
| 114 | 6 | return c.b.build_fma_f32(xv, wv, acc_ic); | |
| 115 | }); | ||
| 116 | 6 | return {r_ic}; | |
| 117 | ✗ | }, | |
| 118 | 12 | [&]() -> std::vector<Value*> { return {acc_w}; })[0]; | |
| 119 | ✗ | }); | |
| 120 | 6 | return {r_w}; | |
| 121 | ✗ | }, | |
| 122 | 12 | [&]() -> std::vector<Value*> { return {acc_h}; })[0]; | |
| 123 | ✗ | }); | |
| 124 | ✗ | } | |
| 125 | |||
| 126 | } // namespace | ||
| 127 | |||
| 128 | 19 | brass::Function* build_conv_direct_mir(brass::Module& mod, std::uint32_t mode_in, Dtype dt, bool has_bias) { | |
| 129 | 19 | const std::uint32_t mode = mode_in & 0x0fu; | |
| 130 | 19 | const std::uint32_t act = (mode_in >> 4) & 0x0fu; | |
| 131 | 19 | if (mode != kConvDirectFwd && mode != kConvDirectTrans) { | |
| 132 | ✗ | throw std::runtime_error("brotensor: mir_conv_direct: unknown mode"); | |
| 133 | } | ||
| 134 | 57 | const std::string name = std::string("mir_conv_direct_") + | |
| 135 | 38 | (mode == kConvDirectFwd ? "fwd_" : "trans_") + | |
| 136 | 19 | (dt == Dtype::FP32 ? "f32" : dt == Dtype::FP16 ? "f16" : "bf16") + | |
| 137 | 38 | (has_bias ? "_b" : "") + | |
| 138 | 19 | (act != LACT_NONE ? ("_x" + std::to_string(act)) : std::string()); | |
| 139 | 19 | std::vector<Type> params(26, Type::i32()); | |
| 140 | 19 | params[0] = Type::ptr(); | |
| 141 | 19 | params[1] = Type::ptr(); | |
| 142 | 19 | params[2] = Type::ptr(); | |
| 143 | 19 | params[3] = Type::ptr(); | |
| 144 | 19 | Function* fn = mod.create_function(name, Type::void_type(), brass::Span<const Type>(params)); | |
| 145 | 19 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 146 | 19 | Ctx c(kb); | |
| 147 | 19 | auto p = entry_params(c, fn); | |
| 148 | |||
| 149 | 19 | Value* plane = c.b.build_mul(p[10], c.b.build_mul(p[11], p[12])); | |
| 150 | 19 | Value* total = c.b.build_mul(p[4], c.b.build_mul(p[9], plane)); | |
| 151 | 19 | Value* cgi = c.b.build_udiv(p[5], p[25]); | |
| 152 | 19 | Value* cgo = c.b.build_udiv(p[9], p[25]); | |
| 153 | 19 | Value* khw = c.b.build_mul(p[14], p[15]); | |
| 154 | 19 | Value* kvol = c.b.build_mul(p[13], khw); | |
| 155 | 19 | Value* ivol = c.b.build_mul(p[6], c.b.build_mul(p[7], p[8])); | |
| 156 | 19 | Value* hw_o = c.b.build_mul(p[12], p[11]); | |
| 157 | |||
| 158 | 38 | kb.for_range(kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* idx) { | |
| 159 | 19 | Value* o = c.b.build_umod(idx, plane); | |
| 160 | 19 | Value* rest = c.b.build_udiv(idx, plane); | |
| 161 | 19 | Value* oc = c.b.build_umod(rest, p[9]); | |
| 162 | 19 | Value* img = c.b.build_udiv(rest, p[9]); | |
| 163 | 19 | Value* ow = c.b.build_umod(o, p[12]); | |
| 164 | 19 | Value* oh = c.b.build_umod(c.b.build_udiv(o, p[12]), p[11]); | |
| 165 | 19 | Value* ot = c.b.build_udiv(o, hw_o); | |
| 166 | 19 | Value* g = c.b.build_udiv(oc, cgo); | |
| 167 | 19 | Value* ocl = c.b.build_umod(oc, cgo); | |
| 168 | 19 | Value* acc0 = has_bias ? load_elem_dt(c, dt, p[3], oc) : c.f32(0.0f); | |
| 169 | 19 | Value* xbase = c.b.build_mul(c.b.build_add(c.b.build_mul(img, p[5]), c.b.build_mul(g, cgi)), ivol); | |
| 170 | |||
| 171 | 19 | Value* acc = (mode == kConvDirectFwd) | |
| 172 | 13 | ? conv_direct_fwd(c, dt, p, ot, oh, ow, oc, cgi, khw, kvol, ivol, xbase, acc0) | |
| 173 | 6 | : conv_direct_trans(c, dt, p, oh, ow, g, ocl, cgi, cgo, khw, ivol, xbase, acc0); | |
| 174 | 19 | if (act != LACT_NONE) acc = emit_gemm_act(c, act, acc); | |
| 175 | 19 | store_elem_dt(c, dt, p[2], idx, acc); | |
| 176 | 19 | }); | |
| 177 | |||
| 178 | 19 | c.b.build_ret_void(); | |
| 179 | 19 | return finalize_fn(fn); | |
| 180 | 19 | } | |
| 181 | |||
| 182 | } // namespace brotensor::kernels | ||
| 183 |