File size: 9,473 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/env bash
###############################################################################
# StepProbe — Master driver for all follow-up experiments.
#
# Runs the six GPU-gated experiment scripts in their priority order (the
# tallest reviewer-risk items first), then regenerates every paper figure
# and LaTeX table so the paper source is up to date.
#
# Every step is skip-if-exists, so if you Ctrl-C mid-run, just re-run and
# it resumes from where it stopped. A single failure halts the master
# script (set -e), but individual sub-scripts tolerate resumption.
#
# Total runtime if everything runs from scratch: ~10-12 hours on one 3090 Ti.
# Re-runs after partial completion are proportionally shorter.
#
# Usage:
#   bash run_all_experiments.sh              # run the default priority order
#   bash run_all_experiments.sh --dry-run    # print the plan, run nothing
#   bash run_all_experiments.sh --skip baselines multi_seed    # skip by tag
#   bash run_all_experiments.sh --only prompt_prefix           # run one phase
#   OPENAI_API_KEY=sk-... bash run_all_experiments.sh          # include A3 validation
#
# Phase tags (for --skip / --only):
#   baselines     — sampling-strategy baselines (A4)
#   multi_seed    — 3-seed robustness on primary cell (A1)
#   classifier    — LLM-judge classifier validation (A3) — API KEY REQUIRED
#   llama_lr      — Llama LR sensitivity sweep (A5)
#   prompt_prefix — training-free intervention (B2-a)
#   scale_up      — 14B scale-up experiment (B1)
#   paper         — regenerate figures + tables (always runs at the end)
###############################################################################
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

# ========================== CONFIG =========================================
LOG_DIR="${PROJECT_DIR}/logs"
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="${LOG_DIR}/run_all_experiments_${TIMESTAMP}.log"
PY="${PY:-python}"
GPU_MEM="${GPU_MEM:-0.55}"

mkdir -p "$LOG_DIR"

# ========================== ARG PARSING ====================================
DRY_RUN=false
CONTINUE_ON_ERROR=false
ONLY_TAGS=()
SKIP_TAGS=()

while [[ $# -gt 0 ]]; do
    case $1 in
        --dry-run)            DRY_RUN=true; shift ;;
        --continue-on-error)  CONTINUE_ON_ERROR=true; shift ;;
        --skip)               shift; while [[ $# -gt 0 && "$1" != --* ]]; do SKIP_TAGS+=("$1"); shift; done ;;
        --only)               shift; while [[ $# -gt 0 && "$1" != --* ]]; do ONLY_TAGS+=("$1"); shift; done ;;
        --help|-h)
            grep -E "^#( |$)" "$0" | sed 's/^# \?//' | head -40
            exit 0 ;;
        *) echo "Unknown option: $1"; exit 1 ;;
    esac
done

FAILED_TAGS=()

should_run() {
    local tag=$1
    # If --only was passed, only those tags run.
    if [[ ${#ONLY_TAGS[@]} -gt 0 ]]; then
        for t in "${ONLY_TAGS[@]}"; do
            [[ "$t" == "$tag" ]] && return 0
        done
        return 1
    fi
    # Otherwise, skip tags listed in --skip.
    for t in "${SKIP_TAGS[@]}"; do
        [[ "$t" == "$tag" ]] && return 1
    done
    return 0
}

# ========================== LOGGING ========================================
log() {
    local msg="[$(date '+%H:%M:%S')] $1"
    echo "$msg" | tee -a "$LOG_FILE"
}

section() {
    log ""
    log "=============================================="
    log "$1"
    log "=============================================="
}

# Run one phase. $1 = tag, $2 = description, $3 = runtime estimate, $4 = command.
run_phase() {
    local tag=$1 desc=$2 eta=$3 cmd=$4

    if ! should_run "$tag"; then
        log "[skip $tag] $desc"
        return 0
    fi

    section "PHASE: $desc  [tag=$tag, ETA $eta]"
    if $DRY_RUN; then
        log "  [DRY-RUN] $cmd"
        return 0
    fi

    local phase_start=$(date +%s)
    # Disable `set -e` around the eval so a non-zero exit doesn't kill us;
    # we want to check the status ourselves. Restore immediately after.
    set +e
    eval "$cmd" 2>&1 | tee -a "$LOG_FILE"
    local rc=${PIPESTATUS[0]}
    set -e

    local phase_end=$(date +%s)
    local dur=$((phase_end - phase_start))

    if [[ $rc -ne 0 ]]; then
        log "FAILED [$tag] after $((dur / 60))m $((dur % 60))s — exit=$rc"
        FAILED_TAGS+=("$tag")
        if $CONTINUE_ON_ERROR; then
            log "  continuing to next phase (--continue-on-error set)"
            return 0
        fi
        log "  Resume with: bash $0 --only $tag"
        log "  Or skip and continue the remaining queue: bash $0 --continue-on-error --skip $tag"
        log "  (Individual scripts are skip-if-exists so restart is cheap.)"
        exit 1
    fi
    log "PHASE COMPLETE [$tag] in $((dur / 60))m $((dur % 60))s"
}

# ========================== PREFLIGHT ======================================
section "StepProbe — full follow-up experiment sweep"
log "  Project dir: $PROJECT_DIR"
log "  Log file:    $LOG_FILE"
log "  GPU:         $(nvidia-smi --query-gpu=name --format=csv,noheader 2>/dev/null | head -1 || echo 'unknown')"
log "  CUDA_VISIBLE_DEVICES=$CUDA_VISIBLE_DEVICES"

if [[ ${#ONLY_TAGS[@]} -gt 0 ]]; then
    log "  Running ONLY: ${ONLY_TAGS[*]}"
elif [[ ${#SKIP_TAGS[@]} -gt 0 ]]; then
    log "  Skipping:     ${SKIP_TAGS[*]}"
fi
if $DRY_RUN; then
    log "  Mode:         DRY-RUN"
fi

OVERALL_START=$(date +%s)

# ========================== PHASE 1 — BASELINES (A4) ======================
# Tallest reviewer-risk: if silver_bullet ≈ random, the novelty claim collapses.
run_phase "baselines" \
    "Sampling-strategy baselines (silver_bullet vs failed_only vs random)" \
    "~90 min" \
    "bash ${PROJECT_DIR}/run_baselines.sh"

# ========================== PHASE 2 — MULTI-SEED (A1) =====================
# Reviewer-rigor blocker for NeurIPS-tier submission.
run_phase "multi_seed" \
    "Multi-seed robustness (3 seeds at T=0.6 on primary cell)" \
    "~2 h" \
    "bash ${PROJECT_DIR}/run_multi_seed.sh"

# ========================== PHASE 3 — CLASSIFIER (A3) =====================
# LLM-judge validation. Requires OPENAI_API_KEY or ANTHROPIC_API_KEY.
if [[ -n "${OPENAI_API_KEY:-}" ]]; then
    run_phase "classifier" \
        "Error-type classifier validation (OpenAI judge, gpt-4o)" \
        "~5 min + API cost" \
        "$PY ${PROJECT_DIR}/scripts/validate_classifier.py \
            --judge openai --judge-model gpt-4o --n-samples 200"
elif [[ -n "${ANTHROPIC_API_KEY:-}" ]]; then
    run_phase "classifier" \
        "Error-type classifier validation (Anthropic judge, claude-3-5-sonnet)" \
        "~5 min + API cost" \
        "$PY ${PROJECT_DIR}/scripts/validate_classifier.py \
            --judge anthropic --judge-model claude-3-5-sonnet-latest --n-samples 200"
elif should_run "classifier"; then
    log "[skip classifier] set OPENAI_API_KEY or ANTHROPIC_API_KEY to include LLM-judge validation"
fi

# ========================== PHASE 4 — LLAMA LR SWEEP (A5) =================
# Converts the §8 regression discussion into evidence-backed narrative.
run_phase "llama_lr" \
    "Llama-family learning-rate sweep (5e-5, 1e-4, 2e-4)" \
    "~2.5 h" \
    "bash ${PROJECT_DIR}/run_llama_lr_sweep.sh"

# ========================== PHASE 5 — PROMPT PREFIX (B2-a) ================
# Headline figure for the reframed paper: training-free intervention.
run_phase "prompt_prefix" \
    "Prompt-prefix injection sweep (k = 0..4)" \
    "~80 min" \
    "bash ${PROJECT_DIR}/run_prompt_prefix.sh"

# ========================== PHASE 6 — 14B SCALE-UP (B1) ===================
# Answers the inevitable "does it scale?" reviewer question.
run_phase "scale_up" \
    "14B scale-up experiment" \
    "~90 min" \
    "bash ${PROJECT_DIR}/run_scale_up.sh"

# ========================== PHASE 7 — PAPER ARTIFACTS =====================
# Always regenerate — this is cheap and ensures every figure/table reflects
# whatever new experiments succeeded above.
run_phase "paper" \
    "Regenerate paper figures + LaTeX tables" \
    "~1 min" \
    "$PY ${PROJECT_DIR}/scripts/compute_ci.py \
        --diagnosis ${PROJECT_DIR}/results/diagnosis \
        --output ${PROJECT_DIR}/results/metrics --n-boot 5000 && \
     $PY ${PROJECT_DIR}/scripts/make_paper_figures.py \
        --metrics ${PROJECT_DIR}/results/metrics \
        --diagnosis ${PROJECT_DIR}/results/diagnosis \
        --segmented ${PROJECT_DIR}/results/segmented \
        --output ${PROJECT_DIR}/figures/paper && \
     $PY ${PROJECT_DIR}/scripts/make_tables.py \
        --metrics-dir ${PROJECT_DIR}/results/metrics \
        --output-dir ${PROJECT_DIR}/figures/paper"

# ========================== DONE ===========================================
OVERALL_END=$(date +%s)
TOTAL=$((OVERALL_END - OVERALL_START))

if [[ ${#FAILED_TAGS[@]} -gt 0 ]]; then
    section "RUN FINISHED WITH FAILURES"
    log "  Failed phases: ${FAILED_TAGS[*]}"
    log "  Resume any of them with: bash $0 --only <tag>"
else
    section "ALL EXPERIMENTS COMPLETE"
fi
log "  Total time:  $((TOTAL / 3600))h $(((TOTAL % 3600) / 60))m"
log "  Log file:    $LOG_FILE"
log ""
log "  Next:"
log "    1. Open figures/paper/ to review all figures and tables."
log "    2. Recompile paper/: cd paper && make   (or upload to Overleaf)."
log "    3. Read §6.3 (baselines), §7 (interventions), §8 (limitations) first"
log "       — those are the sections whose content depends on these runs."