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
feat(scripts): add the doctor, download and trunk-packing helpers
FareedKhan-dev69e3124d895e
diff --git a/scripts/download-model.sh b/scripts/download-model.shnew file mode 100644index 0000000..f2cebfe--- /dev/null+++ b/scripts/download-model.sh@@ -0,0 +1,87 @@+#!/usr/bin/env bash+# download-model.sh, fetch the Kimi K3 checkpoint and verify it byte-exactly.+#+# scripts/download-model.sh <dest_dir>+#+# The checkpoint is 1.56 TB across 96 safetensors shards. A partial or corrupt download+# does not fail loudly, it produces wrong tokens, so the byte total is checked against+# the published figure before anything else is allowed to use it.+#+# Requires a HuggingFace token in $HF_TOKEN or ~/.cache/huggingface/token.++set -euo pipefail++DEST="${1:?usage: download-model.sh <dest_dir>}"+REPO="moonshotai/Kimi-K3"++# Published totals for the released checkpoint. Verified, not assumed.+EXPECT_SHARDS=96+EXPECT_BYTES=1560936091448++command -v python3 >/dev/null || { echo "python3 required"; exit 1; }++if ! python3 -c "import huggingface_hub" 2>/dev/null; then+ echo "installing huggingface_hub…"+ python3 -m pip install --quiet --upgrade "huggingface_hub[cli]"+fi++mkdir -p "$DEST"++echo "downloading $REPO -> $DEST"+echo " 1.56 TB across $EXPECT_SHARDS shards; expect ~30 min at 1 GB/s"+echo++# The token is read from the environment and never echoed.+HF_HUB_ENABLE_HF_TRANSFER=1 \+python3 -m huggingface_hub.commands.huggingface_cli download "$REPO" \+ --local-dir "$DEST" --max-workers 16++echo+echo "verifying…"+# find, not `ls | wc -l`: under `set -euo pipefail` a glob that matches nothing makes+# ls exit non-zero and the script dies HERE, so the "download is incomplete" message+# below -- the entire point of this verification block -- would never be reached.+N=$(find "$DEST" -maxdepth 1 -name '*.safetensors' | wc -l)+B=$(find "$DEST" -maxdepth 1 -name '*.safetensors' -printf '%s\n' | awk '{s+=$1} END{print s+0}')++printf ' shards : %s (expect %s)\n' "$N" "$EXPECT_SHARDS"+printf ' bytes : %s (expect %s)\n' "$B" "$EXPECT_BYTES"++if [ "$N" -ne "$EXPECT_SHARDS" ]; then+ echo "FAIL: wrong shard count, the download is incomplete."+ exit 1+fi+if [ "$B" -ne "$EXPECT_BYTES" ]; then+ echo "FAIL: byte total mismatch (delta $((B - EXPECT_BYTES)))."+ echo " A partial checkpoint yields wrong output silently. Re-run to resume."+ exit 1+fi++# Per-shard sizes. The total above already proves the download is complete; this proves+# WHICH shard is wrong when it is not, turning "re-download 1.56 TB" into "re-download+# 17 GB". It also catches the one case a total cannot: two shards wrong in opposite+# directions by the same amount.+SIZES="$(dirname "$0")/shard_sizes.txt"+if [ -f "$SIZES" ]; then+ bad=0+ while read -r name want; do+ [ -n "$name" ] || continue+ got=$(stat -c%s "$DEST/$name" 2>/dev/null || echo 0)+ if [ "$got" != "$want" ]; then+ printf ' BAD %s: %s bytes, expected %s\n' "$name" "$got" "$want"+ bad=$((bad + 1))+ fi+ done < "$SIZES"+ if [ "$bad" -ne 0 ]; then+ echo "FAIL: $bad shard(s) do not match their published sizes."+ echo " Delete just those files and re-run; the download resumes."+ exit 1+ fi+ printf ' shards : all %s match their published sizes individually\n' "$EXPECT_SHARDS"+fi++echo " RESULT : byte-exact match"+echo+echo "next: scripts/pack-trunk.sh $DEST <trunk_dir>"+echo " packing the trunk is what lets the engine stream it, which is what makes"+echo " the memory budget a dial instead of a floor."diff --git a/scripts/k3-doctor.sh b/scripts/k3-doctor.shnew file mode 100644index 0000000..02f3a4f--- /dev/null+++ b/scripts/k3-doctor.sh@@ -0,0 +1,154 @@+#!/usr/bin/env bash+# k3-doctor, check whether this machine can run Kimi K3, and how fast.+#+# Answers three questions before you spend an hour finding out the hard way:+# 1. Is the toolchain present and does the engine build?+# 2. How much memory is there, and which preset does that imply?+# 3. How fast is the storage the weights will stream from?+#+# Exits non-zero if the machine cannot run the model at all.+#+# LINUX ONLY, like every script under scripts/ and benchmarks/. This one reads+# /proc/meminfo and uses GNU `stat` and `df` options; the engine itself additionally+# needs O_DIRECT and getrusage. The tokenizer and config reader are portable C99 and+# build anywhere, but the streaming engine does not.++set -u++case "$(uname -s)" in+ Linux) ;;+ *) echo "k3-doctor: this script and the streaming engine are Linux-only."+ echo " (/proc/meminfo, O_DIRECT, GNU stat/df). Detected: $(uname -s)"+ exit 1 ;;+esac++RED=$'\033[31m'; GRN=$'\033[32m'; YLW=$'\033[33m'; DIM=$'\033[2m'; RST=$'\033[0m'+[ -t 1 ] || { RED=""; GRN=""; YLW=""; DIM=""; RST=""; }++ok() { printf ' %sok%s %s\n' "$GRN" "$RST" "$*"; }+warn() { printf ' %swarn%s %s\n' "$YLW" "$RST" "$*"; }+bad() { printf ' %sFAIL%s %s\n' "$RED" "$RST" "$*"; FAILED=1; }+hdr() { printf '\n%s\n' "$*"; }++FAILED=0+MODEL_DIR="${1:-}"++printf '%s\n' "Kimi K3, environment check"++# ------------------------------------------------------------------ toolchain --+hdr "toolchain"+if command -v cc >/dev/null 2>&1 || command -v gcc >/dev/null 2>&1; then+ CCBIN=$(command -v cc || command -v gcc)+ ok "C compiler: $($CCBIN --version 2>&1 | head -1)"+else+ bad "no C compiler found (install build-essential or equivalent)"+fi+command -v make >/dev/null 2>&1 && ok "make: $(make --version | head -1)" || bad "make not found"+if command -v python3 >/dev/null 2>&1; then+ ok "python3: $(python3 --version 2>&1)"+else+ warn "python3 not found, the trunk packer and reference tools need it"+fi++# ------------------------------------------------------------------------ cpu --+hdr "cpu"+NCPU=$(getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)+ok "cores: $NCPU"+if grep -qm1 avx2 /proc/cpuinfo 2>/dev/null; then+ ok "AVX2: present"+else+ warn "AVX2 not detected, the engine will run but the expert matmuls lose their fast path"+fi+grep -qm1 avx512f /proc/cpuinfo 2>/dev/null && ok "AVX-512: present" \+ || printf ' %sinfo AVX-512 absent (not required)%s\n' "$DIM" "$RST"++# --------------------------------------------------------------------- memory --+hdr "memory"+MEM_KB=$(awk '/MemTotal/{print $2}' /proc/meminfo 2>/dev/null || echo 0)+MEM_GB=$(( MEM_KB / 1024 / 1024 ))+AVAIL_KB=$(awk '/MemAvailable/{print $2}' /proc/meminfo 2>/dev/null || echo 0)+AVAIL_GB=$(( AVAIL_KB / 1024 / 1024 ))+ok "total: ${MEM_GB} GB, available: ${AVAIL_GB} GB"++# The preset boundaries follow the measured memory ladder; see docs/PERFORMANCE.md.+if [ "$AVAIL_GB" -ge 192 ]; then PRESET=server; EXPECT="~19-21 s/token"+elif [ "$AVAIL_GB" -ge 96 ]; then PRESET=workstation; EXPECT="~24 s/token"+elif [ "$AVAIL_GB" -ge 32 ]; then PRESET=desktop; EXPECT="~28-31 s/token"+elif [ "$AVAIL_GB" -ge 10 ]; then PRESET=laptop; EXPECT="~32 s/token"+else PRESET=""; fi++if [ -n "$PRESET" ]; then+ ok "recommended preset: --preset $PRESET (expect $EXPECT)"+else+ bad "under 10 GB available, below the engine's floor (~8.2 GB measured peak RSS)"+fi++# -------------------------------------------------------------------- storage --+hdr "storage"+TARGET="${MODEL_DIR:-$PWD}"+if [ -d "$TARGET" ]; then+ AVAIL_DISK=$(df -BG "$TARGET" 2>/dev/null | awk 'NR==2{gsub("G","",$4); print $4}')+ ok "free space at $TARGET: ${AVAIL_DISK:-?} GB"+ # 1.56 TB checkpoint + ~109 GB packed trunk.+ if [ -n "${AVAIL_DISK:-}" ] && [ "$AVAIL_DISK" -lt 1700 ]; then+ warn "the full checkpoint needs ~1.56 TB plus ~109 GB for the packed trunk"+ fi++ printf ' %smeasuring sequential read (2 GB, this takes a moment)…%s\n' "$DIM" "$RST"+ TMPF="$TARGET/.k3_doctor_probe"+ if dd if=/dev/zero of="$TMPF" bs=1M count=2048 conv=fsync 2>/dev/null; then+ sync+ RATE=$(dd if="$TMPF" of=/dev/null bs=4M 2>&1 | awk '/copied/{print $(NF-1)" "$NF}')+ rm -f "$TMPF"+ ok "sequential read: ${RATE:-unknown}"+ printf ' %sthe engine streams ~135 GB per token; storage is usually the ceiling%s\n' "$DIM" "$RST"+ else+ warn "could not write a probe file to $TARGET"+ rm -f "$TMPF" 2>/dev/null+ fi+else+ warn "no model directory given; pass one as the first argument to check its storage"+fi++# ---------------------------------------------------------------- model files --+if [ -n "$MODEL_DIR" ]; then+ hdr "model"+ # find, not `ls | wc -l`: a glob matching nothing would abort the script under+ # `set -euo pipefail` instead of reporting zero shards.+ N=$(find "$MODEL_DIR" -maxdepth 1 -name '*.safetensors' | wc -l)+ if [ "$N" -eq 0 ]; then+ warn "no .safetensors shards in $MODEL_DIR, run scripts/download-model.sh"+ else+ ok "shards present: $N"+ [ "$N" -eq 96 ] || warn "expected 96 shards for the full checkpoint"+ fi+ for f in config.json tiktoken.model tokenizer_config.json; do+ [ -f "$MODEL_DIR/$f" ] && ok "$f" || warn "$f missing (needed for text in/out)"+ done+fi++hdr "result"+if [ "$FAILED" -eq 0 ]; then+ printf ' %sthis machine can run Kimi K3%s\n' "$GRN" "$RST"++ # The command printed here is the one a user is most likely to copy, so every part+ # of it has to work as written:+ # --tok is REQUIRED by --prompt. Without it the engine exits 2 rather than+ # guessing where the tokenizer lives.+ # --trunk is what makes the preset mean anything. Omit it and the trunk loads+ # fully resident at ~110 GB, which is the opposite of the budget just+ # recommended.+ # MODEL_DIR is substituted when one was given, so the line can be pasted verbatim.+ if [ -n "$PRESET" ]; then+ M="${MODEL_DIR:-<model_dir>}"+ printf '\n next:\n'+ printf ' %smake -j%s\n' "$DIM" "$RST"+ printf ' %s./scripts/pack-trunk.sh %s ~/k3trunk%s\n' "$DIM" "$M" "$RST"+ printf ' %s./bin/k3 %s --trunk ~/k3trunk --preset %s \\%s\n' "$DIM" "$M" "$PRESET" "$RST"+ printf ' %s --tok %s --prompt "Hello" --gen 8 --incremental%s\n' "$DIM" "$M" "$RST"+ fi+ exit 0+else+ printf ' %sblocking problems above%s\n' "$RED" "$RST"+ exit 1+fidiff --git a/scripts/pack-trunk.sh b/scripts/pack-trunk.shnew file mode 100644index 0000000..df16706--- /dev/null+++ b/scripts/pack-trunk.sh@@ -0,0 +1,32 @@+#!/usr/bin/env bash+# pack-trunk.sh, build the packed trunk that makes memory a dial.+#+# scripts/pack-trunk.sh <model_dir> <trunk_dir>+#+# Each layer's trunk tensors form one contiguous run inside its shard, so this is 93+# sequential range copies rather than a scatter-gather. Afterwards, loading a layer is a+# single pread from a known offset, which is what lets the trunk stream at full device+# bandwidth and overlap compute.+#+# Output is ~108.81 GB. Put it on the fastest local storage you have.+set -euo pipefail+MODEL="${1:?usage: pack-trunk.sh <model_dir> <trunk_dir>}"+TRUNK="${2:?}"++command -v python3 >/dev/null || { echo "python3 required"; exit 1; }+[ -d "$MODEL" ] || { echo "no such model dir: $MODEL"; exit 1; }++# find, not `ls | wc -l`: under `set -euo pipefail` a glob that matches nothing makes+# ls exit non-zero, the pipeline fails, and the script dies AT THIS LINE -- so the+# diagnostic on the next line, which is the whole reason the check exists, never prints.+N=$(find "$MODEL" -maxdepth 1 -name '*.safetensors' | wc -l)+[ "$N" -gt 0 ] || { echo "no .safetensors in $MODEL, run download-model.sh first"; exit 1; }++mkdir -p "$TRUNK"+echo "packing trunk from $N shards -> $TRUNK"+python3 "$(dirname "$0")/../tools/pack_trunk.py" "$MODEL" "$TRUNK" 93++echo+ls -la "$TRUNK"+echo+echo "next: ./bin/k3 $MODEL --trunk $TRUNK --preset server --tok $MODEL --prompt 'Hello' --incremental"diff --git a/scripts/shard_sizes.txt b/scripts/shard_sizes.txtnew file mode 100644index 0000000..ffcd2ea--- /dev/null+++ b/scripts/shard_sizes.txt@@ -0,0 +1,96 @@+model-00001-of-000096.safetensors 2341216112+model-00002-of-000096.safetensors 16990911504+model-00003-of-000096.safetensors 16990911504+model-00004-of-000096.safetensors 16567501776+model-00005-of-000096.safetensors 16990911504+model-00006-of-000096.safetensors 16990911504+model-00007-of-000096.safetensors 16990911504+model-00008-of-000096.safetensors 16567501776+model-00009-of-000096.safetensors 16990911504+model-00010-of-000096.safetensors 16990911504+model-00011-of-000096.safetensors 16990916912+model-00012-of-000096.safetensors 16567507176+model-00013-of-000096.safetensors 16990916912+model-00014-of-000096.safetensors 16990916912+model-00015-of-000096.safetensors 16990916912+model-00016-of-000096.safetensors 16567507176+model-00017-of-000096.safetensors 16990916912+model-00018-of-000096.safetensors 16990916912+model-00019-of-000096.safetensors 16990916912+model-00020-of-000096.safetensors 16567507176+model-00021-of-000096.safetensors 16990916912+model-00022-of-000096.safetensors 16990916912+model-00023-of-000096.safetensors 16990916912+model-00024-of-000096.safetensors 16567507176+model-00025-of-000096.safetensors 16990916912+model-00026-of-000096.safetensors 16990916912+model-00027-of-000096.safetensors 16990916912+model-00028-of-000096.safetensors 16567507176+model-00029-of-000096.safetensors 16990916912+model-00030-of-000096.safetensors 16990916912+model-00031-of-000096.safetensors 16990916912+model-00032-of-000096.safetensors 16567507176+model-00033-of-000096.safetensors 16990916912+model-00034-of-000096.safetensors 16990916912+model-00035-of-000096.safetensors 16990916912+model-00036-of-000096.safetensors 16567507176+model-00037-of-000096.safetensors 16990916912+model-00038-of-000096.safetensors 16990916912+model-00039-of-000096.safetensors 16990916912+model-00040-of-000096.safetensors 16567507176+model-00041-of-000096.safetensors 16990916912+model-00042-of-000096.safetensors 16990916912+model-00043-of-000096.safetensors 16990916912+model-00044-of-000096.safetensors 16567507176+model-00045-of-000096.safetensors 16990916912+model-00046-of-000096.safetensors 16990916912+model-00047-of-000096.safetensors 16990916912+model-00048-of-000096.safetensors 16567507176+model-00049-of-000096.safetensors 16990916912+model-00050-of-000096.safetensors 16990916912+model-00051-of-000096.safetensors 16990916912+model-00052-of-000096.safetensors 16567507176+model-00053-of-000096.safetensors 16990916912+model-00054-of-000096.safetensors 16990916912+model-00055-of-000096.safetensors 16990916912+model-00056-of-000096.safetensors 16567507176+model-00057-of-000096.safetensors 16990916912+model-00058-of-000096.safetensors 16990916912+model-00059-of-000096.safetensors 16990916912+model-00060-of-000096.safetensors 16567507176+model-00061-of-000096.safetensors 16990916912+model-00062-of-000096.safetensors 16990916912+model-00063-of-000096.safetensors 16990916912+model-00064-of-000096.safetensors 16567507176+model-00065-of-000096.safetensors 16990916912+model-00066-of-000096.safetensors 16990916912+model-00067-of-000096.safetensors 16990916912+model-00068-of-000096.safetensors 16567507176+model-00069-of-000096.safetensors 16990916912+model-00070-of-000096.safetensors 16990916912+model-00071-of-000096.safetensors 16990916912+model-00072-of-000096.safetensors 16567507176+model-00073-of-000096.safetensors 16990916912+model-00074-of-000096.safetensors 16990916912+model-00075-of-000096.safetensors 16990916912+model-00076-of-000096.safetensors 16567507176+model-00077-of-000096.safetensors 16990916912+model-00078-of-000096.safetensors 16990916912+model-00079-of-000096.safetensors 16990916912+model-00080-of-000096.safetensors 16567507176+model-00081-of-000096.safetensors 16990916912+model-00082-of-000096.safetensors 16990916912+model-00083-of-000096.safetensors 16990916912+model-00084-of-000096.safetensors 16567507176+model-00085-of-000096.safetensors 16990916912+model-00086-of-000096.safetensors 16990916912+model-00087-of-000096.safetensors 16990916912+model-00088-of-000096.safetensors 16567507176+model-00089-of-000096.safetensors 16990916912+model-00090-of-000096.safetensors 16990916912+model-00091-of-000096.safetensors 16990916912+model-00092-of-000096.safetensors 16567507176+model-00093-of-000096.safetensors 16567507176+model-00094-of-000096.safetensors 4697664072+model-00095-of-000096.safetensors 92289328+model-00096-of-000096.safetensors 802448352