brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
321 / 326
98.5%
Branches:
297 / 484
61.4%
Functions:
30 / 31
96.8%

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
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
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
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
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
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
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
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
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
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
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
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 c.kb.shared_store_f32_indexed(s_col0, slot, acc[0]);
69
3/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
13 if (n.mode == NORM_COL_LN) c.kb.shared_store_f32_indexed(s_col1, slot, acc[1]);
70
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 c.kb.sync();
71
5/10
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 13 times.
✗ Branch 9 not taken.
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
2/2
✓ Branch 0 taken 91 times.
✓ Branch 1 taken 13 times.
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
3/6
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 13 times.
✗ Branch 5 not taken.
13 store_lanes(c, n.dt_g, 1, gp, {c.b.build_add(load_lanes(c, n.dt_g, 1, gp)[0], t0)});
78
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 6 times.
13 if (n.mode == NORM_COL_LN) {
79 6 Value* t1 = c.kb.shared_load_f32_indexed(s_col1, lx);
80
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 6 times.
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
3/6
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
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
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 73 times.
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
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 73 times.
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
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
73 Value* diff = c.b.build_sub(v, mean);
106
1/2
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
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
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
19 if (n.flags.has_s0 || n.flags.has_s1) {
113
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
20 c.kb.if_then(c.b.build_eq(n.tid, c.i32(0)), [&] {
114
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (n.flags.has_s0) c.kb.store_f32(ptr_at(c, p[5], row, 4), mean);
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (n.flags.has_s1) c.kb.store_f32(ptr_at(c, p[6], row, 4), rstd);
116 10 });
117 10 }
118
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
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
3/6
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 19 times.
✗ Branch 5 not taken.
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
4/6
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 16 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
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
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<Value*> xhs(static_cast<std::size_t>(n.lanes));
126
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
127
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 73 times.
92 for (int j = 0; j < n.lanes; ++j) {
128 73 const auto js = static_cast<std::size_t>(j);
129
2/4
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
73 xhs[js] = c.b.build_mul(c.b.build_sub(xs[js], mean), rstd);
130
2/4
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
73 Value* scaled = gs.empty() ? xhs[js] : c.b.build_mul(gs[js], xhs[js]);
131
3/4
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 53 times.
✗ Branch 3 not taken.
73 ys[js] = bs.empty() ? scaled : c.b.build_add(scaled, bs[js]);
132 73 }
133
4/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
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
2/4
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 44 times.
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
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
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
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
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
3/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
14 if (n.mode == NORM_RMS && n.flags.has_s1) {
152
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ 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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
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
4/4
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 3 times.
14 if (n.mode == NORM_RMS && n.flags.has_g) {
160
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g));
161 7 }
162
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
163
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 44 times.
58 for (int j = 0; j < n.lanes; ++j) {
164 44 const auto js = static_cast<std::size_t>(j);
165
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 Value* vr = c.b.build_mul(xs[js], r);
166
3/4
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 17 times.
✓ Branch 2 taken 27 times.
✗ Branch 3 not taken.
44 ys[js] = (!gs.empty()) ? c.b.build_mul(vr, gs[js]) : vr;
167 44 }
168
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 auto sums = c.kb.for_range_reduce_n(
176
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
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
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 auto gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g));
181
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
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
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 64 times.
77 for (int j = 0; j < n.lanes; ++j) {
185 64 const auto js = static_cast<std::size_t>(j);
186
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 Value* dxh = c.b.build_mul(dys[js], gs[js]);
187
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 a1 = c.b.build_add(a1, dxh);
188
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 a2 = c.b.build_fma_f32(dxh, xhs[js], a2);
189 64 }
190
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 return std::vector<Value*>{a1, a2};
191 13 });
192
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* s1 = emit_block_sum_f32(c, n.s_red, sums[0]);
193
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* s2 = emit_block_sum_f32(c, n.s_red, sums[1]);
194
4/6
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
13 Value* rstd = n.flags.has_s1 ? c.kb.load_f32(ptr_at(c, p[6], row, 4)) : p[10];
195
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 Value* scale = c.b.build_mul(rstd, n.invd);
196
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
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
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 auto gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g));
201
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
13 auto xhs = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x));
202
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
203
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 64 times.
77 for (int j = 0; j < n.lanes; ++j) {
204 64 const auto js = static_cast<std::size_t>(j);
205
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 Value* dxh = c.b.build_mul(dys[js], gs[js]);
206
3/6
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 64 times.
✗ Branch 5 not taken.
128 Value* inner = c.b.build_sub(c.b.build_sub(c.b.build_mul(n.d_f, dxh), s1),
207
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 c.b.build_mul(xhs[js], s2));
208
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 ys[js] = c.b.build_mul(scale, inner);
209 64 }
210
2/4
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13 times.
✗ Branch 3 not taken.
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
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 11 times.
14 const bool use_g = (n.mode == NORM_RMS_BWD && n.flags.has_g);
218
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 auto sums = c.kb.for_range_reduce_n(
219
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
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
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
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
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 58 times.
72 for (int j = 0; j < n.lanes; ++j) {
229 58 const auto js = static_cast<std::size_t>(j);
230
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 aq = c.b.build_fma_f32(vs[js], vs[js], aq);
231
1/2
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
58 Value* vd = c.b.build_mul(vs[js], dys[js]);
232
4/6
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
58 ap = (!gs.empty()) ? c.b.build_fma_f32(vd, gs[js], ap) : c.b.build_add(ap, vd);
233 58 }
234
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 return std::vector<Value*>{aq, ap};
235 14 });
236
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 Value* q = emit_block_sum_f32(c, n.s_red, sums[0]);
237
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 Value* p_tot = emit_block_sum_f32(c, n.s_red, sums[1]);
238
4/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 Value* r = norm_rsqrt(c, arg);
240
4/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 3 times.
14 if (n.mode == NORM_RMS_BWD && n.flags.has_s1) {
241
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
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
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 Value* rr = c.b.build_mul(r, r);
244
4/6
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
17 Value* coeff = (n.mode == NORM_RMS_BWD) ? c.b.build_mul(c.b.build_mul(n.invd, rr), p_tot)
245
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 : c.b.build_mul(rr, p_tot);
246
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
4/6
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
14 if (use_g) gs = load_lanes(c, n.dt_g, n.lanes, ptr_at(c, p[2], col, n.esz_g));
253
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
254
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 58 times.
72 for (int j = 0; j < n.lanes; ++j) {
255 58 const auto js = static_cast<std::size_t>(j);
256
3/4
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 16 times.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
58 Value* gdy = (!gs.empty()) ? c.b.build_mul(gs[js], dys[js]) : dys[js];
257
3/6
✓ Branch 0 taken 58 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 58 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 58 times.
✗ Branch 5 not taken.
58 ys[js] = c.b.build_mul(r, c.b.build_sub(gdy, c.b.build_mul(vs[js], coeff)));
258 58 }
259
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
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
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
8 if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4));
274
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
36 for (int j = 0; j < n.lanes; ++j) {
275 28 const auto js = static_cast<std::size_t>(j);
276
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 Value* mx = c.kb.fmax(a, xs[js]);
277
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
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
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
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
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
8 if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4));
287
2/2
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 8 times.
36 for (int j = 0; j < n.lanes; ++j) {
288 28 const auto js = static_cast<std::size_t>(j);
289
2/4
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
28 Value* ex = emit_exp(c, c.b.build_sub(xs[js], m));
290
6/10
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 28 times.
✗ Branch 9 not taken.
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
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
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
4/6
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
8 if (n.flags.has_s0) ms = load_lanes(c, Dtype::FP32, n.lanes, ptr_at(c, p[5], idx, 4));
302
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
303
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
36 for (int j = 0; j < n.lanes; ++j) {
304 28 const auto js = static_cast<std::size_t>(j);
305
3/6
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
28 Value* val = c.b.build_mul(emit_exp(c, c.b.build_sub(xs[js], m)), inv);
306
5/8
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
28 ys[js] = n.flags.has_s0 ? c.b.build_select(mask_off(ms, js), c.f32(0.0f), val) : val;
307 28 }
308
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
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
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 auto dps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x));
319
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 5 times.
30 for (int j = 0; j < n.lanes; ++j) {
320 25 const auto js = static_cast<std::size_t>(j);
321
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
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
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
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
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 auto dps = load_lanes(c, n.dt_x, n.lanes, ptr_at(c, p[4], idx, n.esz_x));
330
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 std::vector<Value*> ys(static_cast<std::size_t>(n.lanes));
331
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 25 times.
30 for (int j = 0; j < n.lanes; ++j) {
332 25 const auto js = static_cast<std::size_t>(j);
333
2/4
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
✗ Branch 3 not taken.
25 ys[js] = c.b.build_mul(ps[js], c.b.build_sub(dps[js], dot));
334 25 }
335
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
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
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 Ctx c(kb);
349
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 auto p = entry_params(c, fn);
350
3/6
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
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
3/6
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 86 times.
✗ Branch 5 not taken.
86 elem_bytes(dt_x), elem_bytes(dt_y), elem_bytes(dt_g), lanes, flags,
354
2/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
86 kb.tid_x(), c.i32(static_cast<int>(kNormBlock)),
355
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 vec_count(c, p[8], lanes),
356
2/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
86 c.b.build_sdiv(c.f32(1.0f), d_f),
357 86 d_f,
358
2/4
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86 times.
✗ Branch 3 not taken.
86 kb.fmax(p[9], c.f32(1e-12f)),
359
3/4
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
86 (mode < NORM_COL_LN) ? kb.shared_alloc_f32(8) : nullptr};
360
361
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 73 times.
86 if (mode >= NORM_COL_LN) {
362
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 build_norm_columns(n);
363 13 } else {
364
4/8
✓ Branch 0 taken 73 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 73 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 73 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 73 times.
✗ Branch 7 not taken.
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
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 19 times.
73 if (mode == NORM_LN) build_norm_ln(n, row, base);
367
4/4
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 40 times.
54 else if (mode == NORM_RMS || mode == NORM_L2) build_norm_rms_l2(n, row, base);
368
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 27 times.
40 else if (mode == NORM_LN_BWD) build_norm_ln_bwd(n, row, base);
369
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 13 times.
27 else if (mode == NORM_RMS_BWD || mode == NORM_L2_BWD) build_norm_rms_l2_bwd(n, row, base);
370
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 5 times.
13 else if (mode == NORM_SOFTMAX) build_norm_softmax(n, base);
371 5 else build_norm_sm_bwd(n, base);
372 73 });
373 }
374
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 c.b.build_ret_void();
375
1/2
✓ Branch 0 taken 86 times.
✗ Branch 1 not taken.
86 return finalize_fn(fn);
376 86 }
377
378 } // namespace brotensor::kernels
379