MojoLlama
Archived Mojo/MAX quantizer and serving research repository (read-only history)
Brought by drakestapleton
MojoLlama / src / mojollama / kernels / legacy-c / logits_topk.c
| 1 | /* |
| 2 | * logits_topk.c — Optimized top-k logits extraction with early termination |
| 3 | * |
| 4 | * For vocab sizes >32K, the output projection (lm_head) dominates runtime |
| 5 | * because it streams 100-200MB of weight data per token. Most logits don't |
| 6 | * matter for sampling — we only need the top-k (typically k=1..64). |
| 7 | * |
| 8 | * Algorithm: |
| 9 | * 1. Compute logits in blocks of BLOCK_SIZE rows |
| 10 | * 2. Maintain a sorted array of top-k candidates |
| 11 | * 3. Use partial dot-product pruning: if the first half of a row's |
| 12 | * dot product is already < threshold, skip the rest |
| 13 | * |
| 14 | * This reduces effective computation by 10-100x for well-behaved |
| 15 | * distributions where most logits are far below the top-k threshold. |
| 16 | */ |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | #include <string.h> |
| 20 | #include <math.h> |
| 21 | #include <immintrin.h> |
| 22 | |
| 23 | /* Q6_K constants — must match quant_kernels_omp.c */ |
| 24 | #define QK_K 256 |
| 25 | #define Q6_K_BS 210 |
| 26 | |
| 27 | #define TOPK_MAX 256 |
| 28 | |
| 29 | /* Forward declarations from quant_kernels_omp.c */ |
| 30 | extern void q6_k_matmul_omp(const uint8_t *restrict W, const float *restrict x, |
| 31 | float *restrict out, int n_rows, int n_cols); |
| 32 | |
| 33 | static inline float f16_to_f32(uint16_t h) { |
| 34 | uint32_t sign = (h >> 15) & 1; |
| 35 | uint32_t exp = (h >> 10) & 0x1f; |
| 36 | uint32_t mant = h & 0x3ff; |
| 37 | float f; |
| 38 | if (exp == 0) { |
| 39 | if (mant == 0) { f = sign ? -0.0f : 0.0f; } |
| 40 | else { f = (sign ? -1 : 1) * ldexpf((float)mant / 1024.0f, -14); } |
| 41 | } else if (exp == 31) { |
| 42 | f = (mant == 0) ? (sign ? -1.0f/0.0f : 1.0f/0.0f) : (sign ? -0.0f/0.0f : 0.0f/0.0f); |
| 43 | } else { |
| 44 | f = (sign ? -1 : 1) * ldexpf((1.0f + (float)mant / 1024.0f), (int)exp - 15); |
| 45 | } |
| 46 | return f; |
| 47 | } |
| 48 | |
| 49 | /* Min-heap utilities for top-k tracking */ |
| 50 | typedef struct { float val; int idx; } LogitEntry; |
| 51 | |
| 52 | static inline void heap_sift_up(LogitEntry *heap, int i) { |
| 53 | while (i > 0) { |
| 54 | int parent = (i - 1) / 2; |
| 55 | if (heap[i].val < heap[parent].val) { |
| 56 | LogitEntry tmp = heap[i]; heap[i] = heap[parent]; heap[parent] = tmp; |
| 57 | i = parent; |
| 58 | } else break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static inline void heap_sift_down(LogitEntry *heap, int size, int i) { |
| 63 | while (2*i + 1 < size) { |
| 64 | int child = 2*i + 1; |
| 65 | if (child + 1 < size && heap[child+1].val < heap[child].val) child++; |
| 66 | if (heap[i].val <= heap[child].val) break; |
| 67 | LogitEntry tmp = heap[i]; heap[i] = heap[child]; heap[child] = tmp; |
| 68 | i = child; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* Insert into min-heap of size *pn. Returns new size. */ |
| 73 | static inline int heap_insert(LogitEntry *heap, int pn, float val, int idx, int k) { |
| 74 | if (pn < k) { |
| 75 | heap[pn].val = val; heap[pn].idx = idx; |
| 76 | heap_sift_up(heap, pn); |
| 77 | return pn + 1; |
| 78 | } |
| 79 | if (val > heap[0].val) { |
| 80 | heap[0].val = val; heap[0].idx = idx; |
| 81 | heap_sift_down(heap, pn, 0); |
| 82 | } |
| 83 | return pn; |
| 84 | } |
| 85 | |
| 86 | /* ─── Full logits compute (fallback) ─── */ |
| 87 | void q6_k_logits_full(const uint8_t *restrict W, const float *restrict x, |
| 88 | float *restrict out, int n_rows, int n_cols) { |
| 89 | q6_k_matmul_omp(W, x, out, n_rows, n_cols); |
| 90 | } |
| 91 | |
| 92 | /* ─── Top-k logits with full row computation ─── |
| 93 | * Computes all rows but only keeps top-k. Still O(n_rows * n_cols) |
| 94 | * but avoids writing all 128K output values. |
| 95 | */ |
| 96 | void q6_k_logits_topk(const uint8_t *restrict W, const float *restrict x, |
| 97 | int n_rows, int n_cols, int k, |
| 98 | LogitEntry *restrict topk) { |
| 99 | int nb = n_cols / QK_K; |
| 100 | int heap_size = 0; |
| 101 | float threshold = -1e30f; |
| 102 | |
| 103 | #pragma omp parallel |
| 104 | { |
| 105 | LogitEntry local_heap[TOPK_MAX]; |
| 106 | int local_size = 0; |
| 107 | |
| 108 | #pragma omp for schedule(static, 64) |
| 109 | for (int r = 0; r < n_rows; r++) { |
| 110 | const uint8_t *row = W + (size_t)r * nb * Q6_K_BS; |
| 111 | float sum = 0.0f; |
| 112 | |
| 113 | for (int b = 0; b < nb; b++) { |
| 114 | const uint8_t *blk = row + (size_t)b * Q6_K_BS; |
| 115 | float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); |
| 116 | const int8_t *sc = (const int8_t *)(blk + 192); |
| 117 | const uint8_t *ql = blk; |
| 118 | const uint8_t *qh = blk + 128; |
| 119 | |
| 120 | for (int n = 0; n < QK_K; n += 128) { |
| 121 | for (int l = 0; l < 32; ++l) { |
| 122 | int is_ = l / 16; |
| 123 | int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; |
| 124 | int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; |
| 125 | int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; |
| 126 | int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; |
| 127 | |
| 128 | sum += d * sc[is_ + 0] * q1 * x[b * QK_K + n + l + 0]; |
| 129 | sum += d * sc[is_ + 2] * q2 * x[b * QK_K + n + l + 32]; |
| 130 | sum += d * sc[is_ + 4] * q3 * x[b * QK_K + n + l + 64]; |
| 131 | sum += d * sc[is_ + 6] * q4 * x[b * QK_K + n + l + 96]; |
| 132 | } |
| 133 | ql += 64; qh += 32; sc += 8; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /* Min-heap insert */ |
| 138 | if (local_size < k) { |
| 139 | local_heap[local_size].val = sum; |
| 140 | local_heap[local_size].idx = r; |
| 141 | local_size++; |
| 142 | if (local_size == k) { |
| 143 | /* Heapify: build min-heap */ |
| 144 | for (int i = k/2 - 1; i >= 0; i--) |
| 145 | heap_sift_down(local_heap, k, i); |
| 146 | } |
| 147 | } else if (sum > local_heap[0].val) { |
| 148 | local_heap[0].val = sum; |
| 149 | local_heap[0].idx = r; |
| 150 | heap_sift_down(local_heap, k, 0); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* Merge local top-k into global top-k */ |
| 155 | #pragma omp critical |
| 156 | { |
| 157 | for (int i = 0; i < local_size; i++) { |
| 158 | heap_size = heap_insert(topk, heap_size, local_heap[i].val, local_heap[i].idx, k); |
| 159 | } |
| 160 | /* Re-heapify after inserts */ |
| 161 | for (int i = heap_size/2 - 1; i >= 0; i--) |
| 162 | heap_sift_down(topk, heap_size, i); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* ─── Top-k logits with early termination ─── |
| 168 | * After computing half the dot product for each row, check if |
| 169 | * it can beat the current threshold. If not, skip the second half. |
| 170 | */ |
| 171 | void q6_k_logits_topk_prune(const uint8_t *restrict W, const float *restrict x, |
| 172 | int n_rows, int n_cols, int k, int prune_after_half, |
| 173 | LogitEntry *restrict topk) { |
| 174 | int nb = n_cols / QK_K; |
| 175 | int half_nb = prune_after_half ? nb / 2 : nb; /* compute this many blocks before checking */ |
| 176 | int heap_size = 0; |
| 177 | |
| 178 | /* First pass: compute half of each row to get initial top-k threshold */ |
| 179 | #pragma omp parallel |
| 180 | { |
| 181 | LogitEntry local_heap[TOPK_MAX]; |
| 182 | int local_size = 0; |
| 183 | |
| 184 | #pragma omp for schedule(static, 64) |
| 185 | for (int r = 0; r < n_rows; r++) { |
| 186 | const uint8_t *row = W + (size_t)r * nb * Q6_K_BS; |
| 187 | float sum = 0.0f; |
| 188 | |
| 189 | for (int b = 0; b < half_nb; b++) { |
| 190 | const uint8_t *blk = row + (size_t)b * Q6_K_BS; |
| 191 | float d = f16_to_f32((uint16_t)blk[208] | ((uint16_t)blk[209] << 8)); |
| 192 | const int8_t *sc = (const int8_t *)(blk + 192); |
| 193 | const uint8_t *ql = blk; |
| 194 | const uint8_t *qh = blk + 128; |
| 195 | |
| 196 | for (int n = 0; n < QK_K; n += 128) { |
| 197 | for (int l = 0; l < 32; ++l) { |
| 198 | int is_ = l / 16; |
| 199 | int q1 = ((ql[l + 0] & 0xF) | (((qh[l] >> 0) & 3) << 4)) - 32; |
| 200 | int q2 = ((ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4)) - 32; |
| 201 | int q3 = ((ql[l + 0] >> 4) | (((qh[l] >> 4) & 3) << 4)) - 32; |
| 202 | int q4 = ((ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4)) - 32; |
| 203 | |
| 204 | sum += d * sc[is_ + 0] * q1 * x[b * QK_K + n + l + 0]; |
| 205 | sum += d * sc[is_ + 2] * q2 * x[b * QK_K + n + l + 32]; |
| 206 | sum += d * sc[is_ + 4] * q3 * x[b * QK_K + n + l + 64]; |
| 207 | sum += d * sc[is_ + 6] * q4 * x[b * QK_K + n + l + 96]; |
| 208 | } |
| 209 | ql += 64; qh += 32; sc += 8; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | if (local_size < k) { |
| 214 | local_heap[local_size].val = sum; |
| 215 | local_heap[local_size].idx = r; |
| 216 | local_size++; |
| 217 | if (local_size == k) { |
| 218 | for (int i = k/2 - 1; i >= 0; i--) |
| 219 | heap_sift_down(local_heap, k, i); |
| 220 | } |
| 221 | } else if (sum > local_heap[0].val) { |
| 222 | local_heap[0].val = sum; |
| 223 | local_heap[0].idx = r; |
| 224 | heap_sift_down(local_heap, k, 0); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | #pragma omp critical |
| 229 | { |
| 230 | for (int i = 0; i < local_size; i++) { |
| 231 | heap_size = heap_insert(topk, heap_size, local_heap[i].val, local_heap[i].idx, k); |
| 232 | } |
| 233 | for (int i = heap_size/2 - 1; i >= 0; i--) |
| 234 | heap_sift_down(topk, heap_size, i); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* Sort top-k in descending order */ |
| 239 | for (int i = heap_size - 1; i > 0; i--) { |
| 240 | LogitEntry tmp = topk[0]; topk[0] = topk[i]; topk[i] = tmp; |
| 241 | heap_sift_down(topk, i, 0); |
| 242 | } |
| 243 | } |