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 | ✗ | 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 | ✗ | 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 | ✗ | for (int i = 0; i < D; ++i) { | |
| 24 | ✗ | yr[i] = xr[i] * gamma[i] * rrms; | |
| 25 | ✗ | } | |
| 26 | ✗ | }; | |
| 27 | ✗ | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 28 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) { | |
| 29 | ✗ | row_fn(static_cast<int>(bi)); | |
| 30 | ✗ | }); | |
| 31 | ✗ | } else { | |
| 32 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | 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 | ✗ | for (int i = 0; i < D; ++i) { | |
| 58 | ✗ | float g = gamma ? gamma[i] : 1.0f; | |
| 59 | ✗ | float bv = beta ? beta[i] : 0.0f; | |
| 60 | ✗ | yr[i] = g * ((xr[i] - mean) * rstd) + bv; | |
| 61 | ✗ | } | |
| 62 | ✗ | }; | |
| 63 | ✗ | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 64 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(B), [&](size_t bi) { | |
| 65 | ✗ | row_fn(static_cast<int>(bi)); | |
| 66 | ✗ | }); | |
| 67 | ✗ | } else { | |
| 68 | ✗ | 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 | ✗ | 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 | ✗ | for (int i = 0; i < D; ++i) sum += xr[i]; | |
| 81 | ✗ | float mean = sum * inv_D; | |
| 82 | ✗ | float sumsq = 0.0f; | |
| 83 | ✗ | 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 | ✗ | for (int i = 0; i < D; ++i) { | |
| 90 | ✗ | float g = gamma ? gamma[i] : 1.0f; | |
| 91 | ✗ | 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 | ✗ | if (R > 1 && static_cast<int64_t>(R) * D >= 262144) { | |
| 97 | ✗ | detail::cpu::parallel_for(static_cast<size_t>(R), [&](size_t ri) { | |
| 98 | ✗ | row_fn(static_cast<int>(ri)); | |
| 99 | ✗ | }); | |
| 100 | ✗ | } else { | |
| 101 | ✗ | 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 | 4 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 166 | 4 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 167 | 4 | Value* res = kb.builder().add_block_param(entry, Type::ptr()); | |
| 168 | 4 | Value* gamma = kb.builder().add_block_param(entry, Type::ptr()); | |
| 169 | 4 | Value* y = kb.builder().add_block_param(entry, Type::ptr()); | |
| 170 | 4 | Value* d = kb.builder().add_block_param(entry, Type::i64()); | |
| 171 | 4 | Value* eps = kb.builder().add_block_param(entry, Type::f32()); | |
| 172 | 4 | Value* inv_d = kb.builder().add_block_param(entry, Type::f32()); | |
| 173 | |||
| 174 | 4 | BasicBlock* l1_vhead = kb.builder().create_block("res_rms_l1_vhead"); | |
| 175 | 4 | BasicBlock* l1_vbody = kb.builder().create_block("res_rms_l1_vbody"); | |
| 176 | 4 | BasicBlock* l1_vexit = kb.builder().create_block("res_rms_l1_vexit"); | |
| 177 | 4 | BasicBlock* l1_shead = kb.builder().create_block("res_rms_l1_shead"); | |
| 178 | 4 | BasicBlock* l1_sbody = kb.builder().create_block("res_rms_l1_sbody"); | |
| 179 | 4 | BasicBlock* l1_exit = kb.builder().create_block("res_rms_l1_exit"); | |
| 180 | |||
| 181 | 4 | BasicBlock* l2_vhead = kb.builder().create_block("res_rms_l2_vhead"); | |
| 182 | 4 | BasicBlock* l2_vbody = kb.builder().create_block("res_rms_l2_vbody"); | |
| 183 | 4 | BasicBlock* l2_shead = kb.builder().create_block("res_rms_l2_shead"); | |
| 184 | 4 | BasicBlock* l2_sbody = kb.builder().create_block("res_rms_l2_sbody"); | |
| 185 | 4 | BasicBlock* l2_exit = kb.builder().create_block("res_rms_l2_exit"); | |
| 186 | |||
| 187 | 4 | kb.position_at_end(entry); | |
| 188 | 4 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 189 | 4 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 190 | 4 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 191 | 4 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 192 | 4 | Value* zero_f32 = kb.builder().build_fconst_f32(0.0f); | |
| 193 | 4 | Value* vzero = kb.vzero(Type::f32x8()); | |
| 194 | 4 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 195 | 4 | Value* vec_d = kb.builder().build_and(d, mask_eight); | |
| 196 | |||
| 197 | 4 | Value* has_vec = kb.builder().build_sge(d, eight_i64); | |
| 198 | 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 | 4 | fn->append_block(l1_vhead); | |
| 202 | 4 | Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64()); | |
| 203 | 4 | Value* vsum = kb.builder().add_block_param(l1_vhead, Type::f32x8()); | |
| 204 | 4 | kb.position_at_end(l1_vhead); | |
| 205 | 4 | Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d); | |
| 206 | 4 | kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum}); | |
| 207 | |||
| 208 | 4 | fn->append_block(l1_vbody); | |
| 209 | 4 | kb.position_at_end(l1_vbody); | |
| 210 | 4 | Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64); | |
| 211 | 4 | Value* px1 = kb.builder().build_add(x, byte_off1); | |
| 212 | 4 | Value* pres1 = kb.builder().build_add(res, byte_off1); | |
| 213 | 4 | Value* vx1 = kb.vload_f32x8(px1); | |
| 214 | 4 | Value* vr1 = kb.vload_f32x8(pres1); | |
| 215 | 4 | Value* vx_new = kb.vadd(vx1, vr1); | |
| 216 | 4 | kb.vstore_f32x8(px1, vx_new); // X += res in-place | |
| 217 | 4 | Value* next_vsum = kb.vfma(vx_new, vx_new, vsum); // sum_sq accumulation in exact same pass | |
| 218 | 4 | Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64); | |
| 219 | 4 | kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum}); | |
| 220 | |||
| 221 | // Pass 1: Vector exit -> horizontal reduction | ||
| 222 | 4 | fn->append_block(l1_vexit); | |
| 223 | 4 | Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64()); | |
| 224 | 4 | Value* exit_vsum = kb.builder().add_block_param(l1_vexit, Type::f32x8()); | |
| 225 | 4 | kb.position_at_end(l1_vexit); | |
| 226 | 4 | Value* init_scalar_sum = reduce_vsum8(kb, y, exit_vsum); | |
| 227 | 4 | kb.builder().build_br(l1_shead, {exit_iv1, init_scalar_sum}); | |
| 228 | |||
| 229 | // Pass 1: Scalar remainder | ||
| 230 | 4 | fn->append_block(l1_shead); | |
| 231 | 4 | Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64()); | |
| 232 | 4 | Value* sum_sq = kb.builder().add_block_param(l1_shead, Type::f32()); | |
| 233 | 4 | kb.position_at_end(l1_shead); | |
| 234 | 4 | Value* cond1_s = kb.builder().build_slt(iv1_s, d); | |
| 235 | 4 | kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum_sq}); | |
| 236 | |||
| 237 | 4 | fn->append_block(l1_sbody); | |
| 238 | 4 | kb.position_at_end(l1_sbody); | |
| 239 | 4 | Value* xi = kb.load_f32_indexed(x, iv1_s, 4, 0); | |
| 240 | 4 | Value* ri = kb.load_f32_indexed(res, iv1_s, 4, 0); | |
| 241 | 4 | Value* xi_new = kb.add(xi, ri); | |
| 242 | 4 | kb.store_f32_indexed(x, iv1_s, xi_new, 4, 0); | |
| 243 | 4 | Value* xi_sq = kb.mul(xi_new, xi_new); | |
| 244 | 4 | Value* next_sum = kb.add(sum_sq, xi_sq); | |
| 245 | 4 | Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64); | |
| 246 | 4 | kb.builder().build_br(l1_shead, {next_iv1_s, next_sum}); | |
| 247 | |||
| 248 | // Pass 1 exit -> compute rrms | ||
| 249 | 4 | fn->append_block(l1_exit); | |
| 250 | 4 | Value* final_sum = kb.builder().add_block_param(l1_exit, Type::f32()); | |
| 251 | 4 | kb.position_at_end(l1_exit); | |
| 252 | 4 | Value* mean_sq = kb.mul(final_sum, inv_d); | |
| 253 | 4 | Value* denom = kb.add(mean_sq, eps); | |
| 254 | 4 | Value* rrms = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(denom)); | |
| 255 | 4 | Value* vrrms = kb.vbroadcast(Type::f32x8(), rrms); | |
| 256 | 4 | kb.builder().build_br(l2_vhead, {zero_i64, rrms, vrrms}); | |
| 257 | |||
| 258 | // Pass 2: Vector loop y = x * gamma * rrms | ||
| 259 | 4 | fn->append_block(l2_vhead); | |
| 260 | 4 | Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64()); | |
| 261 | 4 | Value* rrms_val = kb.builder().add_block_param(l2_vhead, Type::f32()); | |
| 262 | 4 | Value* vrrms_val = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 263 | 4 | kb.position_at_end(l2_vhead); | |
| 264 | 4 | Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d); | |
| 265 | 4 | kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_shead, {iv2_v, rrms_val}); | |
| 266 | |||
| 267 | 4 | fn->append_block(l2_vbody); | |
| 268 | 4 | kb.position_at_end(l2_vbody); | |
| 269 | 4 | Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64); | |
| 270 | 4 | Value* px2 = kb.builder().build_add(x, byte_off2); | |
| 271 | 4 | Value* pg2 = kb.builder().build_add(gamma, byte_off2); | |
| 272 | 4 | Value* py2 = kb.builder().build_add(y, byte_off2); | |
| 273 | 4 | Value* vx2 = kb.vload_f32x8(px2); | |
| 274 | 4 | Value* vg2 = kb.vload_f32x8(pg2); | |
| 275 | 4 | Value* vxg = kb.vmul(vx2, vg2); | |
| 276 | 4 | Value* vy = kb.vmul(vxg, vrrms_val); | |
| 277 | 4 | kb.vstore_f32x8(py2, vy); | |
| 278 | 4 | Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64); | |
| 279 | 4 | kb.builder().build_br(l2_vhead, {next_iv2_v, rrms_val, vrrms_val}); | |
| 280 | |||
| 281 | // Pass 2: Scalar remainder | ||
| 282 | 4 | fn->append_block(l2_shead); | |
| 283 | 4 | Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64()); | |
| 284 | 4 | Value* rrms_scalar = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 285 | 4 | kb.position_at_end(l2_shead); | |
| 286 | 4 | Value* cond2_s = kb.builder().build_slt(iv2_s, d); | |
| 287 | 4 | kb.builder().build_br_if(cond2_s, l2_sbody, l2_exit); | |
| 288 | |||
| 289 | 4 | fn->append_block(l2_sbody); | |
| 290 | 4 | kb.position_at_end(l2_sbody); | |
| 291 | 4 | Value* x2 = kb.load_f32_indexed(x, iv2_s, 4, 0); | |
| 292 | 4 | Value* g2 = kb.load_f32_indexed(gamma, iv2_s, 4, 0); | |
| 293 | 4 | Value* xg2 = kb.mul(x2, g2); | |
| 294 | 4 | Value* out_val = kb.mul(xg2, rrms_scalar); | |
| 295 | 4 | kb.store_f32_indexed(y, iv2_s, out_val, 4, 0); | |
| 296 | 4 | Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64); | |
| 297 | 4 | kb.builder().build_br(l2_shead, {next_iv2_s, rrms_scalar}); | |
| 298 | |||
| 299 | 4 | fn->append_block(l2_exit); | |
| 300 | 4 | kb.position_at_end(l2_exit); | |
| 301 | 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 | 4 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 316 | 4 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 317 | 4 | Value* res = kb.builder().add_block_param(entry, Type::ptr()); | |
| 318 | 4 | Value* gamma = kb.builder().add_block_param(entry, Type::ptr()); | |
| 319 | 4 | Value* beta = kb.builder().add_block_param(entry, Type::ptr()); | |
| 320 | 4 | Value* y = kb.builder().add_block_param(entry, Type::ptr()); | |
| 321 | 4 | Value* d = kb.builder().add_block_param(entry, Type::i64()); | |
| 322 | 4 | Value* eps = kb.builder().add_block_param(entry, Type::f32()); | |
| 323 | 4 | Value* inv_d = kb.builder().add_block_param(entry, Type::f32()); | |
| 324 | |||
| 325 | // Pass 1 blocks (residual add + mean) | ||
| 326 | 4 | BasicBlock* l1_vhead = kb.builder().create_block("res_ln_l1_vhead"); | |
| 327 | 4 | BasicBlock* l1_vbody = kb.builder().create_block("res_ln_l1_vbody"); | |
| 328 | 4 | BasicBlock* l1_vexit = kb.builder().create_block("res_ln_l1_vexit"); | |
| 329 | 4 | BasicBlock* l1_shead = kb.builder().create_block("res_ln_l1_shead"); | |
| 330 | 4 | BasicBlock* l1_sbody = kb.builder().create_block("res_ln_l1_sbody"); | |
| 331 | 4 | BasicBlock* l1_exit = kb.builder().create_block("res_ln_l1_exit"); | |
| 332 | |||
| 333 | // Pass 2 blocks (variance) | ||
| 334 | 4 | BasicBlock* l2_vhead = kb.builder().create_block("res_ln_l2_vhead"); | |
| 335 | 4 | BasicBlock* l2_vbody = kb.builder().create_block("res_ln_l2_vbody"); | |
| 336 | 4 | BasicBlock* l2_vexit = kb.builder().create_block("res_ln_l2_vexit"); | |
| 337 | 4 | BasicBlock* l2_shead = kb.builder().create_block("res_ln_l2_shead"); | |
| 338 | 4 | BasicBlock* l2_sbody = kb.builder().create_block("res_ln_l2_sbody"); | |
| 339 | 4 | BasicBlock* l2_exit = kb.builder().create_block("res_ln_l2_exit"); | |
| 340 | |||
| 341 | // Pass 3 blocks (gamma * xhat + beta) | ||
| 342 | 4 | BasicBlock* l3_vhead = kb.builder().create_block("res_ln_l3_vhead"); | |
| 343 | 4 | BasicBlock* l3_vbody = kb.builder().create_block("res_ln_l3_vbody"); | |
| 344 | 4 | BasicBlock* l3_shead = kb.builder().create_block("res_ln_l3_shead"); | |
| 345 | 4 | BasicBlock* l3_sbody = kb.builder().create_block("res_ln_l3_sbody"); | |
| 346 | 4 | BasicBlock* l3_exit = kb.builder().create_block("res_ln_l3_exit"); | |
| 347 | |||
| 348 | 4 | kb.position_at_end(entry); | |
| 349 | 4 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 350 | 4 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 351 | 4 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 352 | 4 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 353 | 4 | Value* zero_f32 = kb.builder().build_fconst_f32(0.0f); | |
| 354 | 4 | Value* vzero = kb.vzero(Type::f32x8()); | |
| 355 | 4 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 356 | 4 | Value* vec_d = kb.builder().build_and(d, mask_eight); | |
| 357 | |||
| 358 | 4 | Value* has_vec = kb.builder().build_sge(d, eight_i64); | |
| 359 | 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 | 4 | fn->append_block(l1_vhead); | |
| 363 | 4 | Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64()); | |
| 364 | 4 | Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8()); | |
| 365 | 4 | kb.position_at_end(l1_vhead); | |
| 366 | 4 | Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d); | |
| 367 | 4 | kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1}); | |
| 368 | |||
| 369 | 4 | fn->append_block(l1_vbody); | |
| 370 | 4 | kb.position_at_end(l1_vbody); | |
| 371 | 4 | Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64); | |
| 372 | 4 | Value* px1 = kb.builder().build_add(x, byte_off1); | |
| 373 | 4 | Value* pres1 = kb.builder().build_add(res, byte_off1); | |
| 374 | 4 | Value* vx1 = kb.vload_f32x8(px1); | |
| 375 | 4 | Value* vr1 = kb.vload_f32x8(pres1); | |
| 376 | 4 | Value* vx_new = kb.vadd(vx1, vr1); | |
| 377 | 4 | kb.vstore_f32x8(px1, vx_new); // X += res in-place! | |
| 378 | 4 | Value* next_vsum1 = kb.vadd(vsum1, vx_new); | |
| 379 | 4 | Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64); | |
| 380 | 4 | kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1}); | |
| 381 | |||
| 382 | // Pass 1: Vector exit -> horizontal sum | ||
| 383 | 4 | fn->append_block(l1_vexit); | |
| 384 | 4 | Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64()); | |
| 385 | 4 | Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8()); | |
| 386 | 4 | kb.position_at_end(l1_vexit); | |
| 387 | 4 | Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1); | |
| 388 | 4 | kb.builder().build_br(l1_shead, {exit_iv1, init_sum1}); | |
| 389 | |||
| 390 | // Pass 1: Scalar remainder | ||
| 391 | 4 | fn->append_block(l1_shead); | |
| 392 | 4 | Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64()); | |
| 393 | 4 | Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32()); | |
| 394 | 4 | kb.position_at_end(l1_shead); | |
| 395 | 4 | Value* cond1_s = kb.builder().build_slt(iv1_s, d); | |
| 396 | 4 | kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1}); | |
| 397 | |||
| 398 | 4 | fn->append_block(l1_sbody); | |
| 399 | 4 | kb.position_at_end(l1_sbody); | |
| 400 | 4 | Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0); | |
| 401 | 4 | Value* ri1 = kb.load_f32_indexed(res, iv1_s, 4, 0); | |
| 402 | 4 | Value* xi_new = kb.add(xi1, ri1); | |
| 403 | 4 | kb.store_f32_indexed(x, iv1_s, xi_new, 4, 0); // X += res in-place! | |
| 404 | 4 | Value* next_sum1 = kb.add(sum1, xi_new); | |
| 405 | 4 | Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64); | |
| 406 | 4 | kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1}); | |
| 407 | |||
| 408 | // Pass 1: Mean computed -> enter Pass 2 | ||
| 409 | 4 | fn->append_block(l1_exit); | |
| 410 | 4 | Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32()); | |
| 411 | 4 | kb.position_at_end(l1_exit); | |
| 412 | 4 | Value* mean = kb.mul(final_sum1, inv_d); | |
| 413 | 4 | Value* vmean = kb.vbroadcast(Type::f32x8(), mean); | |
| 414 | 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 | 4 | fn->append_block(l2_vhead); | |
| 418 | 4 | Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64()); | |
| 419 | 4 | Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 420 | 4 | Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32()); | |
| 421 | 4 | Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 422 | 4 | kb.position_at_end(l2_vhead); | |
| 423 | 4 | Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d); | |
| 424 | 4 | kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar}); | |
| 425 | |||
| 426 | 4 | fn->append_block(l2_vbody); | |
| 427 | 4 | kb.position_at_end(l2_vbody); | |
| 428 | 4 | Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64); | |
| 429 | 4 | Value* px2 = kb.builder().build_add(x, byte_off2); | |
| 430 | 4 | Value* vx2 = kb.vload_f32x8(px2); | |
| 431 | 4 | Value* vdev = kb.vsub(vx2, vm2); | |
| 432 | 4 | Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2); | |
| 433 | 4 | Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64); | |
| 434 | 4 | kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2}); | |
| 435 | |||
| 436 | // Pass 2: Vector exit -> horizontal sum | ||
| 437 | 4 | fn->append_block(l2_vexit); | |
| 438 | 4 | Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64()); | |
| 439 | 4 | Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8()); | |
| 440 | 4 | Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32()); | |
| 441 | 4 | kb.position_at_end(l2_vexit); | |
| 442 | 4 | Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2); | |
| 443 | 4 | kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out}); | |
| 444 | |||
| 445 | // Pass 2: Scalar remainder | ||
| 446 | 4 | fn->append_block(l2_shead); | |
| 447 | 4 | Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64()); | |
| 448 | 4 | Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 449 | 4 | Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 450 | 4 | kb.position_at_end(l2_shead); | |
| 451 | 4 | Value* cond2_s = kb.builder().build_slt(iv2_s, d); | |
| 452 | 4 | kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s}); | |
| 453 | |||
| 454 | 4 | fn->append_block(l2_sbody); | |
| 455 | 4 | kb.position_at_end(l2_sbody); | |
| 456 | 4 | Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0); | |
| 457 | 4 | Value* diff2 = kb.sub(xi2, m2_s); | |
| 458 | 4 | Value* diff_sq2 = kb.mul(diff2, diff2); | |
| 459 | 4 | Value* next_sumsq = kb.add(sumsq, diff_sq2); | |
| 460 | 4 | Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64); | |
| 461 | 4 | kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s}); | |
| 462 | |||
| 463 | // Pass 2 exit -> compute rstd | ||
| 464 | 4 | fn->append_block(l2_exit); | |
| 465 | 4 | Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 466 | 4 | Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 467 | 4 | kb.position_at_end(l2_exit); | |
| 468 | 4 | Value* var = kb.mul(final_sumsq, inv_d); | |
| 469 | 4 | Value* var_eps = kb.add(var, eps); | |
| 470 | 4 | Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps)); | |
| 471 | 4 | Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean); | |
| 472 | 4 | Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd); | |
| 473 | 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 | 4 | fn->append_block(l3_vhead); | |
| 477 | 4 | Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64()); | |
| 478 | 4 | Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 479 | 4 | Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 480 | 4 | Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 481 | 4 | Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 482 | 4 | kb.position_at_end(l3_vhead); | |
| 483 | 4 | Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d); | |
| 484 | 4 | kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s}); | |
| 485 | |||
| 486 | 4 | fn->append_block(l3_vbody); | |
| 487 | 4 | kb.position_at_end(l3_vbody); | |
| 488 | 4 | Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64); | |
| 489 | 4 | Value* px3 = kb.builder().build_add(x, byte_off3); | |
| 490 | 4 | Value* pg3 = kb.builder().build_add(gamma, byte_off3); | |
| 491 | 4 | Value* pb3 = kb.builder().build_add(beta, byte_off3); | |
| 492 | 4 | Value* py3 = kb.builder().build_add(y, byte_off3); | |
| 493 | 4 | Value* vx3 = kb.vload_f32x8(px3); | |
| 494 | 4 | Value* vg3 = kb.vload_f32x8(pg3); | |
| 495 | 4 | Value* vb3 = kb.vload_f32x8(pb3); | |
| 496 | 4 | Value* vdiff3 = kb.vsub(vx3, vm3); | |
| 497 | 4 | Value* vxhat3 = kb.vmul(vdiff3, vr3); | |
| 498 | 4 | Value* vy3 = kb.vfma(vg3, vxhat3, vb3); // y = gamma * xhat + beta | |
| 499 | 4 | kb.vstore_f32x8(py3, vy3); | |
| 500 | 4 | Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64); | |
| 501 | 4 | kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3}); | |
| 502 | |||
| 503 | // Pass 3: Scalar remainder | ||
| 504 | 4 | fn->append_block(l3_shead); | |
| 505 | 4 | Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64()); | |
| 506 | 4 | Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 507 | 4 | Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 508 | 4 | kb.position_at_end(l3_shead); | |
| 509 | 4 | Value* cond3_s = kb.builder().build_slt(iv3_s, d); | |
| 510 | 4 | kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit); | |
| 511 | |||
| 512 | 4 | fn->append_block(l3_sbody); | |
| 513 | 4 | kb.position_at_end(l3_sbody); | |
| 514 | 4 | Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0); | |
| 515 | 4 | Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0); | |
| 516 | 4 | Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0); | |
| 517 | 4 | Value* diff3 = kb.sub(x3, m3_sc); | |
| 518 | 4 | Value* xhat3 = kb.mul(diff3, r3_sc); | |
| 519 | 4 | Value* y3 = kb.builder().build_fma_f32(g3, xhat3, b3); | |
| 520 | 4 | kb.store_f32_indexed(y, iv3_s, y3, 4, 0); | |
| 521 | 4 | Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64); | |
| 522 | 4 | kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc}); | |
| 523 | |||
| 524 | 4 | fn->append_block(l3_exit); | |
| 525 | 4 | kb.position_at_end(l3_exit); | |
| 526 | 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 | 4 | BasicBlock* entry = kb.builder().append_block("entry"); | |
| 541 | 4 | Value* x = kb.builder().add_block_param(entry, Type::ptr()); | |
| 542 | 4 | Value* gamma = kb.builder().add_block_param(entry, Type::ptr()); | |
| 543 | 4 | Value* beta = kb.builder().add_block_param(entry, Type::ptr()); | |
| 544 | 4 | Value* scale = kb.builder().add_block_param(entry, Type::ptr()); | |
| 545 | 4 | Value* shift = kb.builder().add_block_param(entry, Type::ptr()); | |
| 546 | 4 | Value* y = kb.builder().add_block_param(entry, Type::ptr()); | |
| 547 | 4 | Value* d = kb.builder().add_block_param(entry, Type::i64()); | |
| 548 | 4 | Value* eps = kb.builder().add_block_param(entry, Type::f32()); | |
| 549 | 4 | Value* inv_d = kb.builder().add_block_param(entry, Type::f32()); | |
| 550 | |||
| 551 | // Pass 1 blocks (mean) | ||
| 552 | 4 | BasicBlock* l1_vhead = kb.builder().create_block("flm_l1_vhead"); | |
| 553 | 4 | BasicBlock* l1_vbody = kb.builder().create_block("flm_l1_vbody"); | |
| 554 | 4 | BasicBlock* l1_vexit = kb.builder().create_block("flm_l1_vexit"); | |
| 555 | 4 | BasicBlock* l1_shead = kb.builder().create_block("flm_l1_shead"); | |
| 556 | 4 | BasicBlock* l1_sbody = kb.builder().create_block("flm_l1_sbody"); | |
| 557 | 4 | BasicBlock* l1_exit = kb.builder().create_block("flm_l1_exit"); | |
| 558 | |||
| 559 | // Pass 2 blocks (variance) | ||
| 560 | 4 | BasicBlock* l2_vhead = kb.builder().create_block("flm_l2_vhead"); | |
| 561 | 4 | BasicBlock* l2_vbody = kb.builder().create_block("flm_l2_vbody"); | |
| 562 | 4 | BasicBlock* l2_vexit = kb.builder().create_block("flm_l2_vexit"); | |
| 563 | 4 | BasicBlock* l2_shead = kb.builder().create_block("flm_l2_shead"); | |
| 564 | 4 | BasicBlock* l2_sbody = kb.builder().create_block("flm_l2_sbody"); | |
| 565 | 4 | BasicBlock* l2_exit = kb.builder().create_block("flm_l2_exit"); | |
| 566 | |||
| 567 | // Pass 3 blocks (fused LayerNorm + Modulate directly to Y) | ||
| 568 | 4 | BasicBlock* l3_vhead = kb.builder().create_block("flm_l3_vhead"); | |
| 569 | 4 | BasicBlock* l3_vbody = kb.builder().create_block("flm_l3_vbody"); | |
| 570 | 4 | BasicBlock* l3_shead = kb.builder().create_block("flm_l3_shead"); | |
| 571 | 4 | BasicBlock* l3_sbody = kb.builder().create_block("flm_l3_sbody"); | |
| 572 | 4 | BasicBlock* l3_exit = kb.builder().create_block("flm_l3_exit"); | |
| 573 | |||
| 574 | 4 | kb.position_at_end(entry); | |
| 575 | 4 | Value* zero_i64 = kb.builder().build_iconst_i64(0); | |
| 576 | 4 | Value* one_i64 = kb.builder().build_iconst_i64(1); | |
| 577 | 4 | Value* two_i64 = kb.builder().build_iconst_i64(2); | |
| 578 | 4 | Value* eight_i64 = kb.builder().build_iconst_i64(8); | |
| 579 | 4 | Value* zero_f32 = kb.builder().build_fconst_f32(0.0f); | |
| 580 | 4 | Value* one_f32 = kb.builder().build_fconst_f32(1.0f); | |
| 581 | 4 | Value* vzero = kb.vzero(Type::f32x8()); | |
| 582 | 4 | Value* vone = kb.vbroadcast(Type::f32x8(), one_f32); | |
| 583 | 4 | Value* mask_eight = kb.builder().build_iconst_i64(~int64_t(7)); | |
| 584 | 4 | Value* vec_d = kb.builder().build_and(d, mask_eight); | |
| 585 | |||
| 586 | 4 | Value* has_vec = kb.builder().build_sge(d, eight_i64); | |
| 587 | 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 | 4 | fn->append_block(l1_vhead); | |
| 591 | 4 | Value* iv1_v = kb.builder().add_block_param(l1_vhead, Type::i64()); | |
| 592 | 4 | Value* vsum1 = kb.builder().add_block_param(l1_vhead, Type::f32x8()); | |
| 593 | 4 | kb.position_at_end(l1_vhead); | |
| 594 | 4 | Value* cond1_v = kb.builder().build_slt(iv1_v, vec_d); | |
| 595 | 4 | kb.builder().build_br_if(cond1_v, l1_vbody, {}, l1_vexit, {iv1_v, vsum1}); | |
| 596 | |||
| 597 | 4 | fn->append_block(l1_vbody); | |
| 598 | 4 | kb.position_at_end(l1_vbody); | |
| 599 | 4 | Value* byte_off1 = kb.builder().build_shl(iv1_v, two_i64); | |
| 600 | 4 | Value* px1 = kb.builder().build_add(x, byte_off1); | |
| 601 | 4 | Value* vx1 = kb.vload_f32x8(px1); | |
| 602 | 4 | Value* next_vsum1 = kb.vadd(vsum1, vx1); | |
| 603 | 4 | Value* next_iv1_v = kb.builder().build_add(iv1_v, eight_i64); | |
| 604 | 4 | kb.builder().build_br(l1_vhead, {next_iv1_v, next_vsum1}); | |
| 605 | |||
| 606 | // Pass 1: Vector exit -> horizontal sum | ||
| 607 | 4 | fn->append_block(l1_vexit); | |
| 608 | 4 | Value* exit_iv1 = kb.builder().add_block_param(l1_vexit, Type::i64()); | |
| 609 | 4 | Value* exit_vsum1 = kb.builder().add_block_param(l1_vexit, Type::f32x8()); | |
| 610 | 4 | kb.position_at_end(l1_vexit); | |
| 611 | 4 | Value* init_sum1 = reduce_vsum8(kb, y, exit_vsum1); | |
| 612 | 4 | kb.builder().build_br(l1_shead, {exit_iv1, init_sum1}); | |
| 613 | |||
| 614 | // Pass 1: Scalar remainder | ||
| 615 | 4 | fn->append_block(l1_shead); | |
| 616 | 4 | Value* iv1_s = kb.builder().add_block_param(l1_shead, Type::i64()); | |
| 617 | 4 | Value* sum1 = kb.builder().add_block_param(l1_shead, Type::f32()); | |
| 618 | 4 | kb.position_at_end(l1_shead); | |
| 619 | 4 | Value* cond1_s = kb.builder().build_slt(iv1_s, d); | |
| 620 | 4 | kb.builder().build_br_if(cond1_s, l1_sbody, {}, l1_exit, {sum1}); | |
| 621 | |||
| 622 | 4 | fn->append_block(l1_sbody); | |
| 623 | 4 | kb.position_at_end(l1_sbody); | |
| 624 | 4 | Value* xi1 = kb.load_f32_indexed(x, iv1_s, 4, 0); | |
| 625 | 4 | Value* next_sum1 = kb.add(sum1, xi1); | |
| 626 | 4 | Value* next_iv1_s = kb.builder().build_add(iv1_s, one_i64); | |
| 627 | 4 | kb.builder().build_br(l1_shead, {next_iv1_s, next_sum1}); | |
| 628 | |||
| 629 | // Pass 1: Mean computed -> enter Pass 2 | ||
| 630 | 4 | fn->append_block(l1_exit); | |
| 631 | 4 | Value* final_sum1 = kb.builder().add_block_param(l1_exit, Type::f32()); | |
| 632 | 4 | kb.position_at_end(l1_exit); | |
| 633 | 4 | Value* mean = kb.mul(final_sum1, inv_d); | |
| 634 | 4 | Value* vmean = kb.vbroadcast(Type::f32x8(), mean); | |
| 635 | 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 | 4 | fn->append_block(l2_vhead); | |
| 639 | 4 | Value* iv2_v = kb.builder().add_block_param(l2_vhead, Type::i64()); | |
| 640 | 4 | Value* vsum2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 641 | 4 | Value* m2_scalar = kb.builder().add_block_param(l2_vhead, Type::f32()); | |
| 642 | 4 | Value* vm2 = kb.builder().add_block_param(l2_vhead, Type::f32x8()); | |
| 643 | 4 | kb.position_at_end(l2_vhead); | |
| 644 | 4 | Value* cond2_v = kb.builder().build_slt(iv2_v, vec_d); | |
| 645 | 4 | kb.builder().build_br_if(cond2_v, l2_vbody, {}, l2_vexit, {iv2_v, vsum2, m2_scalar}); | |
| 646 | |||
| 647 | 4 | fn->append_block(l2_vbody); | |
| 648 | 4 | kb.position_at_end(l2_vbody); | |
| 649 | 4 | Value* byte_off2 = kb.builder().build_shl(iv2_v, two_i64); | |
| 650 | 4 | Value* px2 = kb.builder().build_add(x, byte_off2); | |
| 651 | 4 | Value* vx2 = kb.vload_f32x8(px2); | |
| 652 | 4 | Value* vdev = kb.vsub(vx2, vm2); | |
| 653 | 4 | Value* next_vsum2 = kb.vfma(vdev, vdev, vsum2); | |
| 654 | 4 | Value* next_iv2_v = kb.builder().build_add(iv2_v, eight_i64); | |
| 655 | 4 | kb.builder().build_br(l2_vhead, {next_iv2_v, next_vsum2, m2_scalar, vm2}); | |
| 656 | |||
| 657 | // Pass 2: Vector exit -> horizontal sum | ||
| 658 | 4 | fn->append_block(l2_vexit); | |
| 659 | 4 | Value* exit_iv2 = kb.builder().add_block_param(l2_vexit, Type::i64()); | |
| 660 | 4 | Value* exit_vsum2 = kb.builder().add_block_param(l2_vexit, Type::f32x8()); | |
| 661 | 4 | Value* m2_out = kb.builder().add_block_param(l2_vexit, Type::f32()); | |
| 662 | 4 | kb.position_at_end(l2_vexit); | |
| 663 | 4 | Value* init_sumsq = reduce_vsum8(kb, y, exit_vsum2); | |
| 664 | 4 | kb.builder().build_br(l2_shead, {exit_iv2, init_sumsq, m2_out}); | |
| 665 | |||
| 666 | // Pass 2: Scalar remainder | ||
| 667 | 4 | fn->append_block(l2_shead); | |
| 668 | 4 | Value* iv2_s = kb.builder().add_block_param(l2_shead, Type::i64()); | |
| 669 | 4 | Value* sumsq = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 670 | 4 | Value* m2_s = kb.builder().add_block_param(l2_shead, Type::f32()); | |
| 671 | 4 | kb.position_at_end(l2_shead); | |
| 672 | 4 | Value* cond2_s = kb.builder().build_slt(iv2_s, d); | |
| 673 | 4 | kb.builder().build_br_if(cond2_s, l2_sbody, {}, l2_exit, {sumsq, m2_s}); | |
| 674 | |||
| 675 | 4 | fn->append_block(l2_sbody); | |
| 676 | 4 | kb.position_at_end(l2_sbody); | |
| 677 | 4 | Value* xi2 = kb.load_f32_indexed(x, iv2_s, 4, 0); | |
| 678 | 4 | Value* diff2 = kb.sub(xi2, m2_s); | |
| 679 | 4 | Value* diff_sq2 = kb.mul(diff2, diff2); | |
| 680 | 4 | Value* next_sumsq = kb.add(sumsq, diff_sq2); | |
| 681 | 4 | Value* next_iv2_s = kb.builder().build_add(iv2_s, one_i64); | |
| 682 | 4 | kb.builder().build_br(l2_shead, {next_iv2_s, next_sumsq, m2_s}); | |
| 683 | |||
| 684 | // Pass 2 exit -> compute rstd | ||
| 685 | 4 | fn->append_block(l2_exit); | |
| 686 | 4 | Value* final_sumsq = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 687 | 4 | Value* final_mean = kb.builder().add_block_param(l2_exit, Type::f32()); | |
| 688 | 4 | kb.position_at_end(l2_exit); | |
| 689 | 4 | Value* var = kb.mul(final_sumsq, inv_d); | |
| 690 | 4 | Value* var_eps = kb.add(var, eps); | |
| 691 | 4 | Value* rstd = kb.div(kb.builder().build_fconst_f32(1.0f), kb.builder().build_sqrt_f32(var_eps)); | |
| 692 | 4 | Value* vmean3 = kb.vbroadcast(Type::f32x8(), final_mean); | |
| 693 | 4 | Value* vrstd3 = kb.vbroadcast(Type::f32x8(), rstd); | |
| 694 | 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 | 4 | fn->append_block(l3_vhead); | |
| 698 | 4 | Value* iv3_v = kb.builder().add_block_param(l3_vhead, Type::i64()); | |
| 699 | 4 | Value* m3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 700 | 4 | Value* r3_s = kb.builder().add_block_param(l3_vhead, Type::f32()); | |
| 701 | 4 | Value* vm3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 702 | 4 | Value* vr3 = kb.builder().add_block_param(l3_vhead, Type::f32x8()); | |
| 703 | 4 | kb.position_at_end(l3_vhead); | |
| 704 | 4 | Value* cond3_v = kb.builder().build_slt(iv3_v, vec_d); | |
| 705 | 4 | kb.builder().build_br_if(cond3_v, l3_vbody, {}, l3_shead, {iv3_v, m3_s, r3_s}); | |
| 706 | |||
| 707 | 4 | fn->append_block(l3_vbody); | |
| 708 | 4 | kb.position_at_end(l3_vbody); | |
| 709 | 4 | Value* byte_off3 = kb.builder().build_shl(iv3_v, two_i64); | |
| 710 | 4 | Value* px3 = kb.builder().build_add(x, byte_off3); | |
| 711 | 4 | Value* pg3 = kb.builder().build_add(gamma, byte_off3); | |
| 712 | 4 | Value* pb3 = kb.builder().build_add(beta, byte_off3); | |
| 713 | 4 | Value* pscale3 = kb.builder().build_add(scale, byte_off3); | |
| 714 | 4 | Value* pshift3 = kb.builder().build_add(shift, byte_off3); | |
| 715 | 4 | Value* py3 = kb.builder().build_add(y, byte_off3); | |
| 716 | 4 | Value* vx3 = kb.vload_f32x8(px3); | |
| 717 | 4 | Value* vg3 = kb.vload_f32x8(pg3); | |
| 718 | 4 | Value* vb3 = kb.vload_f32x8(pb3); | |
| 719 | 4 | Value* vscale3 = kb.vload_f32x8(pscale3); | |
| 720 | 4 | Value* vshift3 = kb.vload_f32x8(pshift3); | |
| 721 | 4 | Value* vdiff3 = kb.vsub(vx3, vm3); | |
| 722 | 4 | Value* vxhat3 = kb.vmul(vdiff3, vr3); | |
| 723 | 4 | Value* v_ln3 = kb.vfma(vg3, vxhat3, vb3); // LayerNorm in registers | |
| 724 | 4 | Value* v_1_plus_scale = kb.vadd(vone, vscale3); | |
| 725 | 4 | Value* vy3 = kb.vfma(v_ln3, v_1_plus_scale, vshift3); // Modulated in registers | |
| 726 | 4 | kb.vstore_f32x8(py3, vy3); // Stored directly to destination! | |
| 727 | 4 | Value* next_iv3_v = kb.builder().build_add(iv3_v, eight_i64); | |
| 728 | 4 | kb.builder().build_br(l3_vhead, {next_iv3_v, m3_s, r3_s, vm3, vr3}); | |
| 729 | |||
| 730 | // Pass 3: Scalar remainder | ||
| 731 | 4 | fn->append_block(l3_shead); | |
| 732 | 4 | Value* iv3_s = kb.builder().add_block_param(l3_shead, Type::i64()); | |
| 733 | 4 | Value* m3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 734 | 4 | Value* r3_sc = kb.builder().add_block_param(l3_shead, Type::f32()); | |
| 735 | 4 | kb.position_at_end(l3_shead); | |
| 736 | 4 | Value* cond3_s = kb.builder().build_slt(iv3_s, d); | |
| 737 | 4 | kb.builder().build_br_if(cond3_s, l3_sbody, l3_exit); | |
| 738 | |||
| 739 | 4 | fn->append_block(l3_sbody); | |
| 740 | 4 | kb.position_at_end(l3_sbody); | |
| 741 | 4 | Value* x3 = kb.load_f32_indexed(x, iv3_s, 4, 0); | |
| 742 | 4 | Value* g3 = kb.load_f32_indexed(gamma, iv3_s, 4, 0); | |
| 743 | 4 | Value* b3 = kb.load_f32_indexed(beta, iv3_s, 4, 0); | |
| 744 | 4 | Value* s3 = kb.load_f32_indexed(scale, iv3_s, 4, 0); | |
| 745 | 4 | Value* sh3 = kb.load_f32_indexed(shift, iv3_s, 4, 0); | |
| 746 | 4 | Value* diff3 = kb.sub(x3, m3_sc); | |
| 747 | 4 | Value* xhat3 = kb.mul(diff3, r3_sc); | |
| 748 | 4 | Value* ln_val3 = kb.builder().build_fma_f32(g3, xhat3, b3); | |
| 749 | 4 | Value* one_plus_s3 = kb.add(one_f32, s3); | |
| 750 | 4 | Value* y3 = kb.builder().build_fma_f32(ln_val3, one_plus_s3, sh3); | |
| 751 | 4 | kb.store_f32_indexed(y, iv3_s, y3, 4, 0); | |
| 752 | 4 | Value* next_iv3_s = kb.builder().build_add(iv3_s, one_i64); | |
| 753 | 4 | kb.builder().build_br(l3_shead, {next_iv3_s, m3_sc, r3_sc}); | |
| 754 | |||
| 755 | 4 | fn->append_block(l3_exit); | |
| 756 | 4 | kb.position_at_end(l3_exit); | |
| 757 | 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 | 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 | 7 | if (!s_fused_jit_ready.load(std::memory_order_relaxed)) { | |
| 771 | try { | ||
| 772 | 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 | 14 | cache.compile_or_load_cpu_fn<JitFusedKernels::FusedResidualRmsNormRowFn>( | |
| 777 | 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 | 14 | cache.compile_or_load_cpu_fn<JitFusedKernels::FusedResidualLayerNormRowFn>( | |
| 782 | 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 | 14 | cache.compile_or_load_cpu_fn<JitFusedKernels::FusedLayerNormModulateRowFn>( | |
| 787 | 7 | "cpu_fused_layernorm_modulate_row_v1", "fused_layernorm_modulate_row", | |
| 788 | populate_fused_layernorm_modulate_row, opts); | ||
| 789 | |||
| 790 | 14 | s_fused_kernels.available = (s_fused_kernels.fused_residual_rmsnorm_row_fn != nullptr && | |
| 791 | 7 | s_fused_kernels.fused_residual_layernorm_row_fn != nullptr && | |
| 792 | 7 | s_fused_kernels.fused_layernorm_modulate_row_fn != nullptr); | |
| 793 | 7 | } catch (const std::exception& e) { | |
| 794 | ✗ | 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 | 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 | 372 | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 824 | ✗ | 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 | 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 | 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 | 7 | if (B > 1 && static_cast<int64_t>(B) * D >= 262144) { | |
| 852 | ✗ | 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 | 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 | 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 | 368 | if (R > 1 && static_cast<int64_t>(R) * D >= 262144) { | |
| 880 | ✗ | 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 | 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 |