interplane.Get Omega
Read in your language

MojoLlama

Archived Mojo/MAX quantizer and serving research repository (read-only history)

Brought by drakestapleton

branch main Branches Tags History

MojoLlama / src / mojollama / kernels / zaya_batch_forward.c

120 lines · 6354 bytes Raw Blame History
1/* ═════════════════════════════════════════════════════════════════════════
2 * ZAYA batch_forward — interleaved attention/MoE, CCA, res_scales
3 */
4void zaya_batch_forward(const BC *c, const int *tokens, int B, float *ws) {
5 int N=c->N, NH=c->NH, NKH=c->NKH, HD=c->HD, L=c->L;
6 int nq=NH*HD, nk=NKH*HD;
7 int S = nq > N ? nq : N; if (nk > S) S = nk;
8 if (S < 8192) S = 8192;
9
10 float *x = ws, *xn = ws + 4*S, *res = ws + 8*S;
11 float *q = ws + 12*S, *k_ = ws + 16*S;
12 float *att = ws + 20*S, *gate_buf = ws + 24*S, *up_buf = ws + 28*S;
13 float *oproj = ws + 32*S, *ffn = ws + 36*S;
14 float *h_buf = ws + 40*S;
15 int moe_ff = c->zaya_moe_intermediate;
16
17 for (int b = 0; b < B; b++)
18 memcpy(x + b*N, c->emb + (size_t)tokens[b]*N, N*sizeof(float));
19
20 for (int l = 0; l < L; l++) {
21 int is_moe = c->has_moe_layer ? c->has_moe_layer[l] : (l % 2);
22 for (int b = 0; b < B; b++) {
23 float *xb = x + b*N, *rb = res + b*N;
24 float ss = 0;
25 for (int i = 0; i <= N-8; i+=8) {
26 __m256 xv = _mm256_loadu_ps(xb+i);
27 xv = _mm256_min_ps(_mm256_max_ps(xv, _mm256_set1_ps(-1000.0f)), _mm256_set1_ps(1000.0f));
28 _mm256_storeu_ps(xb+i, xv); _mm256_storeu_ps(rb+i, xv);
29 ss += hsum_ps(_mm256_mul_ps(xv, xv));
30 }
31 for (int i = N-(N%8); i < N; i++) {
32 float v = xb[i]; if(v!=v)v=0;else if(v>1000)v=1000;else if(v<-1000)v=-1000;
33 xb[i]=v; rb[i]=v; ss += v*v;
34 }
35 float rms_val = sqrtf(ss/N + c->eps);
36 for (int j = 0; j < N; j++) xb[j] = (xb[j]/rms_val) * c->wAN[l][j];
37 }
38
39 if (!is_moe) {
40 /* attention */
41 batch_matmul(c->q_quant[l], c->wQ[l], xn, q, nq, N, B);
42 batch_matmul(c->k_quant[l], c->wK[l], xn, k_, nk, N, B);
43
44 for (int b = 0; b < B; b++) {
45 float *qb = q + b*nq, *kb = k_ + b*nk;
46 rope_apply(qb, NH, HD, c->max_ctx, c->cos_table, c->sin_table, c->rope_dim);
47 rope_apply(kb, NKH, HD, c->max_ctx, c->cos_table, c->sin_table, c->rope_dim);
48 KVBlock *kv = c->kv_array[l];
49 int pos = kv->pos++;
50 memcpy(kv->k + (size_t)pos * nk, kb, nk * sizeof(float));
51 memcpy(kv->v + (size_t)pos * nk, kb, nk * sizeof(float));
52 int sl = pos + 1;
53 gqa(att + b*nq, qb, kv->k, kv->v, NH, NKH, HD, sl);
54 batch_matmul(c->o_quant[l], c->wO[l], att + b*nq, oproj + b*N, N, nq, 1);
55 float *xbo = x + b*N;
56 for (int i = 0; i < N; i++)
57 xbo[i] = rb[i]*c->w_zaya_res_res_w[l][i] + c->w_zaya_res_res_b[l][i] +
58 oproj[b*N+i]*c->w_zaya_res_hs_w[l][i] + c->w_zaya_res_hs_b[l][i];
59 }
60 } else {
61 /* MoE */
62 int rn = 256;
63 for (int b = 0; b < B; b++) {
64 float *xb = xn + b*N;
65 int ne = c->zaya_expert_n;
66 float h[256], scores[32];
67 batch_matmul(c->zaya_ffn_gate_inp_quant, c->w_zaya_ffn_gate_inp[l], xb, h, rn, N, 1);
68 batch_matmul(c->zaya_ffn_gate_quant, c->w_zaya_ffn_gate[l], h, h_buf, rn, rn, 1);
69 for (int i = 0; i < rn; i++) {
70 float v = h_buf[i];
71 h[i] = 0.5f * v * (1.0f + tanhf(0.7978845608028654f * (v + 0.044715f*v*v*v)));
72 }
73 batch_matmul(c->zaya_mlp2_quant, c->w_zaya_mlp2[l], h, h_buf, rn, rn, 1);
74 batch_matmul(c->zaya_mlp4_quant, c->w_zaya_mlp4[l], h_buf, scores, ne+1, rn, 1);
75 if (c->w_zaya_router_bias[l]) {
76 for (int e = 0; e < ne+1; e++) scores[e] += c->w_zaya_router_bias[l][e];
77 }
78 float mx = scores[0]; for (int e = 1; e < ne+1; e++) if (scores[e] > mx) mx = scores[e];
79 float sum = 0; for (int e = 0; e < ne+1; e++) { scores[e] = expf(scores[e]-mx); sum += scores[e]; }
80 float inv = 1.0f/(sum+1e-10f);
81 for (int e = 0; e < ne+1; e++) scores[e] *= inv;
82 int ec = 0; float ew = scores[0];
83 for (int e = 1; e < ne; e++) { if (scores[e] > ew) { ew = scores[e]; ec = e; } }
84 memset(ffn + b*N, 0, N*sizeof(float));
85 if (ec < ne && ew > 0.01f) {
86 float renorm = 0; for (int e = 0; e < ne; e++) renorm += scores[e];
87 ew /= renorm;
88 int f2 = moe_ff / 2;
89 batch_matmul(c->gate_exp_quant, c->w_gate_exps[l] + (size_t)ec * moe_ff * (N/32)*34,
90 xb, up_buf, moe_ff, N, 1);
91 for (int i = 0; i < f2; i++) {
92 float g = up_buf[i];
93 up_buf[i] = (g / (1.0f + expf(-g))) * up_buf[f2 + i];
94 }
95 batch_matmul(c->down_exp_quant, c->w_down_exps[l] + (size_t)ec * N * (moe_ff/32)*34,
96 up_buf, ffn + b*N, N, f2, 1);
97 for (int i = 0; i < N; i++) ffn[b*N+i] *= ew;
98 }
99 float *xbo = x + b*N;
100 for (int i = 0; i < N; i++)
101 xbo[i] = rb[i]*c->w_zaya_res_res_w[l][i] + c->w_zaya_res_res_b[l][i] +
102 ffn[b*N+i]*c->w_zaya_res_hs_w[l][i] + c->w_zaya_res_hs_b[l][i];
103 }
104 }
105 }
106
107 /* final norm + logits */
108 for (int b = 0; b < B; b++) {
109 float *xb = x + b*N; float ss = 0;
110 for (int i = 0; i <= N-8; i+=8) {
111 __m256 xv = _mm256_loadu_ps(xb+i);
112 xv = _mm256_min_ps(_mm256_max_ps(xv, _mm256_set1_ps(-1000.0f)), _mm256_set1_ps(1000.0f));
113 _mm256_storeu_ps(xb+i, xv); ss += hsum_ps(_mm256_mul_ps(xv, xv));
114 }
115 for (int i = N-(N%8); i < N; i++) { float v=xb[i]; ss += v*v; }
116 float rms_val = sqrtf(ss/N + c->eps);
117 for (int j = 0; j < N; j++) xb[j] = (xb[j]/rms_val) * c->onw[j];
118 batch_matmul(c->outQuant, c->wOut, xb, c->logits + (size_t)b * c->V, c->V, N, 1);
119 }
120}