REAX-1.4 / engine /bench /knowline_parity.sh
decisionmodels's picture
Engine tree (REAX Engine reax14-release-20261126, 2cccfee) and run instructions
a7e82fd verified
Raw History Blame Contribute Delete
12.4 kB
#!/usr/bin/env bash
# GPU parity run of the KnowLine-31B front ends (docs/models/knowline-31b.md) on ONE 96 GB card, one weight group per pod.
# Runs INSIDE an environment with python3, torch, transformers (5.12.1 = the authors' pin), accelerate, vllm 0.30.0,
# huggingface_hub; it rents, stops and deletes nothing on the provider side. One phase per call, or a whole group:
#
# WORK=/workspace/kl DATA_P300=/data/p300 [GATES=/workspace/gates.py] bash bench/knowline_parity.sh <phase> [arg]
# phases: env | fetch <gen1|gen2|copernicus> | render-check <template> | hf-ref <template> <bf16|bf16-fp32head|fp32>
# | arm <template> | gates <template> | group <A|B|C>
# groups: A = KnowLine-31B-Gen1 weights: templates knowline31-gen1 and knowline31-gen1.5 (same files)
# B = KnowLine-31B-Gen2 weights: knowline31-gen2
# C = Copernicus-31B weights: copernicus31
# DATA_P300 dir with p300-input.jsonl (+ p300-gold.jsonl): public open split of JevBench v1.6 -> class C0
# Knobs: PY (python3) DRY_RUN=1 (print, run nothing) OFFLOAD_GPU_GIB (86) OFFLOAD_CPU_GIB (34): fp32 reference with CPU offload
# Servers: vLLM on :8000 (preset bf16), REAX server on :8741, token S1M_INTERNAL_TOKEN=parity, loopback only.
set -euo pipefail
PHASE=${1:?usage: bench/knowline_parity.sh <phase> [arg]}; ARG=${2:-}; ARG2=${3:-}
ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)
WORK=${WORK:-/workspace/kl}; OUT=$WORK/out; REF=$WORK/ref; PY=${PY:-python3}; DRY=${DRY_RUN:-0}
VPORT=8000; SPORT=8741; URL=http://127.0.0.1:$SPORT
export S1M_INTERNAL_TOKEN=parity HF_HUB_DISABLE_TELEMETRY=1 VLLM_NO_USAGE_STATS=1 DO_NOT_TRACK=1
cd "$ROOT"; export PYTHONPATH="$ROOT${PYTHONPATH:+:$PYTHONPATH}"
log() { echo "[$(date -u +%T)] $*"; }
run() { if [ "$DRY" = 1 ]; then echo "+ $*"; else "$@"; fi; }
need() { [ -n "${!1:-}" ] || { echo "set $1 (see the header of $0)"; exit 2; }; }
mkdir -p "$OUT" "$REF"
weights_of() { # template -> weights dir
case "$1" in
knowline31-gen1|knowline31-gen1.5) echo "$WORK/weights/knowline-31b-gen1" ;;
knowline31-gen2) echo "$WORK/weights/knowline-31b-gen2" ;;
copernicus31) echo "$WORK/weights/copernicus-31b" ;;
*) echo "unknown template $1" >&2; exit 2 ;;
esac
}
model_of() { # template -> registry id
case "$1" in
knowline31-gen1) echo knowline-31b-gen1 ;; knowline31-gen1.5) echo knowline-31b-gen1.5 ;;
knowline31-gen2) echo knowline-31b-gen2 ;; copernicus31) echo copernicus-31b ;;
esac
}
# ------------------------------------------------------------------------------------------------ servers
VLLM_PID=""; SRV_PID=""; MEM_PID=""
stop_servers() {
[ -n "$MEM_PID" ] && kill "$MEM_PID" 2>/dev/null || true
[ -n "$SRV_PID" ] && kill "$SRV_PID" 2>/dev/null || true
[ -n "$VLLM_PID" ] && kill "$VLLM_PID" 2>/dev/null || true
[ -n "$SRV_PID$VLLM_PID" ] && wait 2>/dev/null || true
VLLM_PID=""; SRV_PID=""; MEM_PID=""
}
trap stop_servers EXIT
wait_http() { # url seconds pid
[ "$DRY" = 1 ] && return 0
for _ in $(seq 1 $(($2 / 3))); do
curl -sf -m 3 "$1/health" >/dev/null && return 0
kill -0 "$3" 2>/dev/null || { echo "process $3 died while waiting for $1"; return 3; }
sleep 3
done
echo "$1 not healthy after $2 s"; return 4
}
start_vllm() { # template dir tag
local fam=$1 dir=$2 tag=$3
log "vLLM: $fam bf16 $dir"
if [ "$DRY" = 1 ]; then echo "+ $PY -m engine.backends.vllm_readout $fam bf16 $dir --port $VPORT --exec"; return 0; fi
( cd "$ROOT" && exec env PYTHONPATH="$ROOT" "$PY" -m engine.backends.vllm_readout "$fam" bf16 "$dir" --port $VPORT --exec ) > "$OUT/vllm-$tag.log" 2>&1 &
VLLM_PID=$!
wait_http "http://127.0.0.1:$VPORT" 2400 "$VLLM_PID"
( while :; do nvidia-smi -i 0 --query-gpu=memory.used --format=csv,noheader,nounits; sleep 2; done ) > "$OUT/mem-$tag.csv" 2>/dev/null &
MEM_PID=$!
}
start_reax() { # model dir tag
local model=$1 dir=$2 tag=$3
log "REAX server: --model $model"
if [ "$DRY" = 1 ]; then echo "+ REAX_VLLM_BASE_URL=http://127.0.0.1:$VPORT $PY -m engine.serve --model $model --model-dir $dir --port $SPORT --opt preset=bf16"; return 0; fi
( cd "$ROOT" && exec env PYTHONPATH="$ROOT" REAX_VLLM_BASE_URL="http://127.0.0.1:$VPORT" "$PY" -m engine.serve --model "$model" --model-dir "$dir" \
--host 127.0.0.1 --port $SPORT --opt preset=bf16 ) > "$OUT/reax-$tag.log" 2>&1 &
SRV_PID=$!
wait_http "$URL" 1800 "$SRV_PID"
}
stop_reax() { [ -n "$SRV_PID" ] && kill "$SRV_PID" 2>/dev/null && wait "$SRV_PID" 2>/dev/null || true; SRV_PID=""; }
peak_mem() { [ -f "$OUT/mem-$1.csv" ] && sort -n "$OUT/mem-$1.csv" | tail -1 > "$OUT/peak-mem-$1.txt" || true; }
# ------------------------------------------------------------------------------------------------ phases
phase_env() {
log "environment"
if [ "$DRY" = 1 ]; then echo "+ record versions to $OUT/env.json"; return 0; fi
nvidia-smi -L | grep -q . || { echo "no GPU visible"; exit 2; }
local free; free=$(df -BG --output=avail "$WORK" | tail -1 | tr -dc 0-9)
[ "$free" -ge 150 ] || { echo "need >= 150 GB free in $WORK (62.5 GB checkpoint + fp32 offload + caches), have ${free} GB"; exit 2; }
"$PY" - "$ROOT" "$OUT/env.json" <<'EOF'
import importlib.metadata as m, json, platform, subprocess, sys
def v(p):
try: return m.version(p)
except m.PackageNotFoundError: return None
gpu = subprocess.run(["nvidia-smi", "--query-gpu=name,memory.total,driver_version", "--format=csv,noheader"], capture_output=True, text=True).stdout.strip()
rev = subprocess.run(["git", "-C", sys.argv[1], "rev-parse", "HEAD"], capture_output=True, text=True).stdout.strip() or "unknown"
env = {"gpu": gpu, "python": platform.python_version(), "repo_rev": rev,
**{p: v(p) for p in ("torch", "transformers", "vllm", "accelerate", "huggingface_hub", "tokenizers")}}
json.dump(env, open(sys.argv[2], "w"), indent=1); print(json.dumps(env))
EOF
}
phase_fetch() { # gen1 | gen2 | copernicus (gen1 also serves knowline31-gen1.5: the weight files are the same)
local repo rev man dir
case "$ARG" in
gen1) repo=PelaAI/KnowLine-31B-Gen1; rev=7d21f7832d0db289f533cd0c22b2cc604cd925d5; man=$ROOT/manifests/knowline-31b-gen1-7d21f783.sha256 ;;
gen2) repo=PelaAI/KnowLine-31B-Gen2; rev=fd99a9ed2255d8a67dd47a44cc429e638f611593; man=$ROOT/manifests/knowline-31b-gen2-fd99a9ed.sha256 ;;
copernicus) repo=Poindexter-Labs/Copernicus-31B; rev=ae28efb0ae759c3494a1078c0a56eda10a9adb38; man=$ROOT/manifests/copernicus-31b-ae28efb0.sha256 ;;
*) echo "fetch gen1|gen2|copernicus"; exit 2 ;;
esac
case "$ARG" in gen1) dir=$WORK/weights/knowline-31b-gen1 ;; gen2) dir=$WORK/weights/knowline-31b-gen2 ;; copernicus) dir=$WORK/weights/copernicus-31b ;; esac
[ -f "$dir/.complete-$rev" ] && { log "$repo already verified"; return 0; }
log "download $repo @ $rev -> $dir (only the files of the manifest: weights, config, tokenizer, chat template; NO author .py files)"
run mkdir -p "$dir"
run "$PY" -c "
import sys
from huggingface_hub import snapshot_download
names = [l.split(None, 1)[1].strip() for l in open(sys.argv[3]) if l.strip()]
snapshot_download(sys.argv[1], revision=sys.argv[2], local_dir=sys.argv[4], allow_patterns=names)" "$repo" "$rev" "$man" "$dir"
if [ "$DRY" = 1 ]; then echo "+ (cd $dir && sha256sum --check --strict --quiet $man)"; return 0; fi
( cd "$dir" && sha256sum --check --strict --quiet "$man" )
extra=$(cd "$dir" && find . -type f -not -path './.cache/*' -not -name '.complete-*' | sed 's|^\./||' | sort | comm -13 <(awk '{print $2}' "$man" | sort) -)
[ -z "$extra" ] || { echo "unlisted files in $dir: $extra"; exit 3; }
touch "$dir/.complete-$rev"
}
phase_render_check() { # template
need DATA_P300
local t=$ARG
log "render check (CPU): reference (release functions) vs engine, $t"
run "$PY" -m bench.knowline_reference render-check --tokenizer "$(weights_of "$t")" --frontend "$t" --set "p300=$DATA_P300" --out "$OUT/render-check-$t.json"
}
phase_hf_ref() { # template bf16|bf16-fp32head|fp32
need DATA_P300
local t=$ARG mode=${ARG2:-bf16} extra=()
case "$mode" in
bf16) extra=(--dtype bfloat16) ;;
bf16-fp32head) extra=(--dtype bfloat16 --fp32-head) ;; # = SGLang --enable-fp32-lm-head of the KnowLine releases
fp32) extra=(--dtype float32 --offload-gpu-gib "${OFFLOAD_GPU_GIB:-86}" --offload-cpu-gib "${OFFLOAD_CPU_GIB:-34}" --offload-dir "$WORK/offload") ;;
*) echo "hf-ref <template> bf16|bf16-fp32head|fp32"; exit 2 ;;
esac
if [ "$t" = knowline31-gen1.5 ] && [ -f "$REF/ref-knowline31-gen1-$mode.jsonl" ]; then
extra+=(--copy-unchanged-from "$REF/ref-knowline31-gen1-$mode.jsonl" --base-frontend knowline31-gen1) # rows with identical prompts
fi
log "HF transformers reference ($mode, batch 1): $t"
run "$PY" -m bench.knowline_reference hf-run --frontend "$t" --model-dir "$(weights_of "$t")" --set p300 --data-dir "$DATA_P300" \
"${extra[@]}" --out "$REF/ref-$t-$mode.jsonl"
}
phase_arm() { # template: engine bf16 at concurrency 1 vs the HF bf16 reference, then engine c8 vs c1
need DATA_P300
local t=$ARG tag="arm-$ARG"; mkdir -p "$OUT/$tag"
start_vllm "$t" "$(weights_of "$t")" "$tag"
start_reax "$(model_of "$t")" "$(weights_of "$t")" "$tag"
log "$tag: P300 HF bf16 reference vs engine, concurrency 1"
run "$PY" -m bench.agreement --ref "$REF/ref-$t-bf16.jsonl" --cand "$URL" --contract s1 --set p300 --data-dir "$DATA_P300" \
--workers 1 --cand-cache "$OUT/$tag/cand-p300-c1.jsonl" --out "$OUT/$tag/agreement-p300-hf-vs-engine-c1.json"
log "$tag: engine concurrency 8 vs concurrency 1"
run "$PY" -m bench.agreement --ref "$OUT/$tag/cand-p300-c1.jsonl" --cand "$URL" --contract s1 --set p300 --data-dir "$DATA_P300" \
--workers 8 --cand-cache "$OUT/$tag/cand-p300-c8.jsonl" --out "$OUT/$tag/agreement-p300-engine-c8-vs-c1.json"
log "$tag: /v1/systemone answer shape (5 items)"
[ "$DRY" = 1 ] || head -n 5 "$DATA_P300/p300-input.jsonl" | "$PY" -c "
import json, sys, urllib.request
for l in sys.stdin:
r = json.loads(l)
body = json.dumps({'state': r['state'], 'questions': {'q': r['question']}}).encode()
req = urllib.request.Request('$URL/v1/systemone', body, {'Content-Type': 'application/json', 'X-S1M-Internal-Token': 'parity'})
print(json.dumps(json.loads(urllib.request.urlopen(req, timeout=120).read())['answers']['q']))" > "$OUT/$tag/systemone-sample.jsonl"
run "$PY" -m bench.loadgen --url "$URL" --contract s1 --set p300 --data-dir "$DATA_P300" --concurrency 1,8 --seconds 60 --out "$OUT/$tag/load-p300.json"
peak_mem "$tag"; stop_reax; stop_servers
}
phase_gates() { # template: adopted gates (agreement >= self-noise - 1 pp, flips <= self-noise, acc lower bound >= -2 pp); fp32 adjudicates
need DATA_P300; need GATES
local t=$ARG c=$OUT/arm-$ARG/cand-p300-c1.jsonl
run "$PY" "$GATES" --repo "$ROOT" --data "$DATA_P300" --ref "$REF/ref-$t-bf16.jsonl" --ref2 "$REF/ref-$t-bf16-fp32head.jsonl" \
--cand "engine-bf16-c1=$c" --json "$OUT/gates-$t-vs-hf-bf16.json" | tee "$OUT/gates-$t-vs-hf-bf16.txt"
if [ -f "$REF/ref-$t-fp32.jsonl" ] || [ "$DRY" = 1 ]; then
run "$PY" "$GATES" --repo "$ROOT" --data "$DATA_P300" --ref "$REF/ref-$t-fp32.jsonl" --ref2 "$REF/ref-$t-bf16.jsonl" \
--cand "engine-bf16-c1=$c" --cand "hf-bf16-fp32head=$REF/ref-$t-bf16-fp32head.jsonl" --json "$OUT/gates-$t-vs-hf-fp32.json" | tee "$OUT/gates-$t-vs-hf-fp32.txt"
fi
}
group() { # A | B | C
local fetch templates t
case "$ARG" in
A) fetch=gen1; templates="knowline31-gen1 knowline31-gen1.5" ;;
B) fetch=gen2; templates="knowline31-gen2" ;;
C) fetch=copernicus; templates="copernicus31" ;;
*) echo "group A|B|C"; exit 2 ;;
esac
phase_env
ARG=$fetch phase_fetch
for t in $templates; do ARG=$t phase_render_check; done
for t in $templates; do ARG=$t; ARG2=bf16 phase_hf_ref; ARG2=bf16-fp32head phase_hf_ref; done
for t in $templates; do ARG=$t phase_arm; done
for t in $templates; do ARG=$t; ARG2=fp32 phase_hf_ref; done # longest step (CPU offload); runs last so the gates above exist early
for t in $templates; do ARG=$t phase_gates || true; done
}
case "$PHASE" in
env) phase_env ;;
fetch) phase_fetch ;;
render-check) phase_render_check ;;
hf-ref) phase_hf_ref ;;
arm) phase_arm ;;
gates) phase_gates ;;
group) group ;;
*) echo "unknown phase $PHASE"; exit 2 ;;
esac
log "phase $PHASE done"