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
docs: record the chat template, trunk pinning, and this round's hardening
Fareed Khan741cec985c89
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7bb123c..b44a7fa 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,16 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).### Added+- **`--chat`**: a terminal REPL implementing K3's official XTML chat format, message+ envelopes, the `thinking_effort` system preamble, and `reasoning_content` on prior+ assistant turns, rendered exactly as the checkpoint's own encoder does, with JSONL+ history and a parser for one assistant completion. Greedy by default; `--temperature`,+ `--top-p`, and `--seed` opt in to sampling. `--no-think` and `--thinking-effort low`,+ `high`, or `max` skip or size the think channel, which on a short answer is most of+ the cost. Without a chat template the engine had completed a chat-shaped prompt as if+ it were mid document rather than answering it.+- **`--trunk-ring N`**: the streaming trunk's prefetch queue depth is now a flag+ (default 2) instead of fixed at two slots.- **`--stop-id N`** (repeatable, up to 8): generation halts as soon as the model emitsa listed token id. Off by default, so `--gen N` still means exactly N tokens forevery benchmark and oracle gate. The stop id stays in the sequence, so `--save-state`@@ -43,9 +53,31 @@ versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).expert path already does. Without OpenMP the loop still runs one chunk at a time. Ashort read in any chunk fails the whole layer, as before, and the decoded output and`trunk_bytes_read` are unchanged.+- **Trunk layers are pinned largest-first instead of as a prefix.** Prefix pinning+ always pinned the 2.34 GB dense layer first, which sized the streaming ring's slot to+ it even after it stopped needing the ring at all. `K3_PIN_PREFIX=1` restores the old+ order on the same binary.### Fixed+- **Hostile or corrupt safetensors, trunk, and config input could reach undefined+ behavior instead of a refusal.** Integer overflow in shape and offset arithmetic+ (a hostile shape or `data_offsets` pair near `INT64_MAX` could wrap and defeat the+ bounds checks meant to catch it), an out-of-bounds read on an empty trunk layer+ array, a batched MoE prefill path that summed uninitialized memory when an expert+ failed to load instead of contributing zero as the per-token path already does, and+ unchecked allocations in the JSON parser and the shard directory listing that could+ crash through a null pointer rather than fail cleanly. A new weightless test,+ `test_st_faults`, builds mutated copies of the fixture shards and asserts each+ failure mode is refused loudly. No input that was already valid changes.+- **`--ids` with a non-numeric piece silently became token id 0** instead of being+ refused, because a failed `strtol` call and a real id of 0 are indistinguishable+ without checking where parsing stopped.+- **`--layers 0` or a value past the real layer count silently ran the complete+ model**, as if `--layers` had not been given, instead of being refused as the typo+ it almost always is.+- **An `--out` path that could not be written still exited 0**, indistinguishable from+ a run whose result was actually saved. It now exits 3.- **`k3_run.json` was not valid JSON after a run that generated nothing.** With`nout == 0` the `seconds_per_token` field computed `t_total / nout` and emitted abare `inf`, so a harness driving `--gen 0 --save-state` failed on the one run itdiff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.mdindex 369c4e8..71937f2 100644--- a/CONTRIBUTORS.md+++ b/CONTRIBUTORS.md@@ -51,6 +51,23 @@ record.resumed from a byte-exact state.- **[Avicennasis](https://github.com/Avicennasis)** -- hardened `--stop-id` parsingagainst silent typos, added its `stopped_at` line to `k3_run.json` and a weightless- contract gate, and kept `k3_run.json` valid when `--gen 0` generates nothing.+ contract gate, and kept `k3_run.json` valid when `--gen 0` generates nothing; carried+ the XTML chat REPL forward with the tokenizer bundle dropped, `--no-think` and+ `--thinking-effort`, and the Windows portability fixes it needed (`getline`, `fsync`,+ and a `rename` that actually replaces an existing file); found and fixed the missing+ OpenMP link on the CMake Apple/libomp path; largest-first trunk pinning, the+ `--trunk-ring` flag, and two prefetcher re-read bugs found by instrumentation rather+ than by inspection.+- **[Blake Evans](https://github.com/BlakeEvans22)** -- the original K3 XTML chat core+ and REPL, reproducing the checkpoint's own chat format so the engine answers a+ chat-shaped prompt instead of completing it.+- **[FermiHart](https://github.com/FermiHart)** -- a set of parser hardening fixes+ across the safetensors reader, the trunk reader, and the vendored JSON library:+ integer-overflow saturation, offset and size checks that run before the arithmetic+ they protect rather than after, a library that no longer calls `exit()` on an+ allocation failure, and a new weightless fault-injection test; a batched MoE prefill+ path that read uninitialized memory on an expert load failure instead of contributing+ zero, exactly as the per-token path already does; and CLI refusals for a malformed+ `--ids` list, an out-of-range `--layers`, and a lost `--out` file.Thank you, all of you.