brotensor/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
564 / 686
82.2%
Branches:
533 / 1128
47.3%
Functions:
9 / 22
40.9%

src/cpu/cpu_jit_fused.cpp
Line Branch Exec Source
1 #include "cpu_jit.h"
2 #include <brotensor/detail/cpu/thread_pool.h>
3 #include <algorithm>
4 #include <cmath>
5 #include <cstdint>
6
7 namespace brotensor::detail::cpu::jit {
8 namespace ref {
9
10 ✗ inline void fused_residual_rmsnorm(float* X, const float* res, const float* gamma, float eps, float* Y, int B, int D) {
11
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (B <= 0 || D <= 0) return;
12 ✗ const float inv_D = 1.0f / static_cast<float>(D);
13 ✗ auto row_fn = [&](int b) {
14 ✗ float* xr = X + static_cast<size_t>(b) * D;
15 ✗ const float* rr = res + static_cast<size_t>(b) * D;
16 ✗ float* yr = Y + static_cast<size_t>(b) * D;
17 ✗ float sum = 0.0f;
18
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
19 ✗ xr[i] += rr[i];
20 ✗ sum += xr[i] * xr[i];
21 ✗ }
22 ✗ float rrms = 1.0f / std::sqrt(sum * inv_D + eps);
23
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
24 ✗ yr[i] = xr[i] * gamma[i] * rrms;
25 ✗ }
26 ✗ };
27
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
28
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) {
29 ✗ row_fn(static_cast<int>(bi));
30 ✗ });
31 ✗ } else {
32
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < B; ++b) row_fn(b);
33 }
34 ✗ }
35
36 ✗ inline void fused_residual_layernorm(float* X, const float* res, const float* gamma, const float* beta,
37 float eps, float* Y, int B, int D) {
38
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (B <= 0 || D <= 0) return;
39 ✗ const float inv_D = 1.0f / static_cast<float>(D);
40 ✗ auto row_fn = [&](int b) {
41 ✗ float* xr = X + static_cast<size_t>(b) * D;
42 ✗ const float* rr = res + static_cast<size_t>(b) * D;
43 ✗ float* yr = Y + static_cast<size_t>(b) * D;
44 ✗ float sum = 0.0f;
45
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
46 ✗ xr[i] += rr[i];
47 ✗ sum += xr[i];
48 ✗ }
49 ✗ float mean = sum * inv_D;
50 ✗ float sumsq = 0.0f;
51
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
52 ✗ float diff = xr[i] - mean;
53 ✗ sumsq += diff * diff;
54 ✗ }
55 ✗ float var = sumsq * inv_D;
56 ✗ float rstd = 1.0f / std::sqrt(var + eps);
57
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
58
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ float g = gamma ? gamma[i] : 1.0f;
59
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ float bv = beta ? beta[i] : 0.0f;
60 ✗ yr[i] = g * ((xr[i] - mean) * rstd) + bv;
61 ✗ }
62 ✗ };
63
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
64
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) {
65 ✗ row_fn(static_cast<int>(bi));
66 ✗ });
67 ✗ } else {
68
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int b = 0; b < B; ++b) row_fn(b);
69 }
70 ✗ }
71
72 ✗ inline void fused_layernorm_modulate(const float* X, const float* gamma, const float* beta,
73 const float* scale, const float* shift, float eps, float* Y, int R, int D) {
74
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (R <= 0 || D <= 0) return;
75 ✗ const float inv_D = 1.0f / static_cast<float>(D);
76 ✗ auto row_fn = [&](int r) {
77 ✗ const float* xr = X + static_cast<size_t>(r) * D;
78 ✗ float* yr = Y + static_cast<size_t>(r) * D;
79 ✗ float sum = 0.0f;
80
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) sum += xr[i];
81 ✗ float mean = sum * inv_D;
82 ✗ float sumsq = 0.0f;
83
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
84 ✗ float diff = xr[i] - mean;
85 ✗ sumsq += diff * diff;
86 ✗ }
87 ✗ float var = sumsq * inv_D;
88 ✗ float rstd = 1.0f / std::sqrt(var + eps);
89
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int i = 0; i < D; ++i) {
90
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ float g = gamma ? gamma[i] : 1.0f;
91
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ float bv = beta ? beta[i] : 0.0f;
92 ✗ float norm = g * ((xr[i] - mean) * rstd) + bv;
93 ✗ yr[i] = norm * (1.0f + scale[i]) + shift[i];
94 ✗ }
95 ✗ };
96
0/4
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ if (R > 1 && static_cast<int64_t>(R) * D >= 262144) {
97
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<size_t>(R), [&](size_t ri) {
98 ✗ row_fn(static_cast<int>(ri));
99 ✗ });
100 ✗ } else {
101
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ for (int r = 0; r < R; ++r) row_fn(r);
102 }
103 ✗ }
104
105 } // namespace ref
106 } // namespace brotensor::detail::cpu::jit
107
108 #if BROTENSOR_HAS_BRASS_JIT
109
110 #include <brass/codegen/kernel_jit.hpp>
111 #include <brass/mir/builder.hpp>
112 #include <brass/mir/module.hpp>
113 #include <brass/mir/function.hpp>
114 #include "../jit/kernel_disk_cache.h"
115 #include <atomic>
116 #include <cmath>
117 #include <mutex>
118 #include <vector>
119 #include <memory>
120 #include <algorithm>
121 #include <iostream>
122
123 namespace brotensor::detail::cpu::jit {
124
125 namespace {
126
127 using namespace brass;
128 using namespace brass::codegen;
129
130 struct JitFusedKernels {
131 bool available = false;
132
133 using FusedResidualRmsNormRowFn = void (*)(float* x, const float* res, const float* gamma, float* y, uint64_t d, float eps, float inv_d);
134 FusedResidualRmsNormRowFn fused_residual_rmsnorm_row_fn = nullptr;
135
136 using FusedResidualLayerNormRowFn = void (*)(float* x, const float* res, const float* gamma, const float* beta, float* y, uint64_t d, float eps, float inv_d);
137 FusedResidualLayerNormRowFn fused_residual_layernorm_row_fn = nullptr;
138
139 using FusedLayerNormModulateRowFn = void (*)(const float* x, const float* gamma, const float* beta, const float* scale, const float* shift, float* y, uint64_t d, float eps, float inv_d);
140 FusedLayerNormModulateRowFn fused_layernorm_modulate_row_fn = nullptr;
141 };
142
143 // Horizontal sum of an 8-wide float vector using destination scratch buffer
144 20 static Value* reduce_vsum8(KernelBuilder& kb, Value* scratch, Value* vsum) {
145 20 kb.vstore_f32x8(scratch, vsum);
146 20 Value* s0 = kb.load_f32(scratch, 0); Value* s1 = kb.load_f32(scratch, 4);
147 20 Value* s2 = kb.load_f32(scratch, 8); Value* s3 = kb.load_f32(scratch, 12);
148 20 Value* s4 = kb.load_f32(scratch, 16); Value* s5 = kb.load_f32(scratch, 20);
149 20 Value* s6 = kb.load_f32(scratch, 24); Value* s7 = kb.load_f32(scratch, 28);
150 20 Value* sum0123 = kb.add(kb.add(s0, s1), kb.add(s2, s3));
151 20 Value* sum4567 = kb.add(kb.add(s4, s5), kb.add(s6, s7));
152 20 return kb.add(sum0123, sum4567);
153 }
154
155 // ── 1. Fused Residual RMSNorm Row (Option B: X += res in-place + RMSNorm) ───
156 //
157 // Computes in-place residual addition X += res, writes X, and accumulates sum_sq
158 // in the EXACT same vector register pass, eliminating a full memory read/write pass.
159 4 static void populate_fused_residual_rmsnorm_row(Module& mod) {
160 32 Function* fn = mod.create_function("fused_residual_rmsnorm_row", Type::void_type(), {
161 28 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32()
162 });
163 4 KernelBuilder kb(mod, fn);
164
165
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* entry = kb.builder().append_block("entry");
166
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x = kb.builder().add_block_param(entry, Type::ptr());
167
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* res = kb.builder().add_block_param(entry, Type::ptr());
168
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* gamma = kb.builder().add_block_param(entry, Type::ptr());
169
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* y = kb.builder().add_block_param(entry, Type::ptr());
170
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* d = kb.builder().add_block_param(entry, Type::i64());
171
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eps = kb.builder().add_block_param(entry, Type::f32());
172
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* inv_d = kb.builder().add_block_param(entry, Type::f32());
173
174
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vhead = kb.builder().create_block("res_rms_l1_vhead");
175
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vbody = kb.builder().create_block("res_rms_l1_vbody");
176
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vexit = kb.builder().create_block("res_rms_l1_vexit");
177
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_shead = kb.builder().create_block("res_rms_l1_shead");
178
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_sbody = kb.builder().create_block("res_rms_l1_sbody");
179
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_exit = kb.builder().create_block("res_rms_l1_exit");
180
181
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vhead = kb.builder().create_block("res_rms_l2_vhead");
182
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vbody = kb.builder().create_block("res_rms_l2_vbody");
183
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_shead = kb.builder().create_block("res_rms_l2_shead");
184
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_sbody = kb.builder().create_block("res_rms_l2_sbody");
185
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_exit = kb.builder().create_block("res_rms_l2_exit");
186
187 4 kb.position_at_end(entry);
188
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_i64 = kb.builder().build_iconst_i64(0);
189
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* one_i64 = kb.builder().build_iconst_i64(1);
190
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* two_i64 = kb.builder().build_iconst_i64(2);
191
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eight_i64 = kb.builder().build_iconst_i64(8);
192
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_f32 = kb.builder().build_fconst_f32(0.0f);
193
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vzero = kb.vzero(Type::f32x8());
194
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
195
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vec_d = kb.builder().build_and(d, mask_eight);
196
197
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* has_vec = kb.builder().build_sge(d, eight_i64);
198
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32});
199
200 // Pass 1: Vector loop X += res in-place AND accumulates sum_sq in same register pass
201
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vhead);
202
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64());
203
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vsum = kb.builder().add_block_param(l1_vhead, Type::f32x8());
204 4 kb.position_at_end(l1_vhead);
205
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d);
206
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum});
207
208
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vbody);
209 4 kb.position_at_end(l1_vbody);
210
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64);
211
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px1 = kb.builder().build_add(x, byte_off1);
212
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pres1 = kb.builder().build_add(res, byte_off1);
213
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx1 = kb.vload_f32x8(px1);
214
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vr1 = kb.vload_f32x8(pres1);
215
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx_new = kb.vadd(vx1, vr1);
216
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.vstore_f32x8(px1, vx_new); // X += res in-place
217
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_vsum = kb.vfma(vx_new, vx_new, vsum); // sum_sq accumulation in exact same pass
218
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64);
219
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum});
220
221 // Pass 1: Vector exit -> horizontal reduction
222
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vexit);
223
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64());
224
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_vsum = kb.builder().add_block_param(l1_vexit, Type::f32x8());
225 4 kb.position_at_end(l1_vexit);
226
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* init_scalar_sum = reduce_vsum8(kb, y, exit_vsum);
227
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {exit_iv1, init_scalar_sum});
228
229 // Pass 1: Scalar remainder
230
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_shead);
231
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64());
232
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sum_sq = kb.builder().add_block_param(l1_shead, Type::f32());
233 4 kb.position_at_end(l1_shead);
234
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_s = kb.builder().build_slt(iv1_s, d);
235
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum_sq});
236
237
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_sbody);
238 4 kb.position_at_end(l1_sbody);
239
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi = kb.load_f32_indexed(x, iv1_s, 4, 0);
240
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ri = kb.load_f32_indexed(res, iv1_s, 4, 0);
241
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi_new = kb.add(xi, ri);
242
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.store_f32_indexed(x, iv1_s, xi_new, 4, 0);
243
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi_sq = kb.mul(xi_new, xi_new);
244
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_sum = kb.add(sum_sq, xi_sq);
245
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64);
246
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {next_iv1_s, next_sum});
247
248 // Pass 1 exit -> compute rrms
249
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_exit);
250
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_sum = kb.builder().add_block_param(l1_exit, Type::f32());
251 4 kb.position_at_end(l1_exit);
252
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mean_sq = kb.mul(final_sum, inv_d);
253
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* denom = kb.add(mean_sq, eps);
254
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* rrms = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(denom));
255
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vrrms = kb.vbroadcast(Type::f32x8(), rrms);
256
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_vhead, {zero_i64, rrms, vrrms});
257
258 // Pass 2: Vector loop y = x * gamma * rrms
259
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vhead);
260
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64());
261
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* rrms_val = kb.builder().add_block_param(l2_vhead, Type::f32());
262
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vrrms_val = kb.builder().add_block_param(l2_vhead, Type::f32x8());
263 4 kb.position_at_end(l2_vhead);
264
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d);
265
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_shead, {iv2_v, rrms_val});
266
267
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vbody);
268 4 kb.position_at_end(l2_vbody);
269
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64);
270
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px2 = kb.builder().build_add(x, byte_off2);
271
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pg2 = kb.builder().build_add(gamma, byte_off2);
272
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* py2 = kb.builder().build_add(y, byte_off2);
273
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx2 = kb.vload_f32x8(px2);
274
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vg2 = kb.vload_f32x8(pg2);
275
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vxg = kb.vmul(vx2, vg2);
276
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vy = kb.vmul(vxg, vrrms_val);
277
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.vstore_f32x8(py2, vy);
278
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64);
279
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_vhead, {next_iv2_v, rrms_val, vrrms_val});
280
281 // Pass 2: Scalar remainder
282
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_shead);
283
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64());
284
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* rrms_scalar = kb.builder().add_block_param(l2_shead, Type::f32());
285 4 kb.position_at_end(l2_shead);
286
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_s = kb.builder().build_slt(iv2_s, d);
287
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_s, l2_sbody, l2_exit);
288
289
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_sbody);
290 4 kb.position_at_end(l2_sbody);
291
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x2 = kb.load_f32_indexed(x, iv2_s, 4, 0);
292
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* g2 = kb.load_f32_indexed(gamma, iv2_s, 4, 0);
293
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xg2 = kb.mul(x2, g2);
294
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* out_val = kb.mul(xg2, rrms_scalar);
295
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.store_f32_indexed(y, iv2_s, out_val, 4, 0);
296
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64);
297
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_shead, {next_iv2_s, rrms_scalar});
298
299
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_exit);
300 4 kb.position_at_end(l2_exit);
301
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_ret_void();
302 4 }
303
304 // ── 2. Fused Residual LayerNorm Row (X += res in-place + LayerNorm) ─────────
305 //
306 // Computes in-place residual addition X += res, writes X, and computes mean and
307 // variance across vector registers, writing Y = gamma * (X - mean) * rstd + beta
308 // directly in registers without intermediate buffers.
309 4 static void populate_fused_residual_layernorm_row(Module& mod) {
310 36 Function* fn = mod.create_function("fused_residual_layernorm_row", Type::void_type(), {
311 32 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32()
312 });
313 4 KernelBuilder kb(mod, fn);
314
315
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* entry = kb.builder().append_block("entry");
316
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x = kb.builder().add_block_param(entry, Type::ptr());
317
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* res = kb.builder().add_block_param(entry, Type::ptr());
318
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* gamma = kb.builder().add_block_param(entry, Type::ptr());
319
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* beta = kb.builder().add_block_param(entry, Type::ptr());
320
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* y = kb.builder().add_block_param(entry, Type::ptr());
321
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* d = kb.builder().add_block_param(entry, Type::i64());
322
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eps = kb.builder().add_block_param(entry, Type::f32());
323
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* inv_d = kb.builder().add_block_param(entry, Type::f32());
324
325 // Pass 1 blocks (residual add + mean)
326
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vhead = kb.builder().create_block("res_ln_l1_vhead");
327
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vbody = kb.builder().create_block("res_ln_l1_vbody");
328
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vexit = kb.builder().create_block("res_ln_l1_vexit");
329
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_shead = kb.builder().create_block("res_ln_l1_shead");
330
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_sbody = kb.builder().create_block("res_ln_l1_sbody");
331
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_exit = kb.builder().create_block("res_ln_l1_exit");
332
333 // Pass 2 blocks (variance)
334
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vhead = kb.builder().create_block("res_ln_l2_vhead");
335
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vbody = kb.builder().create_block("res_ln_l2_vbody");
336
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vexit = kb.builder().create_block("res_ln_l2_vexit");
337
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_shead = kb.builder().create_block("res_ln_l2_shead");
338
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_sbody = kb.builder().create_block("res_ln_l2_sbody");
339
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_exit = kb.builder().create_block("res_ln_l2_exit");
340
341 // Pass 3 blocks (gamma * xhat + beta)
342
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_vhead = kb.builder().create_block("res_ln_l3_vhead");
343
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_vbody = kb.builder().create_block("res_ln_l3_vbody");
344
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_shead = kb.builder().create_block("res_ln_l3_shead");
345
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_sbody = kb.builder().create_block("res_ln_l3_sbody");
346
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_exit = kb.builder().create_block("res_ln_l3_exit");
347
348 4 kb.position_at_end(entry);
349
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_i64 = kb.builder().build_iconst_i64(0);
350
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* one_i64 = kb.builder().build_iconst_i64(1);
351
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* two_i64 = kb.builder().build_iconst_i64(2);
352
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eight_i64 = kb.builder().build_iconst_i64(8);
353
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_f32 = kb.builder().build_fconst_f32(0.0f);
354
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vzero = kb.vzero(Type::f32x8());
355
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
356
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vec_d = kb.builder().build_and(d, mask_eight);
357
358
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* has_vec = kb.builder().build_sge(d, eight_i64);
359
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32});
360
361 // Pass 1: Vector sum + in-place residual addition X += res
362
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vhead);
363
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64());
364
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8());
365 4 kb.position_at_end(l1_vhead);
366
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d);
367
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1});
368
369
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vbody);
370 4 kb.position_at_end(l1_vbody);
371
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64);
372
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px1 = kb.builder().build_add(x, byte_off1);
373
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pres1 = kb.builder().build_add(res, byte_off1);
374
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx1 = kb.vload_f32x8(px1);
375
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vr1 = kb.vload_f32x8(pres1);
376
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx_new = kb.vadd(vx1, vr1);
377
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.vstore_f32x8(px1, vx_new); // X += res in-place!
378
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_vsum1 = kb.vadd(vsum1, vx_new);
379
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64);
380
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1});
381
382 // Pass 1: Vector exit -> horizontal sum
383
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vexit);
384
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64());
385
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8());
386 4 kb.position_at_end(l1_vexit);
387
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1);
388
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {exit_iv1, init_sum1});
389
390 // Pass 1: Scalar remainder
391
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_shead);
392
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64());
393
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32());
394 4 kb.position_at_end(l1_shead);
395
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_s = kb.builder().build_slt(iv1_s, d);
396
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1});
397
398
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_sbody);
399 4 kb.position_at_end(l1_sbody);
400
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0);
401
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ri1 = kb.load_f32_indexed(res, iv1_s, 4, 0);
402
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi_new = kb.add(xi1, ri1);
403
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.store_f32_indexed(x, iv1_s, xi_new, 4, 0); // X += res in-place!
404
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_sum1 = kb.add(sum1, xi_new);
405
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64);
406
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1});
407
408 // Pass 1: Mean computed -> enter Pass 2
409
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_exit);
410
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32());
411 4 kb.position_at_end(l1_exit);
412
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mean = kb.mul(final_sum1, inv_d);
413
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vmean = kb.vbroadcast(Type::f32x8(), mean);
414
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(has_vec, l2_vhead, {zero_i64, vzero, mean, vmean}, l2_shead, {zero_i64, zero_f32, mean});
415
416 // Pass 2: Vector sum of squared deviations
417
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vhead);
418
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64());
419
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
420
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32());
421
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
422 4 kb.position_at_end(l2_vhead);
423
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d);
424
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar});
425
426
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vbody);
427 4 kb.position_at_end(l2_vbody);
428
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64);
429
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px2 = kb.builder().build_add(x, byte_off2);
430
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx2 = kb.vload_f32x8(px2);
431
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vdev = kb.vsub(vx2, vm2);
432
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2);
433
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64);
434
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2});
435
436 // Pass 2: Vector exit -> horizontal sum
437
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vexit);
438
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64());
439
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8());
440
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32());
441 4 kb.position_at_end(l2_vexit);
442
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2);
443
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out});
444
445 // Pass 2: Scalar remainder
446
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_shead);
447
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64());
448
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32());
449
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32());
450 4 kb.position_at_end(l2_shead);
451
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_s = kb.builder().build_slt(iv2_s, d);
452
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s});
453
454
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_sbody);
455 4 kb.position_at_end(l2_sbody);
456
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0);
457
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff2 = kb.sub(xi2, m2_s);
458
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff_sq2 = kb.mul(diff2, diff2);
459
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_sumsq = kb.add(sumsq, diff_sq2);
460
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64);
461
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s});
462
463 // Pass 2 exit -> compute rstd
464
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_exit);
465
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32());
466
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32());
467 4 kb.position_at_end(l2_exit);
468
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* var = kb.mul(final_sumsq, inv_d);
469
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* var_eps = kb.add(var, eps);
470
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps));
471
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean);
472
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd);
473
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_vhead, {zero_i64, final_mean, rstd, vmean3, vrstd3});
474
475 // Pass 3: Vector LayerNorm Y = gamma * (X - mean) * rstd + beta
476
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_vhead);
477
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64());
478
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
479
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
480
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
481
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
482 4 kb.position_at_end(l3_vhead);
483
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d);
484
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s});
485
486
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_vbody);
487 4 kb.position_at_end(l3_vbody);
488
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64);
489
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px3 = kb.builder().build_add(x, byte_off3);
490
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pg3 = kb.builder().build_add(gamma, byte_off3);
491
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pb3 = kb.builder().build_add(beta, byte_off3);
492
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* py3 = kb.builder().build_add(y, byte_off3);
493
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx3 = kb.vload_f32x8(px3);
494
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vg3 = kb.vload_f32x8(pg3);
495
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vb3 = kb.vload_f32x8(pb3);
496
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vdiff3 = kb.vsub(vx3, vm3);
497
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vxhat3 = kb.vmul(vdiff3, vr3);
498
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vy3 = kb.vfma(vg3, vxhat3, vb3); // y = gamma * xhat + beta
499
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.vstore_f32x8(py3, vy3);
500
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64);
501
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3});
502
503 // Pass 3: Scalar remainder
504
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_shead);
505
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64());
506
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
507
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
508 4 kb.position_at_end(l3_shead);
509
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond3_s = kb.builder().build_slt(iv3_s, d);
510
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit);
511
512
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_sbody);
513 4 kb.position_at_end(l3_sbody);
514
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0);
515
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0);
516
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0);
517
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff3 = kb.sub(x3, m3_sc);
518
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xhat3 = kb.mul(diff3, r3_sc);
519
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* y3 = kb.builder().build_fma_f32(g3, xhat3, b3);
520
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.store_f32_indexed(y, iv3_s, y3, 4, 0);
521
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64);
522
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc});
523
524
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_exit);
525 4 kb.position_at_end(l3_exit);
526
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_ret_void();
527 4 }
528
529 // ── 3. Fused LayerNorm + Modulate Row (Option B: Zero Intermediate Memory) ──
530 //
531 // Computes LayerNorm directly into registers and immediately applies AdaLN
532 // modulation by (1 + scale) + shift into output Y, writing zero intermediate
533 // tensors to memory.
534 4 static void populate_fused_layernorm_modulate_row(Module& mod) {
535 40 Function* fn = mod.create_function("fused_layernorm_modulate_row", Type::void_type(), {
536 36 Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32()
537 });
538 4 KernelBuilder kb(mod, fn);
539
540
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* entry = kb.builder().append_block("entry");
541
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x = kb.builder().add_block_param(entry, Type::ptr());
542
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* gamma = kb.builder().add_block_param(entry, Type::ptr());
543
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* beta = kb.builder().add_block_param(entry, Type::ptr());
544
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* scale = kb.builder().add_block_param(entry, Type::ptr());
545
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* shift = kb.builder().add_block_param(entry, Type::ptr());
546
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* y = kb.builder().add_block_param(entry, Type::ptr());
547
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* d = kb.builder().add_block_param(entry, Type::i64());
548
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eps = kb.builder().add_block_param(entry, Type::f32());
549
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* inv_d = kb.builder().add_block_param(entry, Type::f32());
550
551 // Pass 1 blocks (mean)
552
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vhead = kb.builder().create_block("flm_l1_vhead");
553
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vbody = kb.builder().create_block("flm_l1_vbody");
554
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_vexit = kb.builder().create_block("flm_l1_vexit");
555
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_shead = kb.builder().create_block("flm_l1_shead");
556
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_sbody = kb.builder().create_block("flm_l1_sbody");
557
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l1_exit = kb.builder().create_block("flm_l1_exit");
558
559 // Pass 2 blocks (variance)
560
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vhead = kb.builder().create_block("flm_l2_vhead");
561
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vbody = kb.builder().create_block("flm_l2_vbody");
562
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_vexit = kb.builder().create_block("flm_l2_vexit");
563
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_shead = kb.builder().create_block("flm_l2_shead");
564
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_sbody = kb.builder().create_block("flm_l2_sbody");
565
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l2_exit = kb.builder().create_block("flm_l2_exit");
566
567 // Pass 3 blocks (fused LayerNorm + Modulate directly to Y)
568
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_vhead = kb.builder().create_block("flm_l3_vhead");
569
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_vbody = kb.builder().create_block("flm_l3_vbody");
570
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_shead = kb.builder().create_block("flm_l3_shead");
571
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_sbody = kb.builder().create_block("flm_l3_sbody");
572
2/4
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 BasicBlock* l3_exit = kb.builder().create_block("flm_l3_exit");
573
574 4 kb.position_at_end(entry);
575
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_i64 = kb.builder().build_iconst_i64(0);
576
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* one_i64 = kb.builder().build_iconst_i64(1);
577
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* two_i64 = kb.builder().build_iconst_i64(2);
578
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* eight_i64 = kb.builder().build_iconst_i64(8);
579
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* zero_f32 = kb.builder().build_fconst_f32(0.0f);
580
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* one_f32 = kb.builder().build_fconst_f32(1.0f);
581
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vzero = kb.vzero(Type::f32x8());
582
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vone = kb.vbroadcast(Type::f32x8(), one_f32);
583
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7));
584
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vec_d = kb.builder().build_and(d, mask_eight);
585
586
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* has_vec = kb.builder().build_sge(d, eight_i64);
587
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32});
588
589 // Pass 1: Vector sum
590
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vhead);
591
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64());
592
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8());
593 4 kb.position_at_end(l1_vhead);
594
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d);
595
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1});
596
597
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vbody);
598 4 kb.position_at_end(l1_vbody);
599
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64);
600
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px1 = kb.builder().build_add(x, byte_off1);
601
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx1 = kb.vload_f32x8(px1);
602
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_vsum1 = kb.vadd(vsum1, vx1);
603
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64);
604
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1});
605
606 // Pass 1: Vector exit -> horizontal sum
607
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_vexit);
608
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64());
609
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8());
610 4 kb.position_at_end(l1_vexit);
611
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1);
612
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {exit_iv1, init_sum1});
613
614 // Pass 1: Scalar remainder
615
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_shead);
616
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64());
617
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32());
618 4 kb.position_at_end(l1_shead);
619
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond1_s = kb.builder().build_slt(iv1_s, d);
620
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1});
621
622
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_sbody);
623 4 kb.position_at_end(l1_sbody);
624
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0);
625
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_sum1 = kb.add(sum1, xi1);
626
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64);
627
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1});
628
629 // Pass 1: Mean computed -> enter Pass 2
630
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l1_exit);
631
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32());
632 4 kb.position_at_end(l1_exit);
633
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* mean = kb.mul(final_sum1, inv_d);
634
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vmean = kb.vbroadcast(Type::f32x8(), mean);
635
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(has_vec, l2_vhead, {zero_i64, vzero, mean, vmean}, l2_shead, {zero_i64, zero_f32, mean});
636
637 // Pass 2: Vector sum of squared deviations
638
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vhead);
639
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64());
640
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
641
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32());
642
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8());
643 4 kb.position_at_end(l2_vhead);
644
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d);
645
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar});
646
647
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vbody);
648 4 kb.position_at_end(l2_vbody);
649
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64);
650
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px2 = kb.builder().build_add(x, byte_off2);
651
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx2 = kb.vload_f32x8(px2);
652
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vdev = kb.vsub(vx2, vm2);
653
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2);
654
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64);
655
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2});
656
657 // Pass 2: Vector exit -> horizontal sum
658
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_vexit);
659
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64());
660
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8());
661
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32());
662 4 kb.position_at_end(l2_vexit);
663
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2);
664
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out});
665
666 // Pass 2: Scalar remainder
667
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_shead);
668
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64());
669
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32());
670
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32());
671 4 kb.position_at_end(l2_shead);
672
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond2_s = kb.builder().build_slt(iv2_s, d);
673
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s});
674
675
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_sbody);
676 4 kb.position_at_end(l2_sbody);
677
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0);
678
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff2 = kb.sub(xi2, m2_s);
679
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff_sq2 = kb.mul(diff2, diff2);
680
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_sumsq = kb.add(sumsq, diff_sq2);
681
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64);
682
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s});
683
684 // Pass 2 exit -> compute rstd
685
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l2_exit);
686
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32());
687
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32());
688 4 kb.position_at_end(l2_exit);
689
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* var = kb.mul(final_sumsq, inv_d);
690
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* var_eps = kb.add(var, eps);
691
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps));
692
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean);
693
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd);
694
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_vhead, {zero_i64, final_mean, rstd, vmean3, vrstd3});
695
696 // Pass 3: Vector fused LayerNorm + AdaLN Modulate directly into registers, zero intermediate RAM
697
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_vhead);
698
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64());
699
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
700
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32());
701
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
702
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8());
703 4 kb.position_at_end(l3_vhead);
704
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d);
705
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s});
706
707
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_vbody);
708 4 kb.position_at_end(l3_vbody);
709
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64);
710
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* px3 = kb.builder().build_add(x, byte_off3);
711
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pg3 = kb.builder().build_add(gamma, byte_off3);
712
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pb3 = kb.builder().build_add(beta, byte_off3);
713
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pscale3 = kb.builder().build_add(scale, byte_off3);
714
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* pshift3 = kb.builder().build_add(shift, byte_off3);
715
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* py3 = kb.builder().build_add(y, byte_off3);
716
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vx3 = kb.vload_f32x8(px3);
717
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vg3 = kb.vload_f32x8(pg3);
718
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vb3 = kb.vload_f32x8(pb3);
719
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vscale3 = kb.vload_f32x8(pscale3);
720
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vshift3 = kb.vload_f32x8(pshift3);
721
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vdiff3 = kb.vsub(vx3, vm3);
722
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vxhat3 = kb.vmul(vdiff3, vr3);
723
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* v_ln3 = kb.vfma(vg3, vxhat3, vb3); // LayerNorm in registers
724
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* v_1_plus_scale = kb.vadd(vone, vscale3);
725
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* vy3 = kb.vfma(v_ln3, v_1_plus_scale, vshift3); // Modulated in registers
726
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.vstore_f32x8(py3, vy3); // Stored directly to destination!
727
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64);
728
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3});
729
730 // Pass 3: Scalar remainder
731
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_shead);
732
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64());
733
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
734
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32());
735 4 kb.position_at_end(l3_shead);
736
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* cond3_s = kb.builder().build_slt(iv3_s, d);
737
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit);
738
739
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_sbody);
740 4 kb.position_at_end(l3_sbody);
741
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0);
742
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0);
743
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0);
744
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* s3 = kb.load_f32_indexed(scale, iv3_s, 4, 0);
745
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* sh3 = kb.load_f32_indexed(shift, iv3_s, 4, 0);
746
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* diff3 = kb.sub(x3, m3_sc);
747
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* xhat3 = kb.mul(diff3, r3_sc);
748
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* ln_val3 = kb.builder().build_fma_f32(g3, xhat3, b3);
749
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* one_plus_s3 = kb.add(one_f32, s3);
750
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* y3 = kb.builder().build_fma_f32(ln_val3, one_plus_s3, sh3);
751
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.store_f32_indexed(y, iv3_s, y3, 4, 0);
752
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64);
753
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc});
754
755
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 fn->append_block(l3_exit);
756 4 kb.position_at_end(l3_exit);
757
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 kb.builder().build_ret_void();
758 4 }
759
760 // ── Kernel Initialization & AOT Disk Cache Management ───────────────────────
761 static std::mutex s_fused_jit_mu;
762 static std::atomic<bool> s_fused_jit_ready{false};
763 static JitFusedKernels s_fused_kernels;
764
765 747 static const JitFusedKernels& get_fused_kernels() {
766
2/2
✓ Branch 0 taken 740 times.
✓ Branch 1 taken 7 times.
747 if (s_fused_jit_ready.load(std::memory_order_acquire)) {
767 740 return s_fused_kernels;
768 }
769 7 std::lock_guard<std::mutex> lk(s_fused_jit_mu);
770
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 if (!s_fused_jit_ready.load(std::memory_order_relaxed)) {
771 try {
772
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto& cache = brotensor::jit::KernelDiskCache::instance();
773 7 brotensor::jit::KernelDiskCache::CpuCompileOptions opts;
774
775 7 s_fused_kernels.fused_residual_rmsnorm_row_fn =
776
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 cache.compile_or_load_cpu_fn<JitFusedKernels::FusedResidualRmsNormRowFn>(
777
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 "cpu_fused_residual_rmsnorm_row_v1", "fused_residual_rmsnorm_row",
778 populate_fused_residual_rmsnorm_row, opts);
779
780 7 s_fused_kernels.fused_residual_layernorm_row_fn =
781
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 cache.compile_or_load_cpu_fn<JitFusedKernels::FusedResidualLayerNormRowFn>(
782
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 "cpu_fused_residual_layernorm_row_v1", "fused_residual_layernorm_row",
783 populate_fused_residual_layernorm_row, opts);
784
785 7 s_fused_kernels.fused_layernorm_modulate_row_fn =
786
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 cache.compile_or_load_cpu_fn<JitFusedKernels::FusedLayerNormModulateRowFn>(
787
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 "cpu_fused_layernorm_modulate_row_v1", "fused_layernorm_modulate_row",
788 populate_fused_layernorm_modulate_row, opts);
789
790
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 s_fused_kernels.available = (s_fused_kernels.fused_residual_rmsnorm_row_fn != nullptr &&
791
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 s_fused_kernels.fused_residual_layernorm_row_fn != nullptr &&
792 7 s_fused_kernels.fused_layernorm_modulate_row_fn != nullptr);
793
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
7 } catch (const std::exception& e) {
794
0/6
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ std::cerr << "brotensor: Brass JIT fused kernels initialization failed: " << e.what() << "\n";
795 ✗ s_fused_kernels.available = false;
796 ✗ }
797 7 s_fused_jit_ready.store(true, std::memory_order_release);
798 7 }
799 7 return s_fused_kernels;
800 747 }
801
802 } // namespace
803
804 ✗ void prewarm_fused_jit_kernels() {
805 ✗ (void)get_fused_kernels();
806 ✗ }
807
808 6 void reset_fused_jit_caches_for_testing() {
809 6 std::lock_guard<std::mutex> lk(s_fused_jit_mu);
810 6 s_fused_jit_ready.store(false, std::memory_order_relaxed);
811 6 s_fused_kernels = JitFusedKernels{};
812 6 }
813
814 372 void fused_residual_rmsnorm(float* X, const float* res, const float* gamma, float eps, float* Y, int B, int D) {
815 372 const auto& k = get_fused_kernels();
816
6/12
✓ Branch 0 taken 372 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 372 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 372 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 372 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 372 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 372 times.
372 if (!k.available || k.fused_residual_rmsnorm_row_fn == nullptr || X == Y || gamma == nullptr || B <= 0 || D <= 0) {
817 ✗ ref::fused_residual_rmsnorm(X, res, gamma, eps, Y, B, D);
818 ✗ return;
819 }
820
821 372 const float inv_D = 1.0f / static_cast<float>(D);
822
823
3/4
✓ Branch 0 taken 311 times.
✓ Branch 1 taken 61 times.
✓ Branch 2 taken 311 times.
✗ Branch 3 not taken.
372 if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
824
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<std::size_t>(B), [&](std::size_t bi) {
825 ✗ const int b = static_cast<int>(bi);
826 ✗ float* xr = X + static_cast<std::size_t>(b) * D;
827 ✗ const float* rr = res + static_cast<std::size_t>(b) * D;
828 ✗ float* yr = Y + static_cast<std::size_t>(b) * D;
829 ✗ k.fused_residual_rmsnorm_row_fn(xr, rr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D);
830 ✗ });
831 ✗ } else {
832
2/2
✓ Branch 0 taken 3257 times.
✓ Branch 1 taken 372 times.
3629 for (int b = 0; b < B; ++b) {
833 3257 float* xr = X + static_cast<std::size_t>(b) * D;
834 3257 const float* rr = res + static_cast<std::size_t>(b) * D;
835 3257 float* yr = Y + static_cast<std::size_t>(b) * D;
836 3257 k.fused_residual_rmsnorm_row_fn(xr, rr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D);
837 3257 }
838 }
839 372 }
840
841 7 void fused_residual_layernorm(float* X, const float* res, const float* gamma, const float* beta,
842 float eps, float* Y, int B, int D) {
843 7 const auto& k = get_fused_kernels();
844
7/14
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 7 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 7 times.
7 if (!k.available || k.fused_residual_layernorm_row_fn == nullptr || X == Y || gamma == nullptr || beta == nullptr || B <= 0 || D <= 0) {
845 ✗ ref::fused_residual_layernorm(X, res, gamma, beta, eps, Y, B, D);
846 ✗ return;
847 }
848
849 7 const float inv_D = 1.0f / static_cast<float>(D);
850
851
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if (B > 1 && static_cast<int64_t>(B) * D >= 262144) {
852
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<std::size_t>(B), [&](std::size_t bi) {
853 ✗ const int b = static_cast<int>(bi);
854 ✗ float* xr = X + static_cast<std::size_t>(b) * D;
855 ✗ const float* rr = res + static_cast<std::size_t>(b) * D;
856 ✗ float* yr = Y + static_cast<std::size_t>(b) * D;
857 ✗ k.fused_residual_layernorm_row_fn(xr, rr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D);
858 ✗ });
859 ✗ } else {
860
2/2
✓ Branch 0 taken 417 times.
✓ Branch 1 taken 7 times.
424 for (int b = 0; b < B; ++b) {
861 417 float* xr = X + static_cast<std::size_t>(b) * D;
862 417 const float* rr = res + static_cast<std::size_t>(b) * D;
863 417 float* yr = Y + static_cast<std::size_t>(b) * D;
864 417 k.fused_residual_layernorm_row_fn(xr, rr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D);
865 417 }
866 }
867 7 }
868
869 368 void fused_layernorm_modulate(const float* X, const float* gamma, const float* beta,
870 const float* scale, const float* shift, float eps, float* Y, int R, int D) {
871 368 const auto& k = get_fused_kernels();
872
9/18
✓ Branch 0 taken 368 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 368 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 368 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 368 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 368 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 368 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 368 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 368 times.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✓ Branch 17 taken 368 times.
368 if (!k.available || k.fused_layernorm_modulate_row_fn == nullptr || X == Y || gamma == nullptr || beta == nullptr || scale == nullptr || shift == nullptr || R <= 0 || D <= 0) {
873 ✗ ref::fused_layernorm_modulate(X, gamma, beta, scale, shift, eps, Y, R, D);
874 ✗ return;
875 }
876
877 368 const float inv_D = 1.0f / static_cast<float>(D);
878
879
3/4
✓ Branch 0 taken 308 times.
✓ Branch 1 taken 60 times.
✓ Branch 2 taken 308 times.
✗ Branch 3 not taken.
368 if (R > 1 && static_cast<int64_t>(R) * D >= 262144) {
880
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ detail::cpu::parallel_for(static_cast<std::size_t>(R), [&](std::size_t ri) {
881 ✗ const int r = static_cast<int>(ri);
882 ✗ const float* xr = X + static_cast<std::size_t>(r) * D;
883 ✗ float* yr = Y + static_cast<std::size_t>(r) * D;
884 ✗ k.fused_layernorm_modulate_row_fn(xr, gamma, beta, scale, shift, yr, static_cast<uint64_t>(D), eps, inv_D);
885 ✗ });
886 ✗ } else {
887
2/2
✓ Branch 0 taken 3226 times.
✓ Branch 1 taken 368 times.
3594 for (int r = 0; r < R; ++r) {
888 3226 const float* xr = X + static_cast<std::size_t>(r) * D;
889 3226 float* yr = Y + static_cast<std::size_t>(r) * D;
890 3226 k.fused_layernorm_modulate_row_fn(xr, gamma, beta, scale, shift, yr, static_cast<uint64_t>(D), eps, inv_D);
891 3226 }
892 }
893 368 }
894
895 } // namespace brotensor::detail::cpu::jit
896
897 #else // !BROTENSOR_HAS_BRASS_JIT
898
899 namespace brotensor::detail::cpu::jit {
900
901 void prewarm_fused_jit_kernels() {}
902 void reset_fused_jit_caches_for_testing() {}
903
904 void fused_residual_rmsnorm(float* X, const float* res, const float* gamma, float eps, float* Y, int B, int D) {
905 ref::fused_residual_rmsnorm(X, res, gamma, eps, Y, B, D);
906 }
907 void fused_residual_layernorm(float* X, const float* res, const float* gamma, const float* beta, float eps, float* Y, int B, int D) {
908 ref::fused_residual_layernorm(X, res, gamma, beta, eps, Y, B, D);
909 }
910 void fused_layernorm_modulate(const float* X, const float* gamma, const float* beta,
911 const float* scale, const float* shift, float eps, float* Y, int R, int D) {
912 ref::fused_layernorm_modulate(X, gamma, beta, scale, shift, eps, Y, R, D);
913 }
914
915 } // namespace brotensor::detail::cpu::jit
916
917 #endif // BROTENSOR_HAS_BRASS_JIT
918