#!/bin/bash #SBATCH --job-name=vl #SBATCH --partition=gpu-large #SBATCH --qos=batch-long #SBATCH --gres=gpu:1 #SBATCH --cpus-per-task=2 #SBATCH --mem=80G #SBATCH --time=10:00:00 #SBATCH --output=/weka/s225250685/mats-tist/slurm_logs/orpo_valfix_%j.out set -u cd /weka/s225250685/mats-tist export HF_HOME=/weka/s225250685/Huggingface export HF_HUB_CACHE=/weka/s225250685/Huggingface/hub export DB_EXEC_API_DISABLE=1 export PYTHONNOUSERSITE=1 export NO_PROXY=localhost,127.0.0.1 export PYTHONPATH=/weka/s225250685/mats-tist export TOKENIZERS_PARALLELISM=false PY=/weka/s225250685/conda-envs/handbook/bin/python VLLM=/weka/s225250685/conda-envs/handbook/bin/vllm ACCEL=/weka/s225250685/conda-envs/handbook/bin/accelerate AH=/weka/s225250685/mats-tist/alignment-handbook PLANNER=/weka/s225250685/mats-tist/alignment-handbook/output/planner-iter2-collab-3B SEL_V2=/weka/s225250685/mats-tist/alignment-handbook/output/selector-3B-v2-rows FIXER_V2_ORPO=$AH/output/fixer-v2-1.5B-execerr-orpo-expanded SEM_FIXER_ORPO=$AH/output/semantic-fixer-v3-1.5B-orpo-nogate VALIDATOR_ORPO=$AH/output/validator-v4-0.5B-orpo LOG=/weka/s225250685/mats-tist/slurm_logs/orpo_valfix_${SLURM_JOB_ID}.log : > "$LOG" nvidia-smi --query-gpu=name,memory.total --format=csv,noheader | tee -a "$LOG" kill_vllm() { pkill -9 -f "vllm serve" 2>/dev/null || true pkill -9 -f "VLLM::EngineCore" 2>/dev/null || true sleep 5 } trap kill_vllm EXIT wait_url() { for i in {1..180}; do curl --noproxy '*' -fs "$1" >/dev/null 2>&1 && return 0 sleep 5 done return 1 } ############################################## # STAGE A: Build ORPO training data ############################################## echo "==== [A] building validator v4 ORPO data ====" | tee -a "$LOG" $PY scripts/build_validator_v4_orpo.py 2>&1 | tee -a "$LOG" echo "==== [A] building semantic fixer v3 data ====" | tee -a "$LOG" $PY scripts/build_semantic_fixer_v3.py 2>&1 | tee -a "$LOG" echo "==== [A] rebuilding fixer v2 data (with real exec errors) ====" | tee -a "$LOG" $PY scripts/build_fixer_v2_execerr.py 2>&1 | tee -a "$LOG" echo "==== [A] all data ready ====" | tee -a "$LOG" ############################################## # STAGE B: ORPO — Fixer v2 (exec-error, 1.5B) # chosen = correct SQL, rejected = failed SQL ############################################## if [ -f "$FIXER_V2_ORPO/config.json" ]; then echo "==== [B] fixer v2 ORPO already exists, skipping training ====" | tee -a "$LOG" else echo "==== [B] ORPO training: fixer v2 exec-error (1.5B) ====" | tee -a "$LOG" cd $AH PYTHONPATH=src/ ACCELERATE_LOG_LEVEL=info $ACCEL launch \ --main_process_port 29601 \ --config_file recipes/accelerate_configs/single_gpu0_local.yaml \ scripts/run_orpo.py \ recipes/scaleup-3stage/orpo-fixer-v2-execerr.yaml 2>&1 | tee -a "$LOG" cd /weka/s225250685/mats-tist if [ ! -f "$FIXER_V2_ORPO/config.json" ]; then echo "FIXER V2 ORPO FAILED" | tee -a "$LOG"; exit 1 fi fi echo "==== [B] fixer v2 ORPO ready at $FIXER_V2_ORPO ====" | tee -a "$LOG" ############################################## # STAGE C: ORPO — Validator v4 (0.5B, combined sel+cond) ############################################## if [ -f "$VALIDATOR_ORPO/config.json" ]; then echo "==== [C] validator v4 ORPO already exists, skipping training ====" | tee -a "$LOG" else echo "==== [C] ORPO training: validator v4 combined (0.5B) ====" | tee -a "$LOG" cd $AH PYTHONPATH=src/ ACCELERATE_LOG_LEVEL=info $ACCEL launch \ --main_process_port 29602 \ --config_file recipes/accelerate_configs/single_gpu0_local.yaml \ scripts/run_orpo.py \ recipes/scaleup-3stage/orpo-validator-v4.yaml 2>&1 | tee -a "$LOG" cd /weka/s225250685/mats-tist if [ ! -f "$VALIDATOR_ORPO/config.json" ]; then echo "VALIDATOR V4 ORPO FAILED" | tee -a "$LOG"; exit 1 fi fi echo "==== [C] validator v4 ORPO ready at $VALIDATOR_ORPO ====" | tee -a "$LOG" ############################################## # STAGE D: ORPO — Semantic Fixer v3 (1.5B) ############################################## if [ -f "$SEM_FIXER_ORPO/config.json" ]; then echo "==== [D] semantic fixer v3 ORPO already exists, skipping training ====" | tee -a "$LOG" else echo "==== [D] ORPO training: semantic fixer v3 (1.5B) ====" | tee -a "$LOG" cd $AH PYTHONPATH=src/ ACCELERATE_LOG_LEVEL=info $ACCEL launch \ --main_process_port 29603 \ --config_file recipes/accelerate_configs/single_gpu0_local.yaml \ scripts/run_orpo.py \ recipes/scaleup-3stage/orpo-semantic-fixer-v3.yaml 2>&1 | tee -a "$LOG" cd /weka/s225250685/mats-tist if [ ! -f "$SEM_FIXER_ORPO/config.json" ]; then echo "SEMANTIC FIXER V3 ORPO FAILED" | tee -a "$LOG"; exit 1 fi fi echo "==== [D] semantic fixer v3 ORPO ready at $SEM_FIXER_ORPO ====" | tee -a "$LOG" ############################################## # STAGE E: Launch endpoints + 3-stage K=8 rollout # H200=141GB: planner(0.30) + val(0.08) + fixer_v2(0.15) + sem_fixer(0.15) = 0.68 # One combined validator serves both sel+cond on same port ############################################## kill_vllm echo "==== [E] launching endpoints ====" | tee -a "$LOG" $VLLM serve "$PLANNER" --served-model-name planner --port 8100 --dtype bfloat16 \ --gpu-memory-utilization 0.30 --enforce-eager --max-model-len 8192 > "${LOG}.p" 2>&1 & wait_url http://localhost:8100/v1/models && echo " planner READY" | tee -a "$LOG" # Combined validator v4 serves both sel and cond prompts $VLLM serve "$VALIDATOR_ORPO" --served-model-name validator_sel --port 8101 --dtype bfloat16 \ --gpu-memory-utilization 0.08 --enforce-eager --max-model-len 6144 > "${LOG}.vs" 2>&1 & wait_url http://localhost:8101/v1/models && echo " validator_v4 READY (port 8101)" | tee -a "$LOG" $VLLM serve "$VALIDATOR_ORPO" --served-model-name validator_cond --port 8104 --dtype bfloat16 \ --gpu-memory-utilization 0.08 --enforce-eager --max-model-len 6144 > "${LOG}.vc" 2>&1 & wait_url http://localhost:8104/v1/models && echo " validator_v4 READY (port 8104)" | tee -a "$LOG" $VLLM serve "$FIXER_V2_ORPO" --served-model-name fixer --port 8102 --dtype bfloat16 \ --gpu-memory-utilization 0.15 --enforce-eager --max-model-len 4096 > "${LOG}.f" 2>&1 & wait_url http://localhost:8102/v1/models && echo " fixer_v2_orpo READY" | tee -a "$LOG" $VLLM serve "$SEM_FIXER_ORPO" --served-model-name fixer_v3 --port 8105 --dtype bfloat16 \ --gpu-memory-utilization 0.15 --enforce-eager --max-model-len 4096 > "${LOG}.fs" 2>&1 & wait_url http://localhost:8105/v1/models && echo " sem_fixer_v3_orpo READY" | tee -a "$LOG" OUT_ORPO=eval_results/scaleup_BoN8_d_K8_3stage_planner_iter2_mixedtemp_valv4orpo_fixerv2orpo_semfixerv3orpo_bird_dev.jsonl rm -f "$OUT_ORPO" echo "==== [E] K=8 3-stage: val_v4 ORPO + fixer_v2 ORPO + sem_fixer_v3 ORPO ====" | tee -a "$LOG" $PY scripts/run_pipeline_rollouts.py \ --input_file data/sft_bird_with_evidence_dev_text2sql.json \ --output_file "$OUT_ORPO" \ --planner_host http://localhost:8100 \ --validator_host none \ --validator_sel_host http://localhost:8101 \ --validator_cond_host http://localhost:8104 \ --fixer_host http://localhost:8102 \ --fixer_gate_exec_ok \ --fixer_v3_host http://localhost:8105 \ --K 8 --K_val 1 --K_fix 1 \ --temperature 1.0 --top_p 0.9 \ --max_planner_tokens 1024 --max_validator_tokens 384 --max_fixer_tokens 512 \ --max_questions -1 --n_threads 4 2>&1 | tee -a "$LOG" echo "==== [E] metrics ====" | tee -a "$LOG" $PY scripts/compute_bestofn_metrics.py "$OUT_ORPO" orpo_valv4_fixerv2_semfixerv3 2>&1 | tee -a "$LOG" ############################################## # STAGE F: Apply selector v2 ############################################## kill_vllm echo "==== [F] launching selector v2 ====" | tee -a "$LOG" $VLLM serve "$SEL_V2" --served-model-name selector --port 8103 --dtype bfloat16 \ --gpu-memory-utilization 0.85 --enforce-eager --max-model-len 8192 > "${LOG}.sel" 2>&1 & wait_url http://localhost:8103/v1/models && echo " selector READY" | tee -a "$LOG" label="$(basename $OUT_ORPO .jsonl)_selectorV2rows" $PY scripts/compute_bestofn_with_selector.py \ "$OUT_ORPO" "$label" --selector_host http://localhost:8103 --row_preview 2>&1 | tee -a "$LOG" echo "==== ALL_DONE ====" | tee -a "$LOG"