A 2.78-trillion-parameter Kimi K3 running inference on a single CPU in 8.24 GB of RAM. Portable C99: no BLAS, no framework, no GPU.
C avx2c99cpu-inferencedeep-learningfrom-scratchinference-enginekimi-k3linear-attentionllmllm-inferencemachine-learningmemory-efficientmixture-of-expertsmoemxfp4quantizationsimdsystems-programmingtransformerzero-dependencies
cli: prefill on --gen 0 so a reusable prefix state can be warmed
Fareed Khan53f7248e07f4
diff --git a/src/cli/k3_run.c b/src/cli/k3_run.cindex 3c588b4..d8e4441 100644--- a/src/cli/k3_run.c+++ b/src/cli/k3_run.c@@ -1375,7 +1375,12 @@ int main(int argc, char **argv)* figure against a single step would misstate the I/O share. */double expert_s_total = 0.0, expert_gb_total = 0.0;uint64_t expert_reqs_total = 0, expert_evict_total = 0, expert_bytes_total = 0;- for (int g = 0; nout < gen; g++) {+ /* `nout < gen` drives generation; the `g == 0` disjunct additionally runs the+ * incremental prefill once even when --gen 0, so the prompt's KV and recurrent+ * state are computed and can be saved with ZERO generated tokens. That is what+ * lets --gen 0 --save-state warm a reusable prefix (e.g. a chat system prompt)+ * whose recurrent state is exact rather than one generated token past the end. */+ for (int g = 0; nout < gen || (incremental && g == 0); g++) {k3_cache_reset_stats(&cache);const double ts = now_s();int frc;@@ -1573,7 +1578,11 @@ int main(int argc, char **argv)}free(spec_snap);printf("--------------------------------------------------------------------\n");- printf("%d tokens in %.1f s, %.2f s/token average\n", nout, t_total, t_total / nout);+ if (nout > 0)+ printf("%d tokens in %.1f s, %.2f s/token average\n",+ nout, t_total, t_total / nout);+ else+ printf("prefill only: %d positions cached, 0 tokens generated\n", w.cached);/* Decoded text, when a tokenizer is loaded. Printed as a distinct block rather than* streamed per token: a partially-decoded multi-byte sequence is not valid UTF-8, so@@ -1615,7 +1624,7 @@ int main(int argc, char **argv)"\"stopped_at\":%d,""\"generated_text\":",NL, NL, w.layers_completed, k3_expert_drops, peak_b, t_total,- t_total / nout, (unsigned long long)expert_bytes_total,+ nout ? t_total / nout : 0.0, (unsigned long long)expert_bytes_total,(unsigned long long)(w.trunk ? w.trunk->bytes_read : 0),(unsigned long long)w.ms.embed_bytes_read,(unsigned long long)w.ms.lm_head_bytes_read,