# Testing Two kinds of evidence: tests that run anywhere in a few minutes, on tiny random models with the real architecture, and a small number of long runs on the full model. This file lists both, and what was not checked. ## Running the tests ``` bash scripts/build.sh # or: bash scripts/build.sh cuda, bash scripts/build.sh emu ctest --preset default # or: cuda, cpu, emu ``` | Test | What it checks | Needs | |---|---|---| | `unit` | JSON, number formats, bit planes (lossless round trip), file reading on every I/O engine of the OS, threads, the pre-tokenizer, the choice of the draft length, the GPU memory arena | nothing | | `selftest` | every backend operation against the CPU reference on random data, and every matrix kernel against itself: the same rows alone and in batches of several sizes must give the same bits ([exact.md](exact.md)). On a CUDA build this is the GPU check, run twice: as usual, then with tiny work buffers and packed weights | nothing | | `model_kimi_k31`, `model_kimi_k3`, `model_kimi_linear` | the whole engine on a tiny random model of each family (63, 62 and 61 checks, listed below) | Python 3, NumPy | | `model_kimi_k31_cuda` | the same with the model math on the GPU, and what only a GPU has (74 checks). Skipped without a usable GPU, except in the `emu` build | a CUDA build | | `tokenizer` | pieces against the `regex` module, token ids against tiktoken, decode round trip, on random multilingual text | `pip install tiktoken regex` (skipped without) | The tiny models are built so that widths that differ in the real model differ there too: hidden 64, KDA 80, latent 32, expert 96, and the full-attention sizes and ranks are all unequal. A matrix read with rows and columns swapped, or a buffer sized with the wrong width, cannot pass by luck. ## What the model tests check **The math.** The C++ reader gives the logits of the NumPy reference (`tools/ref_forward.py`, written separately from the engine), from the checkpoint and from the store's bit planes. Every layer run alone matches the reference too, also from a checkpoint folder that holds only some of its shards. The states `sweep --dump-states` records are the reference's hidden states after those layers. **The same bits.** One logits file, byte for byte, when a sweep is cut into chunks, resumed from checkpoints at four places, run one position at a time, read through another I/O engine, or made to sweep 3 to 7 positions too many and put its checkpoint back, which is what checking drafted tokens does. `tools/sweep_diff.py` finds no difference between a sweep and the same tokens taken one position at a time, and finds a planted one. **The speed features change nothing.** The same logits file again with the trunk read ahead or not (one, two or five layers), with a third of it or all of it kept in RAM, with expert records sent to the device as one block or in pieces, from page-locked buffers or ordinary ones, and with each layer's trunk copied ahead of its turn or in its turn. What is kept in RAM is read from the disk once and then not again. **The store.** It is its source bit for bit, experts and trunk (`tools/store_check.py`), and zeros planted in it are found. A conversion with `--delete-source` gives the same store and leaves no shard behind. A conversion without room on the disk refuses before it writes anything. **Draft and check.** The text is real Kimi's own, token for token: with a drafter that knows the text, with one that is never right, with the draft length fixed or chosen by confidence, with the trunk kept in RAM or not and copied ahead or not, at a stop token, and on a second turn. The number of sweeps and of drafted and kept tokens is what a replay of the rule gives. The C++ drafter makes the same guesses as the NumPy one ([drafter.md](drafter.md)). **The tools.** `record_states.py`, `drafter_test.py`, `drafter_run.py` (including a second run with `--before` and a restart), `long_context.py`, `real_check.py` in both modes and `routing_stats.py` are all rehearsed on the tiny model, so the long runs on the full model do not fail on a typo. **The rest.** Tokenizer round trip, the chat format, `run` with text, `chat` over two turns with `/stats` and `/reset`, and refusal of bad input (an unknown option, a token outside the vocabulary, a layer that is not in the store, the preview drafter, `chat` without a drafter). **Only on a GPU build.** With every weight left as stored, so that the copies ahead really return before they are done: the same logits in six settings, and every part of a sweep reports the trunk it sent ahead. The emulated build adds what a real card cannot show: a card of 32 MiB, on which the model fits because the engine cuts its buffers out of slabs and does not fit with one driver allocation per buffer; a system that refuses to page-lock memory; a driver without a second stream; and copies ahead made as late as the rules allow, so that a weight used too early gives other bits. ## Switches for tests | Variable | Effect | |---|---| | `HKIMI_CUDA_ARENA=0` | the engine asks the driver for every buffer separately | | `HKIMI_CUDA_SLAB_KB=n` | slab size of the device arena | | `HKIMI_CUDA_EXPAND=0` | every dense weight stays packed on the card | | `HKIMI_CUDA_ASYNC=0` | no second stream: no weight is loaded ahead of need | | `HKIMI_EMU_VRAM_MB=n` | size of the emulated card. It rounds every allocation up to 2 MiB, as a driver does from about a megabyte up | | `HKIMI_EMU_NO_PIN=1`, `HKIMI_EMU_NO_STREAM=1` | the emulated card refuses to page-lock memory, or to give a second stream | | `HKIMI_EMU_REPORT=1` | the emulated card says at the end how many copies it made late | | `HKIMI_TEST_DISK_FREE=n` | stands in for the free disk space `convert` sees | ## Where it was built and tested | Configuration | Result | |---|---| | Linux, GCC 13, Release with `-march=native` | all tests pass | | Linux, clang 18 with libc++, CUDA kernels emulated on the CPU (`emu`) | all tests pass, including the GPU model test | | CUDA, nvcc 12.9, on a machine without a GPU | builds without warnings for sm_89 (RTX 4090) and sm_120 (RTX 50 cards) | | CUDA on an RTX 4090 | the test suite passed on the card, with the code as it was measured (see "Not checked") | | Windows, cross-compiled with MinGW-w64 GCC 13, run under Wine 9 | unit tests and the K3.1 model test pass, including the completion-port reader | | ThreadSanitizer, CPU build and emulated GPU build | no report over a conversion, sweeps with reading and copying ahead, a drafted answer and a chat | | Tokenizer | no difference against tiktoken and `regex` on 300,000 tokens of random multilingual text | ## On the full model One machine: an RTX 4090 (25.25 GB as CUDA sees it), 48 cores, about 130 GB of RAM, an NVMe volume that reads 6.7 to 7.4 GB/s, Linux in a container (io_uring is blocked there, so the thread-pool reader was used). | What | Result | |---|---| | The checkpoint | 96 shards, 497,170 tensors, laid out as the engine expects | | Conversion (1.56 TB, `--delete-source`) | 39 minutes, and the disk has as much free space after as before | | The store (`tools/store_check.py`) | no damaged expert among the 82,432; experts 0 to 7 of layer 1 are the original shard's bits | | Real Kimi alone | writes correct text: " Paris." after "The capital of France is", and a correct sentence on why the sky is blue | | The same bits (`tools/drafter_run.py`, step 2) | 64 positions at once, 14 of them one at a time, and 5 parts that each swept 7 positions too many: three identical logits files | | The same bits with every speed feature | five ways of bringing the model's bytes to the card, the same logits to the last bit | | The same text as real Kimi alone | yes, on the two prompts where real Kimi also wrote alone (32 tokens each) | | Eight answers of 48 tokens | the same 384 tokens in every run of the test, before and after each speed feature. 3.4 tokens per sweep, 6.0 s per token once the prompt is read (the table by kind of text is in the README) | | A prompt of 2,605 tokens (`tools/long_context.py`) | the same logits swept whole (481 s) and in five parts that cross the limit of 2,048; the answer `26,94,36` is the three numbers asked for, the first of which stands on a line more than 2,048 tokens back | `tools/drafter_run.py` is the run behind most of these lines. It first times a short text swept in several ways, each of which must give the same logits as the others. Then it compares one text swept at once, one position at a time and in overlapping parts, lets real Kimi write two answers alone, and answers every prompt with the drafter. With `--before ` it also requires every answer to be, token for token, the answer of that run. It takes over an hour and is the thing to run on a new machine. `tools/long_context.py` is the check of the long path. Up to 2,048 tokens of context the full-attention layers of K3.1 Pro look at every earlier position; beyond, an indexer picks 2,048 of them for each query. The tool invents a harbour log of 2,600 tokens with three questions at its end: about a line near the beginning (so only the indexer can bring it in), one in the middle and one near the end. There is no second implementation that can run the full model to compare with, so a right answer to the first question is the evidence that the long path does what the model was trained with. `tools/store_check.py` exists because of a bug. An earlier converter filled a lookup table on first use while several threads converted experts, and a thread that came too early wrote zeros where its expert begins. The table is a compile-time constant now. The tool looks for that damage in a store (a scan), and with `--source` or `--fetch` compares the store with the original files bit for bit. A scan cannot see a change of a few weights, since zero is an ordinary value: only the comparison is a proof, and only for what it compared. ## Not checked - **The published tree on the full model and on a real GPU.** The measurements were made with the code just before it was tidied for publication (unused code of an earlier design removed, comments edited). The two versions were compared on the tiny models, built the same way, on the CPU, on the emulated GPU and under Wine: stores, logits, routing, drafted answers and chat transcripts are the same bytes. The tidied version was not run on a real card or on the full model again. - **Windows on a real machine**, Visual Studio, and a GPU under Windows. MSVC is stricter than MinGW in places and may want small fixes. - **macOS.** The file-reading path exists and the libc++ build passes on Linux. Never built on a Mac, and there is no GPU backend for it. - **Other hardware.** One RTX 4090, one drive. No RTX 50 card, no 16 GB card, no machine with 32 or 64 GB of RAM (the 64 GB figures come from limiting the engine on a larger machine). - **Longer runs.** Answers were compared over their first 48 tokens, and prompts up to 2,605 tokens. - **Another implementation of the whole model.** K3.1 Pro ships no modeling code. [model-facts.md](model-facts.md) says what the forward pass was checked against. ## Measuring a run | Question | Where | |---|---| | How long did an answer take? | the line under each answer in `chat`, and `/stats`. `hkimi run --json` gives `seconds`, `prompt_seconds`, `sweep_seconds`, `draft_seconds` | | How well did the drafter guess? | `sweeps`, `drafted` and `kept` in the same result. `--dump-drafts FILE` writes every round's guesses and confidences | | How much memory did it take? | `device_peak_bytes`, `ram_peak_bytes` and `trunk_ram_bytes` in the result, and the `device memory:` and `RAM:` lines of `hkimi sweep` | | Where does a sweep's time go? | `hkimi sweep` splits it into trunk, experts, attention and output head, with the time spent waiting for the disk. With `--split p,q` it prints what each part read: the cost of checking that many new positions | | How fast is the drive? | `hkimi bench-io ` reads the way the engine reads; `scripts/fio_check.sh` is the same question put to `fio` | | How many experts does a text touch? | `hkimi sweep --dump-routing FILE`, then `tools/routing_stats.py FILE` | | Do two ways of running the same tokens agree? | `tools/sweep_diff.py` on the `--dump-routing` and `--dump-logits` files of two sweeps | | Would a drafter pay on my texts? | `tools/record_states.py`, then `tools/drafter_test.py` ([drafter.md](drafter.md)) | ## Adding a test Unit checks go in `tests/unit_tests.cpp` (one `EXPECT` per fact). End-to-end checks go in `tests/run_model_tests.py` as one `t.check(name, condition, detail)` line. A new backend only has to pass `hkimi selftest`.