src/kernels/mir_resample_bwd.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_resample_bwd.h" | ||
| 2 | |||
| 3 | #include "mir_resample.h" | ||
| 4 | #include "../vulkan/shaders/op_codes.h" | ||
| 5 | |||
| 6 | #include <string> | ||
| 7 | #include <utility> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | namespace brotensor::kernels { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | 26 | Value* interp_weight(Ctx& c, Value* o, Value* i, Value* in_dim, Value* out_dim, std::uint32_t imode) { | |
| 15 | 78 | auto [num, den] = emit_src_rational(c, o, in_dim, out_dim, false); | |
| 16 | 78 | auto [i0, rem] = emit_floor_div(c, num, den); | |
| 17 | 26 | Value* last = c.b.build_sub(in_dim, c.i32(1)); | |
| 18 | 26 | Value* zero = c.i32(0); | |
| 19 | 26 | if (imode == 0) { | |
| 20 | 16 | Value* twice = c.b.build_mul(rem, c.kb.const_i64(2)); | |
| 21 | 16 | Value* gt = c.b.build_sgt(twice, den); | |
| 22 | 16 | Value* lt = c.b.build_slt(twice, den); | |
| 23 | 24 | Value* tie = c.b.build_add(i0, c.b.build_and(i0, c.i32(1))); | |
| 24 | 24 | Value* r = c.b.build_select(gt, c.b.build_add(i0, c.i32(1)), c.b.build_select(lt, i0, tie)); | |
| 25 | 8 | Value* hit = c.b.build_eq(clamp_i32(c, r, zero, last), i); | |
| 26 | 8 | return c.b.build_select(hit, c.f32(1.0f), c.f32(0.0f)); | |
| 27 | } | ||
| 28 | 54 | Value* f = c.b.build_sdiv(c.b.build_sitofp_f32_i64(rem), c.b.build_sitofp_f32_i64(den)); | |
| 29 | 54 | Value* w0 = c.b.build_select(c.b.build_eq(clamp_i32(c, i0, zero, last), i), c.b.build_sub(c.f32(1.0f), f), | |
| 30 | 18 | c.f32(0.0f)); | |
| 31 | 54 | Value* w1 = c.b.build_select(c.b.build_eq(clamp_i32(c, c.b.build_add(i0, c.i32(1)), zero, last), i), f, | |
| 32 | 18 | c.f32(0.0f)); | |
| 33 | 18 | return c.b.build_add(w0, w1); | |
| 34 | 26 | } | |
| 35 | |||
| 36 | 26 | std::pair<Value*, Value*> interp_range(Ctx& c, Value* i, Value* in_dim, Value* out_dim) { | |
| 37 | 26 | Value* in2 = c.b.build_mul(c.kb.const_i64(2), c.b.build_zext_i64(in_dim)); | |
| 38 | 26 | Value* in64 = c.b.build_zext_i64(in_dim); | |
| 39 | 26 | Value* out64 = c.b.build_zext_i64(out_dim); | |
| 40 | 26 | Value* i64_v = c.b.build_sext_i64(i); | |
| 41 | 52 | Value* lo_num = c.b.build_sub( | |
| 42 | 26 | c.b.build_mul(c.b.build_sub(c.b.build_mul(i64_v, c.kb.const_i64(2)), c.kb.const_i64(2)), out64), in64); | |
| 43 | 52 | Value* hi_num = c.b.build_sub( | |
| 44 | 26 | c.b.build_mul(c.b.build_add(c.b.build_mul(i64_v, c.kb.const_i64(2)), c.kb.const_i64(3)), out64), in64); | |
| 45 | 26 | Value* lo_raw = c.b.build_sub(c.b.build_trunc_i32(c.b.build_sdiv(lo_num, in2)), c.i32(1)); | |
| 46 | 26 | Value* hi_raw = c.b.build_add(c.b.build_trunc_i32(c.b.build_sdiv(hi_num, in2)), c.i32(1)); | |
| 47 | 26 | lo_raw = c.b.build_select(c.b.build_eq(i, c.i32(0)), c.i32(0), lo_raw); | |
| 48 | 26 | Value* last_out = c.b.build_sub(out_dim, c.i32(1)); | |
| 49 | 26 | hi_raw = c.b.build_select(c.b.build_eq(i, c.b.build_sub(in_dim, c.i32(1))), last_out, hi_raw); | |
| 50 | 52 | return {c.b.build_select(c.b.build_sgt(lo_raw, c.i32(0)), lo_raw, c.i32(0)), | |
| 51 | 26 | c.b.build_select(c.b.build_slt(hi_raw, last_out), hi_raw, last_out)}; | |
| 52 | } | ||
| 53 | |||
| 54 | 8 | std::pair<Value*, Value*> adaptive_win(Ctx& c, Value* o, Value* in_dim, Value* out_dim) { | |
| 55 | 8 | Value* lo = c.b.build_udiv(c.b.build_mul(o, in_dim), out_dim); | |
| 56 | 16 | Value* hi_num = c.b.build_add(c.b.build_mul(c.b.build_add(o, c.i32(1)), in_dim), | |
| 57 | 8 | c.b.build_sub(out_dim, c.i32(1))); | |
| 58 | 8 | return {lo, c.b.build_udiv(hi_num, out_dim)}; | |
| 59 | } | ||
| 60 | |||
| 61 | 18 | std::vector<std::pair<Value*, Value*>> pad_runs(Ctx& c, Value* i, Value* len, Value* pad_lo, Value* lp, | |
| 62 | std::uint32_t pad_mode) { | ||
| 63 | 18 | std::vector<std::pair<Value*, Value*>> runs; | |
| 64 | 18 | Value* interior = c.b.build_add(i, pad_lo); | |
| 65 | 18 | runs.emplace_back(interior, c.b.build_add(interior, c.i32(1))); | |
| 66 | 18 | if (pad_mode == 2) { | |
| 67 | 2 | Value* left_ok = c.b.build_and(c.b.build_eq(i, c.i32(0)), c.b.build_sgt(pad_lo, c.i32(0))); | |
| 68 | 2 | runs.emplace_back(c.i32(0), c.b.build_select(left_ok, pad_lo, c.i32(0))); | |
| 69 | 2 | Value* end_i = c.b.build_add(pad_lo, len); | |
| 70 | 2 | Value* right_ok = c.b.build_and(c.b.build_eq(i, c.b.build_sub(len, c.i32(1))), c.b.build_slt(end_i, lp)); | |
| 71 | 2 | runs.emplace_back(end_i, c.b.build_select(right_ok, lp, end_i)); | |
| 72 | 18 | } else if (pad_mode == 1) { | |
| 73 | 8 | Value* len_gt1 = c.b.build_sgt(len, c.i32(1)); | |
| 74 | 8 | Value* left = c.b.build_sub(pad_lo, i); | |
| 75 | 16 | Value* left_ok = c.b.build_and( | |
| 76 | 8 | len_gt1, c.b.build_and(c.b.build_sge(i, c.i32(1)), c.b.build_sge(left, c.i32(0)))); | |
| 77 | 8 | runs.emplace_back(left, c.b.build_select(left_ok, c.b.build_add(left, c.i32(1)), left)); | |
| 78 | 8 | Value* right = c.b.build_sub(c.b.build_add(pad_lo, c.b.build_mul(c.b.build_sub(len, c.i32(1)), c.i32(2))), i); | |
| 79 | 16 | Value* right_ok = c.b.build_and( | |
| 80 | 8 | len_gt1, | |
| 81 | 8 | c.b.build_and(c.b.build_sle(i, c.b.build_sub(len, c.i32(2))), c.b.build_slt(right, lp))); | |
| 82 | 8 | runs.emplace_back(right, c.b.build_select(right_ok, c.b.build_add(right, c.i32(1)), right)); | |
| 83 | 8 | } | |
| 84 | 18 | return runs; | |
| 85 | 18 | } | |
| 86 | |||
| 87 | } // namespace | ||
| 88 | |||
| 89 | 30 | brass::Function* build_resample_bwd_mir(brass::Module& mod, std::uint32_t mode, Dtype dt, std::uint32_t imode) { | |
| 90 | 90 | const std::string name = "mir_resample_bwd_" + std::to_string(mode) + "_dt" + | |
| 91 | 60 | std::to_string(static_cast<int>(dt)) + "_i" + std::to_string(imode); | |
| 92 | 60 | auto* fn = mod.create_function( | |
| 93 | 30 | name, Type::void_type(), | |
| 94 | 90 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), | |
| 95 | 60 | Type::i32(), Type::i32()}); | |
| 96 | 30 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 97 | 30 | Ctx c(kb); | |
| 98 | 30 | auto p = entry_params(c, fn); | |
| 99 | 30 | Value *dy = p[0], *dx = p[1], *aux = p[2]; | |
| 100 | 30 | Value *planes = p[3], *hi = p[4], *wi = p[5], *ho = p[6], *wo = p[7], *pt = p[8], *pl = p[9]; | |
| 101 | |||
| 102 | 30 | Value* plane_i = c.b.build_mul(hi, wi); | |
| 103 | 30 | Value* plane_o = c.b.build_mul(ho, wo); | |
| 104 | |||
| 105 | 30 | if (mode == RB_MAXPOOL) { | |
| 106 | 8 | c.kb.for_range(c.kb.global_tid_x(), planes, grid_stride_ew(c), [&](Value* pl_idx) { | |
| 107 | 4 | Value* xb = c.b.build_mul(pl_idx, plane_i); | |
| 108 | 4 | Value* yb = c.b.build_mul(pl_idx, plane_o); | |
| 109 | 8 | c.kb.for_range(c.i32(0), plane_i, c.i32(1), [&](Value* i) { | |
| 110 | 4 | c.kb.store_f32(ptr_at(c, dx, c.b.build_add(xb, i), 4), c.f32(0.0f)); | |
| 111 | 4 | }); | |
| 112 | 8 | c.kb.for_range(c.i32(0), plane_o, c.i32(1), [&](Value* o) { | |
| 113 | 4 | Value* y_idx = c.b.build_add(yb, o); | |
| 114 | 4 | Value* idx = c.kb.load_i32(ptr_at(c, aux, y_idx, 4)); | |
| 115 | 4 | Value* valid = c.b.build_and(c.b.build_sge(idx, c.i32(0)), c.b.build_slt(idx, plane_i)); | |
| 116 | 8 | c.kb.if_then(valid, [&] { | |
| 117 | 4 | Value* dst_ptr = ptr_at(c, dx, c.b.build_add(xb, idx), 4); | |
| 118 | 4 | Value* g = load_elem_dt(c, dt, dy, y_idx); | |
| 119 | 4 | c.kb.store_f32(dst_ptr, c.b.build_add(c.kb.load_f32(dst_ptr), g)); | |
| 120 | 4 | }); | |
| 121 | 4 | }); | |
| 122 | 4 | }); | |
| 123 | 4 | c.b.build_ret_void(); | |
| 124 | 4 | return finalize_fn(fn); | |
| 125 | } | ||
| 126 | |||
| 127 | 26 | Value* total = c.b.build_mul(planes, plane_i); | |
| 128 | 52 | c.kb.for_range(c.kb.global_tid_x(), total, grid_stride_ew(c), [&](Value* e) { | |
| 129 | 26 | Value* pl_idx = c.b.build_udiv(e, plane_i); | |
| 130 | 26 | Value* pos = c.b.build_umod(e, plane_i); | |
| 131 | 26 | Value* h = c.b.build_udiv(pos, wi); | |
| 132 | 26 | Value* w = c.b.build_umod(pos, wi); | |
| 133 | 26 | Value* yb = c.b.build_mul(pl_idx, plane_o); | |
| 134 | 26 | Value* acc = c.f32(0.0f); | |
| 135 | |||
| 136 | 26 | if (mode == RB_INTERP) { | |
| 137 | 39 | auto [ylo, yhi] = interp_range(c, h, hi, ho); | |
| 138 | 39 | auto [xlo, xhi] = interp_range(c, w, wi, wo); | |
| 139 | 26 | acc = c.kb.for_range_reduce( | |
| 140 | 65 | ylo, c.b.build_add(yhi, c.i32(1)), c.i32(1), c.f32(0.0f), [&](Value* oh, Value* h_acc) { | |
| 141 | 13 | Value* wy = interp_weight(c, oh, h, hi, ho, imode); | |
| 142 | 13 | Value* row_b = c.b.build_add(yb, c.b.build_mul(oh, wo)); | |
| 143 | 13 | return emit_if_else_values( | |
| 144 | 13 | c, c.b.build_ne(wy, c.f32(0.0f)), {Type::f32()}, | |
| 145 | 26 | [&]() -> std::vector<Value*> { | |
| 146 | 26 | Value* row = c.kb.for_range_reduce( | |
| 147 | 13 | xlo, c.b.build_add(xhi, c.i32(1)), c.i32(1), c.f32(0.0f), | |
| 148 | 26 | [&](Value* ow, Value* w_acc) { | |
| 149 | 13 | Value* wx = interp_weight(c, ow, w, wi, wo, imode); | |
| 150 | 13 | return emit_if_else_values( | |
| 151 | 13 | c, c.b.build_ne(wx, c.f32(0.0f)), {Type::f32()}, | |
| 152 | 26 | [&]() -> std::vector<Value*> { | |
| 153 | 26 | Value* term = c.b.build_mul( | |
| 154 | 13 | wx, load_elem_dt(c, dt, dy, c.b.build_add(row_b, ow))); | |
| 155 | 13 | return {c.b.build_add(w_acc, term)}; | |
| 156 | }, | ||
| 157 | 26 | [&]() -> std::vector<Value*> { return {w_acc}; })[0]; | |
| 158 | ✗ | }); | |
| 159 | 13 | return {c.b.build_add(h_acc, c.b.build_mul(wy, row))}; | |
| 160 | ✗ | }, | |
| 161 | 26 | [&]() -> std::vector<Value*> { return {h_acc}; })[0]; | |
| 162 | ✗ | }); | |
| 163 | 26 | } else if (mode == RB_ADAPTIVE) { | |
| 164 | 12 | auto range1d = [&](Value* coord, Value* in_d, Value* out_d) -> std::pair<Value*, Value*> { | |
| 165 | 8 | Value* l = c.b.build_sub(c.b.build_udiv(c.b.build_mul(coord, out_d), in_d), c.i32(1)); | |
| 166 | 16 | Value* r = c.b.build_udiv( | |
| 167 | 8 | c.b.build_add(c.b.build_mul(c.b.build_add(coord, c.i32(1)), out_d), c.b.build_sub(in_d, c.i32(1))), | |
| 168 | 8 | in_d); | |
| 169 | 16 | return {c.b.build_select(c.b.build_sgt(l, c.i32(0)), l, c.i32(0)), | |
| 170 | 16 | c.b.build_select(c.b.build_slt(c.b.build_add(r, c.i32(1)), out_d), | |
| 171 | 8 | c.b.build_add(r, c.i32(1)), out_d)}; | |
| 172 | }; | ||
| 173 | 12 | auto [ylo, yhi_end] = range1d(h, hi, ho); | |
| 174 | 12 | auto [xlo, xhi_end] = range1d(w, wi, wo); | |
| 175 | 24 | acc = c.kb.for_range_reduce(ylo, yhi_end, c.i32(1), c.f32(0.0f), [&](Value* oh, Value* h_acc) { | |
| 176 | 20 | auto [h0, h1] = adaptive_win(c, oh, hi, ho); | |
| 177 | 12 | Value* h_in = c.b.build_and(c.b.build_sge(h, h0), c.b.build_slt(h, h1)); | |
| 178 | 4 | return emit_if_else_values( | |
| 179 | 4 | c, h_in, {Type::f32()}, | |
| 180 | 16 | [&]() -> std::vector<Value*> { | |
| 181 | 4 | Value* dh = c.b.build_sub(h1, h0); | |
| 182 | 4 | Value* row_b = c.b.build_add(yb, c.b.build_mul(oh, wo)); | |
| 183 | 8 | Value* next_h = c.kb.for_range_reduce( | |
| 184 | 8 | xlo, xhi_end, c.i32(1), h_acc, [&](Value* ow, Value* w_acc) { | |
| 185 | 20 | auto [w0, w1] = adaptive_win(c, ow, wi, wo); | |
| 186 | 12 | Value* w_in = c.b.build_and(c.b.build_sge(w, w0), c.b.build_slt(w, w1)); | |
| 187 | 4 | return emit_if_else_values( | |
| 188 | 4 | c, w_in, {Type::f32()}, | |
| 189 | 16 | [&]() -> std::vector<Value*> { | |
| 190 | 4 | Value* area = c.kb.i32_to_f32(c.b.build_mul(dh, c.b.build_sub(w1, w0))); | |
| 191 | 8 | Value* term = c.b.build_sdiv( | |
| 192 | 4 | load_elem_dt(c, dt, dy, c.b.build_add(row_b, ow)), area); | |
| 193 | 4 | return {c.b.build_add(w_acc, term)}; | |
| 194 | }, | ||
| 195 | 8 | [&]() -> std::vector<Value*> { return {w_acc}; })[0]; | |
| 196 | ✗ | }); | |
| 197 | 4 | return {next_h}; | |
| 198 | ✗ | }, | |
| 199 | 8 | [&]() -> std::vector<Value*> { return {h_acc}; })[0]; | |
| 200 | ✗ | }); | |
| 201 | 4 | } else { | |
| 202 | 9 | auto ry = pad_runs(c, h, hi, pt, ho, imode); | |
| 203 | 9 | auto rx = pad_runs(c, w, wi, pl, wo, imode); | |
| 204 | 28 | for (const auto& yr : ry) { | |
| 205 | 38 | acc = c.kb.for_range_reduce(yr.first, yr.second, c.i32(1), acc, [&](Value* oy, Value* y_acc) { | |
| 206 | 19 | Value* row_b = c.b.build_add(yb, c.b.build_mul(oy, wo)); | |
| 207 | 19 | Value* cur = y_acc; | |
| 208 | 68 | for (const auto& xr : rx) { | |
| 209 | 98 | cur = c.kb.for_range_reduce(xr.first, xr.second, c.i32(1), cur, [&](Value* ox, Value* x_acc) { | |
| 210 | 49 | return c.b.build_add(x_acc, load_elem_dt(c, dt, dy, c.b.build_add(row_b, ox))); | |
| 211 | }); | ||
| 212 | } | ||
| 213 | 19 | return cur; | |
| 214 | ✗ | }); | |
| 215 | } | ||
| 216 | 9 | } | |
| 217 | 26 | store_elem_dt(c, dt, dx, e, acc); | |
| 218 | 26 | }); | |
| 219 | |||
| 220 | 26 | c.b.build_ret_void(); | |
| 221 | 26 | return finalize_fn(fn); | |
| 222 | 30 | } | |
| 223 | |||
| 224 | } // namespace brotensor::kernels | ||
| 225 |