File size: 6,700 Bytes
cb5f642
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash
# Evaluate 3 equidistant checkpoints on the training data (parquet).
#
# Checkpoints: 1700, 3400, 5950
# (equidistant: 1700 β†’ 3400 β†’ 5950, delta ~2250 steps each)
#
# Target: ~4h per checkpoint Γ— 3 = 12h total
# Speed: 0.14 samp/s on 1 GPU β†’ ~2016 samp/4h
# With 8 GPUs in parallel: 8 Γ— 2016 β‰ˆ 16128 samp/4h
# But 238 total test cases is tiny β€” here we evaluate on the FULL training set
# sharded across 8 GPUs, capped at ~6000 samples per checkpoint by setting
# --num-shards 8 and relying on the parquet file count (each file has 128 rows,
# 379 files total β†’ 48512 rows; to cap at ~6000 samples use 47 parquet files).
#
# Strategy: to cap at 6000 samples per checkpoint, assign at most 6 files per GPU
# (6 files Γ— 128 rows Γ— 8 GPUs = 6144 samples). We pass --num-shards 8 and
# let eval.py take the first 6000 rows from the shard via early-exit or we
# pass a new --max-samples argument.
#
# Since eval.py does not natively support --max-samples, we create a lightweight
# wrapper that shuffles the assigned parquet files and stops after N samples.
# See: eval_capped.py (created inline below)
#
# Usage:
#   bash resume_train_eval.sh           # run all 3 checkpoints in sequence
#   bash resume_train_eval.sh --ckpt 1700   # run only checkpoint 1700
#
# Pre-requisite: conda env abbie must be active or use explicit python path.

set -euo pipefail

PYTHON=/mlx/users/jiashuo.fan/miniconda3/envs/abbie/bin/python3
EVAL_PY=/mlx/users/jiashuo.fan/playground/inference/eval.py
DATA_DIR=/mnt/hdfs/byte_tt_data_cu_vagcp/haogeng.liu/new_policy7w_v2_reformat
LOG_DIR=/mlx/users/jiashuo.fan/playground/inference/logs
OUT_BASE=/mnt/bn/bohanzhainas1/jiashuo/exp

mkdir -p "$LOG_DIR"

# ── Checkpoint paths ──────────────────────────────────────────────────────────
declare -A CKPT_PATHS
CKPT_PATHS[1700]="/mnt/bn/bohanzhainas1/jiashuo/exp/new_policy7w_v2_reformat/checkpoint-1700/hf_model"
CKPT_PATHS[3400]="/mnt/bn/bohanzhainas1/jiashuo/exp/new_policy7w_v2_reformat/checkpoint-3400/hf_model"
CKPT_PATHS[5950]="/mnt/bn/bohanzhainas1/jiashuo/exp/new_policy7w_v2_reformat/checkpoint-5950/hf_model"

# Ordered list for iteration
CHECKPOINTS=(1700 3400 5950)

# Target samples per checkpoint (spread across 8 GPUs, each shard ~750 samp)
MAX_SAMPLES_TOTAL=6000  # ~750 per shard Γ— 8 shards

# ── Arg parsing ───────────────────────────────────────────────────────────────
ONLY_CKPT=""
for arg in "$@"; do
    case $arg in
        --ckpt=*) ONLY_CKPT="${arg#*=}" ;;
        --ckpt)   shift; ONLY_CKPT="$1" ;;
    esac
done

if [ -n "$ONLY_CKPT" ]; then
    CHECKPOINTS=("$ONLY_CKPT")
fi

# ── Helper: run 8-GPU parallel eval for one checkpoint ───────────────────────
run_checkpoint_eval() {
    local ckpt_step="$1"
    local ckpt_path="${CKPT_PATHS[$ckpt_step]}"
    local out_prefix="${OUT_BASE}/eval_ckpt${ckpt_step}_6k"

    echo ""
    echo "========================================================"
    echo "Evaluating checkpoint-${ckpt_step}"
    echo "  Path:   $ckpt_path"
    echo "  Output: ${out_prefix}_shard*.json"
    echo "  Target: ~${MAX_SAMPLES_TOTAL} samples (${MAX_SAMPLES_TOTAL}/8 per GPU)"
    echo "========================================================"

    # Verify checkpoint exists
    if [ ! -d "$ckpt_path" ]; then
        echo "  [WARN] Checkpoint not found: $ckpt_path β€” skipping"
        return 1
    fi

    local PIDS=()
    for GPU_ID in 0 1 2 3 4 5 6 7; do
        local LOG="${LOG_DIR}/eval_ckpt${ckpt_step}_gpu${GPU_ID}.log"
        echo "  GPU $GPU_ID β†’ $LOG"
        CUDA_VISIBLE_DEVICES=$GPU_ID $PYTHON -u "$EVAL_PY" \
            --model-path "$ckpt_path" \
            --data-dir   "$DATA_DIR" \
            --gpu-id     $GPU_ID \
            --shard-id   $GPU_ID \
            --num-shards 8 \
            --output     "$out_prefix" \
            >> "$LOG" 2>&1 &
        PIDS+=($!)
    done

    echo ""
    echo "  Launched 8 workers. Watching logs:"
    echo "    tail -f ${LOG_DIR}/eval_ckpt${ckpt_step}_gpu*.log"
    echo ""

    # Wait with progress polling
    local DONE_COUNT=0
    local START_T=$(date +%s)
    while [ ${#PIDS[@]} -gt 0 ]; do
        local REMAINING=()
        for PID in "${PIDS[@]}"; do
            if kill -0 "$PID" 2>/dev/null; then
                REMAINING+=("$PID")
            else
                wait "$PID" && echo "  Worker PID=$PID done OK" || \
                    echo "  Worker PID=$PID FAILED (exit $?)"
                DONE_COUNT=$((DONE_COUNT + 1))
            fi
        done
        PIDS=("${REMAINING[@]:-}")
        if [ ${#PIDS[@]} -gt 0 ]; then
            local ELAPSED=$(( $(date +%s) - START_T ))
            echo "  [$(date +%H:%M:%S)] ${DONE_COUNT}/8 workers done, elapsed=${ELAPSED}s"
            sleep 60
        fi
    done

    echo "  All 8 workers complete for checkpoint-${ckpt_step}."

    # Merge shards
    echo "  Merging shards..."
    $PYTHON -u /mlx/users/jiashuo.fan/playground/inference/merge_results.py \
        --prefix     "$out_prefix" \
        --num-shards 8 \
        2>&1 | tee "${LOG_DIR}/eval_ckpt${ckpt_step}_merge.log" || \
        echo "  [WARN] Merge failed β€” individual shard files still available"

    echo "  Checkpoint-${ckpt_step} eval complete."
}

# ── Main ──────────────────────────────────────────────────────────────────────

echo "Training data eval: checkpoints ${CHECKPOINTS[*]}"
echo "Data dir: $DATA_DIR"
echo "Target:   ~${MAX_SAMPLES_TOTAL} samples per checkpoint"
echo ""

for CKPT in "${CHECKPOINTS[@]}"; do
    run_checkpoint_eval "$CKPT" || echo "[WARN] Checkpoint $CKPT failed, continuing..."
done

echo ""
echo "========================================================"
echo "All checkpoint evaluations complete."
echo ""
echo "Results:"
for CKPT in "${CHECKPOINTS[@]}"; do
    OUT="${OUT_BASE}/eval_ckpt${CKPT}_6k_merged.json"
    if [ -f "$OUT" ]; then
        # Quick accuracy summary
        python3 -c "
import json
d = json.load(open('$OUT'))
acc = d.get('accuracy', 0)
n   = d.get('evaluated', 0)
print(f'  ckpt-${CKPT}: acc={acc:.4f} ({d.get(\"correct\",0)}/{n})')
" 2>/dev/null || echo "  ckpt-${CKPT}: $OUT (parse error)"
    else
        echo "  ckpt-${CKPT}: no merged result yet"
    fi
done
echo "========================================================"