#!/usr/bin/env bash
set -uo pipefail

# ===== 可调参数（用环境变量覆盖）=====
GPU=${GPU:-1}  
SEED=${SEED:-42}  
BATCH=${BATCH:-8}             # OOM 就设 4
N_OUTER=${N_OUTER:-5}
N_INNER=${N_INNER:-3}
N_TRIALS=${N_TRIALS:-20}
EPOCHS=${EPOCHS:-20}          # 每个 trial 最大 epoch
PATIENCE=${PATIENCE:-5}       # 内层早停耐心值（配合 min_delta 生效）

# 固定运行目录（按 seed 区分）→ 断点续跑的关键
RUN_DIR="models/abl_qwen_softrag/seed${SEED}"

export TRANSFORMERS_OFFLINE=1
export PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
export TOKENIZERS_PARALLELISM=false

mkdir -p "${RUN_DIR}"
echo "[$(date)] START seed=${SEED} gpu=${GPU} batch=${BATCH} dir=${RUN_DIR}"

CUDA_VISIBLE_DEVICES=${GPU} python -m lnp_ml.modeling.nested_cv_optuna \
  --input-path data/interim/internal.csv \
  --output-dir models/abl_qwen_softrag \
  --resume-dir "${RUN_DIR}" \
  --seed ${SEED} \
  --use-llm --use-rag --rag-top-k 4 \
  --use-soft-prompt --llm-use-qlora \
  --llm-model-path models/qwen2.5-7b-instruct \
  --no-llm-freeze \
  --n-outer-folds ${N_OUTER} --n-inner-folds ${N_INNER} \
  --n-trials ${N_TRIALS} --epochs-per-trial ${EPOCHS} \
  --inner-patience ${PATIENCE} \
  --batch-size ${BATCH} --device cuda \
  2>&1 | tee -a "${RUN_DIR}/run.log"

echo "[$(date)] DONE seed=${SEED} (exit=${PIPESTATUS[0]})"