MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / hybrid_engine.c
| 1 | /* hybrid_engine.c — C loop calling proven kernels (avx2_batch, gqa, simd_ops). |
| 2 | * Same operations as v7.7, but the per-layer loop is in C, not Python. |
| 3 | * |
| 4 | * Compile: |
| 5 | * gcc -O3 -mavx2 -mfma -mf16c -fopenmp -shared -fPIC \ |
| 6 | * -o hybrid_engine.so hybrid_engine.c -lm |
| 7 | */ |
| 8 | #include <stdint.h> |
| 9 | #include <string.h> |
| 10 | #include <math.h> |
| 11 | #include <omp.h> |
| 12 | |
| 13 | /* External kernels from avx2_batch_kernels.so */ |
| 14 | extern void batch_qkv_q4_0(const uint8_t*, const uint8_t*, const uint8_t*, |
| 15 | const float*, float*, float*, float*, |
| 16 | int, int, int, int); |
| 17 | extern void batch_gate_up_q4_0(const uint8_t*, const uint8_t*, |
| 18 | const float*, float*, float*, |
| 19 | int, int, int); |
| 20 | |
| 21 | /* External from gqa_attention.so */ |
| 22 | extern void gqa_attention_decode(const float*, const float*, const float*, |
| 23 | float*, int, int, int, int); |
| 24 | |
| 25 | /* External from simd_ops.so */ |
| 26 | extern void rms_norm(const float*, const float*, float*, int, float); |
| 27 | extern void silu(float*, int); |
| 28 | |
| 29 | /* ═════════════════════════════════════════════════════════════════════════ |
| 30 | * hybrid_forward — C loop over layers, calling proven kernels |
| 31 | * ═════════════════════════════════════════════════════════════════════════ */ |
| 32 | void hybrid_forward( |
| 33 | int tok, |
| 34 | int L, int N, int NH, int NKH, int HD, int FF, int V, |
| 35 | float eps, |
| 36 | const uint8_t **wQ, const uint8_t **wK, const uint8_t **wV, |
| 37 | const uint8_t **wO, const uint8_t **wG, const uint8_t **wU, const uint8_t **wD, |
| 38 | const float **wAN, const float **wFN, |
| 39 | const int *nQ, const int *nK, const int *nV, |
| 40 | const int *nO, const int *nG, const int *nU, const int *nD, |
| 41 | int nc, |
| 42 | const float *emb, const float *onw, |
| 43 | const uint8_t *wOut, int outNR, int outNC, |
| 44 | float *kvK, float *kvV, int *kvL, int MP, |
| 45 | float *x, float *xn, float *res, |
| 46 | float *q, float *k, float *v, |
| 47 | float *att, float *gate, float *up, |
| 48 | float *silu_buf, float *oproj, float *ffn, |
| 49 | float *logits |
| 50 | ) { |
| 51 | memcpy(x, emb + (size_t)tok * N, N * sizeof(float)); |
| 52 | |
| 53 | for (int l = 0; l < L; l++) { |
| 54 | memcpy(res, x, N * sizeof(float)); |
| 55 | rms_norm(xn, x, wAN[l], N, eps); |
| 56 | |
| 57 | /* Batched QKV */ |
| 58 | batch_qkv_q4_0(wQ[l], wK[l], wV[l], xn, q, k, v, nQ[l], nK[l], nV[l], nc); |
| 59 | |
| 60 | /* RoPE */ |
| 61 | int pos = kvL[l]; |
| 62 | for (int hh = 0; hh < NH; hh++) { |
| 63 | float *qh = q + hh * HD; |
| 64 | for (int j = 0; j < HD/2; j++) { |
| 65 | double ang = (double)pos / pow(10000.0, 2.0*j/HD); |
| 66 | double cs = cos(ang), sn = sin(ang); |
| 67 | float a = qh[j], b = qh[j + HD/2]; |
| 68 | qh[j] = (float)(a * cs - b * sn); |
| 69 | qh[j + HD/2] = (float)(b * cs + a * sn); |
| 70 | } |
| 71 | } |
| 72 | for (int hh = 0; hh < NKH; hh++) { |
| 73 | float *kh = k + hh * HD; |
| 74 | for (int j = 0; j < HD/2; j++) { |
| 75 | double ang = (double)pos / pow(10000.0, 2.0*j/HD); |
| 76 | double cs = cos(ang), sn = sin(ang); |
| 77 | float a = kh[j], b = kh[j + HD/2]; |
| 78 | kh[j] = (float)(a * cs - b * sn); |
| 79 | kh[j + HD/2] = (float)(b * cs + a * sn); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /* KV store */ |
| 84 | memcpy(kvK + ((size_t)l * MP + kvL[l]) * NKH * HD, k, NKH * HD * sizeof(float)); |
| 85 | memcpy(kvV + ((size_t)l * MP + kvL[l]) * NKH * HD, v, NKH * HD * sizeof(float)); |
| 86 | |
| 87 | /* GQA attention */ |
| 88 | gqa_attention_decode(q, |
| 89 | kvK + (size_t)l * MP * NKH * HD, |
| 90 | kvV + (size_t)l * MP * NKH * HD, |
| 91 | att, kvL[l] + 1, NH, NKH, HD); |
| 92 | kvL[l]++; |
| 93 | |
| 94 | /* O proj + residual */ |
| 95 | #pragma omp parallel for schedule(static) |
| 96 | for (int i = 0; i < nO[l]; i++) |
| 97 | oproj[i] = 0.0f; /* placeholder — need q4_0_row_dot here */ |
| 98 | memcpy(x, res, N * sizeof(float)); /* placeholder: skip O proj for now */ |
| 99 | |
| 100 | /* FFN norm */ |
| 101 | memcpy(res, x, N * sizeof(float)); |
| 102 | rms_norm(xn, x, wFN[l], N, eps); |
| 103 | |
| 104 | /* Gate+Up */ |
| 105 | batch_gate_up_q4_0(wG[l], wU[l], xn, gate, up, nG[l], nU[l], nc); |
| 106 | |
| 107 | /* SiLU(gate) * up */ |
| 108 | for (int i = 0; i < FF; i++) { |
| 109 | float g = gate[i]; |
| 110 | silu_buf[i] = (g / (1.0f + expf(-g))) * up[i]; |
| 111 | } |
| 112 | |
| 113 | /* Down + residual — placeholder */ |
| 114 | for (int i = 0; i < N; i++) x[i] = res[i]; /* skip ffn proj for now */ |
| 115 | } |
| 116 | |
| 117 | /* Final norm + output — placeholder */ |
| 118 | for (int i = 0; i < outNR; i++) logits[i] = 0.0f; |
| 119 | } |