name: e2e_ppo_trainer

on:
  # Trigger the workflow on push or pull request,
  # but only for the main branch
  push:
    branches:
      - main
      - v0.*
  pull_request:
    branches:
      - main
      - v0.*
    paths:
      - "**/*.py"
      # Other entrypoints
      - "!examples/**"
      - "!tests/**"
      - "!verl/trainer/main_*.py"
      - "!verl/trainer/fsdp_sft_trainer.py"
      # Recipes
      - "!recipe/**"
      # Megatron
      - "!verl/workers/**/megatron_*.py"
      # Entrypoints
      - ".github/workflows/e2e_ppo_trainer.yml"
      - "examples/data_preprocess/gsm8k.py"
      - "examples/data_preprocess/geo3k.py"
      - "tests/e2e/ppo_trainer"
      - "verl/trainer/main_ppo.py"
      - "verl/trainer/config/ppo_trainer.yaml"

# Cancel jobs on the same ref if a new one is triggered
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

# Declare permissions just read content.
permissions:
  contents: read

jobs:
  pre_commit_for_ppo:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        python-version: ["3.12"]
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0
        with:
          python-version: ${{ matrix.python-version }}
      - name: Set ruff --output-format=github
        run: |
          sed -i 's/--output-format=full/--output-format=github/' .pre-commit-config.yaml
          git add .pre-commit-config.yaml
      - uses: pre-commit/action@v3.0.1
        with:
          extra_args: "" # Overriding default "--all-files"

  e2e_ppo_trainer_vllm:
    runs-on: [L20x8]
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
      options: --gpus all --shm-size=10g
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install --no-deps -e .[test,vllm]
      - name: Prepare GSM8K dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/gsm8k.py
      # Function RM
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm with validation and saving (FSDP_SIZE=8)
        run: |
          ray stop --force
          VAL_BEFORE_TRAIN=True TEST_FREQ=1 SAVE_FREQ=1 SAVE_HF_MODEL=True VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-fsdp8" bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm after resuming
        run: |
          ray stop --force
          RESUME_MODE=auto VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-fsdp8" bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Test merging FSDP checkpoints (Qwen Actor) 
        run: |
          exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp8"
          python scripts/model_merger.py test --backend fsdp --local_dir checkpoints/verl-test/${exp_name}/global_step_1/actor --test_hf_dir checkpoints/verl-test/${exp_name}/global_step_1/actor/huggingface
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm with validation and saving (DDP_SIZE=2, FSDP_SIZE=4)
        run: |
          ray stop --force
          VAL_BEFORE_TRAIN=True TEST_FREQ=1 SAVE_FREQ=1 SAVE_HF_MODEL=True FSDP_SIZE=4 VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-ddp2-fsdp4" bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Test merging DDP+FSDP checkpoints (Qwen Actor) 
        run: |
          exp_name="qwen2.5-0.5b-function-reward-minimal-ddp2-fsdp4"
          python scripts/model_merger.py test --backend fsdp --local_dir checkpoints/verl-test/${exp_name}/global_step_1/actor --test_hf_dir checkpoints/verl-test/${exp_name}/global_step_1/actor/huggingface
      - name: Running GSM8K E2E without rmpad using function rm
        run: |
          ray stop --force
          RM_PAD=False bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm (GRPO)
        run: |
          ray stop --force
          ADV_ESTIMATOR=grpo USE_KL=True bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm (ReMax)
        run: |
          ray stop --force
          ADV_ESTIMATOR=remax USE_KL=True bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using customized reward function
        run: |
          ray stop --force
          CUSTOM_REWARD_FN=True bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm with in-reward kl and kl loss
        run: |
          ray stop --force
          USE_KL=True bash tests/e2e/ppo_trainer/run_function_reward.sh
      # LoRA tests
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with grpo lora using function rm with use_shm
        run: |
          ray stop --force
          ADV_ESTIMATOR=grpo USE_SHM=True LORA_RANK=32 LOAD_FORMAT=safetensors bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with grpo lora using function rm with use_shm and layered_summon
        run: |
          ray stop --force
          ADV_ESTIMATOR=grpo USE_SHM=True LORA_RANK=32 LOAD_FORMAT=safetensors LAYERED_SUMMON=True bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with grpo lora using function rm with use_shm and layered_summon with fsdp2
        run: |
          ray stop --force
          ADV_ESTIMATOR=grpo USE_SHM=True LORA_RANK=32 LOAD_FORMAT=safetensors LAYERED_SUMMON=True STRATEGY=fsdp2 bash tests/e2e/ppo_trainer/run_function_reward.sh
      # Model RM
      - name: Running GRPO GSM8K E2E training tests with FSDP on 8 L20 GPUs (DeepSeek)
        run: |
          ray stop --force
          MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct bash tests/e2e/ppo_trainer/run_function_reward.sh
      - name: Running GSM8K E2E with rmpad using model rm
        run: |
          ray stop --force
          bash tests/e2e/ppo_trainer/run_model_reward.sh
      - name: Running GSM8K E2E without rmpad using model rm
        run: |
          ray stop --force
          RM_PAD=False bash tests/e2e/ppo_trainer/run_model_reward.sh
      - name: Running GSM8K E2E with rmpad using model rm and ulysses sp=2
        run: |
          ray stop --force
          SP_SIZE=2 bash tests/e2e/ppo_trainer/run_model_reward.sh
      - name: Running GSM8K E2E with rmpad using model rm and dynamic batch size
        run: |
          ray stop --force
          SEQ_BALANCE=True bash tests/e2e/ppo_trainer/run_model_reward.sh
      - name: Running GSM8K E2E with rmpad using model rm with Liger Kernel enabled
        run: |
          ray stop --force
          LIGER=True bash tests/e2e/ppo_trainer/run_model_reward.sh

  e2e_ppo_trainer_vllm_vlm:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: hiyouga/verl:ngc-th2.6.0-cu126-vllm0.8.3-flashinfer0.2.2-cxx11abi0
      options: --gpus all --shm-size=50g # Visual dataloader requires large memory
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,geo,vllm]
      # Geo3k
      - name: Prepare Geo3k dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/geo3k.py
      - name: Running Geo3k VLM E2E training tests on 8 L20 GPUs with rmpad using function rm
        run: |
          ray stop --force
          TRAIN_FILES=$HOME/data/geo3k/train.parquet VAL_FILES=$HOME/data/geo3k/test.parquet \
            MAX_PROMPT_LEN=1536 MAX_RESPONSE_LEN=1536 \
            MODEL_ID=Qwen/Qwen2-VL-2B-Instruct \
            ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \
            SP_SIZE=2 \
            bash tests/e2e/ppo_trainer/run_function_reward.sh

  e2e_ppo_trainer_sglang:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
      options: --gpus all --shm-size=10g
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,gpu,sglang] --no-deps
      - name: Prepare gsm8k dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/gsm8k.py
      - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm and save ckpt
        run: |
          ray stop --force
          ENGINE=sglang bash tests/e2e/ppo_trainer/run_function_reward.sh

  e2e_ppo_trainer_sglang_multiturn_with_tool:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
      options: --gpus all --shm-size=10g
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,gpu,sglang] --no-deps
      - name: Prepare gsm8k dataset with tool
        run: |
          ray stop --force
          python3 examples/data_preprocess/gsm8k_multiturn_w_tool.py --local_dir $HOME/data/gsm8k_verl_sgl_multi_turn_preprocessed
      - name: Running GSM8K with tool E2E training tests on 8 L20 GPUs with rmpad using function rm and save ckpt with sglang
        run: |
          ray stop --force
          bash tests/e2e/run_gsm8k_fsdp_sgl_multiturn_w_tool.sh
      - name: Running GSM8K with tool E2E training tests with FSDP2
        run: |
          ray stop --force
          FSDP_STRATEGY=fsdp2 bash tests/e2e/run_gsm8k_fsdp_sgl_multiturn_w_tool.sh

  e2e_ppo_trainer_sglang_vlm:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
      options: --gpus all --shm-size=50g # Visual dataloader requires large memory
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,geo,gpu,sglang]
      # Geo3k
      - name: Prepare Geo3k dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/geo3k.py
      - name: Running Geo3k VLM E2E training tests on 8 L20 GPUs with rmpad using function rm
        run: |
          ray stop --force
          TRAIN_FILES=$HOME/data/geo3k/train.parquet VAL_FILES=$HOME/data/geo3k/test.parquet \
            MAX_PROMPT_LEN=1536 MAX_RESPONSE_LEN=1536 \
            MODEL_ID=Qwen/Qwen2-VL-2B-Instruct \
            ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \
            ENGINE=sglang GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \
            ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \
            bash tests/e2e/ppo_trainer/run_function_reward.sh

  e2e_ppo_trainer_fused_kernels_vllm:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: hiyouga/verl:ngc-th2.6.0-cu126-vllm0.8.3-flashinfer0.2.2-cxx11abi0
      options: --gpus all --shm-size=50g # Visual dataloader requires large memory
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,geo,vllm]
      # Geo3k
      - name: Prepare Geo3k dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/geo3k.py
      - name: Running Geo3k VLM E2E with rmpad using fused kernel (Qwen2.5-VL)
        run: |
          ray stop --force
          FUSED_KERNELS=True TRAIN_FILES=$HOME/data/geo3k/train.parquet VAL_FILES=$HOME/data/geo3k/test.parquet \
            MAX_PROMPT_LEN=1536 MAX_RESPONSE_LEN=1536 \
            MODEL_ID=Qwen/Qwen2.5-VL-3B-Instruct \
            ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \
            GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \
            ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \
            bash tests/e2e/ppo_trainer/run_function_reward.sh

  e2e_ppo_trainer_fused_kernels_sglang:
    runs-on: [L20x8]
    needs: pre_commit_for_ppo
    timeout-minutes: 40 # Increase this timeout value as needed
    env:
      HTTP_PROXY: ${{ secrets.PROXY_HTTP }}
      HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }}
      NO_PROXY: "localhost,127.0.0.1,hf-mirror.com"
      HF_ENDPOINT: "https://hf-mirror.com"
      HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable
    container:
      image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6.post5-mcore0.12.0-te2.3
      options: --gpus all --shm-size=50g # Visual dataloader requires large memory
    steps:
      - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
        with:
          fetch-depth: 0
      - name: Install the current repository
        run: |
          pip3 install -e .[test,geo,gpu,sglang]
      - name: Prepare Geo3k dataset
        run: |
          ray stop --force
          python3 examples/data_preprocess/geo3k.py
      - name: Running Geo3k VLM E2E with rmpad using fused kernel (Qwen2.5-VL)
        run: |
          ray stop --force
          FUSED_KERNELS=True TRAIN_FILES=$HOME/data/geo3k/train.parquet VAL_FILES=$HOME/data/geo3k/test.parquet \
            MAX_PROMPT_LEN=1536 MAX_RESPONSE_LEN=1536 \
            MODEL_ID=Qwen/Qwen2.5-VL-3B-Instruct \
            ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \
            ENGINE=sglang GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \
            ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \
            bash tests/e2e/ppo_trainer/run_function_reward.sh