#!/usr/bin/env bash # GPU worker, phase 1: build ggml with CUDA and execute the real IQ2_XXS/Q2_K expert blocks on the card. # # Image: a CUDA devel image (nvcc required). Input preference: # 1. the mounted safetensors artifact (needs the Runpod network volume, which is # datacenter-local and single-attach), # 2. a packed GGUF already in the workspace, # 3. the first published GGUF shard from Hugging Face (works on any DC). set -uo pipefail trap 'echo "gpu-kernel 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" exec > >(tee -a "$LOGDIR/gpu-kernel-$STAMP.log") 2>&1 ARCH=${CUDA_ARCH:-100a} EVIDENCE_REPO=${EVIDENCE_REPO:-apetersson/v41-quant-worker} GGUF_REPO=${GGUF_REPO:-apetersson/DeepSeek-V4.1-Flash-MixedQ2-GGUF} BUNDLE=${BUNDLE:-v41-runtimes-20260910T1240.tgz} ARTIFACT=${ARTIFACT:-$WORK/q2-iq2-20260910} OUT=${OUT:-$WORK/gpu-kernel-$STAMP} mkdir -p "$OUT" echo "=== gpu kernel worker $STAMP arch=$ARCH work=$WORK" nvidia-smi -L || echo "no GPU visible" nvidia-smi --query-gpu=name,memory.total,compute_cap --format=csv,noheader || true export DEBIAN_FRONTEND=noninteractive apt-get update -qq && apt-get install -y -qq --no-install-recommends \ git cmake g++ make ca-certificates curl python3-pip >/tmp/apt.log 2>&1 python3 -m pip install --quiet --no-cache-dir --break-system-packages numpy pyyaml tqdm huggingface_hub if [ -f "$WORK/scripts/v41_quant_kernel_smoke.py" ]; then echo "--- code already staged in $WORK (no HF fetch on the GPU host)" else curl -fsSL --retry 10 --retry-delay 20 --retry-all-errors -H "Authorization: Bearer ${HF_TOKEN:-}" \ -o /tmp/runtimes.tgz "https://huggingface.co/datasets/$EVIDENCE_REPO/resolve/main/$BUNDLE" \ && tar xzf /tmp/runtimes.tgz -C "$WORK" --warning=no-unknown-keyword fi SCRIPTS="$WORK/scripts" LLAMA=/opt/llama.cpp rm -rf "$LLAMA"; mkdir -p "$LLAMA"; git -C "$LLAMA" init -q . git -C "$LLAMA" remote add origin https://github.com/ggml-org/llama.cpp.git git -C "$LLAMA" fetch -q --depth 1 origin refs/pull/28696/head git -C "$LLAMA" checkout -q FETCH_HEAD echo "llama.cpp: $(git -C "$LLAMA" log --oneline -1)" export PYTHONPATH="$LLAMA/gguf-py" echo "--- building ggml with CUDA (arch $ARCH)" cmake -S "$LLAMA" -B "$LLAMA/build" -DCMAKE_BUILD_TYPE=Release -DGGML_CUDA=ON -DGGML_NATIVE=OFF \ -DCMAKE_CUDA_ARCHITECTURES="$ARCH" -DBUILD_SHARED_LIBS=ON -DLLAMA_BUILD_TESTS=OFF \ -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TOOLS=OFF -DLLAMA_BUILD_SERVER=OFF >"$OUT/cmake.log" 2>&1 cmake --build "$LLAMA/build" --target ggml -j"$(nproc)" >"$OUT/ggml-cuda-build.log" 2>&1 echo "ggml cuda build rc=$?" cc -O2 "$WORK/runtimes/v41-quant/ggml-cuda-smoke/kernel_smoke.c" -o /usr/local/bin/kernel_smoke \ -I"$LLAMA/ggml/include" -L"$LLAMA/build/bin" -lggml -lggml-base -lggml-cpu \ -Wl,-rpath,"$LLAMA/build/bin" echo "harness compile rc=$?" # Input selection ------------------------------------------------------------- SMOKE_INPUT=() if [ -f "$ARTIFACT/plan.json" ]; then SMOKE_INPUT=(--artifact "$ARTIFACT"); echo "--- input: mounted artifact" else EXISTING=$(ls -d "$WORK"/gguf-publish "$WORK"/gguf-mixedq2 2>/dev/null | head -1 || true) if [ -n "$EXISTING" ] && ls "$EXISTING"/*.gguf >/dev/null 2>&1; then SMOKE_INPUT=(--gguf "$EXISTING"); echo "--- input: workspace GGUF $EXISTING" else echo "--- input: waiting for the first GGUF shard of $GGUF_REPO" GGUF_DIR="$WORK/gguf-remote"; mkdir -p "$GGUF_DIR" for attempt in $(seq 1 80); do if python3 - "$GGUF_REPO" "$GGUF_DIR" <<'PYEOF' import os, sys from huggingface_hub import HfApi, hf_hub_download repo, dest = sys.argv[1:3] files = sorted(f for f in HfApi(token=os.environ.get("HF_TOKEN")).list_repo_files(repo) if f.endswith(".gguf")) if not files: print("no shard published yet"); sys.exit(1) hf_hub_download(repo, files[0], local_dir=dest, token=os.environ.get("HF_TOKEN")) print("downloaded", files[0]) PYEOF then break; fi sleep 30 done SMOKE_INPUT=(--gguf "$GGUF_DIR") fi fi echo "--- kernel smoke on real blocks" python3 "$SCRIPTS/v41_quant_kernel_smoke.py" "${SMOKE_INPUT[@]}" \ --kernel-bin /usr/local/bin/kernel_smoke --tensors 6 \ --json "$OUT/kernel-smoke-cuda.json" 2>&1 | tee "$OUT/kernel-smoke-cuda.txt" KERNEL_RC=${PIPESTATUS[0]} python3 - "$OUT" <<'PYEOF' import json, os, sys out = sys.argv[1] path = os.path.join(out, "kernel-smoke-cuda.json") if not os.path.exists(path): print(json.dumps({"status": "no result file"})); raise SystemExit(0) rec = json.load(open(path)) print(json.dumps({ "kernel_agrees": rec.get("kernel_agrees_with_dequantised_reference"), "cosines": [t.get("cosine_vs_reference") for t in rec["tensors"]], "rel_rms": [t.get("relative_rms_vs_reference") for t in rec["tensors"]], "bandwidth_GiB_s": [t.get("weight_bandwidth_GiB_per_s") for t in rec["tensors"]], }, indent=1)) PYEOF # which backend actually ran (a silent CPU fallback must not be read as a GPU result) grep -h "backend:" "$OUT"/kernel-smoke-cuda.txt 2>/dev/null | sort -u || true if [ -n "${HF_TOKEN:-}" ]; then for f in "$OUT/kernel-smoke-cuda.json" "$OUT/kernel-smoke-cuda.txt" \ "$OUT/ggml-cuda-build.log" "$LOGDIR/gpu-kernel-$STAMP.log"; do [ -f "$f" ] || continue python3 - "$f" "$EVIDENCE_REPO" "$STAMP" <<'PYEOF' import os, sys from huggingface_hub import HfApi path, repo, stamp = sys.argv[1:4] HfApi(token=os.environ["HF_TOKEN"]).upload_file( path_or_fileobj=path, path_in_repo=f"runs/{stamp}/gpu-{os.path.basename(path)}", repo_id=repo, repo_type="dataset", commit_message=f"gpu kernel evidence {os.path.basename(path)}") print("pushed", os.path.basename(path)) PYEOF done fi echo "=== gpu kernel worker finished rc=$KERNEL_RC"