File size: 6,493 Bytes
1e59964 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | #!/usr/bin/env bash
###############################################################################
# StepProbe — Baseline restoration comparison.
#
# Tests the value of the silver-bullet selection by training two baseline
# adapters and evaluating all three on the same benchmark cell:
#
# silver_bullet — failed problems, error-type-proportional (the paper's method)
# failed_only — failed problems, uniform random (strip the balancing)
# random — ALL problems (incl. correct), uniform (strip the diagnosis)
#
# If silver_bullet ≈ random, the paper's restoration contribution collapses.
# If silver_bullet > random, the paper has a defensible novelty claim.
#
# Output: results/baselines/<model>_<quant>/<strategy>/...
# figures/paper/fig_paper_7_baselines.pdf
#
# Usage:
# bash run_baselines.sh
# MODEL_TAG=r1-qwen-1.5b bash run_baselines.sh
#
# Expected runtime: 3 × (~20 min training + ~10 min inference) ≈ 90 min on a 3090 Ti.
# (silver_bullet at N=500 is reused from phase 7 if present — skip included.)
###############################################################################
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}"
N_SAMPLES="${N_SAMPLES:-500}"
PY="${PY:-python}"
GPU_MEM="${GPU_MEM:-0.55}"
# Drop MAX_SEQ_LEN=1024 or LORA_RANK=8 if training OOMs while another GPU
# process is resident (e.g. ollama holding ~13 GB).
MAX_SEQ_LEN="${MAX_SEQ_LEN:-2048}"
LORA_RANK="${LORA_RANK:-16}"
BASELINE_ROOT="${PROJECT_DIR}/results/baselines/${MODEL_TAG}_${QUANT_TAG}"
DIAG_DIR="${PROJECT_DIR}/results/diagnosis/${QUANT_TAG}/${MODEL_TAG}"
REF_DIR="${PROJECT_DIR}/results/segmented/fp16/${MODEL_TAG}"
LOG_DIR="${PROJECT_DIR}/logs"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="${LOG_DIR}/baselines_${TIMESTAMP}.log"
mkdir -p "$BASELINE_ROOT" "$LOG_DIR"
log() { echo "[$(date '+%H:%M:%S')] $1" | tee -a "$LOG_FILE"; }
run_or_skip() {
local desc=$1 skip=$2 cmd=$3
if eval "$skip"; then log "SKIP: $desc"; else log "RUN: $desc"; eval "$cmd" 2>&1 | tee -a "$LOG_FILE"; fi
}
log "=============================================="
log "Baseline restoration sweep"
log " Model: $MODEL_HF ($MODEL_TAG)"
log " Quant: $QUANT_TAG"
log " Benchmark: $BENCHMARK"
log " N samples: $N_SAMPLES"
log " Strategies: silver_bullet, failed_only, random"
log "=============================================="
[[ -d "$DIAG_DIR" ]] || { log "ERROR: missing $DIAG_DIR (run phase 5)"; exit 1; }
[[ -d "$REF_DIR" ]] || { log "ERROR: missing $REF_DIR (run phase 4)"; exit 1; }
for STRATEGY in silver_bullet failed_only random; do
log ""
log "=============================================="
log "Strategy: $STRATEGY"
log "=============================================="
CFG_DIR="${BASELINE_ROOT}/${STRATEGY}"
ADAPTER="${CFG_DIR}/qlora/adapter"
MERGED="${CFG_DIR}/qlora/merged_fp16"
INF_DIR="${CFG_DIR}/inference"
OUT_FILE="${INF_DIR}/${BENCHMARK}_run0.jsonl"
# 1. Train restoration with this sampling strategy.
run_or_skip "QLoRA ($STRATEGY, N=$N_SAMPLES, seq=$MAX_SEQ_LEN, r=$LORA_RANK)" \
"[[ -f '${ADAPTER}/adapter_model.safetensors' ]]" \
"$PY -m stepprobe.restore \
--model '$MODEL_HF' \
--diagnosis '$DIAG_DIR' \
--ref '$REF_DIR' \
--output '$CFG_DIR' \
--method qlora \
--max-samples $N_SAMPLES \
--sampling-strategy $STRATEGY \
--epochs 3 \
--lr 2e-4 \
--batch-size 4 \
--max-seq-length $MAX_SEQ_LEN \
--lora-rank $LORA_RANK"
# 2. Merge + inference (skip entirely if inference output exists).
if [[ -f "$OUT_FILE" ]]; then
log "SKIP: inference ($STRATEGY) — $OUT_FILE exists"
else
run_or_skip "Merge adapter → FP16 ($STRATEGY)" \
"[[ -f '${MERGED}/config.json' ]]" \
"$PY ${PROJECT_DIR}/scripts/merge_adapter.py \
--model '$MODEL_HF' \
--adapter '$ADAPTER' \
--output '$MERGED'"
$PY -c "import torch; torch.cuda.empty_cache() if torch.cuda.is_available() else None" 2>&1 | tee -a "$LOG_FILE"
run_or_skip "vLLM inference ($STRATEGY)" \
"[[ -f '$OUT_FILE' ]]" \
"$PY ${PROJECT_DIR}/scripts/run_inference.py \
--model '$MERGED' \
--quant bnb_nf4 --bits 4 \
--benchmark $BENCHMARK \
--output '$INF_DIR' \
--max-tokens 4096 --num-runs 1 \
--gpu-memory-utilization $GPU_MEM"
fi
# 3. Segment + diagnose so accuracy can be read from a diagnosed jsonl.
SEG_DIR="${CFG_DIR}/segmented"
DIAG_OUT="${CFG_DIR}/diagnosis"
run_or_skip "Segment ($STRATEGY)" \
"[[ -f '${SEG_DIR}/${BENCHMARK}_run0.jsonl' ]]" \
"$PY -m stepprobe.segment --input '$INF_DIR' --output '$SEG_DIR' --quant '${QUANT_TAG}_${STRATEGY}'"
run_or_skip "Diagnose ($STRATEGY)" \
"[[ -f '${DIAG_OUT}/${BENCHMARK}_run0.jsonl' ]]" \
"$PY -m stepprobe.diagnose --ref '$REF_DIR' --hyp '$SEG_DIR' --output '$DIAG_OUT' --alignment dtw"
# 4. Disk hygiene.
if [[ -d "$MERGED" ]]; then
log "Cleaning up merged FP16 dir: $MERGED"
rm -rf "$MERGED"
fi
$PY -c "import torch; torch.cuda.empty_cache() if torch.cuda.is_available() else None" 2>&1 | tee -a "$LOG_FILE"
done
log ""
log "=============================================="
log "Rendering fig_paper_7_baselines.pdf"
log "=============================================="
$PY ${PROJECT_DIR}/scripts/make_baselines_figure.py \
--baseline-root "$BASELINE_ROOT" \
--model "$MODEL_TAG" \
--quant "$QUANT_TAG" \
--benchmark "$BENCHMARK" \
--metrics "${PROJECT_DIR}/results/metrics" \
--segmented "${PROJECT_DIR}/results/segmented" \
--output "${PROJECT_DIR}/figures/paper/fig_paper_7_baselines.pdf" 2>&1 | tee -a "$LOG_FILE"
log ""
log "=============================================="
log "DONE — compare silver_bullet vs failed_only vs random"
log " Figure: figures/paper/fig_paper_7_baselines.pdf"
log " Log: $LOG_FILE"
log "=============================================="
|