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/model/q4_matmul_c.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 / model / q4_matmul_c.c` 116 lines · 4741 bytes Raw Blame History 1 /* Q4_0 quantized matmul — GGUF format, multi-threaded via OpenMP. 2 * gcc -O3 -march=native -fopenmp -shared -fPIC -o libq4matmul.so q4_matmul_c.c 3 * 4 * Q4_0 block: [f16 scale (2 bytes)][16 bytes nibbles (32 x 4-bit)] 5 * byte[i] = (value[2i+1] << 4) | value[2i] 6 * dequant: val[i] = (nibble - 8) * scale 7 * block_size = 32, type_size = 18 8 */ 9 #include 10 #include 11 #include 12 #include 13 14 #define Q4_BLOCK_SIZE 32 15 #define Q4_TYPE_SIZE 18 16 17 static inline float f16_to_f32(uint16_t h) { 18 uint32_t sign = (uint32_t)(h >> 15), exp = (uint32_t)((h >> 10) & 0x1f), mant = (uint32_t)(h & 0x3ff), f32; 19 if (exp == 0) { 20 if (mant == 0) { f32 = sign << 31; } 21 else { int shift = 24 - __builtin_clz(mant); f32 = (sign << 31) | ((uint32_t)(113 - shift) << 23) | ((mant << (shift + 13)) & 0x7fffff); } 22 } else if (exp == 31) { f32 = (sign << 31) | 0x7f800000 | (mant << 13); } 23 else { f32 = (sign << 31) | ((exp + 112) << 23) | (mant << 13); } 24 float r; memcpy(&r, &f32, sizeof(r)); return r; 25 } 26 27 static inline float q4_block_dot(const uint8_t* block, const float* x) { 28 uint16_t sb; memcpy(&sb, block, 2); 29 float s = f16_to_f32(sb), total = 0.0f; 30 for (int i = 0; i < 16; i++) { 31 uint8_t b = block[2 + i]; 32 total += (float)((int)(b & 0x0F) - 8) * s * x[i * 2]; 33 total += (float)((int)((b >> 4) & 0x0F) - 8) * s * x[i * 2 + 1]; 34 } 35 return total; 36 } 37 38 /* Multi-threaded Q4_0 matmul: y = x @ W.T */ 39 void q4_matmul_forward_t(const uint8_t* w_raw, const float* x, float* out, 40 int out_rows, int in_cols, int batch) { 41 int bpr = (in_cols + Q4_BLOCK_SIZE - 1) / Q4_BLOCK_SIZE; 42 int stride = bpr * Q4_TYPE_SIZE; 43 #pragma omp parallel for schedule(static) 44 for (int r = 0; r < out_rows; r++) { 45 const uint8_t* row = w_raw + (size_t)r * stride; 46 for (int b = 0; b < batch; b++) { 47 const float* xr = x + (size_t)b * in_cols; 48 float sum = 0.0f; 49 for (int blk = 0; blk < bpr; blk++) 50 sum += q4_block_dot(row + (size_t)blk * Q4_TYPE_SIZE, xr + (size_t)blk * Q4_BLOCK_SIZE); 51 out[(size_t)b * out_rows + r] = sum; 52 } 53 } 54 } 55 56 /* Multi-threaded attention scores: scores[h][k] = q[h] @ K[k][h] / sqrt(d) */ 57 void q4_attn_scores(const float* q, const float* k, float* out, 58 int n_head, int n_keys, int head_dim) { 59 float rs = 1.0f / sqrtf((float)head_dim); 60 #pragma omp parallel for collapse(2) 61 for (int h = 0; h < n_head; h++) { 62 for (int key = 0; key < n_keys; key++) { 63 float sum = 0.0f; 64 const float* qh = q + (size_t)h * head_dim; 65 const float* kh = k + (size_t)key * n_head * head_dim + (size_t)h * head_dim; 66 for (int d = 0; d < head_dim; d++) sum += qh[d] * kh[d]; 67 out[(size_t)h * n_keys + key] = sum * rs; 68 } 69 } 70 } 71 72 /* In-place softmax */ 73 void q4_softmax(float* scores, int n_head, int n_keys) { 74 #pragma omp parallel for 75 for (int h = 0; h < n_head; h++) { 76 float* s = scores + (size_t)h * n_keys; 77 float mx = s[0]; for (int k = 1; k < n_keys; k++) if (s[k] > mx) mx = s[k]; 78 float sum = 0.0f; for (int k = 0; k < n_keys; k++) { s[k] = expf(s[k] - mx); sum += s[k]; } 79 float inv = 1.0f / sum; for (int k = 0; k < n_keys; k++) s[k] *= inv; 80 } 81 } 82 83 /* Attention apply: out[h][d] = sum_k att[h][k] * v[k][h][d] */ 84 void q4_attn_apply(const float* att, const float* v, float* out, 85 int n_head, int n_keys, int head_dim) { 86 #pragma omp parallel for collapse(2) 87 for (int h = 0; h < n_head; h++) { 88 for (int d = 0; d < head_dim; d++) { 89 float sum = 0.0f; 90 const float* ah = att + (size_t)h * n_keys; 91 for (int k = 0; k < n_keys; k++) 92 sum += ah[k] * v[(size_t)k * n_head * head_dim + (size_t)h * head_dim + d]; 93 out[(size_t)h * head_dim + d] = sum; 94 } 95 } 96 } 97 98 /* RMSNorm: out = x / sqrt(mean(x^2) + eps) * weight */ 99 void q4_rms_norm(const float* x, const float* weight, float* out, 100 int rows, int cols, float eps) { 101 #pragma omp parallel for 102 for (int r = 0; r < rows; r++) { 103 const float* xr = x + (size_t)r * cols; 104 float* orow = out + (size_t)r * cols; 105 float ss = 0.0f; 106 for (int c = 0; c < cols; c++) ss += xr[c] * xr[c]; 107 float inv = 1.0f / sqrtf(ss / (float)cols + eps); 108 for (int c = 0; c < cols; c++) orow[c] = xr[c] * inv * weight[c]; 109 } 110 } 111 112 /* SiLU activation: out = x / (1 + exp(-x)) */ 113 void q4_silu(const float* x, float* out, int n) { 114 #pragma omp parallel for 115 for (int i = 0; i < n; i++) out[i] = x[i] / (1.0f + expf(-x[i])); 116 }