#!/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=12:00:00 #SBATCH --output=/weka/s225250685/mats-tist/slurm_logs/orpo_v3_%j.out # ============================================================ # ORPO v3 — fixes over v2: # 1. Separate validator-sel and validator-cond ORPO models # (cleaner training signal vs combined model) # 2. Prompt format aligned with inference exactly # (field labels: "database schema:", "External knowledge:", # "Generated SQL query:", "Execution response:") # 3. Semantic fixer preserve pairs use SAME prompt as inference # (not PRESERVE_PROMPT which never appears at inference) # 4. Preserve rejected = cross-question wrong SQL (valid ORPO contrast) # 5. Exec-error fixer uses EXEC_FIXER_PROMPT at inference # (no validator critique, "Failed SQL" / "Execution error" labels) # 6. 3 epochs for validators (vs 2 in v2) # ============================================================ 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=$AH/output/planner-iter2-collab-3B SEL_V2=$AH/output/selector-3B-v2-rows VAL_SEL=$AH/output/validator-sel-v4-orpo VAL_COND=$AH/output/validator-cond-v4-orpo FIXER_V2=$AH/output/fixer-v2-1.5B-execerr-orpo-expanded SEM_FIXER=$AH/output/semantic-fixer-v3-1.5B-orpo-nogate LOG=/weka/s225250685/mats-tist/slurm_logs/orpo_v3_${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 ORPO training data ====" | tee -a "$LOG" echo " [A1] exec-error fixer data..." | tee -a "$LOG" $PY scripts/build_fixer_v2_execerr.py 2>&1 | tee -a "$LOG" [ -L data/hf_fixer_v2_execerr_expanded ] || ln -sf hf_fixer_v2_execerr data/hf_fixer_v2_execerr_expanded echo " [A2] validator v4 ORPO data (aligned prompts)..." | tee -a "$LOG" $PY scripts/build_validator_v4_orpo.py 2>&1 | tee -a "$LOG" # Split combined validator data into separate sel and cond datasets echo " [A2b] splitting validator data into sel/cond..." | tee -a "$LOG" $PY - << 'PYEOF' from datasets import load_from_disk, DatasetDict, Dataset d = load_from_disk("data/hf_validator_v4_orpo") for role in ["sel", "cond"]: tag = "select" if role == "sel" else "condition" for split in ["train_dpo", "test_dpo"]: ds = d[split].filter(lambda x: x["role"] == tag) print(f" {role} {split}: {len(ds)}") DatasetDict({ "train_dpo": d["train_dpo"].filter(lambda x: x["role"] == tag), "test_dpo": d["test_dpo"].filter(lambda x: x["role"] == tag), }).save_to_disk(f"data/hf_validator_v4_{role}_orpo") print(f" saved → data/hf_validator_v4_{role}_orpo") PYEOF echo " [A3] semantic fixer v3 data (fixed preserve pairs)..." | tee -a "$LOG" $PY scripts/build_semantic_fixer_v3.py 2>&1 | tee -a "$LOG" echo "==== [A] data build done ====" | tee -a "$LOG" ############################################## # STAGE B: ORPO validator-sel (0.5B) — skip if already trained ############################################## if [ -f "$VAL_SEL/config.json" ]; then echo "==== [B] validator-sel already exists, skipping ====" | tee -a "$LOG" else echo "==== [B] ORPO validator-sel (0.5B) ====" | tee -a "$LOG" cd $AH PYTHONPATH=src/ ACCELERATE_LOG_LEVEL=info $ACCEL launch \ --main_process_port 29621 \ --config_file recipes/accelerate_configs/single_gpu0_local.yaml \ scripts/run_orpo.py \ recipes/scaleup-3stage/orpo-validator-sel-v4.yaml 2>&1 | tee -a "$LOG" cd /weka/s225250685/mats-tist if [ ! -f "$VAL_SEL/config.json" ]; then echo "VALIDATOR-SEL ORPO FAILED" | tee -a "$LOG"; exit 1 fi fi echo "==== [B] validator-sel ready: $VAL_SEL ====" | tee -a "$LOG" ############################################## # STAGE C: ORPO validator-cond (0.5B) — skip if already trained ############################################## if [ -f "$VAL_COND/config.json" ]; then echo "==== [C] validator-cond already exists, skipping ====" | tee -a "$LOG" else echo "==== [C] ORPO validator-cond (0.5B) ====" | tee -a "$LOG" cd $AH PYTHONPATH=src/ ACCELERATE_LOG_LEVEL=info $ACCEL launch \ --main_process_port 29622 \ --config_file recipes/accelerate_configs/single_gpu0_local.yaml \ scripts/run_orpo.py \ recipes/scaleup-3stage/orpo-validator-cond-v4.yaml 2>&1 | tee -a "$LOG" cd /weka/s225250685/mats-tist if [ ! -f "$VAL_COND/config.json" ]; then echo "VALIDATOR-COND ORPO FAILED" | tee -a "$LOG"; exit 1 fi fi echo "==== [C] validator-cond ready: $VAL_COND ====" | tee -a "$LOG" ############################################## # STAGE D+E: Fixer v2 and semantic fixer — reuse from ORPO v2 (already trained) ############################################## if [ ! -f "$FIXER_V2/config.json" ]; then echo "FIXER V2 MISSING at $FIXER_V2" | tee -a "$LOG"; exit 1 fi echo "==== [D] fixer v2 ready (reusing ORPO v2): $FIXER_V2 ====" | tee -a "$LOG" if [ ! -f "$SEM_FIXER/config.json" ]; then echo "SEMANTIC FIXER MISSING at $SEM_FIXER" | tee -a "$LOG"; exit 1 fi echo "==== [E] semantic fixer ready (reusing ORPO v2): $SEM_FIXER ====" | tee -a "$LOG" ############################################## # STAGE F: 5-endpoint rollout (K=8, no-gate sem fixer) # H200=141GB: planner(0.30)+val_sel(0.08)+val_cond(0.08)+fixer_v2(0.15)+sem_fixer(0.15)=0.76 ############################################## kill_vllm echo "==== [F] launching 5 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" $VLLM serve "$VAL_SEL" --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-sel READY" | tee -a "$LOG" $VLLM serve "$VAL_COND" --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-cond READY" | tee -a "$LOG" $VLLM serve "$FIXER_V2" --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 READY" | tee -a "$LOG" $VLLM serve "$SEM_FIXER" --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 READY" | tee -a "$LOG" # sem_fixer_no_gate REMOVED: proven to break 31.7% of correct trajs (10.4x break:rescue). # Semantic fixer now gated by validator flags only. OUT=eval_results/scaleup_BoN8_d_K8_3stage_orpov3_sepval_gated_bird_dev.jsonl rm -f "$OUT" echo "==== [F] K=8 rollout: exec-err gate + sem-fixer GATED by validator ====" | tee -a "$LOG" $PY scripts/run_pipeline_rollouts.py \ --input_file data/sft_bird_with_evidence_dev_text2sql.json \ --output_file "$OUT" \ --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 "==== [F] oracle/greedy metrics ====" | tee -a "$LOG" $PY scripts/compute_bestofn_metrics.py "$OUT" orpov3_sepval_gated 2>&1 | tee -a "$LOG" ############################################## # STAGE G: Selector v2 ############################################## kill_vllm echo "==== [G] selector v2 apply ====" | 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="orpov3_sepval_gated_selectorV2rows" $PY scripts/compute_bestofn_with_selector.py \ "$OUT" "$label" --selector_host http://localhost:8103 --row_preview 2>&1 | tee -a "$LOG" echo "==== ALL_DONE ====" | tee -a "$LOG"