src/cpu/cpu_jit.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 | 1 | inline void rms_norm(const float* X, const float* gamma, float eps, float* Y, int B, int D) { | |
| 11 | 1 | if (B <= 0 || D <= 0) return; | |
| 12 | 1 | const float inv_D = 1.0f / static_cast<float>(D); | |
| 13 | 5 | auto row_fn = [&](int b) { | |
| 14 | 4 | const float* xr = X + static_cast<size_t>(b) * D; | |
| 15 | 4 | float* yr = Y + static_cast<size_t>(b) * D; | |
| 16 | 4 | float sum = 0.0f; | |
| 17 | 152 | for (int i = 0; i < D; ++i) sum += xr[i] * xr[i]; | |
| 18 | 4 | float rrms = 1.0f / std::sqrt(sum * inv_D + eps); | |
| 19 | 152 | for (int i = 0; i < D; ++i) yr[i] = xr[i] * gamma[i] * rrms; | |
| 20 | 4 | }; | |
| 21 | 1 | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 22 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) { | |
| 23 | ✗ | row_fn(static_cast<int>(bi)); | |
| 24 | ✗ | }); | |
| 25 | ✗ | } else { | |
| 26 | 5 | for (int b = 0; b < B; ++b) row_fn(b); | |
| 27 | } | ||
| 28 | 1 | } | |
| 29 | |||
| 30 | 371 | inline void swiglu(const float* X, float* Y, int B, int D) { | |
| 31 | 371 | if (B <= 0 || D <= 0) return; | |
| 32 | 3990 | auto row_fn = [&](int b) { | |
| 33 | 3619 | const float* gate = X + static_cast<size_t>(b) * 2 * D; | |
| 34 | 3619 | const float* up = gate + D; | |
| 35 | 3619 | float* out = Y + static_cast<size_t>(b) * D; | |
| 36 | 8817988 | for (int i = 0; i < D; ++i) { | |
| 37 | 8814369 | float g = gate[i]; | |
| 38 | 8814369 | float silu = g / (1.0f + std::exp(-g)); | |
| 39 | 8814369 | out[i] = silu * up[i]; | |
| 40 | 8814369 | } | |
| 41 | 3619 | }; | |
| 42 | 371 | if (B > 1 && static_cast<int64_t>(B) * D >= 16384) { | |
| 43 | 2378 | detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) { | |
| 44 | 2316 | row_fn(static_cast<int>(bi)); | |
| 45 | 2316 | }); | |
| 46 | 62 | } else { | |
| 47 | 1612 | for (int b = 0; b < B; ++b) row_fn(b); | |
| 48 | } | ||
| 49 | 371 | } | |
| 50 | |||
| 51 | 370 | inline void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) { | |
| 52 | 370 | if (L <= 0 || D <= 0) return; | |
| 53 | 3595 | auto row_fn = [&](int l) { | |
| 54 | 3225 | const float* xr = X + static_cast<size_t>(l) * D; | |
| 55 | 3225 | float* yr = Y + static_cast<size_t>(l) * D; | |
| 56 | 8469284 | for (int d = 0; d < D; ++d) { | |
| 57 | 8466059 | yr[d] = xr[d] * (1.0f + scale[d]) + shift[d]; | |
| 58 | 8466059 | } | |
| 59 | 3225 | }; | |
| 60 | 370 | if (L > 1 && static_cast<int64_t>(L) * D >= 262144) { | |
| 61 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(L), [&](size_t li) { | |
| 62 | ✗ | row_fn(static_cast<int>(li)); | |
| 63 | ✗ | }); | |
| 64 | ✗ | } else { | |
| 65 | 3595 | for (int l = 0; l < L; ++l) row_fn(l); | |
| 66 | } | ||
| 67 | 370 | } | |
| 68 | |||
| 69 | 370 | inline void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) { | |
| 70 | 370 | if (L <= 0 || D <= 0) return; | |
| 71 | 3601 | auto row_fn = [&](int l) { | |
| 72 | 3231 | const float* xr = X + static_cast<size_t>(l) * D; | |
| 73 | 3231 | float* yr = Y + static_cast<size_t>(l) * D; | |
| 74 | 8469548 | for (int d = 0; d < D; ++d) { | |
| 75 | 8466317 | yr[d] = xr[d] * v[d]; | |
| 76 | 8466317 | } | |
| 77 | 3231 | }; | |
| 78 | 370 | if (L > 1 && static_cast<int64_t>(L) * D >= 262144) { | |
| 79 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(L), [&](size_t li) { | |
| 80 | ✗ | row_fn(static_cast<int>(li)); | |
| 81 | ✗ | }); | |
| 82 | ✗ | } else { | |
| 83 | 3601 | for (int l = 0; l < L; ++l) row_fn(l); | |
| 84 | } | ||
| 85 | 370 | } | |
| 86 | |||
| 87 | 1 | inline void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta, | |
| 88 | float eps, float* Y, int R, int D) { | ||
| 89 | 1 | if (R <= 0 || D <= 0) return; | |
| 90 | 1 | const float inv_D = 1.0f / static_cast<float>(D); | |
| 91 | 5 | auto row_fn = [&](int r) { | |
| 92 | 4 | const float* xr = X + static_cast<size_t>(r) * D; | |
| 93 | 4 | float* yr = Y + static_cast<size_t>(r) * D; | |
| 94 | 4 | float sum = 0.0f; | |
| 95 | 152 | for (int i = 0; i < D; ++i) sum += xr[i]; | |
| 96 | 4 | float mean = sum * inv_D; | |
| 97 | 4 | float sumsq = 0.0f; | |
| 98 | 152 | for (int i = 0; i < D; ++i) { | |
| 99 | 148 | float diff = xr[i] - mean; | |
| 100 | 148 | sumsq += diff * diff; | |
| 101 | 148 | } | |
| 102 | 4 | float var = sumsq * inv_D; | |
| 103 | 4 | float rstd = 1.0f / std::sqrt(var + eps); | |
| 104 | 152 | for (int i = 0; i < D; ++i) { | |
| 105 | 148 | float g = gamma ? gamma[i] : 1.0f; | |
| 106 | 148 | float b = beta ? beta[i] : 0.0f; | |
| 107 | 148 | yr[i] = g * ((xr[i] - mean) * rstd) + b; | |
| 108 | 148 | } | |
| 109 | 4 | }; | |
| 110 | 1 | if (R > 1 && static_cast<int64_t>(R) * D >= 262144) { | |
| 111 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(R), [&](size_t ri) { | |
| 112 | ✗ | row_fn(static_cast<int>(ri)); | |
| 113 | ✗ | }); | |
| 114 | ✗ | } else { | |
| 115 | 5 | for (int r = 0; r < R; ++r) row_fn(r); | |
| 116 | } | ||
| 117 | 1 | } | |
| 118 | |||
| 119 | } // namespace ref | ||
| 120 | } // namespace brotensor::detail::cpu::jit | ||
| 121 | |||
| 122 | #if BROTENSOR_HAS_BRASS_JIT | ||
| 123 | |||
| 124 | #include <brass/codegen/kernel_jit.hpp> | ||
| 125 | #include <brass/codegen/ml_fusion.hpp> | ||
| 126 | #include <brass/mir/builder.hpp> | ||
| 127 | #include <brass/mir/module.hpp> | ||
| 128 | #include <brass/mir/function.hpp> | ||
| 129 | #include "../jit/kernel_disk_cache.h" | ||
| 130 | #include <atomic> | ||
| 131 | #include <cmath> | ||
| 132 | #include <mutex> | ||
| 133 | #include <vector> | ||
| 134 | #include <memory> | ||
| 135 | #include <algorithm> | ||
| 136 | #include <iostream> | ||
| 137 | |||
| 138 | namespace brotensor::detail::cpu::jit { | ||
| 139 | |||
| 140 | namespace { | ||
| 141 | |||
| 142 | using namespace brass; | ||
| 143 | using namespace brass::codegen; | ||
| 144 | |||
| 145 | struct JitKernels { | ||
| 146 | bool available = false; | ||
| 147 | |||
| 148 | using AdaLNModulateFn = void (*)(const float* x, const float* scale, const float* shift, float* out, uint64_t n); | ||
| 149 | AdaLNModulateFn adaln_fn = nullptr; | ||
| 150 | |||
| 151 | using BroadcastMulFn = void (*)(const float* x, const float* v, float* out, uint64_t n); | ||
| 152 | BroadcastMulFn broadcast_mul_fn = nullptr; | ||
| 153 | |||
| 154 | using RmsNormRowFn = void (*)(const float* x, const float* gamma, float* y, uint64_t d, float eps, float inv_d); | ||
| 155 | RmsNormRowFn rms_norm_row_fn = nullptr; | ||
| 156 | |||
| 157 | using LayerNormRowFn = void (*)(const float* x, const float* gamma, const float* beta, float* y, uint64_t d, float eps, float inv_d); | ||
| 158 | LayerNormRowFn layernorm_row_fn = nullptr; | ||
| 159 | }; | ||
| 160 | |||
| 161 | // Horizontal sum of an 8-wide float vector using destination scratch buffer | ||
| 162 | 36 | static Value* reduce_vsum8(KernelBuilder& kb, Value* scratch, Value* vsum) { | |
| 163 | 36 | kb.vstore_f32x8(scratch, vsum); | |
| 164 | 36 | Value* s0 = kb.load_f32(scratch, 0); Value* s1 = kb.load_f32(scratch, 4); | |
| 165 | 36 | Value* s2 = kb.load_f32(scratch, 8); Value* s3 = kb.load_f32(scratch, 12); | |
| 166 | 36 | Value* s4 = kb.load_f32(scratch, 16); Value* s5 = kb.load_f32(scratch, 20); | |
| 167 | 36 | Value* s6 = kb.load_f32(scratch, 24); Value* s7 = kb.load_f32(scratch, 28); | |
| 168 | 36 | Value* sum0123 = kb.add(kb.add(s0, s1), kb.add(s2, s3)); | |
| 169 | 36 | Value* sum4567 = kb.add(kb.add(s4, s5), kb.add(s6, s7)); | |
| 170 | 36 | return kb.add(sum0123, sum4567); | |
| 171 | } | ||
| 172 | |||
| 173 | // ── 1. Vectorized Broadcast Mul (AVX2 8-wide + scalar remainder) ──────────── | ||
| 174 | 12 | static void populate_broadcast_mul(Module& mod) { | |
| 175 | 60 | Function* fn = mod.create_function("broadcast_mul", Type::void_type(), { | |
| 176 | 48 | Type::ptr(), Type::ptr(), Type::ptr(), Type::i64() | |
| 177 | }); | ||
| 178 | 12 | KernelBuilder kb(mod, fn); | |
| 179 | |||
| 180 | 12 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 181 | 12 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 182 | 12 | Value* v = kb.builder().add_block_param(entry, Type::ptr()); | |
| 183 | 12 | Value* out = kb.builder().add_block_param(entry, Type::ptr()); | |
| 184 | 12 | Value* n = kb.builder().add_block_param(entry, Type::i64()); | |
| 185 | |||
| 186 | 12 | BasicBlock* v_head = kb.builder().create_block("v_head"); | |
| 187 | 12 | BasicBlock* v_body = kb.builder().create_block("v_body"); | |
| 188 | 12 | BasicBlock* s_head = kb.builder().create_block("s_head"); | |
| 189 | 12 | BasicBlock* s_body = kb.builder().create_block("s_body"); | |
| 190 | 12 | BasicBlock* exit = kb.builder().create_block("exit"); | |
| 191 | |||
| 192 | 12 | kb.position_at_end(entry); | |
| 193 | 12 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 194 | 12 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 195 | 12 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 196 | 12 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 197 | 12 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 198 | 12 | Value* vec_n = kb.builder().build_and(n, mask_eight); | |
| 199 | 12 | kb.builder().build_br(v_head, {zero_i64}); | |
| 200 | |||
| 201 | // Vector loop: 8 elements per iteration | ||
| 202 | 12 | fn->append_block(v_head); | |
| 203 | 12 | Value* iv_v = kb.builder().add_block_param(v_head, Type::i64()); | |
| 204 | 12 | kb.position_at_end(v_head); | |
| 205 | 12 | Value* v_cond = kb.builder().build_slt(iv_v, vec_n); | |
| 206 | 12 | kb.builder().build_br_if(v_cond, v_body, {}, s_head, {iv_v}); | |
| 207 | |||
| 208 | 12 | fn->append_block(v_body); | |
| 209 | 12 | kb.position_at_end(v_body); | |
| 210 | 12 | Value* byte_off = kb.builder().build_shl(iv_v, two_i64); | |
| 211 | 12 | Value* px = kb.builder().build_add(x, byte_off); | |
| 212 | 12 | Value* pv = kb.builder().build_add(v, byte_off); | |
| 213 | 12 | Value* py = kb.builder().build_add(out, byte_off); | |
| 214 | 12 | Value* vx = kb.vload_f32x8(px); | |
| 215 | 12 | Value* vv = kb.vload_f32x8(pv); | |
| 216 | 12 | Value* vy = kb.vmul(vx, vv); | |
| 217 | 12 | kb.vstore_f32x8(py, vy); | |
| 218 | 12 | Value* next_iv_v = kb.builder().build_add(iv_v, eight_i64); | |
| 219 | 12 | kb.builder().build_br(v_head, {next_iv_v}); | |
| 220 | |||
| 221 | // Scalar remainder loop | ||
| 222 | 12 | fn->append_block(s_head); | |
| 223 | 12 | Value* iv_s = kb.builder().add_block_param(s_head, Type::i64()); | |
| 224 | 12 | kb.position_at_end(s_head); | |
| 225 | 12 | Value* s_cond = kb.builder().build_slt(iv_s, n); | |
| 226 | 12 | kb.builder().build_br_if(s_cond, s_body, exit); | |
| 227 | |||
| 228 | 12 | fn->append_block(s_body); | |
| 229 | 12 | kb.position_at_end(s_body); | |
| 230 | 12 | Value* xi = kb.load_f32_indexed(x, iv_s, 4, 0); | |
| 231 | 12 | Value* vi = kb.load_f32_indexed(v, iv_s, 4, 0); | |
| 232 | 12 | Value* yi = kb.mul(xi, vi); | |
| 233 | 12 | kb.store_f32_indexed(out, iv_s, yi, 4, 0); | |
| 234 | 12 | Value* next_iv_s = kb.builder().build_add(iv_s, one_i64); | |
| 235 | 12 | kb.builder().build_br(s_head, {next_iv_s}); | |
| 236 | |||
| 237 | 12 | fn->append_block(exit); | |
| 238 | 12 | kb.position_at_end(exit); | |
| 239 | 12 | kb.builder().build_ret_void(); | |
| 240 | 12 | } | |
| 241 | |||
| 242 | // ── 2. Vectorized AdaLN Modulate (AVX2 8-wide + FMA + scalar remainder) ───── | ||
| 243 | 13 | static void populate_adaln_modulate(Module& mod) { | |
| 244 | 78 | Function* fn = mod.create_function("fused_adaln_modulate", Type::void_type(), { | |
| 245 | 65 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64() | |
| 246 | }); | ||
| 247 | 13 | KernelBuilder kb(mod, fn); | |
| 248 | |||
| 249 | 13 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 250 | 13 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 251 | 13 | Value* scale = kb.builder().add_block_param(entry, Type::ptr()); | |
| 252 | 13 | Value* shift = kb.builder().add_block_param(entry, Type::ptr()); | |
| 253 | 13 | Value* out = kb.builder().add_block_param(entry, Type::ptr()); | |
| 254 | 13 | Value* n = kb.builder().add_block_param(entry, Type::i64()); | |
| 255 | |||
| 256 | 13 | BasicBlock* v_head = kb.builder().create_block("v_head"); | |
| 257 | 13 | BasicBlock* v_body = kb.builder().create_block("v_body"); | |
| 258 | 13 | BasicBlock* s_head = kb.builder().create_block("s_head"); | |
| 259 | 13 | BasicBlock* s_body = kb.builder().create_block("s_body"); | |
| 260 | 13 | BasicBlock* exit = kb.builder().create_block("exit"); | |
| 261 | |||
| 262 | 13 | kb.position_at_end(entry); | |
| 263 | 13 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 264 | 13 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 265 | 13 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 266 | 13 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 267 | 13 | Value* one_f32 = kb.builder().build_fconst_f32(1.0f); | |
| 268 | 13 | Value* v_one = kb.vbroadcast(Type::f32x8(), one_f32); | |
| 269 | 13 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 270 | 13 | Value* vec_n = kb.builder().build_and(n, mask_eight); | |
| 271 | 13 | kb.builder().build_br(v_head, {zero_i64}); | |
| 272 | |||
| 273 | // Vector loop: modulated = x * (1.0f + scale) + shift via AVX2 FMA | ||
| 274 | 13 | fn->append_block(v_head); | |
| 275 | 13 | Value* iv_v = kb.builder().add_block_param(v_head, Type::i64()); | |
| 276 | 13 | kb.position_at_end(v_head); | |
| 277 | 13 | Value* v_cond = kb.builder().build_slt(iv_v, vec_n); | |
| 278 | 13 | kb.builder().build_br_if(v_cond, v_body, {}, s_head, {iv_v}); | |
| 279 | |||
| 280 | 13 | fn->append_block(v_body); | |
| 281 | 13 | kb.position_at_end(v_body); | |
| 282 | 13 | Value* byte_off = kb.builder().build_shl(iv_v, two_i64); | |
| 283 | 13 | Value* px = kb.builder().build_add(x, byte_off); | |
| 284 | 13 | Value* pscale = kb.builder().build_add(scale, byte_off); | |
| 285 | 13 | Value* pshift = kb.builder().build_add(shift, byte_off); | |
| 286 | 13 | Value* py = kb.builder().build_add(out, byte_off); | |
| 287 | 13 | Value* vx = kb.vload_f32x8(px); | |
| 288 | 13 | Value* vscale = kb.vload_f32x8(pscale); | |
| 289 | 13 | Value* vshift = kb.vload_f32x8(pshift); | |
| 290 | 13 | Value* v_1_plus_scale = kb.vadd(v_one, vscale); | |
| 291 | 13 | Value* vy = kb.vfma(vx, v_1_plus_scale, vshift); | |
| 292 | 13 | kb.vstore_f32x8(py, vy); | |
| 293 | 13 | Value* next_iv_v = kb.builder().build_add(iv_v, eight_i64); | |
| 294 | 13 | kb.builder().build_br(v_head, {next_iv_v}); | |
| 295 | |||
| 296 | // Scalar remainder loop | ||
| 297 | 13 | fn->append_block(s_head); | |
| 298 | 13 | Value* iv_s = kb.builder().add_block_param(s_head, Type::i64()); | |
| 299 | 13 | kb.position_at_end(s_head); | |
| 300 | 13 | Value* s_cond = kb.builder().build_slt(iv_s, n); | |
| 301 | 13 | kb.builder().build_br_if(s_cond, s_body, exit); | |
| 302 | |||
| 303 | 13 | fn->append_block(s_body); | |
| 304 | 13 | kb.position_at_end(s_body); | |
| 305 | 13 | Value* xi = kb.load_f32_indexed(x, iv_s, 4, 0); | |
| 306 | 13 | Value* scale_i = kb.load_f32_indexed(scale, iv_s, 4, 0); | |
| 307 | 13 | Value* shift_i = kb.load_f32_indexed(shift, iv_s, 4, 0); | |
| 308 | 13 | Value* one_plus_scale = kb.add(one_f32, scale_i); | |
| 309 | 13 | Value* yi = kb.builder().build_fma_f32(xi, one_plus_scale, shift_i); | |
| 310 | 13 | kb.store_f32_indexed(out, iv_s, yi, 4, 0); | |
| 311 | 13 | Value* next_iv_s = kb.builder().build_add(iv_s, one_i64); | |
| 312 | 13 | kb.builder().build_br(s_head, {next_iv_s}); | |
| 313 | |||
| 314 | 13 | fn->append_block(exit); | |
| 315 | 13 | kb.position_at_end(exit); | |
| 316 | 13 | kb.builder().build_ret_void(); | |
| 317 | 13 | } | |
| 318 | |||
| 319 | // ── 3. Vectorized RMSNorm Row (AVX2 FMA reduction + AVX2 affine scale) ───── | ||
| 320 | 12 | static void populate_rms_norm_row(Module& mod) { | |
| 321 | 84 | Function* fn = mod.create_function("rms_norm_row", Type::void_type(), { | |
| 322 | 72 | Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32() | |
| 323 | }); | ||
| 324 | 12 | KernelBuilder kb(mod, fn); | |
| 325 | |||
| 326 | 12 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 327 | 12 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 328 | 12 | Value* gamma = kb.builder().add_block_param(entry, Type::ptr()); | |
| 329 | 12 | Value* y = kb.builder().add_block_param(entry, Type::ptr()); | |
| 330 | 12 | Value* d = kb.builder().add_block_param(entry, Type::i64()); | |
| 331 | 12 | Value* eps = kb.builder().add_block_param(entry, Type::f32()); | |
| 332 | 12 | Value* inv_d = kb.builder().add_block_param(entry, Type::f32()); | |
| 333 | |||
| 334 | 12 | BasicBlock* l1_vhead = kb.builder().create_block("l1_vhead"); | |
| 335 | 12 | BasicBlock* l1_vbody = kb.builder().create_block("l1_vbody"); | |
| 336 | 12 | BasicBlock* l1_vexit = kb.builder().create_block("l1_vexit"); | |
| 337 | 12 | BasicBlock* l1_shead = kb.builder().create_block("l1_shead"); | |
| 338 | 12 | BasicBlock* l1_sbody = kb.builder().create_block("l1_sbody"); | |
| 339 | 12 | BasicBlock* l1_exit = kb.builder().create_block("l1_exit"); | |
| 340 | |||
| 341 | 12 | BasicBlock* l2_vhead = kb.builder().create_block("l2_vhead"); | |
| 342 | 12 | BasicBlock* l2_vbody = kb.builder().create_block("l2_vbody"); | |
| 343 | 12 | BasicBlock* l2_shead = kb.builder().create_block("l2_shead"); | |
| 344 | 12 | BasicBlock* l2_sbody = kb.builder().create_block("l2_sbody"); | |
| 345 | 12 | BasicBlock* l2_exit = kb.builder().create_block("l2_exit"); | |
| 346 | |||
| 347 | 12 | kb.position_at_end(entry); | |
| 348 | 12 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 349 | 12 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 350 | 12 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 351 | 12 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 352 | 12 | Value* zero_f32 = kb.builder().build_fconst_f32(0.0f); | |
| 353 | 12 | Value* vzero = kb.vzero(Type::f32x8()); | |
| 354 | 12 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 355 | 12 | Value* vec_d = kb.builder().build_and(d, mask_eight); | |
| 356 | |||
| 357 | 12 | Value* has_vec = kb.builder().build_sge(d, eight_i64); | |
| 358 | 12 | kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32}); | |
| 359 | |||
| 360 | // Pass 1: Vector loop accumulating sum_sq via vfma | ||
| 361 | 12 | fn->append_block(l1_vhead); | |
| 362 | 12 | Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64()); | |
| 363 | 12 | Value* vsum = kb.builder().add_block_param(l1_vhead, Type::f32x8()); | |
| 364 | 12 | kb.position_at_end(l1_vhead); | |
| 365 | 12 | Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d); | |
| 366 | 12 | kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum}); | |
| 367 | |||
| 368 | 12 | fn->append_block(l1_vbody); | |
| 369 | 12 | kb.position_at_end(l1_vbody); | |
| 370 | 12 | Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64); | |
| 371 | 12 | Value* px1 = kb.builder().build_add(x, byte_off1); | |
| 372 | 12 | Value* vx1 = kb.vload_f32x8(px1); | |
| 373 | 12 | Value* next_vsum = kb.vfma(vx1, vx1, vsum); | |
| 374 | 12 | Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64); | |
| 375 | 12 | kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum}); | |
| 376 | |||
| 377 | // Pass 1: Vector exit -> horizontal vector reduction via y scratch buffer | ||
| 378 | 12 | fn->append_block(l1_vexit); | |
| 379 | 12 | Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64()); | |
| 380 | 12 | Value* exit_vsum = kb.builder().add_block_param(l1_vexit, Type::f32x8()); | |
| 381 | 12 | kb.position_at_end(l1_vexit); | |
| 382 | 12 | Value* init_scalar_sum = reduce_vsum8(kb, y, exit_vsum); | |
| 383 | 12 | kb.builder().build_br(l1_shead, {exit_iv1, init_scalar_sum}); | |
| 384 | |||
| 385 | // Pass 1: Scalar remainder loop | ||
| 386 | 12 | fn->append_block(l1_shead); | |
| 387 | 12 | Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64()); | |
| 388 | 12 | Value* sum_sq = kb.builder().add_block_param(l1_shead, Type::f32()); | |
| 389 | 12 | kb.position_at_end(l1_shead); | |
| 390 | 12 | Value* cond1_s = kb.builder().build_slt(iv1_s, d); | |
| 391 | 12 | kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum_sq}); | |
| 392 | |||
| 393 | 12 | fn->append_block(l1_sbody); | |
| 394 | 12 | kb.position_at_end(l1_sbody); | |
| 395 | 12 | Value* xi = kb.load_f32_indexed(x, iv1_s, 4, 0); | |
| 396 | 12 | Value* xi_sq = kb.mul(xi, xi); | |
| 397 | 12 | Value* next_sum = kb.add(sum_sq, xi_sq); | |
| 398 | 12 | Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64); | |
| 399 | 12 | kb.builder().build_br(l1_shead, {next_iv1_s, next_sum}); | |
| 400 | |||
| 401 | // Pass 1 exit -> compute rrms = 1.0f / sqrt(mean_sq + eps) | ||
| 402 | 12 | fn->append_block(l1_exit); | |
| 403 | 12 | Value* final_sum = kb.builder().add_block_param(l1_exit, Type::f32()); | |
| 404 | 12 | kb.position_at_end(l1_exit); | |
| 405 | 12 | Value* mean_sq = kb.mul(final_sum, inv_d); | |
| 406 | 12 | Value* denom = kb.add(mean_sq, eps); | |
| 407 | 12 | Value* rrms = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(denom)); | |
| 408 | 12 | Value* vrrms = kb.vbroadcast(Type::f32x8(), rrms); | |
| 409 | 12 | kb.builder().build_br(l2_vhead, {zero_i64, rrms, vrrms}); | |
| 410 | |||
| 411 | // Pass 2: Vector loop y[i] = x[i] * gamma[i] * rrms | ||
| 412 | 12 | fn->append_block(l2_vhead); | |
| 413 | 12 | Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64()); | |
| 414 | 12 | Value* rrms_val = kb.builder().add_block_param(l2_vhead, Type::f32()); | |
| 415 | 12 | Value* vrrms_val = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 416 | 12 | kb.position_at_end(l2_vhead); | |
| 417 | 12 | Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d); | |
| 418 | 12 | kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_shead, {iv2_v, rrms_val}); | |
| 419 | |||
| 420 | 12 | fn->append_block(l2_vbody); | |
| 421 | 12 | kb.position_at_end(l2_vbody); | |
| 422 | 12 | Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64); | |
| 423 | 12 | Value* px2 = kb.builder().build_add(x, byte_off2); | |
| 424 | 12 | Value* pg2 = kb.builder().build_add(gamma, byte_off2); | |
| 425 | 12 | Value* py2 = kb.builder().build_add(y, byte_off2); | |
| 426 | 12 | Value* vx2 = kb.vload_f32x8(px2); | |
| 427 | 12 | Value* vg2 = kb.vload_f32x8(pg2); | |
| 428 | 12 | Value* vxg = kb.vmul(vx2, vg2); | |
| 429 | 12 | Value* vy = kb.vmul(vxg, vrrms_val); | |
| 430 | 12 | kb.vstore_f32x8(py2, vy); | |
| 431 | 12 | Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64); | |
| 432 | 12 | kb.builder().build_br(l2_vhead, {next_iv2_v, rrms_val, vrrms_val}); | |
| 433 | |||
| 434 | // Pass 2: Scalar remainder loop | ||
| 435 | 12 | fn->append_block(l2_shead); | |
| 436 | 12 | Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64()); | |
| 437 | 12 | Value* rrms_scalar = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 438 | 12 | kb.position_at_end(l2_shead); | |
| 439 | 12 | Value* cond2_s = kb.builder().build_slt(iv2_s, d); | |
| 440 | 12 | kb.builder().build_br_if(cond2_s, l2_sbody, l2_exit); | |
| 441 | |||
| 442 | 12 | fn->append_block(l2_sbody); | |
| 443 | 12 | kb.position_at_end(l2_sbody); | |
| 444 | 12 | Value* x2 = kb.load_f32_indexed(x, iv2_s, 4, 0); | |
| 445 | 12 | Value* g2 = kb.load_f32_indexed(gamma, iv2_s, 4, 0); | |
| 446 | 12 | Value* xg2 = kb.mul(x2, g2); | |
| 447 | 12 | Value* out_val = kb.mul(xg2, rrms_scalar); | |
| 448 | 12 | kb.store_f32_indexed(y, iv2_s, out_val, 4, 0); | |
| 449 | 12 | Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64); | |
| 450 | 12 | kb.builder().build_br(l2_shead, {next_iv2_s, rrms_scalar}); | |
| 451 | |||
| 452 | 12 | fn->append_block(l2_exit); | |
| 453 | 12 | kb.position_at_end(l2_exit); | |
| 454 | 12 | kb.builder().build_ret_void(); | |
| 455 | 12 | } | |
| 456 | |||
| 457 | // ── 4. Vectorized LayerNorm Row (AVX2 mean + var + affine) ────────────────── | ||
| 458 | 12 | static void populate_layernorm_row(Module& mod) { | |
| 459 | 96 | Function* fn = mod.create_function("layernorm_row", Type::void_type(), { | |
| 460 | 84 | Type::ptr(), Type::ptr(), Type::ptr(), Type::ptr(), Type::i64(), Type::f32(), Type::f32() | |
| 461 | }); | ||
| 462 | 12 | KernelBuilder kb(mod, fn); | |
| 463 | |||
| 464 | 12 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 465 | 12 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 466 | 12 | Value* gamma = kb.builder().add_block_param(entry, Type::ptr()); | |
| 467 | 12 | Value* beta = kb.builder().add_block_param(entry, Type::ptr()); | |
| 468 | 12 | Value* y = kb.builder().add_block_param(entry, Type::ptr()); | |
| 469 | 12 | Value* d = kb.builder().add_block_param(entry, Type::i64()); | |
| 470 | 12 | Value* eps = kb.builder().add_block_param(entry, Type::f32()); | |
| 471 | 12 | Value* inv_d = kb.builder().add_block_param(entry, Type::f32()); | |
| 472 | |||
| 473 | // Pass 1 blocks (mean) | ||
| 474 | 12 | BasicBlock* l1_vhead = kb.builder().create_block("ln_l1_vhead"); | |
| 475 | 12 | BasicBlock* l1_vbody = kb.builder().create_block("ln_l1_vbody"); | |
| 476 | 12 | BasicBlock* l1_vexit = kb.builder().create_block("ln_l1_vexit"); | |
| 477 | 12 | BasicBlock* l1_shead = kb.builder().create_block("ln_l1_shead"); | |
| 478 | 12 | BasicBlock* l1_sbody = kb.builder().create_block("ln_l1_sbody"); | |
| 479 | 12 | BasicBlock* l1_exit = kb.builder().create_block("ln_l1_exit"); | |
| 480 | |||
| 481 | // Pass 2 blocks (variance) | ||
| 482 | 12 | BasicBlock* l2_vhead = kb.builder().create_block("ln_l2_vhead"); | |
| 483 | 12 | BasicBlock* l2_vbody = kb.builder().create_block("ln_l2_vbody"); | |
| 484 | 12 | BasicBlock* l2_vexit = kb.builder().create_block("ln_l2_vexit"); | |
| 485 | 12 | BasicBlock* l2_shead = kb.builder().create_block("ln_l2_shead"); | |
| 486 | 12 | BasicBlock* l2_sbody = kb.builder().create_block("ln_l2_sbody"); | |
| 487 | 12 | BasicBlock* l2_exit = kb.builder().create_block("ln_l2_exit"); | |
| 488 | |||
| 489 | // Pass 3 blocks (affine) | ||
| 490 | 12 | BasicBlock* l3_vhead = kb.builder().create_block("ln_l3_vhead"); | |
| 491 | 12 | BasicBlock* l3_vbody = kb.builder().create_block("ln_l3_vbody"); | |
| 492 | 12 | BasicBlock* l3_shead = kb.builder().create_block("ln_l3_shead"); | |
| 493 | 12 | BasicBlock* l3_sbody = kb.builder().create_block("ln_l3_sbody"); | |
| 494 | 12 | BasicBlock* l3_exit = kb.builder().create_block("ln_l3_exit"); | |
| 495 | |||
| 496 | 12 | kb.position_at_end(entry); | |
| 497 | 12 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 498 | 12 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 499 | 12 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 500 | 12 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 501 | 12 | Value* zero_f32 = kb.builder().build_fconst_f32(0.0f); | |
| 502 | 12 | Value* vzero = kb.vzero(Type::f32x8()); | |
| 503 | 12 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 504 | 12 | Value* vec_d = kb.builder().build_and(d, mask_eight); | |
| 505 | |||
| 506 | 12 | Value* has_vec = kb.builder().build_sge(d, eight_i64); | |
| 507 | 12 | kb.builder().build_br_if(has_vec, l1_vhead, {zero_i64, vzero}, l1_shead, {zero_i64, zero_f32}); | |
| 508 | |||
| 509 | // Pass 1: Vector sum | ||
| 510 | 12 | fn->append_block(l1_vhead); | |
| 511 | 12 | Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64()); | |
| 512 | 12 | Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8()); | |
| 513 | 12 | kb.position_at_end(l1_vhead); | |
| 514 | 12 | Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d); | |
| 515 | 12 | kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1}); | |
| 516 | |||
| 517 | 12 | fn->append_block(l1_vbody); | |
| 518 | 12 | kb.position_at_end(l1_vbody); | |
| 519 | 12 | Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64); | |
| 520 | 12 | Value* px1 = kb.builder().build_add(x, byte_off1); | |
| 521 | 12 | Value* vx1 = kb.vload_f32x8(px1); | |
| 522 | 12 | Value* next_vsum1 = kb.vadd(vsum1, vx1); | |
| 523 | 12 | Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64); | |
| 524 | 12 | kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1}); | |
| 525 | |||
| 526 | // Pass 1: Vector exit -> horizontal sum | ||
| 527 | 12 | fn->append_block(l1_vexit); | |
| 528 | 12 | Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64()); | |
| 529 | 12 | Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8()); | |
| 530 | 12 | kb.position_at_end(l1_vexit); | |
| 531 | 12 | Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1); | |
| 532 | 12 | kb.builder().build_br(l1_shead, {exit_iv1, init_sum1}); | |
| 533 | |||
| 534 | // Pass 1: Scalar remainder | ||
| 535 | 12 | fn->append_block(l1_shead); | |
| 536 | 12 | Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64()); | |
| 537 | 12 | Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32()); | |
| 538 | 12 | kb.position_at_end(l1_shead); | |
| 539 | 12 | Value* cond1_s = kb.builder().build_slt(iv1_s, d); | |
| 540 | 12 | kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1}); | |
| 541 | |||
| 542 | 12 | fn->append_block(l1_sbody); | |
| 543 | 12 | kb.position_at_end(l1_sbody); | |
| 544 | 12 | Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0); | |
| 545 | 12 | Value* next_sum1 = kb.add(sum1, xi1); | |
| 546 | 12 | Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64); | |
| 547 | 12 | kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1}); | |
| 548 | |||
| 549 | // Pass 1: Mean computed -> enter Pass 2 | ||
| 550 | 12 | fn->append_block(l1_exit); | |
| 551 | 12 | Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32()); | |
| 552 | 12 | kb.position_at_end(l1_exit); | |
| 553 | 12 | Value* mean = kb.mul(final_sum1, inv_d); | |
| 554 | 12 | Value* vmean = kb.vbroadcast(Type::f32x8(), mean); | |
| 555 | 12 | kb.builder().build_br_if(has_vec, l2_vhead, {zero_i64, vzero, mean, vmean}, l2_shead, {zero_i64, zero_f32, mean}); | |
| 556 | |||
| 557 | // Pass 2: Vector sum of squared deviations | ||
| 558 | 12 | fn->append_block(l2_vhead); | |
| 559 | 12 | Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64()); | |
| 560 | 12 | Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 561 | 12 | Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32()); | |
| 562 | 12 | Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 563 | 12 | kb.position_at_end(l2_vhead); | |
| 564 | 12 | Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d); | |
| 565 | 12 | kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar}); | |
| 566 | |||
| 567 | 12 | fn->append_block(l2_vbody); | |
| 568 | 12 | kb.position_at_end(l2_vbody); | |
| 569 | 12 | Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64); | |
| 570 | 12 | Value* px2 = kb.builder().build_add(x, byte_off2); | |
| 571 | 12 | Value* vx2 = kb.vload_f32x8(px2); | |
| 572 | 12 | Value* vdev = kb.vsub(vx2, vm2); | |
| 573 | 12 | Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2); | |
| 574 | 12 | Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64); | |
| 575 | 12 | kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2}); | |
| 576 | |||
| 577 | // Pass 2: Vector exit -> horizontal sum | ||
| 578 | 12 | fn->append_block(l2_vexit); | |
| 579 | 12 | Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64()); | |
| 580 | 12 | Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8()); | |
| 581 | 12 | Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32()); | |
| 582 | 12 | kb.position_at_end(l2_vexit); | |
| 583 | 12 | Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2); | |
| 584 | 12 | kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out}); | |
| 585 | |||
| 586 | // Pass 2: Scalar remainder | ||
| 587 | 12 | fn->append_block(l2_shead); | |
| 588 | 12 | Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64()); | |
| 589 | 12 | Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 590 | 12 | Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 591 | 12 | kb.position_at_end(l2_shead); | |
| 592 | 12 | Value* cond2_s = kb.builder().build_slt(iv2_s, d); | |
| 593 | 12 | kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s}); | |
| 594 | |||
| 595 | 12 | fn->append_block(l2_sbody); | |
| 596 | 12 | kb.position_at_end(l2_sbody); | |
| 597 | 12 | Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0); | |
| 598 | 12 | Value* diff2 = kb.sub(xi2, m2_s); | |
| 599 | 12 | Value* diff_sq2 = kb.mul(diff2, diff2); | |
| 600 | 12 | Value* next_sumsq = kb.add(sumsq, diff_sq2); | |
| 601 | 12 | Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64); | |
| 602 | 12 | kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s}); | |
| 603 | |||
| 604 | // Pass 2 exit -> compute rstd | ||
| 605 | 12 | fn->append_block(l2_exit); | |
| 606 | 12 | Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 607 | 12 | Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 608 | 12 | kb.position_at_end(l2_exit); | |
| 609 | 12 | Value* var = kb.mul(final_sumsq, inv_d); | |
| 610 | 12 | Value* var_eps = kb.add(var, eps); | |
| 611 | 12 | Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps)); | |
| 612 | 12 | Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean); | |
| 613 | 12 | Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd); | |
| 614 | 12 | kb.builder().build_br(l3_vhead, {zero_i64, final_mean, rstd, vmean3, vrstd3}); | |
| 615 | |||
| 616 | // Pass 3: Vector affine y[i] = gamma[i] * ((x[i] - mean) * rstd) + beta[i] | ||
| 617 | 12 | fn->append_block(l3_vhead); | |
| 618 | 12 | Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64()); | |
| 619 | 12 | Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 620 | 12 | Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 621 | 12 | Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 622 | 12 | Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 623 | 12 | kb.position_at_end(l3_vhead); | |
| 624 | 12 | Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d); | |
| 625 | 12 | kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s}); | |
| 626 | |||
| 627 | 12 | fn->append_block(l3_vbody); | |
| 628 | 12 | kb.position_at_end(l3_vbody); | |
| 629 | 12 | Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64); | |
| 630 | 12 | Value* px3 = kb.builder().build_add(x, byte_off3); | |
| 631 | 12 | Value* pg3 = kb.builder().build_add(gamma, byte_off3); | |
| 632 | 12 | Value* pb3 = kb.builder().build_add(beta, byte_off3); | |
| 633 | 12 | Value* py3 = kb.builder().build_add(y, byte_off3); | |
| 634 | 12 | Value* vx3 = kb.vload_f32x8(px3); | |
| 635 | 12 | Value* vg3 = kb.vload_f32x8(pg3); | |
| 636 | 12 | Value* vb3 = kb.vload_f32x8(pb3); | |
| 637 | 12 | Value* vdiff3 = kb.vsub(vx3, vm3); | |
| 638 | 12 | Value* vxhat3 = kb.vmul(vdiff3, vr3); | |
| 639 | 12 | Value* vy3 = kb.vfma(vg3, vxhat3, vb3); | |
| 640 | 12 | kb.vstore_f32x8(py3, vy3); | |
| 641 | 12 | Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64); | |
| 642 | 12 | kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3}); | |
| 643 | |||
| 644 | // Pass 3: Scalar remainder | ||
| 645 | 12 | fn->append_block(l3_shead); | |
| 646 | 12 | Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64()); | |
| 647 | 12 | Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 648 | 12 | Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 649 | 12 | kb.position_at_end(l3_shead); | |
| 650 | 12 | Value* cond3_s = kb.builder().build_slt(iv3_s, d); | |
| 651 | 12 | kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit); | |
| 652 | |||
| 653 | 12 | fn->append_block(l3_sbody); | |
| 654 | 12 | kb.position_at_end(l3_sbody); | |
| 655 | 12 | Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0); | |
| 656 | 12 | Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0); | |
| 657 | 12 | Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0); | |
| 658 | 12 | Value* diff3 = kb.sub(x3, m3_sc); | |
| 659 | 12 | Value* xhat3 = kb.mul(diff3, r3_sc); | |
| 660 | 12 | Value* y3 = kb.builder().build_fma_f32(g3, xhat3, b3); | |
| 661 | 12 | kb.store_f32_indexed(y, iv3_s, y3, 4, 0); | |
| 662 | 12 | Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64); | |
| 663 | 12 | kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc}); | |
| 664 | |||
| 665 | 12 | fn->append_block(l3_exit); | |
| 666 | 12 | kb.position_at_end(l3_exit); | |
| 667 | 12 | kb.builder().build_ret_void(); | |
| 668 | 12 | } | |
| 669 | |||
| 670 | // ── Kernel Initialization & AOT Disk Cache Management ─────────────────────── | ||
| 671 | static std::mutex s_core_jit_mu; | ||
| 672 | static std::atomic<bool> s_core_jit_ready{false}; | ||
| 673 | static JitKernels s_core_kernels; | ||
| 674 | |||
| 675 | 3690 | static const JitKernels& get_kernels() { | |
| 676 | 3690 | if (s_core_jit_ready.load(std::memory_order_acquire)) { | |
| 677 | 3675 | return s_core_kernels; | |
| 678 | } | ||
| 679 | 15 | std::lock_guard<std::mutex> lk(s_core_jit_mu); | |
| 680 | 15 | if (!s_core_jit_ready.load(std::memory_order_relaxed)) { | |
| 681 | try { | ||
| 682 | 15 | auto& cache = brotensor::jit::KernelDiskCache::instance(); | |
| 683 | 15 | brotensor::jit::KernelDiskCache::CpuCompileOptions opts; | |
| 684 | |||
| 685 | 30 | s_core_kernels.adaln_fn = cache.compile_or_load_cpu_fn<JitKernels::AdaLNModulateFn>( | |
| 686 | 15 | "cpu_core_adaln_modulate_v1", "fused_adaln_modulate", populate_adaln_modulate, opts); | |
| 687 | 30 | s_core_kernels.broadcast_mul_fn = cache.compile_or_load_cpu_fn<JitKernels::BroadcastMulFn>( | |
| 688 | 15 | "cpu_core_broadcast_mul_v1", "broadcast_mul", populate_broadcast_mul, opts); | |
| 689 | 30 | s_core_kernels.rms_norm_row_fn = cache.compile_or_load_cpu_fn<JitKernels::RmsNormRowFn>( | |
| 690 | 15 | "cpu_core_rms_norm_row_v1", "rms_norm_row", populate_rms_norm_row, opts); | |
| 691 | 30 | s_core_kernels.layernorm_row_fn = cache.compile_or_load_cpu_fn<JitKernels::LayerNormRowFn>( | |
| 692 | 15 | "cpu_core_layernorm_row_v1", "layernorm_row", populate_layernorm_row, opts); | |
| 693 | |||
| 694 | 30 | s_core_kernels.available = (s_core_kernels.adaln_fn != nullptr && | |
| 695 | 15 | s_core_kernels.broadcast_mul_fn != nullptr && | |
| 696 | 15 | s_core_kernels.rms_norm_row_fn != nullptr && | |
| 697 | 15 | s_core_kernels.layernorm_row_fn != nullptr); | |
| 698 | 15 | } catch (const std::exception& e) { | |
| 699 | ✗ | std::cerr << "brotensor: Brass JIT initialization failed: " << e.what() << "\n"; | |
| 700 | ✗ | s_core_kernels.available = false; | |
| 701 | ✗ | } | |
| 702 | 15 | s_core_jit_ready.store(true, std::memory_order_release); | |
| 703 | 15 | } | |
| 704 | 15 | return s_core_kernels; | |
| 705 | 3690 | } | |
| 706 | |||
| 707 | } // namespace | ||
| 708 | |||
| 709 | 1865 | bool is_jit_available() { | |
| 710 | 1865 | return get_kernels().available; | |
| 711 | } | ||
| 712 | |||
| 713 | ✗ | void prewarm_cpu_jit_kernels() { | |
| 714 | ✗ | (void)get_kernels(); | |
| 715 | ✗ | prewarm_fused_jit_kernels(); | |
| 716 | ✗ | prewarm_quant_jit_kernels(); | |
| 717 | ✗ | } | |
| 718 | |||
| 719 | 6 | void reset_cpu_jit_caches_for_testing() { | |
| 720 | { | ||
| 721 | 6 | std::lock_guard<std::mutex> lk(s_core_jit_mu); | |
| 722 | 6 | s_core_jit_ready.store(false, std::memory_order_relaxed); | |
| 723 | 6 | s_core_kernels = JitKernels{}; | |
| 724 | 6 | } | |
| 725 | 6 | reset_fused_jit_caches_for_testing(); | |
| 726 | 6 | reset_quant_jit_caches_for_testing(); | |
| 727 | 6 | brotensor::jit::KernelDiskCache::instance().clear_loaded_libraries(); | |
| 728 | 6 | } | |
| 729 | |||
| 730 | 381 | void rms_norm_forward(const float* X, const float* gamma, float eps, float* Y, int B, int D) { | |
| 731 | 381 | const auto& k = get_kernels(); | |
| 732 | 381 | if (!k.available || k.rms_norm_row_fn == nullptr || X == Y || gamma == nullptr || B <= 0 || D <= 0) { | |
| 733 | 1 | ref::rms_norm(X, gamma, eps, Y, B, D); | |
| 734 | 1 | return; | |
| 735 | } | ||
| 736 | |||
| 737 | 380 | const float inv_D = 1.0f / static_cast<float>(D); | |
| 738 | |||
| 739 | // Single-thread vectorized loop executes in sub-microsecond latency. | ||
| 740 | // Multi-threaded pool dispatch only when workload amortizes thread wake-up overhead. | ||
| 741 | 380 | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 742 | ✗ | detail::cpu::parallel_for(static_cast<std::size_t>(B), [&](std::size_t bi) { | |
| 743 | ✗ | const int b = static_cast<int>(bi); | |
| 744 | ✗ | const float* xr = X + static_cast<std::size_t>(b) * D; | |
| 745 | ✗ | float* yr = Y + static_cast<std::size_t>(b) * D; | |
| 746 | ✗ | k.rms_norm_row_fn(xr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D); | |
| 747 | ✗ | }); | |
| 748 | ✗ | } else { | |
| 749 | 3649 | for (int b = 0; b < B; ++b) { | |
| 750 | 3269 | const float* xr = X + static_cast<std::size_t>(b) * D; | |
| 751 | 3269 | float* yr = Y + static_cast<std::size_t>(b) * D; | |
| 752 | 3269 | k.rms_norm_row_fn(xr, gamma, yr, static_cast<uint64_t>(D), eps, inv_D); | |
| 753 | 3269 | } | |
| 754 | } | ||
| 755 | 381 | } | |
| 756 | |||
| 757 | // SwiGLU has no JIT kernel. brass's MlFusionCompiler::compile_swiglu evaluates | ||
| 758 | // exp(-g) as a degree-5 Taylor polynomial of -g/16 raised to the 16th power; | ||
| 759 | // the polynomial turns negative once -g/16 passes about -3.5, so every gate | ||
| 760 | // above ~55 came out with silu(g) ~ 0 instead of g, and ViT-H SwiGLU FFNs | ||
| 761 | // (DINOv3) put gates well past that. A JIT kernel calling the exact expf per | ||
| 762 | // element measured ~4x slower than this loop (MSVC vectorizes std::exp), so | ||
| 763 | // the row-parallel reference is the kernel. | ||
| 764 | 371 | void swiglu_forward(const float* X, float* Y, int B, int D) { | |
| 765 | 371 | (void)get_kernels(); | |
| 766 | 371 | ref::swiglu(X, Y, B, D); | |
| 767 | 371 | } | |
| 768 | |||
| 769 | // modulate and broadcast_mul are one load-op-store stream per element, which | ||
| 770 | // the compiler already vectorizes: on AArch64 clang's NEON loop is unrolled | ||
| 771 | // and runs these about 2x faster than the JIT's f32x8 (a q-register pair per | ||
| 772 | // iteration, not unrolled), so there the row-parallel reference is the kernel. | ||
| 773 | // The norms keep the JIT everywhere: their reductions are where it wins. | ||
| 774 | #if defined(__aarch64__) || defined(_M_ARM64) | ||
| 775 | constexpr bool kJitStreamingKernels = false; | ||
| 776 | #else | ||
| 777 | constexpr bool kJitStreamingKernels = true; | ||
| 778 | #endif | ||
| 779 | |||
| 780 | 370 | void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) { | |
| 781 | 370 | const auto& k = get_kernels(); | |
| 782 | if (!kJitStreamingKernels || !k.available || k.adaln_fn == nullptr) { | ||
| 783 | 370 | ref::modulate(X, scale, shift, Y, L, D); | |
| 784 | 370 | return; | |
| 785 | } | ||
| 786 | |||
| 787 | if (L > 1 && static_cast<int64_t>(L) * D >= 262144) { | ||
| 788 | detail::cpu::parallel_for(static_cast<std::size_t>(L), [&](std::size_t li) { | ||
| 789 | const int l = static_cast<int>(li); | ||
| 790 | const float* xr = X + static_cast<std::size_t>(l) * D; | ||
| 791 | float* yr = Y + static_cast<std::size_t>(l) * D; | ||
| 792 | k.adaln_fn(xr, scale, shift, yr, static_cast<uint64_t>(D)); | ||
| 793 | }); | ||
| 794 | } else { | ||
| 795 | for (int l = 0; l < L; ++l) { | ||
| 796 | const float* xr = X + static_cast<std::size_t>(l) * D; | ||
| 797 | float* yr = Y + static_cast<std::size_t>(l) * D; | ||
| 798 | k.adaln_fn(xr, scale, shift, yr, static_cast<uint64_t>(D)); | ||
| 799 | } | ||
| 800 | } | ||
| 801 | } | ||
| 802 | |||
| 803 | 370 | void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) { | |
| 804 | 370 | const auto& k = get_kernels(); | |
| 805 | if (!kJitStreamingKernels || !k.available || k.broadcast_mul_fn == nullptr) { | ||
| 806 | 370 | ref::broadcast_mul(X, v, Y, L, D); | |
| 807 | 370 | return; | |
| 808 | } | ||
| 809 | |||
| 810 | if (L > 1 && static_cast<int64_t>(L) * D >= 262144) { | ||
| 811 | detail::cpu::parallel_for(static_cast<std::size_t>(L), [&](std::size_t li) { | ||
| 812 | const int l = static_cast<int>(li); | ||
| 813 | const float* xr = X + static_cast<std::size_t>(l) * D; | ||
| 814 | float* yr = Y + static_cast<std::size_t>(l) * D; | ||
| 815 | k.broadcast_mul_fn(xr, v, yr, static_cast<uint64_t>(D)); | ||
| 816 | }); | ||
| 817 | } else { | ||
| 818 | for (int l = 0; l < L; ++l) { | ||
| 819 | const float* xr = X + static_cast<std::size_t>(l) * D; | ||
| 820 | float* yr = Y + static_cast<std::size_t>(l) * D; | ||
| 821 | k.broadcast_mul_fn(xr, v, yr, static_cast<uint64_t>(D)); | ||
| 822 | } | ||
| 823 | } | ||
| 824 | } | ||
| 825 | |||
| 826 | 333 | void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta, float eps, float* Y, int R, int D) { | |
| 827 | 333 | const auto& k = get_kernels(); | |
| 828 | 333 | if (!k.available || k.layernorm_row_fn == nullptr || X == Y || gamma == nullptr || beta == nullptr || R <= 0 || D <= 0) { | |
| 829 | 1 | ref::layernorm_forward_inference_batched(X, gamma, beta, eps, Y, R, D); | |
| 830 | 1 | return; | |
| 831 | } | ||
| 832 | |||
| 833 | 332 | const float inv_D = 1.0f / static_cast<float>(D); | |
| 834 | |||
| 835 | 332 | if (R > 1 && static_cast<int64_t>(R) * D >= 262144) { | |
| 836 | ✗ | detail::cpu::parallel_for(static_cast<std::size_t>(R), [&](std::size_t ri) { | |
| 837 | ✗ | const int r = static_cast<int>(ri); | |
| 838 | ✗ | const float* xr = X + static_cast<std::size_t>(r) * D; | |
| 839 | ✗ | float* yr = Y + static_cast<std::size_t>(r) * D; | |
| 840 | ✗ | k.layernorm_row_fn(xr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D); | |
| 841 | ✗ | }); | |
| 842 | ✗ | } else { | |
| 843 | 3846 | for (int r = 0; r < R; ++r) { | |
| 844 | 3514 | const float* xr = X + static_cast<std::size_t>(r) * D; | |
| 845 | 3514 | float* yr = Y + static_cast<std::size_t>(r) * D; | |
| 846 | 3514 | k.layernorm_row_fn(xr, gamma, beta, yr, static_cast<uint64_t>(D), eps, inv_D); | |
| 847 | 3514 | } | |
| 848 | } | ||
| 849 | 333 | } | |
| 850 | |||
| 851 | } // namespace brotensor::detail::cpu::jit | ||
| 852 | |||
| 853 | #else // !BROTENSOR_HAS_BRASS_JIT | ||
| 854 | |||
| 855 | namespace brotensor::detail::cpu::jit { | ||
| 856 | |||
| 857 | bool is_jit_available() { return false; } | ||
| 858 | void prewarm_cpu_jit_kernels() {} | ||
| 859 | void reset_cpu_jit_caches_for_testing() {} | ||
| 860 | void rms_norm_forward(const float* X, const float* gamma, float eps, float* Y, int B, int D) { | ||
| 861 | ref::rms_norm(X, gamma, eps, Y, B, D); | ||
| 862 | } | ||
| 863 | void swiglu_forward(const float* X, float* Y, int B, int D) { | ||
| 864 | (void)get_kernels(); | ||
| 865 | ref::swiglu(X, Y, B, D); | ||
| 866 | } | ||
| 867 | void modulate(const float* X, const float* scale, const float* shift, float* Y, int L, int D) { | ||
| 868 | ref::modulate(X, scale, shift, Y, L, D); | ||
| 869 | } | ||
| 870 | void broadcast_mul(const float* X, const float* v, float* Y, int L, int D) { | ||
| 871 | ref::broadcast_mul(X, v, Y, L, D); | ||
| 872 | } | ||
| 873 | void layernorm_forward_inference_batched(const float* X, const float* gamma, const float* beta, float eps, float* Y, int R, int D) { | ||
| 874 | ref::layernorm_forward_inference_batched(X, gamma, beta, eps, Y, R, D); | ||
| 875 | } | ||
| 876 | |||
| 877 | } // namespace brotensor::detail::cpu::jit | ||
| 878 | |||
| 879 | #endif // BROTENSOR_HAS_BRASS_JIT | ||
| 880 |