MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / cengine_batch_clean.c
| 1 | /* cengine_batch.c — vLLM-style concurrent batched inference. |
| 2 | * Uses q4_0_batch_matmul for all weight projections across B tokens. |
| 3 | */ |
| 4 | #include <stdint.h> |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> |
| 7 | #include <string.h> |
| 8 | #include <math.h> |
| 9 | #include <immintrin.h> |
| 10 | #include <omp.h> |
| 11 | #define Q4_0_BS 18 |
| 12 | #define Q8_0_BS 34 |
| 13 | #define BLOCK_SIZE 64 |
| 14 | #define MAX_BLOCKS 1024 |
| 15 | #define QK_K 256 |
| 16 | |
| 17 | /* K-quant block structures (from llama.cpp) */ |
| 18 | typedef struct { |
| 19 | uint16_t d; // half: super-block scale |
| 20 | uint16_t dm; // half: super-block min |
| 21 | uint8_t scales[12]; // 8 × 6-bit scale/min pairs |
| 22 | uint8_t qs[128]; // 4-bit nibbles (256 elements) |
| 23 | } block_q4_K; |
| 24 | |
| 25 | typedef struct { |
| 26 | uint8_t ql[128]; // lower 4 bits of quants |
| 27 | uint8_t qh[64]; // upper 2 bits of quants |
| 28 | int8_t scales[16]; // 16 × int8 scales (one per 16 elements) |
| 29 | uint16_t d; // half: super-block scale |
| 30 | } block_q6_K; |
| 31 | |
| 32 | /* GGML half <-> float conversion */ |
| 33 | static inline float gf16(uint16_t h) { |
| 34 | return _cvtsh_ss(h); |
| 35 | } |
| 36 | |
| 37 | /* Q4_K scale extraction */ |
| 38 | static inline void k4_scale(int j, const uint8_t *q, uint8_t *d, uint8_t *m) { |
| 39 | if (j < 4) { *d = q[j] & 63; *m = q[j+4] & 63; } |
| 40 | else { *d = (q[j+4] & 0xF) | ((q[j-4] >> 6) << 4); |
| 41 | *m = (q[j+4] >> 4) | ((q[j] >> 6) << 4); } |
| 42 | } |