StepProbe / run_multi_seed.sh
Akiyue's picture
Add files using upload-large-folder tool
1e59964 verified
Raw
History Blame Contribute Delete
7.27 kB
#!/usr/bin/env bash
###############################################################################
# StepProbe — multi-seed robustness check.
#
# The main paper reports per-problem bootstrap CIs from a single greedy seed.
# Reviewers at NeurIPS/ICLR tier routinely ask for seed-level variance too.
# This script runs 3 seeds at temperature 0.6 on a primary configuration and
# plots cross-seed accuracy (mean ± std) next to a paired-bootstrap CI, so
# readers can compare within-cell variance (bootstrap) against across-seed
# variance (sampling).
#
# Scope is intentionally tight — primary model, primary benchmark, one method
# on both base and restored. Expected runtime: ~2-2.5 h on a 3090 Ti.
#
# Override via env:
# MODEL_TAG / MODEL_HF / QUANT_TAG / BENCHMARK / SEEDS
###############################################################################
set -euo pipefail
PROJECT_DIR="$(cd "$(dirname "$0")" && pwd)"
cd "$PROJECT_DIR"
export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES:-1}"
export HF_HUB_DOWNLOAD_TIMEOUT=300
MODEL_HF="${MODEL_HF:-Qwen/Qwen2.5-7B-Instruct}"
MODEL_TAG="${MODEL_TAG:-qwen25-7b}"
QUANT_TAG="${QUANT_TAG:-gptq_w4}"
BENCHMARK="${BENCHMARK:-math500}"
TEMPERATURE="${TEMPERATURE:-0.6}"
SEEDS_STR="${SEEDS:-0 1 2}"
IFS=' ' read -r -a SEEDS <<< "$SEEDS_STR"
PY="${PY:-python}"
GPU_MEM="${GPU_MEM:-0.55}"
# Resolve the quantized-base model path and the restored-adapter path from
# the main pipeline's results, so we don't duplicate those artefacts.
#
# Derive the base vLLM --quant flag from QUANT_TAG: a GPTQ model on disk
# has a compressed-tensors config that conflicts with --quant bnb_nf4, so
# the base path has to match the on-disk quantization. For bnb_nf4_w4 the
# base model is the original HF FP16 (runtime-quantized by vLLM), since
# BnB is not offline-quantized to disk.
case "$QUANT_TAG" in
gptq_w*|awq_w*)
BASE_QUANT=${QUANT_TAG%%_w*} # gptq_w4 -> gptq
QUANT_MODEL="${PROJECT_DIR}/results/quantized_models/${MODEL_TAG}_${QUANT_TAG}"
;;
bnb_nf4_w*)
BASE_QUANT="bnb_nf4"
QUANT_MODEL="$MODEL_HF"
;;
*)
echo "ERROR: unknown QUANT_TAG=$QUANT_TAG — expected awq_w*, gptq_w*, or bnb_nf4_w*"
exit 1
;;
esac
BASE_BITS=${QUANT_TAG##*_w} # gptq_w4 -> 4
ADAPTER="${PROJECT_DIR}/results/restored/${QUANT_TAG}/${MODEL_TAG}/qlora/adapter"
REF_DIR="${PROJECT_DIR}/results/segmented/fp16/${MODEL_TAG}"
MS_ROOT="${PROJECT_DIR}/results/multi_seed/${MODEL_TAG}_${QUANT_TAG}"
LOG_DIR="${PROJECT_DIR}/logs"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="${LOG_DIR}/multi_seed_${TIMESTAMP}.log"
mkdir -p "$MS_ROOT" "$LOG_DIR"
log() { echo "[$(date '+%H:%M:%S')] $1" | tee -a "$LOG_FILE"; }
log "=============================================="
log "Multi-seed robustness (temperature=$TEMPERATURE)"
log " Model: $MODEL_HF ($MODEL_TAG)"
log " Quant: $QUANT_TAG"
log " Benchmark: $BENCHMARK"
log " Seeds: ${SEEDS[*]}"
log "=============================================="
# For BnB NF4 the "quant model" is the original HF FP16; we only need the
# disk dir when using AWQ / GPTQ.
if [[ "$BASE_QUANT" != "bnb_nf4" ]]; then
[[ -d "$QUANT_MODEL" ]] || { log "ERROR: missing quantized model at $QUANT_MODEL (run phase 1b)"; exit 1; }
fi
[[ -d "$REF_DIR" ]] || { log "ERROR: missing FP16 reference at $REF_DIR (run phase 4)"; exit 1; }
[[ -d "$ADAPTER" ]] || { log "ERROR: missing adapter at $ADAPTER (run phase 7)"; exit 1; }
# Merge restored adapter once up front (reused across all seeds).
MERGED="${MS_ROOT}/merged_fp16"
if [[ ! -f "${MERGED}/config.json" ]]; then
log "Merging adapter → FP16 (one-time)"
$PY ${PROJECT_DIR}/scripts/merge_adapter.py \
--model "$MODEL_HF" --adapter "$ADAPTER" --output "$MERGED" 2>&1 | tee -a "$LOG_FILE"
else
log "SKIP: merged FP16 already exists at $MERGED"
fi
run_inference() {
# $1: config_name, $2: model path, $3: out_dir, $4: --quant flag, $5: --bits
local cfg=$1 model_path=$2 out_dir=$3 quant_arg=$4 bits_arg=$5
mkdir -p "$out_dir"
for SEED in "${SEEDS[@]}"; do
local out_file="${out_dir}/${BENCHMARK}_run${SEED}.jsonl"
if [[ -f "$out_file" ]]; then
log "SKIP: inference [$cfg seed=$SEED] — $out_file exists"
continue
fi
log "RUN: inference [$cfg seed=$SEED, T=$TEMPERATURE, quant=$quant_arg]"
$PY ${PROJECT_DIR}/scripts/run_inference.py \
--model "$model_path" \
--quant "$quant_arg" --bits "$bits_arg" \
--benchmark "$BENCHMARK" \
--output "$out_dir" \
--max-tokens 4096 \
--num-runs 1 \
--run-offset "$SEED" \
--seed-start "$SEED" \
--temperature "$TEMPERATURE" \
--top-p 0.95 \
--gpu-memory-utilization "$GPU_MEM" 2>&1 | tee -a "$LOG_FILE"
done
}
BASE_DIR="${MS_ROOT}/base/inference"
REST_DIR="${MS_ROOT}/restored/inference"
# Base path uses the on-disk quantization (matches how phase 3 produced
# the main-pipeline base numbers). Restored path uses merged FP16 + NF4
# at load time (matches phase 8).
run_inference "base" "$QUANT_MODEL" "$BASE_DIR" "$BASE_QUANT" "$BASE_BITS"
run_inference "restored" "$MERGED" "$REST_DIR" "bnb_nf4" "4"
# Segment + diagnose each seed.
for CFG in base restored; do
IN_DIR="${MS_ROOT}/${CFG}/inference"
SEG_DIR="${MS_ROOT}/${CFG}/segmented"
DIAG_DIR="${MS_ROOT}/${CFG}/diagnosis"
mkdir -p "$SEG_DIR" "$DIAG_DIR"
for SEED in "${SEEDS[@]}"; do
local_in="${IN_DIR}/${BENCHMARK}_run${SEED}.jsonl"
local_seg="${SEG_DIR}/${BENCHMARK}_run${SEED}.jsonl"
local_diag="${DIAG_DIR}/${BENCHMARK}_run${SEED}.jsonl"
[[ -f "$local_in" ]] || continue
if [[ ! -f "$local_seg" ]]; then
log "RUN: segment [$CFG seed=$SEED]"
# stepprobe.segment iterates all jsonls in the input dir, so we
# isolate a single seed per call by temporary-moving the others.
# Simpler: feed the whole dir and let it process all at once.
:
fi
done
# Batch: one segment + one diagnose call over all seeds.
log "RUN: segment [$CFG, all seeds]"
$PY -m stepprobe.segment --input "$IN_DIR" --output "$SEG_DIR" \
--quant "${QUANT_TAG}_${CFG}_ms" 2>&1 | tee -a "$LOG_FILE"
log "RUN: diagnose [$CFG, all seeds]"
$PY -m stepprobe.diagnose --ref "$REF_DIR" --hyp "$SEG_DIR" \
--output "$DIAG_DIR" --alignment dtw 2>&1 | tee -a "$LOG_FILE"
done
# Clean up merged dir.
if [[ -d "$MERGED" ]]; then
log "Cleaning up merged FP16 dir"
rm -rf "$MERGED"
fi
log ""
log "=============================================="
log "Rendering fig_paper_9_multi_seed.pdf"
log "=============================================="
$PY ${PROJECT_DIR}/scripts/make_multi_seed_figure.py \
--multiseed-root "$MS_ROOT" \
--model "$MODEL_TAG" --quant "$QUANT_TAG" --benchmark "$BENCHMARK" \
--metrics "${PROJECT_DIR}/results/metrics" \
--output "${PROJECT_DIR}/figures/paper/fig_paper_9_multi_seed.pdf" 2>&1 | tee -a "$LOG_FILE"
log ""
log "DONE — multi-seed robustness check"
log " Figure: figures/paper/fig_paper_9_multi_seed.pdf"
log " Log: $LOG_FILE"