Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/MojoLlama/blob/main/src/mojollama/kernels/legacy-c/cengine_batch.c # MojoLlama Archived Mojo/MAX quantizer and serving research repository (read-only history) Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `MojoLlama / src / mojollama / kernels / legacy-c / cengine_batch.c` 543 lines · 24912 bytes Raw Blame History 1 /* cengine_batch_instr.c — instrumented version to find hang location */ 2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #define Q4_0_BS 18 10 #define Q8_0_BS 34 11 #define BLOCK_SIZE 64 12 #define MAX_BLOCKS 1024 13 #define QK_K 256 14 15 /* K-quant block structures (from llama.cpp) */ 16 typedef struct { 17 uint16_t d; 18 uint16_t dm; 19 uint8_t scales[12]; 20 uint8_t qs[128]; 21 } block_q4_K; 22 23 typedef struct { 24 uint8_t ql[128]; 25 uint8_t qh[64]; 26 int8_t scales[16]; 27 uint16_t d; 28 } block_q6_K; 29 30 static inline float gf16(uint16_t h) { return _cvtsh_ss(h); } 31 32 static inline void k4_scale(int j, const uint8_t *q, uint8_t *d, uint8_t *m) { 33 if (j < 4) { *d = q[j] & 63; *m = q[j+4] & 63; } 34 else { *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4); 35 *m = (q[j+4] >> 4) | ((q[j] >> 6) << 4); } 36 } 37 38 static inline float f16_to_f32(uint16_t h) { return _cvtsh_ss(h); } 39 static inline float hsum_ps(__m256 v) { 40 __m128 l=_mm256_castps256_ps128(v),h=_mm256_extractf128_ps(v,1); 41 l=_mm_add_ps(l,h);l=_mm_hadd_ps(l,l);l=_mm_hadd_ps(l,l);return _mm_cvtss_f32(l); 42 } 43 44 void q4_0_batch_matmul(const uint8_t *W, const float *x, float *out, 45 int n_rows, int nc, int B) { 46 int bpr = nc / 32; 47 #pragma omp parallel for schedule(static, 8) 48 for (int r = 0; r < n_rows; r++) { 49 float *acc = (float*)__builtin_alloca(B * sizeof(float)); 50 for (int b = 0; b < B; b++) acc[b] = 0.0f; 51 for (int blk = 0; blk < bpr; blk++) { 52 const uint8_t *bp = W + ((size_t)r * bpr + blk) * Q4_0_BS; 53 float sc = f16_to_f32((uint16_t)bp[0]|((uint16_t)bp[1]<<8)); 54 __m128i nib = _mm_loadu_si128((const __m128i*)(bp + 2)); 55 __m128i lo = _mm_and_si128(nib, _mm_set1_epi8(0x0F)); 56 __m128i hi = _mm_srli_epi16(nib, 4); 57 hi = _mm_and_si128(hi, _mm_set1_epi8(0x0F)); 58 __m256 sv = _mm256_set1_ps(sc); 59 __m128i lo_lo = lo; 60 __m128i lo_hi = _mm_srli_si128(lo, 8); 61 __m256i i32_0 = _mm256_sub_epi32(_mm256_cvtepi8_epi32(lo_lo), _mm256_set1_epi32(8)); 62 __m256i i32_1 = _mm256_sub_epi32(_mm256_cvtepi8_epi32(lo_hi), _mm256_set1_epi32(8)); 63 __m256 blk0 = _mm256_mul_ps(_mm256_cvtepi32_ps(i32_0), sv); 64 __m256 blk1 = _mm256_mul_ps(_mm256_cvtepi32_ps(i32_1), sv); 65 __m128i hi_lo = hi; 66 __m128i hi_hi = _mm_srli_si128(hi, 8); 67 __m256i i32_2 = _mm256_sub_epi32(_mm256_cvtepi8_epi32(hi_lo), _mm256_set1_epi32(8)); 68 __m256i i32_3 = _mm256_sub_epi32(_mm256_cvtepi8_epi32(hi_hi), _mm256_set1_epi32(8)); 69 __m256 blk2 = _mm256_mul_ps(_mm256_cvtepi32_ps(i32_2), sv); 70 __m256 blk3 = _mm256_mul_ps(_mm256_cvtepi32_ps(i32_3), sv); 71 int o = blk * 32; 72 for (int b = 0; b < B; b++) { 73 const float *xb = x + (size_t)b * nc + o; 74 __m256 p0 = _mm256_mul_ps(blk0, _mm256_loadu_ps(xb)); 75 __m256 p1 = _mm256_mul_ps(blk1, _mm256_loadu_ps(xb + 8)); 76 __m256 p2 = _mm256_mul_ps(blk2, _mm256_loadu_ps(xb + 16)); 77 __m256 p3 = _mm256_mul_ps(blk3, _mm256_loadu_ps(xb + 24)); 78 __m256 s01 = _mm256_add_ps(p0, p1); 79 __m256 s23 = _mm256_add_ps(p2, p3); 80 __m256 s = _mm256_add_ps(s01, s23); 81 acc[b] += hsum_ps(s); 82 } 83 } 84 for (int b = 0; b < B; b++) out[(size_t)b * n_rows + r] = acc[b]; 85 } 86 } 87 88 static inline float q4d(const uint8_t *W, const float *x, int nc, int r) { 89 int bp=nc/32; float total=0.0f; 90 for(int b=0;b>4)&0x0F); 97 d += (float)(nib-8)*sc*x[o+j]; 98 } 99 total+=d; 100 } 101 return total; 102 } 103 static inline float q8d(const uint8_t *W, const float *x, int nc, int r) { 104 int bp=nc/32; float t=0; 105 for(int b=0;bmx)mx=dot;} 127 float se=0;for(int s=0;sk = (float*)calloc(1, sz); kv->v = (float*)calloc(1, sz); kv->n_blocks = 0; 142 memset(kv->block_map, -1, sizeof(kv->block_map)); memset(kv->seq_len, 0, sizeof(kv->seq_len)); 143 } 144 int kv_alloc(KVBlock *kv) { return kv->n_blocks < MAX_BLOCKS ? kv->n_blocks++ : -1; } 145 146 void q8_0_batch_matmul(const uint8_t *W, const float *x, float *out, 147 int n_rows, int nc, int B) { 148 int bpr = nc / 32; 149 #pragma omp parallel for schedule(static, 8) 150 for (int r = 0; r < n_rows; r++) { 151 float *acc = (float*)__builtin_alloca(B * sizeof(float)); 152 for (int b = 0; b < B; b++) acc[b] = 0.0f; 153 for (int blk = 0; blk < bpr; blk++) { 154 const uint8_t *bp = W + ((size_t)r * bpr + blk) * Q8_0_BS; 155 float sc = f16_to_f32((uint16_t)bp[0]|((uint16_t)bp[1]<<8)); 156 const int8_t *qs = (const int8_t*)(bp + 2); 157 __m256 sv = _mm256_set1_ps(sc); 158 __m128i q8 = _mm_loadu_si128((const __m128i*)(qs)); 159 __m128i q8b = _mm_loadu_si128((const __m128i*)(qs + 16)); 160 __m128i q8_low = q8; 161 __m128i q8_high = _mm_srli_si128(q8, 8); 162 __m128i q8b_low = q8b; 163 __m128i q8b_high = _mm_srli_si128(q8b, 8); 164 __m256 blk0 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(q8_low)), sv); 165 __m256 blk1 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(q8_high)), sv); 166 __m256 blk2 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(q8b_low)), sv); 167 __m256 blk3 = _mm256_mul_ps(_mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(q8b_high)), sv); 168 int o = blk * 32; 169 for (int b = 0; b < B; b++) { 170 const float *xb = x + (size_t)b * nc + o; 171 __m256 p0 = _mm256_mul_ps(blk0, _mm256_loadu_ps(xb)); 172 __m256 p1 = _mm256_mul_ps(blk1, _mm256_loadu_ps(xb + 8)); 173 __m256 p2 = _mm256_mul_ps(blk2, _mm256_loadu_ps(xb + 16)); 174 __m256 p3 = _mm256_mul_ps(blk3, _mm256_loadu_ps(xb + 24)); 175 __m256 s01 = _mm256_add_ps(p0, p1); 176 __m256 s23 = _mm256_add_ps(p2, p3); 177 __m256 s = _mm256_add_ps(s01, s23); 178 acc[b] += hsum_ps(s); 179 } 180 } 181 for (int b = 0; b < B; b++) out[(size_t)b * n_rows + r] = acc[b]; 182 } 183 } 184 185 /* Batched Q4_K matmul: 256-element super-blocks, 4-bit K-quant (AVX2) */ 186 void q4_k_batch_matmul(const uint8_t *W, const float *x, float *out, 187 int n_rows, int nc, int B) { 188 int bpr = nc / QK_K; 189 #pragma omp parallel for schedule(static, 8) 190 for (int r = 0; r < n_rows; r++) { 191 float *acc = (float*)__builtin_alloca(B * sizeof(float)); 192 for (int b = 0; b < B; b++) acc[b] = 0.0f; 193 for (int blk = 0; blk < bpr; blk++) { 194 const block_q4_K *bp = (const block_q4_K*)(W + ((size_t)r * bpr + blk) * sizeof(block_q4_K)); 195 float d = gf16(bp->d), dm = gf16(bp->dm); 196 const uint8_t *q = bp->qs; 197 int is = 0; 198 float deq[QK_K]; 199 for (int s = 0; s < QK_K; s += 64, q += 32, is += 2) { 200 uint8_t sc1, sc2, m1, m2; 201 k4_scale(is, bp->scales, &sc1, &m1); 202 float d1 = d * sc1, mm1 = dm * m1; 203 k4_scale(is+1, bp->scales, &sc2, &m2); 204 float d2 = d * sc2, mm2 = dm * m2; 205 __m128i qb = _mm_loadu_si128((const __m128i*)q); 206 __m128i qb2 = _mm_loadu_si128((const __m128i*)(q+16)); 207 __m128i lo1 = _mm_and_si128(qb, _mm_set1_epi8(0x0F)); 208 __m128i lo2 = _mm_and_si128(qb2, _mm_set1_epi8(0x0F)); 209 __m128i hi1 = _mm_and_si128(_mm_srli_epi16(qb, 4), _mm_set1_epi8(0x0F)); 210 __m128i hi2 = _mm_and_si128(_mm_srli_epi16(qb2, 4), _mm_set1_epi8(0x0F)); 211 __m256 d1v = _mm256_set1_ps(d1), mm1v = _mm256_set1_ps(mm1); 212 __m256 d2v = _mm256_set1_ps(d2), mm2v = _mm256_set1_ps(mm2); 213 for (int k = 0; k < 4; k++) { 214 __m128i src = (k < 2) ? lo1 : lo2; 215 __m128i src_split = (k & 1) ? _mm_srli_si128(src, 8) : src; 216 int out_off = s + k * 8; 217 _mm256_storeu_ps(deq + out_off, _mm256_sub_ps( 218 _mm256_mul_ps(_mm256_cvtepi32_ps( 219 _mm256_cvtepi8_epi32(src_split)), d1v), mm1v)); 220 } 221 for (int k = 0; k < 4; k++) { 222 __m128i src = (k < 2) ? hi1 : hi2; 223 __m128i src_split = (k & 1) ? _mm_srli_si128(src, 8) : src; 224 int out_off = s + 32 + k * 8; 225 _mm256_storeu_ps(deq + out_off, _mm256_sub_ps( 226 _mm256_mul_ps(_mm256_cvtepi32_ps( 227 _mm256_cvtepi8_epi32(src_split)), d2v), mm2v)); 228 } 229 } 230 int o = blk * QK_K; 231 for (int b = 0; b < B; b++) { 232 const float *xb = x + (size_t)b * nc + o; 233 __m256 sum = _mm256_setzero_ps(); 234 for (int j = 0; j < QK_K; j += 8) { 235 sum = _mm256_fmadd_ps(_mm256_loadu_ps(deq + j), 236 _mm256_loadu_ps(xb + j), sum); 237 } 238 sum = _mm256_and_ps(sum, _mm256_cmp_ps(sum, sum, _CMP_EQ_OQ)); 239 acc[b] += hsum_ps(sum); 240 } 241 } 242 for (int b = 0; b < B; b++) out[(size_t)b * n_rows + r] = acc[b]; 243 } 244 } 245 246 /* AVX2 Batched Q6_K matmul: 256-element blocks, 6-bit K-quant */ 247 void q6_k_batch_matmul(const uint8_t *W, const float *x, float *out, 248 int n_rows, int nc, int B) { 249 int bpr = nc / QK_K; 250 #pragma omp parallel for schedule(static, 8) 251 for (int r = 0; r < n_rows; r++) { 252 float *acc = (float*)__builtin_alloca(B * sizeof(float)); 253 for (int b = 0; b < B; b++) acc[b] = 0.0f; 254 for (int blk = 0; blk < bpr; blk++) { 255 const block_q6_K *bp = (const block_q6_K*)(W + ((size_t)r * bpr + blk) * sizeof(block_q6_K)); 256 float d = gf16(bp->d); 257 float deq[QK_K]; 258 for (int j = 0; j < QK_K; j++) { 259 uint8_t ql = bp->ql[j/2]; 260 uint8_t qh = bp->qh[j/4]; 261 int s = (j & 1) ? (ql >> 4) : (ql & 0xF); 262 int sh = (qh >> ((j & 3) * 2)) & 3; 263 s |= (sh << 4); s -= 32; 264 deq[j] = d * bp->scales[j/16] * s; 265 } 266 int o = blk * QK_K; 267 for (int b = 0; b < B; b++) { 268 const float *xb = x + (size_t)b * nc + o; 269 __m256 sum = _mm256_setzero_ps(); 270 for (int j = 0; j < QK_K; j += 8) 271 sum = _mm256_fmadd_ps(_mm256_loadu_ps(deq + j), 272 _mm256_loadu_ps(xb + j), sum); 273 sum = _mm256_and_ps(sum, _mm256_cmp_ps(sum, sum, _CMP_EQ_OQ)); 274 acc[b] += hsum_ps(sum); 275 } 276 } 277 for (int b = 0; b < B; b++) out[(size_t)b * n_rows + r] = acc[b]; 278 } 279 } 280 typedef struct { 281 int L,N,NH,NKH,HD,FF,V; float eps; 282 const uint8_t **wQ,**wK,**wV,**wO,**wG,**wU,**wD; 283 const float **wAN,**wFN; const int *nQ,*nK,*nV,*nO,*nG,*nU,*nD; 284 int nc; const float *emb,*onw; const uint8_t *wOut; int outNR,outNC,outQuant; 285 KVBlock **kv_array; float *logits; 286 int n_experts, n_experts_per_tok, moe_intermediate; 287 const float **w_gate_inp; 288 const uint8_t **w_gate_exps; 289 const uint8_t **w_up_exps; 290 const uint8_t **w_down_exps; 291 int gate_exp_quant, up_exp_quant, down_exp_quant; 292 const int *q_quant, *k_quant, *v_quant, *o_quant; 293 const int *g_quant, *u_quant, *d_quant; 294 int emb_quant; 295 const uint8_t **wQK; /* fused Q+K weight pointer (NULL if no fusion) */ 296 const int *qk_quant; /* quant type for fused Q+K weight */ 297 float *cos_table; 298 float *sin_table; 299 int max_ctx; 300 } BC; 301 302 void moe_ffn(const BC *c, int l, float *x, float *gate_buf, float *up_buf, float *ffn_buf, int B) { 303 int N = c->N, NE = c->n_experts, NK = c->n_experts_per_tok, M = c->moe_intermediate; 304 size_t gate_stride = (size_t)M * (N / QK_K) * sizeof(block_q4_K); 305 size_t up_stride = (size_t)M * (N / QK_K) * sizeof(block_q4_K); 306 size_t down_stride = (size_t)N * (M / QK_K) * sizeof(block_q6_K); 307 if (c->gate_exp_quant == 14) gate_stride = (size_t)M * (N / QK_K) * sizeof(block_q6_K); 308 if (c->up_exp_quant == 14) up_stride = (size_t)M * (N / QK_K) * sizeof(block_q6_K); 309 if (c->down_exp_quant == 12) down_stride = (size_t)N * (M / QK_K) * sizeof(block_q4_K); 310 float router_logits[256]; 311 int top_idx[16]; float top_val[16]; 312 const float *w_router = c->w_gate_inp[l]; 313 314 for (int b = 0; b < B; b++) { 315 float *xb = x + b*N; 316 317 // Router 318 for (int e = 0; e < NE; e++) { 319 float dot = 0; 320 for (int i = 0; i < N; i++) dot += xb[i] * w_router[e * N + i]; 321 router_logits[e] = dot; 322 } 323 324 // Softmax 325 float mx = router_logits[0]; 326 for (int e = 1; e < NE; e++) if (router_logits[e] > mx) mx = router_logits[e]; 327 float sum = 0; 328 for (int e = 0; e < NE; e++) { router_logits[e] = expf(router_logits[e] - mx); sum += router_logits[e]; } 329 float inv_sum = 1.0f / (sum + 1e-10f); 330 for (int e = 0; e < NE; e++) router_logits[e] *= inv_sum; 331 332 // Top-K 333 for (int k = 0; k < NK; k++) { top_val[k] = -1e30f; top_idx[k] = -1; } 334 for (int e = 0; e < NE; e++) { 335 float v = router_logits[e]; 336 for (int k = 0; k < NK; k++) { 337 if (v > top_val[k]) { 338 for (int k2 = NK-1; k2 > k; k2--) { top_val[k2] = top_val[k2-1]; top_idx[k2] = top_idx[k2-1]; } 339 top_val[k] = v; top_idx[k] = e; break; 340 } 341 } 342 } 343 344 // Sparse expert FFN 345 memset(ffn_buf, 0, N * sizeof(float)); 346 for (int k = 0; k < NK; k++) { 347 int e = top_idx[k]; 348 if (e < 0) continue; 349 float weight = top_val[k]; 350 351 const uint8_t *gate_w = c->w_gate_exps[l] + (size_t)e * gate_stride; 352 if (c->gate_exp_quant == 12) 353 q4_k_batch_matmul(gate_w, xb, gate_buf, M, N, 1); 354 else if (c->gate_exp_quant == 2) 355 q4_0_batch_matmul(gate_w, xb, gate_buf, M, N, 1); 356 357 const uint8_t *up_w = c->w_up_exps[l] + (size_t)e * up_stride; 358 if (c->up_exp_quant == 12) 359 q4_k_batch_matmul(up_w, xb, up_buf, M, N, 1); 360 else if (c->up_exp_quant == 2) 361 q4_0_batch_matmul(up_w, xb, up_buf, M, N, 1); 362 363 for (int i = 0; i < M; i++) { 364 float g = gate_buf[i]; 365 float sg = g / (1.0f + expf(-g)); 366 gate_buf[i] = sg * up_buf[i]; 367 } 368 369 const uint8_t *down_w = c->w_down_exps[l] + (size_t)e * down_stride; 370 if (c->down_exp_quant == 14) 371 q6_k_batch_matmul(down_w, gate_buf, up_buf, N, M, 1); 372 else if (c->down_exp_quant == 2) 373 q4_0_batch_matmul(down_w, gate_buf, up_buf, N, M, 1); 374 375 for (int i = 0; i < N; i++) ffn_buf[i] += weight * up_buf[i]; 376 } 377 } 378 } 379 380 static inline void batch_matmul(int qt, const uint8_t *W, const float *x, float *out, 381 int n_rows, int nc, int B) { 382 if (qt == 12) q4_k_batch_matmul(W, x, out, n_rows, nc, B); 383 else if (qt == 14) q6_k_batch_matmul(W, x, out, n_rows, nc, B); 384 else if (qt == 8) q8_0_batch_matmul(W, x, out, n_rows, nc, B); 385 else q4_0_batch_matmul(W, x, out, n_rows, nc, B); 386 } 387 388 void rope_init(float *cos_table, float *sin_table, int max_ctx, int hd) { 389 int hd2 = hd / 2; 390 for (int pos = 0; pos < max_ctx; pos++) { 391 for (int j = 0; j < hd2; j++) { 392 double theta = (double)pos / pow(10000.0, 2.0 * j / hd); 393 cos_table[pos * hd2 + j] = (float)cos(theta); 394 sin_table[pos * hd2 + j] = (float)sin(theta); 395 } 396 } 397 } 398 399 static inline void rope_apply(float *buf, int nh, int hd, int pos, 400 const float *cos_t, const float *sin_t) { 401 int hd2 = hd / 2; 402 const float *cos_row = cos_t + (size_t)pos * hd2; 403 const float *sin_row = sin_t + (size_t)pos * hd2; 404 for (int h = 0; h < nh; h++) { 405 float *b = buf + (size_t)h * hd; 406 int j = 0; 407 for (; j <= hd2 - 8; j += 8) { 408 __m256 v0 = _mm256_loadu_ps(b + j); 409 __m256 v1 = _mm256_loadu_ps(b + j + hd2); 410 __m256 cv = _mm256_loadu_ps(cos_row + j); 411 __m256 sv = _mm256_loadu_ps(sin_row + j); 412 __m256 nv0 = _mm256_sub_ps(_mm256_mul_ps(v0, cv), _mm256_mul_ps(v1, sv)); 413 __m256 nv1 = _mm256_add_ps(_mm256_mul_ps(v0, sv), _mm256_mul_ps(v1, cv)); 414 _mm256_storeu_ps(b + j, nv0); 415 _mm256_storeu_ps(b + j + hd2, nv1); 416 } 417 for (; j < hd2; j++) { 418 float a0 = b[j], a1 = b[j + hd2]; 419 float cs = cos_row[j], sn = sin_row[j]; 420 b[j] = a0 * cs - a1 * sn; 421 b[j + hd2] = a1 * cs + a0 * sn; 422 } 423 } 424 } 425 426 void emb_lookup(const uint8_t *emb, int token, float *out, int N, int quant) { 427 if (quant == 12) { 428 int bpr = N / QK_K, blk_size = sizeof(block_q4_K); 429 for (int bi = 0; bi < bpr; bi++) { 430 const block_q4_K *bp = (const block_q4_K*)(emb + ((size_t)token * bpr + bi) * blk_size); 431 float d = gf16(bp->d), dm = gf16(bp->dm); 432 int o = bi * QK_K; 433 for (int g = 0; g < 8; g++) { 434 uint8_t sc, mn; k4_scale(g, bp->scales, &sc, &mn); 435 float d_sc = d * ((int)sc - 32) / 8.0f; 436 float d_mn = dm * ((int)mn - 32) / 8.0f; 437 for (int j = g*32; j < (g+1)*32; j++) { 438 int s = (bp->qs[j/2] >> ((j & 1) * 4)) & 0xF; 439 out[o + j] = d_sc * (s - 8) - d_mn * 6.0f; 440 } 441 } 442 } 443 } else { 444 memcpy(out, emb + (size_t)token * N * sizeof(float), N * sizeof(float)); 445 } 446 } 447 448 void batch_forward(const BC *c, const int *tokens, int B, float *ws) { 449 int N=c->N, NH=c->NH, NKH=c->NKH, HD=c->HD, FF=c->FF, L=c->L, nc=c->nc; 450 int S = N; if (NH*HD > S) S = NH*HD; if (FF > S) S = FF; if (NKH*HD > S) S = NKH*HD; 451 int qk_fused = NH*HD + NKH*HD; 452 if (qk_fused > S) S = qk_fused; 453 454 float *x = ws, *xn = ws + B*S, *res = ws + 2*B*S; 455 float *q = ws + 3*B*S, *k = ws + 4*B*S, *v = ws + 5*B*S; 456 float *att = ws + 6*B*S, *gate = ws + 7*B*S, *up = ws + 8*B*S; 457 float *silu = ws + 9*B*S, *oproj = ws + 10*B*S, *ffn = ws + 11*B*S; 458 459 for (int b = 0; b < B; b++) { 460 if (c->emb_quant == 12) 461 emb_lookup((const uint8_t*)c->emb, tokens[b], x + b*N, N, 12); 462 else 463 memcpy(x + b*N, c->emb + (size_t)tokens[b] * N, N * sizeof(float)); 464 } 465 466 for (int l = 0; l < L; l++) { 467 468 for (int b = 0; b < B; b++) { 469 memcpy(res + b*N, x + b*N, N * sizeof(float)); 470 rms(xn + b*N, x + b*N, c->wAN[l], N, c->eps); 471 } 472 473 /* Fused Q+K matmul when available (types match, weight built), else 3 separate */ 474 int use_fused_qk = (c->wQK && c->wQK[l] != NULL); 475 if (use_fused_qk) { 476 int nqk = c->nQ[l] + c->nK[l]; 477 batch_matmul(c->qk_quant[l], c->wQK[l], xn, q, nqk, nc, B); 478 for (int b = 0; b < B; b++) { 479 memcpy(k + (size_t)b * c->nK[l], 480 q + (size_t)b * nqk + c->nQ[l], 481 c->nK[l] * sizeof(float)); 482 } 483 batch_matmul(c->v_quant[l], c->wV[l], xn, v, c->nV[l], nc, B); 484 } else { 485 batch_matmul(c->q_quant[l], c->wQ[l], xn, q, c->nQ[l], nc, B); 486 batch_matmul(c->k_quant[l], c->wK[l], xn, k, c->nK[l], nc, B); 487 batch_matmul(c->v_quant[l], c->wV[l], xn, v, c->nV[l], nc, B); 488 } 489 490 for (int b = 0; b < B; b++) { 491 float *qb = q + b*c->nQ[l], *kb = k + b*c->nK[l], *vb = v + b*c->nV[l]; 492 KVBlock *kv = c->kv_array[b]; 493 int pos = kv->seq_len[0]; 494 rope_apply(qb, NH, HD, pos, c->cos_table, c->sin_table); 495 rope_apply(kb, NKH, HD, pos, c->cos_table, c->sin_table); 496 497 size_t base=(size_t)l*MAX_BLOCKS*BLOCK_SIZE*NKH*HD; 498 int blk=pos/BLOCK_SIZE,off=pos%BLOCK_SIZE; 499 int bid=kv->block_map[0][blk]; 500 if(bid<0){bid=kv_alloc(kv);kv->block_map[0][blk]=bid;} 501 memcpy(kv->k+base+(size_t)bid*BLOCK_SIZE*NKH*HD+off*NKH*HD, kb, NKH*HD*sizeof(float)); 502 memcpy(kv->v+base+(size_t)bid*BLOCK_SIZE*NKH*HD+off*NKH*HD, vb, NKH*HD*sizeof(float)); 503 504 int sl = pos + 1; 505 float *kct=(float*)__builtin_alloca(sl*NKH*HD*sizeof(float)); 506 float *vct=(float*)__builtin_alloca(sl*NKH*HD*sizeof(float)); 507 for(int s=0;sblock_map[0][sb]; 509 size_t src=base+(size_t)sbid*BLOCK_SIZE*NKH*HD+(size_t)so*NKH*HD; 510 memcpy(kct+s*NKH*HD, kv->k+src, NKH*HD*sizeof(float)); 511 memcpy(vct+s*NKH*HD, kv->v+src, NKH*HD*sizeof(float)); 512 } 513 gqa(att+b*N, qb, kct, vct, sl, NH, NKH, HD); 514 } 515 516 batch_matmul(c->o_quant[l], c->wO[l], att, oproj, c->nO[l], c->NH * c->HD, B); 517 for(int b=0;bwFN[l], N, c->eps); 522 } 523 524 if (c->n_experts > 0 && c->n_experts_per_tok > 0) { 525 for(int b=0;bg_quant[l], c->wG[l], xn, gate, c->nG[l], nc, B); 533 batch_matmul(c->u_quant[l], c->wU[l], xn, up, c->nU[l], nc, B); 534 for(int b=0;bd_quant[l], c->wD[l], silu, ffn, c->nD[l], FF, B); 537 for(int b=0;bkv_array[b]->seq_len[0]++; 541 for(int b=0;bonw, N, c->eps); 542 batch_matmul(c->outQuant, c->wOut, xn, c->logits, c->outNR, c->outNC, B); 543 }