brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
355 / 356
99.7%
Branches:
187 / 350
53.4%
Functions:
38 / 38
100.0%

src/kernels/mir_gnorm.cpp
Line Branch Exec Source
1 #include "mir_gnorm.h"
2 #include "mir_common.h"
3 #include "mir_math.h"
4
5 #include "../vulkan/shaders/op_codes.h"
6
7 #include <array>
8 #include <vector>
9
10 namespace brotensor::kernels {
11
12 namespace {
13
14 153 std::array<Value*, 3> chan_combine(Ctx& c, Value* na, Value* ma, Value* qa,
15 Value* nb, Value* mb, Value* qb) {
16 153 Value* n_sum = c.b.build_add(na, nb);
17 153 Value* has_b = c.b.build_sgt(nb, c.f32(0.0f));
18 153 Value* has_a = c.b.build_sgt(na, c.f32(0.0f));
19 153 Value* f = c.b.build_sdiv(nb, c.kb.fmax(n_sum, c.f32(1e-30f)));
20 153 Value* d = c.b.build_sub(mb, ma);
21 153 Value* ma_b = c.b.build_fma_f32(d, f, ma);
22 153 Value* qa_b = c.b.build_fma_f32(c.b.build_mul(d, d), c.b.build_mul(na, f), c.b.build_add(qb, qa));
23 153 Value* mn = c.b.build_select(has_b, c.b.build_select(has_a, n_sum, nb), na);
24 153 Value* mm = c.b.build_select(has_b, c.b.build_select(has_a, ma_b, mb), ma);
25 153 Value* mq = c.b.build_select(has_b, c.b.build_select(has_a, qa_b, qb), qa);
26 153 return {mn, mm, mq};
27 }
28
29 17 std::array<Value*, 2> emit_tile_stats(Ctx& c, Value* part, Value* tile_idx, Value* splits, Value* eps) {
30 17 Value* s_n = c.kb.shared_alloc_f32(kGnormBlock);
31 17 Value* s_m = c.kb.shared_alloc_f32(kGnormBlock);
32 17 Value* s_q = c.kb.shared_alloc_f32(kGnormBlock);
33 17 Value* tid = c.kb.tid_x();
34 17 Value* ts = c.b.build_mul(tile_idx, splits);
35
36
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 auto red = c.kb.for_range_reduce_n(
37 17 tid, splits, c.i32(static_cast<int>(kGnormBlock)),
38 17 {c.f32(0.0f), c.f32(0.0f), c.f32(0.0f)},
39
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
34 [&](Value* s, const std::vector<Value*>& cur) {
40 17 Value* o = c.b.build_mul(c.b.build_add(ts, s), c.i32(3));
41 17 Value* nb = c.kb.load_f32(ptr_at(c, part, o, 4));
42 17 Value* mb = c.kb.load_f32(ptr_at(c, part, c.b.build_add(o, c.i32(1)), 4));
43 17 Value* qb = c.kb.load_f32(ptr_at(c, part, c.b.build_add(o, c.i32(2)), 4));
44 17 auto nxt = chan_combine(c, cur[0], cur[1], cur[2], nb, mb, qb);
45 17 return std::vector<Value*>{nxt[0], nxt[1], nxt[2]};
46 });
47
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 c.kb.shared_store_f32_indexed(s_n, tid, red[0]);
48
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 c.kb.shared_store_f32_indexed(s_m, tid, red[1]);
49
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 c.kb.shared_store_f32_indexed(s_q, tid, red[2]);
50
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 c.kb.sync();
51
52
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 136 times.
153 for (std::uint32_t s = kGnormBlock >> 1; s > 0; s >>= 1) {
53
4/8
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 136 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 136 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 136 times.
✗ Branch 7 not taken.
272 c.kb.if_then(c.b.build_ult(tid, c.i32(static_cast<int>(s))), [&] {
54 136 Value* other = c.b.build_add(tid, c.i32(static_cast<int>(s)));
55 136 auto cmb = chan_combine(
56 136 c,
57 136 c.kb.shared_load_f32_indexed(s_n, tid),
58 136 c.kb.shared_load_f32_indexed(s_m, tid),
59 136 c.kb.shared_load_f32_indexed(s_q, tid),
60 136 c.kb.shared_load_f32_indexed(s_n, other),
61 136 c.kb.shared_load_f32_indexed(s_m, other),
62 136 c.kb.shared_load_f32_indexed(s_q, other));
63 136 c.kb.shared_store_f32_indexed(s_n, tid, cmb[0]);
64 136 c.kb.shared_store_f32_indexed(s_m, tid, cmb[1]);
65 136 c.kb.shared_store_f32_indexed(s_q, tid, cmb[2]);
66 136 });
67
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 c.kb.sync();
68 136 }
69
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 Value* mean = c.kb.shared_load_f32_indexed(s_m, c.i32(0));
70
4/8
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
17 Value* n0 = c.kb.fmax(c.kb.shared_load_f32_indexed(s_n, c.i32(0)), c.f32(1.0f));
71
4/8
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 17 times.
✗ Branch 7 not taken.
17 Value* q0 = c.kb.fmax(c.kb.shared_load_f32_indexed(s_q, c.i32(0)), c.f32(0.0f));
72
2/4
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
17 Value* var = c.b.build_add(c.b.build_sdiv(q0, n0), eps);
73
3/6
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 17 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 17 times.
✗ Branch 5 not taken.
17 Value* rstd = emit_rsqrt(c, c.kb.fmax(var, c.f32(1e-12f)));
74
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 c.kb.sync();
75 17 return {mean, rstd};
76 17 }
77
78 8 void build_gn_stats(Ctx& c, const std::vector<Value*>& p, Dtype dt, int lanes) {
79 8 const int esz = elem_bytes(dt);
80 8 Value* tid = c.kb.tid_x();
81 8 Value* block = c.i32(static_cast<int>(kGnormBlock));
82 8 Value* tile = c.kb.ctaid_y();
83 8 Value* s = c.kb.ctaid_x();
84 8 Value* base = c.b.build_mul(tile, p[7]);
85 8 Value* lo = c.b.build_mul(s, p[12]);
86 16 Value* hi = c.b.build_select(c.b.build_ult(c.b.build_add(lo, p[12]), p[7]),
87 8 c.b.build_add(lo, p[12]), p[7]);
88 8 Value* K = load_lanes(c, dt, 1, ptr_at(c, p[0], c.b.build_add(base, lo), esz))[0];
89 8 Value* s_red = c.kb.shared_alloc_f32(8);
90
91 8 Value* start = c.b.build_add(vec_count(c, lo, lanes), tid);
92 8 Value* end = vec_count(c, hi, lanes);
93
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 auto sums = c.kb.for_range_reduce_n(start, end, block, {c.f32(0.0f), c.f32(0.0f)},
94
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 [&](Value* iv, const std::vector<Value*>& cur) {
95 8 Value* idx = c.b.build_add(base, col_elem_idx(c, iv, lanes));
96 8 auto xs = load_lanes(c, dt, lanes, ptr_at(c, p[0], idx, esz));
97 8 Value* a1 = cur[0];
98 8 Value* a2 = cur[1];
99
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 17 times.
25 for (int j = 0; j < lanes; ++j) {
100
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 Value* d = c.b.build_sub(xs[static_cast<std::size_t>(j)], K);
101
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 a1 = c.b.build_add(a1, d);
102
1/2
✓ Branch 0 taken 17 times.
✗ Branch 1 not taken.
17 a2 = c.b.build_fma_f32(d, d, a2);
103 17 }
104
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 return std::vector<Value*>{a1, a2};
105 8 });
106
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* s1 = emit_block_sum_f32(c, s_red, sums[0]);
107
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* s2 = emit_block_sum_f32(c, s_red, sums[1]);
108
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.
8 Value* cnt = c.kb.fmax(c.kb.i32_to_f32(c.b.build_sub(hi, lo)), c.f32(1.0f));
109
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* s1_over_cnt = c.b.build_sdiv(s1, cnt);
110
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* mean = c.b.build_add(K, s1_over_cnt);
111
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.
8 Value* q = c.kb.fmax(c.b.build_sub(s2, c.b.build_mul(s1, s1_over_cnt)), c.f32(0.0f));
112
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(tid, c.i32(0)), [&] {
113 8 Value* o = c.b.build_mul(c.b.build_add(c.b.build_mul(tile, p[11]), s), c.i32(3));
114 8 c.kb.store_f32(ptr_at(c, p[4], o, 4), cnt);
115 8 c.kb.store_f32(ptr_at(c, p[4], c.b.build_add(o, c.i32(1)), 4), mean);
116 8 c.kb.store_f32(ptr_at(c, p[4], c.b.build_add(o, c.i32(2)), 4), q);
117 8 });
118 8 }
119
120 8 void build_gn_apply(Ctx& c, const std::vector<Value*>& p, Dtype dt, bool silu, int lanes) {
121 8 const int esz = elem_bytes(dt);
122 8 Value* tile = c.kb.ctaid_y();
123 8 auto st = emit_tile_stats(c, p[4], tile, p[11], p[15]);
124 8 Value* mean = st[0];
125 8 Value* rstd = st[1];
126 8 Value* base = c.b.build_mul(tile, p[7]);
127 8 Value* c0 = c.b.build_mul(c.b.build_umod(tile, p[10]), p[9]);
128 8 Value* hw_v = vec_count(c, p[8], lanes);
129
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 emit_vec_grid_stride(c, p[7], lanes, kGnormBlock, [&](Value* iv) {
130 8 Value* ch = c.b.build_add(c0, c.b.build_udiv(iv, hw_v));
131
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 Value* ga = c.b.build_mul(load_lanes(c, dt, 1, ptr_at(c, p[2], ch, esz))[0], rstd);
132 8 Value* be = load_lanes(c, dt, 1, ptr_at(c, p[3], ch, esz))[0];
133 8 Value* idx = c.b.build_add(base, col_elem_idx(c, iv, lanes));
134 8 auto xs = load_lanes(c, dt, lanes, ptr_at(c, p[0], idx, esz));
135
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::vector<Value*> ys(static_cast<std::size_t>(lanes));
136
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 26 times.
34 for (int j = 0; j < lanes; ++j) {
137
2/4
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 Value* v = c.b.build_fma_f32(c.b.build_sub(xs[static_cast<std::size_t>(j)], mean), ga, be);
138
4/6
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
26 if (silu) v = c.b.build_mul(v, emit_sigmoid(c, v));
139 26 ys[static_cast<std::size_t>(j)] = v;
140 26 }
141
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 store_lanes(c, dt, lanes, ptr_at(c, p[1], idx, esz), ys);
142 8 });
143 8 }
144
145 9 void build_gn_bwd_ch(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
146 9 const int esz = elem_bytes(dt);
147 9 Value* ch = c.kb.ctaid_x();
148 9 Value* img = c.kb.ctaid_y();
149 9 Value* tid = c.kb.tid_x();
150 9 Value* tile = c.b.build_add(c.b.build_mul(img, p[10]), c.b.build_udiv(ch, p[9]));
151 9 auto st = emit_tile_stats(c, p[4], tile, p[11], p[15]);
152 9 Value* s_red = c.kb.shared_alloc_f32(8);
153 9 Value* base = c.b.build_mul(c.b.build_add(c.b.build_mul(img, p[14]), ch), p[8]);
154
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto sums = c.kb.for_range_reduce_n(
155 9 tid, p[8], c.i32(static_cast<int>(kGnormBlock)), {c.f32(0.0f), c.f32(0.0f)},
156
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 [&](Value* i, const std::vector<Value*>& cur) {
157 9 Value* idx = c.b.build_add(base, i);
158 9 Value* dy = load_lanes(c, dt, 1, ptr_at(c, p[3], idx, esz))[0];
159 9 Value* xv = load_lanes(c, dt, 1, ptr_at(c, p[0], idx, esz))[0];
160 9 Value* xh = c.b.build_mul(c.b.build_sub(xv, st[0]), st[1]);
161 9 return std::vector<Value*>{c.b.build_add(cur[0], dy), c.b.build_fma_f32(dy, xh, cur[1])};
162 });
163
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Value* s1 = emit_block_sum_f32(c, s_red, sums[0]);
164
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Value* s2 = emit_block_sum_f32(c, s_red, sums[1]);
165
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
18 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
166 9 Value* o = c.b.build_shl(c.b.build_add(c.b.build_mul(img, p[14]), ch), c.i32(1));
167 9 c.kb.store_f32(ptr_at(c, p[5], o, 4), s1);
168 9 c.kb.store_f32(ptr_at(c, p[5], c.b.build_add(o, c.i32(1)), 4), s2);
169
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 c.kb.if_then(c.b.build_eq(c.b.build_umod(ch, p[9]), c.i32(0)), [&] {
170 9 Value* t2 = c.b.build_shl(tile, c.i32(1));
171 9 c.kb.store_f32(ptr_at(c, p[6], t2, 4), st[0]);
172 9 c.kb.store_f32(ptr_at(c, p[6], c.b.build_add(t2, c.i32(1)), 4), st[1]);
173 9 });
174 9 });
175 9 }
176
177 9 void build_gn_bwd_dx(Ctx& c, const std::vector<Value*>& p, Dtype dt) {
178 9 const int esz = elem_bytes(dt);
179 9 Value* tid = c.kb.tid_x();
180 9 Value* block = c.i32(static_cast<int>(kGnormBlock));
181 9 Value* tile = c.kb.ctaid_y();
182 9 Value* img = c.b.build_udiv(tile, p[10]);
183 9 Value* c0 = c.b.build_mul(c.b.build_umod(tile, p[10]), p[9]);
184 9 Value* s_red = c.kb.shared_alloc_f32(8);
185
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 auto sums = c.kb.for_range_reduce_n(tid, p[9], block, {c.f32(0.0f), c.f32(0.0f)},
186
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
18 [&](Value* k, const std::vector<Value*>& cur) {
187 9 Value* ck = c.b.build_add(c0, k);
188 9 Value* o = c.b.build_shl(c.b.build_add(c.b.build_mul(img, p[14]), ck), c.i32(1));
189 9 Value* gv = load_lanes(c, dt, 1, ptr_at(c, p[2], ck, esz))[0];
190 9 Value* r0 = c.kb.load_f32(ptr_at(c, p[5], o, 4));
191 9 Value* r1 = c.kb.load_f32(ptr_at(c, p[5], c.b.build_add(o, c.i32(1)), 4));
192 9 return std::vector<Value*>{c.b.build_fma_f32(gv, r0, cur[0]), c.b.build_fma_f32(gv, r1, cur[1])};
193 });
194
5/10
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✗ Branch 9 not taken.
9 Value* inv_m = c.b.build_sdiv(c.f32(1.0f), c.kb.fmax(c.kb.i32_to_f32(p[7]), c.f32(1.0f)));
195
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 Value* sum1 = c.b.build_mul(emit_block_sum_f32(c, s_red, sums[0]), inv_m);
196
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 Value* sum2 = c.b.build_mul(emit_block_sum_f32(c, s_red, sums[1]), inv_m);
197
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 Value* t2 = c.b.build_shl(tile, c.i32(1));
198
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 Value* mean = c.kb.load_f32(ptr_at(c, p[6], t2, 4));
199
4/8
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 9 times.
✗ Branch 7 not taken.
9 Value* rstd = c.kb.load_f32(ptr_at(c, p[6], c.b.build_add(t2, c.i32(1)), 4));
200
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Value* base = c.b.build_mul(tile, p[7]);
201
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 Value* gid_x = c.kb.global_tid_x();
202
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
9 Value* stride = c.kb.mul(c.kb.nctaid_x(), block);
203
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 c.kb.for_range(gid_x, p[7], stride, [&](Value* i) {
204 9 Value* ch = c.b.build_add(c0, c.b.build_udiv(i, p[8]));
205 9 Value* idx = c.b.build_add(base, i);
206 9 Value* xv = load_lanes(c, dt, 1, ptr_at(c, p[0], idx, esz))[0];
207 9 Value* dy = load_lanes(c, dt, 1, ptr_at(c, p[3], idx, esz))[0];
208 9 Value* gv = load_lanes(c, dt, 1, ptr_at(c, p[2], ch, esz))[0];
209 9 Value* xh = c.b.build_mul(c.b.build_sub(xv, mean), rstd);
210 9 Value* dxh = c.b.build_mul(dy, gv);
211 9 Value* corr = c.b.build_fma_f32(xh, sum2, sum1);
212
2/4
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
18 store_lanes(c, dt, 1, ptr_at(c, p[1], idx, esz),
213 9 {c.b.build_mul(rstd, c.b.build_sub(dxh, corr))});
214 9 });
215 9 }
216
217 10 void build_gn_misc(Ctx& c, const std::vector<Value*>& p, std::uint32_t mode, Dtype dt) {
218 10 const int esz = elem_bytes(dt);
219 10 Value* block = c.i32(static_cast<int>(kGnormBlock));
220
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 3 times.
10 if (mode == GN_BWD_PARAM) {
221
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, p[14], 1, kGnormBlock, [&](Value* ch) {
222
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto sums = c.kb.for_range_reduce_n(c.i32(0), p[13], c.i32(1), {c.f32(0.0f), c.f32(0.0f)},
223
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 [&](Value* img, const std::vector<Value*>& cur) {
224 3 Value* o = c.b.build_shl(c.b.build_add(c.b.build_mul(img, p[14]), ch), c.i32(1));
225 3 Value* v1 = c.kb.load_f32(ptr_at(c, p[5], o, 4));
226 3 Value* v2 = c.kb.load_f32(ptr_at(c, p[5], c.b.build_add(o, c.i32(1)), 4));
227 3 return std::vector<Value*>{c.b.build_add(cur[0], v1), c.b.build_add(cur[1], v2)};
228 });
229
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* yp = ptr_at(c, p[1], ch, esz);
230
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* gp = ptr_at(c, p[2], ch, esz);
231
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 store_lanes(c, dt, 1, yp, {c.b.build_add(load_lanes(c, dt, 1, yp)[0], sums[1])});
232
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
3 store_lanes(c, dt, 1, gp, {c.b.build_add(load_lanes(c, dt, 1, gp)[0], sums[0])});
233 3 });
234
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
10 } else if (mode == GN_BN_INFER) {
235
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 emit_vec_grid_stride(c, p[13], 1, kGnormBlock, [&](Value* i) {
236 3 Value* ch = c.b.build_umod(c.b.build_udiv(i, p[8]), p[14]);
237 3 Value* gv = load_lanes(c, dt, 1, ptr_at(c, p[2], ch, esz))[0];
238 3 Value* bv = load_lanes(c, dt, 1, ptr_at(c, p[3], ch, esz))[0];
239 3 Value* rmv = load_lanes(c, dt, 1, ptr_at(c, p[5], ch, esz))[0];
240 3 Value* rvv = load_lanes(c, dt, 1, ptr_at(c, p[6], ch, esz))[0];
241 3 Value* a = c.b.build_mul(gv, emit_rsqrt(c, c.kb.fmax(c.b.build_add(rvv, p[15]), c.f32(1e-12f))));
242 3 Value* bb = c.b.build_sub(bv, c.b.build_mul(rmv, a));
243 3 Value* xv = load_lanes(c, dt, 1, ptr_at(c, p[0], i, esz))[0];
244
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 store_lanes(c, dt, 1, ptr_at(c, p[1], i, esz), {c.b.build_fma_f32(xv, a, bb)});
245 3 });
246
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 time.
7 } else if (mode == GN_BIAS_GRAD) {
247 3 Value* ch = c.b.build_add(c.b.build_mul(c.kb.ctaid_y(), c.i32(65535)), c.kb.ctaid_x());
248
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.if_then(c.b.build_ult(ch, p[14]), [&] {
249 3 Value* tid = c.kb.tid_x();
250 3 Value* s_red = c.kb.shared_alloc_f32(8);
251
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 Value* s_acc = c.kb.for_range_reduce(c.i32(0), p[13], c.i32(1), c.f32(0.0f),
252 6 [&](Value* img, Value* cur) {
253 3 Value* base = c.b.build_mul(c.b.build_add(c.b.build_mul(img, p[14]), ch), p[8]);
254
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 return c.kb.for_range_reduce(tid, p[8], block, cur, [&](Value* i, Value* inner) {
255 3 Value* xv = load_lanes(c, dt, 1, ptr_at(c, p[0], c.b.build_add(base, i), esz))[0];
256 3 return c.b.build_add(inner, xv);
257 });
258 ✗ });
259 3 Value* tot = emit_block_sum_f32(c, s_red, s_acc);
260
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 c.kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
261 3 Value* yp = ptr_at(c, p[1], ch, esz);
262
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 store_lanes(c, dt, 1, yp, {c.b.build_add(load_lanes(c, dt, 1, yp)[0], tot)});
263 3 });
264 3 });
265 3 } else { // GN_L2_NCHW
266 1 Value* eps_safe = c.kb.fmax(p[15], c.f32(1e-30f));
267
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 emit_vec_grid_stride(c, p[13], 1, kGnormBlock, [&](Value* i) {
268 1 Value* img = c.b.build_udiv(i, p[8]);
269 1 Value* px = c.b.build_umod(i, p[8]);
270 1 Value* base = c.b.build_add(c.b.build_mul(c.b.build_mul(img, p[14]), p[8]), px);
271
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 Value* ss = c.kb.for_range_reduce(c.i32(0), p[14], c.i32(1), c.f32(0.0f), [&](Value* ch, Value* cur) {
272 1 Value* v = load_lanes(c, dt, 1, ptr_at(c, p[0], c.b.build_add(base, c.b.build_mul(ch, p[8])), esz))[0];
273 1 return c.b.build_fma_f32(v, v, cur);
274 });
275 1 Value* inv = c.b.build_sdiv(c.f32(1.0f), c.kb.fmax(emit_sqrt(c, c.kb.fmax(ss, c.f32(0.0f))), eps_safe));
276
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
2 c.kb.for_range(c.i32(0), p[14], c.i32(1), [&](Value* ch) {
277 1 Value* o = c.b.build_add(base, c.b.build_mul(ch, p[8]));
278 1 Value* v = load_lanes(c, dt, 1, ptr_at(c, p[0], o, esz))[0];
279
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 store_lanes(c, dt, 1, ptr_at(c, p[1], o, esz), {c.b.build_mul(v, inv)});
280 1 });
281 1 });
282 }
283 10 }
284
285 } // namespace
286
287 44 Function* build_gnorm_mir(Module& mod, std::uint32_t mode, Dtype dt, bool silu, int lanes) {
288 88 Function* fn = mod.create_function(
289 44 "mir_gnorm", Type::void_type(),
290 440 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
291 308 Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(), Type::i32(),
292 88 Type::i32(), Type::f32()});
293 44 brass::codegen::KernelBuilder kb(mod, fn);
294
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 Ctx c(kb);
295
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 auto p = entry_params(c, fn);
296
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 36 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
44 if (mode == GN_STATS) build_gn_stats(c, p, dt, lanes);
297
3/4
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 28 times.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
36 else if (mode == GN_APPLY) build_gn_apply(c, p, dt, silu, lanes);
298
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
28 else if (mode == GN_BWD_CH) build_gn_bwd_ch(c, p, dt);
299
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
19 else if (mode == GN_BWD_DX) build_gn_bwd_dx(c, p, dt);
300
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 else build_gn_misc(c, p, mode, dt);
301
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 c.b.build_ret_void();
302
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 return finalize_fn(fn);
303 44 }
304
305 5 Function* build_bnorm_mir(Module& mod, std::uint32_t mode) {
306 10 Function* fn = mod.create_function(
307 5 "mir_bnorm", Type::void_type(),
308 55 {Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
309 25 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(),
310 25 Type::i32(), Type::i32(), Type::i32(), Type::f32(), Type::f32()});
311 5 brass::codegen::KernelBuilder kb(mod, fn);
312
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Ctx c(kb);
313
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 auto p = entry_params(c, fn);
314
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* tid = kb.tid_x();
315
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* block = c.i32(static_cast<int>(kGnormBlock));
316
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* s_red = kb.shared_alloc_f32(8);
317 5 Value* N = p[11];
318 5 Value* C = p[12];
319 5 Value* hw = p[13];
320
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 Value* M = c.b.build_mul(N, hw);
321
3/6
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✗ Branch 5 not taken.
5 Value* M_safe = kb.fmax(kb.i32_to_f32(M), c.f32(1.0f));
322
2/4
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
5 Value* invM = c.b.build_sdiv(c.f32(1.0f), M_safe);
323
324 17 auto idx_at = [&](Value* ch, Value* j) -> Value* {
325 12 Value* n = c.b.build_udiv(j, hw);
326 12 Value* px = c.b.build_sub(j, c.b.build_mul(n, hw));
327 12 return c.b.build_add(c.b.build_mul(c.b.build_add(c.b.build_mul(n, C), ch), hw), px);
328 };
329
330
4/8
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
✓ 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.
10 kb.for_range(kb.ctaid_x(), C, kb.nctaid_x(), [&](Value* ch) {
331
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 2 times.
5 if (mode == BN_TRAIN_FWD) {
332
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* sum = kb.for_range_reduce(tid, M, block, c.f32(0.0f), [&](Value* j, Value* a) {
333 2 return c.b.build_add(a, kb.load_f32(ptr_at(c, p[0], idx_at(ch, j), 4)));
334 });
335 2 Value* mean = c.b.build_mul(emit_block_sum_f32(c, s_red, sum), invM);
336
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 Value* sq = kb.for_range_reduce(tid, M, block, c.f32(0.0f), [&](Value* j, Value* a) {
337 2 Value* d = c.b.build_sub(kb.load_f32(ptr_at(c, p[0], idx_at(ch, j), 4)), mean);
338 2 return c.b.build_fma_f32(d, d, a);
339 });
340 2 Value* var_b = c.b.build_mul(emit_block_sum_f32(c, s_red, sq), invM);
341 2 Value* rstd = emit_rsqrt(c, kb.fmax(c.b.build_add(var_b, p[14]), c.f32(1e-12f)));
342 2 Value* mom = p[15];
343 2 Value* one_m = c.b.build_sub(c.f32(1.0f), mom);
344 2 Value* m_sub1 = c.b.build_select(c.b.build_ugt(M, c.i32(1)), c.b.build_sub(M, c.i32(1)), c.i32(1));
345 4 Value* bessel = c.b.build_select(
346 2 c.b.build_ugt(M, c.i32(1)),
347 2 c.b.build_sdiv(M_safe, kb.i32_to_f32(m_sub1)),
348 2 c.f32(1.0f));
349
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
350 2 kb.store_f32(ptr_at(c, p[6], ch, 4), mean);
351 2 kb.store_f32(ptr_at(c, p[7], ch, 4), rstd);
352 2 Value* rm_p = ptr_at(c, p[4], ch, 4);
353 2 Value* rv_p = ptr_at(c, p[5], ch, 4);
354 2 kb.store_f32(rm_p, c.b.build_fma_f32(one_m, kb.load_f32(rm_p), c.b.build_mul(mom, mean)));
355 4 kb.store_f32(rv_p, c.b.build_fma_f32(one_m, kb.load_f32(rv_p),
356 2 c.b.build_mul(mom, c.b.build_mul(var_b, bessel))));
357 2 });
358 2 Value* gv = kb.load_f32(ptr_at(c, p[2], ch, 4));
359 2 Value* bv = kb.load_f32(ptr_at(c, p[3], ch, 4));
360
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 kb.for_range(tid, M, block, [&](Value* j) {
361 2 Value* i = idx_at(ch, j);
362 2 Value* xh = c.b.build_mul(c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)), mean), rstd);
363 2 kb.store_f32(ptr_at(c, p[1], i, 4), c.b.build_fma_f32(xh, gv, bv));
364 2 });
365 2 } else {
366 3 Value* mean = kb.load_f32(ptr_at(c, p[6], ch, 4));
367 3 Value* rstd = kb.load_f32(ptr_at(c, p[7], ch, 4));
368 3 Value* gv = kb.load_f32(ptr_at(c, p[2], ch, 4));
369
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 auto sums = kb.for_range_reduce_n(tid, M, block, {c.f32(0.0f), c.f32(0.0f)},
370
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 [&](Value* j, const std::vector<Value*>& cur) {
371 3 Value* i = idx_at(ch, j);
372 3 Value* dyv = kb.load_f32(ptr_at(c, p[8], i, 4));
373 3 Value* xh = c.b.build_mul(c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)), mean), rstd);
374 3 return std::vector<Value*>{c.b.build_add(cur[0], dyv), c.b.build_fma_f32(dyv, xh, cur[1])};
375 });
376
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* t0 = emit_block_sum_f32(c, s_red, sums[0]);
377
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* t1 = emit_block_sum_f32(c, s_red, sums[1]);
378
4/8
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
6 kb.if_then(c.b.build_eq(tid, c.i32(0)), [&] {
379 3 Value* dgp = ptr_at(c, p[9], ch, 4);
380 3 Value* dbp = ptr_at(c, p[10], ch, 4);
381 3 kb.store_f32(dgp, c.b.build_add(kb.load_f32(dgp), t1));
382 3 kb.store_f32(dbp, c.b.build_add(kb.load_f32(dbp), t0));
383 3 });
384
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* s1 = c.b.build_mul(gv, t0);
385
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 Value* s2 = c.b.build_mul(gv, t1);
386
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
6 kb.for_range(tid, M, block, [&](Value* j) {
387 3 Value* i = idx_at(ch, j);
388 3 Value* xh = c.b.build_mul(c.b.build_sub(kb.load_f32(ptr_at(c, p[0], i, 4)), mean), rstd);
389 3 Value* dxh = c.b.build_mul(kb.load_f32(ptr_at(c, p[8], i, 4)), gv);
390 3 Value* corr = c.b.build_mul(c.b.build_fma_f32(xh, s2, s1), invM);
391 3 kb.store_f32(ptr_at(c, p[1], i, 4), c.b.build_mul(rstd, c.b.build_sub(dxh, corr)));
392 3 });
393 3 }
394 5 });
395
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 c.b.build_ret_void();
396
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 return finalize_fn(fn);
397 5 }
398
399 } // namespace brotensor::kernels
400