__conda_setup="$('/usr/local/lib/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/usr/local/lib/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/usr/local/lib/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/usr/local/lib/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup

conda activate .env/skyrl
cd SkyRL

ray stop
# launch the master node of ray in container
ray start --head --node-ip-address 0.0.0.0 --num-gpus 8

export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
# export CUDA_VISIBLE_DEVICES=0,1
export DATA_DIR='data/SkyRL-SQL-653-data'
DB_PATH='data/OmniSQL-datasets/data'
BASE_MODEL='/mnt/cache/sharemath/models/Qwen/Qwen2.5-Coder-7B-Instruct'
CKPT_PATH='outs'

PROJECT_NAME='MultiTurn-Experiment'
EXPERIMENT_NAME="SkyRL-SQL-1.5B"

KL_LOSS_COEF=0.001
ENTROPY_COEFF=0
KL_LOSS_TYPE=low_var_kl
N_AGENT=5
N_TURNS=5
TEMP=0.6
TOPP=0.95
USE_KL_LOSS=False
LR=1e-6
CLIP_LOW=0.2
CLIP_HIGH=0.2
GRAD_CLIP=0.5
BATCH_SIZE=256
TP_SIZE=4

# PYTHONUNBUFFERED=1 uv run --extra sql --directory . --frozen --env-file .env.sql -m verl.trainer.main_ppo \
PYTHONUNBUFFERED=1 uv run --extra sql --isolated --directory . --frozen --env-file .env.sql -m verl.trainer.main_ppo \
    data.train_files=$DATA_DIR/train.parquet \
    data.val_files=$DATA_DIR/validation.parquet \
    data.train_batch_size=$BATCH_SIZE \
    data.dataloader_num_workers=0 \
    data.val_batch_size=1 \
    data.max_prompt_length=29048 \
    data.max_response_length=3000 \
    actor_rollout_ref.rollout.sql.max_prompt_length=29048 \
    actor_rollout_ref.rollout.sql.max_response_length=3000 \
    actor_rollout_ref.rollout.sql.max_start_length=6144 \
    actor_rollout_ref.rollout.sql.max_obs_length=3000 \
    algorithm.adv_estimator=grpo \
    actor_rollout_ref.model.path=$BASE_MODEL \
    actor_rollout_ref.actor.use_kl_loss=$USE_KL_LOSS \
    actor_rollout_ref.actor.clip_ratio_high=$CLIP_HIGH \
    actor_rollout_ref.actor.clip_ratio_low=$CLIP_LOW \
    actor_rollout_ref.model.enable_gradient_checkpointing=true \
    actor_rollout_ref.actor.fsdp_config.param_offload=True \
    actor_rollout_ref.actor.fsdp_config.optimizer_offload=True \
    actor_rollout_ref.model.use_remove_padding=True \
    actor_rollout_ref.actor.optim.lr=$LR \
    actor_rollout_ref.actor.ppo_mini_batch_size=$BATCH_SIZE \
    actor_rollout_ref.actor.ppo_micro_batch_size=8 \
    actor_rollout_ref.actor.kl_loss_coef=$KL_LOSS_COEF \
    actor_rollout_ref.actor.kl_loss_type=$KL_LOSS_TYPE \
    actor_rollout_ref.actor.entropy_coeff=$ENTROPY_COEFF \
    actor_rollout_ref.rollout.log_prob_micro_batch_size=64 \
    actor_rollout_ref.rollout.tensor_model_parallel_size=$TP_SIZE \
    actor_rollout_ref.rollout.n_trajectories=$N_AGENT \
    actor_rollout_ref.rollout.max_iterations=$N_TURNS \
    actor_rollout_ref.rollout.name=async \
    actor_rollout_ref.rollout.enable_memory_saver=True \
    actor_rollout_ref.rollout.task_type='sql' \
    reward_model.reward_manager=sql \
    actor_rollout_ref.rollout.sql.db_path=$DB_PATH \
    actor_rollout_ref.rollout.gpu_memory_utilization=0.9 \
    actor_rollout_ref.ref.log_prob_micro_batch_size=64 \
    actor_rollout_ref.ref.fsdp_config.param_offload=True \
    actor_rollout_ref.rollout.sampling_params.temperature=$TEMP \
    actor_rollout_ref.rollout.sampling_params.top_p=$TOPP \
    actor_rollout_ref.actor.masking=true \
    trainer.logger=['wandb'] \
    trainer.n_gpus_per_node=8 \
    trainer.nnodes=1 \
    trainer.save_freq=5 \
    trainer.test_freq=-1 \
    trainer.project_name=$PROJECT_NAME \
    trainer.experiment_name=$EXPERIMENT_NAME \
    trainer.total_epochs=20 \
    trainer.default_local_dir=$CKPT_PATH/$PROJECT_NAME/$EXPERIMENT_NAME \
    trainer.max_actor_ckpt_to_keep=10 \
    trainer.max_critic_ckpt_to_keep=10 \
    actor_rollout_ref.actor.grad_clip=$GRAD_CLIP \
    2>&1 