#!/usr/bin/env bash
# GRPO on the GridSim v1 bundle through AgentEnv's agent loop, as the run the verl page plots ran it on one L40S: the
# Qwen3-4B SFT warm start, LoRA r32/alpha 64, lr 2e-5 cosine with 3 warmup steps, 32 rollouts a step (4 days x 8)
# at temperature 1.1, thinking off, at most 4 model turns a rollout, and evals on the 200 eval days at steps 0, 20
# and 40. Run it from a folder holding qwen3-4b-gridsim-sft and the rows python -m agent_env.training.verl wrote
# to data/, in verl's environment with agentenv-framework[verl] installed; with a server that asks for a token,
# export the variable the rows' --token-var names first.
set -euo pipefail
python -m verl.trainer.main_ppo \
  algorithm.adv_estimator=grpo \
  algorithm.use_kl_in_reward=False \
  data.train_files=data/train.parquet \
  data.val_files=data/eval.parquet \
  data.train_batch_size=4 \
  data.max_prompt_length=3072 \
  data.max_response_length=4096 \
  data.filter_overlong_prompts=False \
  data.return_raw_chat=True \
  +data.apply_chat_template_kwargs.enable_thinking=False \
  actor_rollout_ref.model.path=qwen3-4b-gridsim-sft \
  actor_rollout_ref.model.lora_rank=32 \
  actor_rollout_ref.model.lora_alpha=64 \
  actor_rollout_ref.model.use_remove_padding=True \
  actor_rollout_ref.model.enable_gradient_checkpointing=True \
  actor_rollout_ref.actor.optim.lr=2e-5 \
  actor_rollout_ref.actor.optim.lr_warmup_steps=3 \
  actor_rollout_ref.actor.optim.lr_scheduler_type=cosine \
  actor_rollout_ref.actor.ppo_mini_batch_size=4 \
  actor_rollout_ref.actor.use_dynamic_bsz=True \
  actor_rollout_ref.actor.ppo_max_token_len_per_gpu=16384 \
  actor_rollout_ref.actor.use_kl_loss=False \
  actor_rollout_ref.actor.entropy_coeff=0 \
  actor_rollout_ref.actor.fsdp_config.param_offload=False \
  actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \
  actor_rollout_ref.rollout.name=vllm \
  actor_rollout_ref.rollout.tensor_model_parallel_size=1 \
  actor_rollout_ref.rollout.gpu_memory_utilization=0.5 \
  actor_rollout_ref.rollout.n=8 \
  actor_rollout_ref.rollout.temperature=1.1 \
  actor_rollout_ref.rollout.load_format=safetensors \
  actor_rollout_ref.rollout.layered_summon=False \
  actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True \
  actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=16384 \
  actor_rollout_ref.rollout.multi_turn.enable=True \
  actor_rollout_ref.rollout.multi_turn.format=hermes \
  actor_rollout_ref.rollout.multi_turn.max_assistant_turns=4 \
  actor_rollout_ref.rollout.multi_turn.max_tool_response_length=8192 \
  actor_rollout_ref.rollout.agent.agent_loop_config_path=data/agent_loop.yaml \
  actor_rollout_ref.rollout.agent.default_agent_loop=agentenv \
  actor_rollout_ref.rollout.agent.num_workers=4 \
  actor_rollout_ref.rollout.val_kwargs.temperature=1.1 \
  actor_rollout_ref.rollout.val_kwargs.do_sample=True \
  actor_rollout_ref.rollout.val_kwargs.n=1 \
  trainer.v1.sampler.sync_refill_failed_groups=True \
  trainer.logger='[console]' \
  trainer.project_name=agentenv \
  trainer.experiment_name=gridsim-verl \
  trainer.n_gpus_per_node=1 \
  trainer.nnodes=1 \
  trainer.total_training_steps=40 \
  trainer.test_freq=20 \
  trainer.val_before_train=True \
  trainer.save_freq=-1
