#!/usr/bin/env bash # From nothing to a store of the full model, on a Linux machine with an NVIDIA GPU # and about 2 TB of fast NVMe (your own, or a rented one). The cheap checks come # first. Run it inside tmux: the download alone takes an hour or two. # # tmux new -s kimi # bash scripts/setup_linux.sh /workspace (a folder on the big disk) # # Steps, stopping at the first failure: # 1. tools and Python packages # 2. disk check (the disk should read at 3,500 MB/s or more) # 3. build with CUDA and run the tests, including the GPU self-test and the # test with tensors over 2 GB (tools/real_check.py big, 10 GB of disk) # 4. two real layers of Kimi K3.1 Pro, from two or three shards (about 50 GB): # tensor names and shapes, conversion, GPU against CPU, engine against NumPy. # Nothing more is downloaded until this passes. # 5. download the rest of moonshotai/Kimi-K3.1-Pro (1.56 TB in all) # 6. convert to a store, deleting each shard once it is converted (the disk # would not hold both). If the conversion was interrupted, run steps 5 and 6 # again (SKIP="1 2 3 4"): only the shards already deleted are fetched again. # # Skip steps that are done: SKIP="1 2 3" bash scripts/setup_linux.sh /workspace # The script asks before going on after step 2 and before the big download; YES=1 says yes to both. # # On a rented machine the host can read the disk, so type nothing private into it, # and end the rental with Destroy, not Stop (a stopped instance still bills the disk). set -euo pipefail work="${1:?usage: setup_linux.sh }" repo="$(cd "$(dirname "$0")/.." && pwd)" hf_repo="${HK_REPO:-moonshotai/Kimi-K3.1-Pro}" name="$(basename "$hf_repo" | tr '[:upper:]' '[:lower:]')" model="$work/models/$name" store="$work/stores/$name" preset="${HK_PRESET:-cuda}" # test hooks: HK_PRESET=emu builds the CPU emulation of the GPU code, mirror="${HK_MIRROR:-}" # HK_MIRROR= takes the model files from a folder instead of Hugging Face case "$preset" in emu) bdir="$repo/build-emu" ;; cpu) bdir="$repo/build-cpu" ;; *) bdir="$repo/build" ;; esac hkimi="$bdir/hkimi" mkdir -p "$work" skip() { case " ${SKIP:-} " in *" $1 "*) echo "== $1. skipped =="; return 0 ;; esac; return 1; } confirm() { [ "${YES:-}" = 1 ] && return 0 read -r -p "$1 [y/N] " answer [ "$answer" = "y" ] } pip_install() { python3 -m pip install -q "$@" || python3 -m pip install -q --break-system-packages "$@"; } hf_download() { if command -v hf >/dev/null; then hf download "$@"; else huggingface-cli download "$@"; fi; } if ! skip 1; then echo "== 1. tools ==" sudo="" [ "$(id -u)" -eq 0 ] || sudo="sudo" $sudo apt-get update -q $sudo apt-get install -y -q g++ make fio tmux python3-pip pip_install numpy tiktoken regex "huggingface_hub[cli]" export PATH="$HOME/.local/bin:$PATH" # The build needs CMake 3.24 or newer; Ubuntu 22.04 ships 3.22. pip has a current one. have="$(cmake --version 2>/dev/null | sed -n '1s/.* //p')" if [ -z "$have" ] || [ "$(printf '%s\n' 3.24 "$have" | sort -V | head -1)" != "3.24" ]; then pip_install cmake fi cmake --version | head -1 fi export PATH="$HOME/.local/bin:$PATH" if ! skip 2; then echo "== 2. disk ==" bash "$repo/scripts/fio_check.sh" "$work" confirm "Fast enough to keep this machine?" || { echo "stopping here: on a rented box, destroy the instance and pick another one"; exit 1; } fi if ! skip 3; then echo "== 3. build and test ==" cd "$repo" cmake --preset "$preset" cmake --build --preset "$preset" --parallel ctest --preset "$preset" "$hkimi" selftest --backend cuda python3 "$repo/tools/real_check.py" big --hkimi "$hkimi" --work "$work/big-test" --backend cuda rm -rf "$work/big-test" fi if ! skip 4; then echo "== 4. two real layers, before the big download ==" python3 "$repo/tools/real_check.py" layers --hkimi "$hkimi" --work "$work" --repo "$hf_repo" --backend cuda ${mirror:+--mirror "$mirror"} confirm "The layer checks passed. Download the whole model now (1.56 TB for Kimi K3.1 Pro)?" || { echo "stopping here, nothing more was downloaded"; exit 0; } fi if ! skip 5; then echo "== 5. download ==" mkdir -p "$model" if [ -n "$mirror" ]; then for f in "$mirror"/*; do [ -e "$model/$(basename "$f")" ] || cp "$f" "$model"/; done else hf_download "$hf_repo" --local-dir "$model" # the shards of step 4 are already there and are not fetched again fi fi if ! skip 6; then echo "== 6. store ==" if [ -f "$store/manifest.json" ]; then # written last by convert: the store is complete echo "$store is complete already, not converting again" else "$hkimi" inspect "$model" "$hkimi" convert "$model" "$store" --delete-source fi "$hkimi" bench-io "$store/experts" fi echo echo "ready. Get the drafter, then chat:" echo " hf download Inferact/Kimi-K3.1-Pro-DSpark --local-dir $work/models/k31-dspark" echo " $hkimi chat $store --drafter $work/models/k31-dspark"