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/gptoss_batch_forward.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 / gptoss_batch_forward.c` 189 lines · 7402 bytes Raw Blame History 1 /* gptoss_batch_forward.c — Minimal C forward pass for gpt-oss-20b. 2 * Eliminates Python overhead by running the full layer loop in C. 3 * 4 * Compile: 5 * gcc -O3 -march=native -fopenmp -shared -fPIC -o gptoss_batch_forward.so gptoss_batch_forward.c quant_kernels_omp.c gqa_attention.c -lm 6 */ 7 #include 8 #include 9 #include 10 #include 11 #include 12 13 /* Forward declarations from quant_kernels_omp.c */ 14 extern void mxfp4_matmul_omp(const uint8_t *W, const float *x, float *out, int n_rows, int n_cols); 15 extern void moe_forward_omp(const uint8_t** gate_raw, const uint8_t** up_raw, const uint8_t** down_raw, 16 const float* x_norm, int n_ff_expert, int n_embd, 17 int qt_gate, int qt_up, int qt_down, 18 const int* top_indices, const float* top_weights, int top_k, 19 float* combined, float* prealloc_buf, uint8_t* prealloc_q8); 20 extern int quantize_row_q8_0(const float *restrict x, uint8_t *restrict q8, int n_cols); 21 extern void gqa_attention_decode(const float *q, const float *k_cache, const float *v_cache, 22 float *out, int seq_len, int n_head, int n_kv_head, int head_dim, float *workspace); 23 24 #define MAX_EXPERTS 64 25 #define MAX_TOP_K 16 26 27 /* RMS norm */ 28 static void rms_norm(float *out, const float *x, const float *w, int N, float eps) { 29 float ss = 0; 30 for (int i = 0; i < N; i++) ss += x[i] * x[i]; 31 float rms = sqrtf(ss / N + eps); 32 for (int i = 0; i < N; i++) out[i] = (x[i] / rms) * w[i]; 33 } 34 35 /* RoPE apply */ 36 static void rope_apply(float *buf, int nh, int hd, int pos, const float *cos_t, const float *sin_t, int rope_dim) { 37 if (rope_dim <= 0) rope_dim = hd; 38 int hd2 = rope_dim / 2; 39 const float *cos_row = cos_t + (size_t)pos * hd2; 40 const float *sin_row = sin_t + (size_t)pos * hd2; 41 for (int h = 0; h < nh; h++) { 42 float *b = buf + (size_t)h * hd; 43 for (int j = 0; j < hd2; j++) { 44 float a0 = b[j], a1 = b[j + hd2]; 45 float cs = cos_row[j], sn = sin_row[j]; 46 b[j] = a0 * cs - a1 * sn; 47 b[j + hd2] = a1 * cs + a0 * sn; 48 } 49 } 50 } 51 52 /* Router: matmul + softmax + top-k */ 53 static void router_topk(const float *w_router, const float *x_norm, int n_experts, int N, 54 int top_k, int *top_indices, float *top_weights, float *router_buf) { 55 /* Router matmul: w_router is [n_experts, N], x_norm is [N] */ 56 for (int e = 0; e < n_experts; e++) { 57 float dot = 0; 58 const float *wr = w_router + (size_t)e * N; 59 for (int i = 0; i < N; i++) dot += wr[i] * x_norm[i]; 60 router_buf[e] = dot; 61 } 62 /* Softmax */ 63 float mx = router_buf[0]; 64 for (int e = 1; e < n_experts; e++) if (router_buf[e] > mx) mx = router_buf[e]; 65 float sum = 0; 66 for (int e = 0; e < n_experts; e++) { 67 router_buf[e] = expf(router_buf[e] - mx); 68 sum += router_buf[e]; 69 } 70 float inv_sum = 1.0f / (sum + 1e-10f); 71 for (int e = 0; e < n_experts; e++) router_buf[e] *= inv_sum; 72 /* Top-k */ 73 for (int k = 0; k < top_k; k++) { top_indices[k] = -1; top_weights[k] = -1e30f; } 74 for (int e = 0; e < n_experts; e++) { 75 float v = router_buf[e]; 76 for (int k = 0; k < top_k; k++) { 77 if (v > top_weights[k]) { 78 for (int k2 = top_k - 1; k2 > k; k2--) { 79 top_weights[k2] = top_weights[k2-1]; 80 top_indices[k2] = top_indices[k2-1]; 81 } 82 top_weights[k] = v; 83 top_indices[k] = e; 84 break; 85 } 86 } 87 } 88 /* Renormalize top weights */ 89 float tw_sum = 0; 90 for (int k = 0; k < top_k; k++) tw_sum += top_weights[k]; 91 float tw_inv = 1.0f / (tw_sum + 1e-10f); 92 for (int k = 0; k < top_k; k++) top_weights[k] *= tw_inv; 93 } 94 95 /* GPT-OSS forward pass */ 96 void gptoss_forward( 97 const int *token, int pos, 98 const float *emb, int V, 99 const uint8_t **wQ, const uint8_t **wK, const uint8_t **wV, const uint8_t **wO, 100 const float **wAN, const float **wFN, 101 const int *nQ, const int *nK, const int *nV, const int *nO, 102 const int *q_quant, const int *k_quant, const int *v_quant, const int *o_quant, 103 const uint8_t *wOut, int outNR, int outNC, int outQuant, 104 float *k_cache, float *v_cache, int *kv_len, 105 const float *cos_table, const float *sin_table, int max_ctx, int rope_dim, 106 float *workspace, int ws_size, 107 int L, int N, int NH, int NKH, int HD, int FF, float eps, 108 int n_experts, int n_experts_per_tok, int moe_intermediate, 109 const float **w_gate_inp, const uint8_t **w_gate_exps, const uint8_t **w_up_exps, const uint8_t **w_down_exps, 110 int gate_exp_quant, int up_exp_quant, int down_exp_quant, 111 float *logits, 112 float *moe_prealloc_buf, uint8_t *moe_prealloc_q8, 113 float *gqa_ws 114 ) { 115 /* Workspace layout: x, x_norm, residual, q, k, v, att_out, gate, up, silu, o_proj, ffn_out, router_buf */ 116 int inner = NH * HD; 117 int S = N; if (inner > S) S = inner; if (FF > S) S = FF; if (NKH * HD > S) S = NKH * HD; 118 if (S < 8192) S = 8192; 119 float *x = workspace; 120 float *xn = x + S; 121 float *res = xn + S; 122 float *q = res + S; 123 float *k = q + inner; 124 float *v = k + NKH * HD; 125 float *att = v + NKH * HD; 126 float *gate = att + inner; 127 float *up = gate + FF; 128 float *silu = up + FF; 129 float *oproj = silu + FF; 130 float *ffn = oproj + N; 131 float *router_buf = ffn + N; 132 133 int top_indices[MAX_TOP_K]; 134 float top_weights[MAX_TOP_K]; 135 136 /* Embedding lookup */ 137 memcpy(x, emb + (size_t)(*token) * N, N * sizeof(float)); 138 139 for (int l = 0; l < L; l++) { 140 /* Copy to residual */ 141 memcpy(res, x, N * sizeof(float)); 142 143 /* RMS norm 1 */ 144 rms_norm(xn, x, wAN[l], N, eps); 145 146 /* QKV matmuls */ 147 mxfp4_matmul_omp(wQ[l], xn, q, nQ[l], N); 148 mxfp4_matmul_omp(wK[l], xn, k, nK[l], N); 149 mxfp4_matmul_omp(wV[l], xn, v, nV[l], N); 150 151 /* RoPE */ 152 rope_apply(q, NH, HD, pos, cos_table, sin_table, rope_dim); 153 rope_apply(k, NKH, HD, pos, cos_table, sin_table, rope_dim); 154 155 /* KV cache store */ 156 int sl = kv_len[l]; 157 memcpy(k_cache + (size_t)l * max_ctx * NKH * HD + (size_t)sl * NKH * HD, k, NKH * HD * sizeof(float)); 158 memcpy(v_cache + (size_t)l * max_ctx * NKH * HD + (size_t)sl * NKH * HD, v, NKH * HD * sizeof(float)); 159 kv_len[l]++; 160 sl++; 161 162 /* GQA Attention */ 163 gqa_attention_decode(q, k_cache + (size_t)l * max_ctx * NKH * HD, 164 v_cache + (size_t)l * max_ctx * NKH * HD, att, sl, NH, NKH, HD, gqa_ws); 165 166 /* Output projection + residual */ 167 mxfp4_matmul_omp(wO[l], att, oproj, nO[l], inner); 168 for (int i = 0; i < N; i++) x[i] = res[i] + oproj[i]; 169 170 /* RMS norm 2 */ 171 rms_norm(xn, x, wFN[l], N, eps); 172 173 /* MoE: router + top-k */ 174 router_topk(w_gate_inp[l], xn, n_experts, N, n_experts_per_tok, 175 top_indices, top_weights, router_buf); 176 177 /* MoE FFN */ 178 moe_forward_omp(w_gate_exps + l, w_up_exps + l, w_down_exps + l, 179 xn, moe_intermediate, N, 180 gate_exp_quant, up_exp_quant, down_exp_quant, 181 top_indices, top_weights, n_experts_per_tok, ffn, moe_prealloc_buf, moe_prealloc_q8); 182 183 /* Residual */ 184 for (int i = 0; i < N; i++) x[i] = res[i] + ffn[i]; 185 } 186 187 /* Output projection */ 188 mxfp4_matmul_omp(wOut, xn, logits, outNR, N); 189 }