src/kernels/mir_norm.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #include "mir_norm.h" | ||
| 2 | #include "mir_common.h" | ||
| 3 | #include "mir_math.h" | ||
| 4 | |||
| 5 | #include "../vulkan/shaders/op_codes.h" | ||
| 6 | |||
| 7 | #include <functional> | ||
| 8 | #include <vector> | ||
| 9 | |||
| 10 | namespace brotensor::kernels { | ||
| 11 | |||
| 12 | namespace { | ||
| 13 | |||
| 14 | struct NormCtx { | ||
| 15 | Ctx& c; | ||
| 16 | const std::vector<Value*>& p; | ||
| 17 | std::uint32_t mode; | ||
| 18 | Dtype dt_x, dt_y, dt_g; | ||
| 19 | int esz_x, esz_y, esz_g, lanes; | ||
| 20 | NormMirFlags flags; | ||
| 21 | Value* tid; | ||
| 22 | Value* block; | ||
| 23 | Value* n_vecs; | ||
| 24 | Value* invd; | ||
| 25 | Value* d_f; | ||
| 26 | Value* eps_safe; | ||
| 27 | Value* s_red; | ||
| 28 | }; | ||
| 29 | |||
| 30 | 47 | Value* norm_rsqrt(Ctx& c, Value* arg) { | |
| 31 | 47 | return emit_rsqrt(c, c.kb.fmax(arg, c.f32(1e-12f))); | |
| 32 | } | ||
| 33 | |||
| 34 | 13 | void build_norm_columns(NormCtx& n) { | |
| 35 | 13 | Ctx& c = n.c; | |
| 36 | 13 | const auto& p = n.p; | |
| 37 | 13 | Value* lx = c.b.build_and(n.tid, c.i32(31)); | |
| 38 | 13 | Value* ly = c.b.build_and(c.b.build_lshr(n.tid, c.i32(5)), c.i32(7)); | |
| 39 | 13 | Value* s_col0 = c.kb.shared_alloc_f32(8u * 33u); | |
| 40 | 13 | Value* s_col1 = (n.mode == NORM_COL_LN) ? c.kb.shared_alloc_f32(8u * 33u) : nullptr; | |
| 41 | 13 | Value* slot = c.b.build_add(c.b.build_mul(ly, c.i32(33)), lx); | |
| 42 | 13 | Value* step_c = c.b.build_shl(c.kb.nctaid_x(), c.i32(5)); | |
| 43 | 13 | Value* start_c = c.b.build_shl(c.kb.ctaid_x(), c.i32(5)); | |
| 44 | 13 | Value* rows = p[7]; | |
| 45 | 13 | Value* d = p[8]; | |
| 46 | |||
| 47 | 26 | c.kb.for_range(start_c, d, step_c, [&](Value* c0) { | |
| 48 | 13 | Value* col = c.b.build_add(c0, lx); | |
| 49 | 13 | Value* in_b = c.b.build_ult(col, d); | |
| 50 | 13 | Value* r_end = c.b.build_select(in_b, rows, c.i32(0)); | |
| 51 | 13 | const std::vector<Value*> inits = (n.mode == NORM_COL_LN) | |
| 52 | 6 | ? std::vector<Value*>{c.f32(0.0f), c.f32(0.0f)} | |
| 53 | 7 | : std::vector<Value*>{c.f32(0.0f)}; | |
| 54 | 26 | auto acc = c.kb.for_range_reduce_n(ly, r_end, c.i32(8), inits, [&](Value* r, const std::vector<Value*>& cur) { | |
| 55 | 13 | Value* idx = c.b.build_add(c.b.build_mul(r, d), col); | |
| 56 | 13 | Value* xv = load_lanes(c, n.dt_x, 1, ptr_at(c, p[0], idx, n.esz_x))[0]; | |
| 57 | 13 | if (n.mode == NORM_COL_LN) { | |
| 58 | 6 | Value* xhv = load_lanes(c, n.dt_x, 1, ptr_at(c, p[4], idx, n.esz_x))[0]; | |
| 59 | 6 | return std::vector<Value*>{c.b.build_fma_f32(xv, xhv, cur[0]), c.b.build_add(cur[1], xv)}; | |
| 60 | } | ||
| 61 | 7 | if (n.mode == NORM_COL_RMS) { | |
| 62 | 3 | Value* dy = load_lanes(c, n.dt_x, 1, ptr_at(c, p[4], idx, n.esz_x))[0]; | |
| 63 | 3 | Value* rr = c.kb.load_f32(ptr_at(c, p[6], r, 4)); | |
| 64 | 3 | return std::vector<Value*>{c.b.build_fma_f32(c.b.build_mul(dy, xv), rr, cur[0])}; | |
| 65 | } | ||
| 66 | 4 | return std::vector<Value*>{c.b.build_add(cur[0], xv)}; | |
| 67 | 13 | }); | |
| 68 | 13 | c.kb.shared_store_f32_indexed(s_col0, slot, acc[0]); | |
| 69 | 13 | if (n.mode == NORM_COL_LN) c.kb.shared_store_f32_indexed(s_col1, slot, acc[1]); | |
| 70 | 13 | c.kb.sync(); | |
| 71 | 26 | c.kb.if_then(c.b.build_and(c.b.build_eq(ly, c.i32(0)), in_b), [&] { | |
| 72 | 13 | Value* t0 = c.kb.shared_load_f32_indexed(s_col0, lx); | |
| 73 | 104 | for (int k = 1; k < 8; ++k) { | |
| 74 | 91 | t0 = c.b.build_add(t0, c.kb.shared_load_f32_indexed(s_col0, c.b.build_add(c.i32(k * 33), lx))); | |
| 75 | 91 | } | |
| 76 | 13 | Value* gp = ptr_at(c, p[2], col, n.esz_g); | |
| 77 | 13 | store_lanes(c, n.dt_g, 1, gp, {c.b.build_add(load_lanes(c, n.dt_g, 1, gp)[0], t0)}); | |
| 78 | 13 | if (n.mode == NORM_COL_LN) { | |
| 79 | 6 | Value* t1 = c.kb.shared_load_f32_indexed(s_col1, lx); | |
| 80 | 48 | for (int k = 1; k < 8; ++k) { | |
| 81 | 42 | t1 = c.b.build_add(t1, c.kb.shared_load_f32_indexed(s_col1, c.b.build_add(c.i32(k * 33), lx))); | |
| 82 | 42 | } | |
| 83 | 6 | Value* bp = ptr_at(c, p[3], col, n.esz_g); | |
| 84 | 6 | store_lanes(c, n.dt_g, 1, bp, {c.b.build_add(load_lanes(c, n.dt_g, 1, bp)[0], t1)}); | |
| 85 | 6 | } | |
| 86 | 13 | }); | |
| 87 | 13 | c.kb.sync(); | |
| 88 | 13 | }); | |
| 89 | 13 | } | |
| 90 | |||
| 91 | 19 | void build_norm_ln(NormCtx& n, Value* row, Value* base) { | |
| 92 | 19 | Ctx& c = n.c; | |
| 93 | 19 | const auto& p = n.p; | |
| 94 | 38 | Value* sum = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(0.0f), [&](Value* k, Value* a) { | |
| 95 | 19 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 96 | 92 | for (Value* v : load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], c.b.build_add(base, col), n.esz_x))) { | |
| 97 | 73 | a = c.b.build_add(a, v); | |
| 98 | } | ||
| 99 | 19 | return a; | |
| 100 | ✗ | }); | |
| 101 | 19 | Value* mean = c.b.build_mul(emit_block_sum_f32(c, n.s_red, sum), n.invd); | |
| 102 | 38 | Value* sq = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(0.0f), [&](Value* k, Value* a) { | |
| 103 | 19 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 104 | 92 | for (Value* v : load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], c.b.build_add(base, col), n.esz_x))) { | |
| 105 | 73 | Value* diff = c.b.build_sub(v, mean); | |
| 106 | 73 | a = c.b.build_fma_f32(diff, diff, a); | |
| 107 | } | ||
| 108 | 19 | return a; | |
| 109 | ✗ | }); | |
| 110 | 19 | Value* var = c.b.build_mul(emit_block_sum_f32(c, n.s_red, sq), n.invd); | |
| 111 | 19 | Value* rstd = norm_rsqrt(c, c.b.build_add(var, n.eps_safe)); | |
| 112 | 19 | if (n.flags.has_s0 || n.flags.has_s1) { | |
| 113 | 20 | c.kb.if_then(c.b.build_eq(n.tid, c.i32(0)), [&] { | |
| 114 | 10 | if (n.flags.has_s0) c.kb.store_f32(ptr_at(c, p[5], row, 4), mean); | |
| 115 | 10 | if (n.flags.has_s1) c.kb.store_f32(ptr_at(c, p[6], row, 4), rstd); | |
| 116 | 10 | }); | |
| 117 | 10 | } | |
| 118 | 38 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 119 | 19 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 120 | 19 | Value* idx = c.b.build_add(base, col); | |
| 121 | 19 | auto xs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 122 | 19 | std::vector<Value*> gs, bs; | |
| 123 | 19 | if (n.flags.has_g) gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 124 | 19 | if (n.flags.has_b) bs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[3], col, n.esz_g)); | |
| 125 | 19 | std::vector<Value*> xhs(static_cast<std::size_t>(n.lanes)); | |
| 126 | 19 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 127 | 92 | for (int j = 0; j < n.lanes; ++j) { | |
| 128 | 73 | const auto js = static_cast<std::size_t>(j); | |
| 129 | 73 | xhs[js] = c.b.build_mul(c.b.build_sub(xs[js], mean), rstd); | |
| 130 | 73 | Value* scaled = gs.empty() ? xhs[js] : c.b.build_mul(gs[js], xhs[js]); | |
| 131 | 73 | ys[js] = bs.empty() ? scaled : c.b.build_add(scaled, bs[js]); | |
| 132 | 73 | } | |
| 133 | 19 | if (n.flags.has_xh) store_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x), xhs); | |
| 134 | 19 | store_lanes(c, n.dt_y, n.lanes, ptr_at(c, p[1], idx, n.esz_y), ys); | |
| 135 | 19 | }); | |
| 136 | 19 | } | |
| 137 | |||
| 138 | 14 | void build_norm_rms_l2(NormCtx& n, Value* row, Value* base) { | |
| 139 | 14 | Ctx& c = n.c; | |
| 140 | 14 | const auto& p = n.p; | |
| 141 | 28 | Value* sq = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(0.0f), [&](Value* k, Value* a) { | |
| 142 | 14 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 143 | 58 | for (Value* v : load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], c.b.build_add(base, col), n.esz_x))) { | |
| 144 | 44 | a = c.b.build_fma_f32(v, v, a); | |
| 145 | } | ||
| 146 | 14 | return a; | |
| 147 | ✗ | }); | |
| 148 | 14 | Value* q = emit_block_sum_f32(c, n.s_red, sq); | |
| 149 | 14 | Value* arg = (n.mode == NORM_RMS) ? c.b.build_fma_f32(q, n.invd, n.eps_safe) : c.b.build_add(q, n.eps_safe); | |
| 150 | 14 | Value* r = norm_rsqrt(c, arg); | |
| 151 | 14 | if (n.mode == NORM_RMS && n.flags.has_s1) { | |
| 152 | ✗ | c.kb.if_then(c.b.build_eq(n.tid, c.i32(0)), [&] { c.kb.store_f32(ptr_at(c, p[6], row, 4), r); }); | |
| 153 | ✗ | } | |
| 154 | 28 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 155 | 14 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 156 | 14 | Value* idx = c.b.build_add(base, col); | |
| 157 | 14 | auto xs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 158 | 14 | std::vector<Value*> gs; | |
| 159 | 14 | if (n.mode == NORM_RMS && n.flags.has_g) { | |
| 160 | 7 | gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 161 | 7 | } | |
| 162 | 14 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 163 | 58 | for (int j = 0; j < n.lanes; ++j) { | |
| 164 | 44 | const auto js = static_cast<std::size_t>(j); | |
| 165 | 44 | Value* vr = c.b.build_mul(xs[js], r); | |
| 166 | 44 | ys[js] = (!gs.empty()) ? c.b.build_mul(vr, gs[js]) : vr; | |
| 167 | 44 | } | |
| 168 | 14 | store_lanes(c, n.dt_y, n.lanes, ptr_at(c, p[1], idx, n.esz_y), ys); | |
| 169 | 14 | }); | |
| 170 | 14 | } | |
| 171 | |||
| 172 | 13 | void build_norm_ln_bwd(NormCtx& n, Value* row, Value* base) { | |
| 173 | 13 | Ctx& c = n.c; | |
| 174 | 13 | const auto& p = n.p; | |
| 175 | 13 | auto sums = c.kb.for_range_reduce_n( | |
| 176 | 26 | n.tid, n.n_vecs, n.block, {c.f32(0.0f), c.f32(0.0f)}, [&](Value* k, const std::vector<Value*>& cur) { | |
| 177 | 13 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 178 | 13 | Value* idx = c.b.build_add(base, col); | |
| 179 | 13 | auto dys = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 180 | 13 | auto gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 181 | 13 | auto xhs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 182 | 13 | Value* a1 = cur[0]; | |
| 183 | 13 | Value* a2 = cur[1]; | |
| 184 | 77 | for (int j = 0; j < n.lanes; ++j) { | |
| 185 | 64 | const auto js = static_cast<std::size_t>(j); | |
| 186 | 64 | Value* dxh = c.b.build_mul(dys[js], gs[js]); | |
| 187 | 64 | a1 = c.b.build_add(a1, dxh); | |
| 188 | 64 | a2 = c.b.build_fma_f32(dxh, xhs[js], a2); | |
| 189 | 64 | } | |
| 190 | 13 | return std::vector<Value*>{a1, a2}; | |
| 191 | 13 | }); | |
| 192 | 13 | Value* s1 = emit_block_sum_f32(c, n.s_red, sums[0]); | |
| 193 | 13 | Value* s2 = emit_block_sum_f32(c, n.s_red, sums[1]); | |
| 194 | 13 | Value* rstd = n.flags.has_s1 ? c.kb.load_f32(ptr_at(c, p[6], row, 4)) : p[10]; | |
| 195 | 13 | Value* scale = c.b.build_mul(rstd, n.invd); | |
| 196 | 26 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 197 | 13 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 198 | 13 | Value* idx = c.b.build_add(base, col); | |
| 199 | 13 | auto dys = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 200 | 13 | auto gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 201 | 13 | auto xhs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 202 | 13 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 203 | 77 | for (int j = 0; j < n.lanes; ++j) { | |
| 204 | 64 | const auto js = static_cast<std::size_t>(j); | |
| 205 | 64 | Value* dxh = c.b.build_mul(dys[js], gs[js]); | |
| 206 | 128 | Value* inner = c.b.build_sub(c.b.build_sub(c.b.build_mul(n.d_f, dxh), s1), | |
| 207 | 64 | c.b.build_mul(xhs[js], s2)); | |
| 208 | 64 | ys[js] = c.b.build_mul(scale, inner); | |
| 209 | 64 | } | |
| 210 | 13 | store_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[1], idx, n.esz_x), ys); | |
| 211 | 13 | }); | |
| 212 | 13 | } | |
| 213 | |||
| 214 | 14 | void build_norm_rms_l2_bwd(NormCtx& n, Value* row, Value* base) { | |
| 215 | 14 | Ctx& c = n.c; | |
| 216 | 14 | const auto& p = n.p; | |
| 217 | 14 | const bool use_g = (n.mode == NORM_RMS_BWD && n.flags.has_g); | |
| 218 | 14 | auto sums = c.kb.for_range_reduce_n( | |
| 219 | 28 | n.tid, n.n_vecs, n.block, {c.f32(0.0f), c.f32(0.0f)}, [&](Value* k, const std::vector<Value*>& cur) { | |
| 220 | 14 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 221 | 14 | Value* idx = c.b.build_add(base, col); | |
| 222 | 14 | auto vs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 223 | 14 | auto dys = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 224 | 14 | std::vector<Value*> gs; | |
| 225 | 14 | if (use_g) gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 226 | 14 | Value* aq = cur[0]; | |
| 227 | 14 | Value* ap = cur[1]; | |
| 228 | 72 | for (int j = 0; j < n.lanes; ++j) { | |
| 229 | 58 | const auto js = static_cast<std::size_t>(j); | |
| 230 | 58 | aq = c.b.build_fma_f32(vs[js], vs[js], aq); | |
| 231 | 58 | Value* vd = c.b.build_mul(vs[js], dys[js]); | |
| 232 | 58 | ap = (!gs.empty()) ? c.b.build_fma_f32(vd, gs[js], ap) : c.b.build_add(ap, vd); | |
| 233 | 58 | } | |
| 234 | 14 | return std::vector<Value*>{aq, ap}; | |
| 235 | 14 | }); | |
| 236 | 14 | Value* q = emit_block_sum_f32(c, n.s_red, sums[0]); | |
| 237 | 14 | Value* p_tot = emit_block_sum_f32(c, n.s_red, sums[1]); | |
| 238 | 14 | Value* arg = (n.mode == NORM_RMS_BWD) ? c.b.build_fma_f32(q, n.invd, n.eps_safe) : c.b.build_add(q, n.eps_safe); | |
| 239 | 14 | Value* r = norm_rsqrt(c, arg); | |
| 240 | 14 | if (n.mode == NORM_RMS_BWD && n.flags.has_s1) { | |
| 241 | 16 | c.kb.if_then(c.b.build_eq(n.tid, c.i32(0)), [&] { c.kb.store_f32(ptr_at(c, p[6], row, 4), r); }); | |
| 242 | 8 | } | |
| 243 | 14 | Value* rr = c.b.build_mul(r, r); | |
| 244 | 17 | Value* coeff = (n.mode == NORM_RMS_BWD) ? c.b.build_mul(c.b.build_mul(n.invd, rr), p_tot) | |
| 245 | 3 | : c.b.build_mul(rr, p_tot); | |
| 246 | 28 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 247 | 14 | Value* col = col_elem_idx(c, k, n.lanes); | |
| 248 | 14 | Value* idx = c.b.build_add(base, col); | |
| 249 | 14 | auto vs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 250 | 14 | auto dys = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 251 | 14 | std::vector<Value*> gs; | |
| 252 | 14 | if (use_g) gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g)); | |
| 253 | 14 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 254 | 72 | for (int j = 0; j < n.lanes; ++j) { | |
| 255 | 58 | const auto js = static_cast<std::size_t>(j); | |
| 256 | 58 | Value* gdy = (!gs.empty()) ? c.b.build_mul(gs[js], dys[js]) : dys[js]; | |
| 257 | 58 | ys[js] = c.b.build_mul(r, c.b.build_sub(gdy, c.b.build_mul(vs[js], coeff))); | |
| 258 | 58 | } | |
| 259 | 14 | store_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[1], idx, n.esz_x), ys); | |
| 260 | 14 | }); | |
| 261 | 14 | } | |
| 262 | |||
| 263 | 8 | void build_norm_softmax(NormCtx& n, Value* base) { | |
| 264 | 8 | Ctx& c = n.c; | |
| 265 | 8 | const auto& p = n.p; | |
| 266 | 23 | auto mask_off = [&](const std::vector<Value*>& ms, std::size_t j) -> Value* { | |
| 267 | 15 | return c.b.build_slt(ms[j], c.f32(0.5f)); | |
| 268 | }; | ||
| 269 | 16 | Value* m_acc = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(-1e30f), [&](Value* k, Value* a) { | |
| 270 | 8 | Value* idx = c.b.build_add(base, col_elem_idx(c, k, n.lanes)); | |
| 271 | 8 | auto xs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 272 | 8 | std::vector<Value*> ms; | |
| 273 | 8 | if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4)); | |
| 274 | 36 | for (int j = 0; j < n.lanes; ++j) { | |
| 275 | 28 | const auto js = static_cast<std::size_t>(j); | |
| 276 | 28 | Value* mx = c.kb.fmax(a, xs[js]); | |
| 277 | 28 | a = n.flags.has_s0 ? c.b.build_select(mask_off(ms, js), a, mx) : mx; | |
| 278 | 28 | } | |
| 279 | 8 | return a; | |
| 280 | 8 | }); | |
| 281 | 8 | Value* m = emit_block_max_f32(c, n.s_red, m_acc); | |
| 282 | 16 | Value* s_acc = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(0.0f), [&](Value* k, Value* a) { | |
| 283 | 8 | Value* idx = c.b.build_add(base, col_elem_idx(c, k, n.lanes)); | |
| 284 | 8 | auto xs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 285 | 8 | std::vector<Value*> ms; | |
| 286 | 8 | if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4)); | |
| 287 | 36 | for (int j = 0; j < n.lanes; ++j) { | |
| 288 | 28 | const auto js = static_cast<std::size_t>(j); | |
| 289 | 28 | Value* ex = emit_exp(c, c.b.build_sub(xs[js], m)); | |
| 290 | 28 | a = c.b.build_add(a, n.flags.has_s0 ? c.b.build_select(mask_off(ms, js), c.f32(0.0f), ex) : ex); | |
| 291 | 28 | } | |
| 292 | 8 | return a; | |
| 293 | 8 | }); | |
| 294 | 8 | Value* s = emit_block_sum_f32(c, n.s_red, s_acc); | |
| 295 | 16 | Value* inv = c.b.build_select(c.b.build_sgt(s, c.f32(0.0f)), | |
| 296 | 8 | c.b.build_sdiv(c.f32(1.0f), c.kb.fmax(s, c.f32(1e-30f))), c.f32(0.0f)); | |
| 297 | 16 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 298 | 8 | Value* idx = c.b.build_add(base, col_elem_idx(c, k, n.lanes)); | |
| 299 | 8 | auto xs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 300 | 8 | std::vector<Value*> ms; | |
| 301 | 8 | if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4)); | |
| 302 | 8 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 303 | 36 | for (int j = 0; j < n.lanes; ++j) { | |
| 304 | 28 | const auto js = static_cast<std::size_t>(j); | |
| 305 | 28 | Value* val = c.b.build_mul(emit_exp(c, c.b.build_sub(xs[js], m)), inv); | |
| 306 | 28 | ys[js] = n.flags.has_s0 ? c.b.build_select(mask_off(ms, js), c.f32(0.0f), val) : val; | |
| 307 | 28 | } | |
| 308 | 8 | store_lanes(c, n.dt_y, n.lanes, ptr_at(c, p[1], idx, n.esz_y), ys); | |
| 309 | 8 | }); | |
| 310 | 8 | } | |
| 311 | |||
| 312 | 5 | void build_norm_sm_bwd(NormCtx& n, Value* base) { | |
| 313 | 5 | Ctx& c = n.c; | |
| 314 | 5 | const auto& p = n.p; | |
| 315 | 10 | Value* dot = c.kb.for_range_reduce(n.tid, n.n_vecs, n.block, c.f32(0.0f), [&](Value* k, Value* a) { | |
| 316 | 5 | Value* idx = c.b.build_add(base, col_elem_idx(c, k, n.lanes)); | |
| 317 | 5 | auto ps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 318 | 5 | auto dps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 319 | 30 | for (int j = 0; j < n.lanes; ++j) { | |
| 320 | 25 | const auto js = static_cast<std::size_t>(j); | |
| 321 | 25 | a = c.b.build_fma_f32(dps[js], ps[js], a); | |
| 322 | 25 | } | |
| 323 | 5 | return a; | |
| 324 | 5 | }); | |
| 325 | 5 | dot = emit_block_sum_f32(c, n.s_red, dot); | |
| 326 | 10 | c.kb.for_range(n.tid, n.n_vecs, n.block, [&](Value* k) { | |
| 327 | 5 | Value* idx = c.b.build_add(base, col_elem_idx(c, k, n.lanes)); | |
| 328 | 5 | auto ps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[0], idx, n.esz_x)); | |
| 329 | 5 | auto dps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x)); | |
| 330 | 5 | std::vector<Value*> ys(static_cast<std::size_t>(n.lanes)); | |
| 331 | 30 | for (int j = 0; j < n.lanes; ++j) { | |
| 332 | 25 | const auto js = static_cast<std::size_t>(j); | |
| 333 | 25 | ys[js] = c.b.build_mul(ps[js], c.b.build_sub(dps[js], dot)); | |
| 334 | 25 | } | |
| 335 | 5 | store_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[1], idx, n.esz_x), ys); | |
| 336 | 5 | }); | |
| 337 | 5 | } | |
| 338 | |||
| 339 | } // namespace | ||
| 340 | |||
| 341 | 86 | Function* build_norm_mir(Module& mod, std::uint32_t mode, Dtype dt_x, Dtype dt_y, | |
| 342 | Dtype dt_g, NormMirFlags flags, int lanes) { | ||
| 343 | 172 | Function* fn = mod.create_function( | |
| 344 | 86 | "mir_norm", Type::void_type(), | |
| 345 | 430 | {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), | |
| 346 | 344 | Type::i32(), Type::i32(), Type::f32(), Type::f32()}); | |
| 347 | 86 | brass::codegen::KernelBuilder kb(mod, fn); | |
| 348 | 86 | Ctx c(kb); | |
| 349 | 86 | auto p = entry_params(c, fn); | |
| 350 | 86 | Value* d_f = kb.fmax(kb.i32_to_f32(p[8]), c.f32(1.0f)); | |
| 351 | 774 | NormCtx n{ | |
| 352 | 344 | c, p, mode, dt_x, dt_y, dt_g, | |
| 353 | 86 | elem_bytes(dt_x), elem_bytes(dt_y), elem_bytes(dt_g), lanes, flags, | |
| 354 | 86 | kb.tid_x(), c.i32(static_cast<int>(kNormBlock)), | |
| 355 | 86 | vec_count(c, p[8], lanes), | |
| 356 | 86 | c.b.build_sdiv(c.f32(1.0f), d_f), | |
| 357 | 86 | d_f, | |
| 358 | 86 | kb.fmax(p[9], c.f32(1e-12f)), | |
| 359 | 86 | (mode < NORM_COL_LN) ? kb.shared_alloc_f32(8) : nullptr}; | |
| 360 | |||
| 361 | 86 | if (mode >= NORM_COL_LN) { | |
| 362 | 13 | build_norm_columns(n); | |
| 363 | 13 | } else { | |
| 364 | 146 | kb.for_range(kb.ctaid_x(), p[7], kb.nctaid_x(), [&](Value* row) { | |
| 365 | 73 | Value* base = c.b.build_mul(row, p[8]); | |
| 366 | 73 | if (mode == NORM_LN) build_norm_ln(n, row, base); | |
| 367 | 54 | else if (mode == NORM_RMS || mode == NORM_L2) build_norm_rms_l2(n, row, base); | |
| 368 | 40 | else if (mode == NORM_LN_BWD) build_norm_ln_bwd(n, row, base); | |
| 369 | 27 | else if (mode == NORM_RMS_BWD || mode == NORM_L2_BWD) build_norm_rms_l2_bwd(n, row, base); | |
| 370 | 13 | else if (mode == NORM_SOFTMAX) build_norm_softmax(n, base); | |
| 371 | 5 | else build_norm_sm_bwd(n, base); | |
| 372 | 73 | }); | |
| 373 | } | ||
| 374 | 86 | c.b.build_ret_void(); | |
| 375 | 86 | return finalize_fn(fn); | |
| 376 | 86 | } | |
| 377 | |||
| 378 | } // namespace brotensor::kernels | ||
| 379 |