| #!/usr/bin/env bash |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| 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}" |
| SAMPLE_SIZES_STR="${SAMPLE_SIZES:-50 100 250 500}" |
| IFS=' ' read -r -a SAMPLE_SIZES <<< "$SAMPLE_SIZES_STR" |
|
|
| PY="${PY:-python}" |
| MAX_TOKENS="${MAX_TOKENS:-4096}" |
| |
| |
| |
| GPU_MEM="${GPU_MEM:-0.55}" |
|
|
| |
| ABLATION_ROOT="${PROJECT_DIR}/results/ablation/${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}/ablation_${TIMESTAMP}.log" |
|
|
| mkdir -p "$ABLATION_ROOT" "$LOG_DIR" |
|
|
| |
| log() { |
| local msg="[$(date '+%H:%M:%S')] $1" |
| echo "$msg" | tee -a "$LOG_FILE" |
| } |
|
|
| run_or_skip() { |
| local desc=$1 |
| local skip_check=$2 |
| local cmd=$3 |
| if eval "$skip_check"; then |
| log "SKIP: $desc" |
| else |
| log "RUN: $desc" |
| eval "$cmd" 2>&1 | tee -a "$LOG_FILE" |
| fi |
| } |
|
|
| |
| log "==============================================" |
| log "StepProbe restoration ablation" |
| log " Model: $MODEL_HF ($MODEL_TAG)" |
| log " Quant: $QUANT_TAG" |
| log " Benchmark: $BENCHMARK" |
| log " Sample Ns: ${SAMPLE_SIZES[*]}" |
| log " Output: $ABLATION_ROOT" |
| log " Log: $LOG_FILE" |
| log "==============================================" |
|
|
| |
| if [[ ! -d "$DIAG_DIR" ]]; then |
| log "ERROR: missing diagnosis dir: $DIAG_DIR" |
| log " run 'bash run_all.sh --phase 5' to generate it first" |
| exit 1 |
| fi |
| if [[ ! -d "$REF_DIR" ]]; then |
| log "ERROR: missing FP16 reference segmentation: $REF_DIR" |
| log " run 'bash run_all.sh --phase 4' to generate it first" |
| exit 1 |
| fi |
|
|
| |
| for N in "${SAMPLE_SIZES[@]}"; do |
| log "" |
| log "==============================================" |
| log "Ablation N=$N" |
| log "==============================================" |
|
|
| CFG_DIR="${ABLATION_ROOT}/n${N}" |
| ADAPTER="${CFG_DIR}/qlora/adapter" |
| MERGED="${CFG_DIR}/qlora/merged_fp16" |
| INF_DIR="${CFG_DIR}/inference" |
| OUT_FILE="${INF_DIR}/${BENCHMARK}_run0.jsonl" |
|
|
| |
| run_or_skip "QLoRA restoration (N=$N)" \ |
| "[[ -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 \ |
| --epochs 3 \ |
| --lr 2e-4 \ |
| --batch-size 4" |
|
|
| |
| if [[ -f "$OUT_FILE" ]]; then |
| log "SKIP: vLLM inference on $BENCHMARK (N=$N) — $OUT_FILE exists" |
| else |
| run_or_skip "Merge adapter → FP16 (N=$N)" \ |
| "[[ -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 on $BENCHMARK (N=$N)" \ |
| "[[ -f '$OUT_FILE' ]]" \ |
| "$PY ${PROJECT_DIR}/scripts/run_inference.py \ |
| --model '$MERGED' \ |
| --quant bnb_nf4 \ |
| --bits 4 \ |
| --benchmark $BENCHMARK \ |
| --output '$INF_DIR' \ |
| --max-tokens $MAX_TOKENS \ |
| --num-runs 1 \ |
| --gpu-memory-utilization $GPU_MEM" |
| fi |
|
|
| |
| SEG_DIR="${CFG_DIR}/segmented" |
| DIAG_OUT="${CFG_DIR}/diagnosis" |
| run_or_skip "Segment (N=$N)" \ |
| "[[ -f '${SEG_DIR}/${BENCHMARK}_run0.jsonl' ]]" \ |
| "$PY -m stepprobe.segment --input '$INF_DIR' --output '$SEG_DIR' --quant '${QUANT_TAG}_ablation_n${N}'" |
|
|
| run_or_skip "Diagnose vs FP16 (N=$N)" \ |
| "[[ -f '${DIAG_OUT}/${BENCHMARK}_run0.jsonl' ]]" \ |
| "$PY -m stepprobe.diagnose --ref '$REF_DIR' --hyp '$SEG_DIR' --output '$DIAG_OUT' --alignment dtw" |
|
|
| |
| 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_6_ablation.pdf" |
| log "==============================================" |
| $PY ${PROJECT_DIR}/scripts/make_ablation_figure.py \ |
| --ablation-root "$ABLATION_ROOT" \ |
| --model "$MODEL_TAG" \ |
| --quant "$QUANT_TAG" \ |
| --benchmark "$BENCHMARK" \ |
| --metrics "${PROJECT_DIR}/results/metrics" \ |
| --output "${PROJECT_DIR}/figures/paper/fig_paper_6_ablation.pdf" 2>&1 | tee -a "$LOG_FILE" |
|
|
| log "" |
| log "==============================================" |
| log "DONE" |
| log " Figure: figures/paper/fig_paper_6_ablation.pdf" |
| log " Log: $LOG_FILE" |
| log "==============================================" |
|
|