#!/usr/bin/env bash # Headless CPU-pod worker: validate the mixed-Q2 artifact and pack it into GGUF. # No SSH required: everything is driven by this script and evidence is pushed to Hugging Face. # # Inputs (environment): # HF_TOKEN token with write access to EVIDENCE_REPO # EVIDENCE_REPO default apetersson/v41-quant-worker (dataset) # ARTIFACT default /workspace/v41-quant/q2-iq2-20260910 # OUT_DIR default /workspace/v41-quant/gguf-mixedq2 # SOURCE_DIR default /workspace/v41-download/models/DeepSeek-V4.1-Flash/2bc89ac599031fa673cab993f1df02fc4a98c673 # LLAMA_PR_REF default refs/pull/28696/head (V4.1 converter/gguf-py) set -uo pipefail trap 'echo "worker aborted rc=$? at line $LINENO"' ERR STAMP=$(date -u +%Y%m%dT%H%M%SZ) WORK=${WORK:-/workspace/v41-quant} LOGDIR="$WORK/logs" mkdir -p "$LOGDIR" LOG="$LOGDIR/bootstrap-$STAMP.log" exec > >(tee -a "$LOG") 2>&1 echo "=== v41 quant worker start $STAMP" EVIDENCE_REPO=${EVIDENCE_REPO:-apetersson/v41-quant-worker} ARTIFACT=${ARTIFACT:-$WORK/q2-iq2-20260910} OUT_DIR=${OUT_DIR:-$WORK/gguf-mixedq2} SOURCE_DIR=${SOURCE_DIR:-/workspace/v41-download/models/DeepSeek-V4.1-Flash/2bc89ac599031fa673cab993f1df02fc4a98c673} LLAMA_PR_REF=${LLAMA_PR_REF:-refs/pull/28696/head} LLAMA_DIR=/opt/llama.cpp STATUS="$WORK/status.json" PYTHONPATH_HINT="$LLAMA_DIR/gguf-py" export PYTHONPATH="$PYTHONPATH_HINT" # audit/mapping/pack all import gguf-py (PR #28696 head) status() { # phase, detail python3 - "$STATUS" "$1" "$2" "$ARTIFACT" "$OUT_DIR" <<'PY' import json, sys, time, os path, phase, detail, artifact, out = sys.argv[1:6] data = {} if os.path.exists(path): data = json.load(open(path)) data.update(phase=phase, detail=detail, updated_at=time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), artifact=artifact, out_dir=out) json.dump(data, open(path, "w"), indent=1) PY echo "--- phase: $1 ($2)" } push_evidence() { # file... [ -n "${HF_TOKEN:-}" ] || { echo "no HF_TOKEN; skipping evidence push"; return 0; } for f in "$@"; do [ -f "$f" ] || continue python3 - "$f" "$EVIDENCE_REPO" "$STAMP" <<'PY' import sys, os from huggingface_hub import HfApi path, repo, stamp = sys.argv[1:4] api = HfApi(token=os.environ["HF_TOKEN"]) api.upload_file(path_or_fileobj=path, path_in_repo=f"runs/{stamp}/{os.path.basename(path)}", repo_id=repo, repo_type="dataset", commit_message=f"v41 quant worker: {os.path.basename(path)}") print("pushed", os.path.basename(path)) PY done } status setup "installing toolchain" export DEBIAN_FRONTEND=noninteractive apt-get update -qq && apt-get install -y -qq --no-install-recommends \ git cmake g++ make curl ca-certificates python3-pip procps >/tmp/apt.log 2>&1 python3 -m pip install --quiet --no-cache-dir numpy pyyaml tqdm huggingface_hub status bundle "fetching worker bundle" curl -fsSL --retry 6 --retry-delay 5 --retry-all-errors -H "Authorization: Bearer ${HF_TOKEN:-}" -o /tmp/v41-runtimes.tgz "https://huggingface.co/datasets/$EVIDENCE_REPO/resolve/main/v41-runtimes-20260910T1240.tgz" \ && tar xzf /tmp/v41-runtimes.tgz -C "$WORK" --warning=no-unknown-keyword SCRIPTS="$WORK/scripts" ls "$SCRIPTS" >/dev/null || { echo "worker bundle missing $SCRIPTS"; exit 3; } status llama "cloning llama.cpp $LLAMA_PR_REF" rm -rf "$LLAMA_DIR"; mkdir -p "$LLAMA_DIR" git -C "$LLAMA_DIR" init -q . git -C "$LLAMA_DIR" remote add origin https://github.com/ggml-org/llama.cpp.git git -C "$LLAMA_DIR" fetch -q --depth 1 origin "$LLAMA_PR_REF" git -C "$LLAMA_DIR" checkout -q FETCH_HEAD git -C "$LLAMA_DIR" log --oneline -1 | tee "$WORK/llama-ref.txt" status ggml "building ggml (CPU)" cmake -S "$LLAMA_DIR" -B "$LLAMA_DIR/build" -DCMAKE_BUILD_TYPE=Release \ -DBUILD_SHARED_LIBS=ON -DLLAMA_BUILD_TESTS=OFF -DLLAMA_BUILD_EXAMPLES=OFF \ -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_SERVER=OFF >/tmp/cmake.log 2>&1 \ && cmake --build "$LLAMA_DIR/build" --target ggml -j"$(nproc)" >/tmp/build.log 2>&1 echo "ggml build rc=$?" status verify "verifying artifact digests" python3 "$SCRIPTS/v41_quant_experts.py" verify --out "$ARTIFACT" --sample 0 | tee "$WORK/verify.txt" VERIFY_RC=${PIPESTATUS[0]} status audit "fidelity audit" python3 "$SCRIPTS/v41_quant_audit.py" --source "$SOURCE_DIR" --out "$ARTIFACT" \ --per-projection 8 --json "$WORK/audit.json" 2>&1 | tail -30 || echo "audit failed (non-fatal)" push_evidence "$WORK/audit.json" "$WORK/verify.txt" "$WORK/llama-ref.txt" status mapping "gguf mapping report" PYTHONPATH="$PYTHONPATH_HINT" python3 "$SCRIPTS/v41_quant_to_gguf.py" --artifact "$ARTIFACT" \ --config "$SOURCE_DIR/config.json" --check-mapping > "$WORK/gguf-pack-report.json" 2>&1 MAP_RC=$? echo "mapping rc=$MAP_RC"; tail -25 "$WORK/gguf-pack-report.json" push_evidence "$WORK/gguf-pack-report.json" if [ "$MAP_RC" -ne 0 ]; then status failed "mapping incomplete" push_evidence "$LOG" "$STATUS" exit 4 fi status pack "writing GGUF" PYTHONPATH="$PYTHONPATH_HINT" TMPDIR="$WORK/tmp" python3 "$SCRIPTS/v41_quant_to_gguf.py" \ --artifact "$ARTIFACT" --config "$SOURCE_DIR/config.json" --out-dir "$OUT_DIR" \ --split-gb "${SPLIT_GB:-40}" | tee "$WORK/pack.txt" PACK_RC=${PIPESTATUS[0]} status sums "hashing shards" ( cd "$OUT_DIR" && sha256sum ./*.gguf > SHA256SUMS && wc -l SHA256SUMS ) | tee -a "$WORK/pack.txt" python3 - "$OUT_DIR" "$WORK/gguf-summary.json" <<'PY' import glob, json, os, sys out, dest = sys.argv[1:3] files = sorted(glob.glob(os.path.join(out, "*.gguf"))) record = { "files": [{"name": os.path.basename(f), "bytes": os.path.getsize(f)} for f in files], "total_bytes": sum(os.path.getsize(f) for f in files), "shards": len(files), } json.dump(record, open(dest, "w"), indent=1) print(json.dumps({k: v for k, v in record.items() if k != "files"}, indent=1)) PY push_evidence "$WORK/pack.txt" "$WORK/gguf-summary.json" "$OUT_DIR/gguf-validation.json" "$OUT_DIR/SHA256SUMS" status "${PACK_RC:0:1}${VERIFY_RC:0:1}" "pack rc=$PACK_RC verify rc=$VERIFY_RC" if [ "$PACK_RC" -eq 0 ]; then status done "gguf pack complete"; else status failed "pack rc=$PACK_RC"; fi push_evidence "$LOG" "$STATUS" echo "=== worker finished"