tensorfold-1017-prepare.sh 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #!/usr/bin/env bash
  2. # tensorfold-1017-prepare — host-side staging for the TensorFold 2x 1017 lane.
  3. #
  4. # Idempotent: every step is skipped when its output already exists. Runs on
  5. # each launch node before the containers start (recipe `before_serve`), with
  6. # NODE_RANK exported by the launcher.
  7. #
  8. # Everything here works WITHOUT the image: the launcher runs pre-serve hooks
  9. # *before* it builds/syncs the image, so the helper files (Engram packer, chat
  10. # template, CUDA prebuild inputs) are published to the NAS by the recipe image's
  11. # build.sh. The one image-dependent step (CUDA prebuild) is skipped when the
  12. # image is not on this node yet and simply runs on the next serve.
  13. #
  14. # Steps
  15. # 1. node-local copy of the EXL3 2.9bpw checkpoint (EXL3 streams weights on
  16. # every decode step — reading them off the NAS costs most of the decode);
  17. # 2. the chat template the TensorFold HTTP app requires at init (the EXL3
  18. # checkpoint carries none);
  19. # 3. the per-rank Engram packs (layers 1 + 14, ~47 GiB each) built from the
  20. # official 1017 shards 47/48 — rank r owns hash heads [12r, 12r+12);
  21. # 4. the CUDA extension prebuild (CPU-only, 18/18 builders) when the image is
  22. # already present.
  23. #
  24. # Overrides (env): TF1017_ROOT (default /home/kilork/ns/1017-tf),
  25. # TF1017_NAS (default /mnt/nas/ai/huggingface/hub), TF1017_IMAGE,
  26. # TF1017_SKIP_WEIGHTS=1, TF1017_SKIP_PACK=1, TF1017_SKIP_PREBUILD=1.
  27. set -euo pipefail
  28. ROOT="${TF1017_ROOT:-/home/kilork/ns/1017-tf}"
  29. NAS="${TF1017_NAS:-/mnt/nas/ai/huggingface/hub}"
  30. PUB="${TF1017_PUB:-/mnt/nas/ai/sm121/dsv41-tf-1017}"
  31. IMG="${TF1017_IMAGE:-dsv41-tf-1017:v1}"
  32. RANK="${NODE_RANK:-0}"
  33. # Pins are only a fallback: the HF cache's `refs/main` is the truth (a
  34. # re-cut/re-download moves the snapshot, and a hardcoded rev then points at a
  35. # directory that no longer exists — exactly what broke the 2026-10-21 serve).
  36. pin_or_ref() { # <hub dir> <pinned rev>
  37. local d="$NAS/$1" ref
  38. ref=$(cat "$d/refs/main" 2>/dev/null || true)
  39. if [[ -n "$ref" && -d "$d/snapshots/$ref" ]]; then echo "$ref"; else echo "$2"; fi
  40. }
  41. REV="${TF1017_MODEL_REV:-$(pin_or_ref models--LibertAIDAI--DeepSeek-V4.1-Flash-1017-EXL3-2.9bpw afc7aa0aab98ee735c0f54e0a122019af1ac08f1)}"
  42. EREV="${TF1017_ENGRAM_REV:-$(pin_or_ref models--deepseek-ai--DeepSeek-V4.1-Flash-1017 1bae0e1088993b9b82de08f59de5be5a34e42170)}"
  43. WBUNDLE="$NAS/models--LibertAIDAI--DeepSeek-V4.1-Flash-1017-EXL3-2.9bpw/snapshots/$REV"
  44. EBUNDLE="$NAS/models--deepseek-ai--DeepSeek-V4.1-Flash-1017/snapshots/$EREV"
  45. [[ -d "$WBUNDLE" ]] || { echo "[tf1017] FATAL: no checkpoint snapshot at $WBUNDLE" >&2; exit 1; }
  46. [[ -d "$EBUNDLE" ]] || { echo "[tf1017] FATAL: no Engram source snapshot at $EBUNDLE" >&2; exit 1; }
  47. echo "[tf1017] checkpoint rev $REV, Engram rev $EREV"
  48. CKPT="$ROOT/models/checkpoint"
  49. PACKED="$ROOT/engram-packed-1017"
  50. CACHE="$ROOT/cache"
  51. HELP="$ROOT/helpers"
  52. mkdir -p "$CKPT" "$PACKED" "$CACHE" "$HELP" "$ROOT/state"
  53. echo "[tf1017] rank $RANK — root $ROOT"
  54. # 0. helper files (published to the NAS by the image's build.sh) -------------
  55. for f in pack_engram.py prebuild_ext.py prebuild-offline.py pfdense-table.json \
  56. chat_template.jinja; do
  57. if [[ ! -s "$HELP/$f" ]]; then
  58. if [[ -s "$PUB/$f" ]]; then
  59. cp -f "$PUB/$f" "$HELP/$f"
  60. elif docker image inspect "$IMG" >/dev/null 2>&1; then
  61. docker run --rm --network none --entrypoint cat "$IMG" "/opt/sm121-helpers/$f" > "$HELP/$f"
  62. else
  63. echo "[tf1017] FATAL: $f not published at $PUB and image $IMG absent" >&2
  64. exit 1
  65. fi
  66. fi
  67. done
  68. # 1. weights -----------------------------------------------------------------
  69. if [[ "${TF1017_SKIP_WEIGHTS:-0}" != "1" ]]; then
  70. have=$(find "$CKPT" -maxdepth 1 -name '*.safetensors' | wc -l)
  71. if [[ "$have" -lt 43 ]]; then
  72. echo "[tf1017] copying EXL3 checkpoint from the NAS ($have/43 shards present)"
  73. tar -chf - -C "$WBUNDLE" . | tar -xf - -C "$CKPT"
  74. fi
  75. have=$(find "$CKPT" -maxdepth 1 -name '*.safetensors' | wc -l)
  76. [[ "$have" -ge 43 ]] || { echo "[tf1017] FATAL: checkpoint incomplete ($have/43)" >&2; exit 1; }
  77. fi
  78. # 2. chat template -----------------------------------------------------------
  79. [[ -s "$CKPT/chat_template.jinja" ]] || cp "$HELP/chat_template.jinja" "$CKPT/chat_template.jinja"
  80. # 3. per-rank Engram packs (host python + numpy; no image needed) ------------
  81. if [[ "${TF1017_SKIP_PACK:-0}" != "1" ]]; then
  82. if [[ ! -s "$PACKED/engram-l14-r${RANK}of2.bin" ]]; then
  83. echo "[tf1017] packing Engram tables for rank $RANK (~94 GiB, reads 189 GiB from the NAS)"
  84. if [[ "${TF1017_PACK_IN_IMAGE:-1}" = "1" ]] && docker image inspect "$IMG" >/dev/null 2>&1; then
  85. # Prefer the image: it has numpy + the exact python the packer was
  86. # written against. Host python is PEP-668 managed on dgx7/8
  87. # (`error: externally-managed-environment`) — no host installs.
  88. # Mount the whole repo dir (`snapshots/` + `blobs/`), not just the
  89. # snapshot: the snapshot files are symlinks into the hub's blobs,
  90. # so a snapshot-only mount makes model.safetensors.index.json
  91. # dangle inside the container.
  92. # Mount the whole hub: snapshot files are symlinks to the
  93. # per-model blobs, which are themselves symlinks to hub-level
  94. # blobs (huggingface_hub >= 0.34 layout) — anything less leaves
  95. # model.safetensors.index.json dangling in the container.
  96. docker run --rm --network none --entrypoint python3 \
  97. -v "$NAS":/hub:ro -v "$CKPT":/ckpt:ro -v "$PACKED":/out \
  98. -v "$HELP/pack_engram.py":/pack_engram.py:ro \
  99. "$IMG" /pack_engram.py --src "/hub/${EBUNDLE#"$NAS"/}" \
  100. --config /ckpt/config.json --out /out --rank "$RANK"
  101. else
  102. python3 -c 'import numpy' 2>/dev/null \
  103. || python3 -m pip install --user --quiet --break-system-packages numpy \
  104. || { echo "[tf1017] FATAL: numpy missing for the Engram packer and pip refused (no image on this node yet)" >&2; exit 1; }
  105. python3 "$HELP/pack_engram.py" \
  106. --src "$EBUNDLE" --config "$CKPT/config.json" \
  107. --out "$PACKED" --rank "$RANK"
  108. fi
  109. fi
  110. [[ -s "$PACKED/engram-l1-r${RANK}of2.bin" && -s "$PACKED/engram-l14-r${RANK}of2.bin" ]] \
  111. || { echo "[tf1017] FATAL: Engram packs missing for rank $RANK" >&2; exit 1; }
  112. fi
  113. # 4. CUDA extension prebuild (needs the image; skipped on a first serve) -----
  114. if [[ "${TF1017_SKIP_PREBUILD:-0}" != "1" && ! -s "$CACHE/pfdense-table.json" ]]; then
  115. if docker image inspect "$IMG" >/dev/null 2>&1; then
  116. echo "[tf1017] prebuilding CUDA extensions (CPU-only)"
  117. docker run --rm --network none --memory 8g --cpus 4 \
  118. -e CUDA_VISIBLE_DEVICES= -e TORCH_CUDA_ARCH_LIST=12.1 -e MAX_JOBS=4 \
  119. -e TORCH_EXTENSIONS_DIR=/cache/torch_extensions \
  120. -v "$CACHE":/cache \
  121. -v "$HELP/prebuild-offline.py":/offline.py:ro \
  122. -v "$HELP/prebuild_ext.py":/prebuild.py:ro \
  123. -v "$HELP/pfdense-table.json":/table.json:ro \
  124. --entrypoint bash "$IMG" \
  125. -c 'python /offline.py && cp /table.json /cache/pfdense-table.json'
  126. else
  127. echo "[tf1017] image $IMG not on this node yet — extensions will compile at first start"
  128. fi
  129. fi
  130. echo "[tf1017] rank $RANK ready"