diff --git a/verl_0720_main/27b.sh b/verl_0720_main/27b.sh new file mode 100644 index 0000000000000000000000000000000000000000..4b29bcd857377753ab567932c44bb5288f4a0b3a --- /dev/null +++ b/verl_0720_main/27b.sh @@ -0,0 +1,1115 @@ +#!/bin/bash +# Qwen3.5-27B Nanoclaw 多轮 GRPO — VERL 0720 + NPU SP1/31K 长轨迹全优化版 +# +# 关键修改: +# 1) 入口改为 python3 -m verl.trainer.main_ppo,不再使用 recipe.grpo_mindspeed_mm.main_ppo; +# 2) 不再使用 MM_CONFIG_FILE / MindSpeed-MM YAML; +# 3) 显式 text-only:data.return_multi_modal_inputs=False; +# 4) 数据输入改为 Nanoclaw base_tasks 目录,不再使用 Retool parquet/json; +# 5) 保留 27B 作业验证过的 HCCL buffer 与端口范围,降低 HcclAllreduce socket/资源压力; +# 6) 默认 val n=1、log_val_generations=10,降低验证额外资源压力; +# 7) 使用 FSDP2、reshard-after-forward、CPU offload policy、gradient checkpointing 和 activation offload; +# 8) 使用最新版 VERL V1/TransferQueue 和 nanoclaw_recipe,并按参考 YAML 的训练意图映射优化器与 KL; +# 9) 恢复正式训练所需的 22768 response / 16384 assistant 长轨迹预算; +# 10) actor/ref 的全词表 entropy 使用 256-token 分块; +# 11) 启用分块 Torch LM-head,避免完整 logits + NPU cross entropy 的多 GiB 峰值。 + +set -x + +# ================= 正式长轨迹全优化版:Qwen3.5-27B NPU SP1 + 约 31K ================= +export TRAIN_SP=1 +export QWEN35_FLA_BACKEND=disabled +export MAX_PROMPT_LENGTH=8192 +export MAX_RESPONSE_LENGTH=22768 +export MAX_ASSISTANT_RESPONSE_LENGTH=16384 +export MAX_TOOL_RESPONSE_LENGTH=8192 +# 8192 + 22768 = 30960,向上对齐到 32768,确保单条最长轨迹能够组成独立 micro-batch。 +export ACTOR_MAX_TOKEN_LEN_PER_GPU=32768 +export LOG_PROB_MAX_TOKEN_LEN_PER_GPU=32768 +export ROLLOUT_MAX_NUM_BATCHED_TOKENS=32768 +export ACTOR_STRATEGY=fsdp2 +export OFFLOAD=True +# 当前 VERL 的 checkpointing 分支不会传递自定义 chunk size;稳定版明确关闭它,确保实际使用 256。 +export ENTROPY_FROM_LOGITS_WITH_CHUNKING=True +export ENTROPY_FROM_LOGITS_CHUNK_SIZE=256 +export ENTROPY_CHECKPOINTING=False +# 27B 普通路径已实测在 actor update 的 npu_cross_entropy_loss 额外申请 +# 7.23 GiB 时 OOM。fused Torch LM-head 按 token 分块直接计算 log-prob/entropy。 +export USE_FUSED_KERNELS=True +export FUSED_KERNEL_BACKEND=torch +# 27B/31K 默认同时打开 activation offload 和 gradient checkpointing;保留故障隔离开关。 +export ENABLE_ACTIVATION_OFFLOAD=${ENABLE_ACTIVATION_OFFLOAD:-True} + + +npu-smi info || true +pip install --upgrade pip +pip uninstall -y moxing-framework || true + +# ================= 路径配置 ================= +SCRIPT_DIR=/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/verl_0720_nanoclaw/verl_0720_main +if [ -f "${SCRIPT_DIR}/verl/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +elif [ -f "${SCRIPT_DIR}/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR} +else + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +fi +WORK_DIR=${WORK_DIR:-${DEFAULT_WORK_DIR}} +INSTALL_DIR=${INSTALL_DIR:-/home/ma-user} +BKGS=${BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs} +chmod 755 "${INSTALL_DIR}" + +# Nanoclaw 自定义包已随 WORK_DIR 提供:nanoclaw_recipe。 + +GCC_INSTALL_PREFIX=${GCC_INSTALL_PREFIX:-/home/ma-user/gcc-11.3.0} +COMPILED_GCC_ARCHIVE_PATH=${COMPILED_GCC_ARCHIVE_PATH:-/opt/huawei/dataset/zyr_yuyin/bkgs/gcc-11.3.0-compiled-aarch64.tar.gz} + +echo "--> 正在从缓存恢复 GCC 11.3.0..." +tar -xzf "${COMPILED_GCC_ARCHIVE_PATH}" -C /home/ma-user/ +export PATH=${GCC_INSTALL_PREFIX}/bin:${PATH} +export LD_LIBRARY_PATH=${GCC_INSTALL_PREFIX}/lib64:${GCC_INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH:-} +export CC=${GCC_INSTALL_PREFIX}/bin/gcc +export CXX=${GCC_INSTALL_PREFIX}/bin/g++ +echo "--> 验证 GCC 版本:" +gcc --version + +cd "${BKGS}" +cp jemalloc-5.3.0.tar.bz2 "${INSTALL_DIR}" + +VLLM_LATEST_PKGS=${VLLM_LATEST_PKGS:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-05-12/verl_new_26_05_09/pkgs} +rm -rf "${INSTALL_DIR}/vllm" "${INSTALL_DIR}/vllm-ascend" +cp -r "${VLLM_LATEST_PKGS}/vllm" "${INSTALL_DIR}" +cp -r "${VLLM_LATEST_PKGS}/vllm-ascend" "${INSTALL_DIR}" + +CANN_BKGS=${CANN_BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs/cann_0527} +cp "${CANN_BKGS}/Ascend-cann-toolkit_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-910b-ops_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-nnal_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" + +echo "################" +echo "## set verl env" +echo "################" + +cd "${INSTALL_DIR}" + +chmod +x Ascend-cann-toolkit_9.0.0_linux-aarch64.run +bash Ascend-cann-toolkit_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/ascend-toolkit/set_env.sh" + +chmod +x Ascend-cann-910b-ops_9.0.0_linux-aarch64.run +bash Ascend-cann-910b-ops_9.0.0_linux-aarch64.run --install --quiet + +chmod +x Ascend-cann-nnal_9.0.0_linux-aarch64.run +bash Ascend-cann-nnal_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/nnal/atb/set_env.sh" + +export ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME} +export LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:${LD_LIBRARY_PATH:-} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip3 install torch==2.9.0 +pip3 install pyyaml setuptools +pip3 install torch-npu==2.9.0 +pip3 install torchvision==0.24.0 torchaudio==2.9.0 + +ASCEND_TOOLKIT_PYTHON_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/python/site-packages +export PYTHONPATH=${PYTHONPATH:-}:${INSTALL_DIR}:${ASCEND_TOOLKIT_PYTHON_PATH} +pip install pybind11==2.13.6 + +cd "${INSTALL_DIR}/vllm" +VLLM_TARGET_DEVICE=empty pip install . + +cd "${INSTALL_DIR}/vllm-ascend" +pip install -e . +export VLLM_LOGGING_LEVEL=INFO + +cd "${INSTALL_DIR}" +tar -xvf jemalloc-5.3.0.tar.bz2 +cd jemalloc-5.3.0 +./configure --prefix="${INSTALL_DIR}" +make -j"$(nproc)" +make install +export LD_PRELOAD=${INSTALL_DIR}/lib/libjemalloc.so.2:${LD_PRELOAD:-} + +# # ================= 可选:安装 MindSpeed 栈 ================= +# # 纯 VERL engine 路线不依赖 MindSpeed-MM YAML。默认不安装,避免和新版 VERL engine 混淆。 +# INSTALL_MINDSPEED_STACK=${INSTALL_MINDSPEED_STACK:-0} +# if [ "${INSTALL_MINDSPEED_STACK}" = "1" ]; then +# MindSpeed_PATH=${MindSpeed_PATH:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-slow-stable} +# cd "${MindSpeed_PATH}" +# cd Megatron-LM && pip install -e . --no-deps && cd .. +# cd MindSpeed && pip install -e . --no-deps && cd .. +# cd MindSpeed-MM && mkdir -p logs data ckpt && pip install -e . --no-deps && cd .. +# pip install beartype bs4 diffusers==0.30.3 ftfy imageio-ffmpeg pandarallel pytest-mock +# else +# echo "--> Skip MindSpeed/MindSpeed-MM installation for pure VERL engine route." +# fi + + + +# ================= 安装 Triton-Ascend 3.2.1 ================= +# 1. 卸载 triton(增加 -y 自动确认) +pip uninstall -y triton + +# 2. 卸载 triton-ascend(增加 -y 自动确认) +pip uninstall -y triton-ascend +pip install --no-cache-dir --force-reinstall triton==3.5.0 +pip install --no-deps /opt/huawei/dataset/zyr_yuyin/bkgs/triton_ascend-3.2.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl + + +# ================= 安装新版 VERL ================= +cd "${WORK_DIR}" +pip install -r requirements-npu.txt +# NPU requirements 明确要求 numpy<2;editable 安装不能再次按 setup.py 把 NumPy升级到 2.x。 +python3 -m pip install -e . --no-deps +pip install --upgrade 'urllib3==1.26.11' +pip install loguru +pip install tree_sitter==0.21.3 +pip install tree-sitter-java==0.21.0 +pip install tree-sitter-javascript==0.21.4 + +ACL_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/aarch64-linux/lib64 +export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ACL_PATH} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip uninstall -y transformers || true +pip install transformers==5.3.0 +pip install accelerate==1.13.0 mathruler +pip install jsonargparse +pip install deepdiff sympy html2text requests bs4 mpmath swanlab PandoraBox json_repair openai httpx + +# 稳定版只允许 SP=1:不安装普通 FLA,也不进入尚未完成 NPU 适配的 Ulysses CP。 +if [ "${TRAIN_SP}" != "1" ]; then + echo "ERROR: the long-sequence all-optimization profile requires TRAIN_SP=1; got ${TRAIN_SP}." >&2 + exit 2 +fi +export NANOCLAW_REQUIRE_FLA=0 +echo "--> Stable SP1: flash-linear-attention is disabled; Qwen3.5 will not build an Ulysses CP context." + +# Transformers 5.x 会经 sklearn 间接导入 pandas/scipy。固定同一套 NumPy ABI, +# 避免出现 "numpy.dtype size changed"。这些版本均支持 Python 3.11/aarch64。 +NUMPY_VERSION=${NUMPY_VERSION:-1.26.4} +PANDAS_VERSION=${PANDAS_VERSION:-2.2.3} +SCIPY_VERSION=${SCIPY_VERSION:-1.14.1} +SKLEARN_VERSION=${SKLEARN_VERSION:-1.6.1} +python3 -m pip install --no-cache-dir --force-reinstall \ + "numpy==${NUMPY_VERSION}" \ + "pandas==${PANDAS_VERSION}" \ + "scipy==${SCIPY_VERSION}" \ + "scikit-learn==${SKLEARN_VERSION}" + +python3 - <<'PY' || exit 2 +import numpy +import pandas +import scipy +import sklearn +import sys +import transformers +import vllm + +fla_version = "disabled-sp1" + +print( + "[python_stack_preflight] " + f"python={sys.executable} " + f"numpy={numpy.__version__} " + f"pandas={pandas.__version__} " + f"scipy={scipy.__version__} " + f"sklearn={sklearn.__version__} " + f"transformers={transformers.__version__} " + f"vllm={vllm.__version__} " + f"fla={fla_version}" +) +print( + "[python_stack_paths] " + f"numpy={numpy.__file__} " + f"pandas={pandas.__file__}" +) +PY +pip list + +# ================= 检查 Nanoclaw recipe ================= +if [ ! -f "${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" ]; then + echo "ERROR: Nanoclaw recipe not found: ${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" >&2 + exit 2 +fi +test -f "${WORK_DIR}/nanoclaw_recipe/__init__.py" + +# Qwen3.5 MRoPE position_ids 是 3/4 轴张量。未应用此补丁时,NPU +# FlashAttention 会把 seqLen 重复累计(例如 T=10131、sum(seqLen)=30393)。 +QWEN35_MONKEY_PATCH_FILE=${WORK_DIR}/verl/models/transformers/monkey_patch.py +if [ ! -f "${QWEN35_MONKEY_PATCH_FILE}" ] || ! grep -q "def _normalize_fa_position_ids" "${QWEN35_MONKEY_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FlashAttention position_ids normalization patch is missing: ${QWEN35_MONKEY_PATCH_FILE}" >&2 + echo "Upload the modified verl/ directory together with this standalone script." >&2 + exit 2 +fi + +# Nanoclaw rollout model-version metadata 修复;否则完成 actor update 后会在 +# _compute_metrics 中把 min_global_steps=None 强转 int 而崩溃。 +TOOL_AGENT_LOOP_FILE=${WORK_DIR}/verl/experimental/agent_loop/tool_agent_loop.py +TRAINER_BASE_FILE=${WORK_DIR}/verl/trainer/ppo/v1/trainer_base.py +if [ ! -f "${TOOL_AGENT_LOOP_FILE}" ] || ! grep -q "output_min_global_steps" "${TOOL_AGENT_LOOP_FILE}"; then + echo "ERROR: Nanoclaw rollout version metadata merge fix is missing: ${TOOL_AGENT_LOOP_FILE}" >&2 + exit 2 +fi +if [ ! -f "${TRAINER_BASE_FILE}" ] || ! grep -q "def resolve_model_version" "${TRAINER_BASE_FILE}"; then + echo "ERROR: PPO metrics None-version fallback fix is missing: ${TRAINER_BASE_FILE}" >&2 + exit 2 +fi + +# 27B 必须使用 Qwen3.5 分块 fused LM-head。若训练机没有同步更新后的 VERL, +# 在启动 Ray 前直接退出,避免再次运行四十分钟后才在 actor cross entropy OOM。 +QWEN35_MODEL_PATCH_FILE=${WORK_DIR}/verl/models/transformers/qwen3_5.py +FUSED_LINEAR_FILE=${WORK_DIR}/verl/utils/experimental/torch_functional.py +if [ ! -f "${QWEN35_MODEL_PATCH_FILE}" ] || ! grep -q "def forward_with_torch_backend" "${QWEN35_MODEL_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 fused Torch backend is missing: ${QWEN35_MODEL_PATCH_FILE}" >&2 + exit 2 +fi +if ! grep -q "vocab_weights.full_tensor().to(hidden_states.device)" "${QWEN35_MODEL_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FSDP2 CPU-offload fused LM-head device-staging fix is missing: ${QWEN35_MODEL_PATCH_FILE}" >&2 + exit 2 +fi +if [ ! -f "${FUSED_LINEAR_FILE}" ] || ! grep -q "class FusedLinearForPPO" "${FUSED_LINEAR_FILE}"; then + echo "ERROR: chunked FusedLinearForPPO is missing: ${FUSED_LINEAR_FILE}" >&2 + exit 2 +fi + +ACTIVATION_OFFLOAD_FILE=${WORK_DIR}/verl/utils/activation_offload.py +if [ ! -f "${ACTIVATION_OFFLOAD_FILE}" ] || ! grep -q "Missing offload mapping for group" "${ACTIVATION_OFFLOAD_FILE}"; then + echo "ERROR: FSDP2 checkpoint activation-offload on-demand reload fix is missing: ${ACTIVATION_OFFLOAD_FILE}" >&2 + exit 2 +fi + +# ================= PLOG ================= +ma_vj_name=$(echo "${MA_VJ_NAME}" | sed 's:ma-job:modelarts-job:g') +task_name=worker-${VC_TASK_INDEX} +task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name} +mkdir -p "${task_plog_path}" +export ASCEND_PROCESS_LOG_PATH=${task_plog_path}/${VC_TASK_INDEX} +echo "plog path: ${ASCEND_PROCESS_LOG_PATH}" + +MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-${VC_TASK_INDEX}.${MA_VJ_NAME} +MASTER_PORT=${PORT} +MA_CURRENT_INSTANCE_NAME=${MA_CURRENT_INSTANCE_NAME} + +cd "${WORK_DIR}" + +mkdir -p /cache/ray_tmp + +echo "Cleaning up old Ray processes..." +ray stop --force || true +sleep 5 +rm -rf /cache/ray_tmp/* +pkill -9 -f raylet || true +pkill -9 -f plasma_store || true +pkill -9 -f gcs_server || true +echo "Waiting 20s for NPU/Ray resources to be released..." +npu-smi info || true +sleep 20 + +# ================= NPU / HCCL / Ray 环境 ================= +export NON_MEGATRON=true +export MULTI_STREAM_MEMORY_REUSE=2 +export OMP_NUM_THREADS=1 +export PYTORCH_NPU_ALLOC_CONF=${PYTORCH_NPU_ALLOC_CONF:-max_split_size_mb:512} +export VLLM_LOGGING_LEVEL=INFO +export RAY_DEDUP_LOGS=0 +export HCCL_EXEC_TIMEOUT=${HCCL_EXEC_TIMEOUT:-3600} +export HCCL_LOG_LEVEL=${HCCL_LOG_LEVEL:-WARN} +export HCCL_CONNECT_TIMEOUT=${HCCL_CONNECT_TIMEOUT:-3600} +export HCCL_EVENT_TIMEOUT=${HCCL_EVENT_TIMEOUT:-7200} +export ACL_DEVICE_SYNC_TIMEOUT=${ACL_DEVICE_SYNC_TIMEOUT:-7200} +export GLOO_SOCKET_TIMEOUT=${GLOO_SOCKET_TIMEOUT:-7200} + +# 关键:降低 HCCL buffer,增加 socket 端口范围,缓解 HcclAllreduce ra socket batch connect failed。 +export HCCL_BUFFSIZE=${HCCL_BUFFSIZE:-300} +export P2P_HCCL_BUFFSIZE=${P2P_HCCL_BUFFSIZE:-64} +export HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE:-60000-60050} +export HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE:-61000-61050} + +export CUDA_DEVICE_MAX_CONNECTIONS=1 +export VLLM_ASCEND_ENABLE_NZ=${VLLM_ASCEND_ENABLE_NZ:-0} +export HCCL_OP_EXPANSION_MODE=${HCCL_OP_EXPANSION_MODE:-AIV} +export VLLM_ENGINE_ITERATION_TIMEOUT_S=${VLLM_ENGINE_ITERATION_TIMEOUT_S:-3600} +export WANDB_MODE=${WANDB_MODE:-disabled} +export PYTHONUNBUFFERED=1 +export TASK_QUEUE_ENABLE=${TASK_QUEUE_ENABLE:-1} +export COMBINED_ENABLE=${COMBINED_ENABLE:-1} +export TOKENIZERS_PARALLELISM=false +export CLOSE_MATMUL_K_SHIFT=${CLOSE_MATMUL_K_SHIFT:-1} +export ATB_MATMUL_SHUFFLE_K_ENABLE=${ATB_MATMUL_SHUFFLE_K_ENABLE:-0} +export HCCL_DETERMINISTIC=${HCCL_DETERMINISTIC:-true} +export VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING:-0} +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export ASCEND_GLOBAL_LOG_LEVEL=${ASCEND_GLOBAL_LOG_LEVEL:-3} +export HYDRA_FULL_ERROR=1 +export RAY_gcs_server_rpc_server_thread_num=${RAY_gcs_server_rpc_server_thread_num:-32} +export RAY_gcs_server_request_timeout_seconds=${RAY_gcs_server_request_timeout_seconds:-600} +export RAY_timeout_ms=${RAY_timeout_ms:-600000} +export RAY_worker_register_timeout_seconds=${RAY_worker_register_timeout_seconds:-600} +export RAY_USAGE_STATS_ENABLED=0 +export VERL_REUSE_AGENT_LOOP=${VERL_REUSE_AGENT_LOOP:-1} + +ulimit -n 65536 + +# Ray 不要覆盖 ASCEND_RT_VISIBLE_DEVICES;VERL 内部按 local_rank 选卡。 +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + +# ================= 路径与数据配置 ================= +HDFS_ROOT=${HDFS_ROOT:-$PWD} +DATA_ROOT=${DATA_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_ckpt} + +# Nanoclaw 数据输入支持两种目录,优先推荐 0625 扁平格式: +# base_tasks/data_*/env_builder.py +# base_tasks/data_*/prompts.md +# base_tasks/data_*/workplace_verifier.py +# base_tasks/data_*/manifest.json +# 也兼容旧格式:base_tasks/tasks/data_* + base_tasks/scripts|scrips/data_*。 +DEFAULT_NANOCLAW_BASE_TASKS=${DEFAULT_NANOCLAW_BASE_TASKS:-/opt/huawei/dataset/zyr_yuyin/lyf/datasets/nanoclawRLdata/0710_add1000agent_qwen3_7_max_v1/exported_new_data} +train_base_tasks=${TRAIN_DATA_PATH:-${BASE_TASKS:-${DEFAULT_NANOCLAW_BASE_TASKS}}} +val_base_tasks=${VAL_DATA_PATH:-${VAL_BASE_TASKS:-${train_base_tasks}}} +train_files="['$train_base_tasks']" +test_files="['$val_base_tasks']" + +if [ ! -d "${train_base_tasks}" ]; then + echo "ERROR: Nanoclaw TRAIN_DATA_PATH/BASE_TASKS directory not found: ${train_base_tasks}" >&2 + exit 2 +fi +if [ ! -d "${val_base_tasks}" ]; then + echo "ERROR: Nanoclaw VAL_DATA_PATH/VAL_BASE_TASKS directory not found: ${val_base_tasks}" >&2 + exit 2 +fi + +model_path=${MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-27B} +verifier_model_path=${VERIFIER_MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +# 纯 VERL engine 路线:不要使用 MindSpeed-MM YAML。 +unset MM_CONFIG_FILE || true + +# Nanoclaw 工具配置 +tool_config_path=${TOOL_CONFIG_PATH:-nanoclaw_recipe/nanoclaw_tool_config.yaml} +nanoclaw_task_glob=${NANOCLAW_TASK_GLOB:-data_*} +nanoclaw_task_ids=${NANOCLAW_TASK_IDS:-} +# 多机训练必须用所有节点都能访问的共享目录;不要用 /tmp,否则 reward worker 可能跨节点找不到 workspace。 +nanoclaw_temp_root=${NANOCLAW_TEMP_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_workplace_v14_qwen35_27b_sp1_fsdp2_31k_longseq_allopt} +# 默认保留每个 step/data_sample 的目录,方便复盘每条 GRPO 采样;磁盘紧张时手动设 NANOCLAW_CLEANUP_WORKSPACES=True。 +nanoclaw_cleanup_workspaces=${NANOCLAW_CLEANUP_WORKSPACES:-False} +nanoclaw_keep_failed_workspaces=${NANOCLAW_KEEP_FAILED_WORKSPACES:-False} +nanoclaw_env_builder_timeout=${NANOCLAW_ENV_BUILDER_TIMEOUT:-120} +nanoclaw_verifier_timeout=${NANOCLAW_VERIFIER_TIMEOUT:-3600} +nanoclaw_reward_score_mode=${NANOCLAW_REWARD_SCORE_MODE:-ratio} +nanoclaw_allow_bash=${NANOCLAW_ALLOW_BASH:-True} +nanoclaw_max_steps=${NANOCLAW_MAX_STEPS:-} +nanoclaw_require_final_answer=${NANOCLAW_REQUIRE_FINAL_ANSWER:-True} +nanoclaw_final_answer_bonus_enable=${NANOCLAW_FINAL_ANSWER_BONUS_ENABLE:-False} +nanoclaw_final_answer_bonus_score=${NANOCLAW_FINAL_ANSWER_BONUS_SCORE:-0.0} +nanoclaw_turn_penalty_only_positive_score=${NANOCLAW_TURN_PENALTY_ONLY_POSITIVE_SCORE:-False} +nanoclaw_assistant_turn_penalty=${NANOCLAW_ASSISTANT_TURN_PENALTY:-0.0} +nanoclaw_duplicate_tool_call_penalty=${NANOCLAW_DUPLICATE_TOOL_CALL_PENALTY:-0.0} +nanoclaw_repeated_response_penalty=${NANOCLAW_REPEATED_RESPONSE_PENALTY:-0.0} +nanoclaw_repeated_response_min_chars=${NANOCLAW_REPEATED_RESPONSE_MIN_CHARS:-50} +nanoclaw_repeated_response_min_consecutive_repeats=${NANOCLAW_REPEATED_RESPONSE_MIN_CONSECUTIVE_REPEATS:-5} +nanoclaw_mask_looping_responses=${NANOCLAW_MASK_LOOPING_RESPONSES:-True} +nanoclaw_mask_only_positive_advantage=${NANOCLAW_MASK_ONLY_POSITIVE_ADVANTAGE:-True} +nanoclaw_mask_budget_exhausted_last_turn=${NANOCLAW_MASK_BUDGET_EXHAUSTED_LAST_TURN:-True} +nanoclaw_mask_duplicate_tool_result_turns=${NANOCLAW_MASK_DUPLICATE_TOOL_RESULT_TURNS:-True} +nanoclaw_mask_error_tool_result_turns=${NANOCLAW_MASK_ERROR_TOOL_RESULT_TURNS:-True} + +# verify_workplace.py 如需调用本地 OpenAI-compatible API,可用这些变量传入 reward。 +# 默认假设 5 机 40 卡:前 4 个节点加入 Ray 训练,第 5 个节点部署 verifier/vLLM API。 +verifier_api_node_rank=${VERIFIER_API_NODE_RANK:-4} +verifier_api_port=${VERIFIER_API_PORT:-8000} +verifier_api_host=${VERIFIER_API_HOST:-${MA_VJ_NAME}-${MA_TASK_NAME}-${verifier_api_node_rank}.${MA_VJ_NAME}} +verifier_api_start_cmd=${VERIFIER_API_START_CMD:-} +verifier_api_bind_host=${VERIFIER_API_BIND_HOST:-0.0.0.0} +# 9B verifier 默认使用整台 8 卡节点:两份 TP4 副本由 vLLM 内置 DP 统一服务。 +# 如需单副本 TP8,可设置 VERIFIER_API_TP=8 VERIFIER_API_DP=1。 +verifier_api_tp=${VERIFIER_API_TP:-4} +verifier_api_dp=${VERIFIER_API_DP:-2} +verifier_api_devices=${VERIFIER_API_DEVICES:-0,1,2,3,4,5,6,7} +verifier_api_distributed_executor_backend=${VERIFIER_API_DISTRIBUTED_EXECUTOR_BACKEND:-mp} +verifier_api_max_model_len=${VERIFIER_API_MAX_MODEL_LEN:-32768} +verifier_api_max_num_batched_tokens=${VERIFIER_API_MAX_NUM_BATCHED_TOKENS:-32768} +verifier_api_max_num_seqs=${VERIFIER_API_MAX_NUM_SEQS:-160} +verifier_api_gpu_memory_utilization=${VERIFIER_API_GPU_MEMORY_UTILIZATION:-0.70} +verifier_api_enforce_eager=${VERIFIER_API_ENFORCE_EAGER:-0} +verifier_api_enable_graph_mode=${VERIFIER_API_ENABLE_GRAPH_MODE:-1} +verifier_api_enable_prefix_caching=${VERIFIER_API_ENABLE_PREFIX_CACHING:-0} +verifier_api_startup_timeout=${VERIFIER_API_STARTUP_TIMEOUT:-1800} +verifier_api_log=${VERIFIER_API_LOG:-logs/vllm-verifier-api.log} +mock_api_base=${MOCK_API_BASE:-http://${verifier_api_host}:${verifier_api_port}/v1} +mock_api_key=${MOCK_API_KEY:-dummy_key} +mock_model_name=${MOCK_MODEL_NAME:-qwen3_5_9b_verifier} +# verify_workplace.py 内部 OpenAI/httpx 单次请求超时;reward API 排队时宁可多等,不要轻易误判 0 分。 +mock_api_timeout=${MOCK_API_TIMEOUT:-1800} +mock_api_connect_timeout=${MOCK_API_CONNECT_TIMEOUT:-300} +# 强制 verifier/OpenAI judge 请求关闭 thinking,sitecustomize 会自动注入 extra_body.chat_template_kwargs.enable_thinking=False。 +nanoclaw_force_no_thinking=${NANOCLAW_FORCE_NO_THINKING:-1} +nanoclaw_force_max_tokens=${NANOCLAW_FORCE_MAX_TOKENS:-50} +# 默认控制台只打一行 reward 摘要;如需每项 details,设 NANOCLAW_REWARD_PRINT_DETAILS=1。 +nanoclaw_reward_print_details=${NANOCLAW_REWARD_PRINT_DETAILS:-0} +# verifier API 是单独节点,默认低并发,避免 RewardLoopWorker 同时打爆 API 导致排队超时。 +reward_num_workers=${REWARD_NUM_WORKERS:-52} + +project_name=${PROJECT_NAME:-qwen3.5-27b_nanoclaw_grpo_verl_0720} +experiment_name=${EXPERIMENT_NAME:-qwen3.5-27b_nanoclaw_grpo_sp1_fsdp2_31k_longseq_allopt_lr1e6_fixedkl1e-3} +default_local_dir=${DEFAULT_LOCAL_DIR:-$DATA_ROOT/checkpoint/$experiment_name} +start_time=$(date +%Y%m%d)_$(date +%H%M%S) +mkdir -p logs "${default_local_dir}" + +# ================= 算法与并行参数 ================= +adv_estimator=grpo +max_turns=${MAX_TURNS:-35} +max_prompt_length=${MAX_PROMPT_LENGTH:-8192} +max_response_length=${MAX_RESPONSE_LENGTH:-22768} +max_assistant_response_length=${MAX_ASSISTANT_RESPONSE_LENGTH:-16384} +max_tool_response_length=${MAX_TOOL_RESPONSE_LENGTH:-8192} +max_model_len=$((max_prompt_length + max_response_length)) + +# MindSpeed 配置仅作为训练意图参考;以下均使用最新版 VERL 的原生字段。 +actor_lr=${ACTOR_LR:-1e-6} +actor_lr_scheduler_type=${ACTOR_LR_SCHEDULER_TYPE:-constant} +actor_lr_warmup_steps_ratio=${ACTOR_LR_WARMUP_STEPS_RATIO:-0.0} +actor_weight_decay=${ACTOR_WEIGHT_DECAY:-0.01} +actor_adam_beta1=${ACTOR_ADAM_BETA1:-0.9} +actor_adam_beta2=${ACTOR_ADAM_BETA2:-0.95} +actor_clip_grad=${ACTOR_CLIP_GRAD:-1.0} +actor_ppo_epochs=${ACTOR_PPO_EPOCHS:-1} +actor_shuffle=${ACTOR_SHUFFLE:-False} +actor_entropy_coeff=${ACTOR_ENTROPY_COEFF:-0.0} +actor_clip_ratio_low=${ACTOR_CLIP_RATIO_LOW:-0.2} +actor_clip_ratio_high=${ACTOR_CLIP_RATIO_HIGH:-0.2} +# MindSpeed 配置没有 Dual-Clip PPO 对应项,保留该 27B 脚本原来的 C=10。 +actor_clip_ratio_c=${ACTOR_CLIP_RATIO_C:-10.0} + +# YAML 的 fixed init_kl_coef + low_var_kl 对应 VERL 的 reward-KL 路径。 +algorithm_gamma=${ALGORITHM_GAMMA:-1.0} +algorithm_lam=${ALGORITHM_LAM:-0.95} +use_kl_in_reward=${USE_KL_IN_REWARD:-True} +kl_penalty=${KL_PENALTY:-low_var_kl} +kl_ctrl_type=${KL_CTRL_TYPE:-fixed} +kl_coef=${KL_COEF:-0.001} +# 关闭 actor-KL,避免与 reward-KL 重复惩罚。 +actor_use_kl_loss=${ACTOR_USE_KL_LOSS:-False} +actor_kl_loss_coef=${ACTOR_KL_LOSS_COEF:-0.001} +actor_kl_loss_type=${ACTOR_KL_LOSS_TYPE:-low_var_kl} + +train_batch_size=${TRAIN_BATCH_SIZE:-64} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-16} +n_resp_per_prompt=${N_RESP_PER_PROMPT:-8} +# 先压低验证,避免验证和训练稳定性混在一起。 +n_resp_per_prompt_val=${N_RESP_PER_PROMPT_VAL:-1} +log_val_generations=${LOG_VAL_GENERATIONS:-10} + +infer_tp=${INFER_TP:-4} +train_sp=${TRAIN_SP:-1} +offload=${OFFLOAD:-True} + +# 长序列全优化版使用最新版 VERL 官方 Qwen3.5-27B 配方采用的 FSDP2。 +actor_strategy=${ACTOR_STRATEGY:-fsdp2} +fsdp_size=${FSDP_SIZE:-} + +actor_pack=${ACTOR_PACK:-1} +logprob_pack=${LOGPROB_PACK:-2} +actor_max_token_len_per_gpu=${ACTOR_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * actor_pack + train_sp - 1) / train_sp))} +log_prob_max_token_len_per_gpu=${LOG_PROB_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * logprob_pack + train_sp - 1) / train_sp))} +entropy_from_logits_with_chunking=${ENTROPY_FROM_LOGITS_WITH_CHUNKING:-True} +entropy_from_logits_chunk_size=${ENTROPY_FROM_LOGITS_CHUNK_SIZE:-256} +entropy_checkpointing=${ENTROPY_CHECKPOINTING:-False} +use_fused_kernels=${USE_FUSED_KERNELS:-True} +fused_kernel_backend=${FUSED_KERNEL_BACKEND:-torch} +enable_activation_offload=${ENABLE_ACTIVATION_OFFLOAD:-True} +rollout_max_num_batched_tokens=${ROLLOUT_MAX_NUM_BATCHED_TOKENS:-32384} +rollout_gpu_memory_utilization=${ROLLOUT_GPU_MEMORY_UTILIZATION:-0.60} +update_weights_bucket_mb=${UPDATE_WEIGHTS_BUCKET_MB:-2048} + +# Qwen 官方推荐:Instruct/non-thinking reasoning tasks +rollout_temperature=${ROLLOUT_TEMPERATURE:-0.6} +rollout_top_p=${ROLLOUT_TOP_P:-0.95} +rollout_top_k=${ROLLOUT_TOP_K:-20} +rollout_min_p=${ROLLOUT_MIN_P:-0.0} +rollout_presence_penalty=${ROLLOUT_PRESENCE_PENALTY:-0.0} +rollout_frequency_penalty=${ROLLOUT_FREQUENCY_PENALTY:-0.0} +rollout_repetition_penalty=${ROLLOUT_REPETITION_PENALTY:-1.0} + +echo "DEBUG: max_response_length=${max_response_length}, max_assistant_response_length=${max_assistant_response_length}, max_model_len=${max_model_len}" +echo "DEBUG: max_turns=${max_turns}" +echo "DEBUG: max_tool_response_length=${max_tool_response_length}" +echo "DEBUG: entropy_chunking=${entropy_from_logits_with_chunking}, entropy_chunk_size=${entropy_from_logits_chunk_size}, entropy_checkpointing=${entropy_checkpointing}" +echo "DEBUG: fused_lmhead=${use_fused_kernels}, fused_backend=${fused_kernel_backend}, activation_offload=${enable_activation_offload}" +echo "DEBUG: train_batch_size=${train_batch_size}, ppo_mini_batch_size=${ppo_mini_batch_size}, n=${n_resp_per_prompt}" +echo "DEBUG: train_sp=${train_sp}, infer_tp=${infer_tp}, actor_strategy=${actor_strategy}, fsdp_size=${fsdp_size:-}" +echo "DEBUG: Qwen3.5 Ulysses FLA required=${NANOCLAW_REQUIRE_FLA}, backend=${QWEN35_FLA_BACKEND} (TRAIN_SP=${train_sp})" +echo "DEBUG: actor_max_token_len_per_gpu=${actor_max_token_len_per_gpu}, log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu}" +echo "DEBUG: rollout sampling temperature=${rollout_temperature}, top_p=${rollout_top_p}, top_k=${rollout_top_k}, min_p=${rollout_min_p}, presence_penalty=${rollout_presence_penalty}, frequency_penalty=${rollout_frequency_penalty}, repetition_penalty=${rollout_repetition_penalty}" +echo "DEBUG: optimizer lr=${actor_lr}, scheduler=${actor_lr_scheduler_type}, warmup_ratio=${actor_lr_warmup_steps_ratio}, weight_decay=${actor_weight_decay}, betas=(${actor_adam_beta1},${actor_adam_beta2}), clip_grad=${actor_clip_grad}, ppo_epochs=${actor_ppo_epochs}, shuffle=${actor_shuffle}" +echo "DEBUG: KL use_in_reward=${use_kl_in_reward}, penalty=${kl_penalty}, ctrl=${kl_ctrl_type}, coef=${kl_coef}, actor_kl=${actor_use_kl_loss}" +echo "DEBUG: HCCL_BUFFSIZE=${HCCL_BUFFSIZE}, HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE}, HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE}" + +val_before_train=${VAL_BEFORE_TRAIN:-False} +trainer_use_v1=${TRAINER_USE_V1:-True} +test_freq=${TEST_FREQ:-5000} +save_freq=${SAVE_FREQ:-2} + +# ================= 分布式 ================= +export TOTAL_NNODES=${TOTAL_NNODES:-5} +export TRAIN_NNODES=${TRAIN_NNODES:-4} +export NNODES=${NNODES:-${TRAIN_NNODES}} +export NODE_RANK=${VC_TASK_INDEX} +export NPUS_PER_NODE=${NPUS_PER_NODE:-8} +export WORLD_SIZE=$((NPUS_PER_NODE * NNODES)) + +export MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-0.${MA_VJ_NAME} +export MASTER_PORT=${MASTER_PORT:-6167} +export DASHBOARD_PORT=${DASHBOARD_PORT:-8191} +export RAY_PORT=${RAY_PORT:-6167} + +readonly SOCKET_IFNAME=${SOCKET_IFNAME:-eth0} +export HCCL_SOCKET_IFNAME=${HCCL_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export GLOO_SOCKET_IFNAME=${GLOO_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export CURRENT_IP=$(ifconfig ${SOCKET_IFNAME} | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +export RAY_NODE_IP=${MA_CURRENT_IP:-${CURRENT_IP}} + +export ASCEND_RT_VISIBLE_DEVICES=${ASCEND_RT_VISIBLE_DEVICES:-$(seq -s, 0 $((NPUS_PER_NODE - 1)))} + +cat < [Verifier API Node] This node is reserved for vLLM/OpenAI-compatible verifier API." + echo "--> [Verifier API Node] API base: ${mock_api_base}" + export VLLM_ENABLE_GRAPH_MODE=${verifier_api_enable_graph_mode} + mkdir -p "$(dirname "${verifier_api_log}")" + if [ -n "${verifier_api_start_cmd}" ]; then + echo "--> [Verifier API Node] Running VERIFIER_API_START_CMD..." + bash -lc "${verifier_api_start_cmd}" & + verifier_api_pid=$! + else + echo "--> [Verifier API Node] Starting default vLLM verifier API..." + verifier_api_device_count=$(awk -F',' '{print NF}' <<<"${verifier_api_devices}") + verifier_api_expected_device_count=$((verifier_api_tp * verifier_api_dp)) + if [ "${verifier_api_device_count}" -ne "${verifier_api_expected_device_count}" ]; then + echo "ERROR: verifier TP*DP=${verifier_api_tp}*${verifier_api_dp}=${verifier_api_expected_device_count}, but VERIFIER_API_DEVICES=${verifier_api_devices} contains ${verifier_api_device_count} devices." >&2 + exit 2 + fi + export ASCEND_RT_VISIBLE_DEVICES=${verifier_api_devices} + verifier_api_args=( + --model "${verifier_model_path}" + --tokenizer "${verifier_model_path}" + --host "${verifier_api_bind_host}" + --port "${verifier_api_port}" + --served-model-name "${mock_model_name}" + --tensor-parallel-size "${verifier_api_tp}" + --data-parallel-size "${verifier_api_dp}" + --distributed-executor-backend "${verifier_api_distributed_executor_backend}" + --dtype bfloat16 + --max-model-len "${verifier_api_max_model_len}" + --max-num-batched-tokens "${verifier_api_max_num_batched_tokens}" + --max-num-seqs "${verifier_api_max_num_seqs}" + --gpu-memory-utilization "${verifier_api_gpu_memory_utilization}" + --trust-remote-code + ) + if [ "${verifier_api_enforce_eager}" = "1" ] || [ "${verifier_api_enforce_eager}" = "true" ] || [ "${verifier_api_enforce_eager}" = "True" ]; then + verifier_api_args+=(--enforce-eager) + fi + if [ "${verifier_api_enable_prefix_caching}" = "1" ] || [ "${verifier_api_enable_prefix_caching}" = "true" ] || [ "${verifier_api_enable_prefix_caching}" = "True" ]; then + verifier_api_args+=(--enable-prefix-caching) + fi + echo "--> [Verifier API Node] Command: python3 -m vllm.entrypoints.openai.api_server ${verifier_api_args[*]}" + python3 -m vllm.entrypoints.openai.api_server "${verifier_api_args[@]}" >"${verifier_api_log}" 2>&1 & + verifier_api_pid=$! + fi + + echo "--> [Verifier API Node] vLLM API pid=${verifier_api_pid}, log=${verifier_api_log}" + echo "--> [Verifier API Node] Waiting for ${mock_api_base}/models ..." + python3 - "${mock_api_base}/models" "${verifier_api_startup_timeout}" "${verifier_api_log}" "${verifier_api_pid}" <<'PY' +import os +import sys +import time +import urllib.request +from pathlib import Path + +url = sys.argv[1] +timeout = float(sys.argv[2]) +log_path = Path(sys.argv[3]) +pid = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[4] else None +started = time.time() +last_error = None +while time.time() - started < timeout: + if pid is not None: + try: + os.kill(pid, 0) + except OSError: + print(f"ERROR: verifier API process exited early: pid={pid}", file=sys.stderr) + if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) + sys.exit(1) + try: + with urllib.request.urlopen(url, timeout=5) as response: + if 200 <= response.status < 300: + print(f"READY: {url}", file=sys.stderr) + sys.exit(0) + except Exception as exc: + last_error = exc + time.sleep(5) +print(f"ERROR: timed out waiting for {url}; last_error={last_error}", file=sys.stderr) +if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) +sys.exit(1) +PY + verifier_readiness_rc=$? + if [ "${verifier_readiness_rc}" -ne 0 ]; then + echo "ERROR: verifier API readiness check failed with rc=${verifier_readiness_rc}." >&2 + if kill -0 "${verifier_api_pid}" 2>/dev/null; then + kill "${verifier_api_pid}" 2>/dev/null || true + fi + wait "${verifier_api_pid}" 2>/dev/null || true + exit "${verifier_readiness_rc}" + fi + + echo "--> [Verifier API Node] Ready. Keeping node alive." + wait "${verifier_api_pid}" + verifier_api_rc=$? + if [ "${verifier_api_rc}" -ne 0 ]; then + echo "ERROR: verifier API exited with rc=${verifier_api_rc}; log=${verifier_api_log}" >&2 + fi + exit "${verifier_api_rc}" +fi + +export TMPDIR=/cache/ray_tmp +export HCCL_ASYNC_ERROR_HANDLING=${HCCL_ASYNC_ERROR_HANDLING:-0} + +wait_for_ray_npu_resources() { + expected_npu=$1 + timeout_seconds=${2:-900} + begin_ts=$(date +%s) + + while true; do + total_npu=$(python3 - <<'PY' 2>/dev/null +import ray + +try: + ray.init(address="auto", ignore_reinit_error=True, logging_level="ERROR") + print(int(ray.cluster_resources().get("NPU", 0))) + ray.shutdown() +except Exception: + print(0) +PY +) + total_npu=${total_npu:-0} + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + + echo "Ray NPU resources: ${total_npu}/${expected_npu}, elapsed=${elapsed}s" + ray status || true + + if [ "${total_npu}" -ge "${expected_npu}" ]; then + echo "Ray cluster is ready: ${total_npu}/${expected_npu} NPU resources registered." + break + fi + + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for Ray NPU resources: ${total_npu}/${expected_npu}" >&2 + return 1 + fi + + sleep 5 + done +} + +wait_for_verifier_api() { + api_url="${mock_api_base}/models" + timeout_seconds=${VERIFIER_API_CLIENT_WAIT_TIMEOUT:-1800} + begin_ts=$(date +%s) + last_diag_ts=0 + while true; do + verifier_check_output=$(python3 - "${api_url}" <<'PY' 2>&1 +import socket +import sys +import urllib.parse +import urllib.request + +url = sys.argv[1] +parsed = urllib.parse.urlparse(url) +host = parsed.hostname +port = parsed.port or (443 if parsed.scheme == "https" else 80) +print(f"check url={url} host={host} port={port}") +try: + infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + print("dns=" + ",".join(sorted({item[4][0] for item in infos}))) +except Exception as exc: + print(f"dns_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with socket.create_connection((host, port), timeout=5): + print("tcp=ok") +except Exception as exc: + print(f"tcp_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with urllib.request.urlopen(url, timeout=10) as response: + print(f"http_status={response.status}") + raise SystemExit(0 if 200 <= response.status < 300 else 1) +except Exception as exc: + print(f"http_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +PY +) + check_rc=$? + if [ "${check_rc}" = "0" ]; then + echo "Verifier API is ready: ${api_url}" + echo "${verifier_check_output}" + break + fi + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + echo "Waiting for verifier API: ${api_url}, elapsed=${elapsed}s" + if [ $((now_ts - last_diag_ts)) -ge 60 ]; then + last_diag_ts=${now_ts} + echo "--- verifier API check diagnostics ---" + echo "${verifier_check_output}" + echo "--- expected verifier node: rank=${verifier_api_node_rank}, host=${verifier_api_host}, port=${verifier_api_port} ---" + echo "--- check verifier node log: ${verifier_api_log} ---" + echo "--------------------------------------" + fi + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for verifier API: ${api_url}" >&2 + echo "Last verifier API diagnostics:" >&2 + echo "${verifier_check_output}" >&2 + return 1 + fi + sleep 10 + done +} + +# ================= Nanoclaw workspace 根目录 ================= +mkdir -p "${nanoclaw_temp_root}" +if ! touch "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" 2>/dev/null; then + echo "ERROR: Cannot write NANOCLAW_TEMP_ROOT: ${nanoclaw_temp_root}" >&2 + exit 2 +fi +rm -f "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" || true +if [[ "${nanoclaw_temp_root}" == /tmp/* ]]; then + echo "WARNING: NANOCLAW_TEMP_ROOT is under /tmp. Multi-node reward workers may not see rollout workspaces." >&2 + echo "WARNING: Prefer a shared path, e.g. ${DATA_ROOT}/nanoclaw_workspaces" >&2 +fi +echo "DEBUG: Nanoclaw train_base_tasks=${train_base_tasks}" +echo "DEBUG: Nanoclaw val_base_tasks=${val_base_tasks}" +echo "DEBUG: Nanoclaw task_glob=${nanoclaw_task_glob}, task_ids=${nanoclaw_task_ids:-}" +echo "DEBUG: Nanoclaw temp_root=${nanoclaw_temp_root}, cleanup=${nanoclaw_cleanup_workspaces}, keep_failed=${nanoclaw_keep_failed_workspaces}" + +# ================= 生成 Ray runtime env ================= +RUNTIME_ENV_FILE=${WORK_DIR}/verl_engine_runtime_env.generated.yaml +cat > "${RUNTIME_ENV_FILE}" < [Head Node] Starting Ray Head on ${CURRENT_IP}..." + ray start --head \ + --node-ip-address=${RAY_NODE_IP} \ + --port=${RAY_PORT} \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=${DASHBOARD_PORT} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + + sleep 10 + wait_for_ray_npu_resources ${WORLD_SIZE} 900 || exit 1 + wait_for_verifier_api || exit 1 +else + echo "--> [Worker Node] Starting Ray Worker, connecting to ${MASTER_ADDR}:${RAY_PORT}..." + sleep 20 + ray start --address=${MASTER_ADDR}:${RAY_PORT} \ + --node-ip-address=${RAY_NODE_IP} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + sleep 10 +fi + +# ================= 训练参数数组 ================= +training_args=( + python3 -m verl.trainer.main_ppo + +ray_kwargs.ray_init.address=auto + reward.num_workers=${reward_num_workers} + algorithm.adv_estimator=${adv_estimator} + algorithm.gamma=${algorithm_gamma} + algorithm.lam=${algorithm_lam} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_penalty=${kl_penalty} + algorithm.kl_ctrl.type=${kl_ctrl_type} + algorithm.kl_ctrl.kl_coef=${kl_coef} + data.train_files="${train_files}" + data.val_files="${test_files}" + data.return_raw_chat=True + data.return_multi_modal_inputs=False + data.image_key=images + data.shuffle=True + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation=error + data.custom_cls.path=pkg://nanoclaw_recipe.nanoclaw + data.custom_cls.name=CustomRLHFDataset + "data.tool_config_path=${tool_config_path}" + "+data.nanoclaw_task_glob=${nanoclaw_task_glob}" + "+data.nanoclaw_temp_root=${nanoclaw_temp_root}" + "+data.nanoclaw_cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+data.nanoclaw_keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+data.nanoclaw_env_builder_timeout=${nanoclaw_env_builder_timeout}" + "+data.nanoclaw_verifier_timeout=${nanoclaw_verifier_timeout}" + "+data.nanoclaw_reward_score_mode=${nanoclaw_reward_score_mode}" + "+data.nanoclaw_allow_bash=${nanoclaw_allow_bash}" + +data.apply_chat_template_kwargs.enable_thinking=True + reward.custom_reward_function.path=pkg://nanoclaw_recipe.nanoclaw + reward.custom_reward_function.name=compute_score + "+reward.custom_reward_function.reward_kwargs.cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+reward.custom_reward_function.reward_kwargs.keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+reward.custom_reward_function.reward_kwargs.verifier_timeout=${nanoclaw_verifier_timeout}" + "+reward.custom_reward_function.reward_kwargs.reward_score_mode=${nanoclaw_reward_score_mode}" + "+reward.custom_reward_function.reward_kwargs.require_final_answer=${nanoclaw_require_final_answer}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_enable=${nanoclaw_final_answer_bonus_enable}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_score=${nanoclaw_final_answer_bonus_score}" + "+reward.custom_reward_function.reward_kwargs.turn_penalty_only_positive_score=${nanoclaw_turn_penalty_only_positive_score}" + "+reward.custom_reward_function.reward_kwargs.assistant_turn_penalty=${nanoclaw_assistant_turn_penalty}" + "+reward.custom_reward_function.reward_kwargs.duplicate_tool_call_penalty=${nanoclaw_duplicate_tool_call_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_penalty=${nanoclaw_repeated_response_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_chars=${nanoclaw_repeated_response_min_chars}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_consecutive_repeats=${nanoclaw_repeated_response_min_consecutive_repeats}" + "+reward.custom_reward_function.reward_kwargs.mock_api_base=${mock_api_base}" + "+reward.custom_reward_function.reward_kwargs.mock_api_key=${mock_api_key}" + "+reward.custom_reward_function.reward_kwargs.mock_model_name=${mock_model_name}" + "+reward.custom_reward_function.reward_kwargs.mock_api_timeout=${mock_api_timeout}" + "+reward.custom_reward_function.reward_kwargs.mock_api_connect_timeout=${mock_api_connect_timeout}" + actor_rollout_ref.model.path=${model_path} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True + actor_rollout_ref.model.enable_activation_offload=${enable_activation_offload} + actor_rollout_ref.model.use_fused_kernels=${use_fused_kernels} + actor_rollout_ref.model.fused_kernel_options.impl_backend=${fused_kernel_backend} + actor_rollout_ref.actor.strategy=${actor_strategy} + actor_rollout_ref.ref.strategy=${actor_strategy} + actor_rollout_ref.actor.use_kl_loss=${actor_use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${actor_kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=${actor_kl_loss_type} + actor_rollout_ref.actor.clip_ratio_low=${actor_clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${actor_clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=${actor_clip_ratio_c} + actor_rollout_ref.actor.entropy_coeff=${actor_entropy_coeff} + actor_rollout_ref.actor.ppo_epochs=${actor_ppo_epochs} + actor_rollout_ref.actor.shuffle=${actor_shuffle} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.optim.lr_scheduler_type=${actor_lr_scheduler_type} + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=${actor_lr_warmup_steps_ratio} + actor_rollout_ref.actor.optim.weight_decay=${actor_weight_decay} + "actor_rollout_ref.actor.optim.betas=[${actor_adam_beta1},${actor_adam_beta2}]" + actor_rollout_ref.actor.optim.clip_grad=${actor_clip_grad} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_max_token_len_per_gpu} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.actor.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.actor.fsdp_config.offload_policy=${offload} + actor_rollout_ref.actor.fsdp_config.reshard_after_forward=True + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.fsdp_config.offload_policy=${offload} + actor_rollout_ref.ref.fsdp_config.reshard_after_forward=True + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.ref.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.mode=async + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.temperature=${rollout_temperature} + actor_rollout_ref.rollout.top_p=${rollout_top_p} + actor_rollout_ref.rollout.top_k=${rollout_top_k} + actor_rollout_ref.rollout.min_p=${rollout_min_p} + actor_rollout_ref.rollout.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.tensor_model_parallel_size=${infer_tp} + actor_rollout_ref.rollout.max_model_len=${max_model_len} + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=${update_weights_bucket_mb} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.max_num_batched_tokens=${rollout_max_num_batched_tokens} + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.enable_prefix_caching=False + actor_rollout_ref.rollout.multi_turn.enable=True + actor_rollout_ref.rollout.multi_turn.max_user_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_response_length=${max_assistant_response_length} + "actor_rollout_ref.rollout.multi_turn.tool_config_path=${tool_config_path}" + actor_rollout_ref.rollout.multi_turn.format=qwen3_coder + "actor_rollout_ref.rollout.multi_turn.max_tool_response_length=${max_tool_response_length}" + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_memory_utilization} + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.val_kwargs.temperature=${rollout_temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${rollout_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${rollout_top_k} + actor_rollout_ref.rollout.val_kwargs.min_p=${rollout_min_p} + actor_rollout_ref.rollout.val_kwargs.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.val_kwargs.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.val_kwargs.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=${n_resp_per_prompt_val} + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.fsdp_config.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.use_torch_compile=False + critic.fsdp.use_torch_compile=False + trainer.use_v1=${trainer_use_v1} + trainer.critic_warmup=0 + trainer.balance_batch=True + trainer.logger=['console','tensorboard'] + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.nnodes=${NNODES} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.val_before_train=${val_before_train} + trainer.log_val_generations=${log_val_generations} + trainer.save_freq=${save_freq} + trainer.default_local_dir=${default_local_dir} + trainer.test_freq=${test_freq} + trainer.total_epochs=10 +) + +if [ -n "${fsdp_size}" ]; then + training_args+=( + actor_rollout_ref.actor.fsdp_config.fsdp_size=${fsdp_size} + actor_rollout_ref.ref.fsdp_config.fsdp_size=${fsdp_size} + ) +fi + +if [ -n "${nanoclaw_task_ids}" ]; then + training_args+=("+data.nanoclaw_task_ids=${nanoclaw_task_ids}") +fi + +if [ -n "${nanoclaw_max_steps}" ]; then + training_args+=("+data.nanoclaw_max_steps=${nanoclaw_max_steps}") +fi + +# ================= 启动训练主进程:仅主节点执行 ================= +if [ "${NODE_RANK}" = "0" ]; then + echo "--> [Head Node] Starting VERL unified engine training..." + echo "DEBUG: runtime_env=${RUNTIME_ENV_FILE}" + echo "DEBUG: entrypoint=${training_args[*]}" + + ray job submit \ + --address="http://127.0.0.1:${DASHBOARD_PORT}" \ + --runtime-env="${RUNTIME_ENV_FILE}" \ + -- \ + "${training_args[@]}" 2>&1 | tee "logs/qwen3.5-nanoclaw-grpo-verl-engine-${start_time}.log" +else + echo "--> [Worker Node] Setup finished. Keeping node alive for Ray..." + tail -f /dev/null +fi diff --git "a/verl_0720_main/27b.sh\357\200\272Zone.Identifier" "b/verl_0720_main/27b.sh\357\200\272Zone.Identifier" new file mode 100644 index 0000000000000000000000000000000000000000..5ee6a6fa80e6f8f6f212f5497ed2fb2e9f54c4f2 --- /dev/null +++ "b/verl_0720_main/27b.sh\357\200\272Zone.Identifier" @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://huggingface.co/datasets/geminiDeveloper/verl_0720_nanoclaw/tree/main +HostUrl=https://huggingface.co/api/resolve-cache/datasets/geminiDeveloper/verl_0720_nanoclaw/bacc33fa11aa841f3fa8343597382539716bbd0c/27b.sh?download=true&etag=%228ca3b6ac1b3d0febbb9671913ca036ea0ecc9bc3%22 diff --git a/verl_0720_main/27b_old_verl.sh b/verl_0720_main/27b_old_verl.sh new file mode 100644 index 0000000000000000000000000000000000000000..d1771d34cfa47474a6be9d88af7283cb64dd80ec --- /dev/null +++ b/verl_0720_main/27b_old_verl.sh @@ -0,0 +1,1044 @@ +#!/bin/bash +# Qwen3.5-27B Nanoclaw 多轮 GRPO 训练脚本 — VERL 0720 + Nanoclaw 兼容版 +# +# 关键修改: +# 1) 入口改为 python3 -m verl.trainer.main_ppo,不再使用 recipe.grpo_mindspeed_mm.main_ppo; +# 2) 不再使用 MM_CONFIG_FILE / MindSpeed-MM YAML; +# 3) 显式 text-only:data.return_multi_modal_inputs=False; +# 4) 数据输入改为 Nanoclaw base_tasks 目录,不再使用 Retool parquet/json; +# 5) 保留 27B 作业验证过的 HCCL buffer 与端口范围,降低 HcclAllreduce socket/资源压力; +# 6) 默认 val n=1、log_val_generations=10,先验证训练稳定性; +# 7) 支持通过 ACTOR_STRATEGY=fsdp2 FSDP_SIZE=16 切到官方新版 NPU FSDP2/FSDP16 形状。 +# 8) 使用最新版 VERL V1/TransferQueue 和 nanoclaw_recipe,并按参考 YAML 的训练意图映射优化器与 KL。 + +set -x + +npu-smi info || true +pip install --upgrade pip +pip uninstall -y moxing-framework || true + +# ================= 路径配置 ================= +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +if [ -f "${SCRIPT_DIR}/verl/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +elif [ -f "${SCRIPT_DIR}/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR} +else + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +fi +WORK_DIR=${WORK_DIR:-${DEFAULT_WORK_DIR}} +INSTALL_DIR=${INSTALL_DIR:-/home/ma-user} +BKGS=${BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs} +chmod 755 "${INSTALL_DIR}" + +# Nanoclaw 自定义包已随 WORK_DIR 提供:nanoclaw_recipe。 + +GCC_INSTALL_PREFIX=${GCC_INSTALL_PREFIX:-/home/ma-user/gcc-11.3.0} +COMPILED_GCC_ARCHIVE_PATH=${COMPILED_GCC_ARCHIVE_PATH:-/opt/huawei/dataset/zyr_yuyin/bkgs/gcc-11.3.0-compiled-aarch64.tar.gz} + +echo "--> 正在从缓存恢复 GCC 11.3.0..." +tar -xzf "${COMPILED_GCC_ARCHIVE_PATH}" -C /home/ma-user/ +export PATH=${GCC_INSTALL_PREFIX}/bin:${PATH} +export LD_LIBRARY_PATH=${GCC_INSTALL_PREFIX}/lib64:${GCC_INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH:-} +export CC=${GCC_INSTALL_PREFIX}/bin/gcc +export CXX=${GCC_INSTALL_PREFIX}/bin/g++ +echo "--> 验证 GCC 版本:" +gcc --version + +cd "${BKGS}" +cp jemalloc-5.3.0.tar.bz2 "${INSTALL_DIR}" + +VLLM_LATEST_PKGS=${VLLM_LATEST_PKGS:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-05-12/verl_new_26_05_09/pkgs} +rm -rf "${INSTALL_DIR}/vllm" "${INSTALL_DIR}/vllm-ascend" +cp -r "${VLLM_LATEST_PKGS}/vllm" "${INSTALL_DIR}" +cp -r "${VLLM_LATEST_PKGS}/vllm-ascend" "${INSTALL_DIR}" + +CANN_BKGS=${CANN_BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs/cann_0527} +cp "${CANN_BKGS}/Ascend-cann-toolkit_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-910b-ops_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-nnal_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" + +echo "################" +echo "## set verl env" +echo "################" + +cd "${INSTALL_DIR}" + +chmod +x Ascend-cann-toolkit_9.0.0_linux-aarch64.run +bash Ascend-cann-toolkit_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/ascend-toolkit/set_env.sh" + +chmod +x Ascend-cann-910b-ops_9.0.0_linux-aarch64.run +bash Ascend-cann-910b-ops_9.0.0_linux-aarch64.run --install --quiet + +chmod +x Ascend-cann-nnal_9.0.0_linux-aarch64.run +bash Ascend-cann-nnal_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/nnal/atb/set_env.sh" + +export ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME} +export LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:${LD_LIBRARY_PATH:-} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip3 install torch==2.9.0 +pip3 install pyyaml setuptools +pip3 install torch-npu==2.9.0 +pip3 install torchvision==0.24.0 torchaudio==2.9.0 + +ASCEND_TOOLKIT_PYTHON_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/python/site-packages +export PYTHONPATH=${PYTHONPATH:-}:${INSTALL_DIR}:${ASCEND_TOOLKIT_PYTHON_PATH} +pip install pybind11==2.13.6 + +cd "${INSTALL_DIR}/vllm" +VLLM_TARGET_DEVICE=empty pip install . + +cd "${INSTALL_DIR}/vllm-ascend" +pip install -e . +export VLLM_LOGGING_LEVEL=INFO + +cd "${INSTALL_DIR}" +tar -xvf jemalloc-5.3.0.tar.bz2 +cd jemalloc-5.3.0 +./configure --prefix="${INSTALL_DIR}" +make -j"$(nproc)" +make install +export LD_PRELOAD=${INSTALL_DIR}/lib/libjemalloc.so.2:${LD_PRELOAD:-} + +# # ================= 可选:安装 MindSpeed 栈 ================= +# # 纯 VERL engine 路线不依赖 MindSpeed-MM YAML。默认不安装,避免和新版 VERL engine 混淆。 +# INSTALL_MINDSPEED_STACK=${INSTALL_MINDSPEED_STACK:-0} +# if [ "${INSTALL_MINDSPEED_STACK}" = "1" ]; then +# MindSpeed_PATH=${MindSpeed_PATH:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-slow-stable} +# cd "${MindSpeed_PATH}" +# cd Megatron-LM && pip install -e . --no-deps && cd .. +# cd MindSpeed && pip install -e . --no-deps && cd .. +# cd MindSpeed-MM && mkdir -p logs data ckpt && pip install -e . --no-deps && cd .. +# pip install beartype bs4 diffusers==0.30.3 ftfy imageio-ffmpeg pandarallel pytest-mock +# else +# echo "--> Skip MindSpeed/MindSpeed-MM installation for pure VERL engine route." +# fi + + + +# ================= 安装 Triton-Ascend 3.2.1 ================= +# 1. 卸载 triton(增加 -y 自动确认) +pip uninstall -y triton + +# 2. 卸载 triton-ascend(增加 -y 自动确认) +pip uninstall -y triton-ascend +pip install --no-cache-dir --force-reinstall triton==3.5.0 +pip install --no-deps /opt/huawei/dataset/zyr_yuyin/bkgs/triton_ascend-3.2.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl + + +# ================= 安装新版 VERL ================= +cd "${WORK_DIR}" +pip install -r requirements-npu.txt +# NPU requirements 明确要求 numpy<2;editable 安装不能再次按 setup.py 把 NumPy升级到 2.x。 +python3 -m pip install -e . --no-deps +pip install --upgrade 'urllib3==1.26.11' +pip install loguru +pip install tree_sitter==0.21.3 +pip install tree-sitter-java==0.21.0 +pip install tree-sitter-javascript==0.21.4 + +ACL_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/aarch64-linux/lib64 +export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ACL_PATH} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip uninstall -y transformers || true +pip install transformers==5.3.0 +pip install accelerate==1.13.0 mathruler +pip install jsonargparse +pip install deepdiff sympy html2text requests bs4 mpmath swanlab PandoraBox json_repair openai httpx + +# 新版 VERL 的 Qwen3.5 Ulysses/SP patch 会使用 fla.ops.cp 来维护 +# GatedDeltaNet 的跨序列分片状态。上游 FLA 的 NPU 训练兼容性未经当前 VERL 验证; +# WOE-Y/flash-linear-attention-npu 是 CANN 低层算子库,也不是可直接 import fla 的替代包。 +FLASH_LINEAR_ATTENTION_VERSION=${FLASH_LINEAR_ATTENTION_VERSION:-0.5.0} +QWEN35_FLA_BACKEND=${QWEN35_FLA_BACKEND:-disabled} +requested_train_sp=${TRAIN_SP:-1} +if ! [[ "${requested_train_sp}" =~ ^[1-9][0-9]*$ ]]; then + echo "ERROR: TRAIN_SP must be a positive integer, got: ${requested_train_sp}" >&2 + exit 2 +fi +if [ "${requested_train_sp}" -gt 1 ]; then + export NANOCLAW_REQUIRE_FLA=1 + case "${QWEN35_FLA_BACKEND}" in + upstream_experimental) + echo "WARNING: using upstream flash-linear-attention on NPU is experimental and not covered by VERL's NPU tests." >&2 + python3 -m pip install --no-cache-dir "flash-linear-attention==${FLASH_LINEAR_ATTENTION_VERSION}" + ;; + npu_bridge) + echo "--> QWEN35_FLA_BACKEND=npu_bridge: expect a preinstalled NPU-compatible 'fla' Python compatibility layer." + ;; + *) + echo "ERROR: TRAIN_SP=${requested_train_sp} requires Qwen3.5 FLA context parallel, but no verified NPU FLA Python backend is configured." >&2 + echo "Set TRAIN_SP=1 for the supported fallback. Use QWEN35_FLA_BACKEND=upstream_experimental only for an explicit NPU smoke test, or npu_bridge after installing a compatible bridge." >&2 + exit 2 + ;; + esac +else + export NANOCLAW_REQUIRE_FLA=0 + echo "--> TRAIN_SP=1: skip flash-linear-attention; Qwen3.5 does not build an Ulysses CP context." +fi + +# Transformers 5.x 会经 sklearn 间接导入 pandas/scipy。固定同一套 NumPy ABI, +# 避免出现 "numpy.dtype size changed"。这些版本均支持 Python 3.11/aarch64。 +NUMPY_VERSION=${NUMPY_VERSION:-1.26.4} +PANDAS_VERSION=${PANDAS_VERSION:-2.2.3} +SCIPY_VERSION=${SCIPY_VERSION:-1.14.1} +SKLEARN_VERSION=${SKLEARN_VERSION:-1.6.1} +python3 -m pip install --no-cache-dir --force-reinstall \ + "numpy==${NUMPY_VERSION}" \ + "pandas==${PANDAS_VERSION}" \ + "scipy==${SCIPY_VERSION}" \ + "scikit-learn==${SKLEARN_VERSION}" + +python3 - <<'PY' || exit 2 +import numpy +import os +import pandas +import scipy +import sklearn +import sys +import transformers +import vllm + +if os.environ.get("NANOCLAW_REQUIRE_FLA") == "1": + import fla + from fla.ops.cp.comm import conv_cp_send_recv_bwd, conv_cp_send_recv_fwd + from fla.ops.cp.context import build_cp_context + + fla_version = getattr(fla, "__version__", "unknown") +else: + fla_version = "not-required" + +print( + "[python_stack_preflight] " + f"python={sys.executable} " + f"numpy={numpy.__version__} " + f"pandas={pandas.__version__} " + f"scipy={scipy.__version__} " + f"sklearn={sklearn.__version__} " + f"transformers={transformers.__version__} " + f"vllm={vllm.__version__} " + f"fla={fla_version}" +) +print( + "[python_stack_paths] " + f"numpy={numpy.__file__} " + f"pandas={pandas.__file__}" +) +PY +pip list + +# ================= 检查 Nanoclaw recipe ================= +if [ ! -f "${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" ]; then + echo "ERROR: Nanoclaw recipe not found: ${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" >&2 + exit 2 +fi +test -f "${WORK_DIR}/nanoclaw_recipe/__init__.py" + +# ================= PLOG ================= +ma_vj_name=$(echo "${MA_VJ_NAME}" | sed 's:ma-job:modelarts-job:g') +task_name=worker-${VC_TASK_INDEX} +task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name} +mkdir -p "${task_plog_path}" +export ASCEND_PROCESS_LOG_PATH=${task_plog_path}/${VC_TASK_INDEX} +echo "plog path: ${ASCEND_PROCESS_LOG_PATH}" + +MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-${VC_TASK_INDEX}.${MA_VJ_NAME} +MASTER_PORT=${PORT} +MA_CURRENT_INSTANCE_NAME=${MA_CURRENT_INSTANCE_NAME} + +cd "${WORK_DIR}" + +mkdir -p /cache/ray_tmp + +echo "Cleaning up old Ray processes..." +ray stop --force || true +sleep 5 +rm -rf /cache/ray_tmp/* +pkill -9 -f raylet || true +pkill -9 -f plasma_store || true +pkill -9 -f gcs_server || true +echo "Waiting 20s for NPU/Ray resources to be released..." +npu-smi info || true +sleep 20 + +# ================= NPU / HCCL / Ray 环境 ================= +export NON_MEGATRON=true +export MULTI_STREAM_MEMORY_REUSE=2 +export OMP_NUM_THREADS=1 +export PYTORCH_NPU_ALLOC_CONF=${PYTORCH_NPU_ALLOC_CONF:-max_split_size_mb:512} +export VLLM_LOGGING_LEVEL=INFO +export RAY_DEDUP_LOGS=0 +export HCCL_EXEC_TIMEOUT=${HCCL_EXEC_TIMEOUT:-3600} +export HCCL_LOG_LEVEL=${HCCL_LOG_LEVEL:-WARN} +export HCCL_CONNECT_TIMEOUT=${HCCL_CONNECT_TIMEOUT:-3600} +export HCCL_EVENT_TIMEOUT=${HCCL_EVENT_TIMEOUT:-7200} +export ACL_DEVICE_SYNC_TIMEOUT=${ACL_DEVICE_SYNC_TIMEOUT:-7200} +export GLOO_SOCKET_TIMEOUT=${GLOO_SOCKET_TIMEOUT:-7200} + +# 关键:降低 HCCL buffer,增加 socket 端口范围,缓解 HcclAllreduce ra socket batch connect failed。 +export HCCL_BUFFSIZE=${HCCL_BUFFSIZE:-300} +export P2P_HCCL_BUFFSIZE=${P2P_HCCL_BUFFSIZE:-64} +export HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE:-60000-60050} +export HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE:-61000-61050} + +export CUDA_DEVICE_MAX_CONNECTIONS=1 +export VLLM_ASCEND_ENABLE_NZ=${VLLM_ASCEND_ENABLE_NZ:-0} +export HCCL_OP_EXPANSION_MODE=${HCCL_OP_EXPANSION_MODE:-AIV} +export VLLM_ENGINE_ITERATION_TIMEOUT_S=${VLLM_ENGINE_ITERATION_TIMEOUT_S:-3600} +export WANDB_MODE=${WANDB_MODE:-disabled} +export PYTHONUNBUFFERED=1 +export TASK_QUEUE_ENABLE=${TASK_QUEUE_ENABLE:-1} +export COMBINED_ENABLE=${COMBINED_ENABLE:-1} +export TOKENIZERS_PARALLELISM=false +export CLOSE_MATMUL_K_SHIFT=${CLOSE_MATMUL_K_SHIFT:-1} +export ATB_MATMUL_SHUFFLE_K_ENABLE=${ATB_MATMUL_SHUFFLE_K_ENABLE:-0} +export HCCL_DETERMINISTIC=${HCCL_DETERMINISTIC:-true} +export VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING:-0} +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export ASCEND_GLOBAL_LOG_LEVEL=${ASCEND_GLOBAL_LOG_LEVEL:-3} +export HYDRA_FULL_ERROR=1 +export RAY_gcs_server_rpc_server_thread_num=${RAY_gcs_server_rpc_server_thread_num:-32} +export RAY_gcs_server_request_timeout_seconds=${RAY_gcs_server_request_timeout_seconds:-600} +export RAY_timeout_ms=${RAY_timeout_ms:-600000} +export RAY_worker_register_timeout_seconds=${RAY_worker_register_timeout_seconds:-600} +export RAY_USAGE_STATS_ENABLED=0 +export VERL_REUSE_AGENT_LOOP=${VERL_REUSE_AGENT_LOOP:-1} + +ulimit -n 65536 + +# Ray 不要覆盖 ASCEND_RT_VISIBLE_DEVICES;VERL 内部按 local_rank 选卡。 +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + +# ================= 路径与数据配置 ================= +HDFS_ROOT=${HDFS_ROOT:-$PWD} +DATA_ROOT=${DATA_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_ckpt} + +# Nanoclaw 数据输入支持两种目录,优先推荐 0625 扁平格式: +# base_tasks/data_*/env_builder.py +# base_tasks/data_*/prompts.md +# base_tasks/data_*/workplace_verifier.py +# base_tasks/data_*/manifest.json +# 也兼容旧格式:base_tasks/tasks/data_* + base_tasks/scripts|scrips/data_*。 +DEFAULT_NANOCLAW_BASE_TASKS=${DEFAULT_NANOCLAW_BASE_TASKS:-/opt/huawei/dataset/zyr_yuyin/lyf/datasets/nanoclawRLdata/0710_add1000agent_qwen3_7_max_v1/exported_new_data} +train_base_tasks=${TRAIN_DATA_PATH:-${BASE_TASKS:-${DEFAULT_NANOCLAW_BASE_TASKS}}} +val_base_tasks=${VAL_DATA_PATH:-${VAL_BASE_TASKS:-${train_base_tasks}}} +train_files="['$train_base_tasks']" +test_files="['$val_base_tasks']" + +if [ ! -d "${train_base_tasks}" ]; then + echo "ERROR: Nanoclaw TRAIN_DATA_PATH/BASE_TASKS directory not found: ${train_base_tasks}" >&2 + exit 2 +fi +if [ ! -d "${val_base_tasks}" ]; then + echo "ERROR: Nanoclaw VAL_DATA_PATH/VAL_BASE_TASKS directory not found: ${val_base_tasks}" >&2 + exit 2 +fi + +model_path=${MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-27B} +verifier_model_path=${VERIFIER_MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +# 纯 VERL engine 路线:不要使用 MindSpeed-MM YAML。 +unset MM_CONFIG_FILE || true + +# Nanoclaw 工具配置 +tool_config_path=${TOOL_CONFIG_PATH:-nanoclaw_recipe/nanoclaw_tool_config.yaml} +nanoclaw_task_glob=${NANOCLAW_TASK_GLOB:-data_*} +nanoclaw_task_ids=${NANOCLAW_TASK_IDS:-} +# 多机训练必须用所有节点都能访问的共享目录;不要用 /tmp,否则 reward worker 可能跨节点找不到 workspace。 +nanoclaw_temp_root=${NANOCLAW_TEMP_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_workplace_v14_qwen35_27b_16k} +# 默认保留每个 step/data_sample 的目录,方便复盘每条 GRPO 采样;磁盘紧张时手动设 NANOCLAW_CLEANUP_WORKSPACES=True。 +nanoclaw_cleanup_workspaces=${NANOCLAW_CLEANUP_WORKSPACES:-False} +nanoclaw_keep_failed_workspaces=${NANOCLAW_KEEP_FAILED_WORKSPACES:-False} +nanoclaw_env_builder_timeout=${NANOCLAW_ENV_BUILDER_TIMEOUT:-120} +nanoclaw_verifier_timeout=${NANOCLAW_VERIFIER_TIMEOUT:-3600} +nanoclaw_reward_score_mode=${NANOCLAW_REWARD_SCORE_MODE:-ratio} +nanoclaw_allow_bash=${NANOCLAW_ALLOW_BASH:-True} +nanoclaw_max_steps=${NANOCLAW_MAX_STEPS:-} +nanoclaw_require_final_answer=${NANOCLAW_REQUIRE_FINAL_ANSWER:-True} +nanoclaw_final_answer_bonus_enable=${NANOCLAW_FINAL_ANSWER_BONUS_ENABLE:-False} +nanoclaw_final_answer_bonus_score=${NANOCLAW_FINAL_ANSWER_BONUS_SCORE:-0.0} +nanoclaw_turn_penalty_only_positive_score=${NANOCLAW_TURN_PENALTY_ONLY_POSITIVE_SCORE:-False} +nanoclaw_assistant_turn_penalty=${NANOCLAW_ASSISTANT_TURN_PENALTY:-0.0} +nanoclaw_duplicate_tool_call_penalty=${NANOCLAW_DUPLICATE_TOOL_CALL_PENALTY:-0.0} +nanoclaw_repeated_response_penalty=${NANOCLAW_REPEATED_RESPONSE_PENALTY:-0.0} +nanoclaw_repeated_response_min_chars=${NANOCLAW_REPEATED_RESPONSE_MIN_CHARS:-35} +nanoclaw_repeated_response_min_consecutive_repeats=${NANOCLAW_REPEATED_RESPONSE_MIN_CONSECUTIVE_REPEATS:-5} +nanoclaw_mask_looping_responses=${NANOCLAW_MASK_LOOPING_RESPONSES:-True} +nanoclaw_mask_only_positive_advantage=${NANOCLAW_MASK_ONLY_POSITIVE_ADVANTAGE:-True} +nanoclaw_mask_budget_exhausted_last_turn=${NANOCLAW_MASK_BUDGET_EXHAUSTED_LAST_TURN:-True} +nanoclaw_mask_duplicate_tool_result_turns=${NANOCLAW_MASK_DUPLICATE_TOOL_RESULT_TURNS:-True} +nanoclaw_mask_error_tool_result_turns=${NANOCLAW_MASK_ERROR_TOOL_RESULT_TURNS:-True} + +# verify_workplace.py 如需调用本地 OpenAI-compatible API,可用这些变量传入 reward。 +# 默认假设 5 机 40 卡:前 4 个节点加入 Ray 训练,第 5 个节点部署 verifier/vLLM API。 +verifier_api_node_rank=${VERIFIER_API_NODE_RANK:-4} +verifier_api_port=${VERIFIER_API_PORT:-8000} +verifier_api_host=${VERIFIER_API_HOST:-${MA_VJ_NAME}-${MA_TASK_NAME}-${verifier_api_node_rank}.${MA_VJ_NAME}} +verifier_api_start_cmd=${VERIFIER_API_START_CMD:-} +verifier_api_bind_host=${VERIFIER_API_BIND_HOST:-0.0.0.0} +# 9B verifier 默认使用整台 8 卡节点:两份 TP4 副本由 vLLM 内置 DP 统一服务。 +# 如需单副本 TP8,可设置 VERIFIER_API_TP=8 VERIFIER_API_DP=1。 +verifier_api_tp=${VERIFIER_API_TP:-4} +verifier_api_dp=${VERIFIER_API_DP:-2} +verifier_api_devices=${VERIFIER_API_DEVICES:-0,1,2,3,4,5,6,7} +verifier_api_distributed_executor_backend=${VERIFIER_API_DISTRIBUTED_EXECUTOR_BACKEND:-mp} +verifier_api_max_model_len=${VERIFIER_API_MAX_MODEL_LEN:-32768} +verifier_api_max_num_batched_tokens=${VERIFIER_API_MAX_NUM_BATCHED_TOKENS:-32768} +verifier_api_max_num_seqs=${VERIFIER_API_MAX_NUM_SEQS:-160} +verifier_api_gpu_memory_utilization=${VERIFIER_API_GPU_MEMORY_UTILIZATION:-0.70} +verifier_api_enforce_eager=${VERIFIER_API_ENFORCE_EAGER:-0} +verifier_api_enable_graph_mode=${VERIFIER_API_ENABLE_GRAPH_MODE:-1} +verifier_api_enable_prefix_caching=${VERIFIER_API_ENABLE_PREFIX_CACHING:-0} +verifier_api_startup_timeout=${VERIFIER_API_STARTUP_TIMEOUT:-1800} +verifier_api_log=${VERIFIER_API_LOG:-logs/vllm-verifier-api.log} +mock_api_base=${MOCK_API_BASE:-http://${verifier_api_host}:${verifier_api_port}/v1} +mock_api_key=${MOCK_API_KEY:-dummy_key} +mock_model_name=${MOCK_MODEL_NAME:-qwen3_5_9b_verifier} +# verify_workplace.py 内部 OpenAI/httpx 单次请求超时;reward API 排队时宁可多等,不要轻易误判 0 分。 +mock_api_timeout=${MOCK_API_TIMEOUT:-1800} +mock_api_connect_timeout=${MOCK_API_CONNECT_TIMEOUT:-300} +# 强制 verifier/OpenAI judge 请求关闭 thinking,sitecustomize 会自动注入 extra_body.chat_template_kwargs.enable_thinking=False。 +nanoclaw_force_no_thinking=${NANOCLAW_FORCE_NO_THINKING:-1} +nanoclaw_force_max_tokens=${NANOCLAW_FORCE_MAX_TOKENS:-50} +# 默认控制台只打一行 reward 摘要;如需每项 details,设 NANOCLAW_REWARD_PRINT_DETAILS=1。 +nanoclaw_reward_print_details=${NANOCLAW_REWARD_PRINT_DETAILS:-0} +# verifier API 是单独节点,默认低并发,避免 RewardLoopWorker 同时打爆 API 导致排队超时。 +reward_num_workers=${REWARD_NUM_WORKERS:-52} + +project_name=${PROJECT_NAME:-qwen3.5-27b_nanoclaw_grpo_verl_0720} +experiment_name=${EXPERIMENT_NAME:-qwen3.5-27b_nanoclaw_grpo_16k_verl0720_lr1e6_fixedkl1e-3} +default_local_dir=${DEFAULT_LOCAL_DIR:-$DATA_ROOT/checkpoint/$experiment_name} +start_time=$(date +%Y%m%d)_$(date +%H%M%S) +mkdir -p logs "${default_local_dir}" + +# ================= 算法与并行参数 ================= +adv_estimator=grpo +max_turns=${MAX_TURNS:-30} +max_prompt_length=${MAX_PROMPT_LENGTH:-8192} +max_response_length=${MAX_RESPONSE_LENGTH:-22768} +max_assistant_response_length=${MAX_ASSISTANT_RESPONSE_LENGTH:-16384} +max_tool_response_length=${MAX_TOOL_RESPONSE_LENGTH:-8192} +max_model_len=$((max_prompt_length + max_response_length)) + +# MindSpeed 配置仅作为训练意图参考;以下均使用最新版 VERL 的原生字段。 +actor_lr=${ACTOR_LR:-1e-6} +actor_lr_scheduler_type=${ACTOR_LR_SCHEDULER_TYPE:-constant} +actor_lr_warmup_steps_ratio=${ACTOR_LR_WARMUP_STEPS_RATIO:-0.0} +actor_weight_decay=${ACTOR_WEIGHT_DECAY:-0.01} +actor_adam_beta1=${ACTOR_ADAM_BETA1:-0.9} +actor_adam_beta2=${ACTOR_ADAM_BETA2:-0.95} +actor_clip_grad=${ACTOR_CLIP_GRAD:-1.0} +actor_ppo_epochs=${ACTOR_PPO_EPOCHS:-1} +actor_shuffle=${ACTOR_SHUFFLE:-False} +actor_entropy_coeff=${ACTOR_ENTROPY_COEFF:-0.0} +actor_clip_ratio_low=${ACTOR_CLIP_RATIO_LOW:-0.2} +actor_clip_ratio_high=${ACTOR_CLIP_RATIO_HIGH:-0.2} +# MindSpeed 配置没有 Dual-Clip PPO 对应项,保留该 27B 脚本原来的 C=10。 +actor_clip_ratio_c=${ACTOR_CLIP_RATIO_C:-10.0} + +# YAML 的 fixed init_kl_coef + low_var_kl 对应 VERL 的 reward-KL 路径。 +algorithm_gamma=${ALGORITHM_GAMMA:-1.0} +algorithm_lam=${ALGORITHM_LAM:-0.95} +use_kl_in_reward=${USE_KL_IN_REWARD:-True} +kl_penalty=${KL_PENALTY:-low_var_kl} +kl_ctrl_type=${KL_CTRL_TYPE:-fixed} +kl_coef=${KL_COEF:-0.001} +# 关闭 actor-KL,避免与 reward-KL 重复惩罚。 +actor_use_kl_loss=${ACTOR_USE_KL_LOSS:-False} +actor_kl_loss_coef=${ACTOR_KL_LOSS_COEF:-0.001} +actor_kl_loss_type=${ACTOR_KL_LOSS_TYPE:-low_var_kl} + +train_batch_size=${TRAIN_BATCH_SIZE:-64} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-16} +n_resp_per_prompt=${N_RESP_PER_PROMPT:-8} +# 先压低验证,避免验证和训练稳定性混在一起。 +n_resp_per_prompt_val=${N_RESP_PER_PROMPT_VAL:-1} +log_val_generations=${LOG_VAL_GENERATIONS:-10} + +infer_tp=${INFER_TP:-4} +train_sp=${TRAIN_SP:-1} +offload=${OFFLOAD:-True} + +# 默认沿用旧 new-engine 跑通 4k 前几步的形状;如 HCCL allreduce 仍失败,试 ACTOR_STRATEGY=fsdp2 FSDP_SIZE=16。 +actor_strategy=${ACTOR_STRATEGY:-fsdp} +fsdp_size=${FSDP_SIZE:-} + +actor_pack=${ACTOR_PACK:-1} +logprob_pack=${LOGPROB_PACK:-2} +actor_max_token_len_per_gpu=${ACTOR_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * actor_pack + train_sp - 1) / train_sp))} +log_prob_max_token_len_per_gpu=${LOG_PROB_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * logprob_pack + train_sp - 1) / train_sp))} +rollout_max_num_batched_tokens=${ROLLOUT_MAX_NUM_BATCHED_TOKENS:-32384} +rollout_gpu_memory_utilization=${ROLLOUT_GPU_MEMORY_UTILIZATION:-0.60} +update_weights_bucket_mb=${UPDATE_WEIGHTS_BUCKET_MB:-8192} + +# Qwen 官方推荐:Instruct/non-thinking reasoning tasks +rollout_temperature=${ROLLOUT_TEMPERATURE:-0.6} +rollout_top_p=${ROLLOUT_TOP_P:-0.95} +rollout_top_k=${ROLLOUT_TOP_K:-20} +rollout_min_p=${ROLLOUT_MIN_P:-0.0} +rollout_presence_penalty=${ROLLOUT_PRESENCE_PENALTY:-0.0} +rollout_frequency_penalty=${ROLLOUT_FREQUENCY_PENALTY:-0.0} +rollout_repetition_penalty=${ROLLOUT_REPETITION_PENALTY:-1.0} + +echo "DEBUG: max_response_length=${max_response_length}, max_assistant_response_length=${max_assistant_response_length}, max_model_len=${max_model_len}" +echo "DEBUG: max_turns=${max_turns}" +echo "DEBUG: max_tool_response_length=${max_tool_response_length}" +echo "DEBUG: train_batch_size=${train_batch_size}, ppo_mini_batch_size=${ppo_mini_batch_size}, n=${n_resp_per_prompt}" +echo "DEBUG: train_sp=${train_sp}, infer_tp=${infer_tp}, actor_strategy=${actor_strategy}, fsdp_size=${fsdp_size:-}" +echo "DEBUG: Qwen3.5 Ulysses FLA required=${NANOCLAW_REQUIRE_FLA}, backend=${QWEN35_FLA_BACKEND} (TRAIN_SP=${train_sp})" +echo "DEBUG: actor_max_token_len_per_gpu=${actor_max_token_len_per_gpu}, log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu}" +echo "DEBUG: rollout sampling temperature=${rollout_temperature}, top_p=${rollout_top_p}, top_k=${rollout_top_k}, min_p=${rollout_min_p}, presence_penalty=${rollout_presence_penalty}, frequency_penalty=${rollout_frequency_penalty}, repetition_penalty=${rollout_repetition_penalty}" +echo "DEBUG: optimizer lr=${actor_lr}, scheduler=${actor_lr_scheduler_type}, warmup_ratio=${actor_lr_warmup_steps_ratio}, weight_decay=${actor_weight_decay}, betas=(${actor_adam_beta1},${actor_adam_beta2}), clip_grad=${actor_clip_grad}, ppo_epochs=${actor_ppo_epochs}, shuffle=${actor_shuffle}" +echo "DEBUG: KL use_in_reward=${use_kl_in_reward}, penalty=${kl_penalty}, ctrl=${kl_ctrl_type}, coef=${kl_coef}, actor_kl=${actor_use_kl_loss}" +echo "DEBUG: HCCL_BUFFSIZE=${HCCL_BUFFSIZE}, HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE}, HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE}" + +val_before_train=${VAL_BEFORE_TRAIN:-False} +trainer_use_v1=${TRAINER_USE_V1:-True} +test_freq=${TEST_FREQ:-5000} +save_freq=${SAVE_FREQ:-2} + +# ================= 分布式 ================= +export TOTAL_NNODES=${TOTAL_NNODES:-5} +export TRAIN_NNODES=${TRAIN_NNODES:-4} +export NNODES=${NNODES:-${TRAIN_NNODES}} +export NODE_RANK=${VC_TASK_INDEX} +export NPUS_PER_NODE=${NPUS_PER_NODE:-8} +export WORLD_SIZE=$((NPUS_PER_NODE * NNODES)) + +export MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-0.${MA_VJ_NAME} +export MASTER_PORT=${MASTER_PORT:-6167} +export DASHBOARD_PORT=${DASHBOARD_PORT:-8191} +export RAY_PORT=${RAY_PORT:-6167} + +readonly SOCKET_IFNAME=${SOCKET_IFNAME:-eth0} +export HCCL_SOCKET_IFNAME=${HCCL_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export GLOO_SOCKET_IFNAME=${GLOO_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export CURRENT_IP=$(ifconfig ${SOCKET_IFNAME} | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +export RAY_NODE_IP=${MA_CURRENT_IP:-${CURRENT_IP}} + +export ASCEND_RT_VISIBLE_DEVICES=${ASCEND_RT_VISIBLE_DEVICES:-$(seq -s, 0 $((NPUS_PER_NODE - 1)))} + +cat < [Verifier API Node] This node is reserved for vLLM/OpenAI-compatible verifier API." + echo "--> [Verifier API Node] API base: ${mock_api_base}" + export VLLM_ENABLE_GRAPH_MODE=${verifier_api_enable_graph_mode} + mkdir -p "$(dirname "${verifier_api_log}")" + if [ -n "${verifier_api_start_cmd}" ]; then + echo "--> [Verifier API Node] Running VERIFIER_API_START_CMD..." + bash -lc "${verifier_api_start_cmd}" & + verifier_api_pid=$! + else + echo "--> [Verifier API Node] Starting default vLLM verifier API..." + verifier_api_device_count=$(awk -F',' '{print NF}' <<<"${verifier_api_devices}") + verifier_api_expected_device_count=$((verifier_api_tp * verifier_api_dp)) + if [ "${verifier_api_device_count}" -ne "${verifier_api_expected_device_count}" ]; then + echo "ERROR: verifier TP*DP=${verifier_api_tp}*${verifier_api_dp}=${verifier_api_expected_device_count}, but VERIFIER_API_DEVICES=${verifier_api_devices} contains ${verifier_api_device_count} devices." >&2 + exit 2 + fi + export ASCEND_RT_VISIBLE_DEVICES=${verifier_api_devices} + verifier_api_args=( + --model "${verifier_model_path}" + --tokenizer "${verifier_model_path}" + --host "${verifier_api_bind_host}" + --port "${verifier_api_port}" + --served-model-name "${mock_model_name}" + --tensor-parallel-size "${verifier_api_tp}" + --data-parallel-size "${verifier_api_dp}" + --distributed-executor-backend "${verifier_api_distributed_executor_backend}" + --dtype bfloat16 + --max-model-len "${verifier_api_max_model_len}" + --max-num-batched-tokens "${verifier_api_max_num_batched_tokens}" + --max-num-seqs "${verifier_api_max_num_seqs}" + --gpu-memory-utilization "${verifier_api_gpu_memory_utilization}" + --trust-remote-code + ) + if [ "${verifier_api_enforce_eager}" = "1" ] || [ "${verifier_api_enforce_eager}" = "true" ] || [ "${verifier_api_enforce_eager}" = "True" ]; then + verifier_api_args+=(--enforce-eager) + fi + if [ "${verifier_api_enable_prefix_caching}" = "1" ] || [ "${verifier_api_enable_prefix_caching}" = "true" ] || [ "${verifier_api_enable_prefix_caching}" = "True" ]; then + verifier_api_args+=(--enable-prefix-caching) + fi + echo "--> [Verifier API Node] Command: python3 -m vllm.entrypoints.openai.api_server ${verifier_api_args[*]}" + python3 -m vllm.entrypoints.openai.api_server "${verifier_api_args[@]}" >"${verifier_api_log}" 2>&1 & + verifier_api_pid=$! + fi + + echo "--> [Verifier API Node] vLLM API pid=${verifier_api_pid}, log=${verifier_api_log}" + echo "--> [Verifier API Node] Waiting for ${mock_api_base}/models ..." + python3 - "${mock_api_base}/models" "${verifier_api_startup_timeout}" "${verifier_api_log}" "${verifier_api_pid}" <<'PY' +import os +import sys +import time +import urllib.request +from pathlib import Path + +url = sys.argv[1] +timeout = float(sys.argv[2]) +log_path = Path(sys.argv[3]) +pid = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[4] else None +started = time.time() +last_error = None +while time.time() - started < timeout: + if pid is not None: + try: + os.kill(pid, 0) + except OSError: + print(f"ERROR: verifier API process exited early: pid={pid}", file=sys.stderr) + if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) + sys.exit(1) + try: + with urllib.request.urlopen(url, timeout=5) as response: + if 200 <= response.status < 300: + print(f"READY: {url}", file=sys.stderr) + sys.exit(0) + except Exception as exc: + last_error = exc + time.sleep(5) +print(f"ERROR: timed out waiting for {url}; last_error={last_error}", file=sys.stderr) +if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) +sys.exit(1) +PY + verifier_readiness_rc=$? + if [ "${verifier_readiness_rc}" -ne 0 ]; then + echo "ERROR: verifier API readiness check failed with rc=${verifier_readiness_rc}." >&2 + if kill -0 "${verifier_api_pid}" 2>/dev/null; then + kill "${verifier_api_pid}" 2>/dev/null || true + fi + wait "${verifier_api_pid}" 2>/dev/null || true + exit "${verifier_readiness_rc}" + fi + + echo "--> [Verifier API Node] Ready. Keeping node alive." + wait "${verifier_api_pid}" + verifier_api_rc=$? + if [ "${verifier_api_rc}" -ne 0 ]; then + echo "ERROR: verifier API exited with rc=${verifier_api_rc}; log=${verifier_api_log}" >&2 + fi + exit "${verifier_api_rc}" +fi + +export TMPDIR=/cache/ray_tmp +export HCCL_ASYNC_ERROR_HANDLING=${HCCL_ASYNC_ERROR_HANDLING:-0} + +wait_for_ray_npu_resources() { + expected_npu=$1 + timeout_seconds=${2:-900} + begin_ts=$(date +%s) + + while true; do + total_npu=$(python3 - <<'PY' 2>/dev/null +import ray + +try: + ray.init(address="auto", ignore_reinit_error=True, logging_level="ERROR") + print(int(ray.cluster_resources().get("NPU", 0))) + ray.shutdown() +except Exception: + print(0) +PY +) + total_npu=${total_npu:-0} + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + + echo "Ray NPU resources: ${total_npu}/${expected_npu}, elapsed=${elapsed}s" + ray status || true + + if [ "${total_npu}" -ge "${expected_npu}" ]; then + echo "Ray cluster is ready: ${total_npu}/${expected_npu} NPU resources registered." + break + fi + + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for Ray NPU resources: ${total_npu}/${expected_npu}" >&2 + return 1 + fi + + sleep 5 + done +} + +wait_for_verifier_api() { + api_url="${mock_api_base}/models" + timeout_seconds=${VERIFIER_API_CLIENT_WAIT_TIMEOUT:-1800} + begin_ts=$(date +%s) + last_diag_ts=0 + while true; do + verifier_check_output=$(python3 - "${api_url}" <<'PY' 2>&1 +import socket +import sys +import urllib.parse +import urllib.request + +url = sys.argv[1] +parsed = urllib.parse.urlparse(url) +host = parsed.hostname +port = parsed.port or (443 if parsed.scheme == "https" else 80) +print(f"check url={url} host={host} port={port}") +try: + infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + print("dns=" + ",".join(sorted({item[4][0] for item in infos}))) +except Exception as exc: + print(f"dns_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with socket.create_connection((host, port), timeout=5): + print("tcp=ok") +except Exception as exc: + print(f"tcp_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with urllib.request.urlopen(url, timeout=10) as response: + print(f"http_status={response.status}") + raise SystemExit(0 if 200 <= response.status < 300 else 1) +except Exception as exc: + print(f"http_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +PY +) + check_rc=$? + if [ "${check_rc}" = "0" ]; then + echo "Verifier API is ready: ${api_url}" + echo "${verifier_check_output}" + break + fi + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + echo "Waiting for verifier API: ${api_url}, elapsed=${elapsed}s" + if [ $((now_ts - last_diag_ts)) -ge 60 ]; then + last_diag_ts=${now_ts} + echo "--- verifier API check diagnostics ---" + echo "${verifier_check_output}" + echo "--- expected verifier node: rank=${verifier_api_node_rank}, host=${verifier_api_host}, port=${verifier_api_port} ---" + echo "--- check verifier node log: ${verifier_api_log} ---" + echo "--------------------------------------" + fi + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for verifier API: ${api_url}" >&2 + echo "Last verifier API diagnostics:" >&2 + echo "${verifier_check_output}" >&2 + return 1 + fi + sleep 10 + done +} + +# ================= Nanoclaw workspace 根目录 ================= +mkdir -p "${nanoclaw_temp_root}" +if ! touch "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" 2>/dev/null; then + echo "ERROR: Cannot write NANOCLAW_TEMP_ROOT: ${nanoclaw_temp_root}" >&2 + exit 2 +fi +rm -f "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" || true +if [[ "${nanoclaw_temp_root}" == /tmp/* ]]; then + echo "WARNING: NANOCLAW_TEMP_ROOT is under /tmp. Multi-node reward workers may not see rollout workspaces." >&2 + echo "WARNING: Prefer a shared path, e.g. ${DATA_ROOT}/nanoclaw_workspaces" >&2 +fi +echo "DEBUG: Nanoclaw train_base_tasks=${train_base_tasks}" +echo "DEBUG: Nanoclaw val_base_tasks=${val_base_tasks}" +echo "DEBUG: Nanoclaw task_glob=${nanoclaw_task_glob}, task_ids=${nanoclaw_task_ids:-}" +echo "DEBUG: Nanoclaw temp_root=${nanoclaw_temp_root}, cleanup=${nanoclaw_cleanup_workspaces}, keep_failed=${nanoclaw_keep_failed_workspaces}" + +# ================= 生成 Ray runtime env ================= +RUNTIME_ENV_FILE=${WORK_DIR}/verl_engine_runtime_env.generated.yaml +cat > "${RUNTIME_ENV_FILE}" < [Head Node] Starting Ray Head on ${CURRENT_IP}..." + ray start --head \ + --node-ip-address=${RAY_NODE_IP} \ + --port=${RAY_PORT} \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=${DASHBOARD_PORT} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + + sleep 10 + wait_for_ray_npu_resources ${WORLD_SIZE} 900 || exit 1 + wait_for_verifier_api || exit 1 +else + echo "--> [Worker Node] Starting Ray Worker, connecting to ${MASTER_ADDR}:${RAY_PORT}..." + sleep 20 + ray start --address=${MASTER_ADDR}:${RAY_PORT} \ + --node-ip-address=${RAY_NODE_IP} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + sleep 10 +fi + +# ================= 训练参数数组 ================= +training_args=( + python3 -m verl.trainer.main_ppo + +ray_kwargs.ray_init.address=auto + reward.num_workers=${reward_num_workers} + algorithm.adv_estimator=${adv_estimator} + algorithm.gamma=${algorithm_gamma} + algorithm.lam=${algorithm_lam} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_penalty=${kl_penalty} + algorithm.kl_ctrl.type=${kl_ctrl_type} + algorithm.kl_ctrl.kl_coef=${kl_coef} + data.train_files="${train_files}" + data.val_files="${test_files}" + data.return_raw_chat=True + data.return_multi_modal_inputs=False + data.image_key=images + data.shuffle=True + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation=error + data.custom_cls.path=pkg://nanoclaw_recipe.nanoclaw + data.custom_cls.name=CustomRLHFDataset + "data.tool_config_path=${tool_config_path}" + "+data.nanoclaw_task_glob=${nanoclaw_task_glob}" + "+data.nanoclaw_temp_root=${nanoclaw_temp_root}" + "+data.nanoclaw_cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+data.nanoclaw_keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+data.nanoclaw_env_builder_timeout=${nanoclaw_env_builder_timeout}" + "+data.nanoclaw_verifier_timeout=${nanoclaw_verifier_timeout}" + "+data.nanoclaw_reward_score_mode=${nanoclaw_reward_score_mode}" + "+data.nanoclaw_allow_bash=${nanoclaw_allow_bash}" + +data.apply_chat_template_kwargs.enable_thinking=True + reward.custom_reward_function.path=pkg://nanoclaw_recipe.nanoclaw + reward.custom_reward_function.name=compute_score + "+reward.custom_reward_function.reward_kwargs.cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+reward.custom_reward_function.reward_kwargs.keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+reward.custom_reward_function.reward_kwargs.verifier_timeout=${nanoclaw_verifier_timeout}" + "+reward.custom_reward_function.reward_kwargs.reward_score_mode=${nanoclaw_reward_score_mode}" + "+reward.custom_reward_function.reward_kwargs.require_final_answer=${nanoclaw_require_final_answer}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_enable=${nanoclaw_final_answer_bonus_enable}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_score=${nanoclaw_final_answer_bonus_score}" + "+reward.custom_reward_function.reward_kwargs.turn_penalty_only_positive_score=${nanoclaw_turn_penalty_only_positive_score}" + "+reward.custom_reward_function.reward_kwargs.assistant_turn_penalty=${nanoclaw_assistant_turn_penalty}" + "+reward.custom_reward_function.reward_kwargs.duplicate_tool_call_penalty=${nanoclaw_duplicate_tool_call_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_penalty=${nanoclaw_repeated_response_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_chars=${nanoclaw_repeated_response_min_chars}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_consecutive_repeats=${nanoclaw_repeated_response_min_consecutive_repeats}" + "+reward.custom_reward_function.reward_kwargs.mock_api_base=${mock_api_base}" + "+reward.custom_reward_function.reward_kwargs.mock_api_key=${mock_api_key}" + "+reward.custom_reward_function.reward_kwargs.mock_model_name=${mock_model_name}" + "+reward.custom_reward_function.reward_kwargs.mock_api_timeout=${mock_api_timeout}" + "+reward.custom_reward_function.reward_kwargs.mock_api_connect_timeout=${mock_api_connect_timeout}" + actor_rollout_ref.model.path=${model_path} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True + actor_rollout_ref.actor.strategy=${actor_strategy} + actor_rollout_ref.ref.strategy=${actor_strategy} + actor_rollout_ref.actor.use_kl_loss=${actor_use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${actor_kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=${actor_kl_loss_type} + actor_rollout_ref.actor.clip_ratio_low=${actor_clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${actor_clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=${actor_clip_ratio_c} + actor_rollout_ref.actor.entropy_coeff=${actor_entropy_coeff} + actor_rollout_ref.actor.ppo_epochs=${actor_ppo_epochs} + actor_rollout_ref.actor.shuffle=${actor_shuffle} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.optim.lr_scheduler_type=${actor_lr_scheduler_type} + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=${actor_lr_warmup_steps_ratio} + actor_rollout_ref.actor.optim.weight_decay=${actor_weight_decay} + "actor_rollout_ref.actor.optim.betas=[${actor_adam_beta1},${actor_adam_beta2}]" + actor_rollout_ref.actor.optim.clip_grad=${actor_clip_grad} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_max_token_len_per_gpu} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.mode=async + actor_rollout_ref.rollout.temperature=${rollout_temperature} + actor_rollout_ref.rollout.top_p=${rollout_top_p} + actor_rollout_ref.rollout.top_k=${rollout_top_k} + actor_rollout_ref.rollout.min_p=${rollout_min_p} + actor_rollout_ref.rollout.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.tensor_model_parallel_size=${infer_tp} + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=${update_weights_bucket_mb} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.max_num_batched_tokens=${rollout_max_num_batched_tokens} + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.enable_prefix_caching=False + actor_rollout_ref.rollout.multi_turn.enable=True + actor_rollout_ref.rollout.multi_turn.max_user_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_response_length=${max_assistant_response_length} + "actor_rollout_ref.rollout.multi_turn.tool_config_path=${tool_config_path}" + actor_rollout_ref.rollout.multi_turn.format=qwen3_coder + "actor_rollout_ref.rollout.multi_turn.max_tool_response_length=${max_tool_response_length}" + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_memory_utilization} + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.val_kwargs.temperature=${rollout_temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${rollout_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${rollout_top_k} + actor_rollout_ref.rollout.val_kwargs.min_p=${rollout_min_p} + actor_rollout_ref.rollout.val_kwargs.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.val_kwargs.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.val_kwargs.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=${n_resp_per_prompt_val} + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.fsdp_config.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.use_torch_compile=False + critic.fsdp.use_torch_compile=False + trainer.use_v1=${trainer_use_v1} + trainer.critic_warmup=0 + trainer.balance_batch=True + trainer.logger=['console','tensorboard'] + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.nnodes=${NNODES} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.val_before_train=${val_before_train} + trainer.log_val_generations=${log_val_generations} + trainer.save_freq=${save_freq} + trainer.default_local_dir=${default_local_dir} + trainer.test_freq=${test_freq} + trainer.total_epochs=10 +) + +if [ -n "${fsdp_size}" ]; then + training_args+=( + actor_rollout_ref.actor.fsdp_config.fsdp_size=${fsdp_size} + actor_rollout_ref.ref.fsdp_config.fsdp_size=${fsdp_size} + ) +fi + +if [ -n "${nanoclaw_task_ids}" ]; then + training_args+=("+data.nanoclaw_task_ids=${nanoclaw_task_ids}") +fi + +if [ -n "${nanoclaw_max_steps}" ]; then + training_args+=("+data.nanoclaw_max_steps=${nanoclaw_max_steps}") +fi + +# ================= 启动训练主进程:仅主节点执行 ================= +if [ "${NODE_RANK}" = "0" ]; then + echo "--> [Head Node] Starting VERL unified engine training..." + echo "DEBUG: runtime_env=${RUNTIME_ENV_FILE}" + echo "DEBUG: entrypoint=${training_args[*]}" + + ray job submit \ + --address="http://127.0.0.1:${DASHBOARD_PORT}" \ + --runtime-env="${RUNTIME_ENV_FILE}" \ + -- \ + "${training_args[@]}" 2>&1 | tee "logs/qwen3.5-nanoclaw-grpo-verl-engine-${start_time}.log" +else + echo "--> [Worker Node] Setup finished. Keeping node alive for Ray..." + tail -f /dev/null +fi diff --git "a/verl_0720_main/27b_old_verl.sh\357\200\272Zone.Identifier" "b/verl_0720_main/27b_old_verl.sh\357\200\272Zone.Identifier" new file mode 100644 index 0000000000000000000000000000000000000000..d1f72df134394a8ea8dc3f79e9ca66fcb0d64ac3 --- /dev/null +++ "b/verl_0720_main/27b_old_verl.sh\357\200\272Zone.Identifier" @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://huggingface.co/datasets/geminiDeveloper/contextBenchmark/tree/main/0720 +HostUrl=https://huggingface.co/api/resolve-cache/datasets/geminiDeveloper/contextBenchmark/abe015ce7c2d5ba256eb08e79702363e02b7603e/0720%2F27b_old_verl.sh?download=true&etag=%22b92a32a229172e8f354b626b41c6254156d0476b%22 diff --git a/verl_0720_main/9b.sh b/verl_0720_main/9b.sh new file mode 100644 index 0000000000000000000000000000000000000000..75838d485fc5180838e6638f9533e90dc1144069 --- /dev/null +++ b/verl_0720_main/9b.sh @@ -0,0 +1,1118 @@ +#!/bin/bash +# Qwen3.5-9B Nanoclaw 多轮 GRPO — VERL 0720 + NPU SP1/31K 长轨迹正式训练版 +# +# 关键修改: +# 1) 入口改为 python3 -m verl.trainer.main_ppo,不再使用 recipe.grpo_mindspeed_mm.main_ppo; +# 2) 不再使用 MM_CONFIG_FILE / MindSpeed-MM YAML; +# 3) 显式 text-only:data.return_multi_modal_inputs=False; +# 4) 数据输入改为 Nanoclaw base_tasks 目录,不再使用 Retool parquet/json; +# 5) 保留 27B 作业验证过的 HCCL buffer 与端口范围,降低 HcclAllreduce socket/资源压力; +# 6) 默认 val n=1、log_val_generations=10,先验证训练稳定性; +# 7) 使用 FSDP2、reshard-after-forward、CPU offload policy 和 activation offload; +# 8) 使用最新版 VERL V1/TransferQueue 和 nanoclaw_recipe,并按参考 YAML 的训练意图映射优化器与 KL; +# 9) 恢复 Nanoclaw 正式训练需要的 22768 response / 16384 assistant 长轨迹预算; +# 10) 启用分块 Torch LM-head,直接计算 token log-prob/entropy,避免 31K×vocab 完整 logits 常驻显存; +# 11) 普通 entropy fallback 仍使用 256-token 分块,防止非 fused 路径产生十几 GiB 临时张量。 + +set -x + +# ================= 正式长轨迹版:Qwen3.5 NPU SP1 + 约 31K ================= +# Nanoclaw 的工具 observation 也占 response budget。8K response 会让有效轨迹过早 +# 以 max_response_tokens 截断,因此正式训练恢复 22768 total response / 16384 assistant。 +export TRAIN_SP=1 +export QWEN35_FLA_BACKEND=disabled +export MAX_PROMPT_LENGTH=8192 +export MAX_RESPONSE_LENGTH=22768 +export MAX_ASSISTANT_RESPONSE_LENGTH=16384 +export MAX_TOOL_RESPONSE_LENGTH=8192 +# 8192 + 22768 = 30960;向上对齐到 32768,确保单条最长样本不会超过 +# actor/ref/log-prob 动态 micro-batch 的 token 容量。 +export ACTOR_MAX_TOKEN_LEN_PER_GPU=32768 +export LOG_PROB_MAX_TOKEN_LEN_PER_GPU=32768 +export ROLLOUT_MAX_NUM_BATCHED_TOKENS=32768 +export ACTOR_STRATEGY=fsdp2 +export OFFLOAD=True +# 当前 VERL 的 checkpointing 分支不会传递自定义 chunk size;稳定版明确关闭它,确保实际使用 256。 +export ENTROPY_FROM_LOGITS_WITH_CHUNKING=True +export ENTROPY_FROM_LOGITS_CHUNK_SIZE=256 +export ENTROPY_CHECKPOINTING=False +# Qwen3.5 fused Torch LM-head 按 token 分块计算全词表 log-prob/entropy,不保留 +# 完整的 [T, vocab] logits;这是 SP1/31K 相比 16K 必须增加的显存保护。 +export USE_FUSED_KERNELS=True +export FUSED_KERNEL_BACKEND=torch +# 正式长序列版默认打开 activation offload;保留环境变量开关用于 NPU 故障隔离。 +export ENABLE_ACTIVATION_OFFLOAD=${ENABLE_ACTIVATION_OFFLOAD:-True} + + +npu-smi info || true +pip install --upgrade pip +pip uninstall -y moxing-framework || true + +# ================= 路径配置 ================= +SCRIPT_DIR=/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/verl_0720_nanoclaw/verl_0720_main +if [ -f "${SCRIPT_DIR}/verl/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +elif [ -f "${SCRIPT_DIR}/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR} +else + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +fi +WORK_DIR=${WORK_DIR:-${DEFAULT_WORK_DIR}} +INSTALL_DIR=${INSTALL_DIR:-/home/ma-user} +BKGS=${BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs} +chmod 755 "${INSTALL_DIR}" + +# Nanoclaw 自定义包已随 WORK_DIR 提供:nanoclaw_recipe。 + +GCC_INSTALL_PREFIX=${GCC_INSTALL_PREFIX:-/home/ma-user/gcc-11.3.0} +COMPILED_GCC_ARCHIVE_PATH=${COMPILED_GCC_ARCHIVE_PATH:-/opt/huawei/dataset/zyr_yuyin/bkgs/gcc-11.3.0-compiled-aarch64.tar.gz} + +echo "--> 正在从缓存恢复 GCC 11.3.0..." +tar -xzf "${COMPILED_GCC_ARCHIVE_PATH}" -C /home/ma-user/ +export PATH=${GCC_INSTALL_PREFIX}/bin:${PATH} +export LD_LIBRARY_PATH=${GCC_INSTALL_PREFIX}/lib64:${GCC_INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH:-} +export CC=${GCC_INSTALL_PREFIX}/bin/gcc +export CXX=${GCC_INSTALL_PREFIX}/bin/g++ +echo "--> 验证 GCC 版本:" +gcc --version + +cd "${BKGS}" +cp jemalloc-5.3.0.tar.bz2 "${INSTALL_DIR}" + +VLLM_LATEST_PKGS=${VLLM_LATEST_PKGS:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-05-12/verl_new_26_05_09/pkgs} +rm -rf "${INSTALL_DIR}/vllm" "${INSTALL_DIR}/vllm-ascend" +cp -r "${VLLM_LATEST_PKGS}/vllm" "${INSTALL_DIR}" +cp -r "${VLLM_LATEST_PKGS}/vllm-ascend" "${INSTALL_DIR}" + +CANN_BKGS=${CANN_BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs/cann_0527} +cp "${CANN_BKGS}/Ascend-cann-toolkit_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-910b-ops_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-nnal_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" + +echo "################" +echo "## set verl env" +echo "################" + +cd "${INSTALL_DIR}" + +chmod +x Ascend-cann-toolkit_9.0.0_linux-aarch64.run +bash Ascend-cann-toolkit_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/ascend-toolkit/set_env.sh" + +chmod +x Ascend-cann-910b-ops_9.0.0_linux-aarch64.run +bash Ascend-cann-910b-ops_9.0.0_linux-aarch64.run --install --quiet + +chmod +x Ascend-cann-nnal_9.0.0_linux-aarch64.run +bash Ascend-cann-nnal_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/nnal/atb/set_env.sh" + +export ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME} +export LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:${LD_LIBRARY_PATH:-} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip3 install torch==2.9.0 +pip3 install pyyaml setuptools +pip3 install torch-npu==2.9.0 +pip3 install torchvision==0.24.0 torchaudio==2.9.0 + +ASCEND_TOOLKIT_PYTHON_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/python/site-packages +export PYTHONPATH=${PYTHONPATH:-}:${INSTALL_DIR}:${ASCEND_TOOLKIT_PYTHON_PATH} +pip install pybind11==2.13.6 + +cd "${INSTALL_DIR}/vllm" +VLLM_TARGET_DEVICE=empty pip install . + +cd "${INSTALL_DIR}/vllm-ascend" +pip install -e . +export VLLM_LOGGING_LEVEL=INFO + +cd "${INSTALL_DIR}" +tar -xvf jemalloc-5.3.0.tar.bz2 +cd jemalloc-5.3.0 +./configure --prefix="${INSTALL_DIR}" +make -j"$(nproc)" +make install +export LD_PRELOAD=${INSTALL_DIR}/lib/libjemalloc.so.2:${LD_PRELOAD:-} + +# # ================= 可选:安装 MindSpeed 栈 ================= +# # 纯 VERL engine 路线不依赖 MindSpeed-MM YAML。默认不安装,避免和新版 VERL engine 混淆。 +# INSTALL_MINDSPEED_STACK=${INSTALL_MINDSPEED_STACK:-0} +# if [ "${INSTALL_MINDSPEED_STACK}" = "1" ]; then +# MindSpeed_PATH=${MindSpeed_PATH:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-slow-stable} +# cd "${MindSpeed_PATH}" +# cd Megatron-LM && pip install -e . --no-deps && cd .. +# cd MindSpeed && pip install -e . --no-deps && cd .. +# cd MindSpeed-MM && mkdir -p logs data ckpt && pip install -e . --no-deps && cd .. +# pip install beartype bs4 diffusers==0.30.3 ftfy imageio-ffmpeg pandarallel pytest-mock +# else +# echo "--> Skip MindSpeed/MindSpeed-MM installation for pure VERL engine route." +# fi + + + +# ================= 安装 Triton-Ascend 3.2.1 ================= +# 1. 卸载 triton(增加 -y 自动确认) +pip uninstall -y triton + +# 2. 卸载 triton-ascend(增加 -y 自动确认) +pip uninstall -y triton-ascend +pip install --no-cache-dir --force-reinstall triton==3.5.0 +pip install --no-deps /opt/huawei/dataset/zyr_yuyin/bkgs/triton_ascend-3.2.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl + + +# ================= 安装新版 VERL ================= +cd "${WORK_DIR}" +pip install -r requirements-npu.txt +# NPU requirements 明确要求 numpy<2;editable 安装不能再次按 setup.py 把 NumPy升级到 2.x。 +python3 -m pip install -e . --no-deps +pip install --upgrade 'urllib3==1.26.11' +pip install loguru +pip install tree_sitter==0.21.3 +pip install tree-sitter-java==0.21.0 +pip install tree-sitter-javascript==0.21.4 + +ACL_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/aarch64-linux/lib64 +export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ACL_PATH} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip uninstall -y transformers || true +pip install transformers==5.3.0 +pip install accelerate==1.13.0 mathruler +pip install jsonargparse +pip install deepdiff sympy html2text requests bs4 mpmath swanlab PandoraBox json_repair openai httpx + +# 稳定版只允许 SP=1:不安装普通 FLA,也不进入尚未完成 NPU 适配的 Ulysses CP。 +if [ "${TRAIN_SP}" != "1" ]; then + echo "ERROR: the long-sequence all-optimization profile requires TRAIN_SP=1; got ${TRAIN_SP}." >&2 + exit 2 +fi +export NANOCLAW_REQUIRE_FLA=0 +echo "--> Stable SP1: flash-linear-attention is disabled; Qwen3.5 will not build an Ulysses CP context." + +# Transformers 5.x 会经 sklearn 间接导入 pandas/scipy。固定同一套 NumPy ABI, +# 避免出现 "numpy.dtype size changed"。这些版本均支持 Python 3.11/aarch64。 +NUMPY_VERSION=${NUMPY_VERSION:-1.26.4} +PANDAS_VERSION=${PANDAS_VERSION:-2.2.3} +SCIPY_VERSION=${SCIPY_VERSION:-1.14.1} +SKLEARN_VERSION=${SKLEARN_VERSION:-1.6.1} +python3 -m pip install --no-cache-dir --force-reinstall \ + "numpy==${NUMPY_VERSION}" \ + "pandas==${PANDAS_VERSION}" \ + "scipy==${SCIPY_VERSION}" \ + "scikit-learn==${SKLEARN_VERSION}" + +python3 - <<'PY' || exit 2 +import numpy +import pandas +import scipy +import sklearn +import sys +import transformers +import vllm + +fla_version = "disabled-sp1" + +print( + "[python_stack_preflight] " + f"python={sys.executable} " + f"numpy={numpy.__version__} " + f"pandas={pandas.__version__} " + f"scipy={scipy.__version__} " + f"sklearn={sklearn.__version__} " + f"transformers={transformers.__version__} " + f"vllm={vllm.__version__} " + f"fla={fla_version}" +) +print( + "[python_stack_paths] " + f"numpy={numpy.__file__} " + f"pandas={pandas.__file__}" +) +PY +pip list + +# ================= 检查 Nanoclaw recipe ================= +if [ ! -f "${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" ]; then + echo "ERROR: Nanoclaw recipe not found: ${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" >&2 + exit 2 +fi +test -f "${WORK_DIR}/nanoclaw_recipe/__init__.py" + +# Qwen3.5 MRoPE position_ids 是 3/4 轴张量。未应用此补丁时,NPU +# FlashAttention 会把 seqLen 重复累计(例如 T=10131、sum(seqLen)=30393)。 +QWEN35_MONKEY_PATCH_FILE=${WORK_DIR}/verl/models/transformers/monkey_patch.py +if [ ! -f "${QWEN35_MONKEY_PATCH_FILE}" ] || ! grep -q "def _normalize_fa_position_ids" "${QWEN35_MONKEY_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FlashAttention position_ids normalization patch is missing: ${QWEN35_MONKEY_PATCH_FILE}" >&2 + echo "Upload the modified verl/ directory together with this standalone script." >&2 + exit 2 +fi + +# Nanoclaw 会在首次模型调用前写入 rollout metadata。旧 ToolAgentLoop 因此 +# 未复制 min/max_global_steps,导致完整训练 step 后在 metrics 阶段把 None 转 int 崩溃。 +TOOL_AGENT_LOOP_FILE=${WORK_DIR}/verl/experimental/agent_loop/tool_agent_loop.py +TRAINER_BASE_FILE=${WORK_DIR}/verl/trainer/ppo/v1/trainer_base.py +if [ ! -f "${TOOL_AGENT_LOOP_FILE}" ] || ! grep -q "output_min_global_steps" "${TOOL_AGENT_LOOP_FILE}"; then + echo "ERROR: Nanoclaw rollout version metadata merge fix is missing: ${TOOL_AGENT_LOOP_FILE}" >&2 + exit 2 +fi +if [ ! -f "${TRAINER_BASE_FILE}" ] || ! grep -q "def resolve_model_version" "${TRAINER_BASE_FILE}"; then + echo "ERROR: PPO metrics None-version fallback fix is missing: ${TRAINER_BASE_FILE}" >&2 + exit 2 +fi + +# 31K 长序列必须使用分块 LM-head;如果训练机只上传了 shell 而没有对应的 +# VERL Qwen3.5 fused backend,则在启动 Ray 前直接失败,避免数十分钟后才 OOM。 +QWEN35_MODEL_PATCH_FILE=${WORK_DIR}/verl/models/transformers/qwen3_5.py +FUSED_LINEAR_FILE=${WORK_DIR}/verl/utils/experimental/torch_functional.py +if [ ! -f "${QWEN35_MODEL_PATCH_FILE}" ] || ! grep -q "def forward_with_torch_backend" "${QWEN35_MODEL_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 fused Torch backend is missing: ${QWEN35_MODEL_PATCH_FILE}" >&2 + exit 2 +fi +if ! grep -q "vocab_weights.full_tensor().to(hidden_states.device)" "${QWEN35_MODEL_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FSDP2 CPU-offload fused LM-head device-staging fix is missing: ${QWEN35_MODEL_PATCH_FILE}" >&2 + exit 2 +fi +if [ ! -f "${FUSED_LINEAR_FILE}" ] || ! grep -q "class FusedLinearForPPO" "${FUSED_LINEAR_FILE}"; then + echo "ERROR: chunked FusedLinearForPPO is missing: ${FUSED_LINEAR_FILE}" >&2 + exit 2 +fi + +ACTIVATION_OFFLOAD_FILE=${WORK_DIR}/verl/utils/activation_offload.py +if [ ! -f "${ACTIVATION_OFFLOAD_FILE}" ] || ! grep -q "Missing offload mapping for group" "${ACTIVATION_OFFLOAD_FILE}"; then + echo "ERROR: FSDP2 checkpoint activation-offload on-demand reload fix is missing: ${ACTIVATION_OFFLOAD_FILE}" >&2 + exit 2 +fi + +# ================= PLOG ================= +ma_vj_name=$(echo "${MA_VJ_NAME}" | sed 's:ma-job:modelarts-job:g') +task_name=worker-${VC_TASK_INDEX} +task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name} +mkdir -p "${task_plog_path}" +export ASCEND_PROCESS_LOG_PATH=${task_plog_path}/${VC_TASK_INDEX} +echo "plog path: ${ASCEND_PROCESS_LOG_PATH}" + +MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-${VC_TASK_INDEX}.${MA_VJ_NAME} +MASTER_PORT=${PORT} +MA_CURRENT_INSTANCE_NAME=${MA_CURRENT_INSTANCE_NAME} + +cd "${WORK_DIR}" + +mkdir -p /cache/ray_tmp + +echo "Cleaning up old Ray processes..." +ray stop --force || true +sleep 5 +rm -rf /cache/ray_tmp/* +pkill -9 -f raylet || true +pkill -9 -f plasma_store || true +pkill -9 -f gcs_server || true +echo "Waiting 20s for NPU/Ray resources to be released..." +npu-smi info || true +sleep 20 + +# ================= NPU / HCCL / Ray 环境 ================= +export NON_MEGATRON=true +export MULTI_STREAM_MEMORY_REUSE=2 +export OMP_NUM_THREADS=1 +export PYTORCH_NPU_ALLOC_CONF=${PYTORCH_NPU_ALLOC_CONF:-max_split_size_mb:512} +export VLLM_LOGGING_LEVEL=INFO +export RAY_DEDUP_LOGS=0 +export HCCL_EXEC_TIMEOUT=${HCCL_EXEC_TIMEOUT:-3600} +export HCCL_LOG_LEVEL=${HCCL_LOG_LEVEL:-WARN} +export HCCL_CONNECT_TIMEOUT=${HCCL_CONNECT_TIMEOUT:-3600} +export HCCL_EVENT_TIMEOUT=${HCCL_EVENT_TIMEOUT:-7200} +export ACL_DEVICE_SYNC_TIMEOUT=${ACL_DEVICE_SYNC_TIMEOUT:-7200} +export GLOO_SOCKET_TIMEOUT=${GLOO_SOCKET_TIMEOUT:-7200} + +# 关键:降低 HCCL buffer,增加 socket 端口范围,缓解 HcclAllreduce ra socket batch connect failed。 +export HCCL_BUFFSIZE=${HCCL_BUFFSIZE:-300} +export P2P_HCCL_BUFFSIZE=${P2P_HCCL_BUFFSIZE:-64} +export HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE:-60000-60050} +export HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE:-61000-61050} + +export CUDA_DEVICE_MAX_CONNECTIONS=1 +export VLLM_ASCEND_ENABLE_NZ=${VLLM_ASCEND_ENABLE_NZ:-0} +export HCCL_OP_EXPANSION_MODE=${HCCL_OP_EXPANSION_MODE:-AIV} +export VLLM_ENGINE_ITERATION_TIMEOUT_S=${VLLM_ENGINE_ITERATION_TIMEOUT_S:-3600} +export WANDB_MODE=${WANDB_MODE:-disabled} +export PYTHONUNBUFFERED=1 +export TASK_QUEUE_ENABLE=${TASK_QUEUE_ENABLE:-1} +export COMBINED_ENABLE=${COMBINED_ENABLE:-1} +export TOKENIZERS_PARALLELISM=false +export CLOSE_MATMUL_K_SHIFT=${CLOSE_MATMUL_K_SHIFT:-1} +export ATB_MATMUL_SHUFFLE_K_ENABLE=${ATB_MATMUL_SHUFFLE_K_ENABLE:-0} +export HCCL_DETERMINISTIC=${HCCL_DETERMINISTIC:-true} +export VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING:-0} +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export ASCEND_GLOBAL_LOG_LEVEL=${ASCEND_GLOBAL_LOG_LEVEL:-3} +export HYDRA_FULL_ERROR=1 +export RAY_gcs_server_rpc_server_thread_num=${RAY_gcs_server_rpc_server_thread_num:-32} +export RAY_gcs_server_request_timeout_seconds=${RAY_gcs_server_request_timeout_seconds:-600} +export RAY_timeout_ms=${RAY_timeout_ms:-600000} +export RAY_worker_register_timeout_seconds=${RAY_worker_register_timeout_seconds:-600} +export RAY_USAGE_STATS_ENABLED=0 +export VERL_REUSE_AGENT_LOOP=${VERL_REUSE_AGENT_LOOP:-1} + +ulimit -n 65536 + +# Ray 不要覆盖 ASCEND_RT_VISIBLE_DEVICES;VERL 内部按 local_rank 选卡。 +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + +# ================= 路径与数据配置 ================= +HDFS_ROOT=${HDFS_ROOT:-$PWD} +DATA_ROOT=${DATA_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_ckpt} + +# Nanoclaw 数据输入支持两种目录,优先推荐 0625 扁平格式: +# base_tasks/data_*/env_builder.py +# base_tasks/data_*/prompts.md +# base_tasks/data_*/workplace_verifier.py +# base_tasks/data_*/manifest.json +# 也兼容旧格式:base_tasks/tasks/data_* + base_tasks/scripts|scrips/data_*。 +DEFAULT_NANOCLAW_BASE_TASKS=${DEFAULT_NANOCLAW_BASE_TASKS:-/opt/huawei/dataset/zyr_yuyin/lyf/datasets/nanoclawRLdata/0710_add1000agent_qwen3_7_max_v1/exported_new_data} +train_base_tasks=${TRAIN_DATA_PATH:-${BASE_TASKS:-${DEFAULT_NANOCLAW_BASE_TASKS}}} +val_base_tasks=${VAL_DATA_PATH:-${VAL_BASE_TASKS:-${train_base_tasks}}} +train_files="['$train_base_tasks']" +test_files="['$val_base_tasks']" + +if [ ! -d "${train_base_tasks}" ]; then + echo "ERROR: Nanoclaw TRAIN_DATA_PATH/BASE_TASKS directory not found: ${train_base_tasks}" >&2 + exit 2 +fi +if [ ! -d "${val_base_tasks}" ]; then + echo "ERROR: Nanoclaw VAL_DATA_PATH/VAL_BASE_TASKS directory not found: ${val_base_tasks}" >&2 + exit 2 +fi + +model_path=${MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +verifier_model_path=${VERIFIER_MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +# 纯 VERL engine 路线:不要使用 MindSpeed-MM YAML。 +unset MM_CONFIG_FILE || true + +# Nanoclaw 工具配置 +tool_config_path=${TOOL_CONFIG_PATH:-nanoclaw_recipe/nanoclaw_tool_config.yaml} +nanoclaw_task_glob=${NANOCLAW_TASK_GLOB:-data_*} +nanoclaw_task_ids=${NANOCLAW_TASK_IDS:-} +# 多机训练必须用所有节点都能访问的共享目录;不要用 /tmp,否则 reward worker 可能跨节点找不到 workspace。 +nanoclaw_temp_root=${NANOCLAW_TEMP_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_workplace_v14_qwen35_9b_sp1_fsdp2_31k_longseq_allopt} +# 默认保留每个 step/data_sample 的目录,方便复盘每条 GRPO 采样;磁盘紧张时手动设 NANOCLAW_CLEANUP_WORKSPACES=True。 +nanoclaw_cleanup_workspaces=${NANOCLAW_CLEANUP_WORKSPACES:-False} +nanoclaw_keep_failed_workspaces=${NANOCLAW_KEEP_FAILED_WORKSPACES:-False} +nanoclaw_env_builder_timeout=${NANOCLAW_ENV_BUILDER_TIMEOUT:-120} +nanoclaw_verifier_timeout=${NANOCLAW_VERIFIER_TIMEOUT:-3600} +nanoclaw_reward_score_mode=${NANOCLAW_REWARD_SCORE_MODE:-ratio} +nanoclaw_allow_bash=${NANOCLAW_ALLOW_BASH:-True} +nanoclaw_max_steps=${NANOCLAW_MAX_STEPS:-} +nanoclaw_require_final_answer=${NANOCLAW_REQUIRE_FINAL_ANSWER:-True} +nanoclaw_final_answer_bonus_enable=${NANOCLAW_FINAL_ANSWER_BONUS_ENABLE:-False} +nanoclaw_final_answer_bonus_score=${NANOCLAW_FINAL_ANSWER_BONUS_SCORE:-0.0} +nanoclaw_turn_penalty_only_positive_score=${NANOCLAW_TURN_PENALTY_ONLY_POSITIVE_SCORE:-False} +nanoclaw_assistant_turn_penalty=${NANOCLAW_ASSISTANT_TURN_PENALTY:-0.0} +nanoclaw_duplicate_tool_call_penalty=${NANOCLAW_DUPLICATE_TOOL_CALL_PENALTY:-0.0} +nanoclaw_repeated_response_penalty=${NANOCLAW_REPEATED_RESPONSE_PENALTY:-0.0} +nanoclaw_repeated_response_min_chars=${NANOCLAW_REPEATED_RESPONSE_MIN_CHARS:-50} +nanoclaw_repeated_response_min_consecutive_repeats=${NANOCLAW_REPEATED_RESPONSE_MIN_CONSECUTIVE_REPEATS:-5} +nanoclaw_mask_looping_responses=${NANOCLAW_MASK_LOOPING_RESPONSES:-True} +nanoclaw_mask_only_positive_advantage=${NANOCLAW_MASK_ONLY_POSITIVE_ADVANTAGE:-True} +nanoclaw_mask_budget_exhausted_last_turn=${NANOCLAW_MASK_BUDGET_EXHAUSTED_LAST_TURN:-True} +nanoclaw_mask_duplicate_tool_result_turns=${NANOCLAW_MASK_DUPLICATE_TOOL_RESULT_TURNS:-True} +nanoclaw_mask_error_tool_result_turns=${NANOCLAW_MASK_ERROR_TOOL_RESULT_TURNS:-True} + +# verify_workplace.py 如需调用本地 OpenAI-compatible API,可用这些变量传入 reward。 +# 默认假设 5 机 40 卡:前 4 个节点加入 Ray 训练,第 5 个节点部署 verifier/vLLM API。 +verifier_api_node_rank=${VERIFIER_API_NODE_RANK:-4} +verifier_api_port=${VERIFIER_API_PORT:-8000} +verifier_api_host=${VERIFIER_API_HOST:-${MA_VJ_NAME}-${MA_TASK_NAME}-${verifier_api_node_rank}.${MA_VJ_NAME}} +verifier_api_start_cmd=${VERIFIER_API_START_CMD:-} +verifier_api_bind_host=${VERIFIER_API_BIND_HOST:-0.0.0.0} +# 9B verifier 默认使用整台 8 卡节点:两份 TP4 副本由 vLLM 内置 DP 统一服务。 +# 如需单副本 TP8,可设置 VERIFIER_API_TP=8 VERIFIER_API_DP=1。 +verifier_api_tp=${VERIFIER_API_TP:-4} +verifier_api_dp=${VERIFIER_API_DP:-2} +verifier_api_devices=${VERIFIER_API_DEVICES:-0,1,2,3,4,5,6,7} +verifier_api_distributed_executor_backend=${VERIFIER_API_DISTRIBUTED_EXECUTOR_BACKEND:-mp} +verifier_api_max_model_len=${VERIFIER_API_MAX_MODEL_LEN:-32768} +verifier_api_max_num_batched_tokens=${VERIFIER_API_MAX_NUM_BATCHED_TOKENS:-32768} +verifier_api_max_num_seqs=${VERIFIER_API_MAX_NUM_SEQS:-160} +verifier_api_gpu_memory_utilization=${VERIFIER_API_GPU_MEMORY_UTILIZATION:-0.70} +verifier_api_enforce_eager=${VERIFIER_API_ENFORCE_EAGER:-0} +verifier_api_enable_graph_mode=${VERIFIER_API_ENABLE_GRAPH_MODE:-1} +verifier_api_enable_prefix_caching=${VERIFIER_API_ENABLE_PREFIX_CACHING:-0} +verifier_api_startup_timeout=${VERIFIER_API_STARTUP_TIMEOUT:-1800} +verifier_api_log=${VERIFIER_API_LOG:-logs/vllm-verifier-api.log} +mock_api_base=${MOCK_API_BASE:-http://${verifier_api_host}:${verifier_api_port}/v1} +mock_api_key=${MOCK_API_KEY:-dummy_key} +mock_model_name=${MOCK_MODEL_NAME:-qwen3_5_9b_verifier} +# verify_workplace.py 内部 OpenAI/httpx 单次请求超时;reward API 排队时宁可多等,不要轻易误判 0 分。 +mock_api_timeout=${MOCK_API_TIMEOUT:-1800} +mock_api_connect_timeout=${MOCK_API_CONNECT_TIMEOUT:-300} +# 强制 verifier/OpenAI judge 请求关闭 thinking,sitecustomize 会自动注入 extra_body.chat_template_kwargs.enable_thinking=False。 +nanoclaw_force_no_thinking=${NANOCLAW_FORCE_NO_THINKING:-1} +nanoclaw_force_max_tokens=${NANOCLAW_FORCE_MAX_TOKENS:-50} +# 默认控制台只打一行 reward 摘要;如需每项 details,设 NANOCLAW_REWARD_PRINT_DETAILS=1。 +nanoclaw_reward_print_details=${NANOCLAW_REWARD_PRINT_DETAILS:-0} +# verifier API 是单独节点,默认低并发,避免 RewardLoopWorker 同时打爆 API 导致排队超时。 +reward_num_workers=${REWARD_NUM_WORKERS:-52} + +project_name=${PROJECT_NAME:-qwen3.5-9b_nanoclaw_grpo_verl_0720} +experiment_name=${EXPERIMENT_NAME:-qwen3.5-9b_nanoclaw_grpo_sp1_fsdp2_31k_longseq_allopt_lr1e6_fixedkl1e-3} +default_local_dir=${DEFAULT_LOCAL_DIR:-$DATA_ROOT/checkpoint/$experiment_name} +start_time=$(date +%Y%m%d)_$(date +%H%M%S) +mkdir -p logs "${default_local_dir}" + +# ================= 算法与并行参数 ================= +adv_estimator=grpo +max_turns=${MAX_TURNS:-35} +max_prompt_length=${MAX_PROMPT_LENGTH:-8192} +max_response_length=${MAX_RESPONSE_LENGTH:-22768} +max_assistant_response_length=${MAX_ASSISTANT_RESPONSE_LENGTH:-16384} +max_tool_response_length=${MAX_TOOL_RESPONSE_LENGTH:-8192} +max_model_len=$((max_prompt_length + max_response_length)) + +# MindSpeed 配置仅作为训练意图参考;以下均使用最新版 VERL 的原生字段。 +actor_lr=${ACTOR_LR:-1e-6} +actor_lr_scheduler_type=${ACTOR_LR_SCHEDULER_TYPE:-constant} +actor_lr_warmup_steps_ratio=${ACTOR_LR_WARMUP_STEPS_RATIO:-0.0} +actor_weight_decay=${ACTOR_WEIGHT_DECAY:-0.01} +actor_adam_beta1=${ACTOR_ADAM_BETA1:-0.9} +actor_adam_beta2=${ACTOR_ADAM_BETA2:-0.95} +actor_clip_grad=${ACTOR_CLIP_GRAD:-1.0} +actor_ppo_epochs=${ACTOR_PPO_EPOCHS:-1} +actor_shuffle=${ACTOR_SHUFFLE:-False} +actor_entropy_coeff=${ACTOR_ENTROPY_COEFF:-0.0} +actor_clip_ratio_low=${ACTOR_CLIP_RATIO_LOW:-0.2} +actor_clip_ratio_high=${ACTOR_CLIP_RATIO_HIGH:-0.2} +# MindSpeed 配置没有 Dual-Clip PPO 对应项,保留该 27B 脚本原来的 C=10。 +actor_clip_ratio_c=${ACTOR_CLIP_RATIO_C:-10.0} + +# YAML 的 fixed init_kl_coef + low_var_kl 对应 VERL 的 reward-KL 路径。 +algorithm_gamma=${ALGORITHM_GAMMA:-1.0} +algorithm_lam=${ALGORITHM_LAM:-0.95} +use_kl_in_reward=${USE_KL_IN_REWARD:-True} +kl_penalty=${KL_PENALTY:-low_var_kl} +kl_ctrl_type=${KL_CTRL_TYPE:-fixed} +kl_coef=${KL_COEF:-0.001} +# 关闭 actor-KL,避免与 reward-KL 重复惩罚。 +actor_use_kl_loss=${ACTOR_USE_KL_LOSS:-False} +actor_kl_loss_coef=${ACTOR_KL_LOSS_COEF:-0.001} +actor_kl_loss_type=${ACTOR_KL_LOSS_TYPE:-low_var_kl} + +train_batch_size=${TRAIN_BATCH_SIZE:-64} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-16} +n_resp_per_prompt=${N_RESP_PER_PROMPT:-8} +# 先压低验证,避免验证和训练稳定性混在一起。 +n_resp_per_prompt_val=${N_RESP_PER_PROMPT_VAL:-1} +log_val_generations=${LOG_VAL_GENERATIONS:-10} + +infer_tp=${INFER_TP:-4} +train_sp=${TRAIN_SP:-1} +offload=${OFFLOAD:-True} + +# 长序列全优化版使用最新版 VERL 官方 Qwen3.5 路径采用的 FSDP2。 +actor_strategy=${ACTOR_STRATEGY:-fsdp2} +fsdp_size=${FSDP_SIZE:-} + +actor_pack=${ACTOR_PACK:-1} +logprob_pack=${LOGPROB_PACK:-2} +actor_max_token_len_per_gpu=${ACTOR_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * actor_pack + train_sp - 1) / train_sp))} +log_prob_max_token_len_per_gpu=${LOG_PROB_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * logprob_pack + train_sp - 1) / train_sp))} +entropy_from_logits_with_chunking=${ENTROPY_FROM_LOGITS_WITH_CHUNKING:-True} +entropy_from_logits_chunk_size=${ENTROPY_FROM_LOGITS_CHUNK_SIZE:-256} +entropy_checkpointing=${ENTROPY_CHECKPOINTING:-False} +use_fused_kernels=${USE_FUSED_KERNELS:-True} +fused_kernel_backend=${FUSED_KERNEL_BACKEND:-torch} +enable_activation_offload=${ENABLE_ACTIVATION_OFFLOAD:-True} +rollout_max_num_batched_tokens=${ROLLOUT_MAX_NUM_BATCHED_TOKENS:-32384} +rollout_gpu_memory_utilization=${ROLLOUT_GPU_MEMORY_UTILIZATION:-0.60} +update_weights_bucket_mb=${UPDATE_WEIGHTS_BUCKET_MB:-2048} + +# Qwen 官方推荐:Instruct/non-thinking reasoning tasks +rollout_temperature=${ROLLOUT_TEMPERATURE:-0.6} +rollout_top_p=${ROLLOUT_TOP_P:-0.95} +rollout_top_k=${ROLLOUT_TOP_K:-20} +rollout_min_p=${ROLLOUT_MIN_P:-0.0} +rollout_presence_penalty=${ROLLOUT_PRESENCE_PENALTY:-0.0} +rollout_frequency_penalty=${ROLLOUT_FREQUENCY_PENALTY:-0.0} +rollout_repetition_penalty=${ROLLOUT_REPETITION_PENALTY:-1.0} + +echo "DEBUG: max_response_length=${max_response_length}, max_assistant_response_length=${max_assistant_response_length}, max_model_len=${max_model_len}" +echo "DEBUG: max_turns=${max_turns}" +echo "DEBUG: max_tool_response_length=${max_tool_response_length}" +echo "DEBUG: entropy_chunking=${entropy_from_logits_with_chunking}, entropy_chunk_size=${entropy_from_logits_chunk_size}, entropy_checkpointing=${entropy_checkpointing}" +echo "DEBUG: fused_lmhead=${use_fused_kernels}, fused_backend=${fused_kernel_backend}, activation_offload=${enable_activation_offload}" +echo "DEBUG: train_batch_size=${train_batch_size}, ppo_mini_batch_size=${ppo_mini_batch_size}, n=${n_resp_per_prompt}" +echo "DEBUG: train_sp=${train_sp}, infer_tp=${infer_tp}, actor_strategy=${actor_strategy}, fsdp_size=${fsdp_size:-}" +echo "DEBUG: Qwen3.5 Ulysses FLA required=${NANOCLAW_REQUIRE_FLA}, backend=${QWEN35_FLA_BACKEND} (TRAIN_SP=${train_sp})" +echo "DEBUG: actor_max_token_len_per_gpu=${actor_max_token_len_per_gpu}, log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu}" +echo "DEBUG: rollout sampling temperature=${rollout_temperature}, top_p=${rollout_top_p}, top_k=${rollout_top_k}, min_p=${rollout_min_p}, presence_penalty=${rollout_presence_penalty}, frequency_penalty=${rollout_frequency_penalty}, repetition_penalty=${rollout_repetition_penalty}" +echo "DEBUG: optimizer lr=${actor_lr}, scheduler=${actor_lr_scheduler_type}, warmup_ratio=${actor_lr_warmup_steps_ratio}, weight_decay=${actor_weight_decay}, betas=(${actor_adam_beta1},${actor_adam_beta2}), clip_grad=${actor_clip_grad}, ppo_epochs=${actor_ppo_epochs}, shuffle=${actor_shuffle}" +echo "DEBUG: KL use_in_reward=${use_kl_in_reward}, penalty=${kl_penalty}, ctrl=${kl_ctrl_type}, coef=${kl_coef}, actor_kl=${actor_use_kl_loss}" +echo "DEBUG: HCCL_BUFFSIZE=${HCCL_BUFFSIZE}, HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE}, HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE}" + +val_before_train=${VAL_BEFORE_TRAIN:-False} +trainer_use_v1=${TRAINER_USE_V1:-True} +test_freq=${TEST_FREQ:-5000} +save_freq=${SAVE_FREQ:-2} + +# ================= 分布式 ================= +export TOTAL_NNODES=${TOTAL_NNODES:-5} +export TRAIN_NNODES=${TRAIN_NNODES:-4} +export NNODES=${NNODES:-${TRAIN_NNODES}} +export NODE_RANK=${VC_TASK_INDEX} +export NPUS_PER_NODE=${NPUS_PER_NODE:-8} +export WORLD_SIZE=$((NPUS_PER_NODE * NNODES)) + +export MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-0.${MA_VJ_NAME} +export MASTER_PORT=${MASTER_PORT:-6167} +export DASHBOARD_PORT=${DASHBOARD_PORT:-8191} +export RAY_PORT=${RAY_PORT:-6167} + +readonly SOCKET_IFNAME=${SOCKET_IFNAME:-eth0} +export HCCL_SOCKET_IFNAME=${HCCL_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export GLOO_SOCKET_IFNAME=${GLOO_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export CURRENT_IP=$(ifconfig ${SOCKET_IFNAME} | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +export RAY_NODE_IP=${MA_CURRENT_IP:-${CURRENT_IP}} + +export ASCEND_RT_VISIBLE_DEVICES=${ASCEND_RT_VISIBLE_DEVICES:-$(seq -s, 0 $((NPUS_PER_NODE - 1)))} + +cat < [Verifier API Node] This node is reserved for vLLM/OpenAI-compatible verifier API." + echo "--> [Verifier API Node] API base: ${mock_api_base}" + export VLLM_ENABLE_GRAPH_MODE=${verifier_api_enable_graph_mode} + mkdir -p "$(dirname "${verifier_api_log}")" + if [ -n "${verifier_api_start_cmd}" ]; then + echo "--> [Verifier API Node] Running VERIFIER_API_START_CMD..." + bash -lc "${verifier_api_start_cmd}" & + verifier_api_pid=$! + else + echo "--> [Verifier API Node] Starting default vLLM verifier API..." + verifier_api_device_count=$(awk -F',' '{print NF}' <<<"${verifier_api_devices}") + verifier_api_expected_device_count=$((verifier_api_tp * verifier_api_dp)) + if [ "${verifier_api_device_count}" -ne "${verifier_api_expected_device_count}" ]; then + echo "ERROR: verifier TP*DP=${verifier_api_tp}*${verifier_api_dp}=${verifier_api_expected_device_count}, but VERIFIER_API_DEVICES=${verifier_api_devices} contains ${verifier_api_device_count} devices." >&2 + exit 2 + fi + export ASCEND_RT_VISIBLE_DEVICES=${verifier_api_devices} + verifier_api_args=( + --model "${verifier_model_path}" + --tokenizer "${verifier_model_path}" + --host "${verifier_api_bind_host}" + --port "${verifier_api_port}" + --served-model-name "${mock_model_name}" + --tensor-parallel-size "${verifier_api_tp}" + --data-parallel-size "${verifier_api_dp}" + --distributed-executor-backend "${verifier_api_distributed_executor_backend}" + --dtype bfloat16 + --max-model-len "${verifier_api_max_model_len}" + --max-num-batched-tokens "${verifier_api_max_num_batched_tokens}" + --max-num-seqs "${verifier_api_max_num_seqs}" + --gpu-memory-utilization "${verifier_api_gpu_memory_utilization}" + --trust-remote-code + ) + if [ "${verifier_api_enforce_eager}" = "1" ] || [ "${verifier_api_enforce_eager}" = "true" ] || [ "${verifier_api_enforce_eager}" = "True" ]; then + verifier_api_args+=(--enforce-eager) + fi + if [ "${verifier_api_enable_prefix_caching}" = "1" ] || [ "${verifier_api_enable_prefix_caching}" = "true" ] || [ "${verifier_api_enable_prefix_caching}" = "True" ]; then + verifier_api_args+=(--enable-prefix-caching) + fi + echo "--> [Verifier API Node] Command: python3 -m vllm.entrypoints.openai.api_server ${verifier_api_args[*]}" + python3 -m vllm.entrypoints.openai.api_server "${verifier_api_args[@]}" >"${verifier_api_log}" 2>&1 & + verifier_api_pid=$! + fi + + echo "--> [Verifier API Node] vLLM API pid=${verifier_api_pid}, log=${verifier_api_log}" + echo "--> [Verifier API Node] Waiting for ${mock_api_base}/models ..." + python3 - "${mock_api_base}/models" "${verifier_api_startup_timeout}" "${verifier_api_log}" "${verifier_api_pid}" <<'PY' +import os +import sys +import time +import urllib.request +from pathlib import Path + +url = sys.argv[1] +timeout = float(sys.argv[2]) +log_path = Path(sys.argv[3]) +pid = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[4] else None +started = time.time() +last_error = None +while time.time() - started < timeout: + if pid is not None: + try: + os.kill(pid, 0) + except OSError: + print(f"ERROR: verifier API process exited early: pid={pid}", file=sys.stderr) + if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) + sys.exit(1) + try: + with urllib.request.urlopen(url, timeout=5) as response: + if 200 <= response.status < 300: + print(f"READY: {url}", file=sys.stderr) + sys.exit(0) + except Exception as exc: + last_error = exc + time.sleep(5) +print(f"ERROR: timed out waiting for {url}; last_error={last_error}", file=sys.stderr) +if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) +sys.exit(1) +PY + verifier_readiness_rc=$? + if [ "${verifier_readiness_rc}" -ne 0 ]; then + echo "ERROR: verifier API readiness check failed with rc=${verifier_readiness_rc}." >&2 + if kill -0 "${verifier_api_pid}" 2>/dev/null; then + kill "${verifier_api_pid}" 2>/dev/null || true + fi + wait "${verifier_api_pid}" 2>/dev/null || true + exit "${verifier_readiness_rc}" + fi + + echo "--> [Verifier API Node] Ready. Keeping node alive." + wait "${verifier_api_pid}" + verifier_api_rc=$? + if [ "${verifier_api_rc}" -ne 0 ]; then + echo "ERROR: verifier API exited with rc=${verifier_api_rc}; log=${verifier_api_log}" >&2 + fi + exit "${verifier_api_rc}" +fi + +export TMPDIR=/cache/ray_tmp +export HCCL_ASYNC_ERROR_HANDLING=${HCCL_ASYNC_ERROR_HANDLING:-0} + +wait_for_ray_npu_resources() { + expected_npu=$1 + timeout_seconds=${2:-900} + begin_ts=$(date +%s) + + while true; do + total_npu=$(python3 - <<'PY' 2>/dev/null +import ray + +try: + ray.init(address="auto", ignore_reinit_error=True, logging_level="ERROR") + print(int(ray.cluster_resources().get("NPU", 0))) + ray.shutdown() +except Exception: + print(0) +PY +) + total_npu=${total_npu:-0} + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + + echo "Ray NPU resources: ${total_npu}/${expected_npu}, elapsed=${elapsed}s" + ray status || true + + if [ "${total_npu}" -ge "${expected_npu}" ]; then + echo "Ray cluster is ready: ${total_npu}/${expected_npu} NPU resources registered." + break + fi + + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for Ray NPU resources: ${total_npu}/${expected_npu}" >&2 + return 1 + fi + + sleep 5 + done +} + +wait_for_verifier_api() { + api_url="${mock_api_base}/models" + timeout_seconds=${VERIFIER_API_CLIENT_WAIT_TIMEOUT:-1800} + begin_ts=$(date +%s) + last_diag_ts=0 + while true; do + verifier_check_output=$(python3 - "${api_url}" <<'PY' 2>&1 +import socket +import sys +import urllib.parse +import urllib.request + +url = sys.argv[1] +parsed = urllib.parse.urlparse(url) +host = parsed.hostname +port = parsed.port or (443 if parsed.scheme == "https" else 80) +print(f"check url={url} host={host} port={port}") +try: + infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + print("dns=" + ",".join(sorted({item[4][0] for item in infos}))) +except Exception as exc: + print(f"dns_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with socket.create_connection((host, port), timeout=5): + print("tcp=ok") +except Exception as exc: + print(f"tcp_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with urllib.request.urlopen(url, timeout=10) as response: + print(f"http_status={response.status}") + raise SystemExit(0 if 200 <= response.status < 300 else 1) +except Exception as exc: + print(f"http_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +PY +) + check_rc=$? + if [ "${check_rc}" = "0" ]; then + echo "Verifier API is ready: ${api_url}" + echo "${verifier_check_output}" + break + fi + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + echo "Waiting for verifier API: ${api_url}, elapsed=${elapsed}s" + if [ $((now_ts - last_diag_ts)) -ge 60 ]; then + last_diag_ts=${now_ts} + echo "--- verifier API check diagnostics ---" + echo "${verifier_check_output}" + echo "--- expected verifier node: rank=${verifier_api_node_rank}, host=${verifier_api_host}, port=${verifier_api_port} ---" + echo "--- check verifier node log: ${verifier_api_log} ---" + echo "--------------------------------------" + fi + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for verifier API: ${api_url}" >&2 + echo "Last verifier API diagnostics:" >&2 + echo "${verifier_check_output}" >&2 + return 1 + fi + sleep 10 + done +} + +# ================= Nanoclaw workspace 根目录 ================= +mkdir -p "${nanoclaw_temp_root}" +if ! touch "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" 2>/dev/null; then + echo "ERROR: Cannot write NANOCLAW_TEMP_ROOT: ${nanoclaw_temp_root}" >&2 + exit 2 +fi +rm -f "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" || true +if [[ "${nanoclaw_temp_root}" == /tmp/* ]]; then + echo "WARNING: NANOCLAW_TEMP_ROOT is under /tmp. Multi-node reward workers may not see rollout workspaces." >&2 + echo "WARNING: Prefer a shared path, e.g. ${DATA_ROOT}/nanoclaw_workspaces" >&2 +fi +echo "DEBUG: Nanoclaw train_base_tasks=${train_base_tasks}" +echo "DEBUG: Nanoclaw val_base_tasks=${val_base_tasks}" +echo "DEBUG: Nanoclaw task_glob=${nanoclaw_task_glob}, task_ids=${nanoclaw_task_ids:-}" +echo "DEBUG: Nanoclaw temp_root=${nanoclaw_temp_root}, cleanup=${nanoclaw_cleanup_workspaces}, keep_failed=${nanoclaw_keep_failed_workspaces}" + +# ================= 生成 Ray runtime env ================= +RUNTIME_ENV_FILE=${WORK_DIR}/verl_engine_runtime_env.generated.yaml +cat > "${RUNTIME_ENV_FILE}" < [Head Node] Starting Ray Head on ${CURRENT_IP}..." + ray start --head \ + --node-ip-address=${RAY_NODE_IP} \ + --port=${RAY_PORT} \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=${DASHBOARD_PORT} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + + sleep 10 + wait_for_ray_npu_resources ${WORLD_SIZE} 900 || exit 1 + wait_for_verifier_api || exit 1 +else + echo "--> [Worker Node] Starting Ray Worker, connecting to ${MASTER_ADDR}:${RAY_PORT}..." + sleep 20 + ray start --address=${MASTER_ADDR}:${RAY_PORT} \ + --node-ip-address=${RAY_NODE_IP} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + sleep 10 +fi + +# ================= 训练参数数组 ================= +training_args=( + python3 -m verl.trainer.main_ppo + +ray_kwargs.ray_init.address=auto + reward.num_workers=${reward_num_workers} + algorithm.adv_estimator=${adv_estimator} + algorithm.gamma=${algorithm_gamma} + algorithm.lam=${algorithm_lam} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_penalty=${kl_penalty} + algorithm.kl_ctrl.type=${kl_ctrl_type} + algorithm.kl_ctrl.kl_coef=${kl_coef} + data.train_files="${train_files}" + data.val_files="${test_files}" + data.return_raw_chat=True + data.return_multi_modal_inputs=False + data.image_key=images + data.shuffle=True + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation=error + data.custom_cls.path=pkg://nanoclaw_recipe.nanoclaw + data.custom_cls.name=CustomRLHFDataset + "data.tool_config_path=${tool_config_path}" + "+data.nanoclaw_task_glob=${nanoclaw_task_glob}" + "+data.nanoclaw_temp_root=${nanoclaw_temp_root}" + "+data.nanoclaw_cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+data.nanoclaw_keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+data.nanoclaw_env_builder_timeout=${nanoclaw_env_builder_timeout}" + "+data.nanoclaw_verifier_timeout=${nanoclaw_verifier_timeout}" + "+data.nanoclaw_reward_score_mode=${nanoclaw_reward_score_mode}" + "+data.nanoclaw_allow_bash=${nanoclaw_allow_bash}" + +data.apply_chat_template_kwargs.enable_thinking=True + reward.custom_reward_function.path=pkg://nanoclaw_recipe.nanoclaw + reward.custom_reward_function.name=compute_score + "+reward.custom_reward_function.reward_kwargs.cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+reward.custom_reward_function.reward_kwargs.keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+reward.custom_reward_function.reward_kwargs.verifier_timeout=${nanoclaw_verifier_timeout}" + "+reward.custom_reward_function.reward_kwargs.reward_score_mode=${nanoclaw_reward_score_mode}" + "+reward.custom_reward_function.reward_kwargs.require_final_answer=${nanoclaw_require_final_answer}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_enable=${nanoclaw_final_answer_bonus_enable}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_score=${nanoclaw_final_answer_bonus_score}" + "+reward.custom_reward_function.reward_kwargs.turn_penalty_only_positive_score=${nanoclaw_turn_penalty_only_positive_score}" + "+reward.custom_reward_function.reward_kwargs.assistant_turn_penalty=${nanoclaw_assistant_turn_penalty}" + "+reward.custom_reward_function.reward_kwargs.duplicate_tool_call_penalty=${nanoclaw_duplicate_tool_call_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_penalty=${nanoclaw_repeated_response_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_chars=${nanoclaw_repeated_response_min_chars}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_consecutive_repeats=${nanoclaw_repeated_response_min_consecutive_repeats}" + "+reward.custom_reward_function.reward_kwargs.mock_api_base=${mock_api_base}" + "+reward.custom_reward_function.reward_kwargs.mock_api_key=${mock_api_key}" + "+reward.custom_reward_function.reward_kwargs.mock_model_name=${mock_model_name}" + "+reward.custom_reward_function.reward_kwargs.mock_api_timeout=${mock_api_timeout}" + "+reward.custom_reward_function.reward_kwargs.mock_api_connect_timeout=${mock_api_connect_timeout}" + actor_rollout_ref.model.path=${model_path} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True + actor_rollout_ref.model.enable_activation_offload=${enable_activation_offload} + actor_rollout_ref.model.use_fused_kernels=${use_fused_kernels} + actor_rollout_ref.model.fused_kernel_options.impl_backend=${fused_kernel_backend} + actor_rollout_ref.actor.strategy=${actor_strategy} + actor_rollout_ref.ref.strategy=${actor_strategy} + actor_rollout_ref.actor.use_kl_loss=${actor_use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${actor_kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=${actor_kl_loss_type} + actor_rollout_ref.actor.clip_ratio_low=${actor_clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${actor_clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=${actor_clip_ratio_c} + actor_rollout_ref.actor.entropy_coeff=${actor_entropy_coeff} + actor_rollout_ref.actor.ppo_epochs=${actor_ppo_epochs} + actor_rollout_ref.actor.shuffle=${actor_shuffle} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.optim.lr_scheduler_type=${actor_lr_scheduler_type} + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=${actor_lr_warmup_steps_ratio} + actor_rollout_ref.actor.optim.weight_decay=${actor_weight_decay} + "actor_rollout_ref.actor.optim.betas=[${actor_adam_beta1},${actor_adam_beta2}]" + actor_rollout_ref.actor.optim.clip_grad=${actor_clip_grad} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_max_token_len_per_gpu} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.actor.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.actor.fsdp_config.offload_policy=${offload} + actor_rollout_ref.actor.fsdp_config.reshard_after_forward=True + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.fsdp_config.offload_policy=${offload} + actor_rollout_ref.ref.fsdp_config.reshard_after_forward=True + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.ref.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.mode=async + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.temperature=${rollout_temperature} + actor_rollout_ref.rollout.top_p=${rollout_top_p} + actor_rollout_ref.rollout.top_k=${rollout_top_k} + actor_rollout_ref.rollout.min_p=${rollout_min_p} + actor_rollout_ref.rollout.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.tensor_model_parallel_size=${infer_tp} + actor_rollout_ref.rollout.max_model_len=${max_model_len} + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=${update_weights_bucket_mb} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.max_num_batched_tokens=${rollout_max_num_batched_tokens} + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.enable_prefix_caching=False + actor_rollout_ref.rollout.multi_turn.enable=True + actor_rollout_ref.rollout.multi_turn.max_user_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_response_length=${max_assistant_response_length} + "actor_rollout_ref.rollout.multi_turn.tool_config_path=${tool_config_path}" + actor_rollout_ref.rollout.multi_turn.format=qwen3_coder + "actor_rollout_ref.rollout.multi_turn.max_tool_response_length=${max_tool_response_length}" + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_memory_utilization} + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.val_kwargs.temperature=${rollout_temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${rollout_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${rollout_top_k} + actor_rollout_ref.rollout.val_kwargs.min_p=${rollout_min_p} + actor_rollout_ref.rollout.val_kwargs.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.val_kwargs.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.val_kwargs.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=${n_resp_per_prompt_val} + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.fsdp_config.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.use_torch_compile=False + critic.fsdp.use_torch_compile=False + trainer.use_v1=${trainer_use_v1} + trainer.critic_warmup=0 + trainer.balance_batch=True + trainer.logger=['console','tensorboard'] + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.nnodes=${NNODES} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.val_before_train=${val_before_train} + trainer.log_val_generations=${log_val_generations} + trainer.save_freq=${save_freq} + trainer.default_local_dir=${default_local_dir} + trainer.test_freq=${test_freq} + trainer.total_epochs=10 +) + +if [ -n "${fsdp_size}" ]; then + training_args+=( + actor_rollout_ref.actor.fsdp_config.fsdp_size=${fsdp_size} + actor_rollout_ref.ref.fsdp_config.fsdp_size=${fsdp_size} + ) +fi + +if [ -n "${nanoclaw_task_ids}" ]; then + training_args+=("+data.nanoclaw_task_ids=${nanoclaw_task_ids}") +fi + +if [ -n "${nanoclaw_max_steps}" ]; then + training_args+=("+data.nanoclaw_max_steps=${nanoclaw_max_steps}") +fi + +# ================= 启动训练主进程:仅主节点执行 ================= +if [ "${NODE_RANK}" = "0" ]; then + echo "--> [Head Node] Starting VERL unified engine training..." + echo "DEBUG: runtime_env=${RUNTIME_ENV_FILE}" + echo "DEBUG: entrypoint=${training_args[*]}" + + ray job submit \ + --address="http://127.0.0.1:${DASHBOARD_PORT}" \ + --runtime-env="${RUNTIME_ENV_FILE}" \ + -- \ + "${training_args[@]}" 2>&1 | tee "logs/qwen3.5-nanoclaw-grpo-verl-engine-${start_time}.log" +else + echo "--> [Worker Node] Setup finished. Keeping node alive for Ray..." + tail -f /dev/null +fi diff --git "a/verl_0720_main/9b.sh\357\200\272Zone.Identifier" "b/verl_0720_main/9b.sh\357\200\272Zone.Identifier" new file mode 100644 index 0000000000000000000000000000000000000000..e335a6f3af57f48dbeb936a1ab62ec44661d3ef7 --- /dev/null +++ "b/verl_0720_main/9b.sh\357\200\272Zone.Identifier" @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://huggingface.co/datasets/geminiDeveloper/verl_0720_nanoclaw/tree/main +HostUrl=https://huggingface.co/api/resolve-cache/datasets/geminiDeveloper/verl_0720_nanoclaw/bacc33fa11aa841f3fa8343597382539716bbd0c/9b.sh?download=true&etag=%222e9618c5440b127aac84d097bad37e196c42df41%22 diff --git a/verl_0720_main/9b_sp1_16k_fused_lmhead_smoke.sh b/verl_0720_main/9b_sp1_16k_fused_lmhead_smoke.sh new file mode 100644 index 0000000000000000000000000000000000000000..107523bb6149be0d2c1fcc9f4533f31bd49cf901 --- /dev/null +++ b/verl_0720_main/9b_sp1_16k_fused_lmhead_smoke.sh @@ -0,0 +1,1111 @@ +#!/bin/bash +# Qwen3.5-9B Nanoclaw 多轮 GRPO — NPU SP1/16K fused LM-head 两步真实训练实验 +# +# 关键修改: +# 1) 入口改为 python3 -m verl.trainer.main_ppo,不再使用 recipe.grpo_mindspeed_mm.main_ppo; +# 2) 不再使用 MM_CONFIG_FILE / MindSpeed-MM YAML; +# 3) 显式 text-only:data.return_multi_modal_inputs=False; +# 4) 数据输入改为 Nanoclaw base_tasks 目录,不再使用 Retool parquet/json; +# 5) 保留 27B 作业验证过的 HCCL buffer 与端口范围,降低 HcclAllreduce socket/资源压力; +# 6) smoke 默认关闭训练前/训练后验证和 checkpoint,只验证两次完整训练 step; +# 7) 稳定版固定使用已经完成初始化与 rollout 验证的 FSDP1/offload 形状。 +# 8) 使用最新版 VERL V1/TransferQueue 和 nanoclaw_recipe,并按参考 YAML 的训练意图映射优化器与 KL; +# 9) actor/ref 的全词表 entropy 使用 256-token 分块,避免 16K SP1 old-log-prob 阶段产生十几 GiB 临时张量。 +# 10) 启用 Qwen3.5 fused Torch LM-head,按 token 分块直接产生 log-prob/entropy,不保留完整 T×vocab logits; +# 11) 默认仅跑 2 个完整训练 step,覆盖真实 rollout/reward/old-ref-log-prob/actor backward/optimizer/权重同步。 + +set -x + +# ================= 独立稳定版:强制 Qwen3.5 NPU SP1 + 16K ================= +# 不允许外部作业环境把这些核心安全参数覆盖回 SP8/31K。 +export TRAIN_SP=1 +export QWEN35_FLA_BACKEND=disabled +export MAX_PROMPT_LENGTH=8192 +export MAX_RESPONSE_LENGTH=8192 +export MAX_ASSISTANT_RESPONSE_LENGTH=8192 +export MAX_TOOL_RESPONSE_LENGTH=8192 +export ACTOR_MAX_TOKEN_LEN_PER_GPU=16384 +export LOG_PROB_MAX_TOKEN_LEN_PER_GPU=16384 +export ROLLOUT_MAX_NUM_BATCHED_TOKENS=16384 +export ACTOR_STRATEGY=fsdp +export OFFLOAD=True +# 当前 VERL 的 checkpointing 分支不会传递自定义 chunk size;稳定版明确关闭它,确保实际使用 256。 +export ENTROPY_FROM_LOGITS_WITH_CHUNKING=True +export ENTROPY_FROM_LOGITS_CHUNK_SIZE=256 +export ENTROPY_CHECKPOINTING=False +# 本实验只改变 LM-head 路径;保留已经走到 old-log-prob 的 FSDP1/SP1/offload 拓扑。 +export USE_FUSED_KERNELS=True +export FUSED_KERNEL_BACKEND=torch +export ENABLE_ACTIVATION_OFFLOAD=False +# 缩小全局实验规模,但每条样本仍允许真实 16K,显存压力不会被短上下文掩盖。 +export TRAIN_BATCH_SIZE=8 +export PPO_MINI_BATCH_SIZE=8 +export N_RESP_PER_PROMPT=8 +export TOTAL_TRAINING_STEPS=2 +export VAL_BEFORE_TRAIN=False +export TEST_FREQ=-1 +export SAVE_FREQ=-1 +export LOG_VAL_GENERATIONS=0 + + +npu-smi info || true +pip install --upgrade pip +pip uninstall -y moxing-framework || true + +# ================= 路径配置 ================= +SCRIPT_DIR=/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/verl_0720_nanoclaw/verl_0720_main +if [ -f "${SCRIPT_DIR}/verl/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +elif [ -f "${SCRIPT_DIR}/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR} +else + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +fi +WORK_DIR=${WORK_DIR:-${DEFAULT_WORK_DIR}} +INSTALL_DIR=${INSTALL_DIR:-/home/ma-user} +BKGS=${BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs} +chmod 755 "${INSTALL_DIR}" + +# Nanoclaw 自定义包已随 WORK_DIR 提供:nanoclaw_recipe。 + +GCC_INSTALL_PREFIX=${GCC_INSTALL_PREFIX:-/home/ma-user/gcc-11.3.0} +COMPILED_GCC_ARCHIVE_PATH=${COMPILED_GCC_ARCHIVE_PATH:-/opt/huawei/dataset/zyr_yuyin/bkgs/gcc-11.3.0-compiled-aarch64.tar.gz} + +echo "--> 正在从缓存恢复 GCC 11.3.0..." +tar -xzf "${COMPILED_GCC_ARCHIVE_PATH}" -C /home/ma-user/ +export PATH=${GCC_INSTALL_PREFIX}/bin:${PATH} +export LD_LIBRARY_PATH=${GCC_INSTALL_PREFIX}/lib64:${GCC_INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH:-} +export CC=${GCC_INSTALL_PREFIX}/bin/gcc +export CXX=${GCC_INSTALL_PREFIX}/bin/g++ +echo "--> 验证 GCC 版本:" +gcc --version + +cd "${BKGS}" +cp jemalloc-5.3.0.tar.bz2 "${INSTALL_DIR}" + +VLLM_LATEST_PKGS=${VLLM_LATEST_PKGS:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-05-12/verl_new_26_05_09/pkgs} +rm -rf "${INSTALL_DIR}/vllm" "${INSTALL_DIR}/vllm-ascend" +cp -r "${VLLM_LATEST_PKGS}/vllm" "${INSTALL_DIR}" +cp -r "${VLLM_LATEST_PKGS}/vllm-ascend" "${INSTALL_DIR}" + +CANN_BKGS=${CANN_BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs/cann_0527} +cp "${CANN_BKGS}/Ascend-cann-toolkit_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-910b-ops_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-nnal_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" + +echo "################" +echo "## set verl env" +echo "################" + +cd "${INSTALL_DIR}" + +chmod +x Ascend-cann-toolkit_9.0.0_linux-aarch64.run +bash Ascend-cann-toolkit_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/ascend-toolkit/set_env.sh" + +chmod +x Ascend-cann-910b-ops_9.0.0_linux-aarch64.run +bash Ascend-cann-910b-ops_9.0.0_linux-aarch64.run --install --quiet + +chmod +x Ascend-cann-nnal_9.0.0_linux-aarch64.run +bash Ascend-cann-nnal_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/nnal/atb/set_env.sh" + +export ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME} +export LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:${LD_LIBRARY_PATH:-} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip3 install torch==2.9.0 +pip3 install pyyaml setuptools +pip3 install torch-npu==2.9.0 +pip3 install torchvision==0.24.0 torchaudio==2.9.0 + +ASCEND_TOOLKIT_PYTHON_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/python/site-packages +export PYTHONPATH=${PYTHONPATH:-}:${INSTALL_DIR}:${ASCEND_TOOLKIT_PYTHON_PATH} +pip install pybind11==2.13.6 + +cd "${INSTALL_DIR}/vllm" +VLLM_TARGET_DEVICE=empty pip install . + +cd "${INSTALL_DIR}/vllm-ascend" +pip install -e . +export VLLM_LOGGING_LEVEL=INFO + +cd "${INSTALL_DIR}" +tar -xvf jemalloc-5.3.0.tar.bz2 +cd jemalloc-5.3.0 +./configure --prefix="${INSTALL_DIR}" +make -j"$(nproc)" +make install +export LD_PRELOAD=${INSTALL_DIR}/lib/libjemalloc.so.2:${LD_PRELOAD:-} + +# # ================= 可选:安装 MindSpeed 栈 ================= +# # 纯 VERL engine 路线不依赖 MindSpeed-MM YAML。默认不安装,避免和新版 VERL engine 混淆。 +# INSTALL_MINDSPEED_STACK=${INSTALL_MINDSPEED_STACK:-0} +# if [ "${INSTALL_MINDSPEED_STACK}" = "1" ]; then +# MindSpeed_PATH=${MindSpeed_PATH:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-slow-stable} +# cd "${MindSpeed_PATH}" +# cd Megatron-LM && pip install -e . --no-deps && cd .. +# cd MindSpeed && pip install -e . --no-deps && cd .. +# cd MindSpeed-MM && mkdir -p logs data ckpt && pip install -e . --no-deps && cd .. +# pip install beartype bs4 diffusers==0.30.3 ftfy imageio-ffmpeg pandarallel pytest-mock +# else +# echo "--> Skip MindSpeed/MindSpeed-MM installation for pure VERL engine route." +# fi + + + +# ================= 安装 Triton-Ascend 3.2.1 ================= +# 1. 卸载 triton(增加 -y 自动确认) +pip uninstall -y triton + +# 2. 卸载 triton-ascend(增加 -y 自动确认) +pip uninstall -y triton-ascend +pip install --no-cache-dir --force-reinstall triton==3.5.0 +pip install --no-deps /opt/huawei/dataset/zyr_yuyin/bkgs/triton_ascend-3.2.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl + + +# ================= 安装新版 VERL ================= +cd "${WORK_DIR}" +pip install -r requirements-npu.txt +# NPU requirements 明确要求 numpy<2;editable 安装不能再次按 setup.py 把 NumPy升级到 2.x。 +python3 -m pip install -e . --no-deps +pip install --upgrade 'urllib3==1.26.11' +pip install loguru +pip install tree_sitter==0.21.3 +pip install tree-sitter-java==0.21.0 +pip install tree-sitter-javascript==0.21.4 + +ACL_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/aarch64-linux/lib64 +export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ACL_PATH} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip uninstall -y transformers || true +pip install transformers==5.3.0 +pip install accelerate==1.13.0 mathruler +pip install jsonargparse +pip install deepdiff sympy html2text requests bs4 mpmath swanlab PandoraBox json_repair openai httpx + +# 稳定版只允许 SP=1:不安装普通 FLA,也不进入尚未完成 NPU 适配的 Ulysses CP。 +if [ "${TRAIN_SP}" != "1" ]; then + echo "ERROR: the standalone stable profile requires TRAIN_SP=1; got ${TRAIN_SP}." >&2 + exit 2 +fi +export NANOCLAW_REQUIRE_FLA=0 +echo "--> Stable SP1: flash-linear-attention is disabled; Qwen3.5 will not build an Ulysses CP context." + +# Transformers 5.x 会经 sklearn 间接导入 pandas/scipy。固定同一套 NumPy ABI, +# 避免出现 "numpy.dtype size changed"。这些版本均支持 Python 3.11/aarch64。 +NUMPY_VERSION=${NUMPY_VERSION:-1.26.4} +PANDAS_VERSION=${PANDAS_VERSION:-2.2.3} +SCIPY_VERSION=${SCIPY_VERSION:-1.14.1} +SKLEARN_VERSION=${SKLEARN_VERSION:-1.6.1} +python3 -m pip install --no-cache-dir --force-reinstall \ + "numpy==${NUMPY_VERSION}" \ + "pandas==${PANDAS_VERSION}" \ + "scipy==${SCIPY_VERSION}" \ + "scikit-learn==${SKLEARN_VERSION}" + +python3 - <<'PY' || exit 2 +import numpy +import pandas +import scipy +import sklearn +import sys +import transformers +import vllm + +fla_version = "disabled-sp1" + +print( + "[python_stack_preflight] " + f"python={sys.executable} " + f"numpy={numpy.__version__} " + f"pandas={pandas.__version__} " + f"scipy={scipy.__version__} " + f"sklearn={sklearn.__version__} " + f"transformers={transformers.__version__} " + f"vllm={vllm.__version__} " + f"fla={fla_version}" +) +print( + "[python_stack_paths] " + f"numpy={numpy.__file__} " + f"pandas={pandas.__file__}" +) +PY +pip list + +# ================= 检查 Nanoclaw recipe ================= +if [ ! -f "${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" ]; then + echo "ERROR: Nanoclaw recipe not found: ${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" >&2 + exit 2 +fi +test -f "${WORK_DIR}/nanoclaw_recipe/__init__.py" + +# Qwen3.5 MRoPE position_ids 是 3/4 轴张量。未应用此补丁时,NPU +# FlashAttention 会把 seqLen 重复累计(例如 T=10131、sum(seqLen)=30393)。 +QWEN35_MONKEY_PATCH_FILE=${WORK_DIR}/verl/models/transformers/monkey_patch.py +if [ ! -f "${QWEN35_MONKEY_PATCH_FILE}" ] || ! grep -q "def _normalize_fa_position_ids" "${QWEN35_MONKEY_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FlashAttention position_ids normalization patch is missing: ${QWEN35_MONKEY_PATCH_FILE}" >&2 + echo "Upload the modified verl/ directory together with this standalone script." >&2 + exit 2 +fi + +# Nanoclaw 会在首次模型调用前写入 rollout metadata。旧 ToolAgentLoop 因此 +# 未复制 min/max_global_steps,导致完整训练 step 后在 metrics 阶段把 None 转 int 崩溃。 +TOOL_AGENT_LOOP_FILE=${WORK_DIR}/verl/experimental/agent_loop/tool_agent_loop.py +TRAINER_BASE_FILE=${WORK_DIR}/verl/trainer/ppo/v1/trainer_base.py +if [ ! -f "${TOOL_AGENT_LOOP_FILE}" ] || ! grep -q "output_min_global_steps" "${TOOL_AGENT_LOOP_FILE}"; then + echo "ERROR: Nanoclaw rollout version metadata merge fix is missing: ${TOOL_AGENT_LOOP_FILE}" >&2 + exit 2 +fi +if [ ! -f "${TRAINER_BASE_FILE}" ] || ! grep -q "def resolve_model_version" "${TRAINER_BASE_FILE}"; then + echo "ERROR: PPO metrics None-version fallback fix is missing: ${TRAINER_BASE_FILE}" >&2 + exit 2 +fi + +# fused Torch LM-head 必须同时具备 Qwen3.5 backend 和分块 autograd 实现。 +QWEN35_MODEL_PATCH_FILE=${WORK_DIR}/verl/models/transformers/qwen3_5.py +FUSED_LINEAR_FILE=${WORK_DIR}/verl/utils/experimental/torch_functional.py +if [ ! -f "${QWEN35_MODEL_PATCH_FILE}" ] || ! grep -q "def forward_with_torch_backend" "${QWEN35_MODEL_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 fused Torch backend is missing: ${QWEN35_MODEL_PATCH_FILE}" >&2 + exit 2 +fi +if [ ! -f "${FUSED_LINEAR_FILE}" ] || ! grep -q "class FusedLinearForPPO" "${FUSED_LINEAR_FILE}"; then + echo "ERROR: chunked FusedLinearForPPO is missing: ${FUSED_LINEAR_FILE}" >&2 + exit 2 +fi + +# ================= PLOG ================= +ma_vj_name=$(echo "${MA_VJ_NAME}" | sed 's:ma-job:modelarts-job:g') +task_name=worker-${VC_TASK_INDEX} +task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name} +mkdir -p "${task_plog_path}" +export ASCEND_PROCESS_LOG_PATH=${task_plog_path}/${VC_TASK_INDEX} +echo "plog path: ${ASCEND_PROCESS_LOG_PATH}" + +MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-${VC_TASK_INDEX}.${MA_VJ_NAME} +MASTER_PORT=${PORT} +MA_CURRENT_INSTANCE_NAME=${MA_CURRENT_INSTANCE_NAME} + +cd "${WORK_DIR}" + +mkdir -p /cache/ray_tmp + +echo "Cleaning up old Ray processes..." +ray stop --force || true +sleep 5 +rm -rf /cache/ray_tmp/* +pkill -9 -f raylet || true +pkill -9 -f plasma_store || true +pkill -9 -f gcs_server || true +echo "Waiting 20s for NPU/Ray resources to be released..." +npu-smi info || true +sleep 20 + +# ================= NPU / HCCL / Ray 环境 ================= +export NON_MEGATRON=true +export MULTI_STREAM_MEMORY_REUSE=2 +export OMP_NUM_THREADS=1 +export PYTORCH_NPU_ALLOC_CONF=${PYTORCH_NPU_ALLOC_CONF:-max_split_size_mb:512} +export VLLM_LOGGING_LEVEL=INFO +export RAY_DEDUP_LOGS=0 +export HCCL_EXEC_TIMEOUT=${HCCL_EXEC_TIMEOUT:-3600} +export HCCL_LOG_LEVEL=${HCCL_LOG_LEVEL:-WARN} +export HCCL_CONNECT_TIMEOUT=${HCCL_CONNECT_TIMEOUT:-3600} +export HCCL_EVENT_TIMEOUT=${HCCL_EVENT_TIMEOUT:-7200} +export ACL_DEVICE_SYNC_TIMEOUT=${ACL_DEVICE_SYNC_TIMEOUT:-7200} +export GLOO_SOCKET_TIMEOUT=${GLOO_SOCKET_TIMEOUT:-7200} + +# 关键:降低 HCCL buffer,增加 socket 端口范围,缓解 HcclAllreduce ra socket batch connect failed。 +export HCCL_BUFFSIZE=${HCCL_BUFFSIZE:-300} +export P2P_HCCL_BUFFSIZE=${P2P_HCCL_BUFFSIZE:-64} +export HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE:-60000-60050} +export HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE:-61000-61050} + +export CUDA_DEVICE_MAX_CONNECTIONS=1 +export VLLM_ASCEND_ENABLE_NZ=${VLLM_ASCEND_ENABLE_NZ:-0} +export HCCL_OP_EXPANSION_MODE=${HCCL_OP_EXPANSION_MODE:-AIV} +export VLLM_ENGINE_ITERATION_TIMEOUT_S=${VLLM_ENGINE_ITERATION_TIMEOUT_S:-3600} +export WANDB_MODE=${WANDB_MODE:-disabled} +export PYTHONUNBUFFERED=1 +export TASK_QUEUE_ENABLE=${TASK_QUEUE_ENABLE:-1} +export COMBINED_ENABLE=${COMBINED_ENABLE:-1} +export TOKENIZERS_PARALLELISM=false +export CLOSE_MATMUL_K_SHIFT=${CLOSE_MATMUL_K_SHIFT:-1} +export ATB_MATMUL_SHUFFLE_K_ENABLE=${ATB_MATMUL_SHUFFLE_K_ENABLE:-0} +export HCCL_DETERMINISTIC=${HCCL_DETERMINISTIC:-true} +export VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING:-0} +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export ASCEND_GLOBAL_LOG_LEVEL=${ASCEND_GLOBAL_LOG_LEVEL:-3} +export HYDRA_FULL_ERROR=1 +export RAY_gcs_server_rpc_server_thread_num=${RAY_gcs_server_rpc_server_thread_num:-32} +export RAY_gcs_server_request_timeout_seconds=${RAY_gcs_server_request_timeout_seconds:-600} +export RAY_timeout_ms=${RAY_timeout_ms:-600000} +export RAY_worker_register_timeout_seconds=${RAY_worker_register_timeout_seconds:-600} +export RAY_USAGE_STATS_ENABLED=0 +export VERL_REUSE_AGENT_LOOP=${VERL_REUSE_AGENT_LOOP:-1} + +ulimit -n 65536 + +# Ray 不要覆盖 ASCEND_RT_VISIBLE_DEVICES;VERL 内部按 local_rank 选卡。 +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + +# ================= 路径与数据配置 ================= +HDFS_ROOT=${HDFS_ROOT:-$PWD} +DATA_ROOT=${DATA_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_ckpt} + +# Nanoclaw 数据输入支持两种目录,优先推荐 0625 扁平格式: +# base_tasks/data_*/env_builder.py +# base_tasks/data_*/prompts.md +# base_tasks/data_*/workplace_verifier.py +# base_tasks/data_*/manifest.json +# 也兼容旧格式:base_tasks/tasks/data_* + base_tasks/scripts|scrips/data_*。 +DEFAULT_NANOCLAW_BASE_TASKS=${DEFAULT_NANOCLAW_BASE_TASKS:-/opt/huawei/dataset/zyr_yuyin/lyf/datasets/nanoclawRLdata/0710_add1000agent_qwen3_7_max_v1/exported_new_data} +train_base_tasks=${TRAIN_DATA_PATH:-${BASE_TASKS:-${DEFAULT_NANOCLAW_BASE_TASKS}}} +val_base_tasks=${VAL_DATA_PATH:-${VAL_BASE_TASKS:-${train_base_tasks}}} +train_files="['$train_base_tasks']" +test_files="['$val_base_tasks']" + +if [ ! -d "${train_base_tasks}" ]; then + echo "ERROR: Nanoclaw TRAIN_DATA_PATH/BASE_TASKS directory not found: ${train_base_tasks}" >&2 + exit 2 +fi +if [ ! -d "${val_base_tasks}" ]; then + echo "ERROR: Nanoclaw VAL_DATA_PATH/VAL_BASE_TASKS directory not found: ${val_base_tasks}" >&2 + exit 2 +fi + +model_path=${MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +verifier_model_path=${VERIFIER_MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +# 纯 VERL engine 路线:不要使用 MindSpeed-MM YAML。 +unset MM_CONFIG_FILE || true + +# Nanoclaw 工具配置 +tool_config_path=${TOOL_CONFIG_PATH:-nanoclaw_recipe/nanoclaw_tool_config.yaml} +nanoclaw_task_glob=${NANOCLAW_TASK_GLOB:-data_*} +nanoclaw_task_ids=${NANOCLAW_TASK_IDS:-} +# 多机训练必须用所有节点都能访问的共享目录;不要用 /tmp,否则 reward worker 可能跨节点找不到 workspace。 +nanoclaw_temp_root=${NANOCLAW_TEMP_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_workplace_qwen35_9b_sp1_16k_fused_lmhead_smoke} +# 默认保留每个 step/data_sample 的目录,方便复盘每条 GRPO 采样;磁盘紧张时手动设 NANOCLAW_CLEANUP_WORKSPACES=True。 +nanoclaw_cleanup_workspaces=${NANOCLAW_CLEANUP_WORKSPACES:-False} +nanoclaw_keep_failed_workspaces=${NANOCLAW_KEEP_FAILED_WORKSPACES:-False} +nanoclaw_env_builder_timeout=${NANOCLAW_ENV_BUILDER_TIMEOUT:-120} +nanoclaw_verifier_timeout=${NANOCLAW_VERIFIER_TIMEOUT:-3600} +nanoclaw_reward_score_mode=${NANOCLAW_REWARD_SCORE_MODE:-ratio} +nanoclaw_allow_bash=${NANOCLAW_ALLOW_BASH:-True} +nanoclaw_max_steps=${NANOCLAW_MAX_STEPS:-} +nanoclaw_require_final_answer=${NANOCLAW_REQUIRE_FINAL_ANSWER:-True} +nanoclaw_final_answer_bonus_enable=${NANOCLAW_FINAL_ANSWER_BONUS_ENABLE:-False} +nanoclaw_final_answer_bonus_score=${NANOCLAW_FINAL_ANSWER_BONUS_SCORE:-0.0} +nanoclaw_turn_penalty_only_positive_score=${NANOCLAW_TURN_PENALTY_ONLY_POSITIVE_SCORE:-False} +nanoclaw_assistant_turn_penalty=${NANOCLAW_ASSISTANT_TURN_PENALTY:-0.0} +nanoclaw_duplicate_tool_call_penalty=${NANOCLAW_DUPLICATE_TOOL_CALL_PENALTY:-0.0} +nanoclaw_repeated_response_penalty=${NANOCLAW_REPEATED_RESPONSE_PENALTY:-0.0} +nanoclaw_repeated_response_min_chars=${NANOCLAW_REPEATED_RESPONSE_MIN_CHARS:-50} +nanoclaw_repeated_response_min_consecutive_repeats=${NANOCLAW_REPEATED_RESPONSE_MIN_CONSECUTIVE_REPEATS:-5} +nanoclaw_mask_looping_responses=${NANOCLAW_MASK_LOOPING_RESPONSES:-True} +nanoclaw_mask_only_positive_advantage=${NANOCLAW_MASK_ONLY_POSITIVE_ADVANTAGE:-True} +nanoclaw_mask_budget_exhausted_last_turn=${NANOCLAW_MASK_BUDGET_EXHAUSTED_LAST_TURN:-True} +nanoclaw_mask_duplicate_tool_result_turns=${NANOCLAW_MASK_DUPLICATE_TOOL_RESULT_TURNS:-True} +nanoclaw_mask_error_tool_result_turns=${NANOCLAW_MASK_ERROR_TOOL_RESULT_TURNS:-True} + +# verify_workplace.py 如需调用本地 OpenAI-compatible API,可用这些变量传入 reward。 +# 默认假设 5 机 40 卡:前 4 个节点加入 Ray 训练,第 5 个节点部署 verifier/vLLM API。 +verifier_api_node_rank=${VERIFIER_API_NODE_RANK:-4} +verifier_api_port=${VERIFIER_API_PORT:-8000} +verifier_api_host=${VERIFIER_API_HOST:-${MA_VJ_NAME}-${MA_TASK_NAME}-${verifier_api_node_rank}.${MA_VJ_NAME}} +verifier_api_start_cmd=${VERIFIER_API_START_CMD:-} +verifier_api_bind_host=${VERIFIER_API_BIND_HOST:-0.0.0.0} +# 9B verifier 默认使用整台 8 卡节点:两份 TP4 副本由 vLLM 内置 DP 统一服务。 +# 如需单副本 TP8,可设置 VERIFIER_API_TP=8 VERIFIER_API_DP=1。 +verifier_api_tp=${VERIFIER_API_TP:-4} +verifier_api_dp=${VERIFIER_API_DP:-2} +verifier_api_devices=${VERIFIER_API_DEVICES:-0,1,2,3,4,5,6,7} +verifier_api_distributed_executor_backend=${VERIFIER_API_DISTRIBUTED_EXECUTOR_BACKEND:-mp} +verifier_api_max_model_len=${VERIFIER_API_MAX_MODEL_LEN:-32768} +verifier_api_max_num_batched_tokens=${VERIFIER_API_MAX_NUM_BATCHED_TOKENS:-32768} +verifier_api_max_num_seqs=${VERIFIER_API_MAX_NUM_SEQS:-160} +verifier_api_gpu_memory_utilization=${VERIFIER_API_GPU_MEMORY_UTILIZATION:-0.70} +verifier_api_enforce_eager=${VERIFIER_API_ENFORCE_EAGER:-0} +verifier_api_enable_graph_mode=${VERIFIER_API_ENABLE_GRAPH_MODE:-1} +verifier_api_enable_prefix_caching=${VERIFIER_API_ENABLE_PREFIX_CACHING:-0} +verifier_api_startup_timeout=${VERIFIER_API_STARTUP_TIMEOUT:-1800} +verifier_api_log=${VERIFIER_API_LOG:-logs/vllm-verifier-api.log} +mock_api_base=${MOCK_API_BASE:-http://${verifier_api_host}:${verifier_api_port}/v1} +mock_api_key=${MOCK_API_KEY:-dummy_key} +mock_model_name=${MOCK_MODEL_NAME:-qwen3_5_9b_verifier} +# verify_workplace.py 内部 OpenAI/httpx 单次请求超时;reward API 排队时宁可多等,不要轻易误判 0 分。 +mock_api_timeout=${MOCK_API_TIMEOUT:-1800} +mock_api_connect_timeout=${MOCK_API_CONNECT_TIMEOUT:-300} +# 强制 verifier/OpenAI judge 请求关闭 thinking,sitecustomize 会自动注入 extra_body.chat_template_kwargs.enable_thinking=False。 +nanoclaw_force_no_thinking=${NANOCLAW_FORCE_NO_THINKING:-1} +nanoclaw_force_max_tokens=${NANOCLAW_FORCE_MAX_TOKENS:-50} +# 默认控制台只打一行 reward 摘要;如需每项 details,设 NANOCLAW_REWARD_PRINT_DETAILS=1。 +nanoclaw_reward_print_details=${NANOCLAW_REWARD_PRINT_DETAILS:-0} +# verifier API 是单独节点,默认低并发,避免 RewardLoopWorker 同时打爆 API 导致排队超时。 +reward_num_workers=${REWARD_NUM_WORKERS:-52} + +project_name=${PROJECT_NAME:-qwen3.5-9b_nanoclaw_grpo_verl_0720} +experiment_name=${EXPERIMENT_NAME:-qwen3.5-9b_nanoclaw_grpo_sp1_16k_fused_lmhead_smoke} +default_local_dir=${DEFAULT_LOCAL_DIR:-$DATA_ROOT/checkpoint/$experiment_name} +start_time=$(date +%Y%m%d)_$(date +%H%M%S) +mkdir -p logs "${default_local_dir}" + +# ================= 算法与并行参数 ================= +adv_estimator=grpo +max_turns=${MAX_TURNS:-35} +max_prompt_length=${MAX_PROMPT_LENGTH:-8192} +max_response_length=${MAX_RESPONSE_LENGTH:-22768} +max_assistant_response_length=${MAX_ASSISTANT_RESPONSE_LENGTH:-16384} +max_tool_response_length=${MAX_TOOL_RESPONSE_LENGTH:-8192} +max_model_len=$((max_prompt_length + max_response_length)) + +# MindSpeed 配置仅作为训练意图参考;以下均使用最新版 VERL 的原生字段。 +actor_lr=${ACTOR_LR:-1e-6} +actor_lr_scheduler_type=${ACTOR_LR_SCHEDULER_TYPE:-constant} +actor_lr_warmup_steps_ratio=${ACTOR_LR_WARMUP_STEPS_RATIO:-0.0} +actor_weight_decay=${ACTOR_WEIGHT_DECAY:-0.01} +actor_adam_beta1=${ACTOR_ADAM_BETA1:-0.9} +actor_adam_beta2=${ACTOR_ADAM_BETA2:-0.95} +actor_clip_grad=${ACTOR_CLIP_GRAD:-1.0} +actor_ppo_epochs=${ACTOR_PPO_EPOCHS:-1} +actor_shuffle=${ACTOR_SHUFFLE:-False} +actor_entropy_coeff=${ACTOR_ENTROPY_COEFF:-0.0} +actor_clip_ratio_low=${ACTOR_CLIP_RATIO_LOW:-0.2} +actor_clip_ratio_high=${ACTOR_CLIP_RATIO_HIGH:-0.2} +# MindSpeed 配置没有 Dual-Clip PPO 对应项,保留该 27B 脚本原来的 C=10。 +actor_clip_ratio_c=${ACTOR_CLIP_RATIO_C:-10.0} + +# YAML 的 fixed init_kl_coef + low_var_kl 对应 VERL 的 reward-KL 路径。 +algorithm_gamma=${ALGORITHM_GAMMA:-1.0} +algorithm_lam=${ALGORITHM_LAM:-0.95} +use_kl_in_reward=${USE_KL_IN_REWARD:-True} +kl_penalty=${KL_PENALTY:-low_var_kl} +kl_ctrl_type=${KL_CTRL_TYPE:-fixed} +kl_coef=${KL_COEF:-0.001} +# 关闭 actor-KL,避免与 reward-KL 重复惩罚。 +actor_use_kl_loss=${ACTOR_USE_KL_LOSS:-False} +actor_kl_loss_coef=${ACTOR_KL_LOSS_COEF:-0.001} +actor_kl_loss_type=${ACTOR_KL_LOSS_TYPE:-low_var_kl} + +train_batch_size=${TRAIN_BATCH_SIZE:-64} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-16} +n_resp_per_prompt=${N_RESP_PER_PROMPT:-8} +# 先压低验证,避免验证和训练稳定性混在一起。 +n_resp_per_prompt_val=${N_RESP_PER_PROMPT_VAL:-1} +log_val_generations=${LOG_VAL_GENERATIONS:-10} + +infer_tp=${INFER_TP:-4} +train_sp=${TRAIN_SP:-1} +offload=${OFFLOAD:-True} + +# 稳定版固定沿用已经完成初始化与 rollout 验证的 FSDP1/offload 形状。 +actor_strategy=${ACTOR_STRATEGY:-fsdp} +fsdp_size=${FSDP_SIZE:-} + +actor_pack=${ACTOR_PACK:-1} +logprob_pack=${LOGPROB_PACK:-2} +actor_max_token_len_per_gpu=${ACTOR_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * actor_pack + train_sp - 1) / train_sp))} +log_prob_max_token_len_per_gpu=${LOG_PROB_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * logprob_pack + train_sp - 1) / train_sp))} +entropy_from_logits_with_chunking=${ENTROPY_FROM_LOGITS_WITH_CHUNKING:-True} +entropy_from_logits_chunk_size=${ENTROPY_FROM_LOGITS_CHUNK_SIZE:-256} +entropy_checkpointing=${ENTROPY_CHECKPOINTING:-False} +use_fused_kernels=${USE_FUSED_KERNELS:-True} +fused_kernel_backend=${FUSED_KERNEL_BACKEND:-torch} +enable_activation_offload=${ENABLE_ACTIVATION_OFFLOAD:-False} +rollout_max_num_batched_tokens=${ROLLOUT_MAX_NUM_BATCHED_TOKENS:-32384} +rollout_gpu_memory_utilization=${ROLLOUT_GPU_MEMORY_UTILIZATION:-0.60} +update_weights_bucket_mb=${UPDATE_WEIGHTS_BUCKET_MB:-8192} + +# Qwen 官方推荐:Instruct/non-thinking reasoning tasks +rollout_temperature=${ROLLOUT_TEMPERATURE:-0.6} +rollout_top_p=${ROLLOUT_TOP_P:-0.95} +rollout_top_k=${ROLLOUT_TOP_K:-20} +rollout_min_p=${ROLLOUT_MIN_P:-0.0} +rollout_presence_penalty=${ROLLOUT_PRESENCE_PENALTY:-0.0} +rollout_frequency_penalty=${ROLLOUT_FREQUENCY_PENALTY:-0.0} +rollout_repetition_penalty=${ROLLOUT_REPETITION_PENALTY:-1.0} + +echo "DEBUG: max_response_length=${max_response_length}, max_assistant_response_length=${max_assistant_response_length}, max_model_len=${max_model_len}" +echo "DEBUG: max_turns=${max_turns}" +echo "DEBUG: max_tool_response_length=${max_tool_response_length}" +echo "DEBUG: entropy_chunking=${entropy_from_logits_with_chunking}, entropy_chunk_size=${entropy_from_logits_chunk_size}, entropy_checkpointing=${entropy_checkpointing}" +echo "DEBUG: fused_lmhead=${use_fused_kernels}, fused_backend=${fused_kernel_backend}, activation_offload=${enable_activation_offload}" +echo "DEBUG: train_batch_size=${train_batch_size}, ppo_mini_batch_size=${ppo_mini_batch_size}, n=${n_resp_per_prompt}" +echo "DEBUG: train_sp=${train_sp}, infer_tp=${infer_tp}, actor_strategy=${actor_strategy}, fsdp_size=${fsdp_size:-}" +echo "DEBUG: Qwen3.5 Ulysses FLA required=${NANOCLAW_REQUIRE_FLA}, backend=${QWEN35_FLA_BACKEND} (TRAIN_SP=${train_sp})" +echo "DEBUG: actor_max_token_len_per_gpu=${actor_max_token_len_per_gpu}, log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu}" +echo "DEBUG: rollout sampling temperature=${rollout_temperature}, top_p=${rollout_top_p}, top_k=${rollout_top_k}, min_p=${rollout_min_p}, presence_penalty=${rollout_presence_penalty}, frequency_penalty=${rollout_frequency_penalty}, repetition_penalty=${rollout_repetition_penalty}" +echo "DEBUG: optimizer lr=${actor_lr}, scheduler=${actor_lr_scheduler_type}, warmup_ratio=${actor_lr_warmup_steps_ratio}, weight_decay=${actor_weight_decay}, betas=(${actor_adam_beta1},${actor_adam_beta2}), clip_grad=${actor_clip_grad}, ppo_epochs=${actor_ppo_epochs}, shuffle=${actor_shuffle}" +echo "DEBUG: KL use_in_reward=${use_kl_in_reward}, penalty=${kl_penalty}, ctrl=${kl_ctrl_type}, coef=${kl_coef}, actor_kl=${actor_use_kl_loss}" +echo "DEBUG: HCCL_BUFFSIZE=${HCCL_BUFFSIZE}, HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE}, HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE}" + +val_before_train=${VAL_BEFORE_TRAIN:-False} +trainer_use_v1=${TRAINER_USE_V1:-True} +test_freq=${TEST_FREQ:-5000} +save_freq=${SAVE_FREQ:-2} +total_training_steps=${TOTAL_TRAINING_STEPS:-2} + +# ================= 分布式 ================= +export TOTAL_NNODES=${TOTAL_NNODES:-5} +export TRAIN_NNODES=${TRAIN_NNODES:-4} +export NNODES=${NNODES:-${TRAIN_NNODES}} +export NODE_RANK=${VC_TASK_INDEX} +export NPUS_PER_NODE=${NPUS_PER_NODE:-8} +export WORLD_SIZE=$((NPUS_PER_NODE * NNODES)) + +export MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-0.${MA_VJ_NAME} +export MASTER_PORT=${MASTER_PORT:-6167} +export DASHBOARD_PORT=${DASHBOARD_PORT:-8191} +export RAY_PORT=${RAY_PORT:-6167} + +readonly SOCKET_IFNAME=${SOCKET_IFNAME:-eth0} +export HCCL_SOCKET_IFNAME=${HCCL_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export GLOO_SOCKET_IFNAME=${GLOO_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export CURRENT_IP=$(ifconfig ${SOCKET_IFNAME} | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +export RAY_NODE_IP=${MA_CURRENT_IP:-${CURRENT_IP}} + +export ASCEND_RT_VISIBLE_DEVICES=${ASCEND_RT_VISIBLE_DEVICES:-$(seq -s, 0 $((NPUS_PER_NODE - 1)))} + +cat < [Verifier API Node] This node is reserved for vLLM/OpenAI-compatible verifier API." + echo "--> [Verifier API Node] API base: ${mock_api_base}" + export VLLM_ENABLE_GRAPH_MODE=${verifier_api_enable_graph_mode} + mkdir -p "$(dirname "${verifier_api_log}")" + if [ -n "${verifier_api_start_cmd}" ]; then + echo "--> [Verifier API Node] Running VERIFIER_API_START_CMD..." + bash -lc "${verifier_api_start_cmd}" & + verifier_api_pid=$! + else + echo "--> [Verifier API Node] Starting default vLLM verifier API..." + verifier_api_device_count=$(awk -F',' '{print NF}' <<<"${verifier_api_devices}") + verifier_api_expected_device_count=$((verifier_api_tp * verifier_api_dp)) + if [ "${verifier_api_device_count}" -ne "${verifier_api_expected_device_count}" ]; then + echo "ERROR: verifier TP*DP=${verifier_api_tp}*${verifier_api_dp}=${verifier_api_expected_device_count}, but VERIFIER_API_DEVICES=${verifier_api_devices} contains ${verifier_api_device_count} devices." >&2 + exit 2 + fi + export ASCEND_RT_VISIBLE_DEVICES=${verifier_api_devices} + verifier_api_args=( + --model "${verifier_model_path}" + --tokenizer "${verifier_model_path}" + --host "${verifier_api_bind_host}" + --port "${verifier_api_port}" + --served-model-name "${mock_model_name}" + --tensor-parallel-size "${verifier_api_tp}" + --data-parallel-size "${verifier_api_dp}" + --distributed-executor-backend "${verifier_api_distributed_executor_backend}" + --dtype bfloat16 + --max-model-len "${verifier_api_max_model_len}" + --max-num-batched-tokens "${verifier_api_max_num_batched_tokens}" + --max-num-seqs "${verifier_api_max_num_seqs}" + --gpu-memory-utilization "${verifier_api_gpu_memory_utilization}" + --trust-remote-code + ) + if [ "${verifier_api_enforce_eager}" = "1" ] || [ "${verifier_api_enforce_eager}" = "true" ] || [ "${verifier_api_enforce_eager}" = "True" ]; then + verifier_api_args+=(--enforce-eager) + fi + if [ "${verifier_api_enable_prefix_caching}" = "1" ] || [ "${verifier_api_enable_prefix_caching}" = "true" ] || [ "${verifier_api_enable_prefix_caching}" = "True" ]; then + verifier_api_args+=(--enable-prefix-caching) + fi + echo "--> [Verifier API Node] Command: python3 -m vllm.entrypoints.openai.api_server ${verifier_api_args[*]}" + python3 -m vllm.entrypoints.openai.api_server "${verifier_api_args[@]}" >"${verifier_api_log}" 2>&1 & + verifier_api_pid=$! + fi + + echo "--> [Verifier API Node] vLLM API pid=${verifier_api_pid}, log=${verifier_api_log}" + echo "--> [Verifier API Node] Waiting for ${mock_api_base}/models ..." + python3 - "${mock_api_base}/models" "${verifier_api_startup_timeout}" "${verifier_api_log}" "${verifier_api_pid}" <<'PY' +import os +import sys +import time +import urllib.request +from pathlib import Path + +url = sys.argv[1] +timeout = float(sys.argv[2]) +log_path = Path(sys.argv[3]) +pid = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[4] else None +started = time.time() +last_error = None +while time.time() - started < timeout: + if pid is not None: + try: + os.kill(pid, 0) + except OSError: + print(f"ERROR: verifier API process exited early: pid={pid}", file=sys.stderr) + if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) + sys.exit(1) + try: + with urllib.request.urlopen(url, timeout=5) as response: + if 200 <= response.status < 300: + print(f"READY: {url}", file=sys.stderr) + sys.exit(0) + except Exception as exc: + last_error = exc + time.sleep(5) +print(f"ERROR: timed out waiting for {url}; last_error={last_error}", file=sys.stderr) +if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) +sys.exit(1) +PY + verifier_readiness_rc=$? + if [ "${verifier_readiness_rc}" -ne 0 ]; then + echo "ERROR: verifier API readiness check failed with rc=${verifier_readiness_rc}." >&2 + if kill -0 "${verifier_api_pid}" 2>/dev/null; then + kill "${verifier_api_pid}" 2>/dev/null || true + fi + wait "${verifier_api_pid}" 2>/dev/null || true + exit "${verifier_readiness_rc}" + fi + + echo "--> [Verifier API Node] Ready. Keeping node alive." + wait "${verifier_api_pid}" + verifier_api_rc=$? + if [ "${verifier_api_rc}" -ne 0 ]; then + echo "ERROR: verifier API exited with rc=${verifier_api_rc}; log=${verifier_api_log}" >&2 + fi + exit "${verifier_api_rc}" +fi + +export TMPDIR=/cache/ray_tmp +export HCCL_ASYNC_ERROR_HANDLING=${HCCL_ASYNC_ERROR_HANDLING:-0} + +wait_for_ray_npu_resources() { + expected_npu=$1 + timeout_seconds=${2:-900} + begin_ts=$(date +%s) + + while true; do + total_npu=$(python3 - <<'PY' 2>/dev/null +import ray + +try: + ray.init(address="auto", ignore_reinit_error=True, logging_level="ERROR") + print(int(ray.cluster_resources().get("NPU", 0))) + ray.shutdown() +except Exception: + print(0) +PY +) + total_npu=${total_npu:-0} + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + + echo "Ray NPU resources: ${total_npu}/${expected_npu}, elapsed=${elapsed}s" + ray status || true + + if [ "${total_npu}" -ge "${expected_npu}" ]; then + echo "Ray cluster is ready: ${total_npu}/${expected_npu} NPU resources registered." + break + fi + + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for Ray NPU resources: ${total_npu}/${expected_npu}" >&2 + return 1 + fi + + sleep 5 + done +} + +wait_for_verifier_api() { + api_url="${mock_api_base}/models" + timeout_seconds=${VERIFIER_API_CLIENT_WAIT_TIMEOUT:-1800} + begin_ts=$(date +%s) + last_diag_ts=0 + while true; do + verifier_check_output=$(python3 - "${api_url}" <<'PY' 2>&1 +import socket +import sys +import urllib.parse +import urllib.request + +url = sys.argv[1] +parsed = urllib.parse.urlparse(url) +host = parsed.hostname +port = parsed.port or (443 if parsed.scheme == "https" else 80) +print(f"check url={url} host={host} port={port}") +try: + infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + print("dns=" + ",".join(sorted({item[4][0] for item in infos}))) +except Exception as exc: + print(f"dns_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with socket.create_connection((host, port), timeout=5): + print("tcp=ok") +except Exception as exc: + print(f"tcp_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with urllib.request.urlopen(url, timeout=10) as response: + print(f"http_status={response.status}") + raise SystemExit(0 if 200 <= response.status < 300 else 1) +except Exception as exc: + print(f"http_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +PY +) + check_rc=$? + if [ "${check_rc}" = "0" ]; then + echo "Verifier API is ready: ${api_url}" + echo "${verifier_check_output}" + break + fi + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + echo "Waiting for verifier API: ${api_url}, elapsed=${elapsed}s" + if [ $((now_ts - last_diag_ts)) -ge 60 ]; then + last_diag_ts=${now_ts} + echo "--- verifier API check diagnostics ---" + echo "${verifier_check_output}" + echo "--- expected verifier node: rank=${verifier_api_node_rank}, host=${verifier_api_host}, port=${verifier_api_port} ---" + echo "--- check verifier node log: ${verifier_api_log} ---" + echo "--------------------------------------" + fi + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for verifier API: ${api_url}" >&2 + echo "Last verifier API diagnostics:" >&2 + echo "${verifier_check_output}" >&2 + return 1 + fi + sleep 10 + done +} + +# ================= Nanoclaw workspace 根目录 ================= +mkdir -p "${nanoclaw_temp_root}" +if ! touch "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" 2>/dev/null; then + echo "ERROR: Cannot write NANOCLAW_TEMP_ROOT: ${nanoclaw_temp_root}" >&2 + exit 2 +fi +rm -f "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" || true +if [[ "${nanoclaw_temp_root}" == /tmp/* ]]; then + echo "WARNING: NANOCLAW_TEMP_ROOT is under /tmp. Multi-node reward workers may not see rollout workspaces." >&2 + echo "WARNING: Prefer a shared path, e.g. ${DATA_ROOT}/nanoclaw_workspaces" >&2 +fi +echo "DEBUG: Nanoclaw train_base_tasks=${train_base_tasks}" +echo "DEBUG: Nanoclaw val_base_tasks=${val_base_tasks}" +echo "DEBUG: Nanoclaw task_glob=${nanoclaw_task_glob}, task_ids=${nanoclaw_task_ids:-}" +echo "DEBUG: Nanoclaw temp_root=${nanoclaw_temp_root}, cleanup=${nanoclaw_cleanup_workspaces}, keep_failed=${nanoclaw_keep_failed_workspaces}" + +# ================= 生成 Ray runtime env ================= +RUNTIME_ENV_FILE=${WORK_DIR}/verl_engine_runtime_env.generated.yaml +cat > "${RUNTIME_ENV_FILE}" < [Head Node] Starting Ray Head on ${CURRENT_IP}..." + ray start --head \ + --node-ip-address=${RAY_NODE_IP} \ + --port=${RAY_PORT} \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=${DASHBOARD_PORT} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + + sleep 10 + wait_for_ray_npu_resources ${WORLD_SIZE} 900 || exit 1 + wait_for_verifier_api || exit 1 +else + echo "--> [Worker Node] Starting Ray Worker, connecting to ${MASTER_ADDR}:${RAY_PORT}..." + sleep 20 + ray start --address=${MASTER_ADDR}:${RAY_PORT} \ + --node-ip-address=${RAY_NODE_IP} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + sleep 10 +fi + +# ================= 训练参数数组 ================= +training_args=( + python3 -m verl.trainer.main_ppo + +ray_kwargs.ray_init.address=auto + reward.num_workers=${reward_num_workers} + algorithm.adv_estimator=${adv_estimator} + algorithm.gamma=${algorithm_gamma} + algorithm.lam=${algorithm_lam} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_penalty=${kl_penalty} + algorithm.kl_ctrl.type=${kl_ctrl_type} + algorithm.kl_ctrl.kl_coef=${kl_coef} + algorithm.rollout_correction.bypass_mode=False + data.train_files="${train_files}" + data.val_files="${test_files}" + data.return_raw_chat=True + data.return_multi_modal_inputs=False + data.image_key=images + data.shuffle=True + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation=error + data.custom_cls.path=pkg://nanoclaw_recipe.nanoclaw + data.custom_cls.name=CustomRLHFDataset + "data.tool_config_path=${tool_config_path}" + "+data.nanoclaw_task_glob=${nanoclaw_task_glob}" + "+data.nanoclaw_temp_root=${nanoclaw_temp_root}" + "+data.nanoclaw_cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+data.nanoclaw_keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+data.nanoclaw_env_builder_timeout=${nanoclaw_env_builder_timeout}" + "+data.nanoclaw_verifier_timeout=${nanoclaw_verifier_timeout}" + "+data.nanoclaw_reward_score_mode=${nanoclaw_reward_score_mode}" + "+data.nanoclaw_allow_bash=${nanoclaw_allow_bash}" + +data.apply_chat_template_kwargs.enable_thinking=True + reward.custom_reward_function.path=pkg://nanoclaw_recipe.nanoclaw + reward.custom_reward_function.name=compute_score + "+reward.custom_reward_function.reward_kwargs.cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+reward.custom_reward_function.reward_kwargs.keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+reward.custom_reward_function.reward_kwargs.verifier_timeout=${nanoclaw_verifier_timeout}" + "+reward.custom_reward_function.reward_kwargs.reward_score_mode=${nanoclaw_reward_score_mode}" + "+reward.custom_reward_function.reward_kwargs.require_final_answer=${nanoclaw_require_final_answer}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_enable=${nanoclaw_final_answer_bonus_enable}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_score=${nanoclaw_final_answer_bonus_score}" + "+reward.custom_reward_function.reward_kwargs.turn_penalty_only_positive_score=${nanoclaw_turn_penalty_only_positive_score}" + "+reward.custom_reward_function.reward_kwargs.assistant_turn_penalty=${nanoclaw_assistant_turn_penalty}" + "+reward.custom_reward_function.reward_kwargs.duplicate_tool_call_penalty=${nanoclaw_duplicate_tool_call_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_penalty=${nanoclaw_repeated_response_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_chars=${nanoclaw_repeated_response_min_chars}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_consecutive_repeats=${nanoclaw_repeated_response_min_consecutive_repeats}" + "+reward.custom_reward_function.reward_kwargs.mock_api_base=${mock_api_base}" + "+reward.custom_reward_function.reward_kwargs.mock_api_key=${mock_api_key}" + "+reward.custom_reward_function.reward_kwargs.mock_model_name=${mock_model_name}" + "+reward.custom_reward_function.reward_kwargs.mock_api_timeout=${mock_api_timeout}" + "+reward.custom_reward_function.reward_kwargs.mock_api_connect_timeout=${mock_api_connect_timeout}" + actor_rollout_ref.model.path=${model_path} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True + actor_rollout_ref.model.enable_activation_offload=${enable_activation_offload} + actor_rollout_ref.model.use_fused_kernels=${use_fused_kernels} + actor_rollout_ref.model.fused_kernel_options.impl_backend=${fused_kernel_backend} + actor_rollout_ref.actor.strategy=${actor_strategy} + actor_rollout_ref.ref.strategy=${actor_strategy} + actor_rollout_ref.actor.use_kl_loss=${actor_use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${actor_kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=${actor_kl_loss_type} + actor_rollout_ref.actor.clip_ratio_low=${actor_clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${actor_clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=${actor_clip_ratio_c} + actor_rollout_ref.actor.entropy_coeff=${actor_entropy_coeff} + actor_rollout_ref.actor.ppo_epochs=${actor_ppo_epochs} + actor_rollout_ref.actor.shuffle=${actor_shuffle} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.optim.lr_scheduler_type=${actor_lr_scheduler_type} + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=${actor_lr_warmup_steps_ratio} + actor_rollout_ref.actor.optim.weight_decay=${actor_weight_decay} + "actor_rollout_ref.actor.optim.betas=[${actor_adam_beta1},${actor_adam_beta2}]" + actor_rollout_ref.actor.optim.clip_grad=${actor_clip_grad} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_max_token_len_per_gpu} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.actor.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.ref.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.mode=async + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.temperature=${rollout_temperature} + actor_rollout_ref.rollout.top_p=${rollout_top_p} + actor_rollout_ref.rollout.top_k=${rollout_top_k} + actor_rollout_ref.rollout.min_p=${rollout_min_p} + actor_rollout_ref.rollout.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.tensor_model_parallel_size=${infer_tp} + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=${update_weights_bucket_mb} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.max_num_batched_tokens=${rollout_max_num_batched_tokens} + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.enable_prefix_caching=False + actor_rollout_ref.rollout.multi_turn.enable=True + actor_rollout_ref.rollout.multi_turn.max_user_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_response_length=${max_assistant_response_length} + "actor_rollout_ref.rollout.multi_turn.tool_config_path=${tool_config_path}" + actor_rollout_ref.rollout.multi_turn.format=qwen3_coder + "actor_rollout_ref.rollout.multi_turn.max_tool_response_length=${max_tool_response_length}" + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_memory_utilization} + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.val_kwargs.temperature=${rollout_temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${rollout_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${rollout_top_k} + actor_rollout_ref.rollout.val_kwargs.min_p=${rollout_min_p} + actor_rollout_ref.rollout.val_kwargs.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.val_kwargs.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.val_kwargs.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=${n_resp_per_prompt_val} + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.fsdp_config.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.use_torch_compile=False + critic.fsdp.use_torch_compile=False + trainer.use_v1=${trainer_use_v1} + trainer.v1.trainer_mode=sync + trainer.critic_warmup=0 + trainer.balance_batch=True + trainer.logger=['console','tensorboard'] + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.nnodes=${NNODES} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.val_before_train=${val_before_train} + trainer.log_val_generations=${log_val_generations} + trainer.save_freq=${save_freq} + trainer.default_local_dir=${default_local_dir} + trainer.resume_mode=disable + trainer.test_freq=${test_freq} + trainer.total_epochs=10 + trainer.total_training_steps=${total_training_steps} +) + +if [ -n "${fsdp_size}" ]; then + training_args+=( + actor_rollout_ref.actor.fsdp_config.fsdp_size=${fsdp_size} + actor_rollout_ref.ref.fsdp_config.fsdp_size=${fsdp_size} + ) +fi + +if [ -n "${nanoclaw_task_ids}" ]; then + training_args+=("+data.nanoclaw_task_ids=${nanoclaw_task_ids}") +fi + +if [ -n "${nanoclaw_max_steps}" ]; then + training_args+=("+data.nanoclaw_max_steps=${nanoclaw_max_steps}") +fi + +# ================= 启动训练主进程:仅主节点执行 ================= +if [ "${NODE_RANK}" = "0" ]; then + echo "--> [Head Node] Starting VERL unified engine training..." + echo "DEBUG: runtime_env=${RUNTIME_ENV_FILE}" + echo "DEBUG: entrypoint=${training_args[*]}" + + ray job submit \ + --address="http://127.0.0.1:${DASHBOARD_PORT}" \ + --runtime-env="${RUNTIME_ENV_FILE}" \ + -- \ + "${training_args[@]}" 2>&1 | tee "logs/qwen3.5-nanoclaw-grpo-verl-engine-${start_time}.log" +else + echo "--> [Worker Node] Setup finished. Keeping node alive for Ray..." + tail -f /dev/null +fi diff --git a/verl_0720_main/mindspeed_config.yaml b/verl_0720_main/mindspeed_config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..54e2c117c570ccdcb2383ba81e3d205f23046776 --- /dev/null +++ b/verl_0720_main/mindspeed_config.yaml @@ -0,0 +1,132 @@ +defaults: + - model: + - qwen25_32b + +megatron_training: + use_ascend_coc: True + coc_fused_kernel: True + coc_parallel_num: 2 + model: qwen25_32b + tokenizer_type: PretrainedFromHF + tokenizer_name_or_path: /opt/huawei/dataset/zyr_ulan/models/Qwen2.5-32B-Instruct + data_path: /opt/huawei/dataset/zyr_ulan/lyf/datasets/code_only_gemini3_and_gsm8k_train/gemini3_only/data_english_train_gemini3_to_train_clean_1/data_english_train_gemini3_to_train_clean_1 + variable_seq_lengths: true + global_batch_size: 128 + use_fused_rmsnorm: true + use_mcore_models: true + sequence_parallel: true + use_flash_attn: true + no_masked_softmax_fusion: true + attention_softmax_in_fp32: true + no_gradient_accumulation_fusion: true + use_fused_swiglu: true + use_fused_rotary_pos_emb: true + bf16: true + save_interval: 100 + train_iters: 200 + seq_length: 8192 + stage: ray_grpo + attention_dropout: 0.0 + init_method_std: 0.01 + hidden_dropout: 0.0 + distributed_backend: nccl + no_shared_storage: true + dataset_additional_keys: ['labels', 'id', 'llm_checker', 'code_checker'] + split: 100,0,0 + no_shuffle: false + full_shuffle_instruction_dataset: false + seed: 1 + reuse_fp32_param: false + recompute_granularity: 'full' + recompute_method: 'uniform' + recompute_num_layers: 1 + use_distributed_optimizer: true + swap_attention: true + reset_position_ids: true + +actor_config: + model: qwen25_32b + tensor_model_parallel_size: 4 + pipeline_model_parallel_size: 4 + micro_batch_size: 4 + lr: 1e-6 + lr_decay_style: constant + min_lr: 0 + weight_decay: 0.01 + lr_warmup_fraction: 0.0 + clip_grad: 1.0 + adam_beta1: 0.9 + adam_beta2: 0.95 + finetune: true + load: /opt/huawei/dataset/zyr_ulan/models/qwen2.5-32b/Qwen2.5-32B-Instruct-pp4tp4/ + save: /opt/huawei/dataset/zyr_ulan/lyf/models/save/stage_sft_0831/stage_RL/qwen_32b_instruct_RL_lyf_if_code_only_gemini3data_only/ + no_load_optim: true + no_load_rng: true + +rl_config: + is_qwen3: false + full_satisfaction_bonus: 0.0 + code_checker_failure_prob: 0.0 + reward_scaling_strategy: "linear" + tag_style: 'angle' + prompt_style: 'v2' + granular_acc_factor: 0.0 + thinking_bonus: 0.0 + thinking_penalty: 0.0 + use_integrated_worker: true + guarantee_order: false + blocking: true + actor_forward_micro_batch_size: 4 + ref_forward_micro_batch_size: 4 + kl_penalty: low_var_kl + shuffle_mini_batch: false + log_max_throughput: false + num_cpus_for_local_task: 1.0 + use_tensorboard: true + gamma: 1.0 + lam: 0.95 + adv_estimator: group_norm + kl_ctrl_type: fixed + init_kl_coef: 0.001 + mini_batch_size: 128 + max_prompt_length: 2048 + rollout_max_tokens: 6144 + epochs: 1 + clip_ratio: 0.2 + entropy_coeff: 0.0 + n_samples_per_prompt: 8 + rule_reward: true + use_remove_padding: true + verifier_function: ["llm_checker", "code_checker"] + verifier_weight: [0.7, 0.3] + overlong_buffer_enable: true + overlong_buffer: 1536 + overlong_buffer_penalty_factor: 0.0 + use_wandb: false + actor_resource: + num_npus: 32 + +generate_config: + trust_remote_code: true + offload_train_optimizer: true + offload_train_grad: true + offload_train_param: true + infer_tensor_parallel_size: 4 + infer_pipeline_parallel_size: 1 + infer_expert_parallel_size: 1 + max_num_seqs: 256 + max_model_len: 8192 + max_num_batched_tokens: 8192 + dtype: "bfloat16" + gpu_memory_utilization: 0.6 + sampling_config: + logprobs: 1 + max_tokens: 6144 + top_p: 1.0 + top_k: -1 + min_p: 0.0 + temperature: 1.0 + detokenize: false + +profiler_config: + use_profiler: false \ No newline at end of file diff --git "a/verl_0720_main/mindspeed_config.yaml\357\200\272Zone.Identifier" "b/verl_0720_main/mindspeed_config.yaml\357\200\272Zone.Identifier" new file mode 100644 index 0000000000000000000000000000000000000000..d1c921d4b38f165bd09933b0749b1b9dfb80a2cc --- /dev/null +++ "b/verl_0720_main/mindspeed_config.yaml\357\200\272Zone.Identifier" @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://huggingface.co/datasets/geminiDeveloper/contextBenchmark/tree/main/0720 +HostUrl=https://huggingface.co/api/resolve-cache/datasets/geminiDeveloper/contextBenchmark/94e9154f5f65b80efb1223a4cdb0bf6cc533351d/0720%2Fmindspeed_config.yaml?download=true&etag=%2254e2c117c570ccdcb2383ba81e3d205f23046776%22 diff --git a/verl_0720_main/nanoclaw_qwen35_sp1_16k_stable_full.sh b/verl_0720_main/nanoclaw_qwen35_sp1_16k_stable_full.sh new file mode 100644 index 0000000000000000000000000000000000000000..d4b0bfea83a4d763f309656b726a4349d96bc566 --- /dev/null +++ b/verl_0720_main/nanoclaw_qwen35_sp1_16k_stable_full.sh @@ -0,0 +1,1059 @@ +#!/bin/bash +# Qwen3.5 Nanoclaw 多轮 GRPO 训练脚本 — VERL 0720 + NPU SP1/16K 独立稳定版 +# +# 关键修改: +# 1) 入口改为 python3 -m verl.trainer.main_ppo,不再使用 recipe.grpo_mindspeed_mm.main_ppo; +# 2) 不再使用 MM_CONFIG_FILE / MindSpeed-MM YAML; +# 3) 显式 text-only:data.return_multi_modal_inputs=False; +# 4) 数据输入改为 Nanoclaw base_tasks 目录,不再使用 Retool parquet/json; +# 5) 保留 27B 作业验证过的 HCCL buffer 与端口范围,降低 HcclAllreduce socket/资源压力; +# 6) 默认 val n=1、log_val_generations=10,先验证训练稳定性; +# 7) 稳定版固定使用已经完成初始化与 rollout 验证的 FSDP1/offload 形状。 +# 8) 使用最新版 VERL V1/TransferQueue 和 nanoclaw_recipe,并按参考 YAML 的训练意图映射优化器与 KL; +# 9) actor/ref 的全词表 entropy 使用 256-token 分块,避免 16K SP1 old-log-prob 阶段产生十几 GiB 临时张量。 + +set -x + +# ================= 独立稳定版:强制 Qwen3.5 NPU SP1 + 16K ================= +# 不允许外部作业环境把这些核心安全参数覆盖回 SP8/31K。 +export TRAIN_SP=1 +export QWEN35_FLA_BACKEND=disabled +export MAX_PROMPT_LENGTH=8192 +export MAX_RESPONSE_LENGTH=8192 +export MAX_ASSISTANT_RESPONSE_LENGTH=8192 +export MAX_TOOL_RESPONSE_LENGTH=8192 +export ACTOR_MAX_TOKEN_LEN_PER_GPU=16384 +export LOG_PROB_MAX_TOKEN_LEN_PER_GPU=16384 +export ROLLOUT_MAX_NUM_BATCHED_TOKENS=16384 +export ACTOR_STRATEGY=fsdp +export OFFLOAD=True +# 当前 VERL 的 checkpointing 分支不会传递自定义 chunk size;稳定版明确关闭它,确保实际使用 256。 +export ENTROPY_FROM_LOGITS_WITH_CHUNKING=True +export ENTROPY_FROM_LOGITS_CHUNK_SIZE=256 +export ENTROPY_CHECKPOINTING=False + + +npu-smi info || true +pip install --upgrade pip +pip uninstall -y moxing-framework || true + +# ================= 路径配置 ================= +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +if [ -f "${SCRIPT_DIR}/verl/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +elif [ -f "${SCRIPT_DIR}/requirements-npu.txt" ]; then + DEFAULT_WORK_DIR=${SCRIPT_DIR} +else + DEFAULT_WORK_DIR=${SCRIPT_DIR}/verl +fi +WORK_DIR=${WORK_DIR:-${DEFAULT_WORK_DIR}} +INSTALL_DIR=${INSTALL_DIR:-/home/ma-user} +BKGS=${BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs} +chmod 755 "${INSTALL_DIR}" + +# Nanoclaw 自定义包已随 WORK_DIR 提供:nanoclaw_recipe。 + +GCC_INSTALL_PREFIX=${GCC_INSTALL_PREFIX:-/home/ma-user/gcc-11.3.0} +COMPILED_GCC_ARCHIVE_PATH=${COMPILED_GCC_ARCHIVE_PATH:-/opt/huawei/dataset/zyr_yuyin/bkgs/gcc-11.3.0-compiled-aarch64.tar.gz} + +echo "--> 正在从缓存恢复 GCC 11.3.0..." +tar -xzf "${COMPILED_GCC_ARCHIVE_PATH}" -C /home/ma-user/ +export PATH=${GCC_INSTALL_PREFIX}/bin:${PATH} +export LD_LIBRARY_PATH=${GCC_INSTALL_PREFIX}/lib64:${GCC_INSTALL_PREFIX}/lib:${LD_LIBRARY_PATH:-} +export CC=${GCC_INSTALL_PREFIX}/bin/gcc +export CXX=${GCC_INSTALL_PREFIX}/bin/g++ +echo "--> 验证 GCC 版本:" +gcc --version + +cd "${BKGS}" +cp jemalloc-5.3.0.tar.bz2 "${INSTALL_DIR}" + +VLLM_LATEST_PKGS=${VLLM_LATEST_PKGS:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-05-12/verl_new_26_05_09/pkgs} +rm -rf "${INSTALL_DIR}/vllm" "${INSTALL_DIR}/vllm-ascend" +cp -r "${VLLM_LATEST_PKGS}/vllm" "${INSTALL_DIR}" +cp -r "${VLLM_LATEST_PKGS}/vllm-ascend" "${INSTALL_DIR}" + +CANN_BKGS=${CANN_BKGS:-/opt/huawei/dataset/zyr_yuyin/bkgs/cann_0527} +cp "${CANN_BKGS}/Ascend-cann-toolkit_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-910b-ops_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" +cp "${CANN_BKGS}/Ascend-cann-nnal_9.0.0_linux-aarch64.run" "${INSTALL_DIR}" + +echo "################" +echo "## set verl env" +echo "################" + +cd "${INSTALL_DIR}" + +chmod +x Ascend-cann-toolkit_9.0.0_linux-aarch64.run +bash Ascend-cann-toolkit_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/ascend-toolkit/set_env.sh" + +chmod +x Ascend-cann-910b-ops_9.0.0_linux-aarch64.run +bash Ascend-cann-910b-ops_9.0.0_linux-aarch64.run --install --quiet + +chmod +x Ascend-cann-nnal_9.0.0_linux-aarch64.run +bash Ascend-cann-nnal_9.0.0_linux-aarch64.run --install --quiet +source "${INSTALL_DIR}/Ascend/nnal/atb/set_env.sh" + +export ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME} +export LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:${LD_LIBRARY_PATH:-} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip3 install torch==2.9.0 +pip3 install pyyaml setuptools +pip3 install torch-npu==2.9.0 +pip3 install torchvision==0.24.0 torchaudio==2.9.0 + +ASCEND_TOOLKIT_PYTHON_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/python/site-packages +export PYTHONPATH=${PYTHONPATH:-}:${INSTALL_DIR}:${ASCEND_TOOLKIT_PYTHON_PATH} +pip install pybind11==2.13.6 + +cd "${INSTALL_DIR}/vllm" +VLLM_TARGET_DEVICE=empty pip install . + +cd "${INSTALL_DIR}/vllm-ascend" +pip install -e . +export VLLM_LOGGING_LEVEL=INFO + +cd "${INSTALL_DIR}" +tar -xvf jemalloc-5.3.0.tar.bz2 +cd jemalloc-5.3.0 +./configure --prefix="${INSTALL_DIR}" +make -j"$(nproc)" +make install +export LD_PRELOAD=${INSTALL_DIR}/lib/libjemalloc.so.2:${LD_PRELOAD:-} + +# # ================= 可选:安装 MindSpeed 栈 ================= +# # 纯 VERL engine 路线不依赖 MindSpeed-MM YAML。默认不安装,避免和新版 VERL engine 混淆。 +# INSTALL_MINDSPEED_STACK=${INSTALL_MINDSPEED_STACK:-0} +# if [ "${INSTALL_MINDSPEED_STACK}" = "1" ]; then +# MindSpeed_PATH=${MindSpeed_PATH:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-slow-stable} +# cd "${MindSpeed_PATH}" +# cd Megatron-LM && pip install -e . --no-deps && cd .. +# cd MindSpeed && pip install -e . --no-deps && cd .. +# cd MindSpeed-MM && mkdir -p logs data ckpt && pip install -e . --no-deps && cd .. +# pip install beartype bs4 diffusers==0.30.3 ftfy imageio-ffmpeg pandarallel pytest-mock +# else +# echo "--> Skip MindSpeed/MindSpeed-MM installation for pure VERL engine route." +# fi + + + +# ================= 安装 Triton-Ascend 3.2.1 ================= +# 1. 卸载 triton(增加 -y 自动确认) +pip uninstall -y triton + +# 2. 卸载 triton-ascend(增加 -y 自动确认) +pip uninstall -y triton-ascend +pip install --no-cache-dir --force-reinstall triton==3.5.0 +pip install --no-deps /opt/huawei/dataset/zyr_yuyin/bkgs/triton_ascend-3.2.1-cp311-cp311-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl + + +# ================= 安装新版 VERL ================= +cd "${WORK_DIR}" +pip install -r requirements-npu.txt +# NPU requirements 明确要求 numpy<2;editable 安装不能再次按 setup.py 把 NumPy升级到 2.x。 +python3 -m pip install -e . --no-deps +pip install --upgrade 'urllib3==1.26.11' +pip install loguru +pip install tree_sitter==0.21.3 +pip install tree-sitter-java==0.21.0 +pip install tree-sitter-javascript==0.21.4 + +ACL_PATH=/home/ma-user/Ascend/ascend-toolkit/latest/aarch64-linux/lib64 +export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${ACL_PATH} +echo "LD_LIBRARY_PATH=${LD_LIBRARY_PATH}" + +pip uninstall -y transformers || true +pip install transformers==5.3.0 +pip install accelerate==1.13.0 mathruler +pip install jsonargparse +pip install deepdiff sympy html2text requests bs4 mpmath swanlab PandoraBox json_repair openai httpx + +# 稳定版只允许 SP=1:不安装普通 FLA,也不进入尚未完成 NPU 适配的 Ulysses CP。 +if [ "${TRAIN_SP}" != "1" ]; then + echo "ERROR: the standalone stable profile requires TRAIN_SP=1; got ${TRAIN_SP}." >&2 + exit 2 +fi +export NANOCLAW_REQUIRE_FLA=0 +echo "--> Stable SP1: flash-linear-attention is disabled; Qwen3.5 will not build an Ulysses CP context." + +# Transformers 5.x 会经 sklearn 间接导入 pandas/scipy。固定同一套 NumPy ABI, +# 避免出现 "numpy.dtype size changed"。这些版本均支持 Python 3.11/aarch64。 +NUMPY_VERSION=${NUMPY_VERSION:-1.26.4} +PANDAS_VERSION=${PANDAS_VERSION:-2.2.3} +SCIPY_VERSION=${SCIPY_VERSION:-1.14.1} +SKLEARN_VERSION=${SKLEARN_VERSION:-1.6.1} +python3 -m pip install --no-cache-dir --force-reinstall \ + "numpy==${NUMPY_VERSION}" \ + "pandas==${PANDAS_VERSION}" \ + "scipy==${SCIPY_VERSION}" \ + "scikit-learn==${SKLEARN_VERSION}" + +python3 - <<'PY' || exit 2 +import numpy +import pandas +import scipy +import sklearn +import sys +import transformers +import vllm + +fla_version = "disabled-sp1" + +print( + "[python_stack_preflight] " + f"python={sys.executable} " + f"numpy={numpy.__version__} " + f"pandas={pandas.__version__} " + f"scipy={scipy.__version__} " + f"sklearn={sklearn.__version__} " + f"transformers={transformers.__version__} " + f"vllm={vllm.__version__} " + f"fla={fla_version}" +) +print( + "[python_stack_paths] " + f"numpy={numpy.__file__} " + f"pandas={pandas.__file__}" +) +PY +pip list + +# ================= 检查 Nanoclaw recipe ================= +if [ ! -f "${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" ]; then + echo "ERROR: Nanoclaw recipe not found: ${WORK_DIR}/nanoclaw_recipe/nanoclaw.py" >&2 + exit 2 +fi +test -f "${WORK_DIR}/nanoclaw_recipe/__init__.py" + +# Qwen3.5 MRoPE position_ids 是 3/4 轴张量。未应用此补丁时,NPU +# FlashAttention 会把 seqLen 重复累计(例如 T=10131、sum(seqLen)=30393)。 +QWEN35_MONKEY_PATCH_FILE=${WORK_DIR}/verl/models/transformers/monkey_patch.py +if [ ! -f "${QWEN35_MONKEY_PATCH_FILE}" ] || ! grep -q "def _normalize_fa_position_ids" "${QWEN35_MONKEY_PATCH_FILE}"; then + echo "ERROR: Qwen3.5 FlashAttention position_ids normalization patch is missing: ${QWEN35_MONKEY_PATCH_FILE}" >&2 + echo "Upload the modified verl/ directory together with this standalone script." >&2 + exit 2 +fi + +# ================= PLOG ================= +ma_vj_name=$(echo "${MA_VJ_NAME}" | sed 's:ma-job:modelarts-job:g') +task_name=worker-${VC_TASK_INDEX} +task_plog_path=${MA_LOG_DIR}/${ma_vj_name}/${task_name} +mkdir -p "${task_plog_path}" +export ASCEND_PROCESS_LOG_PATH=${task_plog_path}/${VC_TASK_INDEX} +echo "plog path: ${ASCEND_PROCESS_LOG_PATH}" + +MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-${VC_TASK_INDEX}.${MA_VJ_NAME} +MASTER_PORT=${PORT} +MA_CURRENT_INSTANCE_NAME=${MA_CURRENT_INSTANCE_NAME} + +cd "${WORK_DIR}" + +mkdir -p /cache/ray_tmp + +echo "Cleaning up old Ray processes..." +ray stop --force || true +sleep 5 +rm -rf /cache/ray_tmp/* +pkill -9 -f raylet || true +pkill -9 -f plasma_store || true +pkill -9 -f gcs_server || true +echo "Waiting 20s for NPU/Ray resources to be released..." +npu-smi info || true +sleep 20 + +# ================= NPU / HCCL / Ray 环境 ================= +export NON_MEGATRON=true +export MULTI_STREAM_MEMORY_REUSE=2 +export OMP_NUM_THREADS=1 +export PYTORCH_NPU_ALLOC_CONF=${PYTORCH_NPU_ALLOC_CONF:-max_split_size_mb:512} +export VLLM_LOGGING_LEVEL=INFO +export RAY_DEDUP_LOGS=0 +export HCCL_EXEC_TIMEOUT=${HCCL_EXEC_TIMEOUT:-3600} +export HCCL_LOG_LEVEL=${HCCL_LOG_LEVEL:-WARN} +export HCCL_CONNECT_TIMEOUT=${HCCL_CONNECT_TIMEOUT:-3600} +export HCCL_EVENT_TIMEOUT=${HCCL_EVENT_TIMEOUT:-7200} +export ACL_DEVICE_SYNC_TIMEOUT=${ACL_DEVICE_SYNC_TIMEOUT:-7200} +export GLOO_SOCKET_TIMEOUT=${GLOO_SOCKET_TIMEOUT:-7200} + +# 关键:降低 HCCL buffer,增加 socket 端口范围,缓解 HcclAllreduce ra socket batch connect failed。 +export HCCL_BUFFSIZE=${HCCL_BUFFSIZE:-300} +export P2P_HCCL_BUFFSIZE=${P2P_HCCL_BUFFSIZE:-64} +export HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE:-60000-60050} +export HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE:-61000-61050} + +export CUDA_DEVICE_MAX_CONNECTIONS=1 +export VLLM_ASCEND_ENABLE_NZ=${VLLM_ASCEND_ENABLE_NZ:-0} +export HCCL_OP_EXPANSION_MODE=${HCCL_OP_EXPANSION_MODE:-AIV} +export VLLM_ENGINE_ITERATION_TIMEOUT_S=${VLLM_ENGINE_ITERATION_TIMEOUT_S:-3600} +export WANDB_MODE=${WANDB_MODE:-disabled} +export PYTHONUNBUFFERED=1 +export TASK_QUEUE_ENABLE=${TASK_QUEUE_ENABLE:-1} +export COMBINED_ENABLE=${COMBINED_ENABLE:-1} +export TOKENIZERS_PARALLELISM=false +export CLOSE_MATMUL_K_SHIFT=${CLOSE_MATMUL_K_SHIFT:-1} +export ATB_MATMUL_SHUFFLE_K_ENABLE=${ATB_MATMUL_SHUFFLE_K_ENABLE:-0} +export HCCL_DETERMINISTIC=${HCCL_DETERMINISTIC:-true} +export VLLM_ENABLE_V1_MULTIPROCESSING=${VLLM_ENABLE_V1_MULTIPROCESSING:-0} +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export ASCEND_GLOBAL_LOG_LEVEL=${ASCEND_GLOBAL_LOG_LEVEL:-3} +export HYDRA_FULL_ERROR=1 +export RAY_gcs_server_rpc_server_thread_num=${RAY_gcs_server_rpc_server_thread_num:-32} +export RAY_gcs_server_request_timeout_seconds=${RAY_gcs_server_request_timeout_seconds:-600} +export RAY_timeout_ms=${RAY_timeout_ms:-600000} +export RAY_worker_register_timeout_seconds=${RAY_worker_register_timeout_seconds:-600} +export RAY_USAGE_STATS_ENABLED=0 +export VERL_REUSE_AGENT_LOOP=${VERL_REUSE_AGENT_LOOP:-1} + +ulimit -n 65536 + +# Ray 不要覆盖 ASCEND_RT_VISIBLE_DEVICES;VERL 内部按 local_rank 选卡。 +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + +# ================= 路径与数据配置 ================= +HDFS_ROOT=${HDFS_ROOT:-$PWD} +DATA_ROOT=${DATA_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_ckpt} + +# Nanoclaw 数据输入支持两种目录,优先推荐 0625 扁平格式: +# base_tasks/data_*/env_builder.py +# base_tasks/data_*/prompts.md +# base_tasks/data_*/workplace_verifier.py +# base_tasks/data_*/manifest.json +# 也兼容旧格式:base_tasks/tasks/data_* + base_tasks/scripts|scrips/data_*。 +DEFAULT_NANOCLAW_BASE_TASKS=${DEFAULT_NANOCLAW_BASE_TASKS:-/opt/huawei/dataset/zyr_yuyin/lyf/datasets/nanoclawRLdata/0710_add1000agent_qwen3_7_max_v1/exported_new_data} +train_base_tasks=${TRAIN_DATA_PATH:-${BASE_TASKS:-${DEFAULT_NANOCLAW_BASE_TASKS}}} +val_base_tasks=${VAL_DATA_PATH:-${VAL_BASE_TASKS:-${train_base_tasks}}} +train_files="['$train_base_tasks']" +test_files="['$val_base_tasks']" + +if [ ! -d "${train_base_tasks}" ]; then + echo "ERROR: Nanoclaw TRAIN_DATA_PATH/BASE_TASKS directory not found: ${train_base_tasks}" >&2 + exit 2 +fi +if [ ! -d "${val_base_tasks}" ]; then + echo "ERROR: Nanoclaw VAL_DATA_PATH/VAL_BASE_TASKS directory not found: ${val_base_tasks}" >&2 + exit 2 +fi + +model_path=${MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-27B} +verifier_model_path=${VERIFIER_MODEL_PATH:-/opt/huawei/dataset/zyr_yuyin/models/Qwen/Qwen3___5-9B} +# 纯 VERL engine 路线:不要使用 MindSpeed-MM YAML。 +unset MM_CONFIG_FILE || true + +# Nanoclaw 工具配置 +tool_config_path=${TOOL_CONFIG_PATH:-nanoclaw_recipe/nanoclaw_tool_config.yaml} +nanoclaw_task_glob=${NANOCLAW_TASK_GLOB:-data_*} +nanoclaw_task_ids=${NANOCLAW_TASK_IDS:-} +# 多机训练必须用所有节点都能访问的共享目录;不要用 /tmp,否则 reward worker 可能跨节点找不到 workspace。 +nanoclaw_temp_root=${NANOCLAW_TEMP_ROOT:-/opt/huawei/dataset/zyr_yuyin/lyf/verl-nanoclaw-rl/nanoclawRL_temp_workplace_v14_qwen35_27b_16k} +# 默认保留每个 step/data_sample 的目录,方便复盘每条 GRPO 采样;磁盘紧张时手动设 NANOCLAW_CLEANUP_WORKSPACES=True。 +nanoclaw_cleanup_workspaces=${NANOCLAW_CLEANUP_WORKSPACES:-False} +nanoclaw_keep_failed_workspaces=${NANOCLAW_KEEP_FAILED_WORKSPACES:-False} +nanoclaw_env_builder_timeout=${NANOCLAW_ENV_BUILDER_TIMEOUT:-120} +nanoclaw_verifier_timeout=${NANOCLAW_VERIFIER_TIMEOUT:-3600} +nanoclaw_reward_score_mode=${NANOCLAW_REWARD_SCORE_MODE:-ratio} +nanoclaw_allow_bash=${NANOCLAW_ALLOW_BASH:-True} +nanoclaw_max_steps=${NANOCLAW_MAX_STEPS:-} +nanoclaw_require_final_answer=${NANOCLAW_REQUIRE_FINAL_ANSWER:-True} +nanoclaw_final_answer_bonus_enable=${NANOCLAW_FINAL_ANSWER_BONUS_ENABLE:-False} +nanoclaw_final_answer_bonus_score=${NANOCLAW_FINAL_ANSWER_BONUS_SCORE:-0.0} +nanoclaw_turn_penalty_only_positive_score=${NANOCLAW_TURN_PENALTY_ONLY_POSITIVE_SCORE:-False} +nanoclaw_assistant_turn_penalty=${NANOCLAW_ASSISTANT_TURN_PENALTY:-0.0} +nanoclaw_duplicate_tool_call_penalty=${NANOCLAW_DUPLICATE_TOOL_CALL_PENALTY:-0.0} +nanoclaw_repeated_response_penalty=${NANOCLAW_REPEATED_RESPONSE_PENALTY:-0.0} +nanoclaw_repeated_response_min_chars=${NANOCLAW_REPEATED_RESPONSE_MIN_CHARS:-35} +nanoclaw_repeated_response_min_consecutive_repeats=${NANOCLAW_REPEATED_RESPONSE_MIN_CONSECUTIVE_REPEATS:-5} +nanoclaw_mask_looping_responses=${NANOCLAW_MASK_LOOPING_RESPONSES:-True} +nanoclaw_mask_only_positive_advantage=${NANOCLAW_MASK_ONLY_POSITIVE_ADVANTAGE:-True} +nanoclaw_mask_budget_exhausted_last_turn=${NANOCLAW_MASK_BUDGET_EXHAUSTED_LAST_TURN:-True} +nanoclaw_mask_duplicate_tool_result_turns=${NANOCLAW_MASK_DUPLICATE_TOOL_RESULT_TURNS:-True} +nanoclaw_mask_error_tool_result_turns=${NANOCLAW_MASK_ERROR_TOOL_RESULT_TURNS:-True} + +# verify_workplace.py 如需调用本地 OpenAI-compatible API,可用这些变量传入 reward。 +# 默认假设 5 机 40 卡:前 4 个节点加入 Ray 训练,第 5 个节点部署 verifier/vLLM API。 +verifier_api_node_rank=${VERIFIER_API_NODE_RANK:-4} +verifier_api_port=${VERIFIER_API_PORT:-8000} +verifier_api_host=${VERIFIER_API_HOST:-${MA_VJ_NAME}-${MA_TASK_NAME}-${verifier_api_node_rank}.${MA_VJ_NAME}} +verifier_api_start_cmd=${VERIFIER_API_START_CMD:-} +verifier_api_bind_host=${VERIFIER_API_BIND_HOST:-0.0.0.0} +# 9B verifier 默认使用整台 8 卡节点:两份 TP4 副本由 vLLM 内置 DP 统一服务。 +# 如需单副本 TP8,可设置 VERIFIER_API_TP=8 VERIFIER_API_DP=1。 +verifier_api_tp=${VERIFIER_API_TP:-4} +verifier_api_dp=${VERIFIER_API_DP:-2} +verifier_api_devices=${VERIFIER_API_DEVICES:-0,1,2,3,4,5,6,7} +verifier_api_distributed_executor_backend=${VERIFIER_API_DISTRIBUTED_EXECUTOR_BACKEND:-mp} +verifier_api_max_model_len=${VERIFIER_API_MAX_MODEL_LEN:-32768} +verifier_api_max_num_batched_tokens=${VERIFIER_API_MAX_NUM_BATCHED_TOKENS:-32768} +verifier_api_max_num_seqs=${VERIFIER_API_MAX_NUM_SEQS:-160} +verifier_api_gpu_memory_utilization=${VERIFIER_API_GPU_MEMORY_UTILIZATION:-0.70} +verifier_api_enforce_eager=${VERIFIER_API_ENFORCE_EAGER:-0} +verifier_api_enable_graph_mode=${VERIFIER_API_ENABLE_GRAPH_MODE:-1} +verifier_api_enable_prefix_caching=${VERIFIER_API_ENABLE_PREFIX_CACHING:-0} +verifier_api_startup_timeout=${VERIFIER_API_STARTUP_TIMEOUT:-1800} +verifier_api_log=${VERIFIER_API_LOG:-logs/vllm-verifier-api.log} +mock_api_base=${MOCK_API_BASE:-http://${verifier_api_host}:${verifier_api_port}/v1} +mock_api_key=${MOCK_API_KEY:-dummy_key} +mock_model_name=${MOCK_MODEL_NAME:-qwen3_5_9b_verifier} +# verify_workplace.py 内部 OpenAI/httpx 单次请求超时;reward API 排队时宁可多等,不要轻易误判 0 分。 +mock_api_timeout=${MOCK_API_TIMEOUT:-1800} +mock_api_connect_timeout=${MOCK_API_CONNECT_TIMEOUT:-300} +# 强制 verifier/OpenAI judge 请求关闭 thinking,sitecustomize 会自动注入 extra_body.chat_template_kwargs.enable_thinking=False。 +nanoclaw_force_no_thinking=${NANOCLAW_FORCE_NO_THINKING:-1} +nanoclaw_force_max_tokens=${NANOCLAW_FORCE_MAX_TOKENS:-50} +# 默认控制台只打一行 reward 摘要;如需每项 details,设 NANOCLAW_REWARD_PRINT_DETAILS=1。 +nanoclaw_reward_print_details=${NANOCLAW_REWARD_PRINT_DETAILS:-0} +# verifier API 是单独节点,默认低并发,避免 RewardLoopWorker 同时打爆 API 导致排队超时。 +reward_num_workers=${REWARD_NUM_WORKERS:-52} + +project_name=${PROJECT_NAME:-qwen3.5-27b_nanoclaw_grpo_verl_0720} +experiment_name=${EXPERIMENT_NAME:-qwen3.5-27b_nanoclaw_grpo_16k_verl0720_lr1e6_fixedkl1e-3} +default_local_dir=${DEFAULT_LOCAL_DIR:-$DATA_ROOT/checkpoint/$experiment_name} +start_time=$(date +%Y%m%d)_$(date +%H%M%S) +mkdir -p logs "${default_local_dir}" + +# ================= 算法与并行参数 ================= +adv_estimator=grpo +max_turns=${MAX_TURNS:-30} +max_prompt_length=${MAX_PROMPT_LENGTH:-8192} +max_response_length=${MAX_RESPONSE_LENGTH:-22768} +max_assistant_response_length=${MAX_ASSISTANT_RESPONSE_LENGTH:-16384} +max_tool_response_length=${MAX_TOOL_RESPONSE_LENGTH:-8192} +max_model_len=$((max_prompt_length + max_response_length)) + +# MindSpeed 配置仅作为训练意图参考;以下均使用最新版 VERL 的原生字段。 +actor_lr=${ACTOR_LR:-1e-6} +actor_lr_scheduler_type=${ACTOR_LR_SCHEDULER_TYPE:-constant} +actor_lr_warmup_steps_ratio=${ACTOR_LR_WARMUP_STEPS_RATIO:-0.0} +actor_weight_decay=${ACTOR_WEIGHT_DECAY:-0.01} +actor_adam_beta1=${ACTOR_ADAM_BETA1:-0.9} +actor_adam_beta2=${ACTOR_ADAM_BETA2:-0.95} +actor_clip_grad=${ACTOR_CLIP_GRAD:-1.0} +actor_ppo_epochs=${ACTOR_PPO_EPOCHS:-1} +actor_shuffle=${ACTOR_SHUFFLE:-False} +actor_entropy_coeff=${ACTOR_ENTROPY_COEFF:-0.0} +actor_clip_ratio_low=${ACTOR_CLIP_RATIO_LOW:-0.2} +actor_clip_ratio_high=${ACTOR_CLIP_RATIO_HIGH:-0.2} +# MindSpeed 配置没有 Dual-Clip PPO 对应项,保留该 27B 脚本原来的 C=10。 +actor_clip_ratio_c=${ACTOR_CLIP_RATIO_C:-10.0} + +# YAML 的 fixed init_kl_coef + low_var_kl 对应 VERL 的 reward-KL 路径。 +algorithm_gamma=${ALGORITHM_GAMMA:-1.0} +algorithm_lam=${ALGORITHM_LAM:-0.95} +use_kl_in_reward=${USE_KL_IN_REWARD:-True} +kl_penalty=${KL_PENALTY:-low_var_kl} +kl_ctrl_type=${KL_CTRL_TYPE:-fixed} +kl_coef=${KL_COEF:-0.001} +# 关闭 actor-KL,避免与 reward-KL 重复惩罚。 +actor_use_kl_loss=${ACTOR_USE_KL_LOSS:-False} +actor_kl_loss_coef=${ACTOR_KL_LOSS_COEF:-0.001} +actor_kl_loss_type=${ACTOR_KL_LOSS_TYPE:-low_var_kl} + +train_batch_size=${TRAIN_BATCH_SIZE:-64} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-16} +n_resp_per_prompt=${N_RESP_PER_PROMPT:-8} +# 先压低验证,避免验证和训练稳定性混在一起。 +n_resp_per_prompt_val=${N_RESP_PER_PROMPT_VAL:-1} +log_val_generations=${LOG_VAL_GENERATIONS:-10} + +infer_tp=${INFER_TP:-4} +train_sp=${TRAIN_SP:-1} +offload=${OFFLOAD:-True} + +# 稳定版固定沿用已经完成初始化与 rollout 验证的 FSDP1/offload 形状。 +actor_strategy=${ACTOR_STRATEGY:-fsdp} +fsdp_size=${FSDP_SIZE:-} + +actor_pack=${ACTOR_PACK:-1} +logprob_pack=${LOGPROB_PACK:-2} +actor_max_token_len_per_gpu=${ACTOR_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * actor_pack + train_sp - 1) / train_sp))} +log_prob_max_token_len_per_gpu=${LOG_PROB_MAX_TOKEN_LEN_PER_GPU:-$(((max_model_len * logprob_pack + train_sp - 1) / train_sp))} +entropy_from_logits_with_chunking=${ENTROPY_FROM_LOGITS_WITH_CHUNKING:-True} +entropy_from_logits_chunk_size=${ENTROPY_FROM_LOGITS_CHUNK_SIZE:-256} +entropy_checkpointing=${ENTROPY_CHECKPOINTING:-False} +rollout_max_num_batched_tokens=${ROLLOUT_MAX_NUM_BATCHED_TOKENS:-32384} +rollout_gpu_memory_utilization=${ROLLOUT_GPU_MEMORY_UTILIZATION:-0.60} +update_weights_bucket_mb=${UPDATE_WEIGHTS_BUCKET_MB:-8192} + +# Qwen 官方推荐:Instruct/non-thinking reasoning tasks +rollout_temperature=${ROLLOUT_TEMPERATURE:-0.6} +rollout_top_p=${ROLLOUT_TOP_P:-0.95} +rollout_top_k=${ROLLOUT_TOP_K:-20} +rollout_min_p=${ROLLOUT_MIN_P:-0.0} +rollout_presence_penalty=${ROLLOUT_PRESENCE_PENALTY:-0.0} +rollout_frequency_penalty=${ROLLOUT_FREQUENCY_PENALTY:-0.0} +rollout_repetition_penalty=${ROLLOUT_REPETITION_PENALTY:-1.0} + +echo "DEBUG: max_response_length=${max_response_length}, max_assistant_response_length=${max_assistant_response_length}, max_model_len=${max_model_len}" +echo "DEBUG: max_turns=${max_turns}" +echo "DEBUG: max_tool_response_length=${max_tool_response_length}" +echo "DEBUG: entropy_chunking=${entropy_from_logits_with_chunking}, entropy_chunk_size=${entropy_from_logits_chunk_size}, entropy_checkpointing=${entropy_checkpointing}" +echo "DEBUG: train_batch_size=${train_batch_size}, ppo_mini_batch_size=${ppo_mini_batch_size}, n=${n_resp_per_prompt}" +echo "DEBUG: train_sp=${train_sp}, infer_tp=${infer_tp}, actor_strategy=${actor_strategy}, fsdp_size=${fsdp_size:-}" +echo "DEBUG: Qwen3.5 Ulysses FLA required=${NANOCLAW_REQUIRE_FLA}, backend=${QWEN35_FLA_BACKEND} (TRAIN_SP=${train_sp})" +echo "DEBUG: actor_max_token_len_per_gpu=${actor_max_token_len_per_gpu}, log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu}" +echo "DEBUG: rollout sampling temperature=${rollout_temperature}, top_p=${rollout_top_p}, top_k=${rollout_top_k}, min_p=${rollout_min_p}, presence_penalty=${rollout_presence_penalty}, frequency_penalty=${rollout_frequency_penalty}, repetition_penalty=${rollout_repetition_penalty}" +echo "DEBUG: optimizer lr=${actor_lr}, scheduler=${actor_lr_scheduler_type}, warmup_ratio=${actor_lr_warmup_steps_ratio}, weight_decay=${actor_weight_decay}, betas=(${actor_adam_beta1},${actor_adam_beta2}), clip_grad=${actor_clip_grad}, ppo_epochs=${actor_ppo_epochs}, shuffle=${actor_shuffle}" +echo "DEBUG: KL use_in_reward=${use_kl_in_reward}, penalty=${kl_penalty}, ctrl=${kl_ctrl_type}, coef=${kl_coef}, actor_kl=${actor_use_kl_loss}" +echo "DEBUG: HCCL_BUFFSIZE=${HCCL_BUFFSIZE}, HCCL_HOST_SOCKET_PORT_RANGE=${HCCL_HOST_SOCKET_PORT_RANGE}, HCCL_NPU_SOCKET_PORT_RANGE=${HCCL_NPU_SOCKET_PORT_RANGE}" + +val_before_train=${VAL_BEFORE_TRAIN:-False} +trainer_use_v1=${TRAINER_USE_V1:-True} +test_freq=${TEST_FREQ:-5000} +save_freq=${SAVE_FREQ:-2} + +# ================= 分布式 ================= +export TOTAL_NNODES=${TOTAL_NNODES:-5} +export TRAIN_NNODES=${TRAIN_NNODES:-4} +export NNODES=${NNODES:-${TRAIN_NNODES}} +export NODE_RANK=${VC_TASK_INDEX} +export NPUS_PER_NODE=${NPUS_PER_NODE:-8} +export WORLD_SIZE=$((NPUS_PER_NODE * NNODES)) + +export MASTER_ADDR=${MA_VJ_NAME}-${MA_TASK_NAME}-0.${MA_VJ_NAME} +export MASTER_PORT=${MASTER_PORT:-6167} +export DASHBOARD_PORT=${DASHBOARD_PORT:-8191} +export RAY_PORT=${RAY_PORT:-6167} + +readonly SOCKET_IFNAME=${SOCKET_IFNAME:-eth0} +export HCCL_SOCKET_IFNAME=${HCCL_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export GLOO_SOCKET_IFNAME=${GLOO_SOCKET_IFNAME:-${SOCKET_IFNAME}} +export CURRENT_IP=$(ifconfig ${SOCKET_IFNAME} | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +export RAY_NODE_IP=${MA_CURRENT_IP:-${CURRENT_IP}} + +export ASCEND_RT_VISIBLE_DEVICES=${ASCEND_RT_VISIBLE_DEVICES:-$(seq -s, 0 $((NPUS_PER_NODE - 1)))} + +cat < [Verifier API Node] This node is reserved for vLLM/OpenAI-compatible verifier API." + echo "--> [Verifier API Node] API base: ${mock_api_base}" + export VLLM_ENABLE_GRAPH_MODE=${verifier_api_enable_graph_mode} + mkdir -p "$(dirname "${verifier_api_log}")" + if [ -n "${verifier_api_start_cmd}" ]; then + echo "--> [Verifier API Node] Running VERIFIER_API_START_CMD..." + bash -lc "${verifier_api_start_cmd}" & + verifier_api_pid=$! + else + echo "--> [Verifier API Node] Starting default vLLM verifier API..." + verifier_api_device_count=$(awk -F',' '{print NF}' <<<"${verifier_api_devices}") + verifier_api_expected_device_count=$((verifier_api_tp * verifier_api_dp)) + if [ "${verifier_api_device_count}" -ne "${verifier_api_expected_device_count}" ]; then + echo "ERROR: verifier TP*DP=${verifier_api_tp}*${verifier_api_dp}=${verifier_api_expected_device_count}, but VERIFIER_API_DEVICES=${verifier_api_devices} contains ${verifier_api_device_count} devices." >&2 + exit 2 + fi + export ASCEND_RT_VISIBLE_DEVICES=${verifier_api_devices} + verifier_api_args=( + --model "${verifier_model_path}" + --tokenizer "${verifier_model_path}" + --host "${verifier_api_bind_host}" + --port "${verifier_api_port}" + --served-model-name "${mock_model_name}" + --tensor-parallel-size "${verifier_api_tp}" + --data-parallel-size "${verifier_api_dp}" + --distributed-executor-backend "${verifier_api_distributed_executor_backend}" + --dtype bfloat16 + --max-model-len "${verifier_api_max_model_len}" + --max-num-batched-tokens "${verifier_api_max_num_batched_tokens}" + --max-num-seqs "${verifier_api_max_num_seqs}" + --gpu-memory-utilization "${verifier_api_gpu_memory_utilization}" + --trust-remote-code + ) + if [ "${verifier_api_enforce_eager}" = "1" ] || [ "${verifier_api_enforce_eager}" = "true" ] || [ "${verifier_api_enforce_eager}" = "True" ]; then + verifier_api_args+=(--enforce-eager) + fi + if [ "${verifier_api_enable_prefix_caching}" = "1" ] || [ "${verifier_api_enable_prefix_caching}" = "true" ] || [ "${verifier_api_enable_prefix_caching}" = "True" ]; then + verifier_api_args+=(--enable-prefix-caching) + fi + echo "--> [Verifier API Node] Command: python3 -m vllm.entrypoints.openai.api_server ${verifier_api_args[*]}" + python3 -m vllm.entrypoints.openai.api_server "${verifier_api_args[@]}" >"${verifier_api_log}" 2>&1 & + verifier_api_pid=$! + fi + + echo "--> [Verifier API Node] vLLM API pid=${verifier_api_pid}, log=${verifier_api_log}" + echo "--> [Verifier API Node] Waiting for ${mock_api_base}/models ..." + python3 - "${mock_api_base}/models" "${verifier_api_startup_timeout}" "${verifier_api_log}" "${verifier_api_pid}" <<'PY' +import os +import sys +import time +import urllib.request +from pathlib import Path + +url = sys.argv[1] +timeout = float(sys.argv[2]) +log_path = Path(sys.argv[3]) +pid = int(sys.argv[4]) if len(sys.argv) > 4 and sys.argv[4] else None +started = time.time() +last_error = None +while time.time() - started < timeout: + if pid is not None: + try: + os.kill(pid, 0) + except OSError: + print(f"ERROR: verifier API process exited early: pid={pid}", file=sys.stderr) + if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) + sys.exit(1) + try: + with urllib.request.urlopen(url, timeout=5) as response: + if 200 <= response.status < 300: + print(f"READY: {url}", file=sys.stderr) + sys.exit(0) + except Exception as exc: + last_error = exc + time.sleep(5) +print(f"ERROR: timed out waiting for {url}; last_error={last_error}", file=sys.stderr) +if log_path.is_file(): + print("\n".join(log_path.read_text(encoding="utf-8", errors="replace").splitlines()[-120:]), file=sys.stderr) +sys.exit(1) +PY + verifier_readiness_rc=$? + if [ "${verifier_readiness_rc}" -ne 0 ]; then + echo "ERROR: verifier API readiness check failed with rc=${verifier_readiness_rc}." >&2 + if kill -0 "${verifier_api_pid}" 2>/dev/null; then + kill "${verifier_api_pid}" 2>/dev/null || true + fi + wait "${verifier_api_pid}" 2>/dev/null || true + exit "${verifier_readiness_rc}" + fi + + echo "--> [Verifier API Node] Ready. Keeping node alive." + wait "${verifier_api_pid}" + verifier_api_rc=$? + if [ "${verifier_api_rc}" -ne 0 ]; then + echo "ERROR: verifier API exited with rc=${verifier_api_rc}; log=${verifier_api_log}" >&2 + fi + exit "${verifier_api_rc}" +fi + +export TMPDIR=/cache/ray_tmp +export HCCL_ASYNC_ERROR_HANDLING=${HCCL_ASYNC_ERROR_HANDLING:-0} + +wait_for_ray_npu_resources() { + expected_npu=$1 + timeout_seconds=${2:-900} + begin_ts=$(date +%s) + + while true; do + total_npu=$(python3 - <<'PY' 2>/dev/null +import ray + +try: + ray.init(address="auto", ignore_reinit_error=True, logging_level="ERROR") + print(int(ray.cluster_resources().get("NPU", 0))) + ray.shutdown() +except Exception: + print(0) +PY +) + total_npu=${total_npu:-0} + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + + echo "Ray NPU resources: ${total_npu}/${expected_npu}, elapsed=${elapsed}s" + ray status || true + + if [ "${total_npu}" -ge "${expected_npu}" ]; then + echo "Ray cluster is ready: ${total_npu}/${expected_npu} NPU resources registered." + break + fi + + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for Ray NPU resources: ${total_npu}/${expected_npu}" >&2 + return 1 + fi + + sleep 5 + done +} + +wait_for_verifier_api() { + api_url="${mock_api_base}/models" + timeout_seconds=${VERIFIER_API_CLIENT_WAIT_TIMEOUT:-1800} + begin_ts=$(date +%s) + last_diag_ts=0 + while true; do + verifier_check_output=$(python3 - "${api_url}" <<'PY' 2>&1 +import socket +import sys +import urllib.parse +import urllib.request + +url = sys.argv[1] +parsed = urllib.parse.urlparse(url) +host = parsed.hostname +port = parsed.port or (443 if parsed.scheme == "https" else 80) +print(f"check url={url} host={host} port={port}") +try: + infos = socket.getaddrinfo(host, port, type=socket.SOCK_STREAM) + print("dns=" + ",".join(sorted({item[4][0] for item in infos}))) +except Exception as exc: + print(f"dns_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with socket.create_connection((host, port), timeout=5): + print("tcp=ok") +except Exception as exc: + print(f"tcp_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +try: + with urllib.request.urlopen(url, timeout=10) as response: + print(f"http_status={response.status}") + raise SystemExit(0 if 200 <= response.status < 300 else 1) +except Exception as exc: + print(f"http_error={type(exc).__name__}: {exc}") + raise SystemExit(1) +PY +) + check_rc=$? + if [ "${check_rc}" = "0" ]; then + echo "Verifier API is ready: ${api_url}" + echo "${verifier_check_output}" + break + fi + now_ts=$(date +%s) + elapsed=$((now_ts - begin_ts)) + echo "Waiting for verifier API: ${api_url}, elapsed=${elapsed}s" + if [ $((now_ts - last_diag_ts)) -ge 60 ]; then + last_diag_ts=${now_ts} + echo "--- verifier API check diagnostics ---" + echo "${verifier_check_output}" + echo "--- expected verifier node: rank=${verifier_api_node_rank}, host=${verifier_api_host}, port=${verifier_api_port} ---" + echo "--- check verifier node log: ${verifier_api_log} ---" + echo "--------------------------------------" + fi + if [ "${elapsed}" -ge "${timeout_seconds}" ]; then + echo "ERROR: Timed out waiting for verifier API: ${api_url}" >&2 + echo "Last verifier API diagnostics:" >&2 + echo "${verifier_check_output}" >&2 + return 1 + fi + sleep 10 + done +} + +# ================= Nanoclaw workspace 根目录 ================= +mkdir -p "${nanoclaw_temp_root}" +if ! touch "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" 2>/dev/null; then + echo "ERROR: Cannot write NANOCLAW_TEMP_ROOT: ${nanoclaw_temp_root}" >&2 + exit 2 +fi +rm -f "${nanoclaw_temp_root}/.nanoclaw_write_test_${NODE_RANK}" || true +if [[ "${nanoclaw_temp_root}" == /tmp/* ]]; then + echo "WARNING: NANOCLAW_TEMP_ROOT is under /tmp. Multi-node reward workers may not see rollout workspaces." >&2 + echo "WARNING: Prefer a shared path, e.g. ${DATA_ROOT}/nanoclaw_workspaces" >&2 +fi +echo "DEBUG: Nanoclaw train_base_tasks=${train_base_tasks}" +echo "DEBUG: Nanoclaw val_base_tasks=${val_base_tasks}" +echo "DEBUG: Nanoclaw task_glob=${nanoclaw_task_glob}, task_ids=${nanoclaw_task_ids:-}" +echo "DEBUG: Nanoclaw temp_root=${nanoclaw_temp_root}, cleanup=${nanoclaw_cleanup_workspaces}, keep_failed=${nanoclaw_keep_failed_workspaces}" + +# ================= 生成 Ray runtime env ================= +RUNTIME_ENV_FILE=${WORK_DIR}/verl_engine_runtime_env.generated.yaml +cat > "${RUNTIME_ENV_FILE}" < [Head Node] Starting Ray Head on ${CURRENT_IP}..." + ray start --head \ + --node-ip-address=${RAY_NODE_IP} \ + --port=${RAY_PORT} \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=${DASHBOARD_PORT} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + + sleep 10 + wait_for_ray_npu_resources ${WORLD_SIZE} 900 || exit 1 + wait_for_verifier_api || exit 1 +else + echo "--> [Worker Node] Starting Ray Worker, connecting to ${MASTER_ADDR}:${RAY_PORT}..." + sleep 20 + ray start --address=${MASTER_ADDR}:${RAY_PORT} \ + --node-ip-address=${RAY_NODE_IP} \ + --resources="{\"NPU\":${NPUS_PER_NODE}}" \ + --disable-usage-stats \ + --block & + sleep 10 +fi + +# ================= 训练参数数组 ================= +training_args=( + python3 -m verl.trainer.main_ppo + +ray_kwargs.ray_init.address=auto + reward.num_workers=${reward_num_workers} + algorithm.adv_estimator=${adv_estimator} + algorithm.gamma=${algorithm_gamma} + algorithm.lam=${algorithm_lam} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_penalty=${kl_penalty} + algorithm.kl_ctrl.type=${kl_ctrl_type} + algorithm.kl_ctrl.kl_coef=${kl_coef} + data.train_files="${train_files}" + data.val_files="${test_files}" + data.return_raw_chat=True + data.return_multi_modal_inputs=False + data.image_key=images + data.shuffle=True + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation=error + data.custom_cls.path=pkg://nanoclaw_recipe.nanoclaw + data.custom_cls.name=CustomRLHFDataset + "data.tool_config_path=${tool_config_path}" + "+data.nanoclaw_task_glob=${nanoclaw_task_glob}" + "+data.nanoclaw_temp_root=${nanoclaw_temp_root}" + "+data.nanoclaw_cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+data.nanoclaw_keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+data.nanoclaw_env_builder_timeout=${nanoclaw_env_builder_timeout}" + "+data.nanoclaw_verifier_timeout=${nanoclaw_verifier_timeout}" + "+data.nanoclaw_reward_score_mode=${nanoclaw_reward_score_mode}" + "+data.nanoclaw_allow_bash=${nanoclaw_allow_bash}" + +data.apply_chat_template_kwargs.enable_thinking=True + reward.custom_reward_function.path=pkg://nanoclaw_recipe.nanoclaw + reward.custom_reward_function.name=compute_score + "+reward.custom_reward_function.reward_kwargs.cleanup_workspaces=${nanoclaw_cleanup_workspaces}" + "+reward.custom_reward_function.reward_kwargs.keep_failed_workspaces=${nanoclaw_keep_failed_workspaces}" + "+reward.custom_reward_function.reward_kwargs.verifier_timeout=${nanoclaw_verifier_timeout}" + "+reward.custom_reward_function.reward_kwargs.reward_score_mode=${nanoclaw_reward_score_mode}" + "+reward.custom_reward_function.reward_kwargs.require_final_answer=${nanoclaw_require_final_answer}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_enable=${nanoclaw_final_answer_bonus_enable}" + "+reward.custom_reward_function.reward_kwargs.final_answer_bonus_score=${nanoclaw_final_answer_bonus_score}" + "+reward.custom_reward_function.reward_kwargs.turn_penalty_only_positive_score=${nanoclaw_turn_penalty_only_positive_score}" + "+reward.custom_reward_function.reward_kwargs.assistant_turn_penalty=${nanoclaw_assistant_turn_penalty}" + "+reward.custom_reward_function.reward_kwargs.duplicate_tool_call_penalty=${nanoclaw_duplicate_tool_call_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_penalty=${nanoclaw_repeated_response_penalty}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_chars=${nanoclaw_repeated_response_min_chars}" + "+reward.custom_reward_function.reward_kwargs.repeated_response_min_consecutive_repeats=${nanoclaw_repeated_response_min_consecutive_repeats}" + "+reward.custom_reward_function.reward_kwargs.mock_api_base=${mock_api_base}" + "+reward.custom_reward_function.reward_kwargs.mock_api_key=${mock_api_key}" + "+reward.custom_reward_function.reward_kwargs.mock_model_name=${mock_model_name}" + "+reward.custom_reward_function.reward_kwargs.mock_api_timeout=${mock_api_timeout}" + "+reward.custom_reward_function.reward_kwargs.mock_api_connect_timeout=${mock_api_connect_timeout}" + actor_rollout_ref.model.path=${model_path} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True + actor_rollout_ref.actor.strategy=${actor_strategy} + actor_rollout_ref.ref.strategy=${actor_strategy} + actor_rollout_ref.actor.use_kl_loss=${actor_use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${actor_kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=${actor_kl_loss_type} + actor_rollout_ref.actor.clip_ratio_low=${actor_clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${actor_clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=${actor_clip_ratio_c} + actor_rollout_ref.actor.entropy_coeff=${actor_entropy_coeff} + actor_rollout_ref.actor.ppo_epochs=${actor_ppo_epochs} + actor_rollout_ref.actor.shuffle=${actor_shuffle} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.optim.lr_scheduler_type=${actor_lr_scheduler_type} + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=${actor_lr_warmup_steps_ratio} + actor_rollout_ref.actor.optim.weight_decay=${actor_weight_decay} + "actor_rollout_ref.actor.optim.betas=[${actor_adam_beta1},${actor_adam_beta2}]" + actor_rollout_ref.actor.optim.clip_grad=${actor_clip_grad} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_max_token_len_per_gpu} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.actor.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.actor.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.actor.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${train_sp} + actor_rollout_ref.ref.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_with_chunking=${entropy_from_logits_with_chunking} + actor_rollout_ref.ref.fsdp_config.entropy_from_logits_chunk_size=${entropy_from_logits_chunk_size} + actor_rollout_ref.ref.fsdp_config.entropy_checkpointing=${entropy_checkpointing} + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.mode=async + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.temperature=${rollout_temperature} + actor_rollout_ref.rollout.top_p=${rollout_top_p} + actor_rollout_ref.rollout.top_k=${rollout_top_k} + actor_rollout_ref.rollout.min_p=${rollout_min_p} + actor_rollout_ref.rollout.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.tensor_model_parallel_size=${infer_tp} + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=${update_weights_bucket_mb} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${log_prob_max_token_len_per_gpu} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.max_num_batched_tokens=${rollout_max_num_batched_tokens} + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.enable_prefix_caching=False + actor_rollout_ref.rollout.multi_turn.enable=True + actor_rollout_ref.rollout.multi_turn.max_user_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=${max_turns} + actor_rollout_ref.rollout.multi_turn.max_assistant_response_length=${max_assistant_response_length} + "actor_rollout_ref.rollout.multi_turn.tool_config_path=${tool_config_path}" + actor_rollout_ref.rollout.multi_turn.format=qwen3_coder + "actor_rollout_ref.rollout.multi_turn.max_tool_response_length=${max_tool_response_length}" + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_memory_utilization} + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.val_kwargs.temperature=${rollout_temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${rollout_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${rollout_top_k} + actor_rollout_ref.rollout.val_kwargs.min_p=${rollout_min_p} + actor_rollout_ref.rollout.val_kwargs.presence_penalty=${rollout_presence_penalty} + actor_rollout_ref.rollout.val_kwargs.frequency_penalty=${rollout_frequency_penalty} + actor_rollout_ref.rollout.val_kwargs.repetition_penalty=${rollout_repetition_penalty} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=${n_resp_per_prompt_val} + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.actor.fsdp_config.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.use_torch_compile=False + critic.fsdp.use_torch_compile=False + trainer.use_v1=${trainer_use_v1} + trainer.critic_warmup=0 + trainer.balance_batch=True + trainer.logger=['console','tensorboard'] + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.nnodes=${NNODES} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.val_before_train=${val_before_train} + trainer.log_val_generations=${log_val_generations} + trainer.save_freq=${save_freq} + trainer.default_local_dir=${default_local_dir} + trainer.test_freq=${test_freq} + trainer.total_epochs=10 +) + +if [ -n "${fsdp_size}" ]; then + training_args+=( + actor_rollout_ref.actor.fsdp_config.fsdp_size=${fsdp_size} + actor_rollout_ref.ref.fsdp_config.fsdp_size=${fsdp_size} + ) +fi + +if [ -n "${nanoclaw_task_ids}" ]; then + training_args+=("+data.nanoclaw_task_ids=${nanoclaw_task_ids}") +fi + +if [ -n "${nanoclaw_max_steps}" ]; then + training_args+=("+data.nanoclaw_max_steps=${nanoclaw_max_steps}") +fi + +# ================= 启动训练主进程:仅主节点执行 ================= +if [ "${NODE_RANK}" = "0" ]; then + echo "--> [Head Node] Starting VERL unified engine training..." + echo "DEBUG: runtime_env=${RUNTIME_ENV_FILE}" + echo "DEBUG: entrypoint=${training_args[*]}" + + ray job submit \ + --address="http://127.0.0.1:${DASHBOARD_PORT}" \ + --runtime-env="${RUNTIME_ENV_FILE}" \ + -- \ + "${training_args[@]}" 2>&1 | tee "logs/qwen3.5-nanoclaw-grpo-verl-engine-${start_time}.log" +else + echo "--> [Worker Node] Setup finished. Keeping node alive for Ray..." + tail -f /dev/null +fi diff --git a/verl_0720_main/paper_docs/01_nanoclaw_harness_framework.md b/verl_0720_main/paper_docs/01_nanoclaw_harness_framework.md new file mode 100644 index 0000000000000000000000000000000000000000..6998723c24edee8a9171148a4f652fbd4db7d770 --- /dev/null +++ b/verl_0720_main/paper_docs/01_nanoclaw_harness_framework.md @@ -0,0 +1,280 @@ +# NanoClaw:面向通用 Agent 强化学习的轻量级交互框架 + +现有通用 Agent 框架通常以完整的产品运行体验为目标,需要同时处理用户身份、会话管理、长期记忆、插件生态、外部服务、消息渠道、权限审批和多 Agent 编排等问题。这些能力对于真实应用十分重要,但当框架被直接用于强化学习时,也会引入大量与策略学习无关的状态和开销:同一任务可能受到历史记忆、网络服务、插件版本或后台进程的影响;每条 rollout 都需要维护复杂运行时;系统故障也难以区分究竟来自模型策略还是外部基础设施。NanoClaw 的设计目标并不是复刻一个完整的产品级 Agent,而是提取其中真正决定 Agent 学习能力的最小交互闭环,并将其转化为稳定、可验证且能够大规模并行的 RL 训练环境。 + +## 1. 相比 OpenClaw,NanoClaw 精简了什么,又保留了什么 + +OpenClaw 等通用 Agent 框架面向长期运行的真实用户场景,其能力边界通常覆盖持久化会话、用户画像、长期记忆、消息渠道、插件或技能市场、外部服务连接、后台任务、复杂权限控制以及多 Agent 协作。这些模块共同构成了完整的 Agent 产品,但并非训练模型形成工具使用和环境操作能力所必需。NanoClaw 因此主动移除了上述产品层功能,不维护跨任务的长期状态,不依赖动态插件生态,不将网络服务、消息平台或复杂编排系统纳入训练闭环,也不要求每条轨迹启动一套完整的 Agent 服务。 + +下表从训练环境的角度概括了两类框架的边界。这里比较的不是产品功能是否“有用”,而是这些功能是否应当进入每一条 RL rollout。 + +| 比较维度 | OpenClaw 等完整 Agent 框架 | NanoClaw | 对 RL 训练的直接影响 | +|---|---|---|---| +| 会话生命周期 | 面向长期用户会话,任务前后状态可以持续存在 | 每条任务是独立 episode,结束后不继承会话状态 | 避免前一条轨迹的信息进入后一条轨迹 | +| 长期记忆 | 可读取用户记忆、历史偏好和跨会话知识 | 默认不共享跨任务记忆,只使用当前任务和工作区 | 同一输入对应更稳定、可复现的初始状态 | +| 工具与技能 | 可动态安装插件、发现技能并连接第三方服务 | 使用固定且领域无关的基础工具集合 | 不同任务共享同一行动空间,减少插件版本带来的变量 | +| 外部连接 | 可访问邮件、日历、浏览器、消息平台和在线 API | 主要操作隔离工作区内的文件和受限命令环境 | 降低网络波动、认证失败和服务变化造成的奖励噪声 | +| 后台运行 | 可能包含定时任务、事件监听、子 Agent 和异步服务 | 不保留跨 episode 的后台任务或全局 Agent 状态 | rollout 之间无需协调长期进程,可以独立并行 | +| 权限与用户交互 | 需要处理用户身份、审批、渠道权限和安全策略 | 权限边界在任务工作区和受限工具中预先确定 | 减少与任务求解无关的交互分支和系统失败点 | +| 环境状态 | 状态分布在会话、数据库、插件和外部服务中 | 主要状态显式存在于独立工作区中 | 最终状态可以复制、比较、回放和验证 | +| 任务评价 | 更关注完整应用体验和用户侧结果 | verifier 直接评价最终工作区是否满足任务目标 | 可将开放式 Agent 任务转化为明确奖励 | + +上表主要说明 NanoClaw 从完整 Agent 系统中移除了哪些外围复杂性;与之对应,下表总结了框架仍然保留的 Agent 核心能力。这些能力构成了不可继续删减的最小闭环:如果去掉其中任何一项,任务就会退化为普通问答、静态函数调用或不可验证的文本生成,而不再是完整的 Agent 学习过程。 + +| 保留的核心能力 | 在 NanoClaw 中的具体表现 | 为什么必须保留 | 简单例子 | +|---|---|---|---| +| 任务理解与行动规划 | 模型根据开放式指令分析目标、约束和当前证据,再选择下一步行动 | Agent 需要把抽象目标转化为可执行步骤,而不是复现固定动作标签 | 从“只修改重试次数”推断不能改变其他配置,并决定先检查再编辑 | +| 多轮交互与错误恢复 | 工具 observation 持续返回上下文,成功结果和错误结果都会影响后续决策 | 真实任务通常无法一次完成,也无法保证第一次行动总是正确 | 精确替换失败后重新搜索位置,修改完成后再次读取确认 | +| 独立且可修改的真实环境 | 每条轨迹拥有可读取、可改变、可保存的独立工作区 | 只有真实状态变化才能区分“声称完成”和“实际完成” | `config.yaml` 中的字段真实地从 3 变为 5,而非只在回答中声称已修改 | +| 通用且可组合的原子工具 | 使用统一的发现、读取、搜索、写入、精确编辑、补丁、目录操作和受限执行工具 | 工具集必须覆盖完整任务闭环,同时避免把某类 benchmark 的解法编码为专用 API | 同一套工具可以完成代码重构、配置修复、CSV 统计、日志分析和文档生成 | +| 主动终止、状态验证与轨迹归因 | 模型主动 final,verifier 检查最终工作区,完整交互轨迹与奖励对应 | Agent 既要判断何时完成,也要允许多种正确路径获得统一、可归因的奖励 | 将“读取—编辑—复查—结束”的全部决策与最终正向奖励关联 | + +其中,通用原子工具是 NanoClaw 保留 Agent 泛化能力的关键。工具集的设计目标不是覆盖尽可能多的产品功能,而是覆盖工作区任务中不可再分的基础操作,使模型能够通过组合基础动作构造复杂行为。NanoClaw 当前工具的能力边界如下: + +| 工具 | 原子能力 | 为什么具有通用性 | 可覆盖的典型任务 | +|---|---|---|---| +| `list_dir` | 查看指定目录的直接内容或递归结构 | 几乎所有未知环境任务都需要先建立对文件布局的认识,不依赖任何特定文件格式 | 了解代码仓库结构、发现数据目录、检查交付物是否生成 | +| `find` | 按通用路径模式定位文件 | 将“寻找操作对象”从具体任务中抽象出来,同一模式可作用于代码、配置、数据和文档 | 查找所有 Python 文件、定位 YAML 配置、发现 CSV、日志或测试文件 | +| `read_file` | 读取一个文本文件的内容或受限片段 | 为任意文本型环境状态提供统一观察接口,不预设文件语义 | 阅读源代码、配置、Markdown、CSV、JSON、日志和脚本 | +| `grep` | 以正则表达式跨文件搜索内容 | 支持从大规模环境中进行稀疏证据检索,避免为代码搜索、日志搜索或字段搜索分别设计工具 | 查找函数引用、配置键、错误码、用户名、时间戳和异常模式 | +| `write_file` | 创建文件或完整写入新内容 | 统一表示“产生新交付物”,交付物类型由任务决定而非工具决定 | 生成代码、测试、报告、JSON 结果、脚本和说明文档 | +| `edit_file` | 对已知位置进行精确局部替换 | 适合最小修改并保留未涉及内容,可用于任何可文本化文件 | 修改函数名、修复一个配置值、纠正文档段落或更新数据记录 | +| `apply_patch` | 在一个或多个文件中执行成组精确修改 | 将多个局部编辑组合为一致的结构变换,同时仍不绑定具体语言或任务 | 跨文件重构、同步修改实现与测试、批量修复多处配置 | +| `mkdir` | 创建工作区目录结构 | 文件组织本身是环境状态的一部分,适用于任何需要结构化交付的任务 | 创建源码包、报告目录、数据输出目录或测试目录 | +| `bash` | 在受限工作区中运行经过校验的命令、脚本、计算和测试 | 为静态文件操作补充通用计算与执行能力,使工具集不需要为每种数据格式或编程语言新增专用 API;其能力与安全边界在下文单独讨论 | 统计 CSV、聚合日志、运行工作区脚本、执行测试和检查生成结果 | + +### 受限 Bash:通用执行能力及其安全边界 + +`bash` 是 NanoClaw 工具集中不可由普通文件工具替代的一环。`read_file` 和 `grep` 可以观察文本,`edit_file` 和 `apply_patch` 可以改变文本,但许多 Agent 任务还要求模型对环境执行真实计算,例如统计大型 CSV、聚合日志、运行已有分析脚本、检查代码语法或执行测试。如果为每一种计算分别设计 `csv_sum`、`log_counter`、`run_python_test` 等专用接口,工具数量会随着 benchmark 增长,并将任务解法直接编码进 harness。NanoClaw 因此保留了 Bash 的组合能力,但提供的不是具有宿主机权限的任意终端,而是一个以当前 rollout 工作区为根目录、经过命令和路径校验、具有时间与输出预算的受限执行工具。 + +以下命令展示了受限 Bash 在不同任务中的实际用途。它们均由同一个 `bash` 工具执行,不需要为代码、数据和日志任务分别定义 API;示例中的文件存在时,命令可以直接在 NanoClaw 工作区根目录运行。 + +| 任务目的 | 可执行示例 | Bash 提供的不可替代能力 | +|---|---|---| +| 发现待分析代码 | `find . -name '*.py'` | 在未知目录结构中进行递归发现,而不必逐层调用目录工具 | +| 定位并汇总错误日志 | `grep -R -n 'ERROR' logs | cut -d: -f1 | sort | uniq -c` | 将检索、字段提取、排序和计数组合为一次可观察的计算 | +| 统计结构化数据 | `awk -F, 'NR > 1 {sum += $4; n += 1} END {print "rows=" n, "total=" sum}' data/orders.csv` | 对较大文件进行流式计算,避免将全部数据放入模型上下文 | +| 运行通用分析程序 | `python3 scripts/analyze.py` | 让模型先生成或修改工作区脚本,再对真实输入执行复杂逻辑 | +| 验证修改是否正确 | `python3 tests/test_config.py` | 将测试结果作为新的 observation 返回,支持“修改—验证—纠错”闭环 | +| 检查输出规模 | `wc -l results/report.csv` | 用低成本环境计算验证交付物,而不是依赖模型目测或估计 | + +例如,在订单分析任务中,模型不需要逐行读取包含数万条记录的 `orders.csv`。它可以先观察文件头部,随后调用: + +```text + + + +awk -F, 'NR > 1 {sum += $4; n += 1} END {print "rows=" n, "total=" sum}' data/orders.csv + + + + +Tool: +rows=18432 total=967521.40 +``` + +该设计把确定性的扫描和算术计算交给环境,把任务理解、计算方法选择以及结果解释留给模型。一方面,它显著减少长文件进入上下文产生的 token 开销;另一方面,模型必须根据任务主动决定使用哪个命令、处理哪个文件以及如何验证结果,因此并未获得任何与特定 benchmark 绑定的捷径。 + +Bash 的通用性同时带来了必须正面处理的风险:若直接向模型开放宿主机 Shell,模型可能误删文件、启动长期后台进程、读取其他 rollout,甚至搜索 `env_builder`、`verifier` 或标准答案形成奖励作弊。NanoClaw 将这些风险分为“运行安全”和“评测防作弊”两类,并在工具入口、工作区组织和评测流程三个层面设置边界。 + +| 防护维度 | NanoClaw 的限制 | 防止的风险 | +|---|---|---| +| 命令白名单 | 仅允许文件查看与操作、文本检索、排序聚合以及工作区脚本执行等基础命令,如 `cat`、`head`、`tail`、`wc`、`ls`、`find`、`grep`、`sort`、`uniq`、`cut`、`awk`、`sed`、`tr`、`python3` 等;`curl`、`wget`、`ssh` 等不在行动空间中 | 防止模型任意启动系统程序、主动连接外部服务或扩大执行权限 | +| 工作区相对路径 | 命令中的路径操作数必须解析到当前 rollout 工作区内部;绝对路径、`..` 父目录跳转和 `~` 展开均被拒绝,路径解析还会检查符号链接解析后的实际位置 | 防止读取宿主机文件、任务定义目录、其他样本或工作区外的评测代码 | +| Shell 语法限制 | 禁止后台执行符 `&`、反引号、进程替换、命令级环境变量赋值以及 Shell 变量/命令展开;嵌套的 `bash -c` 或 `sh -c` 也不允许 | 防止通过二次解释或环境注入绕过命令与路径校验,或在 episode 结束后留下驻留进程 | +| 脚本递归校验 | 模型可以运行工作区内的 `.sh` 或 `.py` 文件,但脚本路径必须位于工作区;Shell 脚本中的每条命令仍经过白名单和路径检查,Python 脚本则拒绝绝对路径、父目录路径以及 `subprocess`、`os.system`、`requests`、`urllib`、`socket`、动态 `eval/exec`、递归删除等高风险模式 | 防止模型通过“先写脚本、再执行脚本”绕过 Bash 入口限制 | +| 进程生命周期 | 子进程以当前独立工作区为工作目录,`HOME` 与临时目录也重定向到该工作区;不允许后台任务,每次执行均具有超时控制,临时执行脚本在调用后删除 | 限制任务对宿主环境的持续影响,并避免进程跨 episode 存活 | +| Observation 预算 | 标准输出和标准错误由工具捕获,返回内容最多保留 8,000 个字符 | 防止命令一次性向模型上下文灌入无限内容,控制 rollout 成本 | +| 按任务关闭 | `allow_bash` 可以在任务级关闭;不需要通用执行的任务可以只暴露文件原子工具 | 使高风险能力遵循最小权限原则,而不是对所有任务强制开放 | +| Rollout 隔离 | 每条轨迹拥有单独的结果目录和 `workspace_after`;即使两个样本对应同一任务,它们也不会共享可写工作区 | 防止一个模型样本覆盖、读取或利用另一个样本的中间结果 | +| 构建与验证分离 | `env_builder` 在模型开始行动前由训练框架调用,`verifier` 在轨迹结束后由框架调用;二者属于环境控制面,不作为文件复制到模型的可操作工作区中,也不是模型可调用的工具 | 防止模型直接读取环境构造逻辑、验证规则或隐藏答案,再针对 verifier 制造伪结果 | + +其中,最后两项直接针对多 rollout 训练中的相互干扰与奖励作弊。NanoClaw 的数据流可以简化为: + +```text +隐藏的任务控制面 + env_builder ──生成──> rollout_i/workspace_after + ↕ + 模型 ↔ 受限工具 + ↓ + 最终工作区状态 + │ + verifier <────轨迹结束后检查────┘ +``` + +模型处于图中间的数据面,只能观察任务指令、自己的工具调用历史以及自己工作区内的文件。`env_builder` 的源文件、`verifier` 的判分程序、同一批次其他 rollout 的目录均位于这一边界之外。即使 `rm`、`mv` 或 `chmod` 等文件操作被允许,其作用路径也只能落在当前工作区:模型最坏只能破坏自己的环境并因此失去奖励,而不能修改其他样本或训练基础设施。verifier 只接收轨迹完成后的工作区进行检查,模型也不能把“调用 verifier 并观察判分规则”作为探索动作。 + +下表给出若干边界示例。其目的不是依靠提示词劝告模型保持安全,而是在工具执行前直接拒绝越界动作。 + +| 请求示例 | 执行结果 | 原因 | +|---|---|---| +| `grep -R -n 'timeout' .` | 允许 | 白名单命令,搜索范围位于当前工作区 | +| `python3 scripts/analyze.py` | 允许 | 脚本位于工作区并通过安全检查 | +| `cat /etc/passwd` | 拒绝 | 使用宿主机绝对路径 | +| `find .. -name 'verifier.py'` | 拒绝 | 试图跳转到工作区父目录 | +| `cat ../task/env_builder.py` | 拒绝 | 试图读取环境构造代码 | +| `cat $OLDPWD/verifier.py` | 拒绝 | 试图通过 Shell 变量展开隐藏越界路径 | +| `bash -c 'cat hidden_file'` | 拒绝 | 禁止嵌套 Shell 再解释命令 | +| `curl https://example.com/answer` | 拒绝 | 网络命令不在白名单中 | +| `X=1 curl https://example.com/answer` | 拒绝 | 禁止用命令前变量赋值绕过首个命令名检查 | +| `python3 -c 'import requests; ...'` | 拒绝 | Python 网络访问模式被安全检查拦截 | +| `python3 -m some_module` | 拒绝 | 禁止通过模块入口间接启动未审计程序 | +| `python3 long_job.py &` | 拒绝 | 禁止后台进程跨工具调用持续运行 | + +需要指出的是,这些机制定义的是 **Agent RL harness 的受限行动边界**,其目标是阻止模型的常规误操作、直接路径逃逸、跨样本干扰和显式 verifier hacking;它不应被表述为能够对任意恶意本地代码提供形式化安全证明。若将未知模型作为主动攻击者部署到生产环境,还应在 harness 外叠加容器、文件系统 namespace、禁网策略、系统调用过滤和独立低权限用户。论文实验所需的结论更为具体:在统一的受限工具协议下,策略不能通过正常可用动作访问 `env_builder`、`verifier` 或其他 rollout,只能依靠当前任务证据改变自己的最终工作区,并由隐藏 verifier 进行事后评分。因而,Bash 提升的是领域无关的计算与验证能力,而不是开放宿主机权限或提供奖励捷径。 + +这组工具的“较完备性”体现在对一般工作区任务闭环的覆盖,而不在于工具数量。它能够表达以下连续过程: + +```text +发现环境(list_dir / find) + → 获取证据(read_file / grep) + → 改变状态(write_file / edit_file / apply_patch / mkdir) + → 执行计算或测试(bash) + → 再次读取、搜索或执行以验证结果 + → 主动结束任务 +``` + +不同领域的任务可以复用同一套组合逻辑,而不需要增加任务专用工具: + +| 任务类型 | 一种可能的工具组合 | 工具组合所完成的闭环 | +|---|---|---| +| 代码重构 | `find → grep → read_file → apply_patch → bash` | 找到相关源文件和引用,完成跨文件修改,再运行测试或语法检查 | +| 配置修复 | `find → read_file → edit_file → read_file` | 定位并理解配置,只修改目标字段,再确认其他字段保持不变 | +| CSV 数据分析 | `list_dir → read_file → write_file → bash → read_file` | 确认输入数据,编写通用分析脚本,运行统计并检查输出报告 | +| 大型日志排查 | `find → grep → bash → write_file` | 定位日志,用模式搜索和命令聚合异常,生成结构化分析结果 | +| 文档生成 | `find → grep → read_file → write_file` | 从多个来源检索和汇总证据,生成新的报告或说明文档 | +| 代码测试与修复 | `bash → grep → read_file → edit_file → bash` | 运行测试获得失败 observation,定位错误,修改实现并重新验证 | + +这些例子说明,任务泛化不是通过不断扩充插件实现的,而是来自原子工具的可组合性。代码重构和日志分析在语义上差异很大,但都可以分解为环境发现、证据获取、状态变换和结果验证。模型需要学习的是如何依据任务和 observation 选择、排序并组合这些操作,而不是记忆某个任务对应的专用函数。因此,当任务文件结构、领域或自然语言表述发生变化时,行动空间仍然保持稳定,已经学习到的工具使用策略可以继续复用。 + +NanoClaw 对“完备”的主张也具有明确边界:它并不试图覆盖浏览器交互、实时通信、外部 SaaS 或长期个人助理等全部产品场景;在这些场景中,OpenClaw 的扩展工具和服务连接仍然必要。NanoClaw 所追求的是对**可在隔离工作区中表示、执行并验证的 Agent 任务**形成较完备的基础操作集合。在这一边界内,文件发现、内容观察、证据搜索、状态修改、结构创建、通用计算和结果验证均有对应能力,因而无需针对代码、数据、日志或文档任务分别改变 harness。 + +因此,NanoClaw 与 OpenClaw 的区别不在于是否具备 Agent 能力,而在于系统边界不同:OpenClaw 希望承载完整、长期且开放的 Agent 应用,NanoClaw 则将其中与策略学习直接相关的部分压缩为任务、环境、工具、交互和验证五个基本要素。被移除的是面向产品运行的外围复杂性,被保留的是能够产生一般性 Agent 行为的核心机制。由此,模型学习的对象不再是某个具体 Agent 产品的内部规则,而是如何理解任务、获取证据、操作环境、利用反馈并完成目标。 + +例如,对于“将 `config.yaml` 中的 `max_retries` 修改为 5”这一任务,完整 Agent 框架可能首先恢复用户会话、加载长期记忆、检查已安装技能和权限状态,再决定是否调用文件或终端工具;如果历史记忆中恰好存在同名项目,甚至可能改变本次任务的行为。NanoClaw 则始终从一个为当前 rollout 新建的工作区开始,模型只能依据当前任务、工作区文件和统一工具完成修改,最后由 verifier 检查目标字段是否为 5、其他字段是否保持不变。两者都能完成文件编辑,但前者优化的是长期 Agent 产品体验,后者刻意构造了一个边界清楚、初始状态确定、结果可验证的学习样本。 + +## 2. 为什么这种轻量化适合高效且具有泛化性的 RL 训练 + +强化学习要求对同一策略进行大量并行采样,并根据环境反馈稳定地比较不同轨迹。重型 Agent 框架中的长期记忆、外部服务和后台状态会破坏这一前提:即使输入任务相同,不同 rollout 也可能因为隐藏状态不同而得到不同结果;网络、插件或服务异常还可能被错误地归因于模型行为。NanoClaw 为每条轨迹建立独立且可复现的工作区,不共享历史记忆和中间文件,使环境差异主要来自模型自身的行动。这种明确的 episode 边界降低了奖励噪声,也使失败轨迹能够被复现、检查和重新验证。 + +NanoClaw 的主要设计选择可以与 Agent RL 中的具体困难一一对应: + +| Agent RL 中的困难 | 直接使用重型框架时的表现 | NanoClaw 的对应设计 | 训练收益 | +|---|---|---|---| +| 初始状态不一致 | 不同 rollout 可能读取不同历史记忆、会话或插件状态 | 每条轨迹从独立工作区和相同任务定义开始 | 提高样本之间的可比较性,降低奖励方差 | +| rollout 启动成本高 | 每条样本需要初始化完整服务、连接和后台组件 | 只准备任务文件、基础工具和工作区 | 更多资源可用于模型生成和参数更新 | +| 故障来源难区分 | 网络超时、认证失败或插件异常可能被计为模型失败 | 核心交互限制在本地、可控环境中 | 更容易判断失败来自策略还是环境 | +| 动作空间随任务变化 | 不同插件提供不同 API,模型容易记忆 benchmark 接口 | 所有任务共享读取、搜索、编辑和执行等通用动作 | 促进跨文件类型和跨任务迁移 | +| 正确轨迹不唯一 | 过程监督往往只接受参考步骤 | verifier 评价最终状态,不规定固定操作顺序 | 允许探索、试错和自我修正 | +| 并行轨迹相互污染 | 共享目录、记忆或后台服务会传播中间状态 | 每个 rollout 拥有独立工作区 | 轨迹可以安全分发到大量 worker | +| 失败难以复现 | 关键状态存在于外部系统或临时服务中 | 输入文件、工具事件和最终工作区均可保存 | 支持回放、误差分析和 verifier 复查 | + +轻量化还直接降低了 rollout 成本。每条样本只需要准备任务工作区和有限的工具执行环境,不需要为其维护持久化服务、数据库、消息渠道或插件进程。不同轨迹之间没有复杂依赖,可以被自然地分配到大量并行 worker 上;一条轨迹发生工具错误或任务失败时,也不会污染其他样本。训练系统的大部分资源因此可以集中于模型生成和参数更新,而不是消耗在 Agent 产品运行时上。这使得长序列、多轮工具交互和多样本探索能够在可控成本下扩展。 + +NanoClaw 的泛化性同样来自这种最小化设计。框架向模型提供的是读取、搜索、修改和执行等领域无关的原子能力,而不是针对某个 benchmark 定制的任务 API。代码修改、配置处理、文档生成、结构化数据分析和系统排查等任务虽然语义不同,却可以共享同一套行动空间和交互协议。任务差异主要由自然语言指令、初始环境和最终验证标准表达,因此扩展任务不需要为模型重新设计一套专用工具。 + +与此同时,NanoClaw 采用面向最终状态的奖励视角。Agent 任务通常不存在唯一正确轨迹:模型可以使用不同的文件检查顺序、搜索策略或编辑方法得到同样正确的结果。如果奖励依赖预先规定的操作路径,模型只能模仿有限的轨迹,难以形成真正的探索和纠错能力。NanoClaw 关注最终环境是否满足任务目标,从而允许多种有效策略获得相同正向反馈。模型能够在训练中学习更一般的“观察—规划—执行—检查—修正”能力,而不是记忆某一类任务的固定调用模板。 + +因此,效率与泛化在 NanoClaw 中并不是两个独立目标。减少隐藏状态和外围服务,一方面降低了并行采样成本并提高了奖励稳定性,另一方面也避免模型过度适应某个重型框架的产品特性;保留通用工具和开放行动路径,则使有限的运行时能够覆盖更广泛的任务分布。框架越少注入任务专用结构,策略学习就越集中于可迁移的问题求解能力。 + +以同一任务采样多个候选轨迹为例,模型可能分别采用“先完整读取再编辑”“先搜索字段再编辑”或“生成补丁后重新检查”等策略。在共享长期状态的系统中,这些轨迹可能相互看到中间结果,难以判断差异究竟来自策略还是状态污染;在 NanoClaw 中,每个候选都从相同的初始文件副本开始,并在自己的工作区中独立执行。最终,多个不同轨迹可以因为得到同样正确的文件状态而获得正向奖励,失败轨迹也可以根据其独立工作区被准确定位。这一过程同时体现了采样效率、奖励可靠性和策略多样性。 + +## 3. NanoClaw 如何嵌入 RL 训练闭环 + +在 RL 训练中,NanoClaw 被视为连接策略模型与任务环境的轻量交互层。每个训练 episode 从一条任务开始:系统根据任务定义准备一个独立工作区,并向模型提供任务指令和统一的工具能力。模型随后在多轮交互中观察环境、产生思考、选择行动并接收真实工具结果。随着交互推进,模型的每次决定及其环境反馈共同构成一条完整轨迹,直到模型主动给出最终回答,或者达到预设的交互预算。 + +从标准 RL 表述来看,NanoClaw 中各要素具有清晰对应关系: + +| RL 要素 | NanoClaw 中的具体载体 | 在轨迹中的例子 | +|---|---|---| +| 初始状态 | 用户任务、初始工作区和可用工具 | `config.yaml` 初始包含 `max_retries: 3` | +| 策略 | 当前待训练的语言模型 | 根据任务和历史 observation 决定下一步 | +| 动作 | 模型生成的工具调用或最终回答 | 调用 `read_file`、`edit_file`,或主动结束 | +| 环境 | 隔离工作区与工具执行器 | 读取文件、写入修改并返回执行结果 | +| Observation | 工具返回的真实结果 | 文件内容、搜索结果、命令输出或错误信息 | +| 轨迹 | 多轮模型输出、工具调用和 observation | “读取—编辑—复查—结束”的完整交互序列 | +| 终止条件 | 模型主动给出 final answer,或达到交互预算 | 完成修改后停止调用工具 | +| 奖励 | verifier 对最终工作区的检查结果 | 字段修改正确且其他内容未变,得到正向奖励 | +| 策略更新依据 | 奖励与该轨迹中模型输出的对应关系 | 正确轨迹被强化,低质量决策概率被抑制 | + +轨迹结束后,训练系统不要求模型复现参考操作序列,而是检查最终工作区是否达到任务要求。验证结果被转换为该 episode 的奖励,并与轨迹中的模型输出对应起来,用于后续的优势估计和策略更新。由此,NanoClaw 将开放式的 Agent 行为转化为标准 RL 可以处理的结构:策略负责产生行动,工具和工作区构成环境,环境状态变化形成 observation,最终验证器提供 reward。模型可以自由探索行动路径,但必须通过真实环境结果证明任务已经完成。 + +下面给出一个基于 NanoClaw 真实训练协议的简化 episode。示例保留了实际 system prompt 中的关键约束、实际 user prompt 包装方式、真实工具名称,以及训练时使用的 `qwen3_coder` XML 工具调用格式;为了便于阅读,仅省略完整工具 schema、运行元数据和 token 记录。假设初始工作区包含一个 `config.yaml`,任务要求只修改其中的重试次数。 + +```text +System: +You are solving a Nanoclaw workspace task inside VERL multi-turn RL. +A fresh per-rollout workspace is prepared automatically before the first +workspace tool call. +All tool paths must be relative to that workspace. Do not use absolute paths +or '..'. +Inspect files before editing them, make the smallest correct changes, then +stop with a final answer. +The verifier scores the final workspace, not your final prose answer. + +User: +Solve this task by using the workspace tools to inspect and modify files. +Only provide a final answer after the requested workspace changes are complete. + +Task: +将 config.yaml 中的 max_retries 从 3 修改为 5,其他配置保持不变。 + +Assistant: +我需要先检查文件内容,确认目标字段及其上下文,再进行局部修改。 + + + +config.yaml + + + + +Tool observation: +max_retries: 3 +timeout: 30 + +Assistant: +目标字段存在,且只需要替换这一处内容。我将使用精确编辑,避免改动其他配置。 + + + +config.yaml + + +max_retries: 3 + + +max_retries: 5 + + + + +Tool observation: +Success: applied 1 edit(s). + +Assistant: +修改已经完成。为了确认最终状态,我需要重新读取文件。 + + + +config.yaml + + + + +Tool observation: +max_retries: 5 +timeout: 30 + +Assistant final answer: +已完成配置修改。 + +Verifier: +最终工作区中 max_retries == 5,且 timeout 等其他字段保持不变,reward = 1。 +``` + +这个例子展示了 NanoClaw 嵌入 RL 后的最小闭环。模型看到的不是预先标注好的正确工具序列,而是任务、当前工作区和通用工具;每次工具返回都会成为后续决策的新 observation。模型可以选择其他有效路径,例如使用搜索工具定位字段或使用补丁工具完成修改,只要最终工作区满足验证条件,就可以获得相同奖励。训练所约束的是任务结果,而不是某条固定轨迹,因此同一套交互协议既能支持探索,也能将开放式 Agent 行为转换为稳定的 RL 信号。 + +这一嵌入方式进一步体现了轻量化带来的训练效率。NanoClaw 不替代底层分布式 RL 系统,而是作为一层薄的环境接口接入既有的采样、调度和参数更新流程。模型推理仍可由高吞吐推理引擎批量执行,轨迹仍可由分布式 worker 并行采集,NanoClaw 只负责为每条样本提供独立环境、执行工具并保存最终状态。由于 episode 之间没有长期记忆、后台服务或跨样本依赖,增加并行度时不需要同步复杂的 Agent 全局状态,扩展成本主要由模型推理本身决定。 + +最终,NanoClaw 在 RL 中形成了一个闭合且清晰的学习回路:任务定义模型需要解决的问题,通用工具限定模型可以采取的行动,隔离工作区承载可观察和可修改的环境状态,最终验证将任务完成程度转换为奖励,RL 算法再利用这些奖励更新策略。第一部分所描述的产品功能精简,使这一闭环能够稳定复制到大量独立样本;第二部分所描述的通用工具、状态隔离和最终状态奖励,则使并行采样不仅更高效,也能够支持多样化策略探索和跨任务能力迁移。NanoClaw 的作用由此不是增加一套新的重型 Agent 基础设施,而是以最小的运行时成本,为通用 Agent RL 提供真实、可验证且可扩展的环境交互基础。 diff --git a/verl_0720_main/paper_docs/02_nanoclaw_behavior_masking.md b/verl_0720_main/paper_docs/02_nanoclaw_behavior_masking.md new file mode 100644 index 0000000000000000000000000000000000000000..0a4dffc44678992cf2d6c18404d067cc2d417fb5 --- /dev/null +++ b/verl_0720_main/paper_docs/02_nanoclaw_behavior_masking.md @@ -0,0 +1,29 @@ +# Nanoclaw:面向长程 Agent 强化学习的行为感知掩码机制 + +在长程 Agent 强化学习中,最终任务奖励与局部行为质量之间往往并不完全一致。一个 rollout 可能最终完成任务并获得正优势,但其中仍包含无效循环、重复调用工具或因长度预算耗尽而被截断的回复。标准 GRPO 会依据整条轨迹的结果更新响应 token,这意味着成功轨迹中的低质量局部行为也可能得到正向强化。例如,模型虽然在前几轮已经完成关键修改,却在后续不断重复读取相同文件;只要最终 verifier 判定任务成功,这些冗余行为就可能随整条轨迹一起被提高概率。随着训练推进,这类错误信用分配会进一步增加响应长度和无效交互,消耗有限的 token 预算,并最终表现为收敛速度下降和截断率升高。 + +为解决这一问题,Nanoclaw 引入了**行为感知的非对称优势掩码机制**。该机制首先在 rollout 阶段定位具有明确退化特征的 assistant turn,并记录其 token 区间;在 GRPO 完成优势计算后,再根据 token 的优势方向决定是否将其排除在策略损失之外。设基础响应掩码为 \(m_t^{base}\),检测到的低质量行为区间集合为 \(\mathcal{B}\),token 优势为 \(\hat{A}_t\),则实际训练掩码可以写为: + +\[ +m_t = m_t^{base}\left[1-\mathbb{I}\left(t\in\mathcal{B}\ \land\ \hat{A}_t>0\right)\right]. +\] + +因此,当低质量行为位于正优势轨迹中时,对应 token 的掩码被置为 0,避免模型因为最终任务成功而强化该行为;当这些 token 的优势小于或等于 0 时,掩码仍保持有效,使负向梯度继续降低其生成概率。该机制不会删除 rollout、改变 verifier 奖励或移除模型后续决策所需的上下文,而只在 actor 更新前调整哪些 token 参与策略损失。它因而可以被理解为一种细粒度的信用分配修正:**阻断错误的正强化,同时保留正确的负向学习信号**。 + +当前 Nanoclaw 的完整掩码配置包含以下机制: + +| 掩码机制 | 识别对象 | 在训练中的作用 | +|---|---|---| +| 循环回复掩码 | assistant 回复中连续重复的文本片段或 token 序列 | 防止语言循环因轨迹得分较高而被正向强化 | +| 重复工具结果轮次掩码 | 再次产生相同工具、参数和执行结果的 assistant turn | 抑制没有带来新信息或新状态的冗余工具交互 | +| 预算耗尽末轮掩码 | 因达到 response token 上限而终止时的最后一个 assistant turn | 避免不完整、被截断的末轮输出被当作有效正样本学习 | +| 仅掩盖正优势 | 上述异常轮次中满足 \(\hat{A}_t>0\) 的有效 token | 保留异常行为的负优势信号,避免全量掩码导致模型失去纠错机会 | + +一个简单例子可以说明这种非对称设计。假设某条轨迹最终完成任务并获得正优势,但中间包含一次没有新增信息的重复 `read_file`。在未使用掩码时,这一轮与有效编辑步骤都会受到正向更新;启用掩码后,重复读取对应的正优势 token 不参与 loss,而真正推动任务完成的其他步骤仍正常学习。相反,如果一条失败轨迹中的循环回复具有负优势,相关 token 不会被掩盖,模型仍可从失败中学习降低该行为的概率。相比直接丢弃整个异常轮次,这一设计保留了更完整、更有方向性的训练信号。 + +我们在 4B 模型上进行了严格受控的消融实验。实验设置包含 `Mask-ON` 与 `Mask-OFF` 两组:前者开启上述全部行为掩码,后者关闭全部行为掩码;两组使用相同的初始模型、训练数据、优化器与学习率、batch size、rollout 数量、长度预算、奖励函数及其他训练参数。尤其地,两组实验采用相同的 data seed,使每个训练 step 接收到的数据及其 batch 组成保持一致。因此,两条曲线之间的差异不能用样本顺序不同或某一组恰好遇到更简单的 batch 来解释,主要实验变量被限制为是否启用行为掩码。 + +实验结果显示,`Mask-ON` 的任务准确率在训练早期增长更快,并在相同训练步数下持续高于 `Mask-OFF`;与此同时,`Mask-ON` 的响应截断率更低。两项指标呈现一致的机制性证据:更高且增长更快的准确率说明掩码减少了错误正强化,提高了有效策略信号的利用效率;更低的截断率说明模型减少了循环回复和无效工具交互,能够将有限上下文预算用于真正推进任务的步骤。由于两组实验逐 batch 使用相同训练数据,这一对照直接支持性能提升来自掩码机制,而不是数据采样差异。 + +从论文证据链来看,这一结论由三个层次共同支撑。首先,**变量控制**保证除 mask 开关外的训练条件完全一致;其次,**优化结果**表现为准确率曲线更快上升且整体更高;最后,**行为结果**表现为截断率同步下降,与“减少无效 token 和退化轮次”的方法动机相吻合。准确率与截断率的共同变化比单一最终分数更有解释力:如果 mask 只是偶然改变了奖励波动,并不必然同时降低截断;当前结果则表明它确实改善了策略的交互效率。正式论文中可以同时报告逐步训练曲线、达到固定准确率所需的训练步数、最终准确率及平均截断率,并进一步使用多个随机种子报告均值和方差,以补充当前同数据序列受控消融的统计稳健性。 + diff --git a/verl_0720_main/paper_docs/03_nanoclaw_reward_design.md b/verl_0720_main/paper_docs/03_nanoclaw_reward_design.md new file mode 100644 index 0000000000000000000000000000000000000000..7b62436b8c370cbec0d131e54ecc0f318353bc41 --- /dev/null +++ b/verl_0720_main/paper_docs/03_nanoclaw_reward_design.md @@ -0,0 +1,58 @@ +# Nanoclaw:面向开放式 Agent 任务的分层可验证奖励设计 + +奖励函数是 Agent 强化学习中最关键、也最容易引入偏差的组成部分。与数学问答不同,Agent 任务的答案通常不是一个可以直接精确匹配的字符串,而是由模型经过多轮推理和工具调用后形成的环境状态。同一个任务可能存在多条正确轨迹,工作区中的结果也可能具有不同但等价的表达形式。如果直接评价完整轨迹,不仅需要处理数万 token 的推理、工具调用和环境反馈,带来很高的判分成本,还会将 verifier 对操作顺序和表达风格的偏好引入奖励,压缩模型的策略探索空间。更严重的是,模型可能学习迎合轨迹评分器所偏好的表面模式,而不是真正完成任务,从而形成 reward hacking。为此,Nanoclaw 将奖励设计分为两个层次:宏观层面判断模型是否主动完成任务,并只对最终工作区评分;微观层面使用结构化的混合 verifier 判断工作区中的每项结果是否正确。 + +## 主动终止门控与最终状态奖励 + +Nanoclaw 在宏观层面采用 `require_final_answer` 机制。只有当模型主动停止工具调用,并输出一个非空的最终回答时,当前 rollout 才被视为完整结束,随后才会运行 workplace verifier;如果模型因为总 response token 数耗尽、单轮长度耗尽、最大轮数或其他非主动原因终止,则不执行正常的结果评分,奖励直接记为 0。设轨迹为 \(\tau\),最终工作区为 \(W_T\),主动完成指示变量为 \(I_{\mathrm{final}}(\tau)\),工作区验证分数为 \(V(W_T)\),则核心奖励可以表示为: + +\[ +R(\tau)=I_{\mathrm{final}}(\tau)\cdot V(W_T). +\] + +这一门控使“知道何时结束”成为 Agent 策略的一部分。仅仅在工作区中留下部分正确结果还不够,模型还需要判断任务是否已经完成,并主动从 ReAct 循环切换到最终回答。否则,如果截断轨迹也能稳定获得部分奖励,模型就缺少及时结束的动力,甚至可能通过持续输出、反复检查或堆积局部结果获得非零分数。主动终止门控因此同时提供了完成性约束和效率约束,但无需额外设计长度惩罚。 + +在通过终止门控后,奖励只取决于最终 workplace,而不评价中间轨迹。模型可以采用不同的搜索顺序、工具组合和修改方法,只要最终环境满足任务要求,就能够获得相同奖励。这种 outcome-based verification 一方面避免了对长轨迹反复调用大模型判分的资源开销,另一方面不会把某条人工轨迹误当成唯一正确解,从而保留强化学习所需的探索空间。由于 reward 绑定到可检查的环境结果,而不是模型在推理文本中声称“已经完成”,模型也更难仅通过语言表述骗取奖励。 + +## 确定性检查与 LLM 语义检查 + +在具体任务层面,每条数据包含一个 `workplace_verifier`,但 Nanoclaw 不试图用手写代码精确描述所有语义条件。复杂 Agent 任务的输出格式具有较大的合理变化空间,纯代码 verifier 很容易把数据构造者的隐含假设错误地当成任务要求。例如,prompt 可能只要求将结果保存为 `result_email.json`,验证代码却默认该 JSON 必须具有名为 `content` 的顶级字段;此时模型即使生成了语义正确、结构合理的结果,也会因为 prompt 从未声明的 schema 约束被误判。随着任务复杂度增加,枚举所有合法表达形式不仅构造成本很高,也很容易产生大量假阴性。 + +因此,我们采用“**确定性边界检查 + LLM 语义检查**”的混合式 verifier。代码只负责判断具有唯一、客观答案的结构条件,例如目标文件的路径和名称是否正确、要求的结果文件是否存在;除这些能够绝对确定的检查项外,文件内容是否满足指令、答案是否正确、字段语义是否一致等条件均交由 LLM checker 判断。该分工让程序检查承担最可靠的边界约束,让语言模型处理难以通过固定规则覆盖的语义等价性,避免用脆弱的代码 schema 替代用户真正提出的要求。 + +为了使 LLM 判分任务足够简单且稳定,每个 checker 都被改写为包含参考答案的二元判定问题。checker prompt 同时给出待检查内容、明确的判断标准和正确参考,例如: + +```text +请检查答案文本是否包含正确答案“671 元人民币”。 +已知该问题的计算结果应为 671 元人民币,因此正确答案就是 671 元。 +如果满足要求,只回答 YES;否则只回答 NO。 +``` + +这种设计不是让 verifier 重新独立求解完整 Agent 任务,而是将复杂验证分解成若干局部的文本蕴含或一致性判断。参考答案直接出现在 checker prompt 中,模型只需对照候选内容判断条件是否成立,因此即使使用速度较快、参数规模较小的模型,也能保持较高的判断精度,同时显著降低大规模 rollout 的奖励计算成本。 + +## 正向与负向双向检查 + +仅使用“正确内容是否出现”的正向检查仍可能产生 reward hacking:策略可以将大量候选答案、姓名或数值全部写入结果文件,从而碰巧覆盖正确答案。为此,Nanoclaw 将语义检查分为互补的两类。**正向检查**确认必要答案、文本或操作结果确实存在并符合要求;**负向检查**确认输出中没有出现明确禁止、与事实冲突或不应包含的内容。例如,若任务要求列表中不得包含已经付费的客人 James,可以构造如下检查: + +```text +请检查列表中是否包含已经付费的客人 James。 +如果没有出现 James,回答 YES;如果出现了 James,回答 NO。 +``` + +只有同时通过正向和负向检查,输出才既满足召回要求,又满足精确性要求。这使模型无法通过堆砌大量内容获得高分,也减少了“正确答案存在,但同时包含明显错误答案”的假阳性。最终 workplace 分数由多个独立检查项聚合而成,因而能够提供比单一通过/失败信号更细致的任务完成度,同时保持每个子判定足够简单。 + +## 实验一:主动终止门控消融 + +我们在 2B 模型上对 `require_final_answer` 进行了受控消融。`True` 与 `False` 两组实验除该开关外使用完全相同的模型、数据和训练参数。直观上,`False` 组即使在 rollout 被截断时也可以根据已完成的 workplace 获得部分分数,因此在固定轨迹分布下,其平均奖励理论上不低于带门控的 `True` 组。然而实验呈现出相反或至少不支持这一预期的结果:训练最初十余步中,`True` 组的平均奖励更高;随后两组逐渐接近,部分阶段 `True` 组仍略高。同时,`True` 组的平均输出长度明显更短。 + +这一现象说明,允许截断样本获得部分分并没有转化为更好的长期策略。所谓 `False` 组“应当得分更高”的推断隐含了两组产生相同轨迹的假设,但在强化学习中,奖励规则会反过来改变策略分布。关闭门控后,部分分削弱了模型主动结束任务的学习信号,使其更容易继续调用工具或输出直至预算耗尽;这些更长、更不完整的轨迹又降低了有效 rollout 的比例和后续训练信号质量。开启门控则建立了清晰的完成边界:只有“完成环境修改并主动结束”的轨迹才能获得奖励,从而使模型更快学会在适当时机终止。`True` 组能够以更短输出达到相当或更高的平均奖励,表明其单位生成 token 的奖励效率更高,而不是简单依靠截断惩罚压低长度。 + +这一结果还具有直接的系统意义。更短的平均输出减少了 rollout 阶段的生成 token 数和 KV cache 占用,可以提升训练吞吐;推理时也能减少无效工具调用和等待时间。对于没有主动结束的样本,门控还可以跳过昂贵的 workplace verifier 调用。因此,`require_final_answer` 同时改善了策略完成性、训练效率与部署阶段的交互效率。正式论文中可以联合报告平均奖励、任务成功率、主动结束率、截断率和平均输出长度,从而区分“分数变化”与“行为效率变化”。 + +## 实验二:小模型 Verifier 的可靠性 + +为验证训练阶段使用的小型 checker 是否足以承担语义判分,我们对模型采样产生的 workplace 进行了独立复评:训练时使用 Qwen3.5-9B verifier,实验后再使用更大规模的 GLM-5.1 对相同结果重新打分。两者判断的一致率达到 **95%**。这一结果说明,通过将复杂任务拆解为包含参考答案的 YES/NO 检查项,原本开放的结果验证已经被转化为低复杂度判别任务;因此无需在每个 rollout 中部署超大模型,也能够获得与强 verifier 高度一致的判断,并显著降低奖励计算的算力与延迟成本。 + +严格来说,95% 衡量的是 Qwen3.5-9B 与 GLM-5.1 的判分一致性,而不是在人工金标准上的绝对准确率,但它为 checker 设计的可行性提供了直接证据:如果判分问题仍需要复杂推理,小模型与更强模型通常会出现更大分歧。 + +总体而言,Nanoclaw 的奖励函数不是单一模型打分器,而是一套分层验证机制:`require_final_answer` 在轨迹层面建立主动完成门槛,最终状态奖励在策略层面保留多路径探索空间,确定性代码检查保证文件边界正确,带参考答案的 LLM checker 判断复杂语义,正负双向检测共同抑制内容堆砌式 reward hacking。2B 模型的终止门控消融和 Qwen3.5-9B/GLM-5.1 的 95% 判分一致率分别从策略学习与 verifier 可靠性两个角度支持了这一设计。 diff --git a/verl_0720_main/verl/.agent/skills/issue/SKILL.md b/verl_0720_main/verl/.agent/skills/issue/SKILL.md new file mode 100644 index 0000000000000000000000000000000000000000..84ceb8cd11e16e340d1f3bc8d74cfbc2b1db8c6b --- /dev/null +++ b/verl_0720_main/verl/.agent/skills/issue/SKILL.md @@ -0,0 +1,47 @@ +--- +name: issue +description: Create or update a GitHub issue following verl project conventions. +user_invocable: true +--- + +When the user asks to create or update an issue, follow these steps: + +### 1. Gather Context + +Read the following to understand available issue types and their required fields: + +- [`bug-report.yml`](.github/ISSUE_TEMPLATE/bug-report.yml) +- [`feature-request.yml`](.github/ISSUE_TEMPLATE/feature-request.yml) + +If updating an existing issue, read its current title, body, labels, and comments first. + +### 2. Determine Issue Type + +Based on the user's description, select the appropriate template: + +- **Bug report** ([`bug-report.yml`](.github/ISSUE_TEMPLATE/bug-report.yml)) — something is broken or behaves unexpectedly +- **Feature request** ([`feature-request.yml`](.github/ISSUE_TEMPLATE/feature-request.yml)) — a new capability or enhancement +- **Blank issue** — if neither template fits + +### 3. Compose the Issue + +Fill in the template fields based on information from the user and the codebase. For bug reports, run `python scripts/diagnose.py` to gather system info if possible. + +When updating, ensure the title and body still accurately reflect the current state of the issue. + +### 4. Check for Duplicates + +Search for existing issues before creating: + +``` +gh issue list --repo verl-project/verl --state open --search "" +``` + +If a duplicate exists, inform the user instead of creating a new one. + +### 5. Create or Update the Issue + +- **Create**: add `good first issue` and/or `call for contribution` labels if the issue is straightforward and suitable for new contributors. +- **Update**: update title, body, and labels as needed. + +Return the issue URL when done. diff --git a/verl_0720_main/verl/.agent/skills/pr/SKILL.md b/verl_0720_main/verl/.agent/skills/pr/SKILL.md new file mode 100644 index 0000000000000000000000000000000000000000..58055f617776eb5ae24fa220eaa2aa68a15476f4 --- /dev/null +++ b/verl_0720_main/verl/.agent/skills/pr/SKILL.md @@ -0,0 +1,33 @@ +--- +name: pr +description: Create or update a pull request following verl project conventions. +user_invocable: true +--- + +When the user asks to create or update a PR, follow these steps: + +### 1. Gather Context + +Read the following and understand the current branch's changes compared to main: + +- [`CONTRIBUTING.md`](CONTRIBUTING.md) +- [`PULL_REQUEST_TEMPLATE.md`](.github/PULL_REQUEST_TEMPLATE.md) + +If a PR already exists for this branch, also read its current title, body, and review comments. + +### 2. Compose PR Title and Body + +Follow the PR template strictly for both title format and body sections. Only check checklist boxes for steps that have actually been completed. + +When updating, ensure the title and body still accurately reflect **all** changes on the branch, not just the latest commit. + +### 3. Pre-submit Checks + +Run pre-commit and fix any issues before creating or pushing. + +### 4. Create or Update the PR + +- **Create**: target `main` by default unless the user specifies otherwise. +- **Update**: push new commits and update the title and body if the scope has changed. **Read the current PR title and body first** and incorporate any edits the user may have made directly on GitHub — never overwrite with a version generated from scratch. + +Return the PR URL when done. diff --git a/verl_0720_main/verl/.gemini/config.yaml b/verl_0720_main/verl/.gemini/config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..66015ad30ed6768e06dceb91f11004be8a74bb04 --- /dev/null +++ b/verl_0720_main/verl/.gemini/config.yaml @@ -0,0 +1,10 @@ +have_fun: false +code_review: + disable: false + comment_severity_threshold: HIGH + max_review_comments: -1 + pull_request_opened: + help: false + summary: false + code_review: true +ignore_patterns: [] diff --git a/verl_0720_main/verl/.git-blame-ignore-revs b/verl_0720_main/verl/.git-blame-ignore-revs new file mode 100644 index 0000000000000000000000000000000000000000..649ba3ca862e8e47a92b932b337fe189fbd14e7c --- /dev/null +++ b/verl_0720_main/verl/.git-blame-ignore-revs @@ -0,0 +1,13 @@ +# Local uasge: git config blame.ignoreRevsFile .git-blame-ignore-revs + +# [dev] feat: immigrate from yapf & pylint to ruff based on pre-commit +# Changed 268 files, +10k/-9k lines. This is the biggest formatter change. +b00f77d8559b48d57a33c0132a5ba1c81891a536 + +# [ci] refactor: reduce ruff line-length from 300 to 120 +# Changed 238 files, +6k/-1k lines. Global formatting change. +00a10a8ef389556f957a2f36132b2358fd6a109f + +# [Lint] fix: linting errors in all files +# Changed 179 files, +1k/-3k lines. Global lint fix. +8e5ad4688a13de81727c014a3c2e2fb26324bc20 diff --git a/verl_0720_main/verl/.github/CODEOWNERS b/verl_0720_main/verl/.github/CODEOWNERS new file mode 100644 index 0000000000000000000000000000000000000000..f3ce2bb33c7ebb46ea1c88d28f60a531e4fe4bec --- /dev/null +++ b/verl_0720_main/verl/.github/CODEOWNERS @@ -0,0 +1,26 @@ +/docs @eric-haibin-lin @zhaochenyang20 @hongpeng-guo +/docs/amd_tutorial @yushengsu-thu +/docs/slang_multiturn @zhaochenyang20 @SwordFaith +/docs/ascend_tutorial @wucong25 + +/third_party/sglang @zhaochenyang20 @SwordFaith +/third_party/vllm @PeterSH6 @wuxibin89 + +/examples/grpo_trainer @vermouth1992 @PeterSH6 @tardis-key @wucong25 @ji-huazhong + +/verl/single_controller @zw0610 @wuxibin89 @hongpeng-guo +/verl/trainer @eric-haibin-lin @vermouth1992 @tongyx361 @PeterSH6 +/verl/models/mcore @ISEEKYAN @vermouth1992 +/verl/models/transformers @vermouth1992 @PeterSH6 @tardis-key @wucong25 @ji-huazhong +/verl/workers/engine @eric-haibin-lin @vermouth1992 @ZihengJiang @ArronHZG +/verl/workers/roles @eric-haibin-lin @vermouth1992 @ZihengJiang +/verl/workers/engine/fsdp @eric-haibin-lin @vermouth1992 @ZihengJiang +/verl/workers/engine/veomni @wuxibin89 @Luosuu @FoolPlayer +/verl/workers/rollout/vllm_rollout @wuxibin89 @PeterSH6 @chenhaiq @ArronHZG +/verl/workers/rollout/sglang_rollout @zhaochenyang20 @SwordFaith @chenhaiq @ArronHZG +/verl/workers/engine/megatron @ISEEKYAN @vermouth1992 +/verl/experimental @wuxibin89 @ArronHZG + +/tests/single_controller @zw0610 @wuxibin89 +/tests/trainer @eric-haibin-lin @vermouth1992 @tongyx361 @PeterSH6 +/tests/workers/rollout/vllm_rollout @wuxibin89 @PeterSH6 @chenhaiq diff --git a/verl_0720_main/verl/.github/ISSUE_TEMPLATE/bug-report.yml b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/bug-report.yml new file mode 100644 index 0000000000000000000000000000000000000000..67341f4139d9a5348f3887242b7c6accf10abc7b --- /dev/null +++ b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/bug-report.yml @@ -0,0 +1,65 @@ +# modified from https://github.com/huggingface/transformers/blob/main/.github/ISSUE_TEMPLATE/bug-report.yml?plain=1 +name: "\U0001F41B Bug Report" +description: Submit a bug report to help us improve verl +labels: [ "bug" ] +body: + - type: markdown + attributes: + value: | + Thanks for taking the time to fill out this bug report! 🤗 + + - type: textarea + id: system-info + attributes: + label: System Info + description: Please share your system info with us. You can run the command `python scripts/diagnose.py` and copy-paste its output below. + placeholder: verl version, platform, python version, ... + validations: + required: true + + - type: checkboxes + id: information-scripts-examples + attributes: + label: Information + description: 'The problem arises when using:' + options: + - label: "The official example scripts" + - label: "My own modified scripts" + + - type: checkboxes + id: information-tasks + attributes: + label: Tasks + description: "The tasks I am working on are:" + options: + - label: "An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)" + - label: "My own task or dataset (give details below)" + + - type: textarea + id: reproduction + validations: + required: true + attributes: + label: Reproduction + description: | + Please provide a code sample that reproduces the problem you ran into. It can be a Colab link or just a code snippet. + Please include relevant config information with your code. + If you have code snippets, error messages, stack traces please provide them here as well. + Important! Use code tags to correctly format your code. See https://help.github.com/en/github/writing-on-github/creating-and-highlighting-code-blocks#syntax-highlighting + Do not use screenshots, as they are hard to read and (more importantly) don't allow others to copy-and-paste your code. + + placeholder: | + Steps to reproduce the behavior: + + 1. + 2. + 3. + + + - type: textarea + id: expected-behavior + validations: + required: true + attributes: + label: Expected behavior + description: "A clear and concise description of what you would expect to happen." \ No newline at end of file diff --git a/verl_0720_main/verl/.github/ISSUE_TEMPLATE/config.yml b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/config.yml new file mode 100644 index 0000000000000000000000000000000000000000..ac09e8636d7a7577999a55ffdf7095dd0b656e52 --- /dev/null +++ b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/config.yml @@ -0,0 +1,2 @@ +blank_issues_enabled: true +version: 0.1 diff --git a/verl_0720_main/verl/.github/ISSUE_TEMPLATE/feature-request.yml b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/feature-request.yml new file mode 100644 index 0000000000000000000000000000000000000000..79cdb5a0d8e63ef1f0d62fa1160ab9dbc6ec1e28 --- /dev/null +++ b/verl_0720_main/verl/.github/ISSUE_TEMPLATE/feature-request.yml @@ -0,0 +1,32 @@ +# modified from https://github.com/huggingface/transformers/blob/main/.github/ISSUE_TEMPLATE/feature-request.yml?plain=1 +name: "\U0001F680 Feature request" +description: Submit a proposal/request for a new verl feature +labels: [ "Feature request" ] +body: + - type: textarea + id: feature-request + validations: + required: true + attributes: + label: Feature request + description: | + A clear and concise description of the feature proposal. Please provide a link to the paper and code in case they exist. + + - type: textarea + id: motivation + validations: + required: true + attributes: + label: Motivation + description: | + Please outline the motivation for the proposal. Is your feature request related to a problem? e.g., I'm always frustrated when [...]. If this is related to another GitHub issue, please link here too. + + + - type: textarea + id: contribution + validations: + required: true + attributes: + label: Your contribution + description: | + Is there any way that you could help, e.g. by submitting a PR? Make sure to read the CONTRIBUTING.MD [readme](https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md) \ No newline at end of file diff --git a/verl_0720_main/verl/.github/PULL_REQUEST_TEMPLATE.md b/verl_0720_main/verl/.github/PULL_REQUEST_TEMPLATE.md new file mode 100644 index 0000000000000000000000000000000000000000..e8b722b395575a74c8518ecf0d1cc08a7119fd8d --- /dev/null +++ b/verl_0720_main/verl/.github/PULL_REQUEST_TEMPLATE.md @@ -0,0 +1,41 @@ +### What does this PR do? + +> Add **concise** overview of what this PR aims to achieve or accomplish. Reference related GitHub issues and PRs that help with the review. + +### Checklist Before Starting + +- [ ] Search for similar PRs. Paste at least one query link here: ... +- [ ] Format the PR title as `[{modules}] {type}: {description}` (This will be checked by the CI) + - `{modules}` include `fsdp`, `megatron`, `veomni`, `sglang`, `vllm`, `rollout`, `trainer`, `ci`, `training_utils`, `recipe`, `hardware`, `deployment`, `ray`, `worker`, `single_controller`, `misc`, `perf`, `model`, `algo`, `env`, `tool`, `ckpt`, `doc`, `data`, `cfg`, `reward`, `fully_async`, `one_step_off` + - If this PR involves multiple modules, separate them with `,` like `[megatron, fsdp, doc]` + - `{type}` is in `feat`, `fix`, `refactor`, `chore`, `test` + - If this PR breaks any API (CLI arguments, config, function signature, etc.), add `[BREAKING]` to the beginning of the title. + - Example: `[BREAKING][fsdp, megatron] feat: dynamic batching` + +### Test + +> For changes that can not be tested by CI (e.g., algorithm implementation, new model support), validate by experiment(s) and show results like training curve plots, evaluation results, etc. + +### API and Usage Example + +> Demonstrate how the API changes if any, and provide usage example(s) if possible. + +```python +# Add code snippet or script demonstrating how to use this +``` + +### Design & Code Changes + +> Demonstrate the high-level design if this PR is complex, and list the specific changes. + +### Checklist Before Submitting + +> [!IMPORTANT] +> Please check all the following items before requesting a review, otherwise the reviewer might deprioritize this PR for review. + +- [ ] Read the [Contribute Guide](https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md). +- [ ] Apply [pre-commit checks](https://github.com/verl-project/verl/blob/main/CONTRIBUTING.md#code-linting-and-formatting): `pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=always` +- [ ] Add / Update [the documentation](https://github.com/verl-project/verl/tree/main/docs). +- [ ] Add unit or end-to-end test(s) to [the CI workflow](https://github.com/verl-project/verl/tree/main/.github/workflows) to cover all the code. If not feasible, explain why: ... +- [ ] Once your PR is ready for CI, send a message in [the `ci-request` channel](https://verl-project.slack.com/archives/C091TCESWB1) in [the `verl` Slack workspace](https://join.slack.com/t/verl-project/shared_invite/zt-3855yhg8g-CTkqXu~hKojPCmo7k_yXTQ). (If not accessible, please try [the Feishu group (飞书群)](https://applink.larkoffice.com/client/chat/chatter/add_by_link?link_token=772jd4f1-cd91-441e-a820-498c6614126a).) +- [ ] If your PR is related to the `recipe` submodule, please also update the reference to the submodule commit via `git submodule update --remote` or `cd recipe && git pull origin main`. diff --git a/verl_0720_main/verl/.github/dependabot.yml b/verl_0720_main/verl/.github/dependabot.yml new file mode 100644 index 0000000000000000000000000000000000000000..24a3571c620c9c1e5ef7b4011851124c7a020626 --- /dev/null +++ b/verl_0720_main/verl/.github/dependabot.yml @@ -0,0 +1,9 @@ +## Enabled the dependabot to check the dependencies of the project +## Dependabot will open pull requests to update dependencies automatically + +version: 2 +updates: + - package-ecosystem: pip + directory: "/" + schedule: + interval: weekly \ No newline at end of file diff --git a/verl_0720_main/verl/.github/workflows/.stash/e2e_ppo_trainer_torchtitan_vllm.yml b/verl_0720_main/verl/.github/workflows/.stash/e2e_ppo_trainer_torchtitan_vllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..674442cc57bf26e9c00755f50de1d68163b29541 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/.stash/e2e_ppo_trainer_torchtitan_vllm.yml @@ -0,0 +1,171 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +# NOTE: The TorchTitan engine requires a *nightly* PyTorch + torchtitan (the ``spmd_types`` APIs +# are nightly-only). To avoid maintaining a dedicated Docker image, this workflow installs those +# nightlies in-job on top of the standard CI image (see the install step). Because the image's +# vLLM is built against a *stable* torch, the run step preloads the nightly cuBLAS to resolve the +# resulting ``undefined symbol: cublasGemmEx`` mismatch. The pinned nightly versions need periodic +# bumping (old nightly wheels are garbage-collected from the index). Consider running this on a +# schedule / as non-blocking if the nightly toolchain proves flaky. + +name: e2e_ppo_trainer_torchtitan_vllm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + push: + branches: + - main + - v0.* + paths: + - "verl/workers/engine/torchtitan/**" + - "verl/workers/config/engine.py" + pull_request: + branches: + - main + - v0.* + paths: + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_torchtitan_vllm.yml" + - "tests/special_e2e/run_ppo_trainer_torchtitan.sh" + - "verl/workers/engine/torchtitan/**" + - "verl/workers/config/engine.py" + - "examples/data_preprocess/gsm8k.py" + - "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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + e2e_ppo_trainer_torchtitan_vllm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + TorchTitan nightly stack + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + # TorchTitan's spmd_types APIs require a nightly PyTorch; the image's vLLM is built against + # a stable torch (ABI-incompatible -> undefined symbol: torch::Library::_def). Install a + # nightly-torch stack from the PyTorch index. Why these versions: + # vllm 1.0.0.dev20260620 : newest nightly-torch vLLM whose API verl's rollout code + # supports (0704+ refactored vllm.utils and dropped the + # `FlexibleArgumentParser` import path verl relies on). + # torch 2.14.0.dev20260625 : >= the spmd_types DTensor/fully_shard fix floor, and + # ~5 days from vLLM's dev0620 build -> ABI-compatible. + # torchvision 0.29.0.dev20260626 : pins torch dev0625 exactly (0-day ABI gap). + # torchtitan 0.1.0.dev20260701 : provides spmd_backend; declares no torch dep. + # vLLM pins torch==dev0620, so install it (with deps) first, then bump torch/torchvision to + # the versions above with --no-deps (keeps the ABI-safe gap, preserves vLLM's dep set). + INDEX="https://download.pytorch.org/whl/nightly/cu130" + pip3 install --pre vllm==1.0.0.dev20260620+cu130 --extra-index-url "${INDEX}" + pip3 install --pre torchtitan==0.1.0.dev20260701+cu130 --extra-index-url "${INDEX}" + pip3 install --pre --no-deps \ + torch==2.14.0.dev20260625+cu130 \ + torchvision==0.29.0.dev20260626+cu130 \ + --extra-index-url "${INDEX}" + # The image ships classic flash-attn 2 (flash_attn_2_cuda.so) built against stable torch; + # under nightly torch it fails to load (undefined symbol: c10::...materialize_cow_storage). + # vLLM's rotary-embedding path imports flash_attn unconditionally and crashes on the broken + # .so. Remove it so vLLM falls back to its native rotary kernel. + pip3 uninstall -y flash-attn + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset21 + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training test on 8 L20 GPUs with TorchTitan engine (FSDP_SIZE=8, spmd_types, flex, selective AC) + run: | + ray stop --force + # vLLM in the base image is built against stable torch; preload the nightly cuBLAS so + # its C extensions resolve against the nightly torch (fixes undefined symbol cublasGemmEx). + export LD_PRELOAD="$(python3 -c 'import glob, os, sysconfig; c = glob.glob(os.path.join(sysconfig.get_paths()["purelib"], "nvidia", "cu13", "lib", "libcublas.so.13")); print(c[0] if c else "")')" + NUM_GPUS=8 FSDP_SIZE=8 TOTAL_TRAIN_STEPS=2 \ + bash tests/special_e2e/run_ppo_trainer_torchtitan.sh + + cleanup: + runs-on: ubuntu-latest + needs: + [ + setup, + e2e_ppo_trainer_torchtitan_vllm, + ] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/README.md b/verl_0720_main/verl/.github/workflows/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9a0097b847ba5004913cd3924dacb6e8f002f07e --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/README.md @@ -0,0 +1,73 @@ +### Adding a New Workflow + +When adding a new workflow for continuous integration (CI), you have two runner options: a fixed runner or a machine from the vemlp. + +- **Fixed Runner**: To use a fixed runner, specify it in your workflow using the `runs-on` keyword, like `runs-on: [L20x8]`. +- **Vemlp Runner**: Opting for a Vemlp machine allows you to launch tasks elastically. + +Here is a template to assist you. This template is designed for using Vemlp machines. Currently, for each workflow, you need to create a `setup` and a `cleanup` job. When using this template, the main parts you need to modify are the `IMAGE` environment variable and the specific `job steps`. + +```yaml +name: Your Default Workflow + +on: + push: + branches: + - main + - v0.* + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - ".github/workflows/template.yml" + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + +permissions: + contents: read + +env: + IMAGE: "your vemlp image" # e.g. "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_URL: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" # public veFaas api + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + task-id: ${{ steps.create-runner.outputs.task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_URL }}" + image: "${{ env.DEFAULT_IMAGE }}" + + your_job: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'default-runner' }}"] + steps: + xxxx # your jobs + + cleanup: + runs-on: ubuntu-latest + needs: [setup, your_job] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_URL }}" + task-id: "${{ needs.setup.outputs.task-id }}" +``` + +### Model and Dataset +To avoid CI relies on network, we pre-download dataset on a NFS on the CI machine. The path for models are \${HOME}/models and the path for dataset is \${HOME}/models/hf_data. \ No newline at end of file diff --git a/verl_0720_main/verl/.github/workflows/check-pr-title.yml b/verl_0720_main/verl/.github/workflows/check-pr-title.yml new file mode 100644 index 0000000000000000000000000000000000000000..948ce5e3f01498ce4f569230cdb2dd384fc0cbfd --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/check-pr-title.yml @@ -0,0 +1,58 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + + +on: + pull_request: + types: [opened, edited, synchronize] + +jobs: + check-title: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.11' + + - name: Run PR title checker + run: python3 tests/special_sanity/check_pr_title.py + env: + PR_TITLE: ${{ github.event.pull_request.title }} + + - name: Run PR description checker + run: python3 tests/special_sanity/check_pr_description.py + env: + PR_TITLE: ${{ github.event.pull_request.title }} + GITHUB_EVENT_PATH: ${{ github.event_path }} diff --git a/verl_0720_main/verl/.github/workflows/cpu_unit_tests.yml b/verl_0720_main/verl/.github/workflows/cpu_unit_tests.yml new file mode 100644 index 0000000000000000000000000000000000000000..42be901f4de256e9ebd12ef0ae8613fd31ff334a --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/cpu_unit_tests.yml @@ -0,0 +1,121 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: cpu_unit_tests + +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" + - "!tests/special_sanity/**" + - .github/workflows/cpu_unit_tests.yml + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + cpu_unit_tests: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 20 # Increase this timeout value as needed + env: + 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 + TORCH_COMPILE_DISABLE: 1 + TORCHINDUCTOR_DISABLE: 1 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + - name: Download datasets + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k + - name: Running CPU unit tests + run: | + echo '[pytest]' > pytest.ini + echo 'python_files = *_on_cpu.py' >> pytest.ini + pytest -s -x --asyncio-mode=auto tests/ + cleanup: + runs-on: ubuntu-latest + needs: [setup, cpu_unit_tests] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/doc.yml b/verl_0720_main/verl/.github/workflows/doc.yml new file mode 100644 index 0000000000000000000000000000000000000000..ae3af724bbd972b55aa81696acf8903fa34ce55c --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/doc.yml @@ -0,0 +1,103 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + + +name: doc_test + +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" + - "docs/**" + - .github/workflows/doc.yml + +# 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 # for checkout + pages: write # for deploy-pages + id-token: write # for deploy-pages + +jobs: + doc_test: + runs-on: ubuntu-latest + timeout-minutes: 5 # Increase this timeout value as needed + strategy: + matrix: + python-version: ["3.10"] + 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: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip install -r docs/requirements-docs.txt + - name: Check final pip list + run: | + pip3 list + - name: Run doc make html + run: | + cd docs + make clean + make html SPHINXOPTS="--keep-going -w _build/sphinx.log" + if grep -q ": ERROR:" _build/sphinx.log; then + echo "🚨 Sphinx doc build contained ERRORs - see _build/sphinx.log" + exit 1 + fi + if grep -q "WARNING: document isn't included in any toctree" _build/sphinx.log; then + echo "🚨 Sphinx doc build contained WARNING. Please include newly added docs in index.rst. See _build/sphinx.log for details" + exit 1 + fi + if grep -q "WARNING: Inline emphasis" _build/sphinx.log; then + echo "🚨 Sphinx doc build contained WARNING. Please check inline emphasis is correct. See _build/sphinx.log for details" + exit 1 + fi + if grep -q "WARNING: Definition list ends without a blank line" _build/sphinx.log; then + echo "🚨 Sphinx doc build contained WARNING. Please check if the indentation is correct. See _build/sphinx.log for details" + exit 1 + fi diff --git a/verl_0720_main/verl/.github/workflows/docker-build-ascend-a2.yml b/verl_0720_main/verl/.github/workflows/docker-build-ascend-a2.yml new file mode 100644 index 0000000000000000000000000000000000000000..996623030988164d61ef5df1a8e009bb20b71e22 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/docker-build-ascend-a2.yml @@ -0,0 +1,274 @@ +name: docker-build-ascend-a2 + +on: + workflow_dispatch: + push: + branches: ["main"] + paths: + - "docker/ascend/Dockerfile.ascend_9.0.0_a2" + - "docker/ascend/Dockerfile.ascend_9.0.0_a2_v0.8.0" + - ".github/workflows/docker-build-ascend-a2.yml" + + schedule: + - cron: "0 16 * * *" + +env: + QUAY_REPO: quay.io/ascend/verl + +jobs: + # ============================================================ + # 第一阶段:分架构构建并推送 digest(main 镜像) + # ============================================================ + build-push-digest: + name: build-${{ matrix.tag }} + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.tag }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + runs-on: ${{ matrix.runner }} + permissions: + packages: write + contents: read + strategy: + fail-fast: false + matrix: + include: + - arch: linux/amd64 + runner: ubuntu-latest + tag: amd64 + - arch: linux/arm64 + runner: ubuntu-22.04-arm + tag: arm64 + outputs: + tag: ${{ steps.version.outputs.tag }} + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Get base image name and tag + id: base_image + run: | + BASE_IMAGE_FULL=$(grep '^FROM' ./docker/ascend/Dockerfile.ascend_9.0.0_a2 | head -1 | cut -d' ' -f2) + echo "Base image full: $BASE_IMAGE_FULL" + BASE_IMAGE_TAG=$(echo "$BASE_IMAGE_FULL" | cut -d':' -f2) + echo "Base image tag: $BASE_IMAGE_TAG" + NEW_IMAGE_NAME="verl-$BASE_IMAGE_TAG" + echo "New image name: $NEW_IMAGE_NAME" + echo "base_image_tag=$BASE_IMAGE_TAG" >> "$GITHUB_OUTPUT" + echo "new_image_name=$NEW_IMAGE_NAME" >> "$GITHUB_OUTPUT" + + - name: Get image tag + id: version + run: | + echo "tag=latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm" >> "$GITHUB_OUTPUT" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + install: true + driver: docker-container + use: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push by digest (main image) + uses: docker/build-push-action@v6 + id: build_main + with: + context: . + platforms: ${{ matrix.arch }} + file: ./docker/ascend/Dockerfile.ascend_9.0.0_a2 + push: true + outputs: type=image,name=${{ env.QUAY_REPO }},push-by-digest=true,name-canonical=true,push=true + provenance: false + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 + + - name: Export digest (main image) + run: | + mkdir -p ${{ runner.temp }}/digests + digest="${{ steps.build_main.outputs.digest }}" + touch "${{ runner.temp }}/digests/main-${digest#sha256:}" + + - name: Upload digest (main image) + uses: actions/upload-artifact@v4 + with: + name: digests-main-${{ matrix.tag }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 + + # ============================================================ + # 第一阶段(v0.8.0):分架构构建并推送 digest + # ============================================================ + build-push-digest-v080: + name: build-v0.8.0-${{ matrix.tag }} + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-v0.8.0-${{ matrix.tag }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + runs-on: ${{ matrix.runner }} + permissions: + packages: write + contents: read + strategy: + fail-fast: false + matrix: + include: + - arch: linux/amd64 + runner: ubuntu-latest + tag: amd64 + - arch: linux/arm64 + runner: ubuntu-22.04-arm + tag: arm64 + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + install: true + driver: docker-container + use: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push by digest (v0.8.0 image) + uses: docker/build-push-action@v6 + id: build_v080 + with: + context: . + platforms: ${{ matrix.arch }} + file: ./docker/ascend/Dockerfile.ascend_9.0.0_a2_v0.8.0 + push: true + outputs: type=image,name=${{ env.QUAY_REPO }},push-by-digest=true,name-canonical=true,push=true + provenance: false + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 + + - name: Export digest (v0.8.0 image) + run: | + mkdir -p ${{ runner.temp }}/digests-v080 + digest="${{ steps.build_v080.outputs.digest }}" + touch "${{ runner.temp }}/digests-v080/v080-${digest#sha256:}" + + - name: Upload digest (v0.8.0 image) + uses: actions/upload-artifact@v4 + with: + name: digests-v080-${{ matrix.tag }} + path: ${{ runner.temp }}/digests-v080/* + if-no-files-found: error + retention-days: 1 + + # ============================================================ + # 第二阶段:合并多架构 manifest(main 镜像) + # ============================================================ + merge-image: + name: merge + runs-on: ubuntu-latest + needs: build-push-digest + steps: + - name: Download digests (main image) + uses: actions/download-artifact@v4 + with: + path: ${{ runner.temp }}/digests-main + pattern: digests-main-* + merge-multiple: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Merge and push multi-arch image (main) + env: + IMAGE: ${{ env.QUAY_REPO }} + run: | + DIGESTS=$(printf "$IMAGE@sha256:%s " $(ls ${{ runner.temp }}/digests-main | sed 's/^main-//')) + echo "Main image digests: $DIGESTS" + docker buildx imagetools create \ + -t "$IMAGE:${{ needs.build-push-digest.outputs.tag }}" \ + $DIGESTS + + # ============================================================ + # 第二阶段(v0.8.0):合并多架构 manifest + # ============================================================ + merge-image-v080: + name: merge-v0.8.0 + runs-on: ubuntu-latest + needs: build-push-digest-v080 + steps: + - name: Download digests (v0.8.0 image) + uses: actions/download-artifact@v4 + with: + path: ${{ runner.temp }}/digests-v080 + pattern: digests-v080-* + merge-multiple: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Merge and push multi-arch image (v0.8.0) + env: + IMAGE: ${{ env.QUAY_REPO }} + run: | + DIGESTS=$(printf "$IMAGE@sha256:%s " $(ls ${{ runner.temp }}/digests-v080 | sed 's/^v080-//')) + echo "v0.8.0 image digests: $DIGESTS" + docker buildx imagetools create \ + -t "$IMAGE:v0.8.0-cann9.0.0-torch2.9.0post2-910b-ubuntu22.04-py3.11-vllm" \ + $DIGESTS diff --git a/verl_0720_main/verl/.github/workflows/docker-build-ascend-a3.yml b/verl_0720_main/verl/.github/workflows/docker-build-ascend-a3.yml new file mode 100644 index 0000000000000000000000000000000000000000..6f7963a1f559cad55364054453e847466a0cb91d --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/docker-build-ascend-a3.yml @@ -0,0 +1,274 @@ +name: docker-build-ascend-a3 + +on: + workflow_dispatch: + push: + branches: ["main"] + paths: + - "docker/ascend/Dockerfile.ascend_9.0.0_a3" + - "docker/ascend/Dockerfile.ascend_9.0.0_a3_v0.8.0" + - ".github/workflows/docker-build-ascend-a3.yml" + + schedule: + - cron: "0 19 * * *" + +env: + QUAY_REPO: quay.io/ascend/verl + +jobs: + # ============================================================ + # 第一阶段:分架构构建并推送 digest(main 镜像) + # ============================================================ + build-push-digest: + name: build-${{ matrix.tag }} + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-${{ matrix.tag }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + runs-on: ${{ matrix.runner }} + permissions: + packages: write + contents: read + strategy: + fail-fast: false + matrix: + include: + - arch: linux/amd64 + runner: ubuntu-latest + tag: amd64 + - arch: linux/arm64 + runner: ubuntu-22.04-arm + tag: arm64 + outputs: + tag: ${{ steps.version.outputs.tag }} + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Get base image name and tag + id: base_image + run: | + BASE_IMAGE_FULL=$(grep '^FROM' ./docker/ascend/Dockerfile.ascend_9.0.0_a3 | head -1 | cut -d' ' -f2) + echo "Base image full: $BASE_IMAGE_FULL" + BASE_IMAGE_TAG=$(echo "$BASE_IMAGE_FULL" | cut -d':' -f2) + echo "Base image tag: $BASE_IMAGE_TAG" + NEW_IMAGE_NAME="verl-$BASE_IMAGE_TAG" + echo "New image name: $NEW_IMAGE_NAME" + echo "base_image_tag=$BASE_IMAGE_TAG" >> "$GITHUB_OUTPUT" + echo "new_image_name=$NEW_IMAGE_NAME" >> "$GITHUB_OUTPUT" + + - name: Get image tag + id: version + run: | + echo "tag=latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm" >> "$GITHUB_OUTPUT" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + install: true + driver: docker-container + use: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push by digest (main image) + uses: docker/build-push-action@v6 + id: build_main + with: + context: . + platforms: ${{ matrix.arch }} + file: ./docker/ascend/Dockerfile.ascend_9.0.0_a3 + push: true + outputs: type=image,name=${{ env.QUAY_REPO }},push-by-digest=true,name-canonical=true,push=true + provenance: false + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 + + - name: Export digest (main image) + run: | + mkdir -p ${{ runner.temp }}/digests + digest="${{ steps.build_main.outputs.digest }}" + touch "${{ runner.temp }}/digests/main-${digest#sha256:}" + + - name: Upload digest (main image) + uses: actions/upload-artifact@v4 + with: + name: digests-main-${{ matrix.tag }} + path: ${{ runner.temp }}/digests/* + if-no-files-found: error + retention-days: 1 + + # ============================================================ + # 第一阶段(v0.8.0):分架构构建并推送 digest + # ============================================================ + build-push-digest-v080: + name: build-v0.8.0-${{ matrix.tag }} + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-v0.8.0-${{ matrix.tag }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + runs-on: ${{ matrix.runner }} + permissions: + packages: write + contents: read + strategy: + fail-fast: false + matrix: + include: + - arch: linux/amd64 + runner: ubuntu-latest + tag: amd64 + - arch: linux/arm64 + runner: ubuntu-22.04-arm + tag: arm64 + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + with: + install: true + driver: docker-container + use: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push by digest (v0.8.0 image) + uses: docker/build-push-action@v6 + id: build_v080 + with: + context: . + platforms: ${{ matrix.arch }} + file: ./docker/ascend/Dockerfile.ascend_9.0.0_a3_v0.8.0 + push: true + outputs: type=image,name=${{ env.QUAY_REPO }},push-by-digest=true,name-canonical=true,push=true + provenance: false + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 + + - name: Export digest (v0.8.0 image) + run: | + mkdir -p ${{ runner.temp }}/digests-v080 + digest="${{ steps.build_v080.outputs.digest }}" + touch "${{ runner.temp }}/digests-v080/v080-${digest#sha256:}" + + - name: Upload digest (v0.8.0 image) + uses: actions/upload-artifact@v4 + with: + name: digests-v080-${{ matrix.tag }} + path: ${{ runner.temp }}/digests-v080/* + if-no-files-found: error + retention-days: 1 + + # ============================================================ + # 第二阶段:合并多架构 manifest(main 镜像) + # ============================================================ + merge-image: + name: merge + runs-on: ubuntu-latest + needs: build-push-digest + steps: + - name: Download digests (main image) + uses: actions/download-artifact@v4 + with: + path: ${{ runner.temp }}/digests-main + pattern: digests-main-* + merge-multiple: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Merge and push multi-arch image (main) + env: + IMAGE: ${{ env.QUAY_REPO }} + run: | + DIGESTS=$(printf "$IMAGE@sha256:%s " $(ls ${{ runner.temp }}/digests-main | sed 's/^main-//')) + echo "Main image digests: $DIGESTS" + docker buildx imagetools create \ + -t "$IMAGE:${{ needs.build-push-digest.outputs.tag }}" \ + $DIGESTS + + # ============================================================ + # 第二阶段(v0.8.0):合并多架构 manifest + # ============================================================ + merge-image-v080: + name: merge-v0.8.0 + runs-on: ubuntu-latest + needs: build-push-digest-v080 + steps: + - name: Download digests (v0.8.0 image) + uses: actions/download-artifact@v4 + with: + path: ${{ runner.temp }}/digests-v080 + pattern: digests-v080-* + merge-multiple: true + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Merge and push multi-arch image (v0.8.0) + env: + IMAGE: ${{ env.QUAY_REPO }} + run: | + DIGESTS=$(printf "$IMAGE@sha256:%s " $(ls ${{ runner.temp }}/digests-v080 | sed 's/^v080-//')) + echo "v0.8.0 image digests: $DIGESTS" + docker buildx imagetools create \ + -t "$IMAGE:v0.8.0-cann9.0.0-torch2.9.0post2-a3-ubuntu22.04-py3.11-vllm" \ + $DIGESTS diff --git a/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a2.yml b/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a2.yml new file mode 100644 index 0000000000000000000000000000000000000000..3b685bbb14dbac1b6dc8af2a7cfa8813ab137d6e --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a2.yml @@ -0,0 +1,78 @@ +name: docker-build-ascend-sglang-a2 + +on: + workflow_dispatch: + push: + branches: ["main"] + paths: + - "docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2" + - ".github/workflows/docker-build-ascend-sglang-a2.yml" + + schedule: + - cron: "0 16 * * *" + +jobs: + build-ascend-sglang-image-a2: + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + runs-on: ubuntu-latest + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-build-ascend-sglang-image-a2 + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Get base image name and tag + id: base_image + run: | + BASE_IMAGE_FULL=$(grep '^FROM' ./docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2 | head -1 | cut -d' ' -f2) + echo "Base image full: $BASE_IMAGE_FULL" + BASE_IMAGE_TAG=$(echo "$BASE_IMAGE_FULL" | cut -d':' -f2) + echo "Base image tag: $BASE_IMAGE_TAG" + NEW_IMAGE_NAME="verl-sglang-$BASE_IMAGE_TAG" + echo "New image name: $NEW_IMAGE_NAME" + echo "base_image_tag=$BASE_IMAGE_TAG" >> "$GITHUB_OUTPUT" + echo "new_image_name=$NEW_IMAGE_NAME" >> "$GITHUB_OUTPUT" + + - name: Get image tag + id: version + run: | + echo "tag=latest-cann8.5.0-torch_npu2.8.0post2-910b-ubuntu22.04-py3.11-sglang" >> "$GITHUB_OUTPUT" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push images Quay + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + file: ./docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2 + push: true + tags: | + quay.io/ascend/verl:${{ steps.version.outputs.tag }} + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 diff --git a/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a3.yml b/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a3.yml new file mode 100644 index 0000000000000000000000000000000000000000..9eee0e92fbdca1a1cdd584df6c366a3dd2645efe --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/docker-build-ascend-sglang-a3.yml @@ -0,0 +1,78 @@ +name: docker-build-ascend-sglang-a3 + +on: + workflow_dispatch: + push: + branches: ["main"] + paths: + - "docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3" + - ".github/workflows/docker-build-ascend-sglang-a3.yml" + + schedule: + - cron: "0 16 * * *" + +jobs: + build-ascend-sglang-image-a3: + if: ${{ github.event_name != 'pull_request' && github.repository_owner == 'verl-project' }} + runs-on: ubuntu-latest + concurrency: + group: ${{ github.workflow }}-${{ github.ref }}-build-ascend-sglang-image-a3 + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + steps: + - name: Remove unnecessary parts in github actions runners to free up disk space + uses: jlumbroso/free-disk-space@v1.3.1 + with: + tool-cache: true + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.11" + + - name: Get base image name and tag + id: base_image + run: | + BASE_IMAGE_FULL=$(grep '^FROM' ./docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3 | head -1 | cut -d' ' -f2) + echo "Base image full: $BASE_IMAGE_FULL" + BASE_IMAGE_TAG=$(echo "$BASE_IMAGE_FULL" | cut -d':' -f2) + echo "Base image tag: $BASE_IMAGE_TAG" + NEW_IMAGE_NAME="verl-sglang-$BASE_IMAGE_TAG" + echo "New image name: $NEW_IMAGE_NAME" + echo "base_image_tag=$BASE_IMAGE_TAG" >> "$GITHUB_OUTPUT" + echo "new_image_name=$NEW_IMAGE_NAME" >> "$GITHUB_OUTPUT" + + - name: Get image tag + id: version + run: | + echo "tag=latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang" >> "$GITHUB_OUTPUT" + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Quay.io + uses: docker/login-action@v3 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Clean Docker cache before build + run: | + docker system prune -a -f --volumes || true + + - name: Build and push images Quay + uses: docker/build-push-action@v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + file: ./docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3 + push: true + tags: | + quay.io/ascend/verl:${{ steps.version.outputs.tag }} + cache-from: type=gha + cache-to: type=gha,mode=max + build-args: | + BUILDKIT_INLINE_CACHE=1 diff --git a/verl_0720_main/verl/.github/workflows/e2e_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..abb59f00508234c09389cad0ef174520ba0242a8 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ascend.yml @@ -0,0 +1,245 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.* + pull_request: + branches: + - main + paths: + - ".github/workflows/e2e_ascend.yml" + - "examples/data_preprocess/**" + - "examples/grpo_trainer/**" + - "examples/ppo_trainer/**" + - "examples/sft/**" + - "verl/experimental/one_step_off_policy/**" + - "tests/special_npu/**" + - "tests/special_sanity/check_device_api_usage.py" + - "verl/**" + - "pyproject.toml" + - "requirements-npu.txt" + - "setup.py" + +# 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' }} + +permissions: + contents: read + +jobs: + llm_rl_job: + if: github.repository_owner == 'verl-project' + name: E2E Ascend testing for RL training scenarios of LLM models + runs-on: linux-aarch64-a3-8 + timeout-minutes: 120 + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps -e . + uv pip install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + uv pip list + - name: Preprocess gsm8k dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running gsm8k e2e training tests with PPO on ASCEND NPU (FSDP backend) + run: | + ray stop --force + bash tests/special_npu/run_qwen3_06b_ppo.sh + rm -rf $HOME/ckpts + - name: Running gsm8k e2e training tests with GRPO on ASCEND NPU (FSDP backend) + run: | + ray stop --force + bash tests/special_npu/run_qwen3_8b_grpo_profiling.sh + python3 "tests/utils/test_check_profiler_output.py" --profiler_dir="./profiler_data-dis" --device="npu" + rm -rf ./profiler_data-dis + rm -rf $HOME/ckpts + - name: Running gsm8k e2e training tests with GRPO on ASCEND NPU (MindSpeed backend) + run: | + ray stop --force + USE_DIST_CKPT=True bash tests/special_npu/run_qwen3_06b_grpo_mindspeed.sh + rm -rf $HOME/dist_ckpt/qwen3_06b_grpo_mindspeed + rm -rf $HOME/ckpts + - name: Running gsm8k e2e training tests with GRPO on ASCEND NPU (MindSpeed backend, MoE Model) + run: | + ray stop --force + USE_DIST_CKPT=True USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen3moe_minimal.json DUMMY_MODEL_PATH=$HOME/dist_ckpt/qwen3_30b_grpo_mindspeed bash tests/special_npu/run_qwen3_30b_grpo_mindspeed.sh + + engine_mindspeed_llm_rl_job: + if: github.repository_owner == 'verl-project' + name: E2E Ascend testing for RL training scenarios of LLM models using MindSpeed_LLM engine + runs-on: linux-aarch64-a3-8 + timeout-minutes: 120 + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps --no-build-isolation -e . + uv pip install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + uv pip list + - name: Configure related dependencies + run: | + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git /Megatron-LM + rm -rf /MindSpeed + git clone https://gitcode.com/ascend/MindSpeed.git /MindSpeed + git clone https://gitcode.com/ascend/MindSpeed-LLM.git /MindSpeed-LLM + - name: Preprocess gsm8k dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running gsm8k e2e training tests with GRPO on ASCEND NPU (MindSpeedLLM backend) + run: | + ray stop --force + export PYTHONPATH=$PYTHONPATH:/Megatron-LM + export PYTHONPATH=$PYTHONPATH:/MindSpeed + export PYTHONPATH=$PYTHONPATH:/MindSpeed-LLM + rm -rf /root/.cache/torch_extensions/py311_cpu/npu_rotary_position_embedding + bash tests/special_npu/run_qwen3_8b_grpo_mindspeedllm.sh + - name: Running gsm8k e2e training tests with GRPO on ASCEND NPU (MindSpeedLLM backend, MoE Model) + run: | + ray stop --force + export PYTHONPATH=$PYTHONPATH:/Megatron-LM + export PYTHONPATH=$PYTHONPATH:/MindSpeed + export PYTHONPATH=$PYTHONPATH:/MindSpeed-LLM + rm -rf /root/.cache/torch_extensions/py311_cpu/npu_rotary_position_embedding + USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen3moe_minimal.json DUMMY_MODEL_PATH=$HOME/dist_ckpt/qwen3_30b_grpo_mindspeedllm bash tests/special_npu/run_qwen3_30b_grpo_mindspeedllm.sh + rm -rf $HOME/dist_ckpt/qwen3_30b_grpo_mindspeedllm + + vlm_rl_job: + if: github.repository_owner == 'verl-project' + name: E2E Ascend testing for RL training scenarios of VLM models + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 120 + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 80g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip list + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running geo3k e2e training tests with GRPO on ASCEND NPU + run: | + ray stop --force + SAVE_FREQ=-1 TOTAL_TRAINING_STEPS=1 bash tests/special_npu/run_qwen3_vl_8b_Instruct_fsdp2_npu.sh + rm -rf $HOME/ckpts diff --git a/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy.yml b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy.yml new file mode 100644 index 0000000000000000000000000000000000000000..62c944b96f57488957d7dae090cc03b240b9cf5d --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy.yml @@ -0,0 +1,180 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_fully_async_policy + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/fully_async_policy" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Home + - "verl/experimental/fully_async_policy" + # Entrypoints + - ".github/workflows/e2e_fully_async_policy.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/run_fully_async_policy.sh" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + # Test FSDP2 strategy + e2e_fully_async_policy_fsdp2: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 10 # Increase timeout for async training + env: + 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 + ACTOR_STRATEGY: "fsdp2" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + pip3 install cupy-cuda13x==13.6.0 + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running the E2E test with fully_async_policy algorithm (FSDP2) + run: | + ray stop --force + bash tests/special_e2e/run_fully_async_policy.sh + + # Test Megatron strategy + e2e_fully_async_policy_megatron: + needs: [setup, e2e_fully_async_policy_fsdp2] + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 10 # Increase timeout for async training + env: + 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 + ACTOR_STRATEGY: "megatron" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda13x==13.6.0 + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running the E2E test with fully_async_policy algorithm (Megatron) + run: | + ray stop --force + bash tests/special_e2e/run_fully_async_policy.sh + + cleanup: + runs-on: ubuntu-latest + needs: [setup, e2e_fully_async_policy_fsdp2] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..48612f95e969f040705f536398bc62b343383ec3 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_ascend.yml @@ -0,0 +1,192 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_fully_async_policy_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/fully_async_policy" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Home + - "verl/experimental/fully_async_policy" + # Entrypoints + - ".github/workflows/e2e_fully_async_policy_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/run_fully_async_policy.sh" + +# 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: + # Test FSDP2 strategy + e2e_fully_async_policy_fsdp2_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + ACTOR_STRATEGY: "fsdp2" + device_name: "npu" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running the E2E test with fully_async_policy algorithm (FSDP2) + run: | + ray stop --force + bash tests/special_e2e/run_fully_async_policy.sh + + # Test Megatron strategy + e2e_fully_async_policy_megatron_ascend: + if: github.repository_owner == 'verl-project' + needs: e2e_fully_async_policy_fsdp2_ascend + runs-on: linux-aarch64-a3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + ACTOR_STRATEGY: "megatron" + device_name: "npu" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running the E2E test with fully_async_policy algorithm (Megatron) + run: | + ray stop --force + VANILLA_MBRIDGE=True bash tests/special_e2e/run_fully_async_policy.sh diff --git a/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_opd.yml b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_opd.yml new file mode 100644 index 0000000000000000000000000000000000000000..424dea53fd27c9de1f0eb13ee42bc7ed87a3e33c --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_opd.yml @@ -0,0 +1,112 @@ +name: e2e_fully_async_policy_opd + +on: + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/fully_async_policy" + - "verl/trainer/distillation" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/fully_async_policy" + - "verl/trainer/distillation" + - ".github/workflows/e2e_fully_async_policy_opd.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_fully_async_policy_opd.sh" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + # Test Multi-Teacher OPD with Megatron strategy + e2e_fully_async_policy_opd_megatron: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 + env: + 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" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda13x==13.6.0 + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare datasets + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k/ --local_save_dir ${HOME}/data/geo3k + - name: Running the E2E test with fully_async_policy + Multi-Teacher OPD (Megatron) + run: | + ray stop --force + bash tests/special_e2e/run_fully_async_policy_opd.sh + + cleanup: + runs-on: ubuntu-latest + needs: [setup, e2e_fully_async_policy_opd_megatron] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_trtllm.yml b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_trtllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..54a4705d1ea08149fd69f3c62867e545830b00bb --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_fully_async_policy_trtllm.yml @@ -0,0 +1,180 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_fully_async_policy_trtllm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/workers/rollout/trtllm_rollout/**" + - "verl/experimental/fully_async_policy/**" + - "verl/checkpoint_engine/**" + pull_request: + branches: + - main + - v0.* + paths: + - "verl/workers/rollout/trtllm_rollout/**" + - "verl/experimental/fully_async_policy/**" + - "verl/checkpoint_engine/**" + - ".github/workflows/e2e_fully_async_policy_trtllm.yml" + - "tests/special_e2e/run_fully_async_policy.sh" + - "examples/data_preprocess/gsm8k.py" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:trtllm-1.3.0rc15" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + trtllm_async_unit_tests: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 + env: + 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" + TORCH_CUDA_ARCH_LIST: "7.5;8.0;8.9;9.0;10.0;12.0+PTX" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Reinstall FlashInfer for runtime GPU arch + run: | + unset TORCH_CUDA_ARCH_LIST + pip3 install --force-reinstall --no-deps flashinfer-python + - name: Install the current repository + run: | + pip3 install pytest-asyncio + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Run TRT-LLM async unit tests + run: | + unset TORCH_CUDA_ARCH_LIST + export TRTLLM_ENABLE_PDL=0 + export TRTLLM_TEST_MODEL_PATH_ROOT="${HOME}/models" + ray stop --force + pytest -v -s --durations=20 \ + tests/workers/rollout/rollout_trtllm/test_trtllm_abort.py + + # Megatron multi-replica: 4 rollout GPUs split into 2 replicas × TP=2. + e2e_fully_async_policy_trtllm_megatron_multi_replica: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 20 + 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" + ACTOR_STRATEGY: "megatron" + ROLLOUT_NAME: "trtllm" + GEN_TP: "2" # 4 rollout GPUs / TP=2 = 2 replicas + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda12x==14.0.1 + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running TRT-LLM fully-async E2E test with Megatron (multi-replica, 2×TP=2) + run: | + ray stop --force + bash tests/special_e2e/run_fully_async_policy.sh \ + data.max_response_length=128 \ + actor_rollout_ref.rollout.n=2 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.1 \ + actor_rollout_ref.actor.ppo_mini_batch_size=4 \ + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=1536 + + cleanup: + runs-on: ubuntu-latest + needs: [setup, trtllm_async_unit_tests, e2e_fully_async_policy_trtllm_megatron_multi_replica] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy.yml b/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy.yml new file mode 100644 index 0000000000000000000000000000000000000000..fe825d011049dd4fa4c143beefee9e049271f5aa --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy.yml @@ -0,0 +1,181 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_one_step_off_policy + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/one_step_off_policy" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Home + - "verl/experimental/one_step_off_policy" + # Entrypoints + - ".github/workflows/e2e_one_step_off_policy.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/run_one_step_off_policy.sh" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + # Test FSDP2 strategy + e2e_one_step_off_policy_fsdp2: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 10 # Increase timeout for async training + env: + 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 + ACTOR_STRATEGY: "fsdp2" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + pip3 install cupy-cuda13x==13.6.0 + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running the E2E test with one_step_off_policy algorithm (FSDP2) + run: | + ray stop --force + bash tests/special_e2e/run_one_step_off_policy.sh + + # Test Megatron strategy + e2e_one_step_off_policy_megatron: + needs: [setup, e2e_one_step_off_policy_fsdp2] + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 10 # Increase timeout for async training + env: + 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 + ACTOR_STRATEGY: "megatron" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda13x==13.6.0 + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running the E2E test with one_step_off_policy algorithm (Megatron) + run: | + ray stop --force + bash tests/special_e2e/run_one_step_off_policy.sh + + cleanup: + runs-on: ubuntu-latest + needs: + [setup, e2e_one_step_off_policy_fsdp2, e2e_one_step_off_policy_megatron] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..66f4ee9bd3c35b22d5d43aa344d87644920a5091 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_one_step_off_policy_ascend.yml @@ -0,0 +1,193 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_one_step_off_policy_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "verl/experimental/one_step_off_policy" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Home + - "verl/experimental/one_step_off_policy" + # Entrypoints + - ".github/workflows/e2e_one_step_off_policy_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/run_one_step_off_policy.sh" + +# 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: + # Test FSDP2 strategy + e2e_one_step_off_policy_fsdp2_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + ACTOR_STRATEGY: "fsdp2" + device_name: "npu" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running the E2E test with one_step_off_policy algorithm (FSDP2) + run: | + ray stop --force + bash tests/special_e2e/run_one_step_off_policy.sh + + # Test Megatron strategy + e2e_one_step_off_policy_megatron_ascend: + if: github.repository_owner == 'verl-project' + needs: e2e_one_step_off_policy_fsdp2_ascend + runs-on: linux-aarch64-a3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + ACTOR_STRATEGY: "megatron" + device_name: "npu" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running the E2E test with one_step_off_policy algorithm (Megatron) + run: | + ray stop --force + export PYTHONPATH=$PYTHONPATH:/Megatron-LM + VANILLA_MBRIDGE=True bash tests/special_e2e/run_one_step_off_policy.sh diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_grpo_trainer_trtllm.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_grpo_trainer_trtllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..d3276c69d53d175e97d94625b8c5ba649a8cc623 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_grpo_trainer_trtllm.yml @@ -0,0 +1,310 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_trtllm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # Recipes + - "!recipe/**" + # FSDP + - "!verl/workers/**/*dp_*.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Recipes + - "!recipe/**" + # FSDP + - "!verl/workers/**/*dp_*.py" + # Entrypoints + - "verl/workers/rollout/trtllm_rollout/**" + - "tests/workers/rollout/rollout_trtllm/**" + - ".github/workflows/e2e_ppo_grpo_trainer_trtllm.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "examples/data_preprocess/dapo_multiturn_w_tool.py" + - "examples/data_preprocess/aime2024_multiturn_w_tool.py" + - "examples/grpo_trainer/run_qwen3_8b_megatron.sh" + - "examples/grpo_trainer/run_qwen3_30b_a3b_megatron.sh" + # add back when ppo flow is ready + # - "tests/special_e2e/run_ppo_trainer_megatron.sh" + # - "verl/trainer/main_ppo.py" + # - "verl/trainer/config/ppo_megatron_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:trtllm-1.3.0rc15" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + trtllm_unit_tests: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + TORCH_CUDA_ARCH_LIST: "7.5;8.0;8.9;9.0;10.0;12.0+PTX" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Reinstall FlashInfer for runtime GPU arch + run: | + unset TORCH_CUDA_ARCH_LIST + pip3 install --force-reinstall --no-deps flashinfer-python + - name: Install the current repository + run: | + pip3 install pytest-asyncio + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + - name: Run TRTLLM unit tests + run: | + unset TORCH_CUDA_ARCH_LIST # Let TRT-LLM CUTLASS DSL auto-detect runtime GPU arch + export TRTLLM_ENABLE_PDL=0 # Disable Programmatic Dependent Launch (uses SM 9.0 griddepcontrol) + export TRTLLM_TEST_MODEL_PATH_ROOT="${HOME}/models" + ray stop --force + pytest -v -s --durations=20 \ + tests/workers/rollout/rollout_trtllm/test_adapter.py \ + tests/workers/rollout/rollout_trtllm/test_async_server.py \ + tests/workers/rollout/rollout_trtllm/test_inter_node_rollout.py \ + tests/workers/rollout/rollout_trtllm/test_trtllm_rollout_utils.py + + e2e_grpo_trainer_megatron-qwen2: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + TORCH_CUDA_ARCH_LIST: "7.5;8.0;8.9;9.0;10.0;12.0+PTX" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Reinstall FlashInfer for runtime GPU arch + run: | + unset TORCH_CUDA_ARCH_LIST + pip3 install --force-reinstall --no-deps flashinfer-python + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k --local_save_dir ${PWD}/data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (Qwen) + run: | + unset TORCH_CUDA_ARCH_LIST # Let TRT-LLM CUTLASS DSL auto-detect runtime GPU arch + export TRTLLM_ENABLE_PDL=0 # Disable Programmatic Dependent Launch (uses SM 9.0 griddepcontrol) + ray stop --force + ACTOR_TP=2 \ + ACTOR_PP=1 \ + ROLLOUT_TP=2 \ + INFER_BACKEND=trtllm \ + MODEL_PATH="${HOME}/models/Qwen/Qwen2.5-0.5B-Instruct" \ + bash examples/grpo_trainer/run_qwen3_8b_megatron.sh \ + trainer.total_training_steps=1 \ + data.train_batch_size=32 \ + data.max_prompt_length=128 \ + data.max_response_length=64 \ + actor_rollout_ref.rollout.n=1 \ + actor_rollout_ref.actor.ppo_mini_batch_size=32 \ + actor_rollout_ref.rollout.max_num_seqs=32 \ + actor_rollout_ref.rollout.max_num_batched_tokens=1024 \ + actor_rollout_ref.rollout.max_model_len=256 \ + data.train_files="['${PWD}/data/gsm8k/train.parquet']" \ + data.val_files="['${PWD}/data/gsm8k/test.parquet']" \ + trainer.logger='["console"]' + - name: clean up + run: | + rm -rf checkpoints + e2e_grpo_trainer_megatron-vlm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + TORCH_CUDA_ARCH_LIST: "7.5;8.0;8.9;9.0;10.0;12.0+PTX" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Reinstall FlashInfer for runtime GPU arch + run: | + unset TORCH_CUDA_ARCH_LIST + pip3 install --force-reinstall --no-deps flashinfer-python + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + pip3 install qwen_vl_utils + pip3 install mathruler + pip3 install msgspec + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GEO3K dataset + run: | + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k --local_save_dir ${PWD}/data/geo3k + - name: Running GEO3K E2E training tests with FSDP on 8 L20 GPUs (VLM) + run: | + ray stop --force + ROLLOUT_TP=2 \ + INFER_BACKEND=trtllm \ + MODEL_PATH="${HOME}/models/Qwen/Qwen3-VL-2B-Instruct" \ + bash examples/grpo_trainer/run_qwen2_5_vl_7b_fsdp.sh \ + trainer.total_training_steps=1 \ + data.train_batch_size=8 \ + data.max_response_length=64 \ + actor_rollout_ref.actor.ppo_mini_batch_size=8 \ + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=2048 \ + actor_rollout_ref.rollout.n=2 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.35 \ + actor_rollout_ref.rollout.max_model_len=2048 \ + actor_rollout_ref.rollout.max_num_seqs=16 \ + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=2048 \ + data.train_files="['${PWD}/data/geo3k/train.parquet']" \ + data.val_files="['${PWD}/data/geo3k/test.parquet']" \ + trainer.logger='["console"]' + - name: clean up + run: | + rm -rf checkpoints + - name: Prepare GSM8K dataset (data_preprocess) + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k --local_save_dir ${PWD}/data/gsm8k + - name: Running GRPO E2E with FP8 TRT-LLM rollout (Qwen3-0.6B, gsm8k) + run: | + unset TORCH_CUDA_ARCH_LIST # Let TRT-LLM CUTLASS DSL auto-detect runtime GPU arch + export TRTLLM_ENABLE_PDL=0 # Disable Programmatic Dependent Launch (uses SM 9.0 griddepcontrol) + ray stop --force + export INFER_TP=2 ACTOR_TP=2 ACTOR_PP=2 ACTOR_VPP=2 ACTOR_EP=1 ACTOR_CP=2 REF_TP=2 REF_PP=2 REF_VPP=2 REF_EP=1 REF_CP=2 GEN_MOE_TP=null GEN_MOE_EP=null + export NNODES=1 GPUS_PER_NODE=8 TRTLLM_MOE_BACKEND=TRITON + export DATA_DIR=${PWD} DAPO_MATH_TRAIN=${PWD}/data/gsm8k/train.parquet AIME_VAL=${PWD}/data/gsm8k/test.parquet MODEL_PATH=${HOME}/models/Qwen/Qwen3-0.6B + INFER_BACKEND=trtllm ROLLOUT_QUANTIZATION=fp8 bash examples/grpo_trainer/run_qwen3_30b_a3b_megatron.sh \ + reward_model.reward_kwargs.overlong_buffer_cfg.len=258 \ + reward_model.reward_kwargs.max_resp_len=512 \ + data.max_prompt_length=128 \ + data.max_response_length=64 \ + data.train_batch_size=32 \ + actor_rollout_ref.rollout.n=1 \ + actor_rollout_ref.rollout.max_num_seqs=16 \ + actor_rollout_ref.rollout.max_num_batched_tokens=1024 \ + actor_rollout_ref.rollout.max_model_len=1024 \ + actor_rollout_ref.actor.megatron.override_transformer_config.moe_grouped_gemm=False \ + actor_rollout_ref.actor.megatron.override_transformer_config.moe_permute_fusion=False \ + trainer.total_training_steps=1 \ + trainer.logger='["console"]' + - name: clean up + run: | + rm -rf checkpoints + + cleanup: + runs-on: ubuntu-latest + needs: [setup, trtllm_unit_tests, e2e_grpo_trainer_megatron-qwen2, e2e_grpo_trainer_megatron-vlm] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer.yml new file mode 100644 index 0000000000000000000000000000000000000000..b06b22c9d4caad0549fc260e63c38ff43e542c9d --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer.yml @@ -0,0 +1,81 @@ +name: e2e_ppo_trainer + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + + # Megatron + - "!verl/workers/**/megatron_*.py" + + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!**/*.md" + - "!docker/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Docs + - "!docs/**" + + # Megatron + - "!verl/workers/**/megatron_*.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_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: Install the current repository + run: | + pip install pre-commit hydra-core + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - 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" + diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang.yml new file mode 100644 index 0000000000000000000000000000000000000000..dd06867736ebad6ddae78b28cf91451448aaed65 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang.yml @@ -0,0 +1,215 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_sglang + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - "verl/worksers/rollout/sglang_rollout/*" + - ".github/workflows/e2e_ppo_trainer_megatron_sglang.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + e2e_ppo_trainer_megatron-deepseek: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + ENGINE: sglang + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (DeepSeek) + run: | + ray stop --force + OPTIM_MEMORY_EFFICIENT=True ENGINE=sglang SAVE_FREQ=1 MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (DeepSeek) + run: | + ray stop --force + export VLLM_USE_V1=1 + ray start --head + ENGINE=sglang MODE=async RESUME_MODE=auto MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct TOTAL_TRAIN_STEPS=2 bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Profiling GRPO GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (Deepseek) + run: | + ray stop --force + PROFILE_ENABLE=True ENGINE=sglang ADV_ESTIMATOR=grpo USE_DYNAMIC_BSZ=False MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct bash tests/special_e2e/run_ppo_trainer_megatron.sh + if [ -z "$( ls -A '/tmp/ray/session_latest/logs/nsight/' )" ]; then + echo "[ERROR] not found any profiling files" + exit 1 + else + echo "[SUCCESS] profile success" + fi + - name: clean up + run: | + rm -rf checkpoints + + # Qwen3-0.6B: dense, tie_word_embeddings=True + e2e_ppo_trainer_megatron-qwen3: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + ENGINE: sglang + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (Qwen3) testing learning rate scheduler + run: | + ray stop --force + ALL_OFFLOAD=True VAL_BEFORE_TRAIN=True TEST_FREQ=1 SAVE_FREQ=1 LR_WARMUP_STEPS=1 TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with FP8 rollout + run: | + ray stop --force + export VLLM_USE_V1=1 + ROLLOUT_QUANTIZATION=fp8 TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: clean up + run: | + rm -rf checkpoints + + cleanup: + runs-on: ubuntu-latest + needs: + [setup, e2e_ppo_trainer_megatron-deepseek, e2e_ppo_trainer_megatron-qwen3] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2.yml new file mode 100644 index 0000000000000000000000000000000000000000..93a29177511d8e6d5780558b7624a3f7115d9208 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2.yml @@ -0,0 +1,207 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_sglang_2 + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - "verl/worksers/rollout/sglang_rollout/*" + - ".github/workflows/e2e_ppo_trainer_megatron_sglang.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + e2e_ppo_trainer_fsdp_sglang: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 40 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - 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/special_e2e/ppo_trainer/run_function_reward.sh + + e2e_ppo_trainer_fsdp-qwen2_5vl-3b: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + # Geo3k + - name: Prepare GEO3K dataset + run: | + ray stop --force + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k/ + - 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + ENGINE=sglang ROLLOUT_MODE=async GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \ + ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GEO3K VLM E2E with rmpad using torch 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 ROLLOUT_MODE=async GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \ + ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GEO3K VLM E2E with rmpad using triton fused kernel (Qwen2.5-VL) + run: | + ray stop --force + FUSED_KERNELS=True FUSED_KERNEL_BACKEND=triton \ + 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 ROLLOUT_MODE=async GPU_MEMORY_UTILIZATION=0.6 ACTOR_FSDP_PARAM_OFFLOAD=True \ + ACTOR_FSDP_OPTIMIZER_OFFLOAD=True REF_FSDP_PARAM_OFFLOAD=True \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + + cleanup: + runs-on: ubuntu-latest + needs: + [setup, e2e_ppo_trainer_fsdp-qwen2_5vl-3b, e2e_ppo_trainer_fsdp_sglang] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..4aa2f62e18c0883f867374183ac3a513f8917519 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_2_ascend.yml @@ -0,0 +1,145 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_sglang_2_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - "verl/worksers/rollout/sglang_rollout/*" + - ".github/workflows/e2e_ppo_trainer_megatron_sglang_2_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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: + e2e_ppo_trainer_fsdp_sglang_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES: "1" + ASCEND_RT_VISIBLE_DEVICES: "0,1,2,3,4,5,6,7" + HCCL_HOST_SOCKET_PORT_RANGE: "60000-60050" + HCCL_NPU_SOCKET_PORT_RANGE: "61000-61050" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install pytest pytest-asyncio + uv pip install --no-deps --no-build-isolation -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + ray stop --force + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running GSM8K E2E training tests on 8 NPUs with rmpad using function rm and save ckpt + run: | + ray stop --force + ENGINE=sglang bash tests/special_e2e/ppo_trainer/run_function_reward.sh diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..576b9e960b31b17d9ce8eab3671c5c97f04462ea --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_sglang_ascend.yml @@ -0,0 +1,227 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_sglang_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - "verl/worksers/rollout/sglang_rollout/*" + - ".github/workflows/e2e_ppo_trainer_megatron_sglang_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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: + e2e_ppo_trainer_megatron-deepseek_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES: "1" + ASCEND_RT_VISIBLE_DEVICES: "0,1,2,3,4,5,6,7" + HCCL_HOST_SOCKET_PORT_RANGE: "60000-60050" + HCCL_NPU_SOCKET_PORT_RANGE: "61000-61050" + ENGINE: sglang + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install pytest pytest-asyncio + uv pip install --no-deps --no-build-isolation -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 NPUs with Megatron (DeepSeek) + run: | + ray stop --force + VANILLA_MBRIDGE=True OPTIM_MEMORY_EFFICIENT=True ENGINE=sglang COMMON_VPP=NULL USE_REMOVE_PADDING=True SAVE_FREQ=1 MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 NPUs with Megatron (DeepSeek) + run: | + ray stop --force + export VLLM_USE_V1=1 + ray start --head + VANILLA_MBRIDGE=True ENGINE=sglang MODE=async RESUME_MODE=auto COMMON_VPP=NULL USE_REMOVE_PADDING=True MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct TOTAL_TRAIN_STEPS=2 bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Profiling GRPO GSM8K E2E training tests with 3D parallelism on 8 NPUs with Megatron (Deepseek) + run: | + ray stop --force + VANILLA_MBRIDGE=True PROFILE_ENABLE=True ENGINE=sglang ADV_ESTIMATOR=grpo USE_DYNAMIC_BSZ=False USE_REMOVE_PADDING=True MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct bash tests/special_e2e/run_ppo_trainer_megatron.sh + if [ -z "$( ls -A "${HOME}/profiling" )" ]; then + echo "[ERROR] not found any profiling files" + exit 1 + else + echo "[SUCCESS] profile success" + fi + - name: clean up + run: | + rm -rf checkpoints + rm -rf "${HOME}/profiling" + + # Qwen3-0.6B: dense, tie_word_embeddings=True + e2e_ppo_trainer_megatron-qwen3: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES: "1" + ASCEND_RT_VISIBLE_DEVICES: "0,1,2,3,4,5,6,7" + HCCL_HOST_SOCKET_PORT_RANGE: "60000-60050" + HCCL_NPU_SOCKET_PORT_RANGE: "61000-61050" + ENGINE: sglang + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install pytest pytest-asyncio + uv pip install --no-deps --no-build-isolation -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 NPUs with Megatron (Qwen3) testing learning rate scheduler + run: | + ray stop --force + VANILLA_MBRIDGE=True ALL_OFFLOAD=True USE_REMOVE_PADDING=True VAL_BEFORE_TRAIN=True TEST_FREQ=1 SAVE_FREQ=1 LR_WARMUP_STEPS=1 TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: clean up + run: | + rm -rf checkpoints \ No newline at end of file diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..1c80a0d81f17fd685f2c012ecbd990f141c86cbb --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm.yml @@ -0,0 +1,243 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_vllm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_megatron_vllm.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + # deepseek-ai/deepseek-coder-1.3b-instruct: dense, tie_word_embeddings=False + e2e_ppo_trainer_megatron-deepseek: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install math-verify + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + # Full training save&load + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron, use Megatron-Bridge e2e to pre-load and save (Deepseek) + run: | + ray stop --force + ALL_OFFLOAD=True SAVE_FREQ=1 MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct COMMON_PP=4 COMMON_VPP=null COMMON_CP=1 USE_MBRIDGE=True USE_DIST_CKPT=False \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron, use Megatron-Bridge e2e to pre-load and save (Deepseek) + run: | + ray stop --force + RESUME_MODE=auto MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct TOTAL_TRAIN_STEPS=2 SAVE_FREQ=1 COMMON_PP=4 COMMON_VPP=null COMMON_CP=1 USE_MBRIDGE=True USE_DIST_CKPT=False \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh + # LoRA training save&load + - name: clean up and install Megatron-Bridge + run: | + rm -rf checkpoints + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install transformers==5.3.0 + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron, use Megatron-Bridge LoRA e2e to pre-load and save (Deepseek) + run: | + ray stop --force + ALL_OFFLOAD=True SAVE_FREQ=1 MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct COMMON_PP=4 LORA_RANK=8 COMMON_VPP=null COMMON_CP=1 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False USE_DIST_CKPT=False \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron, use Megatron-Bridge LoRA e2e to pre-load and save (Deepseek) + run: | + ray stop --force + RESUME_MODE=auto MODEL_ID=deepseek-ai/deepseek-coder-1.3b-instruct TOTAL_TRAIN_STEPS=2 SAVE_FREQ=1 COMMON_PP=4 LORA_RANK=8 COMMON_VPP=null COMMON_CP=1 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False USE_DIST_CKPT=False \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: clean up + run: | + rm -rf checkpoints + + # Qwen3-0.6B: dense, tie_word_embeddings=True + e2e_ppo_trainer_megatron-qwen3: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install math-verify + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron (Qwen3) testing learning rate scheduler + run: | + ray stop --force + ALL_OFFLOAD=True VAL_BEFORE_TRAIN=True TEST_FREQ=1 SAVE_FREQ=1 LR_WARMUP_STEPS=1 TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with FP8 rollout + run: | + ray stop --force + export VLLM_USE_V1=1 + ROLLOUT_QUANTIZATION=fp8 TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Install Megatron-LM and Megatron-Bridge for Megatron-FSDP + run: | + pip3 uninstall -y mbridge + pip3 install --no-deps --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 + pip3 install --no-deps --no-build-isolation git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e + pip3 install "nvidia-modelopt>=0.37.0" + - name: Running GSM8K E2E PPO training tests on 8 L20 GPUs with Megatron-FSDP (Qwen3) + run: | + ray stop --force + ALL_OFFLOAD=False USE_MBRIDGE=True VANILLA_MBRIDGE=False USE_MEGATRON_FSDP=True \ + TOTAL_TRAIN_STEPS=2 MODEL_ID=Qwen/Qwen3-0.6B \ + COMMON_PP=1 COMMON_VPP=null COMMON_CP=1 COMMON_TP=1 INFER_TP=1 \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh \ + ++actor_rollout_ref.actor.megatron.override_transformer_config.gradient_accumulation_fusion=False \ + ++actor_rollout_ref.ref.megatron.override_transformer_config.gradient_accumulation_fusion=False \ + ++critic.megatron.override_transformer_config.gradient_accumulation_fusion=False + - name: clean up + run: | + rm -rf checkpoints + + cleanup: + runs-on: ubuntu-latest + needs: + [setup, e2e_ppo_trainer_megatron-deepseek, e2e_ppo_trainer_megatron-qwen3] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2.yml new file mode 100644 index 0000000000000000000000000000000000000000..eabd08c9677b07629031ccb651ff79416bad8392 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2.yml @@ -0,0 +1,328 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_vllm_2 + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_megatron_vllm_2.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + e2e_ppo_trainer_megatron-moe-expert-parallel: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 uninstall -y mbridge + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip3 install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron-Bridge (Qwen3-30B-A3B-Instruct-2507) + run: | + ray stop --force + ADV_ESTIMATOR=grpo USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen2moe_minimal.json \ + PPO_MAX_TOKEN_LEN=1024 FWD_MAX_TOKEN_LEN=1024 \ + MAX_PROMPT_LENGTH=512 MAX_RESPONSE_LENGTH=512 \ + MODEL_ID=Qwen/Qwen3-30B-A3B-Instruct-2507 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False \ + COMMON_PP=2 COMMON_VPP=null COMMON_CP=1 COMMON_TP=4 COMMON_EP=4 COMMON_ETP=1 INFER_TP=8 \ + USE_DIST_CKPT=False ALL_OFFLOAD=True SKIP_SAVE_HF_MODEL=1 bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: Running GSM8K E2E training tests with 3D parallelism with FP8 rollout on 8 L20 GPUs with Megatron-Bridge (Qwen3-30B-A3B-Instruct-2507) + run: | + ray stop --force + ADV_ESTIMATOR=grpo USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen2moe_minimal.json \ + PPO_MAX_TOKEN_LEN=1024 FWD_MAX_TOKEN_LEN=1024 \ + MAX_PROMPT_LENGTH=512 MAX_RESPONSE_LENGTH=512 \ + MODEL_ID=Qwen/Qwen3-30B-A3B-Instruct-2507 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False \ + COMMON_PP=2 COMMON_VPP=null COMMON_CP=1 COMMON_TP=4 COMMON_EP=4 COMMON_ETP=1 INFER_TP=2 \ + USE_DIST_CKPT=False ALL_OFFLOAD=True SKIP_SAVE_HF_MODEL=1 ROLLOUT_QUANTIZATION=fp8 bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: clean up + run: | + rm -rf checkpoints + - name: Running GSM8K E2E training tests with 3D parallelism on 8 L20 GPUs with Megatron-Bridge LoRA (Qwen3-30B-A3B-Instruct-2507) + run: | + ray stop --force + ADV_ESTIMATOR=grpo USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen2moe_minimal.json \ + PPO_MAX_TOKEN_LEN=1024 FWD_MAX_TOKEN_LEN=1024 \ + MAX_PROMPT_LENGTH=512 MAX_RESPONSE_LENGTH=512 LORA_RANK=8 CRITIC_LORA_RANK=8 \ + MODEL_ID=Qwen/Qwen3-30B-A3B-Instruct-2507 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False \ + COMMON_PP=2 COMMON_VPP=null COMMON_CP=1 COMMON_TP=4 COMMON_EP=2 COMMON_ETP=1 INFER_TP=8 \ + USE_DIST_CKPT=False LORA_MERGE=True ALL_OFFLOAD=True SKIP_SAVE_HF_MODEL=1 bash tests/special_e2e/run_ppo_trainer_megatron.sh + - name: clean up + run: | + rm -rf checkpoints + + e2e_ppo_trainer_fsdp_vllm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + # 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-fsdp-size8" bash tests/special_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-fsdp-size8" bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test merging FSDP checkpoints (Qwen Actor) + run: | + exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp-size8" + python -m verl.model_merger 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 USE_KL=True VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-ddp-size2-fsdp-size4" bash tests/special_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-ddp-size2-fsdp-size4" + python -m verl.model_merger 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 (FSDP2) + 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-fsdp2-size8" STRATEGY=fsdp2 bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test merging FSDP2 checkpoints (Qwen Actor) + run: | + exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp2-size8" + python -m verl.model_merger 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/special_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 + CUSTOM_REWARD_FN=True ADV_ESTIMATOR=grpo USE_KL=True bash tests/special_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/special_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/special_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 TOTAL_TRAIN_STEPS=1 SAVE_FREQ=1 FSDP_SIZE=4 VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal" bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test GRPO LoRA checkpoints merging function + run: | + export EXP_NAME="qwen2.5-0.5b-function-reward-minimal" + ls checkpoints/verl-test/${EXP_NAME}/global_step_1/actor + cat checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/huggingface/config.json + python3 -m verl.model_merger merge --backend fsdp --local_dir checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/ --target_dir checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/huggingface + - 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/special_e2e/ppo_trainer/run_function_reward.sh + + e2e_ppo_trainer_fsdp-qwen2_5vl-3b: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 40 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip3 list + # Geo3k + - name: Prepare GEO3K dataset + run: | + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k/ + - name: Running GEO3K VLM GRPO 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + + - name: Running GEO3K VLM PPO 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=gae RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GEO3K VLM GRPO E2E lora 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + LORA_RANK=32 LORA_EXCLUDE=".*visual.*" \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + + cleanup: + runs-on: ubuntu-latest + needs: + [ + setup, + e2e_ppo_trainer_megatron-moe-expert-parallel, + e2e_ppo_trainer_fsdp-qwen2_5vl-3b, + e2e_ppo_trainer_fsdp_vllm, + ] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..b7066a4b4a32f9c49b8ab0740bcdbcc6c4ca0933 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_2_ascend.yml @@ -0,0 +1,231 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_vllm_2_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_megatron_vllm_2_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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: + e2e_ppo_trainer_fsdp_vllm_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + # Function RM + - 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 USE_KL=True VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-ddp-size2-fsdp-size4" bash tests/special_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-ddp-size2-fsdp-size4" + python -m verl.model_merger 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 (FSDP2) + 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-fsdp2-size8" STRATEGY=fsdp2 bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test merging FSDP2 checkpoints (Qwen Actor) + run: | + exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp2-size8" + python -m verl.model_merger 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/special_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 + CUSTOM_REWARD_FN=True ADV_ESTIMATOR=grpo USE_KL=True bash tests/special_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 TOTAL_TRAIN_STEPS=1 SAVE_FREQ=1 FSDP_SIZE=4 VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal" bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test GRPO LoRA checkpoints merging function + run: | + export EXP_NAME="qwen2.5-0.5b-function-reward-minimal" + ls checkpoints/verl-test/${EXP_NAME}/global_step_1/actor + cat checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/huggingface/config.json + python3 -m verl.model_merger merge --backend fsdp --local_dir checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/ --target_dir checkpoints/verl-test/${EXP_NAME}/global_step_1/actor/huggingface + - 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/special_e2e/ppo_trainer/run_function_reward.sh + + e2e_ppo_trainer_fsdp-qwen2_5vl-3b_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install trl==0.26.0 + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + # Geo3k + - name: Prepare GEO3K dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running GEO3K VLM GRPO 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GEO3K VLM PPO 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=gae RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GEO3K VLM GRPO E2E lora 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.5-VL-3B-Instruct \ + ADV_ESTIMATOR=grpo RM_PAD=True USE_KL=True ENABLE_CHUNKED_PREFILL=False \ + SP_SIZE=2 \ + LORA_RANK=32 LORA_EXCLUDE=".*visual.*" \ + bash tests/special_e2e/ppo_trainer/run_function_reward.sh diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_rocm.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_rocm.yml new file mode 100644 index 0000000000000000000000000000000000000000..d1290b49b42d5e40d2507043c023945c91b336b9 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_megatron_vllm_rocm.yml @@ -0,0 +1,265 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_megatron_vllm_rocm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # FSDP + - "!verl/workers/**/*dp_*.py" + - "!verl/utils/fsdp_utils.py" + - "!verl/utils/checkpoint/fsdp_checkpoint_manager.py" + - "!verl/model_merger/fsdp_model_merger.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_megatron_vllm_rocm.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_megatron.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_megatron_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: + e2e_ppo_trainer_fsdp_vllm_rocm: + if: github.repository_owner == 'verl-project' + runs-on: [rocm-mi300] + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: "verlai/verl:rocm702-vllm020-te210-20260518" + options: >- + --device /dev/kfd + --device /dev/dri + --privileged + --group-add video + --cap-add SYS_PTRACE + --security-opt seccomp=unconfined + --shm-size 2048g + --ulimit memlock=-1 + --ulimit stack=67108864 + -v /data/models:/root/models + -v /data/data:/root/data + env: + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + # GitHub container jobs force HOME=/github/home. Our data is mounted under + # /root and the verl scripts reference $HOME, so pin HOME back to /root to + # match the local `docker run` behavior and the volume mounts above. + HOME: "/root" + steps: + - name: Check ROCm and GPU info + run: | + rocm-smi || true + rocminfo || true + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + pip list + - name: Prepare GSM8K dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dir ${HOME}/models/hf_data/gsm8k + # Function RM + - name: Running GSM8K E2E training tests on 8 MI300 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-fsdp-size8" bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GSM8K E2E training tests on 8 MI300 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-fsdp-size8" bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test merging FSDP checkpoints (Qwen Actor) + run: | + exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp-size8" + python -m verl.model_merger 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 MI300 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 USE_KL=True VERL_EXP_NAME="qwen2.5-0.5b-function-reward-minimal-ddp-size2-fsdp-size4" bash tests/special_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-ddp-size2-fsdp-size4" + python -m verl.model_merger 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 MI300 GPUs with rmpad using function rm with validation and saving (FSDP2) + 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-fsdp2-size8" STRATEGY=fsdp2 bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Test merging FSDP2 checkpoints (Qwen Actor) + run: | + exp_name="qwen2.5-0.5b-function-reward-minimal-fsdp2-size8" + python -m verl.model_merger 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/special_e2e/ppo_trainer/run_function_reward.sh + - name: Running GSM8K E2E training tests on 8 MI300 GPUs with rmpad using function rm (GRPO) + run: | + ray stop --force + CUSTOM_REWARD_FN=True ADV_ESTIMATOR=grpo USE_KL=True bash tests/special_e2e/ppo_trainer/run_function_reward.sh + - name: Clean up + run: | + rm -rf checkpoints + + e2e_ppo_trainer_megatron-moe-expert-parallel_rocm: + if: github.repository_owner == 'verl-project' + runs-on: [rocm-mi300] + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: "verlai/verl:rocm702-vllm020-te210-20260518" + options: >- + --device /dev/kfd + --device /dev/dri + --privileged + --group-add video + --cap-add SYS_PTRACE + --security-opt seccomp=unconfined + --shm-size 2048g + --ulimit memlock=-1 + --ulimit stack=67108864 + -v /data/models:/root/models + -v /data/data:/root/data + env: + HF_HUB_ENABLE_HF_TRANSFER: "0" + HOME: "/root" + steps: + - name: Check ROCm and GPU info + run: | + rocm-smi || true + rocminfo || true + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install --no-deps TransferQueue==0.1.8 + pip uninstall -y mbridge + pip install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip list + - name: Prepare GSM8K dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dir ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests with 3D parallelism on 8 MI300 GPUs with Megatron-Bridge (Qwen3-30B-A3B-Instruct-2507) + run: | + ray stop --force + ADV_ESTIMATOR=grpo USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen2moe_minimal.json \ + PPO_MAX_TOKEN_LEN=1024 FWD_MAX_TOKEN_LEN=1024 \ + MAX_PROMPT_LENGTH=512 MAX_RESPONSE_LENGTH=512 \ + MODEL_ID=Qwen/Qwen3-30B-A3B-Instruct-2507 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False \ + COMMON_PP=2 COMMON_VPP=null COMMON_CP=1 COMMON_TP=4 COMMON_EP=4 COMMON_ETP=1 INFER_TP=8 \ + USE_DIST_CKPT=False ALL_OFFLOAD=True SKIP_SAVE_HF_MODEL=1 \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh \ + global_profiler.tool=null actor_rollout_ref.actor.profiler.tool=null actor_rollout_ref.ref.profiler.tool=null actor_rollout_ref.rollout.profiler.tool=null critic.profiler.tool=null + - name: Running GSM8K E2E training tests with 3D parallelism with FP8 rollout on 8 MI300 GPUs with Megatron-Bridge (Qwen3-30B-A3B-Instruct-2507) + run: | + ray stop --force + ADV_ESTIMATOR=grpo USE_DUMMY_MODEL=True DUMMY_MODEL_CONFIG_PATH=tests/special_e2e/ppo_trainer/expert_parallel/qwen2moe_minimal.json \ + PPO_MAX_TOKEN_LEN=1024 FWD_MAX_TOKEN_LEN=1024 \ + MAX_PROMPT_LENGTH=512 MAX_RESPONSE_LENGTH=512 \ + MODEL_ID=Qwen/Qwen3-30B-A3B-Instruct-2507 USE_MBRIDGE=True VANILLA_MBRIDGE=False VALUE_VANILLA_MBRIDGE=False \ + COMMON_PP=2 COMMON_VPP=null COMMON_CP=1 COMMON_TP=4 COMMON_EP=4 COMMON_ETP=1 INFER_TP=2 \ + USE_DIST_CKPT=False ALL_OFFLOAD=True SKIP_SAVE_HF_MODEL=1 ROLLOUT_QUANTIZATION=fp8 \ + bash tests/special_e2e/run_ppo_trainer_megatron.sh \ + global_profiler.tool=null actor_rollout_ref.actor.profiler.tool=null actor_rollout_ref.ref.profiler.tool=null actor_rollout_ref.rollout.profiler.tool=null critic.profiler.tool=null + - name: clean up + run: | + rm -rf checkpoints + + cleanup_rocm: + needs: + - e2e_ppo_trainer_fsdp_vllm_rocm + - e2e_ppo_trainer_megatron-moe-expert-parallel_rocm + if: ${{ always() && github.repository_owner == 'verl-project' }} + runs-on: [rocm-mi300] + container: + image: "verlai/verl:rocm702-vllm020-te210-20260518" + steps: + - name: Clean up leftover checkpoints + run: | + rm -rf checkpoints diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..ac96de6a224c07721efe2126ebe44648fb78b73e --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm.yml @@ -0,0 +1,160 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_veomni_vllm + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch. + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!verl/trainer/fsdp_sft_trainer.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!docker/**" + # Docs + - "!**/*.md" + - "!docs/**" + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_veomni_vllm.yml" + - "examples/data_preprocess/gsm8k.py" + - "examples/data_preprocess/geo3k.py" + - "tests/special_e2e/run_ppo_trainer_veomni.sh" + - "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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + e2e_ppo_trainer_veomni_vllm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install veomni==0.1.11 --ignore-requires-python --no-deps --index-url https://pypi.org/simple/ + pip3 install transformers==5.3.0 + - name: Check final pip list + run: | + pip3 list + - name: Prepare GSM8K dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Prepare GEO3K dataset + run: | + ray stop --force + python3 examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/models/hf_data/hiyouga/geometry3k/ + - name: Running GSM8K E2E training tests on 8 L20 GPUs with veomni engine (FSDP_SIZE=4, USP=2) + run: | + ray stop --force + FSDP_SIZE=4 SP_SIZE=2 bash tests/special_e2e/run_ppo_trainer_veomni.sh + - name: Running GEO3K E2E training tests on 8 L20 GPUs with veomni engine (FSDP_SIZE=8, USP=1) + run: | + ray stop --force + MODEL_ID=Qwen/Qwen3-VL-2B-Instruct TRAIN_FILES=${HOME}/data/geo3k/train.parquet VAL_FILES=${HOME}/data/gsm8k/test.parquet FSDP_SIZE=8 SP_SIZE=1 bash tests/special_e2e/run_ppo_trainer_veomni.sh + - name: Test export unfused experts + run: | + torchrun --nproc_per_node=8 tests/utils/veomni/test_special_export_unfused_experts.py + + cleanup: + runs-on: ubuntu-latest + needs: + [ + setup, + e2e_ppo_trainer_veomni_vllm, + ] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..cd55346ac61d68b7318a17dfc30d5cdfead815ce --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_ppo_trainer_veomni_vllm_ascend.yml @@ -0,0 +1,138 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_ppo_trainer_veomni_vllm_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/*trainer*" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + - "!**/*.md" + - "!**/*.sh" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Entrypoints + - ".github/workflows/e2e_ppo_trainer_veomni_vllm_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/run_ppo_trainer_veomni.sh" + - "verl/trainer/main_ppo.py" + - "verl/trainer/config/ppo_trainer.yaml" + # Megatron + - "!verl/workers/**/megatron_*.py" + +# 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: + e2e_ppo_trainer_veomni_vllm_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + device_name: "npu" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install veomni==0.1.11 --ignore-requires-python --no-deps --index-url https://pypi.org/simple/ + pip install transformers==5.3.0 + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running the E2E test with veomni backend + run: | + ray stop --force + bash tests/special_e2e/run_ppo_trainer_veomni.sh + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running GEO3K E2E training tests on 8 L20 GPUs with veomni engine (FSDP_SIZE=8, USP=1) + run: | + ray stop --force + MODEL_ID=Qwen/Qwen3-VL-2B-Instruct TRAIN_FILES=${HOME}/data/geo3k/train.parquet VAL_FILES=${HOME}/data/gsm8k/test.parquet FSDP_SIZE=8 SP_SIZE=1 bash tests/special_e2e/run_ppo_trainer_veomni.sh + - name: Test export unfused experts + run: | + torchrun --nproc_per_node=8 tests/utils/veomni/test_special_export_unfused_experts.py diff --git a/verl_0720_main/verl/.github/workflows/e2e_sft_llm.yml b/verl_0720_main/verl/.github/workflows/e2e_sft_llm.yml new file mode 100644 index 0000000000000000000000000000000000000000..1cdc587517a7414ae50041f932f98c9f4263d6fd --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_sft_llm.yml @@ -0,0 +1,161 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_sft_llm + +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" + + # Megatron + - "!verl/workers/**/megatron_*.py" + # Entrypoints + - ".github/workflows/e2e_sft_llm.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/sft" + - "verl/trainer/fsdp_sft_trainer.py" + - "verl/trainer/config/sft_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + e2e_sft_llm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install peft + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install veomni==0.1.11 --ignore-requires-python --no-deps --index-url https://pypi.org/simple/ + pip3 install transformers==5.3.0 + pip3 uninstall -y mbridge + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + - name: Check final pip list + run: | + pip3 list + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k_multiturn_sft.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Running GSM8K E2E training tests on 8 L20 GPUs with rmpad using function rm + run: | + ray stop --force + bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests on 8 L20 GPUs w/o rmpad using function rm + run: | + ray stop --force + RM_PAD=False bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests on 8 L20 GPUs with sequence parallism + run: | + ray stop --force + SP_SIZE=2 bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests on 8 L20 GPUs with sequence parallism and liger + run: | + ray stop --force + SP_SIZE=2 LIGER=True bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests with LoRA + run: | + ray stop --force + LORA_RANK=32 bash tests/special_e2e/sft/run_sft.sh + - name: Run GSM8K E2E training and resume tests resuming from the checkpoint manager + run: | + ray stop --force + LORA_RANK=32 RESUME_MODE=auto TOTAL_TRAIN_STEP=2 bash tests/special_e2e/sft/run_sft.sh + # TODO: multiturn + - name: Running GSM8K E2E training tests with multiturn and various configs and compare results + run: | + bash tests/special_e2e/sft/test_sft_engine_all.sh + + cleanup: + runs-on: ubuntu-latest + needs: [setup, e2e_sft_llm] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/e2e_sft_llm_ascend.yml b/verl_0720_main/verl/.github/workflows/e2e_sft_llm_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..8564cba3df2bda6f12fb20f44596d4ab942a37fd --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_sft_llm_ascend.yml @@ -0,0 +1,160 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_sft_llm_ascend + +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" + + # Megatron + - "!verl/workers/**/megatron_*.py" + # Entrypoints + - ".github/workflows/e2e_sft_llm_ascend.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/sft" + - "verl/trainer/fsdp_sft_trainer.py" + - "verl/trainer/config/sft_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: + e2e_sft_llm_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + pip install veomni==0.1.11 --ignore-requires-python --no-deps --index-url https://pypi.org/simple/ + pip install transformers==5.3.0 + pip install pandas==2.3.3 + pip uninstall -y mbridge + pip install git+https://github.com/ISEEKYAN/mbridge.git@89eb10 + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare gsm8k dataset + run: | + python3 examples/data_preprocess/gsm8k_multiturn_sft.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running GSM8K E2E training tests on 8 NPUs with rmpad using function rm + run: | + ray stop --force + bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests on 8 NPUs w/o rmpad using function rm + run: | + ray stop --force + RM_PAD=False bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests on 8 NPUs with sequence parallism + run: | + ray stop --force + SP_SIZE=2 bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests with LoRA + run: | + ray stop --force + LORA_RANK=32 bash tests/special_e2e/sft/run_sft.sh + - name: Run GSM8K E2E training and resume tests resuming from the checkpoint manager + run: | + ray stop --force + LORA_RANK=32 RESUME_MODE=auto TOTAL_TRAIN_STEP=2 bash tests/special_e2e/sft/run_sft.sh + - name: Running GSM8K E2E training tests with multiturn and various configs and compare results + run: | + ray stop --force + rm -rf ~/verl/test/log + mkdir -p ~/verl/test/log + export VERL_FILE_LOGGER_ROOT=~/verl/test/log + # test with single gpu as golden + echo "run with single gpu as golden" + BACKEND=fsdp SP_SIZE=1 FSDP_SIZE=1 NUM_GPUS=1 FSDP_STRATEGY=fsdp VERL_FILE_LOGGER_PATH=~/verl/test/log/golden.jsonl bash tests/special_e2e/sft/run_sft_engine.sh + # test with fsdp 1 + echo "run with sp2 fsdp_size2 num_gpus8 fsdp_strategy fsdp pad_mode no_padding" + BACKEND=fsdp SP_SIZE=2 FSDP_SIZE=2 NUM_GPUS=8 FSDP_STRATEGY=fsdp PAD_MODE=no_padding bash tests/special_e2e/sft/run_sft_engine.sh + # test with fsdp 1 use_remove_padding and pad_mode no_padding + echo "run with sp4 fsdp_size4 num_gpus8 fsdp_strategy fsdp pad_mode no_padding use_remove_padding False" + BACKEND=fsdp SP_SIZE=1 FSDP_SIZE=-1 NUM_GPUS=8 FSDP_STRATEGY=fsdp PAD_MODE=no_padding USE_REMOVE_PADDING=False bash tests/special_e2e/sft/run_sft_engine.sh + # test with fsdp 2 + echo "run with sp2 fsdp_size2 num_gpus8 fsdp_strategy fsdp2" + BACKEND=fsdp SP_SIZE=2 FSDP_SIZE=2 NUM_GPUS=8 FSDP_STRATEGY=fsdp2 bash tests/special_e2e/sft/run_sft_engine.sh + # test with veomni + echo "run with sp2 fsdp_size4 num_gpus8 fsdp_strategy fsdp2" + BACKEND=veomni SP_SIZE=2 FSDP_SIZE=4 NUM_GPUS=8 FSDP_STRATEGY=fsdp2 bash tests/special_e2e/sft/run_sft_engine.sh + # test with megatron + echo "run with tp2 pp2 vpp2 cp2 num_gpus8" + BACKEND=megatron VANILLA_MBRIDGE=True TP_SIZE=2 PP_SIZE=2 VPP_SIZE=NULL CP_SIZE=2 NUM_GPUS=8 bash tests/special_e2e/sft/run_sft_engine.sh + # test with cp in ray + echo "run with tp2 pp2 vpp2 cp2 num_gpus8 mode=ray" + BACKEND=megatron VANILLA_MBRIDGE=True TP_SIZE=2 PP_SIZE=2 VPP_SIZE=NULL CP_SIZE=2 NUM_GPUS=8 mode=ray bash tests/special_e2e/sft/run_sft_engine.sh + rm -rf ~/verl/test/log diff --git a/verl_0720_main/verl/.github/workflows/e2e_sft_vlm.yml b/verl_0720_main/verl/.github/workflows/e2e_sft_vlm.yml new file mode 100644 index 0000000000000000000000000000000000000000..45cf2e1cadfa4f987a46b4cf6ba2d43f35d6c2ad --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/e2e_sft_vlm.yml @@ -0,0 +1,137 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: e2e_sft_vlm + +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" + + # Megatron + - "!verl/workers/**/megatron_*.py" + # Entrypoints + - ".github/workflows/e2e_sft_vlm.yml" + - "examples/data_preprocess/gsm8k.py" + - "tests/special_e2e/sft" + - "verl/trainer/fsdp_sft_trainer.py" + - "verl/trainer/config/sft_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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + e2e_sft_vlm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install peft + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install veomni==0.1.11 --ignore-requires-python --no-deps --index-url https://pypi.org/simple/ + pip3 install transformers==5.3.0 + pip3 uninstall -y "flash-attn-4" + pip uninstall -y mbridge + pip install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + pip install "nvidia-modelopt>=0.37.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare pokemon-gpt4o-captions dataset + run: | + ray stop --force + python3 examples/data_preprocess/pokemon.py --local_dataset_path ${HOME}/models/hf_data/pokemon-gpt4o-captions + - name: Running Pokemon E2E training tests with multiturn and various configs and compare results + run: | + MODEL_ID=Qwen/Qwen3-VL-2B-Instruct DATASET_DIR=~/data/pokemon-gpt4o-captions VPP_SIZE=null bash tests/special_e2e/sft/test_sft_engine_all.sh + + cleanup: + runs-on: ubuntu-latest + needs: [setup, e2e_sft_vlm] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/gpu_unit_tests.yml b/verl_0720_main/verl/.github/workflows/gpu_unit_tests.yml new file mode 100644 index 0000000000000000000000000000000000000000..0c3d3a46eab7e3b7995365dfadb355ff036f68d7 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/gpu_unit_tests.yml @@ -0,0 +1,137 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: GPU unit tests + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.4.x + paths: + - "**/*.py" + - .github/workflows/gpu_unit_tests.yml + pull_request: + branches: + - main + - v0.4.x + paths: + # The order that you define paths patterns matters: + # A matching negative pattern (prefixed with !) after a positive match will exclude the path. + # A matching positive pattern after a negative match will include the path again. + - "**/*.py" + # Other entrypoints + - "!examples/**" + - "!tests/special_sanity/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + # Entrypoints + - .github/workflows/gpu_unit_tests.yml + - "tests/**test_*.py" + # Ignore CPU tests + - "!tests/*_on_cpu.py" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + gpu_unit_tests: + if: github.repository_owner == 'verl-project' + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 60 # Increase this timeout value as needed + env: + HTTPS_PROXY: ${{ secrets.PROXY_HTTPS }} + NO_PROXY: "localhost,127.0.0.1" + HF_HUB_ENABLE_HF_TRANSFER: 1 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install hf_transfer + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda13x==13.6.0 pytest-asyncio + pip3 install --ignore-installed blinker mlflow + - name: Check final pip list + run: | + pip3 list + - name: Run all GPU unit tests + run: | + pytest -s -x --ignore-glob="*on_npu.py" --ignore-glob="*test_special_*.py" --ignore-glob='*on_cpu.py' --ignore-glob="*test_vllm*" --ignore-glob="*_sglang*" --ignore-glob="*_hf_rollout*" --ignore-glob="tests/models/" --ignore-glob='tests/special*' --ignore-glob="tests/experimental" --ignore-glob="tests/workers/reward_model" --ignore-glob="*test_shared_memory*" --ignore-glob="tests/workers/rollout/rollout_trtllm" --ignore-glob="*test_bucketed_weight_transfer*" tests/ + - name: Testing LinearCrossEntropyTP Correctness, Computation Time and Memory Consumption + run: | + LOW_MEMORY=True torchrun --standalone --nnodes=1 --nproc-per-node=8 tests/utils/test_special_linear_cross_entropy_tp.py + - name: Testing Megatron KL Loss TP Correctness + run: | + torchrun --standalone --nnodes=1 --nproc-per-node=2 tests/utils/test_special_megatron_kl_loss_tp.py + + cleanup: + runs-on: ubuntu-latest + needs: [setup, gpu_unit_tests] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/model.yml b/verl_0720_main/verl/.github/workflows/model.yml new file mode 100644 index 0000000000000000000000000000000000000000..32303df4a16477c450544a3606d2336637da8b08 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/model.yml @@ -0,0 +1,197 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: model + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/model.yml" + - "tests/special_distributed/test_fsdp_ckpt.py" + - "tests/special_distributed/test_tensor_dict.py" + - "tests/models/**" + - "tests/special_distributed/run_all.sh" + +# Declare permissions just read content. +permissions: + contents: read + +# 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' }} + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + model_rmpad: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 20 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository and upgrade to latest transformers + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - name: Running rmpad model tests on 8 L20 GPUs + flash_attn 2.5.8 + run: | + pytest -s tests/models/test_transformer.py + - name: Running rmpad model tests on 8 L20 GPUs + latest flash_attn + run: | + pytest -s tests/models/test_transformer.py + - name: Running FSDP rmpad model tests on 8 L20 GPUs + latest flash_attn + run: | + STRATEGY=fsdp torchrun --nproc_per_node=8 tests/special_distributed/test_fsdp_ckpt.py + - name: Running transformers ulysses tests on 8 L20 GPUs + latest transformers + run: | + torchrun --nproc_per_node=8 -m pytest tests/models/test_transformers_ulysses.py + - name: Running transformers ulysses tests on 8 L20 GPUs + transformers 4.54.1 + run: | + pip3 install transformers==4.54.1 + torchrun --nproc_per_node=8 -m pytest tests/models/test_transformers_ulysses.py + - name: Run distributed test + run: | + bash tests/special_distributed/run_all.sh + - name: Clean up and recover transformers + run: | + pip3 install --upgrade "transformers==5.3.0" + + # TODO: Move this back to model_rmpad once FSDP2 is stable. + # NOTE: List as an independent job to make rerun easier. + model_rmpad_fsdp2_unstable: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 20 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository and upgrade to latest transformers/flash_attn + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - name: Running FSDP2 rmpad model tests on 8 L20 GPUs + latest flash_attn + run: | + STRATEGY=fsdp2 torchrun --nproc_per_node=8 tests/special_distributed/test_fsdp_ckpt.py + + model_engine: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 20 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install --upgrade "accelerate>=1.13.0" + pip3 uninstall -y mbridge + pip3 install "nvidia-modelopt>=0.37.0" + pip3 install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@e2cfe7e --ignore-requires-python --no-deps --no-build-isolation + pip3 install git+https://github.com/NVIDIA/Megatron-LM.git@0823c73 --ignore-requires-python --no-deps --no-build-isolation + - name: Check final pip list + run: | + pip3 list + - name: Download model config files + run: | + hf download Qwen/Qwen2.5-0.5B-Instruct --local-dir $HOME/models/Qwen/Qwen2.5-0.5B-Instruct + - name: Running mcore engine tests on 8 L20 GPUs + run: | + ray stop --force + pytest -s -x tests/models/test_engine.py + + cleanup: + runs-on: ubuntu-latest + needs: [setup, model_rmpad, model_rmpad_fsdp2_unstable, model_engine] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/model_ascend.yml b/verl_0720_main/verl/.github/workflows/model_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..70e36cdbcddd375127f8286698353a1eabf06f37 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/model_ascend.yml @@ -0,0 +1,145 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: model_ascend + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/model_ascend.yml" + - "tests/special_distributed/test_fsdp_ckpt.py" + - "tests/special_distributed/test_tensor_dict.py" + - "tests/models/**" + - "tests/special_distributed/run_all.sh" + +# 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' }} + +permissions: + contents: read + +jobs: + model_rmpad_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e .[test] + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Running rmpad model tests on 8 NPUs + run: | + pytest -s tests/models/test_transformer.py + - name: Running FSDP rmpad model tests on 8 NPUs + run: | + STRATEGY=fsdp torchrun --nproc_per_node=8 tests/special_distributed/test_fsdp_ckpt.py + - name: Running transformers ulysses tests on 8 NPUs + run: | + torchrun --nproc_per_node=8 -m pytest tests/models/test_transformers_ulysses.py + - name: Run distributed test + run: | + bash tests/special_distributed/run_all.sh + + # TODO: Move this back to model_rmpad once FSDP2 is stable. + # TODO: Move test_engine.py's check_dtype=False, when npu's vllm0.23.0 is ok, run pytest -s -x tests/models/test_engine.py::test_actor_engine[fsdp] check it + # NOTE: List as an independent job to make rerun easier. + model_rmpad_fsdp2_unstable_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip install --no-deps -e .[test] + pip install -r requirements-test.txt + pip install --upgrade "accelerate>=1.13.0" + - name: Check final pip list + run: | + pip3 list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Running FSDP2 rmpad model tests on 8 NPUs + run: | + STRATEGY=fsdp2 torchrun --nproc_per_node=8 tests/special_distributed/test_fsdp_ckpt.py + - name: Running model_engine_ascend + run: | + ray stop --force + VANILLA_MBRIDGE=True pytest tests/models/test_engine.py diff --git a/verl_0720_main/verl/.github/workflows/nightly_ascend.yml b/verl_0720_main/verl/.github/workflows/nightly_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..f03bbf3b267bd61e9691c7a11a08cb6c266a14c4 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/nightly_ascend.yml @@ -0,0 +1,695 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: nightly_ci_ascend + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + # For push, for now only anti-patterns are specified so it is more conservative + # and achieves higher coverage. + schedule: + - cron: "0 17 * * *" + - cron: "0 18 * * *" + - cron: "0 19 * * *" + +# Declare permissions just read content. +permissions: + contents: read + +jobs: + # Test ppo qwen3-8b fsdp vllm + nightlyCI_ppo-qwen3-8b-fsdp-vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running nightlyCI_ppo-qwen3-8b-fsdp-vllm_ascend + run: | + ray stop --force + bash tests/special_npu/nightly_ci_ascend/run_ppo_qwen3-8b_fsdp_npu.sh + - name: Running nightlyCI_ppo-qwen3-8b-fsdp-vllm_ascend checking script + run: | + cd /root/.cache/nightly_log/ + python check_npu.py --log run_ppo_qwen3-8b_fsdp_npu/run_ppo_qwen3-8b_fsdp_npu.log --base run_ppo_qwen3-8b_fsdp_npu/baseline_ppo_qwen3-8b_fsdp_npu.txt + + # Test grpo qwen3_vl_8b_Instruct fsdp2 vllm + nightlyCI_grpo_qwen3_vl_8b_Instruct_fsdp2_vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running nightlyCI_grpo_qwen3_vl_8b_Instruct_fsdp2_vllm_ascend + run: | + ray stop --force + bash tests/special_npu/run_qwen3_vl_8b_Instruct_fsdp2_npu.sh + - name: Running nightlyCI_grpo_qwen3_vl_8b_Instruct_fsdp2_vllm_ascend checking script + run: | + cd /root/.cache/nightly_log/ + python check_npu.py --log run_qwen3_vl_8b_Instruct_fsdp2_npu/run_qwen3_vl_8b_Instruct_fsdp2_npu.log --base run_qwen3_vl_8b_Instruct_fsdp2_npu/baseline_qwen3_vl_8b_Instruct_fsdp2_npu.txt + + # Test grpo qwen3-8b mindspeedllm sglang + nightlyCI_grpo-qwen3-8b-mindspeedllm-sglang: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install --no-deps --no-build-isolation -e . + uv pip install --no-deps TransferQueue==0.1.8 + - name: Check final pip list + run: | + uv pip list + - name: Configure related dependencies + run: | + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git /Megatron-LM + rm -rf /MindSpeed + git clone https://gitcode.com/ascend/MindSpeed.git /MindSpeed + git clone https://gitcode.com/ascend/MindSpeed-LLM.git /MindSpeed-LLM + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running nightlyCI_grpo-qwen3-8b-mindspeedllm-sglang + run: | + ray stop --force + export PYTHONPATH=$PYTHONPATH:/Megatron-LM + export PYTHONPATH=$PYTHONPATH:/MindSpeed + export PYTHONPATH=$PYTHONPATH:/MindSpeed-LLM + rm -rf /root/.cache/torch_extensions/py311_cpu/npu_rotary_position_embedding + bash tests/special_npu/nightly_ci_ascend/run_grpo_qwen3_8b_mindspeedllm_npu.sh + - name: Running nightlyCI_grpo-qwen3-8b-mindspeedllm-sglang checking script + run: | + cd /root/.cache/nightly_log/ + python check_npu.py --log run_grpo_qwen3_8b_mindspeedllm_npu/run_grpo_qwen3_8b_mindspeedllm_npu.log --base run_grpo_qwen3_8b_mindspeedllm_npu/baseline_grpo_qwen3_8b_mindspeedllm_npu.txt + + # Test dapo moonlight-16b megatron vllm + nightlyCI_dapo-moonlight-16b-megatron-vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + clean: true + - name: Install the current repository + run: | + pip install -r requirements-npu.txt + pip install --no-deps -e . + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: update mbridge + run: | + # get mbridge path + MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') + # cuda to npu + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + - name: Running nightlyCI_dapo-moonlight-16b-megatron-vllm_ascend + run: | + ray stop --force + cd recipe + git checkout main + cd .. + export HCCL_OP_EXPANSION_MODE="AIV" + bash tests/special_npu/nightly_ci_ascend/run_dapo_moonlight-16b_megatron_npu.sh + + # Test gspo qwen3-30b megatron vllm + nightlyCI_gspo-qwen3-30b-megatron-vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 60g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Preprocess GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running nightlyCI_gspo-qwen3-30b-megatron-vllm_ascend + run: | + ray stop --force + bash tests/special_npu/nightly_ci_ascend/run_gspo_qwen3_30b_megatron_npu.sh + - name: Running nightlyCI_gspo-qwen3-30b-megatron-vllm_ascend checking script + run: | + cd /root/.cache/nightly_log/ + python check_npu.py --log run_gspo_qwen3_30b_megatron_npu/run_gspo_qwen3_30b_megatron_npu.log --base run_gspo_qwen3_30b_megatron_npu/baseline_gspo_qwen3_30b_megatron_npu.txt + + # Test grpo qwen3-30b megatron sglang + nightlyCI_grpo-qwen3-30b-megatron-sglang_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 60g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + submodules: recursive + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Running nightlyCI_grpo-qwen3-30b-megatron-sglang_ascend + run: | + ray stop --force + bash tests/special_npu/nightly_ci_ascend/run_grpo_qwen3_30b_megatron_sglang_npu.sh + + # Test grpo qwen3_5_2b fsdp2 vllm + nightlyCI_grpo_qwen3_5_2b_fsdp2_vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install -r requirements-npu.txt && pip install -v -e . + - name: Check final pip list + run: | + pip list + - name: Clone Megatron Bridge + run: | + git clone --depth 1 https://github.com/NVIDIA-NeMo/Megatron-Bridge.git /Megatron-Bridge + cd /Megatron-Bridge + git fetch --depth 1 origin de93536e9028ecf1e4dc28608dc80f336dcdfe59 + git checkout de93536e9028ecf1e4dc28608dc80f336dcdfe59 + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running nightlyCI_grpo_qwen3_5_2b_fsdp2_vllm_ascend + run: | + ray stop --force + export PYTHONPATH=/Megatron-Bridge/src:$PYTHONPATH + bash tests/special_npu/nightly_ci_ascend/run_grpo_qwen3_5_2b_fsdp2_npu.sh + +# Test quick start vllm fsdp2 + quick_start_qwen3_0_6b_fsdp2_vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e . + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running quick_start_qwen3_0_6b_fsdp2_vllm_ascend + run: | + ray stop --force + TOTAL_TRAINING_STEPS=1 bash tests/special_npu/quick_start/run_qwen3_0_6b_fsdp2_vllm_ascend.sh + + # Test quick start vllm megatron + quick_start_qwen3_0_6b_megatron_vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 17 * * *' }} + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running quick_start_qwen3_0_6b_megatron_vllm_ascend + run: | + ray stop --force + TOTAL_TRAINING_STEPS=1 bash tests/special_npu/quick_start/run_qwen3_0_6b_megatron_vllm_ascend.sh + + # Test quick start sglang megatron + quick_start_qwen3_0_6b_megatron_sglang_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 18 * * *' }} + runs-on: linux-aarch64-a3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running quick_start_qwen3_0_6b_megatron_sglang_ascend + run: | + ray stop --force + TOTAL_TRAINING_STEPS=1 bash tests/special_npu/quick_start/run_qwen3_0_6b_megatron_sglang_ascend.sh + + # Test quick start sglang fsdp2 + quick_start_qwen3_0_6b_fsdp2_sglang_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 18 * * *' }} + runs-on: linux-aarch64-a3-8 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip install --no-deps TransferQueue==0.1.8 + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running quick_start_qwen3_0_6b_fsdp2_sglang_ascend + run: | + ray stop --force + TOTAL_TRAINING_STEPS=1 bash tests/special_npu/quick_start/run_qwen3_0_6b_fsdp2_sglang_ascend.sh + + # Test grpo qwen3-30b veomni fsdp + nightlyCI_grpo-qwen3-30b-veomni-fsdp_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 19 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 60g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Install the current repository + run: | + uv pip install -r requirements-npu.txt && uv pip install -v -e . + uv pip install transformers==5.3.0 + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running nightlyCI_grpo-qwen3-30b-veomni-fsdp_ascend + run: | + ray stop --force + bash tests/special_npu/nightly_ci_ascend/run_grpo_qwen3_30b_veomni_fsdp.sh + + # Test gspo qwen3-8b fsdp vllm + nightlyCI_gspo-qwen3-8b-fsdp2-vllm_ascend: + if: ${{ github.repository_owner == 'verl-project' && github.event.schedule == '0 19 * * *' }} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare GSM8K dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Running nightlyCI_grpo-qwen3-8b-fsdp2-vllm_ascend + run: | + ray stop --force + bash tests/special_npu/nightly_ci_ascend/run_gspo_qwen3_8b_fsdp2_npu.sh + + # Test grpo qwen33.5-35b megatron vllm + nightlyCI_grpo-qwen3_5-35b-megatron-vllm_ascend: + if: ${{ github.repository_owner == 'verl-project'}} + runs-on: linux-aarch64-a3-16 + timeout-minutes: 180 # Increase this timeout value as needed + container: + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Check final pip list + run: | + pip list + - name: Clone Megatron Bridge + run: | + git clone --depth 1 https://github.com/NVIDIA-NeMo/Megatron-Bridge.git /Megatron-Bridge + cd /Megatron-Bridge + git fetch --depth 1 origin de93536e9028ecf1e4dc28608dc80f336dcdfe59 + git checkout de93536e9028ecf1e4dc28608dc80f336dcdfe59 + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Preprocess geo3k dataset + run: | + python examples/data_preprocess/geo3k.py --local_dataset_path ${HOME}/.cache/datasets/hiyouga/geometry3k + - name: Running nightlyCI_grpo-qwen3_5-35b-megatron-vllm_ascend + run: | + ray stop --force + export PYTHONPATH=/Megatron-Bridge/src:$PYTHONPATH + bash tests/special_npu/nightly_ci_ascend/run_grpo_qwen3_5_35b_megatron_npu.sh diff --git a/verl_0720_main/verl/.github/workflows/nightly_ascend_multinode.yml b/verl_0720_main/verl/.github/workflows/nightly_ascend_multinode.yml new file mode 100644 index 0000000000000000000000000000000000000000..4fb9d01c46237290054e60533cce5db17e72d25d --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/nightly_ascend_multinode.yml @@ -0,0 +1,389 @@ +name: nightly_ci_ascend_multinode + +on: + schedule: + - cron: "0 17 * * *" + +# Declare permissions just read content. +permissions: + contents: read + +env: + SUBMITTER_URL: http://rayjob-submitter.ci-system.svc.cluster.local + IMAGE: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + WORKER_REPLICAS: "2" + +jobs: + ascend-rayjob-e2e: + if: github.repository_owner == 'verl-project' + name: E2E Ascend RayJob multinode PPO + runs-on: linux-aarch64-a3-8 + timeout-minutes: 240 + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-a3-ubuntu22.04-py3.11-vllm + steps: + - name: Checkout source + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + + - name: Copy checkout to shared cache + run: | + rm -rf /root/.cache/cicd_test/verl + mkdir -p /root/.cache/cicd_test/verl + tar \ + --exclude=.git \ + --exclude=.venv \ + --exclude=__pycache__ \ + --exclude=.pytest_cache \ + -cf - . | tar -xf - -C /root/.cache/cicd_test/verl + chmod -R 777 /root/.cache/cicd_test/verl + ls -l /root/.cache/cicd_test + ls -l /root/.cache/cicd_test/verl + + - name: Prepare RayJob submit helper + run: | + cat > /root/.cache/cicd_test/submit_and_wait_rayjob.sh <<'EOF' + #!/usr/bin/env bash + set -euo pipefail + + name_suffix="$1" + script="$2" + args_json="${3:-}" + env_json="${4:-}" + template_patches_json="${5:-}" + + if [[ -z "${args_json}" ]]; then + args_json='[]' + fi + + if [[ -z "${env_json}" ]]; then + env_json='{}' + fi + + if [[ -z "${template_patches_json}" ]]; then + template_patches_json='[]' + fi + + : "${SUBMITTER_URL:?SUBMITTER_URL is required}" + : "${IMAGE:?IMAGE is required}" + : "${GITHUB_REPOSITORY:?GITHUB_REPOSITORY is required}" + : "${GITHUB_SHA:?GITHUB_SHA is required}" + : "${GITHUB_REF_NAME:?GITHUB_REF_NAME is required}" + : "${GITHUB_RUN_ID:?GITHUB_RUN_ID is required}" + + github_run_attempt="${GITHUB_RUN_ATTEMPT:-1}" + worker_replicas="${WORKER_REPLICAS:-2}" + + echo "========== Validate local JSON inputs ==========" + echo "args_json=${args_json}" + echo "env_json=${env_json}" + echo "template_patches_json=${template_patches_json}" + echo "worker_replicas=${worker_replicas}" + + args_json_compact="$(printf '%s' "${args_json}" | jq -c 'if type == "array" then . else error("args must be a JSON array") end')" + env_json_compact="$(printf '%s' "${env_json}" | jq -c 'if type == "object" then . else error("env must be a JSON object") end')" + template_patches_json_compact="$(printf '%s' "${template_patches_json}" | jq -c 'if type == "array" then . else error("template_patches must be a JSON array") end')" + + payload="$(jq -n \ + --arg repository "${GITHUB_REPOSITORY}" \ + --arg commit_sha "${GITHUB_SHA}" \ + --arg branch "${GITHUB_REF_NAME}" \ + --arg github_run_id "${GITHUB_RUN_ID}" \ + --arg github_run_attempt "${github_run_attempt}" \ + --arg image "${IMAGE}" \ + --arg name_suffix "${name_suffix}" \ + --arg script "${script}" \ + --argjson args "${args_json_compact}" \ + --argjson env "${env_json_compact}" \ + --argjson template_patches "${template_patches_json_compact}" \ + --arg worker_replicas "${worker_replicas}" \ + '{repository:$repository,commit_sha:$commit_sha,branch:$branch,github_run_id:$github_run_id,github_run_attempt:$github_run_attempt,image:$image,name_suffix:$name_suffix,script:$script,args:$args,env:$env,worker_replicas:($worker_replicas | tonumber),template_patches:$template_patches}')" + + echo "========== Submit RayJob payload ==========" + echo "${payload}" | jq . + + response_file="$(mktemp)" + http_code="$(curl -sS -o "${response_file}" -w "%{http_code}" -X POST "${SUBMITTER_URL}/api/v1/rayjobs" \ + -H "Content-Type: application/json" \ + -d "${payload}")" + response="$(cat "${response_file}")" + rm -f "${response_file}" + + echo "========== Submitter response ==========" + echo "http_code=${http_code}" + echo "${response}" + + if ! printf '%s' "${response}" | jq . >/dev/null 2>&1; then + echo "Submitter response is not valid JSON." + printf '%s\n' "${response}" + exit 1 + fi + + echo "${response}" | jq . + + if [[ "${http_code}" -lt 200 || "${http_code}" -ge 300 ]]; then + echo "Submitter returned non-2xx status: ${http_code}" + exit 1 + fi + + job_id="$(echo "${response}" | jq -r '.job_id // empty')" + if [[ -z "${job_id}" || "${job_id}" == "null" ]]; then + echo "Failed to parse job_id from submitter response." + exit 1 + fi + + final_status_file="/tmp/rayjob-final-status-${job_id}" + rm -f "${final_status_file}" + + set +e + curl -N -sS \ + --retry 5 \ + --retry-delay 10 \ + --retry-all-errors \ + "${SUBMITTER_URL}/api/v1/rayjobs/${job_id}/watch?format=ndjson" \ + | while IFS= read -r event; do + [[ -z "${event}" ]] && continue + if ! printf '%s' "${event}" | jq . >/dev/null 2>&1; then + echo "[watch raw] ${event}" + continue + fi + + type="$(printf '%s' "${event}" | jq -r '.type // empty')" + case "${type}" in + log) + printf '%s' "${event}" | jq -r '.line // ""' + ;; + status) + status="$(printf '%s' "${event}" | jq -r '.status // "UNKNOWN"')" + message="$(printf '%s' "${event}" | jq -r '.message // ""')" + rayjob_status="$(printf '%s' "${event}" | jq -r '.rayjob_status // ""')" + deployment_status="$(printf '%s' "${event}" | jq -r '.job_deployment_status // ""')" + echo "[rayjob status] status=${status} rayjob_status=${rayjob_status} deployment_status=${deployment_status} ${message}" + ;; + final) + status="$(printf '%s' "${event}" | jq -r '.status // "UNKNOWN"')" + message="$(printf '%s' "${event}" | jq -r '.message // ""')" + echo "[rayjob final] ${status} ${message}" + printf '%s' "${status}" > "${final_status_file}" + ;; + error) + message="$(printf '%s' "${event}" | jq -r '.message // ""')" + echo "[rayjob error] ${message}" + ;; + *) + echo "${event}" + ;; + esac + done + set -e + + final_status="" + if [[ -f "${final_status_file}" ]]; then + final_status="$(cat "${final_status_file}")" + fi + + case "${final_status}" in + SUCCEEDED) exit 0 ;; + FAILED|TIMEOUT|NOT_FOUND) exit 1 ;; + *) echo "Watch ended without successful final status."; exit 1 ;; + esac + EOF + sed -i 's/^ //' /root/.cache/cicd_test/submit_and_wait_rayjob.sh + chmod +x /root/.cache/cicd_test/submit_and_wait_rayjob.sh + cat /root/.cache/cicd_test/submit_and_wait_rayjob.sh + + - name: Prepare multinode testing helper + run: | + cat > /root/.cache/cicd_test/run_multinode_testing.sh <<'EOF' + #!/usr/bin/env bash + set -euxo pipefail + + CACHE_REPO=${CACHE_REPO:-/root/.cache/cicd_test/verl} + WORK_REPO=${WORK_REPO:-/verl} + EXPECTED_WORKERS=${EXPECTED_WORKERS:-2} + + script_path="" + script_args=() + + while [[ $# -gt 0 ]]; do + case "$1" in + --script-path) + script_path="${2:?--script-path requires a value}" + shift 2 + ;; + *) + script_args+=("$1") + shift + ;; + esac + done + + if [[ -z "${script_path}" ]]; then + echo "--script-path is required" >&2 + exit 1 + fi + + if [[ ! -d "${CACHE_REPO}" ]]; then + echo "${CACHE_REPO} does not exist" >&2 + exit 1 + fi + + copy_repo() { + rm -rf "${WORK_REPO}" + mkdir -p "${WORK_REPO}" + cp -a "${CACHE_REPO}/." "${WORK_REPO}/" + } + + install_repo() { + if ! command -v uv >/dev/null 2>&1; then + curl -LsSf https://astral.sh/uv/install.sh | sh + source "$HOME/.local/bin/env" + fi + + export PATH="$HOME/.local/bin:$PATH" + + cd "${WORK_REPO}" + uv pip list --system + uv pip install --system --no-deps -e "${WORK_REPO}" + uv pip list --system + } + + copy_repo + install_repo + + python3 - <<'PY' + import os + import socket + import subprocess + import ray + from ray.util.scheduling_strategies import NodeAffinitySchedulingStrategy + + CACHE_REPO = os.environ.get("CACHE_REPO", "/root/.cache/cicd_test/verl") + WORK_REPO = os.environ.get("WORK_REPO", "/verl") + EXPECTED_WORKERS = int(os.environ.get("EXPECTED_WORKERS", "2")) + + def run_cmd(cmd: str) -> None: + print(f"\n===== RUN: {cmd} =====", flush=True) + subprocess.run(cmd, shell=True, executable="/bin/bash", check=True) + + @ray.remote(num_cpus=1) + def setup_worker() -> str: + hostname = socket.gethostname() + print(f"===== Worker setup starts on {hostname} =====", flush=True) + + run_cmd('cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info') + run_cmd("npu-smi info") + run_cmd( + f""" + rm -rf {WORK_REPO} + mkdir -p {WORK_REPO} + ls -l {CACHE_REPO} + cp -a {CACHE_REPO}/. {WORK_REPO}/ + + if ! command -v uv >/dev/null 2>&1; then + curl -LsSf https://astral.sh/uv/install.sh | sh + source "$HOME/.local/bin/env" + fi + + export PATH="$HOME/.local/bin:$PATH" + + cd {WORK_REPO} + uv pip list --system + uv pip install --system --no-deps -e {WORK_REPO} + uv pip list --system + + python examples/data_preprocess/gsm8k.py --local_dataset_path "$HOME/.cache/datasets/openai/gsm8k" + ls -lh "$HOME/data/gsm8k" + """ + ) + + print(f"===== Worker setup finished on {hostname} =====", flush=True) + return hostname + + ray.init(address="auto") + + worker_nodes = [] + for node in ray.nodes(): + if not node.get("Alive", False): + continue + if float(node.get("Resources", {}).get("CPU", 0)) > 0: + worker_nodes.append(node) + + print("===== Ray alive worker nodes =====", flush=True) + for node in worker_nodes: + print( + { + "NodeID": node["NodeID"], + "NodeManagerAddress": node.get("NodeManagerAddress"), + "Resources": node.get("Resources"), + }, + flush=True, + ) + + if len(worker_nodes) != EXPECTED_WORKERS: + raise RuntimeError(f"Expected {EXPECTED_WORKERS} workers, found {len(worker_nodes)}") + + refs = [ + setup_worker.options( + scheduling_strategy=NodeAffinitySchedulingStrategy(node_id=node["NodeID"], soft=False) + ).remote() + for node in worker_nodes + ] + + print(f"===== Worker setup completed on: {ray.get(refs)} =====", flush=True) + PY + + cd "${WORK_REPO}" + bash "${script_path}" "${script_args[@]}" + rm -rf "$HOME/ckpts" + EOF + sed -i 's/^ //' /root/.cache/cicd_test/run_multinode_testing.sh + chmod +x /root/.cache/cicd_test/run_multinode_testing.sh + cat /root/.cache/cicd_test/run_multinode_testing.sh + + - name: Setup and Verify Cluster Network + run: | + echo "==== [1/2] Setting up network routing (Injecting K8s CoreDNS) ====" + sh -c 'cat < /etc/resolv.conf + nameserver 10.247.3.10 + search ci-system.svc.cluster.local svc.cluster.local cluster.local + options ndots:5 + EOF' + + cat /etc/resolv.conf + + echo "==== nsswitch ====" + cat /etc/nsswitch.conf || true + grep '^hosts:' /etc/nsswitch.conf || true + + echo "==== getent test ====" + getent hosts kubernetes.default.svc.cluster.local || true + getent hosts rayjob-submitter.ci-system.svc.cluster.local || true + + echo "==== [2/2] Testing network connectivity via healthz ====" + curl -sS --fail "${SUBMITTER_URL}/healthz" + + - name: Run multinode E2E RayJob + run: | + /root/.cache/cicd_test/submit_and_wait_rayjob.sh \ + multinode-e2e \ + /root/.cache/cicd_test/run_multinode_testing.sh \ + '["--script-path","/verl/tests/special_npu/run_gspo_qwen3_30b_megatron_npu_multinode.sh"]' \ + '{}' \ + '[ + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/requests/cpu","value":"64"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/requests/memory","value":"512Gi"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/requests/huawei.com~1ascend-1980","value":"16"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/limits/cpu","value":"64"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/limits/memory","value":"512Gi"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/containers/0/resources/limits/huawei.com~1ascend-1980","value":"16"}, + {"op":"replace","path":"/spec/rayClusterSpec/workerGroupSpecs/0/template/spec/volumes/1/emptyDir/sizeLimit","value":"128Gi"} + ]' + + - name: Cleanup RayJobs + if: always() + run: | + curl -sS -X DELETE \ + "${SUBMITTER_URL}/api/v1/workflows/${GITHUB_RUN_ID}?attempt=${GITHUB_RUN_ATTEMPT:-1}" diff --git a/verl_0720_main/verl/.github/workflows/npu_unit_tests.yml b/verl_0720_main/verl/.github/workflows/npu_unit_tests.yml new file mode 100644 index 0000000000000000000000000000000000000000..95afdda7059773630b03e7c9f5a0eb77a921e46c --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/npu_unit_tests.yml @@ -0,0 +1,120 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - `npu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix on ascend device. +# - Since cpu/gpu/npu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: NPU unit tests + +on: + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - .github/workflows/npu_unit_tests.yml + pull_request: + branches: + - main + paths: + # The order that you define paths patterns matters: + # A matching negative pattern (prefixed with !) after a positive match will exclude the path. + # A matching positive pattern after a negative match will include the path again. + - "**/*.py" + # Other entrypoints + - "!examples/**" + - "!tests/special_sanity/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" + - "!recipe/**" + # Entrypoints + - .github/workflows/npu_unit_tests.yml + - "tests/**test_*.py" + # Ignore CPU tests + - "!tests/*_on_cpu.py" + +# 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: + npu_unit_tests: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e .[test] + pip install mlflow pytest-asyncio + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Run all NPU unit tests + run: | + pytest -s -x --ignore-glob="*test_special_*.py" --ignore-glob="*on_cpu.py" --ignore-glob="*test_vllm*" --ignore-glob="*_sglang*" --ignore-glob="*_hf_rollout*" --ignore-glob="tests/models/" --ignore-glob="tests/special*" --ignore-glob="tests/experimental" --ignore-glob="tests/workers/reward_model" --ignore-glob="*test_rvdz*" --ignore-glob="*test_ray_collectives*" --ignore-glob="*test_nvtx_profile*" --ignore-glob="tests/checkpoint_engine" --ignore-glob="*test_shared_memory*" --ignore-glob="tests/workers/rollout/rollout_trtllm" --ignore-glob="*test_fsdp_lora_merge*" --ignore-glob="*test_optimizer_offload_and_load.py*" tests/ -k "not test_preprocess_bshd_engine_preserves_topk_dense_dim_on_gpu and not test_sender_accepts_strided_tensor" + - name: Running NPU profiling unit tests + run: | + pytest -s -x tests/utils/test_special_mstx_profile.py + - name: Running NPU checkpoint_engine unit tests + run: | + pytest -s -x tests/checkpoint_engine/test_correctness_on_npu.py + - name: Running CPU checkpoint_engine unit tests + run: | + pytest -s -x tests/checkpoint_engine/test_global_steps_on_cpu.py diff --git a/verl_0720_main/verl/.github/workflows/pre-commit.yml b/verl_0720_main/verl/.github/workflows/pre-commit.yml new file mode 100644 index 0000000000000000000000000000000000000000..4f6aa4bdf0d10048057777269df9507188b3a264 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/pre-commit.yml @@ -0,0 +1,41 @@ +# c.f. https://github.com/pre-commit/action?tab=readme-ov-file#using-this-action +name: pre-commit + +# No need to avoid / cancel lightweight pre-commit jobs +on: + schedule: + - cron: "0 0 * * 0" + pull_request: + push: + branches: + - main + - v0.* + # Allow manual triggering + workflow_dispatch: + +# Declare permissions just read content. +permissions: + contents: read + +jobs: + pre-commit: + 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: Install the current repository + run: | + pip install pre-commit hydra-core + pip install --no-deps -e . + - 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 + # Check "--all-files" by default + - uses: pre-commit/action@v3.0.1 diff --git a/verl_0720_main/verl/.github/workflows/precommit-autofix.yml b/verl_0720_main/verl/.github/workflows/precommit-autofix.yml new file mode 100644 index 0000000000000000000000000000000000000000..d235da90cd21ce429559eccf962fdb4eee7a5252 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/precommit-autofix.yml @@ -0,0 +1,52 @@ +name: scheduled pre-commit autofix + +on: + schedule: + # Every hour + - cron: "0 * * * *" + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + precommit: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install pre-commit + run: | + python -m pip install --upgrade pip + pip install pre-commit hydra-core + + - name: Run pre-commit + run: | + pre-commit run --all-files || true + + - name: Create or update PR + uses: peter-evans/create-pull-request@v6 + with: + branch: bot/precommit-autofix + delete-branch: true + title: "[ci] chore: scheduled pre-commit autofix" + commit-message: "chore: auto-fix pre-commit issues" + body: | + This PR was created automatically by a scheduled GitHub Action. + + - Runs `pre-commit run --all-files` + - Triggered hourly + labels: | + automated + pre-commit diff --git a/verl_0720_main/verl/.github/workflows/reward_model_sglang.yml b/verl_0720_main/verl/.github/workflows/reward_model_sglang.yml new file mode 100644 index 0000000000000000000000000000000000000000..50e064a030bd36578e365dd282d44b8ab337ddf4 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/reward_model_sglang.yml @@ -0,0 +1,134 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: reward_model_sglang + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/reward_model_sglang.yml" + - "tests/experimental/reward_loop/**" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + reward_model_sglang: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK: "True" + NCCL_SHM_DISABLE: "1" + NCCL_P2P_DISABLE: "1" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + pip3 install sglang-router==0.2.2 + - name: Check final pip list + run: | + pip3 list + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k --local_dir ${HOME}/data/gsm8k + - name: Running sglang generative reward model tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_reward_model_genrm.py + - name: Running sglang discriminative reward model tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_reward_model_disrm.py + # FIXME(@yyDing1): broken + # - name: Running sglang agent loop with reward manager tests on 8 L20 GPUs + # run: | + # ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_standalone.py + # - name: Running sglang agent loop with reward model colocate tests on 8 L20 GPUs + # run: | + # ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_colocate.py + + cleanup: + runs-on: ubuntu-latest + needs: [setup, reward_model_sglang] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/reward_model_sglang_ascend.yml b/verl_0720_main/verl/.github/workflows/reward_model_sglang_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..54c4c20f08f5828fb660e4ccd1e7c3064eed2e6f --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/reward_model_sglang_ascend.yml @@ -0,0 +1,128 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: reward_model_sglang_ascend + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/reward_model_sglang_ascend.yml" + - "tests/experimental/reward_loop/**" + +# 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: + reward_model_sglang_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES: "1" + ASCEND_RT_VISIBLE_DEVICES: "0,1,2,3,4,5,6,7" + HCCL_HOST_SOCKET_PORT_RANGE: "60000-60050" + HCCL_NPU_SOCKET_PORT_RANGE: "61000-61050" + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install pytest pytest-asyncio + uv pip install --no-deps --no-build-isolation -e . + uv pip install sglang-router==0.2.2 + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k --local_dir ${HOME}/data/gsm8k + - name: Running sglang generative reward model tests on 8 NPUs + run: | + ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_reward_model_genrm.py + - name: Running sglang discriminative reward model tests on 8 NPUs + run: | + ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_reward_model_disrm.py + # FIXME(@yyDing1): broken + # - name: Running sglang agent loop with reward manager tests on 8 NPUs + # run: | + # ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_standalone.py + # - name: Running sglang agent loop with reward model colocate tests on 8 NPUs + # run: | + # ROLLOUT_NAME=sglang pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_colocate.py diff --git a/verl_0720_main/verl/.github/workflows/reward_model_vllm.yml b/verl_0720_main/verl/.github/workflows/reward_model_vllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..8564b617db367e377a499653b303a8e8e90ce0ba --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/reward_model_vllm.yml @@ -0,0 +1,138 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: reward_model_vllm + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/reward_model_vllm.yml" + - "tests/experimental/reward_loop/**" + - "tests/special_e2e/run_v1_colocate_async_disrm.sh" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + reward_model_vllm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 30 # Increase this timeout value as needed + env: + 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 + SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK: "True" + NCCL_SHM_DISABLE: "1" + NCCL_P2P_DISABLE: "1" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k --local_dir ${HOME}/data/gsm8k + - name: Running vllm generative reward model tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_reward_model_genrm.py + - name: Running vllm discriminative reward model tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_reward_model_disrm.py + + - name: Running vllm agent loop with reward manager tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_standalone.py + - name: Running vllm agent loop with reward model colocate tests on 8 L20 GPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_colocate.py + - name: Running V1 colocate_async trainer with colocated disRM e2e on 8 L20 GPUs + run: | + ray stop --force + NUM_GPUS=8 ROLLOUT_NAME=vllm bash tests/special_e2e/run_v1_colocate_async_disrm.sh + + cleanup: + runs-on: ubuntu-latest + needs: [setup, reward_model_vllm] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/reward_model_vllm_ascend.yml b/verl_0720_main/verl/.github/workflows/reward_model_vllm_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..99fea92037afcfcb6dd820f5dcfcbd2ab5b0f5da --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/reward_model_vllm_ascend.yml @@ -0,0 +1,112 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: reward_model_vllm_ascend + +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: + - "verl/**/*.py" + # Entrypoints + - ".github/workflows/reward_model_vllm_ascend.yml" + - "tests/experimental/reward_loop/**" + +# 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: + reward_model_vllm_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip install --no-deps -e .[test] + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k --local_dir ${HOME}/data/gsm8k + - name: Running vllm generative reward model tests on 8 NPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_reward_model_genrm.py + - name: Running vllm discriminative reward model tests on 8 NPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_reward_model_disrm.py + - name: Running vllm agent loop with reward manager tests on 8 NPUs + run: | + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_standalone.py + - name: Running vllm agent loop with reward model colocate tests on 8 NPUs + run: | + export HCCL_HOST_SOCKET_PORT_RANGE=auto + export HCCL_NPU_SOCKET_PORT_RANGE=auto + ROLLOUT_NAME=vllm pytest -s -x tests/experimental/reward_loop/test_agent_reward_loop_colocate.py diff --git a/verl_0720_main/verl/.github/workflows/sanity.yml b/verl_0720_main/verl/.github/workflows/sanity.yml new file mode 100644 index 0000000000000000000000000000000000000000..7e11882fef6cb9ef6cdfea81a870c898efe90e46 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/sanity.yml @@ -0,0 +1,89 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. +# name: Check PR Title + +name: sanity + +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" + - .github/workflows/sanity.yml + - "tests/special_sanity/**" + +# 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: + sanity: + runs-on: ubuntu-latest + timeout-minutes: 5 # Increase this timeout value as needed + strategy: + matrix: + python-version: ["3.10"] + 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: Install the current repository + run: | + pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + # Most sanity checks now run via pre-commit (see .pre-commit-config.yaml + # and .github/workflows/pre-commit.yml). Only checks that need the full + # verl installation or CI-only context remain here. + - name: Run sanity test + run: | + pytest -s -x tests/special_sanity + - name: Assert documentation requirement for functions + run: python3 tests/special_sanity/validate_imported_docs.py diff --git a/verl_0720_main/verl/.github/workflows/scorecard.yml b/verl_0720_main/verl/.github/workflows/scorecard.yml new file mode 100644 index 0000000000000000000000000000000000000000..176d15ae2bd470752daaf138fa5aaa90641738e9 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/scorecard.yml @@ -0,0 +1,66 @@ +# This workflow uses actions that are not certified by GitHub. They are provided +# by a third-party and are governed by separate terms of service, privacy +# policy, and support documentation. + +name: Scorecard supply-chain security +on: + # For Branch-Protection check. Only the default branch is supported. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#branch-protection + branch_protection_rule: + # To guarantee Maintained check is occasionally updated. See + # https://github.com/ossf/scorecard/blob/main/docs/checks.md#maintained + schedule: + - cron: "27 7 * * 1" + push: + branches: + - main + - v0.* + +# Declare default permissions as read only. +permissions: read-all + +jobs: + analysis: + name: Scorecard analysis + runs-on: ubuntu-latest + permissions: + # Needed to upload the results to code-scanning dashboard. + security-events: write + # Needed to publish results and get a badge (see publish_results below). + id-token: write + # Uncomment the permissions below if installing in a private repository. + # contents: read + # actions: read + + steps: + - name: "Checkout code" + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + persist-credentials: false + + - name: "Run analysis" + uses: ossf/scorecard-action@0864cf19026789058feabb7e87baa5f140aac736 # v2.3.1 + with: + results_file: results.sarif + results_format: sarif + # (Optional) "write" PAT token. Uncomment the `repo_token` line below if: + # - you want to enable the Branch-Protection check on a *public* repository, or + # - you are installing Scorecard on a *private* repository + # To create the PAT, follow the steps in https://github.com/ossf/scorecard-action?tab=readme-ov-file#authentication-with-fine-grained-pat-optional. + # repo_token: ${{ secrets.SCORECARD_TOKEN }} + + # Public repositories: + # - Publish results to OpenSSF REST API for easy access by consumers + # - Allows the repository to include the Scorecard badge. + # - See https://github.com/ossf/scorecard-action#publishing-results. + # For private repositories: + # - `publish_results` will always be set to `false`, regardless + # of the value entered here. + publish_results: true + + # Upload the results to GitHub's code scanning dashboard (optional). + # Commenting out will disable upload of results to your repo's Code Scanning dashboard + - name: "Upload to code-scanning" + uses: github/codeql-action/upload-sarif@9e8d0789d4a0fa9ceb6b1738f7e269594bdd67f0 #v3.28.9 + with: + sarif_file: results.sarif diff --git a/verl_0720_main/verl/.github/workflows/secrets_scan.yml b/verl_0720_main/verl/.github/workflows/secrets_scan.yml new file mode 100644 index 0000000000000000000000000000000000000000..298ed16c668c67facdb6af2119878da576f5bdf5 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/secrets_scan.yml @@ -0,0 +1,22 @@ +on: + push: + branches: + - main + - v0.* + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 + with: + fetch-depth: 0 + - name: Secret Scanning + uses: trufflesecurity/trufflehog@7dc056a193116ba8d82154bf0549381c8fb8545c # v3.88.14 + with: + extra_args: --results=verified,unknown diff --git a/verl_0720_main/verl/.github/workflows/sgl.yml b/verl_0720_main/verl/.github/workflows/sgl.yml new file mode 100644 index 0000000000000000000000000000000000000000..230be239c521768f572971c03da49ed45ba8bd69 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/sgl.yml @@ -0,0 +1,175 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: sgl + +on: + # workflow_dispatch: # Manual + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - .github/workflows/sgl.yml + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + # vLLM + - "!**/*vllm*" + + # Entrypoints + - ".github/workflows/sgl.yml" + - "tests/rollout/*sglang*" + - "tests/rollout/async_rollout_utils.py" + +# 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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:sgl0512.dev2" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + sgl: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 35 # Increase this timeout value as needed + env: + 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: 1 + SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK: "True" + NCCL_SHM_DISABLE: "1" + NCCL_P2P_DISABLE: "1" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install cupy-cuda13x==13.6.0 pytest-asyncio transformers==5.3.0 + pip3 install hf_transfer pytest-asyncio + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Test the latest SGLang Rollout async with agent loop + run: | + ROLLOUT_NAME=sglang pytest -svvv tests/experimental/agent_loop + - name: Test SGLang agent loop with Continuous Token + run: | + ray stop --force + ENABLE_CONTINUOUS_TOKEN=1 ROLLOUT_NAME=sglang pytest -svvv \ + tests/experimental/agent_loop/test_basic_agent_loop.py::test_single_turn \ + tests/experimental/agent_loop/test_basic_agent_loop.py::test_tool_agent + + sgl_checkpoint_engine: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 35 # Increase this timeout value as needed + env: + 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: 1 + SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK: "True" + NCCL_SHM_DISABLE: "1" + NCCL_P2P_DISABLE: "1" + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install cupy-cuda13x==13.6.0 pytest-asyncio + pip3 install hf_transfer pytest-asyncio + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + - name: Test SGLang ServerAdapter with Checkpoint Engine (NCCL) + run: | + ROLLOUT_NAME=sglang pytest -svvv tests/checkpoint_engine/test_special_server_adapter.py + + cleanup: + runs-on: ubuntu-latest + needs: [setup, sgl, sgl_checkpoint_engine] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/sgl_ascend.yml b/verl_0720_main/verl/.github/workflows/sgl_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..3500e9e9cd7bb65b381fa4e2f11bf3bbf4a39234 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/sgl_ascend.yml @@ -0,0 +1,137 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: sglang_ascend + +on: + # workflow_dispatch: # Manual + # Trigger the workflow on push or pull request, + # but only for the main branch + push: + branches: + - main + - v0.* + paths: + - "**/*.py" + - .github/workflows/sgl_ascend.yml + pull_request: + branches: + - main + - v0.* + paths: + - "**/*.py" + # Other entrypoints + - "!examples/**" + - "!tests/**" + - "!verl/trainer/main_*.py" + - "!verl/trainer/fsdp_sft_trainer.py" # FSDP + - "!verl/workers/**/*dp_*.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + # vLLM + - "!**/*vllm*" + + # Entrypoints + - ".github/workflows/sgl_ascend.yml" + - "tests/rollout/*sglang*" + - "tests/rollout/async_rollout_utils.py" + +# 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: + sglang_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a3-8 + timeout-minutes: 90 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann8.5.0-torch_npu2.8.0post2-a3-ubuntu22.04-py3.11-sglang + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES: "1" + ASCEND_RT_VISIBLE_DEVICES: "0,1,2,3,4,5,6,7" + HCCL_HOST_SOCKET_PORT_RANGE: "60000-60050" + HCCL_NPU_SOCKET_PORT_RANGE: "61000-61050" + ENGINE: sglang + UV_EXTRA_INDEX_URL: "https://repo.huaweicloud.com/ascend/repos/pypi" + UV_INDEX_STRATEGY: unsafe-best-match + UV_INSECURE_HOST: cache-service.nginx-pypi-cache.svc.cluster.local + UV_HTTP_TIMEOUT: 120 + UV_NO_CACHE: "1" + UV_SYSTEM_PYTHON: "1" + UV_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_INDEX_URL: "http://cache-service.nginx-pypi-cache.svc.cluster.local/pypi/simple" + PIP_TRUSTED_HOST: "cache-service.nginx-pypi-cache.svc.cluster.local" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Install uv + run: pip install uv + - name: Check initial pip list from image + run: | + uv pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + uv pip install pytest pytest-asyncio + uv pip install megatron-bridge --no-deps + uv pip install git+https://github.com/ISEEKYAN/mbridge.git@main --no-deps --no-build-isolation + uv pip install --no-deps --no-build-isolation -e . + - name: Check final pip list + run: | + uv pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare gsm8k dataset + run: | + python examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k + - name: Test the latest vLLM Rollout async with agent loop + run: | + export HCCL_HOST_SOCKET_PORT_RANGE=auto + export HCCL_NPU_SOCKET_PORT_RANGE=auto + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + ROLLOUT_NAME=sglang pytest -svvv --ignore=tests/experimental/agent_loop/test_multi_modal.py -k "not test_standalone_rollout[4]" tests/experimental/agent_loop diff --git a/verl_0720_main/verl/.github/workflows/type-coverage-check.yml b/verl_0720_main/verl/.github/workflows/type-coverage-check.yml new file mode 100644 index 0000000000000000000000000000000000000000..268f0c672f0f87e8437d7dc964ee464922ec5d4e --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/type-coverage-check.yml @@ -0,0 +1,31 @@ +name: Type Annotation and Docstring Coverage + +on: + pull_request: + paths: + - '**/*.py' + - '.github/workflows/type-coverage-check.yml' + +jobs: + type-coverage-check: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 # 🚨 Important: fetch full history so `origin/main` is available + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + pip3 install torch torchvision --index-url https://download.pytorch.org/whl/cpu + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Run type annotation coverage check + run: | + python3 tests/special_sanity/type_coverage_check.py + - name: Run docstring coverage check + run: | + python3 tests/special_sanity/check_api_docs.py verl diff --git a/verl_0720_main/verl/.github/workflows/vllm.yml b/verl_0720_main/verl/.github/workflows/vllm.yml new file mode 100644 index 0000000000000000000000000000000000000000..4a7f83016084487bcd791da34c0305e5a0c3dcfe --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/vllm.yml @@ -0,0 +1,183 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: vllm + +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" + # FSDP + - "!verl/workers/**/*dp_*.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + # SGLang + - "!**/*sglang*" + # Entrypoints + - ".github/workflows/vllm.yml" + - "tests/special_e2e/generation" + - "tests/workers/rollout" + - "verl/trainer/main_generation.py" + - "verl/trainer/config/generation.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 + +env: + IMAGE: "verl-ci-cn-beijing.cr.volces.com/verlai/verl:vllm023.dev1" + DYNAMIC_RUNNER_ENDPOINT: "https://sd10g3clalm04ug7alq90.apigateway-cn-beijing.volceapi.com/runner" + +jobs: + setup: + if: github.repository_owner == 'verl-project' + runs-on: ubuntu-latest + outputs: + runner-label: ${{ steps.create-runner.outputs.runner-label }} + mlp-task-id: ${{ steps.create-runner.outputs.mlp-task-id }} + steps: + - uses: actions/checkout@v4 + - id: create-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "create" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-image: "${{ env.IMAGE }}" + + vllm: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 35 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install -r requirements-test.txt + pip3 install -r requirements.txt + pip3 install --no-deps -e . + - name: Check final pip list + run: | + pip3 list + # - name: Download Model to Use + # run: | + # hf download Qwen/Qwen2.5-0.5B-Instruct --local-dir ${HOME}/models/Qwen/Qwen2.5-0.5B-Instruct + # hf download Qwen/Qwen2.5-1.5B-Instruct --local-dir ${HOME}/models/Qwen/Qwen2.5-1.5B-Instruct + # hf download Qwen/Qwen2.5-VL-3B-Instruct --local-dir ${HOME}/models/Qwen/Qwen2.5-VL-3B-Instruct + # hf download OldKingMeister/Qwen2.5-1.5B-Instruct-YaRN --local-dir ${HOME}/models/OldKingMeister/Qwen2.5-1.5B-Instruct-YaRN + # export HF_HUB_OFFLINE=1 + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Test the latest vLLM Rollout async with agent loop + run: | + ROLLOUT_NAME=vllm pytest -svvv tests/experimental/agent_loop + - name: Test vLLM agent loop with Continuous Token + run: | + ray stop --force + ENABLE_CONTINUOUS_TOKEN=1 ROLLOUT_NAME=vllm pytest -svvv \ + tests/experimental/agent_loop/test_basic_agent_loop.py::test_single_turn \ + tests/experimental/agent_loop/test_basic_agent_loop.py::test_tool_agent + - name: Test vllm server abort functionality + run: | + pytest tests/workers/rollout/rollout_vllm/test_vllm_abort.py -v -s + - name: Test vllm rollout generation determinism (same-instance + cross-instance + cross-instance AgentLoop) + run: | + VLLM_DETERMINISM_DENSE_MODEL_PATH=${HOME}/models/Qwen/Qwen2.5-0.5B-Instruct \ + VLLM_DETERMINISM_N_GPUS=2 \ + pytest tests/workers/rollout/rollout_vllm/test_vllm_generation_determinism.py -v -s + + vllm_checkpoint_engine: + needs: setup + runs-on: ["${{ needs.setup.outputs.runner-label || 'L20x8' }}"] + timeout-minutes: 35 # Increase this timeout value as needed + env: + 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 + steps: + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 + with: + fetch-depth: 0 + - name: Install the current repository + run: | + pip3 install pytest-asyncio + pip3 install -r requirements-test.txt + pip3 install --no-deps -e . + pip3 install cupy-cuda13x==13.6.0 + - name: Check final pip list + run: | + pip3 list + - name: Test vLLM ServerAdapter with Checkpoint Engine (NCCL) + run: | + ROLLOUT_NAME=vllm pytest -svvv tests/checkpoint_engine/test_special_server_adapter.py + - name: Test bucketed weight transfer + run: | + pytest -svvv tests/utils/test_bucketed_weight_transfer.py + + cleanup: + runs-on: ubuntu-latest + needs: [setup, vllm, vllm_checkpoint_engine] + if: always() + steps: + - id: destroy-runner + uses: volcengine/vemlp-github-runner@v1 + with: + mode: "destroy" + faas-url: "${{ env.DYNAMIC_RUNNER_ENDPOINT }}" + mlp-task-id: "${{ needs.setup.outputs.mlp-task-id }}" diff --git a/verl_0720_main/verl/.github/workflows/vllm_ascend.yml b/verl_0720_main/verl/.github/workflows/vllm_ascend.yml new file mode 100644 index 0000000000000000000000000000000000000000..969cf90b43f2faaeb1a82bbccdcfefd1a20bb664 --- /dev/null +++ b/verl_0720_main/verl/.github/workflows/vllm_ascend.yml @@ -0,0 +1,124 @@ +# # Tests layout + +# Each folder under tests/ corresponds to a test category for a sub-namespace in verl. For instance: +# - `tests/trainer` for testing functionality related to `verl/trainer` +# - `tests/models` for testing functionality related to `verl/models` +# - ... + +# There are a few folders with `special_` prefix, created for special purposes: +# - `special_distributed`: unit tests that must run with multiple GPUs +# - `special_e2e`: end-to-end tests with training/generation scripts +# - `special_npu`: tests for NPUs +# - `special_sanity`: a suite of quick sanity tests +# - `special_standalone`: a set of test that are designed to run in dedicated environments + +# Accelerators for tests +# - By default tests are run with GPU available, except for the ones under `special_npu`, and any test script whose name ends with `on_cpu.py`. +# - For test scripts with `on_cpu.py` name suffix would be tested on CPU resources in linux environment. + +# # Workflow layout + +# All CI tests are configured by yaml files in `.github/workflows/`. Here's an overview of all test configs: +# 1. A list of always triggered CPU sanity tests: `check-pr-title.yml`, `secrets_scan.yml`, `check-pr-title,yml`, `pre-commit.yml`, `doc.yml` +# 2. Some heavy multi-GPU unit tests, such as `model.yml`, `vllm.yml`, `sgl.yml` +# 3. End-to-end tests: `e2e_*.yml` +# 4. Unit tests +# - `cpu_unit_tests.yml`, run pytest on all scripts with file name pattern `tests/**/test_*_on_cpu.py` +# - `gpu_unit_tests.yml`, run pytest on all scripts with file without the `on_cpu.py` suffix. +# - Since cpu/gpu unit tests by default runs all tests under `tests`, please make sure tests are manually excluded in them when +# - new workflow yaml is added to `.github/workflows` +# - new tests are added to workflow mentioned in 2. + +name: vllm_ascend + +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" + # FSDP + - "!verl/workers/**/*dp_*.py" + # Megatron + - "!verl/workers/**/megatron_*.py" + # SGLang + - "!**/*sglang*" + # Entrypoints + - ".github/workflows/vllm_ascend.yml" + - "tests/special_e2e/generation" + - "tests/workers/rollout" + - "verl/trainer/main_generation.py" + - "verl/trainer/config/generation.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: + vllm_ascend: + if: github.repository_owner == 'verl-project' + runs-on: linux-aarch64-a2b3-8 + timeout-minutes: 60 # Increase this timeout value as needed + container: + image: swr.cn-southwest-2.myhuaweicloud.com/modelfoundry/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" # This is more stable + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: | + pip list + - name: Checkout verl-project/verl repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install the current repository + run: | + pip3 install --no-deps -e .[test] + pip3 install pytest-asyncio + - name: Check final pip list + run: | + pip list + - name: Prepare weights + run: | + ln -s /root/.cache/models ~/models + - name: Prepare gsm8k dataset + run: | + ray stop --force + python3 examples/data_preprocess/gsm8k.py --local_dataset_path ${HOME}/models/hf_data/gsm8k + - name: Test the latest vLLM Rollout async with agent loop with seed 0(non deter) + run: | + export HCCL_HOST_SOCKET_PORT_RANGE=auto + export HCCL_NPU_SOCKET_PORT_RANGE=auto + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + ROLLOUT_NAME=vllm pytest -svvv tests/experimental/agent_loop + - name: Test vllm server abort functionality + run: | + pytest tests/workers/rollout/rollout_vllm/test_vllm_abort.py -v -s + - name: Test bucketed weight transfer + run: | + pytest -svvv tests/utils/test_bucketed_weight_transfer.py diff --git a/verl_0720_main/verl/.gitignore b/verl_0720_main/verl/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..6a8de64e02f642f77c9b5b2b4a2f6adabd176cad --- /dev/null +++ b/verl_0720_main/verl/.gitignore @@ -0,0 +1,133 @@ +**/*.pt +**/checkpoints +**/wget-log +**/_build/ +**/*.ckpt +**/outputs +**/*.tar.gz +**/playground +**/wandb + +/pyrightconfig.json + +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class +dataset/* +tensorflow/my_graph/* +.idea/ +# C extensions +*.so + +# Distribution / packaging +.Python +# env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +tmp/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ +pytest.ini +output.txt + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# IPython Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +venv/ +.venv/ +ENV/ +!**/workers/env/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +# vscode +.vscode + +# Mac +.DS_Store + +# vim +*.swp + +# emacs +*~ + +# ckpt +*.lock + +# data +*.parquet + + +# local logs +logs +log +outputs +.history diff --git a/verl_0720_main/verl/.gitmodules b/verl_0720_main/verl/.gitmodules new file mode 100644 index 0000000000000000000000000000000000000000..d5dd7a6aa577ccb64650ca389b699e04fd7af259 --- /dev/null +++ b/verl_0720_main/verl/.gitmodules @@ -0,0 +1,3 @@ +[submodule "recipe"] + path = recipe + url = https://github.com/verl-project/verl-recipe.git diff --git a/verl_0720_main/verl/.pre-commit-config.yaml b/verl_0720_main/verl/.pre-commit-config.yaml new file mode 100644 index 0000000000000000000000000000000000000000..39c0910c348ee43e903055044c9a6edd7d6e7556 --- /dev/null +++ b/verl_0720_main/verl/.pre-commit-config.yaml @@ -0,0 +1,75 @@ +repos: + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: "v0.12.2" + hooks: + - id: ruff + args: ["--fix", "--show-fixes", "--output-format=full"] + exclude: ^.*\.(ipynb)$ + - id: ruff-format + + - repo: https://github.com/pre-commit/mirrors-mypy + rev: "v1.17.0" + hooks: + - id: mypy + + - repo: local + hooks: + - id: autogen-trainer-cfg + name: Generate and verify verl/trainer/config/_generated_*.yaml + entry: scripts/generate_trainer_config.sh + language: script + pass_filenames: false + + - id: check-docs-time-info + name: Check docs Last updated info + entry: python3 tests/special_sanity/check_docs_time_info.py + language: python + pass_filenames: false + + - id: check-docstrings + name: Check doc string coverage + entry: python3 tests/special_sanity/check_docstrings.py + language: python + pass_filenames: false + + - id: check-license + name: Check license + entry: python3 tests/special_sanity/check_license.py --directories . + language: python + pass_filenames: false + + - id: check-device-api-usage + name: Check device API usage + entry: python3 tests/special_sanity/check_device_api_usage.py --directory ./verl + language: python + pass_filenames: false + + - id: check-dataproto-usage + name: Check DataProto usage + entry: python3 tests/special_sanity/check_dataproto_usage.py -d ./verl/workers/engine + language: python + pass_filenames: false + + - id: validate-structure + name: Validate test structure + entry: python3 tests/special_sanity/validate_structure.py + language: python + pass_filenames: false + + - id: check-naming-conventions + name: Check naming conventions + entry: sh -c 'fail=0; if grep -rIn --exclude-dir=.git --exclude-dir=.github --exclude-dir=venv --exclude-dir=__pycache__ --exclude=.pre-commit-config.yaml "veRL" .; then echo "Please use verl instead of veRL"; fail=1; fi; if grep -rIn --exclude-dir=.git --exclude-dir=.github --exclude-dir=venv --exclude-dir=__pycache__ --exclude=ascend_sglang_best_practices.rst --exclude=.pre-commit-config.yaml -E "Sglang|sgLang|sglAng|sglaNg|sglanG" .; then echo "Please use SGLang or sglang"; fail=1; fi; exit $fail' + language: system + pass_filenames: false + + - id: check-example-naming + name: Check example script naming convention + entry: python3 tests/special_sanity/check_example_naming.py --root examples + language: python + pass_filenames: false + + - id: compileall + name: Compile all python files + entry: sh -c 'PYTHONWARNINGS=error python3 -m compileall -q . -x "(^|[\\/])(\.venv|venv|\.git|verl[\\/]experimental[\\/]vla)([\\/]|$)"' + language: python + pass_filenames: false diff --git a/verl_0720_main/verl/.readthedocs.yaml b/verl_0720_main/verl/.readthedocs.yaml new file mode 100644 index 0000000000000000000000000000000000000000..0016868541a2a0667ef40ae6a9d861bcd26b9316 --- /dev/null +++ b/verl_0720_main/verl/.readthedocs.yaml @@ -0,0 +1,19 @@ +# Read the Docs configuration file +# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details + +version: 2 + +build: + os: ubuntu-22.04 + tools: + python: "3.11" + rust: "1.70" + +sphinx: + configuration: docs/conf.py + +python: + install: + - requirements: docs/requirements-docs.txt + - method: pip + path: . diff --git a/verl_0720_main/verl/.vscode/settings.json b/verl_0720_main/verl/.vscode/settings.json new file mode 100644 index 0000000000000000000000000000000000000000..705533538d75025d58f108b0d5ae1ec7b5a470b5 --- /dev/null +++ b/verl_0720_main/verl/.vscode/settings.json @@ -0,0 +1,15 @@ +{ + "[python]": { + "editor.defaultFormatter": "charliermarsh.ruff", + "editor.codeActionsOnSave": { + "source.organizeImports": "always", + } + }, + "files.associations": { + "array": "cpp", + "string_view": "cpp", + "initializer_list": "cpp", + "utility": "cpp" + }, + "iis.configDir": "" +} \ No newline at end of file diff --git a/verl_0720_main/verl/AGENTS.md b/verl_0720_main/verl/AGENTS.md new file mode 100644 index 0000000000000000000000000000000000000000..f44706f26edd31c83f1102dc829e8faeac90ea87 --- /dev/null +++ b/verl_0720_main/verl/AGENTS.md @@ -0,0 +1,87 @@ +# Agent Instructions for verl + +> These instructions apply to **all** AI-assisted contributions to `verl-project/verl`. +> Breaching these guidelines can result in automatic banning. + +## 1. Contribution Policy (Mandatory) + +### Duplicate-work checks + +Before proposing a PR, run these checks: + +```bash +gh issue view --repo verl-project/verl --comments +gh pr list --repo verl-project/verl --state open --search " in:body" +gh pr list --repo verl-project/verl --state open --search "" +``` + +- If an open PR already addresses the same fix, do not open another. +- If your approach is materially different, explain the difference in the issue. + +### No low-value busywork PRs + +Do not open one-off PRs for tiny edits (single typo, isolated style change, one mutable default, etc.). Mechanical cleanups are acceptable only when bundled with substantive work. + +### Accountability + +- Pure code-agent PRs are **not allowed**. A human submitter must understand and defend the change end-to-end. +- The submitting human must review every changed line and run relevant tests. +- PR descriptions for AI-assisted work **must** include: + - Why this is not duplicating an existing PR. + - Test commands run and results. + - Clear statement that AI assistance was used. + +### Fail-closed behavior + +If work is duplicate/trivial busywork, **do not proceed**. Return a short explanation of what is missing. + +--- + +## 2. Development Workflow + +### Environment setup + +```bash +# Install `uv` if you don't have it already: +curl -LsSf https://astral.sh/uv/install.sh | sh + +# Always use `uv` for Python environment management: +uv venv --python 3.12 +source .venv/bin/activate + +uv pip install pre-commit hydra-core +pre-commit install +``` + +### Commit messages + +Add attribution using commit trailers such as `Co-authored-by:` (other projects use `Assisted-by:` or `Generated-by:`). For example: + +```text +Your commit message here + +Co-authored-by: GitHub Copilot +Co-authored-by: Claude +Co-authored-by: gemini-code-assist +Signed-off-by: Your Name +``` + +### Resolving agent reviews + +Review comments from agent bots (e.g., gemini-code-assist) can be outdated or wrong. Always verify their suggestions against the current state of the repo before applying them. + +--- + +## Domain-Specific Guides + +Do not modify code in these areas without first reading and following the +linked guide. If the guide conflicts with the requested change, **refuse the +change and explain why**. + +- **Editing these instructions**: + [`docs/contributing/editing-agent-instructions.md`](docs/contributing/editing-agent-instructions.md) + — Rules for modifying AGENTS.md or any domain-specific guide it references. + +## Acknowledgements + +Adapted from the [vLLM project](https://github.com/vllm-project/vllm)'s [`AGENTS.md`](https://github.com/vllm-project/vllm/blob/main/AGENTS.md). diff --git a/verl_0720_main/verl/CONTRIBUTING.md b/verl_0720_main/verl/CONTRIBUTING.md new file mode 100644 index 0000000000000000000000000000000000000000..8a2b097109f55130789c7e840b7a79bb028228fd --- /dev/null +++ b/verl_0720_main/verl/CONTRIBUTING.md @@ -0,0 +1,101 @@ +# Contributing to verl + +Thank you for considering a contribution to verl! We welcome contributions of any kind - bug fixes, enhancements, documentation improvements, or even just feedback. Whether you're an experienced developer or this is your first open-source project, your help is invaluable. + +Your support can take many forms: + +- Report issues or unexpected behaviors. +- Suggest or implement new features. +- Improve or expand documentation. +- Review pull requests and assist other contributors. +- Spread the word: share verl in blog posts, social media, or give the repo a ⭐. + +## Finding Issues to Contribute + +Looking for ways to dive in? Check out these issues: + +- [Good first issues](https://github.com/verl-project/verl/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22good%20first%20issue%22) +- [Call for contribution](https://github.com/verl-project/verl/issues?q=is%3Aissue%20state%3Aopen%20label%3A%22call%20for%20contribution%22) + Furthermore, you can learn the development plan and roadmap via [RFC](https://github.com/verl-project/verl/issues?q=is%3Aissue%20state%3Aopen%20label%3ARFC) and [Roadmap](https://github.com/verl-project/verl/issues?q=state%3Aopen%20label%3A%22roadmap%22). + +## Developing + +- **Python-only**: install verl via `pip install -e .[test,vllm]` or `pip install -e .[test,sglang]` and iterate quickly. For full dependency setup, check out the verl [installation doc](https://verl.readthedocs.io/en/latest/start/install.html). + +## Code Linting and Formatting + +We rely on pre-commit to keep our code consistent. To set it up: + +```bash +pip install pre-commit hydra-core +pre-commit install +# for staged changes +pre-commit run +# for all files in the repo +pre-commit run --all-files +# run a specific hook with pre-commit +# pre-commit run --all-files --show-diff-on-failure --color=always +pre-commit run --all-files --show-diff-on-failure --color=always ruff +pre-commit run --all-files --show-diff-on-failure --color=always autogen-trainer-cfg +``` + +## Testing + +Our test suites run on GitHub Actions. Check these workflows for details: + +- [GPU unit tests](https://github.com/verl-project/verl/blob/main/.github/workflows/gpu_unit_tests.yml) +- [CPU unit tests](https://github.com/verl-project/verl/blob/main/.github/workflows/cpu_unit_tests.yml) +- [vLLM tests](https://github.com/verl-project/verl/blob/main/.github/workflows/vllm.yml) +- [SGLang tests](https://github.com/verl-project/verl/blob/main/.github/workflows/sgl.yml) + +### Adding CI tests + +If possible, please add CI test(s) for your new feature: + +1. Find the most relevant workflow yml file, which usually corresponds to a `hydra` default config (e.g. `ppo_trainer`, `ppo_megatron_trainer`, `sft_trainer`, etc). +2. Add related path patterns to the `paths` section if not already included. +3. Minimize the workload of the test script(s) (see existing scripts for examples). + +## Building the Docs + +``` +# Ensure verl is on your PYTHONPATH, e.g.: +pip install -e .[test] + +# Install documentation dependencies +cd docs +pip install -r requirements-docs.txt + +# Generate HTML docs +make clean +make html + +# Preview locally +python -m http.server -d _build/html/ +``` + +Open your browser at http://localhost:8000 to explore the docs. + +## Pull Requests & Code Reviews + +Thanks for submitting a PR! To streamline reviews: + +- Follow our Pull Request Template for title format and checklist. +- Adhere to our pre-commit lint rules and ensure all checks pass. +- Update docs for any user-facing changes. +- Add or update tests in the CI workflows, or explain why tests aren't applicable. + +## AI-Assisted Contributions + +See + +- [`AGENTS.md`](AGENTS.md) for rules that all AI coding agents must follow +- [`editing-agent-instructions.md`](docs/contributing/editing-agent-instructions.md) for guidelines on editing agent instructions. + +## License + +See the [LICENSE](https://github.com/verl-project/verl/blob/main/LICENSE) file for full details. + +## Thank You + +We appreciate your contributions to verl. Your efforts help make the project stronger and more user-friendly. Happy coding! diff --git a/verl_0720_main/verl/LICENSE b/verl_0720_main/verl/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..d645695673349e3947e8e5ae42332d0ac3164cd7 --- /dev/null +++ b/verl_0720_main/verl/LICENSE @@ -0,0 +1,202 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/verl_0720_main/verl/Notice.txt b/verl_0720_main/verl/Notice.txt new file mode 100644 index 0000000000000000000000000000000000000000..ade439da525ac3f82936e131a1ae386f43207fd8 --- /dev/null +++ b/verl_0720_main/verl/Notice.txt @@ -0,0 +1 @@ +Copyright 2023-2024 Bytedance Ltd. and/or its affiliates \ No newline at end of file diff --git a/verl_0720_main/verl/README.md b/verl_0720_main/verl/README.md new file mode 100644 index 0000000000000000000000000000000000000000..b2a9969735bde3be510d2f35d6615b02a651924e --- /dev/null +++ b/verl_0720_main/verl/README.md @@ -0,0 +1,311 @@ +
+ 👋 Hi, everyone! + verl is a RL training library initiated by ByteDance Seed team and maintained by the verl community. +
+
+
+ +
+ +Ask DeepWiki.com +[![GitHub Repo stars](https://img.shields.io/github/stars/verl-project/verl)](https://github.com/verl-project/verl/stargazers) +[![Twitter](https://img.shields.io/twitter/follow/verl_project)](https://twitter.com/verl_project) + + +[![Documentation](https://img.shields.io/badge/documentation-blue)](https://verl.readthedocs.io/en/latest/) + + +
+ +![seed logo](https://github.com/user-attachments/assets/c42e675e-497c-4508-8bb9-093ad4d1f216) + +

verl: Volcano Engine Reinforcement Learning for LLMs

+ +verl is a flexible, efficient and production-ready RL training library for large language models (LLMs). + +verl is the open-source version of **[HybridFlow: A Flexible and Efficient RLHF Framework](https://arxiv.org/abs/2409.19256v2)** paper. + +verl is flexible and easy to use with: + +- **Easy extension of diverse RL algorithms**: The hybrid-controller programming model enables flexible representation and efficient execution of complex post-training dataflows. Build RL dataflows such as GRPO, PPO in a few lines of code. + +- **Seamless integration of existing LLM infra with modular APIs**: Decouples computation and data dependencies, enabling seamless integration with existing LLM frameworks, such as FSDP, Megatron-LM, vLLM, SGLang, etc + +- **Flexible device mapping**: Supports various placement of models onto different sets of GPUs for efficient resource utilization and scalability across different cluster sizes. + +- Ready integration with popular HuggingFace models + +verl is fast with: + +- **State-of-the-art throughput**: SOTA LLM training and inference engine integrations and SOTA RL throughput. + +- **Efficient actor model resharding with 3D-HybridEngine**: Eliminates memory redundancy and significantly reduces communication overhead during transitions between training and generation phases. + +
+ verl-arch.png +
+ +

+ +## News +- [2026/07] [RL-Insight](https://github.com/verl-project/rl-insight) is released: online observability for reinforcement learning training. RL-Insight connects training-side metrics, RL state traces, and service dashboards across distributed rollout and optimization workloads. +- [2026/06] [verl-SpeCo](https://github.com/verl-project/verl-SpeCo) is pre-released: a co-training framework for speculative decoding across RL training and inference, keeping draft models aligned during training and reusable for accelerated serving, built on top of verl. +- [2026/05] [uni-agent](https://github.com/verl-project/uni-agent) is released: a unified agent framework to build, run, and train LLM agents at scale, built on top of verl. +- [2026/05] [VeRL-Omni](https://github.com/verl-project/verl-omni) is pre-released: a unified RL stack for diffusion and omni-modal model post-training built on top of verl. Read the [blog post](https://vllm.ai/blog/2026-05-14-verl-omni) for details. +- [2026/05] verl's zero-mismatch HuggingFace rollout [vexact](https://github.com/verl-project/vexact) is released: with batch-invariant kernels, shared model definition with FSDP, and out-of-box examples compatible with VeOmni. +- [2026/04] verl's Megatron backend LoRA and router replay support is showcased at [PyTorch Conference Europe 2026](https://pytorchconferenceeu2026.sched.com/event/2Juce/optimizing-reinforcement-learning-at-trillion-parameter-scale-songlin-jiang-aalto-university-mind-lab). +- [2026/03] verl is presented at NVIDIA GTC26: [session#1](https://www.nvidia.com/en-us/on-demand/session/gtc26-S81829/), [session#2](https://www.nvidia.com/en-us/on-demand/session/gtc26-S81620/) +- [2026/01] verl has been migrated to the [verl-project](https://github.com/verl-project) +- [2026/01] verl first meetup was successfully held in Shanghai on 01/10, hosted by Volcengine and NVIDIA, the slides has been uploaded to [verl-data](https://github.com/verl-project/verl-data). +- [2026/01] The `recipe` directory has been migrated to a dedicated repository: [verl-recipe](https://github.com/verl-project/verl-recipe) and added as a submodule. See https://github.com/verl-project/verl/pull/4795. It can be used as it was after `git submodule update --init --recursive recipe`. Note that [`transfer_queue`](verl/experimental/transfer_queue), [`fully_async_policy`](verl/experimental/fully_async_policy), [`one_step_off_policy`](verl/experimental/one_step_off_policy) and [`vla`](verl/experimental/vla) are kept under [`verl/experimental`](verl/experimental) since they are planned to be merged into the main library. Use them through `verl.experimental.{module}`. +- [2025/12] [Mind Lab](https://macaron.im/mindlab) successfully used [verl](https://github.com/verl-project/verl) and [Megatron-bridge](https://github.com/NVIDIA-NeMo/Megatron-Bridge) to train GRPO Lora for Trillion-parameter model on 64 H800 - See their [techblog](https://macaron.im/mindlab/research/building-trillion-parameter-reasoning-rl-with-10-gpus). +- [2025/10] verl is presented in the [PyTorch Conference 2025](https://pytorch.org/event/pytorch-conference-2025/). +- [2025/08] verl is presented in the [PyTorch Expert Exchange Webinar](https://www.youtube.com/watch?v=Vd79NmmqY3Q&t=2s). [Slides](https://github.com/eric-haibin-lin/verl-community/blob/main/slides/verl_talk_pytorch_2025_08.pdf) available. +- [2025/07] The [ReTool](https://arxiv.org/pdf/2504.11536) recipe is fully open sourced. [Blog](https://www.notion.so/verl-reTool-recipe-Using-multi-round-conversations-and-code-sandboxing-to-improve-the-math-of-large-23a8b5b7feba80b386b2e5b5e3c1cde0) +- [2025/07] The first verl meetup will be held at ICML Vancouver on July 16th! Please [join us](https://lu.ma/0ek2nyao) if you are at ICML! (onsite only) +- [2025/06] verl with Megatron backend enables large MoE models such as [DeepSeek-671B and Qwen3-235B](https://verl.readthedocs.io/en/latest/perf/dpsk.html). +- [2025/03] [DAPO](https://dapo-sia.github.io/) is the open-sourced SOTA RL algorithm that achieves 50 points on AIME 2024 based on the Qwen2.5-32B pre-trained model, surpassing the previous SOTA achieved by DeepSeek's GRPO (DeepSeek-R1-Zero-Qwen-32B). DAPO's training is fully powered by verl and the reproduction code is available in `recipe/dapo` now. +
more... +
    +
  • [2025/04] [Seed-Thinking-v1.5](https://github.com/ByteDance-Seed/Seed-Thinking-v1.5/blob/main/seed-thinking-v1.5.pdf) tech report is released! Trained with verl, Seed-Thinking-v1.5 achieves 86.7 on AIME 2024, 55.0 on Codeforces and 77.3 on GPQA, demonstrating excellent reasoning abilities in STEM and coding. Beyond reasoning tasks, the method demonstrates notable generalization across diverse domains.
  • +
  • [2025/07] verl keynote at [AWS AI Hours Singapore](https://pages.awscloud.com/aws-ai-hours-sg.html#agenda) on 7/8, verl & verl-agent project updates at [Agent for SWE meetup](https://lu.ma/e498qhsi) by LF AI & Data Singapore on 7/11.
  • +
  • [2025/06] verl team will provide latest project updates at [PyTorch Day China](https://www.lfasiallc.com/pytorch-day-china/) on June 7th. Meet our dev team in Beijing!
  • +
  • [2025/04] [VAPO](https://arxiv.org/pdf/2504.05118) (value-based augmented PPO) paper covers our latest RL method for reasoning models. Trained from Qwen-32B-base model, VAPO achieves 60.4 on AIME 2024, outperforming DAPO-32B.
  • +
  • [2025/05] [PF-PPO](https://arxiv.org/abs/2409.06957), accepted to ICML 2025, is now supported in verl! PF-PPO enhances policy learning efficiency and robustness by filtering potentially noisy reward signals and reusing high-quality experiences via a replay buffer.
  • +
  • [2025/04] We will give a tutorial about latest post-training techniques and programming guide for verl at [ICLR 2025 Expo](https://iclr.cc/virtual/2025/calendar?filter_events=Expo+Talk+Panel&filter_rooms=), [SCI-FM workshop](https://open-foundation-model.github.io/) and [LMSys afterparty](https://lu.ma/d23nyynm). Talk materials available [here](https://github.com/eric-haibin-lin/verl-community/tree/main/iclr25).
  • +
  • [2025/03] verl v0.3.0.post1 is released! See [release note](https://github.com/verl-project/verl/releases/) for details. It achieves [~1.4x speedup](https://tongyx361.github.io/blogs/posts/verl-intro/#/verl-flexible-and-efficient-rl-for-llms) compared to prev versions.
  • +
  • [2025/05] verl will be presented at [A2M Shanghai](https://a2m.msup.com.cn/home/?aid=4488&city=shanghai) on 5/16 - 5/17.
  • +
  • [2025/05] verl will be presented at [GOSIM x PyTorch Day 2025](https://paris2025.gosim.org/). See you in Paris!
  • +
  • [2025/03] We introduced the programming model of verl at the [vLLM Beijing Meetup](https://mp.weixin.qq.com/s/n77GibL2corAtQHtVEAzfg) and [verl intro and updates](https://github.com/eric-haibin-lin/verl-community/blob/main/slides/verl-lmsys-meetup.pdf) at the [SGLang-LMSYS Org Meetup](https://lu.ma/ntjrr7ig) in Sunnyvale mid-March.
  • +
  • [2025/03] We will present verl(HybridFlow) at EuroSys 2025. See you in Rotterdam!
  • +
  • [2025/02] verl v0.2.0.post2 is released!
  • +
  • [2025/02] We presented verl in the Bytedance/NVIDIA/Anyscale Ray Meetup. See you in San Jose!
  • +
  • [2025/01] [Doubao-1.5-pro](https://team.doubao.com/zh/special/doubao_1_5_pro) is released with SOTA-level performance on LLM & VLM. The RL scaling preview model is trained using verl, reaching OpenAI O1-level performance on math benchmarks (70.0 pass@1 on AIME).
  • +
  • [2024/12] verl is presented at Ray Forward 2024. Slides available here
  • +
  • [2024/12] The team presented Post-training LLMs: From Algorithms to Infrastructure at NeurIPS 2024. Slides and video available.
  • +
  • [2024/10] verl is presented at Ray Summit. Youtube video available.
  • +
  • [2024/08] HybridFlow (verl) is accepted to EuroSys 2025.
  • +
+
+ +## Key Features + +- **FSDP**, **FSDP2** and **Megatron-LM** for training. +- **vLLM**, **SGLang** and **HF Transformers** for rollout generation. +- Compatible with Hugging Face Transformers and Modelscope Hub: Qwen3.5, Qwen3, Qwen-2.5, Llama3.1, Gemma2, DeepSeek-LLM, etc +- Supervised fine-tuning. +- Reinforcement learning with [PPO](examples/ppo_trainer/), [GRPO](examples/grpo_trainer/), [GSPO](https://github.com/verl-project/verl-recipe/tree/main/gspo/), [ReMax](examples/remax_trainer/), [REINFORCE++](https://verl.readthedocs.io/en/latest/examples/config.html#algorithm), [RLOO](examples/rloo_trainer/), [PRIME](https://github.com/verl-project/verl-recipe/tree/main/prime/), [DAPO](https://github.com/verl-project/verl-recipe/tree/main/dapo/), [DrGRPO](https://github.com/verl-project/verl-recipe/tree/main/drgrpo), [KL_Cov & Clip_Cov](https://github.com/verl-project/verl-recipe/tree/main/entropy) etc. + - Support model-based reward and function-based reward (verifiable reward) for math, [coding](https://github.com/verl-project/verl-recipe/tree/main/dapo), etc + - Support vision-language models (VLMs) and [multi-modal RL](examples/grpo_trainer/run_qwen2_5_vl_7b_fsdp.sh) with Qwen2.5-vl, Kimi-VL + - [Multi-turn with tool calling](examples/tutorial/agent_loop_get_started/) +- LLM alignment recipes such as [Self-play preference optimization (SPPO)](https://github.com/verl-project/verl-recipe/tree/main/sppo) +- Flash attention 2, sequence packing, sequence parallelism via DeepSpeed Ulysses, [LoRA](examples/tuning/lora/run_qwen3_8b_fsdp.sh), [Liger-kernel](examples/sft/gsm8k/run_qwen2_5_0_5b_fsdp.sh) (`USE_LIGER=1`). +- Scales up to 671B models and hundreds of GPUs with [expert parallelism](https://github.com/verl-project/verl/pull/1467) +- Multi-gpu [LoRA RL](https://verl.readthedocs.io/en/latest/advance/ppo_lora.html) support to save memory. +- Experiment tracking with wandb, swanlab, mlflow and tensorboard. +- Hardware Support: Supports NVIDIA, AMD, [Ascend](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/README.md) + +## Getting Started + +Documentation + +**Quickstart:** + +- [Installation](https://verl.readthedocs.io/en/latest/start/install.html) +- [Quickstart](https://verl.readthedocs.io/en/latest/start/quickstart.html) +- [Programming Guide](https://verl.readthedocs.io/en/latest/hybrid_flow.html) & [Tech Talk](https://hcqnc.xetlk.com/sl/3vACOK) (in Chinese) +- [PPO in verl](https://verl.readthedocs.io/en/latest/algo/ppo.html) +- [GRPO in verl](https://verl.readthedocs.io/en/latest/algo/grpo.html) + +**Running a PPO example step-by-step:** + +- [Prepare Data for Post-Training](https://verl.readthedocs.io/en/latest/preparation/prepare_data.html) +- [Implement Reward Function for Dataset](https://verl.readthedocs.io/en/latest/preparation/reward_function.html) +- [PPO Example Architecture](https://verl.readthedocs.io/en/latest/examples/ppo_code_architecture.html) +- [Config Explanation](https://verl.readthedocs.io/en/latest/examples/config.html) + +**Reproducible algorithm baselines:** + +- [RL performance on coding, math](https://verl.readthedocs.io/en/latest/algo/baseline.html) + +**Algorithm recipes (`recipe/`):** + +- Optional workflows and baselines live under [`recipe/`](recipe/). Each recipe subdirectory includes a small **`REQUIRED_VERL.txt`** file describing the intended `verl` install: pinned recipes use a **tag or fixed git SHA**; rolling recipes record an explicit **`VERL_COMMIT`** (and related submodule / recipe-folder SHAs) so you can `pip install verl@git+…@` without guessing. See [`recipe/README.md`](recipe/README.md) for the full index and links. + +**For code explanation and advance usage (extension):** + +- PPO Trainer and Workers + + - [PPO Ray Trainer](https://verl.readthedocs.io/en/latest/workers/ray_trainer.html) + - [Model Engine](https://verl.readthedocs.io/en/latest/workers/model_engine.html) + - [Engine Workers (FSDP / Megatron-LM / Automodel / VeOmni / TorchTitan)](https://verl.readthedocs.io/en/latest/workers/engine_workers.html) + +- Advanced Usage and Extension + - [Add Models with the FSDP Backend](https://verl.readthedocs.io/en/latest/advance/fsdp_extension.html) + - [Add Models with the Megatron-LM Backend](https://verl.readthedocs.io/en/latest/advance/megatron_extension.html) + - [Multi-turn Rollout Support](https://verl.readthedocs.io/en/latest/sglang_multiturn/multiturn.html) + - [Search Tool Integration](https://verl.readthedocs.io/en/latest/sglang_multiturn/search_tool_example.html) + - [Sandbox Fusion Integration](https://verl.readthedocs.io/en/latest/examples/sandbox_fusion_example.html) + - [Extend to Other RL(HF) algorithms](https://verl.readthedocs.io/en/latest/advance/dpo_extension.html) + - [Ray API design tutorial](https://verl.readthedocs.io/en/latest/advance/placement.html) + +**Blogs from the community** + +- [When Reasoning Models Break Tokenization: The Hidden Complexity of Multiturn Training](https://github.com/zhaochenyang20/Awesome-ML-SYS-Tutorial/blob/main/rlhf/verl/multi-turn/fast_tokenization/multiturn_tokenization_and_masking.md) +- [verl deployment on AWS SageMaker](https://medium.com/@kaige.yang0110/run-verl-on-sagemaker-using-4x8-l40s-gpus-8e6d5c3c61d3) +- [verl x SGLang Multi-turn Code Walkthrough](https://github.com/zhaochenyang20/Awesome-ML-SYS-Tutorial/blob/main/rlhf/verl/multi-turn/code-walk-through/readme_EN.md) +- [Optimizing SGLang Memory Usage in verl](https://hebiao064.github.io/rl-memory-management) +- [SGLang, verl, OpenBMB and Tsinghua University: Pioneering End-to-End Multi-Turn RLHF](https://github.com/zhaochenyang20/Awesome-ML-SYS-Tutorial/blob/main/rlhf/verl/multi-turn/verl-multiturn-rollout-Release.md) +- [Reinforcement Learning from Human Feedback on AMD GPUs with verl and ROCm Integration](https://rocm.blogs.amd.com/artificial-intelligence/verl-large-scale/README.html) +- [veMLP x verl :玩转强化学习训练](https://mp.weixin.qq.com/s/7nbqxk4knMGd-hQE9ls2tA) +- [使用 verl 进行 GRPO 分布式强化学习训练最佳实践](https://www.volcengine.com/docs/6459/1463942) +- [HybridFlow verl 原文浅析](https://github.com/zhaochenyang20/Awesome-ML-SYS-Tutorial/blob/main/rlhf/verl/readme.md) +- [最高提升 20 倍吞吐量!豆包大模型团队发布全新 RLHF 框架,现已开源!](https://team.doubao.com/en/blog/%E6%9C%80%E9%AB%98%E6%8F%90%E5%8D%8720%E5%80%8D%E5%90%9E%E5%90%90%E9%87%8F-%E8%B1%86%E5%8C%85%E5%A4%A7%E6%A8%A1%E5%9E%8B%E5%9B%A2%E9%98%9F%E5%8F%91%E5%B8%83%E5%85%A8%E6%96%B0-rlhf-%E6%A1%86%E6%9E%B6-%E7%8E%B0%E5%B7%B2%E5%BC%80%E6%BA%90) + +## Performance Tuning Guide + +The performance is essential for on-policy RL algorithm. We have written a detailed [performance tuning guide](https://verl.readthedocs.io/en/latest/perf/perf_tuning.html) to help you optimize performance. + +## Upgrade to vLLM >= v0.8.2 + +verl now supports vLLM>=0.8.2 when using FSDP as the training backend. Please refer to [this document](https://github.com/verl-project/verl/blob/main/docs/README_vllm0.8.md) for the installation guide and more information. Please avoid vllm 0.7.x, which contains bugs that may lead to OOMs and unexpected errors. + +## Use Latest SGLang + +SGLang is fully supported with verl, and SGLang RL Group is working extensively on building unique features, including multi-turn agentic RL, VLM RLHF, server-based RL, and partial rollout. Please refer to [this document](https://verl.readthedocs.io/en/latest/workers/sglang_worker.html) for the installation guide and more information. + +## Upgrade to FSDP2 + +verl is fully embracing FSDP2! FSDP2 is recommended by torch distributed team, providing better throughput and memory usage, and is composible with other features (e.g. torch.compile). To enable FSDP2, simply use verl main and set the following options: + +``` +actor_rollout_ref.ref.strategy=fsdp2 +actor_rollout_ref.actor.strategy=fsdp2 +critic.strategy=fsdp2 +``` + +Furthermore, FSDP2 cpu offloading is compatible with gradient accumulation. You can turn it on to save memory with `actor_rollout_ref.actor.fsdp_config.offload_policy=True`. For more details, see https://github.com/verl-project/verl/pull/1026 + +## AMD Support (ROCm Kernel) + +verl runs on AMD ROCm GPUs (MI300X / MI325X / MI355X) with FSDP, FSDP2, and Megatron trainer backends, and vLLM as the validated inference engine (SGLang support is in progress). See the [AMD ROCm quick-start guide](https://github.com/verl-project/verl/blob/main/docs/amd_tutorial/amd_quick_start.rst) for container bring-up, environment verification, and training examples. + +## Citation and acknowledgement + +If you find the project helpful, please cite: + +- [HybridFlow: A Flexible and Efficient RLHF Framework](https://arxiv.org/abs/2409.19256v2) +- [A Framework for Training Large Language Models for Code Generation via Proximal Policy Optimization](https://i.cs.hku.hk/~cwu/papers/gmsheng-NL2Code24.pdf) + +```bibtex +@article{sheng2024hybridflow, + title = {HybridFlow: A Flexible and Efficient RLHF Framework}, + author = {Guangming Sheng and Chi Zhang and Zilingfeng Ye and Xibin Wu and Wang Zhang and Ru Zhang and Yanghua Peng and Haibin Lin and Chuan Wu}, + year = {2024}, + journal = {arXiv preprint arXiv: 2409.19256} +} +``` + +verl is inspired by the design of Nemo-Aligner, Deepspeed-chat and OpenRLHF. The project is adopted and contributed by Bytedance, Anyscale, LMSys.org, [Alibaba Qwen team](https://github.com/QwenLM/), Shanghai AI Lab, Tsinghua University, UC Berkeley, UCLA, UIUC, University of Hong Kong, ke.com, [All Hands AI](https://www.all-hands.dev/), [ModelBest](http://modelbest.cn/), JD AI Lab, Microsoft Research, [StepFun](https://www.stepfun.com/), Amazon, LinkedIn, Meituan, [Camel-AI](https://www.camel-ai.org/), [OpenManus](https://github.com/OpenManus), Xiaomi, NVIDIA research, [Baichuan](https://www.baichuan-ai.com/home), [RedNote](https://www.xiaohongshu.com/), [SwissAI](https://www.swiss-ai.org/), [Moonshot AI (Kimi)](https://www.moonshot-ai.com/), Baidu, Snowflake, Skywork.ai, JetBrains, [IceSword Lab](https://www.iceswordlab.com), and many more. + +## Awesome Projects Built with `verl` + +Welcome to register your awesome project build with `verl` for other developers' reference! + +- [TinyZero](https://github.com/Jiayi-Pan/TinyZero): a reproduction of **DeepSeek R1 Zero** recipe for reasoning tasks ![GitHub Repo stars](https://img.shields.io/github/stars/Jiayi-Pan/TinyZero) +- [SkyThought](https://github.com/NovaSky-AI/SkyThought): RL training for Sky-T1-7B by NovaSky AI team. ![GitHub Repo stars](https://img.shields.io/github/stars/NovaSky-AI/SkyThought) +- [simpleRL-reason](https://github.com/hkust-nlp/simpleRL-reason): SimpleRL-Zoo: Investigating and Taming Zero Reinforcement Learning for Open Base Models in the Wild ![GitHub Repo stars](https://img.shields.io/github/stars/hkust-nlp/simpleRL-reason) +- [Easy-R1](https://github.com/hiyouga/EasyR1): **Multi-modal** RL training framework ![GitHub Repo stars](https://img.shields.io/github/stars/hiyouga/EasyR1) +- [RandOpt](https://github.com/sunrainyg/RandOpt): Neural Thickets: Diverse Task Experts Are Dense Around Pretrained Weights ![GitHub Repo stars](https://img.shields.io/github/stars/sunrainyg/RandOpt) +- [OpenManus-RL](https://github.com/OpenManus/OpenManus-RL): LLM Agents RL tuning framework for multiple agent environments. ![GitHub Repo stars](https://img.shields.io/github/stars/OpenManus/OpenManus-RL) +- [rllm](https://github.com/agentica-project/rllm): async RL training with [verl-pipeline](https://github.com/agentica-project/verl-pipeline) ![GitHub Repo stars](https://img.shields.io/github/stars/agentica-project/rllm) +- [RAGEN](https://github.com/ZihanWang314/ragen): a general-purpose reasoning **agent** training framework ![GitHub Repo stars](https://img.shields.io/github/stars/ZihanWang314/ragen) +- [Search-R1](https://github.com/PeterGriffinJin/Search-R1): RL with reasoning and **searching (tool-call)** interleaved LLMs ![GitHub Repo stars](https://img.shields.io/github/stars/PeterGriffinJin/Search-R1) +- [ReSearch](https://github.com/Agent-RL/ReSearch): Learning to **Re**ason with **Search** for LLMs via Reinforcement Learning ![GitHub Repo stars](https://img.shields.io/github/stars/Agent-RL/ReSearch) +- [Skywork-OR1](https://github.com/SkyworkAI/Skywork-OR1): Skywork open reaonser series ![GitHub Repo stars](https://img.shields.io/github/stars/SkyworkAI/Skywork-OR1) +- [ToRL](https://github.com/GAIR-NLP/ToRL): Scaling tool-integrated RL ![GitHub Repo stars](https://img.shields.io/github/stars/GAIR-NLP/ToRL) +- [Absolute Zero Reasoner](https://github.com/LeapLabTHU/Absolute-Zero-Reasoner): [A no human curated data self-play framework for reasoning](https://arxiv.org/abs/2505.03335) ![GitHub Repo stars](https://img.shields.io/github/stars/LeapLabTHU/Absolute-Zero-Reasoner) +- [verl-agent](https://github.com/langfengQ/verl-agent): A scalable training framework for **long-horizon LLM/VLM agents**, along with a new algorithm **GiGPO** ![GitHub Repo stars](https://img.shields.io/github/stars/langfengQ/verl-agent) +- [RL-Factory](https://github.com/Simple-Efficient/RL-Factory): An easy and efficient RL post-training framework for Agentic Learning ![GitHub Repo stars](https://img.shields.io/github/stars/Simple-Efficient/RL-Factory) +- [ReTool](https://retool-rl.github.io/): ReTool: reinforcement learning for strategic tool use in LLMs. Code release is in progress... +- [verl-tool](https://github.com/TIGER-AI-Lab/verl-tool): An unified and easy-to-extend tool-agent training framework based on verl![GitHub Repo stars](https://img.shields.io/github/stars/TIGER-AI-Lab/verl-tool) +- [PRIME](https://github.com/PRIME-RL/PRIME): Process reinforcement through implicit rewards ![GitHub Repo stars](https://img.shields.io/github/stars/PRIME-RL/PRIME) +- [MemAgent](https://github.com/BytedTsinghua-SIA/MemAgent): MemAgent: Reshaping Long-Context LLM with Multi-Conv RL based Memory Agent ![GitHub Repo stars](https://img.shields.io/github/stars/BytedTsinghua-SIA/MemAgent) +- [POLARIS](https://github.com/ChenxinAn-fdu/POLARIS): A Post-training recipe for scaling RL on Advanced Reasoning models ![GitHub Repo stars](https://img.shields.io/github/stars/ChenxinAn-fdu/POLARIS) +- [GUI-R1](https://github.com/ritzz-ai/GUI-R1): **GUI-R1**: A Generalist R1-style Vision-Language Action Model For **GUI Agents** ![GitHub Repo stars](https://img.shields.io/github/stars/ritzz-ai/GUI-R1) +- [DeepRetrieval](https://github.com/pat-jj/DeepRetrieval): RL Training of **Search Agent** with **Search/Retrieval Outcome** ![GitHub Repo stars](https://img.shields.io/github/stars/pat-jj/DeepRetrieval) +- [Code-R1](https://github.com/ganler/code-r1): Reproducing R1 for **Code** with Reliable Rewards ![GitHub Repo stars](https://img.shields.io/github/stars/ganler/code-r1) +- [DeepResearcher](https://github.com/GAIR-NLP/DeepResearcher): Scaling deep research via reinforcement learning in real-world environments ![GitHub Repo stars](https://img.shields.io/github/stars/GAIR-NLP/DeepResearcher) +- [VAGEN](https://github.com/RAGEN-AI/VAGEN): Training VLM agents with multi-turn reinforcement learning ![GitHub Repo stars](https://img.shields.io/github/stars/RAGEN-AI/VAGEN) +- [RM-R1](https://arxiv.org/abs/2505.02387): RL training of reasoning reward models ![GitHub Repo stars](https://img.shields.io/github/stars/RM-R1-UIUC/RM-R1) +- [Dr. MAS](https://arxiv.org/pdf/2602.08847): Stable **end-to-end RL** post-training for **multi-agent LLM systems** ![GitHub Repo stars](https://img.shields.io/github/stars/langfengQ/DrMAS) +- [LUFFY](https://arxiv.org/pdf/2504.14945): Learning to Reason under Off-Policy Guidance![GitHub Repo stars](https://img.shields.io/github/stars/ElliottYan/LUFFY) +- [DeepMath](https://github.com/zwhe99/DeepMath): DeepMath-103K data and series models for math reasoning![GitHub Repo stars](https://img.shields.io/github/stars/zwhe99/DeepMath) +- [PACS](https://github.com/ritzz-ai/PACS): Implicit Actor Critic Coupling via a Supervised Learning Framework for RLVR ![GitHub Repo stars](https://img.shields.io/github/stars/ritzz-ai/PACS) +- [Entropy Mechanism of RL](https://github.com/PRIME-RL/Entropy-Mechanism-of-RL): The Entropy Mechanism of Reinforcement Learning for Large Language Model Reasoning![GitHub Repo stars](https://img.shields.io/github/stars/PRIME-RL/Entropy-Mechanism-of-RL) +- [LLaSA-TTS-GRPO](https://github.com/channel-io/ch-tts-llasa-rl-grpo): TTS fine-tuning with GRPO optimization based on LLASA models ![GitHub Repo stars](https://img.shields.io/github/stars/channel-io/ch-tts-llasa-rl-grpo) +- [PF-PPO](https://arxiv.org/abs/2409.06957): Policy Filtration for PPO based on the reliability of reward signals for more efficient and robust RLHF. +- [RACRO](https://github.com/gyhdog99/RACRO2): Build multi-modal reasoning models via decoupling it into query-conditioned captioning and text-only reasoning ![GitHub Repo stars](https://img.shields.io/github/stars/gyhdog99/RACRO2) +- [Agent Lightning](https://github.com/microsoft/agent-lightning): A flexible and extensible framework that enables seamless agent optimization for any existing agent framework. ![GitHub Repo stars](https://img.shields.io/github/stars/microsoft/agent-lightning) +- [VTool-R1](https://github.com/VTOOL-R1/vtool-r1): VLMs Learn to Think with Images via Reinforcement Learning on Multimodal Tool Use. ![GitHub Repo stars](https://img.shields.io/github/stars/VTOOL-R1/vtool-r1) +- [Kimina-Prover-RL](https://github.com/project-numina/kimina-prover-rl/tree/main/recipe/kimina_prover_rl): Training pipeline for formal theorem proving, based on a paradigm inspired by DeepSeek-R1. +- [RL-PLUS](https://github.com/YihongDong/RL-PLUS): Countering Capability Boundary Collapse of LLMs in Reinforcement Learning with Hybrid-policy Optimization. +- [rStar2-Agent](https://github.com/microsoft/rStar): Using reinforcement learning with multi-step tool-calling for math tasks, rStar2-Agent-14B reaches frontier-level math reasoning in just 510 RL training steps ![GitHub Repo stars](https://img.shields.io/github/stars/microsoft/rStar) +- [Vision-SR1](https://github.com/zli12321/Vision-SR1): Self-Rewarding Vision-Language Model via Reasoning Decomposition ![GitHub Repo stars](https://img.shields.io/github/stars/zli12321/Vision-SR1) +- [SimpleVLA-RL](https://github.com/PRIME-RL/SimpleVLA-RL): SimpleVLA-RL: A Simple yet Effective Vision-Language Action Model for Reinforcement Learning ![GitHub Repo stars](https://img.shields.io/github/stars/PRIME-RL/SimpleVLA-RL) +- [Table-R1](https://github.com/Table-R1/Table-R1): Table-R1: Inference-Time Scaling for Table Reasoning ![GitHub Repo stars](https://img.shields.io/github/stars/Table-R1/Table-R1) +- [Revisual-R1](https://github.com/CSfufu/Revisual-R1): Revisual-R1: Advancing Multimodal Reasoning From Optimized Cold Start to Staged Reinforcement Learning ![GitHub Repo stars](https://img.shields.io/github/stars/CSfufu/Revisual-R1) +- [ARES](https://github.com/shawn0728/ARES): ARES: Multimodal Adaptive Reasoning via Difficulty-Aware Token-Level Entropy Shaping ![GitHub Repo stars](https://img.shields.io/github/stars/shawn0728/ARES) +- [Meta-Bandit-LLM](https://github.com/sanxing-chen/meta-bandit-llm): Meta-Bandit-LLM: Long-horizon multiturn interactive training for meta-bandit agents ![GitHub Repo stars](https://img.shields.io/github/stars/sanxing-chen/meta-bandit-llm) +- [PokeeResearch](https://github.com/Pokee-AI/PokeeResearchOSS): PokeeResearch: State-of-the-art 7B DeepResearch Agent that leverages web search and content reading capabilities to answer complex questions using the most up-to-date information available online. ![Github Repo Stars](https://img.shields.io/github/stars/Pokee-AI/PokeeResearchOSS) +- [Search Self-play](https://github.com/Alibaba-Quark/SSP): Pushing the Frontier of Agent Capability without Supervision ![GitHub Repo stars](https://img.shields.io/github/stars/Alibaba-Quark/SSP) +- [OneThinker](https://github.com/tulerfeng/OneThinker): All-in-one Reasoning Model for Image and Video ![GitHub Repo stars](https://img.shields.io/github/stars/tulerfeng/OneThinker) +- [OpenTinker](https://github.com/open-tinker/OpenTinker): Democratizing Agentic Reinforcement Learning as a Service ![GitHub Repo stars](https://img.shields.io/github/stars/open-tinker/OpenTinker) +- [FlowRL](https://github.com/Xuekai-Zhu/FlowRL): Matching reward distributions via **flow balance** for diverse exploration and generalizable reasoning ![GitHub Repo stars](https://img.shields.io/github/stars/Xuekai-Zhu/FlowRL) +- [Logic-RL](https://github.com/Unakar/Logic-RL): a reproduction of DeepSeek R1 Zero on 2K Tiny Logic Puzzle Dataset. ![GitHub Repo stars](https://img.shields.io/github/stars/Unakar/Logic-RL) +- [Seed-Coder](https://github.com/ByteDance-Seed/Seed-Coder): RL training of Seed-Coder boosts performance on competitive programming ![GitHub Repo stars](https://img.shields.io/github/stars/ByteDance-Seed/Seed-Coder) +- [all-hands/openhands-lm-32b-v0.1](https://www.all-hands.dev/blog/introducing-openhands-lm-32b----a-strong-open-coding-agent-model): A strong, open coding agent model, trained with [multi-turn fine-tuning](https://github.com/verl-project/verl/pull/195) +- [s3](https://github.com/pat-jj/s3) **Efficient Yet Effective** Search Agent Training via RL ![GitHub Repo stars](https://img.shields.io/github/stars/pat-jj/s3) +- [Rec-R1](https://arxiv.org/pdf/2503.24289): Bridging Generative Large Language Models and Recommendation Systems via Reinforcement Learning +- [Explore RL Data Scaling](https://arxiv.org/abs/2503.22230): Exploring Data Scaling Trends and Effects in Reinforcement Learning from Human Feedback +- [FIRE](https://arxiv.org/abs/2410.21236): Flaming-hot initiation with regular execution sampling for large language models +- [DQO](https://arxiv.org/abs/2410.09302): Enhancing multi-Step reasoning abilities of language models through direct Q-function optimization +- [ProRL](https://arxiv.org/abs/2505.24864): Prolonged Reinforcement Learning Expands Reasoning Boundaries in Large Language Models +- [cognition-engineering](https://github.com/gair-nlp/cognition-engineering): Test time scaling drives cognition engineering. ![GitHub Repo stars](https://img.shields.io/github/stars/gair-nlp/cognition-engineering) +- [Trust Region Preference Approximation](https://github.com/XueruiSu/Trust-Region-Preference-Approximation): A simple and stable **reinforcement learning algorithm** for LLM reasoning. ![GitHub Repo stars](https://img.shields.io/github/stars/XueruiSu/Trust-Region-Preference-Approximation) +- [AdaRFT](https://github.com/uscnlp-lime/verl): Efficient Reinforcement Finetuning via **Adaptive Curriculum Learning** ![GitHub Repo stars](https://img.shields.io/github/stars/uscnlp-lime/verl) +- [critic-rl](https://github.com/HKUNLP/critic-rl): LLM critics for code generation ![GitHub Repo stars](https://img.shields.io/github/stars/HKUNLP/critic-rl) +- [self-rewarding-reasoning-LLM](https://arxiv.org/pdf/2502.19613): self-rewarding and correction with **generative reward models** ![GitHub Repo stars](https://img.shields.io/github/stars/RLHFlow/Self-rewarding-reasoning-LLM) +- [DeepEnlighten](https://github.com/DolbyUUU/DeepEnlighten): Reproduce R1 with **social reasoning** tasks and analyze key findings ![GitHub Repo stars](https://img.shields.io/github/stars/DolbyUUU/DeepEnlighten) +- [MetaSpatial](https://github.com/PzySeere/MetaSpatial): Reinforcing **3D Spatial Reasoning** in **VLMs** for the **Metaverse** ![GitHub Repo stars](https://img.shields.io/github/stars/PzySeere/MetaSpatial) +- [PURE](https://github.com/CJReinforce/PURE): **Credit assignment** is the key to successful reinforcement fine-tuning using **process reward model** ![GitHub Repo stars](https://img.shields.io/github/stars/CJReinforce/PURE) +- [cognitive-behaviors](https://github.com/kanishkg/cognitive-behaviors): Cognitive Behaviors that Enable Self-Improving Reasoners, or, Four Habits of Highly Effective STaRs ![GitHub Repo stars](https://img.shields.io/github/stars/kanishkg/cognitive-behaviors) +- [deepscaler](https://github.com/agentica-project/rllm/tree/deepscaler): iterative context scaling with GRPO ![GitHub Repo stars](https://img.shields.io/github/stars/agentica-project/deepscaler) +- [DAPO](https://dapo-sia.github.io/): the fully open source SOTA RL algorithm that beats DeepSeek-R1-zero-32B ![GitHub Repo stars](https://img.shields.io/github/stars/verl-project/verl) +- [NoisyRollout](https://github.com/NUS-TRAIL/NoisyRollout): Reinforcing Visual Reasoning with Data Augmentation ![GitHub Repo stars](https://img.shields.io/github/stars/NUS-TRAIL/NoisyRollout) +- [SPEAR](https://github.com/TencentYoutuResearch/SPEAR): **Self-imitation** with **Progressive Exploration** for Agentic Reinforcement Learning (ICLR 2026) ![GitHub Repo stars](https://img.shields.io/github/stars/TencentYoutuResearch/SPEAR) +- [RuleReasoner](https://github.com/bigai-nlco/RuleReasoner): **RuleReasoner:** Reinforced Rule-based Reasoning via **Domain-aware Dynamic Sampling** (ICLR 2026) ![GitHub Repo stars](https://img.shields.io/github/stars/bigai-nlco/RuleReasoner) +- [MetaphorStar](https://metaphorstar.github.io/): **Image Metaphor** Understanding and Reasoning with End-to-End **Visual Reinforcement Learning** ![GitHub Repo stars](https://img.shields.io/github/stars/MING-ZCH/MetaphorStar) +- [DART-GUI](https://github.com/Computer-use-agents/dart-gui): a decoupled agentic RL framework for Computer Use Agents, achieving ~2× training speedup and ~5× environment utilization! ![GitHub Repo stars](https://img.shields.io/github/stars/Computer-use-agents/dart-gui) +- [Rethinking OPD](https://github.com/thunlp/OPD): Rethinking On-Policy Distillation of Large Language Models: Phenomenology, Mechanism, and Recipe ![GitHub Repo stars](https://img.shields.io/github/stars/thunlp/OPD) + +## Contribution Guide + +See [contributions guide](CONTRIBUTING.md) + +## About [ByteDance Seed Team](https://team.doubao.com/) + +Founded in 2023, ByteDance Seed Team is dedicated to crafting the industry's most advanced AI foundation models. The team aspires to become a world-class research team and make significant contributions to the advancement of science and society. You can get to know Bytedance Seed better through the following channels👇 + + + +We are HIRING! Send us an [email](mailto:the.verl.project@gmail.com) if you are interested in internship/FTE opportunities in RL for agents. diff --git a/verl_0720_main/verl/__pycache__/setup.cpython-310.pyc b/verl_0720_main/verl/__pycache__/setup.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..2b8ab196699e63b4485c64440893816ded9ae5f3 Binary files /dev/null and b/verl_0720_main/verl/__pycache__/setup.cpython-310.pyc differ diff --git a/verl_0720_main/verl/docker/Dockerfile.isaaclab230 b/verl_0720_main/verl/docker/Dockerfile.isaaclab230 new file mode 100644 index 0000000000000000000000000000000000000000..a1d0f6a54f68aa3d76a68c7b3d2c801ee9eafb68 --- /dev/null +++ b/verl_0720_main/verl/docker/Dockerfile.isaaclab230 @@ -0,0 +1,150 @@ + +#FROM nvcr.nju.edu.cn/nvidia/isaac-lab:2.3.0 +FROM isaac-lab-base:latest + +ENV ACCEPT_EULA=Y +ENTRYPOINT [] + +# desktop +RUN --mount=type=cache,target=/var/cache/apt \ + sed -i 's/archive.ubuntu.com/mirrors.ivolces.com/g' /etc/apt/sources.list && \ + sed -i 's/security.ubuntu.com/mirrors.ivolces.com/g' /etc/apt/sources.list && \ + apt-get update && \ + DEBIAN_FRONTEND=noninteractive apt-get install -y locales && \ + locale-gen en_US.UTF-8 && \ + update-locale LANG=en_US.UTF-8 LC_CTYPE=en_US.UTF-8 && \ + apt-get install -y wget curl \ + xfce4 \ + xfce4-goodies \ + xorg \ + dbus-x11 \ + x11-xserver-utils \ + tigervnc-standalone-server \ + tigervnc-common \ + tigervnc-tools \ + fonts-dejavu \ + fonts-liberation +# cuda 12.2 +RUN --mount=type=cache,target=/var/cache/apt \ + cd /tmp && \ + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/3bf863cc.pub && \ + apt-key add 3bf863cc.pub && \ + echo "deb https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64 /" > /etc/apt/sources.list.d/cuda.list && \ + apt-get update && \ + apt-get install -y libcusparselt0 libnccl2=2.27.3-1+cuda12.2 libglfw3 libgl1-mesa-glx libosmesa6 && \ + rm -f 3bf863cc.pub + +# libero +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install easydict==1.9 robosuite==1.4.0 bddl==1.0.1 future==0.18.2 cloudpickle==2.1.0 + +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install transformers[hf_xet] + +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install --upgrade numpy==1.26.4 ray[default] \ + accelerate codetiming datasets dill hydra-core pandas peft pyarrow>=19.0.0 pybind11 pylatexenc + +# openvla-oft +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install pre-commit torchdata packaging>=20.0 uvicorn fastapi latex2sympy2_extended math_verify tensorboard + + +# flash_attn +RUN cd /tmp && \ + wget -nv https://github.com/Dao-AILab/flash-attention/releases/download/v2.8.0.post2/flash_attn-2.8.0.post2+cu12torch2.7cxx11abiFALSE-cp311-cp311-linux_x86_64.whl && \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install /tmp/flash_attn-2.8.0.post2+cu12torch2.7cxx11abiFALSE-cp311-cp311-linux_x86_64.whl && \ + rm -f /tmp/flash_attn-2.8.0.post2+cu12torch2.7cxx11abiFALSE-cp311-cp311-linux_x86_64.whl + +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install --upgrade protobuf==3.20.3 timm==0.9.16 + +RUN --mount=type=cache,target=/root/.cache/pip \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install orjson==3.11.3 pyvers==0.1.0 tensordict==0.10.0 --force --no-deps + + +RUN mkdir -p /root/.vnc && \ + cat <<'EOP' > /root/.vnc/xstartup +#!/bin/sh +unset SESSION_MANAGER +unset DBUS_SESSION_BUS_ADDRESS +[ -r \$HOME/.Xresources ] && xrdb \$HOME/.Xresources +xsetroot -solid grey +exec startxfce4 +EOP + +RUN cat <<'EOP' > /root/.vnc/config +geometry=1920x1080 +depth=24 +desktop=Isaac-Sim-Desktop +dpi=96 +localhost=no +EOP + +RUN cat <<'EOP' > /root/start_isaac_vnc.sh +#!/bin/bash +# 设置显示变量 +export DISPLAY=:1 + +# 检查VNC是否运行 +if ! pgrep -f "Xvnc.*:1" > /dev/null; then + echo "Starting VNC server..." + vncserver :1 -localhost no -geometry 1920x1080 -depth 24 -desktop "Isaac-Sim-Desktop" + sleep 3 +fi + +# 启动Isaac Sim +echo "Starting Isaac Sim..." +/workspace/isaaclab/_isaac_sim/isaac-sim.sh --allow-root +EOP + +RUN chmod +x /root/.vnc/xstartup && \ + chmod +x /root/start_isaac_vnc.sh + +RUN /workspace/isaaclab/_isaac_sim/isaac-sim.sh --allow-root --ext-precache-mode + +RUN cd /root && \ + git clone https://github.com/Lifelong-Robot-Learning/LIBERO.git && \ + cd LIBERO && \ + git apply <<'EOP' +diff --git a/setup.py b/setup.py +index 59d4900..dbe9811 100644 +--- a/setup.py ++++ b/setup.py +@@ -13,7 +13,8 @@ long_description = "".join(lines) + + setup( + name="libero", +- packages=[package for package in find_packages() if package.startswith("libero")], ++ #packages=[package for package in find_packages() if package.startswith("libero")], ++ packages=["libero"], + install_requires=[], + eager_resources=["*"], + include_package_data=True, +EOP + +RUN cd /root/LIBERO && \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install -e . + +# libero config +RUN mkdir -p /root/.libero && \ +cat <<'EOP' > /root/.libero/config.yaml +assets: /root/LIBERO/libero/libero/./assets +bddl_files: /root/LIBERO/libero/libero/./bddl_files +benchmark_root: /root/LIBERO/libero/libero +datasets: /root/LIBERO/libero/libero/../datasets +init_states: /root/LIBERO/libero/libero/./init_files +EOP + +# from https://github.com/nvidia-china-sae/RobotLearningLab +COPY RobotLearningLab/ /root/RobotLearningLab/ + +RUN cd /workspace/isaaclab/ && \ + rm -rf source && \ + ln -s /root/RobotLearningLab/source source && \ + /workspace/isaaclab/_isaac_sim/python.sh -m pip install -e ./source/isaaclab +# Ray cmd +RUN /workspace/isaaclab/_isaac_sim/python.sh -m pip install colorama && \ +cat <<'EOP' >> /root/.bashrc +alias ray='/workspace/isaaclab/_isaac_sim/python.sh /workspace/isaaclab/_isaac_sim/kit/python/lib/python3.11/site-packages/ray/scripts/scripts.py' +EOP \ No newline at end of file diff --git a/verl_0720_main/verl/docker/Dockerfile.stable.sglang b/verl_0720_main/verl/docker/Dockerfile.stable.sglang new file mode 100644 index 0000000000000000000000000000000000000000..0939a4607ddac3d0b63e0670a6a4d3682e06e714 --- /dev/null +++ b/verl_0720_main/verl/docker/Dockerfile.stable.sglang @@ -0,0 +1,98 @@ +# sgl0512 + +FROM lmsysorg/sglang:v0.5.12 + +ARG CUDA_VERSION=13.0.2 +ARG TRL_VERSION=0.27.0 +ARG TRANSFORMER_ENGINE_VERSION=v2.15 +ARG FLASH_ATTENTION_VERSION=2.8.3 +ARG NSIGHT_VERSION=2025.6.1 +ARG MCORE_VERSION=core_v0.18.0 +ARG VERL_VERSION=v0.7.1 + +# ========================= +# Install cuDNN (network repo) +# ========================= +RUN ARCH=$(if [ "$(uname -m)" = "aarch64" ]; then echo "sbsa"; else echo "x86_64"; fi) && \ + CUDA_VERSION_MAJOR=$(echo ${CUDA_VERSION} | cut -d '.' -f 1) && \ + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/${ARCH}/cuda-keyring_1.1-1_all.deb && \ + sed -i '/developer\.download\.nvidia\.com\/compute\/cuda\/repos/d' /etc/apt/sources.list.d/* && \ + dpkg -i cuda-keyring_1.1-1_all.deb && \ + apt-get update && \ + apt-get -y --allow-downgrades --allow-change-held-packages install \ + cudnn9-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-dev-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-headers-cuda-${CUDA_VERSION_MAJOR} && \ + rm -f cuda-keyring_1.1-1_all.deb && \ + rm -rf /var/lib/apt/lists/* + +ARG PIP_NO_CACHE_DIR=1 + +RUN pip install pybind11 nvidia-mathdx + +RUN MAX_JOBS=128 pip install -v --disable-pip-version-check --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git + +RUN export NVTE_FRAMEWORK=pytorch && MAX_JOBS=128 NVTE_BUILD_THREADS_PER_JOB=4 pip3 install --resume-retries 999 --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@${TRANSFORMER_ENGINE_VERSION} + +# RUN pip install --upgrade transformers tokenizers + +RUN pip install codetiming mathruler pylatexenc cachetools pytest-asyncio + +RUN if [ "$(uname -m)" = "aarch64" ]; then \ + pip show "flash-attn-4"; \ + else \ + pip uninstall -y "flash-attn-4"; \ + fi + +RUN pip install --no-build-isolation flash_attn==2.8.3 + +RUN NSIGHT_VERSION=2025.6.1_2025.6.1.190-1_$(if [ "$(uname -m)" = "aarch64" ]; then echo "arm64"; else echo "amd64"; fi) && \ + wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_6/nsight-systems-${NSIGHT_VERSION}.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 && \ + apt-get install -y ./nsight-systems-${NSIGHT_VERSION}.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-${NSIGHT_VERSION}.deb + +# sglang image has already installed DeepEP + +RUN pip3 install --no-deps trl==0.27.0 + +RUN pip3 install nvtx matplotlib liger_kernel + +RUN pip install torchcodec --index-url=https://download.pytorch.org/whl/cu130 + +RUN pip install qwen-vl-utils==0.0.14 + +RUN if [ "$(uname -m)" = "aarch64" ]; then \ + pip show "sglang-kernel"; \ + else \ + wget https://github.com/sgl-project/whl/releases/download/v0.4.2.post2/sglang_kernel-0.4.2.post2+cu130-cp310-abi3-manylinux2014_x86_64.whl#sha256=4e7ce619274234d182b20da883fcf1d20e7e55cbea90d62244e2b6a3d6c0fc85 && \ + pip install sglang_kernel-0.4.2.post2+cu130-cp310-abi3-manylinux2014_x86_64.whl --force-reinstall; \ + fi + +RUN pip install -U git+https://github.com/ISEEKYAN/mbridge.git@main + +RUN pip install --no-deps megatron-bridge==0.5.0 + +RUN pip install --no-deps git+https://github.com/NVIDIA/Megatron-LM.git@${MCORE_VERSION} + +RUN pip install git+https://github.com/verl-project/verl.git@${VERL_VERSION} && \ + pip uninstall -y verl + +RUN CUDA_VERSION_MAJOR=$(echo ${CUDA_VERSION} | cut -d '.' -f 1) && \ + CUDNN_PKG=libcudnn9-cuda-${CUDA_VERSION_MAJOR} && \ + CUDNN_VERSION=$(dpkg-query -W -f='${Version}' "${CUDNN_PKG}" 2>/dev/null | sed 's/-[0-9]*$//') && \ + if [ -z "${CUDNN_VERSION}" ]; then \ + CUDNN_HDR=$(find /usr/include -name cudnn_version.h | head -1) && \ + CUDNN_VERSION=$(grep -E '^#define CUDNN_(MAJOR|MINOR|PATCHLEVEL) ' "${CUDNN_HDR}" | awk '{print $3}' | paste -sd. -); \ + fi && \ + pip install "nvidia-cudnn-cu${CUDA_VERSION_MAJOR}>=${CUDNN_VERSION}" + +# Override NCCL to >= 2.29.7 for ncclCommSuspend / ncclCommResume +# (RFC: https://github.com/verl-project/verl/issues/6266). +# TODO(xiefan46): remove once torch pin bumps to >= 2.12.0. +RUN pip install --no-deps --upgrade "nvidia-nccl-cu13>=2.29.7,<3.0" diff --git a/verl_0720_main/verl/docker/Dockerfile.stable.trtllm b/verl_0720_main/verl/docker/Dockerfile.stable.trtllm new file mode 100644 index 0000000000000000000000000000000000000000..9b78ae1169681a2547db0402b52e647600ec7a62 --- /dev/null +++ b/verl_0720_main/verl/docker/Dockerfile.stable.trtllm @@ -0,0 +1,109 @@ +# Base image from NGC TensorRT-LLM, which includes a pre-installed TensorRT-LLM. +# For available images, visit: https://nvidia.github.io/TensorRT-LLM/installation/containers.html +# Use TRTLLM_BASE_IMAGE to specify the base image (default: release:1.3.0rc15) +ARG TRTLLM_BASE_IMAGE=nvcr.io/nvidia/tensorrt-llm/release:1.3.0rc15 +FROM ${TRTLLM_BASE_IMAGE} + +# Clear TORCH_CUDA_ARCH_LIST inherited from the base image so that +# FlashInfer's check_cuda_arch() queries the actual GPU at runtime +# instead of rejecting GPUs not in the build-time arch list. +ENV TORCH_CUDA_ARCH_LIST="" + +# ============================================================================== +# Install Megatron dependencies +# ============================================================================== +# DeepEP is required for IBGDA support. +# Clone and build gdrcopy and deepep-nvshmem dependencies. +WORKDIR /home/dpsk_a2a +RUN git clone -b v2.5.1 https://github.com/NVIDIA/gdrcopy.git && \ + pushd gdrcopy && \ + make prefix=/usr/local lib_install && \ + popd && rm -rf gdrcopy && \ + pip install nvidia-nvshmem-cu13==3.3.20 && \ + export NVSHMEM_DIR=/usr/local/lib/python3.12/dist-packages/nvidia/nvshmem && \ + export LD_LIBRARY_PATH="${NVSHMEM_DIR}/lib:$LD_LIBRARY_PATH" && \ + export PATH="${NVSHMEM_DIR}/bin:$PATH" && \ + pushd ${NVSHMEM_DIR}/lib && \ + ln -s libnvshmem_host.so.3 libnvshmem_host.so && \ + popd && \ + git clone -b hybrid-ep https://github.com/deepseek-ai/DeepEP.git && \ + pushd DeepEP && \ + export CPATH=/usr/local/cuda/targets/$(uname -m | sed 's/aarch64/sbsa-linux/;s/x86_64/x86_64-linux/')/include/cccl:$CPATH && \ + TORCH_CUDA_ARCH_LIST="9.0 10.0 12.0" python setup.py install && \ + popd && rm -rf deepep + +# Install Python dependencies +RUN pip3 install --no-cache-dir --no-deps trl==0.27.0 && \ + pip3 install --no-cache-dir transformers==5.3.0 && \ + pip3 install --no-cache-dir nvtx matplotlib liger_kernel cachetools annotated-doc && \ + pip3 install --no-cache-dir cupy-cuda12x==14.0.1 && \ + pip install --no-cache-dir -U git+https://github.com/ISEEKYAN/mbridge.git@641a5a0 && \ + pip install --no-deps --no-cache-dir megatron-core==0.18.0 && \ + pip install --no-deps --no-cache-dir megatron-bridge==0.5.0 && \ + pip install --no-deps --no-cache-dir nvidia-resiliency-ext==0.6.0 + + +# ============================================================================== +# Install verl dependencies +# ============================================================================== +RUN pip install git+https://github.com/verl-project/verl.git@v0.7.1 +RUN pip uninstall -y verl +RUN pip install "verl[mcore] @ git+https://github.com/verl-project/verl.git@v0.7.1" +RUN pip uninstall -y verl + +# Bake in accelerate patches (idempotent; exits 1 if target text is missing) +RUN python3 - <<'PY' +from pathlib import Path +# (a) lazy-import bnb in accelerate/utils/__init__.py +path = Path("/usr/local/lib/python3.12/dist-packages/accelerate/utils/__init__.py") +text = path.read_text() +old = "from .bnb import has_4bit_bnb_layers, load_and_quantize_model\n" +new = ( + "def has_4bit_bnb_layers(*args, **kwargs):\n" + " from .bnb import has_4bit_bnb_layers as _has_4bit_bnb_layers\n" + " return _has_4bit_bnb_layers(*args, **kwargs)\n\n\n" + "def load_and_quantize_model(*args, **kwargs):\n" + " from .bnb import load_and_quantize_model as _load_and_quantize_model\n" + " return _load_and_quantize_model(*args, **kwargs)\n" +) +if old in text: + path.write_text(text.replace(old, new, 1)); print("Patched accelerate.utils bnb lazy imports") +elif "def load_and_quantize_model(*args, **kwargs):" in text: + print("accelerate.utils bnb lazy import patch already present") +else: + raise RuntimeError("accelerate.utils bnb import patch target not found") +# (b) numpy multiarray lookup in accelerate/utils/other.py +other_path = Path("/usr/local/lib/python3.12/dist-packages/accelerate/utils/other.py") +text = other_path.read_text() +old = ( + 'np_core = np._core if is_numpy_available("2.0.0") else np.core\n' + "TORCH_SAFE_GLOBALS = [\n" + " # numpy arrays are just numbers, not objects, so we can reconstruct them safely\n" + " np_core.multiarray._reconstruct,\n" +) +new = ( + 'np_core = np._core if is_numpy_available("2.0.0") else np.core\n' + "np_multiarray = getattr(np_core, \"multiarray\", None)\n" + "if np_multiarray is None:\n" + " np_multiarray = np_core._multiarray_umath\n" + "TORCH_SAFE_GLOBALS = [\n" + " # numpy arrays are just numbers, not objects, so we can reconstruct them safely\n" + " np_multiarray._reconstruct,\n" +) +if old in text: + other_path.write_text(text.replace(old, new, 1)); print("Patched accelerate.utils.other numpy multiarray lookup") +elif "np_multiarray = getattr(np_core" in text: + print("accelerate.utils.other numpy multiarray patch already present") +else: + raise RuntimeError("accelerate.utils.other numpy patch target not found") +PY +RUN python3 -c "import transformers, accelerate; print('transformers:', transformers.__version__, 'accelerate:', accelerate.__version__)" + +# Pin Ray to a version compatible with TRT-LLM 1.3.0rc15 +RUN pip install --no-cache-dir "ray[default]==2.54.1" + +# ============================================================================== +# Install a specific TensorRT-LLM on demand +# ============================================================================== +# Note: The NGC image already includes a pre-installed TensorRT-LLM, but you can install a specific version if needed. +# Refer to https://nvidia.github.io/TensorRT-LLM/installation/index.html for more details. diff --git a/verl_0720_main/verl/docker/Dockerfile.stable.vllm b/verl_0720_main/verl/docker/Dockerfile.stable.vllm new file mode 100644 index 0000000000000000000000000000000000000000..b2fbc12992b6d8c753007d5223206d96bf0c157e --- /dev/null +++ b/verl_0720_main/verl/docker/Dockerfile.stable.vllm @@ -0,0 +1,176 @@ +# vllm: x86_64=0.20.2, aarch64=0.20.2 + +ARG CUDA_VERSION=13.0.2 +FROM nvidia/cuda:${CUDA_VERSION}-devel-ubuntu24.04 + +ARG CUDA_VERSION +ARG PYTHON_VERSION=3.12 +ARG TORCH_VERSION=2.11.0 +ARG TORCH_VISION_VERSION=0.26.0 +ARG TORCH_AUDIO_VERSION=2.11.0 +ARG TRANSFORMERS_VERSION=5.3.0 +ARG VLLM_VERSION=0.23.0 +ARG TRL_VERSION=0.27.0 +ARG TRANSFORMER_ENGINE_VERSION=v2.15 +ARG FLASH_ATTENTION_VERSION=2.8.3 +ARG NSIGHT_VERSION=2025.6.1 +ARG MCORE_VERSION=core_v0.18.0 +ARG VERL_VERSION=v0.7.1 +ARG DEBIAN_FRONTEND=noninteractive +ARG PIP_NO_CACHE_DIR=1 +ARG APT_MIRROR="" +# PEP 668: Ubuntu 24.04 blocks system-wide pip installs; override for Docker +ENV PIP_BREAK_SYSTEM_PACKAGES=1 + +RUN if [ -n "${APT_MIRROR}" ]; then \ + sed -i "s@http://.*archive.ubuntu.com@${APT_MIRROR}@g" /etc/apt/sources.list.d/ubuntu.sources; \ + fi + +RUN apt-get update && apt-get install -y \ + git \ + wget \ + curl \ + cmake \ + build-essential \ + libibverbs-dev \ + libnuma-dev \ + librdmacm-dev \ + numactl \ + software-properties-common \ + vim \ + python${PYTHON_VERSION} \ + python${PYTHON_VERSION}-dev \ + && rm -rf /var/lib/apt/lists/* + +RUN wget https://bootstrap.pypa.io/get-pip.py && \ + python${PYTHON_VERSION} get-pip.py && \ + rm get-pip.py + +RUN ln -sf /usr/bin/python${PYTHON_VERSION} /usr/bin/python3 && \ + ln -sf /usr/bin/python${PYTHON_VERSION} /usr/bin/python + +RUN pip install torch==${TORCH_VERSION} torchvision==${TORCH_VISION_VERSION} torchaudio==${TORCH_AUDIO_VERSION} --index-url https://download.pytorch.org/whl/cu130 + +RUN pip install pybind11 wheel + +# ========================= +# Install cuDNN (network repo) +# ========================= +RUN ARCH=$(if [ "$(uname -m)" = "aarch64" ]; then echo "sbsa"; else echo "x86_64"; fi) && \ + CUDA_VERSION_MAJOR=$(echo ${CUDA_VERSION} | cut -d '.' -f 1) && \ + wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/${ARCH}/cuda-keyring_1.1-1_all.deb && \ + sed -i '/developer\.download\.nvidia\.com\/compute\/cuda\/repos/d' /etc/apt/sources.list.d/* && \ + dpkg -i cuda-keyring_1.1-1_all.deb && \ + apt-get update && \ + apt-get -y --allow-downgrades --allow-change-held-packages install \ + cudnn9-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-dev-cuda-${CUDA_VERSION_MAJOR} \ + libcudnn9-headers-cuda-${CUDA_VERSION_MAJOR} && \ + rm -f cuda-keyring_1.1-1_all.deb && \ + rm -rf /var/lib/apt/lists/* + +RUN pip install nvidia-mathdx ninja + +RUN MAX_JOBS=256 pip install -v --disable-pip-version-check --no-build-isolation \ + --config-settings "--build-option=--cpp_ext" \ + --config-settings "--build-option=--cuda_ext" \ + git+https://github.com/NVIDIA/apex.git + +RUN export NVTE_FRAMEWORK=pytorch && \ + MAX_JOBS=256 NVTE_BUILD_THREADS_PER_JOB=4 \ + pip3 install --resume-retries 999 --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@${TRANSFORMER_ENGINE_VERSION} + + RUN pip install codetiming mathruler pylatexenc cachetools pytest-asyncio + +RUN export FLASH_ATTENTION_FORCE_BUILD="TRUE" && MAX_JOBS=32 pip install --no-build-isolation flash_attn==${FLASH_ATTENTION_VERSION} + +RUN NSIGHT_VERSION=2025.6.1_2025.6.1.190-1_$(if [ "$(uname -m)" = "aarch64" ]; then echo "arm64"; else echo "amd64"; fi) && \ + wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_6/nsight-systems-${NSIGHT_VERSION}.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 && \ + apt-get install -y ./nsight-systems-${NSIGHT_VERSION}.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-${NSIGHT_VERSION}.deb && \ + rm -rf /var/lib/apt/lists/* + +# ========================= +# Install DeepEP +# ========================= +RUN cd /home && mkdir -p dpsk_a2a && cd dpsk_a2a && \ + git clone -b v2.5.1 https://github.com/NVIDIA/gdrcopy.git && \ + cd gdrcopy && \ + make prefix=/usr/local lib_install && \ + cd .. && rm -rf gdrcopy && \ + git clone -b hybrid-ep https://github.com/deepseek-ai/DeepEP.git && \ + export NVSHMEM_DIR=/usr/local/lib/python3.12/dist-packages/nvidia/nvshmem && \ + export LD_LIBRARY_PATH="${NVSHMEM_DIR}/lib:$LD_LIBRARY_PATH" && \ + export PATH="${NVSHMEM_DIR}/bin:$PATH" && \ + cd ${NVSHMEM_DIR}/lib && \ + ln -sf libnvshmem_host.so.3 libnvshmem_host.so && \ + cd /home/dpsk_a2a/DeepEP && \ + git checkout 3f601f7ac1c062c46502646ff04c535013bfca00 && \ + CUDA_TARGET=$(uname -m | sed 's/aarch64/sbsa-linux/;s/x86_64/x86_64-linux/') && \ + export CPATH=/usr/local/cuda/targets/${CUDA_TARGET}/include/cccl:$CPATH && \ + TORCH_CUDA_ARCH_LIST="9.0;10.0" python setup.py install + +# Debian python3-jwt has no pip RECORD; vllm cannot uninstall it when upgrading PyJWT. +RUN apt-get update && \ + apt-get remove -y --purge python3-jwt 2>/dev/null || true && \ + rm -rf /var/lib/apt/lists/* && \ + pip install --ignore-installed PyJWT + +# Apply two unmerged vLLM fixes required by verl weight-sync flows by patching in each +# PR's cumulative diff (GitHub ".diff") with `git apply --3way`, rather than cherry-picking +# commits or ranges. These PR branches are periodically rebased and even contain +# "Merge branch 'main'" commits, so a "v${VLLM_VERSION}_fix..pr" range replays hundreds of +# unrelated main commits and aborts on an empty/merge commit. The net diff carries only the +# PR's own changes, so it stays small and stable regardless of how the branch is rebased. +# #44483: illegal memory access during a partial wake_up (sleep mode). +# #45589: FlashInfer-TRTLLM MoE load OOM. +RUN git clone https://github.com/vllm-project/vllm.git && cd vllm && \ + git checkout v${VLLM_VERSION} && \ + curl -fsSL https://github.com/vllm-project/vllm/pull/44483.diff -o /tmp/vllm-pr-44483.diff && \ + git apply --3way --whitespace=nowarn /tmp/vllm-pr-44483.diff && \ + curl -fsSL https://github.com/vllm-project/vllm/pull/45589.diff -o /tmp/vllm-pr-45589.diff && \ + git apply --3way --whitespace=nowarn /tmp/vllm-pr-45589.diff && \ + MAX_JOBS=256 pip install -e . + +RUN pip3 install --no-deps trl==${TRL_VERSION} + +RUN pip3 install nvtx matplotlib liger_kernel + +RUN pip install transformers==${TRANSFORMERS_VERSION} + +RUN pip install -U git+https://github.com/ISEEKYAN/mbridge.git@main + +RUN pip install --no-deps megatron-bridge==0.5.0 + +RUN pip install --no-deps git+https://github.com/NVIDIA/Megatron-LM.git@${MCORE_VERSION} + +RUN pip install torchcodec --index-url=https://download.pytorch.org/whl/cu130 + +RUN apt-get update && \ + apt-get install -y ffmpeg && \ + ffmpeg -decoders | grep -i nvidia && \ + rm -rf /var/lib/apt/lists/* + +RUN pip install qwen-vl-utils==0.0.14 + +RUN pip install git+https://github.com/verl-project/verl.git@${VERL_VERSION} && pip uninstall -y verl + +RUN CUDA_VERSION_MAJOR=$(echo ${CUDA_VERSION} | cut -d '.' -f 1) && \ + CUDNN_PKG=libcudnn9-cuda-${CUDA_VERSION_MAJOR} && \ + CUDNN_VERSION=$(dpkg-query -W -f='${Version}' "${CUDNN_PKG}" 2>/dev/null | sed 's/-[0-9]*$//') && \ + if [ -z "${CUDNN_VERSION}" ]; then \ + CUDNN_HDR=$(find /usr/include -name cudnn_version.h | head -1) && \ + CUDNN_VERSION=$(grep -E '^#define CUDNN_(MAJOR|MINOR|PATCHLEVEL) ' "${CUDNN_HDR}" | awk '{print $3}' | paste -sd. -); \ + fi && \ + pip install "nvidia-cudnn-cu${CUDA_VERSION_MAJOR}>=${CUDNN_VERSION}" + +# Override NCCL to >= 2.29.7 for ncclCommSuspend / ncclCommResume +# (RFC: https://github.com/verl-project/verl/issues/6266). +# TODO(xiefan46): remove once torch pin bumps to >= 2.12.0. +RUN pip install --no-deps --upgrade "nvidia-nccl-cu13>=2.29.7,<3.0" diff --git a/verl_0720_main/verl/docker/README.md b/verl_0720_main/verl/docker/README.md new file mode 100644 index 0000000000000000000000000000000000000000..728479484b2cb910e12c8e368e6d6dd2eb669e48 --- /dev/null +++ b/verl_0720_main/verl/docker/README.md @@ -0,0 +1,93 @@ +# Dockerfiles of verl + +We provide pre-built Docker images for quick setup. And from this version, we utilize a new image release hierarchy for productivity and stability. + +Start from v0.6.0, we use vllm and sglang release image as our base image. + +Start from v0.7.0, since vllm/vllm-openai:v0.12.0 is a minimal image without some essential libraries, we use nvidia/cuda:12.9.1-devel-ubuntu22.04 as our base image for vllm. + +## Base Image + +- vLLM: https://hub.docker.com/r/nvidia/cuda +- SGLang: https://hub.docker.com/r/lmsysorg/sglang + +## Application Image + +Upon base image, the following packages are added: +- flash_attn +- Megatron-LM +- Apex +- TransformerEngine +- DeepEP + +Latest docker file: +- [Dockerfile.stable.vllm](https://github.com/verl-project/verl/blob/main/docker/Dockerfile.stable.vllm) +- [Dockerfile.stable.sglang](https://github.com/verl-project/verl/blob/main/docker/Dockerfile.stable.sglang) + +All pre-built images are available in dockerhub: https://hub.docker.com/r/verlai/verl. For example, `verlai/verl:sgl059.latest`, `verlai/verl:vllm017.latest`. + +You can find the latest images used for development and ci in our github workflows: +- [.github/workflows/vllm.yml](https://github.com/verl-project/verl/blob/main/.github/workflows/vllm.yml) +- [.github/workflows/sgl.yml](https://github.com/verl-project/verl/blob/main/.github/workflows/sgl.yml) + + +## Building Locally + +To build an image from source: + +```sh +docker build -f docker/Dockerfile.stable.vllm -t verl:vllm-local . +``` + +For users in China who need an apt mirror to speed up package downloads, pass `APT_MIRROR`: + +```sh +docker build -f docker/Dockerfile.stable.vllm \ + --build-arg APT_MIRROR=https://mirrors.tuna.tsinghua.edu.cn \ + -t verl:vllm-local . +``` + +### GB200 / aarch64 + +Pre-built images for GB200 (aarch64) are not yet published. Users should build locally on an aarch64 machine. Pre-built images will be added once available. + +```sh +docker build -f docker/Dockerfile.stable.vllm -t verl:vllm-arm64 . +``` + +## Installation from Docker + +After pulling the desired Docker image and installing desired inference and training frameworks, you can run it with the following steps: + +1. Launch the desired Docker image and attach into it: + +```sh +docker create --runtime=nvidia --gpus all --net=host --shm-size="10g" --cap-add=SYS_ADMIN -v .:/workspace/verl --name verl sleep infinity +docker start verl +docker exec -it verl bash +``` + +2. If you use the images provided, you only need to install verl itself without dependencies: + +```sh +# install the nightly version (recommended) +git clone https://github.com/verl-project/verl && cd verl +pip3 install --no-deps -e . +``` + +[Optional] If you hope to switch between different frameworks, you can install verl with the following command: + +```sh +# install the nightly version (recommended) +git clone https://github.com/verl-project/verl && cd verl +pip3 install -e .[vllm] +pip3 install -e .[sglang] +``` + +## Release History + +- 2026/03/10: update vllm stable image to vllm==0.17.0; update sglang stable image to sglang==0.5.9 +- 2026/01/17: update vllm stable image to torch==2.9.1, cudnn==9.16, deepep==1.2.1 +- 2025/12/23: update vllm stable image to vllm==0.12.0; update sglang stable image to sglang==0.5.6 +- 2025/11/18: update vllm stable image to vllm==0.11.1; update sglang stable image to sglang==0.5.5 + diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a2 new file mode 100644 index 0000000000000000000000000000000000000000..1b0f46bed2afeb618db0a8402798e30e38351f97 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a2 @@ -0,0 +1,84 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.3.rc1-910b-ubuntu22.04-py3.11 + +ARG ASCEND_CANN_PATH="/usr/local/Ascend" +ARG PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple" +ARG PTA_BASE_VERSION="torch_npu-2.7.1.post2-cp311-cp311-manylinux_2_28" +ARG PTA_URL="https://gitcode.com/Ascend/pytorch/releases/download/v7.3.0-pytorch2.7.1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential net-tools iputils-ping && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip config set global.index-url ${PIP_INDEX_URL} && \ + pip config set install.trusted-host mirrors.aliyun.com && \ + pip install --upgrade pip setuptools packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + echo "[LOG INFO] Detected architecture: $ARCH" && \ + # Set extra pip index for x86_64 platform + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.5.8 https://github.com/sgl-project/sglang.git && \ + git clone https://github.com/sgl-project/sgl-kernel-npu.git && cd sgl-kernel-npu && git checkout 46b73de && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. + +# Install repositories with low update frequency +RUN cd sglang && \ + # Install sglang + mv python/pyproject.toml python/pyproject.toml.backup && \ + mv python/pyproject_other.toml python/pyproject.toml && \ + pip install -e "python[srt_npu]" && \ + pip install torch==2.7.1 torchvision==0.22.1 && \ + # Install torch_npu + ARCH=$(uname -m) && wget ${PTA_URL}/${PTA_BASE_VERSION}_${ARCH}.whl && pip install ${PTA_BASE_VERSION}_${ARCH}.whl && \ + echo "[LOG INFO] Torch_npu version is: ${PTA_BASE_VERSION}_${ARCH}.whl" && \ + cd .. + +# Install sgl-kernel-npu +RUN ARCH=$(uname -m) && \ + # Export and source env + export LD_LIBRARY_PATH=${ASCEND_CANN_PATH}/ascend-toolkit/8.3.RC1/${ARCH}-linux/devlib/linux/${ARCH}:$LD_LIBRARY_PATH && \ + source ${ASCEND_CANN_PATH}/ascend-toolkit/set_env.sh && \ + source ${ASCEND_CANN_PATH}/nnal/atb/set_env.sh && \ + pip install pybind11 && \ + cd sgl-kernel-npu && \ + bash build.sh && \ + pip install output/torch_memory_saver*.whl && \ + pip install output/sgl_kernel_npu*.whl && \ + # Deep_ep package is compiled for A3 by default; Recompile in deepep2 mode for A2, following https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/deep_ep/README.md. + bash build.sh -a deepep2 && \ + pip install output/deep_ep*.whl && \ + cd "$(pip show deep-ep | grep -E '^Location:' | awk '{print $2}')" && ln -s deep_ep/deep_ep_cpp*.so && cd - && \ + cd .. + +# Install MindSpeed & Megatron +RUN pip install -e MindSpeed && \ + pip install git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.1 && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton timm && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + pip install ray==2.46.0 click==8.2.1 cachetools setuptools==80.10.2 nvtx && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a3 new file mode 100644 index 0000000000000000000000000000000000000000..dc9133764019db8bf7fd8fb669709ed457ddf3ae --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.3.rc1_a3 @@ -0,0 +1,82 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.3.rc1-a3-ubuntu22.04-py3.11 + +ARG ASCEND_CANN_PATH="/usr/local/Ascend" +ARG PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple" +ARG PTA_BASE_VERSION="torch_npu-2.7.1.post2-cp311-cp311-manylinux_2_28" +ARG PTA_URL="https://gitcode.com/Ascend/pytorch/releases/download/v7.3.0-pytorch2.7.1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential net-tools iputils-ping && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip config set global.index-url ${PIP_INDEX_URL} && \ + pip config set install.trusted-host mirrors.aliyun.com && \ + pip install --upgrade pip setuptools packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + echo "[LOG INFO] Detected architecture: $ARCH" && \ + # Set extra pip index for x86_64 platform + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.5.8 https://github.com/sgl-project/sglang.git && \ + git clone https://github.com/sgl-project/sgl-kernel-npu.git && cd sgl-kernel-npu && git checkout 46b73de && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. + +# Install repositories with low update frequency +RUN cd sglang && \ + # Install sglang + mv python/pyproject.toml python/pyproject.toml.backup && \ + mv python/pyproject_other.toml python/pyproject.toml && \ + pip install -e "python[srt_npu]" && \ + pip install torch==2.7.1 torchvision==0.22.1 && \ + # Install torch_npu + ARCH=$(uname -m) && wget ${PTA_URL}/${PTA_BASE_VERSION}_${ARCH}.whl && pip install ${PTA_BASE_VERSION}_${ARCH}.whl && \ + echo "[LOG INFO] Torch_npu version is: ${PTA_BASE_VERSION}_${ARCH}.whl" && \ + cd .. + +# Install sgl-kernel-npu +RUN ARCH=$(uname -m) && \ + # Export and source env + export LD_LIBRARY_PATH=${ASCEND_CANN_PATH}/ascend-toolkit/8.3.RC1/${ARCH}-linux/devlib/linux/${ARCH}:$LD_LIBRARY_PATH && \ + source ${ASCEND_CANN_PATH}/ascend-toolkit/set_env.sh && \ + source ${ASCEND_CANN_PATH}/nnal/atb/set_env.sh && \ + pip install pybind11 && \ + cd sgl-kernel-npu && \ + bash build.sh && \ + pip install output/torch_memory_saver*.whl && \ + pip install output/sgl_kernel_npu*.whl && \ + pip install output/deep_ep*.whl && \ + cd "$(pip show deep-ep | grep -E '^Location:' | awk '{print $2}')" && ln -s deep_ep/deep_ep_cpp*.so && cd - && \ + cd .. + +# Install MindSpeed & Megatron +RUN pip install -e MindSpeed && \ + pip install git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.1 && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton timm && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + pip install ray==2.46.0 click==8.2.1 cachetools setuptools==80.10.2 nvtx && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2 new file mode 100644 index 0000000000000000000000000000000000000000..ca377fb32cfda6d49aa8ddc591a3a5d136c8b525 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a2 @@ -0,0 +1,77 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-910b-ubuntu22.04-py3.11 + +ARG PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple" +ARG PTA_BASE_VERSION="torch_npu-2.8.0.post2-cp311-cp311-manylinux_2_28" +ARG PTA_URL="https://gitcode.com/Ascend/pytorch/releases/download/v7.3.0-pytorch2.8.0" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential net-tools iputils-ping unzip ca-certificates && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip config set global.index-url ${PIP_INDEX_URL} && \ + pip config set install.trusted-host mirrors.aliyun.com && \ + pip install --upgrade pip setuptools==80.10.2 packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + echo "[LOG INFO] Detected architecture: $ARCH" && \ + # Set extra pip index for x86_64 platform + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone https://github.com/sgl-project/sglang.git && cd sglang && git checkout v0.5.10 && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. + +# Install repositories with low update frequency +RUN cd sglang && \ + # Install sglang + mv python/pyproject.toml python/pyproject.toml.backup && \ + mv python/pyproject_npu.toml python/pyproject.toml && \ + pip install torch==2.8.0 torchvision==0.23.0 && \ + # Install torch_npu + ARCH=$(uname -m) && wget ${PTA_URL}/${PTA_BASE_VERSION}_${ARCH}.whl && pip install ${PTA_BASE_VERSION}_${ARCH}.whl && \ + echo "[LOG INFO] Torch_npu version is: ${PTA_BASE_VERSION}_${ARCH}.whl" && \ + pip install -e python[all_npu] && \ + pip install torch==2.8.0 torchvision==0.23.0 && \ + cd .. + +# Install sgl-kernel-npu +RUN ARCH=$(uname -m) && wget --no-check-certificate https://github.com/sgl-project/sgl-kernel-npu/releases/download/2026.02.01/sgl-kernel-npu-2026.02.01-torch2.8.0-py311-cann8.5.0-910b-${ARCH}.zip && \ + unzip sgl-kernel-npu*.zip && \ + pip install torch_memory_saver*.whl && \ + pip install sgl_kernel_npu*.whl && \ + pip install deep_ep*.whl + +# Install MindSpeed & Megatron +RUN pip install -e MindSpeed && \ + pip install git+https://github.com/NVIDIA/Megatron-LM.git@core_r0.16.0 && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton timm && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Prepare and install verl (update frequently) +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + git clone --recursive https://github.com/volcengine/verl.git && \ + cd verl/recipe && git checkout main && cd .. && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + pip install click==8.2.1 cachetools nvtx opencv-python-headless==4.10.0.84 && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] + diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3 new file mode 100644 index 0000000000000000000000000000000000000000..8a871a6fe0fbe9a5f5e00db8051ee0ef0414e374 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend.sglang_8.5.0_a3 @@ -0,0 +1,76 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-a3-ubuntu22.04-py3.11 + +ARG PIP_INDEX_URL="https://mirrors.aliyun.com/pypi/simple" +ARG PTA_BASE_VERSION="torch_npu-2.8.0.post2-cp311-cp311-manylinux_2_28" +ARG PTA_URL="https://gitcode.com/Ascend/pytorch/releases/download/v7.3.0-pytorch2.8.0" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential net-tools iputils-ping unzip ca-certificates && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip config set global.index-url ${PIP_INDEX_URL} && \ + pip config set install.trusted-host mirrors.aliyun.com && \ + pip install --upgrade pip setuptools==80.10.2 packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + echo "[LOG INFO] Detected architecture: $ARCH" && \ + # Set extra pip index for x86_64 platform + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone https://github.com/sgl-project/sglang.git && cd sglang && git checkout v0.5.10 && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. + +# Install repositories with low update frequency +RUN cd sglang && \ + # Install sglang + mv python/pyproject.toml python/pyproject.toml.backup && \ + mv python/pyproject_npu.toml python/pyproject.toml && \ + pip install torch==2.8.0 torchvision==0.23.0 && \ + # Install torch_npu + ARCH=$(uname -m) && wget ${PTA_URL}/${PTA_BASE_VERSION}_${ARCH}.whl && pip install ${PTA_BASE_VERSION}_${ARCH}.whl && \ + echo "[LOG INFO] Torch_npu version is: ${PTA_BASE_VERSION}_${ARCH}.whl" && \ + pip install -e python[all_npu] && \ + pip install torch==2.8.0 torchvision==0.23.0 && \ + cd .. + +# Install sgl-kernel-npu +RUN ARCH=$(uname -m) && wget --no-check-certificate https://github.com/sgl-project/sgl-kernel-npu/releases/download/2026.02.01/sgl-kernel-npu-2026.02.01-torch2.8.0-py311-cann8.5.0-a3-${ARCH}.zip && \ + unzip sgl-kernel-npu*.zip && \ + pip install torch_memory_saver*.whl && \ + pip install sgl_kernel_npu*.whl && \ + pip install deep_ep*.whl + +# Install MindSpeed & Megatron +RUN pip install -e MindSpeed && \ + pip install git+https://github.com/NVIDIA/Megatron-LM.git@core_r0.16.0 && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton timm && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Prepare and install verl (update frequently) +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + git clone --recursive https://github.com/volcengine/verl.git && \ + cd verl/recipe && git checkout main && cd .. && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + pip install click==8.2.1 cachetools nvtx opencv-python-headless==4.10.0.84 && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a2 new file mode 100644 index 0000000000000000000000000000000000000000..c60df0c1712159e243d090ac35ea720186fb1c1b --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a2 @@ -0,0 +1,61 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.2.rc1-910b-ubuntu22.04-py3.11 + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip setuptools packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.9.1 https://github.com/vllm-project/vllm && \ + git clone --depth 1 --branch v0.9.1 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.2.RC1/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.2.RC1/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install torch & torch_npu & torchvision + pip install torch==2.5.1 torch_npu==2.5.1 torchvision==0.20.1 && \ + # Install vllm + cd vllm && VLLM_TARGET_DEVICE=empty pip install -v -e . && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +ENV PYTHONPATH="/Megatron-LM${PYTHONPATH:+:${PYTHONPATH}}" + +# Prepare and install verl (update frequently) +RUN git clone --depth 1 https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a3 new file mode 100644 index 0000000000000000000000000000000000000000..15850d04577e7bd8d2c1dbfa1b2a8b4aaa343e4f --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.2.rc1_a3 @@ -0,0 +1,61 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.2.rc1-a3-ubuntu22.04-py3.11 + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip setuptools packaging && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.9.1 https://github.com/vllm-project/vllm && \ + git clone --depth 1 --branch v0.9.1 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.2.RC1/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.2.RC1/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install torch & torch_npu & torchvision + pip install torch==2.5.1 torch_npu==2.5.1 torchvision==0.20.1 && \ + # Install vllm + cd vllm && VLLM_TARGET_DEVICE=empty pip install -v -e . && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +ENV PYTHONPATH="/Megatron-LM${PYTHONPATH:+:${PYTHONPATH}}" + +# Prepare and install verl (update frequently) +RUN git clone --depth 1 https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a2 new file mode 100644 index 0000000000000000000000000000000000000000..013df8ff3c78eb9cb7f842fe6657e682bd7c7f45 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a2 @@ -0,0 +1,65 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.3.rc1-910b-ubuntu22.04-py3.11 + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.11.0 https://github.com/vllm-project/vllm.git && \ + git clone --depth 1 --branch v0.11.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.3.RC1/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.3.RC1/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install torch & torch_npu & torchvision + pip install torch==2.7.1 torch_npu==2.7.1 torchvision==0.22.1 transformers==4.57.6 && \ + # Install vllm + cd vllm && VLLM_TARGET_DEVICE=empty pip install -v -e . && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton triton-ascend && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +ENV PYTHONPATH="/Megatron-LM${PYTHONPATH:+:${PYTHONPATH}}" + +# Prepare and install verl (update frequently) +RUN git clone --depth 1 https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a3 new file mode 100644 index 0000000000000000000000000000000000000000..abf5cbfe91753acc731819cbd6d0891b7d1fdbaa --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.3.rc1_a3 @@ -0,0 +1,65 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.3.rc1-a3-ubuntu22.04-py3.11 + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.11.0 https://github.com/vllm-project/vllm.git && \ + git clone --depth 1 --branch v0.11.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout f2b0977e && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.3.RC1/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/ascend-toolkit/8.3.RC1/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install torch & torch_npu & torchvision + pip install torch==2.7.1 torch_npu==2.7.1 torchvision==0.22.1 transformers==4.57.6 && \ + # Install vllm + cd vllm && VLLM_TARGET_DEVICE=empty pip install -v -e . && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + # Remove existing triton or triton-ascend installed by some third-party packages + pip uninstall -y triton triton-ascend && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +ENV PYTHONPATH="/Megatron-LM${PYTHONPATH:+:${PYTHONPATH}}" + +# Prepare and install verl (update frequently) +RUN git clone --depth 1 https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2 new file mode 100644 index 0000000000000000000000000000000000000000..ad739f76c010cdb387c9ad97d5c11b045296f5e1 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2 @@ -0,0 +1,74 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-910b-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910b1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout 2.3.0_core_r0.12.1 && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install transformers + pip install transformers==4.57.6 && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Remove existing triton installed by some third-party packages + pip uninstall -y triton && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2_v0.7.1 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2_v0.7.1 new file mode 100644 index 0000000000000000000000000000000000000000..9b1a474786a6822dd94c9011c71cac96e2f6a06a --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a2_v0.7.1 @@ -0,0 +1,75 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-910b-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910b1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.13.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.13.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout 2.3.0_core_r0.12.1 && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install transformers + pip install transformers==4.57.6 && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Remove existing triton installed by some third-party packages + pip uninstall -y triton && \ + # Install mbridge + pip install mbridge==0.15.1 torch_npu==2.8.0 && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (recipe update frequently, verl is release/v0.7.1) +RUN git clone -b release/v0.7.1 https://github.com/verl-project/verl.git && cd verl && \ + git submodule update --init --recursive --remote && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3 new file mode 100644 index 0000000000000000000000000000000000000000..544b2ee7e5e14368cbf28f0e7833ab4710d61820 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3 @@ -0,0 +1,79 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-a3-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910_9392" + +ENV GIT_SSL_NO_VERIFY=true +RUN pip config --user set global.index https://mirrors.huaweicloud.com/repository/pypi && \ + pip config --user set global.index-url https://mirrors.huaweicloud.com/repository/pypi/simple && \ + pip config --user set global.trusted-host mirrors.huaweicloud.com + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout 2.3.0_core_r0.12.1 && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install transformers + pip install transformers==4.57.6 && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Remove existing triton installed by some third-party packages + pip uninstall -y triton && \ + # Install mbridge + pip install mbridge && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3_v0.7.1 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3_v0.7.1 new file mode 100644 index 0000000000000000000000000000000000000000..966b7e4ba4883b1df9e1e9dcac745517f0686b3f --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.0_a3_v0.7.1 @@ -0,0 +1,75 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.0-a3-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910_9392" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.13.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.13.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout 2.3.0_core_r0.12.1 && cd .. && \ + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install transformers + pip install transformers==4.57.6 && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Remove existing triton installed by some third-party packages + pip uninstall -y triton && \ + # Install mbridge + pip install mbridge torch_npu==2.8.0 && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (recipe update frequently, verl is release/v0.7.1) +RUN git clone -b release/v0.7.1 https://github.com/verl-project/verl.git && cd verl && \ + git submodule update --init --recursive --remote && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a2_qwen3-5 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a2_qwen3-5 new file mode 100644 index 0000000000000000000000000000000000000000..6da873e718317d990a4eb539fa3734882d666446 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a2_qwen3-5 @@ -0,0 +1,114 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.1-910b-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910b1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# 更新cann版本 +RUN set -e && \ + cd /tmp && \ + ARCH=$(uname -m) && \ + if [ "$ARCH" = "aarch64" ]; then \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-toolkit_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-toolkit_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-toolkit_8.5.2_linux-aarch64.run && \ + + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-910b-ops_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-910b-ops_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-910b-ops_8.5.2_linux-aarch64.run && \ + + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-nnal_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-nnal_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-nnal_8.5.2_linux-aarch64.run ; \ + elif [ "$ARCH" = "x86_64" ]; then \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-toolkit_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-toolkit_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-toolkit_8.5.2_linux-x86_64.run && \ + + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-910b-ops_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-910b-ops_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-910b-ops_8.5.2_linux-x86_64.run && \ + + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-nnal_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-nnal_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-nnal_8.5.2_linux-x86_64.run ; \ + fi && \ + + source /usr/local/Ascend/cann/set_env.sh && \ + rm -rf /tmp/* + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone https://github.com/vllm-project/vllm-ascend.git && \ + cd vllm-ascend && git checkout 54879467c41784a446aa5b486a391d9bfbf488fa && cd .. && \ + git clone https://github.com/huggingface/transformers.git && \ + cd transformers && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git + + +# 避免vllm-ascend 安装失败 +ENV MAX_JOBS=1 +ENV MAKEFLAGS="-j1" +ENV CMAKE_BUILD_PARALLEL_LEVEL=1 + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.2/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.2/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install MindSpeed + pip install -e MindSpeed && \ + # Install Megatron-LM + pip install -e Megatron-LM && \ + # Install Megatron-Bridge + pip install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@de93536e --no-deps --no-build-isolation && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install -v -e . --no-build-isolation && cd .. && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install torch & torch_npu & torchvision + pip install torch==2.9.0 torch_npu==2.9.0 torchvision==0.24.0 accelerate==1.13.0 mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (update frequently) +RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && git checkout 4045d67063052dcb800c918c107b8d5a87046006 && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] \ No newline at end of file diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a3_qwen3-5 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a3_qwen3-5 new file mode 100644 index 0000000000000000000000000000000000000000..d9bd13fc1294f0f0115ea6ad3dd8d72684d04641 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_8.5.2_a3_qwen3-5 @@ -0,0 +1,114 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:8.5.1-a3-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910_9392" + +RUN set -e && \ + cd /tmp && \ + ARCH=$(uname -m) && \ + if [ "$ARCH" = "aarch64" ]; then \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-toolkit_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-toolkit_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-toolkit_8.5.2_linux-aarch64.run && \ + + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-A3-ops_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-A3-ops_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-A3-ops_8.5.2_linux-aarch64.run && \ + + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-nnal_8.5.2_linux-aarch64.run && \ + bash Ascend-cann-nnal_8.5.2_linux-aarch64.run --quiet --install && \ + rm -f Ascend-cann-nnal_8.5.2_linux-aarch64.run ; \ + elif [ "$ARCH" = "x86_64" ]; then \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-toolkit_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-toolkit_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-toolkit_8.5.2_linux-x86_64.run && \ + + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-A3-ops_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-A3-ops_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-A3-ops_8.5.2_linux-x86_64.run && \ + + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + wget -v --no-check-certificate https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%208.5.2/Ascend-cann-nnal_8.5.2_linux-x86_64.run && \ + bash Ascend-cann-nnal_8.5.2_linux-x86_64.run --quiet --install && \ + rm -f Ascend-cann-nnal_8.5.2_linux-x86_64.run ; \ + fi && \ + + source /usr/local/Ascend/cann/set_env.sh && \ + rm -rf /tmp/* + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone https://github.com/vllm-project/vllm-ascend.git && \ + cd vllm-ascend && git checkout 54879467c41784a446aa5b486a391d9bfbf488fa && cd .. && \ + git clone https://github.com/huggingface/transformers.git && \ + cd transformers && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git + +# 避免vllm-ascend 安装失败 +ENV MAX_JOBS=1 +ENV MAKEFLAGS="-j1" +ENV CMAKE_BUILD_PARALLEL_LEVEL=1 + +# Install repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.2/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-8.5.2/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + + # Install MindSpeed + pip install -e MindSpeed && \ + # Install Megatron-LM + pip install -e Megatron-LM && \ + # Install Megatron-Bridge + pip install git+https://github.com/NVIDIA-NeMo/Megatron-Bridge.git@de93536e --no-deps --no-build-isolation && \ + + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install -v -e . --no-build-isolation && cd .. && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install torch & torch_npu & torchvision + pip install torch==2.9.0 torch_npu==2.9.0 torchvision==0.24.0 accelerate==1.13.0 mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + + +# Prepare and install verl (update frequently) +RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && git checkout 4045d67063052dcb800c918c107b8d5a87046006 && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2 new file mode 100644 index 0000000000000000000000000000000000000000..fd4a06cdfed3aa81832d692e202630134ffd0347 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2 @@ -0,0 +1,83 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.0.0-910b-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910b1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git && \ + + git clone --depth 1 https://github.com/huggingface/transformers.git && \ + cd transformers && git fetch --depth 1 origin cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + pip install torchvision==0.24.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install mbridge + pip install mbridge && \ + # Install others + pip install "nvidia-modelopt[torch]>=0.37.0" flash-linear-attention==0.5.0 qwen_vl_utils viztracer mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] \ No newline at end of file diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2_v0.8.0 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2_v0.8.0 new file mode 100644 index 0000000000000000000000000000000000000000..ca8960e0a062d31a340fc97abaae833433496c50 --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a2_v0.8.0 @@ -0,0 +1,88 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.0.0-910b-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910b1" + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git && \ + git clone --depth 1 https://github.com/NVIDIA-NeMo/Megatron-Bridge.git && \ + cd Megatron-Bridge && git fetch --depth 1 origin de93536e9028ecf1e4dc28608dc80f336dcdfe59 && git checkout de93536e9028ecf1e4dc28608dc80f336dcdfe59 && cd .. && \ + git clone --depth 1 https://github.com/huggingface/transformers.git && \ + cd transformers && git fetch --depth 1 origin cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + pip install torchvision==0.24.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install mbridge + pip install mbridge && \ + # Install others + pip install "nvidia-modelopt[torch]>=0.37.0" flash-linear-attention==0.5.0 qwen_vl_utils viztracer mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + + +# Prepare and install verl (verl is release/v0.8.0) +RUN git clone -b release/v0.8.0 https://github.com/verl-project/verl.git && cd verl && \ + git submodule update --init --recursive --remote && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# set PYTHONPATH +ENV PYTHONPATH=/Megatron-Bridge/src:$PYTHONPATH + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] \ No newline at end of file diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3 new file mode 100644 index 0000000000000000000000000000000000000000..aa2e7298bfc294119a5174fe737149fdd823a4ea --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3 @@ -0,0 +1,82 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.0.0-a3-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910_9392" + + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git && \ + git clone --depth 1 https://github.com/huggingface/transformers.git && \ + cd transformers && git fetch --depth 1 origin cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + pip install torchvision==0.24.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install mbridge + pip install mbridge && \ + # Install others + pip install "nvidia-modelopt[torch]>=0.37.0" flash-linear-attention==0.5.0 qwen_vl_utils viztracer mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + +# Prepare and install verl (update frequently) +RUN git clone --recursive https://github.com/verl-project/verl.git && \ + cd verl && pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3_v0.8.0 b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3_v0.8.0 new file mode 100644 index 0000000000000000000000000000000000000000..7da924c15a7d27f635756005e717d1c1804f3f2b --- /dev/null +++ b/verl_0720_main/verl/docker/ascend/Dockerfile.ascend_9.0.0_a3_v0.8.0 @@ -0,0 +1,88 @@ +# Pull base image +FROM swr.cn-south-1.myhuaweicloud.com/ascendhub/cann:9.0.0-a3-ubuntu22.04-py3.11 + +ARG SOC_VERSION="ascend910_9392" + + +# Prepare required system dependencies +RUN apt-get update -y && \ + apt-get install -y --no-install-recommends gcc g++ cmake libnuma-dev wget git curl jq vim build-essential && \ + apt-get clean && \ + rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* && \ + pip install --upgrade pip packaging setuptools==80.10.2 && \ + pip cache purge + +# Prepare repositories with low update frequency +RUN ARCH=$(uname -m) && \ + # Set extra pip index for x86_64 platform + echo "[LOG INFO] Detected architecture: $ARCH" && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/"; \ + fi && \ + # Clone libs + git clone --depth 1 --branch v0.18.0 https://github.com/vllm-project/vllm.git && \ + git clone -b releases/v0.18.0 https://github.com/vllm-project/vllm-ascend.git && \ + git clone https://gitcode.com/Ascend/MindSpeed.git && \ + cd MindSpeed && git checkout core_r0.16.0 && cd .. && \ + git clone --depth 1 --branch core_r0.16.0 https://github.com/NVIDIA/Megatron-LM.git && \ + git clone --depth 1 https://github.com/NVIDIA-NeMo/Megatron-Bridge.git && \ + cd Megatron-Bridge && git fetch --depth 1 origin de93536e9028ecf1e4dc28608dc80f336dcdfe59 && git checkout de93536e9028ecf1e4dc28608dc80f336dcdfe59 && cd .. && \ + git clone --depth 1 https://github.com/huggingface/transformers.git && \ + cd transformers && git fetch --depth 1 origin cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && git checkout cc7ab9be508ce6ed3637bba9e50367b29b742dc6 && cd .. + +# Install repositories with low update frequency +RUN export PIP_EXTRA_INDEX_URL=https://triton-ascend.osinfra.cn/pypi/simple/ && \ + export PIP_TRUSTED_HOST=triton-ascend.osinfra.cn && \ + ARCH=$(uname -m) && \ + # Export and source env + if [ "$ARCH" = "aarch64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/aarch64-linux/devlib/linux/aarch64:$LD_LIBRARY_PATH; \ + elif [ "$ARCH" = "x86_64" ]; then \ + export LD_LIBRARY_PATH=/usr/local/Ascend/cann-9.0.0/x86_64-linux/devlib/linux/x86_64/:$LD_LIBRARY_PATH; \ + fi && \ + source /usr/local/Ascend/ascend-toolkit/set_env.sh && \ + source /usr/local/Ascend/nnal/atb/set_env.sh && \ + # Install vllm + cd vllm && pip install -r requirements/build.txt && \ + VLLM_TARGET_DEVICE=empty pip install -v -e. && cd .. && \ + if [ "$ARCH" = "x86_64" ]; then \ + pip install torch==2.9.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + pip install torchvision==0.24.0+cpu --index-url https://download.pytorch.org/whl/cpu/; \ + fi && \ + # Install vllm-ascend + cd vllm-ascend && pip install -r requirements.txt && \ + export COMPILE_CUSTOM_KERNELS=1 && pip install --no-build-isolation --no-deps -v -e . && cd .. && \ + # Install MindSpeed & Megatron + pip install -e MindSpeed && \ + pip install -e Megatron-LM && \ + # Install transformers + cd transformers && pip install -e . && cd .. && \ + # Install mbridge + pip install mbridge && \ + # Install others + pip install "nvidia-modelopt[torch]>=0.37.0" flash-linear-attention==0.5.0 qwen_vl_utils viztracer mathruler && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# get mbridge path and cuda to npu for deepseek model +RUN MBRIDGE_PATH=$(pip show mbridge | grep Location | awk '{print $2}') && \ + TARGET_FILE="${MBRIDGE_PATH}/mbridge/models/ext/deepseek_v3/dequant_fp8_safetensor_io.py" && \ + sed -i '34s/cuda/npu/;51s/cuda/npu/' "$TARGET_FILE" + +# Prepare and install verl (verl is release/v0.8.0) +RUN git clone -b release/v0.8.0 https://github.com/verl-project/verl.git && cd verl && \ + git submodule update --init --recursive --remote && \ + pip install -r requirements-npu.txt && pip install -v -e . && cd .. && \ + # Clear extra files + rm -rf /tmp/* /var/tmp/* && \ + pip cache purge + +# set PYTHONPATH +ENV PYTHONPATH=/Megatron-Bridge/src:$PYTHONPATH + +# Show install results +RUN pip list + +# Setting Default Commands +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/aws/Dockerfile.extention.awsefa b/verl_0720_main/verl/docker/aws/Dockerfile.extention.awsefa new file mode 100644 index 0000000000000000000000000000000000000000..e221e9f1e60bd36869733773e6b26e46924fc2d6 --- /dev/null +++ b/verl_0720_main/verl/docker/aws/Dockerfile.extention.awsefa @@ -0,0 +1,55 @@ +# Base Image support aws EFA +# Build Image with frameworks based on this +FROM verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2 + +# For aws instances with EFA net interface (Sagemaker AI Pod) +# install EFA driver: +######## AWS EFA ############ +ENV NCCL_VERSION=2.25.1-1 +ENV DEBIAN_FRONTEND=noninteractive +ENV EFA_INSTALLER_VERSION=1.40.0 +ENV AWS_OFI_NCCL_VERSION=1.14.2 +ENV FI_EFA_SET_CUDA_SYNC_MEMOPS=0 +ENV FI_PROVIDER=efa + +RUN apt update && apt install -y linux-image-generic libhwloc-dev + +RUN cd /tmp && \ + curl -O https://efa-installer.amazonaws.com/aws-efa-installer-${EFA_INSTALLER_VERSION}.tar.gz && \ + tar -xf aws-efa-installer-${EFA_INSTALLER_VERSION}.tar.gz && \ + cd aws-efa-installer && \ + ./efa_installer.sh -y -g --skip-kmod --skip-limit-conf --no-verify && \ + ldconfig && \ + rm -rf /tmp/aws-efa-installer /var/lib/apt/lists/* + +# NCCL EFA Plugin +RUN cd /tmp && \ + curl -LO https://github.com/aws/aws-ofi-nccl/archive/refs/tags/v${AWS_OFI_NCCL_VERSION}.tar.gz && \ + tar -xzf /tmp/v${AWS_OFI_NCCL_VERSION}.tar.gz && \ + rm /tmp/v${AWS_OFI_NCCL_VERSION}.tar.gz && \ + mv aws-ofi-nccl-${AWS_OFI_NCCL_VERSION} aws-ofi-nccl && \ + cd /tmp/aws-ofi-nccl && \ + ./autogen.sh && \ + ./configure --prefix=/opt/amazon/efa \ + --with-libfabric=/opt/amazon/efa \ + --with-cuda=/usr/local/cuda \ + --enable-platform-aws \ + --with-mpi=/opt/amazon/openmpi && \ + make -j$(nproc) install && \ + rm -rf /tmp/aws-ofi/nccl + +# NCCL +RUN echo "/usr/local/lib" >> /etc/ld.so.conf.d/local.conf && \ + echo "/opt/amazon/openmpi/lib" >> /etc/ld.so.conf.d/efa.conf && \ + ldconfig + +ENV OMPI_MCA_pml=^cm,ucx \ + OMPI_MCA_btl=tcp,self \ + OMPI_MCA_btl_tcp_if_exclude=lo,docker0,veth_def_agent \ + OPAL_PREFIX=/opt/amazon/openmpi \ + NCCL_SOCKET_IFNAME=^docker,lo,veth_def_agent \ + FI_EFA_USE_HUGE_PAGE=0 + +# docker build -t verl:awsefa --label "commit=$(git rev-parse --short HEAD)" . +# on aws: +# docker run --ipc=host --privileged --name verldev --gpus all --network=host --shm-size=1800gb -itd verl:awsefa diff --git a/verl_0720_main/verl/docker/aws/Dockerfile.ngc.vllm0.8.sagemaker b/verl_0720_main/verl/docker/aws/Dockerfile.ngc.vllm0.8.sagemaker new file mode 100644 index 0000000000000000000000000000000000000000..2746fa9f8e115c0c6ad43627b2d4200df75d597b --- /dev/null +++ b/verl_0720_main/verl/docker/aws/Dockerfile.ngc.vllm0.8.sagemaker @@ -0,0 +1,46 @@ +# Using a pre-built image from AWS DLC which contains the current version of python (3.10) and supported cuda version (12.1) +FROM 763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:2.1.0-transformers4.36.0-gpu-py310-cu121-ubuntu20.04 + +# uninstall nv-pytorch fork +RUN pip3 uninstall -y pytorch-quantization \ + pytorch-triton torch torch-tensorrt torchvision \ + xgboost transformer_engine flash_attn apex megatron-core + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install tini +RUN apt-get update && \ + apt-get install -y tini && \ + apt-get clean + +# Install torch-2.6.0 + vllm-0.8.2 +RUN pip install --no-cache-dir vllm==0.8.2 torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 tensordict torchdata==0.11.0 \ + transformers>=4.49.0 accelerate datasets peft hf-transfer \ + ray[default] codetiming hydra-core pandas pyarrow>=15.0.0 pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler \ + pytest pre-commit py-spy pyext ruff tensorboard + +# Install flash_attn-2.7.4.post1 +RUN pip uninstall -y transformer-engine flash-attn && \ + pip install flash-attn==2.7.4.post1 --no-build-isolation + +# Fix cv2 +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --no-cache-dir nvidia-ml-py>=12.560.30 opencv-python-headless==4.8.0.74 fastapi==0.115.6 && \ + pip install --no-cache-dir --upgrade optree>=0.13.0 + +# Install verl +RUN pip install --no-cache-dir verl[vllm] -U + +# Reset pip config +RUN pip config unset global.index-url && \ + pip config unset global.extra-index-url diff --git a/verl_0720_main/verl/docker/rocm/Apptainerfile.rocm b/verl_0720_main/verl/docker/rocm/Apptainerfile.rocm new file mode 100644 index 0000000000000000000000000000000000000000..a040e89b237d6a5a0209f5bae809d862e871e57c --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/Apptainerfile.rocm @@ -0,0 +1,57 @@ +Bootstrap: docker + +# Support - Traing: fsdp; Inference: vllm +# FROM: rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 +# Support - Traing: fsdp; Inference: vllm, sglang +FROM lmsysorg/sglang:v0.4.5-rocm630 + +%environment + export PYTORCH_ROCM_ARCH="gfx90a;gfx942" + + export HIPCC_COMPILE_FLAGS_APPEND="--amdgpu-target=gfx90a;gfx942 -D__HIP_PLATFORM_AMD__" + export CFLAGS="-D__HIP_PLATFORM_AMD__" + export CXXFLAGS="-D__HIP_PLATFORM_AMD__" + +%post + # Create source directory + mkdir -p /opt/src + + # Uninstall and reinstall vllm + pip uninstall -y vllm + cd /opt/src + git clone -b v0.6.3 https://github.com/vllm-project/vllm.git + cd vllm + MAX_JOBS=$(nproc) python3 setup.py install + cd /opt + rm -rf /opt/src/vllm + + # Install dependencies + pip install "tensordict<0.6" --no-deps + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + "ray[data,train,tune,serve]" \ + torchdata \ + transformers \ + wandb \ + orjson \ + pybind11 + + # Clone and install verl from GitHub + cd /opt + git clone https://github.com/verl-project/verl.git + cd verl + # Uncomment to use a specific version + # git checkout v0.3.0.post0 + pip install -e . --no-deps + + # Install torch_memory_saver + pip install git+https://github.com/ExtremeViscent/torch_memory_saver.git --no-deps \ No newline at end of file diff --git a/verl_0720_main/verl/docker/rocm/Dockerfile.rocm b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm new file mode 100644 index 0000000000000000000000000000000000000000..582e3627e102e8b61f03c8c0f081e102ca3bcc95 --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm @@ -0,0 +1,186 @@ +# Global build args shared across stages. +# Declared before the first FROM so the default value lives in ONE place. +ARG GPU_ARCH="gfx942;gfx950" + +# Install basic packages from OS distro +FROM ubuntu:22.04 AS base + +ENV DEBIAN_FRONTEND=noninteractive +ARG PYTHON_VERSION=3.12 + +RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \ + --mount=target=/var/cache/apt,type=cache,sharing=locked \ + apt update && \ + apt install -y git software-properties-common curl rsync dialog gfortran wget sqlite3 ccache vim && \ + if ! python3 --version | grep -q ${PYTHON_VERSION} ; then \ + add-apt-repository -y ppa:deadsnakes/ppa && apt update && \ + apt-get install -y python${PYTHON_VERSION} python${PYTHON_VERSION}-dev python${PYTHON_VERSION}-venv \ + python${PYTHON_VERSION}-lib2to3 python-is-python3 ; fi + +RUN update-alternatives --install /usr/bin/python3 python3 /usr/bin/python${PYTHON_VERSION} 1 && \ + update-alternatives --set python3 /usr/bin/python${PYTHON_VERSION} && \ + ln -sf /usr/bin/python${PYTHON_VERSION}-config /usr/bin/python3-config && \ + curl -sS https://bootstrap.pypa.io/get-pip.py | python${PYTHON_VERSION} + +RUN wget -nv -O /tmp/cmake-3.26.4-linux-x86_64.tar.gz https://cmake.org/files/v3.26/cmake-3.26.4-linux-x86_64.tar.gz && \ + tar zfx /tmp/cmake-3.26.4-linux-x86_64.tar.gz -C /opt/ && \ + mv /opt/cmake-3.26.4-linux-x86_64 /opt/cmake-3.26.4 && \ + rm -f /tmp/cmake-3.26.4-linux-x86_64.tar.gz + +ENV PATH=/opt/cmake-3.26.4/bin:$PATH + +# ccache speeds up repeated source builds (FA / TE / vLLM / aiter). +# The actual cache lives in a BuildKit cache mount, so it persists across +# builds without being baked into the image. +ENV CCACHE_DIR=/root/.cache/ccache +ENV CCACHE_MAXSIZE=50G +ENV CMAKE_C_COMPILER_LAUNCHER=ccache +ENV CMAKE_CXX_COMPILER_LAUNCHER=ccache +ENV CMAKE_HIP_COMPILER_LAUNCHER=ccache + +# Install ROCm deb packages +FROM base AS rocm_deb + +ARG ROCM_VERSION=7.0.2 +ARG AMDGPU_VERSION=7.0.2 +ARG GFX_ARCH=gfx942|gfx950 + +RUN --mount=target=/var/lib/apt/lists,type=cache,sharing=locked \ + --mount=target=/var/cache/apt,type=cache,sharing=locked \ + curl -sL https://repo.radeon.com/rocm/rocm.gpg.key | apt-key add - \ + && printf "deb [arch=amd64] https://repo.radeon.com/rocm/apt/$ROCM_VERSION/ jammy main\n" | tee /etc/apt/sources.list.d/rocm.list \ + && printf "deb [arch=amd64] https://repo.radeon.com/amdgpu/$AMDGPU_VERSION/ubuntu jammy main\n" | tee /etc/apt/sources.list.d/amdgpu.list \ + && printf "Package: *\nPin: release o=repo.radeon.com\nPin-Priority: 600\n" | tee /etc/apt/preferences.d/rocm-pin-600 \ + && apt-get update \ + && DEBIAN_FRONTEND=noninteractive apt-get install -y rocm && \ + find /opt/rocm/lib -type f -name '*gfx*' | grep -Ev "${GFX_ARCH}" | xargs rm -f && \ + find /opt/rocm/lib/hipblaslt/library -type f -name '*gfx*' | grep -Ev "${GFX_ARCH}" | xargs rm -f && \ + find /opt/rocm/lib/rocblas/library -type f -name '*gfx*' | grep -Ev "${GFX_ARCH}" | xargs rm -f && \ + find /opt/rocm/share/miopen/db -type f -name '*gfx*' | grep -Ev "${GFX_ARCH}" | xargs rm -f + +ENV ROCM_HOME=/opt/rocm +ENV CPLUS_INCLUDE_PATH=/opt/rocm/include +ENV LD_LIBRARY_PATH=/opt/rocm/lib +ENV PATH=/opt/rocm/bin:/opt/rocm/llvm/bin:$PATH + +# Install pytorch packages +FROM rocm_deb AS rocm_torch + +RUN --mount=type=cache,target=/root/.cache/pip \ + pip install --upgrade pip "setuptools<80" wheel numpy einops packaging psutil ninja && \ + pip install /opt/rocm/share/amd_smi + +# Install the prebuilt ROCm wheels directly via pip. +# NOTE: these URLs are pinned to a specific ROCm release (rocm-rel-7.0.2), +# Python ABI (cp312) and nightly git hashes. Overriding ROCM_VERSION or +# PYTHON_VERSION alone will NOT change these wheels -- you must also update +# the URLs below to matching builds, otherwise install will 404 or mismatch. +RUN --mount=type=cache,target=/root/.cache/pip \ + pip3 install \ + https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/torch-2.9.1.dev20251204+rocm7.0.2.lw.git351ff442-cp312-cp312-linux_x86_64.whl \ + https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/apex-1.9.0a0+rocm7.0.2.git07c3ee53-cp312-cp312-linux_x86_64.whl \ + https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/torchaudio-2.9.0+rocm7.0.2.gite3c6ee2b-cp312-cp312-linux_x86_64.whl \ + https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/torchvision-0.24.0+rocm7.0.2.gitb919bd0c-cp312-cp312-linux_x86_64.whl \ + https://repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/triton-3.5.1+rocm7.0.2.gita272dfa8-cp312-cp312-linux_x86_64.whl + +ARG GPU_ARCH +ENV PYTORCH_ROCM_ARCH=${GPU_ARCH} + +WORKDIR /root + +# Build Flash Attention wheel +FROM rocm_torch AS fa_build + +ARG FA_REPO="https://github.com/ROCm/flash-attention" +ARG FA_TAG="83f9e450cd10e20701fb109db9c7703d376f282b" + +RUN git clone ${FA_REPO} \ + && cd flash-attention \ + && git checkout ${FA_TAG} \ + && git submodule init \ + && git submodule update + +ARG GPU_ARCH +ARG MAX_JOBS= +RUN --mount=type=cache,target=/root/.cache/ccache \ + cd flash-attention \ + && GPU_ARCHS=${GPU_ARCH} BUILD_TARGET=rocm MAX_JOBS=${MAX_JOBS:-$(nproc)} python3 setup.py bdist_wheel \ + && mkdir /install && cp dist/*.whl /install \ + && ccache -s + +# Install Flash Attention +FROM fa_build AS install_fa + +RUN --mount=type=bind,from=fa_build,source=/install,target=/tmp/install \ + --mount=type=cache,target=/root/.cache/pip \ + pip install /tmp/install/*.whl + +# Install TE +FROM install_fa AS te + +ARG GPU_ARCH +ENV NVTE_USE_HIPBLASLT=1 +ENV NVTE_USE_ROCM=1 +ENV NVTE_FRAMEWORK=pytorch +ENV NVTE_ROCM_ARCH=${GPU_ARCH} +ENV NVTE_USE_CAST_TRANSPOSE_TRITON=0 +ENV NVTE_CK_USES_BWD_V3=1 +ENV NVTE_CK_V3_BF16_CVT=2 + +ARG TE_TAG="386bd316" +ARG MAX_JOBS= +RUN --mount=type=cache,target=/root/.cache/ccache \ + pip install pybind11 && git clone https://github.com/ROCm/TransformerEngine.git && \ + cd TransformerEngine && git checkout ${TE_TAG} && git submodule update --init --recursive && \ + GPU_ARCHS=${GPU_ARCH} MAX_JOBS=${MAX_JOBS:-$(nproc)} python3 setup.py install && \ + ccache -s && \ + cd .. && rm -rf TransformerEngine +# TE patch +RUN FILE=$(find /usr/local/lib/python3.12/dist-packages/ -path "*transformer_engine*/transformer_engine/pytorch/attention/dot_product_attention/utils.py") && \ + sed -i '121s/2\.8\.3/2.8.4/' "$FILE" + +# Install vllm +FROM te AS install_vllm + +ARG VLLM_TAG="1ff9d3353" +ARG MAX_JOBS= +RUN --mount=type=cache,target=/root/.cache/ccache \ + pip install setuptools_scm && \ + mkdir /workspace && cd /workspace && \ + ln -sf /opt/rocm/lib/libamdhip64.so /usr/lib/libamdhip64.so && \ + git clone https://github.com/vllm-project/vllm && \ + cd vllm && git checkout ${VLLM_TAG} && pip install -r requirements/rocm.txt && \ + MAX_JOBS=${MAX_JOBS:-$(nproc)} python3 setup.py develop --no-deps && \ + ccache -s +RUN --mount=type=cache,target=/root/.cache/pip \ + pip uninstall tilelang -y && pip install xgrammar==0.1.32 + +FROM install_vllm AS install_verl +RUN --mount=type=cache,target=/root/.cache/pip \ + pip install cupy-rocm-7-0 + +ENV MIOPEN_DEBUG_CONV_DIRECT=0 +ARG AITER_TAG="45c428e54" +ARG MAX_JOBS= +RUN --mount=type=cache,target=/root/.cache/ccache \ + cd /workspace && git clone --recursive https://github.com/ROCm/aiter.git && \ + cd aiter && git checkout ${AITER_TAG} && \ + MAX_JOBS=${MAX_JOBS:-$(nproc)} python3 setup.py develop && \ + ccache -s + +# For qwen3.5 +RUN --mount=type=cache,target=/root/.cache/pip \ + pip install -U git+https://github.com/ISEEKYAN/mbridge.git@v0.15.1 && \ + pip install --no-deps megatron-bridge==0.5.0 && \ + pip install transformers --upgrade && \ + pip install megatron-core==0.18.0 && \ + pip install mathruler && \ + pip install qwen_vl_utils && \ + pip install flash-linear-attention + +RUN --mount=type=cache,target=/root/.cache/pip \ + cd /workspace && git clone https://github.com/verl-project/verl.git && \ + cd verl && pip install . + +WORKDIR /workspace/verl +CMD ["/bin/bash"] diff --git a/verl_0720_main/verl/docker/rocm/Dockerfile.rocm6 b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm6 new file mode 100644 index 0000000000000000000000000000000000000000..b1345db540503dee1cbc54c678c41174d9b491c2 --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm6 @@ -0,0 +1,322 @@ +# FROM "compute-artifactory.amd.com:5000/rocm-plus-docker/framework/compute-rocm-rel-6.4:94_ubuntu22.04_py3.10_pytorch_release-2.7_575e247" +# FROM "rlfoundation.azurecr.io/rocm6.3.4:vllm-0.8.5-numa-patch-ubuntu-22.04" +FROM "rlsys/rocm-6.3.4-patch:rocm6.3.4-numa-patch_ubuntu-22.04" + +SHELL ["/bin/bash", "-ceuxo", "pipefail"] + +ENV MAX_JOBS=512 + +ENV PATH="/usr/local/python3.12/bin:$PATH" +RUN ln -sf /usr/bin/python3.12 /usr/bin/python && \ + ln -sf /usr/bin/pip3.12 /usr/bin/pip + +############################################ +############################################ +RUN apt-get update +RUN apt-get install -y pkg-config liblzma-dev +############################################ +############################################ + + +########################################### +##########Install TransformerEngine######## +########################################### +WORKDIR /workspace/ +# transformer-engine install +# https://github.com/ROCm/TransformerEngine + +RUN rm -rf TransformerEngine +RUN git clone --recursive https://github.com/ROCm/TransformerEngine.git +WORKDIR /workspace/TransformerEngine +RUN git checkout 236178e5 +# git checkout bb061ade +# git checkout 864405c + +ENV NVTE_FRAMEWORK=pytorch +ENV NVTE_ROCM_ARCH=gfx942 +ENV NVTE_USE_HIPBLASLT=1 +ENV NVTE_USE_ROCM=1 + +# export CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr:${CMAKE_PREFIX_PATH:-}" +ENV CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr" + + +# ENV NVTE_BUILD_MAX_JOBS=$(MAX_JOBS) + +RUN MAX_JOBS=$(MAX_JOBS) pip install . -vvv + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + + +#################################################################################### +################Install vllm - sglang require vllm 0.6.7 dependency################# +#################################################################################### +#### Require vllm 0.6.7 - checkout 113274a0 +WORKDIR /workspace/ +RUN rm -rf vllm +RUN pip uninstall -y vllm +# Refer to here (down-grade vllm to 0.6.3): https://docs.vllm.ai/en/v0.6.3/getting_started/amd-installation.html +RUN git clone https://github.com/ROCm/vllm.git +# git clone https://github.com/vllm-project/vllm.git +WORKDIR /workspace/vllm +RUN git checkout 113274a0 +ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" +#ENV MAX_JOBS=512 +ENV MAX_JOBS=${MAX_JOBS} +RUN pip install "boto3>=1.26.0" +RUN pip install setuptools_scm +# will add src into py. You can delete the repo +RUN python3 setup.py install +WORKDIR /workspace/ +#################################################################################### +#################################################################################### +#################################################################################### + + + +########################################### +############For hack docker################ +########################################### +RUN pip install setuptools==75.8.0 +########################################### +########################################### +########################################### + + + +########################################### +############build sgalng################### +########################################### +# Set environment variables +ENV BASE_DIR=/sgl-workspace +ENV BUILD_TYPE=all +ENV SGL_REPO=https://github.com/sgl-project/sglang +ENV SGL_BRANCH=v0.4.6.post5 +ENV TRITON_REPO=https://github.com/ROCm/triton.git +ENV TRITON_COMMIT=improve_fa_decode_3.0.0 +ENV AITER_REPO=https://github.com/ROCm/aiter.git +ENV AITER_COMMIT=v0.1.2 +# v0.1.2 version - commit id: 9d11f47 +# ENV AITER_COMMIT=9d11f47 + +ENV HIP_FORCE_DEV_KERNARG=1 +ENV HSA_NO_SCRATCH_RECLAIM=1 +ENV SGLANG_SET_CPU_AFFINITY=1 +ENV SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1 +ENV NCCL_MIN_NCHANNELS=112 +ENV MOE_PADDING=1 +ENV VLLM_FP8_PADDING=1 +ENV VLLM_FP8_ACT_PADDING=1 +ENV VLLM_FP8_WEIGHT_PADDING=1 +ENV VLLM_FP8_REDUCE_CONV=1 +ENV TORCHINDUCTOR_MAX_AUTOTUNE=1 +ENV TORCHINDUCTOR_MAX_AUTOTUNE_POINTWISE=1 +ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" +ENV AMDGPU_TARGETS=gfx942 +ENV ROCM_ARCH=gfx942 +ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + +# Switch to working directory +WORKDIR /sgl-workspace + +# Clean and create directory +RUN rm -rf /sgl-workspace && mkdir -p /sgl-workspace + +# Clone and build sglang +RUN git clone ${SGL_REPO} \ + && cd sglang \ + && git checkout ${SGL_BRANCH} || echo "Using default branch" \ + && cd sgl-kernel \ + && rm -f pyproject.toml \ + && mv pyproject_rocm.toml pyproject.toml \ + && python setup_rocm.py install \ + && cd .. \ + && if [ "$BUILD_TYPE" = "srt" ]; then \ + python -m pip --no-cache-dir install -e "python[srt_hip]"; \ + else \ + python -m pip --no-cache-dir install -e "python[all_hip]"; \ + fi \ + && cd /sgl-workspace \ + && cp -r /sgl-workspace/sglang /sglang \ + && python -m pip cache purge + +# Install common Python packages +RUN pip install IPython orjson python-multipart torchao pybind11 + +# Rebuild Triton +RUN pip uninstall -y triton || true \ + && git clone ${TRITON_REPO} \ + && cd triton \ + && git checkout ${TRITON_COMMIT} \ + && cd python \ + && python3 setup.py install \ + && cd /sgl-workspace + + +# ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942 --amdgpu-lower-module-lds-strategy=1" +# ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" + +# Build aiter +#version: Commit 9d11f47 + # && git checkout ${AITER_COMMIT} \ +RUN pip uninstall -y aiter || true +RUN git clone ${AITER_REPO} \ + && cd aiter \ + && git checkout ${AITER_COMMIT} \ + && git submodule sync \ + && git submodule update --init --recursive \ + && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py install \ + && cd /sgl-workspace + # && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py develop \ + # && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py develop \ + +# Copy MI300X config +RUN find /sgl-workspace/sglang/python/sglang/srt/layers/quantization/configs/ \ + /sgl-workspace/sglang/python/sglang/srt/layers/moe/fused_moe_triton/configs/ \ + -type f -name '*MI300X*' | \ + xargs -I {} sh -c 'vf_config=$(echo "$1" | sed "s/MI300X/MI300X_VF/"); cp "$1" "$vf_config"' -- {} + +# Environment setup complete. +RUN echo "Environment setup complete." + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + + + +########################################### +###############vllm v0.8.5################# +########################################### +# ENV GITHUB_USERNAME=yushengsu-thu +# ENV GITHUB_MAIL=yushengsu@gmail.com + +# RUN git config --global user.name "${GITHUB_USERNAME}" \ +# && git config --global user.email "${GITHUB_MAIL}" + +WORKDIR /workspace/ + +ENV VLLM_TARGET_DEVICE=rocm +ENV ROCM_PATH=/opt/rocm +ENV SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev + +# Find the repo path in: DockerFile/Dockerfile.rocm_yang +# RUN git clone https://github.com/RLFoundation/vllm-patch.git +RUN pip uninstall -y vllm || true +RUN rm -rf vllm-patch +RUN git clone https://github.com/RLFoundation/vllm-patch.git \ + && cd vllm-patch \ + && git checkout v0.8.5-sleep-numa \ + && rm -rf build/ dist/ *.egg-info \ + && ln -sf /opt/rocm/lib/libamdhip64.so /usr/lib/libamdhip64.so \ + && SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py install + # RUN SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py develop + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + +######################################### +#### Install megatron-core############### +######################################### +RUN pip uninstall -y megatron-core && \ + git clone https://github.com/yushengsu-thu/Megatron-LM-amd_version.git && \ + cd Megatron-LM-amd_version && \ + pip install -vvv -e . && \ + cd /workspace/ +######################################### +######################################### +######################################### + + + + +####################################### +################apex################### +####################################### +WORKDIR /workspace/ +RUN pip uninstall -y apex && \ + git clone https://github.com/ROCm/apex.git && \ + cd apex && \ + python setup.py install && \ + cd /workspace/ +####################################### +####################################### +####################################### + + + + +################################################################################ +###########################Add torch_memory_saver############################### +################################################################################ +# Set environment variables +ENV HIPCC_COMPILE_FLAGS_APPEND="--amdgpu-target=gfx90a;gfx942 -D__HIP_PLATFORM_AMD__" +ENV CFLAGS="-D__HIP_PLATFORM_AMD__" +ENV CXXFLAGS="-D__HIP_PLATFORM_AMD__" +RUN pip install "git+https://github.com/YangWang92/torch_memory_saver_numa.git@numa" +################################################################################ +################################################################################ +################################################################################ + + + +######################################## +######Install ray####################### +######################################## +# need to add this patch: https://github.com/ray-project/ray/pull/53531/files +RUN pip uninstall ray -y +RUN pip install "ray[data,train,tune,serve]>=2.47.0" +######################################## +######################################## +######################################## + + + +########################################## +#######Install other dependencies######### +########################################## +RUN pip install "tensordict==0.6.2" --no-deps && \ + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + torchdata \ + wandb \ + orjson \ + pybind11 + +WORKDIR /workspace/ +RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && \ + pip install -e . +########################################## +########################################## +########################################## + + + +WORKDIR /workspace/ + +CMD ["/usr/bin/bash"] diff --git a/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.3.0.post1 b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.3.0.post1 new file mode 100644 index 0000000000000000000000000000000000000000..caef6b4f2e8e6317e2b18466927e05d17e94d750 --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.3.0.post1 @@ -0,0 +1,58 @@ +# Build the docker in the repo dir: +# docker build -f docker/Dockerfile.rocm -t verl-rocm:03.04.2015 . +# docker images # you can find your built docker + + +# Support - Traing: fsdp; Inference: vllm +# FROM rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 +# Support - Traing: fsdp; Inference: vllm, sglang +FROM lmsysorg/sglang:v0.4.6.post5-rocm630 + +# Set working directory +# WORKDIR $PWD/app + +# Set environment variables +ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + +ENV HIPCC_COMPILE_FLAGS_APPEND="--amdgpu-target=gfx90a;gfx942 -D__HIP_PLATFORM_AMD__" +ENV CFLAGS="-D__HIP_PLATFORM_AMD__" +ENV CXXFLAGS="-D__HIP_PLATFORM_AMD__" + +# Install vllm +RUN pip uninstall -y vllm && \ + rm -rf vllm && \ + git clone -b v0.6.3 https://github.com/vllm-project/vllm.git && \ + cd vllm && \ + MAX_JOBS=$(nproc) python3 setup.py install && \ + cd .. && \ + rm -rf vllm + +# Copy the entire project directory +COPY . . + +# Install dependencies +RUN pip install "tensordict==0.6.2" --no-deps && \ + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + "ray[data,train,tune,serve]<2.45.0" \ + torchdata \ + transformers \ + wandb \ + orjson \ + pybind11 + +RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && \ + pip install -e . + +# Install torch_memory_saver +RUN pip install git+https://github.com/ExtremeViscent/torch_memory_saver.git --no-deps diff --git a/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.4.1 b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.4.1 new file mode 100644 index 0000000000000000000000000000000000000000..afcab9686b248660d0a0721abec7f9a1ef8ebc7a --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/Dockerfile.rocm_verl-0.4.1 @@ -0,0 +1,323 @@ +# FROM "compute-artifactory.amd.com:5000/rocm-plus-docker/framework/compute-rocm-rel-6.4:94_ubuntu22.04_py3.10_pytorch_release-2.7_575e247" +# FROM "rlfoundation.azurecr.io/rocm6.3.4:vllm-0.8.5-numa-patch-ubuntu-22.04" +FROM "rlsys/rocm-6.3.4-patch:rocm6.3.4-numa-patch_ubuntu-22.04" + +SHELL ["/bin/bash", "-ceuxo", "pipefail"] + +ENV MAX_JOBS=512 + +ENV PATH="/usr/local/python3.12/bin:$PATH" +RUN ln -sf /usr/bin/python3.12 /usr/bin/python && \ + ln -sf /usr/bin/pip3.12 /usr/bin/pip + +############################################ +############################################ +RUN apt-get update +RUN apt-get install -y pkg-config liblzma-dev +############################################ +############################################ + + +########################################### +##########Install TransformerEngine######## +########################################### +WORKDIR /workspace/ +# transformer-engine install +# https://github.com/ROCm/TransformerEngine + +RUN rm -rf TransformerEngine +RUN git clone --recursive https://github.com/ROCm/TransformerEngine.git +WORKDIR /workspace/TransformerEngine +RUN git checkout 236178e5 +# git checkout bb061ade +# git checkout 864405c + +ENV NVTE_FRAMEWORK=pytorch +ENV NVTE_ROCM_ARCH=gfx942 +ENV NVTE_USE_HIPBLASLT=1 +ENV NVTE_USE_ROCM=1 + +# export CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr:${CMAKE_PREFIX_PATH:-}" +ENV CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr" + + +# ENV NVTE_BUILD_MAX_JOBS=$(MAX_JOBS) + +RUN MAX_JOBS=$(MAX_JOBS) pip install . -vvv + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + + +#################################################################################### +################Install vllm - sglang require vllm 0.6.7 dependency################# +#################################################################################### +#### Require vllm 0.6.7 - checkout 113274a0 +WORKDIR /workspace/ +RUN rm -rf vllm +RUN pip uninstall -y vllm +# Refer to here (down-grade vllm to 0.6.3): https://docs.vllm.ai/en/v0.6.3/getting_started/amd-installation.html +RUN git clone https://github.com/ROCm/vllm.git +# git clone https://github.com/vllm-project/vllm.git +WORKDIR /workspace/vllm +RUN git checkout 113274a0 +ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" +#ENV MAX_JOBS=512 +ENV MAX_JOBS=${MAX_JOBS} +RUN pip install "boto3>=1.26.0" +RUN pip install setuptools_scm +# will add src into py. You can delete the repo +RUN python3 setup.py install +WORKDIR /workspace/ +#################################################################################### +#################################################################################### +#################################################################################### + + + +########################################### +############For hack docker################ +########################################### +RUN pip install setuptools==75.8.0 +########################################### +########################################### +########################################### + + + +########################################### +############build sgalng################### +########################################### +# Set environment variables +ENV BASE_DIR=/sgl-workspace +ENV BUILD_TYPE=all +ENV SGL_REPO=https://github.com/sgl-project/sglang +ENV SGL_BRANCH=v0.4.6.post5 +ENV TRITON_REPO=https://github.com/ROCm/triton.git +ENV TRITON_COMMIT=improve_fa_decode_3.0.0 +ENV AITER_REPO=https://github.com/ROCm/aiter.git +ENV AITER_COMMIT=v0.1.2 +# v0.1.2 version - commit id: 9d11f47 +# ENV AITER_COMMIT=9d11f47 + +ENV HIP_FORCE_DEV_KERNARG=1 +ENV HSA_NO_SCRATCH_RECLAIM=1 +ENV SGLANG_SET_CPU_AFFINITY=1 +ENV SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1 +ENV NCCL_MIN_NCHANNELS=112 +ENV MOE_PADDING=1 +ENV VLLM_FP8_PADDING=1 +ENV VLLM_FP8_ACT_PADDING=1 +ENV VLLM_FP8_WEIGHT_PADDING=1 +ENV VLLM_FP8_REDUCE_CONV=1 +ENV TORCHINDUCTOR_MAX_AUTOTUNE=1 +ENV TORCHINDUCTOR_MAX_AUTOTUNE_POINTWISE=1 +ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" +ENV AMDGPU_TARGETS=gfx942 +ENV ROCM_ARCH=gfx942 +ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + +# Switch to working directory +WORKDIR /sgl-workspace + +# Clean and create directory +RUN rm -rf /sgl-workspace && mkdir -p /sgl-workspace + +# Clone and build sglang +RUN git clone ${SGL_REPO} \ + && cd sglang \ + && git checkout ${SGL_BRANCH} || echo "Using default branch" \ + && cd sgl-kernel \ + && rm -f pyproject.toml \ + && mv pyproject_rocm.toml pyproject.toml \ + && python setup_rocm.py install \ + && cd .. \ + && if [ "$BUILD_TYPE" = "srt" ]; then \ + python -m pip --no-cache-dir install -e "python[srt_hip]"; \ + else \ + python -m pip --no-cache-dir install -e "python[all_hip]"; \ + fi \ + && cd /sgl-workspace \ + && cp -r /sgl-workspace/sglang /sglang \ + && python -m pip cache purge + +# Install common Python packages +RUN pip install IPython orjson python-multipart torchao pybind11 + +# Rebuild Triton +RUN pip uninstall -y triton || true \ + && git clone ${TRITON_REPO} \ + && cd triton \ + && git checkout ${TRITON_COMMIT} \ + && cd python \ + && python3 setup.py install \ + && cd /sgl-workspace + + +# ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942 --amdgpu-lower-module-lds-strategy=1" +# ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" + +# Build aiter +#version: Commit 9d11f47 + # && git checkout ${AITER_COMMIT} \ +RUN pip uninstall -y aiter || true +RUN git clone ${AITER_REPO} \ + && cd aiter \ + && git checkout ${AITER_COMMIT} \ + && git submodule sync \ + && git submodule update --init --recursive \ + && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py install \ + && cd /sgl-workspace + # && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py develop \ + # && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py develop \ + +# Copy MI300X config +RUN find /sgl-workspace/sglang/python/sglang/srt/layers/quantization/configs/ \ + /sgl-workspace/sglang/python/sglang/srt/layers/moe/fused_moe_triton/configs/ \ + -type f -name '*MI300X*' | \ + xargs -I {} sh -c 'vf_config=$(echo "$1" | sed "s/MI300X/MI300X_VF/"); cp "$1" "$vf_config"' -- {} + +# Environment setup complete. +RUN echo "Environment setup complete." + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + + + +########################################### +###############vllm v0.8.5################# +########################################### +# ENV GITHUB_USERNAME=yushengsu-thu +# ENV GITHUB_MAIL=yushengsu@gmail.com + +# RUN git config --global user.name "${GITHUB_USERNAME}" \ +# && git config --global user.email "${GITHUB_MAIL}" + +WORKDIR /workspace/ + +ENV VLLM_TARGET_DEVICE=rocm +ENV ROCM_PATH=/opt/rocm +ENV SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev + +# Find the repo path in: DockerFile/Dockerfile.rocm_yang +# RUN git clone https://github.com/RLFoundation/vllm-patch.git +RUN pip uninstall -y vllm || true +RUN rm -rf vllm-patch +RUN git clone https://github.com/RLFoundation/vllm-patch.git \ + && cd vllm-patch \ + && git checkout v0.8.5-sleep-numa \ + && rm -rf build/ dist/ *.egg-info \ + && ln -sf /opt/rocm/lib/libamdhip64.so /usr/lib/libamdhip64.so \ + && SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py install + # RUN SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py develop + +WORKDIR /workspace/ +########################################### +########################################### +########################################### + + + + +######################################### +#### Install megatron-core############### +######################################### +RUN pip uninstall -y megatron-core && \ + git clone https://github.com/yushengsu-thu/Megatron-LM-amd_version.git && \ + cd Megatron-LM-amd_version && \ + pip install -vvv -e . && \ + cd /workspace/ +######################################### +######################################### +######################################### + + + + +####################################### +################apex################### +####################################### +WORKDIR /workspace/ +RUN pip uninstall -y apex && \ + git clone https://github.com/ROCm/apex.git && \ + cd apex && \ + python setup.py install && \ + cd /workspace/ +####################################### +####################################### +####################################### + + + + +################################################################################ +###########################Add torch_memory_saver############################### +################################################################################ +# Set environment variables +ENV HIPCC_COMPILE_FLAGS_APPEND="--amdgpu-target=gfx90a;gfx942 -D__HIP_PLATFORM_AMD__" +ENV CFLAGS="-D__HIP_PLATFORM_AMD__" +ENV CXXFLAGS="-D__HIP_PLATFORM_AMD__" +RUN pip install "git+https://github.com/YangWang92/torch_memory_saver_numa.git@numa" +################################################################################ +################################################################################ +################################################################################ + + + +######################################## +######Install ray####################### +######################################## +# need to add this patch: https://github.com/ray-project/ray/pull/53531/files +RUN pip uninstall ray -y +RUN pip install "ray[data,train,tune,serve]>=2.47.0" +######################################## +######################################## +######################################## + + + +########################################## +#######Install other dependencies######### +########################################## +RUN pip install "tensordict==0.6.2" --no-deps && \ + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + torchdata \ + wandb \ + orjson \ + pybind11 + +WORKDIR /workspace/ +RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && \ + pip install -e . +########################################## +########################################## +########################################## + + + +WORKDIR /workspace/ + +CMD ["/usr/bin/bash"] +CMD ["/usr/bin/bash"] diff --git a/verl_0720_main/verl/docker/rocm/README.md b/verl_0720_main/verl/docker/rocm/README.md new file mode 100644 index 0000000000000000000000000000000000000000..031395657a13d3bf7140b2e85ba4534059b6a1f3 --- /dev/null +++ b/verl_0720_main/verl/docker/rocm/README.md @@ -0,0 +1,101 @@ +# ROCm (AMD GPU) Dockerfile of verl + +This directory provides the Docker recipe for running verl on **AMD GPUs with +the ROCm software stack**. The NVIDIA images described in +[`../README.md`](../README.md) do not work on AMD hardware, so use +[`Dockerfile.rocm`](Dockerfile.rocm) instead. + +For an end-to-end walkthrough (build, run, and example PPO/GRPO commands), see +the tutorial: [`docs/amd_tutorial/amd_build_dockerfile_page.rst`](../../docs/amd_tutorial/amd_build_dockerfile_page.rst). + +> The other `Dockerfile.rocm*` / `Apptainerfile.rocm` files in this directory are +> kept only as historical references for older verl releases (ROCm 6.x, pinned +> verl 0.3.x / 0.4.x). New work should target `Dockerfile.rocm`. + +## Supported Hardware + +The image targets the following GPU architectures (`GPU_ARCH`): + +- `gfx942` — MI300 series (MI300X / MI300A / MI325X) +- `gfx950` — MI350 series (MI350X / MI355X) + +Other architectures (e.g. `gfx90a` for MI200/MI250) can be built by overriding +`GPU_ARCH`, but are not validated here. + +## Key Versions + +| Component | Version | +| --------- | ------- | +| ROCm | 7.0.2 | +| Python | 3.12 | +| PyTorch | 2.9.1 (ROCm 7.0.2 wheel) | +| Triton | 3.5.1 | +| vLLM | source @ `1ff9d3353` | +| Flash Attention | ROCm fork (CK backend) @ `83f9e450` | +| TransformerEngine | ROCm fork @ `386bd316` | +| aiter | ROCm @ `45c428e54` | +| Megatron-core | 0.16.0 | +| Megatron-Bridge | 0.5.0 | + +## What the Image Contains + +Starting from a clean `ubuntu:22.04` base, `Dockerfile.rocm` installs: + +**Prebuilt (downloaded), not compiled:** +- ROCm 7.0.2 runtime + dev packages (via the `repo.radeon.com` apt repo) +- `torch`, `apex`, `torchaudio`, `torchvision`, `triton` — prebuilt ROCm wheels + from `repo.radeon.com/rocm/manylinux/rocm-rel-7.0.2/` + +**Built from source (pinned commits):** +- Flash Attention (ROCm fork, CK backend) +- TransformerEngine (ROCm fork) +- vLLM +- aiter + +**Also installed:** `cupy-rocm`, `mbridge`, `megatron-core`, `megatron-bridge`, +`transformers`, and the verl package itself. + +> Because Flash Attention / TransformerEngine / vLLM / aiter are compiled from +> source for the selected GPU architectures, the first build is slow (often +> 1-2+ hours). The image enables `ccache` (cached via a BuildKit cache mount) +> so that subsequent rebuilds are faster. + +## Building Locally + +The Dockerfile uses BuildKit cache mounts (`RUN --mount=...`), so **BuildKit is +required** (with the `buildx` plugin). If you see +`the --mount option requires BuildKit`, install `buildx` +(`sudo apt-get install -y docker-buildx`) and prefix the build with +`DOCKER_BUILDKIT=1`. + +```sh +DOCKER_BUILDKIT=1 docker build \ + -f docker/rocm/Dockerfile.rocm \ + -t verl-rocm:local . +``` + +### Useful build arguments + +| Build arg | Default | Purpose | +| --------- | ------- | ------- | +| `GPU_ARCH` | `gfx942;gfx950` | GPU architectures to compile kernels for. Set to a single arch (e.g. `gfx942`) to roughly halve Flash Attention build time. | +| `ROCM_VERSION` / `AMDGPU_VERSION` | `7.0.2` | ROCm / amdgpu apt repo version. Note: the prebuilt torch/triton/etc. wheel URLs in the Dockerfile are pinned to ROCm 7.0.2; changing this also requires updating those URLs. | +| `PYTHON_VERSION` | `3.12` | Python version. Note: the prebuilt wheel URLs are pinned to the `cp312` ABI; changing this also requires updating those URLs. | +| `MAX_JOBS` | `$(nproc)` | Parallel compile jobs. Lower it (e.g. `64`) if the vLLM build runs out of memory. | +| `FA_TAG` / `TE_TAG` / `VLLM_TAG` / `AITER_TAG` | pinned | Source commits for the from-source components. | + +Example — build only for MI300 with a memory-safe job count: + +```sh +DOCKER_BUILDKIT=1 docker build \ + -f docker/rocm/Dockerfile.rocm \ + --build-arg GPU_ARCH=gfx942 \ + --build-arg MAX_JOBS=64 \ + -t verl-rocm:mi300 . +``` + +## Release History + +- 2026/06/03: ROCm 7.0.2 stack — torch==2.9.1, triton==3.5.1, vLLM @`1ff9d3353`, + Flash Attention (CK) @`83f9e450`, TransformerEngine @`386bd316`, + aiter @`45c428e54`, megatron-core==0.16.0; targets gfx942 / gfx950. diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12 b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12 new file mode 100644 index 0000000000000000000000000000000000000000..68c3e1a043b3955f3773ca1665de5d8d1dd4fc17 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12 @@ -0,0 +1,41 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.6.post5 and torch-memory-saver +RUN pip install --resume-retries 999 "sglang[all]==0.4.6.post5" --no-cache-dir --find-links https://flashinfer.ai/whl/cu124/torch2.6/flashinfer-python && pip install torch-memory-saver --no-cache-dir + +# Some sglang operations in 0.4.6.post5 require vllm +# [Warning] vllm can have some packages not compatible with sglang, for example, flashinfer +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Fix for transformers 4.53.0 +RUN pip3 install --no-cache-dir "transformers[hf_xet]<4.52.0" + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12.deepep b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12.deepep new file mode 100644 index 0000000000000000000000000000000000000000..f08aeee9c7389f036e4b8ca032209495d9f5bffe --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.12.deepep @@ -0,0 +1,82 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.6.post5 and torch-memory-saver +RUN pip install --resume-retries 999 "sglang[all]==0.4.6.post5" --no-cache-dir --find-links https://flashinfer.ai/whl/cu124/torch2.6/flashinfer-python && pip install torch-memory-saver --no-cache-dir + +# Some sglang operations in 0.4.6.post5 require vllm +# [Warning] vllm can have some packages not compatible with sglang, for example, flashinfer +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Fix for transformers 4.53.0 +RUN pip3 install --no-cache-dir "transformers[hf_xet]<4.52.0" + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.13.preview b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.13.preview new file mode 100644 index 0000000000000000000000000000000000000000..0e0bdd43fec1eac3e4a85de5416e34fad4ac7c69 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.sglang.vllm.mcore0.13.preview @@ -0,0 +1,82 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.6.post5 and torch-memory-saver +RUN pip install --resume-retries 999 "sglang[all]==0.4.6.post5" --no-cache-dir --find-links https://flashinfer.ai/whl/cu124/torch2.6/flashinfer-python && pip install torch-memory-saver --no-cache-dir + +# Some sglang operations in 0.4.6.post5 require vllm +# [Warning] vllm can have some packages not compatible with sglang, for example, flashinfer +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.5 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_r0.13.0 + +# Fix for transformers 4.53.0 +RUN pip3 install --no-cache-dir "transformers[hf_xet]<4.52.0" + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12 b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12 new file mode 100644 index 0000000000000000000000000000000000000000..616c701c523a76eb11cfe8364a40ca7329023f73 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12 @@ -0,0 +1,47 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install torch-2.6.0+cu124 + vllm-0.8.5.post1 +# torch-2.6.0+cu124: cxx11abi=False +# torch-2.6.0+cu126: cxx11abi=True +# see https://github.com/flashinfer-ai/flashinfer/issues/911 +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Install flashinfer-0.2.2.post1+cu126 (cxx11abi=True) +# vllm-0.8.3 does not support flashinfer>=0.2.3 +# see https://github.com/vllm-project/vllm/pull/15777 +RUN aria2c --max-tries=9999 https://github.com/flashinfer-ai/flashinfer/releases/download/v0.2.2.post1/flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + pip install --no-cache-dir flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + rm flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Fix for transformers 4.53.0 +RUN pip3 install --no-cache-dir "transformers[hf_xet]<4.52.0" + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12.deepep b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12.deepep new file mode 100644 index 0000000000000000000000000000000000000000..a2be5e998dd32a2bcfbdcc63bedd2dd1ae634943 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.12.deepep @@ -0,0 +1,88 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install torch-2.6.0+cu124 + vllm-0.8.5.post1 +# torch-2.6.0+cu124: cxx11abi=False +# torch-2.6.0+cu126: cxx11abi=True +# see https://github.com/flashinfer-ai/flashinfer/issues/911 +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Install flashinfer-0.2.2.post1+cu126 (cxx11abi=True) +# vllm-0.8.3 does not support flashinfer>=0.2.3 +# see https://github.com/vllm-project/vllm/pull/15777 +RUN aria2c --max-tries=9999 https://github.com/flashinfer-ai/flashinfer/releases/download/v0.2.2.post1/flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + pip install --no-cache-dir flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + rm flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Fix for transformers 4.53.0 +RUN pip3 install --no-cache-dir "transformers[hf_xet]<4.52.0" + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.13.preview b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.13.preview new file mode 100644 index 0000000000000000000000000000000000000000..be6183a8479fe3e379f7ddaa06034c3e1ff64278 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.13.preview @@ -0,0 +1,85 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install torch-2.6.0+cu124 + vllm-0.8.5.post1 +# torch-2.6.0+cu124: cxx11abi=False +# torch-2.6.0+cu126: cxx11abi=True +# see https://github.com/flashinfer-ai/flashinfer/issues/911 +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.8.5.post1 + +# Install flashinfer-0.2.2.post1+cu126 (cxx11abi=True) +# vllm-0.8.3 does not support flashinfer>=0.2.3 +# see https://github.com/vllm-project/vllm/pull/15777 +RUN aria2c --max-tries=9999 https://github.com/flashinfer-ai/flashinfer/releases/download/v0.2.2.post1/flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + pip install --no-cache-dir flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl && \ + rm flashinfer_python-0.2.2.post1+cu124torch2.6-cp38-abi3-linux_x86_64.whl + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.5 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.base b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.base new file mode 100644 index 0000000000000000000000000000000000000000..25b1d9431e237287bfd720788974894d1c07873e --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.base @@ -0,0 +1,113 @@ +# Base Docker Image of verl, with CUDA/Torch/FlashAttn/Apex/TransformerEngine, without other frameworks +# Target: verlai/verl:base-v2-cu124-cudnn9.8-torch2.6-fa2.8.0-te2.3 +# Start from the NVIDIA official image (ubuntu-22.04 + cuda-12.6 + python-3.10) +# https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel-24-08.html +FROM nvcr.io/nvidia/pytorch:24.08-py3 + +# Define environments +ENV MAX_JOBS=16 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Define installation arguments +ARG APT_SOURCE=https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ +ARG PIP_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple + +# Set apt source +RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \ + { \ + echo "deb ${APT_SOURCE} jammy main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-updates main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-backports main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-security main restricted universe multiverse"; \ + } > /etc/apt/sources.list + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install tini +RUN apt-get update && \ + apt-get install -y tini aria2 && \ + apt-get clean + +# Change pip source +RUN pip config set global.index-url "${PIP_INDEX}" && \ + pip config set global.extra-index-url "${PIP_INDEX}" && \ + python -m pip install --upgrade pip + +# Uninstall nv-pytorch fork +RUN pip uninstall -y torch torchvision torchaudio \ + pytorch-quantization pytorch-triton torch-tensorrt \ + xgboost transformer_engine flash_attn apex megatron-core grpcio + +# Reinstall CUDA 12.4 +RUN aria2c https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-ubuntu2204.pin && \ + mv cuda-ubuntu2204.pin /etc/apt/preferences.d/cuda-repository-pin-600 + +RUN aria2c --always-resume=true --max-tries=99999 https://developer.download.nvidia.com/compute/cuda/12.4.1/local_installers/cuda-repo-ubuntu2204-12-4-local_12.4.1-550.54.15-1_amd64.deb && \ + dpkg -i cuda-repo-ubuntu2204-12-4-local_12.4.1-550.54.15-1_amd64.deb && \ + cp /var/cuda-repo-ubuntu2204-12-4-local/cuda-*-keyring.gpg /usr/share/keyrings/ && \ + apt-get update && \ + apt-get -y install cuda-toolkit-12-4 && \ + rm cuda-repo-ubuntu2204-12-4-local_12.4.1-550.54.15-1_amd64.deb && \ + update-alternatives --set cuda /usr/local/cuda-12.4 && \ + rm -rf /usr/local/cuda-12.6 + +RUN pip install --resume-retries 999 --no-cache-dir torch==2.6.0 torchvision==0.21.0 torchaudio==2.6.0 + +RUN pip install --resume-retries 999 --no-cache-dir "tensordict==0.6.2" torchdata "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +# Install flash-attn-2.7.4.post1 (cxx11abi=False) +RUN wget -nv https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu12torch2.6cxx11abiFALSE-cp310-cp310-linux_x86_64.whl && \ + pip install --no-cache-dir flash_attn-2.7.4.post1+cu12torch2.6cxx11abiFALSE-cp310-cp310-linux_x86_64.whl + +# Fix packages +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +# Install cudnn +RUN aria2c --max-tries=9999 https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-*-keyring.gpg /usr/share/keyrings/ && \ + apt-get update && \ + apt-get -y install cudnn-cuda-12 && \ + rm cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb + +# Install Apex +RUN git clone https://github.com/NVIDIA/apex.git && \ + cd apex && \ + pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" ./ + +# Profiling tools +RUN aria2c --always-resume=true --max-tries=99999 https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_3/nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 && \ + dpkg -i ./nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb + +# Fix opencv +RUN pip install --resume-retries 999 --no-cache-dir opencv-python + +RUN pip install --resume-retries 999 --no-cache-dir opencv-fixer && \ + python -c "from opencv_fixer import AutoFix; AutoFix()" + +RUN pip install --resume-retries 999 --no-cache-dir cuda-bindings + +# Reset pip config +RUN pip config unset global.index-url && \ + pip config unset global.extra-index-url + +RUN apt-get update && \ + apt-get install -y libfreeimage3 libfreeimage-dev zlib1g htop + diff --git a/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/README.md b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/README.md new file mode 100644 index 0000000000000000000000000000000000000000..d242e06a570cb7f2b29bc2f26f17159a6c5ae443 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.4-cu124-torch2.6-fa2.7.4/README.md @@ -0,0 +1,31 @@ +# verl image with verl v0.4.x + +## Important packages version + +```txt +cuda==12.4 +cudnn==9.8.0 +torch==2.6.0 +flash_attn=2.7.4 +sglang==0.4.6.post5 +vllm==0.8.5.post1 +nvidia-cudnn-cu12==9.8.0.87 +transformer_engine==2.3 +megatron.core==core_v0.12.2 +# Preview +transformer_engine==2.5 +megatron.core==core_r0.13.0 +``` + +## Target + +- Base image: + - `verlai/verl:base-verl0.4-cu124-cudnn9.8-torch2.6-fa2.7.4` +- App image: + - `verlai/verl:app-verl0.4-sglang0.4.6.post5-vllm0.8.5-mcore0.12.2-te2.2`: SGLang requires vLLM in 0.4.6.post5 version, vLLM can have some package conflicts with SGLang + - `verlai/verl:app-verl0.4-sglang0.4.6.post5-vllm0.8.5-mcore0.12.2-te2.2-deepep`: Built with deepep + - `verlai/verl:app-verl0.4-vllm0.8.5-mcore0.12.2-te2.2` + - `verlai/verl:app-verl0.4-vllm0.8.5-mcore0.12.2-te2.2-deepep`: Built with deepep +- Preview image: + - `verlai/verl:app-verl0.4-sglang0.4.6.post5-vllm0.8.5-mcore0.13.0-te2.2-preview` + - `verlai/verl:app-verl0.4-vllm0.8.5-mcore0.13.0-te2.2-preview` \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.10.post2.mcore0.13 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.10.post2.mcore0.13 new file mode 100644 index 0000000000000000000000000000000000000000..d1be8fb450bf0fe62fcf3d2cc3a140867ce99827 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.10.post2.mcore0.13 @@ -0,0 +1,37 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.7.4 + +# Define environments +ENV MAX_JOBS=8 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.10 +# Install FlashInfer Python package +RUN pip install --upgrade pip setuptools packaging +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation flashinfer-python==0.2.9rc1 +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation "sglang[all]==0.4.10.post2" + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]==4.55.4" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.13.0 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.9.post6.mcore0.13 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.9.post6.mcore0.13 new file mode 100644 index 0000000000000000000000000000000000000000..d79201a92eec3d778e32a023cfa3a3b32bf3ac55 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.sglang0.4.9.post6.mcore0.13 @@ -0,0 +1,37 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.7.4 + +# Define environments +ENV MAX_JOBS=8 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.10 +# Install FlashInfer Python package +RUN pip install --upgrade pip setuptools packaging +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation flashinfer-python==0.2.9rc1 +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation "sglang[all]==0.4.9.post6" + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]==4.55.4" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.13.0 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.13 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.13 new file mode 100644 index 0000000000000000000000000000000000000000..9d73e0ffeeb1c8d17c6d9022d5e5411dc5d70cc1 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.13 @@ -0,0 +1,38 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.7.4 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install torch-2.7.1+cu126 + vllm-0.10.0 +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.10.0 + +# Fix packages +# transformers 4.54.0 still not support +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.55.4" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.13.0 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge + +# Fix qwen vl +RUN pip3 install --no-cache-dir --no-deps trl \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.15 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.15 new file mode 100644 index 0000000000000000000000000000000000000000..296fd3b74f641efce2c90d6aec81fdcb81b8a867 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.app.vllm.mcore0.15 @@ -0,0 +1,39 @@ +# Start from the verl base image +# Dockerfile.base +FROM iseekyan/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.7.4-h100 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install torch-2.7.1+cu126 + vllm-0.10.0 +RUN pip install --resume-retries 999 --no-cache-dir vllm==0.10.0 + +# Fix packages +# transformers 4.54.0 still not support +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.55.4" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.7 +RUN pip install onnxscript + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.15.0rc4 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge==v0.15.0 + +# Fix qwen vl +RUN pip3 install --no-cache-dir --no-deps trl \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.base.torch2.7.1 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.base.torch2.7.1 new file mode 100644 index 0000000000000000000000000000000000000000..6d85cf512016e4cc0988da3d099230d5d0c2b5d7 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/Dockerfile.base.torch2.7.1 @@ -0,0 +1,133 @@ +# Base Docker Image of verl, with CUDA/Torch/FlashAttn/Apex/TransformerEngine, without other frameworks +# Target: verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.8.0-fi0.2.6 +# Start from the NVIDIA official image (ubuntu-22.04 + cuda-12.6 + python-3.10) +# https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel-24-08.html +FROM nvcr.io/nvidia/pytorch:24.08-py3 + +# Define environments +ENV MAX_JOBS=16 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Define installation arguments +ARG APT_SOURCE=https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ +ARG PIP_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple + +# Set apt source +RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \ + { \ + echo "deb ${APT_SOURCE} jammy main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-updates main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-backports main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-security main restricted universe multiverse"; \ + } > /etc/apt/sources.list + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install tini +RUN apt-get update && \ + apt-get install -y tini aria2 libfreeimage3 libfreeimage-dev zlib1g htop && \ + apt-get clean + +# Change pip source +RUN pip config set global.index-url "${PIP_INDEX}" && \ + pip config set global.extra-index-url "${PIP_INDEX}" && \ + python -m pip install --upgrade pip + +# Uninstall nv-pytorch fork +RUN pip uninstall -y torch torchvision torchaudio \ + pytorch-quantization pytorch-triton torch-tensorrt \ + xgboost transformer_engine flash_attn apex megatron-core grpcio + +RUN pip install --resume-retries 999 --no-cache-dir torch==2.7.1 torchvision==0.22.1 torchaudio==2.7.1 + +# Install flash-attn-2.7.4.post1, although built with torch2.6, it is compatible with torch2.7 +# https://github.com/Dao-AILab/flash-attention/issues/1644#issuecomment-2899396361 +RUN ABI_FLAG=$(python -c "import torch; print('TRUE' if torch._C._GLIBCXX_USE_CXX11_ABI else 'FALSE')") && \ + URL="https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.4.post1/flash_attn-2.7.4.post1+cu12torch2.6cxx11abi${ABI_FLAG}-cp310-cp310-linux_x86_64.whl" && \ + FILE="flash_attn-2.7.4.post1+cu12torch2.6cxx11abi${ABI_FLAG}-cp310-cp310-linux_x86_64.whl" && \ + wget -nv "${URL}" && \ + pip install --no-cache-dir "${FILE}" + +# Fix packages +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +# Install cudnn +RUN aria2c --max-tries=9999 https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-*-keyring.gpg /usr/share/keyrings/ && \ + apt-get update && \ + apt-get -y install cudnn-cuda-12 && \ + rm cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb + +# Install Apex +RUN pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" --resume-retries 999 git+https://github.com/NVIDIA/apex.git + +# Profiling tools +RUN aria2c --always-resume=true --max-tries=99999 https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_3/nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 + +RUN apt-get install -y ./nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb + +RUN pip install --resume-retries 999 --no-cache-dir "tensordict==0.6.2" torchdata "transformers[hf_xet]>=4.52.3" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas cuda-bindings \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install + +# Reset pip config +RUN pip config unset global.index-url && \ + pip config unset global.extra-index-url + diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/README.md b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/README.md new file mode 100644 index 0000000000000000000000000000000000000000..79527b76c241d6aa28c54b0e11b262863fdc1c86 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7-fa2.7.4/README.md @@ -0,0 +1,27 @@ +# verl image with verl v0.5 + +## Important packages version + +```txt +cuda==12.6 +cudnn==9.8.0 +torch==2.7.1 +flash_attn=2.7.4.post1 +sglang==0.4.9.post6 +vllm==0.8.5.post1 +nvidia-cudnn-cu12==9.8.0.87 +transformer_engine==2.3 +megatron.core==core_v0.12.2 +# Preview +transformer_engine==2.5 +megatron.core==core_r0.13.0 +``` + +## Target + +- Base image: + - `verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.7.4`: We offer a base image with deep ep built in, for vllm/sglang +- App image: + - `verlai/verl:app-verl0.5-transformers4.55.4-vllm0.10.0-mcore0.13.0-te2.2` + - `verlai/verl:app-verl0.5-transformers4.55.4-sglang0.4.10.post2-mcore0.13.0-te2.2` + - `iseekyan/verl:app-verl0.5-transformers4.55.4-vllm0.10.0-mcore0.15.0-te2.7` diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.12 b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.12 new file mode 100644 index 0000000000000000000000000000000000000000..205814a234bf4d1162a2fa89f1cd051d6463fb47 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.12 @@ -0,0 +1,37 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.8.0 + +# Define environments +ENV MAX_JOBS=8 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.8 and torch-memory-saver +# Install FlashInfer Python package +RUN pip install --upgrade pip setuptools packaging +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation flashinfer-python==0.2.6.post1 +RUN pip install --resume-retries 999 --no-cache-dir "sglang[all]==0.4.8" && pip install torch-memory-saver --no-cache-dir + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.3 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.13.preview b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.13.preview new file mode 100644 index 0000000000000000000000000000000000000000..5704813d9eee80a7ca790b204a836e81d58cbf1a --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.mcore0.13.preview @@ -0,0 +1,37 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.8.0 + +# Define environments +ENV MAX_JOBS=8 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.8 and torch-memory-saver +# Install FlashInfer Python package +RUN pip install --upgrade pip setuptools packaging +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation flashinfer-python==0.2.6.post1 +RUN pip install --resume-retries 999 --no-cache-dir "sglang[all]==0.4.8" && pip install torch-memory-saver --no-cache-dir + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.5 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_v0.12.2 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.base b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.base new file mode 100644 index 0000000000000000000000000000000000000000..53089fba400d56a3a37e1e943439c367ee40df55 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/Dockerfile.base @@ -0,0 +1,132 @@ +# Base Docker Image of verl, with CUDA/Torch/FlashAttn/Apex/TransformerEngine, without other frameworks +# Target: verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.8.0-fi0.2.6 +# Start from the NVIDIA official image (ubuntu-22.04 + cuda-12.6 + python-3.10) +# https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel-24-08.html +FROM nvcr.io/nvidia/pytorch:24.08-py3 + +# Define environments +ENV MAX_JOBS=16 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Define installation arguments +ARG APT_SOURCE=https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ +ARG PIP_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple + +# Set apt source +RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \ + { \ + echo "deb ${APT_SOURCE} jammy main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-updates main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-backports main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-security main restricted universe multiverse"; \ + } > /etc/apt/sources.list + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install tini +RUN apt-get update && \ + apt-get install -y tini aria2 libfreeimage3 libfreeimage-dev zlib1g htop && \ + apt-get clean + +# Change pip source +RUN pip config set global.index-url "${PIP_INDEX}" && \ + pip config set global.extra-index-url "${PIP_INDEX}" && \ + python -m pip install --upgrade pip + +# Uninstall nv-pytorch fork +RUN pip uninstall -y torch torchvision torchaudio \ + pytorch-quantization pytorch-triton torch-tensorrt \ + xgboost transformer_engine flash_attn apex megatron-core grpcio + +RUN pip install --resume-retries 999 --no-cache-dir torch==2.7.1 torchvision==0.22.1 torchaudio==2.7.1 + +# Install flash-attn-2.8.0.post2 (cxx11abi=True) +RUN ABI_FLAG=$(python -c "import torch; print('TRUE' if torch._C._GLIBCXX_USE_CXX11_ABI else 'FALSE')") && \ + URL="https://github.com/Dao-AILab/flash-attention/releases/download/v2.8.0.post2/flash_attn-2.8.0.post2+cu12torch2.7cxx11abi${ABI_FLAG}-cp310-cp310-linux_x86_64.whl" && \ + FILE="flash_attn-2.8.0.post2+cu12torch2.7cxx11abi${ABI_FLAG}-cp310-cp310-linux_x86_64.whl" && \ + wget -nv "${URL}" && \ + pip install --no-cache-dir "${FILE}" + +# Fix packages +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +# Install cudnn +RUN aria2c --max-tries=9999 https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-*-keyring.gpg /usr/share/keyrings/ && \ + apt-get update && \ + apt-get -y install cudnn-cuda-12 && \ + rm cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb + +# Install Apex +RUN pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" --resume-retries 999 git+https://github.com/NVIDIA/apex.git + +# Profiling tools +RUN aria2c --always-resume=true --max-tries=99999 https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_3/nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 + +RUN apt-get install -y ./nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb + +RUN pip install --resume-retries 999 --no-cache-dir "tensordict==0.6.2" torchdata "transformers[hf_xet]>=4.53" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas cuda-bindings \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pyext pre-commit ruff + +# Install DeepEP +## the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +## Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy + +## Build deepep-nvshmem +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install + +# Reset pip config +RUN pip config unset global.index-url && \ + pip config unset global.extra-index-url + diff --git a/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/README.md b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/README.md new file mode 100644 index 0000000000000000000000000000000000000000..4cbf4a974a5844376ddc36ade862140af55a6ba5 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-cu126-torch2.7.1-fa2.8.0/README.md @@ -0,0 +1,27 @@ +# verl image with verl v0.5 + +## Important packages version + +```txt +cuda==12.6 +cudnn==9.8.0 +torch==2.7.1 +flash_attn=2.8.0 ## +sglang==0.4.8 +vllm==0.8.5.post1 +nvidia-cudnn-cu12==9.8.0.87 +transformer_engine==2.3 +megatron.core==core_v0.12.2 +# Preview +transformer_engine==2.5 +megatron.core==core_r0.13.0 +``` + +## Target + +- Base image: + - `verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.1-fa2.8.0`: We offer a base image with deep ep built in +- App image: + - `verlai/verl:app-verl0.5-sglang0.4.9-mcore0.12.2` + - `verlai/verl:app-verl0.5-sglang0.4.9-mcore0.13.0-preview` +- vllm temporarily not support latest version \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.megatron b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.megatron new file mode 100644 index 0000000000000000000000000000000000000000..d41ea19d69bccb39436a79a0798a9cb0cbefec04 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.app.sglang.megatron @@ -0,0 +1,36 @@ +# Start from the verl base image +# Dockerfile.base +FROM verlai/verl:base-verl0.5-preview-cu128-cudnn9.8-torch2.7.1-fa2.8.0-fi0.2.6 + +# Define environments +ENV MAX_JOBS=8 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Install sglang-0.4.8 and torch-memory-saver +# Install FlashInfer Python package +RUN pip install --resume-retries 999 --no-cache-dir --no-build-isolation flashinfer-python==0.2.6.post1 +RUN pip install --resume-retries 999 --no-cache-dir "sglang[all]==0.4.8" && pip install torch-memory-saver --no-cache-dir + +# Fix packages +RUN pip install --no-cache-dir "tensordict==0.6.2" "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pre-commit ruff + +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --resume-retries 999 --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +RUN pip install --resume-retries 999 --no-cache-dir nvidia-cudnn-cu12==9.8.0.87 + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --resume-retries 999 --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.5 + +# Install Megatron-LM +RUN pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/Megatron-LM.git@core_r0.13.0 + +# Install mbridge +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.base b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.base new file mode 100644 index 0000000000000000000000000000000000000000..29c49faa848c0e15350abaf48cad7f8a899f4eb6 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/Dockerfile.base @@ -0,0 +1,91 @@ +# Base Docker Image of verl, with CUDA/Torch/FlashAttn/Apex/TransformerEngine, without other frameworks +# Target: verlai/verl:base-verl0.5-preview-cu128-cudnn9.8-torch2.7.1-fa2.8.0-fi0.2.6 +# Start from the NVIDIA official image (ubuntu-22.04 + cuda-12.6 + python-3.10) +# https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel-24-08.html +FROM nvcr.io/nvidia/pytorch:25.02-py3 + +# Define environments +ENV MAX_JOBS=16 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" + +# Define installation arguments +ARG APT_SOURCE=https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ +ARG PIP_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple + +# Set apt source +RUN cp /etc/apt/sources.list /etc/apt/sources.list.bak && \ + { \ + echo "deb ${APT_SOURCE} jammy main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-updates main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-backports main restricted universe multiverse"; \ + echo "deb ${APT_SOURCE} jammy-security main restricted universe multiverse"; \ + } > /etc/apt/sources.list + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install tini +RUN apt-get update && \ + apt-get install -y tini aria2 libfreeimage3 libfreeimage-dev zlib1g htop && \ + apt-get clean + +# Change pip source +RUN pip config set global.index-url "${PIP_INDEX}" && \ + pip config set global.extra-index-url "${PIP_INDEX}" && \ + python -m pip install --upgrade pip + +# Uninstall nv-pytorch fork +RUN pip uninstall -y torch torchvision torchaudio \ + pytorch-quantization pytorch-triton torch-tensorrt \ + xgboost transformer_engine flash_attn apex megatron-core grpcio + +RUN pip install --resume-retries 999 --no-cache-dir torch==2.7.1 torchvision==0.22.1 torchaudio==2.7.1 --index-url https://download.pytorch.org/whl/cu128 + +# Install flash-attn-2.8.0.post2 (cxx11abi=True) +RUN ABI_FLAG=$(python -c "import torch; print('TRUE' if torch._C._GLIBCXX_USE_CXX11_ABI else 'FALSE')") && \ + URL="https://github.com/Dao-AILab/flash-attention/releases/download/v2.8.0.post2/flash_attn-2.8.0.post2+cu12torch2.7cxx11abi${ABI_FLAG}-cp312-cp312-linux_x86_64.whl" && \ + FILE="flash_attn-2.8.0.post2+cu12torch2.7cxx11abi${ABI_FLAG}-cp312-cp312-linux_x86_64.whl" && \ + wget -nv "${URL}" && \ + pip install --no-cache-dir "${FILE}" + +# Fix packages +RUN pip uninstall -y pynvml nvidia-ml-py && \ + pip install --no-cache-dir --upgrade "nvidia-ml-py>=12.560.30" "fastapi[standard]>=0.115.0" "optree>=0.13.0" "pydantic>=2.9" "grpcio>=1.62.1" + +# Install cudnn +RUN aria2c --max-tries=9999 https://developer.download.nvidia.com/compute/cudnn/9.8.0/local_installers/cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + dpkg -i cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb && \ + cp /var/cudnn-local-repo-ubuntu2204-9.8.0/cudnn-*-keyring.gpg /usr/share/keyrings/ && \ + apt-get update && \ + apt-get -y install cudnn-cuda-12 && \ + rm cudnn-local-repo-ubuntu2204-9.8.0_1.0-1_amd64.deb + +# Install Apex +RUN pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" --resume-retries 999 git+https://github.com/NVIDIA/apex.git + +# Profiling tools +RUN aria2c --always-resume=true --max-tries=99999 https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_3/nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 + +RUN apt-get install -y ./nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.3.1/target-linux-x64/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-2025.3.1_2025.3.1.90-1_amd64.deb + +RUN pip install --resume-retries 999 --no-cache-dir "tensordict==0.6.2" torchdata "transformers[hf_xet]>=4.51.0" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas cuda-bindings \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pre-commit ruff + +# Reset pip config +RUN pip config unset global.index-url && \ + pip config unset global.extra-index-url + diff --git a/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/README.md b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/README.md new file mode 100644 index 0000000000000000000000000000000000000000..bb16d2abece29efc5b839279d78dc21191240b63 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.5-preview-cu128-torch2.7.1-fa2.8.0/README.md @@ -0,0 +1,26 @@ +# verl image with verl v0.5 + +## Important packages version + +```txt +cuda==12.8 +cudnn==9.8.0 +torch==2.7.1 +flash_attn=2.8.0 ## +sglang==0.4.8 +transformer_engine==2.5 +megatron.core==core_r0.13.0 +nvidia-cudnn-cu12==9.8.0.87 +``` + +## Target + +- Base image: + - `verlai/verl:base-verl0.5-preview-cu128-cudnn9.8-torch2.7.1-fa2.8.0`: We offer a base image with flash infer 0.2.6.post1 built in +- App image: + - `verlai/verl:app-verl0.5-preview-sglang0.4.8-mcore0.13.0-preview` +- vllm temporarily not support latest version + +## !!!Notice!!! + +- pyext is lack of maintainace and cannot work with python 3.12, consider using replacement and deprecating this package. \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.app.sglang b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.app.sglang new file mode 100644 index 0000000000000000000000000000000000000000..23dbea72ccce3030beb76691ba1832fc04dcdc76 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.app.sglang @@ -0,0 +1,4 @@ +FROM verlai/verl:base-verl0.6-cu128-cudnn9.8-torch2.8.0-fa2.7.4 + +RUN pip install --no-cache-dir "sglang[all]==0.5.2" +RUN pip install --no-cache-dir "torch-memory-saver==0.0.9rc1" diff --git a/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.base b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.base new file mode 100644 index 0000000000000000000000000000000000000000..7bfd48169cc0ed666507d6560669e4c6f3511a11 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.base @@ -0,0 +1,108 @@ +# Start from the NVIDIA official image (ubuntu-24.04 + cuda-12.8 + python-3.12) +# https://docs.nvidia.com/deeplearning/frameworks/pytorch-release-notes/rel-25-03.html +FROM nvcr.io/nvidia/pytorch:25.03-py3 + +# Define environments +ENV MAX_JOBS=32 +ENV VLLM_WORKER_MULTIPROC_METHOD=spawn +ENV DEBIAN_FRONTEND=noninteractive +ENV NODE_OPTIONS="" +ENV PIP_ROOT_USER_ACTION=ignore +ENV HF_HUB_ENABLE_HF_TRANSFER="1" +ENV PIP_CONSTRAINT="" + +ARG PIP_INDEX=https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple + +# Change pip source +RUN pip config set global.index-url "${PIP_INDEX}" && \ + pip config set global.extra-index-url "${PIP_INDEX}" && \ + pip config set global.no-cache-dir "true" && \ + python -m pip install --upgrade pip + +# Install systemctl +RUN apt-get update && \ + apt-get install -y -o Dpkg::Options::="--force-confdef" systemd && \ + apt-get clean + +# Install libxml2 +RUN apt-get update && \ + apt-get install -y libxml2 aria2 && \ + apt-get clean + +# Uninstall nv-pytorch fork +RUN pip uninstall -y torch torchvision torchaudio \ + pytorch-quantization pytorch-triton torch-tensorrt \ + transformer_engine flash_attn apex megatron-core \ + xgboost opencv grpcio + +# Fix packages +RUN pip install --no-cache-dir tensordict torchdata "transformers[hf_xet]==4.55.4" accelerate datasets peft hf-transfer \ + "numpy<2.0.0" "pyarrow>=19.0.1" pandas \ + ray[default] codetiming hydra-core pylatexenc qwen-vl-utils wandb dill pybind11 liger-kernel mathruler blobfile xgrammar \ + pytest py-spy pre-commit ruff + +# Fix cv2 +RUN rm -rf /usr/local/lib/python3.11/dist-packages/cv2 + +# Install torch +RUN pip install --no-cache-dir torch==2.8.0 --index-url https://download.pytorch.org/whl/cu128 + +# Install flash-attn +RUN pip install --no-cache-dir --no-build-isolation flash_attn==2.7.4.post1 + +# Install DeepEP +# the dependency of IBGDA +RUN ln -s /usr/lib/x86_64-linux-gnu/libmlx5.so.1 /usr/lib/x86_64-linux-gnu/libmlx5.so + +# Clone and build deepep and deepep-nvshmem +RUN git clone -b v2.3.1 https://github.com/NVIDIA/gdrcopy.git && \ + git clone https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout a84a248 + +# Prepare nvshmem +RUN wget https://developer.nvidia.com/downloads/assets/secure/nvshmem/nvshmem_src_3.2.5-1.txz && \ + tar -xvf nvshmem_src_3.2.5-1.txz && mv nvshmem_src deepep-nvshmem && \ + cd deepep-nvshmem && git apply ../DeepEP/third-party/nvshmem.patch + +## Build deepep-nvshmem +RUN apt-get install -y ninja-build cmake + +ENV CUDA_HOME=/usr/local/cuda +### Set MPI environment variables. Having errors when not set. +ENV CPATH=/usr/local/mpi/include:$CPATH +ENV LD_LIBRARY_PATH=/usr/local/mpi/lib:$LD_LIBRARY_PATH +ENV LD_LIBRARY_PATH=/usr/local/x86_64-linux-gnu:$LD_LIBRARY_PATH +ENV GDRCOPY_HOME=/workspace/gdrcopy +ENV GDRCOPY_INCLUDE=/workspace/gdrcopy/include + +RUN cd deepep-nvshmem && \ + NVSHMEM_SHMEM_SUPPORT=0 \ + NVSHMEM_UCX_SUPPORT=0 \ + NVSHMEM_USE_NCCL=0 \ + NVSHMEM_MPI_SUPPORT=0 \ + NVSHMEM_IBGDA_SUPPORT=1 \ + NVSHMEM_PMIX_SUPPORT=0 \ + NVSHMEM_TIMEOUT_DEVICE_POLLING=0 \ + NVSHMEM_USE_GDRCOPY=1 \ + cmake -G Ninja -S . -B build/ -DCMAKE_INSTALL_PREFIX=/workspace/deepep-nvshmem/install && cmake --build build/ --target install + +ENV NVSHMEM_DIR=/workspace/deepep-nvshmem/install +ENV LD_LIBRARY_PATH=$NVSHMEM_DIR/lib:$LD_LIBRARY_PATH +ENV PATH=$NVSHMEM_DIR/bin:$PATH + +## Build deepep +RUN cd DeepEP && \ + python setup.py install + +# Install Apex +RUN pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git + +# Install TransformerEngine +RUN export NVTE_FRAMEWORK=pytorch && pip3 install --no-deps --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@v2.2.1 + +# Install Megatron-LM +RUN git clone -b core_v0.13.0 https://github.com/NVIDIA/Megatron-LM.git && \ + cd Megatron-LM && pip3 install --no-deps -e . + +# Install mbridge +RUN pip3 install --no-cache-dir git+https://github.com/ISEEKYAN/mbridge.git diff --git a/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.vllm011.mcore_gpt-oss b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.vllm011.mcore_gpt-oss new file mode 100644 index 0000000000000000000000000000000000000000..4d57448686a1ae1270429fa0c02168682d36731f --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.6-cu128-torch2.8.0-fa2.7.4/Dockerfile.vllm011.mcore_gpt-oss @@ -0,0 +1,15 @@ +FROM nvcr.io/nvidia/nemo:25.07.gpt_oss + +RUN git clone -b v0.11.0 --depth 1 https://github.com/vllm-project/vllm.git /opt/vllm + +RUN pip install setuptools_scm + +RUN cd /opt/vllm && pip install --no-deps --no-build-isolation --no-cache-dir -e . + +RUN pip install cbor2 setproctitle blake3 openai_harmony pybase64 msgspec partial_json_parser py-cpuinfo diskcache gguf + +RUN pip install --upgrade transformers tokenizers + +RUN pip install codetiming tensordict mathruler pylatexenc + +RUN pip3 install --no-cache-dir mbridge \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.sglang056exp b/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.sglang056exp new file mode 100644 index 0000000000000000000000000000000000000000..9d422183544d4659c7ffbfb1631bfce20757c5b7 --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.sglang056exp @@ -0,0 +1,63 @@ +# Dockerfile for verlai/verl:sgl056.exp +FROM lmsysorg/sglang:v0.5.6.post1 + +RUN pip install pybind11 + +RUN pip install nvidia-mathdx + +RUN pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" git+https://github.com/NVIDIA/apex.git + +RUN export NVTE_FRAMEWORK=pytorch && MAX_JOBS=128 NVTE_BUILD_THREADS_PER_JOB=4 pip3 install --resume-retries 999 --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.11 + +RUN pip install --upgrade --no-cache-dir transformers tokenizers + +RUN pip install codetiming tensordict mathruler pylatexenc qwen_vl_utils + +RUN pip install --no-cache-dir --no-build-isolation flash_attn==2.8.1 + +RUN NSIGHT_VERSION=2025.6.1_2025.6.1.190-1_$(if [ "$(uname -m)" = "aarch64" ]; then echo "arm64"; else echo "amd64"; fi) && \ + wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_6/nsight-systems-${NSIGHT_VERSION}.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 && \ + apt-get install -y ./nsight-systems-${NSIGHT_VERSION}.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-${NSIGHT_VERSION}.deb + + +# ========================= +# Install HybridEP +# ========================= +WORKDIR /home/ +RUN git clone --branch hybrid-ep https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout 3f601f7ac1c062c46502646ff04c535013bfca00 && \ + TORCH_CUDA_ARCH_LIST="9.0;10.0" pip install --no-build-isolation . + +# ========================= +# Install Qwen3-Next dependencies +# ========================= +WORKDIR /home/ +# Install causal-conv1d and flash-linear-attention +RUN cd /tmp && \ + git clone https://github.com/Dao-AILab/causal-conv1d.git && \ + cd causal-conv1d && \ + unset PIP_CONSTRAINT && \ + CAUSAL_CONV1D_FORCE_BUILD=TRUE pip install --no-build-isolation . && \ + cd .. && \ + rm -rf causal-conv1d && \ + pip install flash-linear-attention + +RUN pip install --no-cache-dir torch-memory-saver + +RUN pip3 install --no-cache-dir --no-deps trl + +RUN pip3 install nvtx matplotlib liger_kernel + +RUN pip install -U git+https://github.com/ISEEKYAN/mbridge.git + +RUN pip install --no-deps --no-cache-dir git+https://github.com/NVIDIA/Megatron-LM.git@1d462bd37dac21cfa14177405d4921eedb987052 # latest dev branch on 20251209 + +RUN pip install git+https://github.com/verl-project/verl.git@v0.6.1 + +RUN pip uninstall -y verl \ No newline at end of file diff --git a/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.vllm012exp b/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.vllm012exp new file mode 100644 index 0000000000000000000000000000000000000000..832a645627365ff9ea4cfc8b3a8f6566a4db377e --- /dev/null +++ b/verl_0720_main/verl/docker/verl0.6.1-experimental/Dockerfile.vllm012exp @@ -0,0 +1,69 @@ +# dockerfile for verlai/verl:vll012.exp +FROM nvcr.io/nvidia/pytorch:25.11-py3 + +RUN git clone -b v0.12.0 --depth 1 https://github.com/vllm-project/vllm.git /opt/vllm + +RUN pip install setuptools_scm + +RUN cd /opt/vllm && pip install --no-deps --no-build-isolation --no-cache-dir -e . + +RUN pip install -r /opt/vllm/requirements/common.txt + + +RUN pip install pybind11 + +RUN export NVTE_FRAMEWORK=pytorch && MAX_JOBS=128 NVTE_BUILD_THREADS_PER_JOB=4 pip3 install --resume-retries 999 --no-cache-dir --no-build-isolation git+https://github.com/NVIDIA/TransformerEngine.git@release_v2.11 + +RUN pip install --upgrade --no-cache-dir transformers tokenizers + +RUN pip install codetiming tensordict mathruler pylatexenc qwen_vl_utils + +RUN pip install flash_attn +#==2.8.1 + +RUN apt update && apt install numactl + +RUN NSIGHT_VERSION=2025.6.1_2025.6.1.190-1_$(if [ "$(uname -m)" = "aarch64" ]; then echo "arm64"; else echo "amd64"; fi) && \ + wget https://developer.nvidia.com/downloads/assets/tools/secure/nsight-systems/2025_6/nsight-systems-${NSIGHT_VERSION}.deb && \ + apt-get update && apt-get install -y libxcb-cursor0 && \ + apt-get install -y ./nsight-systems-${NSIGHT_VERSION}.deb && \ + rm -rf /usr/local/cuda/bin/nsys && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys /usr/local/cuda/bin/nsys && \ + rm -rf /usr/local/cuda/bin/nsys-ui && \ + ln -s /opt/nvidia/nsight-systems/2025.6.1/nsys-ui /usr/local/cuda/bin/nsys-ui && \ + rm nsight-systems-${NSIGHT_VERSION}.deb + + +# ========================= +# Install HybridEP +# ========================= +WORKDIR /home/ +RUN git clone --branch hybrid-ep https://github.com/deepseek-ai/DeepEP.git && \ + cd DeepEP && git checkout 3f601f7ac1c062c46502646ff04c535013bfca00 && \ + TORCH_CUDA_ARCH_LIST="9.0;10.0" pip install --no-build-isolation . + +# ========================= +# Install Qwen3-Next dependencies +# ========================= +WORKDIR /home/ +# Install causal-conv1d and flash-linear-attention +RUN cd /tmp && \ + git clone https://github.com/Dao-AILab/causal-conv1d.git && \ + cd causal-conv1d && \ + unset PIP_CONSTRAINT && \ + CAUSAL_CONV1D_FORCE_BUILD=TRUE pip install --no-build-isolation . && \ + cd .. && \ + rm -rf causal-conv1d && \ + pip install flash-linear-attention + +RUN pip3 install --no-cache-dir --no-deps trl + +RUN pip3 install nvtx matplotlib liger_kernel + +RUN pip install -U git+https://github.com/ISEEKYAN/mbridge.git + +RUN pip install --no-deps --no-cache-dir git+https://github.com/NVIDIA/Megatron-LM.git@1d462bd37dac21cfa14177405d4921eedb987052 # latest dev branch on 20251209 + +RUN pip install git+https://github.com/verl-project/verl.git@v0.6.1 + +RUN pip uninstall -y verl \ No newline at end of file diff --git a/verl_0720_main/verl/docs/Makefile b/verl_0720_main/verl/docs/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..8bda904a9b0b29dfcf538cb52b806dd910710a4a --- /dev/null +++ b/verl_0720_main/verl/docs/Makefile @@ -0,0 +1,20 @@ +# Minimal makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +SPHINXPROJ = verl +SOURCEDIR = . +BUILDDIR = _build + +# Put it first so that "make" without argument is like "make help". +help: + @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) + +.PHONY: help Makefile + +# Catch-all target: route all unknown targets to Sphinx using the new +# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). +%: Makefile + @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) diff --git a/verl_0720_main/verl/docs/README.md b/verl_0720_main/verl/docs/README.md new file mode 100644 index 0000000000000000000000000000000000000000..8c5db04874138435ef986342a7b8be668b81d0b0 --- /dev/null +++ b/verl_0720_main/verl/docs/README.md @@ -0,0 +1,22 @@ +# verl documentations + +## Build the docs + +```bash +# If you want to view auto-generated API docstring, please make sure verl is available in python path. For instance, install verl via: +# pip install .. -e[test] + +# Install dependencies needed for building docs. +pip install -r requirements-docs.txt + +# Build the docs. +make clean +make html +``` + +## Open the docs with your browser + +```bash +python -m http.server -d _build/html/ +``` +Launch your browser and navigate to http://localhost:8000 to view the documentation. Alternatively you could drag the file `_build/html/index.html` to your local browser and view directly. diff --git a/verl_0720_main/verl/docs/README_vllm0.7.md b/verl_0720_main/verl/docs/README_vllm0.7.md new file mode 100644 index 0000000000000000000000000000000000000000..ffef3200767c32d0a714b96af4722065fb016fd5 --- /dev/null +++ b/verl_0720_main/verl/docs/README_vllm0.7.md @@ -0,0 +1,73 @@ +# Upgrading to vllm >= 0.7 + +Note: verl+vllm 0.8.3 is now stable. Please see ``docs/README_vllm0.8.md`` for upgrade guide. + +## Installation + +Note: At time of writing, verl+vllm 0.7.x supports **FSDP** for training and **vLLM** for rollout. + +``` +# Create the conda environment +conda create -n verl python==3.10 +conda activate verl + +# Install verl +git clone https://github.com/verl-project/verl.git +cd verl +pip3 install -e . + +# Install the latest stable version of vLLM +pip3 install vllm==0.7.3 + +# Install flash-attn +pip3 install flash-attn --no-build-isolation + +``` + +Note that if you are installing lower versions of vLLM (0.7.0, 0.7.1, 0.7.2), you need to make some tiny patches manually on vllm (/path/to/site-packages/vllm after installation) after the above steps: + +- vllm/distributed/parallel_state.py: Remove the assertion below: + +``` +if (world_size + != tensor_model_parallel_size * pipeline_model_parallel_size): + raise RuntimeError( + f"world_size ({world_size}) is not equal to " + f"tensor_model_parallel_size ({tensor_model_parallel_size}) x " + f"pipeline_model_parallel_size ({pipeline_model_parallel_size})") + +``` + +- vllm/executor/uniproc_executor.py: change `local_rank = rank` to `local_rank = int(os.environ["LOCAL_RANK"])` +- vllm/model_executor/model_loader/weight_utils.py: remove the `torch.cuda.empty_cache()` in `pt_weights_iterator` + +## Features + +### Use cuda graph + +After installation, examples using FSDP as training backends can be used. By default, the `enforce_eager` is set to True, which disables the cuda graph. To enjoy cuda graphs and the sleep mode of vLLM>=0.7, add the following lines to the bash script: + +``` +actor_rollout_ref.rollout.enforce_eager=False \ +actor_rollout_ref.rollout.free_cache_engine=True \ + +``` + +For a typical job like examples/ppo_trainer/run_qwen3_8b_fsdp.sh, the rollout generation time is 85 seconds with vLLM0.7.0. By enabling the cudagraph, the generation duration is further reduced to 62 seconds. + +**Note:** Currently, if the `n` is greater than 1 in `SamplingParams` in vLLM>=0.7, there is a potential performance issue on the stability of rollout generation time (Some iterations would see generation time bursts) using vLLM's V0 Engine. + +### Use vLLM V1 Engine + +Using the vLLM V1 engine can avoid instability issues and achieve additional performance improvements. To use the V1 engine, you can first uninstall the previously installed vLLM and then follow the steps below to install the newer version. + +``` +git clone https://github.com/vllm-project/vllm.git +cd vllm +git checkout 2275784 +sed -i "903a\ data_parallel_size = world_size // pipeline_model_parallel_size // tensor_model_parallel_size" ./vllm/distributed/parallel_state.py +VLLM_USE_PRECOMPILED=1 pip install --editable . +``` + +Then you can enable the V1 engine by setting `export VLLM_USE_V1=1`. In some benchmark tests, the V1 engine demonstrates a 1.5x speed improvement over the vLLM V0 engine. +The stable support of the vLLM V1 engine is available on verl main. diff --git a/verl_0720_main/verl/docs/README_vllm0.8.md b/verl_0720_main/verl/docs/README_vllm0.8.md new file mode 100644 index 0000000000000000000000000000000000000000..3641b30155853e3cfa1c51e83d0dc94b224dd50b --- /dev/null +++ b/verl_0720_main/verl/docs/README_vllm0.8.md @@ -0,0 +1,52 @@ +# Upgrading to vLLM >= 0.8 + +Last updated: 05/04/2025. + +## Installation + +Note: This version of verl+vLLM 0.8+ supports **FSDP** for training and **vLLM** for rollout. + +```bash +# Create the conda environment +conda create -n verl python==3.10 +conda activate verl + +# Install verl +git clone https://github.com/verl-project/verl.git +cd verl +pip3 install -e . + +# Install the latest stable version of vLLM +pip3 install vllm==0.8.3 + +# Install flash-attn +pip3 install flash-attn --no-build-isolation + +``` + +We have a pre-built docker image for verl+vLLM 0.8.3. You can direct import it with the following command: + +```bash +docker pull hiyouga/verl:ngc-th2.6.0-cu126-vllm0.8.3-flashinfer0.2.2-cxx11abi0 +``` + +## Features + +vLLM 0.8+ supports cuda graph and V1 engine by default in verl. To enable these features, remember to add the following lines to the bash script: + +```bash +actor_rollout_ref.rollout.enforce_eager=False \ +actor_rollout_ref.rollout.free_cache_engine=True \ +``` + +and also **remove** the environment variable if it exists: + +## Notes + +When you just directly upgrade vllm>=0.8, some dependency packages may undergo version changes. If you encounter the following problems: + +```bash +in from torch.multiprocessing.reductions import ForkingPickler ImportError: cannot import name 'ForkingPickler' from 'torch.multiprocessing.reductions' (/opt/conda/lib/python3.11/site-packages/torch/multiprocessing/reductions.py) +``` + +You need to upgrade `tensordict` to version 0.6.2 using the command `pip install tensordict==0.6.2`. diff --git a/verl_0720_main/verl/docs/_static/custom.css b/verl_0720_main/verl/docs/_static/custom.css new file mode 100644 index 0000000000000000000000000000000000000000..32f08475754bc280bca407d1643ec3aa68eeacf3 --- /dev/null +++ b/verl_0720_main/verl/docs/_static/custom.css @@ -0,0 +1,217 @@ +/* Make the documentation use full screen width */ +.wy-nav-content { + max-width: none !important; + width: 100% !important; + padding: 1.618em 3.236em !important; +} + +/* Adjust the content wrapper - will be set by JavaScript */ +.wy-nav-content-wrap { + margin-left: 300px; + transition: margin-left 0.2s ease; + width: auto !important; + position: relative !important; + background: white !important; + min-height: 100vh !important; +} + +/* Make the main content area responsive */ +.rst-content { + max-width: none !important; + width: 100% !important; +} + +/* Optional: Adjust table widths to prevent overflow */ +.rst-content table.docutils { + width: 100% !important; + table-layout: auto !important; +} + +/* Optional: Better code block width handling */ +.rst-content .highlight { + width: 100% !important; +} + +/* Content area positioning already handled above */ + +/* Optional: Improve readability with some margin on very wide screens */ +@media (min-width: 1400px) { + .wy-nav-content { + max-width: none !important; + margin: 0 auto !important; + } +} + +/* Resizable sidebar styles */ +.wy-nav-side { + position: fixed !important; + top: 0 !important; + bottom: 0 !important; + left: 0 !important; + width: 300px; + min-width: 200px; + max-width: 600px; + display: flex; + flex-direction: column; + z-index: 200 !important; +} + +/* Ensure sidebar header (logo, search) adapts to width */ +.wy-side-nav-search { + width: 100% !important; + box-sizing: border-box !important; + padding: 0.809em 0.809em !important; +} + +.wy-side-nav-search input[type="text"] { + width: 100% !important; + box-sizing: border-box !important; +} + +/* Make logo/title area responsive */ +.wy-side-nav-search > div.version { + width: 100% !important; +} + +.wy-side-nav-search > a { + width: 100% !important; + display: block !important; + white-space: nowrap !important; + overflow: hidden !important; + text-overflow: ellipsis !important; +} + +/* Responsive adjustments for narrow sidebar */ +@media (max-width: 300px) { + .wy-side-nav-search > a { + font-size: 0.9em !important; + } + + .wy-side-nav-search input[type="text"] { + font-size: 0.8em !important; + } +} + +/* Ensure search input doesn't overflow */ +.wy-side-nav-search form { + width: 100% !important; + margin: 0 !important; +} + +/* Make search icon responsive */ +.wy-side-nav-search .wy-dropdown { + width: 100% !important; +} + +/* Adjust search results dropdown width */ +.wy-side-nav-search .wy-dropdown-menu { + width: 100% !important; + max-width: none !important; + left: 0 !important; + right: 0 !important; +} + +/* Resize handle is created by JavaScript */ + +/* Make sure the sidebar content doesn't overflow */ +.wy-side-scroll { + width: 100% !important; + flex: 1 !important; + overflow-y: auto !important; + overflow-x: hidden !important; + padding-right: 10px !important; + box-sizing: border-box !important; + scroll-behavior: auto !important; /* Prevent smooth scrolling on sidebar itself */ +} + +/* Ensure proper scroll behavior for main content area */ +html { + scroll-behavior: smooth !important; +} + +/* Ensure anchor links work properly in main content */ +.wy-nav-content-wrap { + scroll-behavior: smooth !important; +} + +/* Fix scroll to target for anchor links */ +.rst-content { + scroll-behavior: smooth !important; +} + +/* Fix anchor scroll offset to account for fixed header */ +.rst-content .section { + scroll-margin-top: 60px; +} + +/* Fix anchor scroll offset for headers */ +.rst-content h1, .rst-content h2, .rst-content h3, .rst-content h4, .rst-content h5, .rst-content h6 { + scroll-margin-top: 60px; +} + +/* Fix anchor scroll offset for specific scroll targets */ +.rst-content .headerlink { + scroll-margin-top: 60px; +} + +/* Fix sidebar navigation styling */ +.wy-menu-vertical { + width: 100% !important; +} + +.wy-menu-vertical li { + width: 100% !important; +} + +.wy-menu-vertical a { + width: 100% !important; + word-wrap: break-word !important; + white-space: normal !important; +} + +/* Content area margin is handled by JavaScript */ + +/* Custom drag handle (more visible) */ +.resize-handle { + position: absolute; + top: 0; + right: 0; + width: 8px; + height: 100%; + background: #ccc; + cursor: col-resize; + z-index: 1001; + opacity: 0.3; + transition: opacity 0.2s ease; +} + +.resize-handle:hover { + opacity: 0.8; + background: #999; +} + +.resize-handle::before { + content: ''; + position: absolute; + top: 50%; + left: 50%; + width: 2px; + height: 20px; + background: #666; + transform: translate(-50%, -50%); + border-radius: 1px; +} + +.resize-handle:hover::before { + background: #333; +} + +/* Ensure smooth resizing */ +.wy-nav-side.resizing { + user-select: none; + pointer-events: none; +} + +.wy-nav-side.resizing .wy-side-scroll { + overflow: hidden; +} \ No newline at end of file diff --git a/verl_0720_main/verl/docs/_static/js/resizable-sidebar.js b/verl_0720_main/verl/docs/_static/js/resizable-sidebar.js new file mode 100644 index 0000000000000000000000000000000000000000..3f040848edfee669fb4a56c07aaa3f048e052f8f --- /dev/null +++ b/verl_0720_main/verl/docs/_static/js/resizable-sidebar.js @@ -0,0 +1,263 @@ +// Resizable sidebar functionality +document.addEventListener('DOMContentLoaded', function() { + const sidebar = document.querySelector('.wy-nav-side'); + const content = document.querySelector('.wy-nav-content-wrap'); + + if (!sidebar || !content) return; + + // Create resize handle + const resizeHandle = document.createElement('div'); + resizeHandle.className = 'resize-handle'; + sidebar.appendChild(resizeHandle); + + let isResizing = false; + let startX = 0; + let startWidth = 0; + + // Get initial width + const getInitialWidth = () => { + return 300; // Default width + }; + + // Save width to localStorage + const saveWidth = (width) => { + localStorage.setItem('sidebar-width', width); + }; + + // Load width from localStorage + const loadWidth = () => { + const savedWidth = localStorage.getItem('sidebar-width'); + if (savedWidth) { + const width = parseInt(savedWidth, 10); + if (width >= 200 && width <= 600) { + return width; + } + } + return getInitialWidth(); + }; + + // Apply width to sidebar and content + const applyWidth = (width) => { + // Update sidebar width + sidebar.style.width = width + 'px'; + + // Update content margin with !important to override any CSS + content.style.setProperty('margin-left', width + 'px', 'important'); + + // Also update any other content wrapper that might exist + const contentInner = document.querySelector('.wy-nav-content'); + if (contentInner) { + contentInner.style.setProperty('margin-left', '0px', 'important'); + } + + // Force reflow and repaint + sidebar.offsetHeight; + content.offsetHeight; + + // Trigger window resize event to notify other components + window.dispatchEvent(new Event('resize')); + }; + + // Initialize with saved width + const initialWidth = loadWidth(); + applyWidth(initialWidth); + + // Mouse down on resize handle + resizeHandle.addEventListener('mousedown', (e) => { + isResizing = true; + startX = e.clientX; + startWidth = parseInt(window.getComputedStyle(sidebar).width, 10); + + sidebar.classList.add('resizing'); + document.body.style.cursor = 'col-resize'; + document.body.style.userSelect = 'none'; + + // Add overlay to prevent iframe issues + const overlay = document.createElement('div'); + overlay.style.cssText = ` + position: fixed; + top: 0; + left: 0; + width: 100%; + height: 100%; + z-index: 9999; + cursor: col-resize; + `; + overlay.id = 'resize-overlay'; + document.body.appendChild(overlay); + + e.preventDefault(); + }); + + // Mouse move + document.addEventListener('mousemove', (e) => { + if (!isResizing) return; + + const width = startWidth + e.clientX - startX; + const clampedWidth = Math.max(200, Math.min(600, width)); + applyWidth(clampedWidth); + }); + + // Mouse up + document.addEventListener('mouseup', () => { + if (!isResizing) return; + + isResizing = false; + sidebar.classList.remove('resizing'); + document.body.style.cursor = ''; + document.body.style.userSelect = ''; + + // Remove overlay + const overlay = document.getElementById('resize-overlay'); + if (overlay) { + overlay.remove(); + } + + // Save the current width + const currentWidth = parseInt(window.getComputedStyle(sidebar).width, 10); + saveWidth(currentWidth); + }); + + // Handle window resize - removed to prevent infinite loop + // The sidebar width is fixed and managed by drag functionality, no need to recalculate on window resize + + // Double-click to reset to default width + resizeHandle.addEventListener('dblclick', () => { + const defaultWidth = 300; + applyWidth(defaultWidth); + saveWidth(defaultWidth); + }); +}); + +// the sidebar will be scrolled when the cursor is hovered over it +document.addEventListener('DOMContentLoaded', function() { + const nav = window.SphinxRtdTheme && window.SphinxRtdTheme.Navigation; + + if (!nav) return; + + nav.onScroll = function() { + this.winScroll = false; + this.winPosition = this.win.scrollTop(); + }; +}); + +// Fix navigation issues - Using MutationObserver for reliable initialization +document.addEventListener('DOMContentLoaded', function() { + let navigationFixed = false; + + function setupNavigationFix() { + if (navigationFixed) return; + + // Find all links in the sidebar + const sidebarLinks = document.querySelectorAll('.wy-menu-vertical a'); + + // Only proceed if we have sidebar links + if (sidebarLinks.length === 0) return; + + console.log('Setting up navigation fix...'); + + sidebarLinks.forEach(function(link) { + const href = link.getAttribute('href'); + + // Clone the link to remove all existing event listeners + const newLink = link.cloneNode(true); + + // Add our own click handler + newLink.addEventListener('click', function(e) { + console.log('Link clicked:', href); + + // If it's an anchor link within the same page + if (href && href.startsWith('#') && href !== '#') { + e.preventDefault(); + e.stopPropagation(); + + const targetId = href.substring(1); + const targetElement = document.getElementById(targetId); + + if (targetElement) { + // Calculate offset for fixed header + const headerHeight = 60; + const elementPosition = targetElement.getBoundingClientRect().top; + const offsetPosition = elementPosition + window.pageYOffset - headerHeight; + + window.scrollTo({ + top: offsetPosition, + behavior: 'smooth' + }); + + // Update URL hash + if (history.pushState) { + history.pushState(null, null, '#' + targetId); + } else { + location.hash = '#' + targetId; + } + } + } + // For external links, navigate normally + else if (href && !href.startsWith('#') && !href.startsWith('javascript:')) { + console.log('Navigating to external link:', href); + window.location.href = href; + } + }); + + // Replace the old link with the new one + link.parentNode.replaceChild(newLink, link); + }); + + navigationFixed = true; + + // Handle initial page load with hash + if (window.location.hash) { + // Use requestAnimationFrame for better timing + requestAnimationFrame(() => { + const targetId = window.location.hash.substring(1); + const targetElement = document.getElementById(targetId); + if (targetElement) { + const headerHeight = 60; + const elementPosition = targetElement.getBoundingClientRect().top; + const offsetPosition = elementPosition + window.pageYOffset - headerHeight; + + window.scrollTo({ + top: offsetPosition, + behavior: 'smooth' + }); + } + }); + } + } + + // Try to set up navigation fix immediately + setupNavigationFix(); + + // If it didn't work, use MutationObserver to watch for when sidebar links are added + if (!navigationFixed) { + const observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + if (mutation.type === 'childList' && mutation.addedNodes.length > 0) { + // Check if sidebar links were added + const sidebarLinks = document.querySelectorAll('.wy-menu-vertical a'); + if (sidebarLinks.length > 0) { + setupNavigationFix(); + if (navigationFixed) { + observer.disconnect(); + } + } + } + }); + }); + + // Start observing the document for changes + observer.observe(document.body, { + childList: true, + subtree: true + }); + + // Fallback timeout in case MutationObserver doesn't work + setTimeout(function() { + if (!navigationFixed) { + setupNavigationFix(); + } + observer.disconnect(); + }, 5000); + } +}); \ No newline at end of file diff --git a/verl_0720_main/verl/docs/_static/js/runllm-widget.js b/verl_0720_main/verl/docs/_static/js/runllm-widget.js new file mode 100644 index 0000000000000000000000000000000000000000..bec345cacc5b943693e1bf1973a7a6d863b0d85e --- /dev/null +++ b/verl_0720_main/verl/docs/_static/js/runllm-widget.js @@ -0,0 +1,14 @@ +document.addEventListener("DOMContentLoaded", function () { + var script = document.createElement("script"); + script.type = "module"; + script.id = "runllm-widget-script"; + script.src = "https://widget.runllm.com"; + script.setAttribute("version", "stable"); + script.setAttribute("crossorigin", "true"); + script.setAttribute("runllm-keyboard-shortcut", "Mod+j"); + script.setAttribute("runllm-name", "verl Chatbot"); + script.setAttribute("runllm-position", "TOP_RIGHT"); + script.setAttribute("runllm-assistant-id", "679"); + script.async = true; + document.head.appendChild(script); + }); \ No newline at end of file diff --git a/verl_0720_main/verl/docs/_static/logo.png b/verl_0720_main/verl/docs/_static/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..424f538ee96d0916efaf6a59dbec674e06e40148 --- /dev/null +++ b/verl_0720_main/verl/docs/_static/logo.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:fd27c16b2122527e513ea8884e0ad175f59c73af2ca1e10b1acaab38196a8638 +size 84701 diff --git a/verl_0720_main/verl/docs/advance/agent_loop.rst b/verl_0720_main/verl/docs/advance/agent_loop.rst new file mode 100644 index 0000000000000000000000000000000000000000..d5b41ff7cfdbe6864e02fbac2b1753f0fbef4271 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/agent_loop.rst @@ -0,0 +1,238 @@ +Agent Loop +========== + +Last updated: 07/17/2025. + +.. versionadded:: 0.4.2 + [status: alpha] + +.. warning:: + Agent Loop is ready for use, but the API may change in future releaes. + +Agent Loop is designed as general interface for multi-turn rollout and agentic reinforcement learning. + +**Design goal**: + +- Plugable user defined agent loop +- Provide standard request generate api with different inference frameworks +- Provide request level load balance between multiple inference servers + +**Non-goal**: + +- How tool is defined and how to call tool + +In high level overview, agent loop is given a prompt, run user defined loop: call LLM generate api, call tools, ... +and return the final output. The final output is then calculated reward and used as trajectory for RL training. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/agent_loop_overview.svg?raw=true + + +API Design +---------- + +``AgentLoopBase`` class is the abstraction of agent loop, and ``run`` method is the only interface that user need to implement. +The run method, given prompt messages in format: [{"role": "user"}, {"content": "..."}], and additional sampling params, +could do whatever user wants, such as + +- call LLM generate api +- call tools: web search, database query, code sandbox, ... +- environment interaction +- reflection +- ... + +.. code:: python + + class AgentLoopBase(ABC): + @abstractmethod + async def run(self, sampling_params: dict[str, Any], **kwargs) -> AgentLoopOutput: + """Run agent loop to interact with LLM server and environment. + + Args: + sampling_params (Dict[str, Any]): LLM sampling params. + **kwargs: dataset fields from `verl.utils.dataset.RLHFDataset`. + + Returns: + AgentLoopOutput: Agent loop output. + """ + raise NotImplementedError + +After running user defined loop, run method should return ``AgentLoopOutput``, including prompt token ids, +response token ids, and response mask. + +.. code:: python + + class AgentLoopOutput(BaseModel): + """Agent loop output.""" + + prompt_ids: list[int] + """Prompt token ids.""" + response_ids: list[int] + """Response token ids including LLM generated token, tool response token.""" + response_mask: list[int] + """Response mask, 1 for LLM generated token, 0 for tool response token.""" + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/agent_loop_output.svg?raw=true + +.. note:: AgentLoopOutput only output one trajectory for a given prompt, multiple trajectories output is still under discussion. + +Architecture Design +------------------- + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/agent_loop_architecture.png?raw=true + +A single PPO step contain two phase: rollout and train. In rollout phase: + +1. PPOTrainer sample a batch from dataset and call ``AgentLoopManager.generate_sequences``. +2. AgentLoopManager ``wake_up`` all async LLM server instances, which will sync weights between inference engine(vLLM/SGLang) and training engine(FSDP/Megatron-LM). +3. AgentLoopManager split batch into chunks and send each chunk to ``AgentLoopWorker``. +4. AgentLoopWorker receive chunk and for each prompt, spawn a user defined ``AgentLoopBase`` instance, run ``run`` coroutine until end and get ``AgentLoopOutput``. + +.. tip:: + AgentLoopWorker schedules multiple coroutines concurrently. If number of AgentLoopWorker equals batch_size, then each worker is response for one prompt. + +In agent loop, when user need LLM generate response: + +5. Call ``LLMServerClient.generate`` with prompt_ids. +6. LLMServerClient select a server instance with least request in first turn and send request to it. (In following turns, the request will be sent to the same server instance). +7. AsyncLLMServer receive a request, issue ipc/rpc with model_runner, and generate response. (There's slight differences between vLLM and SGLang, see below). + +When all prompts in all AgentLoopWorker finish, AgentLoopManager gather results and return to PPOTrainer. + +8. AgentLoopManager ``sleep`` all server instances, which will free kv cache and offload weights to CPU memory. + +AsyncLLMServer +~~~~~~~~~~~~~~ + +AsyncLLMServer is the abstraction of LLM server with two types of generation api: + +- `OpenAI chat completion `_: generate response for the given chat conversation. +- Token in token out: generate response ids for the given token ids. + +We have officially supported vLLM and SGLang AsyncLLMServer, both of them implement the two api and are well tested. +Other inference engine should be easy to plug-in by implement the ``AsyncServerBase`` class. + +.. code:: python + + class AsyncServerBase(ABC): + @abstractmethod + async def chat_completion(self, raw_request: Request) -> JSONResponse: + """OpenAI chat completion API. + + Args: + raw_request (Request): raw json request + + Returns: + JSONResponse: json response + + API reference: https://platform.openai.com/docs/api-reference/chat/create + """ + raise NotImplementedError + + @abstractmethod + async def generate(self, prompt_ids: list[int], sampling_params: dict[str, Any], request_id: str) -> list[int]: + """Generate response ids given prompt ids. + + Args: + prompt_ids (List[int]): prompt ids + sampling_params (Dict[str, Any]): sampling params + request_id (str): request id + + Returns: + List[int]: response ids + """ + raise NotImplementedError + + +Chat completion vs Token in token out +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. warning:: + The following conclusion is based on our recent experience and is still open to investigation and discussion. + +Almost all agent frameworks (LangGraph, CrewAI, LlamaIndex, etc) call LLM with OpenAI chat completion api, and +keep chat history as messages. So user may expect that we should use the chat completion api in multi-turn rollout. + +But based on our recent experience on single-turn training on DAPO and multi-turn training on `retool `_, +we found the token_ids from apply the final messages may not equal to the token_ids by concat prompt_ids and response_ids in each turn. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/multi_turn.png?raw=true + +**Where does this inconsistency happened?** + +First, the tool parser may alter the content. For example + +.. code:: json + + {"role": "assistant", "content": "Let me call a ... and get the result"} + +After tool_calls extraction, the messages is like this: + +.. code:: json + + {"role": "assistant", "content": "Let me call a and get the result", "tool_calls": [{"name": "foo", "arguments": "{}"}]} + +Encode the extracted message back is not equal to the original LLM generated response_ids. + +Second, the `decode-encode` may also lead to inconsistency: `Agent-R1 issue#30 `_. + +**What is the impact of this inconsistency?** + +This inconsistency is not a big problem for serving/agent system, but is critical to RL training. +It causes the trajectory deviate from the policy model distribution. We have observed that apply_chat_template +to the final chat history messages make PPO training not even converged in single-turn. + +vLLM +^^^^ + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/async_vllm.png?raw=true + +For vLLM, the Async LLM Engine is running in same process as the server, and ModelRunner is running in same process as FSDP/Megatron-LM workers. +Async LLM Engine communicate with ModelRunner through ZeroMQ. When server receive a request, it directly call engine to generate response_ids. + +SGLang +^^^^^^ + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/async_sglang.png?raw=true + +For SGLang, the Async LLM Engine is running in same process as FSDP/Megatron-LM worker-0, and it spawn multiple subprocesses as ModelRunner. +Also, Async LLM Engine communicate with ModelRunner through ZeroMQ. When server receive a request, it remote call the worker-0 and get response_ids. + +LLMServerClient +~~~~~~~~~~~~~~~~~~~~~ + +LLMServerClient serve as proxy to multiple AsyncLLMServer instances, provides: + +- load balance: select a server instance with least request in first turn and send request to it. +- sticky session: bind request_id to server instance, so that the same request_id will be sent to the same server instance in following turns. + +LLMServerClient is passed to ``AgentLoopBase.__init__``, whenever user want to interact with LLM in agent loop, +they can call ``LLMServerClient.generate`` to generate response_ids. + +.. code:: python + + class LLMServerClient: + async def generate( + self, + request_id, + *, + prompt_ids: list[int], + sampling_params: dict[str, Any], + ) -> list[int]: + """Generate tokens from prompt ids. + + Args: + request_id (str): request id for sticky session. + prompt_ids (List[int]): List of prompt token ids. + sampling_params (Dict[str, Any]): Sampling parameters for the chat completion. + + Returns: + List[int]: List of generated token ids. + """ + ... + +Next +---- + +- :doc:`Agentic RL Training<../start/agentic_rl>`: Quick start agentic RL training with gsm8k dataset. +- `LangGraph MathExpression `_: Demonstrate how to use LangGraph to build agent loop. +- `Retool `_: End-to-end retool paper reproduction using tool agent. diff --git a/verl_0720_main/verl/docs/advance/async-on-policy-distill.md b/verl_0720_main/verl/docs/advance/async-on-policy-distill.md new file mode 100644 index 0000000000000000000000000000000000000000..55b8d392206c94968d6ade5a29ce82eb8d267c8f --- /dev/null +++ b/verl_0720_main/verl/docs/advance/async-on-policy-distill.md @@ -0,0 +1,242 @@ +# Recipe: Async On-Policy Knowledge Distillation Trainer + +**Authors:** Brilliant Hanabi, furunding + +**Last updated:** 2025-11-08 + +## 1. Background + +On-policy knowledge distillation (KD) trains a student policy to imitate a stronger teacher using samples drawn from the student's current policy. For each on-policy rollout the teacher returns soft, top-k token distributions and the student is optimized with a token-wise sparse KL objective that focuses learning on the teacher's high-probability modes. Because training examples come from the student's own state distribution, KD reduces distributional mismatch relative to off-policy distillation or supervised fine-tuning (SFT), improving stability and sample efficiency. Compared with reinforcement learning, KD avoids high-variance reward-based optimization and complex reward design by providing dense, informative per-token targets, which typically yields faster convergence and simpler scaling. Recent empirical and implementation-focused writeups (e.g., [ThinkingMachines' blog on on-policy distillation](https://thinkingmachines.ai/blog/on-policy-distillation/)) also demonstrate that on-policy distillation can deliver high-quality behavior with substantially lower compute and data requirements than many alternative approaches. + +Built on verl’s Ray-based single-controller components, we initially assembled a strictly on-policy KD pipeline where rollout generation, teacher knowledge acquisition, and policy optimization ran in lockstep. In practice, this synchronous design proved highly inefficient: the three stages had to wait for one another, creating pipeline bubbles and underutilized GPUs. To address this, we extend the asynchronous schedulers introduced by the One-Step-Off Policy pipeline to overlap these phases. This overlap preserves the same distillation objective while trading some strict on-policy guarantees for substantial gains in end-to-end throughput and hardware utilization. + +## 2. Distillation Overview and Objective + +This recipe centers on on-policy knowledge distillation: the student policy learns from a stronger teacher on samples generated by the current policy (on-policy). For each input prompt, the student (actor) generates responses; the teacher provides top-k token distributions, and the student is trained to match them token-wise. + +Core components: + +1. Teacher signal: top-k log-probabilities and token indices per valid token position. +2. Student objective: sparse, token-level KL divergence between student logits and teacher top-k distribution. + +Objective: encourage student probabilities $Q$ to cover teacher modes $P$ using token-wise $\mathrm{KL}(P\,\|\,Q)$ computed on the teacher's top-k support. + +## 3. Efficient System Design + +### 3.1 Schedulers (One-Step / Two-Step Off-Policy) + +The native (serial) on-policy distillation process is shown in the figure below. + +![Zero-Step-Off Scheduler](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/zero-step-off-distill.png) + +This recipe supports optional schedulers that overlap generation, teacher querying, and updates to improve throughput without changing the distillation objective. + +#### 3.1.1 One-Step-Off-Policy + +![One-Step-Off Scheduler](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/one-step-off-distill.png) + +- Warm-up: 2 steps. +- Overlap pattern: rollout while actor update; weight sync while teacher retrieving. +- Timing keys: `sync_rollout_weights`, `wait_prev_gen`, `wait_prev_teacher`. + +#### 3.1.2 Two-Step-Off-Policy + +![Two-Step-Off Scheduler](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/two-step-off-distill.png) + +- Warm-up: 3 steps. +- Overlap pattern: rollout, actor update while teacher retrieving; interleave weight sync. +- Timing keys: `sync_rollout_weights`, `max(wait_prev_gen, wait_prev_prev_teacher)`. + +Tip: Use `two_step_off` when teacher takes much more time than sync; `one_step_off` for simpler overlapping. + +Practical details: + +- Inputs per batch: `teacher_topk_logps`, `teacher_topk_indices`, `attention_mask` (to select valid token positions). +- Loss injection: last pipeline stage computes KL via a logits processor; earlier stages remain unchanged. +- Optional dynamic micro-batching groups sequences by density to reduce padding overhead. + +The pipeline: + +1. Actor parameters are synchronized to a rollout worker group (nccl broadcast) with a little bit latency. +2. Rollout workers (vLLM-backed) generate sequences asynchronously (`async_generate_sequences`). +3. Teacher client service (ZeroMQ based) returns top-k log-probabilities + token indices for each sequence (batched micro-requests), enabling KL-based guidance. +4. Megatron actor performs a KL divergence computation between student logits and teacher top-k distributions (custom TP-aware kernel in `megatron_kl_loss.py`). +5. Scheduling strategies (`one_step_off_scheduler`, `two_step_off_scheduler`) can overlap phases (optional for throughput): + +### 3.2 Weights sync between actor and rollout + +We initially followed the weight synchronization path from the One-Step-Off-Policy recipe (Ray collective broadcast across all actor and rollout ranks, plus Megatron-side allgather of parameter shards). In practice this became the dominant bottleneck, so we made three changes: + +1. Batch-and-bulk load on the rollout side: instead of streaming tensors one-by-one (in one-step-off-policy recipe), we stage a bundle of parameter tensors and issue a single batched load into the rollout engine. In our setup this reduced the weight-loading time by roughly 3×. +2. Batch-and-bulk broadcast between the actor and rollout: instead of streaming tensors one-by-one (in one-step-off-policy recipe), we stage a bundle of parameter tensors and issue a single batched broadcast between the actor and rollout workers. +3. Replace allgather with gather-to-root in Megatron: parameter shards are gathered to actor rank 0 (rather than allgathered to everyone), and that root then serves as the single source for broadcasting to rollout ranks. On top of the previous change, 2 and 3 changes delivered an additional ~4× speedup in the synchronization phase. + +## 4. High-Level Data & Control Flow + +``` +Driver (TaskRunner) + ├─ Initialize Ray, tokenizer, datasets, worker groups + ├─ Build ResourcePoolManager (actor vs rollout GPU layouts) + ├─ Trainer.fit() + ├─ init_workers(): build actor + rollout groups, broadcast weight metadata, create nccl collective group + ├─ continuous_iterator(): epochs → batches + ├─ scheduler (see Section 6) + • _async_gen_next_batch(): optional weight sync + non-blocking rollout + • _async_get_teacher_knowledge(): submit teacher requests, store future + ├─ For each step: + • Sync rollout weights + • Retrieve (batch, gen_output, teacher_output) from futures + • Merge gen + teacher outputs → DataProto + • Compute metrics (response length stats, timing, throughput) + • Update actor (forward_backward_batch + KL loss + optimizer step) + • (Optional) save checkpoint +``` + +> Note: Schedulers are optional and explained later; the distillation objective is independent of how phases are overlapped. + +## 5. Key Components + +### 5.1 `OnPolicyDistillTrainer` (`ray_trainer.py`) +- Creates `GenerationBatchFuture` objects holding rollout and (later) teacher futures. +- Adds scheduling + teacher integration + modified metric emission (KL, timing, MFU). + +### 5.2 Actor Worker (Megatron) +- `OnPolicyDistillActor.update_policy()` orchestrates micro-batch forward/backward. +- KL Loss injection via `logits_processor` during forward on pipeline last stage. + +### 5.3 Rollout Worker (vLLM / SGLang) +- Pure inference mode (`init_model` builds model; no optimizer). +- `async_generate_sequences` returns a Ray future for overlapping. + +### 5.4 Teacher Service (`teacher/`) +- Proxy + worker architecture (ZMQ REQ/REP) for batched top-k retrieval. +- `TeacherClient.submit()` returns a `Future`; aggregator composes micro-batches. +- Configurable temperature, max tokens, only-response mode. + +### 5.5 KL Loss (`megatron_kl_loss.py`) +- Performs normalization & stable per-token probability construction across TP shards. +- Gradient is (student_probs - teacher_sparse_probs) scaled by upstream grad. + +## 6. Configuration Highlights (`on_policy_distill_trainer.yaml`) + +| Section | Purpose | Notable Keys | +|---------|---------|-------------| +| actor_rollout_ref.teacher | Teacher server | server_ip, server_port, n_server_workers | +| trainer | Global training control | total_epochs, save_freq, scheduler (one_step_off | two_step_off), n_gpus_per_node, nnodes | +| rollout | Resource split for rollout | n_gpus_per_node, nnodes | + +**Remember to set `trainer.n_gpus_per_node`, `trainer.nnodes`, `rollout.n_gpus_per_node` and `rollout.nnodes` to allocate GPU resources.** + +### Dynamic Batch Size + +Enable by: + +``` +actor_rollout_ref.actor.use_dynamic_bsz=True +actor_rollout_ref.actor.max_token_len=6000 # cap post-group token length +``` + +Improves utilization under variable sequence lengths. + +### Resource Guidelines + +- Actor pool: `trainer.nnodes * trainer.n_gpus_per_node` GPUs. +- Rollout pool: `rollout.nnodes * rollout.n_gpus_per_node` GPUs. +- Ensure teacher server capacity ≈ `n_server_workers` to avoid stalls (monitor `wait_prev_teacher`). + +## 7. Usage Examples + +### 7.1 Launch Teacher Server + +Before training process, you should have a teacher server to provide logp information. + +We provide a toy teacher server example with vLLM. It needs `telnet` to check proxy status, and `python` command to run. So if you have not installed `telnet`, you can just delete these code in `start_server.sh`. And some OS use `python3` rather than `python`, so you also need to modify it. Also you can change the port of teacher if you meet port conflict. + +There are 3 arguments can be set for vllm backend `--tp-size`, `--n-logprobs` and `--ckpt-path` in `start_server.sh` / `worker.py`. You should set before you start server. + +We also provide a toy multi-node teacher server. You can start the main node using `start_server.sh` and start the slave nodes using `join_server.sh`. Still remember to set args in `join_server.sh`, especially the `$PROXY_IP` and `$PROXY_BACKEND_PORT` of main node. + +When training, student will automatically use the teacher's topk (n-logprobs) to set its own topk argument at line 83 of `recipe/gkd/megatron_kl_loss.py`, so you don't need to set student's topk argument. + +```bash +cd recipe/gkd/teacher +bash start_server.sh +# Exports ports and launches proxy + worker (default vLLM backend) +``` + +Verify with: + +```bash +telnet localhost 15555 +``` + +### 7.2 Minimal Local (Megatron + vLLM) Run + +```bash +python3 -m recipe.gkd.main_gkd \ + --config-path=recipe/gkd/config \ + --config-name=on_policy_distill_trainer \ + actor_rollout_ref.model.path=/path/to/MODEL \ + data.train_files=/path/to/train.parquet \ + trainer.total_epochs=2 \ + trainer.n_gpus_per_node=4 rollout.n_gpus_per_node=2 \ + actor_rollout_ref.teacher.server_ip=127.0.0.1 \ + actor_rollout_ref.teacher.server_port=15555 \ + trainer.scheduler=one_step_off +``` + +(Requires a running teacher server). + +### 7.3 Ray Job Submission (Distilled 16B Example) + +See `run_moonlight_dsv3_training.sh` for a full script including: + +- Dist ckpt path setup (`dist_checkpointing_path`) +- Expert parallel sizing (EP / ETP) +- Dynamic batch sizing +- Two-step-off scheduling for deeper overlap. + +Submit (after adjusting paths): + +```bash +bash recipe/gkd/run_moonlight_dsv3_training.sh +``` + +## 8. Metrics & Monitoring + +Emitted metrics include (prefixes may vary): + +- Timing: `timing/wait_prev_gen`, `timing/sync_rollout_weights`, `timing/get_teacher_knowledge`, `timing/update_actor`. +- Sequence stats: `response_seq_len/*` (avg, max, min, counts). +- Performance: `perf/mfu/actor`, `perf/max_memory_allocated_gb`, `perf/cpu_memory_used_gb`. +- Distillation: `actor/kl_loss`, `actor/grad_norm`, `actor/lr`. + +Interpretation Tips: + +- High `wait_prev_teacher` → scale `n_server_workers` and allocate more teacher GPUs or reduce per-request batch size, or just use `two_step_off`. +- High `wait_prev_gen` with uniform lengths → allocate more rollout GPUs. +- High `sync_rollout_weights` → check NCCL env / network congestion and try to modify `actor_rollout_ref.rollout.update_weights_bucket_megabytes`. + +## 9. Extensibility Notes + +- Add new schedulers by following interface returning `(epoch, batch, gen_output, teacher_output, timing_dict)`. +- Integrate different distillation signals (e.g., hidden states, intermediate reasoning tokens) by extending `teacher_utils.get_teacher_knowledge` and modifying `logits_processor`. + +## 10. Functional Support Summary + +| Category | Supported | +|----------|-----------| +| Train engine | Megatron | +| Rollout engine | vLLM | +| Distillation signal | Teacher top-k logprobs & indices | +| Scheduling | one_step_off, two_step_off | + +## 11. Quick Checklist Before Running + +- Teacher server reachable (`telnet `). +- `actor_rollout_ref.model.path` contains the correct Megatron/HF config artifacts. +- `train_files` points to a parquet dataset compatible with this recipe's dataset loader. +- NCCL environment vars set (see `config/runtime_env.yaml`). + +--- +Feel free to open issues or PRs to extend scheduler variants, add new distillation objectives, or broaden engine support, and more improvement. diff --git a/verl_0720_main/verl/docs/advance/attention_implementation.rst b/verl_0720_main/verl/docs/advance/attention_implementation.rst new file mode 100644 index 0000000000000000000000000000000000000000..c068bd92115d38a86b4ba9414ae4c5e5a18a2218 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/attention_implementation.rst @@ -0,0 +1,119 @@ +.. _attention-implementation-override: + +Attention Implementation Override +================================== + +Last updated: 10/31/2025. + +By default, VERL's FSDP workers use ``flash_attention_2`` as the attention implementation for improved performance. +However, you can now override this setting to use different attention implementations based on your needs. + +Supported Attention Implementations +----------------------------------- + +The following attention implementations are supported (subject to model and hardware compatibility): + +- ``flash_attention_2``: High-performance attention implementation (default) +- ``eager``: Standard PyTorch attention implementation +- ``sdpa``: Scaled Dot-Product Attention (PyTorch native) + +When to Override +---------------- + +You might want to override the attention implementation in the following scenarios: + +- **Debugging**: Use ``eager`` for easier debugging and better error messages +- **Compatibility**: Some models or hardware configurations may not support ``flash_attention_2`` +- **Memory constraints**: Different implementations have different memory characteristics +- **Performance tuning**: Testing different implementations for optimal performance + +Configuration Examples +----------------------- + +PPO Training with Eager Attention +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +To override the attention implementation for the actor, rollout, and reference models: + +.. code:: bash + + python3 ppo_trainer.py \ + +actor_rollout_ref.model.override_config.attn_implementation=eager \ + [other parameters...] + +PPO Training with SDPA Attention +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + python3 ppo_trainer.py \ + +actor_rollout_ref.model.override_config.attn_implementation=sdpa \ + [other parameters...] + +Critic Model Override +~~~~~~~~~~~~~~~~~~~~~ + +For training configurations that include a critic model, you can also override its attention implementation: + +.. code:: bash + + python3 ppo_trainer.py \ + +actor_rollout_ref.model.override_config.attn_implementation=eager \ + +critic.model.override_config.attn_implementation=eager \ + [other parameters...] + +YAML Configuration +~~~~~~~~~~~~~~~~~~ + +You can also specify the attention implementation in your YAML configuration file: + +.. code:: yaml + + actor_rollout_ref: + model: + override_config: + attn_implementation: eager + # other overrides... + + critic: # if using a critic model + model: + override_config: + attn_implementation: eager + # other overrides... + +Important Notes +--------------- + +**Backward Compatibility**: If you don't specify ``attn_implementation`` in the override config, +VERL will continue to use ``flash_attention_2`` by default, ensuring backward compatibility with existing configurations. + +**Model Support**: Not all models support all attention implementations. Ensure your model is compatible +with the chosen attention implementation before training. + +**Performance Impact**: Different attention implementations have varying performance characteristics. +``flash_attention_2`` typically offers the best performance, while ``eager`` provides better debugging capabilities. + +**Hardware Dependencies**: Some attention implementations (like ``flash_attention_2``) may require +specific hardware or CUDA versions. If you encounter compatibility issues, try using ``eager`` or ``sdpa``. + +Troubleshooting +--------------- + +If you encounter errors when using a specific attention implementation: + +1. **Check model compatibility**: Verify that your model supports the chosen attention implementation +2. **Try eager attention**: Use ``attn_implementation=eager`` as a fallback for debugging +3. **Check hardware requirements**: Ensure your hardware supports the attention implementation +4. **Review error messages**: Attention implementation errors often provide clear guidance on supported options + +Example Error Resolution +~~~~~~~~~~~~~~~~~~~~~~~~ + +If you see an error like "flash_attention_2 is not supported", you can resolve it by switching to eager attention: + +.. code:: bash + + # Instead of the default flash_attention_2 + python3 ppo_trainer.py +actor_rollout_ref.model.override_config.attn_implementation=eager + +This override ensures your training can proceed while you investigate the flash attention compatibility issue. diff --git a/verl_0720_main/verl/docs/advance/checkpoint.rst b/verl_0720_main/verl/docs/advance/checkpoint.rst new file mode 100644 index 0000000000000000000000000000000000000000..ea66549737c17e05bebcfb8d4775580486487547 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/checkpoint.rst @@ -0,0 +1,355 @@ +.. _checkpoint-page: + +Using Checkpoints to Support Fault Tolerance Training +===================================================== + +Last updated: 04/23/2026. + +There could be training errors or machine failure during the whole RLHF training process, +so it is recommended to enable checkpoints to minimize your loss. + +The API Interface has already been listed in :ref:`config-explain-page`, +and we will not repeat them. But there are still some technique details +we hope to clarify. + +The ``checkpoint.save_contents`` / ``checkpoint.load_contents`` field accepts any combination of +``model``, ``optimizer``, ``extra`` and ``hf_model``. The semantics are aligned between FSDP and +Megatron: + +- ``model`` -- the framework-native model state. For **FSDP** this is the per-rank sharded state; + for **Megatron** this follows whether the engine provides a mbridge ``bridge`` and + ``use_dist_checkpointing``: HF weights under ``model/huggingface/`` when the HF path is active, + Megatron shards under ``model/dist_ckpt/`` when ``use_dist_checkpointing=True``, or **both** + when a ``bridge`` is present and dist shards are also enabled for the ``model`` slot. +- ``optimizer`` -- the optimizer state (sharded for both FSDP and Megatron). +- ``extra`` -- LR scheduler state, RNG states, and (for Megatron) the serialised + ``TransformerConfig``. +- ``hf_model`` -- the full model in HuggingFace format. **Megatron requires a non-``None`` mbridge + ``bridge``** (the checkpoint manager checks ``bridge``, not a separate flag) whenever ``hf_model`` + appears in ``save_contents`` or ``load_contents``. In practice the engine supplies the bridge when + mbridge is enabled (``use_mbridge=True`` in YAML). If the checkpoint also uses + ``use_dist_checkpointing=True`` for the ``model`` slot, HF export (``model/huggingface/``) is + written **in addition to** Megatron shards under ``model/dist_ckpt/``. When only the HF ``model`` + path is used (no dist shards for weights), ``model`` and ``hf_model`` refer to the same HF tree + and are deduplicated (saved once). + +.. note:: + + For FSDP, ``checkpoint.save_contents`` other than ``hf_model`` are binded together to save and + load. We recommend to include ``model``, ``optimizer`` and ``extra`` all. + +Checkpoint Saving Directory Structure +------------------------------------- + +Commonly, we use the ``default_local_dir`` declared in ``ppo_trainer.yaml`` or ``ppo_megatron_trainer.yml`` +to work as preffix when saving checkpoints, which is ``checkpoints/${trainer.project_name}/${trainer.experiment_name}``. + +So the inner checkpoint structure of **FSDP** is like: + +.. code:: + + checkpoints/${trainer.project_name}/${trainer.experiment_name} + ├── global_steps_${i} + │ ├── actor + │ │ ├── huggingface # default save config and tokenizer, save huggingface model if include ``hf_model`` in checkpoint.contents + │ │ └── fsdp_config.json # FSDP config file, including world_size and fsdp version + │ │ ├── model_world_size_{self.world_size}_rank_{self.rank}.pt + │ │ ├── optim_world_size_{self.world_size}_rank_{self.rank}.pt + │ │ └── extra_state_world_size_{self.world_size}_rank_{self.rank}.pt + │ ├── critic + │ │ ├── huggingface + │ │ └── fsdp_config.json + │ │ ├── model_world_size_{self.world_size}_rank_{self.rank}.pt + │ │ ├── optim_world_size_{self.world_size}_rank_{self.rank}.pt + │ │ └── extra_state_world_size_{self.world_size}_rank_{self.rank}.pt + └── latest_checkpointed_iteration.txt + +All model shards, optimizers and extra states are stored together, in a sharded and distributed way. + +LoRA-only Checkpoints +--------------------- + +When the model is trained with LoRA adapters, set +``checkpoint.save_lora_only = True`` to save only the adapter weights +instead of the full model state dict. This reduces checkpoint size +from ~54 GiB to ~150 MiB for a 27B model. + +- **Save**: Only parameter keys containing ``lora_`` or ``.adapter_`` are + kept; all base-model weights are excluded. +- **Load**: The checkpoint manager detects LoRA-only checkpoints + automatically by checking whether all keys are adapter keys. + ``load_state_dict(strict=False)`` is used, followed by validation + that no unexpected keys exist. +- **Backward compatibility**: Full checkpoints (all weights) are loaded + with ``strict=True`` as before; the presence of ``save_lora_only`` + does not affect them. + +The LoRA-only checkpoint is written to the same ``model_*.pt`` shard +file as a full checkpoint, so the directory layout is identical. + +While **Megatron** current checkpoint structure (layout schema v2) is: + +.. code:: + + checkpoints/${trainer.project_name}/${trainer.experiment_name} + ├── global_steps_${i} + │ ├── actor + │ │ ├── ckpt_contents.json # manifest mapping each saved content (model, optimizer, …) to its on-disk path; see "Locating saved contents" below + │ │ ├── transformer_config.json # serialised Megatron TransformerConfig (written when ``extra`` is in save_contents) + │ │ ├── model + │ │ │ ├── huggingface # HF weights + config + tokenizer (mbridge ``bridge`` + ``model`` / ``hf_model`` in save_contents) + │ │ │ └── dist_ckpt # Megatron model shards when ``use_dist_checkpointing``; PEFT adapter shards may live here with mbridge + │ │ ├── optimizer + │ │ │ └── dist_ckpt # optimizer + lr_scheduler shards (written when ``optimizer`` is in save_contents) + │ │ └── extra + │ │ └── dist_ckpt # rng_state shards (written when ``extra`` is in save_contents) + │ └── critic # same layout as actor + └── latest_checkpointed_iteration.txt + +.. note:: + + **Migrating pre-v2 checkpoints.** Older verl releases produced a flatter + layout with a single root-level ``dist_ckpt/`` directory (containing the + optimizer, rng, and optionally model shards) and a root-level + ``huggingface/`` directory. The v2 loader rejects that layout at + ``load_checkpoint`` time with a clear error. Convert an old checkpoint + in place with:: + + python scripts/migrate_megatron_checkpoint_layout.py \ + --checkpoint /path/to/global_step_N/actor + + or migrate every step under a run with + ``--checkpoint-root /path/to/run --all-steps``. The migration defaults + to hardlinking the old ``dist_ckpt`` data into the new + ``model/optimizer/extra`` subdirectories, so it is fast and does not + duplicate disk usage. + +.. tip:: + + **Locating saved contents.** Every Megatron checkpoint directory contains a + ``ckpt_contents.json`` manifest at its root. To find where a specific piece of + the checkpoint lives (HF weights, optimizer shards, tokenizer, PEFT adapters, + …), open ``ckpt_contents.json`` and look up the logical name under the + ``contents`` map — each entry has a ``path`` field (relative to the checkpoint + directory) and a ``format`` field. The manifest is written last during the + save, so its presence also indicates a fully-complete checkpoint. A typical + manifest looks like: + + .. code:: json + + { + "schema_version": 2, + "framework": "megatron", + "role": "actor", + "arch": "Qwen3ForCausalLM", + "global_step": 100, + "world_size": 8, + "backend": {"has_bridge": true, "use_dist_checkpointing": false, "peft": false}, + "save_contents": ["model", "optimizer", "extra"], + "contents": { + "model": {"path": "model/huggingface", "format": "huggingface", "backend": "mbridge"}, + "optimizer": {"path": "optimizer/dist_ckpt", "format": "megatron_dist_checkpoint"}, + "lr_scheduler": {"path": "optimizer/dist_ckpt", "format": "megatron_dist_checkpoint", "key": "lr_scheduler"}, + "rng_state": {"path": "extra/dist_ckpt", "format": "megatron_dist_checkpoint", "key": "rng_state"}, + "transformer_config": {"path": "transformer_config.json", "format": "json"}, + "hf_config": {"path": "model/huggingface", "format": "huggingface"}, + "tokenizer": {"path": "model/huggingface", "format": "huggingface"} + }, + "directories": { + "model/huggingface": "HuggingFace-format artifacts written via mbridge: model weights, config.json, …", + "optimizer/dist_ckpt": "Megatron dist_checkpointing shards for the optimizer state …", + "extra/dist_ckpt": "Megatron dist_checkpointing shards for extra state (rng_state)." + }, + "saved_any_dist_ckpt": true + } + +Megatron Checkpoint Manager Backends +------------------------------------ + +Megatron model weights are controlled by two booleans on +``actor_rollout_ref.actor.megatron`` (and the symmetric ``critic`` / ``ref`` keys): + +- **``use_mbridge`` (default ``True``)** -- when enabled, the Megatron engine builds the mbridge / + Megatron-Bridge instance passed into ``MegatronCheckpointManager`` as ``bridge``, which is + **required** for HuggingFace-format model weights under + ``global_step_${i}/${role}/model/huggingface/``. The manager itself only checks ``bridge is not + None`` for ``hf_model`` (and other HF model paths). + +- **``use_dist_checkpointing``** -- when ``True``, Megatron ``dist_checkpointing`` shards for the + ``model`` slot are written/read under ``global_step_${i}/${role}/model/dist_ckpt/``. + +The two flags are **independent**: both may be ``True`` to persist resume-friendly shards **and** +an HF tree in the same step. Setting ``use_mbridge=False`` disables HF export/load via mbridge; +``use_dist_checkpointing=False`` disables Megatron model shards for the ``model`` slot (unless +PEFT still needs adapter shards under ``model/dist_ckpt/``). + +Optimizer + LR-scheduler (``optimizer/dist_ckpt/``) and RNG state (``extra/dist_ckpt/``) always +go through ``dist_checkpointing`` into their own sibling directories. + +.. note:: + + Prefer tuning ``use_mbridge`` and ``use_dist_checkpointing`` explicitly. Avoid relying on + informal equivalences between the two; hybrid checkpoints need both enabled. + +The diagram below (from `RFC #5630 +`_) summarises how each combination of backend +and ``save_contents`` entry is resolved: + +.. image:: https://github.com/user-attachments/assets/6036822e-9d8a-4c1f-bbcc-a15dcb584c1b + :alt: Megatron checkpoint manager backend × save_contents behaviour + :align: center + +In tabular form: + ++-----------------------------+----------------+----------------------------------------------------------+ +| Flags (mbridge / dist_ckpt) | save_contents | Behaviour | ++=============================+================+==========================================================+ +| mbridge only | ``model`` | HF weights under ``model/huggingface/``. | ++-----------------------------+----------------+----------------------------------------------------------+ +| mbridge only | ``hf_model`` | Same (HF tree); ``model`` / ``hf_model`` deduplicated. | ++-----------------------------+----------------+----------------------------------------------------------+ +| mbridge only | both | Same HF checkpoint saved **once** (deduplicated). | ++-----------------------------+----------------+----------------------------------------------------------+ +| dist_ckpt only | ``model`` | Sharded weights under ``model/dist_ckpt/``. | ++-----------------------------+----------------+----------------------------------------------------------+ +| dist_ckpt only | ``hf_model`` | **Error** -- ``hf_model`` needs a ``bridge`` | +| | | (enable mbridge in engine). | ++-----------------------------+----------------+----------------------------------------------------------+ +| mbridge + dist_ckpt | ``model`` | Sharded weights under ``model/dist_ckpt/`` only (no HF | +| | | weight export unless ``hf_model`` is also listed). | ++-----------------------------+----------------+----------------------------------------------------------+ +| mbridge + dist_ckpt | ``model`` + | Megatron shards **and** HF export (two on-disk trees). | +| | ``hf_model`` | | ++-----------------------------+----------------+----------------------------------------------------------+ + +In all rows above, ``optimizer`` and ``extra`` (when listed in ``save_contents``) are saved through +``dist_checkpointing`` into their own directories -- ``optimizer/dist_ckpt/`` and +``extra/dist_ckpt/``. PEFT/LoRA adapter shards are written into ``model/dist_ckpt/`` even with +the mbridge backend (because mbridge handles only base-model weights), sitting next to the +mbridge-produced ``model/huggingface/`` tree. + +Recommended Configurations +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- **Default / production**: keep ``use_mbridge=True`` and use ``save_contents=['model', + 'optimizer', 'extra']``. The ``model/huggingface/`` folder produced by mbridge can be loaded + directly by HuggingFace Transformers without any further conversion step. +- **HuggingFace-only export**: ``save_contents=['hf_model']`` (mbridge required). Useful when + you only need a deployable HF checkpoint and not a resumable training state. +- **Pure Megatron sharded model**: ``use_mbridge=False`` and ``use_dist_checkpointing=True`` + with ``save_contents=['model', 'optimizer', 'extra']``. The model goes into ``model/dist_ckpt/``. + You can later run ``python -m verl.model_merger merge --backend megatron ...`` (see below) to + produce an HF checkpoint. + +- **Hybrid (resume + HF export)**: ``use_mbridge=True``, ``use_dist_checkpointing=True``, and + e.g. ``save_contents=['model', 'hf_model', 'optimizer', 'extra']`` to write both + ``model/dist_ckpt/`` and ``model/huggingface/`` in one step. + +Convert FSDP and Megatron Checkpoints to HuggingFace Format Model +----------------------------------------------------------------- + +We provide a tool to convert the FSDP and Megatron checkpoints to HuggingFace format model. +The tool is located in ``verl/model_merger``. For older versions of verl that don't include fsdp_config.json in checkpoints, you can use the legacy model merger located at ``verl/scripts/legacy_model_merger.py``. + +The script supports two main sub-commands: `merge` (to convert and save checkpoints) and `test` (to validate merged checkpoints against a reference model). +The arguments for the `merge` sub-command are as follows: + +.. code:: bash + + usage: python -m verl.model_merger merge [-h] --backend {fsdp,megatron} [--local_dir LOCAL_DIR] [--tie-word-embedding] [--is-value-model] [--use_cpu_initialization] [--target_dir TARGET_DIR] + [--hf_upload_path HF_UPLOAD_PATH] [--private] + + options: + -h, --help show this help message and exit + --backend {fsdp,megatron} + The backend of the model + --local_dir LOCAL_DIR + Path to the saved model checkpoints + --tie-word-embedding Whether to tie word embedding weights (currently only Megatron supported) + --is-value-model Whether the model is a value model (currently only Megatron supported) + --use_cpu_initialization + Whether to use CPU initialization for the model. This is useful for large models that cannot fit into GPU memory during initialization. + --target_dir TARGET_DIR + Directory to save the merged huggingface model + --hf_upload_path HF_UPLOAD_PATH + Hugging Face repository ID to upload the model + --private Whether to upload the model to a private Hugging Face repository + +Example usage for merging Megatron checkpoints: + +.. code:: bash + + python -m verl.model_merger merge \ + --backend megatron \ + --tie-word-embedding \ + --local_dir checkpoints/verl_megatron_gsm8k_examples/qwen2_5_0b5_megatron_saveload/global_step_1/actor \ + --target_dir /path/to/merged_hf_model + +Example usage for distributed merging Megatron checkpoints: + +.. code:: bash + + torchrun --nproc_per_node 1 --nnodes 8 --node_rank ${RANK} -m verl.model_merger merge \ + --backend megatron \ + --tie-word-embedding \ + --local_dir checkpoints/verl_megatron_gsm8k_examples/qwen2_5_0b5_megatron_saveload/global_step_1/actor \ + --target_dir /path/to/merged_hf_model + +Example usage for merging FSDP checkpoints: + +.. code:: bash + + python -m verl.model_merger merge \ + --backend fsdp \ + --local_dir checkpoints/verl_fsdp_gsm8k_examples/qwen2_5_0b5_fsdp_saveload/global_step_1/actor \ + --target_dir /path/to/merged_hf_model + + +Megatron Merger details +----------------------- + +Current implement of decoder layers uses ``nn.ModuleList`` to store the layers, +and thus the model layers on every PP rank and VPP rank starts their index from 0. + +There are 3 ways to correct this behavior: + +1. Modify the decoder layer's state_dict, add ``offset`` to each layer's index, thus rewrite ``nn.ModuleList`` implementation. +2. Modify the layer index when saving checkpoint and recover them when loading checkpoint. +3. The Checkpoint merger do this work, calculate the actual ``offset`` from ``state_dict`` only, a little complex. + +Current implementation use solution 2. + + +HuggingFace to Megatron DistCheckpoint details +---------------------------------------------- + +Through ``mbridge``, we can directly save the mcore model to huggingface format during training. +No need to convert the model to Megatron dist-checkpoint format. + +.. note:: + + Megatron provides multiple optimizer checkpoint formats controlled by: + + - ``dist_ckpt_optim_fully_reshardable``: + + - ``False`` (default, dp-reshardable): + The optimizer checkpoint supports resuming with different data parallel sizes. + This format is faster and has lower memory overhead during checkpoint saving. + + - ``True`` (fully-reshardable): + The optimizer checkpoint supports resuming with arbitrary parallelism configurations. + However, this format is slower and introduces additional memory overhead. + + - ``distrib_optim_fully_reshardable_mem_efficient``: + + When using fully-reshardable format, enabling this option switches communication + from NCCL to Gloo to reduce CUDA memory usage, at the cost of performance. + +.. warning:: + + When ``dist_ckpt_optim_fully_reshardable=True``, saving optimizer checkpoints requires + gathering optimizer states on data parallel rank 0. Although the final checkpoint is + sharded, this introduces a temporary aggregation step during saving. + + This may increase CPU memory usage and lead to OOM issues for large models. + We recommend using the default dp-reshardable format in most cases. diff --git a/verl_0720_main/verl/docs/advance/deepseek_v4_integration.rst b/verl_0720_main/verl/docs/advance/deepseek_v4_integration.rst new file mode 100644 index 0000000000000000000000000000000000000000..6cbd5083b8847a6ad4b0e76c05e0b9a5791ce902 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/deepseek_v4_integration.rst @@ -0,0 +1,139 @@ +Adding DeepSeek V4 support +========================== + +Last updated: 07/12/2026. + +This guide describes the DeepSeek-V4-Flash integration for a Megatron actor and +a vLLM rollout model. Most of the model-specific work is at two boundaries: + +* synchronizing quantized weights from Megatron to vLLM; and +* replaying expert routes between rollout, log-probability computation, and + actor updates. + +Weight synchronization is required for online training. Router replay is a +separate feature and is required only when R2 or R3 is enabled. DeepSeek V4 +also needs a few independent compatibility fixes for model construction and +fused kernels. + +Synchronizing quantized weights +------------------------------- + +DeepSeek-V4-Flash does not use a single FP8 representation for all weights: + +* Dense weights contain E4M3 FP8 values with UE8M0 scales. +* Routed MoE experts contain packed FP4 weights. +* During initial loading, vLLM converts the raw expert ``w13``, ``w2``, and + scale tensors into an MXFP4 or MegaMoE layout. This conversion can replace + or remove the original parameters. + +Megatron exports subsequent updates in the raw checkpoint layout. Those +tensors therefore cannot be copied directly into the post-processed vLLM +parameters, even if their names match. + +The update path restores the representation expected by vLLM's +``load_weights`` method before accepting an update: + +1. Restore the raw dense and expert parameters together with their + ``weight_loader`` metadata. +2. Load every weight bucket before running any non-idempotent model + post-processing. +3. Preserve UE8M0 scales in their original dtype instead of dequantizing and + requantizing them through another floating-point format. +4. Let the vLLM loaders apply tensor-parallel sharding and merge ``w1`` and + ``w3`` into ``w13``. +5. After the final bucket, rebuild the MXFP4 or MegaMoE representation once. + +Running post-processing after each bucket is incorrect because later buckets +would be loaded into parameters that an earlier post-processing pass already +transformed. The bucketed transfer path must prepare the model before the first +bucket and finalize it only after the last bucket. + +The DeepSeek V4 parameter restoration, loaders, and scale handling are in +``verl/utils/vllm/vllm_dsv4_fp8_utils.py``. The generic FP8 dispatch is in +``verl/utils/vllm/vllm_fp8_utils.py``, and the bucket-level orchestration is in +``verl/workers/rollout/vllm_rollout/utils.py``. + +Incorrect synchronization can fail immediately with an expert shape mismatch +such as ``target 1024 vs loaded 2048``. It can also complete ``load_weights`` +while leaving the expert layout or scales incorrect. In that case, the visible +symptom is a severe regression in rollout/actor log-probability correlation. +A successful load is therefore not sufficient evidence that synchronization +is correct. + +Replaying expert routes +----------------------- + +R2 records routes during actor log-probability computation and replays them +during the actor update. R3 records routes in the rollout backend and replays +them in Megatron. Neither mode is needed merely to run DeepSeek V4; they are +alignment features enabled by configuration. + +The first three routed layers in DeepSeek-V4-Flash use a hash router. Their +expert IDs come from ``input_ids`` and a token-to-expert table rather than from +learned router logits. As a result, the existing learned top-k interception +does not observe these layers. + +Supporting replay requires the Megatron path to: + +1. Pass ``input_ids`` into the decoder so the hash router has the same input as + the rollout model. +2. Record the three hash-router outputs in addition to the later learned + top-k-router outputs. +3. Preserve routed-layer order across vLLM and Megatron. + +Without the hash-router entries, vLLM reports routes for every MoE layer while +Megatron omits the first three. Every subsequent route is then replayed against +the wrong layer, even when the route tensors have compatible shapes. + +R3 also needs a causal replay mask. The mask must include rows that influence +response-token logits, not only rows marked as response tokens. When a replayed +top-k result contains duplicate expert indices, dispatcher token counts must be +derived from the resulting routing map rather than assumed to equal +``num_tokens * topk``. + +The decoder input and hash-router interception are implemented in +``verl/models/mcore/model_forward_fused.py`` and +``verl/utils/megatron/router_replay_patch.py``. R3 mask construction and route +distribution are implemented in +``verl/utils/megatron/router_replay_utils.py``. + +Model and kernel compatibility +------------------------------ + +The following requirements are independent of weight synchronization and +router replay: + +* Some Transformers releases do not register the ``deepseek_v4`` model type. + Verl uses the vLLM configuration only for that exact missing-model-type case; + unrelated configuration errors continue to propagate. +* When MTP is disabled in Verl, it must also be disabled in the Megatron Bridge + provider. The associated CSA metadata must be trimmed to match the resulting + layer count. +* The fused DSA kernel requires each local THD shard to contain at least one CSA + window. Shorter local shards must be padded before the kernel call and + unpadded afterward. +* The DeepSeek V4 decoder may return a tuple. The fused forward path accepts + that tuple without changing the tensor-only behavior used by existing + models. + +Verification +------------ + +Verify the integration in this order: + +1. Disable router replay and run at least two optimizer steps. The second + actor-to-rollout update exercises synchronization after vLLM has already + post-processed its parameters once. +2. Compare rollout and actor token log probabilities. Report Pearson + correlation together with maximum, mean, and standard-deviation + differences. A large mismatch at this stage indicates a weight, scale, or + packed-layout problem rather than a replay problem. +3. Enable the intended R2 or R3 mode and repeat the comparison. Verify that the + three hash-routed layers and all learned-router layers are recorded and + replayed in the same order. +4. Save and reload a training checkpoint containing model, optimizer, + scheduler, and trainer state. Successful weight transfer does not validate + optimizer checkpointing for mixed FP32 and quantized parameters. +5. Run with prompt and response lengths representative of the target workload. + Short smoke tests can miss sequence-packing, attention-window, and routed + expert failures. diff --git a/verl_0720_main/verl/docs/advance/delta_weight_sync.md b/verl_0720_main/verl/docs/advance/delta_weight_sync.md new file mode 100644 index 0000000000000000000000000000000000000000..a5ef45e5fe522d0d1bd207ce0fb6d4d322753341 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/delta_weight_sync.md @@ -0,0 +1,138 @@ +# Delta Weight Sync + +Last updated: 07/10/2026. + +## Motivation + +In a disaggregated setup (``hybrid_engine=False``) the trainer must broadcast its updated weights to the +rollout engine after every step. By default this is a full-weight broadcast whose cost grows with model +size. Because RL updates are highly sparse — under typical learning rates over 99% of BF16 weight bytes +are unchanged step-over-step — you can instead broadcast only the parameters that changed (a *delta*), +cutting the weight-sync traffic to the sparsity ratio while staying lossless (bit-exact; a per-flush +checksum is verified on the receiver). + +When to use: disaggregated training with a trainer↔rollout link. Two effects stack here, and they +pay off differently: + +- **Sparse wire (the "delta" part)**: only ~1–3% of parameter bytes change per step, so the + broadcast payload shrinks accordingly. This effect grows with model size and network distance — + on a fast intra-node link with a small model, a full broadcast is already cheap. +- **Shard-local diff + sparse gather (the "sharded" part)**: no rank ever materializes full + tensors or a full-model snapshot, and the gather moves only changed elements. This removes the + full-tensor all-gather and rank-0 staging costs that the plain ``nccl`` engine pays *regardless + of network speed* — which is why ``delta_sharded`` beat the full broadcast at every size we + measured (0.5B through 72B, 1.3–3.1×), not just at the large end. + +This is why ``delta_sharded`` is the only delta backend we ship: an earlier full-gather variant +(diff on a rank-0 full-model snapshot) was consistently slower than ``delta_sharded`` at every +size we measured, so it was dropped in favor of the sharded design. + +## Design + +The ``delta_sharded`` backend plugs into the standard checkpoint-engine flow (``CheckpointEngineManager`` → +``CheckpointEngineWorker``), so they work with any trainer that drives weight sync through the +checkpoint engine (including the V1 ``separate_async`` trainer). + +- **Export contract**: the trainer's ``get_per_tensor_param_shard()`` yields + ``(name, local_shard, ShardSpec)`` per local parameter — the spec (see + :mod:`verl.workers.engine.spec`) describes the shard's placement declaratively (DeviceMesh + + Placements), and the engine derives the flat offset, gather group, and contributing rank itself. + All layout knowledge stays on the trainer side; the engine is trainer-agnostic. +- **Diff**: each rank byte-diffs **its own shard** against a pinned-CPU snapshot of that shard from + the previous sync (no rank holds a full-model snapshot). The comparison is bit-exact (integer + view inequality), so the reconstruction is lossless by construction — no thresholds, no drift. +- **Sparse gather + encoding**: only the changed ``(position, value)`` pairs are gathered to rank 0 + (batched, variable-length), translated to full-tensor coordinates, and packed as a shared + ``(positions, values)`` payload plus a per-parameter manifest (``indices`` encoding: int32 + absolute positions). +- **Transport**: the sparse payload is broadcast over the existing NCCL collective group in + bucket-sized flushes (streamed: each flush is sent and freed as it is produced, so sender peak + memory stays ~2 buckets regardless of model size). +- **Apply**: each rollout worker hands its local copy of the sparse payload to its colocated SGLang + TP worker via same-GPU ``update_weights_from_tensor`` IPC, where a verl-shipped loader — + registered automatically through SGLang's stock ``--custom-weight-loader`` hook, so **no SGLang + fork or patch is needed** — verifies the flush checksum (fail loud), densifies each parameter's + delta into a NaN-masked tensor, and overwrites only the changed positions *in place* on the live + weights. No full-model mirror is staged anywhere on the rollout side: receiver peak memory is one + bucket plus one decode chunk, independent of model size. +- **Seeding**: the first sync is an explicit **dense** pass — the raw weights stream through the same + bucketed wire with no positions attached (values only), populating the trainer-side snapshot as they + go — so a dummy-initialized rollout gets a correct base without any sparse-encoding overhead. + Subsequent syncs are sparse. + +## Backend + +### ``delta_sharded`` (sharded snapshot) + +``delta_sharded`` pushes the diff *below* the all-gather: each actor rank pins a snapshot of +only **its** FSDP shard, byte-diffs the shard locally, and gathers just the changed ``(position, value)`` +pairs to rank 0 (via the engine's ``get_per_tensor_param_shard()`` export). So the gather volume drops +from the full parameter to the sparsity ratio (~1–3%), and no rank needs a full-model snapshot — the +memory and the gather traffic both shard with the world size. + +```shell + actor_rollout_ref.rollout.checkpoint_engine.backend=delta_sharded \ + +actor_rollout_ref.rollout.checkpoint_engine.engine_kwargs.delta_sharded.encoding=indices +``` + +The assembled delta is **bit-identical** to full-gather-then-diff, so the wire format, the per-flush +checksum, and the rollout-side receiver are all unchanged. Each rank computes its shard's absolute +position in the full flattened parameter purely locally (from the DTensor spec, no extra collective). + +**Supported training engines**: the shard export requires ``Shard(0)`` DTensor parameters, which both +FSDP versions provide: + +- **FSDP2** (``fully_shard``, ``actor.strategy=fsdp2``): native DTensor params; the export never stages + the whole shard on the GPU (``state_dict()`` is reference-only, shards move lazily per parameter). +- **FSDP1** (``actor.strategy=fsdp``, the default): verl configures ``SHARDED_STATE_DICT``, whose export + also emits per-rank ``Shard(0)`` DTensors. FSDP1's state-dict export runs through the unshard + machinery, so the whole-shard GPU staging round trip is kept for it (it is skipped for FSDP2). + Single-GPU FSDP1 uses ``FULL_STATE_DICT`` (plain tensors) and degrades to the replicated/rank-0 path — + still correct, just not shard-parallel. + +Other shard dimensions than ``Shard(0)`` are not supported and raise. + +> **Config note**: the training engine reads the **top-level** ``actor_rollout_ref.actor.strategy``; +> setting only ``actor.fsdp_config.strategy`` does *not* select FSDP2. + +## Measured results + +All numbers: H100 nodes, GSM8K GRPO, verl V1 ``separate_async`` (disaggregated trainer/rollout), +FSDP2 + param/optimizer offload, SGLang rollout, per-step steady-state weight sync. + +| model (placement) | ``delta_sharded`` | ``nccl`` (full broadcast) | speedup | saved / step | +|---|---|---|---|---| +| Qwen2.5-7B (1+1 nodes, sustained over 200 steps) | **3.8 s** | 9.1 s | 2.4x | 5.2 s | +| Qwen2.5-32B (2+2 nodes) | **12.5 s** | 23.2 s | 1.9x | 10.7 s | +| Qwen2.5-72B (4+4 nodes, TP8) | **12.0 s** | 36.9 s | **3.1x** | **24.9 s** | + +The delta sync time stays essentially flat from 32B to 72B -- the sharded sparse gather amortizes +over the larger trainer world -- while the full broadcast grows linearly with parameter bytes, so +the advantage widens with scale. The per-step changed ratio is stable at ~1-3% of parameter bytes +across sizes and stays there over long runs. + +Correctness evidence (details in the PR): + +- **200-step GRPO equivalence at 7B** (delta vs nccl, 400 syncs): reward trajectories track + phase-for-phase, final rewards within sampling noise, zero receiver checksum failures. +- **Bit-exact round-trip**: perturb -> apply as delta -> revert -> apply as delta reproduces + greedy generations byte-identically on every prompt. + +## Usage + +A runnable example is ``verl/experimental/one_step_off_policy/shell/grpo_0.6b_gsm8k_fsdp2_sglang_delta_sharded_2_6.sh`` — +the SGLang 2+6 disaggregated GRPO recipe with ``backend=delta_sharded``. + +Current scope: disaggregated (``hybrid_engine=False``) + SGLang rollout in BF16, FSDP1/FSDP2 training engines. +Selecting a delta backend with any other rollout engine raises ``NotImplementedError`` at worker startup; +a per-backend apply interface (vllm/trt-llm plugins) is planned. + +## Roadmap + +Planned extensions, in design order: + +- **Megatron-core trainers**: the same ``delta_sharded`` backend via a Megatron + ``get_per_tensor_param_shard`` export whose spec carries the native mcore→HF conversion as a + pure-permutation ``to_hf`` closure (implemented and validated in a stacked follow-up PR). +- **Quantized rollout (fp8 etc.)**: diff the quantized bytes (quantize-then-diff) so a low-precision + rollout engine can consume deltas without a bf16 intermediate. diff --git a/verl_0720_main/verl/docs/advance/determinism.md b/verl_0720_main/verl/docs/advance/determinism.md new file mode 100644 index 0000000000000000000000000000000000000000..d5bdce8513c97e47a21e39d50f71a9226a8fe016 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/determinism.md @@ -0,0 +1,148 @@ +# Full Determinism for Reproducible RL Training + +**Authors**: Haichuan Hu, Yongxiang Huang, Jiawei Zhang, Nguyen Long + +Last updated: 06/16/2026. + +## Overview + +By default, RL training in verl is **not** bitwise reproducible: identical configs run twice can produce different reward curves due to nondeterminism in GPU kernels, request scheduling, hash-based routing, and batch composition. The full determinism feature closes these gaps, enabling two identical runs to produce **bitwise-aligned reward curves**. + +Useful for: + +- **Debugging**: reproduce a training failure exactly, step-by-step +- **Regression testing**: verify that a code change has no silent effect on training outcomes +- **Research**: ensure fair comparison when evaluating algorithmic changes + +## Quick Start + +```yaml +actor_rollout_ref: + rollout: + full_determinism: true + seed: 42 + actor: + fsdp_config: + full_determinism: true + ref: + fsdp_config: + full_determinism: true + +reward: + reward_model: + enable: true + rollout: + full_determinism: true + seed: 42 +``` + +Or via Hydra overrides: + +```bash +python -m verl.trainer.main_ppo \ + actor_rollout_ref.rollout.full_determinism=true \ + actor_rollout_ref.rollout.seed=42 \ + actor_rollout_ref.actor.fsdp_config.full_determinism=true \ + actor_rollout_ref.ref.fsdp_config.full_determinism=true \ + reward.reward_model.enable=true \ + reward.reward_model.rollout.full_determinism=true \ + reward.reward_model.rollout.seed=42 \ + [other config overrides...] +``` + +> **Important:** `PYTHONHASHSEED` must be set **before the Python interpreter starts**. verl handles this automatically — it sets `PYTHONHASHSEED` from `rollout.seed` before `ray.init()` and propagates it to all Ray actors. Do NOT set it manually. + +## Configuration Reference + +| Parameter | Default | Scope | Description | +|-----------|---------|-------|-------------| +| `actor_rollout_ref.rollout.full_determinism` | `false` | Rollout | Enables deterministic rollout generation | +| `actor_rollout_ref.rollout.seed` | `42` | Rollout | Base seed; each replica uses `replica_rank + seed` | +| `actor_rollout_ref.actor.fsdp_config.full_determinism` | `false` | Actor | Enables deterministic PyTorch ops for actor | +| `actor_rollout_ref.ref.fsdp_config.full_determinism` | `false` | Ref model | Enables deterministic PyTorch ops for reference model | +| `reward.reward_model.rollout.full_determinism` | `false` | Reward model | Enables deterministic RM inference (forces `max_num_seqs=1`) | +| `reward.reward_model.rollout.seed` | `42` | Reward model | Base seed for RM vLLM server | + +## How It Works + +Determinism is enforced at five layers. All must be enabled for full E2E reproducibility: + +### PyTorch-level + +`enable_full_determinism(seed)` sets `PYTHONHASHSEED`, `CUBLAS_WORKSPACE_CONFIG`, `FLASH_ATTENTION_DETERMINISTIC`, seeds all RNGs, calls `torch.use_deterministic_algorithms(True, warn_only=True)`, and disables cuDNN benchmarking. Applied in all training engine implementations. + +### Environment propagation + +`main_ppo.run_ppo()` sets three env vars before `ray.init()`: + +- `PYTHONHASHSEED` — freezes Python hash() and dict ordering +- `VERL_FULL_DETERMINISM` — signals subprocesses to apply determinism +- `VLLM_BATCH_INVARIANT` — makes vLLM outputs independent of batch composition + +These are forwarded to all Ray actors via `PPO_RAY_RUNTIME_ENV`. + +### vLLM batch invariance + per-request seed + +`VLLM_BATCH_INVARIANT=1` ensures vLLM outputs don't depend on which other requests are batched together. Each `generate()` call injects `SamplingParams.seed = replica_rank + config.seed` to reset RNG per request. + +### Priority scheduling + deterministic routing + +Without determinism, request order depends on arrival timing and server selection on dict iteration order. When `full_determinism=true`: + +- Each sample gets a globally unique priority injected into the batch (`non_tensor_batch["priority"]`), so each rollout request is scheduled with a stable, distinct priority +- `SingleTurnAgentLoop` uses `request_id=f"det-{priority}"` instead of random UUID +- `GlobalRequestLoadBalancer` tie-breaking uses `hash(request_id) % len(candidates)` — deterministic with frozen `PYTHONHASHSEED` + +> **Note:** `priority` is a vLLM-only parameter. `LLMServerClient.generate()` automatically filters it for non-vLLM backends. + +### Reward model serialization + +vLLM's `/classify` endpoint (used by RM) does not support priority or batch invariance. When `full_determinism=true`, `RewardModelManager` forces `max_num_seqs=1`, serializing RM inference one request at a time. + +## Side Effects and Limitations + +**Performance**: deterministic PyTorch kernels are slower, cuDNN benchmarking is disabled, and RM `max_num_seqs=1` causes severe throughput loss. Typical E2E throughput drops 10–30% without RM; RM determinism can drop significantly more. + +**Recommendation**: Only enable for debugging, regression testing, or research. Leave disabled for production training. + +**Nondeterministic fallbacks**: Some GPU ops have no deterministic implementation. `torch.use_deterministic_algorithms(True, warn_only=True)` warns when these are encountered. + +**Backend support**: + +| Backend | Rollout | Reward model | +|---------|---------|--------------| +| vLLM | ✅ | ✅ (serialized) | +| SGLang | ❌ | ❌ | +| TensorRT-LLM | ❌ | ❌ | + +**PYTHONHASHSEED**: Must be set before process start. verl handles this automatically; manual Ray actor creation must propagate it via `runtime_env`. + +**Data parallelism**: Each replica uses `replica_rank + seed`, producing different but internally reproducible outputs. Two runs with the same config produce bitwise-aligned results. + +**Multi-turn agent not supported**: Full determinism only works for single-turn rollouts (`single_turn_agent_loop`). Multi-turn rollouts (`tool_agent_loop`) are **not** bitwise reproducible, for two reasons: + +- `tool_agent_loop` uses a random UUID per trajectory as `request_id` and does not pass `priority` to `server_manager.generate()`, so the deterministic routing and priority scheduling described above do not apply. +- Even with those added, each turn is interleaved with external tool calls whose execution time varies across runs, so the order in which requests arrive at vLLM cannot be made deterministic. This is inherent to multi-turn agentic workloads. + +For bitwise-reproducible rollouts, use `single_turn_agent_loop`. (The per-request sampling seed is still applied to multi-turn requests inside the vLLM server, but this alone is not sufficient for end-to-end reproducibility.) + +## Verifying Determinism + +Rollout determinism (bitwise reproducible vLLM generation): + +```bash +VLLM_DETERMINISM_DENSE_MODEL_PATH=${HOME}/models/Qwen/Qwen2.5-0.5B-Instruct \ +VLLM_DETERMINISM_N_GPUS=2 \ +pytest tests/workers/rollout/rollout_vllm/test_vllm_generation_determinism.py -v -s +``` + +E2E training (bitwise-aligned reward curves across two full PPO runs): + +```bash +python tests/experimental/reward_loop/run_determinism_e2e_with_rm.py \ + --policy_model ~/models/Qwen/Qwen2.5-0.5B-Instruct \ + --rm_model ~/models/Skywork/Skywork-Reward-V2-Llama-3.2-1B \ + --train_files ~/data/gsm8k/train.parquet \ + --val_files ~/data/gsm8k/test.parquet \ + --n_gpus 2 --n_steps 5 +``` diff --git a/verl_0720_main/verl/docs/advance/dpo_extension.rst b/verl_0720_main/verl/docs/advance/dpo_extension.rst new file mode 100644 index 0000000000000000000000000000000000000000..25d159cea6aa774367021358498d6f1509bf0b92 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/dpo_extension.rst @@ -0,0 +1,273 @@ +Extend to other RL(HF) algorithms +================================= + +Last updated: 02/25/2025. + +We already implemented the complete training pipeline of the PPO +algorithms. To extend to other algorithms, we analyze the high-level +principle to use verl and provide a tutorial to implement the DPO +algorithm. Users can follow the similar paradigm to extend to other RL algorithms. + +.. note:: **Key ideas**: Single process drives multi-process computation and data communication. + +Overall Approach +---------------- + +Step 1: Consider what multi-machine multi-GPU computations are needed +for each model, such as ``generate_sequence`` , ``compute_log_prob`` and +``update_policy`` in the actor_rollout model. Implement distributed +single-process-multiple-data (SPMD) computation and encapsulate them +into APIs + +Step 2: Based on different distributed scenarios, including FSDP and 3D +parallelism in Megatron-LM, implement single-process control of data +interaction among multi-process computations. + +Step 3: Utilize the encapsulated APIs to implement the control flow + +Example: Online DPO +------------------- + +We use verl to implement a simple online DPO algorithm. The algorithm +flow of Online DPO is as follows: + +1. There is a prompt (rollout) generator which has the same weight as + the actor model. After a batch of prompts are fed into the generator, + it generates N responses for each prompt. +2. Send all the prompts + responses to a verifier for scoring, which can + be reward model or a rule-based function. Then sort them in pairs to + form a training batch. +3. Use this training batch to train the actor model using DPO. During + the process, a reference policy is needed. + +Step 1: What are the multi-machine multi-GPU computations +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**Sample Generator** + +Implementation details: + +.. code:: python + + from verl.single_controller.base import Worker + from verl.single_controller.ray import RayWorkerGroup, RayClassWithInitArgs, RayResourcePool + import ray + + @ray.remote + class SampleGenerator(Worker): + def __init__(self, config): + super().__init__() + self.config = config + + def generate_sequences(self, data): + pass + +Here, ``SampleGenerator`` can be viewed as a multi-process pulled up by +``torchrun``, with each process running the same code (SPMD). +``SampleGenerator`` needs to implement a ``generate_sequences`` API for +the control flow to call. The implementation details inside can use any +inference engine including vllm, sglang and huggingface. Users can +largely reuse the code in +verl/verl/workers/rollout/vllm_rollout/vllm_rollout.py and we won't +go into details here. + +**ReferencePolicy inference** + +API: compute reference log probability + +.. code:: python + + from verl.single_controller.base import Worker + import ray + + @ray.remote + class ReferencePolicy(Worker): + def __init__(self): + super().__init__() + self.model = Model() + + def infer(self, data): + return self.model(data) + +**Actor update** + +API: Update actor model parameters + +.. code:: python + + from verl.single_controller.base import Worker + import ray + + @ray.remote + class DPOActor(Worker): + def __init__(self): + super().__init__() + self.model = Model() + self.model = FSDP(self.model) # or other distributed strategy + self.optimizer = optim.Adam(self.model.parameters(), lr=1e-3) + self.loss_fn = xxx + + def update(self, data): + self.optimizer.zero_grad() + logits = self.model(data) + loss = self.loss_fn(logits) + loss.backward() + self.optimizer.step() + +**Notes: How to distinguish between control processes and distributed computation processes** +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Control processes are generally functions directly decorated with + ``@ray.remote`` +- Computation processes are all wrapped into a ``RayWorkerGroup``. + +Users can reuse most of the distribtued computation logics implemented +in PPO algorithm, including FSDP and Megatron-LM backend in +verl/verl/trainer/ppo. + +Step 2: Based on different distributed scenarios, implement single-process control of multi-process data interaction +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +**The core problem to solve here is how a single process sends data to +multiple processes, drives multi-process computation, and how the +control process obtains the results of multi-process computation.** +First, we initialize the multi-process ``WorkerGroup`` in the control +process. + +.. code:: python + + @ray.remote(num_cpus=1) + def main_task(config): + # construct SampleGenerator + resource_pool = RayResourcePool(process_on_nodes=[8] * 2) # 16 GPUs + ray_cls = RayClassWithInitArgs(SampleGenerator, config=config) + # put SampleGenerator onto resource pool + worker_group = RayWorkerGroup(resource_pool, ray_cls) + + # construct reference policy + +As we can see, in the control process, multiple processes are wrapped +into a ``RayWorkerGroup``. Inside this ``WorkerGroup``, there is a +``self._workers`` member, where each worker is a RayActor +(https://docs.ray.io/en/latest/ray-core/actors.html) of SampleGenerator. +ray_trainer.md also provide an implementation of +``MegatronRayWorkerGroup``. + +Assuming the model is distributed using FSDP, and there is a batch of +data on the control process, for data parallelism, the underlying +calling process is: + +.. code:: python + + data = xxx + data_list = data.chunk(dp_size) + + output = [] + for d in data_list: + # worker_group._workers[i] is a SampleGenerator + output.append(worker_group._workers[i].generate_sequences.remote(d)) + + output = ray.get(output) + output = torch.cat(output) + +Single process calling multiple processes involves the following 3 +steps: + +1. Split the data into DP parts on the control process. +2. Send the data to remote, call the remote computation through RPC, and + utilize multi-process computation. +3. Obtain the computation results of each worker on the control process + and merge them. + +Frequently calling these 3 steps on the controller process greatly hurts +code readability. **In verl, we have abstracted and encapsulated these 3 +steps, so that the worker's method + dispatch + collect can be +registered into the worker_group** + +.. code:: python + + from verl.single_controller.base.decorator import register + + def dispatch_data(worker_group, data): + return data.chunk(worker_group.world_size) + + def collect_data(worker_group, data): + return torch.cat(data) + + dispatch_mode = { + 'dispatch_fn': dispatch_data, + 'collect_fn': collect_data + } + + @register(dispatch_mode=dispatch_mode) + def generate_sequences(self, data): + pass + +In this way, we can directly call the method inside the worker through +the ``worker_group`` on the control (driver) process (which is a single +process): + +.. code:: python + + output = worker_group.generate_sequences(data) + +This single line includes data splitting, data distribution and +computation, and data collection. + +Furthermore, the model parallelism size of each model is usually fixed, +including dp, tp, pp. So for these common distributed scenarios, we have +pre-implemented specific dispatch and collect methods,in `decorator.py `_, which can be directly used to wrap the computations. + +.. code:: python + + from verl.single_controller.base.decorator import register, Dispatch + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def generate_sequences(self, data: DataProto) -> DataProto: + pass + +Here it requires the data interface to be ``DataProto``. Definition of +``DataProto`` is in `protocol.py `_. + +Step 3: Main training loop +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +With the above training flows, we can implement the algorithm's control +flow. It is recommended that ``main_task`` is also a ray remote process. + +.. code:: python + + @ray.remote(num_cpus=1) + def main_task(config): + # construct SampleGenerator + resource_pool = RayResourcePool(process_on_nodes=[8] * 2) # 16 GPUs + ray_cls = RayClassWithInitArgs(SampleGenerator, config=config) + # put SampleGenerator onto resource pool + sample_gen = RayWorkerGroup(resource_pool, ray_cls) + + # construct reference policy + ray_cls = RayClassWithInitArgs(ReferencePolicy) + ref_policy = RayWorkerGroup(resource_pool, ray_cls) + + # construct actor + ray_cls = RayClassWithInitArgs(DPOActor) + dpo_policy = RayWorkerGroup(resource_pool, ray_cls) + + dataloader = DataLoader() + + for data in dataloader: + # generate data + data = sample_gen.generate_sequences(data) + # generate scores for each data + data = generate_scores(data) + # generate pairwise data using scores + data = generate_pairwise_data(data) + # generate ref_log_prob + data.batch['ref_log_prob'] = ref_policy.infer(data) + # update using dpo + dpo_policy.update(data) + # logging + +Here, different ``WorkerGroups`` can be placed in the same resource pool or +in different resource pools using ``create_colocated_worker_cls`` +similar as in `ray_trainer.py `_. diff --git a/verl_0720_main/verl/docs/advance/dynamic_schedule.md b/verl_0720_main/verl/docs/advance/dynamic_schedule.md new file mode 100644 index 0000000000000000000000000000000000000000..f1e1bffd6a6c03b88235d223f972041e64daccd6 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/dynamic_schedule.md @@ -0,0 +1,509 @@ +# Dynamic Resource Scheduling for Fully-Async Training + +**Author:** `https://github.com/meituan-search` + +Last updated: 07/10/2026. + +This module provides **hybrid inference resource dynamic scheduling** for the fully-async training framework, enabling Trainer-node GPUs to participate in rollout generation during idle periods and thus improving overall GPU utilization. + +> **At a glance** +> - Two kinds of inference replicas: **Standalone** (dedicated rollout nodes, always on) + **Hybrid** (share Trainer-node GPUs; activated during training idle time, weights offloaded and memory returned before each training step). +> - A pluggable **Policy** decides when to activate/deactivate Hybrid replicas; `DynamicResourceController` owns the lifecycle (state machine `STANDALONE_ONLY <-> HYBRID_ACTIVE`). +> - When Standalone capacity is sufficient, Hybrid skips rollout entirely and stays focused on training, avoiding needless mode-switching; weight sync to Hybrid replicas can also be skipped, saving communication. +> - Measured (Qwen3.5-35B-A3B / DAPO-Math-17K): ~**15.3%** faster end-to-end training time, with a reward curve identical to baseline. + +--- + +## 1. Overview + +### Problem + +In the fully-async separated architecture, Trainer-node GPUs sit idle while waiting for rollout data, and Standalone Rollout nodes wait during training. This leads to suboptimal GPU utilization on both sides. + +### Solution + +A **Hybrid + Standalone dual-mode inference resource** design: + +- **Standalone replicas**: Always-on inference replicas on dedicated rollout nodes. +- **Hybrid replicas**: Inference replicas that share Trainer-node GPUs. They are activated during training idle time; before each training step, weights are offloaded and GPU memory is returned to the training engine. + +![Dynamic Resource Architecture](https://github.com/zpltys/Blob/blob/main/dynamic_resource.png?raw=true) + +The figure above illustrates the **resource-time layout** of the dynamic scheduling system across three consecutive training steps. The vertical axis shows GPU resources split into two pools: + +- **Standalone Resource** (top, GPU 0–n): Dedicated rollout GPUs that **continuously perform rollout** across all steps — they are always active during the *Rollout* phase (blue) and idle during *Trainer* / *Weight Sync*. +- **Hybrid Resource** (bottom, GPU n+1–n+m): Trainer-node GPUs that **switch between rollout and training**. When in *Rollout* mode they join the Rollout LoadBalancer to generate samples (blue); when in *Trainer* mode they run PPO mini-batch updates (yellow); at each *Weight Sync* boundary the latest weights are broadcast to all replicas. + +The horizontal axis shows three steps with distinct Hybrid behaviours: + +- **Step 1 & 2**: At the start of each step, the MessageQueue does not yet have enough samples. Hybrid resources activate into *Rollout* mode to help generate samples alongside Standalone resources, then switch back to *Trainer* once sufficient samples are buffered. +- **Step 3**: Before this step begins, the MessageQueue **already contains enough samples** (buffered from previous steps). Hybrid resources skip Rollout entirely and go directly into *Trainer* mode — this is the key optimisation: when Standalone capacity is sufficient, Trainer GPUs stay focused on training without unnecessary context-switching overhead. + +The transition threshold is controlled by `dynamic_schedule_deactivate_ratio`: the controller deactivates Hybrid replicas once `deactivate_ratio × required_samples × trigger_parameter_sync_step` samples have been collected. A central *Rollout LoadBalancer* dispatches generation requests from the *MessageQueue* across all active replicas. + +Two request-handling mechanisms ensure smooth transitions between modes: + +- **Hybrid → Trainer (deactivation)**: When Hybrid resources switch from Rollout back to Trainer, any **in-flight requests** running on them are aborted and automatically **redistributed by the LoadBalancer to Standalone resources**, which continue the rollout. This is transparent to upper layers thanks to the retry mechanism in `FullyAsyncLLMServerClient`. +- **Trainer → Hybrid (activation)**: After a training step ends, if Hybrid resources switch back into Rollout mode, the `dynamic_schedule_enable_rebalance` parameter controls whether to perform a **reshuffle**: when enabled, the controller clears the LoadBalancer's sticky-session cache and aborts in-flight requests across all active replicas, then resumes them so requests are redistributed via least-loaded routing — naturally balancing load toward the newly activated Hybrid replicas (which start with 0 in-flight requests). + +- **Weight Sync (parameter synchronisation)**: During the *Weight Sync* phase, weights are first broadcast from Trainer to all **Standalone** rollout replicas (always required). The policy's `should_activate_after_step()` is then evaluated to decide whether Hybrid resources should enter Rollout mode for the next step. If activation is needed, weights are **additionally synced to Hybrid replicas**; otherwise this second sync is **skipped entirely**, saving significant communication overhead — this is exactly what happens in Step 3 of the figure above. + +`DynamicResourceController` manages the lifecycle of hybrid replicas. A pluggable **Policy** decides when to activate and deactivate: + +``` +State machine: STANDALONE_ONLY <-> HYBRID_ACTIVE + +Activate (after weight sync): + 1. add_replicas — register hybrid replicas in the load balancer + 2. resume_generation_replicas — allow hybrid replicas to accept requests + +Deactivate (order is critical): + 1. remove_replicas — cut routing first; prevents retry loop re-routing to dying replicas + 2. abort_replicas — abort in-flight requests; partial-rollout retries go to standalone + 3. sleep_replicas — release KV cache + offload weights, return GPU to training engine +``` + +### Policy Call Order Per Training Step + +``` +1. should_deactivate() — before training; decide whether to deactivate hybrid replicas +2. deactivate_wait_samples() — if (1) is True; return the minimum buffered-sample threshold +3. should_activate_after_step() — after weight sync; decide whether to (re-)activate hybrid replicas +4. request_rebalance() — after activation; redistribute requests across replicas (if enabled) +5. update_after_step() — after weight sync; update policy internal state +``` + +--- + +## 2. Configuration Parameters + +All dynamic scheduling parameters live under the `async_training` section of your training config +(`fully_async_ppo_trainer.yaml` or `fully_async_ppo_megatron_trainer.yaml`): + +### Core Parameters + +| Parameter | Type | Default | Description | +|-----------|------|---------|-------------| +| `use_dynamic_resource_scheduling` | bool | `False` | Master switch. When `True`, hybrid rollout replicas are initialised on Trainer-node GPUs at startup (sleeping, memory returned to the training engine). | +| `dynamic_schedule_policy` | str | `"default"` | Name of the scheduling policy (`"default"`, `"static_fully_async"`, `"fixed_ratio"`, or a custom registered name). | +| `dynamic_schedule_deactivate_ratio` | float | `0.3` | Sample-collection ratio threshold. The controller waits until `deactivate_ratio × required_samples × trigger_parameter_sync_step` samples are buffered before deactivating. Lower → earlier deactivation; `1.0` → wait for a full batch. | +| `dynamic_schedule_enable_rebalance` | bool | `True` | Whether to rebalance (abort + clear sticky cache + resume) in-flight requests across all active replicas after hybrid activation, via least-loaded routing. | + +### Existing Async-Training Parameters (used by dynamic scheduling) + +| Parameter | Type | Default | Description | +|-----------|------|---------|-------------| +| `staleness_threshold` | float | `0.1` | Allowed sample staleness ratio; affects `buffer_samples = expected × staleness_threshold`. | +| `trigger_parameter_sync_step` | int | `4` | Number of collections per weight-sync step; used in the deactivate wait formula above. | +| `require_batches` | int | `1` | Number of ppo_mini_batches per collection; determines `required_samples`. | + + +### Rollout Resource Config (under `actor_rollout_ref.rollout`) + +| Parameter | Description | +|-----------|-------------| +| `gpu_memory_utilization` | Memory utilization for **hybrid** replicas sharing GPU with training. Keep low (e.g. 0.3–0.5). | +| `standalone_gpu_memory_utilization` | Memory utilization for **standalone** replicas on dedicated rollout nodes (e.g. 0.6–0.8). Falls back to `gpu_memory_utilization` if `null`. | + +### Standalone Node Config (under `rollout`) + +| Parameter | Description | +|-----------|-------------| +| `rollout.nnodes` | Number of standalone rollout nodes. Set to `0` for pure colocated mode (hybrid only, no dedicated rollout nodes). | + +--- + +## 3. Built-in Policies + +### 3.1 `default` — DefaultDynamicSchedulePolicy + +**File:** `verl/experimental/fully_async_policy/dynamic_schedule/default_policy.py` + +The recommended policy with **adaptive deactivate_ratio** and a **cost/benefit activation gate**: + +| Method | Behaviour | +|--------|-----------| +| `should_deactivate()` | Returns `is_hybrid_active` (deactivate whenever active) | +| `deactivate_wait_samples()` | Returns `deactivate_ratio × required_samples × trigger_parameter_sync_step` | +| `should_activate_after_step()` | Activates when generated samples fall behind expectation (a positive gap exists) **and** the estimated wall-clock saved exceeds the switch (activate+deactivate) cost | +| `update_after_step()` | Adapts `deactivate_ratio` from the `step_wait_samples` signal (see below); disabled when `only_hybrid=True` | +| `request_rebalance()` | Clears sticky cache + aborts in-flight requests + resumes, so requests are redistributed via least-loaded routing | + +**`update_after_step()` adaptation logic** — keyed off `step_wait_samples` (the count of samples that actually had to be waited on for generation, excluding samples served instantly from queue backlog): + +- This cycle had real waits (`total_wait_samples > 0`, rollout is the bottleneck): `ratio += clip(total_wait_samples / step_required_samples, 0.02, 0.1)` — the larger the shortfall fraction, the bigger the step (bounded), biasing toward deactivating **later**. +- No real waits this cycle (training is the bottleneck): `ratio -= 0.02`, biasing toward deactivating **earlier**. + +**Cost/benefit gate** (`should_activate_after_step`): re-activating hybrid replicas costs one activate + one (future) deactivate cycle — `switch_cost`, the rolling average of the last 3 measured activate+deactivate durations (falls back to 10 s with no history). Activation only proceeds when the standalone-only generation shortfall, converted to wall-clock via a per-sample-time estimate (`total_wait / total_wait_samples`), exceeds `switch_cost`. Before any real generation-rate signal has been observed, the per-sample-time estimate defaults to a deliberately pessimistic 1000 s, biasing toward activation until real timing data narrows it. + +**Use case:** Hybrid + Standalone mixed deployment targeting maximum GPU utilization. + +### 3.2 `static_fully_async` — StaticFullyAsyncPolicy + +**File:** `verl/experimental/fully_async_policy/dynamic_schedule/static_fully_async_policy.py` + +Equivalent to the original fully-async strategy, designed for baseline comparisons or colocated fallback: + +| Method | Behaviour | +|--------|-----------| +| `should_deactivate()` | Returns `is_hybrid_active` (deactivate whenever active) | +| `deactivate_wait_samples()` | **Always returns `0`** (deactivate immediately, no waiting) | +| `should_activate_after_step()` | **Always returns `False`** (never re-activate after weight sync) | +| `update_after_step()` | No-op | +| `request_rebalance()` | No-op (inherits base default) | + +**Key properties:** + +1. **Equivalent to standard Fully-Async**: Hybrid replicas are deactivated immediately at each training step start and never re-activated. Trainer GPUs are always 100% returned to training — same behaviour as running without `use_dynamic_resource_scheduling`. +2. **Colocated fallback**: When `rollout.nnodes=0`, `only_hybrid=True` and the system runs in classic colocated mode (training + inference share the same GPUs, no separate rollout nodes). + +### 3.3 `fixed_ratio` — FixedRatioDynamicSchedulePolicy + +**File:** `verl/experimental/fully_async_policy/dynamic_schedule/fixed_ratio_policy.py` + +Identical to `default` except that `update_after_step()` is a no-op — `deactivate_ratio` stays at its initial value throughout training, with no adaptation. `should_activate_after_step()` is also simplified to a pure gap check (no cost/benefit gate). Useful for fixed-ratio ablation experiments. + +> **Registration note:** `fixed_ratio` is **not** imported in `__init__.py`, so `@register_policy` does not fire on a default import. Add `from .fixed_ratio_policy import FixedRatioDynamicSchedulePolicy` to `verl/experimental/fully_async_policy/dynamic_schedule/__init__.py`, or import it manually in your entry script, before referencing it in config. + +--- + +## 4. Monitoring Metrics + +Dynamic resource scheduling emits a set of `dynamic_resource/*` metrics (in addition to the general +`fully_async/*` metrics documented in [`fully_async.md`](./fully_async.md)) +to help quantify how effectively Hybrid and Standalone GPUs are utilized. This section gives the +formal definition of each metric. + +### Notation + +| Symbol | Meaning | +|--------|---------| +| $a$ | `hybrid_gpus` = `trainer.nnodes × trainer.n_gpus_per_node` (Trainer-node GPUs that can switch between rollout/train) | +| $b$ | `standalone_gpus` = `rollout.nnodes × rollout.n_gpus_per_node` (dedicated rollout-node GPUs) | +| $\text{compute}_i$ | Training-GPU compute time of micro fit-step $i$ (sum of `timing_raw` keys: `reward`, `old_log_prob`, `ref`, `values`, `adv`, `update_critic`, `update_actor`) | +| $\text{alloc}_i$ | Allocated wall-clock time of micro fit-step $i$ (from `_fit_generate()` through `_fit_dump_data()`, including `param_sync` and activation overhead) | +| $\text{cap}_k$ | Concurrency capacity of rollout interval $k$ = $\min(\text{servers}_k \times s,\ \text{maxReq})$ | +| $\text{active}_k$ | Active task count during rollout interval $k$ | +| sync cycle | The window between two consecutive parameter-sync events (i.e. between two `reset_staleness()` calls / every `trigger_parameter_sync_step` collections), possibly spanning multiple Trainer micro fit-steps (including partial-rollout resumption) | + +### 4.1 `dynamic_resource/train_resource_utilization` + +Fraction of the Trainer's "training turn" wall-clock time that is spent on actual training-GPU +compute, aggregated over an entire sync cycle. + +For each micro fit-step $i = 1, \dots, n$ within the cycle: + +- **Numerator** ($\text{compute}_i$): sum of the `timing_raw` training-compute keys + (`reward`, `old_log_prob`, `ref`, `values`, `adv`, `update_critic`, `update_actor`). + — `ref` is the timing key recorded via `marked_timer(str(Role.RefPolicy), …)` (the enum's + string value is `"ref"`, not `"RefPolicy"`). Missing keys — e.g. `values`/`update_critic` + when `use_critic=False` — default to 0. +- **Denominator** ($\text{alloc}_i$): wall-clock time from `_fit_generate()` through + `_fit_update_weights()` and `_fit_dump_data()` (includes `timing_s/param_sync` and any hybrid + activation time; these are intentionally **not** subtracted) + +Both quantities are summed across all micro-steps in the cycle first, and the ratio is computed +once from the summed totals (not averaged per micro-step): + +$$ +U_{\text{train}} = \frac{\sum_{i=1}^{n} \text{compute}_i}{\sum_{i=1}^{n} \text{alloc}_i} +$$ + +The un-ratioed numerator and denominator are also emitted directly as the raw metrics +`dynamic_resource/train_compute_time_s` and `dynamic_resource/train_allocated_time_s` +(aggregated by **sum** across the cycle's micro-steps), so the ratio is computed once from the +summed totals by `MetricsAggregator` rather than averaged per micro-step. + +### 4.2 `dynamic_resource/rollout_resource_utilization` + +Fraction of rollout concurrency capacity actually used during the Rollouter's step (the window +between the previous and current `reset_staleness()` call). + +The Rollouter records an event-driven history of `(len(active_tasks), max_concurrent_samples)` +tuples — appended every time a sample is submitted, completes, or is drained during a pause, +**and** whenever `max_concurrent_samples` itself changes (i.e. a replica is +activated/deactivated under dynamic resource scheduling). This gives timestamps +$t_0 < t_1 < \dots < t_m$ with two quantities held constant over each interval $[t_k, t_{k+1})$: +the active-task count $\text{active}_k$ and the concurrency capacity $\text{cap}_k$ +($t_0$ is the previous step's end / this step's start; $t_m$ is "now", appended when +`reset_staleness()` is called, closing out the window including any trailing drain/idle time). + +Recording the capacity alongside the active count per interval is essential because under dynamic +resource scheduling $\text{cap}_k$ is **not** constant over a step window — replicas can be +activated/deactivated mid-window. Using a single end-of-window capacity for the whole window +would misattribute utilization for intervals that had a different capacity, so each interval uses +its own $\text{cap}_k$. + +The capacity for each interval is $\text{cap}_k = \min(\text{servers}_k \times s,\ \text{maxReq})$, +where $\text{servers}_k$ is `get_active_server_count()` at interval $k$, $s$ is +`concurrent_samples_per_replica`, and $\text{maxReq}$ is `max_required_samples`: + +$$ +\text{cap}_k = \min(\text{servers}_k \times s,\ \text{maxReq}) +$$ + +Each interval's effective load is $\min(\text{cap}_k, \text{active}_k)$ (the capacity +$\text{cap}_k$ is already clamped, so the actual served concurrency is the smaller of capacity +and active tasks). Weighting each interval by its capacity-time (so a higher-capacity interval +counts proportionally more): + +$$ +U_{\text{rollout}} = \frac{\sum_k (t_{k+1} - t_k) \cdot \min(\text{cap}_k, \text{active}_k)}{\sum_k (t_{k+1} - t_k) \cdot \text{cap}_k} +$$ + +Returns `0.0` if there is no time span to integrate over (e.g. total capacity-time weight is 0; +intervals with $\text{cap}_k = 0$ contribute zero weight and are skipped). + +### 4.3 `dynamic_resource/resource_utilization` + +Cluster-wide GPU utilization for the sync cycle, combining the Hybrid GPUs' time-split between +rollout and train with the Standalone GPUs (which spend 100% of their time on rollout). + +Let $x \in [0, 1]$ be the fraction of the cycle's wall-clock time that Hybrid GPUs spent doing +rollout (the rest, $1-x$, they spent training), estimated as the ratio of summed +"wait for enough samples" time (Hybrid-rollout wall-clock time, only recorded when dynamic +resource scheduling deactivates Hybrid replicas this cycle) over the summed step time: + +$$ +x = \mathrm{clip}\left(\frac{\text{wait}}{\text{step}},\ 0,\ 1\right) +$$ + +where $\text{wait}$ is the summed `timing_s/wait_for_enough_samples` and $\text{step}$ is the +summed `timing_s/step`. When dynamic resource scheduling never switches Hybrid GPUs into rollout +mode this cycle (e.g. disabled, or `wait_for_enough_samples` was skipped), $x = 0$ (100% of +Hybrid time is training). + +Weighting each utilization by the GPU-seconds it was measured over — Hybrid GPUs spend +$(1-x) \cdot a$ GPU-time training at $U_{\text{train}}$ and $x \cdot a$ GPU-time doing +rollout at $U_{\text{rollout}}$; Standalone GPUs ($b$ of them) spend all their time doing +rollout at $U_{\text{rollout}}$: + +$$ +U_{\text{cluster}} = \frac{(1-x) \cdot a \cdot U_{\text{train}} + (x \cdot a + b) \cdot U_{\text{rollout}}}{a + b} +$$ + +### 4.4 `dynamic_resource/mq_size` + +A snapshot (not an average) of the number of samples remaining in the MessageQueue for subsequent +steps, taken right after a Trainer `fit_step()` (including any partial-rollout resumption) finishes. +If we denote this snapshot as $Q$, then $Q$ equals the value returned by +`message_queue_client.get_queue_size_sync()`. Reported as the last value observed in the cycle +rather than an average across the cycle's +micro-steps. + +### 4.5 Related rollouter-side metric: `fully_async/rollouter/step_generated_samples` + +Number of samples fully generated by the Rollouter during the current param version (reset to 0 at +each `reset_staleness()` call). It is returned in the rollouter's `timing_raw` alongside +`dynamic_resource/rollout_resource_utilization` and is the underlying signal the Rollouter uses to +track per-step throughput; it is documented here because it is computed in the same code path as +the `dynamic_resource/*` metrics above. + +### Caveats + +- **`rollout_resource_utilization` per-interval capacity**: under dynamic resource scheduling the + capacity $S_k$ can change mid-window as replicas are activated/deactivated; each interval uses + its own $S_k$ rather than a single end-of-window value, so capacity changes are reflected + faithfully. Intervals recorded before `max_concurrent_samples` is first known (a single seed + point at init with placeholder capacity 0) are corrected once the real capacity is available. +- **`resource_utilization` assumes `should_deactivate() == is_hybrid_active`**: the $x$ estimate + relies on `timing_s/wait_for_enough_samples` being a faithful proxy for "Hybrid GPUs are in + rollout mode this cycle", which holds for the built-in `default`/`static_fully_async` policies + but may not hold for custom policies with different activation semantics. +- **`train_resource_utilization` and `rollout_resource_utilization` are not perfectly comparable**: + the former's denominator includes `param_sync`/activation overhead (not subtracted), while the + latter's denominator is pure rollout window time — keep this in mind when interpreting the + combined `resource_utilization`. + +--- + +## 5. Experimental Results + +### Benchmark: Qwen3.5-35B-A3B on DAPO-Math-17K + +| Item | Config | +|------|--------| +| Model | Qwen3.5-35B-A3B | +| Dataset | DAPO-Math-17k (train) / AIME-2024 (val) | +| Backend | Megatron (TP=4, PP=2, EP=8) | +| Hardware | H20 (8 GPUs/node) | +| Script | `verl/experimental/fully_async_policy/shell/run_qwen35_35b_a3b_math_dynamic_megatron.sh` | +| Key hyperparams | `dynamic_schedule_policy="default"`, `dynamic_schedule_deactivate_ratio=0.6`, `dynamic_schedule_enable_rebalance=True`, `staleness_threshold=0.5`, `trigger_parameter_sync_step=4`; hybrid `gpu_memory_utilization=0.45`, standalone `standalone_gpu_memory_utilization=0.7` | + +**Baseline 1**: 16 GPU training + 16 GPU rollout (2 dedicated trainer nodes + 2 rollout nodes). +**Baseline 2**: 8 GPU training + 24 GPU rollout (1 dedicated trainer nodes + 3 rollout nodes). +**Dynamic scheduling**: 16 GPU training (2 nodes) + 16 GPU rollout (2 node), with Hybrid replicas on Trainer GPUs. + +#### Result: 15.3% Faster End-to-End Training Time + +Dynamic resource scheduling reduces total wall-clock training time by **~15.3%** compared to the 8+24 baseline, and **~17.5%** compared to the 16+16 baseline, while producing an **identical reward curve** — confirming that the training quality is not compromised by the time-sliced GPU sharing. + +**Reward curve** (dynamic scheduling vs baseline): + +
+ +
+ +Both curves overlap closely throughout training, demonstrating that dynamic scheduling introduces no regression in model quality. + +**Per-step runtime comparison**: + +
+ +
+ +The per-step runtime plot shows that dynamic scheduling reduces step latency by leveraging Trainer GPUs during rollout idle windows, with the gap widening as training progresses and the policy adapts the `deactivate_ratio`. + +#### Resource Utilization Breakdown + +The three static configurations differ in their **training/inference load balance**, which the metrics below make explicit. The key intuition: `dynamic_resource/mq_size` (the number of samples buffered in the MessageQueue after each `fit_step()`) directly reveals whether the bottleneck is on the production side (rollout) or the consumption side (trainer). + +- **16-16-static** (16 GPU train + 16 GPU rollout): the trainer consumes samples faster than the 16-GPU rollout can produce them, so the **bottleneck is on the production side** — the queue is constantly drained to near zero and the trainer is left waiting for new samples. +- **24-8-static** (8 GPU train + 24 GPU rollout): the 24-GPU rollout produces samples faster than the 8-GPU trainer can consume them, so the **bottleneck is on the consumption (training) side** — samples pile up and the queue sits at its capacity limit. +- **dynamic**: by reallocating Trainer-node GPUs between rollout and training based on real-time queue depth, the controller keeps production and consumption matched — the queue stays at a moderate level, avoiding both the trainer starvation of 16-16 and the sample buildup of 24-8. + +**MessageQueue size** (`dynamic_resource/mq_size`): + +
+ +
+ +The 16-16-static curve hugs zero (production-bound, trainer starves); the 24-8-static curve sits at the capacity ceiling (consumption-bound, samples back up); the dynamic curve stays in between, holding the generation/consumption balance. + +**Cluster-wide GPU utilization** (`dynamic_resource/resource_utilization`): + +
+ +
+ +16-16-static wastes training-side GPU time waiting for samples; 24-8-static wastes rollout-side GPU time over-producing unconsumed samples. Dynamic scheduling keeps both sides' GPU time on useful work, yielding the highest cluster-wide utilization. + +**Rollout-side utilization** (`dynamic_resource/rollout_resource_utilization`): + +
+ +
+ +16-16-static (production-bound) keeps its 16 rollout GPUs pinned near capacity; 24-8-static (production-rich) leaves much of its 24-GPU rollout capacity idle since the trainer cannot keep up. Dynamic scheduling holds rollout utilization at a high, stable level — neither bottlenecked like 16-16 nor idle like 24-8. + +**Train-side utilization** (`dynamic_resource/train_resource_utilization`): + +
+ +
+ +24-8-static has samples in abundance, so the trainer never waits and achieves the highest train-side utilization. 16-16-static's trainer spends much of its turn waiting for samples (the wait is inside the metric's denominator, lowering the ratio). Dynamic scheduling approaches the 24-8 level when samples are sufficient, while avoiding the 16-16 wait waste. + +**Summary of load-balance trade-offs**: + +| Config | Bottleneck | MQ backlog | Rollout util | Train util | +|--------|-----------|------------|--------------|------------| +| 16-16-static | Production (rollout) | Near zero (trainer starves) | Pinned at capacity | Low (waits for samples) | +| 24-8-static | Consumption (trainer) | At capacity limit | Idle capacity | High (never waits) | +| dynamic | Matched dynamically | Moderate | High & stable | Near 24-8 | + +Dynamic scheduling's value: by shifting GPUs between training and rollout to match the actual load, it simultaneously avoids the 16-16 trainer starvation and the 24-8 sample buildup, achieving higher end-to-end throughput without adding more total GPUs. + +--- + +## 6. Adding a Custom Policy + +Four steps to support a new policy: + +### Step 1: Subclass the base class + +```python +from verl.experimental.fully_async_policy.dynamic_schedule import ( + DynamicSchedulePolicyBase, + DynamicScheduleContext, + register_policy, +) + +@register_policy("my_policy") +class MyDynamicSchedulePolicy(DynamicSchedulePolicyBase): + + def __init__(self, deactivate_ratio: float = 0.5, only_hybrid: bool = False): + self.deactivate_ratio = deactivate_ratio + self.only_hybrid = only_hybrid + + def should_deactivate( + self, + global_steps: int, + is_hybrid_active: bool, + ctx: DynamicScheduleContext, + ) -> bool: + """Return True to deactivate hybrid replicas this step.""" + return is_hybrid_active + + def deactivate_wait_samples(self, ctx: DynamicScheduleContext) -> int: + """Return minimum buffered-sample count before deactivation proceeds.""" + return int(ctx.required_samples * ctx.trigger_parameter_sync_step * self.deactivate_ratio) + + def should_activate_after_step( + self, + global_steps: int, + is_hybrid_active: bool, + ctx: DynamicScheduleContext, + ) -> bool: + """Return True to re-activate after weight sync.""" + return ctx.total_generated_samples < ctx.expected_samples + ctx.buffer_samples + + # Optional: override to update internal state after each step + def update_after_step(self, global_steps: int, ctx: DynamicScheduleContext) -> None: + pass + + # Optional: override to customise request redistribution after activation + def request_rebalance(self, global_steps: int, ctx: DynamicScheduleContext) -> None: + pass +``` + +### Step 2: Register the import + +Place the file inside `verl/experimental/fully_async_policy/dynamic_schedule/` and add an import in `__init__.py`: + +```python +# verl/experimental/fully_async_policy/dynamic_schedule/__init__.py +from .my_policy import MyDynamicSchedulePolicy +``` + +Or import it manually in your entry script to trigger `@register_policy`. + +### Step 3: Reference in config + +```yaml +async_training: + use_dynamic_resource_scheduling: True + dynamic_schedule_policy: "my_policy" + dynamic_schedule_deactivate_ratio: 0.5 + dynamic_schedule_enable_rebalance: True +``` + +### Step 4: Use `DynamicScheduleContext` fields + +| Field | Type | Description | +|-------|------|-------------| +| `required_samples` | `int` | Min samples per collection (`ppo_mini_batch_size × require_batches`) | +| `trigger_parameter_sync_step` | `int` | Collections per weight-sync step | +| `step_required_samples` | `int` | Derived: `required_samples × trigger_parameter_sync_step` — total samples expected in one weight-sync cycle | +| `total_generated_samples` | `int` | Cumulative rollout samples since training began | +| `expected_samples` | `int` | Theoretical samples needed up to current sync step | +| `buffer_samples` | `int` | Allowed buffer headroom (`expected × staleness_threshold`) | +| `step_wait_times` | `list[float]` | Per-collection wait times within latest step (seconds) | +| `step_wait_samples` | `list[int]` | Per-collection count of samples that actually had to be waited on (`max(0, required_samples - queue_size_at_collection_start)`), parallel to `step_wait_times`; used together to compute a generation-rate signal not skewed by samples served instantly from queue backlog | +| `only_hybrid` | `bool` | `True` when there are no standalone replicas | +| `last_activate_duration_s` | `float` | Duration of last activate cycle (weight sync + onload), seconds | +| `last_deactivate_duration_s` | `float` | Duration of last deactivate cycle (offload), seconds | + +--- + +## 7. File Structure + +``` +verl/experimental/fully_async_policy/dynamic_schedule/ +├── __init__.py # Public exports + policy registry +├── base.py # DynamicSchedulePolicyBase ABC, DynamicScheduleContext, registry +├── default_policy.py # DefaultDynamicSchedulePolicy (adaptive dynamic scheduling) +├── static_fully_async_policy.py # StaticFullyAsyncPolicy (original fully-async / colocated fallback) +├── fixed_ratio_policy.py # FixedRatioDynamicSchedulePolicy (fixed ratio, no adaptation) +└── dynamic_resource_controller.py # DynamicResourceController (state machine + lifecycle) +``` diff --git a/verl_0720_main/verl/docs/advance/fsdp_extension.rst b/verl_0720_main/verl/docs/advance/fsdp_extension.rst new file mode 100644 index 0000000000000000000000000000000000000000..181e109082262f26334034337c5915d522049759 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/fsdp_extension.rst @@ -0,0 +1,97 @@ + +Add models with the FSDP backend +================================== + +Last updated: 02/09/2025. + +Model +-------------------------- + +In principle, our FSDP backend can support any HF model and we can +sychronoize the actor model weight with vLLM using `hf_weight_loader.py` under `third_party/vllm`. +However, ``hf_weight_loader`` is will gather the full state_dict of a +model during synchronization, which may cause OOM. We suggest using +``dtensor_weight_loader`` which gather the full model parameter layer by +layer to reduce the peak memory usage. We already support dtensor weight +loader for the models below in `dtensor_weight_loader.py` under `third_party/vllm`: + +- ``GPT2LMHeadModel`` +- ``LlamaForCausalLM`` +- ``LLaMAForCausalLM`` +- ``MistralForCausalLM`` +- ``InternLMForCausalLM`` +- ``AquilaModel`` +- ``AquilaForCausalLM`` +- ``Phi3ForCausalLM`` +- ``GemmaForCausalLM`` +- ``Gemma2ForCausalLM`` +- ``GPTBigCodeForCausalLM`` +- ``Starcoder2ForCausalLM`` +- ``Qwen2ForCausalLM`` +- ``DeepseekV2ForCausalLM`` + +To implement ``dtensor_weight_loader`` of a model that's supported in +vLLM, follow the guide of gemma model below: + +1. Copy the + ``load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]])`` from the vllm model class + to ``dtensor_weight_loaders.py`` +2. Modify the arguments to + ``(actor_weights: Dict, vllm_model: nn.Module)`` +3. Replace the ``self`` to ``vllm_model`` +4. Add the + ``local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight)`` + before each ``param = params_dict[name]`` and modify the following + weight loading using ``local_loaded_weight``. +5. Register the implemented dtensor weight loader to ``__MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__``. + +.. code-block:: diff + + - def load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): + + def gemma_dtensor_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + stacked_params_mapping = [ + # (param_name, shard_name, shard_id) + ("qkv_proj", "q_proj", "q"), + ("qkv_proj", "k_proj", "k"), + ("qkv_proj", "v_proj", "v"), + ("gate_up_proj", "gate_proj", 0), + ("gate_up_proj", "up_proj", 1), + ] + - params_dict = dict(self.named_parameters()) + + params_dict = dict(vllm_model.named_parameters()) + loaded_params = set() + - for name, loaded_weight in weights: + + for name, loaded_weight in actor_weights.items(): + for (param_name, shard_name, shard_id) in stacked_params_mapping: + if shard_name not in name: + continue + name = name.replace(shard_name, param_name) + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[name] + weight_loader = param.weight_loader + - weight_loader(param, loaded_weight, shard_id) + + weight_loader(param, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + # lm_head is not used in vllm as it is tied with embed_token. + # To prevent errors, skip loading lm_head.weight. + if "lm_head.weight" in name: + continue + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", + default_weight_loader) + - weight_loader(param, loaded_weight) + + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + loaded_params.add(name) + unloaded_params = params_dict.keys() - loaded_params + if unloaded_params: + raise RuntimeError( + "Some weights are not initialized from checkpoints: " + f"{unloaded_params}") \ No newline at end of file diff --git a/verl_0720_main/verl/docs/advance/fully_async.md b/verl_0720_main/verl/docs/advance/fully_async.md new file mode 100644 index 0000000000000000000000000000000000000000..c8cdca56b1108c7ea18284c500b614637ebdc60f --- /dev/null +++ b/verl_0720_main/verl/docs/advance/fully_async.md @@ -0,0 +1,568 @@ +# Recipe: Fully Async Policy Trainer + +**Author:** `https://github.com/meituan-search` + +Last updated: 05/25/2026. + +This document introduces a fully asynchronous PPO training system that completely decouples the Trainer and Rollouter, +supporting asynchronous sample generation and training. +Under this system, we achieved a 2.35x-2.67x performance improvement when training the Qwen2.5-7B model with 128 GPUs, +without significantly affecting the results. + +## Introduction + +### Background + +The separated rollout and train architecture, compared to the colocate architecture, can allocate resources more +flexibly and design more flexible training logic, thereby addressing issues such as low GPU utilization and training +efficiency caused by long-tail problems. +The one_step_off_policy alleviates the problem of long rollout times and achieves some gains in training efficiency by +designing a separated architecture and performing asynchronous training between rollout and train for one round. +However, it forcibly uses data from one round of asynchronous training, which is not flexible enough and cannot +completely eliminate the impact of long-tail on training efficiency. +In other frameworks such as AReaL, Magistral, StreamRL, and AsyncFlow, asynchronous training and streaming training have +been implemented based on the separated architecture and have achieved gains. +We borrow from their methods and implemented them in VERL. The fully_async_policy supports asynchronous, streaming, and +partial +rollout training. +By reasonably setting parameters such as resource allocation and parameter synchronization frequency, fully_async_policy +can significantly improve training efficiency. + +> Magistral https://arxiv.org/abs/2506.10910 +> +> AReaL: A Large-Scale Asynchronous Reinforcement Learning System for Language +> Reasoning https://arxiv.org/abs/2505.24298 +> +> StreamRL: Scalable, Heterogeneous, and Elastic RL for LLMs with Disaggregated Stream +> Generation https://arxiv.org/abs/2504.15930 +> +> AsyncFlow: An Asynchronous Streaming RL Framework for Efficient LLM Post-Training https://arxiv.org/abs/2507.01663 +> + +### Core Contributions + +* **Resource Isolation**: Unlike using hybrid_engine, Rollouter and Trainer use separate computing resources and need to + specify the resources they occupy separately. +* **Parallel Generation and Training**: While the Trainer is training, the Rollouter is generating new samples. +* **Multi-step Asynchronous**: Compared to one step off policy, it supports asynchronous settings from 0.x steps to + multiple steps, making the asynchronous solution more flexible. +* **NCCL Parameter Synchronization**: Based on the nccl communication primitive, refer + to [checkpoint-engine](https://github.com/MoonshotAI/checkpoint-engine) to + achieve efficient parameter synchronization between Rollouter and Trainer. +* **Stream Inference and Training**: Rollouter generates data sample by sample, and data transmission uses a single + sample as the minimum transmission unit. +* **Asynchronous Training and Freshness Control**: By setting the parameter async_training.staleness_threshold, it + supports training with samples generated by old parameters. +* **PartialRollout**: The Rollouter's inference process supports partial rollout logic. During parameter + synchronization, by adding `sleep() and resume()` logic, it + saves samples from ongoing rollouts and continues using them in the next rollout, reducing the time spent waiting for + ongoing tasks to finish during parameter synchronization. + +Currently, the supported usage mode is megatron/fsdp+vllm. vllm must use the server mode based on AgentLoop. + +## Design + +The overall architecture of fully_async_policy is shown in the figure below. fully_async_policy mainly consists of four +parts: Rollouter, MessageQueue, Trainer, and ParameterSynchronizer. + +![fully_async_policy_structure]( +https://github.com/ArronHZG/verl-community/blob/main/docs/fully_async_policy_structure.svg?raw=true) + +1. Rollouter generates sequences sample by sample and puts the generated samples into the MessageQueue, with the + production speed controlled by freshness. +2. MessageQueue is used to temporarily store samples generated by Rollouter. +3. Trainer fetches samples from MessageQueue sample by sample. After fetching `require_batches*ppo_mini_batch_size` + samples, it will perform training. After training for async_training.trigger_parameter_sync_step rounds, it triggers + a parameter synchronization with Rollouter. +4. ParameterSynchronizer implements the NCCL synchronous parameter synchronization capability. + +The source of benefits compared to the base scheme lies in the fact that in the colocate case, using more resources for +rollout cannot solve the idleness caused by long-tail samples. +After we perform resource isolation, the time for rollout and train may be longer than before (because fewer resources +are used), +but the overlap in their time consumption reduces the end-to-end time consumption. + +![fully_async_policy_revenue]( +https://github.com/ArronHZG/verl-community/blob/main/docs/fully_async_policy_revenue.svg?raw=true) + +## Usage + +### Parameter Description + +| super params | implication | +|------------------------------------------------------------------|------------------------------------------------------------------------------------------------| +| `trainer.nnodes` | Number of nodes for Trainer | +| `trainer.n_gpus_per_node` | Number of GPUs per node for Trainer | +| `rollout.nnodes` | Number of nodes for Rollouter | +| `rollout.n_gpus_per_node` | Number of GPUs per node for Rollouter | +| `data.train_batch_size` | In the fully async strategy, this value is not effective (default is 0) | +| `data.gen_batch_size` | In the fully async strategy, uses streaming sample production logic (default is 1) | +| `rollout.total_rollout_steps` | Total number of rollout samples | +| `rollout.test_freq` | How many times Rollouter updates parameters before performing a validation | +| `actor_rollout_ref.actor.ppo_mini_batch_size` | The ppo_mini_batch_size is a global num across all workers/gpus | +| `actor_rollout_ref.actor.use_rollout_log_probs=True` | Use log_probs generated by rollout | +| `algorithm.rollout_correction.bypass_mode` | Whether to compute log_prob using the training model's parameters during the training phase. | +| `async_training.require_batches` | Number of ppo_mini_batch_size that FullyAsyncTrainer fetches at once | +| `async_training.trigger_parameter_sync_step` | Indicates how many local updates FullyAsyncTrainer performs before a parameter synchronization | +| `async_training.staleness_threshold` | Freshness control | +| `async_training.partial_rollout` | Whether to perform partial_rollout | +| `async_training.use_trainer_do_validate` | Whether use trainer node to do validate process, default `False` | + +**Further Explanation:** + +* `rollout.total_rollout_steps` + + Compared to colocate, the quantity can be aligned by multiplying train_batch_size and step: + `rollout.total_rollout_steps = data.train_batch_size * step`. + +* `async_training.trigger_parameter_sync_step` + + In the fully async strategy, it indicates how many local updates the Trainer performs (i.e., how many times it fetches + `require_batches * ppo_mini_batch_size` samples) before a parameter synchronization with Rollouter. + Between every two parameter synchronizations between Rollouter and Trainer, the Trainer will process + `trigger_parameter_sync_step* require_batches*ppo_mini_batch_size` samples. + To fairly compare speed with colocate, `trigger_parameter_sync_step` should be set to + `data.train_batch_size / (require_batches * ppo_mini_batch_size)`. + +* `async_training.staleness_threshold` + + In the fully async strategy, it indicates the maximum proportion of stale samples allowed to be used. + + * `staleness_threshold`=0, indicates synchronous training. + Rollouter will generate a fixed number of samples between two parameter updates, the sample count is: + + `rollout_num = (trigger_parameter_sync_step*require_batches*ppo_mini_batch_size)` + * `staleness_threshold`>0, indicates asynchronous training, can be set to a decimal for more flexible asynchronous + calls. + Rollouter will generate at most the following number of samples between two parameter updates: + + `rollout_num = (1+staleness_threshold)*(trigger_parameter_sync_step*require_batches*ppo_mini_batch_size) - num_staleness_sample` + + `num_staleness_sample` represents the number of stale samples generated in excess during the last rollout. + + Since it's a streaming system, rollout continues to generate and trainer continues to consume. If rollouter is slower, + trainer will trigger parameter synchronization earlier, and rollouter will not actually produce rollout_num samples. + When rollout is fast enough, setting `staleness_threshold` to 1 is basically equivalent to one_step_off policy. + To avoid too many expired samples affecting training accuracy, it is recommended to set this value to less than 1. + +* `async_training.partial_rollout` + + partial_rollout only actually takes effect when staleness_threshold>0. + +* `async_training.require_batches` + + In streaming training, require_batches should be set to 1, indicating that training is performed after producing + enough ppo_mini_batch_size samples. + In actual testing, we found that if fewer samples are issued at once, due to the order of data distribution, it can + cause training instability and longer response lengths. + Here, we additionally provide require_batches for streaming distribution and control the number of samples + participating in training at once. + +* `actor_rollout_ref.actor.use_rollout_log_probs=True` + + In reinforcement learning algorithms, log_probs have implicit correlations with parameter versions and tokens. Due to + the settings of algorithms like PPO/GRPO/DAPO, when calculating importance sampling, + old_log_prob must use the log_probs corresponding to the rollout parameters and tokens to ensure algorithm + correctness. In the fully + async strategy, we default to old_log_prob being calculated by rollout rather than by trainer. + +* `algorithm.rollout_correction.bypass_mode` + + > algorithm.rollout_correction.bypass_mode default is True, using rollout log prob. + + During the training process, we observed that metrics and response lengths may become unstable in the later + stages of training. To mitigate this issue, we can use + the [Rollout Importance Sampling](https://verl.readthedocs.io/en/latest/advance/rollout_is.html) + technique for importance sampling. To utilize Rollout Importance Sampling, we need to compute log_prob using + the training engine, which requires enabling this switch. + Additionally, when `algorithm.rollout_correction.bypass_mode=False` and Rollout Importance Sampling are enabled under + mode d + (async stream pipeline with partial rollout), our implementation approximates `Areal's Decoupled PPO`. + +* `async_training.use_trainer_do_validate` + + It controls whether to use the trainer's `do_validate` method for validation. + If set to True, the trainer will perform validation after each parameter update. It can reduce the validation time + overhead and trainer node idle time. + If set to False, the trainer will not perform validation. + +### Supported Modes + +1. on policy pipeline: + 1. **trigger_parameter_sync_step=1, staleness_threshold=0** + 2. Rollouter produces `require_batches*ppo_mini_batch_size` samples at once, Trainer fetches these samples for + training, and after training completes, Trainer and Rollouter perform a parameter synchronization; + 3. During the rollout phase, if there are long-tail samples but few rollout samples, shorter samples cannot fill + idle resources, causing some resource waste. + 4. As shown in figure a; + +2. stream off policy pipeline: + 1. **trigger_parameter_sync_step>1, staleness_threshold=0** + 2. Synchronous streaming training will be performed. Rollouter produces + `require_batches*ppo_mini_batch_size*trigger_parameter_sync_step` samples at once, Trainer performs a local + training every time it fetches `require_batches*ppo_mini_batch_size` samples, and after training + trigger_parameter_sync_step times, Trainer and Rollouter perform a parameter synchronization; + 3. Compared to a, since more samples are generated at once, resource idleness will be lower. + 4. In one step training, there will be two periods of resource idleness: when fetching the first batch of samples, + train waits for `require_batches*ppo_mini_batch_size` samples to be produced, and during the last parameter + update, rollout waits for training to complete. + 5. As shown in figure b; + +3. async stream pipeline with stale samples: + 1. **trigger_parameter_sync_step>=1, staleness_threshold>0, partial_rollout=False** + 2. After each parameter update, Rollouter will plan to produce at most rollout_num samples (in practice, the number + of samples generated may be less than this value depending on rollout speed). + 3. If the rollout process is relatively fast, Rollouter will generate some additional samples num_stale_samples + before parameter synchronization for immediate use by Trainer after synchronization. + When triggering parameter synchronization, if Rollouter has ongoing tasks, it will wait for the tasks to complete + and not add new tasks; + 4. Compared to b, except for the first step training, subsequent training will not have the time to wait for the + first batch rollout to finish, but will have the time to wait for active tasks to finish. + 5. As shown in figure c; + +4. async stream pipeline with partial rollout: + 1. **trigger_parameter_sync_step>=1, staleness_threshold>0, partial_rollout=True** + 2. Compared to c, when triggering parameter synchronization, if Rollouter has samples being produced, it will + interrupt the rollout process and perform parameter synchronization. The interrupted samples will continue to be + generated after synchronization. This reduces the time to wait for active tasks to finish. + 3. As shown in figure d; + +![fully_async_policy_mode]( +https://github.com/ArronHZG/verl-community/blob/main/docs/fully_async_policy_mode.svg?raw=true) + +### Key Metrics + +| metrics | implication | +|------------------------------------------------|--------------------------------------------------------------------------------------------------------| +| `trainer/idle_ratio` | Trainer idle rate | +| `rollouter/idle_ratio` | Rollouter idle rate | +| `fully_async/count/stale_samples_processed` | Total number of old samples used in training | +| `fully_async/count/stale_trajectory_processed` | Total number of old trajectories used in training (one sample produces rollout.n trajectories) | +| `fully_async/partial/total_partial_num` | Number of partial samples processed by Trainer between two trigger_parameter_sync_step | +| `fully_async/partial/partial_ratio` | Ratio of partial samples processed by Trainer between two trigger_parameter_sync_step | +| `fully_async/partial/max_partial_span` | Maximum parameter span of partial samples processed by Trainer between two trigger_parameter_sync_step | + +### Parameter Tuning Recommendations + +* Resource Allocation and Adjustment: + * Reasonable resource allocation is the prerequisite for achieving good training efficiency. The ideal resource + allocation should make the rollout time and train time close, thereby minimizing pipeline bubbles in the entire + training process, + avoiding resource idleness, and ensuring Trainer does not use old samples. In real training scenarios, resource + allocation can be adjusted based on the idle time of rollout and train during actual training, + which can be obtained from rollouter/idle_ratio and trainer/idle_ratio. If rollouter/idle_ratio is high and + trainer/idle_ratio is low, + Trainer resources should be increased and Rollouter resources should be reduced, and vice versa. + +* Key Parameters: + * staleness_threshold: Setting it too high will cause more old samples to be used, affecting model performance. It + is recommended to set it to less than 1. + * require_batches: The closer to 1, the closer to a pure streaming process, the smaller the training bubbles, and + the faster the acceleration effect that can be achieved in terms of speed, but it will affect the order of sample + processing; + * trigger_parameter_sync_step: The smaller the setting, the closer to on policy, but it will cause frequent + parameter synchronization. Long-tail samples waste resources that cannot be filled by short samples, resulting in + low resource utilization. + The larger the setting, the higher the computational efficiency, but the accuracy will be affected by off policy. + * rollout.test_freq: It will occupy Rollouter resources and is not recommended to be set too small. + +* Mode Selection: By adjusting different parameters, the Fully Async architecture supports optimization acceleration at + different levels, suitable for tasks in different scenarios. + * For small-scale tasks that need to ensure training stability and on-policy nature, and have low speed + requirements, the on policy pipeline mode (Mode 1) can be tried. + * For scenarios that need to improve training throughput but are sensitive to staleness, the stream off policy + pipeline mode can be tried. That is, by + setting trigger_parameter_sync_step>1 to improve training efficiency, but still maintaining the synchronization + mechanism (staleness_threshold=0) (Mode 2). + * For large-scale tasks with high training speed requirements and can tolerate a certain degree of off-policy and + staleness, setting staleness_threshold> + 0 and partial_rollout=True can improve training efficiency, using the async stream pipeline mode (Mode 3 or 4). + +### Quick Start + +```shell +rollout_mode="async" +rollout_name="vllm" # sglang or vllm +if [ "$rollout_mode" = "async" ]; then + export VLLM_USE_V1=1 + return_raw_chat="True" +fi + +train_prompt_bsz=0 +gen_prompt_bsz=1 +n_resp_per_prompt=16 +train_prompt_mini_bsz=32 +total_rollout_steps=$(((512*400))) +test_freq=10 +staleness_threshold=0 +trigger_parameter_sync_step=16 +partial_rollout=False + + +python -m verl.experimental.fully_async_policy.fully_async_main \ + train_batch_size=${train_prompt_bsz} \ + data.gen_batch_size=${gen_prompt_bsz} \ + data.return_raw_chat=${return_raw_chat} \ + actor_rollout_ref.rollout.n=${n_resp_per_prompt} \ + actor_rollout_ref.actor.strategy=fsdp2 \ + critic.strategy=fsdp2 \ + actor_rollout_ref.hybrid_engine=False \ + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} \ + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} \ + actor_rollout_ref.rollout.name=${rollout_name} \ + actor_rollout_ref.rollout.mode=${rollout_mode} \ + trainer.nnodes="${NNODES_TRAIN}" \ + trainer.n_gpus_per_node="${NGPUS_PER_NODE}" \ + rollout.nnodes="${NNODES_ROLLOUT}" \ + rollout.n_gpus_per_node="${NGPUS_PER_NODE}" \ + rollout.total_rollout_steps="${total_rollout_steps}" \ + rollout.test_freq="${test_freq}" \ + async_training.staleness_threshold="${staleness_threshold}" \ + async_training.trigger_parameter_sync_step="${trigger_parameter_sync_step}" \ + async_training.partial_rollout="${partial_rollout}" +``` + +## Experiments + +### Asynchronous Training on 7B Model + +We used Qwen2.5-Math-7B to verify the benefits of the fully async strategy under long candidates and multiple resources. +Using the `async stream pipeline with stale samples` strategy, we achieved about 2x performance improvement on 32 cards, +64 cards, and 128 cards without significantly affecting experimental results. + +* Machine: H20 +* Model: Qwen2.5-Math-7B +* Rollout length: max_response_length FSDP2: 28K tokens; +* Algorithm: DAPO +* Dataset: TRAIN_FILE: dapo-math-17k.parquet TEST_FILE: aime-2024.parquet +* Engine: vllm+FSDP2 +* rollout.n: 16 +* ppo_mini_batch_size: 32 +* test_freq: 20 + +* colocate sync: + * step: 400 + * train_batch_size: 512 + +* fully_async_policy + * total_rollout_steps: 512*400 + * require_batches: 4 + * trigger_parameter_sync_step: 4 + * staleness_threshold: 0.5 + * partial_rollout: True + +| training mode | resource allocation | step | gen | old_log_prob | update_actor | total time
100 step | total time
200 step | total time
300 step | total time
400 step | acc/mean@1 | +|:--------------------:|:---------------------:|:--------:|:--------:|:--------------:|:---------------:|:------------------------:|:------------------------:|:------------------------:|:------------------------:|:-------------------------------:| +| colocate sync | 32 | 790.10 | 357.41 | 107.71 | 269.80 | 13h 44m | 1d 3h 43m | 2d 9h 22m | 3d 17h 5m | max: 0.3313
last: 0.2448 | +| fully_async_policy | 16:16 | 294.77 | 21.26 | \ | 313.81 | 7h 58m
(1.72x) | 16h 21m
(1.70x) | 1d 0h 53m
(2.31x) | 1d 9h 26m
(2.66x) | max: 0.3302
last: 0.2333 | +| colocate sync | 64 | 365.28 | 150.72 | 70.26 | 133.41 | 10h 22m | 20h 45m | 1d 7h 6m | 1d 17h 32m | max: 0.3365
last: 0.2333 | +| fully_async_policy | 32:32 | 189.26 | 28.46 | \ | 156.98 | 4h 57m
(2.09x) | 10h 14m
(2.03x) | 16h 58m
(1.83x) | 21h 40m
(1.92x) | max: 0.3677
last: 0.3406 | +| colocate sync | 128 | 356.30 | 177.85 | 53.92 | 113.81 | 8h 36m | 17h 56m | 1d 5h 6m | 1d 16h 48m | max: 0.3573
last: 0.2958 | +| fully_async_policy | 64:64 | 150.63 | 33.14 | \ | 113.16 | 3h 13m
(2.67x) | 6h 46m
(2.65x) | 10h 53m
(2.67x) | 17h 22m
(2.35x) | max: 0.3521
last: 0.3094 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-colocate_async?nw=nwuserhouzg + +### 128-card 7B Asynchronous Mode Experiment + +We used Qwen2.5-Math-7B to verify the effects of various modes supported by fully async. +We can see that the benefit brought by streaming is approximately 1.6x, and after combining staleness and +partial_rollout, the benefit reaches 2.35x. + +| mode | step | gen | old_log_prob | update_actor | total time
100 step | total time
200 step | total time
300 step | total time
400 step | acc/mean@1 | +|:-------------------------------------------------------------------------------------------------------:|:--------:|:--------:|:--------------:|:--------------:|:------------------------:|:------------------------:|:------------------------:|:------------------------:|:------------------------------:| +| colocate sync | 356.30 | 177.85 | 53.92 | 113.81 | 8h 36m | 17h 56m | 1d 5h 6m | 1d 16h 48m | max: 0.3573
last: 0.2958 | +| `stream off policy pipeline`
(+fully async: trigger_parameter_sync_step= 4,
require_batches= 4) | 231.34 | 128.47 | \ | 98.77 | 4h 25m | 9h 41m | 15h 2m | 1d 1h 53m | max: 0.2844
last: 0.2604 | +| `async stream pipeline with stale samples`
(+staleness_threshold=0.5) | | | | | | | | | | +| `async stream pipeline with partial rollout`
(+partial_rollout=True) | 150.63 | 33.14 | \ | 113.16 | 3h 13m | 6h 46m | 10h 53m | 17h 22m | max: 0.3521
last: 0.3094 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-stream_stale_partial?nw=nwuserhouzg + +### 128-card Stale Ablation Experiment + +Under the `async stream pipeline with partial rollout` mode, we verified the impact of staleness settings on training +efficiency. +We found that the larger the staleness, the more obvious the final gains. +We also noticed that the times for staleness values of 0.3 and 0.5 are quite close, because as the training steps +increase, the response length changes significantly, causing training instability. +Further analysis and optimization are needed for this issue. + +| staleness_threshold | step | gen | old_log_prob | update_actor | total time
100 step | total time
200 step | total time
300 step | total time
400 step | acc/mean@1 | +|:---------------------:|:--------:|:--------:|:--------------:|:--------------:|:------------------------:|:------------------------:|:------------------------:|:------------------------:|:-----------------------------:| +| 0 | 231.34 | 128.47 | \ | 98.77 | 4h 25m | 9h 41m | 15h 2m | 1d 1h 53m | max: 0.2844
last: 0.2604 | +| 0.1 | 171.30 | 58.17 | \ | 109.12 | 3h 53m | 8h 37m | 14h 25m | 19h 59m | max: 0.3542
last: 0.2979 | +| 0.3 | 146.11 | 38.88 | \ | 103.22 | 3h 18m | 6h 49m | 11h 40m | 17h 20m | max: 0.3469
last: 0.2865 | +| 0.5 | 150.63 | 33.14 | \ | 113.16 | 3h 13m | 6h 46m | 10h 53m | 17h 22m | max: 0.3521
last: 0.3094 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-stream_stale_partial?nw=nwuserhouzg + +### 128-card 7B require_batches Ablation Experiment + +In multiple tests, we found that the number of samples issued each time in streaming affects the response length during +training, which in turn affects training time. We verified the impact on results by modifying +`async_training.require_batches`. + +| require_batches | step | gen | old_log_prob | update_actor | total time
100 step | total time
200 step | total time
300 step | acc/mean@1 | +|:-----------------:|:--------:|:-------:|:--------------:|:--------------:|:------------------------:|:------------------------:|:------------------------:|:-----------------------------:| +| 1 | 203.47 | 30.88 | \ | 181.08 | 3h 31m | 8h 29m | 17h 36m | max: 0.349
last: 0.326 | +| 2 | 158.72 | 26.32 | \ | 128.08 | 3h 35m | 7h 38m | 13h 57m | max: 0.351
last: 0.3406 | +| 4 | 124.64 | 25.62 | \ | 95.06 | 3h 13m | 6h 46m | 10h 53m | max: 0.3521
last: 0.3521 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-ablation_require_batches?nw=nwuserhouzg + +### 30B Model Mode Experiment + +We achieved a 1.7x performance improvement with `async stream pipeline with staleness samples` strategy on the +Qwen3-30B-A3B-Base model compared to the colocate setup. It is worth noting that this is far from the upper limit of +performance gains achievable through asynchrony. Firstly, the comparative experiments used a maximum response length of +only 8k, which is much shorter than the 20k sequence length in previous experiments, resulting in a less pronounced +rollout tail effect. Secondly, we adopted a highly skewed resource allocation, with rollout using 96 GPUs and trainer +using 32 GPUs, which is not an optimal configuration. During the experiments, we observed that the current verl +implementation imposes certain constraints, such as requiring data to be evenly divisible by the number of GPUs, making +resource adjustment less flexible. Additionally, as asynchronous training and deployment accelerate, the performance gap +is gradually narrowing. Therefore, enabling more flexible resource allocation and dynamic resource adjustment in the +future will be our next focus. + +* Machine: H20 +* Model: Qwen3-30B-A3B-Base +* Rollout length: max_response_length : 8K tokens; +* Algorithm: GRPO +* Dataset: TRAIN_FILE: dapo-math-17k.parquet TEST_FILE: aime-2024.parquet +* Engine: vllm+Megatron +* rollout.n: 16 +* ppo_mini_batch_size: 128 +* test_freq: 20 + +* colocate sync: + * step:400 + * train_batch_size: 512 + +* fully_async_policy + * total_rollout_steps: 512*400 + * trigger_parameter_sync_step: 512/128 = 4 + * staleness_threshold: 0.5 + * partial_rollout: True + +| Training Mode | Resource Allocation | Step | Gen | Old Log Prob | Ref | Update Actor | Total Time 100 Step | Total Time 200 Step | Total Time 300 Step | Total Time 400 Step | Acc/Mean@1 | +|--------------------|---------------------|--------|--------|--------------|-------|--------------|---------------------|---------------------|---------------------|---------------------|-----------------------------| +| Colocate Sync | 128 | 497.89 | 348.05 | 28.73 | 20.86 | 86.27 | 13h 36m | 1d 3h 48m | 1d 19h 4m | 2d 11h 39m | max: 0.3500
last: 0.3208 | +| Fully Async Policy | 96:32 | 282.75 | 22.06 | \ | 50.05 | 206.63 | 6h 45m (2.01x) | 14h 48m (1.88x) | 1d 0h 9m (1.78x) | 1d 10h 41m (1.72x) | max: 0.3813
last: 0.3448 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-30B?nw=nwuserhouzg | | | + +### checkpoint-engine Ablation Experiment +We tested the single-step parameter synchronization time of the checkpoint-engine on three models: Qwen2.5-Math-7B, Qwen3-30B-A3B, and Qwen3-235B-A22B, using default checkpoint-engine configurations. All experiments were performed on H20 machines, and the Megatron engine was used for training. + +| model | trainer rank | rollout rank | checkpoint-engine | total sync time | +|:---------------:|:--------------:|:-------------:|:-------------------:|:-----------------:| +| Qwen2.5-Math-7B | 4 | 4 | False | 0.12s | +| Qwen2.5-Math-7B | 4 | 4 | True | 0.02s | +| Qwen3-30B-A3B | 16 | 16 | False | 15.76s | +| Qwen3-30B-A3B | 16 | 16 | True | 4.38s | +| Qwen3-235B-A22B | 64 | 64 | False | 58.57s | +| Qwen3-235B-A22B | 64 | 64 | True | 23.70s | + + +### use_trainer_do_validate Experiment +We tested the effect of setting `use_trainer_do_validate=True` on the training process. The results show that setting +this parameter to True can reduce the validation time overhead and trainer node idle time. +We used Qwen2.5-Math-7B to verify the benefits of `use_trainer_do_validate=True` on the training process, we achieved about 2x performance improvement on validation time, and the trainer node idle time is reduced by about 40%. + +* Machine: H20 +* Model: Qwen2.5-Math-7B +* Rollout length: max_response_length FSDP2: 10K tokens; +* Algorithm: DAPO +* Dataset: TRAIN_FILE: dapo-math-17k.parquet TEST_FILE: aime-2024.parquet +* Engine: vllm+FSDP2 +* rollout.n: 16 +* ppo_mini_batch_size: 32 +* test_freq: 10 + +* fully_async_policy + * total_rollout_steps: 512*400 + * require_batches: 4 + * trigger_parameter_sync_step: 4 + * staleness_threshold: 0.5 + * partial_rollout: True + +| training mode | resource allocation | step | gen | old_log_prob | update_actor | validate time | total time
50 step | acc/mean@2 | +|:------------------:|:-------------------:|:-------:|:-------:|:------------:|:------------:|:-------------:|:---------------------:|:----------:| +| colocate sync | 16 | 484.623 | 52.939 | 0 | 430.263 | 205.080 | 7h9m | 22.6 | +| fully_async_policy | 8:8 | 489.953 | 52.622 | 0 | 435.874 | 95.699 | 7h2m | 21.0 | + + +## Multi-Turn Tool Calling + +Referencing **recipe/retool** and **ToolAgentLoop**, we implemented **AsyncPartialToolAgentLoop**, a multi-turn +tool-calling loop that supports partial_rollout for **fully_async_policy**. + +### Core Design + +`AsyncPartialToolAgentLoop` inherits from `ToolAgentLoop` and is adapted for the asynchronous training mode of +`fully_async_policy`. When `partial_rollout=True`, the Rollouter interrupts ongoing generation tasks before +synchronizing parameters with the Trainer. `AsyncPartialToolAgentLoop` is capable of: + +1. **Interrupting Tasks**: Responding to an interrupt signal to save the current state. Currently, interruptions occur + during the `GENERATING` process or after other states have completed. +2. **Resuming Tasks**: Resuming execution from the saved state after parameter synchronization is complete, rather than + starting over. + +### How to Use + +RL training with multi-turn tool calling in `fully_async_policy` is similar to `recipe/retool`. It is enabled by +specifying `multi_turn` configurations in the config file. + +1. **SFT Stage**: First, the model should undergo SFT to learn how to follow tool-calling format instructions. +2. **Multi-turn Configuration**: In the `fully_async_policy` training configuration, set the following parameters: + ```yaml + actor_rollout_ref: + rollout: + multi_turn: + enable: True # AsyncPartialToolAgentLoop will be used by default in fully_async_policy mode + # Other multi_turn related configurations + ``` +3. **Async Parameters**: To improve efficiency, enable `partial_rollout` and `staleness_threshold` when using multi-turn + tool calling: + ```yaml + async_training: + partial_rollout: True + staleness_threshold: 0.5 + # Other async parameters + ``` +4. **Example**: See `recipe/fully_async_policy/shell/dapo_7b_async_retool.sh`. + +### Experimental Results + +To validate the performance of `fully_async_policy` on multi-turn tool-calling tasks, we compared it with the standard +`colocate` synchronous mode. Key parameter settings are as follows. + +* **SFT Model**: Based on `Qwen2.5-7B-Instruct`, trained for 6 epochs on the `ReTool-SFT` dataset +* **RL Algorithm**: DAPO +* **Dataset**: + * Train: `DAPO-Math-17k` + * Test: `aime_2025` +* **Resource and Mode Comparison**: + * `colocate sync`: 32 H20 gpus + * `fully_async_policy`: 16 gpus for Trainer + 16 gpus for Rollouter +* **Key Configurations**: + 1. **Tool Calling Configuration**: + * `multi_turn.enable: True` + * `multi_turn.max_user_turns: 16` + * `multi_turn.max_assistant_turns: 16` + * `multi_turn.tool_config_path: recipe/retool/sandbox_fusion_tool_config.yaml` + 2. **`colocate sync` Configuration**: + * `ppo_mini_batch_size: 16` + * `train_batch_size: 64` + 3. **`fully_async_policy` Configuration**: + * `ppo_mini_batch_size: 16` + * `trigger_parameter_sync_step: 4` + * `require_batches: 1` + * `staleness_threshold: 1` + * `partial_rollout: True` + +| training mode | Resource allocation | step | gen | old_log_prob | update_actor | total time
100 step | total time
200 step | aime_2025
acc/mean@30 | +|:--------------------:|:---------------------:|:---------:|:---------:|:--------------:|:--------------:|:------------------------:|:------------------------:|:-------------------------------:| +| colocate | 32 | 375.47 | 228.03 | 35.19 | 111.84 | 9h 46m | 22h 28m | start:0.1078
last:0.2056 | +| fully_async_policy | 16: 16 | 221.36 | 40.59 | \ | 179.58 | 6h 19m
(1.55x) | 14h 4m
(1.60x) | start:0.11
last:0.2044 | + +> source data: https://wandb.ai/hou-zg-meituan/fully-async-policy-multiturn-tool?nw=nwuserhouzg \ No newline at end of file diff --git a/verl_0720_main/verl/docs/advance/grafana_prometheus.md b/verl_0720_main/verl/docs/advance/grafana_prometheus.md new file mode 100644 index 0000000000000000000000000000000000000000..3b59f936728e2142df8765b6f886804069566cd9 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/grafana_prometheus.md @@ -0,0 +1,193 @@ +# Use Prometheus and Grafana to Monitor Rollout + +**Author:** `https://github.com/meituan-search` + +Last updated: 12/05/2025. + +Monitor the rollout computation process using Prometheus and Grafana when using verl to enhance system observability and facilitate further performance optimization. + +We provide an additional training monitoring capability, leveraging Prometheus and Grafana to display rollout information during training and enhance system observability to facilitate further performance optimization. + +The system automatically configures Prometheus to scrape metrics from rollout servers, eliminating manual configuration steps. + +## Overview + +The figures below show the performance of Qwen235B on the AIME2024 dataset with a response length of 20k, where the emergence of a long-tail problem is clearly observable. + +![fully_async_policy_structure](https://github.com/ArronHZG/verl-community/blob/main/docs/grafana_validate.png?raw=true) + +The following figure presents the fully asynchronous training of the Qwen235B model. Here, resource idleness is distinctly noticeable, indicating that rollout resources can be reduced. + +![fully_async_policy_structure](https://github.com/ArronHZG/verl-community/blob/main/docs/grafana_fully_async_train.png?raw=true) + +Through the above two examples, we also illustrate the necessity of system observability. + +## Architecture Overview + +The overall workflow consists of the following steps: + +1. **Multi-node Ray Cluster Setup**: Start Ray cluster across multiple nodes with Grafana and Prometheus information configured in environment variables on the master node +2. **Start Grafana Service**: Launch Grafana on the master node for visualization of monitoring dashboards +3. **Start Prometheus Service**: Launch Prometheus on the master node for metrics collection and storage +4. **verl Async Rollout Mode**: verl uses async rollout mode to obtain rollout server ports and IP addresses +5. **Automatic Prometheus Configuration**: verl automatically rewrites the Prometheus configuration to add monitoring for rollout servers and notifies Prometheus to reload the configuration +6. **Metrics Collection**: After program execution, metrics can be viewed in Prometheus +7. **Dashboard Visualization**: Upload and view monitoring metrics in Grafana dashboards + +## Detailed Setup Steps + +### Step 1: Environment Variables and Start Ray Cluster + +First, set the necessary environment variables and start the Ray service. + +> Reference: [configure-manage-dashboard](https://docs.ray.io/en/latest/cluster/configure-manage-dashboard.html) + +```bash +# Master node environment variables +export GF_SERVER_HTTP_PORT=3000 # Grafana service default port (customizable) +export PROMETHEUS_PORT=9090 # Prometheus service default port (customizable) +export RAY_HEAD_PORT=6379 # Ray master node port (customizable) +export RAY_DASHBOARD_PORT=8265 # Ray dashboard default port (customizable) +export GRAFANA_PATHS_DATA=/tmp/grafana # Grafana data storage directory (customizable) +export RAY_GRAFANA_HOST="http://${master_ip}:${GF_SERVER_HTTP_PORT}" # Ray-associated Grafana address +export RAY_PROMETHEUS_HOST="http://${master_ip}:${PROMETHEUS_PORT}" # Ray-associated Prometheus address + +# Start Ray on master node +ray start --head --port=${RAY_HEAD_PORT} --dashboard-port=${RAY_DASHBOARD_PORT} + +# Start Ray on worker nodes +ray start --address={master_addr}:${RAY_HEAD_PORT} +``` + +**Verification:** Visit `http://master_ip:8265` to confirm Ray has started successfully. + +### Step 2: Start Grafana (Visualization Dashboard) + +Grafana is used to display metrics collected by Prometheus (such as cache hit rate, throughput, etc.): + +```bash +# Master node +nohup grafana-server \ + --config /tmp/ray/session_latest/metrics/grafana/grafana.ini \ + --homepath /usr/share/grafana \ + web > grafana.log 2>&1 & +``` + +**Verification:** Visit `http://master_ip:3000` to confirm Grafana has started successfully (default credentials: `admin/admin`). + +If you need to change the port, modify the `GF_SERVER_HTTP_PORT` environment variable, and grafana-server will automatically recognize it. + +### Step 3: Start Prometheus (Metrics Collection) + +Prometheus is responsible for scraping metrics from vLLM services and storing them as time-series data: + +```bash +# Master node +nohup prometheus \ + --config.file /tmp/ray/session_latest/metrics/prometheus/prometheus.yml \ + --web.enable-lifecycle \ + --web.listen-address=:${PROMETHEUS_PORT} \ + > prometheus.log 2>&1 & +``` + +**Verification:** Visit `http://master_ip:9090` to confirm Prometheus service has started successfully. + +### Step 4 & 5: Start verl Training + +Start verl training with the following parameters configured: + +**Required Configuration:** + +- `actor_rollout_ref.rollout.mode="async"` +- `actor_rollout_ref.rollout.disable_log_stats=False` +- `actor_rollout_ref.rollout.prometheus.enable=True` + +If use default port, this parameter can be omitted. + +- `actor_rollout_ref.rollout.prometheus.port=9090` + +If use default path, this parameter can be omitted. + +- `actor_rollout_ref.rollout.prometheus.file="/tmp/ray/session_latest/metrics/prometheus/prometheus.yml"` + +served_model_name uses `model_path.split("/")[-1]` for data statistics by default. +Users can also customize other aliases: + +- `actor_rollout_ref.rollout.prometheus.served_model_name="Qwen3-235B"` + +**Shell Script Example:** + +```bash +WORKING_DIR=${WORKING_DIR:-"${PWD}"} +RUNTIME_ENV=${RUNTIME_ENV:-"${WORKING_DIR}/verl/trainer/runtime_env.yaml"} + +rollout_mode="async" +rollout_name="vllm" # Options: sglang or vllm +if [ "$rollout_mode" = "async" ]; then + export VLLM_USE_V1=1 + return_raw_chat="True" +fi + +# Synchronous training +ray job submit --no-wait --runtime-env="${RUNTIME_ENV}" \ + --working-dir "${WORKING_DIR}" \ + -- python3 -m verl.trainer.main_ppo \ + data.return_raw_chat=${return_raw_chat} \ + actor_rollout_ref.rollout.name=${rollout_name} \ + actor_rollout_ref.rollout.mode=${rollout_mode} \ + actor_rollout_ref.rollout.disable_log_stats=False \ + actor_rollout_ref.rollout.prometheus.enable=True + ... + +# Asynchronous training +ray job submit --no-wait --runtime-env="${RUNTIME_ENV}" \ + --working-dir "${WORKING_DIR}" \ + -- python3 verl.experimental.fully_async_policy.fully_async_main \ + data.return_raw_chat=${return_raw_chat} \ + actor_rollout_ref.rollout.name=${rollout_name} \ + actor_rollout_ref.rollout.mode=${rollout_mode} \ + actor_rollout_ref.rollout.disable_log_stats=False \ + actor_rollout_ref.rollout.prometheus.enable=True + ... +``` + +### Step 6: View Metrics in Prometheus + +After task execution, verify that Prometheus is correctly collecting metrics. + +**Verification:** Visit the Prometheus interface at `http://master_ip:9090` and search for `vllm:` or `sglang:` to +confirm metrics are being reported correctly. + +**Troubleshooting:** + +If no metrics appear: + +1. Check logs for `AgentLoopManager` to find the server port +2. Visit `http://master_ip:server_port/metrics` to verify server metrics are available +3. Confirm that `actor_rollout_ref.rollout.disable_log_stats=False` is set + +### Step 7: View Metrics in Grafana + +After task execution, log in to Grafana to view and customize monitoring dashboards. + +**Login:** Visit `http://master_ip:3000` (default credentials: `admin/admin`) + +**Import Dashboard:** + +1. Select `Dashboards` → `New` → `Import` → `Upload dashboard JSON file` +2. Upload a pre-built dashboard JSON file + +**Available Dashboards:** + +- [vLLM Grafana Dashboard style 1](https://github.com/ArronHZG/verl-community/blob/main/docs/grafana/vllm_grafana.json) +- [vLLM Grafana Dashboard style 2](https://github.com/vllm-project/vllm/blob/main/examples/online_serving/dashboards/grafana/performance_statistics.json) +- [vLLM Grafana Dashboard style 2](https://github.com/vllm-project/vllm/blob/main/examples/online_serving/dashboards/grafana/query_statistics.json) +- [SGLang Grafana Dashboard](https://github.com/sgl-project/sglang/blob/main/examples/monitoring/grafana/dashboards/json/sglang-dashboard.json) + +## Additional Resources + +- [Ray Monitoring Documentation](https://docs.ray.io/en/latest/cluster/configure-manage-dashboard.html) +- [Prometheus Documentation](https://prometheus.io/docs/) +- [Grafana Documentation](https://grafana.com/docs/) +- [vLLM GitHub Repository](https://github.com/vllm-project/vllm) +- [SGLang GitHub Repository](https://github.com/sgl-project/sglang) diff --git a/verl_0720_main/verl/docs/advance/megatron_extension.rst b/verl_0720_main/verl/docs/advance/megatron_extension.rst new file mode 100644 index 0000000000000000000000000000000000000000..a96b15b408061d7c05368e8e3db0ba81f81fe3e2 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/megatron_extension.rst @@ -0,0 +1,20 @@ +Add models with the Megatron-LM backend +========================================= + +Last updated: 04/25/2025. + +Model +----------- + + +If use latest verl, we have direct support of ``GPTModel`` for Megatron backend. +You can use the similar way of using Megatron to pretrain custom models. +We list the steps here: + +1. Find `model_initializer.py `_ +2. If your model is configurable by ``TransformerLayerSpec`` , you can + directly use ``GPTModel``. Otherwise, Please implement a new + ``ModelLayerSpec`` and ``ModelLayer`` here. +3. Use the right ``LayerSpec`` , ``TransformerConfig`` and ``HuggingfaceConfig`` + as arguments to initialize the GPTModel. +4. Return the model at last. diff --git a/verl_0720_main/verl/docs/advance/megatron_lite_backend.rst b/verl_0720_main/verl/docs/advance/megatron_lite_backend.rst new file mode 100644 index 0000000000000000000000000000000000000000..b465754ce018b9d926374af67382ca16ac6fd5b4 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/megatron_lite_backend.rst @@ -0,0 +1,81 @@ +Megatron Lite backend +===================== + +Last updated: 06/17/2026. + +Megatron Lite (``mlite``) is Megatron's experimental, agent-friendly training +path for work that needs to move quickly. It is optimized for fast iteration, +small reviewable changes, and agentic development: model/runtime code can be +changed without touching unrelated Megatron subsystems, and new experiments can +live in their own source checkout instead of being copied into the verl tree. + +The verl integration intentionally keeps the backend glue outside this +repository. The ``mlite`` checkout provides ``megatron.lite`` and the +``verl_mlite`` launcher/config package used by the example scripts here. Put +custom extensions in your own code path, add that path through ``MLITE_ROOT`` or +``PYTHONPATH``, and keep verl focused on orchestration. See the upstream +Megatron Lite path at +`NVIDIA/Megatron-LM experimental/lite `_. + +For the ``dist_opt`` optimizer path, Megatron Lite is intended to preserve +Megatron-Core behavior rather than trade correctness for flexibility. In +deterministic runs, the ``mlite`` path has been validated against the +Megatron-Core distributed optimizer path with bitwise-aligned loss and gradient +norms, and its step time / throughput are also aligned with the Core path. + +Install the backend +------------------- + +Clone Megatron-LM's upstream ``dev`` branch and install its Megatron Lite verl +integration: + +.. code-block:: bash + + git clone -b dev https://github.com/NVIDIA/Megatron-LM.git + pip install -e Megatron-LM/experimental/lite/examples/verl + +Alternatively, keep the checkout outside the Python environment and set +``MLITE_ROOT`` when running a launcher. The scripts add both +``$MLITE_ROOT/experimental/lite`` and +``$MLITE_ROOT/experimental/lite/examples/verl`` to ``PYTHONPATH``. + +Run an example +-------------- + +The DeepSeek-V4 examples use the ``mlite`` engine for training and vLLM for +rollout where applicable: + +.. code-block:: bash + + MODEL_PATH=/path/to/deepseek-v4 \ + MLITE_ROOT=/path/to/mlite \ + OPTIMIZER=fsdp2 \ + bash examples/sft/gsm8k/run_deepseek_v4_megatron_lite.sh + +.. code-block:: bash + + MODEL_PATH=/path/to/deepseek-v4 \ + MLITE_ROOT=/path/to/mlite \ + OPTIMIZER=fsdp2 \ + bash examples/grpo_trainer/run_deepseek_v4_megatron_lite.sh + +``OPTIMIZER`` accepts ``dist_opt`` for the vanilla Megatron distributed +optimizer and ``fsdp2`` for the Megatron Lite FSDP2 wrapper. The DeepSeek-V4 +launchers default to a 128-GPU mesh with PP4, EP8, CP4, full activation +recompute, and ``fsdp2``. + +Further reading +--------------- + +For a practical discussion of long-sequence MoE RL tuning with Megatron Lite, +including memory, recompute, communication overlap, and FSDP2 trade-offs, see +`Making Long-Context MoE RL Training Easier to Tune `_. + +DeepSeek-V4 DSA note +-------------------- + +DeepSeek-V4 uses fused DSA kernels on Hopper and Blackwell GPUs. In addition to +the normal verl runtime, the critical DSA-only dependencies are +``nvidia-cutlass-dsl==4.5.2`` and ``nvidia-cudnn-frontend``. The +``nvidia-cudnn-frontend`` 1.24.1 release is sufficient for Blackwell, while +Hopper still needs a develop-branch build with ``IndexerForwardSm90`` support. diff --git a/verl_0720_main/verl/docs/advance/mtp.md b/verl_0720_main/verl/docs/advance/mtp.md new file mode 100644 index 0000000000000000000000000000000000000000..b1c4a77f117f631a6225ef997a3894213dd5b4b7 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/mtp.md @@ -0,0 +1,112 @@ +# Guide to Using MTP in SFT/RL Training and Inference + +**Author**: `https://github.com/meituan-search` + +Last updated: 02/15/2026 + +## 1. Scope of Support + +Currently, RL training can be performed on mimo-7B-RL, Qwen-next, and Deepseek series models based on the MTP architecture. The support rules for training and inference engines are as follows: + +- **Training Engine**: Only supports the `mbridge/Megatron-Bridge + megatron` combination; other training engines are not compatible at this time; + +- **Inference Engine**: Compatible with all engines, but the model must be in the corresponding engine's compatibility list; + +- **Dependency Versions**: + + - mbridge: Apply the patches and review suggestions from PR: [#62](https://github.com/ISEEKYAN/mbridge/pull/62) (Already merged into the main branch); + + - Megatron-Bridge: Apply the patches and review suggestions from PR if you want to try out mimo-7B-RL: [#2387](https://github.com/NVIDIA-NeMo/Megatron-Bridge/pull/2387) (will be merged into the main branch in the future); + + - megatron: Use the latest dev version (commit: [23e092f41ec8bc659020e401ddac9576c1cfed7e](https://github.com/NVIDIA/Megatron-LM/tree/23e092f41ec8bc659020e401ddac9576c1cfed7e)), which supports MTP + CP training methods. + + - sglang: Use the specified branch: [https://github.com/ArronHZG/sglang/tree/fix_mtp_update_weights_from_tensor](https://github.com/ArronHZG/sglang/tree/fix_mtp_update_weights_from_tensor), [PR](https://github.com/sgl-project/sglang/pull/17870) , which fix the MTP update weights from tensor OOM issue. + +## 2. MTP Training Configuration (Core Parameters) + +The MTP training process can be flexibly controlled through the following configurations. All configurations are based on the `actor_rollout_ref.model.mtp` prefix: + +| Configuration Scenario | Core Parameters | Description | +|------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------| +| Load MTP Parameters Only | `enable=True` | VRAM usage will increase, but the exported parameters include the MTP module and can be directly used for online deployment | +| Full-Parameter MTP Training | `enable=True`
`enable_train=True`
`mtp_loss_scaling_factor=0.1` | MTP Loss will apply to all model parameters | +| MTP Parameter-Only Training | `enable=True`
`enable_train=True`
`detach_encoder=True` | Freeze the Encoder layer, update only MTP module parameters, MTP Loss applies only to MTP parameters | +| MTP Accelerated Rollout | 1. vLLM configuration:
`enable=True`
`enable_rollout=True`
`method="mtp"`
`num_speculative_tokens=1`
2. SGLang configuration:
`enable=True`
`enable_rollout=True`
`speculative_algorithm="EAGLE"`
`speculative_num_steps=2`
`speculative_eagle_topk=2`
`speculative_num_draft_tokens=4` | Achieve inference acceleration during the Rollout phase based on MTP | + +## 3. Experimental Results + +The experiment was conducted as follows: + +* model = mimo-7B-math +* max_response_length = 8k + +Experiment chart: + +![fully_async_policy_revenue]( +https://github.com/ArronHZG/verl-community/blob/main/docs/mimo-7b-mtp.png?raw=true) + +The wandb link for the graph: [wandb](https://wandb.ai/hou-zg-meituan/mimo-7b-sft-mtp?nw=nwuserhouzg) + +**Scenarios with No Significant Effect** + +The following configurations will not have a noticeable impact on training results: + +1. The base model does not carry MTP parameters; + +2. The base model carries MTP parameters, but the MTP module is not trained; + +3. The base model carries MTP parameters and trains MTP, with `mtp_loss_scaling_factor=0`; + +4. The base model carries MTP parameters, trains MTP and detaches the encoder, with `mtp_loss_scaling_factor=0.1`. + +**Scenarios with Significant Effect** + +Only the following configuration will have a noticeable impact on training results: + +- The base model carries MTP parameters, MTP Loss applies to all model parameters, and `mtp_loss_scaling_factor=0.1`. + +**Recommended Training Method** + +It is recommended to adopt the `detach_encoder=True` approach for MTP training. + +## 4. Performance Notes for MTP in Rollout Inference + +Enabling MTP improves the rollout acceptance rate by around 14%. However, on H20 GPUs, overall throughput does not increase and even decreases slightly. + +![spec_log]( +https://github.com/ArronHZG/verl-community/blob/main/docs/spec_log.png?raw=true) + +The effectiveness of MTP-accelerated Rollout is significantly affected by **model size** and **inference hardware**. Key reference information is as follows: + +**Hardware Tensor Core Performance** + +| Hardware Model | FP16 Performance (TFLOPS) | +|----------------|---------------------------| +| H20 | 148 | +| H800 | 1,671 | +| H200 | 1,979 | + +**Measured Performance and Recommendations** + +Taking the mimo-7B model deployed separately on H20 hardware using SGLang as an example: After enabling MTP speculative decoding, the Rollout throughput decreases by approximately 50%. + +- Current priority recommendation: Do not enable MTP acceleration during the inference phase for now; + +- Future planning: Further optimization of the speculative logic in the Rollout phase will be conducted to improve throughput performance. + +## 5. SFT training + +The SFT training with MTP is supported, using the same MTP training configuration as RL training. + +An example configuration for running SFT can be found in `examples/sft/gsm8k/run_mimo_7b_mtp_megatron.sh` + +**SFT result** + +The experiment was conducted using following data: +- model = mimo-7B-math +- dataset = gsm8k + +The result: [wandb link](https://wandb.ai/hou-zg-meituan/mimo-7b-sft-mtp?nw=nwuserhouzg) + +The presence of mtp layer has limited effect on main loss. However, when MTP layer is detached, the mtp_loss converges to a higher value. + diff --git a/verl_0720_main/verl/docs/advance/one_step_off.md b/verl_0720_main/verl/docs/advance/one_step_off.md new file mode 100644 index 0000000000000000000000000000000000000000..165f7cf785de64a4e7b8bfce4db21d17ad218a62 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/one_step_off.md @@ -0,0 +1,326 @@ +# Recipe: One Step Off Policy Async Trainer + +**Author:** `https://github.com/meituan-search` + +Last updated: 07/17/2025. + +## Introduction + +### Background + +The current reinforcement learning training process implemented by verl is synchronous, adhering to the algorithmic +workflows of established methods like PPO, GRPO, and DAPO. In each step, training samples are generated by the latest +model, and the model is updated after training completes. While this approach aligns with off-policy reinforcement +learning and stabilizes RL training, but it suffers from severe efficiency issues. +Model updates must wait for the longest output in the generation phase to complete. +During the generation of long-tail samples, GPUs remain idle, resulting in significant underutilization. +The more severe the long-tail problem in sample generation, the lower the overall training efficiency. +For example, in DAPO 32B training, the Rollout phase accounts for approximately 70% of the total time, +and increasing resources does not reduce the Rollout duration. + +![DAPO 32B Math Performance](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/dapo_32b_math.png) + +> source data: https://wandb.ai/verl-org/DAPO%20Reproduction%20on%20verl/workspace?nw=nwusertongyuxuan361 + +### Solution + +We have implemented the **One Step Off Async Trainer** to help alleviate this issue. This approach parallelizes the +generation and training processes, utilizing samples generated in the previous step for current training. +It also involves appropriately partitioning resources, allocating dedicated resources for generation while automatically +assigning the remainder to training. By reducing resources allocated to the generation phase, we mitigate GPU idle time +during long-tail sample generation. Throughout this process, generation and training parameters maintain a one-step off +policy. + +![One Step Off Policy Diagram](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/one_step_off_policy.png) + +> reference: [AReaL: A Large-Scale Asynchronous Reinforcement Learning System for Language Reasoning](https://arxiv.org/abs/2505.24298) + +Our core contributions include: + +1. **Parallel Generation and Training**: + Samples for the next batch are asynchronously generated while the current batch is being trained. + +2. **Resource Isolation**: + Unlike `hybrid_engine`, this method requires explicit resource allocation for rollout, with remaining resources + automatically assigned to training. + +3. **NCCL Parameter Synchronization**: + Employs NCCL communication primitives for seamless parameter transfer between generation and training modules. + +### Experimental Results + +- **Machine Configuration**: 2 nodes with 16 H20 GPUs each + - Generation: 4 GPUs + - Training: 12 GPUs +- **Model**: Qwen2.5-Math-7B +- **Rollout Configuration**: +- **Max Response Length**: FSDP2: 20,480 tokens; Megatron: 8,192 tokens +- **Algorithm**: DAPO +- **Rollout Engine**: vLLM + +| training mode | engine | step | gen | wait_prev_gen | generate_sequences | old_log_prob | update_actor | total time | acc/best@32/mean | acc/maj@32/mean | +| ---------------------- | ------------- | ---- | --- | ------------- | ------------------ | ------------ | ------------ | -------------- | ---------------- | --------------- | +| colocate sync | VLLM+FSDP2 | 749 | 321 | - | 247 | 88 | 286 | 19h18m | 0.5948 | 0.417 | +| one-step-overlap async | VLLM+FSDP2 | 520 | - | 45 | 458 | 108 | 337 | 15h34m(+23%) | 0.6165 | 0.494 | +| colocate sync | VLLM+Megatron | 699 | 207 | - | 162 | 119 | 344 | 18h21m | 0.605 | 0.4217 | +| one-step-overlap async | VLLM+Megatron | 566 | - | 59 | 501 | 120 | 347 | 13h06m (+40%) | 0.6569 | 0.4038 | + +- colocate sync: step ≈ gen + old_log_prob + update_actor +- one-step-overlap async: step ≈ wait_prev_gen + old_log_prob + update_actor + +![One Step Off Megatron Performance](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/docs/one_step_off_megatron.png) + +> source data: https://wandb.ai/hou-zg-meituan/one-step-off-policy?nw=nwuserhouzg + +## Implementation + +### One Step Off Policy Async Pipeline + +Our implemented **One Step Off Policy Async Pipeline** integrates seamlessly into existing training logic at minimal +cost, +eliminating the need for additional sample storage management. The core mechanism uses `async_gen_next_batch` +for asynchronous rollout generation while maintaining continuous operation during epoch transitions +via `create_continuous_iterator`. + +```python +# iterator generator, simplify one-step integration of the training process +def _create_continuous_iterator(self): + for epoch in range(self.config.trainer.total_epochs): + iterator = iter(self.train_dataloader) + for batch_dict in iterator: + yield epoch, batch_dict + + +# read next batch samples, parameters sync and launch asyn gen_seq +def _async_gen_next_batch(self, continuous_iterator): + # read train_data + try: + epoch, batch_dict = next(continuous_iterator) + except StopIteration: + return None + batch = DataProto.from_single_dict(batch_dict) + gen_batch = batch_pocess(batch) + # sync weights from actor to rollout + self.sync_rollout_weights() + # async generation + gen_batch_output = self.rollout_wg.async_generate_sequences(gen_batch) + # future encapsulated + return GenerationBatchFuture(epoch, batch, gen_batch_output) + + +continuous_iterator = self._create_continuous_iterator() +# run rollout first to achieve one-step-off +batch_data_future = self._async_gen_next_batch(continuous_iterator) + +while batch_data_future is not None: + # wait for the gen_seq result from the previous step + batch = batch_data_future.get() + # launch the next async call to generate sequences + batch_data_future = self._async_gen_next_batch(continuous_iterator) + + # compute advantages + batch = critic.compute_values(batch) + batch = reference.compute_log_prob(batch) + batch = reward.compute_reward(batch) + batch = compute_advantages(batch) + + # model update + critic_metrics = critic.update_critic(batch) + actor_metrics = actor.update_actor(batch) +``` + +### Parameter Synchronization + +The exciting point is that our nccl based weights updating for rollout model has great performance. +At most of time, the latency is under 300ms, which is negligible for RLHF. + +> **sync_rollout_weights**:The time for synchronizing parameters from actor to rollout is extremely fast and can almost +> be ignored because it is implemented with nccl. + +```python +class ActorRolloutRefWorker: + # actor acquires the meta-info of model parameters for parameter sync + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def get_actor_weights_info(self): + params = self._get_actor_params() + ret = [] + for key, tensor in params.items(): + ret.append((key, tensor.size(), tensor.dtype)) + self._weights_info = ret + return ret + + # rollout sets the meta-info of model parameters for parameter sync + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def set_actor_weights_info(self, weights_info): + self._weights_info = weights_info + + +class AsyncRayPPOTrainer(RayPPOTrainer): + def init_workers(self): + ... + # rollout obtains the meta-info of model parameters from the actor for parameter sync + weights_info = self.actor_wg.get_actor_weights_info()[0] + self.rollout_wg.set_actor_weights_info(weights_info) + + # Create an actor-rollout communication group for parameter sync + self.create_weight_sync_group +``` + +```python +# The driving process invokes the actor and rollout respectively to create a weight synchronization group based on nccl/hccl. +def create_weight_sync_group(self): + master_address = ray.get(self.actor_wg.workers[0]._get_node_ip.remote()) + master_port = ray.get(self.actor_wg.workers[0]._get_free_port.remote()) + world_size = len(self.actor_wg.workers + self.rollout_wg.workers) + self.actor_wg.create_weight_sync_group( + master_address, + master_port, + 0, + world_size, + ) + ray.get( + self.rollout_wg.create_weight_sync_group( + master_address, + master_port, + len(self.actor_wg.workers), + world_size, + ) + ) + +# drive process call the actor and rollout respectively to sync parameters by nccl +def sync_rollout_weights(self): + self.actor_wg.sync_rollout_weights() + ray.get(self.rollout_wg.sync_rollout_weights()) + + +# fsdp model parameter sync +@register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) +def sync_rollout_weights(self): + params = self._get_actor_params() if self._is_actor else None + if self._is_rollout: + inference_model = ( + self.rollout.inference_engine.llm_engine.model_executor.driver_worker.worker.model_runner.model + ) + from verl.utils.vllm.patch import patch_vllm_moe_model_weight_loader + patch_vllm_moe_model_weight_loader(inference_model) + # Model parameters are broadcast tensor-by-tensor from actor to rollout + for key, shape, dtype in self._weights_info: + tensor = torch.empty(shape, dtype=dtype, device=get_torch_device().current_device()) + if self._is_actor: + assert key in params + origin_data = params[key] + if hasattr(origin_data, "full_tensor"): + origin_data = origin_data.full_tensor() + if torch.distributed.get_rank() == 0: + tensor.copy_(origin_data) + from ray.util.collective import collective + + collective.broadcast(tensor, src_rank=0, group_name="actor_rollout") + if self._is_rollout: + inference_model.load_weights([(key, tensor)]) +``` + +### PPO Correctness + +To ensure the correctness of the PPO algorithm, we use rollout log_probs for PPO importance sampling. +For the related algorithm details, please refer to: https://verl.readthedocs.io/en/latest/algo/rollout_corr_math.html +The default mode is `bypass_ppo_clip`, but other modification strategies can also be explored. + +### AgentLoop + +In the current implementation, we no longer provide SPMD model rollout mode. +Instead, we have switched to AgentLoop mode, which also supports multi-turn tool calling. + +## Usage + +### FSDP2 Configuration Example + +```shell +python3 -m verl.experimental.one_step_off_policy.async_main_ppo \ + --config-path=config \ + --config-name='one_step_off_ppo_trainer.yaml' \ + actor_rollout_ref.actor.strategy=fsdp2 \ + # actor and rollout are placed separately + actor_rollout_ref.hybrid_engine=False \ + # actor and rollout resource + trainer.nnodes=1 \ + trainer.n_gpus_per_node=6 \ + rollout.nnodes=1 \ + rollout.n_gpus_per_node=2 +``` + +### Megatron Configuration Example + +```shell +python3 -m verl.experimental.one_step_off_policy.async_main_ppo \ + --config-path=config \ + --config-name='one_step_off_ppo_megatron_trainer.yaml' \ + actor_rollout_ref.actor.strategy=megatron \ + # actor and rollout are placed separately + actor_rollout_ref.hybrid_engine=False \ + # actor and rollout resource + trainer.nnodes=1 \ + trainer.n_gpus_per_node=6 \ + rollout.nnodes=1 \ + rollout.n_gpus_per_node=2 +``` + +### Configuration Guidelines + +1. **Card Number Relationships** + Maintain either of these relationships for optimal batch distribution: + + - `actor_rollout_ref.rollout.n` should be an integer divisor of: + `trainer.n_gpus_per_node * trainer.nnodes` + - `actor_rollout_ref.rollout.n * data.train_batch_size` should be evenly divisible by: + `trainer.n_gpus_per_node * trainer.nnodes` + + > Rationale: Ensures training samples can be evenly distributed across training GPUs when using partial resources for + > generation. + +2. **Dynamic Resource Tuning** + Adjust `trainer.nnodes` `trainer.n_gpus_per_node` `rollout.nnodes` `rollout.n_gpus_per_node` based on phase + durations: + - **Ideal state**: Rollout and training phases have comparable durations + - **Diagnostic metrics**: + - Monitor `wait_prev_gen` duration + - Analyze `sequence_length` distribution + - **Adjustment strategy**: + - High `wait_prev_gen` + uniform sequence lengths → Increase rollout resources + - High `wait_prev_gen` + long-tail sequences → Optimize stopping criteria (resource increase won't help) + > **wait_prev_gen**:The time consumed waiting for the previous rollout to end (the part that is not fully + > overlapped). + > **Resource Configuration Strategies:** + - **Resource-constrained scenario**: Optimize resource utilization by adjusting GPU allocation ratios, + keeping the number of nodes equal to allow training and rollout to share nodes; + - Configure `trainer.nnodes = rollout.nnodes` with + `trainer.n_gpus_per_node + rollout.n_gpus_per_node = physical_gpus_per_node`. Control rollout resource + allocation by adjusting `n_gpus_per_node`. + - **Resource-abundant scenario**: Optimize performance by adjusting the number of nodes, + keeping the number of GPUs per node equal to enable independent scaling of training and rollout + parallelism. + - Configure `trainer.n_gpus_per_node = rollout.n_gpus_per_node` and control rollout resource allocation by + adjusting `trainer.nnodes` and `rollout.nnodes`to achieve optimal performance. + > **Note**: The total number of nodes required by the system is not simply `trainer.nnodes + rollout.nnodes`. The + > actual calculation depends on GPU capacity: + > + > - When `trainer.n_gpus_per_node + rollout.n_gpus_per_node <= physical_gpus_per_node`, + > the required node count is `max(trainer.nnodes, rollout.nnodes)` + > - When `trainer.n_gpus_per_node + rollout.n_gpus_per_node > physical_gpus_per_node`, + > the required node count is `trainer.nnodes + rollout.nnodes` + +### Delta Weight Sync + +For disaggregated runs the trainer→rollout weight broadcast can ship only the changed parameters +(a *delta*) instead of the full weights. See the dedicated design doc: +[Delta Weight Sync](delta_weight_sync.md), covering the ``delta`` and ``delta_sharded`` +checkpoint-engine backends, their configuration, and the roadmap (Megatron, fp8). + +## Functional Support + +| Category | Support Situation | +| ------------------ | --------------------------------------------------------------------------------------------------------------- | +| train engine | FSDP2
Megatron | +| rollout engine | vLLM | +| AdvantageEstimator | GRPO
GRPO_PASSK
REINFORCE_PLUS_PLUS
RLOO
OPO
REINFORCE_PLUS_PLUS_BASELINE
GPG | +| Reward | all | diff --git a/verl_0720_main/verl/docs/advance/placement.rst b/verl_0720_main/verl/docs/advance/placement.rst new file mode 100644 index 0000000000000000000000000000000000000000..ae944ec3c53384c3dffbb10635948cea9c262cad --- /dev/null +++ b/verl_0720_main/verl/docs/advance/placement.rst @@ -0,0 +1,13 @@ +Ray API Design Tutorial +======================================= + +Last updated: 10/30/2024. + +We provide a tutorial for our Ray API design, including: + +- Ray basic concepts +- Resource Pool and RayWorkerGroup +- Data Dispatch, Execution and Collection +- Initialize the RayWorkerGroup and execute the distributed computation in the given Resource Pool + +See details in `tutorial.ipynb `_. \ No newline at end of file diff --git a/verl_0720_main/verl/docs/advance/ppo_lora.rst b/verl_0720_main/verl/docs/advance/ppo_lora.rst new file mode 100644 index 0000000000000000000000000000000000000000..23c5a21c6783bfe86e9bc65c47c4f2b5bfc64272 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/ppo_lora.rst @@ -0,0 +1,215 @@ +RL(HF) algorithms with LoRA Support +=========================================== + +Last updated: 02/03/2026. + +We support LoRA (Low-Rank Adaptation) for reinforcement learning algorithms such as PPO, GRPO, and others. + +LoRA is a parameter-efficient fine-tuning technique that injects trainable low-rank matrices into pre-trained weights (typically linear layers). This reduces memory footprint and compute cost, making it possible to fine-tune large models with limited hardware. + +The benefits this brings include: + +- reinforcement learning with very large models (e.g. 70B+) with modest hardware (e.g. 8x80G GPUs), +- enable larger batch sizes due to reduced memory usage, +- simplify model transfer and deployment, as only LoRA adapters need to be saved, +- Combine with techniques like `SLoRA `_ or `CCoE `_ to serve multiple LoRA adapters efficiently + +This guide explains how to enable LoRA in RL training and configure related parameters. + +FSDP Backend Usage Guide +------------------------ + +.. note:: + + This section applies to **FSDP/FSDP2 backend only**. For Megatron backend, see the :ref:`megatron-lora` section below. + +1. Lora is available in the `verl.trainer.ppo.ray_trainer.RayPPOTrainer`. Examples are provided via the `verl.trainer.main_ppo` entry point. + +2. LoRA is supported via huggingface peft with fsdp/fsdp2 and both vllm and sglang rollout backends. + +- `strategy=fsdp` or `strategy=fsdp2` +- `rollout.name=vllm` or `rollout.name=sglang` + +3. Required configurations for LoRA: + +- `actor_rollout_ref.model.lora_rank`: int, set to a reasonable value greater than 0 (e.g., 8, 16, 32, 64) +- `actor_rollout_ref.model.lora_alpha`: float, the alpha term in LoRA +- `actor_rollout_ref.rollout.load_format="safetensors"`: required. This enables vLLM to load the base model. +- `actor_rollout_ref.model.target_modules`: the target modules for LoRA. Typically set to "all-linear". + +4. Optional configurations for LoRA: + +- `actor_rollout_ref.model.lora_adapter_path`: string, path to a pretrained LoRA adapter directory. + If provided, loads existing adapter instead of creating new one. Enables multi-stage training from previously saved adapters. + Directory need contain `adapter_model.safetensors` and `adapter_config.json`. +- `actor_rollout_ref.model.lora.merge`: bool, whether to merge LoRA adapters into the base model weights before transferring to the rollout engine (vLLM or SGLang). + If True, LoRA adapters are merged into base weights and full merged weights are synced. If False, only LoRA adapter deltas are transferred natively. + For SGLang, ``merge=True`` is currently required. Native adapter loading (``merge=False``) for SGLang is planned. + +5. Recommend options: + +- `actor_rollout_ref.model.use_shm=True`: preload the model into `/dev/shm` to improve model loading speed. +- `actor_rollout_ref.rollout.layered_summon=True`: this enables the actor-model to gather the FSDP shards per layers when synchronizing the LoRA Adapter to vLLM, thereby reducing GPU peak memory. Recommended if the model is very large (70B+) or the GPU memory is limited (< 48GB) + +.. _megatron-lora: + +Megatron Backend Usage Guide +---------------------------- + +.. warning:: + + The FSDP-specific config options are **NOT applicable** to Megatron backend, and they will be ignored if set. Only options listed under ``lora`` key are applicable: + + - ``actor_rollout_ref.model.lora.*`` + - ``critic.model.lora.*`` + +You need to install and enable Megatron-Bridge for Megatron LoRA support. + +Make sure you use Megatron-Bridge later than 0.2.0, and we recommended using 0.5.0 or later for proper support, and use the following settings to enable Megatron-Bridge: + +- ``actor_rollout_ref.actor.megatron.use_mbridge=True`` +- ``actor_rollout_ref.actor.megatron.vanilla_mbridge=False`` + +**Key Differences from FSDP LoRA:** + +1. **LoRA Implementation**: Verl Megatron backend uses Megatron-Bridge's native LoRA implementation, which differs from HuggingFace PEFT. + +2. **Weight Sync / Refit Mechanism**: Currently, Megatron-Bridge can support syncing weights by either merging LoRA adapters into the base model weights before transferring to vLLM (for better inference speed but more refit time and potential precision loss), as well as loading separate adapters. + +**Configuration for Megatron LoRA:** + +.. code-block:: yaml + + actor_rollout_ref: + model: + lora: + # LoRA type: "lora", "vlm_lora", "canonical_lora", or "dora" + type: lora + + # whether to sync weights / refit by either merging LoRA adapters into the base model weights before transferring to vLLM (for better inference speed but more refit time and potential precision loss). If this is False, it will load separate adapters. + merge: False + + # LoRA rank (Dimension of the low-rank projection space.). Set to 0 to disable LoRA + rank: 0 + + # Weighting factor for the low-rank projection. Defaults to 32 + alpha: 32 + + # Dropout rate for the low-rank projection. Defaults to 0.0 + dropout: 0.0 + + # A list of module names to apply LoRA to. + # For fused LoRA, Defaults to all linear layers ['linear_qkv', 'linear_proj', 'linear_fc1', 'linear_fc2']. + # For canonical LoRA: ["linear_q", "linear_k", "linear_v", "linear_proj", "linear_fc1_up", "linear_fc1_gate", "linear_fc2"] + # - 'linear_qkv': Apply LoRA to the fused linear layer used for query, key, and value projections in self-attention + # - 'linear_proj': Apply LoRA to the linear layer used for projecting the output of self-attention + # - 'linear_fc1': Apply LoRA to the first fully-connected layer in MLP + # - 'linear_fc2': Apply LoRA to the second fully-connected layer in MLP + # Target modules can also contain wildcards. For example, you can specify + # target_modules=['*.layers.0.*.linear_qkv', '*.layers.1.*.linear_qkv'] to add LoRA to only linear_qkv on the first two layers + # + # Note: + # For MLA (e.g., DeepSeek), you should use ["linear_kv_down_proj","linear_kv_up_proj","linear_q_down_proj","linear_q_up_proj","linear_q_proj"] + # Instead of "linear_qkv" or ["linear_q","linear_k","linear_v"] + # By default, MoE routers are excluded from LoRA adaptation, and you will need to specify "router" in target_modules to include them. + target_modules: + - linear_qkv + - linear_proj + - linear_fc1 + - linear_fc2 + + # A list of module names not to apply LoRa to. It will match all nn.Linear & nn.Linear-adjacent modules whose name + # does not match any string in exclude_modules. If used, will require target_modules to be empty list or None + exclude_modules: [] + + # Position for applying dropout, can be 'pre' (before the low-rank projection) or 'post' (after). Defaults to 'pre' + dropout_position: pre + + # Initialization method for the low-rank matrix A. Defaults to "xavier". + lora_A_init_method: xavier + + # Initialization method for the low-rank matrix B. Defaults to "zero". + lora_B_init_method: zero + + # Enables the experimental All-to-All (A2A) communication strategy. Defaults to False + a2a_experimental: False + + # Parameter data type for LoRA weights. Default to null, which will use model's dtype. + dtype: null + + # Path to pre-trained LoRA adapter weights (null to train from scratch) + adapter_path: null + + # Whether to fully shard LoRA adapters. Defaults to False + # https://docs.vllm.ai/en/latest/api/vllm/config/lora/#vllm.config.lora.LoRAConfig.fully_sharded_loras + fully_sharded_loras: bool + + # VLMLoRA additionally allows the user to specify whether the language or vision models should be frozen. + # For example, a common finetuning workload for multimodal models is to apply adapters to language model and fully + # finetune the vision model. + freeze_vision_model: True + freeze_vision_projection: True + freeze_language_model: True + +LoRA training experiment with Qwen3-8B on 8 * H200 single node comparing FSDP and Megatron backend (script adapted from examples/tuning/lora/run_qwen3_8b_fsdp.sh): + +.. image:: https://github.com/user-attachments/assets/0482f423-01a3-4e52-a7ee-8b9cd79b7b1a +.. image:: https://github.com/user-attachments/assets/6ce10400-8164-47d8-90a6-c1bf002fb9e8 +.. image:: https://github.com/user-attachments/assets/092d3a43-4eba-425e-a584-8d83c1f02de4 + + +Best Practices and Notes +------------------------- + +1. **Learning rate**: it is recommended to increase the value of learning rate by an order of magnitude. + +2. **LoRA Rank**: + +- Too small a rank can hurt convergence. +- LoRA rank recommendation from @thelongestusernameofall: + + - A very small lora_rank can lead to slower convergence or worse training performance. It is recommended to set lora_rank to be>=32. Tests have shown that for a 0.5B model, with lora_rank=32,the training convergence speed and final performance are almost identical to non-LoRA training + - For a 32B model,with lora_rank=128,the training convergence speed and final performance are also almost identical to non-LoRA training. + - More comprehensive reference results are coming soon. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/f2b80b8b26829124dd393b7a795a0640eff11644/docs/lora.jpg?raw=true + +3. **FSDP-Specific:** Reference configuration for RL training with the Qwen2.5-72B model using 8 x 80GB GPUs (increase lora_rank if needed): + +.. code-block:: + + data.train_batch_size=64 \ + actor_rollout_ref.model.use_shm=True \ + actor_rollout_ref.model.lora_rank=32 \ + actor_rollout_ref.model.lora_alpha=32 \ + actor_rollout_ref.model.target_modules=all-linear \ + actor_rollout_ref.actor.optim.lr=3e-5 \ + actor_rollout_ref.actor.fsdp_config.fsdp_size=8 \ + actor_rollout_ref.actor.fsdp_config.param_offload=True \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=True \ + actor_rollout_ref.rollout.tensor_model_parallel_size=8 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.rollout.n=5 \ + actor_rollout_ref.rollout.max_num_seqs=64 \ + actor_rollout_ref.rollout.max_model_len=1536 \ + actor_rollout_ref.rollout.max_num_batched_tokens=1536 \ + actor_rollout_ref.rollout.load_format=safetensors \ + actor_rollout_ref.rollout.layered_summon=True \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + actor_rollout_ref.actor.ulysses_sequence_parallel_size=1 \ + +Example Scripts +------------------- + +For end-to-end examples, refer to the scripts below: + +**FSDP Examples:** + +- LoRA training from scratch: examples/tuning/lora/run_qwen3_8b_fsdp.sh +- LoRA training from adapter path: examples/tuning/lora/run_qwen3_8b_from_adapter_fsdp.sh +- LoRA training for VLMs: examples/tuning/lora/run_qwen2_5_vl_7b_fsdp.sh + +**Megatron Examples:** + +- LoRA training with MoE: examples/tuning/lora/run_qwen3_30b_a3b_megatron.sh diff --git a/verl_0720_main/verl/docs/advance/reward_loop.rst b/verl_0720_main/verl/docs/advance/reward_loop.rst new file mode 100644 index 0000000000000000000000000000000000000000..36dedcd5d16528e9c328b5714c27a4d690407481 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/reward_loop.rst @@ -0,0 +1,308 @@ +Reward Loop +=========== + +.. _yyding: https://yyding1.github.io + +Author: `Yuyang Ding `_ + +Last updated: 2/10/2026. + +Introduction +------------ + +Reward Loop is the default reward computation implementation in verl. +It is designed to support efficient, flexible, and easy-to-use reward computation. + +This document introduces the usage and architectural design. + +Key features include: + +1. **Distributed reward manager**, enabling scalable and efficient reward computation. +2. **Support for hybrid reward settings**, including both generative and discriminative reward models, as well as more complex reward scenarios. +3. **Simple and extensible interface**, for easily defining customized reward functions. + +Distributed Reward manager +-------------------------- + +.. image:: https://github.com/yyDing1/verl-materials/blob/main/distributed_reward_manager.svg?raw=true + +How distributed +~~~~~~~~~~~~~~~ + +Under the single_controller setup, actor rollout and reward computation can be abstracted as: + +.. code:: python + + # initalize rollout manager and async reward loop manager + async_rollout_manager = AgentLoopManager(config) + async_reward_manager = RewardLoopManager(config) + # actor rollout using `async_rollout_manager` + gen_batch = async_rollout_manager.generate_sequences(batch) + # compute reward using `async_reward_manager` + reward_batch = async_reward_manager.compute_rm_score(gen_batch) + +Within the ``RewardLoopManager``, multiple ``RewardWorker`` are launched across all nodes to enable distributed reward computation. +The number of parallel workers can be configured via ``config.reward.num_workers``. + +Upon receiving a batch reward request, the batch is partitioned into smaller chunks and distributed to each reward worker for parallel execution. +User only need to invoke ``compute_rm_score``. + +.. code:: python + + class RewardLoopManager: + """ + RewardLoopManager run in single controller. + This class will create reward loop workers and manage them. + """ + def _init_reward_loop_workers(self): + self.reward_loop_workers = [...] + + def compute_rm_score(self, data): + chunks = data.chunk(len(self.reward_loop_workers)) + outputs = ray.get( + [ + worker.compute_score_batch.remote(chunk) + for worker, chunk in zip(self.reward_loop_workers, chunks, strict=True) + ] + ) + outputs_flat = [item for sublist in outputs for item in sublist] + ... + +This is how the reward manager is parallelized and distributed across all nodes. + +Streaming Reward with Rollout +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Furthermore, we check whether actor rollout and reward computation can be performed in a streaming manner, +where the reward is calculated as soon as each sample is rolled out. + +.. code:: python + + # agent_reward_loop: streaming reward computation with actor rollout + # two conditions satisfied: (1) rule-based reward, or (2) reward model with extra resource pool + enable_agent_reward_loop = not use_rm or config.reward.reward_model.enable_resource_pool + + # if enable_agent_reward_loop, we directly pass reward_loop_workers to agent loop manager + # to stream reward computation with actor rollout + reward_loop_worker_handles = async_reward_manager.reward_loop_workers if enable_agent_reward_loop else None + async_rollout_manager = AgentLoopManager( + config=config, + worker_group=actor_rollout_wg, + rollout_resource_pool=actor_rollout_resource_pool, + reward_loop_worker_handles=reward_loop_worker_handles, + ) + +Hybrid Reward Scenarios Usage +----------------------------- + +As described above, each ``reward_loop_worker`` is responsible for handling reward requests. +The rewards can be categorized as follows: + +- **Rule-based Reward**: The reward is determined by predefined rules, e.g., checking whether the predicted answer matches the ground truth via string matching. +- **Discriminative Reward Model (DisRM)**: The reward is produced by a specified discriminative reward model, such as ``Skywork/Skywork-Reward-Llama-3.1-8B-v0.2``. +- **Generative Reward Model (GenRM)**: The reward is obtained using a generative reward model, for example ``dyyyyyyyy/FAPO-GenRM-4B``. +- **Hybrid Reward Scenarios**: A combination of the above reward types, e.g., rule + GenRM. + +.. code:: python + + class RewardLoopWorker: + + async def compute_score_batch(self, data: DataProto) -> list[dict]: + tasks = [] + for i in range(len(data)): + tasks.append(asyncio.create_task(self.compute_score(data[i : i + 1]))) + outputs = await asyncio.gather(*tasks) + return outputs + + async def compute_score(self, data: DataProto) -> dict: + if self.config.reward.custom_reward_function.path is not None: + # directly use user-customized reward function + return await self.reward_manager.run_single(data) + else: + if self.config.reward.reward_model.enable: + # we assume the rm is disrm + # genrm must set custom_reward_function + return await self.compute_score_disrm(data[-1:]) # only pass the last output to discriminative reward model + else: + return await self.reward_manager.run_single(data) + +Each ``RewardLoopWorker`` will initalize one ``RewardManager``, splits the batch into individual data items and processes them in parallel using asynchronous tasks. + +Reward Manager +~~~~~~~~~~~~~~ + +The ``RewardManager`` maintains a reward function and defines its computation logic, including: + +- **naive**: The simplest implementation. +- **dapo**: DAPO implementation with an overlong reward penalty. +- **limit**: Restricts the concurrency of the reward function, useful when external API calls are rate-limited. +- **remote**: Runs in a separate process, effective for CPU-intensive tasks such as ``Math-Verify``. + +Users can also customize their own ``RewardManager``, inheriting from ``RewardManagerBase``, and implementing the ``run_single`` function. + +.. code:: python + + @register("user_costomized") + class UserCostomizedRewardManager(RewardManagerBase): + async def run_single(self, data: DataProto) -> dict: + data_item = data[-1] + # your own reward manager + ... + +After defining it, users can specify their custom reward manager by setting ``reward.reward_manager.name=user_costomized``. + +When trajectories consist of multiple output sequences (currently supported only by the ``main_ppo_sync`` trainer) reward managers may consider all outputs when computing their scores. +In that case, the ``data`` argument passed to ``run_single`` will contain all outputs in the trajectory. +However, the default reward managers (e.g. ``naive``, ``dapo``, etc.) will only consider the last sequence by default, as they are typically designed for single-output tasks. +The same is true in the ``UserCostomizedRewardManager`` example above, as indicated by the line ``data_item = data[-1]``. + + +Rule-Based Reward +~~~~~~~~~~~~~~~~~ + +If ``reward.custom_reward_function`` is provided, the user-defined reward function will be used. Otherwise, it falls back to the default reward function. + +Note that The custom function can be either synchronous or asynchronous; the system automatically detects its type and loads it accordingly. + +We recommend **using asynchronous functions** when reward computation need to involve external model API calls or sandboxed execution, as they are significantly more efficient. + +.. code:: python + + async def compute_score(data_source, solution_str, ground_truth, extra_info): + """Compute a score by sending an async request to a remote service.""" + + # prepare request payload + payload = {"messages": [{"role": "user", "content": "check the correcness of the question and response ..."}], ...} + + # send async HTTP request + async with aiohttp.ClientSession() as session: + async with session.post("https://api.openai.com/v1/chat/completions", json=payload) as resp: + result = await resp.json() + + # parse and return score + score = int(result["choices"][0]["message"]["content"].strip().split("\n")[-1]) + return {"score": score} + +Model-Base Reward +~~~~~~~~~~~~~~~~~ + +**For discriminative reward model (DisRM)**, we provide a simple implementation: + +.. code:: python + + class RewardLoopWorker: + async def compute_score_disrm(self, data) -> dict: + disrm_prompt = await self._preprocess_reward_inputs(data) + payloads = { + "model": model_name, + "input": disrm_prompt, + "activation": False, + } + output = await self._post_request(payloads, "classify") + rm_score = output["data"][-1]["probs"][-1] + return {"reward_score": rm_score} + +pass the question and the model rollout as inputs to the reward model and obtain a reward score. This is also the standard practice for most DisRM. + +Users should provide ``reward.reward_model.model_path`` to specify the reward model. + +**For generative reward model (GenRM)** + +For generative reward model scenarios, users need to specify both ``reward.reward_model.model_path`` and ``reward.custom_reward_function``. + +The custom reward function should implement the following components: + +- Convert the question and the model rollout into a GenRM input prompt using a custom prompt template. +- Invoke the GenRM to perform generation with custom sampling parameters. For this purpose, the Reward Loop provides an HTTP interface (i.e., ``reward_router_address``) for interacting with GenRM. +- Parse the GenRM output using a custom parser and extract the reward score. + +As these steps are highly customizable and task-dependent, we offer this flexibility entirely to the user-defined reward function. + +Below we provide an example of a custom reward function using GenRM. + +.. code:: python + + async def compute_score_gsm8k( + data_source: str, + solution_str: str, + ground_truth: str, + extra_info: dict, + reward_router_address: str, # an HTTP router endpoint provided by Reward Loop + reward_model_tokenizer: PreTrainedTokenizer, + ): + """Compute the reward score.""" + + # Step 1: Prepare prompt and request payload + grm_prompt = GRM_PROMPT_TEMPLATE.format(problem=extra_info["question"], solution=solution_str) + messages = [{"role": "user", "content": grm_prompt}] + sampling_params = {"temperature": 0.7, "top_p": 0.8, "max_tokens": 4096} + chat_complete_request = {"messages": messages, **sampling_params} + + # Step 2: Send async request to the reward model + # here, chat_complete sends async http request to the router address + result = await chat_complete( + router_address=reward_router_address, + chat_complete_request=chat_complete_request, + ) + + # Step 3: Parse model response and extract score + grm_response = result.choices[0].message.content.strip() + try: + score_str = grm_response.split("\n\n")[-1].strip() + score = int(score_str) + except Exception: + score = 0 + + return {"score": score} + +**For hybrid reward scenarios**, such as combining rule-based rewards with GenRM similarly as above, + +.. _recipe/fapo: https://github.com/verl-project/verl-recipe/tree/main/fapo + +A runnable and reproducible example that demonstrates how to use a rule-based reward function together with a GenRM is provided in the `recipe/fapo`_ directory for reference. Welcome to use and cite. + +Reward Model Arch Design +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +We support multiple execution modes for reward models during: + +- **Colocate Mode**: The reward model shares the same resource pool as the actor/rollout/reference models. In this setup, all rollouts must complete first, after which the reward model is awakened to perform inference. +- **Standalone Mode**: The reward model runs on a separate resource pool, independent from the actor/rollout/reference models. In this setup, each sample is evaluated by the reward model immediately after its rollout finishes. + +The standalone mode can enable the streaming manner stated above. + +By default, the system runs in colocate mode. Users can enable standalone mode by setting ``reward.reward_model.enable_resource_pool=True`` and allocating the corresponding resources via ``reward.reward_model.nnodes`` and ``reward.reward_model.n_gpus_per_node``. + +.. image:: https://github.com/yyDing1/verl-materials/blob/main/reward_loop.svg?raw=true + + +To support flexible and scalable reward model computation, we implement a reward router that coordinates requests among multiple reward model servers. + +Each reward model runs as an independent server and is registered with the router. +This router will forward the requests to the registered reward servers with load balancing and return the results. +This design allows us to expose a single unified router address to user-defined reward functions, enabling them to access various reward models seamlessly through the same interface. + +.. image:: https://github.com/yyDing1/verl-materials/blob/main/reward_loop_full.svg?raw=true + +.. code:: python + + class RewardModelManager: + """Reward model manager.""" + + def __init__( + self, + config: RewardModelConfig, + resource_pool: RayResourcePool = None, + ): + """ + Initialize the reward model manager. + + Args: + config (RewardModelConfig): Reward model configuration. + resource_pool (RayResourcePool, optional): Resource pool. Defaults to None. + """ + self.config = config + self.resource_pool = resource_pool + self._initialize_llm_servers() + self._initialize_router() + diff --git a/verl_0720_main/verl/docs/advance/rl_insight.md b/verl_0720_main/verl/docs/advance/rl_insight.md new file mode 100644 index 0000000000000000000000000000000000000000..bb1ab80faf45c27462e5692c9274941a17f2f05e --- /dev/null +++ b/verl_0720_main/verl/docs/advance/rl_insight.md @@ -0,0 +1,156 @@ +# Use RL-Insight to Monitor Training + +Last updated: 07/15/2026. + +[RL-Insight](https://github.com/verl-project/rl-insight) provides online observability for RL training. In verl, it can receive trainer scalar metrics, async rollout engine metrics, TransferQueue metrics, and rollout state traces, then show them in Grafana dashboards managed by the RL-Insight server. + +## When to Use + +Use RL-Insight when you want one monitor view for: + +- trainer metrics such as rewards, losses, and throughput +- async vLLM or SGLang rollout server metrics +- TransferQueue metrics when TransferQueue is enabled +- RL state timelines around rollout generation +- CPU, memory, network, and Ascend NPU hardware metrics + +## Step 1: Install and Start RL-Insight + +Install RL-Insight in the environment where the monitor server runs. Prefer the latest source version: + +```bash +pip install "git+https://github.com/verl-project/rl-insight.git" +``` + +Or install a released package: + +```bash +pip install "rl-insight>=0.2.0" +``` + +### Install monitor services + +`rl-insight server install` downloads Prometheus, Tempo, and Grafana into `~/.rl-insight/services`. The machine that runs this command needs network access to **GitHub release assets** and **`dl.grafana.com`**. + +If that machine can reach those hosts: + +```bash +rl-insight server install +rl-insight server start +``` + +If it cannot (common in air-gapped or restricted clusters), download the archives on a networked machine first, copy them to the RL-Insight host, then install from a local directory that contains all three archives. `/path/to/archives` below is only an example path — use any directory you choose, as long as the three packages are placed together in that directory. + +Default download URLs for `linux-amd64` (installer versions): + +| Service | Version | Download URL | +| --- | --- | --- | +| Prometheus | `2.54.1` | https://github.com/prometheus/prometheus/releases/download/v2.54.1/prometheus-2.54.1.linux-amd64.tar.gz | +| Tempo | `2.6.1` | https://github.com/grafana/tempo/releases/download/v2.6.1/tempo_2.6.1_linux_amd64.tar.gz | +| Grafana | `13.0.0` | https://dl.grafana.com/oss/release/grafana-13.0.0.linux-amd64.tar.gz | + +For `linux-arm64`, replace `amd64` with `arm64` in the filenames and URLs (Tempo uses `linux_arm64` in the archive name). Filenames must match exactly. + +```bash +rl-insight server install --local-archive /path/to/archives +rl-insight server start +``` + +`rl-insight server start` prints the detected server IP, Grafana URL, and related endpoints. Use that printed IP in the steps below. By default, RL-Insight uses: + +| Service | Default port | Purpose | +| --- | --- | --- | +| RL-Insight server | `18080` | Receives metrics and trace registrations | +| Prometheus | `9090` | Stores and queries metrics | +| Tempo | `3200` | Stores traces | +| Grafana | `3000` | Shows dashboards | + +## Step 2: Enable RL-Insight in verl + +Set the RL-Insight server address before submitting the training job. `` must be the IP of the machine where you ran `rl-insight server start` (the address printed by that command), and it must be reachable from the training processes: + +```bash +export RL_INSIGHT_SERVER_URL="http://:18080" +``` + +For a multi-node Ray cluster, add the variable to the runtime environment file submitted with the verl job, typically `verl/trainer/runtime_env.yaml`. This propagates the RL-Insight server address to workers on every node: + +```yaml +env_vars: + RL_INSIGHT_SERVER_URL: "http://:18080" +``` + +If your launch script passes another file through `ray job submit --runtime-env`, add the variable to that file instead. + +Add `rl_insight` to `trainer.logger`. When `rl_insight` is enabled, verl sets `VERL_RL_INSIGHT_ENABLE=1` and initializes the RL-Insight client in each process that uses it. + +```bash +python3 -m verl.trainer.main_ppo \ + trainer.logger='["console","rl_insight"]' \ + trainer.project_name=verl \ + trainer.experiment_name=ppo_rl_insight \ + ... +``` + +Trainer scalar metrics are reported to RL-Insight automatically through the logger backend. + +## Step 3: Monitor Rollout and TransferQueue Metrics + +For rollout engine metrics and TransferQueue metrics, keep rollout stats enabled and expose the TransferQueue metrics endpoint: + +```bash +python3 -m verl.trainer.main_ppo \ + trainer.logger='["console","rl_insight"]' \ + actor_rollout_ref.rollout.disable_log_stats=False \ + transfer_queue.metrics.enabled=True \ + ... +``` + +When rollout replicas or TransferQueue metrics endpoints start, verl registers them with RL-Insight. The generation path is also wrapped with RL-Insight state traces for vLLM and SGLang rollout workers. + +## Step 4: Add Hardware Metrics (Optional) + +To monitor CPU, memory, network, or Ascend NPU metrics, follow the [RL-Insight Hardware Monitoring guide](https://github.com/verl-project/rl-insight/blob/main/docs/monitor/hardware/index.md). The guide explains how to install or reuse the exporters and register their monitoring endpoints with RL-Insight. + +## View Dashboards + +1. Check the terminal output of `rl-insight server start` and open the printed Grafana URL. By default it is `http://:3000`, where `` is the RL-Insight host. +2. Log in with the default credentials: + - username: `admin` + - password: `admin` +3. In the left navigation, open **Dashboards**, then open the **RL-Insight** folder. +4. Select the dashboard that matches your run, for example: + - `verl_trainer_v1_with_vllm_engine` for vLLM rollout + - `verl_trainer_v1_with_sglang_engine` for SGLang rollout +5. Set the time range to a recent window such as **Last 5 minutes** / **Last 15 minutes** while training is still running. + +The dashboards should include training metrics, rollout metrics, TransferQueue metrics if enabled, and rollout state timelines. Example views: + +**RL state timeline (sync mode)** + +![sync timeline](https://github.com/mengchengTang/verl-data/raw/master/sync_timeline.png) + +**RL state timeline (separate async mode)** + +![separate async timeline](https://github.com/mengchengTang/verl-data/raw/master/separate_async_timeline.png) + +**Inference engine metrics across replicas** + +![infer engine metric of all replicas](https://github.com/mengchengTang/verl-data/raw/master/infer_engine_metric_of_all_replica.png) + +**TransferQueue metrics** + +![transfer queue metric](https://github.com/mengchengTang/verl-data/raw/master/transfer_queue_metric.png) + +**CPU hardware metrics** + +![CPU hardware metrics](https://github.com/mengchengTang/verl-data/blob/master/cpu%E6%8C%87%E6%A0%87.png?raw=1) + +## Troubleshooting + +- If trainer metrics do not appear, check that `trainer.logger` contains `rl_insight` and `RL_INSIGHT_SERVER_URL` points to the machine that runs `rl-insight server start`. +- If rollout metrics do not appear, check that `actor_rollout_ref.rollout.disable_log_stats=False` is set. +- If TransferQueue metrics do not appear, check that `transfer_queue.metrics.enabled=True` is set. +- If `server install` fails to download packages, use the offline `--local-archive` path above. + +For more RL-Insight server installation details, see the [RL-Insight server installation guide](https://github.com/verl-project/rl-insight/blob/main/docs/monitor/server_installation.md) and [quick start](https://github.com/verl-project/rl-insight/blob/main/docs/monitor/quick_start.md). diff --git a/verl_0720_main/verl/docs/advance/rollout_trace.rst b/verl_0720_main/verl/docs/advance/rollout_trace.rst new file mode 100644 index 0000000000000000000000000000000000000000..dc5934abf05f6457b27e2aab1f0202d47897435a --- /dev/null +++ b/verl_0720_main/verl/docs/advance/rollout_trace.rst @@ -0,0 +1,169 @@ +Trace Function Usage Instructions +======================================== + +Last updated: 07/10/2025. + +Applicable Scenarios +-------------------- + +Agentic RL involves multiple turns of conversations, tool invocations, and user interactions during the rollout process. During the Model Training process, it is necessary to track function calls, inputs, and outputs to understand the flow path of data within the application. The Trace feature helps, in complex multi-round conversations, to view the transformation of data during each interaction and the entire process leading to the final output by recording the inputs, outputs, and corresponding timestamps of functions, which is conducive to understanding the details of how the model processes data and optimizing the training results. + +The Trace feature integrates commonly used Agent trace tools, including wandb weave, mlflow, and Trackio, which are already supported. Users can choose the appropriate trace tool according to their own needs and preferences. Here, we introduce the usage of each tool. + + +Trace Parameter Configuration +----------------------------- + +- ``actor_rollout_ref.rollout.trace.backend=mlflow|weave|trackio`` # the trace backend type +- ``actor_rollout_ref.rollout.trace.token2text=True`` # To show decoded text in trace view +- ``actor_rollout_ref.rollout.trace.max_samples_per_step_per_worker=N`` # Limit traces per worker (optional) + +Limiting Trace Volume +~~~~~~~~~~~~~~~~~~~~~~ + +By default, all samples are traced, which can generate large amounts of data and incur significant costs with trace backends like Weave or MLflow. To limit trace volume while maintaining representative coverage, use ``max_samples_per_step_per_worker``. + +Example configuration: + +.. code-block:: yaml + + actor_rollout_ref: + rollout: + trace: + backend: weave + token2text: False + max_samples_per_step_per_worker: 5 # Each worker traces 5 random samples + +Each agent loop worker independently selects up to N unique samples to trace per training step. For GRPO (``n > 1``), all rollouts for selected samples are traced. Total traces per step = max_samples_per_step_per_worker * num_workers * n. + +Example: With 4 workers, max_samples_per_step_per_worker=5, and GRPO n=4, you get 4 * 5 * 4 = 80 traces per step instead of tracing all samples. Set to null (default) to trace all samples. + + +Glossary +-------- + ++----------------+------------------------------------------------------------------------------------------------------+ +| Object | Explaination | ++================+======================================================================================================+ +| trajectory | A complete multi-turn conversation includes: | +| | 1. LLM output at least once | +| | 2. Tool Call | ++----------------+------------------------------------------------------------------------------------------------------+ +| step | The training step corresponds to the global_steps variable in the trainer | ++----------------+------------------------------------------------------------------------------------------------------+ +| sample_index | The identifier of the sample, defined in the extra_info.index of the dataset. It is usually a number,| +| | but may also be a uuid in some cases. | ++----------------+------------------------------------------------------------------------------------------------------+ +| rollout_n | In the GROP algorithm, each sample is rolled out n times. rollout_n represents the serial number of | +| | the rollout. | ++----------------+------------------------------------------------------------------------------------------------------+ +| validate | Whether the test dataset is used for evaluation? | ++----------------+------------------------------------------------------------------------------------------------------+ + +Rollout trace functions +----------------------- + +There are 2 functions used for tracing: + +1. ``rollout_trace_op``: This is a decorator function used to mark the functions to trace. In default, only few method has it, you can add it to more functions to trace more infor. +2. ``rollout_trace_attr``: This function is used to mark the entry of a trajectory and input some info to trace. If you add new type of agent, you may need to add it to enable trace. + + +Usage of wandb weave +-------------------- + +1.1 Basic Configuration +~~~~~~~~~~~~~~~~~~~~~~~ + +1. Set the ``WANDB_API_KEY`` environment variable +2. Configuration Parameters + + 1. ``actor_rollout_ref.rollout.trace.backend=weave`` + 2. ``trainer.logger=['console', 'wandb']``: This item is optional. Trace and logger are independent functions. When using Weave, it is recommended to also enable the wandb logger to implement both functions in one system. + 3. ``trainer.project_name=$project_name`` + 4. ``trainer.experiment_name=$experiment_name`` + 5. ``actor_rollout_ref.rollout.mode=async``: Since trace is mainly used for agentic RL, need to enable agent toop using async mode for either vllm or sglang. + +Note: +The Weave Free Plan comes with a default monthly network traffic allowance of 1GB. During the training process, the amount of trace data generated is substantial, reaching dozens of gigabytes per day, so it is necessary to select an appropriate wandb plan. + + +1.2 View Trace Logs +~~~~~~~~~~~~~~~~~~~ + +After executing the training, on the project page, you can see the WEAVE sidebar. Click Traces to view it. + +Each Trace project corresponds to a trajectory. You can filter and select the trajectories you need to view by step, sample_index, rollout_n, and experiment_name. + +After enabling token2text, prompt_text and response_text will be automatically added to the output of ToolAgentLoop.run, making it convenient to view the input and output content. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/weave_trace_list.png?raw=true + +1.3 Compare Trace Logs +~~~~~~~~~~~~~~~~~~~~~~ + +Weave can select multiple trace items and then compare the differences among them. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/weave_trace_compare.png?raw=true + +Usage of mlflow +--------------- + +1. Basic Configuration +~~~~~~~~~~~~~~~~~~~~~~ + +1. Set the ``MLFLOW_TRACKING_URI`` environment variable, which can be: + + 1. Http and https URLs corresponding to online services + 2. Local files or directories, such as ``sqlite:////tmp/mlruns.db``, indicate that data is stored in ``/tmp/mlruns.db``. When using local files, it is necessary to initialize the file first (e.g., start the UI: ``mlflow ui --backend-store-uri sqlite:////tmp/mlruns.db``) to avoid conflicts when multiple workers create files simultaneously. + +2. Configuration Parameters + + 1. ``actor_rollout_ref.rollout.trace.backend=mlflow`` + 2. ``trainer.logger=['console', 'mlflow']``. This item is optional. Trace and logger are independent functions. When using mlflow, it is recommended to also enable the mlflow logger to implement both functions in one system. + 3. ``trainer.project_name=$project_name`` + 4. ``trainer.experiment_name=$experiment_name`` + + +2. View Log +~~~~~~~~~~~ + +Since ``trainer.project_name`` corresponds to Experiments in mlflow, in the mlflow view, you need to select the corresponding project name, then click the "Traces" tab to view traces. Among them, ``trainer.experiment_name`` corresponds to the experiment_name of tags, and tags corresponding to step, sample_index, rollout_n, etc., are used for filtering and viewing. + +For example, searching for ``"tags.step = '1'"`` can display all trajectories of step 1. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/mlflow_trace_list.png?raw=true + +Opening one of the trajectories allows you to view each function call process within it. + +After enabling token2text, prompt_text and response_text will be automatically added to the output of ToolAgentLoop.run, making it convenient to view the content. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/mlflow_trace_view.png?raw=true + +Note: + +1. mlflow does not support comparing multiple traces +2. rollout_trace can not associate the mlflow trace with the run, so the trace content cannot be seen in the mlflow run logs. + + +Usage of Trackio +---------------- + +1. Basic Configuration +~~~~~~~~~~~~~~~~~~~~~~ + +1. Configuration Parameters + + 1. ``actor_rollout_ref.rollout.trace.backend=trackio`` + 2. ``trainer.logger=['console', 'trackio']``. This item is optional for rollout traces, but recommended so metrics, validation generations, and traces are available in one Trackio run. + 3. ``trainer.project_name=$project_name`` + 4. ``trainer.experiment_name=$experiment_name`` + +2. View Log +~~~~~~~~~~~ + +After executing training, open the Trackio dashboard and select the configured project and experiment. Rollout trace operations are logged as Trackio traces with ``step``, ``sample_index``, ``rollout_n``, ``validate``, and ``experiment_name`` metadata. Validation generations are also logged as Trackio traces when ``trackio`` is enabled in ``trainer.logger``. Click on any Trace to expand it: + +.. image:: https://huggingface.co/buckets/trackio/doc-images/resolve/traces-verl.png + + diff --git a/verl_0720_main/verl/docs/advance/rope.rst b/verl_0720_main/verl/docs/advance/rope.rst new file mode 100644 index 0000000000000000000000000000000000000000..9463549e47d055552a273e83a851fc76f93f9d1a --- /dev/null +++ b/verl_0720_main/verl/docs/advance/rope.rst @@ -0,0 +1,39 @@ +RoPE Scaling override +======================================= + +Last updated: 05/14/2025. + +Some models such as `Qwen/Qwen2.5-7B-Instruct `_ support RoPE Scaling but don't have it defined in their config.json file. +For example, this model supports this configuration: + +.. code:: python + + { + ..., + "rope_scaling": { + "factor": 4.0, + "original_max_position_embeddings": 32768, + "type": "yarn" + } + } + + + +In order to support a longer context for such models, you must override the model configs when starting the trainer. + +PPO example: + +.. code:: bash + + +actor_rollout_ref.model.override_config.rope_scaling.type=yarn \ + +actor_rollout_ref.model.override_config.rope_scaling.factor=4.0 \ + +actor_rollout_ref.model.override_config.rope_scaling.original_max_position_embeddings=32768 \ + + +And for the critic model + +.. code:: bash + + +critic.model.override_config.rope_scaling.type=yarn \ + +critic.model.override_config.rope_scaling.factor=4.0 \ + +critic.model.override_config.rope_scaling.original_max_position_embeddings=32768 \ diff --git a/verl_0720_main/verl/docs/advance/skip_manager.rst b/verl_0720_main/verl/docs/advance/skip_manager.rst new file mode 100644 index 0000000000000000000000000000000000000000..bf5657f1224d12484936299ca5b291c2c40974c4 --- /dev/null +++ b/verl_0720_main/verl/docs/advance/skip_manager.rst @@ -0,0 +1,583 @@ +SkipManager: Skip everything in the RL pipeline. +=========== + +Last updated: 2026-07-08 + +.. contents:: :local: + :depth: 1 + +1. Overview +----------- + +**SkipManager** (``verl.utils.skip.SkipManager``) is a general-purpose framework for **skipping +selected steps** in verl training flows. By bypassing expensive stages on configured steps, it helps +save **time**, **memory**, or other resources and improves **developer iteration speed** during +debugging and experimentation. + +Skip behavior is centralized under the top-level Hydra key ``skip``. Modules register by **role** +(for example ``"rollout"``, ``"rollout_tq"``, or ``"async_rollout"``) and are attached with +@SkipManager.annotate(role=...) (or ``@SkipManager.annotate_tq(role=..., phase=...)`` for the +V1 two-phase path). Each role declares which integer **steps** in config are eligible for skip +logic. **Today only rollout-related roles are implemented**; the same mechanism can be extended to +other pipeline stages (see section 6). + +Typical use cases +~~~~~~~~~~~~~~~~~ + +SkipManager is intended for development workflows where repeating full training is costly: + +1. **Faster iteration**: skip heavy stages on chosen steps (e.g. generation) while exercising the + rest of the pipeline. +2. **Deterministic replay**: cache and reload intermediate results to reproduce a prior run on + specific steps. +3. **Resource savings**: avoid recomputing or holding large tensors when bisecting bugs or tuning + downstream logic. + +The built-in ``rollout`` / ``rollout_tq`` / ``async_rollout`` modules apply this to sequence +generation; other roles can follow the same pattern as they are added. + +Supported entry points today +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. list-table:: + :header-rows: 1 + :widths: 38 28 34 + + * - Training entry + - Skip role / config + - Status + * - ``main_ppo.py`` with trainer.use_v1=False (``RayPPOTrainer``) + - ``skip.rollout`` + - **Supported** + * - main_ppo.py with trainer.use_v1=True (V1 PPOTrainer + TransferQueue) + - ``skip.rollout_tq + - **Supported** (see section 4) + * - ``fully_async_main`` (``FullyAsyncRollouter``) + - ``skip.async_rollout`` + - **Supported** + + +2. Configuration (``skip.rollout`` / ``skip.rollout_tq`` / ``skip.async_rollout``) +--------------------------------------------------------------------- + +All three roles use the same Hydra field set (``RolloutSkipConfig`` / +``RolloutTqSkipConfig`` / ``AsyncRolloutSkipConfig`` in ``verl/utils/skip/config.py``). Defaults +live in verl/trainer/config/ppo_trainer.yaml under the respective skip.* key. + +Parameters +~~~~~~~~~~ + +- **enable** (bool): Master switch for this role. +- **dump_dir** (str): Root directory for cached shards (``~`` is expanded). +- **steps** (list[int]): Steps on which skip logic is *eligible*. Outside this list, the decorated + function always runs normally. + + - For ``skip.rollout``: trainer **global_steps** (via ``SkipManager.set_step``). + - For ``skip.rollout_tq: trainer **global_steps** (via ``SkipManager.set_step``). + - For ``skip.async_rollout``: the feed-order index parsed from ``sample_id`` (see section 5) — + **not** trainer ``global_steps``. + +- **action** (``cache`` \| ``repeat``): + + - **cache**: If a valid dump exists for the current step, load it and skip generation; otherwise + run generation and write under that step directory. + - **repeat**: If any valid dump exists, load from a **substitute** step chosen by the algorithm + below; otherwise run generation and dump as usual. + +.. note:: + + Only cache and repeat are validated in config today, even though SkipAction in + verl.utils.skip.base_skip lists additional enum values for future modules. + +``repeat`` step selection +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``rollout`` / ``async_rollout`` (``RolloutSkip._find_latest_step``) +................................................................... + +When ``action=repeat`` and the current step directory is missing or incomplete: + +1. If the directory for the **current** step is valid, use the current step. +2. Else use the **largest** available step **strictly less than** the current step. +3. Else use the **smallest** available step **strictly greater than** the current step. +4. If no valid dump exists, skip does not apply: the wrapped function runs and may dump afterward. + +``repeat`` does **not** guarantee the cached batch matches the current prompt or trainer step—use +it for debugging and iteration, and prefer ``cache`` when you need step-aligned replay. + +``rollout_tq`` (``RolloutTqSkip._resolve_load_step_v1``) +........................................................ + +Same fallback strategy as above, but checks the V1-format cache file (tq_batch.pt, see +on-disk layout below) instead of gen_batch.dp. + +Hydra CLI examples +~~~~~~~~~~~~~~~~~~ + +Colocated PPO (``skip.rollout``): + +.. code-block:: bash + + skip.rollout.enable=True + skip.rollout.dump_dir=/path/to/rollout_dump + skip.rollout.steps=[1,2,3,10] + skip.rollout.action=cache + +TransferQueue-based V1 trainer (``skip.rollout_tq``): + +.. code-block:: bash + + skip.rollout_tq.enable=True + skip.rollout_tq.dump_dir=/path/to/rollout_dump + skip.rollout_tq.steps=[1,3,5] + skip.rollout_tq.action=cache + +Fully async (``skip.async_rollout``): + +.. code-block:: bash + + skip.async_rollout.enable=True + skip.async_rollout.dump_dir=/path/to/rollout_dump + skip.async_rollout.steps=[1,2,3,4,5] + skip.async_rollout.action=cache + +To pass a long step list from **bash** only (not valid inside static YAML): + +.. code-block:: bash + + skip.async_rollout.steps="[$(seq -s, 1 128)]" + +On-disk layout +~~~~~~~~~~~~~~ + +All roles share the same project-level directory structure; only the per-step files differ. + +.. code-block:: text + + {dump_dir}/{experiment_name}_{project_name}/ + └── GBS{gbs}_N{n}_in{prompt_len}_out{response_len}/ + ├── {step}/ + │ ├── gen_batch.dp # rollout / async_rollout + │ ├── tq_batch.pt # rollout_tq + │ └── meta.json + └── ... + +- **experiment_name** / **project_name**: from ``trainer.experiment_name`` and + ``trainer.project_name`` in the run config. +- **gbs**, **n**, **prompt_len**, **response_len**: from ``data.gen_batch_size`` (or train batch + size), ``actor_rollout_ref.rollout.n``, ``data.max_prompt_length``, and + ``data.max_response_length``. + +Caches from colocated main_ppo (larger **GBS**) and fully async streaming (typically **GBS=1**) +are generally **not** interchangeable unless these metadata match. rollout (gen_batch.dp) +and rollout_tq (tq_batch.pt) use different file formats and are **never** interchangeable, +even when the project metadata matches. + +.. list-table:: + :header-rows: 1 + :widths: 22 18 60 + + * - Role + - Cache file + - Contents + * - rollout / async_rollout + - gen_batch.dp + - A DataProto saved via DataProto.save_to_disk — the full generate_sequences output + (prompts, responses, log_probs, etc.). + * - rollout_tq + - tq_batch.pt + - A torch.save payload containing: tensordict (all trajectory fields read from TQ via + tq.kv_batch_get), tags (per-trajectory tag list), keys (trajectory-level TQ keys), + global_steps (int). See section 4 for details. + +Minimal workflow (cache) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. **First run** with ``enable=True``, ``action=cache``, and ``steps`` listing the steps you care + about. Empty ``dump_dir`` → generation runs and writes ``the cache file`` + ``meta.json`` per step. +2. **Second run** with the same config and compatible trainer metadata → listed steps load from + disk instead of regenerating. +3. **Partial caches** (some step dirs missing): those steps regenerate on the next run; other steps + still load if present. + +Relationship to legacy RolloutSkip +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If **both** ``skip.rollout.enable`` and legacy ``actor_rollout_ref.rollout.skip.enable`` are true, +SkipManager emits a ``DeprecationWarning`` and **forces** the legacy flag to ``False`` so only one +mechanism runs. + + +3. Rollout quick start (``rollout`` role) +----------------------------------------- + +Use ``skip.rollout`` when training with main_ppo.py and trainer.use_v1=False +(RayPPOTrainer) and the standard ``AgentLoopManager.generate_sequences`` path. Configuration +fields and ``cache`` / ``repeat`` semantics are in section 2. + +**Wiring** + +- RayPPOTrainer.fit() calls SkipManager.init(self.config) and + SkipManager.set_step(self.global_steps) each training step. +- AgentLoopManager.generate_sequences is decorated with + @SkipManager.annotate(role="rollout"). + +The decorated function is a single entry point: it receives prompts, drives full-batch generation +(chunk dispatch, concat, timing), and returns the complete DataProto. Skip logic wraps this +unit as a whole — on cache-hit it returns the cached DataProto without invoking the LLM; on +cache-miss it runs generation and dumps the result. + + +4. Trainer V1 quick start (``rollout_tq`` role) +--------------------------------------------------- + +Use skip.rollout_tq when training with main_ppo.py and trainer.use_v1=True +(V1 PPOTrainer + TransferQueue). This covers all V1 trainer modes: sync, +colocate_async, and separate_async. + +.. important:: + + Unlike rollout (section 3), the V1 TransferQueue path does **not** have a single + generate_sequences entry point. Rollout is split across two methods that run at different + points in the training step: + + - **Submit phase** — PPOTrainer._add_batch_to_generate: samples a batch from the dataloader, + assigns uids, and dispatches prompts to the AgentLoopManager for generation. + - **Sample phase** — ReplayBuffer.sample: waits for trajectories to finish, then collects + them from TQ into a KVBatchMeta. + + A single decorator cannot cover both because the cache-hit short-circuit must happen *inside* + the submit phase — after uid generation (needed for key mapping) but before real rollout + submission (which we want to skip). The solution is SkipManager.annotate_tq, a two-phase + decorator selected by phase="submit" or phase="sample". + +Wiring +~~~~~~ + +- PPOTrainer.fit() calls SkipManager.init(self.config) and + SkipManager.set_step(self.global_steps) each training step. +- PPOTrainer._add_batch_to_generate is decorated with + @SkipManager.annotate_tq(role="rollout_tq", phase="submit"). +- ReplayBuffer.sample is decorated with + @SkipManager.annotate_tq(role="rollout_tq", phase="sample"). + +Method splitting +~~~~~~~~~~~~~~~~~ + +To give the submit-phase decorator a clean interception window, _add_batch_to_generate is +split into two sub-methods: + +.. code-block:: python + + @SkipManager.annotate_tq(role="rollout_tq", phase="submit") + def _add_batch_to_generate(self): + batch = self._next_train_batch() # dataloader + uid assignment + self._submit_batch_to_rollout(batch) # tag registration + generate_sequences + + def _next_train_batch(self): + """Advance the dataloader and return a batch with fresh uids.""" + ... + + def _submit_batch_to_rollout(self, batch): + """Register prompt tags in TransferQueue and dispatch to AgentLoopManager.""" + ... + +When skip is **disabled** or the current step is outside skip.rollout_tq.steps, the decorator +passes through and the function body runs normally (_next_train_batch then +_submit_batch_to_rollout). + +When skip is **enabled** and the step is eligible, the decorator takes over and the function body +does not execute. Instead the decorator calls _next_train_batch itself (keeping the dataloader +aligned even on cache-hit steps), then branches on cache availability. + +.. note:: + + If future changes add logic between _next_train_batch and _submit_batch_to_rollout + inside _add_batch_to_generate, that logic must also be reflected in the decorator's + submit-phase branch, since the decorator bypasses the function body when skip is active. + +Two-phase flow +~~~~~~~~~~~~~~ + +**Phase 1 — cache-miss** (first run, no tq_batch.pt on disk): + +.. code-block:: text + + step() + | + +- _add_batch_to_generate() [submit decorator: skip enabled, cache-miss] + | +- _next_train_batch() -> batch with fresh uids + | +- maybe_load_and_inject() -> False (no cache) + | +- _submit_batch_to_rollout() -> real LLM generation dispatched to TQ + | + +- replay_buffer.sample() [sample decorator: skip enabled] + | +- (original sample runs) -> waits for trajectories, returns KVBatchMeta + | +- should_save() -> True (no cache, partition="train") + | +- prepare_data() -> kv_batch_get all fields -> torch.save -> tq_batch.pt + | + +- (downstream: reward, advantage, actor/critic update ...) + +**Phase 2 — cache-hit** (subsequent run, tq_batch.pt exists): + +.. code-block:: text + + step() + | + +- _add_batch_to_generate() [submit decorator: skip enabled, cache-hit] + | +- _next_train_batch() -> batch with fresh uids + | +- maybe_load_and_inject() -> True -> load_dump_data() + | | +- torch.load(tq_batch.pt) -> old keys, tags, tensordict + | | +- group old trajectories by uid prefix + | | +- map new uids -> cached groups (modulo cycling) + | | +- index_select_tensor_dict -> select/repeat trajectory rows + | | +- kv_batch_put trajectories with new keys + updated tags + | | +- kv_batch_put prompt-level keys with status="finished" + | +- return (skip _submit_batch_to_rollout -- no real LLM call) + | + +- replay_buffer.sample() [sample decorator: skip enabled] + | +- (original sample runs) -> finds finished prompts immediately, returns KVBatchMeta + | +- should_save() -> False (cache exists) + | +- (no prepare_data call) + | + +- (downstream: reward, advantage, actor/critic update ...) + +On-disk format: ``tq_batch.pt`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``prepare_data`` saves a ``torch.save`` payload with four keys: + +.. list-table:: + :header-rows: 1 + :widths: 20 80 + + * - Key + - Contents + * - ``tensordict`` + - A ``TensorDict`` containing **all** trajectory fields read from TQ via + ``tq.kv_batch_get(keys=batch.keys)`` (no ``select_fields`` filter). Includes ``prompts``, + ``responses``, ``response_mask``, dataset fields (``messages``, ``datasource``, etc.), and + any fields written by the agent loop. ``prompts`` / ``responses`` are ``NestedTensor`` + (jagged); the rest are regular tensors. + * - ``tags`` + - ``list[dict]`` — per-trajectory tags, one per entry in ``keys``, in the same order. + Each tag carries ``global_steps``, ``status``, ``seq_len``, etc. + * - ``keys`` + - ``list[str]`` — trajectory-level TQ keys in ``{uid}_{session_id}_{index}`` format. + The uid prefix is a UUID4 (no underscores), so ``key.split("_")[0]`` recovers the parent + prompt uid. + * - ``global_steps`` + - ``int`` — the step at which the dump was created, for sanity checking. + +``meta.json`` records ``{"global_steps": , "num_trajectories": }`` and is used by +``_check_valid_v1_step_path`` for completeness validation. + +Why ``kv_batch_get`` without ``select_fields``? +............................................... + +``KVBatchMeta`` from ``ReplayBuffer.sample`` holds only key/tag references — the trajectory data +body lives in TQ storage units. TQ clears keys at the end of each step (``tq.kv_clear`` in +``PPOTrainer.fit``), so ``prepare_data`` must read the full data body **before** that cleanup. +Reading without ``select_fields`` ensures all downstream fields (reward, log-prob, masks) are +captured for a faithful replay. + +Cache-hit injection: key remapping +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +``load_dump_data`` cannot reuse old keys directly — new uids were just generated by +``_next_train_batch``. It remaps cached trajectories onto new uids: + +1. **Group by uid**: Parse ``old_keys`` and group trajectory indices by ``key.split("_")[0]``. + Each group is one prompt GRPO group (usually *n* trajectories, but may be fewer if sessions + failed). + +2. **Map new uids to groups**: For each new uid at position prompt_idx, select + ``groups[prompt_idx % num_cached_groups]``. If cached groups are fewer than new prompts, groups + cycle (modulo). If more, only the first ``num_prompts`` groups are used. + +3. **Fill *n* trajectories per prompt**: For each session_id in [0, n), pick + ``group[session_id % len(group)]``. If a group has fewer than *n* entries (partial failures), + trajectories cycle within the group to fill the slot. + +4. **Build new keys**: ``{new_uid}_{session_id}_0`` — matching the standard TQ key format. + +5. **Update tags**: Each trajectory tag's ``global_steps`` / ``min_global_steps`` / + ``max_global_steps`` are overwritten to the **current** step so ``ReplayBuffer``'s staleness + check (``_drop_max_off_policy_samples``) does not discard them as off-policy. The ``is_prompt`` + flag is removed from trajectory tags (it belongs only on prompt-level keys). + +6. **Select tensor rows**: ``index_select_tensor_dict(data, traj_indices)`` picks (and possibly + duplicates) rows from the cached ``tensordict``. This handles both regular tensors and + NestedTensor (unbind -> select -> nested_tensor_from_tensor_list rebuild). + +7. **Two ``kv_batch_put`` calls**: + + - **Trajectory data**: ``keys=new_keys``, ``fields=new_fields``, ``tags=new_tags`` — writes the + actual trajectory content into TQ storage. + - **Prompt-level markers**: ``keys=new_prompt_uids``, ``tags=[{"is_prompt": True, "status": + "finished", ...}]`` — marks each new prompt as finished so ``ReplayBuffer.sample``'s + ``_has_enough_samples`` passes immediately without polling. + +After injection, TQ holds the same structure as a normal completed rollout: ``n`` trajectory keys +per prompt plus one prompt-level key with ``status="finished"``. ``replay_buffer.sample`` picks +them up on the next call and returns a KVBatchMeta whose data is the injected cache. + + +5. Fully async quick start (``async_rollout`` role) +--------------------------------------------------- + +In :doc:`advance/fully_async`, Trainer and Rollouter run in separate processes. Rollout generation +happens on the Rollouter via streaming single-sample dispatch. Use ``skip.async_rollout`` (not +``skip.rollout``) when launching ``fully_async_main``. Shared Hydra fields and on-disk layout are +in section 2. + +.. important:: + + In ``async_rollout``, a step is **not** the trainer timeline. It is only the **prompt request / + feed order** on the Rollouter: the monotonic index in ``sample_{epoch}_{index}`` when + ``FullyAsyncRollouter`` enqueues the next prompt. Under concurrent rollout, completion order can + differ from feed order; do not treat these indices as trainer ``global_steps`` or parameter-sync + boundaries when configuring ``skip.async_rollout.steps``. + +Step key from ``sample_id`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Each fed sample carries an id of the form ``sample_{epoch}_{index}`` (for example +``sample_0_42``). The integer matched against ``skip.async_rollout.steps`` and used for on-disk +directories is the **last segment** — Rollouter feed-order index at enqueue time. + +**Wiring** + +- ``FullyAsyncRollouter`` calls ``SkipManager.init(self.config)`` in the Rollouter process. +- ``FullyAsyncAgentLoopManager.generate_sequences_single`` is decorated with + ``@SkipManager.annotate(role="async_rollout")`` and receives ``sample_id`` for online step + resolution. + + +6. Design and implementation +---------------------------- + +SkipManager API +~~~~~~~~~~~~~~~ + +``SkipManager`` (``verl.utils.skip.skip_manager``) is a class-level registry: + +- **``init(config)``**: Parse ``config.skip`` into ``SkipManagerConfig``, instantiate one skip module + per registered role, and store them in ``SkipManager.skip_instances``. +- **``set_step(step: int)``**: Set ``SkipManager.step`` for roles with ``support_online_step = + False`` (trainer ``global_steps`` in ``main_ppo`` and V1 ``PPOTrainer``). +- **``annotate(role, **kwargs)``**: Decorator factory for sync or async functions (used by + ``rollout`` and ``async_rollout``). +- **``annotate_tq(role, phase)``**: Two-phase decorator factory for the V1 TransferQueue path + (used by ``rollout_tq``). See section 4. + +Decorator flow: ``annotate`` (``rollout`` / ``async_rollout``) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: text + + call decorated function + │ + ▼ + skip disabled or role missing? ──yes──► run original function + │no + ▼ + resolve step (set_step vs extract_step) + │ + ▼ + step ∉ config.steps? ──yes──► run original function + │no + ▼ + meet_precondition (cache/repeat)? ──yes──► warp_function (load cache) + │no + ▼ + run original function → prepare_data (dump) + + +Decorator flow: ``annotate`` (``rollout_tq``) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. image:: https://raw.githubusercontent.com/mikequan0425/verl-community/main/SkipManager_for_trainer_v1.png + :align: center + :width: 90% + :alt: SkipManager_for_trainer_v1 + + +BaseSkip interface +~~~~~~~~~~~~~~~~~~ + +Each skip module subclasses ``BaseSkip`` (``verl.utils.skip.base_skip``) and registers via +``@register_skip("role_name")``. + +- **``support_actions``**: Allowed ``SkipAction`` values for this module. +- **``support_online_step``**: When ``True``, use ``extract_step`` per call instead of + ``SkipManager.step``. + +Instance methods: ``is_enabled``, ``meet_precondition``, ``warp_function``, ``prepare_data``, and +``extract_step`` (required when ``support_online_step`` is ``True``). + +``RolloutSkip`` / ``RolloutTqSkip`` / ``AsyncRolloutSkip`` (``verl.utils.skip.rollout_skip``) +implement generation caching for the three roles. ``RolloutTqSkip`` extends ``RolloutSkip`` and +adds V1-specific methods: ``should_save``, ``maybe_load_and_inject``, ``load_dump_data``, +``has_v1_cache``, ``_resolve_load_step_v1``. + +Intercepted functions +~~~~~~~~~~~~~~~~~~~~~ + +.. list-table:: + :header-rows: 1 + :widths: 14 36 28 22 + + * - Role + - Decorated function + - Defined in + - Step source + * - ``rollout`` + - ``AgentLoopManager.generate_sequences`` + - ``verl/experimental/agent_loop/agent_loop.py`` + - ``SkipManager.set_step`` -> trainer ``global_steps`` + * - ``rollout_tq`` + - PPOTrainer._add_batch_to_generate (phase="submit") + - verl/trainer/ppo/v1/trainer_base.py + - SkipManager.set_step -> trainer global_steps + * - ``rollout_tq`` + - ReplayBuffer.sample (phase="sample") + - verl/trainer/ppo/v1/replay_buffer.py + - SkipManager.set_step -> trainer ``global_steps`` + * - ``async_rollout`` + - ``FullyAsyncAgentLoopManager.generate_sequences_single`` + - ``verl/experimental/fully_async_policy/fully_async_rollouter.py`` + - ``extract_step`` → ``sample_id`` suffix → **prompt feed order** + +**``rollout``** wraps the full batch Agent Loop RPC (chunk dispatch, concat, timing) as one skip +unit. + +**``rollout_tq``** wraps two methods with a single ``annotate_tq`` decorator, selected by +``phase``. The submit phase intercepts *before* real generation; the sample phase intercepts +*after* sampling to persist results. + +**``async_rollout``** wraps one streaming sample's ``generate_sequences_single(self, prompts, +sample_id)`` so concurrent samples resolve step independently. + +Step resolution: ``set_step`` vs ``support_online_step`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +See section 2 for ``steps`` semantics per role. + +- **Shared ``SkipManager.step``**: One class-level slot per process. Fits sequential trainer loops + (``main_ppo``): ``set_step(global_steps)`` before rollout. +- **Online step**: ``AsyncRolloutSkip`` sets ``support_online_step = True`` and parses + ``sample_id`` on each call so in-flight async samples do not share a single counter. For + ``repeat``, ``RolloutSkip`` recomputes ``_find_latest_step`` on every ``meet_precondition`` and + ``warp_function`` call (no shared mutable step field on the skip instance). + +Extending with custom skip modules +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. Subclass ``BaseSkip`` from ``verl.utils.skip.base_skip``. +2. Decorate the class with ``@register_skip("your_role_name")``. +3. Add a matching field under ``SkipManagerConfig``. +4. Attach ``@SkipManager.annotate(role="your_role_name")``. For concurrent pipelines, prefer + ``support_online_step = True`` and pass step identity through call arguments. +5. For split-architecture paths (like V1's submit/sample separation), use + @SkipManager.annotate_tq(role=..., phase=...) and split the target method so the decorator + can intercept between the two sub-steps. diff --git a/verl_0720_main/verl/docs/algo/baseline.md b/verl_0720_main/verl/docs/algo/baseline.md new file mode 100644 index 0000000000000000000000000000000000000000..a907c8f52ccea7ed348ad72326cc22707c3e2e30 --- /dev/null +++ b/verl_0720_main/verl/docs/algo/baseline.md @@ -0,0 +1,73 @@ +# Algorithm Baselines + +Last updated: 06/18/2025. + +## Math related datasets + +### GSM8k + +Assuming GSM8k/math dataset is preprocessed via: + +```bash +python3 examples/data_preprocess/*.py +``` + +Refer to the table below to reproduce RL training from different pre-trained checkpoints. Below is the performance on the GSM8k dataset if not specified otherwise. More comprehensive benchmark results areavailable in the recipe folder. + +| Hardware | Model | Method | Test score | Details | +| ---------- | -------------------------------- | --------------- | ------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| NVIDIA GPU | google/gemma-2-2b-it | hf checkpoint | 23.9 | [Huggingface](https://huggingface.co/google/gemma-2-2b-it#benchmark-results) | +| NVIDIA GPU | google/gemma-2-2b-it | SFT | 52.06 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/gemma-2-2b-it-sft-0.411.log) | +| NVIDIA GPU | google/gemma-2-2b-it | SFT + PPO | 64.02 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/gemma-2-2b-it-ppo-bsz512_4-prompt1024-resp-512-0.640.log), [wandb](https://api.wandb.ai/links/verl-team/h7ux8602) | +| NVIDIA GPU | Qwen/Qwen2.5-0.5B-Instruct | hf checkpoint | 49.6 | [Qwen blog](https://qwen.ai/blog?id=qwen2.5-llm) | +| NVIDIA GPU | Qwen/Qwen2.5-0.5B-Instruct | PPO | 56.7 | [command and log](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-0.5B-bsz256_2-prompt1024-resp512-0.567.log) | +| NVIDIA GPU | Qwen/Qwen2.5-0.5B-Instruct | PRIME | 58.7 | [script](https://github.com/verl-project/verl-recipe/blob/main//prime/run_prime_qwen.sh), [wandb](https://api.wandb.ai/links/zefan-wang-thu-tsinghua-university/rxd1btvb) | +| NVIDIA GPU | Qwen/Qwen2.5-0.5B-Instruct | GRPO-LoRA | 54.3 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-0.5B-bsz64_2-prompt512-resp1024-lorarank32-score0.543.log) | +| NVIDIA GPU | Qwen/Qwen2.5-1.5B-Instruct | GRPO-LoRA | 77.9 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-1.5B-bsz64_2-prompt512-resp1024-lorarank32-score0.779.log) | +| NVIDIA GPU | Qwen/Qwen2.5-3B-Instruct | GRPO-LoRA | 86.1 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-3B-bsz64_2-prompt512-resp1024-lorarank32-score0.861.log) | +| NVIDIA GPU | deepseek-ai/deepseek-llm-7b-chat | PPO (Megatron) | 69.5 [1] | [log](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/deepseek-llm-7b-chat-megatron-bsz256_4-prompt512-resp512-0.695.log), [wandb](https://wandb.ai/verl-team/verl_megatron_gsm8k_examples/runs/10fetyr3) | +| NVIDIA GPU | Qwen/Qwen2-7B-Instruct | GRPO | 89 | [script](https://github.com/verl-project/verl/blob/a65c9157bc0b85b64cd753de19f94e80a11bd871/examples/grpo_trainer/run_qwen2-7b_seq_balance.sh) | +| NVIDIA GPU | Qwen/Qwen2-7B-Instruct | GRPO (FSDP2) | 89.8 | [log](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/qwen2-7b-fsdp2.log) | +| NVIDIA GPU | Qwen/Qwen2-7B-Instruct | GRPO (Megatron) | 89.6 | [log](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/qwen2-7b_math_megatron.log) | +| NVIDIA GPU | Qwen/Qwen2.5-7B-Instruct | ReMax | 97 | [script](https://github.com/eric-haibin-lin/verl/blob/main/examples/remax_trainer/run_qwen2.5-3b_seq_balance.sh), [wandb](https://wandb.ai/liziniu1997/verl_remax_example_gsm8k/runs/vxl10pln) | +| NVIDIA GPU | Qwen/Qwen2.5-7B-Instruct | SPPO | 65.6 (MATH) | [SPPO script](https://github.com/verl-project/verl-recipe/tree/main/sppo/README.md) | +| NVIDIA GPU | Qwen/Qwen2.5-7B-Instruct | GRPO-LoRA | 93.4 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-7B-bsz64_8-prompt512-resp1024-lorarank32-score0.934.log) | +| NVIDIA GPU | Mixtral-8x22B-Instruct-v0.1 | Instruct model | 83.7 | [Qwen Blog](https://qwen.ai/blog?id=qwen2.5-llm) | +| NVIDIA GPU | Mixtral-8x22B-Instruct-v0.1 | RLOO (Megatron) | 92.3 | [wandb](https://api.wandb.ai/links/ppo_dev/sbuiuf2d) | +| NVIDIA GPU | Qwen/Qwen2.5-7B-Instruct | SPIN | 92 | [script](https://github.com/verl-project/verl-recipe/tree/main/spin/README.md) | +| NVIDIA GPU | Qwen/Qwen2-7B-Instruct | GPG | 88 | [log](https://github.com/diqiuzhuanzhuan/verldata/blob/main/run_logs/qwen2-7b_math.log), [wandb](https://wandb.ai/diqiuzhuanzhuan/verl_gpg_example_gsm8k_math/runs/ab86c4va) | +| NVIDIA GPU | Qwen/Qwen2-7B-Instruct | GPG (Megatron) | 88 | [log](https://github.com/diqiuzhuanzhuan/verldata/blob/main/run_logs/qwen2-7b_math_megatron.log), [wandb](https://wandb.ai/diqiuzhuanzhuan/verl_gpg_example_gsm8k_math/runs/yy8bheu8) | +| NVIDIA GPU | Qwen/Qwen2.5-VL-7B-Instruct | GRPO (Megatron) | 65.4 (GEO3k) | [script](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen2_5_vl_7b_megatron.sh), [wandb](https://api.wandb.ai/links/megatron-core-moe-dev/1yngvkek) | +| AMD MI300 | deepseek-ai/deepseek-llm-7b-chat | PPO | 70.5 [1] | [log](https://github.com/yushengsu-thu/verl_training_log/blob/main/gsm8k/ppo_run_deepseek7b_llm.log) | +| AMD MI300 | deepseek-ai/deepseek-llm-7b-chat | GRPO | 71.4 [1] | [log](https://github.com/yushengsu-thu/verl_training_log/blob/main/gsm8k/grpo_run_deepseek7b_llm.log) | +| NVIDIA GPU | Qwen/Qwen2.5-14B-Instruct | GRPO-LoRA | 94.6 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-14B-bsz64_8-prompt512-resp1024-lorarank32-score0.946.log) | +| NVIDIA GPU | Qwen/Qwen2.5-32B-Instruct | GRPO-LoRA | 95.8 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-32B-bsz64_8-prompt512-resp1024-lorarank32-score0.958.log) | +| NVIDIA GPU | Qwen/Qwen2.5-72B-Instruct | GRPO-LoRA | 96.0 | [command and logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-72B-bs64_8-prompt512-resp1024-lorarank32-score0.960.log) | + +### DAPO math-17k + +- Training DAPO math-17k dataset: https://huggingface.co/datasets/BytedTsinghua-SIA/DAPO-Math-17k +- Testing: AIME'24: https://huggingface.co/datasets/BytedTsinghua-SIA/AIME-2024 + +Note: + +- For Qwen/Qwen2.5-Math-7B, we directly modify the max_position_embeddings to 32768 without observing performance degradation in order to train longer response length. + +| Hardware | Model | Method | Test score | Details | +| ---------- | -------------------------- | ----------------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| NVIDIA GPU | Qwen/Qwen2.5-Math-7B (32k) | DAPO | 36.3 | [command](https://github.com/verl-project/verl-recipe/blob/main//dapo/test_dapo_7b_math.sh), [logs](https://wandb.ai/verl-org/DAPO%20Reproduction%20on%20verl/runs/ow47vvon?nw=nwusertongyuxuan361) | +| NVIDIA GPU | Qwen/Qwen2.5-7B-Instruct | DAPO + Code Interpreter | 40.0 | [command](https://github.com/verl-project/verl-recipe/blob/main//retool/run_qwen2_7b_dapo.sh) | + +## Coding related datasets + +Below is the result on leetcode if not specified otherwise. + +| Hardware | Model | Method | Test score | Details | +| ---------- | ----------------------- | ------ | ---------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| NVIDIA GPU | PRIME-RL/Eurus-2-7B-SFT | RPIME | 36.1 | [script](https://github.com/verl-project/verl-recipe/blob/main//prime/run_prime_qwen_code.sh), [swanlab](https://swanlab.cn/@wangzefan/prime_example/runs/7f541qhspgmy8nmhdlx35/chart) | + +### Notes + +[1] During evaluation, we have only extracted answers following the format `"####"`. A more flexible answer extraction, longer response length, and better prompt engineering may lead to a higher score. + +[2] The default value of `actor_rollout_ref.actor.entropy_coeff` is set to `0.0` since verl 0.3.x on 2025-05-30, which is different from previous versions. diff --git a/verl_0720_main/verl/docs/algo/dapo.md b/verl_0720_main/verl/docs/algo/dapo.md new file mode 100644 index 0000000000000000000000000000000000000000..09ae88971d0e4f1a5a796c821823973eb5816bfb --- /dev/null +++ b/verl_0720_main/verl/docs/algo/dapo.md @@ -0,0 +1,187 @@ +# Recipe: Decoupled Clip and Dynamic Sampling Policy Optimization (DAPO) + +Last updated: 06/19/2025. + +> Open-Source Algorithm Implementation & Expriement Running: [Yuxuan Tong](https://tongyx361.github.io/), [Guangming Sheng](https://hk.linkedin.com/in/guangming-sheng-b50640211) + +🏠 [Homepage](https://dapo-sia.github.io/) | 📝 [Paper@arXiv](https://arxiv.org/abs/2503.14476) | 🤗 [Datasets&Models@HF](https://huggingface.co/collections/BytedTsinghua-SIA/dapo-67d7f1517ee33c8aed059da0) | 🐱 [Code@GitHub](https://github.com/verl-project/verl-recipe/tree/main/dapo/recipe/dapo) | 🐱 [Repo@GitHub](https://github.com/BytedTsinghua-SIA/DAPO) + +> We propose the **D**ecoupled Clip and Dynamic s**A**mpling **P**olicy **O**ptimization (DAPO) algorithm. By making our work publicly available, we provide the broader research community and society with practical access to scalable reinforcement learning, enabling all to benefit from these advancements. Our system is based on the awesome [verl](https://github.com/verl-project/verl) framework. Thanks for their great work! Applying DAPO training to Qwen2.5-32B base model proves to outperform the previous state-of-the-art DeepSeek-R1-Zero-Qwen-32B on AIME 2024, achieving **50%** accuracy with **50%** less training steps. +> +> ![dapo-main-result](https://dapo-sia.github.io/static/images/score.png) + +## Quickstart + +1. Prepare the datasets **on the Ray cluster**: + +```bash +bash prepare_dapo_data.sh # This downloads the datasets to ${HOME}/verl/data by default +``` + +2. Submit the job to the Ray cluster **from any machine**: + +```bash +cd verl # Repo root +export RAY_ADDRESS="http://${RAY_IP:-localhost}:8265" # The Ray cluster address to connect to +export WORKING_DIR="${PWD}" # The local directory to package to the Ray cluster +# Set the runtime environment like env vars and pip packages for the Ray cluster in yaml +export RUNTIME_ENV="./recipe/dapo/runtime_env.yaml" # This sets environment variables for the Ray cluster +bash recipe/dapo/run_dapo_qwen2.5_32b.sh # or other scripts +``` + +## Reproduction Runs + +| Setup | AIME 2024 Acc. | Hardware | Image | Commit | Environment Variables | Training Script | Training Record | +| -------------------------------------------- | -------------- | --------- | -------------------------------------------------------------------- | -------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- | +| DAPO | 52% | 16x8xH800 | `hiyouga/verl:ngc-th2.6.0-cu126-vllm0.8.3-flashinfer0.2.2-cxx11abi0` | [`4f80e4`](https://github.com/verl-project/verl/tree/4f80e465c2ec79ab9c3c30ec74b9745de61d0490) | [runtime_env.yaml](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/runtime_env.yaml) | [run_dapo_qwen2.5_32b.sh](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/run_dapo_qwen2.5_32b.sh) | [W&B](https://wandb.ai/verl-org/DAPO%20Reproduction%20on%20verl/workspace?nw=wmb4qxfht0n) | +| DAPO w/o Dynamic Sampling | 50% | 16x8xH800 | `hiyouga/verl:ngc-th2.6.0-cu126-vllm0.8.3-flashinfer0.2.2-cxx11abi0` | [`4f80e4`](https://github.com/verl-project/verl/tree/4f80e465c2ec79ab9c3c30ec74b9745de61d0490) | [runtime_env.yaml](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/runtime_env.yaml) | [run_dapo_wo_ds_qwen2.5_32b.sh](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/run_dapo_wo_ds_qwen2.5_32b.sh) | [W&B](https://wandb.ai/verl-org/DAPO%20Reproduction%20on%20verl/workspace?nw=wmb4qxfht0n) | +| DAPO w/o Token-level Loss & Dynamic Sampling | 44% | 16x8xH20 | `hiyouga/verl:ngc-th2.5.1-cu120-vllm0.7.4-hotfix` | [`4f80e4`](https://github.com/verl-project/verl/tree/4f80e465c2ec79ab9c3c30ec74b9745de61d0490) | [runtime_env.yaml](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/runtime_env.yaml) | [run_dapo_early_qwen2.5_32b.sh](https://github.com/verl-project/verl/blob/4f80e465c2ec79ab9c3c30ec74b9745de61d0490/recipe/dapo/run_dapo_early_qwen2.5_32b.sh) | [W&B](https://wandb.ai/verl-org/DAPO%20Reproduction%20on%20verl/workspace?nw=wmb4qxfht0n) | + +> [!IMPORTANT] +> +> **📢 Call for Contribution!** +> +> Welcome to submit your reproduction runs and setups! + +## Configuration + +### Separated Clip Epsilons (-> Clip-Higher) + +An example configuration: + +```yaml +actor_rollout_ref: + actor: + clip_ratio_low: 0.2 + clip_ratio_high: 0.28 +``` + +`clip_ratio_low` and `clip_ratio_high` specify the $\varepsilon_{\text {low }}$ and $\varepsilon_{\text {high }}$ in the DAPO objective. + +Core relevant code: + +```python +pg_losses1 = -advantages * ratio +pg_losses2 = -advantages * torch.clamp(ratio, 1 - cliprange_low, 1 + cliprange_high) +pg_losses = torch.maximum(pg_losses1, pg_losses2) +``` + +### Dynamic Sampling (with Group Filtering) + +An example configuration: + +```yaml +data: + gen_batch_size: 1536 + train_batch_size: 512 +algorithm: + filter_groups: + enable: True + metric: acc # score / seq_reward / seq_final_reward / ... + max_num_gen_batches: 10 # Non-positive values mean no upper limit +``` + +Setting `filter_groups.enable` to `True` will filter out groups whose outputs' `metric` are all the same, e.g., for `acc`, groups whose outputs' accuracies are all 1 or 0. + +The trainer will repeat sampling with `gen_batch_size` until there are enough qualified groups for `train_batch_size` or reaching the upper limit specified by `max_num_gen_batches`. + +Core relevant code: + +```python +prompt_bsz = self.config.data.train_batch_size +if num_prompt_in_batch < prompt_bsz: + print(f'{num_prompt_in_batch=} < {prompt_bsz=}') + num_gen_batches += 1 + max_num_gen_batches = self.config.algorithm.filter_groups.max_num_gen_batches + if max_num_gen_batches <= 0 or num_gen_batches < max_num_gen_batches: + print(f'{num_gen_batches=} < {max_num_gen_batches=}. Keep generating...') + continue + else: + raise ValueError( + f'{num_gen_batches=} >= {max_num_gen_batches=}. Generated too many. Please check your data.' + ) +else: + # Align the batch + traj_bsz = self.config.data.train_batch_size * self.config.actor_rollout_ref.rollout.n + batch = batch[:traj_bsz] +``` + +### Flexible Loss Aggregation Mode (-> Token-level Loss) + +An example configuration: + +```yaml +actor_rollout_ref: + actor: + loss_agg_mode: "token-mean" # / "seq-mean-token-sum" / "seq-mean-token-mean" + # NOTE: "token-mean" is the default behavior +``` + +Setting `loss_agg_mode` to `token-mean` will mean the (policy gradient) loss across all the tokens in all the sequences in a mini-batch. + +Core relevant code: + +```python +if loss_agg_mode == "token-mean": + loss = verl_F.masked_mean(loss_mat, loss_mask) +elif loss_agg_mode == "seq-mean-token-sum": + seq_losses = torch.sum(loss_mat * loss_mask, dim=-1) # token-sum + loss = torch.mean(seq_losses) # seq-mean +elif loss_agg_mode == "seq-mean-token-mean": + seq_losses = torch.sum(loss_mat * loss_mask, dim=-1) / torch.sum(loss_mask, dim=-1) # token-mean + loss = torch.mean(seq_losses) # seq-mean +else: + raise ValueError(f"Invalid loss_agg_mode: {loss_agg_mode}") +``` + +### Overlong Reward Shaping + +An example configuration: + +```yaml +data: + max_response_length: 20480 # 16384 + 4096 +reward_model: + overlong_buffer: + enable: True + len: 4096 + penalty_factor: 1.0 +``` + +Setting `overlong_buffer.enable` to `True` will penalize the outputs whose lengths are overlong but still within the hard context limit. + +Specifically, the penalty increases linearly from `0` to `overlong_buffer.penalty_factor` when the length of the output exceeds the `max_response_length - overlong_buffer.len` by `0` to `overlong_buffer.len` tokens. + +Core relevant code: + +```python +if self.overlong_buffer_cfg.enable: + overlong_buffer_len = self.overlong_buffer_cfg.len + expected_len = self.max_resp_len - overlong_buffer_len + exceed_len = valid_response_length - expected_len + overlong_penalty_factor = self.overlong_buffer_cfg.penalty_factor + overlong_reward = min(-exceed_len / overlong_buffer_len * overlong_penalty_factor, 0) + reward += overlong_reward +``` + +## FAQ + +### Where is the "Overlong Filtering" in the paper? + +Most experiments in the paper, including the best-performant one, are run without Overlong Filtering because it's somehow overlapping with Overlong Reward Shaping in terms of properly learning from the longest outputs. So we don't implement it here. + +### What's the difference between [the `recipe/dapo` directory in the `main` branch](https://github.com/verl-project/verl-recipe/tree/main/dapo) and the [`recipe/dapo` branch](https://github.com/verl-project/verl-recipe/tree/main/dapo/recipe/dapo)? + +[The `recipe/dapo` branch](https://github.com/verl-project/verl-recipe/tree/main/dapo/recipe/dapo) is for **as-is reproduction** and thus won't be updated with new features. + +[The `recipe/dapo` directory in the `main` branch](https://github.com/verl-project/verl-recipe/tree/main/dapo) works as an example of how to extend the latest `verl` to implement an algorithm recipe, which will be maintained with new features. + +### Why can't I produce similar results after modifications? + +RL infrastructures nowadays still have inherent unrobustness, on which we are still working hard to improve. + +We strongly recommend to only modify one thing at a time. + +We also list some known problems here: + +1. Enabling CUDA graph (`enforce_eager=False`) might cause model performance degradation, whose cause is still under investigation. diff --git a/verl_0720_main/verl/docs/algo/dppo.md b/verl_0720_main/verl/docs/algo/dppo.md new file mode 100644 index 0000000000000000000000000000000000000000..d4cd798638c18e179802c6c2ce41698e0bf6028c --- /dev/null +++ b/verl_0720_main/verl/docs/algo/dppo.md @@ -0,0 +1,96 @@ +# Divergence Proximal Policy Optimization (DPPO) + +Last updated: 02/25/2026. + + +
+ +## Rethinking the Trust Region in LLM Reinforcement Learning + +[![Paper](https://img.shields.io/badge/paper-A42C25?style=for-the-badge&logo=arxiv&logoColor=white )](https://arxiv.org/pdf/2602.04879) +[![Github](https://img.shields.io/badge/Stable_RL-000000?style=for-the-badge&logo=github&logoColor=000&logoColor=white)](https://github.com/sail-sg/Stable-RL) +[![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=twitter&logoColor=white)](https://x.com/QPHutu/status/2019435642539897303) + +
+ + +## ✨Getting started + +1. Prepare the datasets by running [prepare_dapo_data.sh](https://github.com/verl-project/verl-recipe/blob/3490a22a0a3adeb7e4787fe70b1060b642efbae4/dapo/prepare_dapo_data.sh): + +```bash +bash prepare_dapo_data.sh # This downloads the datasets to ${HOME}/verl/data by default +``` + +2. Prepare the model: + +```bash +hf download Qwen/Qwen3-30B-A3B-Base --local-dir ${HOME}/verl/models/Qwen3-30B-A3B-Base +``` + +3. Run the script: + +```bash +# run DPPO-Binary-KL +LOSS_MODE=dppo_kl bash examples/dppo_trainer/run_qwen3_30b_a3b_megatron.sh + +# run DPPO-Binary-TV +LOSS_MODE=dppo_tv bash examples/dppo_trainer/run_qwen3_30b_a3b_megatron.sh + +# run GRPO baseline +LOSS_MODE=vanilla CLIP_LOW=0.2 CLIP_HIGH=0.2 bash examples/dppo_trainer/run_qwen3_30b_a3b_megatron.sh +# or GRPO with clip higher +LOSS_MODE=vanilla CLIP_LOW=0.2 CLIP_HIGH=0.28 bash examples/dppo_trainer/run_qwen3_30b_a3b_megatron.sh +``` + +## 📖Introduction + +
+ issue +
+ +Comparison of **PPO** and the proposed **DPPO** (the Binary-TV variant). **(Left)** The surrogate objective and corresponding masks for PPO and DPPO. PPO (and variants like GRPO) employs a heuristic mask based on the probability ratio. In contrast, DPPO utilizes a more principled mask based on a direct approximation of policy divergence (e.g., Total Variation), ensuring updates stay within a theoretically grounded trust region. **(Right)** Experimental results on the AIME24 using Qwen3-30B-A3B-Base. DPPO significantly outperforms GRPO baselines, achieving superior training stability and final performance even without rollout routing replay (R3). + +
+ issue +
+ +DPPO variants achieve stable training while controlling the training-inference mismatch at a low level. In contrast, methods without a trust region (PG-IS, CISPO) or with a misspecified one (MiniRL) suffer from growing mismatch and eventual collapse. + +
+ issue +
+ +The plots show numerical differences between a training and an inference engine for Qwen3-30B-A3B-Base with identical parameters. **(Left)** The probability ratio (used in PPO) is highly volatile for low-probability tokens. **(Right)** In contrast, the TV divergence is more stable. This highlights a key flaw of PPO's clipping mechanism: it **over-penalizes low-probability tokens**, which can slow down learning; and **under-penalizes high-probability tokens**, which can permit large, destabilizing updates. + + +
+ issue +
+ +The most frequently clipped tokens (by GRPO) are important to the reasoning task! +They are dominated by: +- numbers, like 1, 4 +- mathematical symbols, like +, -, = +- reasoning and structural Words: Wait, Thus, Next + +## Top-K divergence approximation + +We only implement the DPPO-Binary-TV/DPPO-Binary-KL here due to their simplicity. + +For the TopK divergence approximation, please refer to the [the original repo](https://github.com/sail-sg/Stable-RL) for a complete implementation. + +## Citation +If you find our works useful for your research, please consider citing: + +```bibtex +@article{qi2026dppo, + title={Rethinking the Trust Region in LLM Reinforcement Learning}, + author={Qi, Penghui and Zhou, Xiangxin and Liu, Zichen and Pang, Tianyu and Du, Chao and Lin, Min and Lee, Wee Sun}, + journal={arXiv preprint arXiv:2602.04879}, + year={2026} +} +``` + +## 🌻Acknowledgement +We implement our reinforcement learning algorithm extending from [verl](https://github.com/verl-project/verl). We utilize [vLLM](https://github.com/vllm-project/vllm) and [sglang](https://github.com/sgl-project/sglang) for inference. Our models are trained primarily on [Qwen3 family](https://huggingface.co/collections/Qwen/qwen3). Our training data is built from [DAPO-MATH](https://huggingface.co/datasets/BytedTsinghua-SIA/DAPO-Math-17k). Thanks for their great contributions! diff --git a/verl_0720_main/verl/docs/algo/entropy.md b/verl_0720_main/verl/docs/algo/entropy.md new file mode 100644 index 0000000000000000000000000000000000000000..091190f79e6521cd755e9f3fab54565851da8afe --- /dev/null +++ b/verl_0720_main/verl/docs/algo/entropy.md @@ -0,0 +1,115 @@ +# Recipe: Entropy Mechanism + +Last updated: 06/27/2025. + + +
+ + The Entropy Mechanism of Reinforcement Learning for Large Language Model Reasoning. + +[![Paper](https://img.shields.io/badge/paper-A42C25?style=for-the-badge&logo=arxiv&logoColor=white)](https://arxiv.org/pdf/2505.22617) [![Github](https://img.shields.io/badge/PRIME-000000?style=for-the-badge&logo=github&logoColor=000&logoColor=white)](https://github.com/PRIME-RL/Entropy-Mechanism-of-RL) [![alphaXiv](https://img.shields.io/badge/discussion-A42C25?style=for-the-badge&logo=arxiv&logoColor=white&color=blue +)](https://www.alphaxiv.org/abs/2505.22617) [![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=twitter&logoColor=white)](https://x.com/stingning/status/1928088554166505667) [![Twitter](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=twitter&logoColor=white)](https://x.com/charlesfornlp/status/1928089451080585283) [![Twitter-ak](https://img.shields.io/badge/Twitter-%23000000.svg?style=for-the-badge&logo=twitter&logoColor=white)](https://x.com/_akhaliq/status/1928077929105268861) + + + + +
+ + +## 🎉News + +- **[2025/05/29]** 🎉 Ranked **#1** of the day on [Huggingface Daily Papers](https://huggingface.co/papers?date=2025-05-29). +- **[2025/05/29]** Released our Paper on arXiv. See [here](https://arxiv.org/pdf/2505.22617). We provide insights into the entropy mechanism of RL for LLMs and propose two simple yet effective strategies to alleviate the entropy collapse. + + + +## ✨Getting started + +After preparing the training data, for training Qwen2.5-7B on a single node, taking the KL-Cov approach as an example, you can simply run: + +``` +cd verl +conda activate your_env +bash recipe/dapo/7b_kl_cov.sh +``` + +While for training Qwen2.5-32B on multi nodes, you can run the following commands: + +``` +cd verl +conda activate your_env +bash recipe/dapo/32b_kl_cov.sh +``` + +## 📖Introduction + +
+ issue +
+ +This paper addresses the entropy collapse issue in scaling reinforcement learning (RL) for large language models (LLMs), where policy entropy drops sharply during training, leading to overconfidence and performance saturation. We empirically establish a relationship between entropy ($H$) and performance ($R$): $R=−aexp(H)+b$, showing performance is bottlenecked by entropy exhaustion. + +
+ issue +
+ +Theoretically, we find entropy changes are driven by the covariance between action probability and logit updates, which correlates with advantage in Policy Gradient methods. High-probability, high-advantage actions reduce entropy, while rare, high-advantage actions increase it. Empirically, the covariance term remains positive, explaining entropy’s monotonic decline. To mitigate this, we propose ​​Clip-Cov​​ and ​​KL-Cov​​, which restrict updates for high-covariance tokens. These methods effectively prevent entropy collapse, and improve performance. + +## 📃Evaluation + +
+ issue +
+ + +Our method is able to maintain a considerably higher level of entropy throughout training. For example, when the baseline's entropy reaches a plateau and can no longer be consumed, the KL-Cov method still sustains an entropy level over 10 times higher. Meanwhile, the response length of the policy model steadily increases, and its performance on the test set consistently surpasses that of the baseline. This indicates that our model is able to explore more freely during training, learning better policy through RL. +| **Method** | **AIME24** | **AIME25** | **AMC** | **MATH-500** | **OMNI-MATH** | **OlympiadBench** | **Minerva** | **Avg.** | +| ----------------- | ---------: | ---------: | -------: | -----------: | ------------: | ----------------: | ----------: | -------: | +| *Qwen2.5-7B* | | | | | | | | | +| GRPO | 21.2 | 9.6 | 58.7 | 78.8 | 27.9 | 40.7 | 36.7 | 38.6 | +| w. Clip-higher | 18.1 | 11.5 | 56.6 | 79.2 | 29.8 | 43.3 | 40.4 | 38.8 | +| w. **`CLIP-Cov`** | 22.1 | **15.8** | 58.2 | 80.4 | **30.5** | **44.1** | **41.1** | 40.4 | +| w. **`KL-Cov`** | **22.6** | 12.9 | **61.4** | **80.8** | 29.1 | 42.6 | 38.2 | **40.6** | +| *Qwen2.5-32B* | | | | | | | | | +| GRPO | 21.8 | 16.2 | 69.7 | 84.2 | 35.2 | 43.6 | 45.5 | 45.8 | +| w. Clip-higher | 35.6 | 22.3 | 69.5 | 77.2 | 35.1 | 42.5 | 43.0 | 47.2 | +| w. **`CLIP-Cov`** | 32.3 | 22.7 | 67.2 | **87.0** | **42.0** | **57.2** | 46.0 | 50.3 | +| w. **`KL-Cov`** | **36.8** | **30.8** | **74.5** | 84.6 | 39.1 | 49.0 | **46.3** | **52.2** | + +Our two approaches both achieve non-trivial improvements across all benchmarks. Compared to GRPO, our method outperforms it by 2.0% on average for the 7B model and by 6.4% for the 32B model. Moreover, we observe that our method yields more substantial gains on the larger Qwen2.5-32B. Specifically, our method achieves improvements of 15.0% and 14.6% compared to GRPO on the most challenging benchmarks, AIME24 and AIME25, respectively. + + +## 🎈Citation +If you find this paper or repo helpful, please cite us. + +```bibtex +@article{cui2025entropy, + title={The Entropy Mechanism of Reinforcement Learning for Reasoning Language Models}, + author={Cui, Ganqu and Zhang, Yuchen and Chen, Jiacheng and Yuan, Lifan and Wang, Zhi and Zuo, Yuxin and Li, Haozhan and Fan, Yuchen and Chen, Huayu and Chen, Weize and others}, + journal={arXiv preprint arXiv:2505.22617}, + year={2025} +} +``` +## 🌻Acknowledgement +We implement our reinforcement learning algorithm extending from [verl](https://github.com/verl-project/verl). We utilize [vLLM](https://github.com/vllm-project/vllm) for inference. Our models are trained primarily on [Qwen2.5 family](https://github.com/QwenLM/Qwen2.5). Our training data is built from [DAPO-MATH](https://huggingface.co/datasets/BytedTsinghua-SIA/DAPO-Math-17k). Thanks for their great contributions! + +## 📬 Contact + +For questions, discussion, or collaboration opportunities, feel free to contact: +- Ganqu Cui: cuiganqu@pjlab.org.cn +- Yuchen Zhang: yuchen.zhang2003@gmail.com +- Jiacheng Chen: jackchan9345@gmail.com +- Ning Ding: ningding.cs@gmail.com + diff --git a/verl_0720_main/verl/docs/algo/gpg.md b/verl_0720_main/verl/docs/algo/gpg.md new file mode 100644 index 0000000000000000000000000000000000000000..36bede8c319040ae713ef335372f2caa40ce44a3 --- /dev/null +++ b/verl_0720_main/verl/docs/algo/gpg.md @@ -0,0 +1,36 @@ +# GPG: Group Policy Gradient + +Last updated: 07/03/2025. + +Group Policy Gradient (GPG) is a minimalist reinforcement learning (RL) method that enhances the reasoning ability of large language models without relying on supervised fine-tuning or complex tricks. GPG revisits traditional policy gradients and directly optimizes the RL objective—no surrogate losses, no KL penalties, no critic, and no reference model. Compared to GRPO, GPG is simpler, more efficient, and achieves better results on many tasks. For more details, please refer to the original paper [GPG: A Simple and Strong Reinforcement Learning Baseline for Model Reasoning +](https://arxiv.org/abs/2504.02546). + +## Key Components +- Use a corrected advantage function to improve policy gradient accuracy and training efficiency. +- By eliminating the critic and reference models, avoiding KL divergence constraints, significantly simplifies the training process compared to Group Relative Policy Optimization (GRPO) + +## Configuration +To configure GPG within the framework, use the following YAML settings. + +```yaml +algorithm: + adv_estimator: gpg +actor_rollout_ref: + actor: + policy_loss: + loss_mode: "gpg" +``` + +## Advanced Extensions +GPG is a simple and strong baseline for model reasoning. Although it avoids using KL loss in its original form, you can still use KL loss to further improve the performance. + +```yaml +algorithm: + adv_estimator: gpg +actor_rollout_ref: + actor: + use_kl_loss: True # enable kl regularization + kl_loss_coef: 0.01 + policy_loss: + loss_mode: "gpg" +``` \ No newline at end of file diff --git a/verl_0720_main/verl/docs/algo/grpo.md b/verl_0720_main/verl/docs/algo/grpo.md new file mode 100644 index 0000000000000000000000000000000000000000..0a1fb1d324ae89ad97eb49dbc2e9331aaf2da314 --- /dev/null +++ b/verl_0720_main/verl/docs/algo/grpo.md @@ -0,0 +1,72 @@ +# Group Relative Policy Optimization (GRPO) + +Last updated: 05/31/2025. + +In reinforcement learning, classic algorithms like PPO rely on a "critic" model to estimate the value of actions, guiding the learning process. However, training this critic model can be resource-intensive. + +GRPO simplifies this process by eliminating the need for a separate critic model. Instead, it operates as follows: +- Group Sampling: For a given problem, the model generates multiple possible solutions, forming a "group" of outputs. +- Reward Assignment: Each solution is evaluated and assigned a reward based on its correctness or quality. +- Baseline Calculation: The average reward of the group serves as a baseline. +- Policy Update: The model updates its parameters by comparing each solution's reward to the group baseline, reinforcing better-than-average solutions and discouraging worse-than-average ones. + +This approach reduces computational overhead by avoiding the training of a separate value estimation model, making the learning process more efficient. For more details, refer to the original paper [DeepSeekMath: Pushing the Limits of Mathematical Reasoning in Open Language Models](https://arxiv.org/pdf/2402.03300) + +## Key Components + +- No Value Function (Critic-less): unlike PPO, GRPO does not train a separate value network (critic) +- Group Sampling (Grouped Rollouts): instead of evaluating one rollout per input, GRPO generates multiple completions (responses) from the current policy for each prompt. This set of completions is referred to as a group. +- Relative Rewards: within each group, completions are scored (e.g., based on correctness), and rewards are normalized relative to the group. + +## Configuration + +Note that all configs containing `micro_batch_size` are used to configure the maximum sample or token count per forward or backward pass to avoid GPU OOMs, whose value should not change algorithmic/convergence behavior. + +Despite that many configurations start with the `ppo_` prefix, they work across different RL algorithms in verl, as the GRPO training loop is similar to that of PPO (without critic). + +![image](https://github.com/user-attachments/assets/16aebad1-0da6-4eb3-806d-54a74e712c2d) + +- `actor_rollout.ref.rollout.n`: For each prompt, sample n times. Default to 1. For GRPO, please set it to a value larger than 1 for group sampling. + +- `data.train_batch_size`: The global batch size of prompts used to generate a set of sampled trajectories/rollouts. The number of responses/trajectories is `data.train_batch_size * actor_rollout.ref.rollout.n` + +- `actor_rollout_ref.actor.ppo_mini_batch_size`: The set of sampled trajectories is split into multiple mini-batches with batch_size=ppo_mini_batch_size for PPO actor updates. The ppo_mini_batch_size is a global size across all workers. + +- `actor_rollout_ref.actor.ppo_epochs`: Number of epochs for GRPO updates on one set of sampled trajectories for actor + +- `actor_rollout_ref.actor.clip_ratio`: The GRPO clip range. Default to 0.2 + +- `algorithm.adv_estimator`: Default is gae. Please set it to grpo instead + +- `actor_rollout_ref.actor.loss_agg_mode`: Default is "token-mean". Options include "token-mean", "seq-mean-token-sum", "seq-mean-token-mean". The original GRPO paper takes the sample-level loss (seq-mean-token-mean), which may be unstable in long-CoT scenarios. All GRPO example scripts provided in verl uses the default configuration "token-mean" for loss aggregation instead. + +Instead of adding KL penalty in the reward, GRPO regularizes by directly adding the KL divergence between the trained policy and the reference policy to the loss: + +- `actor_rollout_ref.actor.use_kl_loss`: To use kl loss in the actor. When used, we are not applying KL in the reward function. Default is False. Please set it to True for GRPO. + +- `actor_rollout_ref.actor.kl_loss_coef`: The coefficient of kl loss. Default is 0.001. + +- `actor_rollout_ref.actor.kl_loss_type`: Support kl(k1), abs, mse(k2), low_var_kl(k3) and full. Appending "+" in the end (e.g., 'k1+' and 'k3+') would apply straight through to employ k2 for unbiased gradient estimation, regardless of the kl value estimation (see https://github.com/verl-project/verl/pull/2953#issuecomment-3162113848 for more details). How to calculate the kl divergence between actor and reference policy. See this blog post for detailed analysis: http://joschu.net/blog/kl-approx.html + +## Advanced Extensions + +### DrGRPO + +[Understanding R1-Zero-Like Training: A Critical Perspective](https://arxiv.org/pdf/2503.20783) claims there's optimization bias in GRPO, which leads to artificially longer responses, especially for incorrect outputs. This inefficiency stems from the way GRPO calculates advantages using group-based reward normalization. Instead, DrGRPO aggregates token-level losses by normalizing with a global constant to eliminate length bias. + +Configure the following to enable DrGRPO, with all other parameters the same as GRPO's: + +- `actor_rollout_ref.actor.loss_agg_mode`: "seq-mean-token-sum-norm", which turns off seq-dim averaging +- `actor_rollout_ref.actor.loss_scale_factor`: (Optional) Set to a constant integer (e.g., max response length) to ensure consistent normalization throughout training. If not set, uses the current batch's response length. +- `actor_rollout_ref.actor.use_kl_loss`: Please set it to False for DrGRPO +- `algorithm.norm_adv_by_std_in_grpo`: False, which turns off standard deviation norm + +## Reference Example + +Qwen2.5 GRPO training log and commands: [link](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/qwen2-7b-fsdp2.log) + +```bash +bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh +``` + +For more reference performance, please see https://verl.readthedocs.io/en/latest/algo/baseline.html diff --git a/verl_0720_main/verl/docs/algo/opd.md b/verl_0720_main/verl/docs/algo/opd.md new file mode 100644 index 0000000000000000000000000000000000000000..2fd5f60c276e1e10ca87a686126e25e252f4fe5e --- /dev/null +++ b/verl_0720_main/verl/docs/algo/opd.md @@ -0,0 +1,778 @@ +# On-Policy Distillation (OPD) + +**Author:** [Jacob Helwig](https://jacobhelwig.github.io/) + +Last updated: 05/26/2026. + +## Background + +### Summary + +1. OPD distills knowledge from teacher model(s) into a student model on states sampled from the student policy. +2. Compared with SFT or standard KD, OPD reduces exposure bias by aligning training-time states with inference-time states. +3. Compared with RLVR, OPD provides dense, continuous, token-level supervision rather than sparse outcome-level rewards. + +### Knowledge Distillation + +Knowledge distillation (KD) transfers behavior from a teacher model to a student model. In mathematical reasoning, for example, standard KD samples reasoning traces and final answers from the teacher, then trains the student with a next-token prediction objective against the teacher distribution. + +This can introduce exposure bias. During training, the student observes states sampled from the teacher. At inference time, however, states are sampled from the student. Unless the teacher and student induce the same state distribution, the student may not learn how the teacher would act in the states the student actually visits. + +For example, the student may prefer algebraic proofs while the teacher prefers geometric proofs. Standard KD primarily distills the teacher's behavior along geometric-proof trajectories, even if the student continues to generate algebraic-proof trajectories at inference time. + +### On-Policy RL + +RLVR has no train/inference state mismatch by construction: rollouts are sampled from the student policy, so the states the student trains on are exactly the states it would visit at inference time. If a rollout produces a correct final answer, the policy is updated to increase the likelihood of the sampled solution. + +This aligns training and inference states, but the reward is sparse and outcome-based. A rollout typically contributes a binary success signal at the sequence level rather than dense token-level feedback. + +### On-Policy Distillation + +On-policy distillation (OPD) [1,2,3] combines the state alignment of on-policy RL with the dense supervision of KD. The student samples rollouts from its own policy. Given each student-generated state, the teacher provides next-token log-probabilities, and the student is trained to match the teacher distribution at those states. + +Intuitively, the teacher provides guidance conditioned on the trajectory the student actually chose. If the student follows an algebraic proof path, the teacher supplies supervision for what it would do from that algebraic state. + +Formally, let $x \sim p_{\mathrm{data}}$ be a prompt, $y \sim \pi_{\theta}(\cdot \mid x)$ be a student rollout, and $s_t = (x, y_{.*` — one entry per teacher ([`DistillationTeacherModelConfig`](../../verl/workers/config/distillation.py)) +- `distillation.distillation_loss.*` — loss-mode and aggregation settings ([`DistillationLossConfig`](../../verl/workers/config/distillation.py)) + +Defaults below are the YAML defaults from +[`verl/trainer/config/distillation/distillation.yaml`](../../verl/trainer/config/distillation/distillation.yaml). + +--- + +### `distillation.enabled` (bool) + +Whether on-policy distillation is enabled. Default: `false`. + +When `true`, `main_ppo` allocates a separate teacher resource pool and spins up +one or more teacher inference servers; the actor loss switches from `ppo_loss` +to `distillation_ppo_loss`. + +### `distillation.n_gpus_per_node` (int) + +Number of GPUs per node in the teacher resource pool. Default: `8`. + +### `distillation.nnodes` (int) + +Number of nodes in the teacher resource pool. Default: `0` (effectively +disables the pool — must be set to `≥ 1` when `enabled=True`). + +**Constraint:** the total teacher pool size (`n_gpus_per_node × nnodes`) must +exactly equal the sum of `(num_replicas × per_replica_world_size)` across all +configured teachers, or `DistillationConfig.__post_init__` raises. + +### `distillation.teacher_key` (str) + +Field on each sample's data proto used to route the sample to the right +teacher in multi-teacher setups. Default: `"data_source"`. + +- **Single-teacher**: ignored (everything goes to the sole teacher). +- **Multi-teacher**: the value of `sample[teacher_key]` must match the `key` + of one of the configured teachers, or + `AsyncTeacherLLMServerManager._resolve_teacher_key` raises. + +### `distillation.teacher_models` (dict) + +Map of teacher entries. Each value is a `DistillationTeacherModelConfig`. + +The single-teacher entry is named `teacher_model` by convention. **Pitfall:** +when adding more named teachers, the `teacher_model` entry is silently popped +— so do **not** keep `teacher_model` as one entry alongside other named +teachers. Either rely on it alone, or rename it (e.g. `teacher_model1`) and +add the others. + +```bash +# WRONG: teacher_model is popped, only teacher_model2 is used +distillation.teacher_models.teacher_model.key=openai/gsm8k +distillation.teacher_models.teacher_model.model_path=Qwen/Qwen3-4B ++distillation.teacher_models.teacher_model2.key=hiyouga/geometry3k ++distillation.teacher_models.teacher_model2.model_path=Qwen/Qwen3-VL-4B-Instruct + +# RIGHT: rename the first teacher ++distillation.teacher_models.teacher_model1.key=openai/gsm8k ++distillation.teacher_models.teacher_model1.model_path=Qwen/Qwen3-4B ++distillation.teacher_models.teacher_model2.key=hiyouga/geometry3k ++distillation.teacher_models.teacher_model2.model_path=Qwen/Qwen3-VL-4B-Instruct +``` + +--- + +### `distillation.teacher_models..key` (str) + +Identifier used to route samples to this teacher in multi-teacher mode. Must +match the value of `sample[distillation.teacher_key]`. Default: `null` +(required for multi-teacher; auto-set to `"default"` for single-teacher). + +### `distillation.teacher_models..model_path` (str) + +Local path or Hugging Face model id for the teacher. **Required.** + +The teacher must share the student's tokenizer/vocab — typically satisfied by +picking a teacher in the same model family (e.g. `Qwen3-32B` teacher for a +`Qwen3-8B` student). + +### `distillation.teacher_models..num_replicas` (int) + +Number of inference replicas of this teacher to launch. Default: `0`. + +Each replica occupies +`per_replica_world_size = inference.tensor_model_parallel_size * inference.data_parallel_size * inference.pipeline_model_parallel_size` +GPUs, so the teacher's total footprint is `num_replicas × per_replica_world_size`. + +For a **single teacher**, you may leave this at `0`: `_resolve_teacher_models` +auto-fills it as `pool_size // per_replica_world_size`. + +### `distillation.teacher_models..inference.*` + +Inference-engine config for this teacher; see [`RolloutConfig`](../../verl/workers/config/rollout.py). Same shape as +`actor_rollout_ref.rollout.*`. Notable defaults inherited from the YAML: + +- `inference.name` — e.g. `vllm` or `sglang`. +- `inference.tensor_model_parallel_size` — default `2`. +- `inference.gpu_memory_utilization` — default `0.5`. +- `inference.max_model_len` — must accommodate `student_prompt_length + + student_response_length + 1`; otherwise + `validate_and_prepare_for_distillation` raises. +- `inference.engine_kwargs.vllm.max_logprobs` — auto-bumped to `≥ + distillation.distillation_loss.topk` whenever the active loss mode requires + top-k. + +`validate_and_prepare_for_distillation` rewrites +`inference.prompt_length := prompt_length + response_length` and +`inference.response_length := 1`, since the teacher only scores the +(prompt + response) prefix and emits one dummy token. + +--- + +### `distillation.distillation_loss.loss_mode` (str) + +Distillation divergence to use. Default: `"k3"`. + +Two registered families: + +- **Top-k** (`forward_kl_topk`): forward KL using the teacher's top-k logits. +- **Single-sample KL estimators** (`kl`, `k1`, `abs`, `mse`, `k2`, + `low_var_kl`, `k3`): per-token Monte Carlo estimators of reverse KL + computed from the student's `log_probs` and the teacher's single + `log_prob` at the sampled token. + +### `distillation.distillation_loss.topk` (int, optional) + +`k` for top-k distillation losses. Default: `32`. + +Only used when `loss_mode` requires top-$k$ (e.g. `forward_kl_topk`). Drives both +the teacher's `prompt_logprobs` request size and (for vLLM) the engine's +`max_logprobs` cap. + +### `distillation.distillation_loss.use_task_rewards` (bool) + +Whether to add the standard PPO/GRPO task-reward loss on top of the +distillation loss. Default: `true`. + +- `true`: final loss is `policy_loss + distillation_loss_coef × distill_loss`. +- `false`: the PPO term is zeroed and only the distillation loss contributes. + +Orthogonal to `use_policy_gradient` (which controls how the *distillation +signal itself* is applied). + +### `distillation.distillation_loss.distillation_loss_coef` (float) + +Coefficient on the distillation loss when combined with task rewards. +Default: `1.0`. Only takes effect when `use_task_rewards=true`. + +### `distillation.distillation_loss.loss_max_clamp` (float, optional) + +Per-token clamp on the distillation loss to `[-clamp, +clamp]`. Default: +`null` (no clamp). + +### `distillation.distillation_loss.log_prob_min_clamp` (float, optional) + +Lower clamp on log probabilities used inside divergence computations, to +prevent `log q − log p` from blowing up when `p` or `q` are near zero. +Default: `null`. + +### `distillation.distillation_loss.use_policy_gradient` (bool) + +How the distillation signal is applied. `true` corresponds to PG OPD, `false` to GKD OPD. Default: `false`. + + +**Validation:** + +- `use_policy_gradient=False` + `loss_mode="k1"` $\to$ `ValueError`. The k1 loss + has no gradient through the teacher logprob, so backpropagating it directly + is meaningless. +- `use_policy_gradient=True` + `loss_mode="forward_kl_topk"` $\to$ warning. The + PG update only moves $\nabla_\theta\log\pi_\theta(y_t|s_t)$ for the sampled token $y_t$, so the top-$k$ + distributional signal is largely unused. + +### `distillation.distillation_loss.policy_loss_mode` (str) + +Name of the policy loss to use when `use_policy_gradient=True`. Default: +`"vanilla"`. **Currently only `"vanilla"` is supported**; anything else raises +`NotImplementedError`. + +### `distillation.distillation_loss.clip_ratio` (float) + +PPO clip ratio used by the policy-gradient update when +`use_policy_gradient=True`. Default: `0.2`. + +### `distillation.distillation_loss.clip_ratio_low` (float) + +Lower bound of the PPO clip range. Default: `0.2`. + +### `distillation.distillation_loss.clip_ratio_high` (float) + +Upper bound of the PPO clip range. Default: `0.2`. + +### `distillation.distillation_loss.global_batch_info` / `loss_settings` + +Internal fields populated at runtime — **do not set from the user side.** +`loss_settings` is auto-populated from `loss_mode` via +`get_distillation_loss_settings`; `global_batch_info` is filled by the actor +worker before the loss runs. + +## Usage + +Example scripts are available in `examples/on_policy_distillation_trainer`. This section shows how to configure different OPD recipes. + +### Quick start + +For single-teacher OPD, first enable distillation, allocate a teacher resource pool, and specify the teacher model and inference server settings: + +```yaml +distillation: + enabled: true + + n_gpus_per_node: 2 + nnodes: 1 + + teacher_models: + teacher_model: + model_path: Qwen/Qwen3-32B + inference: + name: vllm + gpu_memory_utilization: 0.8 +``` + +The teacher must share the student's tokenizer and vocabulary. This is usually true for models from the same family, such as a `Qwen3-8B` student and a `Qwen3-32B` teacher. + +In most OPD runs, disable the standard PPO/GRPO reference-policy KL. Otherwise, the student is simultaneously regularized toward the reference policy and distilled from the teacher: + +```yaml +actor_rollout_ref: + actor: + use_kl_loss: false +algorithm: + use_kl_in_reward: false +``` + +### GKD OPD + +For efficiency, the current implementation of GKD OPD uses a top-$k$ approximation to forward KL using the top-$k$ teacher logits and the forward KL: + +$$ +\mathcal{L}_{\mathrm{GKD}}^{(k)}(s_t) += +\sum_{v \in \operatorname{TopK}(\nu(\cdot \mid s_t))} +\nu(v \mid s_t) +\bigl[ +\log \nu(v \mid s_t) +- +\log \pi_\theta(v \mid s_t) +\bigr]. +$$ + +The reason GKD OPD is implemented only over the teacher top-$k$ logits is because current inference servers return log-probabilities for the sampled token and the teacher top-$k$ tokens, but do not support gathering log-probabilities at arbitrary token IDs. Therefore, the implementation supports teacher-top-$k$ forward KL, but not student-top-$k$ reverse KL. + +To use GKD OPD, set `loss_mode=forward_kl_topk`, choose `topk`, and disable policy-gradient distillation: + +```yaml +distillation: + distillation_loss: + loss_mode: forward_kl_topk + topk: 128 + use_policy_gradient: false +``` + +Do not use `forward_kl_topk` with `use_policy_gradient=true`. The top-$k$ loss contains distributional information for many teacher-preferred tokens, but a policy-gradient update only acts through the sampled token: + +$$ +\nabla_\theta \mathcal{L}_{\mathrm{PG}} +\propto +- A_t \nabla_\theta \log \pi_\theta(y_t \mid s_t). +$$ + +Thus, the update cannot directly assign credit to the non-sampled top-$k$ tokens. This discards most of the distributional signal and can produce misleading updates. For example, if the student already matches the teacher on the sampled token but overestimates other teacher-top-$k$ tokens, the forward KL is still positive; using it as a policy-gradient reward would incorrectly push on the sampled token. + + +### PG OPD + +PG OPD treats the negative reverse-KL estimate as a reward and applies a policy-gradient update. To use PG OPD with the `k1` estimator, set `loss_mode=k1`, enable policy-gradient distillation, and configure the PPO clipping range: + +```yaml +distillation: + distillation_loss: + loss_mode: k1 + use_policy_gradient: true + policy_loss_mode: vanilla + clip_ratio_low: 0.2 + clip_ratio_high: 0.28 +``` + +Currently, only `policy_loss_mode=vanilla` is supported. Other policy-loss modes, such as `dppo_tv`, require additional parameters and are not implemented for OPD. + +### Task rewards + +OPD can be optimized alone or combined with the standard PPO/GRPO task-reward loss. + +When `use_task_rewards=true`, the final loss is + +$$ +\mathcal{L} += +\mathcal{L}_{\mathrm{policy}} ++ +\lambda_{\mathrm{distill}} +\mathcal{L}_{\mathrm{distill}}, +$$ + +where $\mathcal{L}_{\mathrm{policy}}$ is the PPO/GRPO task-reward loss, $\mathcal{L}_{\mathrm{distill}}$ is the distillation loss, and $\lambda_{\mathrm{distill}}$ is set by `distillation_loss_coef`. + +To combine task rewards with distillation: + +```yaml +distillation: + distillation_loss: + use_task_rewards: true + distillation_loss_coef: 1.5 +``` + +When `use_task_rewards=false`, the PPO/GRPO task-reward loss is zeroed and the update optimizes only the distillation loss. + +### Multi-teacher OPD + +Multiple teachers can be configured by adding one entry under `distillation.teacher_models` per teacher. Each teacher has a routing `key`, model path, replica count, and inference configuration. + +```yaml +distillation: + n_gpus_per_node: 8 + nnodes: 2 + teacher_key: data_source + + teacher_models: + gsm8k: + key: "openai/gsm8k" + model_path: Qwen/Qwen3-32B + num_replicas: 2 + inference: + name: vllm + tensor_model_parallel_size: 2 + gpu_memory_utilization: 0.6 + + geo3k: + key: "hiyouga/geometry3k" + model_path: Qwen/Qwen3-VL-32B-Instruct + num_replicas: 3 + inference: + name: vllm + tensor_model_parallel_size: 4 + gpu_memory_utilization: 0.8 + +data: + shuffle: true + reward_fn_key: data_source +``` + +In this example, the teacher pool has `8 * 2 = 16` GPUs. Assuming `data_parallel_size=1` and `pipeline_model_parallel_size=1`, the teacher footprints are: + +$$ +\text{gsm8k}: 2 \text{ replicas} \times 2 \text{ GPUs} = 4 \text{ GPUs} +$$ + +$$ +\text{geo3k}: 3 \text{ replicas} \times 4 \text{ GPUs} = 12 \text{ GPUs} +$$ + +so the total teacher footprint is $4 + 12 = 16$ GPUs, matching the resource pool. + +Teacher replicas are assigned by linearly splitting the teacher resource pool into contiguous GPU bundles. Each individual replica must occupy the expected number of nodes implied by its `per_replica_world_size`: + +```python +per_replica_world_size = tensor_model_parallel_size * data_parallel_size * pipeline_model_parallel_size +``` + +With `n_gpus_per_node=8`, the example above aligns cleanly: + +```text +node 0: [gsm8k replica 0: 2 GPUs] [gsm8k replica 1: 2 GPUs] [geo3k replica 0: 4 GPUs] +node 1: [geo3k replica 1: 4 GPUs] [geo3k replica 2: 4 GPUs] +``` + +No replica crosses a node boundary unless its `per_replica_world_size` requires multiple nodes. + +A similar-looking configuration can fail if a replica's GPU bundle does not fall entirely within a single node. For example, with the same `n_gpus_per_node=8`, `nnodes=2` (pool size 16) but two teachers configured as + +- `a`: `tensor_model_parallel_size=3`, `num_replicas=2` $\to$ 6 GPUs total +- `b`: `tensor_model_parallel_size=5`, `num_replicas=2` $\to$ 10 GPUs total + +the pool size still matches (`6 + 10 = 16`), but the linear bundle layout becomes + +```text +node 0: [a_0: 0,1,2] [a_1: 3,4,5] [b_0: 6,7,... +node 1: ...,8,9,10] [b_1: 11,12,13,14,15] +``` + +Replica `b_0` spans bundles `[6, 11)` — straddling node 0 (bundles 6, 7) and node 1 (bundles 8, 9, 10). A 5-GPU replica with `n_gpus_per_node=8` is expected to fit on a single node (`ceil(5 / 8) = 1`), so `_validate_replica_node_alignment` raises. To fix it, adjust `num_replicas` and the per-teacher inference parallelism. + +#### Teacher routing + +The `teacher_key` controls routing. It must name a top-level field on each sample's `non_tensor_batch`. `data_source` is one such field, set by the dataset loader. In the example above, `teacher_key=data_source`, so samples with `data_source="openai/gsm8k"` are routed to the `gsm8k` teacher, and samples with `data_source="hiyouga/geometry3k"` are routed to the `geo3k` teacher. + +When routing by data source, enable data shuffling. Without shuffling, a dataset created by concatenating other datasets may activate only one teacher for long contiguous stretches. For example, if GSM8K examples are followed by Geo3K examples, then training will use only the GSM8K teacher for the first portion of the epoch and only the Geo3K teacher for the remaining portion. + +## Metrics + +OPD logs metrics under `actor/distillation/*`. + +### Core metrics + +- `actor/distillation/loss` + Unscaled distillation loss. When `use_task_rewards=true`, compare this with `actor/pg_loss` to choose `distillation_loss_coef`. + +- `actor/distillation/abs_loss` + Absolute value of the distillation loss. This is mainly useful for signed estimators such as `k1`, where divergences can be negative. + +- `actor/distillation/loss_min` / `actor/distillation/loss_max` + Minimum and maximum per-token distillation loss in the batch. + +### Top-$k$ metrics + +These metrics are logged for top-$k$ loss modes such as `forward_kl_topk`. + +- `actor/distillation/student_mass` + Average student probability mass assigned to the teacher top-$k$ tokens. + +- `actor/distillation/teacher_mass` + Average teacher probability mass assigned to its own top-$k$ tokens. + +- `actor/distillation/student_mass_min` / `actor/distillation/student_mass_max` + Minimum and maximum student mass on the teacher top-$k$ tokens within the batch. + +- `actor/distillation/teacher_mass_min` / `actor/distillation/teacher_mass_max` + Minimum and maximum teacher mass on the teacher top-$k$ tokens within the batch. + +- `actor/distillation/overlap_ratio` + Average fraction of teacher top-$k$ tokens that also appear in the student's + top-$k$ tokens, computed as + $|\operatorname{TopK}_{\nu}(s_t) \cap \operatorname{TopK}_{\pi_\theta}(s_t)| / k$ + over response tokens. A value near `1.0` means the teacher and student top-$k$ + token sets largely match at student-visited states. + +- `actor/distillation/overlap_token_advantage` + Average negative teacher-token KL contribution on teacher top-$k$ tokens that + also appear in the student's top-$k$ tokens. The per-token value is averaged + only over positions with at least one overlapping token; if no response + position has overlap, the metric is reported as `0.0`. + +`teacher_mass` indicates how much of the teacher distribution is covered by the selected top-$k$. Low `teacher_mass` means the top-$k$ approximation is truncating substantial teacher probability mass. This can happen either by selecting too small $k$, or due to unstable optimization leading to the student generating low probability sequences. + +`student_mass` indicates how much probability the student assigns to the teacher-preferred tokens. + +The overlap metrics follow the token-level top-$k$ overlap analysis in [8]. +They are logging-only diagnostics and do not change the distillation loss or +gradient. + +## Debugging + +A useful technique for debugging modifications and additions to the distillation pipeline is to set the student to be the same model as the teacher. The loss should be approximately zero (not exact, due to differences between train/inference engines). + +## Architecture + +OPD has two components: + +1. **Teacher logprob computation** — runs on a dedicated teacher resource pool as part of the agent loop. +2. **Student optimization** — runs on the train workers, the same actor workers + that handle PPO/GRPO updates. + +### Teacher logprob computation + +Teacher logprob computation is interleaved with rollouts inside the **Agent +Loop**. Each sample's teacher call fires as soon as its rollout finishes (no batch-wide barrier) so teacher work overlaps with the still-running +rollouts on other samples. + +1. **Input.** `AgentLoopManager.generate_sequences(prompts: DataProto)` receives + a batch of prompts. + +2. **Chunking across workers.** The manager splits the batch evenly across its + `AgentLoopWorker` actors, then dispatches each + chunk via `worker.generate_sequences.remote(chunk)`. + +3. **Per-sample fan-out inside a worker.** Inside + `AgentLoopWorker.generate_sequences`, each sample in the chunk is launched as + its own asyncio task running `_run_agent_loop`. The agent loop runs on the + rollout GPUs and produces a rollout (prompt + response token ids). + +4. **Postprocess hook.** `_run_agent_loop` calls + `self._agent_loop_postprocess(...)`. This is where teacher logprob + computation is triggered, per sample, as soon as that sample's rollout is + ready. + +5. **Worker-side teacher dispatch.** `_agent_loop_postprocess` calls + `self._compute_teacher_logprobs(...)`, which extracts the routing value from + the sample's non-tensor fields using `sample_kwargs[self.teacher_key]` + (default `teacher_key="data_source"`), then calls + `self.teacher_server_manager.compute_teacher_logprobs_single(...)`. + +6. **Teacher selection.** + `AsyncTeacherLLMServerManager.compute_teacher_logprobs_single` resolves the + teacher via `_resolve_teacher_key`: + + - **Single-teacher**: routing key is ignored; the sole configured teacher + is used. + - **Multi-teacher**: `routing_key` must match a configured teacher in + `distillation.teacher_models`; otherwise an error is raised. + + The resolved key indexes into the per-teacher `LLMServerClient` dict to pick + the right client. + +7. **Sampling params for logprob computation.** The manager builds + sampling params with `max_tokens=1` plus `prompt_logprobs=topk` (or `0`), + so the teacher computes logprobs for the (prompt + response) sequence rather than + generating new tokens. `topk` is set to `distillation.distillation_loss.topk` + when the loss mode requires top-$k$ (e.g. `forward_kl_topk`); otherwise `0` + (single-sample logprob only). + + **Temperature is always forced to `1.0`** regardless of the configured value. + `prompt_logprobs` is computed via a forward pass over the existing (prompt + response) + tokens — no sampling occurs — so temperature has no effect on the result. + The default distillation config copies the student rollout temperature via Hydra + interpolation (`temperature: ${oc.select:actor_rollout_ref.rollout.temperature}`), + which would silently set a non-1.0 value when rollout temperature differs from 1.0 + (e.g. 0.7). The manager ignores the configured value and always uses `temperature=1.0`, + logging a warning if the configured value is not 1.0. + +8. **Server-side load balancing.** The manager calls `client.generate(...)`, + which acquires a backing server through the shared `GlobalRequestLoadBalancer` + actor. + +9. **Backend execution.** With the vLLM backend the selected server is a + `vLLMHttpServer` actor; its `generate` method runs the forward pass and + returns a `TokenOutput` containing `prompt_ids` and `prompt_logprobs` for + the full (prompt + response) sequence. The SGLang backend has an analogous + server class. + +10. **Return path.** `compute_teacher_logprobs_single` packs the response into + two tensors `teacher_ids` and `teacher_logprobs`, each of shape `(S, 1 or K)` + where `S` is the sequence length and `K` is `topk` (or `1`). These are + stashed on the rollout output and later concatenated into the per-batch + `DataProto` for the student training step. + + +### Student training + +Using the `DataProto` produced by the Agent Loop (rollouts + teacher logprobs in +`teacher_logprobs`), the student step proceeds as follows. + +1. **Train entry.** `TrainingWorker.train_batch` invokes + `self.engine.train_batch(data, loss_function=self.loss_fn)`. When + distillation is enabled, `self.loss_fn` is bound to `distillation_ppo_loss` + at worker init; otherwise it is the standard `ppo_loss`. + +2. **Forward pass and (optional) inline top-k loss.** The training engine's + forward step runs the model forward and, for top-$k$ loss modes + (`distillation_use_topk=True`), invokes `distillation_ppo_loss` **as a + logits processor** while the full logits tensor is still in memory; this is + the `student_logits is not None` branch of `distillation_ppo_loss`. The + logits-processor branch dispatches to `compute_forward_kl_topk`, which has a + separate implementation per training engine (FSDP and Megatron). Per-token + `distillation_losses`, `student_mass`, `teacher_mass`, `overlap_count`, and + `overlap_token_advantage` tensors are written back into `model_output` so the + full logits can be freed before the final loss step. The overlap tensors are + used only for logging. + +3. **Final loss.** After the forward, the engine calls the loss function with + `model_output` (full logits already freed); this is the + `student_logits is None` branch of `distillation_ppo_loss`, where: + + 1. **Per-token distillation loss** is produced by `distillation_loss(...)`, + which dispatches via `get_distillation_loss_fn(loss_mode)` to one of the registered distillation losses. + + 2. **Optional clamp.** If `loss_max_clamp` is set, per-token losses are + clamped to `[-clamp, +clamp]` (k1 in particular can be negative). + + 3. **Aggregation mode** — controlled by `use_policy_gradient`: + + - `False` (GKD OPD): straight backprop on `distillation_losses`. + - `True` (PG OPD): treat `-distillation_losses` as + advantages and run PPO-style clipped importance sampling. + + 4. **Combine with task rewards.** A standard PPO policy loss is computed + from the rollout's task rewards via `ppo_loss(...)`. If + `use_task_rewards=False` it is zeroed; otherwise the final loss is + `policy_loss + distillation_loss_coef * distill_loss`. + +The returned scalar loss is what `engine.train_batch` backpropagates. + +## Files + +### **Core Implementation** + +- `verl/experimental/teacher_loop/teacher_model.py` — `MultiTeacherModelManager` and `TeacherModelManager`; spin up teacher inference replicas on the dedicated teacher resource pool and expose per-teacher `LLMServerClient` factories +- `verl/experimental/teacher_loop/teacher_manager.py` — `AsyncTeacherLLMServerManager`; routes per-sample teacher calls (single- or multi-teacher) and builds teacher sampling params +- `verl/experimental/agent_loop/agent_loop.py` — `AgentLoopWorker._compute_teacher_logprobs`; per-sample teacher dispatch from `_agent_loop_postprocess`, packs `teacher_logprobs` into the rollout output +- `verl/trainer/distillation/fsdp/losses.py` — FSDP backend `compute_forward_kl_topk` +- `verl/trainer/distillation/megatron/losses.py` — Megatron backend `compute_forward_kl_topk` +- `verl/workers/engine_workers.py` — `ActorRolloutRefWorker.init_model`; binds `distillation_ppo_loss` as the actor's `loss_fn` when distillation is enabled +- `verl/workers/engine/{fsdp,megatron}/transformer_impl.py` — training-engine forward steps; invoke `distillation_ppo_loss` first as a logits processor (top-$k$ modes) and again as the final loss +- `verl/trainer/main_ppo.py` — `is_distillation_enabled` gate; allocates the dedicated `teacher_pool` resource pool +- `verl/trainer/ppo/ray_trainer.py` — constructs `MultiTeacherModelManager` and hands its `get_client()` dict to `AgentLoopWorker(... teacher_client=...)` +- `verl/workers/rollout/llm_server.py` — `LLMServerClient` and `GlobalRequestLoadBalancer` used for both student rollout and teacher logprob computation + +### **Configuration Files** + +- `verl/trainer/config/distillation/distillation.yaml` — YAML defaults for the `distillation.*` config tree +- `verl/workers/config/distillation.py` — dataclass schema (`DistillationConfig`, `DistillationLossConfig`, `DistillationTeacherModelConfig`) + +### **Documentation** + +- `docs/algo/opd.md` — this document + +### **Example Scripts** + +- `examples/on_policy_distillation_trainer/README.md` — script index +- `examples/on_policy_distillation_trainer/run_qwen3_8b_fsdp.sh` — text, vLLM rollout, FSDP student, single teacher +- `examples/on_policy_distillation_trainer/run_qwen3_8b_megatron.sh` — text, vLLM rollout, Megatron student, single teacher +- `examples/on_policy_distillation_trainer/run_qwen3_vl_8b_fsdp.sh` — VL student/teacher, vLLM rollout, FSDP student +- `examples/on_policy_distillation_trainer/run_qwen3_8b_mopd_fsdp.sh` — multi-teacher (gsm8k text + geo3k VL), routed by `data_source` + +### **Tests** + +- `tests/workers/test_distillation_topk_symmetry_on_cpu.py` — top-k loss symmetry and overlap metric checks +- `tests/utils/test_special_megatron_kl_loss_tp.py` — Megatron KL loss and overlap metrics under tensor parallelism +- `tests/special_e2e/run_fully_async_policy_opd.sh` — end-to-end OPD with the fully-async rollouter diff --git a/verl_0720_main/verl/docs/algo/opo.md b/verl_0720_main/verl/docs/algo/opo.md new file mode 100644 index 0000000000000000000000000000000000000000..338f3a762d9585c608af28cdf4e75837dbfe11e4 --- /dev/null +++ b/verl_0720_main/verl/docs/algo/opo.md @@ -0,0 +1,33 @@ +# On-Policy RL with Optimal Reward Baseline (OPO) + +Last updated: 06/02/2025. + +Loose on-policy constraints and suboptimal baselines in reinforcement learning often lead to training instability such as large policy shifts and entropy collapse. OPO addresses these challenges by using exact on-policy training with the theretically optimal reward baseline for advantage estimation. It achieves lower policy shifts and higher output entropy, encouraging more diverse and less repetitive responses. + +OPO uses group sampling to generate multiple outputs for each input like GRPO. Unlike group-based algorithms which typically use the mean reward of a group as its baseline, OPO employs a theoretically optimal baseline: the length-weighted reward of the group. It also omits the standard deviation normalization. By adopting these two key components, OPO enables the training of a single policy model with the objective of maximizing only the expected reward. For more detailes, refer to the original paper [On-Policy RL with Optimal Reward Baseline](https://arxiv.org/pdf/2505.23585). + +## Key Components + +- Exact On-Policy Training: always generates responses from the current policy, without using any pre-generated data or off-policy data. +- Optimal Reward Baseline: uses a length-weighted reward of the group as the baseline for normalizing the rewards. + +## Configuration + +To configure OPO within the framework, use the following YAML settings. These parameters are crucial for enabling exact on-policy training and activating the optimal reward baseline. + +```yaml +algorithm: + adv_estimator: opo # Use OPO for optimal reward baseline +data: + train_batch_size: 1024 +actor_rollout_ref: + actor: + ppo_mini_batch_size: 1024 # ppo_mini_batch_size should equal to train_batch_size to enable exact on-policy training + entropy_coeff: 0 # disable entropy regularization + use_kl_loss: False # disable kl regularization + kl_loss_coef: 0 +``` + +## Advanced Extensions + +OPO can also be extended to other algorithms like RLOO and Reinforce++. It just needs to adjust their configurations to enable exact on-policy training and incorporate the optimal length-weighted reward baseline with minimal modifications to their advantage estimation functions. diff --git a/verl_0720_main/verl/docs/algo/otb.md b/verl_0720_main/verl/docs/algo/otb.md new file mode 100644 index 0000000000000000000000000000000000000000..84b943986dd0ffe3eb0e040e3fcc32608aa2b80e --- /dev/null +++ b/verl_0720_main/verl/docs/algo/otb.md @@ -0,0 +1,99 @@ +# Optimal Token Baseline (OTB) + +Last updated: 02/23/2026. + +📝 [ArXiv](https://www.arxiv.org/abs/2602.07078) | 📒 [Blog](https://richardli.xyz/optimal-token-baseline) | 🤗 [Datasets](https://huggingface.co/datasets/Jiawei415/DPAO_filter) + +Optimal Token Baseline (OTB) is a dynamic token-level baseline for gradient variance reduction in policy-gradient reinforcement learning. It weights updates with the "Realized Energy" statistic that tracks how much uncertainty has accumulated up to each token, so noisy regions get downweighted while confident regions carry more weight. + +## Key properties + +- _Token-level baselines:_ OTB adapts per token by tracking realized energy, avoiding the padding artifacts that appear when group means dilute the signal with `EOS` tokens. +- _Forward-only overhead:_ The realized-energy statistic is computed via the **Logit-Gradient Proxy**, so OTB requires no extra backward passes or gradient-norm kernels. + +## Logit-Gradient Proxy + +Computing true uncertainty per token would normally mandate per-token backward passes. OTB sidesteps this by estimating realized energy entirely from forward probabilities, so it introduces negligible runtime overhead in practice. + +## Mechanics at a glance + +For each prompt group of size `N`, OTB computes rewards-to-go `G_t` and cumulative variance weights `W_t`. The optimal baseline per token is + +``` +B*_t = (Σ_i G_t^{(i)} · W_t^{(i)}) / (Σ_i W_t^{(i)} + ε), +W_t = Σ_{j=1}^t (1 - 2π_j + Σπ_j²), +Σπ_j² = exp(logsumexp(2·logits_j) - 2·logsumexp(logits_j)). +``` + +The final advantage is `(G_t - B*_t) · mask_t`, so padding tokens stay at zero. + +## Integration in VERL + +- `AdvantageEstimator.OPTIMAL_TOKEN_BASELINE` registers `compute_optimal_token_baseline_advantage`, invoked whenever `algorithm.adv_estimator` is set to `optimal_token_baseline`. +- `ActorRolloutRefWorker.compute_log_prob` emits an additional tensor `sum_pi_squared` (Σπ² per token) when `actor.calculate_sum_pi_squared=True`. This requires disabling fused log-prob kernels, because they do not surface logits. +- Trainers assert `sum_pi_squared` exists, regroup trajectories by `non_tensor_batch["uid"]`, and run the OTB calculation. If rollout IS is active, they rescale the weights by `rollout_is_weights**2` before aggregating. +- In Ulysses sequence-parallel setups, the actor gathers, unpads, and returns Σπ² in the same way it handles log-probabilities, so OTB supports sharded sequence-parallel models out of the box. +## Configuration checklist + +- `actor_rollout_ref.actor.calculate_sum_pi_squared: true` (mandatory). +- `actor_rollout_ref.model.use_fused_kernels: false` (required until fused kernels emit logits). +- `algorithm.adv_estimator: optimal_token_baseline` for single-turn RL and `tir_optimal_token_baseline` for multi-turn RL. +- Group sampling (`actor_rollout_ref.rollout.n > 1`) to unlock OTB’s variance reduction; with `n=1` the baseline collapses to returns. + +Example OmegaConf overlay: + +```yaml +algorithm: + adv_estimator: optimal_token_baseline + +actor_rollout_ref: + actor: + calculate_sum_pi_squared: true + rollout: + n: 8 +``` + +## Example script + +See `examples/otb_trainer/run_qwen3_8b_fsdp.sh` for a reference training loop. + +## Gradient Variance Proxy Metrics + +All gradient-variance analysis in the Optimal Token Baseline work starts from the variance identity + +``` +Var(ĝ) = E[||ĝ||²] - ||E[ĝ]||², +``` + +which states that the variance of any stochastic gradient equals the mean squared magnitude minus the squared norm of its expectation. + +For a trajectory `τ`, the policy-gradient estimator is + +``` +ĝ(τ) = ∇ log π_θ(τ) · A(τ), A(τ) = R(τ) - B. +``` + +The logit-gradient proxy approximates the squared gradient norm without an extra backward pass: + +``` +||ĝ(τ)||² ≈ Ŵ(τ) · A(τ)², +``` + +where `Ŵ(τ)` is the realized energy built. Given a mini-batch `{τ_i}` of size `N`, we decompose its statistics into three diagnostics: + +- **Signal strength (squared norm of the mean gradient)** + ``` + S = || (1/N) · Σ ĝ(τ_i) ||² + ``` +- **Total power (signal + noise)** + ``` + P_total = (1/N) · Σ Ŵ(τ_i) · A(τ_i)² + ``` +- **Pure noise (estimated variance of the batch mean)** + ``` + Var_proxy = (1/(N-1)) · (P_total - S) + ``` + +`verl/trainer/ppo/metric_utils.py#L306` implements these diagnostics via `compute_variance_proxy_metrics`, emitting `variance_proxy/proxy1_signal_strength`, `variance_proxy/proxy2_total_power`, and `variance_proxy/proxy3_pure_noise`. + +Tracking these metrics provides a forward-only, low-overhead view of gradient health for any advantage estimator that supplies `sum_pi_squared`. diff --git a/verl_0720_main/verl/docs/algo/ppo.md b/verl_0720_main/verl/docs/algo/ppo.md new file mode 100644 index 0000000000000000000000000000000000000000..e17620aee6303907a50d777053620b26fea6f1eb --- /dev/null +++ b/verl_0720_main/verl/docs/algo/ppo.md @@ -0,0 +1,105 @@ +# Proximal Policy Optimization (PPO) + +Last updated: 06/19/2025. + +Proximal Policy Optimization (PPO) is a family of policy gradient methods for reinforcement learning, proposed by OpenAI in 2017. PPO strikes a balance between simplicity, stability, and performance, making it one of the most widely used algorithms in modern RL applications, including large-scale language model fine-tuning. + +Traditional policy gradient methods like REINFORCE or Vanilla Policy Gradient suffer from: + +- High variance and sample inefficiency. +- Instability due to large policy updates. + +PPO addresses this problem using a clipped surrogate objective that avoids overly large updates without requiring second-order derivatives. + +For more technical details regarding PPO, we suggest reading the introduction in the [OpenAI spinning up tutorial](https://spinningup.openai.com/en/latest/algorithms/ppo.html), and the paper [Proximal Policy Optimization Algorithms](https://arxiv.org/abs/1707.06347). + +## Key Components + +- Actor-Critic Architecture: PPO requires both an actor model (policy) and a critic model (value function). This differs from other algorithms like GRPO and RLOO that don't require a critic model. + +- Generalized Advantage Estimation (GAE): PPO uses GAE for computing advantage values, which helps reduce variance in policy gradient estimates while maintaining low bias. + +- Clipped Surrogate Objective: The core of PPO is implemented through the clipped surrogate objective function that limits policy updates. + +## Configuration + +Note that all configs containing `micro_batch_size` are used to configure the maximum sample or token count per forward or backward pass to avoid GPU OOMs, whose value should not change algorithmic/convergence behavior. + +Most critic configs are similar to those of actors. Note that the critic model is omitted from the figure below. + +![image](https://github.com/user-attachments/assets/16aebad1-0da6-4eb3-806d-54a74e712c2d) + +- `data.train_batch_size`: The global batch size of prompts used to generate a set of sampled trajectories/rollouts. The number of responses/trajectories is `data.train_batch_size * actor_rollout.ref.rollout.n` + +- `actor_rollout_ref.actor.ppo_mini_batch_size`: The set of sampled trajectories is split into multiple mini-batches with batch_size=ppo_mini_batch_size for PPO actor updates. The ppo_mini_batch_size is a global size across all workers + +- `critic.ppo_mini_batch_size`: The set of sampled trajectories is split into multiple mini-batches with batch_size=ppo_mini_batch_size for PPO critic updates. The ppo_mini_batch_size is a global size across all workers + +- `actor_rollout_ref.actor.clip_ratio`: The PPO clip range. Default to 0.2 + +- `actor_rollout_ref.actor.ppo_epochs`: Number of epochs for PPO updates on one set of sampled trajectories for actor + +- `critic.ppo_epochs`: Number of epochs for PPO updates on one set of sampled trajectories for critic. Defaults to `actor_rollout_ref.actor.ppo_epochs` + +- `algorithm.gemma`: discount factor + +- `algorithm.lam`: The lambda term that trades off between bias and variance in the GAE estimator + +- `algorithm.adv_estimator`: Support gae, grpo, reinforce_plus_plus, reinforce_plus_plus_baseline, rloo + +## Advanced Extensions + +### KL Divergence Control + +Options to prevent the policy from diverging too far from a reference policy. Two mechanisms are available: KL reward penalty and KL loss. For more technical details, see [Training language models to follow instructions with human feedback](https://arxiv.org/abs/2203.02155) + +Options to use KL loss for KL divergence control: + +- `actor_rollout_ref.actor.use_kl_loss`: to use kl loss in the actor. When used, we are not applying KL in the reward function. Default is False + +- `actor_rollout_ref.actor.kl_loss_coef`: The coefficient of kl loss. Default is 0.001. + +- `actor_rollout_ref.actor.kl_loss_type`: Support kl(k1), abs, mse(k2), low_var_kl(k3) and full. Appending "+" in the end (e.g., 'k1+' and 'k3+') would apply straight through to employ k2 for unbiased gradient estimation, regardless of the kl value estimation (see https://github.com/verl-project/verl/pull/2953#issuecomment-3162113848 for more details). How to calculate the kl divergence between actor and reference policy. See this blog post for detailed analysis: http://joschu.net/blog/kl-approx.html + +Options to use KL penalty in the reward: + +- `algorithm.use_kl_in_reward`: Whether to enable in-reward kl penalty. Default is False. + +- `algorithm.kl_penalty`: Support kl(k1), abs, mse(k2), low_var_kl(k3) and full. This defines the way to calculate the kl divergence between actor and reference policy. For specific options, refer to `kl_penalty` in core_algos.py. See this blog post for detailed analysis: http://joschu.net/blog/kl-approx.html + +- `algorithm.kl_ctrl.kl_coef`: The (initial) coefficient of in-reward kl_penalty. Default is 0.001. +- `algorithm.kl_ctrl.type`: 'fixed' for FixedKLController and 'adaptive' for AdaptiveKLController. +- `algorithm.kl_ctrl.horizon`: See source code of AdaptiveKLController for details. +- `algorithm.kl_ctrl.target_kl`: See source code of AdaptiveKLController for details. + +### Dual-clip PPO + +The Dual-Clip PPO introduces a approach by applying a lower bound to the policy ratio when the advantage is less than zero, when multiplied by a large raito, does not exceed a specified lower bound. + +![image](https://github.com/user-attachments/assets/fc232181-d8b0-4307-8dd2-4dc0a4c1c139) + +- `actor_rollout_ref.actor.clip_ratio_c`: lower bound of the value for Dual-clip PPO, defaults to 3.0 + +## Reference Example + +Qwen2.5 training log and commands: [link](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-0.5B-bsz256_2-prompt1024-resp512-0.567.log) + +```bash +bash run_gemma.sh + trainer.n_gpus_per_node=1 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + trainer.logger=console \ + critic.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + actor_rollout_ref.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + data.train_batch_size=256 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size=2 \ + critic.ppo_micro_batch_size=2 +``` + +Reference performance with verl v0.2: + +| Model | Method | Score | Link | +|-------------------------------|------------------|-------|------------------------------------------------------------------------------------------------| +| Qwen/Qwen2.5-0.5B-Instruct | pretrained model | 36.4 | [Qwen Blog](https://qwenlm.github.io/blog/qwen2.5-llm/) | +| Qwen/Qwen2.5-0.5B-Instruct | PPO | 56.7 | [PPO Command and Logs](https://github.com/eric-haibin-lin/verl-data/blob/experiments/gsm8k/Qwen2.5-0.5B-bsz256_2-prompt1024-resp512-0.567.log) | diff --git a/verl_0720_main/verl/docs/algo/rollout_corr.md b/verl_0720_main/verl/docs/algo/rollout_corr.md new file mode 100644 index 0000000000000000000000000000000000000000..5bb91657db862c667e75dab0cbdbc87bcdafa50a --- /dev/null +++ b/verl_0720_main/verl/docs/algo/rollout_corr.md @@ -0,0 +1,1327 @@ +# Rollout Correction + +**Author:** [Yingru Li](https://richardli.xyz/) + +Last updated: 10/30/2025. + +--- + +> **📖 Documentation Structure** +> +> - **This document** - Practical usage guide: configurations, presets, troubleshooting +> - **[Mathematical Formulations](rollout_corr_math.md)** - Theoretical foundations, derivations, and algorithmic details +> +> Start here for implementation, refer to the math doc for theory and design rationale. + +--- + +This document provides a comprehensive overview of the Rollout Correction implementation in verl. + +**Note on Naming**: This feature is called "Rollout Correction" to reflect the complete functionality: importance sampling (IS) weights and rejection sampling (RS). The internal variable `rollout_is_weights` retains its name as it specifically refers to the IS weights component. + +### BibTeX Citation + +```bibtex +@online{liu-li-2025-rl-collapse, + title = {When Speed Kills Stability: Demystifying {RL} Collapse from the Training-Inference Mismatch}, + author = {Liu, Jiacai and Li, Yingru and Fu, Yuqian and Wang, Jiawei and Liu, Qian and Shen, Yu}, + year = {2025}, + month = sep, + url = {https://richardli.xyz/rl-collapse} +} + +@article{li2025trust, + title={Trust Region Masking for Long-Horizon LLM Reinforcement Learning}, + author={Li, Yingru and Liu, Jiacai and Xu, Jiawei and Tong, Yuxuan and Li, Ziniu and Liu, Qian and Wang, Baoxiang}, + journal={arXiv preprint arXiv:2512.23075}, + year={2025} +} +``` + +### Blog Series + +- Main blog post: https://richardli.xyz/rl-collapse +- [Part 1: Why Mismatch Breaks LLM-RL](https://richardli.xyz/rl-collapse-1) (analytical framework using TV distance for bias and χ²-divergence for variance) +- [Part 2: The Gradient Estimator Trials](https://richardli.xyz/rl-collapse-2) (token-level vs sequence-level correction bias-variance tradeoff) +- [Part 3: When Math Meets Reality—Toxic Tails and Length Traps](https://richardli.xyz/rl-collapse-3) (why rejection over clipping, and geometric-level RS) +- Latest Paper: https://arxiv.org/abs/2512.23075 + +## Overview + +Rollout Correction provides a unified framework to handle **general off-policy problems** in RL training. Any scenario where the data collection distribution differs from the training distribution can benefit from these methods. + +**Common off-policy scenarios:** + +1. **Policy Mismatch** (Implementation Differences) + + - Different precision: FP8 vs FP16 vs BF16 vs FP32 + - Different backends: vLLM vs SGLang vs FSDP vs Megatron + - Different implementations even with identical weights + +2. **Temporal Lag** (Model Staleness) + + - Rollout uses older checkpoint while training has progressed + - Asynchronous rollout workers with stale parameters + - Common in distributed/async RL systems + +3. **Replay Buffers** + + - Training on historical trajectories from earlier iterations + - Experience replay from different policy versions + - Data augmentation or resampling strategies + +4. **Off-Policy Algorithms** + + - Behavioral cloning from expert demonstrations + - DAPO (data from auxiliary policies) + - Any algorithm using trajectories from a different policy + +5. **Data Quality Filtering** + - Reweighting or filtering collected data + - Preference learning with modified distributions + - Curriculum learning with distribution shifts + +These off-policy gaps can cause training instability and policy collapse. Rollout Correction uses importance sampling (IS) weights and rejection sampling (RS) to correct for any distribution shift between data collection and training. + +**Important Note on Common Implementation Mistakes:** + +Many LLM-RL implementations incorrectly apply PPO by **ignoring the actual rollout policy** π_rollout and assuming the training reference policy π_old is the behavior policy. This is mathematically incorrect when π_rollout ≠ π_old (which is typical in LLM-RL due to precision/backend differences between rollout and training). + +**This is not PPO's fault** - PPO itself is mathematically correct. The issue is the incorrect assumption that π_old = π_rollout in naive implementations. + +This critical implementation mistake that leads to RL training collapse was identified in the blog post ["When Speed Kills Stability: Demystifying RL Collapse from the Training-Inference Mismatch"](https://richardli.xyz/rl-collapse) and motivated the development of this rollout correction framework. + +**Mathematically correct approaches:** + +- **Decoupled mode**: Three policies (π_rollout, π_old, π_θ) with IS correction from π_rollout to π_old +- **Bypass mode**: Two policies (π_rollout = π_old, π_θ) using actual rollout policy as PPO anchor +- **Bypass + Policy Gradient mode**: Two policies (π_rollout, π_θ) with IS/RS correction and no PPO clipping + +See [Mathematical Formulations](rollout_corr_math.md#37-common-implementation-mistake) for detailed explanation. + +### Key Design Principle: Separation of IS Weights and Rejection Sampling + +The implementation cleanly separates two orthogonal mechanisms: + +1. **IS Weights** (`rollout_is_weights`): Continuous reweighting for gradient correction + + - Policy ratio: π_old/π_rollout (decoupled) or π_θ/π_rollout (bypass) + - **Safety-bounded**: Clamped to [exp(-20), exp(20)] ≈ [2e-9, 5e8] to prevent overflow + - Token level: Bounds per-token ratios + - Sequence level: Bounds product of ratios (broadcast to all tokens) + - **Truncated**: Upper clamped via `.clamp(max=rollout_is_threshold)` (TIS: Truncated Importance Sampling) + - **Zeroed at padding**: Multiplied by response_mask to zero out padding positions + - Used to weight policy gradients (variance reduction) + +2. **Rejection Sampling** (`modified_response_mask`): Binary filtering for outlier exclusion + - Creates binary mask: 1 = keep, 0 = reject + - Rejects tokens/sequences with IS ratios outside [lower_threshold, upper_threshold] + - Modifies response_mask to exclude rejected samples from training + +This separation ensures: + +- ✅ IS weights provide continuous reweighting (reduce variance) +- ✅ Rejection sampling provides hard filtering (remove extreme outliers) +- ✅ Both mechanisms can be enabled independently or together +- ✅ Safety bounds prevent numerical overflow in all cases + +## Quick Start: Using Verified Presets + +**NEW**: We now provide typed configuration with verified presets for common scenarios. These presets have been validated with tens of thousands of GPU hours across various models and training scenarios. + +### Python API + +```python +from verl.trainer.config.algorithm import RolloutCorrectionConfig + +# === Decoupled PPO mode (3 policies: π_rollout, π_old, π_θ) === +# IS weights correct for gap between π_old and π_rollout +config = RolloutCorrectionConfig.decoupled_token_is() # Token-TIS +config = RolloutCorrectionConfig.decoupled_seq_is() # Seq-TIS +config = RolloutCorrectionConfig.decoupled_seq_is_rs() # Seq-MIS +config = RolloutCorrectionConfig.decoupled_geo_rs() # Geo-RS (ratio mode) +config = RolloutCorrectionConfig.decoupled_geo_rs_token_tis() # Geo-RS + Token-TIS + +# === K3 KL Estimator presets (more stable for small KL) === +config = RolloutCorrectionConfig.decoupled_k3_rs() # K3-RS only +config = RolloutCorrectionConfig.decoupled_k3_rs_token_tis() # K3-RS + Token-TIS + +# === Bypass PPO mode (2 policies: π_rollout = π_old, π_θ) - fast === +# PPO ratio handles IS, so no explicit IS weights needed +config = RolloutCorrectionConfig.bypass_ppo_clip() # PPO-clip only +config = RolloutCorrectionConfig.bypass_ppo_clip_geo_rs() # PPO-clip + Geo-RS +config = RolloutCorrectionConfig.bypass_ppo_clip_k3_rs() # PPO-clip + K3-RS + +# === Bypass PG mode (2 policies, no PPO clipping) - fast === +# IS weights computed on-the-fly as π_θ / π_rollout +config = RolloutCorrectionConfig.bypass_pg_is() # Seq-TIS + PG +config = RolloutCorrectionConfig.bypass_pg_geo_rs() # Geo-RS + PG +config = RolloutCorrectionConfig.bypass_pg_geo_rs_token_tis() # Geo-RS + Token-TIS + PG + +# === Other === +config = RolloutCorrectionConfig.disabled() # Metrics only (no correction) +``` + +### YAML Configuration (Advanced) + +For advanced customization or YAML-based configs: + +```yaml +algorithm: + rollout_correction: + rollout_is: token # IS weights: "token", "sequence", or null + rollout_is_threshold: 2.0 # TIS upper bound, or "0.5_5.0" for IcePop + rollout_is_batch_normalize: false # Batch normalize IS weights to mean=1.0 + rollout_rs: null # Rejection sampling: comma-separated canonical options (e.g. "token_k1,seq_max_k2") + rollout_rs_threshold: null # Threshold spec: float(s) or "lower_upper" string(s) + bypass_mode: false # Skip old_log_prob computation (sets π_old = π_rollout) + loss_type: ppo_clip # Loss type in bypass mode: "ppo_clip" (default) or "reinforce" + +# REQUIRED: Enable log prob calculation +actor_rollout_ref: + rollout: + calculate_log_probs: true +``` + +## Files + +### **Core Implementation** + +- `verl/trainer/ppo/rollout_corr_helper.py` - Contains `compute_rollout_correction_and_rejection_mask()` and `compute_offpolicy_metrics()` +- `verl/trainer/ppo/core_algos.py` - Rollout Correction integration with PPO and REINFORCE modes (`compute_policy_loss_bypass_mode()`, `compute_policy_loss_reinforce()`) +- `verl/trainer/ppo/ray_trainer.py` - Bypass mode implementation (skips `old_log_prob` computation) +- `verl/workers/utils/losses.py` - `ppo_loss` loss function wired to actor `TrainingWorker` via `verl.workers.engine_workers.ActorRolloutRefWorker.init_model` +- `verl/trainer/ppo/core_algos.py` - `@register_policy_loss("bypass_mode")` policy loss that invokes `compute_rollout_correction_and_rejection_mask` and emits off-policy metrics + +### **Configuration Files** + +- `verl/trainer/config/algorithm.py` - Rollout Correction parameters in `RolloutCorrectionConfig` +- `verl/workers/config/actor.py` - Rollout Correction parameters in `PolicyLossConfig` +- `verl/trainer/config/actor/actor.yaml` - Rollout Correction configuration section +- `verl/trainer/config/ppo_trainer.yaml` - Algorithm config with Rollout Correction + +### **Documentation** + +- `docs/examples/config.rst` - Configuration parameter descriptions + +### **Example Scripts** + +- `recipe/dapo/run_dapo_qwen2.5_32b_rollout_corr.sh` - DAPO example with Rollout Correction +- `examples/rollout_correction/run_qwen2_5_7b_fsdp.sh` - Basic example +- `examples/rollout_correction/run_qwen2_5_7b_fsdp_multi_rs.sh` - Multi-RS example + +### **Tests** + +- `tests/trainer/ppo/test_rollout_corr.py` - Unit tests for IS/RS mechanisms +- `tests/trainer/ppo/test_rollout_corr_integration.py` - Integration tests + +## Configuration Parameters + +All parameters are under `algorithm.rollout_correction`: + +### `rollout_is` (str or null) + +Importance sampling weights aggregation level: + +- `null` = No IS weights computed (metrics-only mode) +- `"token"`: Per-token IS weights + - **Decoupled mode**: ρ_t = π_old(t)/π_rollout(t) + - **Bypass/Pure IS mode**: ρ_t = π_θ(t)/π_rollout(t) + - Independent truncation per token + - Typical threshold: 1.5 - 5.0 +- `"sequence"`: Per-sequence weight ρ_seq = ∏_t ρ_t + - Multiplicative aggregation across sequence + - Typical threshold: 2.0 - 10.0 + +All IS weights are safety-bounded to [exp(-20), exp(20)] ≈ [2e-9, 5e8] + +### `rollout_is_threshold` (str or float) + +Threshold specification for IS weighting. Default: `2.0` + +- Single float or float-like string: TIS via `.clamp(max=rollout_is_threshold)` +- `"lower_upper"` string such as `"0.5_5.0"`: IcePop, zero weights outside `[lower, upper]` +- Applied to IS weights for variance reduction +- Separate from rejection sampling (controlled by `rollout_rs` parameters) +- Unlike `rollout_rs`, IcePop does not modify `response_mask`; it only changes the IS coefficients + +### `rollout_is_batch_normalize` (bool) + +Apply batch normalization to IS weights. Default: `False` + +- `True`: Normalize IS weights to have mean=1.0 within each batch + - **Token-level IS**: Normalizes over all token weights + - **Sequence-level IS**: Normalizes over sequence means (one weight per sequence) +- `False`: Use raw (truncated) IS weights +- Reduces variance by ensuring average weight is 1.0 per batch +- Applied AFTER truncation to preserve truncation semantics +- Only affects IS weight values, not rejection sampling + +### `rollout_rs` (str or null) + +Rejection sampling aggregation modes. Supply a comma-separated string (spaces optional) using the canonical options implemented in `rollout_corr_helper`: + +- `token_k1`: Token-level rejection with `-log r` bounds (ratio thresholds supplied as `lower_upper`). Example: `"0.6_1.4"` +- `token_k2`: Token-level rejection with `0.5 * (log r)^2` (upper bound only) +- `token_k3`: Token-level rejection with `exp(log r) - 1 - log r` (upper bound only) +- `seq_sum_k1`: Sequence-level rejection with sum of `-log r` (ratio bounds) +- `seq_sum_k2`: Sequence-level rejection with sum of `0.5 * (log r)^2` (upper bound only) +- `seq_sum_k3`: Sequence-level rejection with sum of `exp(log r) - 1 - log r` (upper bound only) +- `seq_mean_k1`: Sequence-level rejection with mean of `-log r` (ratio bounds) +- `seq_mean_k2`: Sequence-level rejection with mean of `0.5 * (log r)^2` (upper bound only) +- `seq_mean_k3`: Sequence-level rejection with mean of `exp(log r) - 1 - log r` (upper bound only) +- `seq_max_k2`: Sequence-level rejection with max of `0.5 * (log r)^2` (upper bound only) +- `seq_max_k3`: Sequence-level rejection with max of `exp(log r) - 1 - log r` (upper bound only) + +### `rollout_rs_threshold` (str, float, or null) + +Threshold specification for rejection sampling. + +- Provide **one entry per option**, separated by commas. A single entry is broadcast to every option. +- **K1 KL modes (`*k1`)**: Use `"lower_upper"` strings (e.g. `"0.7_1.3"`). Supplying a float implies only the upper bound; the lower bound defaults to its reciprocal. +- **K2/K3 KL modes (`*k2`/`*k3`)**: Supply positive upper bounds (float or numeric string). +- Set to `null` to disable thresholds entirely (only valid when `rollout_rs` is null). + +## Understanding the Framework: Components and Combinations + +The rollout correction framework is built from **orthogonal components** that can be combined flexibly. Understanding these components helps you choose the right configuration for your scenario. + +### Key Components + +1. **Operating Mode** (Section: [Operation Modes](#operation-modes)) + + - **Decoupled**: Three policies (π_rollout, π_old, π_θ) with separate π_old computation + - **Bypass**: Two policies (π_rollout = π_old, π_θ), skips π_old computation + +2. **Loss Function** (in bypass mode, controlled by `loss_type`) + + - **PPO-clip** (`loss_type="ppo_clip"`, default): PPO clipped objective (IS handled by ratio) + - **REINFORCE** (`loss_type="reinforce"`): Policy gradient with explicit IS weights (no clipping) + +3. **IS/RS Aggregation Level** + - **Token**: Per-token IS weights/rejection + - **Sequence**: Sequence-level IS weights/rejection + +See [Mathematical Formulations](rollout_corr_math.md#3-algorithmic-components-and-combinations) for detailed theory. + +--- + +## Preset Configuration Guide + +This section provides detailed guidance on choosing and using the verified presets. Each preset is a specific combination of components optimized for common scenarios. + +### Understanding the Presets + +#### Available Preset Methods + +| Preset Method | Estimator | Mode | IS Level | RS Level | Properties | +| ------------------------------------------------------------------------------ | ---------------- | ------------------ | -------- | -------- | --------------------------------------- | +| **Decoupled PPO Mode** (3 policies: π_rollout, π_old, π_θ) | +| `decoupled_token_is()` | Token-TIS | Decoupled | token | - | Token-level IS weights | +| `decoupled_seq_is()` | Seq-TIS | Decoupled | sequence | - | Sequence-level IS weights | +| `decoupled_seq_is_rs()` | Seq-MIS | Decoupled | sequence | sequence | Sequence IS + seq_sum_k1 RS | +| `decoupled_geo_rs()` | Geo-RS | Decoupled | - | sequence | Geometric RS (seq_mean_k1) | +| `decoupled_geo_rs_token_tis()` | Geo-RS-Token-TIS | Decoupled | token | sequence | Geometric RS + token IS | +| **K3 KL Estimator** (more stable for small KL values) | +| `decoupled_k3_rs()` | K3-RS | Decoupled | - | sequence | seq_mean_k3 RS | +| `decoupled_k3_rs_token_tis()` | K3-RS-Token-TIS | Decoupled | token | sequence | seq_mean_k3 RS + token IS | +| **Bypass Mode (PPO-clip)** (2 policies; ratio handles IS, RS masks outliers) | +| `bypass_ppo_clip()` | - | Bypass (PPO-clip) | - | - | PPO-clip only | +| `bypass_ppo_clip_geo_rs()` | Geo-RS | Bypass (PPO-clip) | - | sequence | PPO-clip + Geo-RS | +| `bypass_ppo_clip_k3_rs()` | K3-RS | Bypass (PPO-clip) | - | sequence | PPO-clip + K3-RS | +| **Bypass Mode (REINFORCE)** (2 policies; explicit IS weights, no PPO clipping) | +| `bypass_pg_is()` | Seq-TIS | Bypass (REINFORCE) | sequence | - | REINFORCE with explicit IS | +| `bypass_pg_geo_rs()` | Geo-RS | Bypass (REINFORCE) | - | sequence | REINFORCE with Geo-RS | +| `bypass_pg_geo_rs_token_tis()` | Geo-RS-Token-TIS | Bypass (REINFORCE) | token | sequence | REINFORCE + Geo-RS + token IS | +| **Other** | +| `disabled()` | - | - | - | - | Metrics only, no correction | + +**Note:** + +- **Bypass mode** sets π_old = π_rollout and uses `loss_type` to select the loss function: + - `"ppo_clip"` (default): PPO clipped objective where ratio = π_θ/π_rollout already handles IS + - `"reinforce"`: REINFORCE with explicit IS weights as π_θ/π_rollout +- Both loss types benefit from rejection sampling (RS) which masks out-of-distribution samples. +- All estimators (Token-TIS, Seq-TIS, Seq-MIS, Geo-RS, ...) are compatible with Decoupled and Bypass modes. + +#### Other Supported Combinations (Manual Configuration Required) + +**Other supported combinations without preset methods:** + +- Token IS + Token RS: Token-level IS weights + Token-level RS mask +- Pure token RS: Token-level RS only, no IS weights +- Pure sequence RS: Sequence-level RS only, no IS weights + +See [detailed configuration examples below](#additional-useful-configurations-not-exposed-as-presets) for manual configurations. + +**Key properties:** + +- Any aggregation level (token/sequence) works in either decoupled or bypass mode +- All combinations are fully supported by the implementation +- Rejection sampling is independent of IS weighting +- Pure RS (`bypass_pg_rs`) uses bypass + geometric RS with `loss_type="reinforce"` (no IS weights) + +--- + +### 1. Decoupled Mode with Token-level Importance Sampling (`decoupled_token_is`) + +**Configuration:** + +```python +config = RolloutCorrectionConfig.decoupled_token_is(threshold=2.0) +``` + +**Components:** + +- **Operating Mode**: Decoupled (3 policies) +- **Loss**: PPO with clipping (only for the second drift correction) +- **IS Aggregation**: Token-level +- **RS**: None (can be added separately) + +**Equivalent YAML:** + +```yaml +algorithm: + rollout_correction: + rollout_is: token + rollout_is_threshold: 2.0 + rollout_rs: null + bypass_mode: false # Decoupled mode +``` + +**Properties:** + +- Independent truncation per token +- Lower variance than sequence-level (product of ratios bounded individually) +- Typical threshold: 1.5 - 5.0 + +**Theory:** See [rollout_corr_math.md §3.3.1](rollout_corr_math.md#331-token-level-aggregation) + +--- + +### 2. Decoupled Mode with Sequence-level Importance Sampling (`decoupled_seq_is`) + +**Also known as: Seq-TIS (Sequence-Level Truncated IS)** + +**Configuration:** + +```python +config = RolloutCorrectionConfig.decoupled_seq_is(threshold=2.0) +``` + +**Components:** + +- **Operating Mode**: Decoupled (3 policies) +- **Loss**: PPO with clipping (only for the second drift correction) +- **IS Aggregation**: Sequence-level (Seq-TIS) +- **RS**: None (can be added separately) + +**Equivalent YAML:** + +```yaml +algorithm: + rollout_correction: + rollout_is: sequence + rollout_is_threshold: 2.0 + rollout_rs: null + bypass_mode: false # Decoupled mode +``` + +**Properties:** + +- Multiplicative aggregation across sequence +- More sensitive to outliers than token-level +- Typical threshold: 2.0 - 10.0 (higher than token-level) + +**Theory:** See [rollout_corr_math.md §3.3.2](rollout_corr_math.md#332-sequence-level-aggregation) + +--- + +### 3. Decoupled Mode with Sequence-level IS + Rejection Sampling (`decoupled_seq_is_rs`) + +**Also known as: Seq-MIS (Sequence-Level Masked IS)** + +**Configuration:** + +```python +config = RolloutCorrectionConfig.decoupled_seq_is_rs(is_threshold=2.0, rs_threshold="0.5_2.0") +``` + +**Components:** + +- **Operating Mode**: Decoupled (3 policies) +- **Loss**: PPO with clipping (only for the second drift correction) +- **IS Aggregation**: Sequence-level (Seq-TIS) +- **RS**: Sequence-level rejection (Seq-MIS) + +**Equivalent YAML:** + +```yaml +algorithm: + rollout_correction: + rollout_is: sequence + rollout_is_threshold: 2.0 + rollout_rs: seq_sum_k1 + rollout_rs_threshold: 0.5_2.0 + bypass_mode: false # Decoupled mode +``` + +**Properties:** + +- Double mechanism: IS reweighting (Seq-TIS) + rejection filtering (Seq-MIS) +- Lower effective sample size (rejects outliers) +- For severe off-policy gaps or when the distribution tail is "toxic" (garbage/adversarial samples) + +**When to use Seq-MIS over Seq-TIS:** + +- **Seq-TIS (clipping only)**: Maximizes information efficiency; extracts signal from all samples. Use when data is clean and mismatch is moderate. +- **Seq-MIS (rejection)**: Maximizes safety; acts as a hard trust region filter. Use when mismatch is severe or when high-weight samples are likely garbage rather than signal. + +**Theory:** See [rollout_corr_math.md §3.5](rollout_corr_math.md#35-rejection-sampling-rs) + +--- + +### 6. Bypass Mode with PPO-clip (`bypass_ppo_clip`) + +**Configuration:** + +```python +config = RolloutCorrectionConfig.bypass_ppo_clip() +``` + +**Components:** + +- **Operating Mode**: Bypass (2 policies: π_rollout = π_old, π_θ) +- **Loss**: PPO-clip (IS handled by ratio, no explicit IS weights) +- **IS Aggregation**: None (PPO ratio handles it) +- **RS**: None + +**Equivalent YAML:** + +```yaml +rollout_correction: + rollout_is: null + rollout_rs: null + bypass_mode: true + loss_type: ppo_clip +``` + +**Properties:** + +- PPO clipped objective in bypass mode +- The PPO ratio = π_θ/π_rollout already handles IS (no explicit IS weights needed) +- Skips `actor.compute_log_prob()` forward pass (2 policies instead of 3) +- No rejection sampling - use `bypass_ppo_clip_geo_rs()` for RS + +**Configuration requirement:** + +- Set `actor_rollout_ref.rollout.calculate_log_probs: true` + +**Additional requirements for bypass mode:** + +- Set `actor_rollout_ref.actor.use_rollout_log_probs: true` +- Set `actor_rollout_ref.actor.policy_loss.loss_mode: bypass_mode` +- Set rollout correction config via `actor_rollout_ref.actor.policy_loss.rollout_correction` + +**Theory:** See [rollout_corr_math.md §3.1.2](rollout_corr_math.md#312-bypass-mode-two-policies) + +--- + +### 7. REINFORCE with IS (`bypass_pg_is`) + +**Configuration:** + +```python +config = RolloutCorrectionConfig.bypass_pg_is(threshold=2.0) +``` + +**Components:** + +- **Operating Mode**: Bypass (2 policies: π_rollout, π_θ) +- **Loss**: REINFORCE (policy gradient with explicit IS weights, no PPO clipping) +- **IS Aggregation**: Sequence-level +- **RS**: None + +**Equivalent YAML:** + +```yaml +rollout_correction: + rollout_is: sequence + rollout_is_threshold: 2.0 + rollout_rs: null + bypass_mode: true + loss_type: reinforce # REINFORCE with explicit IS weights +``` + +**Properties:** + +- REINFORCE loss with explicit IS weights (no PPO clipping) +- Single forward pass (skips old_log_prob computation) +- IS weights computed on-the-fly in loss function + +**Theory:** See [rollout_corr_math.md §3.2.2](rollout_corr_math.md#322-policy-gradient-loss-with-isrs-correction) + +--- + +## Additional Useful Configurations (Not Exposed as Presets) + +These configurations are **fully supported** but don't have convenience preset methods yet. + +### 1. Token IS + Token RS (`token_is_rs`) + +Token-level IS weights with token-level RS mask. + +**Python:** + +```python +config = RolloutCorrectionConfig( + rollout_is="token", + rollout_is_threshold=2.0, + rollout_rs="token_k1", + rollout_rs_threshold=2.0, +) +``` + +**Properties:** Per-token IS weights + per-token RS mask. + +### 2. Pure Token RS (`token_rs`) + +Token-level RS only, no IS weights. + +**Python:** + +```python +config = RolloutCorrectionConfig( + rollout_is=None, + rollout_rs="token_k1", + rollout_rs_threshold=2.0, +) +``` + +**Properties:** Token-level RS mask, no IS reweighting. + +### 3. Pure Sequence RS (`seq_rs`) + +Sequence-level RS only, no IS weights. + +**Python:** + +```python +config = RolloutCorrectionConfig( + rollout_is=None, + rollout_rs="seq_sum_k1", + rollout_rs_threshold="0.5_2.0", +) +``` + +**Properties:** Sequence-level RS mask, no IS reweighting. + +--- + +### Summary: How IS Weights are Processed + +IS weights (`rollout_is_weights`) go through a fixed processing pipeline: + +**Stage 1: Safety Bound (Prevent Overflow)** + +- Token level: `exp(clamp(log_ratio, -20, 20))` per token → bounds each token to [2e-9, 5e8] +- Sequence level: `exp(clamp(sum(log_ratio), -20, 20))` → bounds product to [2e-9, 5e8], broadcast to all tokens + +**Stage 2: Truncation (Reduce Variance)** + +- `.clamp(max=rollout_is_threshold)` → caps weights at upper threshold (TIS: Truncated Importance Sampling) +- No lower truncation (preserves unbiasedness for small weights) + +**Stage 3: Padding Zeroing (Correct Aggregation)** + +- `weights * response_mask` → zeros out padding positions + +**Stage 4: Optional Batch Normalization** + +- If `rollout_is_batch_normalize=True`: Normalize weights to mean=1.0 within batch +- Applied after truncation to preserve truncation semantics + +**Rejection Sampling (Separate Mechanism)** + +Rejection sampling modifies `response_mask` (NOT weights) through `compute_rollout_rejection_mask()`: + +- Computes safety-bounded ratios independently +- Creates binary mask: tokens/sequences outside [lower_threshold, upper_threshold] → 0 (rejected) +- Modified mask used for loss aggregation + +## Operation Modes + +The framework provides **two operating modes** for computing π_old, which can be combined with different loss functions. + +### Operating Modes and Configuration + +| Configuration | `bypass_mode` | `loss_type` | Operating Mode | Loss Function | Description | +| ---------------------- | ------------- | ---------------------- | -------------- | ------------- | ----------------------------------------------------------------- | +| **Decoupled** | `false` | N/A | Decoupled | PPO | Computes `old_log_prob` separately via `actor.compute_log_prob()` | +| **Bypass + PPO-clip** | `true` | `"ppo_clip"` (default) | Bypass | PPO-clip | PPO clipped objective (IS handled by ratio) | +| **Bypass + REINFORCE** | `true` | `"reinforce"` | Bypass | REINFORCE | Policy gradient with explicit IS weights (no PPO clipping) | + +### Operating Mode Details + +#### Decoupled Mode (Three Policies) + +**Policy setup:** + +- π_rollout: Behavior policy (data collection) +- π_old: Proximal policy (computed via `actor.compute_log_prob()` at start of training epoch) +- π_θ: Current policy (being updated) + +**Configuration:** `bypass_mode = false` + +**Properties:** + +- ✅ Achieves batch size invariance +- ✅ Separately corrects Drift 1 (rollout→old) and Drift 2 (old→current) +- ✅ Efficient stale data utilization +- ❌ Extra forward pass needed (`actor.compute_log_prob()`) + +**Theory:** See [rollout_corr_math.md §3.1.1](rollout_corr_math.md#311-decoupled-mode-three-policies) + +#### Bypass Mode (Two Policies) + +**Policy setup:** + +- π_rollout: Behavior policy (data collection) +- π_old = π_rollout: Proximal policy equals behavior policy +- π_θ: Current policy (being updated) + +**Configuration:** `bypass_mode = true` + +**Properties:** + +- ✅ Skips `actor.compute_log_prob()` call (faster) +- ✅ Handles off-policy correction via IS/RS (when using policy gradient with IS/RS) +- ✅ Uses two policies instead of three (π_rollout = π_old) +- ⚠️ Does not separate proximal policy from behavior policy (unlike decoupled mode) + +**Theory:** See [rollout_corr_math.md §3.1.2](rollout_corr_math.md#312-bypass-mode-two-policies) + +--- + +### IS/RS Aggregation Levels (Orthogonal to Operating Mode) + +The aggregation level can be chosen **independently** of the operating mode. Any aggregation level works in either decoupled or bypass mode. + +| `rollout_is` | `rollout_rs` | Behavior | +| ------------------------- | ------------------------------------------------------------------ | --------------------------------------------------------------------------------- | +| `null` | `null` | **Disabled**: No computation, no metrics, no rejection | +| `null` | `"token_k1"`, `"seq_sum_k1"`, `"seq_mean_k1"`, `"seq_max_k2"`, etc | **Rejection only**: Compute metrics, NO weight correction, YES rejection sampling | +| `"token"` or `"sequence"` | `null` | **IS weights only**: Weight correction enabled, NO rejection sampling | +| `"token"` or `"sequence"` | `"token_k1"`, `"seq_sum_k1"`, `"seq_mean_k1"`, `"seq_max_k2"`, etc | **Full correction**: Both weight correction and rejection sampling enabled | + +### Key Insights + +- ✅ Any IS/RS aggregation level (token/sequence/geometric) can be used in **either** decoupled or bypass mode +- ✅ You can use **rejection sampling alone** without IS weight correction (`rollout_is=null, rollout_rs="token_k1"`) +- ✅ You can use **IS weights alone** without outlier rejection (`rollout_is="token", rollout_rs=null`) +- ✅ You can use **both together** (`rollout_is="token", rollout_rs="token_k1"`) +- ✅ You can **monitor metrics only** without any correction by setting both to `null` but still providing rollout_log_probs + +**Theory:** See [rollout_corr_math.md §3.3](rollout_corr_math.md#33-isrs-aggregation-levels) for details on aggregation levels. + +### Example Workflow + +**Recommended: Bypass Mode** + +This workflow uses bypass mode for efficiency. + +1. **Start with metrics only** to understand the off-policy gap: + + ```yaml + rollout_correction: + rollout_is: null + rollout_rs: null + bypass_mode: true # Bypass mode (recommended) + loss_type: ppo_clip # Default: PPO clipped objective + ``` + + Monitor `rollout_corr/kl`, `rollout_corr/log_ppl_abs_diff`, `rollout_corr/chi2_token` to assess off-policy gap. + +2. **Enable rejection sampling** if you see high outlier fractions: + + ```yaml + rollout_correction: + rollout_is: null + rollout_rs: sequence # or "geometric" for higher sensitivity + rollout_rs_threshold: 2.0 + bypass_mode: true # Bypass mode + loss_type: ppo_clip # or "reinforce" for explicit IS weights + ``` + + This excludes outliers from training without modifying gradients. + +3. **Enable full IS correction** (with REINFORCE loss) once comfortable with metrics: + ```yaml + rollout_correction: + rollout_is: sequence # Recommended: unbiased, suitable for most cases + rollout_is_threshold: 2.0 + rollout_rs: sequence # or "geometric" for more aggressive filtering + rollout_rs_threshold: 2.0 + bypass_mode: true # Bypass mode + loss_type: reinforce # REINFORCE with explicit IS weights + ``` + +**Benefits of bypass mode:** + +- ✅ Skips expensive `actor.compute_log_prob()` forward pass (faster) +- ✅ `loss_type` controls the loss function: "ppo_clip" (default) or "reinforce" +- ✅ PPO-clip: IS handled by ratio (no explicit weights), RS mask applied +- ✅ REINFORCE: Explicit IS weights computed on-the-fly (π_θ / π_rollout) +- ✅ Both loss types work with all IS/RS combinations + +## Usage + +### Basic Setup + +```yaml +algorithm: + rollout_correction: + rollout_is: token # Enable IS weights at token level + rollout_is_threshold: 2.0 # Threshold for IS weights + rollout_rs: null # No rejection sampling + +actor_rollout_ref: + rollout: + calculate_log_probs: true # Required! +``` + +### Additional Configurations for Bypass Mode + +- Set `actor_rollout_ref.actor.use_rollout_log_probs: true` +- Set `actor_rollout_ref.actor.policy_loss.loss_mode: bypass_mode` +- Set rollout correction config via `actor_rollout_ref.actor.policy_loss.rollout_correction` + +### Metrics + +All metrics are prefixed with `rollout_corr/` in logs. For example, `rollout_is_mean` appears as `rollout_corr/rollout_is_mean`. + +These metrics cover both: + +- **Diagnostic metrics**: KL divergence, perplexity differences (measuring off-policy gap) +- **Correction statistics**: IS weights, rejection rates (measuring correction applied) + +#### **Core IS Weight Metrics** + +- **`rollout_is_mean`**: Mean importance sampling weight across all valid tokens + + - Value close to 1.0 indicates minimal off-policy gap + +- **`rollout_is_std`**: Standard deviation of IS weights + + - Higher values indicate greater variance in IS weights + +- **`rollout_is_min`**: Minimum IS weight observed + + - Shows the most underweighted token/sequence + - For sequence/geometric: computed from unclamped log-space ratios (true minimum) + - For token: computed from safety-bounded weights + +- **`rollout_is_max`**: Maximum IS weight observed + - Shows the most overweighted token/sequence + - For sequence/geometric: computed from unclamped log-space ratios (true maximum before safety bound) + - For token: computed from safety-bounded weights (before threshold clamping) + - Compare with `rollout_is_threshold` to see truncation impact + +#### **Effective Sample Size** + +- **`rollout_is_eff_sample_size`**: Effective sample size after IS weighting + - **Formula**: `1 / mean(weights²)` where weights are normalized + - **Range**: 0.0 to 1.0 (as fraction of original batch) + - Lower values indicate weight concentration on fewer samples + +#### **Threshold Exceedance Metrics** + +- **`rollout_is_ratio_fraction_high`**: Fraction of weights exceeding upper threshold + + - Shows how often truncation/masking occurs on high end + - For sequence/geometric: computed from unclamped log-space ratios (true exceedance) + - For token: computed from safety-bounded weights (before threshold clamping) + +- **`rollout_is_ratio_fraction_low`**: Fraction of weights below lower threshold (1/upper_threshold) + - Diagnostic metric showing how many weights are below the reciprocal threshold + - For sequence/geometric: computed from unclamped log-space ratios (true exceedance) + - For token: computed from safety-bounded weights (before truncation) + +#### **Sequence-Level Metrics** (for sequence aggregation) + +- **`rollout_is_seq_mean`**: Mean IS weight at sequence level + + - Should match `rollout_is_mean` for sequence-level aggregation + +- **`rollout_is_seq_std`**: Standard deviation of sequence-level IS weights + +- **`rollout_is_seq_min`**: Minimum sequence-level IS weight + +- **`rollout_is_seq_max`**: Maximum sequence-level IS weight + +- **`rollout_is_seq_max_deviation`**: Maximum absolute deviation from 1.0 at sequence level + + - Shows worst-case sequence off-policy gap + +- **`rollout_is_seq_fraction_high`**: Fraction of sequences exceeding upper threshold + +- **`rollout_is_seq_fraction_low`**: Fraction of sequences below lower threshold + +#### **Rejection Sampling Metrics** (when `rollout_rs` is enabled) + +- **`rollout_rs_masked_fraction`**: Fraction of tokens rejected via rejection sampling + + - **Important**: Rejection sampling modifies `response_mask` (sets rejected tokens to 0) + - **Separate from IS weights**: IS weights are still truncated; rejection is an independent filtering step + - Only present when `rollout_rs` is enabled (token/sequence/geometric) + +- **`rollout_rs_seq_masked_fraction`**: Fraction of sequences with at least one rejected token + - Shows sequence-level impact of rejection sampling + - Token-level RS: sequence rejected if ANY token is outside [lower, upper] + - Sequence-level RS: entire sequence rejected or accepted based on sequence-level ratio + - Geometric RS: entire sequence rejected or accepted based on geometric mean + +#### **Off-Policy Diagnostic Metrics** (Training vs Rollout Policy) + +**Note on terminology:** These metrics use "training" to refer to the training reference policy and "rollout" to refer to π_rollout (the behavior policy used for data collection). + +- **Decoupled mode**: "training" = π_old (computed at start of training epoch) +- **Bypass/Pure IS mode**: "training" = π_θ (current policy being trained) + +In bypass/pure IS mode, metrics measure the drift between π_θ and π_rollout directly. + +- **`training_ppl`**: Perplexity of training reference policy (π_old in decoupled mode, π_θ in bypass/pure IS mode) + + - **Formula**: `exp(-mean(log_probs))` + - Lower values indicate higher model confidence + +- **`rollout_ppl`**: Perplexity of rollout policy π_rollout (e.g., vLLM BF16) + +- **`ppl_ratio`**: Ratio of training PPL to rollout PPL + + - **Formula**: `exp(mean(log(training_ppl / rollout_ppl)))` + - **Meaning**: > 1.0 means training is less confident than rollout + +- **`training_log_ppl`**: Log perplexity of training policy + + - Useful for identifying trends (linear scale) + +- **`rollout_log_ppl`**: Log perplexity of rollout policy + +- **`log_ppl_diff`**: Mean difference in log perplexities + + - **Formula**: `mean(log_ppl_rollout - log_ppl_training)` + - Sign indicates which policy is more confident + +- **`log_ppl_abs_diff`**: Mean absolute log perplexity difference + + - Magnitude of off-policy gap regardless of direction + +- **`log_ppl_diff_max`**: Maximum log perplexity difference across sequences + + - Identifies worst-case sequence + +- **`log_ppl_diff_min`**: Minimum log perplexity difference across sequences + +- **`kl`**: KL divergence KL(π_rollout || π_training) + + - **Formula**: `mean(log_prob_rollout - log_prob_training)` + - **Note**: Can be negative (rollout is less confident) + +- **`k3_kl`**: K3 divergence (equals KL(π_rollout || π_training) in expectation) + + - **Formula**: `mean(exp(log_ratio) - log_ratio - 1)` + - More stable than direct KL (non-negative per token) + - Always >= 0 + +- **`chi2_token`**: Chi-squared divergence at token level + + - **Formula**: `mean(ratio²) - 1` where ratio = π_training/π_rollout + - Measures second moment of IS weight distribution + - Always non-negative + +- **`chi2_seq`**: Chi-squared divergence at sequence level + - **Formula**: `mean((∏_t ratio_t)²) - 1` + - Sequence-level second moment of IS weights + - More sensitive than token-level chi-squared + +#### **Example: Accessing Metrics in Code** + +```python +# Metrics are returned from compute_rollout_correction_and_rejection_mask +from verl.trainer.ppo.rollout_corr_helper import compute_rollout_correction_and_rejection_mask + +# Returns 3 values (weights, modified_response_mask, metrics) +weights_proto, modified_response_mask, metrics = compute_rollout_correction_and_rejection_mask( + old_log_prob=training_log_probs, # from training policy + rollout_log_prob=rollout_log_probs, # from rollout policy + response_mask=response_mask, + rollout_is="token", # Enable IS weights at token level + rollout_is_threshold=2.0, + rollout_rs="token_k1", + rollout_rs_threshold="0.5_2.0", +) + +# Extract IS weights (processed, zeroed at padding) +is_weights = weights_proto.batch["rollout_is_weights"] + +# IS weights processing (with IS enabled at token level): +# 1. Safety-bounded: exp(clamp(log_ratio, -20, 20)) per token +# 2. Truncated: .clamp(max=2.0) to cap extreme weights +# 3. Zeroed at padding positions +# Note: Truncation is ALWAYS applied to IS weights (TIS: Truncated Importance Sampling) + +# modified_response_mask has rejection applied (since rollout_rs="token_k1"): +# 1. RS rejection: tokens outside [0.5, 2.0] masked to 0 via response_mask +# Note: RS and IS are separate mechanisms - both can be enabled independently + +# All metrics have 'rollout_corr/' prefix +print(f"Mean IS weight: {metrics['rollout_corr/rollout_is_mean']:.3f}") +print(f"Effective sample size: {metrics['rollout_corr/rollout_is_eff_sample_size']:.3f}") +print(f"RS masked fraction: {metrics['rollout_corr/rollout_rs_masked_fraction']:.3f}") +print(f"KL divergence: {metrics['rollout_corr/kl']:.3f}") + +# Check IS weights for valid tokens (non-padding) +valid_weights = is_weights[response_mask.bool()] +print(f"\n✓ IS weights min (valid tokens): {valid_weights.min():.4f}") +print(f"✓ IS weights max (valid tokens): {valid_weights.max():.4f}") +print(f"✓ All valid IS weights > 0: {(valid_weights > 0).all()}") +print(f"✓ IS weights are capped at threshold: {(valid_weights <= 2.0).all()}") + +# Check rejection via response_mask +rejected_tokens = (response_mask == 1) & (modified_response_mask == 0) +print(f"\n✓ Rejected {rejected_tokens.sum()} tokens via response_mask") +print(f"✓ Rejection sampling modifies response_mask (separate from IS weight truncation)") +print(f"✓ IS weights are always truncated to [0, threshold] after safety bounding") + +# Check for warning conditions +if metrics['rollout_corr/rollout_is_mean'] < 0.5 or metrics['rollout_corr/rollout_is_mean'] > 2.0: + print("⚠️ Warning: Mean IS weight far from 1.0, significant off-policy gap detected") + +if metrics['rollout_corr/rollout_is_eff_sample_size'] < 0.3: + print("⚠️ Warning: Low effective sample size, high weight concentration") +``` + +#### **Example: Monitoring Metrics During Training** + +```python +# In your training loop +for epoch in range(num_epochs): + for batch_idx, batch in enumerate(dataloader): + # ... rollout phase ... + + # Compute IS weights and get metrics + rollout_corr_config = config.algorithm.get("rollout_correction", None) + if rollout_corr_config is not None: + weights_proto, modified_response_mask, metrics = compute_rollout_correction_and_rejection_mask( + old_log_prob=batch.old_log_prob, + rollout_log_prob=batch.rollout_log_prob, + response_mask=batch.response_mask, + rollout_is=rollout_corr_config.get("rollout_is", None), + rollout_is_threshold=rollout_corr_config.get("rollout_is_threshold", 2.0), + rollout_rs=rollout_corr_config.get("rollout_rs", None), + rollout_rs_threshold=rollout_corr_config.get("rollout_rs_threshold", None), + ) + + # Log to tensorboard/wandb + for metric_name, metric_value in metrics.items(): + logger.log_scalar(metric_name, metric_value, step=global_step) + + # IMPORTANT: Update batch response_mask with rejection applied + batch.response_mask = modified_response_mask + + # Use IS weights in training (always safety-bounded, zeroed at padding) + is_weights = weights_proto.batch["rollout_is_weights"] + # ... apply weights to policy gradient ... +``` + +#### **Example: Conditional Alerting Based on Metrics** + +```python +def check_rollout_correction_health(metrics, config): + """Check if Rollout Correction metrics indicate healthy training.""" + warnings = [] + + # Check mean IS weight + mean_weight = metrics['rollout_corr/rollout_is_mean'] + if mean_weight < 0.5 or mean_weight > 2.0: + warnings.append(f"Mean IS weight {mean_weight:.3f} is far from 1.0") + + # Check effective sample size + ess = metrics['rollout_corr/rollout_is_eff_sample_size'] + if ess < 0.3: + warnings.append(f"Effective sample size {ess:.3f} is too low") + + # Check standard deviation + std = metrics['rollout_corr/rollout_is_std'] + if std > 1.0: + warnings.append(f"IS weight std {std:.3f} is too high") + + # Check KL divergence + kl = metrics['rollout_corr/kl'] + if abs(kl) > 0.1: + warnings.append(f"KL divergence {kl:.3f} indicates significant off-policy gap") + + # Check chi-squared divergence + if 'rollout_corr/chi2_token' in metrics: + chi2_token = metrics['rollout_corr/chi2_token'] + if chi2_token > 1.0: + warnings.append(f"Chi-squared divergence (token) {chi2_token:.3f} indicates severe distribution shift") + + if warnings: + print("⚠️ Rollout Correction Health Warnings:") + for warning in warnings: + print(f" - {warning}") + return False + else: + print("✅ Rollout Correction metrics look healthy") + return True + +# Use in training +_, _, metrics = compute_rollout_correction_and_rejection_mask(...) +is_healthy = check_rollout_correction_health(metrics, config) + +if not is_healthy: + # Consider adjusting config or investigating issues + print("Consider:") + print(" - Tightening rollout_is_threshold") + print(" - Switching to geometric aggregation level") + print(" - Checking if rollout and training policies are too different") +``` + +### Running Examples + +Start with the basic token-level truncate configuration: + +```bash +bash examples/rollout_correction/run_qwen2_5_7b_fsdp.sh +``` + +Monitor metrics for 1-2 epochs before adjusting parameters. + +## Configuration Examples + +### Example 1: IS Weights Only (Token Level) + +```yaml +algorithm: + rollout_correction: + rollout_is: token + rollout_is_threshold: 2.0 + rollout_rs: null # No rejection sampling +``` + +### Example 2: Rejection Sampling Only (No IS Weights) + +```yaml +algorithm: + rollout_correction: + rollout_is: null # No IS weights + rollout_rs: token_k1 + rollout_rs_threshold: "0.5_2.0" +``` + +### Example 3: Both IS and RS (Token RS) + +```yaml +algorithm: + rollout_correction: + rollout_is: token + rollout_is_threshold: 2.0 + rollout_rs: token_k1 + rollout_rs_threshold: "0.5_2.0" +``` + +### Example 5: Bypass Mode with PPO-clip (Default) + +```yaml +algorithm: + rollout_correction: + rollout_is: token + rollout_is_threshold: 2.0 + rollout_rs: token_k1 + rollout_rs_threshold: "0.5_2.0" + bypass_mode: true # Skip old_log_prob computation + loss_type: ppo_clip # PPO clipped objective (default) +``` + +**Skips expensive `actor.compute_log_prob()` forward pass. PPO ratio = π_θ/π_rollout handles IS.** + +### Example 6: Bypass Mode with REINFORCE + +```yaml +rollout_correction: + rollout_is: sequence # Explicit IS correction in loss + rollout_is_threshold: 2.0 + rollout_rs: null # Optional: can add rejection sampling + bypass_mode: true + loss_type: reinforce # REINFORCE with explicit IS weights +``` + +**No PPO clipping, pure policy gradient with IS correction** + +### Example 7: Bypass Mode with PPO-clip + Rejection Sampling + +```yaml +rollout_correction: + rollout_is: sequence # Computed for metrics + rollout_is_threshold: 2.0 + rollout_rs: seq_max_k2 # Sequence max χ²/2 guard + rollout_rs_threshold: 2.5 + bypass_mode: true + loss_type: ppo_clip # PPO clipped objective (IS handled by ratio) +``` + +**PPO clipping with rejection sampling. IS handled by PPO ratio (no explicit IS weights).** + +## Troubleshooting + +### Issue: High spread in IS weights + +**Symptoms:** `rollout_is_std` > 1.0, `rollout_is_eff_sample_size` < 0.3 + +**Solutions:** + +1. Switch from `sequence` to `geometric` level +2. Tighten thresholds +3. Verify rollout and training aren't too different + +### Issue: Mean IS weight far from 1.0 + +**Symptoms:** `rollout_is_mean` < 0.5 or > 2.0 + +**Solutions:** + +1. Verify `calculate_log_probs=True` is set +2. Check rollout_log_probs are correctly passed +3. Check for systematic distribution shift + +### Debugging: Visualizing Metrics + +**Example: Plot IS weight distribution** + +```python +import matplotlib.pyplot as plt +import numpy as np + +def plot_is_metrics(metrics_history): + """Plot rollout IS metrics over training steps.""" + fig, axes = plt.subplots(2, 3, figsize=(15, 10)) + + # Plot 1: Mean IS weight over time + axes[0, 0].plot(metrics_history['rollout_corr/rollout_is_mean']) + axes[0, 0].axhline(y=1.0, color='r', linestyle='--', label='Ideal') + axes[0, 0].set_title('Mean IS Weight') + axes[0, 0].set_xlabel('Step') + axes[0, 0].legend() + + # Plot 2: Effective sample size + axes[0, 1].plot(metrics_history['rollout_corr/rollout_is_eff_sample_size']) + axes[0, 1].axhline(y=0.5, color='g', linestyle='--', label='Good') + axes[0, 1].axhline(y=0.3, color='r', linestyle='--', label='Warning') + axes[0, 1].set_title('Effective Sample Size') + axes[0, 1].set_xlabel('Step') + axes[0, 1].legend() + + # Plot 3: KL divergence over time + axes[1, 0].plot(metrics_history['rollout_corr/kl'], label='KL') + axes[1, 0].plot(metrics_history['rollout_corr/k3_kl'], label='K3 KL') + axes[1, 0].axhline(y=0, color='g', linestyle='--', alpha=0.3) + axes[1, 0].set_title('KL Divergence') + axes[1, 0].set_xlabel('Step') + axes[1, 0].legend() + + # Plot 4: PPL ratio over time + axes[1, 1].plot(metrics_history['rollout_corr/ppl_ratio']) + axes[1, 1].axhline(y=1.0, color='r', linestyle='--', label='Ideal') + axes[1, 1].set_title('PPL Ratio (Training/Rollout)') + axes[1, 1].set_xlabel('Step') + axes[1, 1].legend() + + # Plot 5: Chi-squared divergence + if 'rollout_corr/chi2_token' in metrics_history: + axes[1, 2].plot(metrics_history['rollout_corr/chi2_token'], label='Token-level') + if 'rollout_corr/chi2_seq' in metrics_history: + axes[1, 2].plot(metrics_history['rollout_corr/chi2_seq'], label='Seq-level') + axes[1, 2].axhline(y=1.0, color='r', linestyle='--', label='Warning') + axes[1, 2].set_title('Chi-squared Divergence') + axes[1, 2].set_xlabel('Step') + axes[1, 2].legend() + else: + axes[1, 2].axis('off') + + plt.tight_layout() + plt.savefig('rollout_is_metrics.png', dpi=150) + print("Saved plot to rollout_is_metrics.png") +``` + +**Example: Metric collection during training** + +```python +# Collect metrics over time +metrics_history = { + 'rollout_corr/rollout_is_mean': [], + 'rollout_corr/rollout_is_eff_sample_size': [], + 'rollout_corr/kl': [], + 'rollout_corr/k3_kl': [], + 'rollout_corr/ppl_ratio': [], + 'rollout_corr/chi2_token': [], + 'rollout_corr/chi2_seq': [], +} + +# In training loop +for step in range(num_steps): + # ... compute IS weights and rejection mask ... + _, _, metrics = compute_rollout_correction_and_rejection_mask(...) + + # Store metrics + for key in metrics_history.keys(): + if key in metrics: + metrics_history[key].append(metrics[key]) + + # Plot every 100 steps + if step % 100 == 0: + plot_is_metrics(metrics_history) +``` + +## Performance Impact + +- **Memory overhead**: ~1% of model memory +- **Computational overhead**: 1-3% depending on level +- **Training stability**: Significantly improved when off-policy gap exists + +## Testing + +Run the test suite to verify everything works: + +```bash +# Basic unit tests +python tests/trainer/ppo/test_rollout_corr.py + +# Integration tests (if pytest is available) +pytest tests/trainer/ppo/test_rollout_corr_integration.py -v +``` + +Expected output: All tests pass ✓ + +## Additional Resources + +- **Implementation**: `verl/trainer/ppo/rollout_corr_helper.py` +- **Examples**: `examples/rollout_correction/` +- **DAPO Example**: `recipe/dapo/run_dapo_qwen2.5_32b_rollout_corr.sh` + +## Summary + +Rollout Correction provides a unified framework for handling general off-policy problems in RL: + +- ✅ Corrects ANY distribution shift between data collection and training +- ✅ Supports diverse scenarios: policy mismatch, staleness, replay buffers, off-policy algorithms +- ✅ Numerical stability with safety bounds and rejection mechanisms +- ✅ Comprehensive diagnostics: KL, perplexity, χ² divergence +- ✅ Flexible methods from token-level to sequence-level aggregation +- ✅ Memory-efficient implementation + +## References + +- **[Mathematical Formulations](rollout_corr_math.md)** - Detailed mathematical theory and derivations for all rollout correction methods +- [Your Efficient RL Framework Secretly Brings You Off-Policy RL Training](https://fengyao.notion.site/off-policy-rl) diff --git a/verl_0720_main/verl/docs/algo/rollout_corr_math.md b/verl_0720_main/verl/docs/algo/rollout_corr_math.md new file mode 100644 index 0000000000000000000000000000000000000000..7118c1ddb4987d4211659af14ca5f6567bce08fe --- /dev/null +++ b/verl_0720_main/verl/docs/algo/rollout_corr_math.md @@ -0,0 +1,961 @@ +# Mathematical Formulations of Rollout Correction Methods in `verl` + +**Author:** [Yingru Li](https://richardli.xyz) +**Last updated:** 2025-11-04 + +--- + +> **📖 Documentation Structure** +> - **This document** - Mathematical theory: formulations, derivations, and algorithmic foundations +> - **[Rollout Correction Usage Guide](rollout_corr.md)** - Practical implementation: configurations, presets, troubleshooting +> +> Start here for theory and design rationale, refer to the usage guide for implementation. + +--- + +### BibTeX Citation + +```bibtex +@online{liu-li-2025-rl-collapse, + title = {When Speed Kills Stability: Demystifying {RL} Collapse from the Training-Inference Mismatch}, + author = {Liu, Jiacai and Li, Yingru and Fu, Yuqian and Wang, Jiawei and Liu, Qian and Shen, Yu}, + year = {2025}, + month = sep, + url = {https://richardli.xyz/rl-collapse} +} + + +@article{li2025trust, + title={Trust Region Masking for Long-Horizon LLM Reinforcement Learning}, + author={Li, Yingru and Liu, Jiacai and Xu, Jiawei and Tong, Yuxuan and Li, Ziniu and Liu, Qian and Wang, Baoxiang}, + journal={arXiv preprint arXiv:2512.23075}, + year={2025} +} +``` + +### Blog Series + +- Main blog post: https://richardli.xyz/rl-collapse +- [Part 1: Why Mismatch Breaks LLM-RL](https://richardli.xyz/rl-collapse-1) (analytical framework using TV distance for bias and χ²-divergence for variance) +- [Part 2: The Gradient Estimator Trials](https://richardli.xyz/rl-collapse-2) (token-level vs sequence-level correction bias-variance tradeoff) +- [Part 3: When Math Meets Reality—Toxic Tails and Length Traps](https://richardli.xyz/rl-collapse-3) (why rejection over clipping, and geometric-level RS) +- Latest Paper: https://arxiv.org/abs/2512.23075 + +## Abstract + +This document provides the definitive mathematical formulations for rollout correction methods in `verl`, following the natural progression from **REINFORCE** to **PPO** to **Decoupled PPO**. + +Rollout correction provides a unified framework to handle **general off-policy problems** in RL training - any scenario where the data collection distribution differs from the training distribution. + +**Applicable scenarios include:** +- **Policy mismatch**: Different precision (FP8 vs FP16 vs BF16 vs FP32), different backends (vLLM vs SGLang vs FSDP vs Megatron) +- **Temporal lag**: Model staleness, asynchronous rollout workers +- **Replay buffers**: Training on historical trajectories from earlier policy versions +- **Off-policy algorithms**: Behavioral cloning, DAPO, expert demonstrations +- **Data filtering**: Reweighting, preference learning, curriculum learning + +--- + +## Table of Contents + +1. [Theoretical Foundation: From REINFORCE to Decoupled PPO](#1-theoretical-foundation-from-reinforce-to-decoupled-ppo) +2. [Implementation in verl: The Three-Policy Framework](#2-implementation-in-verl-the-three-policy-framework) +3. [Algorithmic Components and Combinations](#3-algorithmic-components-and-combinations) +4. [Off-Policy Diagnostic Metrics](#4-off-policy-diagnostic-metrics) +5. [Summary and Decision Guide](#5-summary-and-decision-guide) +6. [Implementation References](#6-implementation-references) + +--- + +## 1. Theoretical Foundation: From REINFORCE to Decoupled PPO + +This section establishes the theoretical progression that `verl` implements. + +### 1.1 REINFORCE: Policy Gradient Baseline + +The REINFORCE algorithm ([Williams, 1992](https://doi.org/10.1007/BF00992696)) is the foundation of policy gradient methods. + +**Vanilla REINFORCE (On-Policy)** + +For trajectories $\tau = (s_0, a_0, s_1, a_1, \ldots, s_T, a_T)$ sampled from the current policy $\pi_\theta$, the policy gradient is: + +$$ +\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta} \left[ \sum_{t=0}^T \nabla_\theta \log \pi_\theta(a_t|s_t) \cdot A_t \right] +$$ + +where $A_t$ is the advantage function at timestep $t$. + +**Off-Policy REINFORCE** + +When trajectories are sampled from a different behavior policy $\mu$, we apply importance sampling over the **joint trajectory distribution**: + +$$ +\nabla_\theta J(\theta) = \mathbb{E}_{\tau \sim \mu} \left[ \frac{P_{\pi_\theta}(\tau)}{P_\mu(\tau)} \sum_{t=0}^T \nabla_\theta \log \pi_\theta(a_t|s_t) \cdot A_t \right] +$$ + +where the trajectory-level importance weight is: + +$$ +\frac{P_{\pi_\theta}(\tau)}{P_\mu(\tau)} = \frac{p(s_0) \prod_{t=0}^T \pi_\theta(a_t|s_t) p(s_{t+1}|s_t, a_t)}{p(s_0) \prod_{t=0}^T \mu(a_t|s_t) p(s_{t+1}|s_t, a_t)} = \prod_{t=0}^T \frac{\pi_\theta(a_t|s_t)}{\mu(a_t|s_t)} +$$ + +The transition dynamics $p(s_{t+1}|s_t, a_t)$ and initial state $p(s_0)$ cancel out, leaving only the product of per-step action probability ratios. + +**Key properties:** +- **Off-policy capable**: Can learn from any behavior policy via importance sampling +- **No trust region**: Policy updates not constrained + +**Implementation in verl:** The `bypass_pg_is` preset implements off-policy REINFORCE with truncated importance sampling. + +### 1.2 PPO: Adding Trust Region Control + +Proximal Policy Optimization ([Schulman et al., 2017](https://arxiv.org/abs/1707.06347)) adds a clipped surrogate objective: + +$$ +L_{\text{PPO}}(\theta) = -\mathbb{E}_{(s,a) \sim \mu} \left[ \min\left( r_t(\theta) A_t, \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) A_t \right) \right] +$$ + +where $r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\mu(a_t|s_t)}$ and $\epsilon$ is the clip range (typically 0.2). + +**Key properties:** +- **Two policies**: $\mu$ (reference for clipping) and $\pi_\theta$ (being updated) +- **Trust region via clipping**: Limits policy update magnitude via ratio $r_t(\theta) = \frac{\pi_\theta}{\mu}$ + +### 1.3 Decoupled PPO: Achieving Batch Size Invariance + +Decoupled PPO ([Hilton et al., 2021](https://arxiv.org/abs/2110.00641)) solves PPO's batch size sensitivity by **decoupling two roles**: +1. **Proximal policy** $\pi_{\text{prox}}$: The anchor policy for PPO clipping (controls policy update size) +2. **Behavior policy** $\mu$: The policy that collected the data (for off-policy correction via importance sampling) + +**The problem**: Standard PPO controls policy update size via the ratio $\frac{\pi_\theta}{\pi_{\text{old}}}$, where $\pi_{\text{old}}$ is assumed to be both the proximal policy *and* the behavior policy. This coupling makes the algorithm sensitive to batch size because aggregating data from multiple workers or using replay buffers changes the effective behavior policy. + +**The solution**: Decouple these two roles, leading to a **three-policy formulation**: + +$$ +L_{\text{DecoupledPPO}}(\theta) = -\mathbb{E}_{(s,a) \sim \mu} \left[ w_t \cdot \min\left( r_t(\theta) A_t, \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) A_t \right) \right] +$$ + +where: +- $w_t = \frac{\pi_{\text{prox}}(a_t|s_t)}{\mu(a_t|s_t)}$: Importance sampling weight (corrects for behavior policy $\mu$). Here $\pi_{\text{prox}}$ is frozen during training, so $w_t$ is constant (no stopgrad operator needed). +- $r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\pi_{\text{prox}}(a_t|s_t)}$: PPO ratio (controls policy update size against proximal policy $\pi_{\text{prox}}$) + +**Key properties**: By decoupling: +- **Batch size invariance**: Policy update control (via $\pi_{\text{prox}}$) is independent of data aggregation +- **Flexible behavior policy**: Any $\mu$ can be used (different workers, replay buffers, or stale checkpoints) +- **Stale data utilization**: Older trajectories can be corrected via importance sampling +- **Clipping preserved**: Clipping against $\pi_{\text{prox}}$ limits update magnitude + +**This is the algorithm that `verl` implements via its three-policy framework.** + +--- + +## 2. Implementation in verl: The Three-Policy Framework + +The `verl` library implements decoupled PPO using three distinct policies, each serving a specific role. + +### 2.1 Policy Roles and Notation + +**$\pi_{\text{rollout}}$ (Behavior Policy $\mu$)** +The policy used for data collection. This is the behavior distribution $\mu$ from theory. + +- **When created**: During rollout/data collection phase +- **Purpose**: Generate trajectories for training +- **Common sources**: + - Policy mismatch: Same weights, different implementation (precision, backend) + - Temporal lag: Stale checkpoint from async workers + - Replay buffer: Historical data from earlier iterations + - Off-policy algorithms: Expert demonstrations, auxiliary policies (DAPO) + - Data filtering: Reweighted or filtered data +- **Fixed**: Frozen during training on a batch + +**$\pi_{\text{old}}$ (Proximal Policy $\pi_{\text{prox}}$)** +The reference policy for PPO clipping. This is the "proximal policy" from decoupled PPO theory. + +- **When created**: + - **Decoupled mode**: Computed at start of training epoch via `actor.compute_log_prob()` + - **Bypass mode**: Set equal to $\pi_{\text{rollout}}$ (skips separate computation) +- **Purpose**: + - Anchor point for PPO clipping (controls policy update size) + - When separate from $\pi_{\text{rollout}}$: Enables batch size invariance and efficient use of stale data +- **Fixed**: Frozen during all PPO update epochs on the same batch + +**$\pi_{\theta}$ (Current Policy)** +The policy being actively optimized during training. + +- **Updated**: Every gradient step +- **Purpose**: The policy we're improving + +### 2.2 Operating Modes + +The three-policy framework can operate in two modes: + +**Decoupled Mode (Three Policies)** +- Computes $\pi_{\text{old}}$ separately at the start of each training epoch +- **Algorithm**: Full decoupled PPO with three policies (mathematically correct) +- **Properties**: Achieves batch size invariance; separately corrects Drift 1 (rollout→old) and Drift 2 (old→current) + +**Bypass Mode (Two Policies)** +- Sets $\pi_{\text{old}} = \pi_{\text{rollout}}$ (skips separate computation) +- **Algorithm**: Uses $\pi_{\text{rollout}}$ as both behavior policy and proximal policy (mathematically correct) +- **Key difference**: Proximal policy equals behavior policy, so no IS correction needed between them +- **Properties**: Faster (skips `actor.compute_log_prob()` call); does not achieve batch size invariance + +### 2.3 Two Distribution Shifts + +The three-policy framework handles two types of distribution drift: + +**Drift 1: $\pi_{\text{rollout}} \to \pi_{\text{old}}$ (Off-Policy Gap)** + +This is the distribution shift between the data collection policy and the training reference policy. + +- **Nature**: Ranges from negligible (same checkpoint, minor differences) to severe (replay buffers, expert data) +- **Correction**: Importance sampling weight $w_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ +- **Optional**: Can be ignored (bypass mode) when negligible + +**Drift 2: $\pi_{\text{old}} \to \pi_{\theta}$ (Policy Update Drift)** + +This is the drift from policy parameter updates during training. + +- **Nature**: Occurs as $\pi_\theta$ is updated via gradient descent +- **Correction**: PPO clipping on ratio $r_t(\theta) = \frac{\pi_\theta(a_t|s_t)}{\pi_{\text{old}}(a_t|s_t)}$ +- **Universal**: Applies to both on-policy and off-policy training + +### 2.4 Notation Summary + +- $\pi_{\text{rollout}}$: Behavior policy (data collection) +- $\pi_{\text{old}}$: Proximal policy (PPO anchor) +- $\pi_{\theta}$: Current policy (being updated) +- $\rho_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$: Per-token IS ratio (corrects Drift 1) +- $r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{old}}(a_t|s_t)}$: PPO ratio (corrects Drift 2) +- $A_t$: Advantage at token $t$ +- $T$: Set of valid tokens in a sequence +- $C_{\text{IS}}$: Upper threshold for IS weights (e.g., 2.0) +- $C_{\text{RS-upper}}$: Upper threshold for RS mask (e.g., 2.0) +- $C_{\text{RS-lower}}$: Lower threshold for RS mask (typically $1/C_{\text{RS-upper}}$) +- $\epsilon$: PPO clip range (typically 0.2) + +--- + +## 3. Algorithmic Components and Combinations + +The rollout correction framework in `verl` is built from **orthogonal components** that can be combined flexibly: + +1. **Operating Mode**: How $\pi_{\text{old}}$ is computed (Decoupled vs Bypass) +2. **Loss Function**: PPO (with clipping) vs Pure IS (policy gradient only) +3. **IS/RS Aggregation Level**: Token, Sequence, or Geometric + +This section explains each component and their valid combinations. + +### 3.1 Operating Modes: Decoupled vs Bypass + +The operating mode determines how the proximal policy $\pi_{\text{old}}$ is computed. + +#### 3.1.1 Decoupled Mode (Three Policies) + +**Configuration:** `bypass_mode = false` + +**Policy setup:** +- $\pi_{\text{rollout}}$: Behavior policy (data collection) +- $\pi_{\text{old}}$: Proximal policy (computed via `actor.compute_log_prob()` at start of training epoch) +- $\pi_{\theta}$: Current policy (being updated) + +**IS ratio:** $\rho_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ (corrects Drift 1: rollout→old) + +**PPO ratio:** $r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{old}}(a_t|s_t)}$ (corrects Drift 2: old→current) + +**Properties:** +- ✅ Achieves batch size invariance +- ✅ Separately corrects two distribution drifts +- ✅ Efficient stale data utilization +- ❌ Extra forward pass needed (`actor.compute_log_prob()`) + +#### 3.1.2 Bypass Mode (Two Policies) + +**Configuration:** `bypass_mode = true` + +**Policy setup:** +- $\pi_{\text{rollout}}$: Behavior policy (data collection) +- $\pi_{\text{old}} = \pi_{\text{rollout}}$: Proximal policy equals behavior policy +- $\pi_{\theta}$: Current policy (being updated) + +**Ratios:** +- **With PPO-clip loss** (`loss_type = "ppo_clip"`, default): PPO ratio $r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ clips against rollout policy (IS handled by ratio) +- **With REINFORCE loss** (`loss_type = "reinforce"`): IS ratio $\rho_t = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ computed on-the-fly in loss function + +**Properties:** +- ✅ Skips `actor.compute_log_prob()` call (faster) +- ✅ Handles off-policy correction via IS/RS (when using policy gradient with IS/RS) +- ✅ Uses two policies instead of three (π_rollout = π_old) +- ⚠️ Does not separate proximal policy from behavior policy (unlike decoupled mode) + +--- + +### 3.2 Loss Functions: PPO vs Policy Gradient + +#### 3.2.1 PPO Loss (with Clipping) + +**Configuration:** `loss_type = "ppo_clip"` (default in bypass mode) + +**Loss function:** + +$$ +L_{\text{PPO}}(\theta) = -\mathbb{E}_t \left[ w_t \cdot \min\left( r_t(\theta) A_t, \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) A_t \right) \right] +$$ + +where: +- $w_t$: IS weight (depends on aggregation level, see Section 3.3). In decoupled mode, $w_t = \frac{\pi_{\text{old}}}{\pi_{\text{rollout}}}$ where $\pi_{\text{old}}$ is frozen, so $w_t$ is constant (no stopgrad needed). In bypass mode with PPO loss, no separate IS weights are typically computed. +- $r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{old}}(a_t|s_t)}$: PPO ratio +- $\epsilon$: Clip range (typically 0.2) + +**Properties:** +- Trust region control via clipping +- Limits policy update magnitude +- Standard in RL training + +#### 3.2.2 Policy Gradient Loss (with IS/RS Correction) + +**Configuration:** `loss_type = "reinforce"` (requires `bypass_mode = true`) + +**Loss function** (example with sequence-level IS): + +$$ +L_{\text{PG}}(\theta) = -\mathbb{E}_{(s,a) \sim \pi_{\text{rollout}}} \left[ \text{stopgrad}(w_{\text{seq}}(\theta)) \cdot \sum_{t \in T} \log \pi_{\theta}(a_t|s_t) \cdot A_t \right] +$$ + +where: +- $w_{\text{seq}}(\theta)$: Sample weight (IS or RS, see §3.3-3.4 for details) +- For IS: $w_{\text{seq}}(\theta) = \min\left( \prod_{t \in T} \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}, C_{\text{IS}} \right)$ +- For RS: $w_{\text{seq}}(\theta) \in \{0, 1\}$ (binary rejection mask) +- **stopgrad operator**: The weight $w_{\text{seq}}(\theta)$ is computed using $\pi_\theta$ but treated as a **constant coefficient** when computing $\nabla_\theta L$. This is essential for importance sampling correctness (see theoretical justification below). + +**Effective gradient:** + +$$ +\nabla_\theta L_{\text{PG}} = -\mathbb{E}_{(s,a) \sim \pi_{\text{rollout}}} \left[ \text{stopgrad}(w_{\text{seq}}(\theta)) \cdot \sum_{t \in T} \nabla_\theta \log \pi_{\theta}(a_t|s_t) \cdot A_t \right] +$$ + +**Theoretical Justification for stopgrad:** + +The stopgrad operator is **mathematically required** by importance sampling theory, not an implementation detail. Here's why: + +**The fundamental principle**: Importance sampling is a technique to **change the measure** (reweight samples from one distribution to estimate expectations under another), not to optimize the reweighting function itself. + +**Formal derivation**: + +1. **Original objective**: We want to optimize $J(\theta) = \mathbb{E}_{\tau \sim \pi_\theta}[\sum_t A_t]$. + +2. **Off-policy setting**: We only have samples from $\pi_{\text{rollout}}$, so we use importance sampling: + $$ + J(\theta) = \mathbb{E}_{\tau \sim \pi_{\text{rollout}}} \left[ \underbrace{\frac{P_{\pi_\theta}(\tau)}{P_{\pi_{\text{rollout}}}(\tau)}}_{w(\tau;\theta)} \sum_t A_t \right] + $$ + +3. **Computing the policy gradient**: The correct gradient uses the **policy gradient theorem BEFORE importance sampling**: + $$ + \begin{aligned} + \nabla_\theta J(\theta) &= \nabla_\theta \mathbb{E}_{\tau \sim \pi_\theta}\left[\sum_t A_t\right] \\ + &= \mathbb{E}_{\tau \sim \pi_\theta} \left[\sum_t A_t \nabla_\theta \log \pi_\theta(a_t|s_t) \right] \quad \text{(policy gradient theorem)} \\ + &= \mathbb{E}_{\tau \sim \pi_{\text{rollout}}} \left[ w(\tau;\theta) \sum_t A_t \nabla_\theta \log \pi_\theta(a_t|s_t) \right] \quad \text{(change of measure)} + \end{aligned} + $$ + + In the final line, $w(\tau;\theta)$ appears as a **multiplicative coefficient** from the change of measure, not as something we differentiate. + +4. **What goes wrong without stopgrad**: If we naively compute $\nabla_\theta \left[w(\theta) \log \pi_\theta \right]$ in the loss, we get: + $$ + \nabla_\theta \left[w(\theta) \log \pi_\theta \right] = \underbrace{\log \pi_\theta \cdot \nabla_\theta w(\theta)}_{\text{WRONG: bias term}} + \underbrace{w(\theta) \cdot \nabla_\theta \log \pi_\theta}_{\text{CORRECT: IS-weighted gradient}} + $$ + + The first term $\log \pi_\theta \cdot \nabla_\theta w(\theta)$ is an artifact of the computational trick (using loss times log-prob), not part of the true policy gradient. It biases the gradient estimator and optimizes a different objective than $J(\theta)$. + +5. **Implementation requirement**: In PyTorch, to compute only the second term, we must use: + ```python + loss = -advantages * log_prob * rollout_is_weights.detach() # stopgrad on weights + ``` + Without `.detach()`, autograd computes both terms, giving an incorrect gradient. + +**Intuition**: The IS weight $w(\theta)$ tells us "how much to trust this sample" for estimating the gradient under $\pi_\theta$. We update $\theta$ to maximize the reweighted objective, but we don't update $\theta$ to maximize the weight itself—that would be circular reasoning (optimizing the correction factor instead of the actual objective). + +**Properties:** +- **Algorithm**: Off-policy policy gradient with IS/RS correction +- **Loss types** (`loss_type` config option in bypass mode): + - `"ppo_clip"` (default): PPO clipped objective + - $L = -\mathbb{E}[\min(r \cdot A, \text{clip}(r) \cdot A)]$ where $r = \pi_\theta / \pi_{\text{rollout}}$ + - Note: IS weights NOT applied (PPO ratio already handles it; would be double-counting) + - `"reinforce"`: Pure policy gradient with explicit IS weights, no PPO clipping + - $L = -\mathbb{E}[w \cdot \log \pi_\theta(a|s) \cdot A]$ where $w = \pi_\theta / \pi_{\text{rollout}}$ +- **Always uses bypass mode**: Direct $\pi_\theta$ to $\pi_{\text{rollout}}$ comparison +- **Fast**: Single forward pass + +**Implementation:** `compute_policy_loss_bypass_mode()` and `compute_policy_loss_reinforce()` in [core_algos.py](../../verl/trainer/ppo/core_algos.py) + +--- + +### 3.3 IS/RS Aggregation Levels + +The aggregation level determines how per-token probability ratios are combined into IS weights and/or rejection masks. This choice is **orthogonal to the operating mode** - you can use any aggregation level in either decoupled or bypass mode. + +#### 3.3.1 Token-Level Aggregation + +**IS weights:** $w_t = \min(\rho_t, C_{\text{IS}})$ where $\rho_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ (decoupled) or $\rho_t = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$ (bypass/pure IS) + +**Configuration:** +```python +rollout_is = "token" # IS weights +rollout_rs = "token_k1" # Optional: rejection sampling (ratio bounds) +``` + +**Properties:** +- Independent truncation per token +- Lower variance than sequence-level (product of ratios bounded individually) +- **Bias-variance tradeoff**: Token-level correction has $O(T^2 \Delta_{\max})$ bias where $T$ is sequence length and $\Delta_{\max}$ is maximum per-token policy divergence. This bias becomes significant when the rollout policy deviates substantially from the training policy. Sequence-level correction is unbiased but has higher variance. +- Typical threshold: 1.5 - 5.0 +- Optional batch normalization [§3.4](rollout_corr_math.md#34-batch-normalization): Normalizes over all token weights to ensure $\mathbb{E}[\tilde{w}_t] = 1$ (reduces variance) +- **When to use**: Token-level works well when rollout policy stays within the trust region of training policy. When mismatch is significant, the bias becomes intolerable and sequence-level correction is preferred. + +**Loss function (REINFORCE + Token IS):** + +$$ +L_{\text{REINFORCE+TIS}}(\theta) = -\mathbb{E}_t \left[ \text{stopgrad}(w_t) \cdot \log \pi_\theta(a_t|s_t) \cdot A_t \right] +$$ + +where $w_t = \min(\rho_t, C_{\text{IS}})$ are the truncated token-level IS weights. The stopgrad operator ensures that when computing $\nabla_\theta L$, the weights are treated as constants (see §3.2.2 for theoretical justification). This formulation can also be combined with PPO clipping by replacing the REINFORCE gradient with the clipped surrogate objective. + +**Implementation:** +- IS weights: `compute_rollout_correction_weights()` in [rollout_corr_helper.py](../../verl/trainer/ppo/rollout_corr_helper.py#L325-L402) +- Loss: `compute_policy_loss()` in [core_algos.py](../../verl/trainer/ppo/core_algos.py#L812-L884) + +#### 3.3.2 Sequence-Level Aggregation + +**IS weights:** $w_{\text{seq}} = \min\left( \prod_{t \in T} \rho_t, C_{\text{IS}} \right) = \min\left( \exp\left(\sum_{t \in T} \log \rho_t\right), C_{\text{IS}} \right)$ (broadcast to all tokens) + +**Configuration:** +```python +rollout_is = "sequence" # IS weights +rollout_rs = "seq_sum_k1" # Optional: rejection sampling +``` + +**Properties:** +- Multiplicative aggregation across sequence +- More sensitive to outliers than token-level +- Typical threshold: 2.0 - 10.0 +- Optional batch normalization [§3.4](rollout_corr_math.md#34-batch-normalization): Normalizes over sequence means (one weight per sequence) + +**Terminology Note:** +- **Seq-TIS (Sequence-Level Truncated IS)**: Clips the sequence ratio $\rho(\tau) \to \min(\rho(\tau), C)$. Maximizes information efficiency by extracting signal from all samples. Best for clean data with moderate mismatch. +- **Seq-MIS (Sequence-Level Masked IS)**: Rejects (masks) sequences with $\rho(\tau) > C$ instead of clipping. Acts as a hard trust region filter. Best for severe mismatch or when the distribution tail is "toxic" (contains garbage/adversarial samples rather than signal). + +**Loss function (REINFORCE + Sequence IS):** + +$$ +L_{\text{REINFORCE+SeqIS}}(\theta) = -\mathbb{E}_t \left[ \text{stopgrad}(w_{\text{seq}}) \cdot \log \pi_\theta(a_t|s_t) \cdot A_t \right] +$$ + +where $w_{\text{seq}}$ is broadcast to all tokens in the sequence. The stopgrad operator ensures correct IS gradient computation (see §3.2.2). This formulation can also be combined with PPO clipping. + +#### 3.3.3 Geometric Mean Aggregation (Geo-RS) + +**Geometric mean ratio:** $\rho_{\text{geo}} = \exp\left( \frac{1}{|T|} \sum_{t \in T} \log \rho_t \right) = \left(\prod_{t \in T} \rho_t\right)^{1/|T|}$ (broadcast to all tokens) + +**Configuration:** +```python +rollout_is = null # No IS weights, pure rejection +rollout_rs = "seq_mean_k1" # Geometric mean rejection sampling (ratio bounds) +``` + +**Properties:** +- Length-invariant (normalizes by sequence length) +- Ideal ratio = 1.0 (policies match) +- Typical bounds: `"0.999_1.001"` (~±0.1%) +- **Used for rejection sampling only, not IS weighting** + +**The Length Trap Problem:** + +Standard IS estimators have a systematic **length bias** that penalizes long sequences. The importance ratio $\rho(y)$ is multiplicative: + +$$ +\rho(y) = \prod_{t=1}^T \frac{\pi(y_t|y_{= 0 per token (equals 0 when ρ = 1) +- More stable than geometric ratio checks because each token term is non-negative +- Only upper threshold applies (no lower threshold since K3 >= 0) +- Typical threshold: 0.001 - 0.01 + +**Why K3 over geometric ratio?** +- Geometric ratio uses average log-ratio; small numerical bias can flip sign +- K3 = E[ρ - log ρ - 1] is non-negative per token, offering a smoother detector +- Both estimate the same quantity: KL(π_rollout || π_old) +- For small divergences, K3 ≈ 0.5 × Var(log_ratio) + +**Combined Estimator (K3-RS-Token-TIS):** + +For best results, combine K3 filter with token-level IS weights: + +$$ +\hat{g}_{\text{k3-rs-token-tis}}(y) = \underbrace{\mathbb{I}\left( K3_{\text{seq}} \le C_{\text{k3}} \right)}_{\text{K3 Filter}} \cdot \prod_t \min(\rho_t, C) \cdot f(y) +$$ + +This is implemented by combining `rollout_rs="seq_mean_k3"` with `rollout_is="token"`. + + +--- + +### 3.4 Batch Normalization + +An optional variance reduction technique that normalizes IS weights to have mean 1.0 within each batch. + +**Configuration:** +```python +rollout_is_batch_normalize = True # Default: False +``` + +**Normalization formula (aggregation-aware):** + +For **token-level IS** (§3.3.1): + +$$ +\tilde{w}_t = \frac{w_t}{\frac{1}{\sum_{i,t} m_{i,t}} \sum_{i,t} w_{i,t} \cdot m_{i,t}} +$$ + +where $w_{i,t}$ are truncated token IS weights, $m_{i,t}$ is the response mask, and normalization is over **all tokens**. + +For **sequence-level IS** (§3.3.2): + +$$ +\tilde{w}_i = \frac{w_i}{\frac{1}{B}\sum_{j=1}^B \bar{w}_j} +$$ + +where $\bar{w}_j = \frac{1}{T_j}\sum_{t=1}^{T_j} w_{j,t} \cdot m_{j,t}$ is the per-sequence mean (all tokens in a sequence have the same weight), and normalization is over **sequences**. + +**Properties:** +- Applied **after** truncation to preserve truncation semantics +- Ensures $\mathbb{E}[\tilde{w}] = 1$ within each batch +- **Aggregation-aware**: Token-level normalizes over tokens; sequence-level normalizes over sequences +- Uses `masked_mean` to respect padding tokens +- Reduces gradient magnitude variance by removing random batch-level scale fluctuations + +**Metrics:** +- `rollout_is_batch_norm_factor`: The normalization factor applied (batch mean before normalization) + +**Implementation:** [rollout_corr_helper.py](../../verl/trainer/ppo/rollout_corr_helper.py#L401-L421) + +--- + +### 3.5 Rejection Sampling (RS) + +Rejection sampling can be added to **any combination** of operating mode and aggregation level. It modifies the `response_mask` to exclude outlier tokens/sequences. + +**Configuration examples:** +```python +rollout_rs = "token_k1" # Token-level ratio bounds +rollout_rs_threshold = "0.6_1.6" + +rollout_rs = "seq_sum_k1" # Sequence sum of log ratios +rollout_rs_threshold = "0.5_2.0" + +rollout_rs = "seq_mean_k3" # Sequence mean of K3 divergence +rollout_rs_threshold = 0.01 +``` + +**Acceptance set:** +- **Token-level**: $\mathcal{A}_{\text{token}} = \{ t : C_{\text{RS-lower}} \leq \rho_t \leq C_{\text{RS-upper}} \}$ +- **Sequence-level**: $\mathcal{A}_{\text{seq}} = \{ \text{seq} : C_{\text{RS-lower}} \leq \prod_{t \in T} \rho_t \leq C_{\text{RS-upper}} \}$ +- **Geometric**: $\mathcal{A}_{\text{geo}} = \{ \text{seq} : C_{\text{RS-lower}} \leq \rho_{\text{geo}} \leq C_{\text{RS-upper}} \}$ + +**Properties:** +- Separate from IS weighting (can use RS without IS) +- Reduces effective sample size +- Filters extreme outliers + +**Implementation:** `compute_rollout_rejection_mask()` in [rollout_corr_helper.py](../../verl/trainer/ppo/rollout_corr_helper.py#L80-L188) + +--- + +### 3.6 Combination Matrix + +**Key insight:** Estimators (how IS/RS is computed) and operating modes (decoupled PPO vs bypass PG) are **orthogonal**. Any estimator can be combined with any operating mode. + +#### Estimator × Operating Mode + +| Estimator | Configuration | Compatible Modes | +|-----------|---------------|------------------| +| **Token-TIS** | `rollout_is="token"` | Decoupled PPO, Bypass PG | +| **Seq-TIS** | `rollout_is="sequence"` | Decoupled PPO, Bypass PG | +| **Seq-MIS** | `rollout_is="sequence"` + `rollout_rs="seq_sum_k1"` | Decoupled PPO, Bypass PG | +| **Geo-RS** | `rollout_rs="seq_mean_k1"` (geometric mean) | Decoupled PPO, Bypass PG | +| **Geo-RS-Token-TIS** | `rollout_is="token"` + `rollout_rs="seq_mean_k1"` | Decoupled PPO, Bypass PG | +| **K3-RS** | `rollout_rs="seq_mean_k3"` | Decoupled PPO, Bypass PG | +| **K3-RS-Token-TIS** | `rollout_is="token"` + `rollout_rs="seq_mean_k3"` | Decoupled PPO, Bypass PG | + +**Note:** In bypass mode, `loss_type` controls the loss function. Use "ppo_clip" (default) or "reinforce". + +#### Available Preset Methods + +| Preset Method | Estimator | Mode | Properties | +|---------------|-----------|------|------------| +| **Decoupled PPO Mode** (3 policies: π_rollout, π_old, π_θ) | +| `decoupled_token_is()` | Token-TIS | Decoupled PPO | Per-token IS weights | +| `decoupled_seq_is()` | Seq-TIS | Decoupled PPO | Sequence-level IS weights | +| `decoupled_seq_is_rs()` | Seq-MIS | Decoupled PPO | Sequence IS + sequence RS | +| `decoupled_geo_rs()` | Geo-RS | Decoupled PPO | Geometric RS | +| `decoupled_geo_rs_token_tis()` | Geo-RS-Token-TIS | Decoupled PPO | Geometric filter + token IS | +| **K3 KL Estimator** (more stable for small KL values) | +| `decoupled_k3_rs()` | K3-RS | Decoupled PPO | K3 rejection, no IS weights | +| `decoupled_k3_rs_token_tis()` | K3-RS-Token-TIS | Decoupled PPO | K3 filter + token clipped weight | +| **Bypass Mode (PPO-clip)** (ratio handles IS, RS masks outliers) | +| `bypass_ppo_clip()` | - | Bypass (PPO-clip) | PPO-clip only | +| `bypass_ppo_clip_geo_rs()` | Geo-RS | Bypass (PPO-clip) | PPO-clip + Geo-RS (ratio) | +| `bypass_ppo_clip_k3_rs()` | K3-RS | Bypass (PPO-clip) | PPO-clip + K3-RS | +| **Bypass Mode (REINFORCE)** (explicit IS weights, no PPO clipping) | +| `bypass_pg_is()` | Seq-TIS | Bypass (REINFORCE) | REINFORCE + Seq IS | +| `bypass_pg_geo_rs()` | Geo-RS | Bypass (REINFORCE) | REINFORCE + Geo-RS (ratio) | +| `bypass_pg_geo_rs_token_tis()` | Geo-RS-Token-TIS | Bypass (REINFORCE) | REINFORCE + Geo filter + token IS | +| **Other** | +| `disabled()` | - | - | Metrics only | + +**Note:** Bypass mode sets π_old = π_rollout and uses `loss_type` to select the loss function. + +#### Additional Supported Combinations (Manual Configuration) + +These combinations are **fully supported** but require manual configuration: + +**1. Token IS + Token RS** +```python +config = RolloutCorrectionConfig( + rollout_is="token", + rollout_is_threshold=2.0, + rollout_rs="token_k1", + rollout_rs_threshold="0.5_2.0", +) +``` +**Properties:** Token-level IS weights + token-level RS mask. + +**2. Pure Token RS** +```python +config = RolloutCorrectionConfig( + rollout_is=None, + rollout_rs="token_k1", + rollout_rs_threshold="0.5_2.0", +) +``` +**Properties:** Token-level RS mask only, no IS weights. + +**3. Pure Sequence RS** +```python +config = RolloutCorrectionConfig( + rollout_is=None, + rollout_rs="seq_sum_k1", + rollout_rs_threshold="0.5_2.0", +) +``` +**Properties:** Sequence-level RS mask only, no IS weights. + +**Key properties:** +- Any IS aggregation level (token/sequence) can be used in either decoupled or bypass mode +- Rejection sampling can be added to any combination +- Geometric aggregation is typically used for RS only (not IS weighting) +- Pure RS (`bypass_pg_rs`) uses bypass + geometric RS with `loss_type="reinforce"` for REINFORCE (no IS weights) +- All combinations in the table above are valid and supported by the implementation + +--- + +### 3.7 Common Implementation Mistake + +#### Incorrect LLM-RL Implementation (PPO Without Rollout Correction) + +**Theory:** Naive LLM-RL implementation that incorrectly applies PPO by **ignoring the actual rollout policy** and assuming $\pi_{\text{old}} = \pi_{\text{rollout}}$. + +**Note:** This incorrect implementation pattern was identified in [Liu, Li, et al. (2025)](https://richardli.xyz/rl-collapse) as a key cause of training instability in LLM-RL systems, motivating the development of this rollout correction framework. + +**Loss Function:** + +$$ +L_{\text{PPO}}(\theta) = -\mathbb{E}_t \left[ \min\left( r_t(\theta) A_t, \text{clip}(r_t(\theta), 1-\epsilon, 1+\epsilon) A_t \right) \right] +$$ + +where $r_t(\theta) = \frac{\pi_{\theta}(a_t|s_t)}{\pi_{\text{old}}(a_t|s_t)}$ (ignores $\pi_{\text{rollout}}$). + +**Why it's wrong:** +- **Ignores $\pi_{\text{rollout}}$**: Uses $\pi_{\text{old}}$ as behavior policy instead of actual $\pi_{\text{rollout}}$ +- **Policy mismatch**: In LLM-RL, rollout typically uses different precision/backend/checkpoint than training, causing $\pi_{\text{rollout}} \neq \pi_{\text{old}}$ even with same model weights +- **Not PPO's fault**: PPO itself is correct; the issue is the incorrect assumption + +**Correct alternatives:** +1. **Decoupled mode**: Three policies with IS correction from $\pi_{\text{rollout}}$ to $\pi_{\text{old}}$ +2. **Bypass mode**: Two policies using $\pi_{\text{rollout}}$ as both behavior policy and proximal policy +3. **Bypass + Policy Gradient mode**: Two policies with IS/RS correction and no PPO clipping + +**Implementation:** `compute_policy_loss()` in [core_algos.py](../../verl/trainer/ppo/core_algos.py#L812-L884) + +--- + +## 4. Off-Policy Diagnostic Metrics + +These metrics quantify the severity of off-policy drift. + +**Note on notation:** Metrics use $\rho_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$. In bypass mode, $\pi_{\text{old}} = \pi_{\text{rollout}}$, so metrics measure rollout→current drift using $\rho_t = \frac{\pi_{\theta}}{\pi_{\text{rollout}}}$ instead. + +### 4.1 KL Divergence + +**Direct KL estimator:** + +$$ +\text{KL}(\pi_{\text{rollout}} \| \pi_{\text{old}}) = \mathbb{E}_{t \sim \pi_{\text{rollout}}} \left[ \log \pi_{\text{rollout}}(a_t|s_t) - \log \pi_{\text{old}}(a_t|s_t) \right] +$$ + +**K3 KL estimator** (alternative formulation): + +$$ +\text{KL}_{\text{K3}} = \mathbb{E}_{t \sim \pi_{\text{rollout}}} \left[ \rho_t - \log \rho_t - 1 \right] +$$ + +where $\rho_t = \frac{\pi_{\text{old}}(a_t|s_t)}{\pi_{\text{rollout}}(a_t|s_t)}$. + +### 4.2 Perplexity + +**Old policy perplexity:** + +$$ +\text{PPL}_{\text{old}} = \exp\left( -\frac{1}{|T|} \sum_{t \in T} \log \pi_{\text{old}}(a_t|s_t) \right) +$$ + +**Rollout policy perplexity:** + +$$ +\text{PPL}_{\text{rollout}} = \exp\left( -\frac{1}{|T|} \sum_{t \in T} \log \pi_{\text{rollout}}(a_t|s_t) \right) +$$ + +**PPL ratio** (inverse of geometric mean IS weight): + +$$ +\text{PPL}_{\text{ratio}} = \frac{\text{PPL}_{\text{old}}}{\text{PPL}_{\text{rollout}}} = \exp\left( -\frac{1}{|T|} \sum_{t \in T} \log \rho_t \right) = \left(\prod_{t \in T} \rho_t\right)^{-1/|T|} +$$ + +**Interpretation:** Values > 1 mean $\pi_{\text{old}}$ assigns lower probability than $\pi_{\text{rollout}}$ to the observed actions (distribution shift). + +### 4.3 Chi-squared Divergence + +Measures the second moment of the IS weight distribution. + +**Token-level:** + +$$ +\chi^2_{\text{token}} = \mathbb{E}_{t \sim \pi_{\text{rollout}}} \left[ \rho_t^2 \right] - 1 +$$ + +**Sequence-level:** + +$$ +\chi^2_{\text{seq}} = \mathbb{E}_{\text{seq} \sim \pi_{\text{rollout}}} \left[ \left(\prod_{t \in T} \rho_t\right)^2 \right] - 1 +$$ + +**Interpretation:** +- $\chi^2 = 0$: Policies are identical +- $\chi^2 > 0$: Higher values indicate more severe off-policy distribution shift + +**Implementation:** `compute_offpolicy_metrics()` in [rollout_corr_helper.py](../../verl/trainer/ppo/rollout_corr_helper.py#L670-L776) + +--- + +## 5. Summary and Decision Guide + +### 5.1 Method Summary Table + +| Method | Theory | Policies | PPO Clip | IS Correction | Correctness | Speed | +|--------|--------|----------|----------|---------------|-------------|-------| +| **Bypass Mode** (π_old = π_rollout, `loss_type` selects algorithm) | +| `loss_type="ppo_clip"` (default) | PPO (ratio = π_θ/π_rollout) | 2 (rollout, θ) | ✅ | RS mask only (ratio handles IS) | ✅ Correct | **Fast** | +| `loss_type="reinforce"` | Off-policy REINFORCE | 2 (rollout, θ) | ❌ | ✅ (explicit IS weights) | ✅ Correct | **Fast** | +| **Bypass Mode Presets (PPO-clip)** | +| `bypass_ppo_clip` | PPO only | 2 (rollout, θ) | ✅ | - | ✅ Correct | **Fast** | +| `bypass_ppo_clip_geo_rs` | PPO + Geo-RS | 2 (rollout, θ) | ✅ | Geo-RS mask (ratio) | ✅ Correct | **Fast** | +| **Bypass Mode Presets (REINFORCE)** | +| `bypass_pg_is` | REINFORCE + Seq-TIS | 2 (rollout, θ) | ❌ | ✅ Seq-TIS | ✅ Correct | **Fast** | +| `bypass_pg_geo_rs` | REINFORCE + Geo-RS | 2 (rollout, θ) | ❌ | Geo-RS only (ratio) | ✅ Correct | **Fast** | +| `bypass_pg_geo_rs_token_tis` | REINFORCE + Geo RS + Token IS | 2 (rollout, θ) | ❌ | ✅ Geo-RS-Token-TIS | ✅ Correct | **Fast** | +| **Decoupled PPO Mode** (IS weights = π_old / π_rollout) | +| `decoupled_token_is` | Decoupled PPO | 3 (rollout, old, θ) | ✅ | ✅ Token-TIS | ✅ Correct | Standard | +| `decoupled_seq_is` | Decoupled PPO | 3 (rollout, old, θ) | ✅ | ✅ Seq-TIS | ✅ Correct | Standard | +| `decoupled_seq_is_rs` | Decoupled PPO + RS | 3 (rollout, old, θ) | ✅ | ✅ Seq-MIS | ✅ Correct | Standard | +| `decoupled_geo_rs` | Decoupled PPO + Geo-RS | 3 (rollout, old, θ) | ✅ | Geo-RS only (ratio) | ✅ Correct | Standard | +| `decoupled_geo_rs_token_tis` | Decoupled PPO + Geo RS + Token IS | 3 (rollout, old, θ) | ✅ | ✅ Geo-RS-Token-TIS | ✅ Correct | Standard | +| **Incorrect (for reference)** | +| Naive LLM-RL | Incorrect PPO usage | 2 (old, θ) | ✅ | ❌ | ⚠️ Incorrect | Standard | + +**Notes:** +- **Bypass mode** sets π_old = π_rollout and uses `loss_type` to select the loss function: + - `"ppo_clip"` (default): PPO clipped ratio (IS handled by ratio = π_θ/π_rollout, no explicit IS weights to avoid double-counting) + - `"reinforce"`: Explicit IS weights applied as $w \cdot \log \pi \cdot A$ +- Both loss types benefit from rejection sampling (RS) which masks out-of-distribution samples + +### 5.2 Estimator Hierarchy + +These estimators define **how IS weights and rejection masks are computed**. They are orthogonal to the operating mode (decoupled PPO vs bypass policy gradient) and can be combined with either. + +| Estimator | Configuration | Mechanism | Best For | +|-----------|---------------|-----------|----------| +| **Token-TIS** | `rollout_is="token"` | Clips per-token ratios | Lower variance IS with acceptable bias | +| **Seq-TIS** | `rollout_is="sequence"` | Clips sequence ratio $\rho(\tau) \to \min(\rho(\tau), C)$ | Clean data with moderate mismatch; unbiased | +| **Seq-MIS** | `rollout_is="sequence"` + `rollout_rs="seq_sum_k1"` | Rejects sequences with $\rho(\tau) > C$ | Severe mismatch; filters "toxic tail" (garbage data) | +| **Geo-RS** | `rollout_rs="seq_mean_k1"` | Rejects on geometric mean ratio exp(E[log(r)]) | Length-invariant trust region | +| **Geo-RS-Token-TIS** | `rollout_is="token"` + `rollout_rs="seq_mean_k1"` | Geometric filter + token IS weights | Ratio-based length normalization + lower variance IS | +| **K3-RS** | `rollout_rs="seq_mean_k3"` | Rejects on K3 KL divergence | Small KL values; smooth detector | +| **K3-RS-Token-TIS** | `rollout_is="token"` + `rollout_rs="seq_mean_k3"` | K3 filter + token IS weights | Small KL + lower variance IS | + +**Note:** Each estimator can be used with either: +- **Decoupled PPO** (`bypass_mode=false`): Three policies with PPO clipping +- **Bypass Mode** (`bypass_mode=true`): Two policies with configurable loss type + - `loss_type="ppo_clip"` (default): PPO clipped objective (IS via ratio, RS mask applied) + - `loss_type="reinforce"`: REINFORCE with explicit IS weights + +### 5.3 Method Characteristics by Scenario + +**Choosing estimator by off-policy severity:** +- **Negligible** (same checkpoint, minor differences): No IS correction needed; use bypass mode for efficiency +- **Moderate** (async workers, slight staleness): Token-TIS provides per-token IS correction with lower variance +- **Severe** (replay buffers, old data): Seq-TIS or Seq-MIS provides sequence-level IS correction; use Seq-MIS when high-weight samples are likely garbage + +**Choosing estimator by sequence length:** +- **Short sequences** (standard chat): Seq-TIS is optimal +- **Long sequences** (CoT, agents): K1-RS or K1-RS-Token-TIS to avoid Length Trap + +**Choosing operating mode:** +- **Batch size invariance needed**: Use decoupled mode (`bypass_mode=false`) +- **Computational efficiency needed**: Use bypass mode (`bypass_mode=true`) to skip `old_log_prob` computation +- **No PPO clipping**: Use bypass mode with `loss_type="reinforce"` + +### 5.4 Decoupled Mode vs Bypass Mode + +**Decoupled mode** (computes `old_log_prob` separately): +- Implements full decoupled PPO with three policies (mathematically correct) +- Separately measures and corrects Drift 1 (rollout→old) and Drift 2 (old→current) +- Achieves batch size invariance and efficient stale data utilization +- Enables accurate off-policy metrics monitoring + +**Bypass mode** (sets $\pi_{\text{old}} = \pi_{\text{rollout}}$): +- Uses $\pi_{\text{rollout}}$ as both behavior policy and proximal policy (mathematically correct) +- Computational efficiency: Skips separate `old_log_prob` computation +- Does not achieve batch size invariance (proximal policy depends on data collection) + +--- + +## 6. Implementation References + +- **[Rollout Correction Usage Guide](rollout_corr.md)** - Practical configuration and troubleshooting +- **Config:** [verl/trainer/config/algorithm.py](../../verl/trainer/config/algorithm.py) +- **IS/RS Helper:** [verl/trainer/ppo/rollout_corr_helper.py](../../verl/trainer/ppo/rollout_corr_helper.py) +- **PPO Loss:** [verl/trainer/ppo/core_algos.py](../../verl/trainer/ppo/core_algos.py) +- **Tests:** [tests/trainer/ppo/test_rollout_corr.py](../../tests/trainer/ppo/test_rollout_corr.py) + +--- + +## References + +- **Williams, R. J. (1992).** "Simple statistical gradient-following algorithms for connectionist reinforcement learning." *Machine Learning*, 8(3-4), 229-256. https://doi.org/10.1007/BF00992696 +- **Schulman, J., Wolski, F., Dhariwal, P., Radford, A., & Klimov, O. (2017).** "Proximal policy optimization algorithms." *arXiv preprint arXiv:1707.06347.* https://arxiv.org/abs/1707.06347 +- **Hilton, J., Cobbe, K., & Schulman, J. (2021).** "Batch size-invariance for policy optimization." *arXiv preprint arXiv:2110.00641.* https://arxiv.org/abs/2110.00641 + - Introduced decoupled PPO: separating proximal policy (for controlling policy update size) from behavior policy (for off-policy correction) to achieve batch size invariance diff --git a/verl_0720_main/verl/docs/algo/spin.md b/verl_0720_main/verl/docs/algo/spin.md new file mode 100644 index 0000000000000000000000000000000000000000..682ce43954f5cefef358d45b4ff097941bd077c3 --- /dev/null +++ b/verl_0720_main/verl/docs/algo/spin.md @@ -0,0 +1,179 @@ +# Recipe: Self-Play Fine-Tuning (SPIN) + +Last updated: 05/31/2025. + +`verl` provides a recipe inspired by the paper **"Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models"** (SPIN). SPIN is a language model finetuning algorithm that enables iterative self-improvement through a self-play mechanism inspired by game theory. + +**Core Idea:** Models learn by playing against themselves, reducing reliance on external preference datasets or stronger teacher models: + +1. **Synthetic Data Generation:** The current model generates responses, creating its own training data from previous iterations. +2. **Two-Player Game Setup:** A game involving two players acted by a single LLM. +3. **Iterative Training:** The model progressively improves by refining its policy, with each iteration's model becoming the opponent for the next iteration. + +Paper Authors: [Zixiang Chen](https://github.com/uclaml/SPIN)\*, [Yihe Deng](https://github.com/uclaml/SPIN)\*, [Huizhuo Yuan](https://scholar.google.com/citations?user=8foZzX4AAAAJ)\*, [Kaixuan Ji](https://scholar.google.com/citations?user=FOoKDukAAAAJ), [Quanquan Gu](https://web.cs.ucla.edu/~qgu/) + +[[Webpage](https://uclaml.github.io/SPIN/)] [[Huggingface](https://huggingface.co/papers/2401.01335)] [[Paper](https://arxiv.org/abs/2401.01335)] [[Original Implementation](https://github.com/uclaml/SPIN)] + +verl Implementation Authors: [Chendong Wang](https://cdwang96.github.io/), [Chenyang Zhao](https://github.com/zhaochenyang20) + +--- + +## Key Function (compute_online_dpo_loss) and Related works +SPIN (Chen et al., 2024) proposes an iterative self-play mechanism to fine-tune language models. In each iteration, SPIN's training objective, when using a logistic loss function, is equivalent to Direct Preference Optimization (DPO) loss (Rafailov et al., 2023). + +This `verl` recipe realizes SPIN's core concept by using DPO loss iteratively (Xu et al., 2023; Xiong et al., 2023; Snorkel AI, 2024). This means that in each iteration, we fine-tune the LLM using DPO loss for preference optimization. Notably, Xu et al. (2023) explored iterative preference optimization with pairwise cringe loss, while Xiong et al. (2023) discussed how to bridge theory and practice for RLHF under KL constraints using iterative training. The concept of iterative preference learning was also explored in online DPO (Guo et al., 2024), which focuses on direct alignment from online AI feedback. In online DPO, preference data is dynamically updated during training, allowing the model to learn from its own generated data. + +Specifically, we developed the **`compute_online_dpo_loss`** function and built this SPIN recipe on top of it. By incorporating online preference generation, this approach enables continuously refining language models without relying on fixed external preference datasets. + +**Reference Papers:** +* [Self-Play Fine-Tuning Converts Weak Language Models to Strong Language Models](https://arxiv.org/abs/2401.01335) (Chen et al., 2024) +* [Direct Preference Optimization: Your Language Model is Secretly a Reward Model](https://arxiv.org/abs/2305.18290) (Rafailov et al., 2023) +* [Somethings are more cringe than others: Preference optimization with the pairwise cringe loss](https://arxiv.org/abs/2312.16682) (Xu et al., 2023) +* [Iterative preference learning from human feedback: Bridging theory and practice for rlhf under kl-constraint](https://arxiv.org/abs/2312.11456) (Xiong et al., 2023) +* [Snorkel-Mistral-PairRM-DPO](https://huggingface.co/snorkelai/Snorkel-Mistral-PairRM-DPO) (Snorkel AI, 2024) +* [Direct language model alignment from online ai feedback](https://arxiv.org/abs/2402.04792) (Guo et al., 2024) + + +## Our Online DPO Implementation + +Our `compute_online_dpo_loss` function adapts `verl`'s existing PPO infrastructure (based on `verl` v0.3.0.post1) for this iterative online DPO. Key aspects of our implementation include: + +* **No Critic:** Unlike PPO, we omit the value function critic. +* **Dynamic Reference Model:** An explicit reference policy (`ref_policy_wg`) is used for DPO loss. This reference model's weights can be periodically updated from the actor (`ref_update_freq`), providing a dynamic baseline. +* **Online Preference Generation:** The `compute_onlineDPO_pref` function (in `core_algos.py`) dynamically creates chosen/rejected pairs based on a reward source (e.g., rule-based ranking for math problems). +* **DPO Loss Integration:** We replace PPO's policy loss with our `compute_online_dpo_loss` (in `core_algos.py`) within the actor update (`dp_actor.py`), directly optimizing the policy using the generated preferences. +* **Iterative Training Orchestration:** The `SpinTrainer` (in `spin_trainer.py`) manages the entire self-play loop: generation, preference labeling, optional reference model updates, and policy updates, enabling continuous self-improvement aligned with SPIN's principles. + +--- +## Algorithm + +This recipe implements an Online algorithm adapted to the `verl` Reinforcement Learning framework, which provides an alternative to PPO for fine-tuning language models. + +**Online Loop:** Instead of maximizing a scalar reward signal in PPO, this approach directly optimizes the policy model to align with preference data generated *online* during training: + +1. **Generation:** The current model generates multiple responses for each prompt in a batch. +2. **Preference Labeling:** A function evaluates these generated responses to determine which one is preferred (chosen) and which is dispreferred (rejected). This can be done using a reward function or implicit ranking based on specific rules. (In this recipe, we use rule-based ranking on the math problem). +3. **Update:** This preference tuple (`prompt`, `chosen_response`, `rejected_response`) is used to update the actor model using `compute_online_dpo_loss`, comparing against a reference model. + +**Connection with SPIN:** +Instead of only using a fixed target data distribution, the online generation loop in step 2 will dynamically change the target data distribution by using a certain Preference Labeling method (rule-based ranking on the math problem by selecting the better one in this recipe). This explores the direction mentioned in SPIN's paper Section 7 about "dynamically changing target data distribution" to potentially elevate LLM performance beyond the fixed human-annotated data ceiling. + +--- + +## Reproduce the Experiment (Example Setup) + +The following steps outline how to set up the environment and run the SPIN recipe, based on the provided test log using GSM8K and Qwen2.5-3B-Instruct. + +1. **Setup Environment (Example using Docker):** + ```bash + # Start a container with GPU access and shared memory + docker run -it --name spin_test --gpus all \ + --shm-size=32g \ + --ipc=host \ + -v /path/to/host/.cache:/root/.cache \ + -e HF_TOKEN= \ + lmsysorg/sglang:latest \ + /bin/bash + + # Inside the container or on your host machine: + # Ensure /tmp is writable + mkdir -p /tmp + chmod 1777 /tmp + + # Install Python 3.10 (if not present) and venv + sudo apt update + sudo apt install -y python3.10 python3.10-venv tmux + python3 -m ensurepip --upgrade + + # Create and activate a virtual environment + python3 -m venv ~/.python/spin_env + source ~/.python/spin_env/bin/activate + + # Install uv (fast package installer) + python3 -m pip install uv + ``` + +2. **Install verl and Dependencies:** + ```bash + # Clone the verl repository and checkout the spin branch + cd ~ + git clone git@github.com:verl-project/verl.git && cd verl + + # Install flash-attn (handle potential build issues) + python3 -m uv pip install wheel packaging + python3 -m uv pip install flash-attn --no-build-isolation --no-deps + + # Install verl with sglang extras + python3 -m uv pip install -e ".[sglang]" + ``` + *Note: If `flash-attn` installation fails, try the manual steps again or consult its documentation.* + +3. **Login & Download Data/Model:** + ```bash + # Login to Weights & Biases (optional, for logging) + export WANDB_API_KEY= + # wandb login + + # Download the GSM8K dataset + python3 examples/data_preprocess/gsm8k.py --local_save_dir ~/data/gsm8k # Adjusted path + + # Download the base model (Example: Qwen2.5-3B-Instruct) + hf download Qwen/Qwen2.5-3B-Instruct --local-dir $HOME/models/Qwen2.5-3B-Instruct + ``` + +4. **Configure:** + * Modify the configuration file (e.g., `config/spin_trainer.yaml` or the one specified in the run script) with correct paths to your downloaded model, data, desired hyperparameters (`dpo_beta`, learning rate, etc.), and distributed training settings (nodes, GPUs per node). + * Pay attention to `actor_rollout_ref.model`, `data` paths, `reward_model` config (if using one), and `trainer.ref_update_freq`. + +5. **Run Training:** + ```bash + # Set CUDA visible devices (adjust based on your hardware and config) + export CUDA_VISIBLE_DEVICES=0,1,2,3 + + # Launch the training script (e.g., test.sh or a custom script) + # Ensure test.sh points to the correct config and main script + bash recipe/spin/run_spin.sh + ``` + +--- + +## Configuration + +* The primary configuration is typically managed through a YAML file specified in the launch script (e.g., `config/spin_trainer.yaml`). +* Key configuration sections: + * `data`: Paths to training/validation prompt files, batch sizes, sequence lengths. + * `actor_rollout_ref`: Paths to the base model (used for actor and initial reference), FSDP settings, optimization parameters (learning rate, scheduler). + * `reward_model`: Configuration for the reward model used for online preference labeling (path, batch size, etc.). Can be omitted if using a simpler reward function. + * `algorithm`: DPO-specific hyperparameters like `dpo_beta`, `dpo_loss_type`. + * `trainer`: Distributed training settings (nodes, GPUs per node), logging (WandB), checkpointing frequency, and `ref_update_freq` (set > 0 to enable periodic reference model updates from the actor). + +--- + +## Key Files + +* `main_spin.py`: Main entry point using Hydra to load the config and launch the `SpinTrainer`. +* `spin_trainer.py`: Defines the `SpinTrainer` class, orchestrating the Online DPO training loop. +* `fsdp_workers.py`: Implements Ray workers (Actor, Reference) potentially using FSDP. +* `dp_actor.py`: Contains the actor class, including the DPO policy update logic. +* `core_algos.py`: Includes helper functions for `compute_online_dpo_loss` and `compute_onlineDPO_pref`. +* `config/spin_trainer.yaml` (or similar): Main Hydra configuration file for the recipe. +* `run_spin.sh` (or similar): Example bash script for launching a training run. +* `README.md`: This file. + +--- + +## Acknowledgement + +We sincerely thank the contribution and guidance from the `verl` community and advisors, including (adapted from SPPO): + +* [Zixiang Chen](https://sites.google.com/view/zxchen) +* [Yuhao Yang](https://github.com/yhyang201) +* [Yifan Zhang](https://github.com/yifanzhang-pro) +* [Yongan Xiang](https://github.com/BearBiscuit05) +* [Junrong Lin](https://github.com/ocss884) +* [Yuxuan Tong](https://github.com/tongyx361) +* [Guangming Shen](https://github.com/PeterSH6) +* [Biao He](https://www.linkedin.com/in/biao-he/) +* [Qingquan Song](https://qingquansong.github.io/) +* [Chenyang Zhao](https://zhaochenyang20.github.io/Chayenne/) +* [Quanquan Gu](https://web.cs.ucla.edu/~qgu/) diff --git a/verl_0720_main/verl/docs/algo/sppo.md b/verl_0720_main/verl/docs/algo/sppo.md new file mode 100644 index 0000000000000000000000000000000000000000..77dbbacdd15b01a13cf8614738fca91b3174234c --- /dev/null +++ b/verl_0720_main/verl/docs/algo/sppo.md @@ -0,0 +1,52 @@ +# Recipe: Self-Play Preference Optimization (SPPO) + +Last updated: 05/28/2025. + +verl provides a community recipe implementation for the paper [Self-Play Preference Optimization for Language Model Alignment](https://arxiv.org/abs/2405.00675). SPPO can significantly enhance the performance of an LLM without strong external signals such as responses or preferences from GPT-4. It can outperform the model trained with iterative direct preference optimization (DPO), among other methods. SPPO is theoretically grounded, ensuring that the LLM can converge to the von Neumann winner (i.e., Nash equilibrium) under general, potentially intransitive preference, and empirically validated through extensive evaluations on multiple datasets. + +Paper Authors: [Yue Wu](https://yuewu.us/)\*, [Zhiqing Sun](https://www.cs.cmu.edu/~zhiqings/)\*, [Huizhuo Yuan](https://scholar.google.com/citations?user=8foZzX4AAAAJ)\*, [Kaixuan Ji](https://scholar.google.com/citations?user=FOoKDukAAAAJ), [Yiming Yang](https://www.cs.cmu.edu/~yiming/), [Quanquan Gu](https://web.cs.ucla.edu/~qgu/) + +verl Implementation Authors: [Yuhao Yang](https://github.com/yhyang201), [Chenyang Zhao](https://github.com/zhaochenyang20) + +[[Webpage](https://uclaml.github.io/SPPO/)] [[Huggingface](https://huggingface.co/papers/2405.00675)] [[Paper](https://arxiv.org/abs/2405.00675)][[Original Implementation](https://github.com/uclaml/SPPO)] + +## Reproduce the Experiment + +We evaluate the performance of SPPO on the MATH dataset. Starting from an initial score of 46.6 with Qwen2.5-7B-Instruct, we achieve a score of 65.6 after 20 epochs of training, placing our model approximately in the top 20 on the [MATH leaderboard](https://paperswithcode.com/sota/math-word-problem-solving-on-math). It's important to note that verl's internal evaluation metrics may not perfectly align with the official evaluation methodology for Qwen2.5-7B-Instruct. Therefore, for consistency and fair comparison, we report only the results based on verl's evaluation framework. + +``` +git clone git@github.com:verl-project/verl.git +cd verl +python3 -m uv pip install -e ".[sglang]" + +export WANDB_API_KEY= + +python3 examples/data_preprocess/math_dataset.py --local_dir ~/data/math +hf download Qwen/Qwen2.5-7B-Instruct --local-dir $HOME/models/Qwen2.5-7B-Instruct + +export CUDA_VISIBLE_DEVICES=0,1,2,3 +bash recipe/sppo/run_qwen2.5-7b_rm.sh +``` + +Note that the installation would occasionally fail to install flash-attn. If this happens, you can install it manually by running: + +```bash +python3 -m uv pip install wheel +python3 -m uv pip install packaging +python3 -m uv pip install flash-attn --no-build-isolation --no-deps +``` + +## Acknowledgement + +We sincerely thank the contribution and guidance from: + +- [Yue Wu](https://yuewu.us/) +- [Chendong Wang](https://cdwang96.github.io/) +- [Yifan Zhang](https://github.com/yifanzhang-pro) +- [Yongan Xiang](https://github.com/BearBiscuit05) +- [Junrong Lin](https://github.com/ocss884) +- [Yuxuan Tong](https://github.com/tongyx361) +- [Guangming Shen](https://github.com/PeterSH6) +- [Biao He](https://www.linkedin.com/in/biao-he/) +- [Qingquan Song](https://qingquansong.github.io/) +- [Quanquan Gu](https://web.cs.ucla.edu/~qgu/) diff --git a/verl_0720_main/verl/docs/amd_tutorial/amd_build_dockerfile_page.rst b/verl_0720_main/verl/docs/amd_tutorial/amd_build_dockerfile_page.rst new file mode 100644 index 0000000000000000000000000000000000000000..096dad26e604e00e263b6a3a6364f38246accf49 --- /dev/null +++ b/verl_0720_main/verl/docs/amd_tutorial/amd_build_dockerfile_page.rst @@ -0,0 +1,796 @@ +Getting started with AMD (ROCM Kernel) +===================================================== + +Last updated: 07/06/2025. + +Author: `Yusheng Su `_ + +Setup +----- + +If you run on AMD GPUs (MI300) with ROCM platform, you cannot use the previous quickstart to run verl. You should follow the following steps to build a docker and set ``RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES`` or ``RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES`` when starting ray in verl's RLHF training. + + +docker/Dockerfile.rocm +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + FROM "rlsys/rocm-6.3.4-patch:rocm6.3.4-numa-patch_ubuntu-22.04" + + SHELL ["/bin/bash", "-ceuxo", "pipefail"] + + ENV MAX_JOBS=512 + + ENV PATH="/usr/local/python3.12/bin:$PATH" + RUN ln -sf /usr/bin/python3.12 /usr/bin/python && \ + ln -sf /usr/bin/pip3.12 /usr/bin/pip + + ############################################ + RUN apt-get update + RUN apt-get install -y pkg-config liblzma-dev + ############################################ + + ########################################### + ##########Install TransformerEngine######## + ########################################### + WORKDIR /workspace/ + # transformer-engine install + # https://github.com/ROCm/TransformerEngine + RUN rm -rf TransformerEngine + RUN git clone --recursive https://github.com/ROCm/TransformerEngine.git + WORKDIR /workspace/TransformerEngine + git checkout 236178e5 + # git checkout bb061ade + # git checkout 864405c + ENV NVTE_FRAMEWORK=pytorch + ENV NVTE_ROCM_ARCH=gfx942 + ENV NVTE_USE_HIPBLASLT=1 + ENV NVTE_USE_ROCM=1 + # export CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr:${CMAKE_PREFIX_PATH:-}" + ENV CMAKE_PREFIX_PATH="/opt/rocm:/opt/rocm/hip:/usr/local:/usr" + RUN MAX_JOBS=$(MAX_JOBS) pip install . -vvv + WORKDIR /workspace/ + ########################################### + ########################################### + ########################################### + + + + + + #################################################################################### + ################Install vllm - sglang require vllm 0.6.7 dependency################# + #################################################################################### + #### Require vllm 0.6.7 - checkout 113274a0 + WORKDIR /workspace/ + RUN rm -rf vllm + RUN pip uninstall -y vllm + # Refer to here (down-grade vllm to 0.6.3): https://docs.vllm.ai/en/v0.6.3/getting_started/amd-installation.html + RUN git clone https://github.com/ROCm/vllm.git + # git clone https://github.com/vllm-project/vllm.git + WORKDIR /workspace/vllm + RUN git checkout 113274a0 + ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + #ENV MAX_JOBS=512 + ENV MAX_JOBS=${MAX_JOBS} + RUN pip install "boto3>=1.26.0" + RUN pip install setuptools_scm + # will add src into py. You can delete the repo + RUN python3 setup.py install + WORKDIR /workspace/ + #################################################################################### + #################################################################################### + #################################################################################### + + + + ########################################### + ############For hack docker################ + ########################################### + RUN pip install setuptools==75.8.0 + ########################################### + ########################################### + ########################################### + + + + ########################################### + ############build sgalng################### + ########################################### + # Set environment variables + ENV BASE_DIR=/sgl-workspace + ENV BUILD_TYPE=all + ENV SGL_REPO=https://github.com/sgl-project/sglang + ENV SGL_BRANCH=v0.4.6.post5 + ENV TRITON_REPO=https://github.com/ROCm/triton.git + ENV TRITON_COMMIT=improve_fa_decode_3.0.0 + ENV AITER_REPO=https://github.com/ROCm/aiter.git + ENV AITER_COMMIT=v0.1.2 + # v0.1.2 version - commit id: 9d11f47 + # ENV AITER_COMMIT=9d11f47 + ENV HIP_FORCE_DEV_KERNARG=1 + ENV HSA_NO_SCRATCH_RECLAIM=1 + ENV SGLANG_SET_CPU_AFFINITY=1 + ENV SGLANG_ALLOW_OVERWRITE_LONGER_CONTEXT_LEN=1 + ENV NCCL_MIN_NCHANNELS=112 + ENV MOE_PADDING=1 + ENV VLLM_FP8_PADDING=1 + ENV VLLM_FP8_ACT_PADDING=1 + ENV VLLM_FP8_WEIGHT_PADDING=1 + ENV VLLM_FP8_REDUCE_CONV=1 + ENV TORCHINDUCTOR_MAX_AUTOTUNE=1 + ENV TORCHINDUCTOR_MAX_AUTOTUNE_POINTWISE=1 + ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" + ENV AMDGPU_TARGETS=gfx942 + ENV ROCM_ARCH=gfx942 + ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + # Switch to working directory + WORKDIR /sgl-workspace + # Clean and create directory + RUN rm -rf /sgl-workspace && mkdir -p /sgl-workspace + + # Clone and build sglang + RUN git clone ${SGL_REPO} \ + && cd sglang \ + && git checkout ${SGL_BRANCH} || echo "Using default branch" \ + && cd sgl-kernel \ + && rm -f pyproject.toml \ + && mv pyproject_rocm.toml pyproject.toml \ + && python setup_rocm.py install \ + && cd .. \ + && if [ "$BUILD_TYPE" = "srt" ]; then \ + python -m pip --no-cache-dir install -e "python[srt_hip]"; \ + else \ + python -m pip --no-cache-dir install -e "python[all_hip]"; \ + fi \ + && cd /sgl-workspace \ + && cp -r /sgl-workspace/sglang /sglang \ + && python -m pip cache purge + + # Install common Python packages + RUN pip install IPython orjson python-multipart torchao pybind11 + # Rebuild Triton + RUN pip uninstall -y triton || true \ + && git clone ${TRITON_REPO} \ + && cd triton \ + && git checkout ${TRITON_COMMIT} \ + && cd python \ + && python3 setup.py install \ + && cd /sgl-workspace + # ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942 --amdgpu-lower-module-lds-strategy=1" + # ENV HIPCC_COMPILE_FLAGS_APPEND="--offload-arch=gfx942" + + # Build aiter + #version: Commit 9d11f47 + # && git checkout ${AITER_COMMIT} \ + RUN pip uninstall -y aiter || true + RUN git clone ${AITER_REPO} \ + && cd aiter \ + && git checkout ${AITER_COMMIT} \ + && git submodule sync \ + && git submodule update --init --recursive \ + && PREBUILD_KERNELS=1 GPU_ARCHS=gfx942 python3 setup.py install \ + && cd /sgl-workspace + + # Copy MI300X config + RUN find /sgl-workspace/sglang/python/sglang/srt/layers/quantization/configs/ \ + /sgl-workspace/sglang/python/sglang/srt/layers/moe/fused_moe_triton/configs/ \ + -type f -name '*MI300X*' | \ + xargs -I {} sh -c 'vf_config=$(echo "$1" | sed "s/MI300X/MI300X_VF/"); cp "$1" "$vf_config"' -- {} + + # Environment setup complete. + RUN echo "Environment setup complete." + + WORKDIR /workspace/ + ########################################### + ########################################### + ########################################### + + + + + + + ########################################### + ###############vllm v0.8.5################# + ########################################### + WORKDIR /workspace/ + + ENV VLLM_TARGET_DEVICE=rocm + ENV ROCM_PATH=/opt/rocm + ENV SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev + # Find the repo path in: DockerFile/Dockerfile.rocm_yang + # RUN git clone https://github.com/RLFoundation/vllm-patch.git + RUN pip uninstall -y vllm || true + RUN rm -rf vllm-patch + RUN git clone https://github.com/RLFoundation/vllm-patch.git \ + && cd vllm-patch \ + && git checkout v0.8.5-sleep-numa \ + && rm -rf build/ dist/ *.egg-info \ + && ln -sf /opt/rocm/lib/libamdhip64.so /usr/lib/libamdhip64.so \ + && SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py install + # RUN SETUPTOOLS_SCM_PRETEND_VERSION=0.8.5.dev PYTORCH_ROCM_ARCH="gfx90a;gfx942" MAX_JOBS=${MAX_JOBS} python3 setup.py develop + WORKDIR /workspace/ + ########################################### + ########################################### + ########################################### + + + + + ######################################### + #### Install megatron-core############### + ######################################### + RUN pip uninstall -y megatron-core && \ + git clone https://github.com/yushengsu-thu/Megatron-LM-amd_version.git && \ + cd Megatron-LM-amd_version && \ + pip install -vvv -e . && \ + cd /workspace/ + ######################################### + ######################################### + ######################################### + + + + + ####################################### + ################apex################### + ####################################### + WORKDIR /workspace/ + RUN pip uninstall -y apex && \ + git clone git@github.com:ROCm/apex.git && \ + cd apex && \ + python setup.py install && \ + cd /workspace/ + ####################################### + ####################################### + ####################################### + + + ################################################################################ + ###########################Add torch_memory_saver############################### + ################################################################################ + # Set environment variables + ENV HIPCC_COMPILE_FLAGS_APPEND="--amdgpu-target=gfx90a;gfx942 -D__HIP_PLATFORM_AMD__" + ENV CFLAGS="-D__HIP_PLATFORM_AMD__" + ENV CXXFLAGS="-D__HIP_PLATFORM_AMD__" + RUN pip install "git+https://github.com/YangWang92/torch_memory_saver_numa.git@numa" + ################################################################################ + ################################################################################ + ################################################################################ + + + + ######################################## + ######Install ray####################### + ######################################## + # need to add this patch: https://github.com/ray-project/ray/pull/53531/files + RUN pip uninstall ray -y + RUN pip install "ray[data,train,tune,serve]>=2.47.0" + ######################################## + ######################################## + ######################################## + + + ########################################## + #######Install other dependencies######### + ########################################## + RUN pip install "tensordict==0.6.2" --no-deps && \ + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + torchdata \ + wandb \ + orjson \ + pybind11 + + WORKDIR /workspace/ + RUN git clone https://github.com/verl-project/verl.git && \ + cd verl && \ + pip install -e . + ########################################## + ########################################## + ########################################## + + WORKDIR /workspace/ + CMD ["/usr/bin/bash"] + + +Build the image: +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + docker docker/build -t verl-rocm . + +Run the container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Note: You can pull the docker from this DockerHub: [RLSys Foundation](https://hub.docker.com/u/yushengsuthu) +Pull the image: +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + docker pull rlsys/verl:verl-0.4.1_ubuntu-22.04_rocm6.3.4-numa-patch_vllm0.8.5_sglang0.4.6.post4 + + docker tag rlsys/verl:verl-0.4.1_ubuntu-22.04_rocm6.3.4-numa-patch_vllm0.8.5_sglang0.4.6.post4 verl-rocm:latest + +Run the container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + +Optional: Running without root and with user permissions +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +.. code-block:: bash + + docker run --rm -it \ + --device /dev/dri \ + --device /dev/kfd \ + -p 8265:8265 \ + --group-add video \ + --cap-add SYS_PTRACE \ + --security-opt seccomp=unconfined \ + --privileged \ + -v $HOME/.ssh:/root/.ssh \ + -v $HOME:$HOME \ + --shm-size 128G \ + -w $PWD \ + verl-rocm \ + /bin/bash + +(Optional): If you do not want to root mode and require assign yourself as the user +Please add ``-e HOST_UID=$(id -u)`` and ``-e HOST_GID=$(id -g)`` into the above docker launch script. + +Example +------- + +Due to to special setting in AMD (ROCM) torch, +1. If your ``ray>=2.45.0`` (default), you need to set ``RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES`` when starting ray in verl's RLHF training and add this [patch](https://github.com/ray-project/ray/pull/53531/files). +2. If your ``ray<2.45.0``, you need to set ``RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES`` when starting ray in verl's RLHF training. +Inference ``$ENGINE`` can be ``vllm`` or ``sglang``. We choose ``vllm`` as default in the following examples. + + + +PPO +~~~ + +.. code-block:: bash + + YOUR_PROJECT_NAME=r1-verl-ppo-upstream + YOUR_RUN_NAME=r1-training_ppo-upstream + # export HYDRA_FULL_ERROR=1 + + export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + + # [ray] < 2.45.0 + #export RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES=1 + + # [ray] >= 2.45.0 + export RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES=1 # Patch with https://github.com/ray-project/ray/pull/52794 + + GPUS_PER_NODE=8 + MODEL_PATH=Qwen/Qwen2.5-0.5B-Instruct + python3 examples/data_preprocess/gsm8k.py --local_save_dir data/gsm8k + python3 -c "import transformers; transformers.pipeline('text-generation', model='$MODEL_PATH')" + ENGINE=vllm #sglang + + PYTHONUNBUFFERED=1 python3 -m verl.trainer.main_ppo \ + data.train_files=data/gsm8k/train.parquet \ + data.val_files=data/gsm8k/test.parquet \ + data.train_batch_size=256 \ + data.val_batch_size=1312 \ + data.max_prompt_length=512 \ + data.max_response_length=256 \ + actor_rollout_ref.model.path=$MODEL_PATH \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=4 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.name=$ENGINE \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=4 \ + critic.optim.lr=1e-5 \ + critic.model.path=$MODEL_PATH \ + critic.ppo_micro_batch_size_per_gpu=4 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.logger=console \ + trainer.project_name=$YOUR_PROJECT_NAME \ + trainer.experiment_name=$YOUR_RUN_NAME \ + trainer.val_before_train=False \ + trainer.n_gpus_per_node=$GPUS_PER_NODE \ + trainer.nnodes=1 \ + trainer.save_freq=10 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 #2>&1 | tee verl_demo.log + +GRPO +~~~~ + +.. code-block:: bash + + YOUR_PROJECT_NAME=r1-verl-grpo-upstream + YOUR_RUN_NAME=r1-training_grpo-upstream + # export HYDRA_FULL_ERROR=1 + # export FSDP_VERBOSE=1 + + #export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + + # [ray] < 2.45.0 + #export RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES=1 + + # [ray] >= 2.45.0 + export RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES=1 # Patch with https://github.com/ray-project/ray/pull/52794 + + GPUS_PER_NODE=8 + MODEL_PATH=Qwen/Qwen2.5-0.5B-Instruct + # MODEL_PATH=Qwen/Qwen2-7B-Instruct + python3 examples/data_preprocess/gsm8k.py --local_save_dir data/gsm8k + python3 -c "import transformers; transformers.pipeline('text-generation', model='$MODEL_PATH')" + ENGINE=vllm #sglang + + python3 -m verl.trainer.main_ppo \ + algorithm.adv_estimator=grpo \ + data.train_files=data/gsm8k/train.parquet \ + data.val_files=data/gsm8k/test.parquet \ + data.train_batch_size=1024 \ + data.val_batch_size=1312 \ + data.max_prompt_length=512 \ + data.max_response_length=1024 \ + actor_rollout_ref.model.path=$MODEL_PATH \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.use_dynamic_bsz=True \ + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=24000 \ + actor_rollout_ref.actor.use_kl_loss=True \ + actor_rollout_ref.actor.kl_loss_coef=0.001 \ + actor_rollout_ref.actor.kl_loss_type=low_var_kl \ + actor_rollout_ref.model.enable_gradient_checkpointing=Flase \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=$ENGINE \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ + actor_rollout_ref.rollout.n=5 \ + actor_rollout_ref.ref.fsdp_config.param_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=console \ + trainer.project_name=$YOUR_PROJECT_NAME \ + trainer.experiment_name=$YOUR_RUN_NAME \ + trainer.n_gpus_per_node=$GPUS_PER_NODE \ + trainer.val_before_train=False \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 + + + +Multi-node training: slurm with Docker/Podman container +--------------------------------------------------------------------------------------- + +If you want to run multi-node training with slurm, you can use the following script. + +.. note:: + 1. You need to use ``podman`` or ``docker`` in the following script. We will release the apptainer script later. + 2. If you want to use ``podman``, you just replace ``docker`` with ``podman`` in the following script. + +The script includes the following steps: + +1. SLURM Configuration +2. Environment Setup +3. Docker/Podman Container Setup +4. Ray Cluster Initialization +5. Data Preprocessing +6. Model Setup +7. Training Launch + + +slurm_script.sh +~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + #!/bin/bash + + #SBATCH --job-name=verl-ray-on-slurm + #SBATCH --nodes=2 + #SBATCH --ntasks-per-node=2 + #SBATCH --mem=200G + #SBATCH --time=30-00:00:00 + #SBATCH --gpus-per-node=8 + #SBATCH --cpus-per-task=28 + #SBATCH --output=../verl_log/slurm-%j.out + #SBATCH --error=../verl_log/slurm-%j.err + #SBATCH --nodelist=gpu-[0,1] + + + # load necessary modules + ### Run this setup + # [Cluster]: Use docker + # docker pull docker.io/rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 + + + ########################################################################## + ###The following setting should be set in different project and cluster### + ########################################################################## + + ### Project + CONTAINER_NAME="multinode_verl_training" + IMG="verl.rocm" + DOCKERFILE="docker/Dockerfile.rocm" + # echo $PWD + verl_workdir="${HOME}/projects/verl_upstream" + export TRANSFORMERS_CACHE="${HOME}/.cache/huggingface" + export HF_HOME=$TRANSFORMERS_CACHE + + ### Cluster Network Setting + export NCCL_DEBUG=TRACE + export GPU_MAX_HW_QUEUES=2 + export TORCH_NCCL_HIGH_PRIORITY=1 + export NCCL_CHECKS_DISABLE=1 + # export NCCL_IB_HCA=rdma0,rdma1,rdma2,rdma3,rdma4,rdma5,rdma6,rdma7 + export NCCL_IB_HCA=mlx5_0,mlx5_1,mlx5_2,mlx5_3,mlx5_4,mlx5_5,mlx5_8,mlx5_9 + export NCCL_IB_GID_INDEX=3 + export NCCL_CROSS_NIC=0 + export CUDA_DEVICE_MAX_CONNECTIONS=1 + export NCCL_PROTO=Simple + export RCCL_MSCCL_ENABLE=0 + export TOKENIZERS_PARALLELISM=false + export HSA_NO_SCRATCH_RECLAIM=1 + ########################################################################## + + ## Assign using GPUs + export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + + ### For rocm and training script + # [ray] < 2.45.0 + #export RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES=1 + + # [ray] >= 2.45.0 + export RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES=1 # Patch with https://github.com/ray-project/ray/pull/52794 + + + # Build and launch the Docker container + srun bash -c " + # Exit on any error + set -e + + # Clean up dangling images (images with tag) + docker image prune -f + + # Need to pull the docker first + docker pull rlsys/verl:verl-0.4.1_ubuntu-22.04_rocm6.3.4-numa-patch_vllm0.8.5_sglang0.4.6.post4 + + if ! docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "${IMG}"; then + echo \"Building ${IMG} image...\" + docker build -f \"${DOCKERFILE}\" -t \"${IMG}\" . + else + echo \"${IMG} image already exists, skipping build\" + fi + + # Removing old container if exists + docker rm \"${CONTAINER_NAME}\" 2>/dev/null || true + + # Checking network devices + ibdev2netdev + + # Launch the docker + docker run --rm -d \ + -e HYDRA_FULL_ERROR=1 \ + -e RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES=1 \ + -e RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES=1 \ + -e NCCL_DEBUG=${NCCL_DEBUG} \ + -e GPU_MAX_HW_QUEUES=${GPU_MAX_HW_QUEUES} \ + -e TORCH_NCCL_HIGH_PRIORITY=${TORCH_NCCL_HIGH_PRIORITY} \ + -e NCCL_CHECKS_DISABLE=${NCCL_CHECKS_DISABLE} \ + -e NCCL_IB_HCA=${NCCL_IB_HCA} \ + -e NCCL_IB_GID_INDEX=${NCCL_IB_GID_INDEX} \ + -e NCCL_CROSS_NIC=${NCCL_CROSS_NIC} \ + -e CUDA_DEVICE_MAX_CONNECTIONS=${CUDA_DEVICE_MAX_CONNECTIONS} \ + -e NCCL_PROTO=${NCCL_PROTO} \ + -e RCCL_MSCCL_ENABLE=${RCCL_MSCCL_ENABLE} \ + -e TOKENIZERS_PARALLELISM=${TOKENIZERS_PARALLELISM} \ + -e HSA_NO_SCRATCH_RECLAIM=${HSA_NO_SCRATCH_RECLAIM} \ + -e TRANSFORMERS_CACHE=${TRANSFORMERS_CACHE} \ + -e HF_HOME=${HF_HOME} \ + --network host \ + --device /dev/dri \ + --device /dev/kfd \ + --device /dev/infiniband \ + --group-add video \ + --cap-add SYS_PTRACE \ + --security-opt seccomp=unconfined \ + --privileged \ + -v \${HOME}:\${HOME} \ + -v \${HOME}/.ssh:/root/.ssh \ + -w "${verl_workdir}" \ + --shm-size 128G \ + --name \"${CONTAINER_NAME}\" \ + \"${IMG}\" \ + tail -f /dev/null + + echo \"Container setup completed\" + " + # (Optional): If you do not want to root mode and require assign yuorself as the user + # Please add `-e HOST_UID=$(id -u)` and `-e HOST_GID=$(id -g)` into the above docker launch script. + + + + + + ### Ray launch the nodes before training + + # Getting the node names + nodes_array=($(scontrol show hostnames "$SLURM_JOB_NODELIST" | tr '\n' ' ')) + + head_node=${nodes_array[0]} + head_node_ip=$(srun --nodes=1 --ntasks=1 -w "$head_node" hostname --ip-address) + + # if we detect a space character in the head node IP, we'll + # convert it to an ipv4 address. This step is optional. + if [[ "$head_node_ip" == *" "* ]]; then + IFS=' ' read -ra ADDR <<<"$head_node_ip" + if [[ ${#ADDR[0]} -gt 16 ]]; then + head_node_ip=${ADDR[1]} + else + head_node_ip=${ADDR[0]} + fi + echo "IPV6 address detected. We split the IPV4 address as $head_node_ip" + fi + + port=6379 + ip_head=$head_node_ip:$port + export ip_head + echo "IP Head: $ip_head" + + # make sure we set environment variables before Ray initialization + + # Print out all env variables + printenv + + echo "Starting HEAD at $head_node" + srun --nodes=1 --ntasks=1 -w "$head_node" \ + docker exec "${CONTAINER_NAME}" \ + ray start --head --node-ip-address="$head_node_ip" --port=$port \ + --dashboard-port=8266 \ + --num-cpus "${SLURM_CPUS_PER_TASK}" --num-gpus "${SLURM_GPUS_PER_NODE}" --block & + # optional, though may be useful in certain versions of Ray < 1.0. + sleep 10 + + # number of nodes other than the head node + worker_num=$((SLURM_JOB_NUM_NODES - 1)) + + for ((i = 1; i <= worker_num; i++)); do + node_i=${nodes_array[$i]} + echo "Debug: Starting worker on node_i = ${node_i}" + if [ -z "$node_i" ]; then + echo "Error: Empty node name for worker $i" + continue + fi + echo "Starting WORKER $i at $node_i" + srun --nodes=1 --ntasks=1 -w "$node_i" \ + docker exec "${CONTAINER_NAME}" \ + ray start --address "$ip_head" --num-cpus "${SLURM_CPUS_PER_TASK}" --num-gpus "${SLURM_GPUS_PER_NODE}" --block & + sleep 5 + done + + + + + # Ray initlization test (See whether any error in the above execution) + echo "Testing Ray initialization in the slurm nodes..." + docker exec "${CONTAINER_NAME}" python3 -c ' + import ray + try: + ray.init(address="auto") + print("\n=== Ray Cluster Status ===") + print(f"Number of nodes: {len(ray.nodes())}") + for node in ray.nodes(): + print("Node: {}, Status: {}".format(node["NodeManagerHostname"], node["Alive"])) + # print(f"Node: {node}") + ray.shutdown() + print("Ray initialization successful!") + except Exception as e: + print(f"Ray initialization failed: {str(e)}") + ' + echo "=== Ray test completed ===" + ###### + + + + # Run data preprocessing + + echo "Starting data preprocessing..." + docker exec "${CONTAINER_NAME}" \ + python3 "examples/data_preprocess/gsm8k.py" "--local_save_dir" "../data/gsm8k" + + echo "Starting data preprocessing..." + docker exec "${CONTAINER_NAME}" \ + python3 "examples/data_preprocess/math_dataset.py" "--local_dir" "../data/math" + + train_files="../data/gsm8k/train.parquet" + val_files="../data/gsm8k/test.parquet" + + # Download and test model + echo "Loading model..." + docker exec "${CONTAINER_NAME}" \ + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2-7B-Instruct')" + MODEL_PATH="Qwen/Qwen2-7B-Instruct" + + # Set model path after pipeline test + MODEL_PATH="Qwen/Qwen2.5-0.5B-Instruct" + + echo "== Data and model loading Done ==" + + echo "Start to train..." + + docker exec "${CONTAINER_NAME}" \ + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2-7B-Instruct')" + MODEL_PATH="Qwen/Qwen2-7B-Instruct" + + + PYTHONUNBUFFERED=1 srun --overlap --nodes=${SLURM_NNODES} --ntasks=1 -w "$head_node" \ + docker exec "${CONTAINER_NAME}" \ + python3 -m verl.trainer.main_ppo \ + data.train_files=$train_files \ + data.val_files=$val_files \ + data.train_batch_size=1024 \ + data.max_prompt_length=1024 \ + data.max_response_length=1024 \ + actor_rollout_ref.model.path=$MODEL_PATH \ + actor_rollout_ref.model.enable_gradient_checkpointing=False \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.model.enable_gradient_checkpointing=True \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.9 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.use_remove_padding=True \ + critic.model.path=$MODEL_PATH \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size_per_gpu=8 \ + critic.fsdp.param_offload=False \ + critic.fsdp.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.0001 \ + trainer.critic_warmup=0 \ + trainer.logger='["console","wandb"]' \ + trainer.project_name='verl_example' \ + trainer.experiment_name='Qwen2.5-32B-Instruct_function_rm' \ + trainer.n_gpus_per_node=${SLURM_GPUS_PER_NODE} \ + trainer.val_before_train=False \ + trainer.nnodes=${SLURM_NNODES} \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 + + +Run slurm_script.sh +~~~~~~~~~~~~~~~~~~~~ +Just sbatch your slurm_script.sh + +.. code-block:: bash + + sbatch slurm_script.sh + diff --git a/verl_0720_main/verl/docs/amd_tutorial/amd_quick_start.rst b/verl_0720_main/verl/docs/amd_tutorial/amd_quick_start.rst new file mode 100644 index 0000000000000000000000000000000000000000..48dcd1d5b3c9ec271c5df5384f8e55d0c9c2a91c --- /dev/null +++ b/verl_0720_main/verl/docs/amd_tutorial/amd_quick_start.rst @@ -0,0 +1,136 @@ +Getting started with AMD ROCm +========================================= + +Last updated: 05/17/2026. + +Author: `Mingjie Lu `_, `Xiaohong Kou `_, `Fuwei Yang `_ + +Overview +-------- + +This document is a quick-start tutorial for running VeRL on AMD ROCm. +It provides a production-style bring-up flow for container startup, environment +verification, and training examples. + +Current software and hardware scope: + +- Runtime modes: fully supports **Fully Async** and **Colocate**. +- Inference engine: **vLLM** validated; **SGLang** support is ongoing. +- Trainer backends: **FSDP**, **FSDP2** and **Megatron**. +- GPU targets: + + - MI300X / MI325X (``gfx942``) + - MI355X (``gfx950``) + +Software Baseline +----------------- + +Use the following prebuilt image for tutorial and validation: + +- ``amdagi/verl-dev:rocm7.0.2_56_te2.10_vllm0.20_py312`` + +The Docker build recipe remains unchanged: + +- `docker/rocm/Dockerfile.rocm `_ + +Host Prerequisites +------------------ + +Before launching the container, ensure: + +1. AMD ROCm 7.0.2 host driver stack is installed and healthy. +2. Docker has access to ``/dev/kfd`` and ``/dev/dri``. +3. Dataset and model storage paths are ready. + +Launch Container +---------------- + +.. code-block:: bash + + NAME=verl_release + DOCKER=amdagi/verl-dev:rocm7.0.2_56_te2.10_vllm0.20_py312 + + docker pull $DOCKER + + docker run -it --name $NAME --device /dev/kfd --device /dev/dri \ + --privileged --network=host \ + --group-add video --cap-add=SYS_PTRACE --security-opt seccomp=unconfined \ + --shm-size=2048g \ + --ulimit memlock=-1 --ulimit stack=67108864 \ + -w /workspace \ + $DOCKER \ + /bin/bash + +Environment Check (Inside Container) +------------------------------------ + +.. code-block:: bash + + # ROCm and visible GPU targets + rocminfo | grep -E "gfx942|gfx950" || true + + # PyTorch + ROCm sanity check + python - <<'PY' + import torch + print("torch:", torch.__version__) + print("rocm :", torch.version.hip) + print("cuda_available:", torch.cuda.is_available()) + if torch.cuda.is_available(): + print("gpu_count:", torch.cuda.device_count()) + print("device_0:", torch.cuda.get_device_name(0)) + PY + +Feature Support Matrix +---------------------- + +.. list-table:: Current support status + :header-rows: 1 + + * - Category + - Status + - Notes + * - Runtime mode + - Fully supported + - Fully Async and Colocate are production-ready + * - Inference engine + - vLLM validated + - SGLang integration is ongoing + * - Trainer backend + - Fully supported + - FSDP, Megatron + * - Hardware + - Fully supported + - MI300X / MI325X (gfx942), MI355X (gfx950) + +Example Workflow +---------------- + +1) Colocate mode + FSDP (GRPO, Qwen3-8B) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +For Qwen3-8B FSDP training, enable both parameter and optimizer offload to avoid OOM. + +.. code-block:: bash + + # Configure these in your launch script or Hydra overrides: + # actor_rollout_ref.actor.fsdp_config.param_offload=True + # actor_rollout_ref.actor.fsdp_config.optimizer_offload=True + bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh + +2) Colocate mode + Megatron (GRPO, Qwen3.5-35B) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + bash examples/grpo_trainer/run_qwen3_5-35b-megatron.sh + +3) Fully Async mode +~~~~~~~~~~~~~~~~~~~ + +``RAY_EXPERIMENTAL_NOSET_ROCR_VISIBLE_DEVICES`` and +``RAY_EXPERIMENTAL_NOSET_HIP_VISIBLE_DEVICES`` are no longer required in this release. + +.. code-block:: bash + + # For qwen2.5-math-7b, update max_position_embeddings to 32768 in config.json after model download. + bash verl/experimental/fully_async_policy/shell/dapo_7b_math_fsdp2_4_4.sh diff --git a/verl_0720_main/verl/docs/amd_tutorial/amd_vllm_page.rst b/verl_0720_main/verl/docs/amd_tutorial/amd_vllm_page.rst new file mode 100644 index 0000000000000000000000000000000000000000..7c230acab8792406e0ecb82d1a4fb417ba027a2e --- /dev/null +++ b/verl_0720_main/verl/docs/amd_tutorial/amd_vllm_page.rst @@ -0,0 +1,41 @@ +verl performance tuning for AMD (ROCm Kernel) +===================================================== + +Last updated: 11/13/2025. + +Author: `Yang Wang `_, `Songlin Jiang `_ + +Use vLLM Sleep Mode for AMD MI3xx series GPUs +-------------------------------------------------------------- + +By default, verl requires vLLM to enable sleep mode, which allows vLLM to offload GPU memory to CPU memory after rollout. This feature has been merged into the main branch of vLLM for version later than 0.11.0. + +For now, you can use the vLLM main branch and build it from the source code, or you can directly install vLLM from the pre-built ROCm wheels for vLLM version later than 0.11.0 when it's available. + +1. Clone the vLLM repository and build it with the following commands: + +.. code-block:: bash + + git clone https://github.com/vllm-project/vllm.git + cd vllm + git reset --hard 4ca5cd5740c0cd7788cdfa8b7ec6a27335607a48 # You can also use a later commit as you wish + python -m pip install -r requirements/rocm.txt + VLLM_TARGET_DEVICE=rocm ROCM_PATH=/opt/rocm/ python3 setup.py develop + +2. Additionally, we recommend you to use the ROCm version later than or equal to ROCm 7.0. + +After the upgrade, you can verify whether sleep mode is working by trying out `these scripts `_. + +If sleep mode is working, you should see the memory usage reduce after sleep. + +After applying the vLLM patch and completing the installation, you can enable sleep mode in verl to reduce memory overhead. This allows verl to offload unused GPU memory during rollout, significantly lowering the memory footprint during long-context training or multi-node reinforcement learning. + + +Enable CUDA Graph and Bypass ROCm-related issues +-------------------------------------------------------------- + +Due to potential issues with CUDA graph capture in ROCm, we've found that vLLM's CUDA graph feature cannot be enabled on multiple nodes in verl on AMD platforms with vLLM V1 mode. This leads to significantly slower rollout performance. + +Our investigation shows that ROCm may trigger an unexpected crash when attempting to capture large batches with CUDA graph. One workaround is to set ``actor_rollout_ref.rollout.cudagraph_capture_sizes`` to values such as ``[1, 2, 4, 8, 16, 32, 64]`` (change depending on your GPU memory size). + +Then, you can choose to enable CUDA graph by setting ``actor_rollout_ref.rollout.enforce_eager`` to ``False`` in your verl configuration file. diff --git a/verl_0720_main/verl/docs/amd_tutorial/index.rst b/verl_0720_main/verl/docs/amd_tutorial/index.rst new file mode 100644 index 0000000000000000000000000000000000000000..ed9a492b6fcfa4ba71c6a1b90f1dce452e16132b --- /dev/null +++ b/verl_0720_main/verl/docs/amd_tutorial/index.rst @@ -0,0 +1,12 @@ +AMD (ROCm) Tutorial +===================== + +Last updated: 06/05/2026. + +.. toctree:: + :maxdepth: 1 + :caption: Getting Started + + amd_quick_start.rst + amd_vllm_page.rst + amd_build_dockerfile_page.rst \ No newline at end of file diff --git a/verl_0720_main/verl/docs/api/data.rst b/verl_0720_main/verl/docs/api/data.rst new file mode 100644 index 0000000000000000000000000000000000000000..5baa5b51bfdb79f6ead72f1f46141720248bd813 --- /dev/null +++ b/verl_0720_main/verl/docs/api/data.rst @@ -0,0 +1,61 @@ +Data interface +========================= + +Last updated: 05/19/2025 (API docstrings are auto-generated). + +DataProto is the interface for data exchange. + +The :class:`verl.DataProto` class contains two key members: + +- batch: a :class:`tensordict.TensorDict` object for the actual data +- meta_info: a :class:`Dict` with additional meta information + +TensorDict +~~~~~~~~~~~~ + +:attr:`DataProto.batch` is built on top of :class:`tensordict`, a project in the PyTorch ecosystem. +A TensorDict is a dict-like container for tensors. To instantiate a TensorDict, you must specify key-value pairs as well as the batch size. + +.. code-block:: python + + >>> import torch + >>> from tensordict import TensorDict + >>> tensordict = TensorDict({"zeros": torch.zeros(2, 3, 4), "ones": torch.ones(2, 3, 5)}, batch_size=[2,]) + >>> tensordict["twos"] = 2 * torch.ones(2, 5, 6) + >>> zeros = tensordict["zeros"] + >>> tensordict + TensorDict( + fields={ + ones: Tensor(shape=torch.Size([2, 3, 5]), device=cpu, dtype=torch.float32, is_shared=False), + twos: Tensor(shape=torch.Size([2, 5, 6]), device=cpu, dtype=torch.float32, is_shared=False), + zeros: Tensor(shape=torch.Size([2, 3, 4]), device=cpu, dtype=torch.float32, is_shared=False)}, + batch_size=torch.Size([2]), + device=None, + is_shared=False) + +One can also index a tensordict along its batch_size. The contents of the TensorDict can be manipulated collectively as well. + +.. code-block:: python + + >>> tensordict[..., :1] + TensorDict( + fields={ + ones: Tensor(shape=torch.Size([1, 3, 5]), device=cpu, dtype=torch.float32, is_shared=False), + twos: Tensor(shape=torch.Size([1, 5, 6]), device=cpu, dtype=torch.float32, is_shared=False), + zeros: Tensor(shape=torch.Size([1, 3, 4]), device=cpu, dtype=torch.float32, is_shared=False)}, + batch_size=torch.Size([1]), + device=None, + is_shared=False) + >>> tensordict = tensordict.to("cuda:0") + >>> tensordict = tensordict.reshape(6) + +For more about :class:`tensordict.TensorDict` usage, see the official tensordict_ documentation. + +.. _tensordict: https://pytorch.org/tensordict/stable/overview.html + + +Core APIs +~~~~~~~~~~~~~~~~~ + +.. autoclass:: verl.DataProto + :members: to, select, union, make_iterator, concat diff --git a/verl_0720_main/verl/docs/api/single_controller.rst b/verl_0720_main/verl/docs/api/single_controller.rst new file mode 100644 index 0000000000000000000000000000000000000000..44ea366ffe4b12ce5293821877ce70a0073f2152 --- /dev/null +++ b/verl_0720_main/verl/docs/api/single_controller.rst @@ -0,0 +1,30 @@ +Single Controller interface +============================ + +Last updated: 05/27/2025 (API docstrings are auto-generated). + +The Single Controller provides a unified interface for managing distributed workers +using Ray or other backends and executing functions across them. +It simplifies the process of dispatching tasks and collecting results, particularly +when dealing with data parallelism or model parallelism. + + +Core APIs +~~~~~~~~~~~~~~~~~ + +.. autoclass:: verl.single_controller.Worker + :members: __init__, __new__, get_master_addr_port, get_cuda_visible_devices, world_size, rank + +.. autoclass:: verl.single_controller.WorkerGroup + :members: __init__, world_size + +.. autoclass:: verl.single_controller.ClassWithInitArgs + :members: __init__, __call__ + +.. autoclass:: verl.single_controller.ResourcePool + :members: __init__, world_size, local_world_size_list, local_rank_list + +.. autoclass:: verl.single_controller.ray.RayWorkerGroup + :members: __init__ + +.. autofunction:: verl.single_controller.ray.create_colocated_worker_cls \ No newline at end of file diff --git a/verl_0720_main/verl/docs/api/trainer.rst b/verl_0720_main/verl/docs/api/trainer.rst new file mode 100644 index 0000000000000000000000000000000000000000..abfa51f01a31606f436a95fde13770577b9ab540 --- /dev/null +++ b/verl_0720_main/verl/docs/api/trainer.rst @@ -0,0 +1,31 @@ +Trainer Interface +================================ + +Last updated: 06/08/2025 (API docstrings are auto-generated). + +Trainers drive the training loop. Introducing new trainer classes in case of new training paradiam is encouraged. + +.. autosummary:: + :nosignatures: + + verl.trainer.ppo.ray_trainer.RayPPOTrainer + + +Core APIs +~~~~~~~~~~~~~~~~~ + +.. autoclass:: verl.trainer.ppo.ray_trainer.RayPPOTrainer + :members: __init__, init_workers, fit + +.. automodule:: verl.utils.tokenizer + :members: hf_tokenizer + +.. automodule:: verl.trainer.ppo.core_algos + :members: agg_loss, kl_penalty, compute_policy_loss, kl_penalty + +.. automodule:: verl.trainer.ppo.reward + :members: load_reward_manager, compute_reward, compute_reward_async + +.. autoclass:: verl.workers.reward_manager.NaiveRewardManager + +.. autoclass:: verl.workers.reward_manager.DAPORewardManager diff --git a/verl_0720_main/verl/docs/api/utils.rst b/verl_0720_main/verl/docs/api/utils.rst new file mode 100644 index 0000000000000000000000000000000000000000..e15e3a5a32bdbb129a25d93b12e751385caa30b5 --- /dev/null +++ b/verl_0720_main/verl/docs/api/utils.rst @@ -0,0 +1,76 @@ +Utilities +============ + +Last updated: 05/19/2025 (API docstrings are auto-generated). + +This section documents the utility functions and classes in the VERL library. + +Python Functional Utilities +------------------------------ + +.. automodule:: verl.utils.py_functional + :members: append_to_dict + +File System Utilities +------------------------ + +.. automodule:: verl.utils.fs + :members: copy_to_local + +Tracking Utilities +--------------------- + +.. automodule:: verl.utils.tracking + :members: Tracking + +Metrics Utilities +--------------------- + +.. automodule:: verl.utils.metric + :members: reduce_metrics + +Checkpoint Management +------------------------ + +.. automodule:: verl.utils.checkpoint.checkpoint_manager + :members: find_latest_ckpt_path + +.. automodule:: verl.utils.checkpoint.fsdp_checkpoint_manager + :members: FSDPCheckpointManager + +Dataset Utilities +--------------------- + +.. automodule:: verl.utils.dataset.rl_dataset + :members: RLHFDataset, collate_fn + +Torch Functional Utilities +----------------------------- + +.. automodule:: verl.utils.torch_functional + :members: get_constant_schedule_with_warmup, masked_whiten, masked_mean, logprobs_from_logits + +Sequence Length Balancing +---------------------------- + +.. automodule:: verl.utils.seqlen_balancing + :members: get_reverse_idx, rearrange_micro_batches + +Ulysses Utilities +-------------------- + +.. automodule:: verl.utils.ulysses + :members: gather_outputs_and_unpad, ulysses_pad_and_slice_inputs + +FSDP Utilities +------------------ + +.. automodule:: verl.utils.fsdp_utils + :members: get_fsdp_wrap_policy, get_init_weight_context_manager, init_fn, load_fsdp_model_to_gpu, load_fsdp_optimizer, offload_fsdp_model_to_cpu, offload_fsdp_optimizer, + +Debug Utilities +------------------- + +.. automodule:: verl.utils.profiler + :members: log_gpu_memory_usage, GPUMemoryLogger + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/README.md b/verl_0720_main/verl/docs/ascend_tutorial/README.md new file mode 100644 index 0000000000000000000000000000000000000000..944bdb0975606343c605f5070deaf4a22cea72ff --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/README.md @@ -0,0 +1,69 @@ +# Ascend Tutorial +## 简介 + +昇腾全面支持 verl 使用与开发,本文档全面介绍了如何在华为昇腾芯片 NPU 上使用 verl。 + +Last updated: 05/14/2026. + +## 目录结构 + +``` +ascend_tutorial/ +├── get_start/ # 快速入门指南 +├── feature_support/ # 特性支持说明 +├── model_support/ # 模型支持说明 +├── dev_guide/ # 开发指南 +├── faq/ # 常见问题解答 +└── contribution_guide/ # 社区贡献指南 +``` +## 最新消息 +- [verl-ascend-recipe 建仓](https://github.com/verl-project/verl-ascend-recipe) - 新增昇腾recipe +- [verl on Ascend 2026Q2 roadmap](https://github.com/verl-project/verl/issues/5526) - 2026Q2 roadmap 已发布 + +## 快速开始 +- [Docker 构建使用指南](./get_start/dockerfile_build_guidance.rst) - 构建并使用昇腾环境的 Docker 镜像 +- [自定义环境安装](./get_start/install_guidance.rst) - 在昇腾 NPU 上自定义安装 verl +- [快速上手](./get_start/quick_start.rst) - 快速上手在昇腾 NPU 上运行 verl + +## 特性支持说明 + +- [verl特性支持](./dev_guide/model_dev/parameter_and_metrics.md) - 支持的verl框架特性/参数列表 +- [NPU特性支持](./feature_support/npu_advance_features.md) - NPU相关常用特性/环境变量说明 + +## 模型支持说明 + +- [模型与算法支持说明](./model_support/model_and_algorithm_support.md) - 支持的模型/算法列表 +- [最佳实践示例](./model_support/examples) - 最佳实践与模型部署示例 + + +## 开发指南 + +- [模型开发](./dev_guide/model_dev) + - [模型迁移](./dev_guide/model_dev/transfer_to_npu_guide.md) - 模型迁移指南 + - [训练参数与指标](./dev_guide/model_dev/parameter_and_metrics.md) - 训练参数与指标 + - [模型评测](./dev_guide/model_dev/evaluation.md) - 模型评测指南 +- [精度调试](./dev_guide/precision_analysis) + - [精度分析](./dev_guide/precision_analysis/precision_alignment_zh.md) - 精度对齐指南 + - [精度调试器](./dev_guide/precision_analysis/precision_debugger_zh.md) - 精度问题排查工具 +- [性能调优](./dev_guide/performance) + - [性能分析](./dev_guide/performance/ascend_performance_analysis_guide.md) - 性能分析指南 + - [性能调优](./dev_guide/performance/perf_tuning_on_ascend.rst) - 性能调优指南 + - [profiling采集](./dev_guide/performance/ascend_profiling_zh.rst) - profiling 工具使用指南 + + +## 支持与反馈 + +如果您在使用过程中遇到问题,欢迎通过以下方式获取帮助: + +1. 查看 [FAQ](./faq/faq.rst) +2. 在 GitHub Issues 中提交问题 +3. 联系昇腾技术支持 + +## 贡献指南 +- [verl 社区贡献](../contributing) - verl 社区贡献指南 +- [昇腾 CI 指南](./contribution_guide/ascend_ci_guide_zh.rst) - 昇腾环境 CI 配置与测试 + +## 相关资源 + +- [verl 官方文档](https://verl.readthedocs.io/) +- [昇腾开发者社区](https://www.hiascend.com/) diff --git a/verl_0720_main/verl/docs/ascend_tutorial/contribution_guide/ascend_ci_guide_zh.rst b/verl_0720_main/verl/docs/ascend_tutorial/contribution_guide/ascend_ci_guide_zh.rst new file mode 100644 index 0000000000000000000000000000000000000000..cfe665a4ebc0bd037686f966b76db10182671d4a --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/contribution_guide/ascend_ci_guide_zh.rst @@ -0,0 +1,191 @@ +NPU-CI 添加指导 +=========== + +Last updated: 02/02/2026. + +我们在 verl 上提供基于华为昇腾设备的CI用例添加指导。 + +verl 仓库使用 GitHub Actions 作为 CI 平台,通过分层测试架构保障代码质量与系统稳定性。 +NPU 相关的工作流主要包括: + +* ``npu_unit_test.yml``:运行单元测试。 +* 以 ``_ascend.yml`` 结尾的文件:运行针对 Ascend NPU 的端到端测试或专项测试。 + +添加新用例指南 +----------------------------------- + +1. 数据集与权重 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +流水机器上的权重与绝对路径: + ++---------------------------------------+-------------------------------------------------------------------+ +| 模型名称 | 绝对路径 | ++=======================================+===================================================================+ +| Qwen2.5-0.5B | ``${HOME}/.cache/models/Qwen/Qwen2.5-0.5B`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen2.5-0.5B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen2.5-0.5B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen2.5-1.5B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen2.5-1.5B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen2.5-7B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen2.5-7B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen2.5-VL-3B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen2.5-VL-3B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-0.6B | ``${HOME}/.cache/models/Qwen/Qwen3-0.6B`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-8B | ``${HOME}/.cache/models/Qwen/Qwen3-8B`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-8B-Base | ``${HOME}/.cache/models/Qwen/Qwen3-8B-Base`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-30B-A3B-Instruct-2507 | ``${HOME}/.cache/models/Qwen/Qwen3-30B-A3B-Instruct-2507`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-32B | ``${HOME}/.cache/models/Qwen/Qwen3-32B`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-VL-2B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen3-VL-2B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-VL-4B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen3-VL-4B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-4B-Instruct-2507 | ``${HOME}/.cache/models/Qwen/Qwen3-4B-Instruct-2507`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3-VL-8B-Instruct | ``${HOME}/.cache/models/Qwen/Qwen3-VL-8B-Instruct`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Skywork-Reward-V2-Llama-3.2-1B | ``${HOME}/.cache/models/Skywork/Skywork-Reward-V2-Llama-3.2-1B`` | ++---------------------------------------+-------------------------------------------------------------------+ +| Qwen3.5-2B | ``${HOME}/.cache/models/Qwen/Qwen3.5-2B`` | ++---------------------------------------+-------------------------------------------------------------------+ + +流水机器上的数据集与绝对路径: + ++--------------+---------------------------------------------------+ +| 数据集名称 | 绝对路径 | ++==============+===================================================+ +| gsm8k | ``${HOME}/.cache/datasets/openai/gsm8k`` | ++--------------+---------------------------------------------------+ +| geo3k | ``${HOME}/.cache/datasets/hiyouga/geometry3k`` | ++--------------+---------------------------------------------------+ + +**Note** + + ${HOME}是root + + GPU用例中权重在~/models/路径下,如需适配可以用软链接,``ln -s /root/.cache/models ~/models`` + + 以下为原始数据集,请按需进行数据处理,示例如下。 + + ``python examples/data_preprocess/gsm8k_multiturn_sft.py --local_dataset_path ${HOME}/.cache/datasets/openai/gsm8k`` + + +2. 工作流 YAML 模板 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +如需新增一个工作流,可参考以下模板创建 ``.github/workflows/your_yml_ascend.yml`` 文件。 + +主要修改部分包括: + +* 工作流名称(``name``) +* 触发条件(``on``) +* 运行环境(``runs-on``) +* 容器镜像(``container.image``) +* 具体执行步骤(``jobs..steps``) + +.. code-block:: yaml + :linenos: + + name: your_yml_ascend # 工作流唯一标识 + # 触发条件配置 + on: + push: + branches: + - main + - v0.* + pull_request: + branches: + - main + paths: + - ".github/workflows/your_yml_ascend.yml" # 必须包含此工作流文件路径 + - "path/to/affected_files" # 需监控的相关代码路径 + + # 并发控制策略 + concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} # 仅非main分支取消进行中的任务 + + permissions: + contents: read # 最小权限原则 + + jobs: + your_job_name: # 任务唯一标识 + if: github.repository_owner == 'verl-project' # 仅在主仓库运行 + runs-on: linux-aarch64-a2-4 # 硬件规格:a2实例,4卡NPU + timeout-minutes: 60 # 任务超时阈值(分钟) + container: + #运行镜像 该示例为vllm的镜像 + image: swr.ap-southeast-1.myhuaweicloud.com/base_image/ascend-ci/verl/verl:latest-cann9.0.0-torch_npu2.9.0post2-910b-ubuntu22.04-py3.11-vllm + options: >- + --shm-size 16g # 共享内存配置 + env: + HF_ENDPOINT: "https://hf-mirror.com" + HF_HUB_ENABLE_HF_TRANSFER: "0" + steps: + - name: Check npu and CANN info + run: | + cat /usr/local/Ascend/ascend-toolkit/latest/"$(uname -i)"-linux/ascend_toolkit_install.info + npu-smi info + - name: Check initial pip list from image + run: pip list + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + clean: true + - name: Install dependencies + run: | + pip install --no-deps -e . + - name: Verify environment + run: pip list + # 以下为具体测试步骤(根据需求定制) + - name: Preprocess dataset + run: python examples/data_preprocess/your_script.py --local_dataset_path ${HOME}/.cache/datasets/your_dataset + - name: Execute NPU test + run: | + ray stop --force + bash tests/special_npu/your_test_script.sh + +**Note** + + + ${HOME}/.cache/文件夹内一旦添加新内容,不会因CI跑完容器销毁而删除,请避免往该文件夹添加内容。 + + +3. 添加单元测试 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +步骤: + +(1) 在 ``tests/`` 目录下创建或修改单元测试文件(例如 ``test_xxx.py``)。 +(2) 若测试文件路径未被 ``npu_unit_test.yml`` 中的 ``--ignore-glob`` 规则排除,则会在以下命令中自动执行: + + .. code-block:: yaml + + pytest -s -x --ignore-glob="xxx" --ignore-glob="xxx" tests/ + +(3) 若测试路径在 ``--ignore-glob`` 排除范围内,需在 ``npu_unit_test.yml`` 中新增一个 step 来显式运行该测试。 +(4) 如新增一批相关用例,建议单独创建专门的工作流文件以保持清晰。 + +4. 添加端到端测试脚本 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +步骤: + +(1) 在 ``tests/special_npu/`` 目录下创建端到端测试脚本。 +(2) 在 ``.github/workflows/`` 目录中找到功能最接近的以 ``_ascend.yml`` 结尾的工作流文件,在其中添加一个 step 调用该脚本。 +(3) 若测试场景独立或较复杂,可考虑单独创建新的工作流文件。 + +5. 测试策略建议 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +* **单元测试**:覆盖核心函数、类与方法,确保逻辑正确。 +* **集成/端到端测试**:覆盖典型训练、推理 pipeline,验证多模块协同与硬件适配。 +* **资源管理**:一个workflow里的多个job为并行运行,请合理设置超时时间,避免任务长时间挂起,请控制单个 job 的运行时间在 40min 以内。 + +通过以上步骤,可系统化地为 verl 仓库添加 NPU 相关的自动化测试,确保代码变更在合并前经过充分验证。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/evaluation.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/evaluation.md new file mode 100644 index 0000000000000000000000000000000000000000..675f0f0ed04ba7e4500764bdcdba69c97af0f19f --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/evaluation.md @@ -0,0 +1,105 @@ +# 模型评测 + +Last updated: 07/14/2026. + +不同模型步骤一致,仅以Qwen3-30B为例列举 + +我们通过 AISBench 评估模型,该工具支持vllm/sglang多种推理后端的评估 + +## 1.安装方法 + +~~~bash +git clone https://gitee.com/aisbench/benchmark.git +cd benchmark +pip install -e . +~~~ + + +## 2.下载评估数据集 + +~~~bash +cd path/to/benchmark/ais_bench/datasets +wget https://opencompass.oss-cn-shanghai.aliyuncs.com/datasets/data/math.zip +unzip math.zip +rm math.zip +~~~ + +## 3.权重转换 + +当前verl已经支持mbridge直接保存hf格式模型权重,无需转换即可使用。 + +如果模型权重不是hf格式,需要先转换为hf格式,再进行评估。 + +此处参照verl原生[转换方法](../../../../docs/advance/checkpoint.rst) + +## 4.vllm推理评测 + +**启动vllm_server服务** + +通过以下命令拉起推理服务端,需要修改的参数:model和tensor-parallel-size。 + +model:保存训练后权重转换完的huggingface模型地址; + +tensor-parallel-size:张量并行副本数,TP建议和训练时infer的配置保持一致; + +data-parallel-size:数据并行副本数,DP建议和训练时infer的配置保持一致,默认为1; + +port:可任意设置空闲端口; + +~~~bash +vllm serve /path/to/Qwen3-30B/ \ + --served-model-name auto \ + --gpu-memory-utilization 0.9 \ + --max-num-seqs 24 \ + --max-model-len 22528 \ + --max-num-batched-tokens 22528 \ + --enforce-eager \ + --trust-remote-code \ + --distributed_executor_backend=mp \ + --tensor-parallel-size 8 \ + --data-parallel-size 1 \ + --generation-config vllm \ + --port 8080 +~~~ + +**修改AISBench推理配置启动vllm_client评测** + +打开推理配置文件 benchmark/ais_bench/benchmark/configs/models/vllm_api/vllm_api_stream_chat.py + +host_port需与服务端的port一致,根据模型配置修改max_seq_len和max_out_len +~~~bash +from ais_bench.benchmark.models import VLLMCustomAPIChatStream +from ais_bench.benchmark.utils.model_postprocessors import extract_non_reasoning_content + +models = [ + dict( + attr="service", + type=VLLMCustomAPIChatStream, + abbr='vllm-api-stream-chat', + path="", + model="", + request_rate = 0, + retry = 2, + host_ip = "localhost", + host_port = 8080, + max_out_len = 512, + batch_size=1, + trust_remote_code=False, + generation_kwargs = dict( + temperature = 0.5, + top_k = 10, + top_p = 0.95, + seed = None, + repetition_penalty = 1.03, + ), + pred_postprocessor=dict(type=extract_non_reasoning_content) + ) +] +~~~ + +另起一个窗口进行评测,开启评测命令: +~~~bash + ais_bench --models vllm_api_stream_chat --datasets math500_gen_0_shot_cot_chat_prompt +~~~ +## 5.sglang推理评测 +参照 [sglang最佳实践](../../model_support/examples/ascend_sglang_best_practices.rst)中评测进行 \ No newline at end of file diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/parameter_and_metrics.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/parameter_and_metrics.md new file mode 100644 index 0000000000000000000000000000000000000000..88254f0696b2a21956df05f73563ca4ea31bf945 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/parameter_and_metrics.md @@ -0,0 +1,881 @@ +# 训练配置参数与指标说明 + +Last updated: 07/02/2026. + +如需查看 NPU 相关特性,请访问:[NPU 高级特性指南](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/feature_support/npu_advance_features.md) 。 + +verl 通过层级化的 YAML 配置文件管理所有参数,涉及到的所有配置文件均在 `verl/trainer/config` 目录下。 + +--- + +## 1. 配置参数说明 + +### 1.1 公共配置参数 + +以下参数在 FSDP 方案和 Megatron 方案中均存在且含义一致。 + +#### 1.1.1 Actor 优化器配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.optim.lr` | `1.0e-06` | Actor 学习率 | +| `actor_rollout_ref.actor.optim.lr_warmup_steps_ratio` | `0.0` | 学习率预热步数占总训练步数的比例 | +| `actor_rollout_ref.actor.optim.total_training_steps` | `-1` | 总训练步数,-1 表示自动计算 | +| `actor_rollout_ref.actor.optim.weight_decay` | `0.01` | 权重衰减,用于防止模型过拟合 | +| `actor_rollout_ref.actor.optim.lr_warmup_steps` | `-1` | 学习率预热步数,-1 表示由 ratio 自动计算 | +| `actor_rollout_ref.actor.optim.betas` | `[0.9, 0.999]` | Adam 优化器的一阶和二阶动量系数 | +| `actor_rollout_ref.actor.optim.clip_grad` | `1.0` | 梯度裁剪阈值 | +| `actor_rollout_ref.actor.optim.override_optimizer_config` | `null` / `{}` | 覆盖优化器配置(FSDP 为 null,Megatron 为 {}) | + +#### 1.1.2 Actor 策略配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.strategy` | `fsdp` / `megatron` | 训练策略,FSDP 方案为 fsdp,Megatron 方案为 megatron | +| `actor_rollout_ref.actor.ppo_mini_batch_size` | `256` | PPO 训练的 mini batch 大小 | +| `actor_rollout_ref.actor.ppo_micro_batch_size` | `null` | PPO 训练的 micro batch 大小 | +| `actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu` | `null` | 每 GPU 的 PPO micro batch 大小 | +| `actor_rollout_ref.actor.use_dynamic_bsz` | `false` | 是否使用动态 batch size | +| `actor_rollout_ref.actor.ppo_max_token_len_per_gpu` | `16384` | 每 GPU 的 PPO 最大 token 长度 | +| `actor_rollout_ref.actor.clip_ratio` | `0.2` | PPO 裁剪比例,控制策略更新幅度,一般取值范围 [0.1, 0.3] | +| `actor_rollout_ref.actor.clip_ratio_low` | `0.2` | PPO 下界裁剪比例 | +| `actor_rollout_ref.actor.clip_ratio_high` | `0.2` | PPO 上界裁剪比例 | +| `actor_rollout_ref.actor.tau_pos` | `1.0` | 正优势裁剪的 tau 参数 | +| `actor_rollout_ref.actor.tau_neg` | `1.05` | 负优势裁剪的 tau 参数 | +| `actor_rollout_ref.actor.freeze_vision_tower` | `false` | 是否冻结视觉塔(多模态模型) | +| `actor_rollout_ref.actor.clip_ratio_c` | `3.0` | 裁剪比例的上限常数 | +| `actor_rollout_ref.actor.loss_agg_mode` | `token-mean` | 损失聚合模式,可选 token-mean 等 | +| `actor_rollout_ref.actor.loss_scale_factor` | `null` | 损失缩放因子 | +| `actor_rollout_ref.actor.entropy_coeff` | `0` | 熵正则化系数,控制策略探索程度 | +| `actor_rollout_ref.actor.calculate_entropy` | `false` | 是否计算策略熵 | +| `actor_rollout_ref.actor.use_kl_loss` | `false` | 是否使用 KL 散度损失 | +| `actor_rollout_ref.actor.use_prefix_grouper` | `false` | 是否使用前缀分组器 | +| `actor_rollout_ref.actor.use_torch_compile` | `true` | 是否使用 torch.compile 加速 | +| `actor_rollout_ref.actor.kl_loss_coef` | `0.001` | KL 损失系数 | +| `actor_rollout_ref.actor.kl_loss_type` | `low_var_kl` | KL 损失类型,可选 low_var_kl 等 | +| `actor_rollout_ref.actor.ppo_epochs` | `1` | PPO 更新轮数 | +| `actor_rollout_ref.actor.shuffle` | `false` | 训练时是否对 mini batch 进行 shuffle | +| `actor_rollout_ref.actor.data_loader_seed` | `42` | 数据加载器随机种子 | +| `actor_rollout_ref.actor.grad_clip` | `1.0` | 梯度裁剪值 | +| `actor_rollout_ref.actor.ulysses_sequence_parallel_size` | `1` | Ulysses 序列并行大小 | +| `actor_rollout_ref.actor.entropy_from_logits_with_chunking` | `false` | 是否使用分块方式从 logits 计算熵 | +| `actor_rollout_ref.actor.entropy_from_logits_chunk_size` | `2048` | 熵计算分块大小 | +| `actor_rollout_ref.actor.entropy_checkpointing` | `false` | 是否对熵计算使用梯度检查点 | +| `actor_rollout_ref.actor.use_remove_padding` | 引用自 `model.use_remove_padding` | 是否移除 padding | +| `actor_rollout_ref.actor.calculate_sum_pi_squared` | `false` | 是否计算策略概率平方和 | +| `actor_rollout_ref.actor.sum_pi_squared_checkpointing` | `false` | 是否对策略概率平方和计算使用梯度检查点 | +| `actor_rollout_ref.actor.use_fused_kernels` | 引用自 `model.use_fused_kernels` | 是否使用融合内核 | + +#### 1.1.3 Policy Loss 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.policy_loss.loss_mode` | `vanilla` | 策略损失模式,可选 vanilla、clip_cov、kl_cov、dppo_tv、dppo_kl、gspo、sapo、geo_mean、cispo、gpg、bypass_mode、reinforce_is 等 | +| `actor_rollout_ref.actor.policy_loss.clip_cov_ratio` | `0.0002` | clip_cov 模式的协方差比率 | +| `actor_rollout_ref.actor.policy_loss.clip_cov_lb` | `1.0` | clip_cov 模式的协方差下界 | +| `actor_rollout_ref.actor.policy_loss.clip_cov_ub` | `5.0` | clip_cov 模式的协方差上界 | +| `actor_rollout_ref.actor.policy_loss.kl_cov_ratio` | `0.0002` | kl_cov 模式的协方差比率 | +| `actor_rollout_ref.actor.policy_loss.ppo_kl_coef` | `0.1` | PPO KL 散度系数 | + +#### 1.1.4 Rollout 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.name` | `???` | Rollout 引擎名称,需用户指定 | +| `actor_rollout_ref.rollout.mode` | `async` | Rollout 模式,可选 async、sync 等 | +| `actor_rollout_ref.rollout.nnodes` | `0` | Rollout 使用的节点数 | +| `actor_rollout_ref.rollout.n_gpus_per_node` | 引用自 `trainer.n_gpus_per_node` | 每节点 GPU 数 | +| `actor_rollout_ref.rollout.temperature` | `1.0` | 采样温度,控制生成随机性 | +| `actor_rollout_ref.rollout.top_k` | `-1` | Top-K 采样参数,-1 表示不启用 | +| `actor_rollout_ref.rollout.top_p` | `1` | Top-P(nucleus)采样参数 | +| `actor_rollout_ref.rollout.prompt_length` | 引用自 `data.max_prompt_length` | Prompt 最大长度 | +| `actor_rollout_ref.rollout.response_length` | 引用自 `data.max_response_length` | Response 最大长度 | +| `actor_rollout_ref.rollout.dtype` | `bfloat16` | Rollout 推理数据类型 | +| `actor_rollout_ref.rollout.gpu_memory_utilization` | `0.5` | GPU 内存利用率,推理时使用 GPU 内存的比例 | +| `actor_rollout_ref.rollout.ignore_eos` | `false` | 是否忽略 EOS token | +| `actor_rollout_ref.rollout.enforce_eager` | `false` | 是否强制使用 PyTorch eager 模式 | +| `actor_rollout_ref.rollout.cudagraph_capture_sizes` | `null` | CUDA Graph 捕获大小列表 | +| `actor_rollout_ref.rollout.free_cache_engine` | `true` | 是否在每次推理后释放缓存引擎 | +| `actor_rollout_ref.rollout.tensor_model_parallel_size` | `2` | 推理时 TP 并行大小 | +| `actor_rollout_ref.rollout.data_parallel_size` | `1` | 推理时数据并行大小 | +| `actor_rollout_ref.rollout.expert_parallel_size` | `1` | 推理时专家并行大小 | +| `actor_rollout_ref.rollout.pipeline_model_parallel_size` | `1` | 推理时 PP 并行大小 | +| `actor_rollout_ref.rollout.max_num_batched_tokens` | `8192` | 单步最大批处理 token 数 | +| `actor_rollout_ref.rollout.max_model_len` | `null` | 模型最大序列长度,null 表示自动推断 | +| `actor_rollout_ref.rollout.max_num_seqs` | `1024` | 推理并发最大样本数 | +| `actor_rollout_ref.rollout.enable_chunked_prefill` | `true` | 是否启用分块预填充 | +| `actor_rollout_ref.rollout.enable_prefix_caching` | `true` | 是否启用前缀缓存(KV Cache 复用) | +| `actor_rollout_ref.rollout.logprobs_mode` | `processed_logprobs` | logprobs 计算模式 | +| `actor_rollout_ref.rollout.scheduling_policy` | `fcfs` | 调度策略,可选 fcfs 等 | +| `actor_rollout_ref.rollout.load_format` | `dummy` | 模型加载格式 | +| `actor_rollout_ref.rollout.log_prob_micro_batch_size` | `null` | log prob 计算的 micro batch 大小 | +| `actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu` | `null` | 每 GPU 的 log prob micro batch 大小 | +| `actor_rollout_ref.rollout.log_prob_use_dynamic_bsz` | 引用自 `actor.use_dynamic_bsz` | log prob 是否使用动态 batch size | +| `actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu` | 引用自 `actor.ppo_max_token_len_per_gpu` | 每 GPU 的 log prob 最大 token 长度 | +| `actor_rollout_ref.rollout.disable_log_stats` | `true` | 是否禁用推理日志统计 | +| `actor_rollout_ref.rollout.do_sample` | `true` | 是否进行采样(false 则为贪心解码) | +| `actor_rollout_ref.rollout.n` | `1` | 每个 prompt 生成的 response 数量 | +| `actor_rollout_ref.rollout.over_sample_rate` | `0` | 过采样率 | +| `actor_rollout_ref.rollout.multi_stage_wake_up` | `false` | 是否启用多阶段唤醒 | +| `actor_rollout_ref.rollout.calculate_log_probs` | `false` | 是否在 rollout 阶段计算 log probs | +| `actor_rollout_ref.rollout.skip_tokenizer_init` | `true` | 是否跳过分词器初始化 | +| `actor_rollout_ref.rollout.enable_rollout_routing_replay` | `false` | 是否启用 rollout 路由重放 | +| `actor_rollout_ref.rollout.quantization` | `null` | 量化方式 | +| `actor_rollout_ref.rollout.quantization_config_file` | `null` | 量化配置文件路径 | +| `actor_rollout_ref.rollout.layered_summon` | `false` | 是否启用分层召唤(仅 FSDP 方案) | + +#### 1.1.5 Rollout 验证采样配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.val_kwargs.top_k` | `-1` | 验证时 Top-K 采样参数 | +| `actor_rollout_ref.rollout.val_kwargs.top_p` | `1.0` | 验证时 Top-P 采样参数 | +| `actor_rollout_ref.rollout.val_kwargs.temperature` | `0` | 验证时采样温度,0 表示贪心解码 | +| `actor_rollout_ref.rollout.val_kwargs.n` | `1` | 验证时每个 prompt 生成的 response 数 | +| `actor_rollout_ref.rollout.val_kwargs.do_sample` | `false` | 验证时是否采样 | + +#### 1.1.6 Multi-Turn 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.multi_turn.enable` | `false` | 是否启用多轮对话 | +| `actor_rollout_ref.rollout.multi_turn.max_assistant_turns` | `null` | 最大助手轮数 | +| `actor_rollout_ref.rollout.multi_turn.tool_config_path` | `null` | 工具配置文件路径 | +| `actor_rollout_ref.rollout.multi_turn.max_user_turns` | `null` | 最大用户轮数 | +| `actor_rollout_ref.rollout.multi_turn.max_parallel_calls` | `1` | 最大并行工具调用数 | +| `actor_rollout_ref.rollout.multi_turn.max_tool_response_length` | `256` | 工具响应最大长度 | +| `actor_rollout_ref.rollout.multi_turn.tool_response_truncate_side` | `middle` | 工具响应截断方向 | +| `actor_rollout_ref.rollout.multi_turn.interaction_config_path` | `null` | 交互配置文件路径 | +| `actor_rollout_ref.rollout.multi_turn.use_inference_chat_template` | `false` | 是否使用推理聊天模板 | +| `actor_rollout_ref.rollout.multi_turn.tokenization_sanity_check_mode` | `strict` | 分词完整性检查模式 | +| `actor_rollout_ref.rollout.multi_turn.format` | `hermes` | 多轮对话格式 | +| `actor_rollout_ref.rollout.multi_turn.num_repeat_rollouts` | `null` | 重复 rollout 次数 | + +#### 1.1.7 Agent 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.agent.num_workers` | `8` | Agent 工作进程数 | +| `actor_rollout_ref.rollout.agent.default_agent_loop` | `single_turn_agent` | 默认 Agent 循环类型 | +| `actor_rollout_ref.rollout.agent.agent_loop_config_path` | `null` | Agent 循环配置文件路径 | +| `actor_rollout_ref.rollout.agent.custom_async_server.path` | `null` | 自定义异步服务路径 | +| `actor_rollout_ref.rollout.agent.custom_async_server.name` | `null` | 自定义异步服务名称 | + +#### 1.1.8 Checkpoint Engine 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.checkpoint_engine.backend` | `naive` | Checkpoint 引擎后端 | +| `actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes` | `2048` | 权重更新桶大小(MB) | + +#### 1.1.9 Trace 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.trace.project_name` | 引用自 `trainer.project_name` | 追踪项目名称 | +| `actor_rollout_ref.rollout.trace.experiment_name` | 引用自 `trainer.experiment_name` | 追踪实验名称 | +| `actor_rollout_ref.rollout.trace.backend` | `null` | 追踪后端 | +| `actor_rollout_ref.rollout.trace.token2text` | `false` | 是否将 token 转为文本 | +| `actor_rollout_ref.rollout.trace.max_samples_per_step_per_worker` | `null` | 每步每 worker 最大样本数 | + +#### 1.1.10 Prometheus 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.rollout.prometheus.enable` | `false` | 是否启用 Prometheus 监控 | +| `actor_rollout_ref.rollout.prometheus.port` | `9090` | Prometheus 端口 | +| `actor_rollout_ref.rollout.prometheus.file` | `/tmp/ray/session_latest/metrics/prometheus/prometheus.yml` | Prometheus 配置文件路径 | +| `actor_rollout_ref.rollout.prometheus.served_model_name` | 引用自 `model.path` | 服务模型名称 | + +#### 1.1.11 Reference 模型配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.ref.rollout_n` | 引用自 `rollout.n` | Rollout 次数 | +| `actor_rollout_ref.ref.strategy` | 引用自 `actor.strategy` | 训练策略 | +| `actor_rollout_ref.ref.use_torch_compile` | 引用自 `actor.use_torch_compile` | 是否使用 torch.compile | +| `actor_rollout_ref.ref.log_prob_micro_batch_size` | `null` | log prob 计算的 micro batch 大小 | +| `actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu` | `null` | 每 GPU 的 log prob micro batch 大小 | +| `actor_rollout_ref.ref.log_prob_use_dynamic_bsz` | 引用自 `actor.use_dynamic_bsz` | log prob 是否使用动态 batch size | +| `actor_rollout_ref.ref.log_prob_max_token_len_per_gpu` | 引用自 `actor.ppo_max_token_len_per_gpu` | 每 GPU 的 log prob 最大 token 长度 | +| `actor_rollout_ref.ref.ulysses_sequence_parallel_size` | 引用自 `actor.ulysses_sequence_parallel_size` | Ulysses 序列并行大小 | +| `actor_rollout_ref.ref.entropy_from_logits_with_chunking` | `false` | 是否使用分块方式从 logits 计算熵 | +| `actor_rollout_ref.ref.entropy_checkpointing` | `false` | 是否对熵计算使用梯度检查点 | + +#### 1.1.12 Critic 优化器配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `critic.optim.lr` | `1.0e-05` | Critic 学习率 | +| `critic.optim.lr_warmup_steps_ratio` | `0.0` | 学习率预热步数比例 | +| `critic.optim.total_training_steps` | `-1` | 总训练步数 | +| `critic.optim.weight_decay` | `0.01` | 权重衰减 | +| `critic.optim.lr_warmup_steps` | `-1` | 学习率预热步数 | +| `critic.optim.betas` | `[0.9, 0.999]` | Adam 优化器动量系数 | +| `critic.optim.clip_grad` | `1.0` | 梯度裁剪阈值 | +| `critic.optim.override_optimizer_config` | `null` / `{}` | 覆盖优化器配置 | + +#### 1.1.13 Critic 策略配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `critic.strategy` | `fsdp` / `megatron` | 训练策略 | +| `critic.enable` | `null` | 是否启用 Critic,null 表示自动决定 | +| `critic.ppo_mini_batch_size` | 引用自 `actor.ppo_mini_batch_size` | PPO mini batch 大小 | +| `critic.ppo_micro_batch_size` | `null` | PPO micro batch 大小 | +| `critic.ppo_micro_batch_size_per_gpu` | `null` | 每 GPU 的 PPO micro batch 大小 | +| `critic.use_dynamic_bsz` | 引用自 `actor.use_dynamic_bsz` | 是否使用动态 batch size | +| `critic.ppo_max_token_len_per_gpu` | `32768` | 每 GPU 的 PPO 最大 token 长度 | +| `critic.forward_max_token_len_per_gpu` | 引用自 `critic.ppo_max_token_len_per_gpu` | 前向计算每 GPU 最大 token 长度 | +| `critic.ppo_epochs` | 引用自 `actor.ppo_epochs` | PPO 更新轮数 | +| `critic.shuffle` | 引用自 `actor.shuffle` | 是否 shuffle | +| `critic.data_loader_seed` | `42` / 引用自 `actor.data_loader_seed` | 数据加载器随机种子 | +| `critic.cliprange_value` | `0.5` | Critic 值函数裁剪范围 | +| `critic.loss_agg_mode` | 引用自 `actor.loss_agg_mode` | 损失聚合模式 | +| `critic.grad_clip` | `1.0` | 梯度裁剪值 | +| `critic.ulysses_sequence_parallel_size` | `1` | Ulysses 序列并行大小 | +| `critic.forward_micro_batch_size` | 引用自 `critic.ppo_micro_batch_size` | 前向计算 micro batch 大小 | +| `critic.forward_micro_batch_size_per_gpu` | 引用自 `critic.ppo_micro_batch_size_per_gpu` | 前向计算每 GPU micro batch 大小 | + +#### 1.1.14 Critic 模型配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `critic.model.path` | `~/models/deepseek-llm-7b-chat` | Critic 模型路径 | +| `critic.model.tokenizer_path` | 引用自 `model.path` | 分词器路径 | +| `critic.model.override_config` | `{}` | 覆盖模型配置 | +| `critic.model.external_lib` | 引用自 `model.external_lib` | 外部库路径 | +| `critic.model.trust_remote_code` | 引用自 `model.trust_remote_code` | 是否信任远程代码 | +| `critic.model.use_shm` | `false` | 是否使用共享内存 | +| `critic.model.enable_gradient_checkpointing` | `true` | 是否启用梯度检查点 | +| `critic.model.enable_activation_offload` | `false` | 是否启用激活卸载 | +| `critic.model.use_remove_padding` | `false` / `true` | 是否移除 padding | +| `critic.model.lora_rank` | `0` | LoRA 秩 | +| `critic.model.lora_alpha` | `16` | LoRA alpha | +| `critic.model.target_modules` | `all-linear` | LoRA 目标模块 | +| `critic.model.tiled_mlp.enabled` | `false` | 是否启用分片 MLP | +| `critic.model.tiled_mlp.num_shards` | `4` | MLP 分片数 | + +#### 1.1.15 数据配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `data.tokenizer` | `null` | 分词器路径 | +| `data.use_shm` | `false` | 是否使用共享内存 | +| `data.train_files` | `~/data/rlhf/gsm8k/train.parquet` | 训练数据文件路径 | +| `data.val_files` | `~/data/rlhf/gsm8k/test.parquet` | 验证数据文件路径 | +| `data.train_max_samples` | `-1` | 训练最大样本数,-1 表示不限制 | +| `data.val_max_samples` | `-1` | 验证最大样本数 | +| `data.prompt_key` | `prompt` | 数据中 prompt 的键名 | +| `data.reward_fn_key` | `data_source` | 奖励函数的键名 | +| `data.max_prompt_length` | `512` | 最大 prompt 长度 | +| `data.max_response_length` | `512` | 最大 response 长度 | +| `data.train_batch_size` | `1024` | 训练 batch 大小 | +| `data.val_batch_size` | `null` | 验证 batch 大小 | +| `data.tool_config_path` | 引用自 `rollout.multi_turn.tool_config_path` | 工具配置文件路径 | +| `data.return_raw_input_ids` | `false` | 是否返回原始 input ids | +| `data.return_raw_chat` | `true` | 是否返回原始聊天内容 | +| `data.return_full_prompt` | `false` | 是否返回完整 prompt | +| `data.shuffle` | `true` | 是否 shuffle 训练数据 | +| `data.seed` | `null` | 数据 shuffle 随机种子 | +| `data.dataloader_num_workers` | `8` | 数据加载器工作进程数 | +| `data.image_patch_size` | `14` | 图像 patch 大小 | +| `data.validation_shuffle` | `false` | 验证时是否 shuffle | +| `data.filter_overlong_prompts` | `false` | 是否过滤超长 prompt | +| `data.filter_overlong_prompts_workers` | `1` | 过滤超长 prompt 的工作进程数 | +| `data.truncation` | `error` | 截断策略 | +| `data.image_key` | `images` | 图像数据的键名 | +| `data.video_key` | `videos` | 视频数据的键名 | +| `data.trust_remote_code` | `false` | 是否信任远程代码 | +| `data.return_multi_modal_inputs` | `true` | 是否返回多模态输入 | + +#### 1.1.16 奖励配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `reward.num_workers` | `8` | 奖励计算工作进程数 | +| `reward.custom_reward_function.path` | `null` | 自定义奖励函数路径 | +| `reward.custom_reward_function.name` | `compute_score` | 自定义奖励函数名称 | +| `reward.reward_manager.source` | `register` | 奖励管理器来源 | +| `reward.reward_manager.name` | `naive` | 奖励管理器名称 | +| `reward.reward_model.enable` | `false` | 是否启用奖励模型 | +| `reward.reward_model.enable_resource_pool` | `false` | 是否启用奖励模型资源池 | +| `reward.reward_model.n_gpus_per_node` | `8` | 奖励模型每节点 GPU 数 | +| `reward.reward_model.nnodes` | `0` | 奖励模型节点数 | +| `reward.reward_model.model_path` | `null` | 奖励模型路径 | +| `reward.sandbox_fusion.url` | `null` | Sandbox Fusion URL | +| `reward.sandbox_fusion.max_concurrent` | `64` | Sandbox Fusion 最大并发数 | +| `reward.sandbox_fusion.memory_limit_mb` | `1024` | Sandbox Fusion 内存限制(MB) | + +#### 1.1.17 算法配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `algorithm.gamma` | `1.0` | 折扣因子 | +| `algorithm.lam` | `1.0` | GAE lambda 参数 | +| `algorithm.adv_estimator` | `gae` | 优势估计方法,可选 gae 等 | +| `algorithm.norm_adv_by_std_in_grpo` | `true` | GRPO 中是否按标准差归一化优势 | +| `algorithm.use_kl_in_reward` | `false` | 是否在奖励中使用 KL 惩罚 | +| `algorithm.kl_penalty` | `kl` | KL 惩罚类型 | +| `algorithm.kl_ctrl.type` | `fixed` | KL 控制器类型,可选 fixed、kl_adapter 等 | +| `algorithm.kl_ctrl.kl_coef` | `0.001` | KL 惩罚系数 | +| `algorithm.kl_ctrl.horizon` | `10000` | KL 适配器的 horizon | +| `algorithm.kl_ctrl.target_kl` | `0.1` | 目标 KL 散度 | +| `algorithm.use_pf_ppo` | `false` | 是否使用 PF-PPO | +| `algorithm.pf_ppo.reweight_method` | `pow` | PF-PPO 重加权方法 | +| `algorithm.pf_ppo.weight_pow` | `2.0` | PF-PPO 加权幂次 | + +#### 1.1.18 Rollout Correction 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `algorithm.rollout_correction.rollout_is` | `null` | 是否启用 IS 重要性采样校正 | +| `algorithm.rollout_correction.rollout_is_threshold` | `2.0` | IS 权重阈值 | +| `algorithm.rollout_correction.rollout_rs` | `null` | 是否启用拒绝采样校正 | +| `algorithm.rollout_correction.rollout_rs_threshold` | `null` | RS 阈值 | +| `algorithm.rollout_correction.bypass_mode` | `false` | 是否启用旁路模式 | +| `algorithm.rollout_correction.loss_type` | `ppo_clip` | 校正损失类型 | +| `algorithm.rollout_correction.rollout_is_batch_normalize` | `false` | IS 权重是否批量归一化 | + +#### 1.1.19 训练器配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `trainer.balance_batch` | `true` | 是否平衡 batch | +| `trainer.total_epochs` | `30` | 总训练 epoch 数 | +| `trainer.total_training_steps` | `null` | 总训练步数,null 表示由 epoch 自动计算 | +| `trainer.project_name` | `verl_examples` | 项目名称 | +| `trainer.experiment_name` | `gsm8k` | 实验名称 | +| `trainer.logger` | `[console, wandb]` | 日志后端列表 | +| `trainer.log_val_generations` | `0` | 验证生成日志数量 | +| `trainer.nnodes` | `1` | 训练节点数 | +| `trainer.n_gpus_per_node` | `8` | 每节点 GPU 数 | +| `trainer.save_freq` | `-1` | 保存频率,-1 表示不保存 | +| `trainer.esi_redundant_time` | `0` | ESI 冗余时间 | +| `trainer.resume_mode` | `auto` | 恢复模式,可选 auto 等 | +| `trainer.resume_from_path` | `null` | 恢复路径 | +| `trainer.val_before_train` | `true` | 训练前是否先验证 | +| `trainer.val_only` | `false` | 是否仅验证 | +| `trainer.test_freq` | `-1` | 测试频率 | +| `trainer.critic_warmup` | `0` | Critic 预热步数 | +| `trainer.default_hdfs_dir` | `null` | 默认 HDFS 目录 | +| `trainer.del_local_ckpt_after_load` | `false` | 加载后是否删除本地 checkpoint | +| `trainer.default_local_dir` | `checkpoints/${trainer.project_name}/${trainer.experiment_name}` | 默认本地 checkpoint 目录 | +| `trainer.max_actor_ckpt_to_keep` | `null` | 最多保留的 Actor checkpoint 数 | +| `trainer.max_critic_ckpt_to_keep` | `null` | 最多保留的 Critic checkpoint 数 | +| `trainer.ray_wait_register_center_timeout` | `300` | Ray 注册中心等待超时(秒) | +| `trainer.device` | `cuda` | 训练设备 | +| `trainer.use_legacy_worker_impl` | `auto` | 是否使用旧版 worker 实现 | +| `trainer.rollout_data_dir` | `null` | 保存每轮 rollout 结果的地址配置 | + +#### 1.1.20 模型配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.model.path` | `~/models/deepseek-llm-7b-chat` | 模型路径 | +| `actor_rollout_ref.model.hf_config_path` | `null` | HuggingFace 配置路径 | +| `actor_rollout_ref.model.tokenizer_path` | `null` | 分词器路径 | +| `actor_rollout_ref.model.use_shm` | `false` | 是否使用共享内存 | +| `actor_rollout_ref.model.trust_remote_code` | `false` | 是否信任远程代码 | +| `actor_rollout_ref.model.custom_chat_template` | `null` | 自定义聊天模板 | +| `actor_rollout_ref.model.external_lib` | `null` | 外部库路径 | +| `actor_rollout_ref.model.override_config` | `{}` | 覆盖模型配置 | +| `actor_rollout_ref.model.enable_gradient_checkpointing` | `true` | 是否启用梯度检查点 | +| `actor_rollout_ref.model.enable_activation_offload` | `false` | 是否启用激活卸载 | +| `actor_rollout_ref.model.use_remove_padding` | `true` / `false` | 是否移除 padding | +| `actor_rollout_ref.model.lora_rank` | `0` | LoRA 秩,0 表示不使用 LoRA | +| `actor_rollout_ref.model.lora_alpha` | `16` | LoRA alpha | +| `actor_rollout_ref.model.target_modules` | `all-linear` | LoRA 目标模块 | +| `actor_rollout_ref.model.exclude_modules` | `null` | LoRA 排除模块 | +| `actor_rollout_ref.model.lora_adapter_path` | `null` | LoRA 适配器路径 | +| `actor_rollout_ref.model.use_liger` | `false` | 是否使用 Liger 内核 | +| `actor_rollout_ref.model.use_fused_kernels` | `false` | 是否使用融合内核 | +| `actor_rollout_ref.model.fused_kernel_options.impl_backend` | `torch` | 融合内核实现后端 | +| `actor_rollout_ref.model.tiled_mlp.enabled` | `false` | 是否启用分片 MLP | +| `actor_rollout_ref.model.tiled_mlp.num_shards` | `4` | MLP 分片数 | + +#### 1.1.21 公共引擎配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.hybrid_engine` | `true` | 是否使用混合引擎(训练推理共享权重) | +| `actor_rollout_ref.nccl_timeout` | `600` | NCCL 通信超时(秒) | +| `transfer_queue.enable` | `false` | 是否启用传输队列 | + +--- + +### 1.2 FSDP 专属配置参数 + +以下参数仅在 FSDP 方案(`_generated_ppo_trainer.yaml`)中存在。 + +#### 1.2.1 FSDP 优化器配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.optim.optimizer` | `AdamW` | 优化器类型 | +| `actor_rollout_ref.actor.optim.optimizer_impl` | `torch.optim` | 优化器实现 | +| `actor_rollout_ref.actor.optim.min_lr_ratio` | `0.0` | 最小学习率比例 | +| `actor_rollout_ref.actor.optim.num_cycles` | `0.5` | 余弦调度周期数 | +| `actor_rollout_ref.actor.optim.lr_scheduler_type` | `constant` | 学习率调度器类型 | +| `actor_rollout_ref.actor.optim.zero_indexed_step` | `true` | 步数是否从 0 开始计数 | +| `actor_rollout_ref.actor.optim.warmup_style` | `null` | 预热风格 | + +#### 1.2.2 Actor FSDP 引擎配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.fsdp_config.wrap_policy.min_num_params` | `0` | FSDP 包装的最小参数数 | +| `actor_rollout_ref.actor.fsdp_config.param_offload` | `false` | 是否将参数卸载到 CPU | +| `actor_rollout_ref.actor.fsdp_config.optimizer_offload` | `false` | 是否将优化器状态卸载到 CPU | +| `actor_rollout_ref.actor.fsdp_config.offload_policy` | `false` | 卸载策略 | +| `actor_rollout_ref.actor.fsdp_config.reshard_after_forward` | `true` | 前向计算后是否重新分片 | +| `actor_rollout_ref.actor.fsdp_config.fsdp_size` | `-1` | FSDP 组大小,-1 表示全局 | +| `actor_rollout_ref.actor.fsdp_config.forward_prefetch` | `false` | 是否预取前向参数 | +| `actor_rollout_ref.actor.fsdp_config.model_dtype` | `fp32` | 模型计算数据类型 | +| `actor_rollout_ref.actor.fsdp_config.use_orig_params` | `false` | 是否使用原始参数 | +| `actor_rollout_ref.actor.fsdp_config.seed` | `42` | 随机种子 | +| `actor_rollout_ref.actor.fsdp_config.full_determinism` | `false` | 是否启用完全确定性 | +| `actor_rollout_ref.actor.fsdp_config.forward_only` | `false` | 是否仅前向计算(Actor 为 false) | +| `actor_rollout_ref.actor.fsdp_config.strategy` | `fsdp` | 策略类型 | +| `actor_rollout_ref.actor.fsdp_config.dtype` | `bfloat16` | 模型存储数据类型 | + +#### 1.2.3 Reference FSDP 引擎配置 + +与 Actor FSDP 引擎配置结构相同,主要区别: + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.ref.fsdp_config.forward_only` | `true` | Reference 模型仅前向计算 | + +其余参数(`wrap_policy`、`param_offload`、`optimizer_offload`、`reshard_after_forward`、`fsdp_size`、`dtype` 等)默认值与 Actor FSDP 引擎配置一致。 + +#### 1.2.4 Critic FSDP 引擎配置 + +与 Actor FSDP 引擎配置结构相同,主要区别: + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `critic.model.fsdp_config.forward_only` | `false` | Critic 模型需要训练 | +| `critic.model.fsdp_config.use_remove_padding` | `false` | Critic 不移除 padding | + +其余参数默认值与 Actor FSDP 引擎配置一致。 + +--- + +### 1.3 Megatron 专属配置参数 + +以下参数仅在 Megatron 方案(`_generated_ppo_megatron_trainer.yaml`)中存在。 + +#### 1.3.1 Megatron 优化器配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.optim.optimizer` | `adam` | 优化器类型 | +| `actor_rollout_ref.actor.optim.lr_warmup_init` | `0.0` | 学习率预热初始值 | +| `actor_rollout_ref.actor.optim.lr_decay_steps` | `null` | 学习率衰减步数 | +| `actor_rollout_ref.actor.optim.lr_decay_style` | `constant` | 学习率衰减风格,可选 constant、cosine、exponential 等 | +| `actor_rollout_ref.actor.optim.min_lr` | `0.0` | 最小学习率 | +| `actor_rollout_ref.actor.optim.weight_decay_incr_style` | `constant` | 权重衰减增长风格 | +| `actor_rollout_ref.actor.optim.lr_wsd_decay_style` | `exponential` | WSD 学习率衰减风格 | +| `actor_rollout_ref.actor.optim.lr_wsd_decay_steps` | `null` | WSD 学习率衰减步数 | +| `actor_rollout_ref.actor.optim.use_checkpoint_opt_param_scheduler` | `false` | 是否使用 checkpoint 优化器参数调度器 | + +#### 1.3.2 Actor Megatron 引擎配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.actor.megatron.param_offload` | `false` | 是否将参数卸载到 CPU | +| `actor_rollout_ref.actor.megatron.grad_offload` | `false` | 是否将梯度卸载到 CPU | +| `actor_rollout_ref.actor.megatron.optimizer_offload` | `false` | 是否将优化器状态卸载到 CPU | +| `actor_rollout_ref.actor.megatron.tensor_model_parallel_size` | `1` | TP 并行大小 | +| `actor_rollout_ref.actor.megatron.expert_model_parallel_size` | `1` | 专家并行大小 | +| `actor_rollout_ref.actor.megatron.expert_tensor_parallel_size` | `null` | 专家 TP 并行大小 | +| `actor_rollout_ref.actor.megatron.pipeline_model_parallel_size` | `1` | PP 并行大小 | +| `actor_rollout_ref.actor.megatron.virtual_pipeline_model_parallel_size` | `null` | 虚拟 PP 并行大小 | +| `actor_rollout_ref.actor.megatron.context_parallel_size` | `1` | 上下文并行大小 | +| `actor_rollout_ref.actor.megatron.sequence_parallel` | `true` | 是否启用序列并行 | +| `actor_rollout_ref.actor.megatron.use_distributed_optimizer` | `true` | 是否使用分布式优化器 | +| `actor_rollout_ref.actor.megatron.use_dist_checkpointing` | `false` | 是否使用分布式 checkpoint | +| `actor_rollout_ref.actor.megatron.dist_checkpointing_path` | `null` | 分布式 checkpoint 路径 | +| `actor_rollout_ref.actor.megatron.dist_checkpointing_prefix` | `''` | 分布式 checkpoint 前缀 | +| `actor_rollout_ref.actor.megatron.dist_ckpt_optim_fully_reshardable` | `false` | 分布式 checkpoint 优化器是否完全可重分片 | +| `actor_rollout_ref.actor.megatron.distrib_optim_fully_reshardable_mem_efficient` | `false` | 分布式优化器重分片是否内存高效 | +| `actor_rollout_ref.actor.megatron.seed` | `42` | 随机种子 | +| `actor_rollout_ref.actor.megatron.use_mbridge` | `true` | 是否启用 Bridge 权重转换 | +| `actor_rollout_ref.actor.megatron.vanilla_mbridge` | `false` | 是否使用已弃用的老版 mBridge;默认使用 Megatron-Bridge | +| `actor_rollout_ref.actor.megatron.use_remove_padding` | `true` | 是否移除 padding | +| `actor_rollout_ref.actor.megatron.forward_only` | `false` | 是否仅前向计算 | +| `actor_rollout_ref.actor.megatron.dtype` | `bfloat16` | 模型数据类型 | +| `actor_rollout_ref.actor.megatron.load_weight` | `true` | 是否加载权重 | + +#### 1.3.3 Megatron Transformer 覆盖配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `override_transformer_config.recompute_granularity` | `null` | 重计算粒度 | +| `override_transformer_config.recompute_modules` | `[core_attn]` | 重计算模块列表 | +| `override_transformer_config.recompute_method` | `null` | 重计算方法 | +| `override_transformer_config.recompute_num_layers` | `null` | 重计算层数 | +| `override_transformer_config.attention_backend` | `flash` | 注意力后端 | + +#### 1.3.4 Reference Megatron 引擎配置 + +与 Actor Megatron 引擎配置结构相同,主要区别: + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `actor_rollout_ref.ref.megatron.forward_only` | `true` | Reference 模型仅前向计算 | + +其余参数默认值引用自 Actor Megatron 引擎配置(如 `param_offload`、`tensor_model_parallel_size` 等)。 + +#### 1.3.5 Critic Megatron 引擎配置 + +与 Actor Megatron 引擎配置结构相同,主要区别: + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `critic.megatron.forward_only` | `false` | Critic 模型需要训练 | + +#### 1.3.6 Megatron LoRA 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `model.lora.type` | `lora` | LoRA 类型 | +| `model.lora.merge` | `false` | 是否合并 LoRA 权重 | +| `model.lora.rank` | `0` | LoRA 秩,0 表示不使用 | +| `model.lora.alpha` | `32` | LoRA alpha | +| `model.lora.dropout` | `0.0` | LoRA dropout | +| `model.lora.target_modules` | `[linear_qkv, linear_proj, linear_fc1, linear_fc2]` | LoRA 目标模块 | +| `model.lora.exclude_modules` | `[]` | LoRA 排除模块 | +| `model.lora.dropout_position` | `pre` | LoRA dropout 位置 | +| `model.lora.lora_A_init_method` | `xavier` | LoRA A 矩阵初始化方法 | +| `model.lora.lora_B_init_method` | `zero` | LoRA B 矩阵初始化方法 | +| `model.lora.a2a_experimental` | `false` | 是否启用 a2a 实验性功能 | +| `model.lora.dtype` | `null` | LoRA 数据类型 | +| `model.lora.adapter_path` | `null` | LoRA 适配器路径 | +| `model.lora.freeze_vision_model` | `true` | 是否冻结视觉模型 | +| `model.lora.freeze_vision_projection` | `true` | 是否冻结视觉投影 | +| `model.lora.freeze_language_model` | `true` | 是否冻结语言模型 | + +#### 1.3.7 模型 override_config(Megatron 方案) + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `model.override_config.model_config` | `{}` | 模型配置覆盖 | +| `model.override_config.moe_config.freeze_moe_router` | `false` | 是否冻结 MoE 路由 | + +#### 1.3.8 Rollout layer_name_map(Megatron 方案) + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `rollout.layer_name_map.qkv_layer_name` | `qkv` | QKV 层名称映射 | +| `rollout.layer_name_map.gate_proj_layer_name` | `gate_up` | Gate 投影层名称映射 | + +--- + +### 1.4 高级配置参数 + +#### 1.4.1 Profiler 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `profiler.enable` | `false` | 是否启用 Profiler | +| `profiler.tool` | 引用自 `global_profiler.tool` | Profiler 工具,可选 nsys、npu、torch、torch_memory | +| `profiler.all_ranks` | `false` | 是否在所有 rank 上启用 | +| `profiler.ranks` | `[]` | 指定启用的 rank 列表 | +| `profiler.save_path` | 引用自 `global_profiler.save_path` | Profiler 结果保存路径 | + +#### 1.4.2 Global Profiler 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `global_profiler.tool` | `null` | 全局 Profiler 工具 | +| `global_profiler.steps` | `null` | Profiler 采集步数 | +| `global_profiler.profile_continuous_steps` | `false` | 是否连续步采集 | +| `global_profiler.save_path` | `outputs/profile` | 全局保存路径 | + +#### 1.4.3 Router Replay 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `router_replay.mode` | `disabled` | 路由重放模式,可选 disabled、record、replay | +| `router_replay.record_file` | `null` | 路由记录文件路径 | +| `router_replay.replay_file` | `null` | 路由重放文件路径 | + +#### 1.4.4 Checkpoint 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `checkpoint.save_contents` | `[model, optimizer, extra]` | Checkpoint 保存内容 | +| `checkpoint.load_contents` | 引用自 `checkpoint.save_contents` | Checkpoint 加载内容 | +| `checkpoint.async_save` | `false` | 是否异步保存 Checkpoint | +| `checkpoint.mbridge_config` | `{}` | mBridge 配置 | + +#### 1.4.5 QAT 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `qat.enable` | `false` | 是否启用量化感知训练(QAT) | +| `qat.mode` | `w4a16` | 量化模式 | +| `qat.group_size` | `16` | 量化分组大小 | +| `qat.ignore_patterns` | `[lm_head, embed_tokens, re:.*mlp.gate$]` | 量化忽略的模式列表 | +| `qat.activation_observer` | `static_minmax` | 激活值观察器类型 | +| `qat.quantization_config_path` | `null` | 量化配置文件路径 | + +#### 1.4.6 MTP 配置 + +| 参数名 | 默认值 | 说明 | +|--------|--------|------| +| `mtp.enable` | `false` | 是否启用多 token 预测(MTP) | +| `mtp.enable_train` | `false` | 是否在训练中启用 MTP | +| `mtp.enable_rollout` | `false` | 是否在推理中启用 MTP | +| `mtp.detach_encoder` | `false` | 是否分离编码器 | +| `mtp.mtp_loss_scaling_factor` | `0.1` | MTP 损失缩放因子 | +| `mtp.speculative_algorithm` | `EAGLE` | 推测解码算法 | +| `mtp.speculative_num_steps` | `3` | 推测步数 | +| `mtp.speculative_eagle_topk` | `1` | EAGLE Top-K | +| `mtp.speculative_num_draft_tokens` | `4` | 推测 draft token 数 | +| `mtp.method` | `mtp` | MTP 方法 | +| `mtp.num_speculative_tokens` | `1` | 推测 token 数 | + +--- + +## 2. 训练指标说明 + +强化学习算法每个 iteration 打印的日志指标说明如下: + +### 2.1 训练基础指标 + +| 指标 | 说明 | +|------|------| +| `training/global_step` | 当前全局训练步数 | +| `training/epoch` | 当前训练 epoch | + +### 2.2 Actor 模型指标 + +| 指标 | 说明 | +|------|------| +| `actor/pg_loss` | 策略梯度损失(PPO clip loss),基于优势函数的策略梯度目标函数值 | +| `actor/kl_loss` | KL 散度损失,衡量当前策略与参考策略之间的偏离程度(仅 `use_kl_loss=True` 时打印) | +| `actor/entropy` | 策略熵,表示策略的随机性或探索能力(仅 `calculate_entropy=True` 或 `entropy_coeff!=0` 时打印) | +| `actor/grad_norm` | Actor 梯度范数(裁剪后),表示反向传播中参数梯度的整体幅度 | +| `actor/lr` | Actor 当前学习率 | +| `actor/pg_clipfrac` | PPO 裁剪机制生效的比例,反映策略更新幅度的稳定性 | +| `actor/ppo_kl` | PPO 算法的实际 KL 散度(当前策略 vs 旧策略) | +| `actor/pg_clipfrac_lower` | PPO 下界裁剪比例(部分 `loss_mode` 有此指标) | +| `actor/reward_kl_penalty` | KL 惩罚值,当前策略与参考策略的 KL 均值(仅 `use_kl_in_reward=True` 时打印) | +| `actor/reward_kl_penalty_coeff` | KL 惩罚系数 beta(仅 `use_kl_in_reward=True` 时打印) | +| `actor/kl_coef` | KL 损失系数(仅 `use_kl_loss=True` 时打印) | + +### 2.3 Critic 模型指标 + +| 指标 | 说明 | +|------|------| +| `critic/vf_loss` | 值函数损失 | +| `critic/vf_clipfrac` | Critic 裁剪机制生效的比例,反映值函数更新幅度的稳定性 | +| `critic/vpred_mean` | 预测值的均值 | +| `critic/grad_norm` | Critic 梯度范数(裁剪后) | +| `critic/lr` | Critic 当前学习率 | +| `critic/vf_explained_var` | 值函数解释方差 1 - Var(returns-values)/Var(returns)(仅 `use_critic=True` 时打印) | + +### 2.4 数据统计指标 + +| 指标 | 说明 | +|------|------| +| `critic/score/mean` | 非中止样本的序列分数均值 | +| `critic/score/max` | 非中止样本的序列分数最大值 | +| `critic/score/min` | 非中止样本的序列分数最小值 | +| `critic/rewards/mean` | 非中止样本的序列奖励均值 | +| `critic/rewards/max` | 非中止样本的序列奖励最大值 | +| `critic/rewards/min` | 非中止样本的序列奖励最小值 | +| `critic/advantages/mean` | 有效 token 的优势值均值 | +| `critic/advantages/max` | 有效 token 的优势值最大值 | +| `critic/advantages/min` | 有效 token 的优势值最小值 | +| `critic/returns/mean` | 有效 token 的回报均值 | +| `critic/returns/max` | 有效 token 的回报最大值 | +| `critic/returns/min` | 有效 token 的回报最小值 | +| `critic/values/mean` | 有效 token 的 Critic 值均值(仅 `use_critic=True` 时打印) | +| `critic/values/max` | 有效 token 的 Critic 值最大值(仅 `use_critic=True` 时打印) | +| `critic/values/min` | 有效 token 的 Critic 值最小值(仅 `use_critic=True` 时打印) | +| `response_length/mean` | 响应长度均值(含中止样本) | +| `response_length/max` | 响应长度最大值 | +| `response_length/min` | 响应长度最小值 | +| `response_length/clip_ratio` | 响应长度达到最大长度的比例 | +| `response_length_non_aborted/mean` | 非中止样本的响应长度均值 | +| `response_length_non_aborted/max` | 非中止样本的响应长度最大值 | +| `response_length_non_aborted/min` | 非中止样本的响应长度最小值 | +| `response_length_non_aborted/clip_ratio` | 非中止样本的响应长度达到最大长度的比例 | +| `response/aborted_ratio` | 中止样本(响应长度为 0)的比例 | +| `prompt_length/mean` | 提示长度均值 | +| `prompt_length/max` | 提示长度最大值 | +| `prompt_length/min` | 提示长度最小值 | +| `prompt_length/clip_ratio` | 提示长度达到最大长度的比例 | +| `num_turns/mean` | 多轮对话轮数均值(仅多轮对话时打印) | +| `num_turns/max` | 多轮对话轮数最大值(仅多轮对话时打印) | +| `num_turns/min` | 多轮对话轮数最小值(仅多轮对话时打印) | +| `tool_call_counts/mean` | 工具调用次数均值(仅存在 `tool_call_counts` 时打印) | +| `tool_call_counts/max` | 工具调用次数最大值 | +| `tool_call_counts/min` | 工具调用次数最小值 | + +### 2.5 时间指标 + +| 指标 | 说明 | +|------|------| +| `timing_s/gen` | 生成(rollout)耗时(秒) | +| `timing_s/ref` | Reference 模型计算 log_p 耗时(秒) | +| `timing_s/values` | Critic 模型计算 values 耗时(秒) | +| `timing_s/adv` | 计算优势值耗时(秒) | +| `timing_s/update_critic` | Critic 模型更新耗时(秒) | +| `timing_s/update_actor` | Actor 模型更新耗时(秒) | +| `timing_s/step` | 一步总耗时(秒) | +| `timing_s/old_log_prob` | Actor 模型计算旧 log_p 耗时(秒) | +| `timing_s/reward` | 奖励计算耗时(秒) | +| `timing_s/testing` | 验证耗时(秒) | +| `timing_s/save_checkpoint` | 保存 checkpoint 耗时(秒) | +| `timing_s/update_weights` | 权重同步耗时(秒) | +| `timing_per_token_ms/gen` | 生成阶段每 token 耗时(毫秒) | +| `timing_per_token_ms/ref` | Reference 模型每 token 耗时(毫秒) | +| `timing_per_token_ms/values` | Critic 模型每 token 耗时(毫秒) | +| `timing_per_token_ms/adv` | 优势值计算每 token 耗时(毫秒) | +| `timing_per_token_ms/update_critic` | Critic 更新每 token 耗时(毫秒) | +| `timing_per_token_ms/update_actor` | Actor 更新每 token 耗时(毫秒) | + +### 2.6 性能指标 + +| 指标 | 说明 | +|------|------| +| `perf/total_num_tokens` | 本步处理的总 token 数 | +| `perf/time_per_step` | 本步总耗时(秒) | +| `perf/throughput` | 吞吐量:tokens / (time * n_gpus) | +| `perf/max_memory_allocated_gb` | GPU 最大已分配内存(GB) | +| `perf/max_memory_reserved_gb` | GPU 最大预留内存(GB) | +| `perf/cpu_memory_used_gb` | CPU 已使用内存(GB) | +| `perf/mfu/actor` | Actor 训练的 MFU(模型浮点利用率) | +| `perf/mfu/critic` | Critic 训练的 MFU | +| `perf/mfu/actor_infer` | Actor 推理阶段的 MFU | + +### 2.7 方差代理指标 + +| 指标 | 说明 | +|------|------| +| `variance_proxy/proxy1_signal_strength` | 信号强度:梯度均值的平方范数 \|\|g_mean\|\|^2 | +| `variance_proxy/proxy2_total_power` | 总功率:梯度平方范数的期望 E[\|\|g_tau\|\|^2] | +| `variance_proxy/proxy3_pure_noise` | 纯噪声:梯度方差代理 (1/(N-1)) * (Proxy2 - Proxy1) | +| `variance_proxy/expected_a_squared` | 优势平方的期望 E[A^2] | +| `variance_proxy/expected_w` | W-score 代理的期望 E[W] | + +### 2.8 条件性指标 + +以下指标仅在特定条件满足时打印: + +#### 2.8.1 Rollout Correction 指标 + +仅启用 `rollout_correction` 时打印,均带 `rollout_corr/` 前缀。 + +**IS 权重指标**(仅启用 IS 校正时): + +| 指标 | 说明 | +|------|------| +| `rollout_corr/rollout_is_mean` | IS 权重均值 | +| `rollout_corr/rollout_is_max` | IS 权重最大值 | +| `rollout_corr/rollout_is_min` | IS 权重最小值 | +| `rollout_corr/rollout_is_std` | IS 权重标准差 | +| `rollout_corr/rollout_is_ratio_fraction_high` | 超过上限阈值的 IS 权重比例 | +| `rollout_corr/rollout_is_ratio_fraction_low` | 低于下限阈值的 IS 权重比例 | +| `rollout_corr/rollout_is_eff_sample_size` | 有效样本大小(ESS) | +| `rollout_corr/rollout_is_seq_mean` | 序列级 IS 权重均值 | +| `rollout_corr/rollout_is_seq_std` | 序列级 IS 权重标准差 | +| `rollout_corr/rollout_is_seq_max` | 序列级 IS 权重最大值 | +| `rollout_corr/rollout_is_seq_min` | 序列级 IS 权重最小值 | +| `rollout_corr/rollout_is_seq_max_deviation` | 序列级 IS 权重与理想值 1.0 的最大偏差 | +| `rollout_corr/rollout_is_seq_fraction_high` | 序列级 IS 权重超过上限的比例 | +| `rollout_corr/rollout_is_seq_fraction_low` | 序列级 IS 权重低于下限的比例 | +| `rollout_corr/rollout_is_batch_norm_factor` | IS 权重批量归一化因子(仅 `rollout_is_batch_normalize=True` 时打印) | + +**Rejection Sampling 指标**(仅启用 RS 校正时): + +| 指标 | 说明 | +|------|------| +| `rollout_corr/rollout_rs_{option}_mean` | RS 统计量均值 | +| `rollout_corr/rollout_rs_{option}_max` | RS 统计量最大值 | +| `rollout_corr/rollout_rs_{option}_min` | RS 统计量最小值 | +| `rollout_corr/rollout_rs_{option}_std` | RS 统计量标准差 | +| `rollout_corr/rollout_rs_{option}_fraction_high` | 超过上限阈值的比例 | +| `rollout_corr/rollout_rs_{option}_fraction_low` | 低于下限阈值的比例 | +| `rollout_corr/rollout_rs_{option}_seq_mean` | 序列级 RS 统计量均值 | +| `rollout_corr/rollout_rs_{option}_seq_std` | 序列级 RS 统计量标准差 | +| `rollout_corr/rollout_rs_{option}_seq_max` | 序列级 RS 统计量最大值 | +| `rollout_corr/rollout_rs_{option}_seq_min` | 序列级 RS 统计量最小值 | +| `rollout_corr/rollout_rs_{option}_seq_max_deviation` | 序列级 RS 统计量与 0 的最大偏差 | +| `rollout_corr/rollout_rs_{option}_seq_fraction_high` | 序列级超过上限的比例 | +| `rollout_corr/rollout_rs_{option}_seq_fraction_low` | 序列级低于下限的比例 | +| `rollout_corr/rollout_rs_{option}_masked_fraction` | token 级被 mask 掉的比例 | +| `rollout_corr/rollout_rs_{option}_seq_masked_fraction` | 序列级被 mask 掉的比例 | +| `rollout_corr/rollout_rs_masked_fraction` | 总体 token 级被 mask 掉的比例 | +| `rollout_corr/rollout_rs_seq_masked_fraction` | 总体序列级被 mask 掉的比例 | + +**Off-policy 诊断指标**(仅启用 off-policy 诊断时): + +| 指标 | 说明 | +|------|------| +| `rollout_corr/training_ppl` | 训练策略的困惑度 | +| `rollout_corr/training_log_ppl` | 训练策略的 log 困惑度 | +| `rollout_corr/kl` | KL(π_rollout \|\| π_training) 直接估计 | +| `rollout_corr/k3_kl` | K3 KL 估计(更稳定) | +| `rollout_corr/rollout_ppl` | Rollout 策略的困惑度 | +| `rollout_corr/rollout_log_ppl` | Rollout 策略的 log 困惑度 | +| `rollout_corr/log_ppl_diff` | log PPL 差值(rollout - training) | +| `rollout_corr/log_ppl_abs_diff` | log PPL 绝对差值的均值 | +| `rollout_corr/log_ppl_diff_max` | log PPL 差值最大值 | +| `rollout_corr/log_ppl_diff_min` | log PPL 差值最小值 | +| `rollout_corr/ppl_ratio` | PPL 比率(training_ppl / rollout_ppl) | +| `rollout_corr/chi2_token` | token 级卡方散度 | +| `rollout_corr/chi2_seq` | 序列级卡方散度 | + +#### 2.8.2 序列长度平衡指标 + +仅启用 `balance_batch` 时打印: + +| 指标 | 说明 | +|------|------| +| `global_seqlen/min` | 平衡前各 DP 分区的最小序列长度和 | +| `global_seqlen/max` | 平衡前各 DP 分区的最大序列长度和 | +| `global_seqlen/minmax_diff` | 平衡前 max - min 差值 | +| `global_seqlen/balanced_min` | 平衡后各 DP 分区的最小序列长度和 | +| `global_seqlen/balanced_max` | 平衡后各 DP 分区的最大序列长度和 | +| `global_seqlen/mean` | 各分区的平均序列长度和 | + +#### 2.8.3 GDPO 奖励指标 + +仅使用 GDPO 估计器时打印: + +| 指标 | 说明 | +|------|------| +| `gdpo/{key}/mean` | GDPO 各奖励分量的均值 | +| `gdpo/{key}/std` | GDPO 各奖励分量的标准差 | +| `gdpo/{key}/max` | GDPO 各奖励分量的最大值 | +| `gdpo/{key}/min` | GDPO 各奖励分量的最小值 | + +#### 2.8.4 训推一致性指标 + +仅存在 `actor_rollout_ref.rollout.calculate_log_probs=True` 时打印: + +| 指标 | 说明 | +|------|------| +| `training/rollout_probs_diff_valid` | 标记为 1(有效) | +| `training/rollout_probs_diff_max` | rollout 与 actor 概率差异的最大值 | +| `training/rollout_probs_diff_mean` | rollout 与 actor 概率差异的均值 | +| `training/rollout_probs_diff_std` | rollout 与 actor 概率差异的标准差 | +| `training/rollout_actor_probs_pearson_corr` | rollout 与 actor 概率的 Pearson 相关系数 | + +#### 2.8.5 验证指标 + +验证阶段打印: + +| 指标 | 说明 | +|------|------| +| `val-core/{data_source}/{var_name}/{metric_name}` | 核心验证指标(mean@N, maj@N, best@N 等) | +| `val-aux/{data_source}/{var_name}/{metric_name}` | 辅助验证指标(std@N, worst@N 等) | +| `val-aux/num_turns/mean` | 验证集多轮对话轮数均值 | +| `val-aux/num_turns/max` | 验证集多轮对话轮数最大值 | +| `val-aux/num_turns/min` | 验证集多轮对话轮数最小值 | diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/transfer_to_npu_guide.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/transfer_to_npu_guide.md new file mode 100644 index 0000000000000000000000000000000000000000..14b1787ecc08eff714f1794044565b02a7d6a2f3 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/model_dev/transfer_to_npu_guide.md @@ -0,0 +1,296 @@ +# Transfer to NPU guide + +Last updated: 05/14/2026 + +本文为开发者提供从 GPU 迁移至 NPU或在 NPU 上独立适配模型的完整实践经验,涵盖前期准备、各组件打通、精度对齐、性能优化及长跑评测全流程。 + +## 一、前期准备 + +搭建可支持 NPU 运行的基础运行环境,保证模型正常加载、数据集可顺利读取,作为后续迁移调试、业务跑通的基础。 + + +### 1.1 软硬件环境与依赖配置 + +参照官方文档[install\_guidance.rst](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/get_start/install_guidance.rst);若模型依赖的推理引擎 vllm、vllm_ascend 和训练引擎Megatron、MindSpeed、transformers 版本与教程存在差异,**以模型实际适配版本为准**。 + +### 1.2 模型权重 + +BF16 为 VeRL 框架中 FSDP 与 Megatron 等训练后端**默认混合精度训练数据类型**。昇腾 NPU 环境统一采用 **BF16** 作为基准精度格式,权重需对齐反量化为 BF16。目前 A2、A3 机型**暂不支持 FP8 精度训练**,仅支持 BF16 精度;A5 机型后续版本将开放 FP8 低精度训练能力。 + +### 1.3 数据准备 + +数据需参照[Prepare Data for Post-Training](https://verl.readthedocs.io/en/latest/preparation/prepare_data.html)将数据集预处理为 parquet 格式:(1) 确保它包含计算强化学习奖励所需的必要字段;(2) 读取速度更快。 + +## 二、各组件联调打通 + +VeRL 框架采用推理引擎、训练引擎与权重同步桥接(Checkpoint Engine)相解耦的架构设计,可实现计算与数据的深度分离,为模型向昇腾 NPU 迁移适配提供了灵活的扩展基础。在开展模型在NPU上的迁移与适配工作时,建议优先完成推理引擎、训练引擎、Megatron-Bridge 各组件的单独适配与验证,待各组件运行稳定后,再推进 VeRL 整网链路的打通与调试。关于 VeRL 不同推理、训练后端在昇腾 NPU 上的具体特性支持,可参考[昇腾特性文档](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/feature_support/ascend_backend_features.md)。 + +### 2.1 推理引擎适配 + +VeRL 推理引擎采用分层架构设计,通过抽象接口与工厂模式,实现 vllm、sglang 等多种主流推理后端的灵活支持。在完成 GPU 向 NPU 的迁移适配过程中,推理引擎适配推荐按以下流程操作: + +在 NPU 上跑通 VeRL 整网链路前,建议参考 [vllm-ascend](https://github.com/vllm-project/vllm-ascend/tree/main/docs/source/tutorials/models)、[sglang](https://github.com/sgl-project/sglang/blob/main/docs_new/docs/basic_usage) 官方模型部署教程,优先调通**单实例推理链路**,完整验证模型加载与初始化、Tokenizer 加载正常、单轮 / 批量生成、停止词终止、长上下文推理等**基础推理功能**,前置底层推理引擎稳定可用后,再接入 VeRL 训练流程。 + +### 2.2 训练引擎选择与适配 + +VeRL 主线代码将训练引擎抽象为 `Engine`类,通过标准化接口层实现调度逻辑与底层训练实现的解耦。该架构设计支持 FSDP、Megatron、MindSpeed-LLM 等多种训练后端灵活接入、即插即用,无需修改 VeRL 核心算法与调度逻辑,大幅降低迁移适配成本。 + +当前 NPU 已通过 `is_npu_available` 接口完成设备自动检测,并自动应用对应的 NPU 设备适配补丁。目前只需通过配置 model_engine=fsdp/megatron,即可一键切换训练后端至 FSDP、Megatron,系统会自动加载对应后端的 NPU 适配逻辑,无需额外修改代码。VeRL中昇腾对Megatron做了适配与优化,具体特性配置参考[verl-MindSpeed特性文档](https://gitcode.com/Ascend/MindSpeed/blob/master/docs/zh/user-guide/verl.md)设置。 + +### 2.3 Megatron-Bridge适配 + +Megatron-Bridge 主要用于在 VeRL 框架下,完成推理引擎依赖的 HuggingFace 权重与 Megatron-Core 所需 mcore 权重的双向转换,可通过以下配置启用该功能: + +``` +actor_rollout_ref.actor.megatron.use_mbridge=True +actor_rollout_ref.actor.megatron.vanilla_mbridge=False +``` + +Megatron-Bridge已在社区原生适配大量主流模型结构,支持列表可参考:[supported model](https://github.com/NVIDIA-NeMo/Megatron-Bridge/blob/main/docs/models/README.md),在昇腾 NPU 环境开展模型迁移适配时,可基于社区现有能力完成基础配置,但仍有部分模型特殊结构与场景需要补充定制化适配。 + +以​DSA (DeepSeek Sparse Attention)稀疏注意力结构为示例,介绍定制化适配的方法。昇腾 MindSpeed 支持基于吸收矩阵的 DSA能力,该特性要求将 Megatron 中原有的 `linear_kv_up_proj` 算子拆分为 `linear_k_up_proj` 与 `linear_v_up_proj` 两个独立算子。拆分所需权重需从 HuggingFace 格式的 `self_attn.kv_b_proj.weight` 转换生成,而上述原生 PR 并未适配该算子拆分逻辑。 + +因此需手动改造适配相关权重转换逻辑,保障吸收矩阵可正常加载与生效。只有在吸收矩阵可用的基础上,才能正常使能 [sparse\_flash\_attention](https://gitcode.com/cann/ops-transformer/tree/master/attention/sparse_flash_attention) 与 [lightning\_indexer](https://gitcode.com/cann/ops-transformer/tree/master/attention/lightning_indexer) 融合算子;通过引入两个融合算子,可大幅减少内存访问频次、优化内存占用率,同时提升计算性能,最终实现大模型训练与推理链路的运行效率提升及资源开销降低。 + +### 2.4 整网功能打通 + +完成推理引擎适配验证、训练引擎适配开发,参照[参数配置说明](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/dev_guide/model_dev/parameter_and_metrics.md)根据实际业务需求,配置推理引擎、训练引擎的相关参数,完成 VeRL 整网功能打通,确保全流程稳定运行。 + +## 三、精度对齐 + +大模型强化学习的精度问题定位链路复杂、影响因素繁多,各类精度问题通常由**训练阶段、推理阶段、训推一致性**问题引入。**精度对齐**是保障训练流程可复现、问题可调试的核心关键。 + +训练与推理阶段的精度对齐,可参考官方文档:[精度对齐文档](../precision_analysis/precision_alignment_zh.md)。因此本节不再赘述基础阶段对齐流程,将**重点围绕训推一致性场景**,基于 msprobe 精度工具,开展精度对齐落地实践与问题定位排查工作。 + +### 3.1 精度监控配置 + +整网跑通后,需启用精度监控参数,设置 `actor_rollout_ref.rollout.calculate_log_probs=True`。在训练过程中需重点观察以下关键指标,以此判断训推一致性及模型训练稳定性: + +* **训推一致性参考指标**: + * `training/rollout_probs_diff_mean`(rollout概率差异均值),模型正常收敛状态下,该指标建议维持在 0.01 以内;若数值持续高于 0.01 或与 GPU 基准存在明显偏离,可判定存在训推精度异常。 + * `training/rollout_probs_diff_max`(rollout概率差异最大值) + * `training/rollout_actor_probs_pearson_corr`(rollout与actor概率的皮尔逊相关系数) +* **模型训练稳定性指标**: + * `actor/grad_norm`:需关注其是否呈整体下降趋势,以此判断模型训练是否正常收敛。 + +此外,配置参数 `trainer.rollout_data_dir=./rollout_dump/` 用于保存训练过程中的 Rollout 中间结果。通过人工核查导出的 Rollout 数据,校验模型回复是否符合预期、输出有无乱码与重复回答现象,可进一步从表象上确认推理引擎适配无误。 + +### 3.2 采集精度数据 + +当 training/rollout_probs_diff_mean 超出 0.01 合理阈值、或与 GPU 基准标杆出现明显偏离时,需进一步通过[msprobe](../precision_analysis/precision_debugger_zh.md)精度工具采集数据做根因定位。 + +### 3.3 训推差异点排查与对齐实践 + +完成数据采集后,优先读取 `construction.json` 文件进行模块级数据比对。先保证 `layer.0.input_layernorm` 输入数据完全一致,再逐模块逐层校验,定位训练与推理输出首次出现不一致的位置。 + +而对于大尺寸模型,微小数值差异会随着逐层累积、放大,导致训练(training)和推理(rollout)的结果差异明显,甚至会出现同一个token训推输出概率分别为0和1的现象,因此需尽可能将每一处差异点对齐至完全相等。 + +定位到差异节点后,适配修改方案同样是关键难点。由于业内各开源社区对相关模块存在多套不同实现,为保障模型实现逻辑的正确性,需多方参考权威源码与技术报告,综合确定最终对齐方案。 + +#### 3.3.1 常见训推不一致 + +在大模型强化学习实践中,可将训推不一致的典型根因归纳为以下五类: + +1. **框架实现不一致**:由于训练、推理框架实现逻辑不同导致。有时是“语义正确”的(如算子拆分方式不同但数学等价),有时是“语义错误”的(如遗漏了某个缩放因子,或多了某个操作),需结合源码与技术报告严格鉴定。 +2. **精度类型差异**:如训练侧全程 BF16,而推理侧在某些归一化等敏感算子中隐式升精到 FP32 计算再降精,导致截断误差。 +3. **超参数不一致**:如LayerNorm模块中硬编码的 `eps` 值未做统一。 +4. **并行策略**:训练时的张量并行 vs 推理时的连续批处理,导致浮点累加顺序差异。 +5. **随机性控制**:Dropout、采样策略在训推阶段的实现偏差。 + +下面列举 GLM-5 模型迁移适配过程中遇到的典型训推不一致实际案例。 + +#### 3.3.2 案例一:FFN激活函数的框架实现不一致 + +从上往下依次比对,排查到第一层的 MLP 激活函数处输出不一致。 + +推理侧已正常使用 NPU 优化的 `npu_swiglu` 融合算子,但训练侧仍执行原生 GLU 小算子实现。 + +* **根因**:尽管已在 VeRL 参数中添加了 `swiglu` 使能配置,但 Megatron-Bridge 在 NPU 适配 PR 中,未显式配置 `provider.bias_activation_fusion=True`,导致代码未进入 NPU 融合算子分支。 +* **修复方案**:在 Megatron-Bridge 中添加配置项使训练侧正确调用融合算子: + ``` + +actor_rollout_ref.actor.megatron.override_transformer_config.swiglu=True \ + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True \ + ``` + +#### 3.3.3 案例二:indexer_k_norm 的精度与超参数不一致 + +在严格对齐过程中,发现 `indexer_k_norm` 处存在精度类型与超参不一致: + +* **精度差异**:推理侧在 LayerNorm 中存在升精度到 fp32 操作 `F.layer_norm( x.float(), (self.dim,), self.weight, self.bias, self.eps).type_as(x)`,而训练侧 Megatron 实现为 BF16。微小差异经多层累积不可忽视。 +* **修复方案**:统一训练侧代码增加升精降精操作。 +* **超参差异**:GLM5 推理侧vllm继承 DeepSeek-V3.2 逻辑,`k_norm` 的 EPS 值被硬编码为 `1e-6`;而训练引擎及官方技术报告统一采用 `1e-5`。 +* **修复方案**:将推理侧 EPS 修改为 `1e-5` 与训练侧对齐。 + +``` +self.k_norm=LayerNorm(self.head_dim,eps=1e-6 -> 1e-5) +``` + +#### 3.3.4 案例三:lightning_indexer 模块逻辑缺失与冗余 + +排查发现lightning_indexer模块,训练与推理侧该模块具体实现存在不一致,具体表现为: + +* **缺失(推理侧遗漏)**:推理侧缺失了 `weights` 的缩放逻辑。参考 Megatron 训练侧、slime 及 transformers 的标准实现,均包含该缩放,故在推理侧补齐以对齐前向: + +``` +weights, _ = self.weights_proj(x) ++weights = weights * (self.n_head**-0.5) * (self.head_dim**-0.5) +``` + +* **冗余(训练侧多余错误实现)**:训练侧 Megatron 实现中多出了 `rotate_activation`(哈达玛变换)。经查阅大量资料确认,该操作专用于量化场景,在 BF16 格式中属于错误实现。参考 [Transformer PR#45017](https://github.com/huggingface/transformers/pull/45017),将训练侧该冗余逻辑移除。 + +``` +class DSAIndexer(MegatronModule): + def forward_with_scores( +- q = rotate_activation(q) +- k = rotate_activation(k) +``` + +### 3.4 MoE 大模型通用路由稳定方案 + +在典型的 RL 训练流程中,通常采用 vLLM 等高效推理引擎完成样本采样,再将采样数据送入 Megatron 等训练框架做模型训练优化。 + +对于常规稠密模型,推理与训练框架间的实现、环境差异仅会产生轻微数值偏差;但**大尺寸 MoE 模型**下该问题会被急剧放大。核心根源在于 MoE 动态路由机制:微小的框架实现、运行环境差异,就可能导致同一输入 Token 被分配至完全不同的专家组合,从而走向截然不同的计算路径。 + +这种路由决策的不一致,可能会导致MoE模型RL训练不稳定。它使得从推理阶段获取的“经验”对于训练阶段而言变得完全不同,优化信号因此失真,最终导致灾难性的后果。 + +为了解决这一通用问题,业界引入了 **Routing Replay(路由回放)** 机制。其核心思想是通过锁定特定阶段的专家路由路径,屏蔽微小扰动对路由决策的干扰,从而保证模型训练的稳定性。目前主流包含R2和R3两种变体: + +* **(1)Vanilla Routing Replay (R2)**: (对应`actor_rollout_ref.actor.router_replay.mode="R2"`) + + * **机制**:在梯度更新阶段,复现训练引擎在上一轮采样阶段计算出的专家路径。 + * **作用**:主要缓解**策略陈旧性**对路由的影响。随着策略的更新,当前前向传播计算出的路由可能与生成旧数据时的路由不一致,R2通过回放旧路由来维持优化信号的连贯性。 +* **(2)Rollout Routing Replay (R3)**:(对应`actor_rollout_ref.actor.megatron.router_replay.mode="R3"`) + + * **机制**:在序列生成过程中捕捉推理引擎的路由分布,并将其直接重放到训练引擎中。 + * **作用**:同时解决**训练-推理偏差**和**策略陈旧性**两个问题。它确保了训练阶段计算Loss所依据的专家路径,与实际推理生成结果时的专家路径绝对一致。 + +因此,无论是侧重缓解策略陈旧的 R2,还是实现全链路对齐的 R3,Routing Replay 机制都有效弥合了推理与训练框架间的路由鸿沟。在**大尺寸 MoE 模型**的训推一致性对齐中,该机制已成为保障精度对齐与训练稳定的核心手段。目前,DeepSeek-V3.2、GLM-5、MiMo-V2 等主流大模型均已采用了 R3 模式的 Routing Replay 技术。 + +因此对于大尺寸 MoE 模型,在实际配置中通常推荐使用对齐更彻底的 R3 模式: + +``` +actor_rollout_ref.actor.router_replay.mode="R3" \ +actor_rollout_ref.rollout.enable_rollout_routing_replay=True \ +``` + +## 四、性能优化 + +在昇腾 NPU 上进行大模型 RL(强化学习)训练性能优化时,基础配置调优可优先参考官方文档:[perf_tuning.rst](https://github.com/verl-project/verl/blob/04833f01/docs/perf/perf_tuning.rst)。为实现更高效的优化,建议遵循**数据采集​​→​瓶颈定位​→配置调优→迭代验证**的标准化流程,该流程可显著提升 Rollout、Reward、Update 等核心阶段的吞吐量,同时有效降低资源空转与负载不均问题。性能分析与调优的具体操作,可严格参照以下官方指引: + +1. [Ascend Performance Analysis Guide](../performance/ascend_performance_analysis_guide.md) +2. [Profiling 数据采集与使能配置](../performance/ascend_profiling_zh.rst) + +### 4.1 推理性能优化 + +Rollout 阶段作为大模型 RL 训练的核心推理环节,其推理耗时在整个训练流程中占据绝大部分,以下是该阶段常见的性能优化手段: + +1. **启用图模式功能**:图模式将整个计算图提前编译优化,可以实现算子融合、内存复用、常量折叠等深度优化,显著提升执行效率。 +2. **CPU 绑核加速算子下发**:通过 CPU 绑核可提升算子下发效率;自 vllm-ascend v0.18.0rc1 版本起,ARM 架构昇腾服务器已默认开启该能力。 +3. **HCCL 通信算法配置为 AIV 模式**:将环境变量 `HCCL_OP_EXPANSION_MODE` 设置为 `AIV` 模式,指定通信算法的编排与展开逻辑运行在 Device 侧 Vector Core 计算单元。 +4. **启用异步调度**:能够消除 Worker 连续两次 execute_model 执行间隙,让 Worker 可直接获取已调度完成的 SchedulerOutput 进行模型推理,无需阻塞等待调度。 + +对应配置参数如下: + +``` +# 图模式启用 +actor_rollout_ref.rollout.enforce_eager=False ++actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_mode="FULL_DECODE_ONLY" ++actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_capture_sizes="[2, 4, 8, 16, 24, 32]" +# CPU绑核 +++actor_rollout_ref.rollout.engine_kwargs.vllm.additional_config.enable_cpu_binding=True +# 异步调度启用 +++actor_rollout_ref.rollout.engine_kwargs.vllm.async_scheduling=True +``` + +### 4.2 训练性能优化 + +大模型 RL 训练的 Update 阶段具有序列长度差异大、显存消耗高等特点。除了基础的算子融合,还需结合序列并行与显存-计算权衡策略来打破瓶颈。常见训练性能优化特性可参考 [MindSpeed-verl 文档](https://gitcode.com/Ascend/MindSpeed/blob/master/docs/zh/user-guide/verl.md) 完成启用,核心优化手段包括: + +1. ​**算子融合**​:启用 RoPE、SwiGLU、RMSNorm、DSA 等融合算子。通过算子融合减少计算开销与显存,提升训练效率。 +2. ​**Remove padding**​:RL 训练中各 Response 长度参差不齐,传统 Padding 策略会导致大量无效计算。开启 Remove padding后可将多个短序列打包填满 Tensor,极大提升 NPU 计算单元的利用率(MFU)。 + +## 五、评测验证 + +训练完成后,需对目标数据集进行评测验证,确保模型迁移后的业务效果达标。不同模型的评测步骤一致,以下以 GLM-5 为例,详细说明评测流程(采用 AISBenchmark 工具,支持 vllm/sglang 多种推理后端的评估)。 + +评测采用了数学类的数据集aime2025与研究生级专业理科数据集gpqa,验证在目标方向上分数上升,且无关方向不会出现知识灾难遗忘情况。 + +### 5.1 安装aisbench + +```shell +git clone https://gitee.com/aisbench/benchmark.git +cd benchmark +pip install -e . +``` + +### 5.2 下载评估数据集 + +```shell +# linux服务器内,处于工具根路径下 +cd path/to/benchmark/ais_bench/datasets +wget http://opencompass.oss-cn-shanghai.aliyuncs.com/datasets/data/aime2025.zip +unzip aime2025.zip +rm aime2025.zip +``` + +### 5.3 修改AISBench配置代码使能vllm/sglang推理评测 + +打开 benchmark/ais_bench/benchmark/configs/models/vllm_api/vllm_api_stream_chat.py 文件,这是推理评测配置文件,输出长度`max_out_len`建议与训练的`max_response_len`保持一致。 + +```shell +from ais_bench.benchmark.models import VLLMCustomAPIChat +from ais_bench.benchmark.utils.postprocess.model_postprocessors import extract_non_reasoning_content + +models = [ + dict( + attr="service", + type=VLLMCustomAPIChat, + abbr='vllm-api-general-chat', + path="/path/to/GLM-5", # 修改为 GLM-5 模型路径 + model="GLM-5", + stream=True, + request_rate = 0, + use_timestamp=False, + max_seq_len=2048, + retry = 2, + api_key="", + host_ip = "localhost", # 推理服务的IP + host_port = 12890 , # 推理服务的端口 + max_out_len = 8192, # 最大输出tokens长度 + batch_size=48, # 推理的最大并发数 + trust_remote_code=False, + generation_kwargs = dict( + temperature = 0, + seed = 1234, + ), + pred_postprocessor=dict(type=extract_non_reasoning_content) + ) +] +``` + +### 5.4 多机拉起推理服务端 + +参考[vllm_ascend GLM5指南](https://github.com/vllm-project/vllm-ascend/blob/main/docs/source/tutorials/models/GLM5.md#multi-node-deployment)拉起双机A3推理服务,`host_port`与上一小节配置保持一致,`max_model_len`设置为训练时的`max_prompt_length`与`max_response`之和。 + +### 5.5 启动vllm评测任务 + +执行以下命令启动在线推理评测任务,调用已部署的 vLLM 推理后端,加载对应模型配置完成自动化评测: + +``` +ais_bench --models vllm_api_stream_chat --datasets aime2025_gen_0_shot_chat_prompt +``` + +模型经过训练后,核心能力指标实现稳定提升:在AIME2025 数学推理数据集上评测得分稳步上涨,同时在GPQA 研究生级专业理科数据集上也实现了持续的分数增益,无知识退化、无灾难性遗忘问题,训练优化效果符合预期。 + +| 评测数据集 | GLM5-base | 10step | 15step | 40step | 50step | +| ---------- | --------- | ------ | ------ | ------ | ------ | +| aime2025 | 47.5 | 49.17 | 49.17 | 48.33 | 52.5 | +| gpqa | 64.65 | 68.81 | 68.43 | 69.07 | 71.21 | + +## 六、总结 + +本文完整覆盖了大模型从 GPU 迁移至昇腾 NPU 或在 NPU 上独立适配的全流程实践,主要分为环境搭建、组件联调、精度对齐、性能优化、评测验证五大关键环节,为开发者提供可落地、可复用的操作指南与问题解决方案。 + +前期准备阶段需重点把控环境依赖版本、模型权重精度与数据集格式,为后续适配奠定基础;组件联调环节需遵循先单组件验证后整网打通的原则,优先确保推理、训练引擎及权重转换工具的稳定适配,针对特殊模型结构需完成定制化改造;精度对齐是迁移适配的核心,需重点监控训推一致性指标,通过逐模块排查解决框架实现、精度类型等常见差异,MoE 模型需启用 Routing Replay 机制保障训练稳定;性能优化需遵循标准化流程,聚焦推理与训练核心阶段,通过图模式、算子融合等手段提升效率、降低资源消耗;最终通过标准化评测验证,确保模型迁移后业务效果达标、无知识退化。 + +整体而言,遵循本文流程可有效降低 NPU 迁移适配成本,规避常见坑点,实现大模型在昇腾 NPU 上的稳定、高效运行。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_performance_analysis_guide.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_performance_analysis_guide.md new file mode 100644 index 0000000000000000000000000000000000000000..0ad2d7e5faa91b8ef02eed988a6ba12692f4c6b9 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_performance_analysis_guide.md @@ -0,0 +1,153 @@ +# Ascend Performance Analysis Guide + +Last updated: 02/24/2026. + +## 背景介绍 + +随着DeepSeek-R1的发布,大模型强化学习(RL)训练受到广泛关注。在昇腾NPU环境下,verl框架已积累了丰富的性能调优经验。本文系统总结了包括性能数据采集与分析在内的方法论,旨在帮助开发者更高效地运用MindStudio工具链,实现强化学习场景下的性能优化。 + +### 强化学习计算流程概述 + +1. **Rollout**:策略(actor)模型基于输入的prompt序列,推理生成回答(response序列) +2. **ref logprob**:基于prompt和生成的response,reference模型计算ref logprob用于KL散度计算 +3. **logprob**:基于prompt和生成的response,actor模型计算logprob用于重要性采样 +4. **reward**:基于prompt和生成的response,奖励模型评估奖励值R_N。 +5. **update**:基于计算得到的R_N、ref logprob、logprob计算优化函数和策略梯度,对actor模型进行更新 + +![rl_data_stream](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/rl_data_stream.png) + +## profiling工具使能 + +### 使能方法 + +使能和配置教程可参考:[ascend_profiling_zh](./ascend_profiling_zh.rst) + +## 性能分析方法论 + +### 整体性能概览分析 + +#### 1. 长耗时任务与资源空泡分析 + +- **操作**:使用MindStudio Insight加载profiling数据,自动识别不同计算阶段,通过RL页签流水图定位长耗时任务与NPU资源空泡 +- **价值**:快速掌握不同阶段耗时占比 +- **效果展示**: + +![Bubble_analysis](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/Bubble_analysis.png) + +#### 2. 负载均衡分析 + +- **操作**:通过MindStudio Insight直接查看MSTX打点数据,观察Rollout阶段不同DP Rank的负载均衡情况 +- **价值**:快速识别负载不均问题 +- **效果展示:** + +![Load_Balancing_Analysis](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/Load_Balancing_Analysis.gif) + +#### 3. 集群整体性能分析 + +- **操作**:结合MSTT的rl_analysis功能,生成集群Timeline缩略图,观察各阶段整体耗时 +- **价值**:宏观掌握集群性能瓶颈 +- **操作指南**:[rl_analysis使用文档](https://gitcode.com/Ascend/mstt/raw/pre-research/profiler/msprof_analyze/docs/features/rl_analysis.md) +- **效果展示**: + +![Cluster%20Performance%20Analysis](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/Cluster%20Performance%20Analysis.png) + +### 细粒度分析 + +#### 性能分析 + +- **操作**:可通过 MindStudio Insight Windows 或 Linux 版本加载 Profiling 数据 +- **价值**:MindStudio Insight 支持分析任务调度效率、算子执行性能、计算资源利用率、集合通信性能等。其 Timeline 视图具备任务拆解与 Overlap 分析功能(**为 MindStudio 独有核心特性,在 NV 及其他竞品中不具备,是 AI 调优的必备工具**),并支持鼠标交互式分析。 +- **效果展示**: + +![performance%20analysis](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/performance%20analysis.png) + +#### 内存分析 + +##### **通过 Profiling 结合调用栈分析系统内存变化** + +- **操作**:采集数据时开启调用栈和内存视图功能。 +- **价值**:观察框架、CANN内存申请释放情况,可结合调用栈跟踪到前端python代码。 +- **效果展示**:结合调用栈进行内存变化分析。效果如下所示: + +![in-memory%20analytics](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/in-memory%20analytics.gif) + +##### **使用 msleaks 工具进行深层次内存分析** + +- **操作步骤**:参考 [msleaks 工具使用指南](https://www.hiascend.com/document/detail/zh/CANNCommunityEdition/83RC1alpha003/devaids/msleaks/atlas_msleaks_0001.html)。 +- **价值**:可以查看框架内存申请总量折线图/内存块图,并直接对应调用栈,可深层次分析框架内存使用情况。 +- **效果展示**: + +![msleaks](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/msleaks.gif) + +## 性能分析案例 + +要做具体的性能分析,profiling要开启**level1**,否则算子的关键信息会缺失。 + +### 1.host bound诊断 + +host bound是指CPU任务量总和大于NPU,导致NPU执行出现空泡的现象。可以通过看Host2Device的同步连线来判断,如果连线都是歪的,那证明这里的set信号早于wait信号,NPU一ready就执行了,那也是device bound: + +![host_bound_1](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/host_bound_1.png) + +如果确诊为host bound,那么我们可以打开CPU侧,找出各算子的下发耗时。注意找的时候需要找出所有CPU耗时的累加值,而不能找单层,因为首次调用的耗时是很长的。例如下图的GmmSwigluQuant,CPU上首次调用需要1ms,后续每次只需要200μs。 + +![host_bound_2](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/host_bound_2.png) + +此时有的算子在负重前行,有的算子拖了后腿,后者多于了前者。我们优先**找出来host耗时大于device的top算子,这些算子是拖后腿的**,可以交予算子团队重点分析。 + +### 2.组网合理性分析 + +有的时候,模型组网没有按照最高效的方式来,这一点在profiling中是非常易于识别的,下面会介绍一下分析思路并给出例子。 + +通常来讲,LLM中的大的热点算子是Attention和FFN中的矩阵乘计算,二者加起来在prefill下可能达到计算耗时的70%+,decode下可能达到50%+。如果整体的耗时比例不符合预期,或者profiling中出现了一些新面孔,或者拼接类算子太多了,这都值得我们去分析一下模型组网,是不是使用算子的方式错了?尤其是拼接类算子,是值得我们逐一分析的。 + +对于slice/split/concat这样的拼接类算子,还有transpose/cast这种转换算子,他们的存在往往是前后算子不直接配套造成的。如果前一个算子可以直接对输出做好尾处理,往往可以节省一个算子的启动开销和一次冗余读写。但这样的改变不一定符合算子的基本设计原则。 + +举一个正例,对于某次Matmul的输出shape为[m, n0 + n1],在这后面我们接了两个slice,输入均为这个[m, n0 + n1]的tensor,输出分别为[m, n0]和[m, n1]。第一个优化的思路是将两个slice改为一个split,这样耗时可以基本减半,[m, n0 + n1]的显存也可以尽早释放。进一步优化的思路是将矩阵乘的权重从[k, n0 + n1]分割为[k, n0]和[k, n1],将原来的矩阵乘任务分成两个(前提是这两个的耗时加起来不比之前的劣化太多,分核策略不能出问题),从而彻底消除这个slice/split操作。 + +![network_1](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/network_1.png) + +举一个反例,Rmsnorm(fp16)+Cast(fp16->fp32)+Matmul(fp32),Rmsnorm虽然输入输出都是fp16,但考虑到累加运算的精度,内部是fp32做计算的。如果将Cast融到Rmsnorm内,本就内部使用fp32做计算的Rmsnorm就可以省去一个末尾fp32->fp16的cast,加上我们干掉的Cast,总共节省两个cast的同时避免了一次精度丢失。虽然这样看起来精度性能双收了,但fp16进,fp32出的Rmsnorm是反原则的(核心输入和输出需要是同数据类型),除非我们能在广大开源模型中频繁找到这样的结构,证明它的普适性,否则算子团队是不允许做这样的算子的。 + +![network_2](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/network_2.png) + +### 3.算子性能初诊 + +需要利用 `"./ASCEND_PROFILER_OUTPUT/operator_details.csv"`来做分析,从而判断算子是否有性能问题。 + +Profiling工具会统计这些流水线在不同核上的平均繁忙时间(xxx_time),与最慢核的完整kernel耗时(task_duration)做除法,得到流水线利用率(xxx_ratio)。这些流水线之间虽然互有依赖,且搬运类流水线会互抢带宽,但算子只要设计得当,是可以做到互相掩盖的。因此我们可以初步认为,**当算子的执行耗时大到一定程度上,算子应当在某一条流水线上形成bound**,即利用率要高到一定程度。经验上,在单算子耗时达到50μs时,就可以认为算子应当在bound流水线上,达成80%+的占用率了。 + +以下图为例,第一行是一个FA算子,第二行是一个Matmul算子,FA在vec流水线上达到了88.1%的利用率,Matmul算子在mac流水线上达到了89.8%的利用率,他们的性能可以认为是合格的。 + +![Operator%20performance](https://github.com/chengminhua/verl_data/raw/main/MindStudio_Insight_use/Operator%20performance.png) + +### 4.亲和shape调整 + +对于一个模型而言,超参是我们控制不了的,但我们可以控制并发度、权重格式、切分策略等因素来迎合算子,使其发挥出最大的性能,这一节主要从算子搬运效率和负载均衡两个方面出发,讨论模型侧值得尝试的调整方向。 + +#### 4.1 搬运效率亲和的shape + +mte2是一个自身效率严重受shape影响的流水线。要想让mte2保证最大搬运效率,我们需要保障如下两个条件至少满足其一: + +**(1)被搬运的矩阵使用nz作为format(最优) +(2)被搬运的矩阵的尾轴512B对齐,且不为16KB的整数倍(近似最优)** + +对于权重矩阵来说,推理阶段尤其是decode,我们通常满足(1),训练阶段我们通常满足(2)。**如果我们做不到(1),我们就要迎合(2)**。典型的手段有: + +1、如果没达成(1),矩阵的首轴是亲和的而尾轴不亲和,那么对它做transpose +2、调整TP切分策略,避免出现不亲和的尾轴 + +#### 4.2 负载均衡亲和的shape + +在算子shape不大时,受制于算子语义,我们有可能不能把所有核都利用起来,或者即使开满核,负载均衡却很差。这一小节主要是对decode阶段的小shape做分析。 + +首先,我们明确出当前NPU卡是多少核的,如果不清楚,跑出来的profiling里都是20、40这样的数,就说明是20核,反之是24核。这里我的24核其实是代表了一个cube和两个vector组成的小组,我们可以认为是一个cube作为主核,带了两个vector作为从核。如果一个算子是纯vector算子,那么就不再有组的概念,40或48个vector核会作为主核直接独立去拿逻辑任务。 + +对于LLM中的vector算子,它的一种常见分核策略有可能是分在最高维,也就是batch维,常见于对低维(也叫尾轴)有规约操作的norm类、动态量化类等算子;另一种是整体拍平,允许算子切分得非常细的算子,如elementwise算子。对于第一种,我们就可以在模型侧关注它的负载均衡问题。例如我们打48batch,而硬件却是个40个vector核,那这40个核会循环2次,第二次有多数的核会无事可做,这个batch数就可以认为是不友好的。如果将batch打到64或80,性能可以预见会是无损的。同样的情况下,如果是48核的卡,那我们可以认为这就是个非常友好的batch数。 + +对于cube类算子,它常见的分核策略是以base快去切分M和N(K轴是累加轴,对它分核会引入确定性问题)。最常见的分块是baseM=128,baseN=256。在decode阶段,我们的耗时基本可以看做都是在搬权重,这是因为激活的M极小,M方向大概率只分了一块,那么右矩阵就只需要搬一次。所以我们在M≤128的范围内可以尽情提高M,对性能都基本是无损的,如果M大于128,可以认为(128, 256]是下一个性能分档。 +除了M外,N轴切分的任务也影响算子亲和性,以deepseekR1中的MLA预处理为例,它会使用同一个激活(shape为[batch_size, 7168])与两个权重做矩阵乘(shape为[7168, 1536]和[7168, 576])。在batch_size打不大的情况下,即使baseN缩短为128,N轴都不能用满核数,所以此时这两个矩阵乘各自的耗时,会约等于将他们权重N轴拼起来乘(shape为[7168, 2112])的矩阵乘的耗时。如果仅考虑模型竞争力,我们更希望对这两个权重做合并,否则两个小的矩阵乘带宽利用率都会非常差。 + +对于Attention算子,它常见的分核策略是q_seqlen、batch_size和kv_headnum。增量阶段q_seqlen会以MTP和GQA倍数做合并,但是通常也不会大过128,划分不出第二个任务,那么并行度基本就是batch_size * kv_headnum。 + +总的来说,我们可以依据shape信息和算子类别,对算子是否有负载均衡问题作出识别,从而对我们切分策略选择,最高吞吐量的batch策略作出预判。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_en.rst b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_en.rst new file mode 100644 index 0000000000000000000000000000000000000000..22e82fa9bac77679f3f584bc6766cec1ddbc20ac --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_en.rst @@ -0,0 +1,496 @@ +Profiling Data Collection Guide +================================================================================== + +Last updated: 07/13/2026. + +This is a tutorial for data collection using the GRPO or DAPO algorithm based on the FSDP or MindSpeed (Megatron) backend on Ascend devices. + +Configuration +------------- + +Use two levels of profile settings to control data collection + +- Global collection control: Use parameters in verl/trainer/config/ppo_trainer.yaml (FSDP) or verl/trainer/config/ppo_megatron_trainer.yaml (MindSpeed) to control the collection mode and steps. +- Role profile control: Use parameters in each role to control various parameters. + +Global Collection Control +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Use parameters in ppo_trainer.yaml to control the collection steps and mode: + +- global_profiler: Control the ranks and mode of profiling + + - tool: The profiling tool to use, options are nsys, npu, torch, + torch_memory. + + - nsys: NVIDIA's official system-level performance analysis tool. + - npu: Huawei Ascend chip's native performance analysis tool. + - torch: PyTorch framework's built-in profiler. + - torch_memory: PyTorch's memory trace analyzer (based on memory history snapshot functionality). + + - steps: This parameter can be set as a list that has + collection steps, such as [2, 4], which means it will collect steps 2 + and 4. If set to null, no collection occurs. + - save_path: The path to save the collected data. Default is + "outputs/profile". + +Role Profiler Control +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +In each role's ``profiler`` field, you can control the collection mode for that role. + +- enable: Whether to enable profiling for this role. +- all_ranks: Whether to collect data from all ranks. +- ranks: A list of ranks to collect data from. If empty, no data is collected. +- tool_config: Configuration for the profiling tool used by this role. + +Use parameters in each role's ``profiler.tool_config.npu`` to control specific collection behavior: + +- level: Collection level - options are level_none, level0, level1, and level2 + + - level_none: Disables all level-based data collection (turns off profiler_level). + - level0: Collects high-level application data, underlying NPU data, and operator execution details on NPU. After balancing data volume and analytical capability, level0 is the recommended default configuration. + - level1: Extends level0 by adding CANN-layer AscendCL data and AI Core performance metrics on NPU. + - level2: Extends level1 by adding CANN-layer Runtime data and AI CPU metrics. + +- contents: A list of options to control the collection content, for example + npu, cpu, memory, shapes, module, stack. + + - npu: Whether to collect device-side performance data. + - cpu: Whether to collect host-side performance data. + - memory: Whether to enable memory analysis. + - shapes: Whether to record tensor shapes. + - module: Whether to record framework-layer Python call stack information. Compared to stack, it is recommended to use module for recording call stack information, as it incurs lower performance overhead. + - stack: Whether to record operator call stack information. + +- analysis: Whether to enable automatic data parsing. +- discrete: Whether to use discrete mode. +- profile_token_start: Effective only for the rollout role; defines the start response-token index for rollout decoding collection. It is applied only when valid (0-based, ``profile_token_end > profile_token_start``, and the window is within response length). +- profile_token_end: Effective only for the rollout role; defines the stop response-token index (exclusive) for rollout decoding collection. It is applied only when valid (0-based, ``profile_token_end > profile_token_start``, and the window is within response length). + +Examples +-------- + +Disabling Collection +~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: null # disable profile + +End-to-End Collection +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: [1, 2, 5] + save_path: ./outputs/profile + actor_rollout_ref: + actor: # Set the profiler collection configuration parameters for the actor role + profiler: + enable: True + all_ranks: True + tool_config: + npu: + discrete: True + contents: [npu, cpu] # Control collection list, default cpu, npu; can configure memory, shapes, module, etc. + +Separation of Training and Inference Phases +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: [1, 2, 5] + save_path: ./outputs/profile + actor_rollout_ref: + actor: + profiler: + enable: True # Set to True to collect the training phase + all_ranks: False + ranks: [0] # Global Rank 0 + tool_config: + npu: + discrete: True + contents: [npu, cpu] + rollout: + profiler: + enable: True # Set to True to collect the inference phase + all_ranks: False + ranks: [0] # In Agent Loop mode, this refers to the Replica Rank of the inference instance (e.g., the 0th instance) + tool_config: + npu: + discrete: True # Discrete mode must be enabled in Agent Loop mode + # Optional: lightweight collection of inference data, collecting by response token interval; when start/stop are not set, the entire rollout phase is collected + profile_token_start: 30 + profile_token_end: 60 + # ref follow actor settings + +Quick Start +----------- + +Disabling Collection +~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.steps=null + +End-to-End Collection +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.tool=npu + global_profiler.steps="[1, 2, 5]" # Collection steps + global_profiler.save_path=./outputs/profile + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.ranks="[0]" # Only collect rank 0 + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True # Discrete mode is recommended, data of each phase is stored separately + actor_rollout_ref.actor.profiler.tool_config.npu.contents="['npu','cpu']" # Control collection list, default cpu, npu; can configure memory, shapes, module, etc. + actor_rollout_ref.actor.profiler.tool_config.npu.level=level1 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=False # Disable automatic data parsing + # rollout & ref follow actor settings + + +Lightweight Collection of Inference Data +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.tool=npu + global_profiler.steps="[1, 2, 5]" # Collection steps + global_profiler.save_path=./outputs/profile + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.ranks="[0]" # Only collect rank 0 + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True # Discrete mode is recommended, data of each phase is stored separately + actor_rollout_ref.actor.profiler.tool_config.npu.contents="['npu','cpu']" # Control collection list, default cpu, npu; can configure memory, shapes, module, etc. + actor_rollout_ref.actor.profiler.tool_config.npu.level=level1 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=False # Disable automatic data parsing + + actor_rollout_ref.rollout.profiler.enable=True + actor_rollout_ref.rollout.profiler.all_ranks=False + actor_rollout_ref.rollout.profiler.ranks="[0]" # Only collect rank 0 data. + # Optional: lightweight collection of inference data, If start and stop are not set, the entire rollout phase is collected. + actor_rollout_ref.rollout.profiler.tool_config.npu.profile_token_start=30 + actor_rollout_ref.rollout.profiler.tool_config.npu.profile_token_end=60 + # ref follow actor settings + +**Agent Loop Mode Description**: + +In `Agent Loop <../advance/agent_loop.rst>`_ mode, performance data for the Rollout phase **must be collected using discrete mode**. In this case, the Profiler is triggered by the inference engine backend. + +1. Rank Definition: ranks in the Rollout configuration refers to the Replica Rank (inference instance index), not the Global Rank. + +2. Inference Engine Support: Currently, vLLM and SGLang engines are supported without additional settings. Specific details are as follows: + + - vLLM Engine: Automatically collects AsyncLLM scheduling stacks and inference process performance data. Does not support setting analysis (defaults to no analysis, requires offline analysis) and profiler_level (defaults to level1). + - SGLang Engine: Automatically collects inference process performance data. Does not support the memory option in contents. Does not support setting analysis (defaults to enabled) and profiler_level (defaults to level0). + +**Fully Async Policy Mode Description**: + +1. In `Fully Async Policy `_ mode, ``global_profiler.steps`` refers to the step after each ``update_weights`` round, which is consistent with synchronous mode, not a per mini-batch step within a single training round. + +2. Because it reuses AgentLoop collection capabilities, the notes for `Fully Async Policy `_ mode are the same as for AgentLoop. + +Visualization +------------- + +Collected data is stored in the user-defined save_path and can be visualized using the `MindStudio Insight `_ tool. + +Additionally, in a Linux environment, the MindStudio Insight tool is provided in the form of a `JupyterLab Plugin `_, offering a more intuitive and highly interactive user interface. The advantages of the JupyterLab plugin are as follows: + +- Seamless integration: Supports running the MindStudio Insight tool directly within the Jupyter environment, eliminating the need to switch platforms or copy data from the server, enabling data to be collected and used immediately. +- Fast startup: Allows MindStudio Insight to be launched quickly via the JupyterLab command line or graphical interface. +- Smooth operation: In a Linux environment, launching MindStudio Insight through JupyterLab effectively resolves lag issues compared to full-package communication, significantly improving the operation experience. +- Remote access: Supports remotely launching MindStudio Insight. Users can connect to the service via a local browser for direct visual analysis, reducing the difficulty of uploading and downloading data during large-model training or inference. + +If the analysis parameter is set to False, offline parsing is required after data collection: + +.. code:: python + + import torch_npu + # Set profiler_path to the parent directory of the "localhost.localdomain___ascend_pt" folder + torch_npu.profiler.profiler.analyse(profiler_path=profiler_path) + + +Advanced Guide: Fine-grained Collection +--------------------------------------- + +Background and Challenges +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Although the configuration-based collection method mentioned above is convenient, it faces challenges in training scenarios with **Long Context** or **Large Global Batch Size**. +Within a complete training step (Step), model computation exhibits high-frequency and repetitive characteristics: + +1. Rollout phase: Sequence generation (Generate Sequence) is an autoregressive process involving thousands of forward computations of the Decoder model. +2. Training phase: To control peak memory usage, verl typically adopts a Micro-Batch strategy, dividing large data streams into multiple micro-batches for computation. + + - compute_log_prob (Actor/Ref): Involves multiple rounds of pure forward propagation. + - update_policy (Actor/Critic): Involves multiple rounds of forward and backward propagation. + +This characteristic leads to massive and repetitive operator records from full profiling. As shown in the image below: + +.. image:: https://raw.githubusercontent.com/mengchengTang/verl-data/master/verl_ascend_profiler.png + +Even with ``discrete`` mode enabled, performance data files for a single stage can still reach several TB, leading to **parsing failures** or **visualization tool lag**. + +Solution: Critical Path Sampling +~~~~~~~~~~~~~~~~~~~~~~ + +To solve the above problems, we can adopt a **Critical Path Sampling** strategy: Based on the API interface provided by `torch_npu.profiler `_ , directly modify Python source code to collect only representative data segments (such as specific Decode Steps or the first Micro-Batch). + + **Important Notes** + + 1. This chapter involves direct source code modification. It is recommended to back up files before modification and restore them after debugging. + 2. When using code instrumentation for collection, be sure to **disable global collection** (``global_profiler: steps: null``) in ``ppo_trainer.yaml`` or ``ppo_megatron_trainer.yaml`` to avoid Profiler conflicts. + +1. Add Script to Control Collection Granularity +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + export PROFILE_STEP=2 # Collect specified steps + export ROLLOUT_PROFILE=true + export UPDATE_PROFILE=true + export WITH_MODULES=false # Collect Python call stack + export WITH_STACK=false # Collect operator call stack + export WITH_MEMORY=false # Collect memory + export WITH_SHAPE=true # Collect tensor shapes + export PROFILE_RANKS=0 # Collect rank 0 + export UPDATE_PROFILE_PATH="./outputs/update_profile" + export ROLLOUT_PROFILE_PATH="./outputs/rollout_profile" + +2. Fine-grained Collection in Rollout Phase +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +For vLLM or SGLang inference engines, we can control the ``schedule`` parameter to collect model forward propagation performance data for specific tokens. + +**vLLM Engine** + +- **Reference Version**: vLLM v0.18.0, vLLM-Ascend v0.18.1 +- **Modified File**: ``vllm-ascend/vllm_ascend/worker/worker.py`` + +.. code-block:: diff + + class NPUWorker(WorkerBase): + + def __init__(self, *args, **kwargs): + # ... existing code ... + + # Profile collection + + import os + + import torch_npu + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + # Initialize profiler + + import torch_npu + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.profiler_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # Skip the first 29 steps, warmup 1 step, collect 30 steps, repeat 1 time. + + schedule=torch_npu.profiler.schedule(wait=29, warmup=1, active=30, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('ROLLOUT_PROFILE_PATH'), analyse_flag=True) # Data save path, whether to parse online + + ) + + self.profiler_npu.start() + # ... existing code ... + + def execute_model(self, scheduler_output=None, intermediate_tensors=None, **kwargs): + # ... existing code ... + output = self.model_runner.execute_model(scheduler_output, + intermediate_tensors) + + + import os + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + self.profiler_npu.step() # Drive schedule to collect partial decode steps + + # ... existing code ... + +**SGLang Engine** + +- **Reference Version**: SGLang master branch +- **Modified File**: ``sglang/python/sglang/srt/model_executor/model_runner.py`` + +.. code-block:: diff + + # ... existing imports ... + + import torch_npu + + class ModelRunner: + + def __init__(self, *args, **kwargs): + # ... existing init code ... + + # Profile collection + + import os + + import torch_npu + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + # Initialize profiler + + import torch_npu + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.profiler_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # Skip the first 29 steps, warmup 1 step, collect 30 steps, repeat 1 time. + + schedule=torch_npu.profiler.schedule(wait=29, warmup=1, active=30, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('ROLLOUT_PROFILE_PATH'), analyse_flag=True) # Data save path, whether to parse online + + ) + + self.profiler_npu.start() + def forward(self, forward_batch, **kwargs): + # ... existing code ... + + + import os + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + self.profiler_npu.step() # Drive schedule to collect partial decode steps + + return output + +3. Fine-grained Collection in update_policy (Actor & Critic) Phase +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The Update phase includes forward and backward propagation. In the unified model engine, mini-batch iteration is driven by +``TrainingWorker.train_mini_batch`` in ``verl/workers/engine_workers.py``, +which calls ``train_batch`` for each mini-batch. + +**FSDP Backend** + +The FSDP backend supports collection at both Mini-Batch and Micro-Batch granularities. +For Mini-Batch scope, instrument ``TrainingWorker.train_mini_batch``; +For Micro-Batch scope, instrument the micro-batch loop inside the FSDP engine's +``forward_backward_batch``. + +- **Modified File**: ``verl/workers/engine_workers.py`` + (``TrainingWorker.train_mini_batch``, Mini-Batch granularity) or + ``verl/workers/engine/fsdp/transformer_impl.py`` + (``FSDPEngineWithLMHead.forward_backward_batch``, Micro-Batch granularity) + +.. code-block:: diff + + class TrainingWorker(Worker, DistProfilerExtension): + + def __init__(self, config: TrainingWorkerConfig): + # ... + + self.step = 1 + + def train_mini_batch(self, data: TensorDict) -> TensorDict: + # ... + + + import os + + import torch_npu + + if self.step == int(os.environ.get('PROFILE_STEP', 1)) and os.environ.get('UPDATE_PROFILE', "false") == "true": + + # Prepare profiler + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.prof_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # Only collect the first Mini Batch (including all Micro-Batch computations and one optimizer update) + + schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('UPDATE_PROFILE_PATH'), analyse_flag=True) + + ) + + if str(torch.distributed.get_rank()) in os.environ.get('PROFILE_RANKS', "0").split(','): + + self.prof_npu.start() + + for batch_idx, mini_batch_td in enumerate(dataloader): + # ... internally calls self.train_batch(mini_batch_td), which in the engine + # runs Forward & Backward on each micro-batch and completes one optimizer update ... + actor_output = self.train_batch(mini_batch_td) + + + if self.step == int(os.environ.get('PROFILE_STEP', 1)) and os.environ.get('UPDATE_PROFILE', "false") == "true": + + # Drive schedule to collect mini batch; for micro-batch granularity, move self.prof_npu.step() into the micro_batch loop + + if str(torch.distributed.get_rank()) in os.environ.get('PROFILE_RANKS', "0").split(','): + + self.prof_npu.step() + + # This mini batch ends + + self.step += 1 + + +**Megatron Backend** + +The Megatron backend supports collection at the Mini-Batch granularity, with the same entry point +``TrainingWorker.train_mini_batch``: The Megatron engine internally runs Megatron +pipeline-parallel forward/backward schedule and one optimizer step. + +- **Modified File**: ``verl/workers/engine_workers.py`` + (``TrainingWorker.train_mini_batch``) -- identical to the FSDP snippet above; + it is recommended to rename the output directory (e.g. ``./outputs/megatron_actor_update_profile``) + to distinguish traces from different backends. + + +4. Fine-grained Collection in compute_log_prob (Actor & Ref) Phase +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +This phase computes probability distributions for new and old policies. In the unified model engine, both actor and ref log-prob +computation goes through ``TrainingWorker.infer_batch``, which dispatches to the corresponding backend engine +``BaseEngine.infer_batch``. + +**FSDP Backend** + +The FSDP backend allows fine-grained control at the Micro-Batch level. Instrument the micro-batch loop inside the FSDP engine forward pass. + + +- **Modified File**: ``verl/workers/engine/fsdp/transformer_impl.py`` + (``FSDPEngineWithLMHead.forward_backward_batch`` / ``forward_step``) + +.. code-block:: diff + + # ... import dependencies ... + + import torch_npu + + class FSDPEngineWithLMHead(FSDPEngine): + + def forward_backward_batch(self, data: TensorDict, loss_function, forward_only=False): + + + role = "Ref" if forward_only and not self.optimizer_config else "Actor" + + # Prepare profiler (same configuration as above, omitted) + + experimental_config = torch_npu.profiler._ExperimentalConfig(...) + + self.prof_npu = torch_npu.profiler.profile( + + # ... (same configuration as above, omitted) + + # wait=0, warmup=0, active=1: directly collect first micro-batch + + schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(f"./outputs/{role}_compute_log_prob", analyse_flag=True) + + ) + + + # forward_backward_batch is shared by ref and actor; use the role flag to distinguish; + + # To collect actor_compute_log_prob, switch to role == "Actor": + + if role == "Ref": + + self.prof_npu.start() + + for micro_batch in micro_batches: + + # ... original computation logic ... + with torch.no_grad(): + output = self.forward_step(micro_batch, loss_function, forward_only=True) + + + # Drive schedule to collect micro batch + + if role == "Ref": + + self.prof_npu.step() + + # ... + + +**Megatron Backend** + +The Micro-Batch scheduling in the Megatron backend is managed internally +by Megatron's pipeline-parallel ``forward_backward_func`` and does not +currently support fine-grained collection at the Micro-Batch level +through simple code instrumentation. It is recommended to use the global +profiler configuration for collection. diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_zh.rst b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_zh.rst new file mode 100644 index 0000000000000000000000000000000000000000..2e1cd4d56da5691de9769033179e4eb1e70ddefc --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/ascend_profiling_zh.rst @@ -0,0 +1,494 @@ +Profiling采集指导 +================================================================================== + +Last updated: 07/13/2026. + +这是一份在昇腾设备上基于FSDP或MindSpeed(Megatron)后端,使用GRPO或DAPO算法进行数据采集的教程。 + +配置 +---- + +使用两级profile设置来控制数据采集 + +- 全局采集控制:使用verl/trainer/config/ppo_trainer.yaml(FSDP),或verl/trainer/config/ppo_megatron_trainer.yaml(MindSpeed)中的配置项控制采集的模式和步数。 +- 角色profile控制:通过每个角色中的配置项控制采集等参数。 + +全局采集控制 +~~~~~~~~~~~~ + +通过 ppo_trainer.yaml 中的参数控制采集步数和模式: + +- global_profiler: 控制采集的rank和模式 + + - tool: 使用的采集工具,选项有 nsys、npu、torch、torch_memory。 + + - nsys: NVIDIA 官方的系统级性能分析工具。 + - npu: 华为昇腾(Ascend)芯片的原生性能分析工具。 + - torch: PyTorch 框架内置的性能分析器。 + - torch_memory: PyTorch 的显存轨迹分析器(基于显存历史快照功能)。 + + - steps: 此参数可以设置为包含采集步数的列表,例如 [2, 4],表示将采集第2步和第4步。如果设置为 null,则不进行采集。 + - save_path: 保存采集数据的路径。默认值为 "outputs/profile"。 + +角色profiler控制 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +在每个角色的 ``profiler`` 字段中,您可以控制该角色的采集模式。 + +- enable: 是否为此角色启用性能分析。 +- all_ranks: 是否从所有rank收集数据。 +- ranks: 要收集数据的rank列表。如果为空,则不收集数据。 +- tool_config: 此角色使用的性能分析工具的配置。 + +通过每个角色的 ``profiler.tool_config.npu`` 中的参数控制具体采集行为: + +- level: 采集级别——选项有 level_none、level0、level1 和 level2 + + - level_none: 禁用所有基于级别的数据采集(关闭 profiler_level)。 + - level0: 采集高级应用数据、底层NPU数据和NPU上的算子执行详情。在权衡数据量和分析能力后,level0是推荐的默认配置。 + - level1: 在level0基础上增加CANN层AscendCL数据和NPU上的AI Core性能指标。 + - level2: 在level1基础上增加CANN层Runtime数据和AI CPU指标。 + +- contents: 控制采集内容的选项列表,例如 + npu、cpu、memory、shapes、module、stack。 + + - npu: 是否采集设备端性能数据。 + - cpu: 是否采集主机端性能数据。 + - memory: 是否启用内存分析。 + - shapes: 是否记录张量形状。 + - module: 是否记录框架层Python调用栈信息。相较于stack,更推荐使用module记录调用栈信息,因其产生的性能膨胀更低。 + - stack: 是否记录算子调用栈信息。 + +- analysis: 是否启用自动数据解析。 +- discrete: 是否使用离散模式。 +- profile_token_start:仅在 rollout role 下生效,用于指定 rollout 解码阶段的采集起始 response token;参数合法时生效(从 0 开始,满足 ``profile_token_end > profile_token_start``,且区间在 response 长度内)。 +- profile_token_end:仅在 rollout role 下生效,用于指定 rollout 解码阶段的采集结束 response token(右边界不包含);参数合法时生效(从 0 开始,满足 ``profile_token_end > profile_token_start``,且区间在 response 长度内)。 + +示例 +---- + +禁用采集 +~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: null # disable profile + +端到端采集 +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: [1, 2, 5] + save_path: ./outputs/profile + actor_rollout_ref: + actor: # 设置 actor role 的 profiler 采集配置参数 + profiler: + enable: True + all_ranks: True + tool_config: + npu: + discrete: True + contents: [npu, cpu] # 控制采集列表,默认cpu、npu,可配置memory、shapes、module等 + +训练和推理阶段分离 +~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + global_profiler: + steps: [1, 2, 5] + save_path: ./outputs/profile + actor_rollout_ref: + actor: + profiler: + enable: True # 设置为 True 以采集训练阶段 + all_ranks: False + ranks: [0] # 全局 Rank 0 + tool_config: + npu: + discrete: True + contents: [npu, cpu] + rollout: + profiler: + enable: True # 设置为 True 以采集推理阶段 + all_ranks: False + ranks: [0] # 在 Agent Loop 模式下,此处指推理实例的 Replica Rank (例如第 0 个实例) + tool_config: + npu: + discrete: True # Agent Loop 模式下必须开启离散模式 + # 可选:轻量化采集推理数据,按 response token 区间采集;不设置 start/stop 时采集整个 rollout 阶段 + profile_token_start: 30 + profile_token_end: 60 + # ref follow actor settings + +快速开始 +-------- + +禁用采集 +~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.steps=null + +端到端采集 +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.tool=npu + global_profiler.steps="[1, 2, 5]" # 采集步数 + global_profiler.save_path=./outputs/profile + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.ranks="[0]" # 只采集rank0 + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True # 推荐使用离散模式,各阶段数据分开存储 + actor_rollout_ref.actor.profiler.tool_config.npu.contents="['npu','cpu']" # 控制采集列表,默认cpu、npu,可配置memory、shapes、module等 + actor_rollout_ref.actor.profiler.tool_config.npu.level=level1 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=False # 禁用自动数据解析 + # rollout & ref follow actor settings + + +轻量化采集推理数据 +~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + global_profiler.tool=npu + global_profiler.steps="[1, 2, 5]" # 采集步数 + global_profiler.save_path=./outputs/profile + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.ranks="[0]" # 只采集rank0 + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True # 推荐使用离散模式,各阶段数据分开存储 + actor_rollout_ref.actor.profiler.tool_config.npu.contents="['npu','cpu']" # 控制采集列表,默认cpu、npu,可配置memory、shapes、module等 + actor_rollout_ref.actor.profiler.tool_config.npu.level=level1 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=False # 禁用自动数据解析 + + actor_rollout_ref.rollout.profiler.enable=True + actor_rollout_ref.rollout.profiler.all_ranks=False + actor_rollout_ref.rollout.profiler.ranks="[0]" # 只采集rank0 + # 可选:轻量化采集推理数据,按 response token 区间采集;不设置 start/stop 时采集整个 rollout 阶段 + actor_rollout_ref.rollout.profiler.tool_config.npu.profile_token_start=30 + actor_rollout_ref.rollout.profiler.tool_config.npu.profile_token_end=60 + # ref follow actor settings + +**Agent Loop 模式说明**: + +在 `Agent Loop <../advance/agent_loop.rst>`_ 模式下,Rollout 阶段的性能数据 **必须使用离散模式** 采集,此时 Profiler 由推理引擎后端触发。 + +1. Rank 定义:Rollout 配置中的 ranks 指代 Replica Rank(推理实例索引),而非全局 Rank。 + +2. 推理引擎支持:当前支持vLLM和SGLang引擎,无需额外设置。具体说明如下: + + - vLLM 引擎:自动采集 AsyncLLM 调度栈及推理进程性能数据。不支持设置 analysis(默认不解析,需离线解析)和 profiler_level(默认 level1)。 + - SGLang 引擎:自动采集推理进程性能数据。不支持 contents 中的 memory 配置项。不支持设置 analysis(默认解析)和 profiler_level(默认 level0)。 + +**Fully Async Policy 模式说明**: + +1. 在 `Fully Async Policy `_ 模式下,`global_profiler.steps` 代表每一轮`update_weights`后的`step`,这点和同步模式下保持同步,而非单轮的`mini-batch step`。 + +2. 因为复用AgentLoop采集能力,因此在 `Fully Async Policy `_ 模式下的注意事项和AgentLoop相同。 + +可视化 +------ + +采集后的数据存放在用户设置的save_path下,可通过 `MindStudio Insight `_ 工具进行可视化。 + +另外在Linux环境下,MindStudio Insight工具提供了 `JupyterLab插件 `_ 形态,提供更直观和交互性强的操作界面。JupyterLab插件优势如下: + +- 无缝集成:支持在Jupyter环境中直接运行MindStudio Insight工具,无需切换平台,无需拷贝服务器上的数据,实现数据即采即用。 +- 快速启动:通过JupyterLab的命令行或图形界面,可快速启动MindStudio Insight工具。 +- 运行流畅:在Linux环境下,通过JupyterLab环境启动MindStudio Insight,相较于整包通信,有效解决了运行卡顿问题,操作体验显著提升。 +- 远程访问:支持远程启动MindStudio Insight,可通过本地浏览器远程连接服务直接进行可视化分析,缓解了大模型训练或推理数据上传和下载的困难。 + +如果analysis参数设置为False,采集之后需要进行离线解析: + +.. code:: python + + import torch_npu + # profiler_path请设置为"localhost.localdomain___ascend_pt"目录的上一级目录 + torch_npu.profiler.profiler.analyse(profiler_path=profiler_path) + + +进阶指南:精细化采集 +-------------------- + +背景与挑战 +~~~~~~~~~~ + +上述基于配置文件的采集方式虽然便捷,但在 **长序列 (Long Context)** 或 **大全局批量 (Large Global Batch Size)** 的训练场景中面临挑战。 +在一个完整的训练步 (Step) 内,模型计算呈现出高频次、重复性的特征: + +1. Rollout 阶段:序列生成 (Generate Sequence) 是一个自回归过程,涉及成千上万次 Decoder 模型的前向计算。 +2. Training 阶段:为了控制显存峰值,verl 通常采用 Micro-Batch 策略,将庞大的数据流切分为多个微批次进行计算。 + + - compute_log_prob (Actor/Ref):涉及多轮纯前向传播。 + - update_policy (Actor/Critic):涉及多轮前向与反向传播。 + +这种特性会导致全量 Profiling 产生海量且重复的算子记录。如下图所示: + +.. image:: https://raw.githubusercontent.com/mengchengTang/verl-data/master/verl_ascend_profiler.png + :alt: 全量 Profiling 产生海量重复算子记录示意图 + +即使使用了 ``discrete`` 模式,单个阶段的性能数据文件仍可能达到数 TB,导致 **解析失败** 或 **可视化工具卡顿**。 + +解决方案:关键路径采样 +~~~~~~~~~~~~~~~~~~~~~~ + +为了解决上述问题,我们可以采用 **关键路径采样** 策略:基于 `torch_npu.profiler `_ 提供的API接口,直接修改 Python 源码,仅采集具有代表性的数据片段(如特定 Decode Step 或首个 Micro-Batch)。 + + **重要提示** + + 1. 本章节涉及直接修改源码。建议修改前备份文件,调试完成后恢复。 + 2. 使用代码插桩采集时,请务必在 ``ppo_trainer.yaml`` 或 ``ppo_megatron_trainer.yaml`` 中**禁用全局采集** (``global_profiler: steps: null``),以避免 Profiler 冲突。 + +1. 添加脚本控制采集粒度 +~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + export PROFILE_STEP=2 # 采集指定步数 + export ROLLOUT_PROFILE=true + export UPDATE_PROFILE=true + export WITH_MODULES=false # 采集python调用栈 + export WITH_STACK=false # 采集算子调用栈 + export WITH_MEMORY=false # 采集内存 + export WITH_SHAPE=true # 采集张量形状 + export PROFILE_RANKS=0 # 采集rank0 + export UPDATE_PROFILE_PATH="./outputs/update_profile" + export ROLLOUT_PROFILE_PATH="./outputs/rollout_profile" + +2. Rollout 阶段精细化采集 +~~~~~~~~~~~~~~~~~~~~~~~~~ + +对于 vLLM 或 SGLang 推理引擎,我们可以通过控制 ``schedule`` 参数来控制采集模型在特定token的前向传播性能数据。 + +**vLLM 引擎** + +- **参考版本**:vLLM v0.18.0, vLLM-Ascend v0.18.1 +- **修改文件**:``vllm-ascend/vllm_ascend/worker/worker.py`` + +.. code-block:: diff + + class NPUWorker(WorkerBase): + + def __init__(self, *args, **kwargs): + # ... existing code ... + + + # profile采集 + + import os + + import torch_npu + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + # Initialize profiler + + import torch_npu + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.profiler_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # 跳过前29步,warmup一步,采集30步,重复1次。 + + schedule=torch_npu.profiler.schedule(wait=29, warmup=1, active=30, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('ROLLOUT_PROFILE_PATH'), analyse_flag=True) # 采集数据保存路径,是否在线解析 + + ) + + self.profiler_npu.start() + + # ... existing code ... + + def execute_model(self, scheduler_output=None, intermediate_tensors=None, **kwargs): + # ... existing code ... + output = self.model_runner.execute_model(scheduler_output, + intermediate_tensors) + + + import os + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + self.profiler_npu.step() # 驱动 schedule,对部分decode step进行采集 + + # ... existing code ... + +**SGLang 引擎** + +- **参考版本**:SGLang master 分支 +- **修改文件**:``sglang/python/sglang/srt/model_executor/model_runner.py`` + +.. code-block:: diff + + # ... existing imports ... + + import torch_npu + + class ModelRunner: + + def __init__(self, *args, **kwargs): + # ... existing init code ... + + + # profile采集 + + import os + + import torch_npu + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + # Initialize profiler + + import torch_npu + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.profiler_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # 跳过前29步,warmup一步,采集30步,重复1次。 + + schedule=torch_npu.profiler.schedule(wait=29, warmup=1, active=30, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('ROLLOUT_PROFILE_PATH'), analyse_flag=True) # 采集数据保存路径,是否在线解析 + + ) + + self.profiler_npu.start() + + def forward(self, forward_batch, **kwargs): + # ... existing code ... + + + import os + + if os.environ.get('ROLLOUT_PROFILE', "false") == "true": + + self.profiler_npu.step() # 驱动 schedule,对部分decode step进行采集 + + return output + +3. update_policy (Actor & Critic) 阶段精细化采集 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Update 阶段包含前向和反向传播。统一模型引擎下,mini-batch 循环由 +``verl/workers/engine_workers.py`` 中的 ``TrainingWorker.train_mini_batch`` +驱动,它会对每个 mini-batch 调用 ``train_batch``。 + +**FSDP 后端** + +FSDP 后端支持设置对 Mini-Batch 和 Micro-Batch 的粒度进行采集。 +Mini-Batch 级别请插桩 ``TrainingWorker.train_mini_batch``; +Micro-Batch 级别请插桩 FSDP 引擎的 ``forward_backward_batch`` 中的 +micro-batch 循环。 + +- **修改文件**:``verl/workers/engine_workers.py`` + (``TrainingWorker.train_mini_batch``,Mini-Batch 粒度)或 + ``verl/workers/engine/fsdp/transformer_impl.py`` + (``FSDPEngineWithLMHead.forward_backward_batch``,Micro-Batch 粒度) + +.. code-block:: diff + + class TrainingWorker(Worker, DistProfilerExtension): + + def __init__(self, config: TrainingWorkerConfig): + # ... + + self.step = 1 + + def train_mini_batch(self, data: TensorDict) -> TensorDict: + # ... + + + import os + + import torch_npu + + if self.step == int(os.environ.get('PROFILE_STEP', 1)) and os.environ.get('UPDATE_PROFILE', "false") == "true": + + # 准备 profiler + + experimental_config = torch_npu.profiler._ExperimentalConfig( + + profiler_level=torch_npu.profiler.ProfilerLevel.Level1, + + ) + + self.prof_npu = torch_npu.profiler.profile( + + activities=[torch_npu.profiler.ProfilerActivity.CPU, torch_npu.profiler.ProfilerActivity.NPU], + + with_modules=os.environ.get('WITH_MODULES', "false") == "true", + + profile_memory=os.environ.get('WITH_MEMORY', "false") == "true", + + record_shapes=os.environ.get('WITH_SHAPE', "false") == "true", + + with_stack=os.environ.get('WITH_STACK', "false") == "true", + + experimental_config=experimental_config, + + # 仅采集第一个 Mini Batch(包含所有 Micro-Batch 的计算和一次优化器更新) + + schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(os.environ.get('UPDATE_PROFILE_PATH'), analyse_flag=True) + + ) + + if str(torch.distributed.get_rank()) in os.environ.get('PROFILE_RANKS', "0").split(','): + + self.prof_npu.start() + + for batch_idx, mini_batch_td in enumerate(dataloader): + # ... 内部调用 self.train_batch(mini_batch_td),后者在引擎内部 + # 对每个 micro-batch 执行 Forward & Backward,并完成一次优化器更新 ... + actor_output = self.train_batch(mini_batch_td) + + + if self.step == int(os.environ.get('PROFILE_STEP', 1)) and os.environ.get('UPDATE_PROFILE', "false") == "true": + + # 驱动 schedule,对mini batch进行采集,如果想对micro batch进行,则将self.prof_npu.step()移动到micro_batch的循环内 + + if str(torch.distributed.get_rank()) in os.environ.get('PROFILE_RANKS', "0").split(','): + + self.prof_npu.step() + + # 此mini batch结束 + + self.step += 1 + + +**Megatron 后端** + +Megatron 后端支持以 Mini-Batch 的粒度进行采集,入口同样是 +``TrainingWorker.train_mini_batch``:Megatron 引擎内部会调用 Megatron 的 +流水并行 forward/backward 调度并执行一次优化器 step。 + +- **修改文件**:``verl/workers/engine_workers.py`` + (``TrainingWorker.train_mini_batch``)—— 与上方 FSDP 代码片段完全一致, + 建议将输出目录改名(例如 ``./outputs/megatron_actor_update_profile``) + 以区分不同后端的 trace。 + +4. compute_log_prob (Actor & Ref) 阶段精细化采集 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +该阶段计算新旧策略的概率分布。统一模型引擎下,actor 和 ref 的 log-prob +计算都走 ``TrainingWorker.infer_batch``,最终分发到对应后端引擎的 +``BaseEngine.infer_batch`` 上。 + +**FSDP 后端** + +FSDP 后端允许在 Micro-Batch 级别进行精细控制,可在 FSDP 引擎 forward 过程 +的 micro-batch 循环内插桩。 + +- **修改文件**:``verl/workers/engine/fsdp/transformer_impl.py`` + (``FSDPEngineWithLMHead.forward_backward_batch`` / ``forward_step``) + +.. code-block:: diff + + # ... 引入依赖 ... + + import torch_npu + + class FSDPEngineWithLMHead(FSDPEngine): + + def forward_backward_batch(self, data: TensorDict, loss_function, forward_only=False): + + + role = "Ref" if forward_only and not self.optimizer_config else "Actor" + + # 准备 profiler (配置同上,略) + + experimental_config = torch_npu.profiler._ExperimentalConfig(...) + + self.prof_npu = torch_npu.profiler.profile( + + # ... (配置同上,略) + + # wait=0, warmup=0, active=1: 直接采集第一个 micro-batch + + schedule=torch_npu.profiler.schedule(wait=0, warmup=0, active=1, repeat=1), + + on_trace_ready=torch_npu.profiler.tensorboard_trace_handler(f"./outputs/{role}_compute_log_prob", analyse_flag=True) + + ) + + + # forward_backward_batch 被 ref 和 actor 共用,通过 role 标志位区分; + + # 如需采集 actor_compute_log_prob,可改为 role == "Actor": + + if role == "Ref": + + self.prof_npu.start() + + for micro_batch in micro_batches: + + # ... 原始计算逻辑 ... + with torch.no_grad(): + output = self.forward_step(micro_batch, loss_function, forward_only=True) + + + # 驱动 schedule,对micro batch进行采集 + + if role == "Ref": + + self.prof_npu.step() + + # ... + + +**Megatron 后端** + +Megatron 后端的 Micro-Batch 调度由 Megatron 的流水并行 +``forward_backward_func`` 内部管理,暂不支持通过简单的代码插桩进行 +Micro-Batch 级别的精细化采集。建议使用全局 profiler 配置进行采集。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/perf_tuning_on_ascend.rst b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/perf_tuning_on_ascend.rst new file mode 100644 index 0000000000000000000000000000000000000000..56b5573865bd73c943ef214756746d2e2b12e596 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/performance/perf_tuning_on_ascend.rst @@ -0,0 +1,256 @@ +Performance Tuning Guide on Ascend +==================================== + +Last updated: 01/29/2026. + +Author: `Xiaobo Hu `_, `Haozhe Li `_ + +`Perf Tuning `_ 中介绍的性能调优方法在昇腾设备中同样适用。本文重点介绍了昇腾特有的一些调优手段,包括融合算子优化、特定硬件配置和昇腾亲和特性等。 + +融合算子 +-------------------------- + +常用融合算子列表 +********************************** + +融合算子的优化原理为,通过数学意义上的等价替换,将多个算子融为一个算子的计算,减少冗余计算,同时减少下发次数,从而提高性能。几个典型的NPU融合算子列举如下,目前均已在 npu_patch.py 中对 Qwen2、Qwen3 系列模型完成替换。 + +当前verl中使用的全量融合算子请查阅 `npu_patch.py `_ + +Matrix Computation-Communication operator fusion (MC2) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +MC2 是 CANN 中一系列计算通信融合算子的统称,这些算子将原本串行的通信和计算操作融合在一起,通过内部的切分和流水线并行执行来优化性能。 + +在 vllm-ascend 中,可以通过指定环境变量: + +.. code-block:: sh + + export VLLM_ASCEND_ENABLE_MATMUL_ALLREDUCE=1 + +在前向计算的 ``RowParallelLinear`` 中使能 ``torch_npu.npu_mm_all_reduce_base`` ,将分离的 ``matmul`` 和 ``allreduce`` 合并为一个融合算子。 + +`RotaryMul&RotaryMulGrad `_ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +torch_npu 接口: ``torch_npu.npu_rotary_mul(x, r1, r2)`` + +参数说明: + +- x: q,k,shape要求输入为4维,一般为 ``[B, N, S, D]`` 或 ``[B, S, N, D]`` 或 ``[S, B, N, D]`` 。 + +- r1: cos值 ,shape要求输入为4维,一般为 ``[1, 1, S, D]`` 或 ``[1, S, 1, D]`` 或 ``[S, 1, 1, D]`` 。 + +- r2: sin 值,shape要求输入为4维,一般为 ``[1, 1, S, D]`` 或 ``[1, S, 1, D]`` 或 ``[S, 1, 1, D]`` 。 + +`RmsNorm&RmsNormGrad `_ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +torch_npu 接口: ``torch_npu.npu_rms_norm(self, gamma, epsilon=1e-06) -> (Tensor, Tensor)`` +参数说明: + +- self: Tensor 类型,shape 支持 1-8 维。 + +- gamma: Tensor 类型,通常为weight,shape 要求与 self 的后几维保持一致。 + +- epsilon: Float 数据类型,用于防止除 0 错误。 + +输出说明: + +- 第 1 个输出为 Tensor,计算公式的最终输出y。 + +- 第 2 个输出为 Tensor, rms_norm 的中间结果 rstd ,用于反向计算。 + +`Swiglu `_ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +torch_npu 接口: ``torch_npu.npu_swiglu(Tensor self, int dim=-1) -> (Tensor)`` + +参数说明: + +- self: Tensor 类型,shape支持 1-8 维。 + +- dim: Int 类型,默认为 -1。 + +输出说明: + +- 输出为 Tensor,计算公式的最终输出 y。 + +`GroupMatMul `_ +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +函数原型: + +.. code:: python + + npu_grouped_matmul( + x, + weight, + *, + bias=None, + scale=None, + offset=None, + antiquant_scale=None, + antiquant_offset=None, + per_token_scale=None, + group_list=None, + activation_input=None, + activation_quant_scale=None, + activation_quant_offset=None, + split_item=0, group_type=None, + group_list_type=0, + act_type=0, + output_dtype=None, + tuning_config=None + ) -> List[Tensor] + +详细使用方法见标题文档链接 + +FSDP后端融合算子使用方法 +********************************** + +在 ``verl/models/transformers/npu_patch.py`` 目录中,已经把可用的融合算子通过 patch 的形式进行替换,无需进行其他操作即可默认进行使用 + +Megatron后端融合算子使用方法 +********************************** + +Megatron 的融合算子集成在 MindSpeed 中,需要添加特定参数开启: + +1. **Flash Attention(必须开启)** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True + ++actor_rollout_ref.ref.megatron.override_transformer_config.use_flash_attn=True + +2. **RotaryMul** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.apply_rope_fusion=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rotary_pos_emb=True + +3. **RMSNorm** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rmsnorm=True + +4. **GroupMatMul** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_grouped_gemm=True + +5. **Swiglu** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True + +6. **Permute/Unpermute** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.fused_permute_unpermute=True + +7. **MC2** + :: + + +actor_rollout_ref.actor.megatron.override_transformer_config.use_ascend_mc2=True + +昇腾通用配置 +-------------------------- + +`算子下发 `_ +************************************************************************************************************************************************************************************************************ + +通过 ``TASK_QUEUE_ENABLE`` 可配置 task_queue 算子下发队列优化等级,默认为 Level 1 优化。该配置可以减少host下发时间,可用于缓解由下发导致的整体free过大问题。 + +.. image :: https://github.com/verl-project/verl-data/blob/main/images/ascend/perf_tuning_task_queue.png + :width: 500px + +Level 0 : 不开启下发流水优化。 + +Level 1 : 将算子下发任务分为两段,一部分任务(主要是 aclnn 算子的调用)放在新增的二级流水上,一、二级流水通过算子队列传递任务,相互并行,通过部分掩盖减少整体的下发耗时,提升端到端性能。 + +Level 2 : 基于 Level 1 的优化进一步平衡了一、二级流水的任务负载,主要是将 workspace 相关任务迁移至二级流水,掩盖效果更好,性能收益更大。该配置仅在二进制场景生效,建议配置值为 Level 2 优化。 + +`通讯算法编排展开 `_ +************************************************************************************************************************************************************************************************************ +使用环境变量 ``HCCL_OP_EXPANSION_MODE=AIV`` 用于配置通信算法的编排展开位置,支持如下取值: + +- **AI_CPU:** 代表通信算法的编排展开位置在 Device 侧的 AI CPU,Device 侧根据硬件型号自动选择相应的调度器。 + +- **AIV:** 代表通信算法的编排展开位置在 Device 侧的 Vector Core,执行也在 Vector Core。 + +- **HOST:** 代表通信算法的编排展开位置为 Host 侧 CPU,Device 侧根据硬件型号自动选择相应的调度器。 + +- **HOST_TS:** 代表通信算法的编排展开位置为 Host 侧 CPU,Host 向 Device 的 Task Scheduler 下发任务,Device 的 Task Scheduler 进行任务调度执行。 + +推理阶段调优 +-------------------------- + +Chunked Prefill in V1 +*************************** + +VLLM 当前版本已默认启用 VLLM V1,使用以下配置启用 Chunked Prefill: + +.. code-block:: sh + + actor_rollout_ref.rollout.enable_chunked_prefill=True + +原理参考 `VLLM 官方文档 `_。 + +Graph Mode +*************************** + +与 CUDA 类似,NPU 通过以下配置启用 **ACL Graph**: + +.. code-block:: sh + + actor_rollout_ref.rollout.enforce_eager=False + +.. note:: + ACL Graph 与 ``taskqueue Level 2`` 原理冲突,**二者无法同时开启**。 + +训练阶段调优 +-------------------------- + +FSDP +********************************** + +.. csv-table:: + :header: "FSDP", "说明" + :widths: 30, 60 + + "/","仅切分优化器(Zero-1)" + SHARD_GRAD_OP,切分梯度和优化器(Zero-2) + "HYBRID_SHARD","切分权重、梯度和优化器(Zero-3)" + "2D device_mesh+HYBRID_SHARD","又称HSDP(FSDP+DDP)例如device_mesh=[2,8], 每8个rank为一个FSDP组,组内进行FSDP切分,共有两个组,两个组间进行DDP,通过allreduce同步梯度。" + "2D device_mesh+HYBRID_SHARD_ZERO2","HSDP的Zero2版本" + NO_SHARD,DDP + +FSDP 不支持 Zero-1, VeRL中会根据卡数和 ``actor_rollout_ref.actor.fsdp_config.fsdp_size`` 来决定 device mesh 的取值,默认使用 Zero-3 进行切分;如果模型较小(建议小于 7B 时),可以通过控制参数 ``actor_rollout_ref.actor.fsdp_config.reshard_after_forward`` 为 ``True`` 在 FSDP/FSDP2 上使用 Zero-2 来优化性能. + +Megatron +********************************** + +在模型较大时,使用 Megatron 作为训练后端可以更灵活的进行性能调优。 + +当 DP 并行显存无法容纳模型时,优先开启 TP 来切分模型权重,如果模型仍然过大,再开启 PP 来进一步切分;如果序列过长导致激活太大,则可以开启 CP 和 SP 来进行优化;在 MoE 模型中则可以额外开启 EP 来控制对专家的切分,如果专家过小,为了避免将权重切得过于细碎,则可以开启 ETP 来避免 MoE 部分的 TP 切分,而将多个完整的专家分布到 DP 和 TP 上。 + +TP、PP、EP、ETP和 Megatron 使用方式一样,CP 和 SP 在 NPU 上开启方式: + +- SP: ``Sequence Parallel`` 在 Tensor Parallel 的基础上进一步提高计算效率,是一种通过将输入数据的序列维度进行切分的并行计算方式。在 NPU 上通过 MindSpeed 来调用SP: + :: + + actor_rollout_ref.actor.megatron.override_transformer_config.sequence_parallel=True + +- CP: ``Context Parallel`` 是一种在多个 GPU/NPU 上并行处理神经网络激活值的方法,它通过在序列维度上对输入张量进行划分来实现。在 NPU 上通过 MindSpeed 来调用 CP (两个参数必须同时添加): + :: + + actor_rollout_ref.actor.megatron.context_parallel_size + actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_size + +Megatron-distributed optimizer +********************************** + +在面对较大尺寸模型时,通常需要将优化器分片到一个 DP 域内的每张卡上来节省显存。Megatron 后端下在 NPU 上开启分布式优化器: + +:: + + +actor_rollout_ref.actor.megatron.override_transformer_config.use_distributed_optimizer=True diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_alignment_zh.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_alignment_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..0de09225a87a2395d70644bfcea1fa6e2011ad39 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_alignment_zh.md @@ -0,0 +1,141 @@ +# Precision Alignment + +在 VeRL 框架中进行强化学习(RL)训练时,**精度对齐**是确保训练过程可复现、可调试的关键环节。 + +本文档总结了在 VeRL 上对NPU和GPU进行精度对齐的方法以供参考。 + +Last updated: 05/09/2026. + +## 1. 环境与权重对齐 + +### 1.1 依赖版本对齐 + +VeRL、transformers版本需要进行强对齐,否则会直接影响精度结果。 + +其他关键依赖(torch、megatron、vllm)如无法进行强对齐,需优先保持一致或相近。 + +### 1.2 模型权重对齐 + +检查模型的权重和config.json文件是否完全一致 + + +## 2. 输入数据对齐 + +在verl训练启动脚本中添加如下配置: + +```bash +data.shuffle=False +data.validation_shuffle=False +``` + + +## 3. 配置对齐 + +在NPU与GPU做精度对齐时,需检查配置是否完全对齐。包含: +1. 直接对比脚本写入配置 +2. 运行过程中保存日志,收集日志打屏中的配置进行对比,可比较默认参数配置是否一致,需保证关键参数对齐 + + +## 4. 固定确定性 + +### 4.1 固定随机种子 + +在环境中安装 `msprobe` : + +```bash +pip install mindstudio-probe +``` + +在 worker 文件开头添加确定性函数: + +```python +from msprobe.pytorch import seed_all +seed_all(mode=True) +``` + +### 4.2 固定通信环境变量 + +在多卡通信情况下: + +- HCCL通信下(默认场景): + + - export CLOSE_MATMUL_K_SHIFT=1 + - export ATB_MATMUL_SHUFFLE_K_ENABLE=0 + - export HCCL_DETERMINISTIC="true" + - export VLLM_ENABLE_V1_MULTIPROCESSING=0 + +- LCCL通信下(通过export HCCL_OP_EXPANSION_MODE="AIV"使能): + + - export CLOSE_MATMUL_K_SHIFT=1 + - export ATB_MATMUL_SHUFFLE_K_ENABLE=0 + - export LCCL_DETERMINISTIC=1 + - export ATB_LLM_LCOC_ENABLE=0 + - export VLLM_ENABLE_V1_MULTIPROCESSING=0 + +在单卡无通信情况下: + + - export CLOSE_MATMUL_K_SHIFT=1 + - export ATB_MATMUL_SHUFFLE_K_ENABLE=0 + - export VLLM_ENABLE_V1_MULTIPROCESSING=0 + + + +## 5. 验证训练精度 + +### 5.1 训练打桩 + +**打桩**即保留当前阶段的输入输出数据,便于从结果上对比分析。在进行精度问题排查时,需要进行打桩辅助问题定位。常见的打桩方式是直接将rollout阶段的数据直接dump下来。 + +**第一步:在 GPU 环境生成基准数据** + +先跑一次GPU脚本,开启如下配置: + +```bash +trainer.rollout_data_dir='/path/dump/data_json' +``` +可以保存每步推理结果为jsonl文件。 + +**第二步:在 NPU 环境复现验证** + +NPU上开启如下参数,复用上一步生成的序列,端到端运行: + +```bash +skip.rollout.enable=True \ +skip.rollout.dump_dir=/path/to/rollout_dump \ +``` + +**第三步:对比指标** + +在打桩输入相同推理结果,训练配置保持一致,并且固定随机性的情况下,比较NPU与GPU的rewards/pg_loss/grad_norm值是否存在差异。 + + +## 6. 验证推理精度 + +### 6.1 resharding + +在推理正式开始前,vllm会进行**dummy run**,通过推理一个 token 来评估推理时的显存占用,进而分配显存。可以在 vLLM 的 LLM 初始化时指定参数 load_format 来指定 dummy run 的权重是随机初始化的(dummy)还是真实权重(safetensors)。在 VeRL 中,通过参数 **actor_rollout_ref.rollout.load_format** 指定该参数。 + +当出现推理乱码现象时,如果引擎初始化方式为**load_format=dummy**,则sharding高概率存在问题,即使换成了safetensors后吐字正常,sharding也是存在问题的,需要对比前向。 + + +### 6.2 推理结果对齐 + +```bash +trainer.rollout_data_dir='/path/dump/data_json' +``` + +保存每步推理结果为jsonl文件,可以直接打开jsonl文件快速确认整网推理结果是否乱码,用于推理精度问题定界。 + + +在dump推理数据之前,若复现推理精度问题占用的资源较多,可以先尝试缩小推理精度问题复现成本,减少复现的规模,减少需要dump和对比的数据。在多batch、长序列的场景下,可以通过发送单batch请求,减少序列长度尝试复现。 + + +## 7. dump对比 + +[精度调试工具](./precision_debugger_zh.md),定位到问题出现的阶段之后,可以通过msprobe工具进行数据dump来细致定位。 + +在推理或训练过程中,模型可能出现输出偏离预期、生成异常、甚至产生 NaN/Inf 等数值不稳定问题。要定位根因,需要对模型执行路径进行精细化监控,采集中间特征、权重、激活值以及各关键层的输入输出,并记录提示词、张量 dtype、硬件配置等上下文信息。通过捕获这些核心张量及元数据,可以系统性地追踪精度退化或数值错误的来源。 + + + + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_debugger_zh.md b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_debugger_zh.md new file mode 100644 index 0000000000000000000000000000000000000000..7a54027002f183805c5ebec0040f22f412aed8d9 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/dev_guide/precision_analysis/precision_debugger_zh.md @@ -0,0 +1,373 @@ +# Precision Debugger (msprobe) in verl + +Last updated: 04/13/2026. + +This guide explains how to collect precision data in verl using the +`msprobe` PrecisionDebugger. + +## Prerequisites + +* Install `msprobe` in the training environment: + +```bash +pip install mindstudio-probe +``` + +* Prepare a `config.json` for msprobe (see examples below). +* Enable profiler for the roles you want to collect. + +Reference: +* `https://gitcode.com/Ascend/msprobe.git` + +## Configuration + +PrecisionDebugger is integrated through verl's unified profiler interface. +Use a minimal two-part setup: + +* `global_profiler` selects the tool and config file. +* role `profiler.enable=True` turns on profiling for that role. + +### Global profiling control + +In `global_profiler`, set the profiler tool to `precision_debugger` and +configure the msprobe-specific options under `global_tool_config`. + +```yaml +global_profiler: + tool: precision_debugger + steps: [1, 2, 5] + save_path: "outputs/profile" + global_tool_config: + precision_debugger: + _target_: verl.utils.profiler.config.PrecisionDebuggerToolConfig + config_path: /path/to/config.json + stages: + - actor_update + - actor_compute_log_prob + - ref_compute_log_prob + - compute_values + - critic_update + - compute_rm_score + strict: False +``` + +Notes: + +* `global_profiler.steps` is the only step filter for PrecisionDebugger. +* Dumps are written under `global_profiler.save_path`. +* Actual dump path is `{global_profiler.save_path}/step_{global_step}/{stage}`. +* Do not set `dump_path` in `config.json`; output path is controlled by verl. + +### Role profiling control + +Enable profiling for the roles you want to collect: + +```yaml +actor_rollout_ref: + actor: + profiler: + enable: True + ref: + profiler: + enable: True +critic: + profiler: + enable: True +``` + +## Supported stages + +PrecisionDebugger collects data from the following stages: + +* `actor_update` +* `actor_compute_log_prob` +* `ref_compute_log_prob` +* `compute_values` +* `critic_update` +* `compute_rm_score` + +Rollout generation is intentionally skipped (`rollout_generate` is ignored). + +The current integration is designed for training-side stages. In a typical PPO +run, the most common useful combinations are: + +* actor/ref only: + `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` +* actor/ref/critic: + `actor_compute_log_prob`, `ref_compute_log_prob`, `compute_values`, + `critic_update`, `actor_update` + +## msprobe config.json common examples + +### `statistics` mode + +```json +{ + "task": "statistics", + "rank": [], + "step": [], + "level": "L1", + "async_dump": false, + "statistics": { + "scope": [], + "list": [], + "tensor_list": [], + "data_mode": ["all"], + "summary_mode": "statistics" + } +} +``` + +### `tensor` mode + +```json +{ + "task": "tensor", + "rank": [], + "step": [], + "level": "L1", + "async_dump": false, + "tensor": { + "scope": [], + "list": [], + "data_mode": ["all"], + "summary_mode": "statistics" + } +} +``` + +## Minimal example + +The following example enables PrecisionDebugger on steps `1` and `2`. +If you need rank filtering, configure it only in msprobe `config.json`. + +```yaml +global_profiler: + tool: precision_debugger + steps: [1, 2] + global_tool_config: + precision_debugger: + _target_: verl.utils.profiler.config.PrecisionDebuggerToolConfig + config_path: /path/to/dump_config.json + stages: + - actor_compute_log_prob + - ref_compute_log_prob + - actor_update + strict: False + +actor_rollout_ref: + actor: + profiler: + enable: True + ref: + profiler: + enable: True +``` + +## Minimal CLI example + +Use only the required flags: + +```bash +python3 -m verl.trainer.main_ppo \ + global_profiler.tool=precision_debugger \ + global_profiler.steps='[1,2]' \ + global_profiler.save_path=outputs/profile \ + +global_profiler.global_tool_config.precision_debugger.config_path=/path/to/config.json \ + actor_rollout_ref.actor.profiler.enable=True \ + actor_rollout_ref.ref.profiler.enable=True +``` + +Optional stage filter: + +```bash ++global_profiler.global_tool_config.precision_debugger.stages='[actor_compute_log_prob,ref_compute_log_prob,actor_update]' +``` + +## Output layout + +Verl organizes PrecisionDebugger output by training global step and stage. +Inside each stage directory, msprobe creates its own `step*/rank*` layout. + +Example: + +```text +outputs/profile/ + step_1/ + actor_compute_log_prob/step0/rank0/dump.json + actor_update/step0/rank0/dump.json + ref_compute_log_prob/step0/rank0/dump.json + step_2/ + actor_compute_log_prob/step0/rank0/dump.json + actor_update/step0/rank0/dump.json + ref_compute_log_prob/step0/rank0/dump.json +``` + +Observed output from a real run: + +* Outer `step_` directories are created by verl. +* Inner `step0/rank0/` directories and `dump.json` files are created by msprobe. +* With the current integration, each profiled stage is collected in an + independent dump session, so stage-local output typically lands in `step0`. + +## How results are written + +The verl integration wraps each profiled stage with: + +* `debugger.start(model=...)` +* execute the stage +* `debugger.stop()` +* `service.reset_status()` if the msprobe runtime exposes it + +Verl does **not** manually call `debugger.step()` in the current integration. +Instead, each stage writes to its own dump directory and resets msprobe runtime +status after `stop()` to avoid stale `dump.json` cache growth across stages. + +For L0 collection, PrecisionDebugger must bind to the actual model used in the +stage. The profiler resolves the model inside +`verl/utils/profiler/precision_debugger_profile.py` and supports both legacy +workers and the newer model-engine worker path. + +## Overhead and disk usage + +Below are measurements from a real PPO run on Ascend with: + +* model: `Qwen2-0.5B` +* profiled steps: `[1, 2]` +* rank: `0` +* stages: + * L1: `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` + * L0: `actor_compute_log_prob`, `ref_compute_log_prob`, `compute_values`, + `critic_update`, `actor_update` + +### Time overhead + +| Run | Model | Profiled steps | Measured step time | +|---|---|---:|---:| +| Baseline | `Qwen2-0.5B` | None | about `16-18 s/step` in steady state | +| L0 | `Qwen2-0.5B` | `step 1` | `66.81 s` | +| L0 | `Qwen2-0.5B` | `step 2` | `48.78 s` | +| L0 | `Qwen2-0.5B` | non-profiled later steps | about `17 s/step` | +| L1 | `Qwen2-0.5B` | `step 1` | `177.35 s` | +| L1 | `Qwen2-0.5B` | `step 2` | `161.80 s` | +| L1 | `Qwen2-0.5B` | non-profiled later steps | about `17 s/step` | + +In this experiment, profiled L0 steps were about `3x-4x` slower than the +baseline steady-state step time, and profiled L1 steps were about `9x-10x` +slower. Non-profiled later steps remained close to baseline in both cases. + +In general, PrecisionDebugger should be treated as a heavy-weight precision +debugging tool rather than a lightweight profiler. In larger models or broader +stage coverage, it is common to observe `tens-X` performance inflation for +profiled steps. + +### Disk usage + +| Level | Model | Stages | Scope | Disk usage | +|---|---|---|---|---:| +| L1 | `Qwen2-0.5B` | `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` | total for `step_1` and `step_2` | `21 MB` | +| L1 | `Qwen2-0.5B` | `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` | per step | about `11 MB` | +| L1 | `Qwen2-0.5B` | `actor_update` | per step | about `5.1-5.2 MB` | +| L1 | `Qwen2-0.5B` | `actor_compute_log_prob` | per step | about `2.6 MB` | +| L1 | `Qwen2-0.5B` | `ref_compute_log_prob` | per step | about `2.6 MB` | +| L0 | `Qwen2-0.5B` | `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` | total for `step_1` and `step_2` | `8.8 MB` | +| L0 | `Qwen2-0.5B` | `actor_compute_log_prob`, `ref_compute_log_prob`, `actor_update` | per step | about `4.4 MB` | +| L0 | `Qwen2-0.5B` | `actor_update` | per step | about `2.5 MB` | +| L0 | `Qwen2-0.5B` | `actor_compute_log_prob` | per step | about `1.1 MB` | +| L0 | `Qwen2-0.5B` | `ref_compute_log_prob` | per step | about `0.86-0.87 MB` | + +In this experiment, total L1 disk usage was about `2.4x` the L0 disk usage for +the measured actor/ref stage set. + +These numbers depend on: + +* selected stages +* number of profiled steps +* dump level and task +* model shape and sequence length + +## How to analyze results + +At minimum, check: + +* which `step_` directory was generated +* which stage directories exist under that step +* whether `dump.json` exists under `step0/rank0` + +For downstream analysis, use standard msprobe tools such as: + +* `msprobe compare` +* `msprobe visualization` + +Example compare usage: + +```bash +msprobe compare \ + --target-path /path/to/target_dump/dump.json \ + --golden-path /path/to/golden_dump/dump.json +``` + +You can compare: + +* the same stage across two runs +* different global steps of the same stage +* different ranks when multi-rank collection is enabled + +For more advanced analysis workflows, refer to the official msprobe +documentation for compare and visualization commands. + +## Usage notes + +* Verl integrates PrecisionDebugger through `DistProfiler.annotate` wrappers on + training stages. +* PrecisionDebugger is automatically discrete: each profiled stage is + collected in an independent `start -> stop -> reset_status` session. It does + not currently expose the unified profiler `discrete` configuration used by + tools such as `nsys` or `npu`. +* `global_steps` is read from batch `meta_info` or from worker attributes. +* If `strict` is `True`, missing msprobe or unknown stages raise errors. +* If a stage prints `PrecisionDebugger model not resolved`, that stage ran + normally but no dump was collected because verl could not bind msprobe to a + valid model object. +* Because dump cost is high, prefer collecting a small number of representative + steps first, then narrow the stage set if necessary. + +## Quality checklist + +Use this checklist to verify your setup is complete and reproducible: + +* `global_profiler.tool=precision_debugger` +* `global_profiler.steps` includes the target step +* `+global_profiler.global_tool_config.precision_debugger.config_path=...` is set +* role `profiler.enable=True` is set for the stages you need +* `msprobe` is importable in the runtime environment +* output exists under `{global_profiler.save_path}/step_//...` + +## Troubleshooting + +### No dump directory is generated + +Check: + +* `global_profiler.tool=precision_debugger` +* `global_profiler.steps` contains the target step +* role profiler is enabled for the target role +* msprobe is installed in the training environment + +### `PrecisionDebugger model not resolved` + +This means the stage was reached, but verl could not find the actual model used +by that worker. The stage itself still runs, but dump is skipped. This usually +indicates: + +* a new worker path was introduced and profiler model resolution needs to be + updated +* the role or engine backend differs from the paths currently supported by the + resolver + +### `dump.json` keeps growing unexpectedly + +If `stop()` is called without resetting msprobe runtime state, cached dump data +may continue to accumulate across stage invocations. The current verl +integration resets msprobe runtime status after `stop()` when the service API +supports it. diff --git a/verl_0720_main/verl/docs/ascend_tutorial/faq/faq.rst b/verl_0720_main/verl/docs/ascend_tutorial/faq/faq.rst new file mode 100644 index 0000000000000000000000000000000000000000..995be18cedc142c546ca73f9b4c06dc9ad3635c2 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/faq/faq.rst @@ -0,0 +1,205 @@ +NPU 常见问题解答 +================ + +Last updated: 05/13/2026. + +本文档总结了在 NPU 上执行 VERL 训练和推理时遇到的常见问题及解决方案。 + +环境配置问题 +------------ + +### Q1: NPU 设备不可见怎么办? + +**问题现象**:torch_npu.npu.is_available() 返回 False + +**解决方案**: + +.. code-block:: bash + + # 检查设备可见性 + echo $ASCEND_RT_VISIBLE_DEVICES + + # 设置可见设备并禁用ray自动设置 + export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + + # 检查驱动状态 + npu-smi info + +调试和诊断 +---------- + +### Q1: 如何启用 NPU 性能分析? + +使用 VERL 内置的 profiler: + +.. code-block:: shell + + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=true \ + actor_rollout_ref.actor.profiler.tool_config.npu.contents=npu,cpu \ + actor_rollout_ref.actor.profiler.tool_config.npu.level=1 \ + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=true + +### Q2: 如何排查 NPU 训练失败的问题? + +**排查步骤**: + +1. 检查环境变量配置 +2. 验证设备可见性 +3. 检查 CANN 版本兼容性 +4. 查看日志中的具体错误信息 +5. 使用最小化示例复现问题 + +**启用详细日志**: + +.. code-block:: bash + + # VERL 框架日志 + export VERL_LOGGING_LEVEL=DEBUG + + # 昇腾 NPU 日志(0=DEBUG, 1=INFO, 2=WARNING, 3=ERROR) + export ASCEND_GLOBAL_LOG_LEVEL=0 + export ASCEND_SLOG_PRINT_TO_STDOUT=1 + + # HCCL 通信日志 + export HCCL_DEBUG=INFO + +常见错误信息 +------------ + +### Q1: "torch_npu detected, but NPU device is not available or visible" + +**原因**:NPU 驱动未正确安装或设备不可见 + +**解决方案**:检查驱动安装状态和 ASCEND_RT_VISIBLE_DEVICES 设置 + +### Q2: "KeyError: decoder.layers.0.self_attention.q_layernorm.weight" + +**原因**:MindSpeed版本过低 + +**解决方案**:切换MindSpeed至 2.3.0_core_r0.12.1 + +### Q3: "AssertionError: Weight ... is too large to fit in the bucket" + +**问题现象**:在分布式训练权重同步时,出现如下错误: + +.. code-block:: text + + AssertionError: Weight model.embed_tokens.weight(torch.Size([151936, 4096]), torch.float32) is too large to fit in the bucket. + Please increase rollout.update_weights_bucket_megabytes(2048 MB). + +**原因**:模型某个权重张量的大小超过了权重传输 bucket 的默认容量(2048 MB)。在 verl 框架中,模型权重通过 bucket(缓冲区)进行分块打包传输。当单个权重张量超过 bucket 大小时,断言检查失败。 + +**权重大小计算方法**: + +权重张量的内存占用(字节)= 各维度大小的乘积 × 每个元素的字节数 + +其中数据类型对应的字节数为: + +- ``torch.float32`` → 4 字节 +- ``torch.float16`` / ``torch.bfloat16`` → 2 字节 +- ``torch.int8`` → 1 字节 + +以本例中的 ``model.embed_tokens.weight`` 为例: + +.. code-block:: text + + 张量形状: torch.Size([151936, 4096]) + 数据类型: torch.float32 (4 字节) + 权重大小 = 151936 × 4096 × 4 = 2,483,027,968 字节 ≈ 2369 MB + + 默认 bucket 大小 = 2048 MB < 2369 MB → 触发断言失败 + +**解决方案**:在启动训练时增加 ``update_weights_bucket_megabytes`` 参数,使 bucket 容量大于最大权重张量的内存占用: + +.. code-block:: bash + + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=4096 + +**参数值选择建议**: + +1. **计算模型中最大权重张量的内存占用**:遍历模型所有参数,找出 ``nbytes`` 最大的那个,将其转换为 MB(除以 1024²)。 + +2. **向上取整到 2 的幂次**:为便于内存分配和管理,建议将计算结果向上取整到最近的 2 的幂次(如 2048、4096、8192 等)。例如最大权重为 2369 MB,则取 4096 MB。 + +3. **预留适当余量**:考虑到内存对齐和运行时开销,建议 bucket 大小至少为最大权重大小的 1.2~1.5 倍,再向上取整到 2 的幂次。 + +4. **注意内存限制**:bucket 大小会直接影响 worker 节点的内存占用,设置过大会导致 OOM。应在满足权重传输需求的前提下,尽量选择较小的值。 + +**常见模型的推荐值**: + +.. list-table:: + :header-rows: 1 + + * - 模型规模 + - 典型最大权重形状 + - 推荐 bucket 大小 + * - 7B (Qwen2 等) + - [151936, 4096] float32 + - 4096 MB + * - 14B + - [152064, 5120] float32 + - 4096 MB + * - 72B + - [152064, 8192] float32 + - 8192 MB + +### Q4: 非共享存储下 checkpoint 加载失败,找不到 common.pt / .metadata / metadata.json + +**问题现象**:使用 verl + Megatron 后端在**非共享存储**的多机环境下,保存 checkpoint 正常,但重新加载时报错,提示找不到以下文件: + +.. code-block:: text + + FileNotFoundError: common.pt + FileNotFoundError: .metadata + FileNotFoundError: metadata.json + +**原因**:当前 checkpoint 机制对非共享存储的支持不完善。具体表现为: + +- **分布式训练权重是分节点保存的**,每个节点只保存自己负责的分片权重,不会只在主节点保存全部权重。 +- 但 ``common.pt``、``.metadata``、``metadata.json`` 等元数据文件**仅保存在执行保存操作的节点上**(通常是 rank 0 所在节点),其他节点本地没有这些文件。 +- 加载 checkpoint 时,每个节点都需要读取这些元数据文件来还原模型状态,但非共享存储下其他节点本地路径中不存在这些文件,导致加载失败。 + +**临时解决方案**:手动将元数据文件从保存节点复制到所有其他节点: + +.. code-block:: bash + + # 假设 checkpoint 保存在 rank 0 节点的 /path/to/ckpt/ 目录下 + # 将元数据文件从 rank 0 节点复制到其他所有节点 + + # 需要复制的文件 + /path/to/ckpt/common.pt + /path/to/ckpt/.metadata + /path/to/ckpt/metadata.json + + # 示例:使用 scp 复制到其他节点 + scp /path/to/ckpt/common.pt node1:/path/to/ckpt/ + scp /path/to/ckpt/.metadata node1:/path/to/ckpt/ + scp /path/to/ckpt/metadata.json node1:/path/to/ckpt/ + + # 对所有节点重复上述操作 + +**注意事项**: + +- 每次保存 checkpoint 后都需要重新复制元数据文件,因为保存操作可能会更新这些文件的内容。 +- 如果训练过程中频繁保存 checkpoint(如按步数自动保存),建议编写脚本在保存后自动触发复制,避免遗漏。 +- 长期方案应等待框架层面支持非共享存储的 checkpoint 加载,使元数据文件能自动同步到所有节点。 + +参考资料 +-------- + +- `NPU 性能优化指南 <../dev_guide/performance/perf_tuning_on_ascend.rst>`_ +- `NPU 快速开始指南 <../get_start/quick_start.rst>`_ +- `NPU CI 指南 <../contribution_guide/ascend_ci_guide_zh.rst>`_ +- Ascend NPU 文档: https://www.hiascend.com/document +- CANN 工具包文档: https://www.hiascend.com/software/cann + +获取更多帮助 +------------ + +如果以上 FAQ 无法解决您的问题,请: + +1. 查看完整的错误日志 +2. 在 GitHub Issues 中搜索类似问题 +3. 提供详细的错误信息和环境配置 +4. 提供最小可复现示例 \ No newline at end of file diff --git a/verl_0720_main/verl/docs/ascend_tutorial/feature_support/ascend_backend_features.md b/verl_0720_main/verl/docs/ascend_tutorial/feature_support/ascend_backend_features.md new file mode 100644 index 0000000000000000000000000000000000000000..ff4b687eae5c6cfa1600dcf1b706f90c97e256a4 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/feature_support/ascend_backend_features.md @@ -0,0 +1,349 @@ +# Ascend Backend Features Guide +================================================================================== + +Last updated: 03/03/2026. + +昇腾全面支持verl生态建设,本文将介绍NPU上对于verl的适配工作及后端特性支持供开发者进行参考 + +--- + +## 推理后端 + +当前verl支持vllm/sglang这两种主流推理后端,均可在昇腾NPU上运行。 + +### 1. vllm: + +昇腾通过vllm-ascend插件来支持vllm推理后端,该插件是 vLLM 社区支持 Ascend 后端的推荐方法。它遵循[[RFC]](https://github.com/vllm-project/vllm/issues/11162),提供了一个可插拔接口,将 Ascend NPU 与 vLLM 解耦。 + +#### 参数特性支持 + +| vllm参数| verl对应通用参数 | 简介 | +| --- | --- | --- | +| `model_path` | `actor_rollout_ref.model.path` |模型权重文件的路径| +| `gpu_memory_utilization` | `actor_rollout_ref.rollout.gpu_memory_utilization` |用于控制每个阶段可使用的 GPU 内存量。它被指定为一个介于 0.0 和 1.0 之间的分数,其中:- 0.8 表示 GPU 总内存的 80%- 1.0 表示 GPU 总内存的 100%(不推荐,没有预留缓冲)| +| `enforce_eager`| `actor_rollout_ref.rollout.enforce_eager` |禁用图模式,verl默认为False| +| `enable_chunked_prefill`| `actor_rollout_ref.rollout.enable_chunked_prefill` | 分块预填充允许将大预填充分块成更小的块,并将它们与解码请求一起批处理。| +| `free_cache_engine`| `actor_rollout_ref.rollout.free_cache_engine` |在部署生成阶段之后卸载 KVCache,默认值为 True。| +| `max_model_len` | `actor_rollout_ref.rollout.max_model_len` | 模型能够处理的最大序列长度。它限制了单个输入序列的最大长度 | +| `tp_size`| `actor_rollout_ref.rollout.tensor_model_parallel_size * data_parallel_size`|TP并行度| +| `dp_size`| `actor_rollout_ref.rollout.data_parallel_size`|DP并行度| +| `ep_size`| `actor_rollout_ref.rollout.expert_parallel_size`|EP并行度| +| `node_rank`| `无,根据实际实例和卡数自动计算` |实例中的节点排序| +| `load_format`| `actor_rollout_ref.rollout.load_format` |要加载的模型权重格式| +| `disable_log_stats`| `actor_rollout_ref.rollout.disable_log_stats`|控制是否记录 rollout 统计日志 | +| `nnodes`| 无,根据实际实例和卡数自动计算 | 每个实例包含的节点数量 | +| `trust_remote_code`| `actor_rollout_ref.model.trust_remote_code`|是否允许在 Hub 上定义自定义模型,并将其写入自己的建模文件中| +| `max_num_seqs` | `actor_rollout_ref.rollout.max_num_seqs` |正在运行的请求的最大数量| +| `max_num_batched_tokens`| `actor_rollout_ref.rollout.max_num_batched_tokens` |在一次批处理(batch)中可以处理的最大总Token数| +| `skip_tokenizer_init`| `actor_rollout_ref.rollout.skip_tokenizer_init` |跳过初始化分词器并将 input_ids 传递到推理请求中| +| `enable_prefix_caching` | `actor_rollout_ref.rollout.enable_prefix_caching` |用于启用自动前缀缓存 | +| `quantization`| `actor_rollout_ref.rollout.quantization`,默认为None|`量化方法`| + +### 2. sglang: + +对于sglang推理后端,昇腾通过直接向sglang社区进行持续建设与维护来支持相关功能。 +此外在verl中使用sglang还涉及以下组件 + +| 组件| 描述| +| --- | --- | +| [sgl_kernel_npu](https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/sgl_kernel_npu/README.md) | Ascend NPU SGL 优化推理内核集合,包括注意力机制、归一化、激活函数、LoRA 适配器等。 | +| [deepep](https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/deep_ep/README.md) | DeepEP的 Ascend 实现,为MoE模型提供高度优化的专家并行 (EP) 通信内核 | + +#### 参数特性支持 + +verl中通过rollout config管理推理后端参数使能,包含通用参数和engine_kwargs自定义传参。 +以下列举在verl中常见设置的sglang特性参数,更多参数介绍请参考 [sglang社区NPU特性支持](https://docs.sglang.io/docs/hardware-platforms/ascend-npus/ascend_npu_support_features) + +| sglang参数| verl对应通用参数 | 简介| +| --- | --- | --- | +| model_path | actor_rollout_ref.model.path|模型权重文件的路径| +| mem_fraction_static| actor_rollout_ref.rollout.gpu_memory_utilization |用于静态分配(模型权重和键值缓存内存池)的内存比例| +| disable_cuda_graph| actor_rollout_ref.rollout.enforce_eager|禁用图模式,verl默认为False| +| enable_memory_saver| 无,verl中默认设置为True | 允许使用 release_memory_occupation 和 resume_memory_occupation 来节省内存 +| base_gpu_id| 无,根据实际实例和卡数自动计算 |用于分配每个实例上计算卡资源时的初始ID +| gpu_id_step| 无,默认设置为1| 使用的连续计算卡ID 之间的差值 +| tp_size| actor_rollout_ref.rollout.tensor_model_parallel_size * data_parallel_size|TP并行度| +| dp_size| actor_rollout_ref.rollout.data_parallel_size|DP并行度| +| ep_size| actor_rollout_ref.rollout.expert_parallel_size|EP并行度| +| node_rank| 无,根据实际实例和卡数自动计算 |实例中的节点排序| +| load_format| actor_rollout_ref.rollout.load_format|要加载的模型权重格式| +| dist_init_addr| 无,自动计算|用于初始化分布式后端的主机地址| +| nnodes| 无,根据实际实例和卡数自动计算|每个实例包含的节点数量| +| trust_remote_code| actor_rollout_ref.model.trust_remote_code|是否允许在 Hub 上定义自定义模型,并将其写入自己的建模文件中| +| max_running_requests| actor_rollout_ref.rollout.max_num_seqs |正在运行的请求的最大数量| +| log_level| 无,默认设置为error |日志记录器的日志级别| +| skip_tokenizer_init| actor_rollout_ref.rollout.skip_tokenizer_init |跳过初始化分词器并将 input_ids 传递到推理请求中| +| skip_server_warmup| 无,默认设置为True |跳过预热| +| quantization| actor_rollout_ref.rollout.quantization,默认为None|量化方法| +| attention_backend|actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend|attention内核,NPU应该设置为ascend| + +--- + +## 训练后端 + +### 1. FSDP + +昇腾通过torch_npu提供FSDP相关支持能力,当前pytorch api支持度参照[版本说明](https://www.hiascend.com/document/detail/zh/Pytorch/730/apiref/PyTorchNativeapi/docs/zh/native_apis/pytorch_2-7-1/torch-distributed-fsdp.md)。 + +#### FSDP1 +##### 参数特性支持 +| verl参数 | 简介| +| --- | --- | +| `actor_rollout_ref.actor.fsdp_config.param_offload` |是否卸载模型权重到CPU,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.optimizer_offload` |是否卸载优化器状态到CPU,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.reshard_after_forward` |控制前向计算后的参数行为,平衡内存与通信。默认值为True:前向后重新分片参数,反向时重新全收集| +| `actor_rollout_ref.actor.fsdp_config.fsdp_size` | 每个FSDP分片组中的NPU数量;默认值-1表示自动。| + +| `actor_rollout_ref.actor.fsdp_config.forward_prefetch` |在前向计算完成前预取下一次前向传播的 all-gather,仅用于FSDP1,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.use_orig_params` | FSDP是否会使用module的原始参数来初始化,仅用于FSDP1,默认值为False| +| `actor_rollout_ref.actor.ulysses_sequence_parallel_size`|Ulysses序列并行大小| +| `actor_rollout_ref.actor.entropy_from_logits_with_chunking`|通过分块计算熵以减少显存峰值,默认值为False| +| `actor_rollout_ref.actor.entropy_from_logits_chunk_size`|熵计算分块大小,默认值为2048| +| `actor_rollout_ref.actor.fsdp_config.entropy_checkpointing`|在训练时对熵计算启用重计算,降低显存峰值,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.forward_only` |是否只进行前向计算,默认值为False| + +#### FSDP2 +##### 参数特性支持 +| verl参数 | 简介| +| --- | --- | +| `actor_rollout_ref.actor.fsdp_config.param_offload` |是否卸载模型权重到CPU,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.optimizer_offload` |是否卸载优化器状态到CPU,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.reshard_after_forward` |控制前向计算后的参数行为,平衡内存与通信。默认值为True:前向后重新分片参数,反向时重新全收集| +| `actor_rollout_ref.actor.fsdp_config.fsdp_size` | 每个FSDP分片组中的NPU数量;默认值-1表示自动。| +| `actor_rollout_ref.actor.ulysses_sequence_parallel_size`|Ulysses序列并行大小| +| `actor_rollout_ref.actor.entropy_from_logits_with_chunking`|通过分块计算熵以减少显存峰值,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.entropy_checkpointing`|在训练时对熵计算启用重计算,降低显存峰值,默认值为False| +| `actor_rollout_ref.actor.fsdp_config.forward_only` |是否只进行前向计算,默认值为False| + + + +### 2. Megatron + +Megatron 是 NVIDIA 推出的一个专注于模型并行的训练框架仓库。如果一个仓库(例如 Verl)的训练后端使用了 Megatron,同时又希望在 NPU 上运行该仓库,那么就需要额外安装 MindSpeed 来提供底层支持。下文将介绍 MindSpeed 是如何实现无感替换 Megatron 中的关键组件,从而使其能够适配 NPU 的。 + +MindSpeed 底层的替换原理采用了 Monkey Patch 技术 + +* MindSpeed Monkey Patch框架 + +在verl里面通过`from mindspeed.megatron_adaptor import repatch `触发patch,调用栈如下: + +~~~ +from mindspeed.megatron_adaptor import repatch +├── 执行 megatron_adaptor.py 模块导入 +├── 导入 features_manager 模块 +├── 执行 mindspeed/features_manager/__init__.py +├── @AutoExecuteFunction 装饰器触发 +├── patch_features() 自动执行 +└── 进行`apply_features_pre_patches`和`apply_features_patches`操作 +~~~ + +`Patch`类是整个patch系统的核心,实现了函数/类的动态替换 + +~~~python +class Patch: +~~~ + +`parse_path`方法实现了动态模块导入和创建 + +~~~python +def parse_path(module_path, function_name, create_dummy): +~~~ + +patch系统支持多层装饰器叠加 + +~~~python +def apply_patch(self): + final_patch_func = self.orig_func + if self.patch_func is not None: + final_patch_func = self.patch_func + + # 应用所有装饰器 + for wrapper in self.wrappers: + final_patch_func = wrapper(final_patch_func) +~~~ + +* MindSpeedPatchesManager类 + +`MindSpeedPatchesManager`作为全局单例管理所有patch + +~~~python +class MindSpeedPatchesManager: + patches_info: Dict[str, Patch] = {} +~~~ + +* Feature集成模式 + +各个Feature通过继承`MindSpeedFeature`基类集成patch系统 + +~~~python +class MindSpeedFeature: + """Base class for mindspeed features.""" + + def __init__(self, feature_name: str, optimization_level: int = 2): + self.feature_name = feature_name.lower().strip().replace('-', '_') + self.optimization_level = optimization_level + self.default_patches = self.optimization_level == 0 + + def is_need_apply(self, args): + """Check the feature is need to apply.""" + return (self.optimization_level <= args.optimization_level and getattr(args, self.feature_name, None)) \ + or self.default_patches + + def register_args(self, parser: ArgumentParser): + """Register cli arguments to enable the feature.""" + pass + + def pre_validate_args(self, args: Namespace): + """Validate the arguments of mindspeed before megatron args validation + and store some arguments of the mindspeed temporarily, + in case that megatron validate fails. + for example: + ```python + origin_context_parallel_size = args.context_parallel_size + args.context_parallel_size = 1 + ``` + """ + pass + + def validate_args(self, args: Namespace): + """Restore the arguments of the mindspeed. + + for example: + ```python + args.context_parallel_size = origin_context_parallel_size + ``` + """ + pass + + def post_validate_args(self, args: Namespace): + """validate mindspeed arguments after megatron arguments validation.""" + pass + + def pre_register_patches(self, patch_manager: MindSpeedPatchesManager, args: Namespace): + """Register all patch functions before import megatron""" + pass + + def register_patches(self, patch_manager: MindSpeedPatchesManager, args: Namespace): + """Register all patch functions the feature is related.""" + pass + + def incompatible_check(self, global_args, check_args): + """Register all incompatible functions the feature is related.""" + if getattr(global_args, self.feature_name, None) and getattr(global_args, check_args, None): + raise AssertionError('{} and {} are incompatible.'.format(self.feature_name, check_args)) + + def dependency_check(self, global_args, check_args): + """Register all dependency functions the feature is related.""" + if getattr(global_args, self.feature_name, None) and not getattr(global_args, check_args, None): + raise AssertionError('{} requires {}.'.format(self.feature_name, check_args)) + + @staticmethod + def add_parser_argument_choices_value(parser, argument_name, new_choice): + """Add a new choice value to the existing choices of a parser argument.""" + for action in parser._actions: + exist_arg = isinstance(action, argparse.Action) and argument_name in action.option_strings + if exist_arg and action.choices is not None and new_choice not in action.choices: + action.choices.append(new_choice) +~~~ + +#### 参数特性支持 +| verl参数 | 简介| +| --- | --- | +| `actor_rollout_ref.actor.megatron.optimizer_offload` |是否卸载模型优化器到CPU,默认值为False| +| `actor_rollout_ref.actor.megatron.use_mbridge` |是否启用 mbridge:为 True(默认)时 engine 会构造 `bridge` 并交给 checkpoint manager,从而可读写 `model/huggingface/`;`save_contents` / `load_contents` 含 `hf_model` 时 manager 要求 `bridge` 非空(通常即此项为 True)。可与 `use_dist_checkpointing` 同时开启,在同一 checkpoint 中同时写入 HF 树与 `model/dist_ckpt/` 分片。为 False 时一般无 `hf_model`;仅 `model` 槽位走 `dist_checkpointing` 时需配合 `use_dist_checkpointing=True`| +| `actor_rollout_ref.actor.megatron.param_offload` |是否卸载模型权重到CPU,默认值为False| +| `actor_rollout_ref.actor.megatron.tensor_model_parallel_size` | 张量并行大小;默认值为1。| +| `actor_rollout_ref.actor.megatron.pipeline_model_parallel_size` |流水并行大小,默认值为1| +| `actor_rollout_ref.actor.megatron.expert_model_parallel_size` | 专家并行大小,默认值为1| +| `actor_rollout_ref.actor.megatron.expert_tensor_parallel_size`|TP拓展EP大小,默认值为null| +| `actor_rollout_ref.actor.context_parallel_size`|序列并行大小,默认值为1| +| `actor_rollout_ref.actor.megatron.override_transformer_config.deallocate_pipeline_outputs`|张量在发送到下一个pp stage后,输出数据被释放,降低显存峰值,默认值为False| +| `actor_rollout_ref.actor.megatron.override_transformer_config.persist_layer_norm` |是否使用持久化 LayerNorm,默认值为False| +| `actor_rollout_ref.actor.megatron.override_transformer_config.moe_grouped_gemm` |是否使用Group GEMM,默认值为False| +| `actor_rollout_ref.actor.megatron.override_transformer_config.moe_router_dtype` |用于路由和专家输出加权平均的数据类型。使用 fp32 或 fp64 可以提高稳定性,尤其是在专家数量较多时,默认值为fp32| +| `actor_rollout_ref.actor.megatron.override_transformer_config.account_for_loss_in_pipeline_split` |如果设置为 True,在流水线并行的划分和放置策略中,loss 层会被视为一个标准的 Transformer 层来处理。默认为False。| +| `actor_rollout_ref.actor.megatron.override_transformer_config.account_for_embedding_in_pipeline_split` |如果设置为 True,在流水线并行的划分和放置策略中,输入embedding 层会被视为一个标准的 Transformer 层来处理。默认为False。| +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity` |重新计算激活的粒度,可选项为'full', 'selective' and 'none'。其中full代表重新计算整个transformer layer,selective代表只计算transformer layer中的核心注意力部分。默认为'none'。| +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method` |该参数需将recompute_granularity设置为'full'才生效,可选项为'uniform', 'block'。默认为None。| +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers` |该参数需将recompute_granularity设置为'full'才生效,默认为None。若recompute_method设置为uniform,该参数含义为每个均匀划分的重新计算单元的transformer layers数量。例如你可以指定为--recompute_granularity full --recompute_method uniform --recompute_num_layers 4。recompute_num_layers越大,显存占用越小,计算成本越大。注意:当前进程中的模型层数需能被recompute_num_layers整除。默认为None。| +| `actor_rollout_ref.actor.megatron.use_dist_checkpointing` |为 True 时,`model` 槽位使用 Megatron `dist_checkpointing` 分片(`model/dist_ckpt/`)。与 `use_mbridge` 独立:两者可同时为 True 以保存/加载分片 + HF 导出。默认 False| +| `actor_rollout_ref.actor.megatron.dist_checkpointing_path` |分布式权重路径,默认值为null| +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn` |是否使用Flash Attention,默认值为true| +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rotary_pos_emb` |是否使用融合旋转位置编码,默认值为False| +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu` |是否使用融合swiglu,默认值为False| +| `actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_first_pipeline_stage` |第一个pipeline stage 的层数,默认值为none| +| `actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_last_pipeline_stage` |最后一个pipeline stage 的层数,默认值为none| + +注:`actor_rollout_ref.actor.megatron.use_mbridge` 与 `actor_rollout_ref.actor.megatron.virtual_pipeline_model_parallel_size` (VPP) 暂不支持同时开启。由于 verl 默认开启 mbridge,使用 VPP 参数时请手动将 `actor_rollout_ref.actor.megatron.use_mbridge` 置为 False。 + +### 3. VeOmni + +VeOmni 是一个统一的强化学习训练后端,专为大规模模型的高效训练而设计。它基于 FSDP 构建,提供了丰富的并行策略和优化功能,特别适合 MoE 模型和大规模分布式训练场景。 + +#### 参数特性支持 + +| verl参数 | 简介| +| --- | --- | +| `actor_rollout_ref.actor.veomni.param_offload` | 是否卸载模型权重到CPU,默认值为False| +| `actor_rollout_ref.actor.veomni.optimizer_offload` | 是否卸载优化器状态到CPU,默认值为False| +| `actor_rollout_ref.actor.veomni.fsdp_size` | 每个FSDP分片组中的NPU数量;默认值-1表示自动| +| `actor_rollout_ref.actor.veomni.ulysses_parallel_size` | Ulysses序列并行大小,默认值为1| +| `actor_rollout_ref.actor.veomni.expert_parallel_size` | 专家并行大小,默认值为1| +| `actor_rollout_ref.actor.veomni.mixed_precision` | 是否启用混合精度训练,默认值为true| +| `actor_rollout_ref.actor.veomni.enable_full_shard` | 是否启用完全分片(ZeRO-3),默认值为true| +| `actor_rollout_ref.actor.veomni.forward_prefetch` | 是否在前向计算完成前预取下一次前向传播的all-gather,默认值为true| +| `actor_rollout_ref.actor.veomni.attn_implementation` | Attention实现方式,支持eager、sdpa、flash_attention_2、flash_attention_3、veomni_flash_attention_2_with_sp、veomni_flash_attention_3_with_sp、native-sparse等| +| `actor_rollout_ref.actor.veomni.moe_implementation` | MoE实现方式,支持eager或fused,默认值为fused| +| `actor_rollout_ref.actor.veomni.cross_entropy_loss_implementation` | 交叉熵损失实现,默认值为eager| +| `actor_rollout_ref.actor.veomni.rms_norm_implementation` | RMSNorm实现,默认值为eager| +| `actor_rollout_ref.actor.veomni.swiglu_mlp_implementation` | SwiGLU MLP实现,默认值为eager| +| `actor_rollout_ref.actor.veomni.rotary_pos_emb_implementation` | 旋转位置编码实现,默认值为eager| +| `actor_rollout_ref.actor.veomni.load_balancing_loss_implementation` | MoE负载均衡损失实现,默认值为eager| +| `actor_rollout_ref.actor.veomni.use_torch_compile` | 是否使用torch compile,默认值为false| +| `actor_rollout_ref.actor.veomni.forward_only` | 是否只进行前向计算,默认值为false| +| `actor_rollout_ref.actor.veomni.enable_fsdp_offload` | 是否启用FSDP的CPU卸载,默认值为false| +| `actor_rollout_ref.actor.veomni.enable_reentrant` | 是否使用可重入的梯度检查点,默认值为false| +| `actor_rollout_ref.actor.veomni.ckpt_manager` | 检查点管理器,默认值为dcp| +| `actor_rollout_ref.actor.veomni.init_device` | 模型权重初始化设备,支持cpu、cuda、meta、npu,默认值为meta| +| `actor_rollout_ref.actor.veomni.activation_gpu_limit` | 激活卸载时GPU上允许保留的激活显存限制(GB),默认值为0.0| +| `actor_rollout_ref.rollout.moe_load_balance_metrics_interval` | Rollout侧MoE专家负载指标上报间隔,默认值为0(禁用);需要同时开启`actor_rollout_ref.rollout.enable_rollout_routing_replay`以记录路由决策| + +#### Router Replay 支持 + +VeOmni 后端支持 MoE 模型的 Router Replay 功能,通过 `actor_rollout_ref.actor.veomni.router_replay` 配置: + +| 参数 | 简介 | +| --- | --- | +| `mode` | Router replay模式,支持disabled(禁用)、R2(记录并重放路由决策)、R3(在rollout端记录并重放)| +| `record_file` | 记录路由决策的文件路径,在R2/R3模式下必需| +| `replay_file` | 加载路由决策进行重放的文件路径,在replay模式下必需| + +#### 使用示例 + +VeOmni 后端特别适合大规模 MoE 模型的 GRPO 训练,典型配置如下: + +```bash +# 设置 VeOmni 训练后端 +model_engine=veomni + +# 配置并行策略 +actor_rollout_ref.actor.veomni.fsdp_size=16 +actor_rollout_ref.actor.veomni.ulysses_parallel_size=1 +actor_rollout_ref.actor.veomni.expert_parallel_size=1 + +# 配置内存优化 +actor_rollout_ref.actor.veomni.param_offload=True +actor_rollout_ref.actor.veomni.optimizer_offload=True + +# 配置算子实现 +actor_rollout_ref.actor.veomni.attn_implementation=veomni_flash_attention_2_with_sp +actor_rollout_ref.actor.veomni.moe_implementation=fused +``` + +#### 主要特性 + +- **高效并行策略**:支持数据并行、Ulysses序列并行、专家并行的灵活组合 +- **内存优化**:支持参数卸载、优化器卸载和激活卸载,有效降低显存占用 +- **MoE优化**:提供融合的MoE实现和Router Replay功能,提升MoE模型训练效率 +- **算子优化**:支持多种attention和MLP算子实现,可根据硬件选择最优实现 +- **灵活部署**:支持NVIDIA GPU和华为Ascend NPU,具有良好的跨平台兼容性 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/feature_support/npu_advance_features.md b/verl_0720_main/verl/docs/ascend_tutorial/feature_support/npu_advance_features.md new file mode 100644 index 0000000000000000000000000000000000000000..f867d1929152c9bd751ad77ba18ce548ca511220 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/feature_support/npu_advance_features.md @@ -0,0 +1,254 @@ +# NPU 高级特性指南 + +> 本文档介绍昇腾 NPU 在 verl 生态中的高级特性与优化能力,供开发者参考。 +> +Last updated: 05/13/2026. + +--- + +## 目录 + +- [NPU 高级特性指南](#npu-高级特性指南) + - [目录](#目录) + - [1. 推理后端高级特性](#1-推理后端高级特性) + - [1.1 vLLM 推理后端](#11-vllm-推理后端) + - [1.2 SGLang 推理后端](#12-sglang-推理后端) + - [高级参数配置](#高级参数配置) + - [2. 训练后端高级特性](#2-训练后端高级特性) + - [2.1 FSDP 训练后端](#21-fsdp-训练后端) + - [2.2 Megatron 训练后端](#22-megatron-训练后端) + - [MindSpeed Monkey Patch 框架原理](#mindspeed-monkey-patch-框架原理) + - [Megatron 高级参数配置](#megatron-高级参数配置) + - [内存与计算优化](#内存与计算优化) + - [融合算子加速](#融合算子加速) + - [流水线并行优化](#流水线并行优化) + - [权重管理](#权重管理) + - [3. 性能优化特性](#3-性能优化特性) + - [3.1 内存优化](#31-内存优化) + - [3.2 计算加速](#32-计算加速) + - [3.3 并行策略](#33-并行策略) + - [4. 混合专家模型 (MoE) 特性](#4-混合专家模型-moe-特性) + - [vLLM/SGLang 推理 MoE 支持](#vllmsglang-推理-moe-支持) + - [Megatron 训练 MoE 支持](#megatron-训练-moe-支持) + - [5. 限制与注意事项](#5-限制与注意事项) + - [附录:参数速查表](#附录参数速查表) + - [推理后端参数速查](#推理后端参数速查) + - [训练后端参数速查](#训练后端参数速查) + +--- + +## 1. 推理后端高级特性 + +当前 verl 支持 vLLM 和 SGLang 两种主流推理后端,均可在昇腾 NPU 上运行。以下列出各后端支持的高级特性参数。 + +### 1.1 vLLM 推理后端 + +昇腾通过 **vllm-ascend 插件** 支持 vLLM 推理后端。该插件遵循 [RFC](https://github.com/vllm-project/vllm/issues/11162),提供可插拔接口将 Ascend NPU 与 vLLM 解耦。 + +--- + +### 1.2 SGLang 推理后端 + +昇腾通过向 SGLang 社区持续建设与维护来支持相关功能,涉及以下核心组件: + +| 组件 | 描述 | +|:---|:---| +| [sgl_kernel_npu](https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/sgl_kernel_npu/README.md) | Ascend NPU 优化推理内核集合,含注意力机制、归一化、激活函数、LoRA 适配器等 | +| [deepep](https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/deep_ep/README.md) | DeepEP 的 Ascend 实现,为 MoE 模型提供高度优化的专家并行 (EP) 通信内核 | + +#### 高级参数配置 + +| SGLang 参数 | verl 对应通用参数 | 功能说明 | +|:---|:---|:---| +| `attention_backend` | `actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend` | **注意力后端选择** — NPU 上应设置为 `ascend` 以调用昇腾优化内核 | +| `quantization` | `actor_rollout_ref.rollout.quantization` | **量化支持** — 支持模型量化加载与推理 | + + +> 更多 SGLang NPU 特性参数请参考 [sglang 社区 NPU 特性支持文档](https://docs.sglang.io/docs/hardware-platforms/ascend-npus/ascend_npu_support_features) + +--- + +## 2. 训练后端高级特性 + +### 2.1 FSDP 训练后端 + +昇腾通过 `torch_npu` 提供 FSDP 相关支持能力。 + +### 2.2 Megatron 训练后端 + +Megatron 是 NVIDIA 推出的模型并行训练框架。在 NPU 上运行需额外安装 **MindSpeed** 提供底层支持。MindSpeed 采用 **Monkey Patch** 技术无感替换 Megatron 关键组件,实现 NPU 适配。 + +#### MindSpeed Monkey Patch 框架原理 + +**触发入口:** +```python +from mindspeed.megatron_adaptor import repatch +``` + +**调用链:** +``` +repatch +├── 执行 megatron_adaptor.py 模块导入 +├── 导入 features_manager 模块 +├── 执行 mindspeed/features_manager/__init__.py +├── @AutoExecuteFunction 装饰器触发 +├── patch_features() 自动执行 +└── 进行 apply_features_pre_patches 和 apply_features_patches 操作 +``` + +**核心组件:** + +| 组件 | 职责 | +|:---|:---| +| `Patch` 类 | 实现函数/类的动态替换,支持多层装饰器叠加 | +| `parse_path()` | 动态模块导入和创建 | +| `MindSpeedPatchesManager` | 全局单例管理所有 patch 注册 | +| `MindSpeedFeature` | Feature 基类,各特性通过继承集成 patch 系统 | + +#### Megatron 高级参数配置 + +##### 内存与计算优化 + +| verl 参数 | 功能说明 | +|:---|:---| +| `actor_rollout_ref.actor.megatron.override_transformer_config.deallocate_pipeline_outputs` | **流水线输出释放** — 张量发送到下一 PP stage 后释放输出数据,降低显存峰值,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity` | **重计算粒度控制** — 可选 `full` / `selective` / `none`。`full` 重算整个 Transformer 层,`selective` 仅重算注意力核心部分,默认 `none` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method` | **重计算方法** — 需 `recompute_granularity=full`,可选 `uniform` / `block`,默认 `None` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers` | **重计算层数** — 需 `recompute_granularity=full`,值越大显存占用越小、计算成本越高,需能被当前进程模型层数整除 | + +##### 融合算子加速 + +| verl 参数 | 功能说明 | +|:---|:---| +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn` | **Flash Attention** — 是否使用 Flash Attention 加速注意力计算,默认 `true` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rotary_pos_emb` | **融合旋转位置编码** — 使用融合算子加速 RoPE 计算,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu` | **融合 SwiGLU** — 使用融合算子加速 SwiGLU 激活函数,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.persist_layer_norm` | **持久化 LayerNorm** — 使用持久化策略优化 LayerNorm,默认 `False` | + +##### 流水线并行优化 + +| verl 参数 | 功能说明 | +|:---|:---| +| `actor_rollout_ref.actor.megatron.override_transformer_config.account_for_loss_in_pipeline_split` | **Loss 层流水线划分** — 将 loss 层视为标准 Transformer 层参与划分,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.account_for_embedding_in_pipeline_split` | **Embedding 层流水线划分** — 将输入 embedding 层视为标准 Transformer 层参与划分,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_first_pipeline_stage` | **首 stage 层数** — 指定第一个 pipeline stage 的层数,默认 `none` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_last_pipeline_stage` | **末 stage 层数** — 指定最后一个 pipeline stage 的层数,默认 `none` | + +##### 权重管理 + +| verl 参数 | 功能说明 | +|:---|:---| +| `actor_rollout_ref.actor.megatron.use_mbridge` | **MBridge 权重转换** — 启用 mbridge 进行权重格式转换 | +| `actor_rollout_ref.actor.megatron.use_dist_checkpointing` | **分布式 checkpoint** — 使用分布式格式保存/加载权重,默认 `False` | +| `actor_rollout_ref.actor.megatron.dist_checkpointing_path` | **分布式权重路径** — 分布式 checkpoint 加载路径,默认 `null` | + +--- + +## 3. 性能优化特性 + +### 3.1 内存优化 + +| 特性 | 推理/训练 | 说明 | +|:---|:---|:---| +| KV Cache 动态释放 (`free_cache_engine`) | 推理 (vLLM) | 生成阶段后自动卸载 KV Cache,默认启用 | +| 内存节省模式 (`enable_memory_saver`) | 推理 (SGLang) | 支持显存动态释放/恢复,verl 默认 `True` | +| 参数 CPU 卸载 (`param_offload`) | 训练 (FSDP/Megatron) | 将模型权重卸载到 CPU | +| 优化器 CPU 卸载 (`optimizer_offload`) | 训练 (FSDP/Megatron) | 将优化器状态卸载到 CPU | +| 分块熵计算 (`entropy_from_logits_with_chunking`) | 训练 (FSDP) | 分块计算熵值降低显存峰值 | +| 熵计算分块大小 (`entropy_from_logits_chunk_size`) | 训练 (FSDP) | 熵计算分块大小 | +| 熵计算重计算 (`entropy_checkpointing`) | 训练 (FSDP) | 对熵计算启用重计算 | +| 流水线输出释放 (`deallocate_pipeline_outputs`) | 训练 (Megatron) | PP 场景下释放已传递的张量 | +| 激活重计算 (`recompute_granularity`) | 训练 (Megatron) | 支持 full/selective/none 三级粒度控制 | + +### 3.2 计算加速 + +| 特性 | 推理/训练 | 说明 | +|:---|:---|:---| +| 分块预填充 (`enable_chunked_prefill`) | 推理 (vLLM) | 大预填充分块并与解码 batch 处理 | +| 前缀缓存 (`enable_prefix_caching`) | 推理 (vLLM) | 自动缓存共享前缀,减少重复计算 | +| Flash Attention | 训练 (Megatron) | 使用 Flash Attention 加速注意力计算,默认启用 | +| 融合旋转位置编码 (`use_fused_rotary_pos_emb`) | 训练 (Megatron) | 融合算子加速 RoPE | +| 融合 SwiGLU (`use_fused_swiglu`) | 训练 (Megatron) | 融合算子加速 SwiGLU 激活函数 | +| 持久化 LayerNorm (`persist_layer_norm`) | 训练 (Megatron) | 优化 LayerNorm 执行策略 | +| Group GEMM (`moe_grouped_gemm`) | 训练 (Megatron) | MoE 场景下的 Group GEMM 优化 | + +### 3.3 并行策略 + +| 并行类型 | vLLM | SGLang | FSDP | Megatron | 说明 | +|:---|:---|:---|:---|:---|:---| +| 数据并行 (DP) | ✅ | ✅ | ✅ | ✅ | 数据维度并行 | +| 张量并行 (TP) | ✅ | ✅ | — | ✅ | 层内张量切分 | +| 流水线并行 (PP) | — | — | — | ✅ | 层间流水线切分 | +| 专家并行 (EP) | ✅ | ✅ | — | ✅ | MoE 专家维度并行 | +| 序列并行 (SP/Ulysses) | ✅ | ✅ | ✅ | ✅ | 序列维度切分,支持长序列 | +| 上下文并行 (CP) | ✅ | — | — | ✅ | 上下文并行处理 | + +--- + +## 4. 混合专家模型 (MoE) 特性 + +### vLLM/SGLang 推理 MoE 支持 + +- **专家并行 (EP)** — 通过 `ep_size` 参数配置,将不同专家分配到不同 NPU 设备 +- SGLang 通过 [deepep](https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/deep_ep/README.md) 提供高度优化的 EP 通信内核 + +### Megatron 训练 MoE 支持 + +| verl 参数 | 功能说明 | +|:---|:---| +| `actor_rollout_ref.actor.megatron.expert_model_parallel_size` | 专家并行 (EP) 大小,默认 `1` | +| `actor_rollout_ref.actor.megatron.expert_tensor_parallel_size` | TP 拓展 EP 大小,默认 `null` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.moe_grouped_gemm` | **Group GEMM** — MoE 场景下使用 Group GEMM 优化专家计算,默认 `False` | +| `actor_rollout_ref.actor.megatron.override_transformer_config.moe_router_dtype` | **路由数据类型** — 路由与专家输出加权平均的数据类型,可选 `fp32`/`fp64`,默认 `fp32`,提高多专家场景稳定性 | + +--- + +## 5. 限制与注意事项 + +1. **mbridge 与 VPP 互斥** + - `actor_rollout_ref.actor.megatron.use_mbridge` 与 `actor_rollout_ref.actor.megatron.virtual_pipeline_model_parallel_size` (VPP) **暂不支持同时开启** + - 由于 verl 默认开启 mbridge,使用 VPP 时需手动将 `use_mbridge` 置为 `False` + +2. **FSDP1 vs FSDP2 差异** + - `forward_prefetch` 和 `use_orig_params` 仅适用于 FSDP1 + - FSDP2 为默认推荐版本,API 支持度参照 [昇腾 PyTorch 版本说明](https://www.hiascend.com/document/detail/zh/Pytorch/730/apiref/PyTorchNativeapi/docs/zh/native_apis/pytorch_2-7-1/torch-distributed-fsdp.md) + +3. **重计算参数依赖关系** + - `recompute_method` 需 `recompute_granularity='full'` 才生效 + - `recompute_num_layers` 需 `recompute_granularity='full'` 才生效 + - 当 `recompute_method='uniform'` 时,`recompute_num_layers` 表示每个重计算单元的 Transformer 层数,需能被当前进程模型层数整除 + +4. **SGLang NPU 特有配置** + - `attention_backend` 必须设置为 `ascend` 以调用昇腾优化内核 + - `enable_memory_saver` 在 verl 中默认启用,无需额外配置 + +--- + +## 附录:参数速查表 + +### 推理后端参数速查 + +| 参数类别 | vLLM 参数 | SGLang 参数 | verl 通用参数 | +|:---|:---|:---|:---| +| 模型路径 | `model_path` | `model_path` | `actor_rollout_ref.model.path` | +| 显存控制 | `gpu_memory_utilization` | `mem_fraction_static` | `actor_rollout_ref.rollout.gpu_memory_utilization` | +| 图模式 | `enforce_eager` | `disable_cuda_graph` | `actor_rollout_ref.rollout.enforce_eager` | +| 量化 | `quantization` | `quantization` | `actor_rollout_ref.rollout.quantization` | +| 最大序列长度 | `max_model_len` | — | `actor_rollout_ref.rollout.max_model_len` | +| 最大并发数 | `max_num_seqs` | `max_running_requests` | `actor_rollout_ref.rollout.max_num_seqs` | +| 分词器 | `skip_tokenizer_init` | `skip_tokenizer_init` | `actor_rollout_ref.rollout.skip_tokenizer_init` | +| 远程代码 | `trust_remote_code` | `trust_remote_code` | `actor_rollout_ref.model.trust_remote_code` | +| TP 并行 | `tp_size` | `tp_size` | `actor_rollout_ref.rollout.tensor_model_parallel_size` | +| DP 并行 | `dp_size` | `dp_size` | `actor_rollout_ref.rollout.data_parallel_size` | +| EP 并行 | `ep_size` | `ep_size` | `actor_rollout_ref.rollout.expert_parallel_size` | + +### 训练后端参数速查 + +| 参数类别 | FSDP 参数 | Megatron 参数 | +|:---|:---|:---| +| 参数卸载 | `fsdp_config.param_offload` | `megatron.param_offload` | +| 优化器卸载 | `fsdp_config.optimizer_offload` | `megatron.optimizer_offload` | +| 序列并行 | `ulysses_sequence_parallel_size` | `context_parallel_size` | +| Flash Attention | — | `override_transformer_config.use_flash_attn` | +| 重计算粒度 | — | `override_transformer_config.recompute_granularity` | +| 分布式 Checkpoint | — | `use_dist_checkpointing` | diff --git a/verl_0720_main/verl/docs/ascend_tutorial/get_start/dockerfile_build_guidance.rst b/verl_0720_main/verl/docs/ascend_tutorial/get_start/dockerfile_build_guidance.rst new file mode 100644 index 0000000000000000000000000000000000000000..68696e90eaddc1c461b3bb4e96077af0405404fa --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/get_start/dockerfile_build_guidance.rst @@ -0,0 +1,170 @@ +Ascend Dockerfile Build Guidance +=================================== + +Last updated: 06/23/2026. + + +镜像获取与公开镜像地址 +-------------------------- + +昇腾在 `quay.io/ascend/verl `_ 中托管每日构建的 A2/A3 镜像,基于 `Dockerfile <../../../docker/ascend>`_ 构建,具体说明见 :ref:`Dockerfile构建镜像脚本清单 `。 + +每日构建镜像名格式:latest-{CANN版本}-{torch_npu版本}[-{适用产品信息}-{操作系统}]-{Python版本}[-{推理后端}-{其他字段}] + +verl release版本镜像名格式:{verl release版本号}-{CANN版本}-{torch_npu版本}[-{适用产品信息}-{操作系统}]-{Python版本}[-{推理后端}-{其他字段}] + + + +镜像硬件支持 +----------------------------------- + +Atlas 200T A2 Box16 + +Atlas 900 A2 PODc + +Atlas 800T A3 + + +最新镜像内各组件版本信息清单 +---------------- + +================= ============ +组件 版本 +================= ============ +基础镜像 Ubuntu 22.04 +Python 3.11 +CANN 9.0.0 +torch 2.9.0 +torch_npu 2.9.0 +torchvision 0.24.0 +vLLM 0.18.0 +vLLM-ascend 0.18.0 +Megatron-LM core_r0.16.0 +MindSpeed core_r0.16.0 +triton-ascend 3.2.1 +mbridge 0.15.1 +SGLang v0.5.10 +sgl-kernel-npu 2026.02.01 +================= ============ + + + +.. _ascend-dockerfile-list: + +Dockerfile构建镜像脚本清单 +--------------------------- + +**通用镜像** + +============== ==================== ============== ============================================================== +设备类型 CANN基础镜像版本 推理后端 参考文件 +============== ==================== ============== ============================================================== +A2 9.0.0 vLLM `Dockerfile.ascend_9.0.0_a2 `_ +A3 9.0.0 vLLM `Dockerfile.ascend_9.0.0_a3 `_ +A2 8.5.0 vLLM `Dockerfile.ascend_8.5.0_a2 `_ +A3 8.5.0 vLLM `Dockerfile.ascend_8.5.0_a3 `_ +A2 8.5.0 SGLang `Dockerfile.ascend.sglang_8.5.0_a2 `_ +A3 8.5.0 SGLang `Dockerfile.ascend.sglang_8.5.0_a3 `_ +A2 8.3.RC1 vLLM `Dockerfile.ascend_8.3.rc1_a2 `_ +A3 8.3.RC1 vLLM `Dockerfile.ascend_8.3.rc1_a3 `_ +A2 8.3.RC1 SGLang `Dockerfile.ascend.sglang_8.3.rc1_a2 `_ +A3 8.3.RC1 SGLang `Dockerfile.ascend.sglang_8.3.rc1_a3 `_ +A2 8.2.RC1 vLLM `Dockerfile.ascend_8.2.rc1_a2 `_ +A3 8.2.RC1 vLLM `Dockerfile.ascend_8.2.rc1_a3 `_ +============== ==================== ============== ============================================================== + + +**verl release版本镜像** + +============== ==================== ============== ============== ============================================================== +设备类型 CANN基础镜像版本 推理后端 verl版本 参考文件 +============== ==================== ============== ============== ============================================================== +A2 9.0.0 vLLM release/v0.8.0 `Dockerfile.ascend_9.0.0_a2_v0.8.0 `_ +A3 9.0.0 vLLM release/v0.8.0 `Dockerfile.ascend_9.0.0_a3_v0.8.0 `_ +A2 8.5.0 vLLM release/v0.7.1 `Dockerfile.ascend_8.5.0_a2_v0.7.1 `_ +A3 8.5.0 vLLM release/v0.7.1 `Dockerfile.ascend_8.5.0_a3_v0.7.1 `_ +============== ==================== ============== ============== ============================================================== + + +**模型定制镜像** + +============== ==================== ============== ============== ============================================================== +设备类型 CANN基础镜像版本 推理后端 模型 参考文件 +============== ==================== ============== ============== ============================================================== +A2 8.5.2 vLLM Qwen3.5 `Dockerfile.ascend_8.5.2_a2_qwen3-5 `_ +A3 8.5.2 vLLM Qwen3.5 `Dockerfile.ascend_8.5.2_a3_qwen3-5 `_ +============== ==================== ============== ============== ============================================================== + + + +**说明:** + +* 推理后端为 ``vLLM`` 镜像中,vLLM、vLLM-ascend、MindSpeed、Megatron-LM、verl 为源码安装,源码位于镜像根目录 ``/`` 下。 +* 推理后端为 ``SGLang`` 镜像中,SGLang、MindSpeed、verl 为源码安装,源码位于镜像根目录 ``/`` 下。 + + +镜像构建命令示例 +-------------------- + +.. code:: bash + + # Navigate to the directory containing the Dockerfile + cd {verl-root-path}/docker/ascend + + # Build the image + # vLLM + docker build -f Dockerfile.ascend_8.5.0_a2 -t verl-ascend:8.5.0-a2 . + # SGLang + docker build -f Dockerfile.ascend.sglang_8.5.0_a2 -t verl-ascend-sglang:8.5.0-a2 . + + # Query local images after build + docker images + +**说明:** + +* 以 vLLM 的镜像为例,``Dockerfile.ascend_8.5.0_a2`` 为 Dockerfile 文件名,``verl-ascend:8.5.0-a2`` 中,verl-ascend 为自定义的镜像名称,8.5.0-a2 为自定义的镜像标签 + +容器启动命令模板 +---------------- + +.. code:: bash + + docker run -dit \ + --ipc=host \ + --network host \ + --name {your_docker_name} \ + --privileged \ + -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \ + -v /usr/local/Ascend/firmware:/usr/local/Ascend/firmware \ + -v /usr/local/sbin:/usr/local/sbin \ + -v /usr/sbin:/usr/sbin \ + -v /home:/home \ + -v /data:/data \ + {image_name}:{tag} \ + /bin/bash + +**说明:** + +* 如需挂载其他本地路径到容器,请自行添加 ``-v <宿主机路径>:<容器内路径>`` +* 建议将 ``{your_docker_name}`` 替换为具有实际意义的容器名称 +* ``--privileged`` 参数授予容器扩展权限,请根据实际安全需求评估是否必要 +* ``{image_name}:{tag}`` 请换成容器构建时对应的镜像名称与标签 + +启动容器 +-------- + +.. code:: bash + + docker start {your_docker_name} + +进入正在运行的容器 +------------------ + +.. code:: bash + + docker exec -it {your_docker_name} bash + + +声明 +-------------------- +verl中提供的ascend相关Dockerfile、镜像皆为参考样例,可用于尝鲜体验,如在生产环境中使用请通过官方正式途径沟通,谢谢。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance.rst b/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance.rst new file mode 100644 index 0000000000000000000000000000000000000000..755d5741dca36f95dee2aabd59236d6bd8dcaa52 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance.rst @@ -0,0 +1,293 @@ +Ascend Install Guidance +================= + +Last updated: 2026/05/20. + +关键更新 +-------- + +- 2026/05/13:vLLM 已按 `PR + #6291 `__\ 将 vLLM / + vLLM-Ascend 从 ``0.13.0`` 更新为 ``0.18.0``\ ,vLLM + 对应基础环境版本同步调整为 torch ``2.9.0``\ 、torch_npu + ``2.9.0.post2``\ 。 +- 2025/12/11:verl 存量场景目前支持自动识别 NPU 设备类型。原则上,GPU + 脚本在昇腾上运行时不再需要显式设置 + ``trainer.device=npu``\ ;新增特性仍可通过设置 ``trainer.device`` + 优先指定设备类型。 + +.. + + [说明] 自动识别 NPU 设备类型的前提,是运行程序所在环境包含 + ``torch_npu`` 软件包。如环境中不包含 ``torch_npu``\ ,仍需显式指定 + ``trainer.device=npu``\ 。 + +目录 +-------- + +- `硬件支持 <#硬件支持>`_ +- `框架后端支持说明 <#框架后端支持说明>`_ +- `部署指南 <#部署指南>`_ + - `Docker镜像获取、构建和使用 <#1-docker镜像获取构建和使用>`_ + - `自定义安装-vLLM + FSDP/Megatron <#2-自定义安装-vllm--fsdpmegatron>`_ + - `自定义安装-SGLang + FSDP/Megatron <#3-自定义安装-sglang--fsdpmegatron>`_ + - `训练后端拓展-MindSpeed-LLM后端部署 <#4-训练后端拓展>`_ +- `附录 <#附录>`_ + +硬件支持 +-------- + +Atlas 200T A2 Box16 + +Atlas 900 A2 PODc + +Atlas 800T A3 + +`Atlas 950DT A5 `_ + + +框架后端支持说明 +---------------- + +当前NPU上支持以下常见训推后端的部署,您可以根据我们的 `镜像部署指南 `__ 直接获取发布的镜像,也可以根据下文进行自定义安装。 + +.. list-table:: + :header-rows: 1 + + * - 推理引擎 + - 训练引擎 + * - vLLM + - FSDP/FSDP2/Megatron + * - SGLang + - FSDP/FSDP2/Megatron + +训练后端拓展 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +verl将训推后端抽象解耦,支持灵活接入自定义各类训推后端,当前拓展训练后端如下: + +MindSpeed-LLM:MindSpeed-LLM是基于昇腾生态的大语言模型分布式训练套件,当前已接入verl,安装部署方法参照章节 `训练后端拓展-MindSpeed-LLM后端部署 <#mindspeed-llm-训练后端支持>`_ + + +部署指南 +-------- + +1. Docker镜像获取、构建和使用 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +您可以从 `quay.io/ascend/verl `_ 获取相关镜像,或者自行从DockerFile构建,相关说明参照 +`镜像部署指南 `__\ 。 + + +2. 自定义安装-vLLM + FSDP/Megatron +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + +关键版本支持与依赖 +^^^^^^^^^^^^^^^^^ + +============= ======================================= =================== +依赖 版本 说明 +============= ======================================= =================== +HDK ``26.0.rc1`` NPU硬件驱动与固件 +CANN ``9.0.0`` CANN软件,帮助开发者实现在昇腾软硬件平台上开发和运行AI业务 +Python ``>=3.10, <3.12``\ ,推荐 ``3.11`` +torch ``2.9.0`` PyTorch 深度学习框架基础包 +torch_npu ``2.9.0.post2`` NPU PyTorch 适配插件 +torchvision ``0.24.0`` PyTorch 图像处理库 +torchaudio ``2.9.0`` PyTorch 音频处理库 +triton ``3.5.0`` Triton,用于编写自定义算子 +triton-ascend ``3.2.1`` NPU Triton 适配 +transformers ``5.3.0`` Hugging Face 大模型库,提供模型架构与预训练权重 +vLLM ``0.18.0`` 高性能 LLM 推理与服务引擎 +vLLM-Ascend ``0.18.0`` NPU vLLM 后端适配 +Megatron-LM ``core_r0.16.0`` 大规模分布式训练框架 +MindSpeed ``core_r0.16.0`` Megatron-LM 在昇腾 NPU 上的适配和优化组件 +============= ======================================= =================== + + +安装前准备(HDK & CANN) +^^^^^^^^^^^^^^^^^^^^^^^^ + +CANN是NPU上的异构计算架构,以下为arm平台A3安装指令,请参照如下指令下载HDK和CANN并安装, +或者根据系统硬件型号从 `CANN社区 `_ 下载安装 + +.. code:: bash + + #配置用户属组 + sudo groupadd HwHiAiUser + sudo useradd -g HwHiAiUser -d /home/HwHiAiUser -m HwHiAiUser -s /bin/bash + # 安装依赖&配源 + sudo yum makecache + sudo yum install -y gcc python3 python3-pip kernel-headers-$(uname -r) kernel-devel-$(uname -r) + sudo curl https://repo.oepkgs.net/ascend/cann/ascend.repo -o /etc/yum.repos.d/ascend.repo && yum makecache + # 安装NPU驱动 + sudo yum install -y Atlas-A3-hdk-npu-driver-26.0.rc1 + # 安装Toolkit,可指定--install-path 自定义路径 + sudo yum install -y Ascend-cann-toolkit-9.0.0 + sudo yum install -y Ascend-cann-A3-ops-9.0.0 + # 安装后验证 + source /usr/local/Ascend/ascend-toolkit/set_env.sh + python3 -c "import acl;print(acl.get_soc_name())" + +源码安装 +^^^^^^^^^^^^^^^^^^^^^^^^ + +我们提供了基于conda一键部署 `安装脚本 <../../../scripts/install_vllm_mcore_npu.sh>`_ , 脚本分步骤安装环境,如果中途遇到安装报错,请根据当前步骤报错信息提示查看原因,或通过issue给我们留言,我们将尽快解决 + +.. code:: bash + + # 注意:在 x86 平台安装时,pip 需要配置额外的源,指令如下: + # pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/" + # 使能CANN环境, 如果您自定义了CANN的路径,请根据自定义路径修改以下使能命令 + source /usr/local/Ascend/ascend-toolkit/set_env.sh + source /usr/local/Ascend/nnal/atb/set_env.sh + conda create -n verl-vllm-npu python=3.11 -y + conda activate verl-vllm-npu + git clone --recursive https://github.com/verl-project/verl.git + bash verl/scripts/install_vllm_mcore_npu.sh + # 如果您仅需要使用FSDP后端 + # USE_MEGATRON=0 bash scripts/install_vllm_mcore_npu.sh + +3. 自定义安装-SGLang + FSDP/Megatron +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +关键版本支持与依赖 +^^^^^^^^^^^^^^^^^ + +============= ======================================= =================== +依赖 版本 说明 +============= ======================================= =================== +HDK ``25.5.0`` NPU硬件驱动与固件 +CANN ``>=8.5.0`` CANN软件,帮助开发者实现在昇腾软硬件平台上开发和运行AI业务 +Python ``>=3.10, <3.12``\ ,推荐 ``3.11`` +torch ``2.8.0`` PyTorch 深度学习框架基础包 +torch_npu ``2.8.0.post2`` NPU PyTorch 适配插件 +SGLang ``v0.5.10`` 高性能 LLM 推理引擎 +triton ``3.5.0`` Triton,用于编写自定义算子 +triton-ascend ``3.2.1`` NPU Triton 适配 +transformers ``5.3.0`` Hugging Face 大模型库,提供模型架构与预训练权重 +Megatron-LM ``core_r0.16.0`` 大规模分布式训练框架 +MindSpeed ``core_r0.16.0`` Megatron-LM 在昇腾 NPU 上的适配和优化组件 +============= ======================================= =================== + + +安装前准备(HDK & CANN) +^^^^^^^^^^^^^^^^^^^^^^^^ + +CANN是NPU上的异构计算架构,以下为arm平台A3安装指令,请参照如下指令下载HDK和CANN并安装, +或者根据系统硬件型号从 `CANN社区 `_ 下载安装 + +.. code:: bash + + #配置用户属组 + sudo groupadd HwHiAiUser + sudo useradd -g HwHiAiUser -d /home/HwHiAiUser -m HwHiAiUser -s /bin/bash + # 安装依赖&配源 + sudo yum makecache + sudo yum install -y gcc python3 python3-pip kernel-headers-$(uname -r) kernel-devel-$(uname -r) + sudo curl https://repo.oepkgs.net/ascend/cann/ascend.repo -o /etc/yum.repos.d/ascend.repo && yum makecache + # 安装NPU驱动 + sudo yum install -y Atlas-A3-hdk-npu-driver-25.5.0 + # 安装Toolkit,可指定--install-path 自定义路径 + sudo yum install -y Ascend-cann-toolkit-8.5.0 + sudo yum install -y Ascend-cann-A3-ops-8.5.0 + # 安装后验证 + source /usr/local/Ascend/ascend-toolkit/set_env.sh + python3 -c "import acl;print(acl.get_soc_name())" + +源码安装 +^^^^^^^^^^^^^^^^^^^^^^^^ + +我们提供了基于conda一键部署 `安装脚本 <../../../scripts/install_sglang_mcore_npu.sh>`_ , 脚本分步骤安装环境,如果中途遇到安装报错,请根据当前步骤报错信息提示查看原因,或通过issue给我们留言,我们将尽快解决 + +.. code:: bash + + # 注意:在 x86 平台安装时,pip 需要配置额外的源,指令如下: + # pip config set global.extra-index-url "https://download.pytorch.org/whl/cpu/" + # 使能CANN环境, 如果您自定义了CANN的路径,请根据自定义路径修改以下使能命令 + source /usr/local/Ascend/ascend-toolkit/set_env.sh + source /usr/local/Ascend/nnal/atb/set_env.sh + conda create -n verl-sgl-npu python=3.11 -y + conda activate verl-sgl-npu + git clone --recursive https://github.com/verl-project/verl.git + bash verl/scripts/install_sglang_mcore_npu.sh + # 如果您仅需要使用FSDP后端 + # USE_MEGATRON=0 bash verl/scripts/install_sglang_mcore_npu.sh + +SGLang 使用注意事项 +^^^^^^^^^^^^^^^ + +当前 NPU 上支持 SGLang 后端必须添加以下环境变量: + +.. code:: bash + + # 支持 NPU 单卡多进程 + export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 + export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + + # 规避 Ray 在 device 侧调用无法根据 is_npu_available 接口识别设备可用性 + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + + # 根据当前设备和需要卡数定义 + export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + # in A3 + # export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + + # 使能推理 EP 时需要 + export SGLANG_DEEPEP_BF16_DISPATCH=1 + +4. 训练后端拓展 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +MindSpeed-LLM 训练后端支持 +^^^^^^^^^^^^^^^ + +如需使用基于 Megatron/MindSpeed 体系的 MindSpeed-LLM 训练后端,需要额外下载 +MindSpeed-LLM。需要注意的是,MindSpeed-LLM 训练后端依赖 MindSpeed-LLM +master 分支、MindSpeed master 分支以及 Megatron-LM ``core_v0.12.1`` +分支。 + +MindSpeed-LLM 及相关依赖的源码安装指令: + +.. code:: bash + + # 下载 MindSpeed-LLM、MindSpeed 和 Megatron-LM + git clone https://gitcode.com/Ascend/MindSpeed-LLM.git + git clone https://gitcode.com/Ascend/MindSpeed.git + git clone --depth 1 --branch core_v0.12.1 https://github.com/NVIDIA/Megatron-LM.git + + # 配置环境变量 + export PYTHONPATH=$PYTHONPATH:/your/path/Megatron-LM + export PYTHONPATH=$PYTHONPATH:/your/path/MindSpeed + export PYTHONPATH=$PYTHONPATH:/your/path/MindSpeed-LLM + + # 安装 mbridge + pip install mbridge + +MindSpeed-LLM 作为基于 Megatron/MindSpeed 体系的昇腾 LLM 训练后端使用时,使用方式如下: + +1. 使能 verl worker 模型 ``strategy`` 配置为 ``mindspeed``\ ,例如 + ``actor_rollout_ref.actor.strategy=mindspeed``\ 。 +2. MindSpeed-LLM 自定义入参可通过 ``llm_kwargs`` 参数传入,例如对 MOE + 模型开启 GMM 特性可使用 + ``+actor_rollout_ref.actor.mindspeed.llm_kwargs.moe_grouped_gemm=True``\ 。 +3. 更多特性信息可参考 `MindSpeed-LLM + 内的特性文档 `__\ 。 + +附录 +---------------- + +昇腾暂不支持生态库说明 +~~~~~~~~~~~~~~~~~~~~~~ + +verl 中昇腾暂不支持生态库如下: + ++------------------+--------------------------------------------------+ +| 软件 | 说明 | ++==================+==================================================+ +| ``flash_attn`` | 不支持通过独立 ``flash_attn`` 包使能 flash | +| | attention 加速,支持通过 transformers 使用 | ++------------------+--------------------------------------------------+ + + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance_A5.rst b/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance_A5.rst new file mode 100644 index 0000000000000000000000000000000000000000..a1f51799c67e4ba7e0ffff51e6bf477a75c91463 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/get_start/install_guidance_A5.rst @@ -0,0 +1,81 @@ +Last updated: 07/09/2026. + +关键版本支持与依赖 +^^^^^^^^^^^^^^^^^ +============= ================================================= =================== +依赖 版本 说明 +============= ================================================= =================== +CANN 待Q2 CANN版本正式商发后更新链接 CANN软件,帮助开发者实现在昇腾软硬件平台上开发和运行AI业务 +Python ``3.11`` Python版本 +torch ``2.10.0`` PyTorch 深度学习框架基础包 +torch_npu 待Q2 torch_npu版本正式商发后更新链接 NPU PyTorch 适配插件 +triton ``3.5.0`` Triton,用于编写自定义算子 +triton-ascend ``3.2.2`` NPU Triton 适配 +transformers ``4.57.3`` Hugging Face 大模型库,提供模型架构与预训练权重 +vLLM ``0.20.2`` 高性能 LLM 推理与服务引擎 +vLLM-Ascend ``fac8784c2572b14b1134f04d9818926b4a297f3a`` NPU vLLM 后端适配 +Megatron-LM ``core_r0.12.0`` 大规模分布式训练框架 +MindSpeed ``0c6c0ceaa523a96032dee1539a52032155e6404e`` Megatron-LM 在昇腾 NPU 上的适配和优化组件 +============= ================================================= =================== + +环境安装步骤 +^^^^^^^^^^^^^^^^^ + +vLLM推理后端支持 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +.. code:: bash + + #安装vllm + git clone https://github.com/vllm-project/vllm.git + cd vllm + git checkout v0.20.2 + VLLM_TARGET_DEVICE=empty pip install -v -e . + cd .. + + #安装vllm-ascend + #安装之前要先source cann环境: source /usr/local/Ascend/cann/set_env.sh + git clone https://github.com/vllm-project/vllm-ascend.git + cd vllm-ascend + git checkout fac8784c2572b14b1134f04d9818926b4a297f3a + git cherry-pick c8b402071ecfc9c36c4eba195dfa6bffea1988f2 + pip install -v -e . --no-build-isolation --extra-index-url https://triton-ascend.osinfra.cn/pypi/simple/ --trusted-host triton-ascend.osinfra.cn + cd .. + + +Megatron 训练后端支持 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +MindSpeed和Megatron及相关依赖的源码安装指令: + +.. code:: bash + + # MindSpeed + git clone https://gitcode.com/Ascend/MindSpeed.git + cd MindSpeed + git checkout 0c6c0ceaa523a96032dee1539a52032155e6404e + pip install -e . + cd .. + + # Megatron + git clone https://github.com/NVIDIA/Megatron-LM.git + cd Megatron-LM + git checkout core_r0.12.0 + pip install -e . + cd .. + + # 配置环境变量 + export PYTHONPATH=$PYTHONPATH:your path/Megatron-LM + export PYTHONPATH=$PYTHONPATH:your path/MindSpeed + + # 安装 mbridge + pip install mbridge + +verl 依赖安装 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + git clone https://github.com/verl-project/verl.git + cd verl + pip install -e . + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/get_start/quick_start.rst b/verl_0720_main/verl/docs/ascend_tutorial/get_start/quick_start.rst new file mode 100644 index 0000000000000000000000000000000000000000..f7be015461b9d72eb1fdd43f8aceac91807c286c --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/get_start/quick_start.rst @@ -0,0 +1,153 @@ +Ascend Quickstart +================= + +**Last updated:** 2026/07/14. + +关键更新 +-------- + +- 2026/06/30:新增覆盖四种常用训推后端组合,便于用户在 quickstart 阶段快速选择合适的启动脚本。 +- 2026/05/13:将 quick start 和 install guidance 分开。 +- 2025/12/11:verl 存量场景目前支持自动识别 NPU 设备类型,GPU 脚本在昇腾上运行,原则上不再需要显式设置 ``trainer.device=npu`` 参数,新增特性通过设置 ``trainer.device`` 仍可优先使用,逐步适配自动识别能力。 + + +目录 +-------- + +- `硬件支持 <#硬件支持>`_ +- `Qwen3-0.6B GSM8K GRPO Quick Start <#qwen3-06b-gsm8k-grpo-quick-start>`_ + - `权重准备 <#权重准备>`_ + - `数据准备 <#数据准备>`_ + - `运行方式 <#运行方式>`_ +- `SGLang 后端使能说明 <#sglang-后端使能说明>`_ + - `vLLM 后端脚本转换为 SGLang <#vllm-后端脚本转换为-sglang>`_ + +硬件支持 +-------- + +- Atlas 200T A2 Box16 +- Atlas 900 A2 PODc +- Atlas 800T A3 + + + +Qwen3-0.6B GSM8K GRPO Quick Start +--------------------------------- + +本文面向 Ascend NPU 环境,提供基于 GSM8K 和 Qwen3-0.6B 的最小 GRPO 训练验证流程。 + +文档覆盖四种常用训推后端组合,便于用户在 quickstart 阶段快速选择合适的启动脚本。 + +运行本文脚本前,请确认已完成 verl Ascend 环境安装。 +环境安装详见 `install_guidance <./install_guidance.rst>`_ 。 + +四个脚本均默认使用 ``Qwen/Qwen3-0.6B`` 和 GSM8K 数据集进行基础链路验证。 + +主要用于检查: + +- verl 入口是否可用; +- 数据是否可读取; +- actor、rollout、reference worker 是否能初始化; +- vLLM-Ascend/sglang rollout 是否能生成; +- 训练链路是否能完成首个 step。 + +权重准备 +~~~~~~ + +权重需自行从huggingface上下载 + +脚本中的默认读取权重路径为 ``~/models/Qwen/Qwen3-0.6B`` + +建议将权重放在该路径下,或者修改脚本中MODEL_PATH指向本地路径 + + +数据准备 +~~~~~~ + +.. code-block:: bash + + python3 examples/data_preprocess/gsm8k.py --local_dataset_path /download/path/hf_data/gsm8k/ + +gsm8k原始数据集需自行从huggingface上下载 + +生成文件: + +.. code-block:: text + + ~/data/gsm8k/train.parquet + ~/data/gsm8k/test.parquet + +运行方式 +~~~~~~ + +相关脚本均已放置于 ``tests/special_npu/quick_start/`` 路径下 + +首先进入verl路径: ``cd /your/path/verl`` + +使能CANN环境:如果您自定义了CANN的路径,请根据自定义路径修改以下使能命令 + +.. code-block:: bash + + source /usr/local/Ascend/ascend-toolkit/set_env.sh + source /usr/local/Ascend/nnal/atb/set_env.sh + +Quick Start 当前提供四种常用训推后端组合。用户可根据训练后端和 rollout 后端选择对应脚本 + +.. list-table:: + :header-rows: 1 + :widths: 20 20 20 60 + + * - 组合 + - 训练后端 + - rollout 后端 + - 运行方式 + * - vLLM + FSDP2 + - FSDP2 + - vLLM-Ascend + - bash tests/special_npu/quick_start/run_qwen3_0_6b_fsdp2_vllm_ascend.sh + * - vLLM + Megatron + - Megatron + - vLLM-Ascend + - bash tests/special_npu/quick_start/run_qwen3_0_6b_megatron_vllm_ascend.sh + * - SGLang + FSDP2 + - FSDP2 + - SGLang + - bash tests/special_npu/quick_start/run_qwen3_0_6b_fsdp2_sglang_ascend.sh + * - SGLang + Megatron + - Megatron + - SGLang + - bash tests/special_npu/quick_start/run_qwen3_0_6b_megatron_sglang_ascend.sh + +脚本内具体参数说明详见 `训练配置参数与指标说明 `_ + +SGLang 后端使能说明 +------------------------------------------- + +当前 verl 已解析推理常见参数,详见 `async_sglang_server.py <../../../verl/workers/rollout/sglang_rollout/async_sglang_server.py>`_ 中 ``ServerArgs`` 初始化传参。 + +其他 `SGLang 参数 `_ 均可通过 ``engine_kwargs`` 进行参数传递。 + +vLLM 后端脚本转换为 SGLang +~~~~~~~~ + +如需自行将 vLLM 后端推理脚本转换为 SGLang,需要添加或修改以下参数。 + +.. code-block:: bash + + # 必须 + actor_rollout_ref.rollout.name=sglang \ + +actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend="ascend" \ + + # 可选 + # 使能推理 EP,详细使用方法见: + # https://github.com/sgl-project/sgl-kernel-npu/blob/main/python/deep_ep/README_CN.md + ++actor_rollout_ref.rollout.engine_kwargs.sglang.deepep_mode="auto" \ + ++actor_rollout_ref.rollout.engine_kwargs.sglang.moe_a2a_backend="deepep" \ + + # MoE 模型多 DP 时必须设置为 True + +actor_rollout_ref.rollout.engine_kwargs.sglang.enable_dp_attention=False \ + + # chunked_prefill 默认关闭 + +actor_rollout_ref.rollout.engine_kwargs.sglang.chunked_prefill_size=-1 + + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/index.rst b/verl_0720_main/verl/docs/ascend_tutorial/index.rst new file mode 100644 index 0000000000000000000000000000000000000000..4d048d7fc49e096a447db35b9f133f5e5964d531 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/index.rst @@ -0,0 +1,54 @@ +Ascend (NPU) Tutorial +===================== + +Last updated: 06/05/2026. + +.. toctree:: + :maxdepth: 1 + :caption: Getting Started + + README.md + get_start/dockerfile_build_guidance + get_start/install_guidance + get_start/quick_start + get_start/install_guidance_A5 + +.. toctree:: + :maxdepth: 1 + :caption: Feature Support + + feature_support/ascend_backend_features + feature_support/npu_advance_features + +.. toctree:: + :maxdepth: 1 + :caption: Model Support + + model_support/model_and_algorithm_support + model_support/examples/ascend_retool_best_practice + model_support/examples/ascend_sglang_best_practices + model_support/examples/ascend_vllm_best_practices + model_support/examples/dapo_multi_model_optimization_practice + model_support/examples/gspo_optimization_practice + model_support/examples/qwen3_5_megatron_npu + +.. toctree:: + :maxdepth: 1 + :caption: Developer Guide + + dev_guide/model_dev/evaluation + dev_guide/model_dev/parameter_and_metrics + dev_guide/model_dev/transfer_to_npu_guide + dev_guide/precision_analysis/precision_alignment_zh + dev_guide/precision_analysis/precision_debugger_zh + dev_guide/performance/ascend_performance_analysis_guide + dev_guide/performance/perf_tuning_on_ascend + dev_guide/performance/ascend_profiling_zh + dev_guide/performance/ascend_profiling_en + +.. toctree:: + :maxdepth: 1 + :caption: FAQ & Contributing + + faq/faq + contribution_guide/ascend_ci_guide_zh diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_retool_best_practice.rst b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_retool_best_practice.rst new file mode 100644 index 0000000000000000000000000000000000000000..6e668f2eaa866b79a2ad3cb141437a2866177f99 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_retool_best_practice.rst @@ -0,0 +1,281 @@ +Ascend ReTool Best Practice +=================================== + +Last updated: 07/03/2026. + +引言 +---------------------------------- + +ReTool论文参考([ReTool](https://arxiv.org/pdf/2504.11536)) +集成代码解释器工具,通过多轮实时代码执行进行策略部署,并教会模型根据结果反馈学习何时以及如何调用工具。 + +1. 环境构建 +2. 模型训练 + +用例模型脚本以及其需要的硬件条件各自如下: + +.. list-table:: + :header-rows: 1 + + * - 模型 + - NPU型号 + - 节点数量 + - 训练与推理后端 + * - ``Qwen2.5-7B`` + - Atlas 900 A2 + - 1 + - ``vLLM + FSDP`` + +环境构建 +----------------------------------- +1. 从自定义Conda环境进行构建 + +.. list-table:: + :header-rows: 1 + + * - software + - version + * - Python + - ``3.11`` + * - CANN + - ``==9.0.0.B160`` (CANN900B160) + * - torch + - ``==2.9.0`` + * - torch_npu + - ``==2.9.0`` + * - triton_ascend + - ``==3.2.1`` + * - verl + - ``main`` + * - vllm + - ``v0.18.0`` + * - vllm-ascend + - ``v0.18.0`` + * - transformers + - ``5.3.0`` + +模型训练与评估 +----------------------------------- +1. 模型数据准备 +^^^^^^^^^^^ +`Qwen2.5-7B` +^^^^^^^^^^^ +**下载模型权重** + +.. code-block:: bash + + git clone https://huggingface.co/Qwen/Qwen2.5-7B-Instruct + +**下载训练数据集** + +.. code-block:: bash + + git clone https://huggingface.co/datasets/BytedTsinghua-SIA/DAPO-Math-17k + +**下载评估数据集** + +.. code-block:: bash + + git clone https://huggingface.co/datasets/Maxwell-Jia/AIME_2024 + +**预训练数据预处理** + +.. code-block:: bash + + python3 recipe/retool/retool_sft_preprocess.py + +*注:自动下载ReTool-SFT,最后生成数据默认保存在~/ReTool-SFT/data目录下* + +**执行预训练脚本** + +.. code-block:: bash + + bash recipe/retool/run_qwen2_7b_sft_npu.sh # 需适配脚本中路径 + +**合并预训练权重生成checkpoint** + +.. code-block:: bash + + python3 -m verl.model_merger merge --backend fsdp \ + --local_dir /PATH/TO/checkpoint/multiturn-sft-qwen-2.5-7b-instruct/global_step_372 \ + --target_dir /PATH/TO/checkpoint/multiturn-sft-qwen-2.5-7b-instruct/global_step_372/huggingface + +2. 代码沙箱准备 + +开源沙箱代码及部署参考 +https://github.com/bytedance/SandboxFusion + +**沙箱代码下载** + +.. code-block:: bash + + git clone -b main https://github.com/bytedance/SandboxFusion.git + +**沙箱安装** + +.. code-block:: bash + + cd SandboxFusion + conda create -n sandbox -y python=3.11 + conda activate sandbox + pip install poetry + poetry lock + poetry install + mkdir -p docs/build + cd runtime/python + bash install-python-runtime.sh + cd ../../ + make run-online + +3. 训练 + +示例配置文件如下,在recipe/retool目录下创建一个run_qwen2.5_7b_dapo_npu.sh +根据开发者实际路径配置情况修改模型训练脚本中的以下参数 + +.. code-block:: bash + + set -x + + export VLLM_USE_V1=1 + export TORCHDYNAMO_DISABLE=1 + export VLLM_ASCEND_ENABLE_NZ=0 + export TASK_QUEUE_ENABLE=1 + export VLLM_ENABLE_GRAPH_MODE=1 + export HCCL_OP_EXPANSION_MODE="AIV" + export VLLM_ASCEND_ENABLE_MLP_OPTIMIZE=1 + export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 + + # ================= data/model/tool ================= + HDFS_ROOT=${HDFS_ROOT:-"${PWD}"} + DATA_ROOT=${DATA_ROOT:-"${PWD}"} + + dapo_math_17k=$DATA_ROOT/dataset/BytedTsinghua-SIA/DAPO-Math-17k + aime_2024=$DATA_ROOT/dataset/Maxwell-Jia/AIME_2024 + #aime_2025=$DATA_ROOT/dataset/yentinglin/aime_2025 + model_path=$DATA_ROOT/dataset/checkpoint/multiturn-sft-qwen-2.5-7b-instruct/global_step_372/huggingface + + train_files="['$dapo_math_17k']" + test_files="['$aime_2024']" + + # tool + tool_config_path=recipe/retool/sandbox_fusion_tool_config.yaml + + # wandb + project_name=retool + experiment_name=qwen2.5-7b_dapo + default_local_dir=$DATA_ROOT/checkpoint/$experiment_name + + # 创建日志文件 + export TIMESTAMP=$(date +%Y%m%d_%H%M%S) + LOG_DIR="$HDFS_ROOT/verl/logs/$project_name/$experiment_name" + # 判断路径是否存在 + if [ ! -d "$LOG_DIR" ]; then + # 路径不存在,创建路径 + mkdir -p "$LOG_DIR" + echo "Directory $LOG_DIR created." + else + echo "Directory $LOG_DIR already exists." + fi + + LOG_FILE="${LOG_DIR}/${TIMESTAMP}.log" + touch "$LOG_FILE" + echo "Log file $LOG_FILE created." + + # ================= algorithm ================= + adv_estimator=grpo + + use_kl_in_reward=False + kl_coef=0.0 + use_kl_loss=False + kl_loss_coef=0.0 + + clip_ratio_low=0.2 + clip_ratio_high=0.28 + + max_turns=16 + max_prompt_length=2048 + max_response_length=20480 + actor_lr=1e-6 + + train_batch_size=32 + ppo_mini_batch_size=16 + + n_resp_per_prompt=16 + n_resp_per_prompt_val=30 + + # ================= performance ================= + infer_tp=2 # vllm + train_sp=4 # train + offload=True + + actor_max_token_len_per_gpu=$(( (max_prompt_length + max_response_length) * 1 )) + log_prob_max_token_len_per_gpu=$(( actor_max_token_len_per_gpu * 4 )) + + PYTHONUNBUFFERED=1 python3 -m verl.trainer.main_ppo \ + algorithm.adv_estimator=$adv_estimator \ + algorithm.use_kl_in_reward=$use_kl_in_reward \ + algorithm.kl_ctrl.kl_coef=$kl_coef \ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.return_raw_chat=True \ + data.train_batch_size=$train_batch_size \ + data.max_prompt_length=$max_prompt_length \ + data.max_response_length=$max_response_length \ + data.filter_overlong_prompts=True \ + data.truncation='error' \ + data.custom_cls.path=recipe/retool/retool.py \ + data.custom_cls.name=CustomRLHFDataset \ + custom_reward_function.path=recipe/retool/retool.py \ + custom_reward_function.name=compute_score \ + actor_rollout_ref.model.path=$model_path \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.model.enable_gradient_checkpointing=True \ + actor_rollout_ref.actor.use_kl_loss=$use_kl_loss \ + actor_rollout_ref.actor.kl_loss_coef=$kl_loss_coef \ + actor_rollout_ref.actor.clip_ratio_low=$clip_ratio_low \ + actor_rollout_ref.actor.clip_ratio_high=$clip_ratio_high \ + actor_rollout_ref.actor.clip_ratio_c=10.0 \ + actor_rollout_ref.actor.optim.lr=$actor_lr \ + actor_rollout_ref.actor.use_dynamic_bsz=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=$ppo_mini_batch_size \ + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=$actor_max_token_len_per_gpu \ + actor_rollout_ref.actor.ulysses_sequence_parallel_size=$train_sp \ + actor_rollout_ref.actor.fsdp_config.param_offload=$offload \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=$offload \ + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=$log_prob_max_token_len_per_gpu \ + actor_rollout_ref.rollout.max_num_batched_tokens=$actor_max_token_len_per_gpu \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.mode=async \ + actor_rollout_ref.rollout.max_num_seqs=1024 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=$infer_tp \ + actor_rollout_ref.rollout.multi_turn.enable=True \ + actor_rollout_ref.rollout.multi_turn.max_user_turns=$max_turns \ + actor_rollout_ref.rollout.multi_turn.max_assistant_turns=$max_turns \ + actor_rollout_ref.rollout.multi_turn.tool_config_path=$tool_config_path \ + actor_rollout_ref.rollout.multi_turn.format=hermes \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.9 \ + actor_rollout_ref.rollout.n=$n_resp_per_prompt \ + actor_rollout_ref.rollout.val_kwargs.top_p=0.6 \ + actor_rollout_ref.rollout.val_kwargs.temperature=1.0 \ + actor_rollout_ref.rollout.val_kwargs.n=$n_resp_per_prompt_val \ + actor_rollout_ref.rollout.enable_chunked_prefill=True \ + actor_rollout_ref.rollout.enforce_eager=False \ + trainer.logger=['console'] \ + trainer.project_name=$project_name \ + trainer.experiment_name=$experiment_name \ + trainer.n_gpus_per_node=8 \ + trainer.val_before_train=False \ + trainer.log_val_generations=20 \ + trainer.nnodes=1 \ + trainer.save_freq=100 \ + trainer.default_local_dir=$default_local_dir \ + trainer.test_freq=20 \ + trainer.device=npu \ + actor_rollout_ref.actor.entropy_from_logits_with_chunking=True \ + actor_rollout_ref.ref.entropy_from_logits_with_chunking=True \ + actor_rollout_ref.actor.use_torch_compile=False \ + actor_rollout_ref.ref.use_torch_compile=False \ + actor_rollout_ref.actor.entropy_checkpointing=True \ + actor_rollout_ref.ref.entropy_checkpointing=True \ + actor_rollout_ref.ref.use_torch_compile=False \ + trainer.total_epochs=1 $@ > $LOG_FILE 2>&1 & diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_sglang_best_practices.rst b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_sglang_best_practices.rst new file mode 100644 index 0000000000000000000000000000000000000000..16eb86cced7d3893401993c03f750af37f3154d2 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_sglang_best_practices.rst @@ -0,0 +1,279 @@ +Ascend SGLang Best Practice +=================================== + +Last updated: 06/02/2026. + +.. _Qwen3-30B: https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_megatron.sh +.. _doclink: https://github.com/verl-project/verl/blob/c98cb8cc/docs/ascend_tutorial/examples/ascend_sglang_best_practices.rst +引言 +---------------------------------- + +SGLang 是当前主流的高性能开源推理引擎, 昇腾已经全面原生支持该推理引擎在verl中使用, +仅需简单的构建流程,开发者即可完成环境构建,本文将帮助开发者了解以下内容: + +1. 环境构建 +2. 模型训练与评估 +3. 性能采集 + +用例模型脚本以及其需要的硬件条件如下: + +- 注:verl近期进行了脚本清理与命名变更, 推荐根据 commit id c98cb8cc 对应文档 `doclink`_ 及对应脚本进行构建避免链接失效 + ++----------------------+---------------------+----------+------------------------+ +| 模型 | NPU型号 | 节点数量 | 训推后端 | ++======================+=====================+==========+========================+ +| `Qwen3-30B`_ | Atlas 800T A3 | 1 | SGLang + Megatron | ++----------------------+---------------------+----------+------------------------+ + +环境构建 +----------------------------------- +我们在 `install_guidance <../../get_start/install_guidance.rst>`_ 中提供了两种构建环境的方法, 1.从镜像文件Dockerfile进行构建 2.从自定义Conda环境进行构建 + +在本实践中, 我们额外指定verl 的commit id 以避免引入其他问题 + +.. code-block:: bash + + cd verl + git checkout 772c224 +模型训练与评估 +----------------------------------- +1.模型数据准备 +^^^^^^^^^^^ +`Qwen3-30B`_ +^^^^^^^^^^^ +**下载模型权重** + +--local-dir: 模型保存路径 + +.. code-block:: bash + + export HF_ENDPOINT=https://hf-mirror.com + huggingface-cli download --resume-download Qwen/Qwen3-30B-A3B --local-dir /path/to/local_dir + +**下载数据集** + +.. code-block:: bash + + git clone https://www.modelscope.cn/datasets/AI-ModelScope/DAPO-Math-17k.git + +**HuggingFace To Megatron权重转换(可选)** + +.. code-block:: bash + + python scripts/converter_hf_to_mcore.py \ + --hf_model_path Qwen/Qwen3-30B-A3B \ + --output_path Qwen/Qwen3-30B-A3B-mcore \ + --use_cpu_initialization # Only work for MoE models +*注:verl当前已支持mbridge进行灵活的hf和mcore之间的权重转换,可以修改以下相关参数直接加载hf权重* + +.. code-block:: bash + + actor_rollout_ref.actor.megatron.use_dist_checkpointing=False + actor_rollout_ref.actor.megatron.use_mbridge=True + +2.训练 +^^^^^^^^^^^ +根据开发者实际路径配置情况修改模型训练脚本中的以下参数 + +.. code-block:: bash + + # Model Weights Paths + MODEL_PATH=Qwen/Qwen3-30B-A3B + MCORE_MODEL_PATH=Qwen/Qwen3-30B-A3B-mcore + RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} + CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + + # File System Paths + TRAIN_FILE=$RAY_DATA_HOME/dataset/dapo-math-17k.parquet + TEST_FILE=$RAY_DATA_HOME/dataset/aime-2024.parquet + + #保存频率,-1默认不保存,如需评测请修改此参数 + trainer.save_freq=-1 + +对于单机任务 `Qwen3-30B`_ , 可以直接bash执行verl仓上示例脚本 + +.. code-block:: bash + + bash examples/grpo_trainer/run_qwen3moe-30b_sglang_megatron_npu.sh +如果您想扩展至多节点,我们推荐使用以下脚本进行大规模多节点训练拉起 + +.. code-block:: bash + + pkill -9 python + ray stop --force + rm -rf /tmp/ray + export RAY_DEDUP_LOGS=0 + export HYDRA_FULL_ERROR=1 + # TASK_QUEUE_ENABLE,下发优化,图模式设置为1,非图模式设置为2 + export TASK_QUEUE_ENABLE=1 + export HCCL_ASYNC_ERROR_HANDLING=0 + export HCCL_EXEC_TIMEOUT=3600 + export HCCL_CONNECT_TIMEOUT=3600 + + export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 + export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + # 修改为当前需要跑的用例路径 + DEFAULT_SH="./run_*.sh" + echo "Use $DEFAULT_SH" + + ulimit -n 32768 + mkdir logs + + NNODES=2 + NPUS_PER_NODE=8 + # 修改为对应主节点IP + MASTER_ADDR="IP FOR MASTER NODE" + # 修改为当前节点的通信网卡 + SOCKET_IFNAME="Your SOCKET IFNAME" + export HCCL_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" + export GLOO_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" + # 获取当前IP + CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') + if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then + # 主节点启动 + ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + + while true; do + ray_status_output=$(ray status) + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') + device_count=$((npu_count_int / $NPUS_PER_NODE)) + + # 判断device_count 是否与 NNODES 相等 + if [ "$device_count" -eq "$NNODES" ]; then + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." + ray status + bash $DEFAULT_SH + break + else + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" + sleep 5 + fi + done + else + # 子节点尝试往主节点注册 ray 直到成功 + while true; do + # 尝试连接 ray 集群 + ray start --address="$MASTER_ADDR:6766" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP + + # 检查连接是否成功 + ray status + if [ $? -eq 0 ]; then + echo "Successfully connected to the Ray cluster!" + break + else + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." + sleep 5 + fi + done + fi + + sleep 600 + +DEFAULT_SH:修改为训练所用配置 sh 文件路径。 + +NNODES 和 NPUS_PER_NODE:修改为使用节点数量和每个节点 NPU 数量。在此案例中分别为2和8。 + +MASTER_ADDR:修改为对应主节点 IP。即所有节点的 MASTER_ADDR 应该相同。 + +SOCKET_IFNAME, HCCL_SOCKET_IFNAME, GLOO_SOCKET_IFNAME: 修改为对应通信网卡,通信网卡可以通过以下命令获取: + +.. code-block:: bash + + ifconfig |grep "$(hostname -I |awk '{print $1}'|awk -F '.' '{print $0}')" -B 1|awk -F ':' '{print$1}' | head -1 | tail -1 + +3.模型评估 +^^^^^^^^^^^ + +不同模型步骤一致,仅以Qwen3-30b为例列举 + +我们通过 AISBenchmark 评估模型,该工具支持vllm/sglang多种推理后端的评估 + +**安装方法** + +.. code-block:: bash + + git clone https://gitee.com/aisbench/benchmark.git + cd benchmark + pip install -e . + pip install math_verify latex2sympy2_extended + +**下载评估数据集** + +.. code-block:: bash + + cd path/to/benchmark/ais_bench/datasets + wget http://opencompass.oss-cn-shanghai.aliyuncs.com/datasets/data/math.zip + unzip math.zip + rm math.zip + +**修改AISBench配置代码使能sglang推理评测** + +打开 benchmark/ais_bench/benchmark/configs/models/vllm_api/vllm_api_stream_chat.py 文件,这是推理配置文件 + +.. code-block:: bash + + from ais_bench.benchmark.models import VLLMCustomAPIChatStream + from ais_bench.benchmark.utils.model_postprocessors import extract_non_reasoning_content + from ais_bench.benchmark.clients import OpenAIChatStreamClient, OpenAIChatStreamSglangClient + + models = [ + dict( + attr="service", + type=VLLMCustomAPIChatStream, + abbr='sgl-api-stream-chat', + path="/path/to/Qwen3-30B", # 修改为 Qwen3-30B 模型路径 + model="qwen3-30b", + request_rate = 0, + max_seq_len=2048, + retry = 2, + host_ip = "localhost", # 推理服务的IP + host_port = 8005, # 推理服务的端口 + max_out_len = 8192, # 最大输出tokens长度 + batch_size=48, # 推理的最大并发数 + trust_remote_code=False, + custom_client=dict(type=OpenAIChatStreamSglangClient), #使用sglang客户端 + generation_kwargs = dict( + temperature = 0, + seed = 1234, + ), + pred_postprocessor=dict(type=extract_non_reasoning_content) + ) + ] + + +**启动sglang_server服务** + +.. code-block:: bash + + python -m sglang.launch_server --model-path "/path/to/Qwen3-30B" --tp-size 4 --dp-size 1 --port 8005 + +**启动sglang_client评测** + +.. code-block:: bash + + ais_bench --models vllm_api_stream_chat --datasets math500_gen_0_shot_cot_chat_prompt + +**评测结果** + +经过训练,模型在Math-500上的评分显著上升 + ++------+----------------------+---------+----------+------+----------------------+ +| iter | dataset | version | metric | mode | sgl-api-stream-chat | ++======+======================+=========+==========+======+======================+ +| 0 | math_prm800k_500 | c4b6f0 | accuracy | gen | 84.4 | ++------+----------------------+---------+----------+------+----------------------+ +| 150 | math_prm800k_500 | c4b6f0 | accuracy | gen | 91.7 | ++------+----------------------+---------+----------+------+----------------------+ + +性能采集 +----------------------------------- +关于NPU profiling的详细文档请参考 `ascend_profiling_zh <../../dev_guide/performance/ascend_profiling_zh.rst>`_ + +在 `Qwen3-30B`_ 的脚本中提供了基本的采集性能选项PROF_CONFIG,默认设置 global_profiler.steps=null 关闭采集, 开发者可根据实际需要进行参数修改 + +采集完成后,开发者可以使用 `MindStudio Insight `_ 进行数据解析 + +注: verl框架侧进行采集全量 Profiling 产生海量且重复的算子记录,可以根据文档修改代码仅采集关键阶段 \ No newline at end of file diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_vllm_best_practices.rst b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_vllm_best_practices.rst new file mode 100644 index 0000000000000000000000000000000000000000..729c944a03c159028efb13322849794c0aa256b7 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/ascend_vllm_best_practices.rst @@ -0,0 +1,306 @@ +Ascend vLLM Best Practice +=================================== + +Last updated: 06/06/2026. + +.. _Qwen3-30B: https://github.com/verl-project/verl/blob/release/v0.7.1/examples/grpo_trainer/run_qwen3moe-30b_grpo_megatron_vllm_npu.sh +.. _doclink: https://github.com/verl-project/verl/blob/c98cb8cc/docs/ascend_tutorial/examples/ascend_vllm_best_pratice.rst +引言 +---------------------------------- + +vLLM 是当前主流的高性能开源推理引擎, 昇腾已经全面原生支持该推理引擎在verl中使用, +仅需简单的构建流程,开发者即可完成环境构建,本文将提供两个经典用例来帮助开发者了解以下内容: + +1. 环境构建 +2. 模型训练与评估 +3. 性能采集 + +用例模型脚本以及其需要的硬件条件如下: + +- 注:verl近期进行了脚本清理与命名变更, 推荐根据 commit id c98cb8cc 对应文档 `doclink`_ 及对应脚本进行构建避免链接失效 + ++----------------------+---------------------+----------+----------------------------+ +| 模型 | NPU型号 | 节点数量 | 训推后端 | ++======================+=====================+==========+============================+ +| `Qwen3-30B`_ | Atlas 800T A3 | 1 | vLLM + Megatron | ++----------------------+---------------------+----------+----------------------------+ + + +环境构建 +----------------------------------- +我们在 `install_guidance <../../get_start/install_guidance.rst>`_ 中提供了两种构建环境的方法, 1.从镜像文件DockerFile进行构建 2.从自定义Conda环境进行构建 + +在本实践中, 我们额外指定verl 的commit id 以避免引入其他问题 + +.. code-block:: bash + + cd verl + git checkout release/v0.7.1 +模型训练与评估 +----------------------------------- +1.模型数据准备 +^^^^^^^^^^^ +`Qwen3-30B`_ +^^^^^^^^^^^ +**下载模型权重** + +--local-dir: 模型保存路径 + +.. code-block:: bash + + export HF_ENDPOINT=https://hf-mirror.com + huggingface-cli download --resume-download Qwen/Qwen3-30B-A3B-Base --local-dir /path/to/local_dir + +**下载数据集** + +.. code-block:: bash + + git clone https://www.modelscope.cn/datasets/modelscope/gsm8k.git + +**HuggingFace To Megatron权重转换(可选)** + +.. code-block:: bash + + python scripts/converter_hf_to_mcore.py \ + --hf_model_path Qwen/Qwen3-30B-A3B-Base \ + --output_path Qwen/Qwen3-30B-A3B-Base-mcore \ + --use_cpu_initialization # Only work for MoE models +*注:verl当前已支持mbridge进行灵活的hf和mcore之间的权重转换,可以修改以下相关参数直接加载hf权重* + +.. code-block:: bash + + actor_rollout_ref.actor.megatron.use_dist_checkpointing=False + actor_rollout_ref.actor.megatron.use_mbridge=True + +2.训练 +^^^^^^^^^^^ +根据开发者实际路径配置情况修改模型训练脚本中的以下参数 + +.. code-block:: bash + + # Model Weights Paths + MODEL_PATH=Qwen/Qwen3-30B-A3B-Base + MCORE_MODEL_PATH=Qwen/Qwen3-30B-A3B-Base-mcore + RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} + CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + + # File System Paths + TRAIN_FILE=$RAY_DATA_HOME/dataset/gsm8k/test.parquet + TEST_FILE=$RAY_DATA_HOME/dataset/gsm8k/test.parquet + + #保存频率,-1默认不保存,如需评测请修改此参数 + trainer.save_freq=-1 + +对于单机任务 `Qwen3-30B`_ , 可以直接bash执行verl仓上示例脚本,如: + +.. code-block:: bash + + bash examples/grpo_trainer/run_qwen3moe-30b_grpo_megatron_vllm_npu.sh +如果您想扩展至多节点 ,我们推荐使用以下脚本进行大规模多节点训练拉起 + +.. code-block:: bash + + pkill -9 python + ray stop --force + rm -rf /tmp/ray + export RAY_DEDUP_LOGS=0 + export HYDRA_FULL_ERROR=1 + # TASK_QUEUE_ENABLE,下发优化,图模式设置为1,非图模式设置为2 + export TASK_QUEUE_ENABLE=1 + export HCCL_ASYNC_ERROR_HANDLING=0 + export HCCL_EXEC_TIMEOUT=3600 + export HCCL_CONNECT_TIMEOUT=3600 + + export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 + export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 + export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + # 修改为当前需要跑的用例路径 + DEFAULT_SH="./run_*.sh" + echo "Use $DEFAULT_SH" + + ulimit -n 32768 + mkdir logs + + NNODES=2 + NPUS_PER_NODE=8 + # 修改为对应主节点IP + MASTER_ADDR="IP FOR MASTER NODE" + # 修改为当前节点的通信网卡 + SOCKET_IFNAME="Your SOCKET IFNAME" + export HCCL_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" + export GLOO_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" + # 获取当前IP + CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') + if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then + # 主节点启动 + ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + + while true; do + ray_status_output=$(ray status) + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') + device_count=$((npu_count_int / $NPUS_PER_NODE)) + + # 判断device_count 是否与 NNODES 相等 + if [ "$device_count" -eq "$NNODES" ]; then + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." + ray status + bash $DEFAULT_SH + break + else + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" + sleep 5 + fi + done + else + # 子节点尝试往主节点注册 ray 直到成功 + while true; do + # 尝试连接 ray 集群 + ray start --address="$MASTER_ADDR:6766" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP + + # 检查连接是否成功 + ray status + if [ $? -eq 0 ]; then + echo "Successfully connected to the Ray cluster!" + break + else + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." + sleep 5 + fi + done + fi + + sleep 600 + +DEFAULT_SH:修改为训练所用配置 sh 文件路径。 + +NNODES 和 NPUS_PER_NODE:修改为使用节点数量和每个节点 NPU 数量。在此案例中分别为2和8。 + +MASTER_ADDR:修改为对应主节点 IP。即所有节点的 MASTER_ADDR 应该相同。 + +SOCKET_IFNAME, HCCL_SOCKET_IFNAME, GLOO_SOCKET_IFNAME: 修改为对应通信网卡,通信网卡可以通过以下命令获取: + +.. code-block:: bash + + ifconfig |grep "$(hostname -I |awk '{print $1}'|awk -F '.' '{print $0}')" -B 1|awk -F ':' '{print$1}' | head -1 | tail -1 + +3.模型评估 +^^^^^^^^^^^ + +不同模型步骤一致,仅以Qwen3-30b为例列举 + +我们通过 AISBenchmark 评估模型,该工具支持vllm/sglang多种推理后端的评估 + +**安装方法** + +.. code-block:: bash + + git clone https://gitee.com/aisbench/benchmark.git + cd benchmark + pip install -e . + pip install math_verify latex2sympy2_extended + +**下载评估数据集** + +.. code-block:: bash + + cd /examples/benchmark/ais_bench/datasets + mkdir aime/ + cd aime/ + wget https://opencompass.oss-cn-shanghai.aliyuncs.com/datasets/data/aime.zip + unzip aime.zip + rm aime.zip + +**修改AISBench配置代码使能vllm推理评测** + +.. code-block:: bash + + vim /examples/benchmark/ais_bench/benchmark/configs/models/vllm_api/vllm_api_general.py + +python文件内容如下,host_port需与服务端的port一致,根据模型配置修改max_seq_len和max_out_len,推理示例设置为2k推20k: + +.. code-block:: bash + + from ais_bench.benchmark.models import VLLMCustomAPI + + models = [ + dict( + attr="service", + type=VLLMCustomAPI, + abbr='vllm-api-general', + path="/path/to/Qwen3-30B", # 修改为 Qwen3-30B 模型路径 + model="qwen3-30b", + request_rate = 0, + retry = 2, + host_ip = "localhost", # 推理服务的IP + host_port = 6380, + max_seq_len = 2048, # 最大输入tokens长度 + max_out_len = 20480, # 最大输出tokens长度 + batch_size=48, # 推理的最大并发数 + trust_remote_code=False, + generation_kwargs = dict( + temperature = 0.5, + top_k = 10, + top_p = 0.95, + seed = None, + repetition_penalty = 1.03, + ) + ) + ] + + +**启动vllm_server服务** + +通过以下命令拉起NPU服务端,需要修改的参数:model和tensor-parallel-size。 + +/path/to/Qwen3-30B/:保存训练后权重的huggingface模型地址; +tensor-parallel-size:张量并行副本数,TP建议和训练时infer的配置保持一致; +data-parallel-size:数据并行副本数,DP建议和训练时infer的配置保持一致,默认为1; +port:可任意设置空闲端口; + +.. code-block:: bash + + cd /path/to/vllm + vllm serve /path/to/Qwen3-30B/ \ + --served-model-name auto \ + --gpu-memory-utilization 0.9 \ + --max-num-seqs 24 \ + --max-model-len 10240 \ + --max-num-batched-tokens 10240 \ + --enforce-eager \ + --trust-remote-code \ + --distributed_executor_backend=mp \ + --tensor-parallel-size 4 \ + --data-parallel-size 1 \ + --generation-config vllm \ + --port 6380 + + +**启动vllm_client评测** + +.. code-block:: bash + + cd /examples/benchmark + ais_bench --models vllm_api_general --datasets aime2024_gen + + +**评测结果** + +经过训练,模型在aime2024上的评分显著上升 + ++------+----------+---------+----------+------+-----------------------+ +| iter | dataset | version | metric | mode | vllm-api-stream-chat | ++======+==========+=========+==========+======+=======================+ +| 0 | aime2024 | a4b6f0 | accuracy | gen | 85.4 | ++------+----------+---------+----------+------+-----------------------+ +| 150 | aime2024 | a4b6f0 | accuracy | gen | 91.2 | ++------+----------+---------+----------+------+-----------------------+ + +性能采集 +----------------------------------- +关于NPU profiling的详细文档请参考 `ascend_profiling_zh <../../dev_guide/performance/ascend_profiling_zh.rst>`_ + +采集完成后,开发者可以使用 `MindStudio Insight `_ 进行数据解析 + +注: verl框架侧进行采集全量 Profiling 产生海量且重复的算子记录,可以根据文档修改代码仅采集关键阶段 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/dapo_multi_model_optimization_practice.md b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/dapo_multi_model_optimization_practice.md new file mode 100644 index 0000000000000000000000000000000000000000..89d9f732c562760a34a05aa383707010a5823564 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/dapo_multi_model_optimization_practice.md @@ -0,0 +1,360 @@ +# DAPO multi model optimization practice + +## DAPO 介绍 + +Last updated: 07/03/2026. + +DAPO的论文可以参考:[DAPO](https://arxiv.org/pdf/2503.14476),其中包含以下几个关键技术。 + +* **Clip-Higher**: 通过对重要性采样比的上限剪裁促进了系统的多样性并避免了熵坍缩(Entropy Collapse)。 +* **Dynamic Sampling**: 提高了训练效率和稳定性。DAPO提出了一种执行动态采样的策略,并过滤掉准确率等于1和0的提示组,从而保持批次间具有有效梯度的提示数量一致。 +* **Token-level Policy Gradient Loss**: 在长链思维强化学习 (long-CoT RL) 场景中至关重要。 +* **Overlong Reward Shaping**: 减少奖励噪声并稳定了训练。 + +在verl中,可以进行如下设置,从而进行DAPO算法的运行。 + +- **奖励模型的管理策略为 DAPO** + 在dapo算法中,必须配置成dapo。 + +```bash +reward_model.reward_manager.name=dapo +``` + +- **Clip-Higher 更高裁剪** + `clip_ratio_low` 和 `clip_ratio_high` 用于指定 DAPO 目标函数中的 $\varepsilon_{\text {low }}$ 和 $\varepsilon_{\text {high }}$。 + +```bash +clip_ratio_low=0.2 # 裁剪比例下限,默认值为0.2 +clip_ratio_high=0.28 # 裁剪比例上限,默认值为0.28 +``` + +- **动态采样的相关配置** + 将 `filter_groups.enable` 设置为 `True` 会过滤掉输出 `metric` 完全相同的组,例如对于 `acc` 指标,过滤掉输出准确率全部为 1 或 0 的组。 + 训练器会使用 `gen_batch_size` 进行重复采样,直到生成足够数量的符合条件的组,或者达到 `max_num_gen_batches` 所指定的上限为止。 + +```bash +data.gen_batch_size=${gen_prompt_bsz} +algorithm.filter_groups.enable=${enable_filter_groups} # 动态采样开关 +algorithm.filter_groups.metric=${filter_groups_metric} # 使用准确率作为过滤标准 +algorithm.filter_groups.max_num_gen_batches=${max_num_gen_batches} # 最大生成批次数量,最多重复生成数据的次数 +``` + +- **Token-level Loss** + 将 `loss_agg_mode` 设置为 `token-mean` 意味着计算一个批次中所有序列内所有 token 的(策略梯度)损失的平均值。 + +```bash +actor_rollout_ref.actor.loss_agg_mode=${loss_agg_mode} +# 注意:“token-mean”是默认行为。 +``` + +- **奖励模型对超长回答的惩罚配置** + 将 `overlong_buffer.enable` 设置为 `True` 将对输出长度过长但仍未超过硬上下文限制的输出进行惩罚。具体来说,当输出的长度超过 `max_response_length - overlong_buffer.len` 且超出 `0` 到 `overlong_buffer.len` 个 token 时,惩罚值会从 `0` 线性增加到 `overlong_buffer.penalty_factor`。 + +```bash +reward_model.overlong_buffer.enable=${enable_overlong_buffer} # 启用超长缓冲区惩罚,开启对超长输出的惩罚机制 +reward_model.overlong_buffer.len=${overlong_buffer_len} # 缓冲区长度,定义缓冲区的token,最大惩罚强度 +reward_model.overlong_buffer.penalty_factor=${overlong_penalty_factor} #惩罚因子,最大惩罚强度 +``` + +相关参数涉及的代码可以参考:[Recipe: Decoupled Clip and Dynamic Sampling Policy Optimization (DAPO)](https://github.com/verl-project/verl-recipe/blob/main/dapo/README.md) + +## 硬件要求 + + +当前支持Atlas 800T A3 与 Atlas 900 A3 SuperPoD。完成跑完本次最佳实践需要 1 台 Atlas 900 A3 SuperPoD。关键软件版本可以参考:[Ascend Quickstart](https://github.com/verl-project/verl/blob/main/docs/ascend_tutorial/get_start/quick_start.rst) + + +## 安装基础环境 + +| software | version | +| ------------- | ---------------------------------------------------------- | +| Python | 3.11 | +| CANN | ==9.0.0.B160 (CANN900B160) | +| torch | ==2.9.0 | +| torch_npu | ==2.9.0 | +| triton_ascend | ==3.2.1 | +| verl | main | +| vllm | v0.18.0 | +| vllm-ascend | v0.18.0 | +| transformers | 5.3.0 | + +在本实践中,我们通过指定 verl 的 commit id 以避免引入其他问题。注意:main 分支可能会因迭代重构导致 patch 出问题,如需稳定版本可切换至 `release/v0.8.0`。 + +```bash +cd verl +git checkout main +# 指定相应的recipe版本 +git submodule update --init --recursive recipe +cd recipe +git checkout main +``` + +## 模型训练 + +### 数据集准备 + +Geometry3k 数据集是由加利福尼亚大学洛杉矶分校与浙江大学联合研发的几何领域专用数据集,核心面向视觉问答(VQA)任务展开研究与模型训练。该数据集总计包含 3002 个样本,采用图像和文本两种模态数据形式构建,其中文本模态涵盖各类几何问题描述,图像则以可视化图表呈现问题中的几何图形信息,包括三角形、圆形、四边形等基础几何形状,以及不同图形间的位置、嵌套、相交等关联关系。可以从Hugging Face库下载对应的原始数据集:[Geometry3k ](https://huggingface.co/datasets/hiyouga/geometry3k) + +```shell +# 下载原始数据并预处理 +python ./examples/data_preprocess/geo3k.py --local_dir=./data/geo3k +``` + +### 权重下载 + +从Hugging Face库下载对应的模型权重:[Qwen3-VL-30B-A3B-Instruct](https://huggingface.co/Qwen/Qwen3-VL-30B-A3B-Instruct/tree/main) + +### jemalloc安装 + +为了确保 Ray 进程能够正常回收内存,需要安装并使能 jemalloc 库进行内存管理。 + +#### Ubuntu 操作系统 + +通过操作系统源安装jemalloc(注意: 要求ubuntu版本>=20.04): + +```shell +sudo apt install libjemalloc2 +``` + +在启动任务前执行如下命令通过环境变量导入jemalloc,需先通过 **find /usr -name libjemalloc.so.2** 确认文件是否存在 : + +```shell +# arm64架构 +export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libjemalloc.so.2 +# x86_64架构 +export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 +``` + +#### OpenEuler 操作系统 + +执行如下命令通过操作系统源安装jemalloc + +```shell +yum install jemalloc +``` + +如果上述方法无法正常安装,可以通过源码编译安装。前往jemalloc官网下载最新稳定版本,官网地址:https://github.com/jemalloc/jemalloc/releases/ + +```shell +tar -xvf jemalloc-{version}.tar.bz2 +cd jemalloc-{version} +./configure --prefix=/usr/local +make +make install +``` + +### 全局变量导入 + +- 为了确保 Ray 进程能够正常回收内存,需要安装并使能 jemalloc 库进行内存管理,用于更好地管理内存,避免长跑过程中内存 OOM。 + +```bash +# 根据实际安装路径设置 jemalloc 环境变量,例如安装路径为:/usr/local/lib/libjemalloc.so.2(可通过 find /usr -name libjemalloc.so.2 确认文件是否存在) +export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 +``` + +- 某些模型是通过 vllm ascend 进行优化的。但在某些情况下,优化后的模型可能并不适用。此时,将此值设置为 0 即可禁用优化后的模型。 + +```bash +export USE_OPTIMIZED_MODEL=0 +``` + +- 启用vLLM V1 + +```bash +export VLLM_USE_V1=1 +``` + +- 昇腾多卡通信的兜底配置,延长连接超时时间,避免集群环境下训练启动因连接慢而失败 + +```bash +export HCCL_CONNECT_TIMEOUT=5400 +``` + +- 控制 vLLM 在昇腾芯片上是否启用NZ优化 + +```bash +export VLLM_ASCEND_ENABLE_NZ=0 +``` + +### 训练 +```bash +# Model Weights Paths +MODEL_PATH=hf_weights/Qwen3-VL-30B-A3B-Instruct +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + +# File System Paths +TRAIN_FILE=$RAY_DATA_HOME/datasets/geo3k/train.parquet +TEST_FILE=$RAY_DATA_HOME/datasets/geo3k/test.parquet + +# 保存频率,-1默认不保存,如需评测请修改此参数 +trainer.save_freq=-1 +``` + +- 对于单机任务 Qwen3-VL-30B ,可以使用以下脚本将训练拉起。 + +```bash +pkill -9 python +ray stop --force +rm -rf /tmp/ray +export VLLM_USE_V1=1 +export HCCL_CONNECT_TIMEOUT=5400 +export VLLM_ASCEND_ENABLE_NZ=0 +export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 +# Some models are optimized by vllm ascend. While in some case, e.g. rlhf training, +# the optimized model may not be suitable. In this case, set this value to 0 to disable the optimized model. +export USE_OPTIMIZED_MODEL=0 +export CPU_AFFINITY_CONF=2 +export HCCL_OP_EXPANSION_MODE="AIV" +export VLLM_VERSION="0.18.0" + +# 修改为对应主节点IP +MASTER_ADDR="IP FOR MASTER NODE" +# 每个节点中 NPU 数 +NPUS_PER_NODE=16 +ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + +bash recipe/dapo/run_dapo_qwen3_vl_30b_fsdp2_npu.sh +``` +- 对于多节点任务 Qwen3-VL-30B ,我们推荐使用以下脚本进行大规模多节点训练拉起,根据实际需要修改`NNODES`与`NPUS_PER_NODE` ,并修改配置脚本中参数`trainer.nnodes`和`trainer.n_gpus_per_node`与之相对应。 + +```bash +pkill -9 python +ray stop --force +rm -rf /tmp/ray +export VLLM_USE_V1=1 +export HCCL_CONNECT_TIMEOUT=5400 +export VLLM_ASCEND_ENABLE_NZ=0 +export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 +# Some models are optimized by vllm ascend. While in some case, e.g. rlhf training, +# the optimized model may not be suitable. In this case, set this value to 0 to disable the optimized model. +export USE_OPTIMIZED_MODEL=0 +export CPU_AFFINITY_CONF=2 +export HCCL_OP_EXPANSION_MODE="AIV" +export VLLM_VERSION="0.18.0" + +# 修改为当前需要跑的用例路径 +DEFAULT_SH="./run_*.sh" +echo "Use $DEFAULT_SH" + +ulimit -n 32768 +mkdir logs + +# 集群中计算节点数 +NNODES=2 +# 每个节点中 NPU 数 +NPUS_PER_NODE=8 +# 修改为对应主节点IP +MASTER_ADDR="IP FOR MASTER NODE" +# 修改为当前节点的通信网卡 +SOCKET_IFNAME="Your SOCKET IFNAME" +export HCCL_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" +export GLOO_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" +# 获取当前IP +CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then + # 主节点启动 + ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + + while true; do + ray_status_output=$(ray status) + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') + device_count=$((npu_count_int / $NPUS_PER_NODE)) + + # 判断device_count 是否与 NNODES 相等 + if [ "$device_count" -eq "$NNODES" ]; then + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." + ray status + bash $DEFAULT_SH + break + else + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" + sleep 5 + fi + done +else + # 子节点尝试往主节点注册 ray 直到成功 + while true; do + # 尝试连接 ray 集群 + ray start --address="$MASTER_ADDR:6766" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP + + # 检查连接是否成功 + ray status + if [ $? -eq 0 ]; then + echo "Successfully connected to the Ray cluster!" + break + else + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." + sleep 5 + fi + done +fi + +sleep 600 +``` +DEFAULT_SH: 修改为训练所用配置 sh 文件路径。在此案例中修改为 [Qwen3_VL_30B](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_vl_30b_fsdp2_npu.sh) 路径。 + +NNODES 和 NPUS_PER_NODE: 修改为使用节点数量和每个节点 NPU 数量。在此案例中分别为2和8。 + +MASTER_ADDR:修改为对应主节点 IP。即所有节点的 MASTER_ADDR 应该相同。 + +SOCKET_IFNAME, HCCL_SOCKET_IFNAME, GLOO_SOCKET_IFNAME: 修改为对应通信网卡,通信网卡可以通过以下命令获取: + +```bash +ifconfig |grep "$(hostname -I |awk '{print $1}'|awk -F '.' '{print $0}')" -B 1|awk -F ':' '{print$1}' | head -1 | tail -1 +``` + +## 优化参考 + +- **启动动态批次大小** + 根据单 GPU 的最大 Token 总数(ppo_max_token_len_per_gpu)动态调整批次大小 + +```bash +actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} +actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} +actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} +``` + +- **单个 GPU 能处理的最大 Token 总数** + 当`use_dynamic_bsz=True`时,单 GPU 在一个微批次中能处理的最大 Token 数量 + +```bash +actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} +actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} +actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} +``` + +- **单个 GPU 微批次大小** + 当`use_dynamic_bsz=True`时,框架会以该值为初始批次大小,再根据`ppo_max_token_len_per_gpu`向上 / 向下调整 + +```bash +actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=2 +actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=2 +actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=2 +``` + +- **启用 FSDP2 框架** + “将模型参数、梯度、优化器状态分片存储在不同 GPU 上”,避免单卡加载全量模型导致显存溢出。 + +```bash +# 启用 FSDP2 框架 +actor_rollout_ref.actor.strategy=fsdp2 +actor_rollout_ref.ref.strategy=fsdp2 +critic.strategy=fsdp2 + +# 仅用于 FSDP2:前向传播后重新分片以减少内存占用。 +actor_rollout_ref.actor.fsdp_config.reshard_after_forward=True +# 仅用于 FSDP2:是否在模型前向传播后重新分片以节省内存。 +actor_rollout_ref.ref.fsdp_config.reshard_after_forward=True +``` + +- **启用专家并行配置** + 指定有多少个 GPU用于并行计算不同的专家网络。 + +```bash +# MoE 架构 Actor 模型的专家并行配置 +actor_rollout_ref.rollout.expert_parallel_size=8 +``` + + diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/gspo_optimization_practice.md b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/gspo_optimization_practice.md new file mode 100644 index 0000000000000000000000000000000000000000..c5518382bea43c51198cca562928497588400684 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/gspo_optimization_practice.md @@ -0,0 +1,414 @@ +# NPU Qwen3-32B GSPO Optimization Practice + +Last updated: 07/03/2026. + +为保障使用体验,请切换 verl 至 main 分支,commit id 为 9d05508f5e3bd8ecb70cf94ab10dc087b57a716d。注意:main 分支可能会因迭代重构导致 patch 出问题,如需稳定版本可切换至 `release/v0.8.0`。 + +本实践对应的训练脚本:[run_qwen3_32b_fsdp.sh](../../../../examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh)(`examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh`)。 + +## 算法适配 + +GSPO通过将优化颗粒度从**token级**提升到**sequence级**,规避了GRPO会遇到的**方差急剧增大**导致训练不稳定的情况,增加了训练的稳定性,同时该算法也在一定程度上提升了算法的收敛速度。 + +想要成功在verl仓库中调用到GSPO算法,需要进行如下的必要配置 + +```bash +# 核心算法配置 +algorithm.adv_estimator=grpo \ # 使用GRPO优势估计器 +algorithm.use_kl_in_reward=False \ # 不在奖励中添加KL惩罚 +# GSPO策略损失模式 +actor_rollout_ref.actor.policy_loss.loss_mode=gspo \ # 启用GSPO策略损失 +# 极小裁剪范围(GSPO特色) +actor_rollout_ref.actor.clip_ratio_low=0.0003 \ # 裁剪下界,论文推荐值 +actor_rollout_ref.actor.clip_ratio_high=0.0004 \ # 裁剪上界,论文推荐值 +# KL配置(GSPO不使用KL loss) +actor_rollout_ref.actor.use_kl_loss=False \ # 禁用KL损失 +actor_rollout_ref.actor.kl_loss_coef=0.0 \ # KL损失系数设为0 +# 序列级损失聚合模式(GSPO核心) +actor_rollout_ref.actor.loss_agg_mode=seq-mean-token-mean \ # 序列级平均,GSPO论文推荐 +# 批次配置 +actor_rollout_ref.rollout.n=16 \ # 每个prompt生成16个响应(组采样) +``` + +一般选择入口函数为 `verl.trainer.main_ppo`。完整可运行示例见 [run_qwen3_32b_fsdp.sh](../../../../examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh)。 + +## 基础环境 + +当前支持Atlas 800T A3 与 Atlas 900 A3 SuperPoD。完成本次最佳实践需要 4台Atlas 800T A3。 + +### 安装基础环境 + +| software | version | +| ------------- | ---------------------------------------------------------- | +| Python | 3.11 | +| CANN | ==9.0.0.B160 (CANN900B160) | +| torch | ==2.9.0 | +| torch_npu | ==2.9.0 | +| triton_ascend | ==3.2.1 | +| verl | main | +| vllm | v0.18.0 | +| vllm-ascend | v0.18.0 | +| transformers | 5.3.0 | + + +```bash +cd verl +git checkout main +# 指定相应的recipe版本 +git submodule update --init --recursive recipe +``` + +### 权重获取 + +从Hugging Face库下载对应的模型权重:[Qwen/Qwen3-32B · Hugging Face](https://huggingface.co/Qwen/Qwen3-32B) + +### 数据集准备 + +```bash +# 下载math-17k数据集 +git clone https://huggingface.co/datasets/BytedTsinghua-SIA/DAPO-Math-17k + +# 下载AIME_2024测试数据集 +git clone https://huggingface.co/datasets/Maxwell-Jia/AIME_2024 +``` + +### jemalloc安装 + +为了确保 Ray 进程能够正常回收内存,需要安装并使能 jemalloc 库进行内存管理。 + +#### Ubuntu 操作系统 + +通过操作系统源安装jemalloc(注意:要求ubuntu版本>=20.04): + +```shell +sudo apt install libjemalloc2 +``` + +在启动任务前执行如下命令通过环境变量导入jemalloc,需先通过 **find /usr -name libjemalloc.so.2** 确认文件是否存在 : + +```shell +# arm64架构 +export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libjemalloc.so.2 +# x86_64架构 +export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 +``` + +#### OpenEuler 操作系统 + +执行如下命令通过操作系统源安装jemalloc + +```shell +yum install jemalloc +``` + +如果上述方法无法正常安装,可以通过源码编译安装。前往jemalloc官网下载最新稳定版本,官网地址:https://github.com/jemalloc/jemalloc/releases/ + +```shell +tar -xvf jemalloc-{version}.tar.bz2 +cd jemalloc-{version} +./configure --prefix=/usr/local +make +make install +``` + +在启动任务前执行如下命令通过环境变量导入jemalloc: + +```shell +#根据实际安装路径设置环境变量,例如安装路径为:/usr/local/lib/libjemalloc.so.2,可通过以下命令来设置环境变量(可通过 find /usr -name libjemalloc.so.2 确认文件是否存在) +export LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libjemalloc.so.2 +``` + +### 多机任务拉起 + +针对本实践提供的多机任务,可用下面的脚本拉起 + +```bash +pkill -9 python +ray stop --force +rm -rf /tmp/ray + +export RAY_DEDUP_LOGS=0 +export HYDRA_FULL_ERROR=1 +export TASK_QUEUE_ENABLE=1 +export HCCL_EXEC_TIMEOUT=3600 +export HCCL_CONNECT_TIMEOUT=3600 +export HCCL_ASYNC_ERROR_HANDLING=0 +export CPU_AFFINITY_CONF=1 +export VLLM_USE_V1=1 +export VLLM_ATTENTION_BACKEND=XFORMERS +export VLLM_ASCEND_ENABLE_FLASHCOMM=1 +export VLLM_ASCEND_ENABLE_PREFETCH_MLP=1 +export VLLM_ASCEND_ENABLE_DENSE_OPTIMIZE=1 +export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 + +# 修改为当前需要跑的用例路径 +DEFAULT_SH="./run_*.sh" +echo "Use $DEFAULT_SH" + +ulimit -n 32768 +mkdir logs + +NNODES=4 +NPUS_PER_NODE=16 +# 修改为对应主节点IP +MASTER_ADDR="IP FOR MASTER NODE" +# 修改为当前节点的通信网卡 +SOCKET_IFNAME="Your SOCKET IFNAME" +export HCCL_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" +export GLOO_SOCKET_IFNAME="SOCKET IFNAME FOR CURRENT NODE" +# 获取当前IP +CURRENT_IP=$(ifconfig $SOCKET_IFNAME | grep -Eo 'inet (addr:)?([0-9]{1,3}\.){3}[0-9]{1,3}' | awk '{print $NF}') +if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then + # 主节点启动 + ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + + while true; do + ray_status_output=$(ray status) + npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1) + npu_count_int=$(echo "$npu_count" | awk '{print int($1)}') + device_count=$((npu_count_int / $NPUS_PER_NODE)) + + # 判断device_count 是否与 NNODES 相等 + if [ "$device_count" -eq "$NNODES" ]; then + echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script." + ray status + bash $DEFAULT_SH + break + else + echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count" + sleep 5 + fi + done +else + # 子节点尝试往主节点注册 ray 直到成功 + while true; do + # 尝试连接 ray 集群 + ray start --address="$MASTER_ADDR:6766" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP + + # 检查连接是否成功 + ray status + if [ $? -eq 0 ]; then + echo "Successfully connected to the Ray cluster!" + break + else + echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..." + sleep 5 + fi + done +fi + +sleep 600 +``` + +DEFAULT_SH:修改为训练所用配置 sh 文件路径。在此案例中修改为 [run_qwen3_32b_fsdp.sh](../../../../examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh)(即 `examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh`)。 + +NNODES 和 NPUS_PER_NODE:修改为使用节点数量和每个节点 NPU 数量。在此案例中分别为4和16。 + +MASTER_ADDR:修改为对应主节点 IP。即所有节点的 MASTER_ADDR 应该相同。 + +SOCKET_IFNAME, HCCL_SOCKET_IFNAME, GLOO_SOCKET_IFNAME: 修改为对应通信网卡,通信网卡可以通过以下命令获取: + +```bash +ifconfig |grep "$(hostname -I |awk '{print $1}'|awk -F '.' '{print $0}')" -B 1|awk -F ':' '{print$1}' | head -1 | tail -1 +``` + +## 性能调优 + +优化从训练、推理、调度和其他四个方面入手。 + +### 训练 + +#### 动态bsz + +```bash +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length) / sp_size)) +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length) / sp_size)) +``` + +**这个优化点主要调整上面这两个参数,不过需要注意这两个参数调整得太大会导致OOM** + +**主要调整** `actor_ppo_max_token_len`,调大了会降低训练的耗时,调整 `infer_ppo_max_token_len`没有明显的收益,可以不动 + +**这两个参数的作用介绍如下:** + +**这两个参数用于控制动态批处理(dynamic batch size)模式下每个GPU处理的最大token数量** + +- **`actor_ppo_max_token_len`**: Actor模型在PPO更新(前向+反向传播)时每个GPU能处理的最大token数 +- **`infer_ppo_max_token_len`**: 推理阶段(Reference policy和Rollout)计算log概率时每个GPU能处理的最大token数 + +### 推理 + +#### ACLgraph+FULL_DECODE_ONLY + +推理算子下发方面的优化,平均能有 `15%~20%`左右的性能收益。 + +先看单开**ACLgraph**,如下: + +```bash +# 开启ACLgraph+FULL_DECODE_ONLY(注意:当设置此参数为False时,TASK_QUEUE_ENABLE必须设置为1,不然会报错) +actor_rollout_ref.rollout.enforce_eager=False \ +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_capture_sizes='[8,16,32,64,128]' \ +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_mode='FULL_DECODE_ONLY' +``` + +`FULL_DECODE_ONLY`开启成功后有如下输出: + +![FULL_DECODE_ONLY result](https://github.com/wucong25/verl-data/blob/main/ascend_acl_graph.png) + +**`cudagraph_capture_sizes`参数设置指南** + +cudagraph_capture_sizes设置的值对应的是批大小,这里的批大小不是配置里的DP域对应的那个批次大小,这里是相较于vllm来说的批大小,单位为**token** + +默认生成的算法如下,可做参考 + +![cudagraph_capture_sizes](https://github.com/wucong25/verl-data/blob/main/ascend_set_cudagraph_sizes.png) + +##### 推理后端切换 + +使用方式:`export VLLM_ATTENTION_BACKEND=XFORMERS` + +![VLLM_ATTENTION_BACKEND](https://github.com/wucong25/verl-data/blob/main/ascend_vllm_attn_backend.png) + +注:需要注意某些后端在一些比较老的vllm-ascend版本内并不支持 + +##### 使能vllm v1版本 + +使用方式:`export VLLM_USE_V1=1` + +可以常开,一般都是正收益。 + +### 调度 + +#### AIV + +打开方式:设置 `export HCCL_OP_EXPANSION_MODE="AIV"` + +HCCL_OP_EXPANSION_MODE环境变量用于配置通信算法的编排展开位置,支持如下取值: + +- AI_CPU:代表通信算法的编排展开位置在Device侧的AI CPU计算单元。 +- AIV:代表通信算法的编排展开位置在Device侧的Vector Core计算单元。 +- HOST:代表通信算法的编排展开位置为Host侧CPU,Device侧根据硬件型号自动选择相应的调度器。 +- HOST_TS:代表通信算法的编排展开位置为Host侧CPU,Host向Device的Task Scheduler下发任务,Device的Task Scheduler进行任务调度执行。 + +下面介绍两种展开机制 + +##### HOST展开 + +image-20260113194257095 + +- 软件栈工作在host CPU,通信算法展开一个个task +- 每个task调用runtime接口,下发到device的rtsqueue +- STARS从rtsqueue上顺序拿取task +- 根据task类型分别调用SDMA和RDMA引擎。 + **单算子瓶颈**:hostbound 每个task提交是2~5us,一个通信算子有几百个task,单算子场景不会在device上缓存,下发一个执行一个 + +##### AICPU机制展开 + +image-20260113194333218 + +- host侧不下发一个个task,把通信算子作为一个个kernel,放在通信算子kernel的队列上去。 +- STARS调度kernel队列流上的kernel,把kernel放到AiCPU上去执行。 +- AICPU调用函数(kernel),用一个线程执行kernel 函数,在函数内把通信task展开,把task放到rtsqueue上,STARS调用。 +- 降低host和aicpu交互,由几百次降低为一次。 +- task的提交在AICPU上提交,做了提交的部分合并。 + +#### TASK_QUEUE_ENABLE + +**使用方式:**`export TASK_QUEUE_ENABLE=2` + +TASK_QUEUE_ENABLE,下发优化,图模式设置为1(即开启图模式的时候这个要设置为1),非图模式设置为2 + +示意图: + +![ascend task queue](https://github.com/wucong25/verl-data/blob/main/ascend_task_queue2.png) + +##### 绑核优化 + +**使用方式:**`export CPU_AFFINITY_CONF=1` + +详细设置原理可看:https://www.hiascend.com/document/detail/zh/Pytorch/600/ptmoddevg/trainingmigrguide/performance_tuning_0059.html + +### 其他 + +以下内容汇总了若干全局环境变量的调优配置。由于这些参数在训练阶段与推理阶段往往都能带来正向收益,且目前尚缺乏足够精细的消融实验来严格区分它们各自对训练或推理的贡献占比,故统一归拢在此,供后续持续监控与进一步拆解分析。 + +#### 使能jemalloc + +使用方式(注意需要先安装jemalloc库):`export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2` + +**安装使用教程:**[MindSpeed-RL/docs/install_guide.md · Ascend/MindSpeed-RL - AtomGit | GitCode](https://gitcode.com/Ascend/MindSpeed-RL/blob/master/docs/install_guide.md#高性能内存库-jemalloc-安装) + +#### 多流复用 + +内存方面有优化 + +使能方式:`export MULTI_STREAM_MEMORY_REUSE=1` + +原理介绍:https://www.hiascend.com/document/detail/zh/Pytorch/600/ptmoddevg/trainingmigrguide/performance_tuning_0040.html + +#### VLLM_ASCEND_ENABLE_FLASHCOMM + +使用方式:`export VLLM_ASCEND_ENABLE_FLASHCOMM=1` + +启用昇腾 NPU 特有的FLASHCOMM高速通信优化技术 + +地址:https://vllm-ascend.readthedocs.io/zh-cn/latest/user_guide/release_notes.html + +#### VLLM_ASCEND_ENABLE_DENSE_OPTIMIZE + +使用方式:`export VLLM_ASCEND_ENABLE_DENSE_OPTIMIZE=1` + +启用昇腾 NPU针对大模型推理的稠密计算优化 + +地址:https://vllm-ascend.readthedocs.io/zh-cn/latest/user_guide/release_notes.html + +#### VLLM_ASCEND_ENABLE_PREFETCH_MLP + +使用方式:`export VLLM_ASCEND_ENABLE_PREFETCH_MLP=1` + +启用 MLP 层的权重预取机制 + +image-20251124173132677 + +### verl框架参数设置 + +以下是内存方面的一些设置开关(注意,这个里面的优化都或多或少会导致吞吐量有一定程度的劣化) + +```bash +# 梯度检查点 (Gradient Checkpointing) +# 作用: 通过重新计算激活值来节省显存,以计算换内存。在前向传播时不保存中间激活值,反向传播时重新计算,可以显著降低显存占用,允许使用更大的batch size。 +actor_rollout_ref.model.enable_gradient_checkpointing=True \ + +# 参数卸载 (Parameter Offload) +# 作用: 将模型参数卸载到CPU内存,训练时再加载回GPU。 +actor_rollout_ref.actor.fsdp_config.param_offload=True \ +actor_rollout_ref.ref.fsdp_config.param_offload=True \ + +# 优化器状态卸载 (Optimizer Offload) +# 作用: 将优化器状态(如Adam的动量)卸载到CPU。优化器状态通常占用大量显存(对于Adam,每个参数需要额外8字节),卸载可以节省显存。 +actor_rollout_ref.actor.fsdp_config.optimizer_offload=True \ + +# 释放推理引擎缓存 (Free Cache Engine) +# 作用: 在训练阶段释放推理引擎的KV cache和权重。这是3D-HybridEngine的核心优化,允许在同一GPU上交替进行推理和训练,显著降低显存需求。 +actor_rollout_ref.rollout.free_cache_engine=True \ + +# 熵计算优化 +# entropy_checkpointing: 在训练时对熵计算启用重计算,降低显存峰值 +# entropy_from_logits_with_chunking: 分块处理logits张量(如2048 tokens一组),避免一次性加载整个[bsz*seq_len, vocab]张量 +actor_rollout_ref.actor.entropy_checkpointing=True \ +actor_rollout_ref.ref.entropy_checkpointing=True \ +actor_rollout_ref.actor.entropy_from_logits_with_chunking=True \ +actor_rollout_ref.ref.entropy_from_logits_with_chunking=True \ + +# 推理引擎显存配置 +# gpu_memory_utilization: 控制vLLM使用的GPU显存比例(0.90 = 90%) +# enforce_eager=False: 启用CUDA graphs加速推理,但会占用额外显存 +actor_rollout_ref.rollout.gpu_memory_utilization=0.90 \ +actor_rollout_ref.rollout.enforce_eager=False \ +``` + +## NPU调优参考文章 + +环境变量相关:[环境变量列表-Ascend Extension for PyTorch6.0.0-昇腾社区](https://www.hiascend.com/document/detail/zh/Pytorch/600/apiref/Envvariables/Envir_001.html) + +社区性能调优教程:[性能调优流程-Ascend Extension for PyTorch6.0.0-昇腾社区](https://www.hiascend.com/document/detail/zh/Pytorch/600/ptmoddevg/trainingmigrguide/performance_tuning_0001.html) diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/qwen3_5_megatron_npu.md b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/qwen3_5_megatron_npu.md new file mode 100644 index 0000000000000000000000000000000000000000..a11a317422aeaa903105d5366821bfea9b912088 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/examples/qwen3_5_megatron_npu.md @@ -0,0 +1,148 @@ +# Qwen3.5 Megatron NPU 使用指南 + +Last updated: 09/07/2026. + +本文用于指导在 Ascend NPU 上使用 verl + Megatron + vLLM 跑通 Qwen3.5-35B-A3B 和 Qwen3.5-122B-A10B GRPO 示例。 + +## 版本要求 + +| software | version | +| --- |---------------------------------------------------------------| +| Docker image | `quay.io/ascend/verl:v0.8.0-cann9.0.0-torch2.9.0post2-a3-ubuntu22.04-py3.11-vllm` | +| verl | 0.8.0 | +| Python | 3.11 | +| CANN | 9.0.0 | +| Megatron-LM | 0.16.0 | +| MindSpeed | 0.16.0 | +| Megatron-Bridge | `de93536e` | + +建议直接使用上表中的镜像: + +```bash +docker pull quay.io/ascend/verl:v0.8.0-cann9.0.0-torch2.9.0post2-a3-ubuntu22.04-py3.11-vllm +``` + +## 模型和脚本 + +| model | HF model | script | +|-------------------| --- | --- | +| Qwen3.5-35B-A3B | `Qwen/Qwen3.5-35B-A3B` | `examples/grpo_trainer/run_qwen3_5_35b_megatron.sh` | +| Qwen3.5-122B-A10B | `Qwen/Qwen3.5-122B-A10B` | `examples/grpo_trainer/run_qwen3_5_122b_a10b_megatron.sh` | +| Qwen3.5-397B-A17B | `Qwen/Qwen3.5-397B-A17B` | `examples/grpo_trainer/run_qwen3_5_397b_megatron.sh` | + +## 硬件和并行配置 + +示例脚本默认使用如下 NPU 配置,可以通过同名环境变量覆盖: + +| model | nnodes | devices per node | TP | PP | CP | EP | ETP | GEN_DP | GEN_TP | GEN_EP | +| --- |--------| --- | --- | --- | --- |----| --- |---|----|----| +| Qwen3.5-35B-A3B | 1 | 16 | 2 | 2 | 1 | 8 | 1 | 1 | 8 | 1 | +| Qwen3.5-122B-A10B | 4 | 16 | 2 | 4 | 1 | 16 | 1 | 1 | 16 | 1 | +| Qwen3.5-397B-A17B | 16 | 16 | 2 | 4 | 1 | 64 | 1 | 16 | 16 | 256 | + +## 数据和模型准备 + +脚本默认使用 Geo3K 数据集,并会下载到 `$HOME/data/geo3k`: + +```bash +hf download tyzhu/geo3k --repo-type dataset --local-dir $HOME/data/geo3k +``` + +模型权重可以使用 Hugging Face 模型名,也可以提前下载到本地路径: + +```bash +hf download Qwen/Qwen3.5-35B-A3B --local-dir /path/to/Qwen3.5-35B-A3B +hf download Qwen/Qwen3.5-122B-A10B --local-dir /path/to/Qwen3.5-122B-A10B +hf download Qwen/Qwen3.5-397B-A17B --local-dir /path/to/Qwen3.5-397B-A17B +``` + +## 启动训练 + +训练前需要先启动 Ray 集群。通用多节点说明可参考 [Multinode Training](../../../start/multinode.rst),Ascend 多节点脚本示例可参考 [Ascend SGLang Best Practices](ascend_sglang_best_practices.rst)。 + +最小启动方式如下,单机任务只需要执行 head 节点命令;多机任务需要在其他节点执行 worker 节点命令。`MASTER_ADDR` 在所有节点上保持一致,`CURRENT_IP` 设置为当前节点 IP。 + +```bash +MASTER_ADDR= +CURRENT_IP= +NPUS_PER_NODE=16 + +# head node +ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}' + +# worker nodes, only needed for multi-node jobs +ray start --address="$MASTER_ADDR:6766" --node-ip-address=$CURRENT_IP --resources='{"NPU": '$NPUS_PER_NODE'}' + +ray status +``` + +通过 `ray status` 确认 NPU 资源数量符合预期后,在主节点执行训练脚本。Qwen3.5-35B-A3B 默认需要 16 个 NPU 资源,Qwen3.5-122B-A10B 默认需要 64 个 NPU 资源。 + +### Qwen3.5-35B-A3B + +```bash +export DEVICE=npu +export HF_MODEL_PATH=/path/to/Qwen3.5-35B-A3B + +bash examples/grpo_trainer/run_qwen3_5_35b_megatron.sh +``` + +如果需要覆盖数据路径: + +```bash +DEVICE=npu \ +HF_MODEL_PATH=/path/to/Qwen3.5-35B-A3B \ +train_path=/path/to/train.parquet \ +test_path=/path/to/test.parquet \ +bash examples/grpo_trainer/run_qwen3_5_35b_megatron.sh +``` + +### Qwen3.5-122B-A10B + +```bash +export DEVICE=npu +export HF_MODEL_PATH=/path/to/Qwen3.5-122B-A10B + +bash examples/grpo_trainer/run_qwen3_5_122b_a10b_megatron.sh +``` + +如果需要覆盖数据、保存路径或并行配置: + +```bash +DEVICE=npu \ +HF_MODEL_PATH=/path/to/Qwen3.5-122B-A10B \ +train_files=/path/to/train.parquet \ +test_files=/path/to/test.parquet \ +save_path=/path/to/checkpoints \ +NDEVICES_PER_NODE=16 \ +nnodes=4 \ +bash examples/grpo_trainer/run_qwen3_5_122b_a10b_megatron.sh +``` + +### Qwen3.5-397B-A17B + +```bash +export DEVICE=npu +export HF_MODEL_PATH=/path/to/Qwen3.5-397B-A17B + +bash examples/grpo_trainer/run_qwen3_5_397b_megatron.sh +``` + +如果需要覆盖数据、保存路径或并行配置: + +```bash +DEVICE=npu \ +HF_MODEL_PATH=/path/to/Qwen3.5-397B-A17B \ +train_files=/path/to/train.parquet \ +test_files=/path/to/test.parquet \ +save_path=/path/to/checkpoints \ +NDEVICES_PER_NODE=16 \ +nnodes=16 \ +bash examples/grpo_trainer/run_qwen3_5_397b_megatron.sh +``` + +## 注意事项 + +- 脚本会通过 `torch_npu` 自动识别 NPU 环境;如需手动指定,设置 `DEVICE=npu`。 +- Qwen3.5 的 Gated Delta Net 当前不使用 packed sequence,因此脚本中保持 `use_remove_padding=False` 和 `use_dynamic_bsz=False`。 +- NPU 分支会设置 `vanilla_mbridge=False`、`use_flash_attn=True`、`moe_token_dispatcher_type=alltoall` 等 Ascend 适配参数。 diff --git a/verl_0720_main/verl/docs/ascend_tutorial/model_support/model_and_algorithm_support.md b/verl_0720_main/verl/docs/ascend_tutorial/model_support/model_and_algorithm_support.md new file mode 100644 index 0000000000000000000000000000000000000000..7e00c14090b33e08ce6b90bd3b8299521c1a2e63 --- /dev/null +++ b/verl_0720_main/verl/docs/ascend_tutorial/model_support/model_and_algorithm_support.md @@ -0,0 +1,61 @@ +# NPU Model & Algorithms Support Status + +Last updated: 05/14/2026. + +## Table 1 RL Algorithms + +| No. | model | algorithm | download link | actor.strategy | rollout.name | shell location | Usage Guide | hardware | +|---|---------------------|------------|------|------------|-----------|------------------------------|------------|-----------------------------------| +| 1 | Qwen2.5-7B-Instruct | DAPO | [7B](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_7b_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 2 | Qwen2.5-VL-3B-Instruct | DAPO | [3B](https://huggingface.co/Qwen/Qwen2.5-VL-3B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_vl_3b_fsdp2_npu.sh) | - | Atlas 900 A2 PODc | +| 3 | Qwen2.5-VL-7B-Instruct | GRPO | [7B](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen2_5_vl_7b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 4 | Qwen2.5-VL-7B-Instruct | GRPO | [7B](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) | FSDP | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen2_5_vl_7b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 5 | Qwen2.5-VL-7B-Instruct | DAPO | [7B](https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_vl_7b_fsdp2_npu.sh) | - | Atlas 900 A2 PODc | +| 6 | Qwen2.5-32B | GRPO | [32B](https://huggingface.co/Qwen/Qwen2.5-32B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen2_5_32b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 7 | Qwen2.5-32B | DAPO | [32B](https://huggingface.co/Qwen/Qwen2.5-32B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_32b_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 8 | Qwen2.5-32B | DAPO | [32B](https://huggingface.co/Qwen/Qwen2.5-32B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_32b_fsdp2_4k_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 9 | Qwen2.5-32B | DAPO | [32B](https://huggingface.co/Qwen/Qwen2.5-32B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_32b_fsdp2_20k_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 10 | Qwen2.5-VL-32B-Instruct | DAPO | [32B](https://huggingface.co/Qwen/Qwen2.5-VL-32B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen2.5_vl_32b_fsdp2_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 11 | Qwen3-4B | GRPO | [4B](https://huggingface.co/Qwen/Qwen3-4B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_4b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 12 | Qwen3-8B | GRPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_8b_fsdp.sh) | - | Atlas 900 A2 PODc | +| 13 | Qwen3-8B | GRPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_8b_fsdp.sh) | - | Atlas 900 A2 PODc | +| 14 | Qwen3-8B | PPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/ppo_trainer/run_qwen3_8b_fsdp.sh) | - | Atlas 900 A2 PODc | +| 15 | Qwen3-8B | SAPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/sapo_trainer/run_qwen3_8b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 16 | Qwen3-8B | GSPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/gspo_trainer/run_qwen3_8b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 17 | Qwen3-8B | GRPO | [8B](https://huggingface.co/Qwen/Qwen3-8B) | MindSpeed_LLM | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_32b_mindspeed.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 18 | Qwen3-8B | GRPO(One_Step_Off_Policy) | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/verl/experimental/one_step_off_policy/shell/grpo_qwen3_8b_gsm8k_fsdp2_8_8_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 19 | Qwen3-8B-Base | DAPO | [8B](https://huggingface.co/Qwen/Qwen3-8B-Base) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_8b_base_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 20 | Qwen3-14B-Base | DAPO | [14B](https://huggingface.co/Qwen/Qwen3-14B-Base) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_14b_base_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 21 | Qwen3-30B | DAPO | [30B](https://huggingface.co/Qwen/Qwen3-30B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_30b_fsdp_6k_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 22 | Qwen3-30B-A3B-Base | DAPO | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B-Base) | FSDP | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_moe_30b_base_fsdp_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 23 | Qwen3-30B-A3B | GRPO | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B) | Megatron | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_megatron.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 24 | Qwen3-30B-A3B | GRPO | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B) | MindSpeed_LLM | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_mindspeed.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 25 | Qwen3-30B-A3B | DAPO | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B) | Megatron | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_moe_30b_megatron_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 26 | Qwen3-30B-A3B | GRPO (fully_async) | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/verl/experimental/fully_async_policy/shell/dapo_30b_a3b_math_fsdp_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 27 | Qwen3-32B | GRPO | [32B](https://huggingface.co/Qwen/Qwen3-32B) | FSDP | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_32b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 28 | Qwen3-32B | GRPO | [32B](https://huggingface.co/Qwen/Qwen3-32B) | MindSpeed_LLM | sglang | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_32b_mindspeed.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 29 | Qwen3-235B-A22B | GRPO | [235B](https://huggingface.co/Qwen/Qwen3-235B-A22B) | Megatron | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_235b_a22b_megatron.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 30 | Qwen3-235B-A22B | GRPO (fully_async) | [235B](https://huggingface.co/Qwen/Qwen3-235B-A22B) | Megatron | vllm | [`link`](https://github.com/verl-project/verl/blob/main/verl/experimental/fully_async_policy/shell/grpo_qwen3_235b_megatron_npu.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 31 | Qwen3-235B-A22B-Instruct | GRPO | [235B](https://huggingface.co/Qwen/Qwen3-235B-A22B-Instruct) | Megatron | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/ascend_extras/grpo_trainer/run_qwen3_235b_256k_megatron.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 32 | Qwen3-VL-8B-Instruct | GRPO | [8B](https://huggingface.co/Qwen/Qwen3-VL-8B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_vl_8b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 33 | Qwen3-VL-8B-Instruct | GRPO (fully_async) | [8B](https://huggingface.co/Qwen/Qwen3-VL-8B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/verl/experimental/fully_async_policy/shell/geo3k_qwen3vl_8b_fsdp2_16_16_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 34 | Qwen3-VL-30B-A3B-Instruct | GRPO | [30B](https://huggingface.co/Qwen/Qwen3-VL-30B-A3B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_vl_30b_a3b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 35 | Qwen3-VL-30B-A3B-Instruct | DAPO | [30B](https://huggingface.co/Qwen/Qwen3-VL-30B-A3B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl-recipe/blob/main/dapo/run_dapo_qwen3_vl_30b_fsdp2_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 36 | Qwen3-VL-30B-A3B-Instruct | GRPO (fully_async) | [30B](https://huggingface.co/Qwen/Qwen3-VL-30B-A3B-Instruct) | Megatron | vllm | [`link`](https://github.com/verl-project/verl/blob/main/verl/experimental/fully_async_policy/shell/geo3k_qwen3vl_30b_megatron_6_2_npu_async.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 37 | Qwen3.5-27B | GRPO | [27B](https://huggingface.co/Qwen/Qwen3.5-27B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_5_27b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 38 | Qwen3.5-27B | GRPO | [27B](https://huggingface.co/Qwen/Qwen3.5-27B) | FSDP(MindSpeed-MM) | vllm | [`link`](https://github.com/verl-project/verl-ascend-recipe/blob/main/grpo_mindspeed_mm/run_qwen3_5-27b_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 39 | Qwen3.5-35B-A3B | GRPO | [35B](https://huggingface.co/Qwen/Qwen3.5-35B-A3B) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_5_35b_fsdp.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 40 | Qwen3.5-35B-A3B | GRPO | [35B](https://huggingface.co/Qwen/Qwen3.5-35B-A3B) | FSDP2(MindSpeed-MM) | vllm | [`link`](https://github.com/verl-project/verl-ascend-recipe/blob/main/grpo_mindspeed_mm/run_qwen3_5-35b_npu.sh) | - | Atlas 200T A2 Box16, Atlas 800T A3 | +| 41 | Qwen3.5-122B-A10B | GRPO | [122B](https://huggingface.co/Qwen/Qwen3.5-122B-A10B) | Megatron | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_5_122b_a10b_megatron.sh) | [`link`](examples/qwen3_5_megatron_npu.md) | Atlas 800T A3 | +| 42 | Qwen3-Next-80B-A3B-Instruct | GRPO | [80B](https://huggingface.co/Qwen/Qwen3-Next-80B-A3B-Instruct) | FSDP2 | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_next_80b_a3b_fsdp.sh) | - | Atlas 900 A2 PODc, Atlas 200T A2 Box16, Atlas 800T A3 | +| 43 | DeepSeek-V3 | DAPO | [671B](https://huggingface.co/deepseek-ai/DeepSeek-V3-Base/) | Megatron | vllm | [`link`](https://github.com/verl-project/verl-ascend-recipe/blob/main/dapo/DeepSeek-V3/scripts/run_dapo_deepseekv3_671b_megatron_8k_npu.sh) | [`link`](https://github.com/verl-project/verl-ascend-recipe/blob/main/dapo/DeepSeek-V3/README.md) | Atlas 800T A3 | +| 44 | Qwen3-1.7B | GRPO | [1.7B](https://huggingface.co/Qwen/Qwen3-1.7B) | VeOmni | vllm | [`link`](https://github.com/verl-project/verl-ascend-recipe/blob/main/verl_ascend_practice/run_qwen3_1_7b_npu.sh) | - | Atlas 800T A3 | +| 45 | Qwen3-30B-A3B | GRPO | [30B](https://huggingface.co/Qwen/Qwen3-30B-A3B) | VeOmni | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_30b_veomni.sh) | - | Atlas 800T A3 | +| 46 | Qwen3-VL-30B-A3B-Instruct | GRPO | [30B](https://huggingface.co/Qwen/Qwen3-VL-30B-A3B-Instruct) | VeOmni | vllm | [`link`](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_vl_30b_moe_veomni.sh) | - | Atlas 800T A3 | + +## Table 2 SFT Algorithms + +| No. | model | algorithm | download link | actor.strategy | shell location | hardware | +|-----|-------|-----------|---------------|----------------|----------------|----------| +| 1 | Qwen3-8B | SFT-PEFT | [8B](https://huggingface.co/Qwen/Qwen3-8B) | FSDP | [`link`](https://github.com/verl-project/verl/blob/main/examples/sft/gsm8k/run_qwen3_8b_fsdp.sh) | Atlas 900 A2 PODc | +| 2 | Qwen2.5-7B-Instruct | ReTool-SFT | [7B](https://huggingface.co/Qwen/Qwen2.5-7B-Instruct) | FSDP | [`link`](https://github.com/verl-project/verl-recipe/blob/main/retool/run_qwen2_7b_sft_npu.sh) | Atlas 900 A2 PODc | diff --git a/verl_0720_main/verl/docs/blog/v0.7.md b/verl_0720_main/verl/docs/blog/v0.7.md new file mode 100644 index 0000000000000000000000000000000000000000..46be13b94ecc781c52dbf970cfa01af89a579a05 --- /dev/null +++ b/verl_0720_main/verl/docs/blog/v0.7.md @@ -0,0 +1,274 @@ +# verl 0.7 release blog + +**Author:** verl team + +Last updated: 01/03/2026. + +## Overview +verl adopts a Hybrid-Controller architecture (also known as HybridFlow). Sharing design principles with asynchronous sharded dataflow systems like Google Pathways, verl models Reinforcement Learning (RL) algorithms, such as PPO, GRPO, DAPO, and others, as a multi-stage, multi-model and parallelizable dataflow graph. + +To balance flexibility with performance, verl unifies two distinct programming models: + +**High-Level Single-Controller (MPMD)**: At the orchestration level, a single process `RLTrainer` manages the global computation graph. It handles macro-tasks such as scheduling rollout generation, triggering reward scoring, and dispatching distributed training jobs. + +**Internal Multi-Controller (SPMD)**: Internally, the Model Engine operates in standard distributed training mode. Workers execute identical programs, via trainer backends like FSDP, Megatron, or VeOmni, or rollout executors (not rollout server) like vLLM/SGLang/TensorRT-LLM, to perform heavy distributed computation, synchronizing via collective communication. + +
+ hybridflow.png +
+ +This hybrid approach offers significant advantages: + +**Flexible Orchestration**: The single-controller design allows verl to dynamically manage complex constraints within the computation graph, including flexible data dependencies, diverse resource allocation and model placement, and fine-grained asynchronous staleness control. + +**Abstraction of Complexity**: We encapsulate complex parallel strategies—such as 5D parallelism (DP, TP, CP, PP, and EP)—strictly within the Model Engine. This allows users to focus entirely on RL algorithm implementation without getting bogged down by the details of distributed training. + +Furthermore, leveraging Ray placement groups, verl provides `ResourcePool` and `WorkerGroup` abstractions. These enable flexible GPU sharing among the various roles in the RL process—such as actor, critic, reward, and rollout—allowing components to share resources efficiently while remaining isolated. + +As illustrated in the diagram below, the overall architecture of verl is divided into two layers: + +- **verl-core**: provides four components required for the RL pipeline: model engine, rollout engine, checkpoint engine, and transfer queue. Each component exposes abstract interfaces, making them both extensible and pluggable. +- **verl-trainer**: builds upon these components, construct various RL pipelines—such as on-policy, one-step-off-policy, and fully asynchronous—tailored to meet the demands of diverse scenarios. + +
+ verl-arch.png +
+ + +## verl-core +### Model Engine + +The Model Engine serves as verl's core training engine, defining a set of abstract interfaces that support pluggable backends. It operates in SPMD mode: +- SFT: Workers are launched via torchrun. +- RL: Workers are executed via the WorkerGroup API, invoked by the single-controller. + +The abstract interfaces include methods like `initialize`, `forward`, `optimizer_step`, and `load`/`offload`. Integrating a new training engine simply requires inheriting and implementing these interfaces. Crucially, because all backends adhere to this unified abstraction, adding a new Model Engine requires absolutely no code modification on the caller side. The RLTrainer remains completely agnostic to the backend's specific parallel strategy when calling these interfaces, while the WorkerGroup automatically handles data dispatch and collection based on the underlying parallelism. + +Currently, the Model Engine supports the following backends (more backend maybe supported in future, e.g torchtitan): +|Backend|Parallelism|Performance|Support Model|New Model Support Time +|-----|-----|----|----|----| +|FSDP| FSDP+SP|Dense medium/MoE low| all transformer models|Day 0 +|MCore| DP+TP+PP+EP+CP|High| see [Megatron-Bridge](https://github.com/NVIDIA-NeMo/Megatron-Bridge) support model list|few weeks or month +|VeOmni| FSDP+SP+EP|Medium| see [VeOmni](https://github.com/ByteDance-Seed/VeOmni) support model list|~1 week + +```python +class BaseEngine: + def initialize(self): + """Instantiate or load the model, optimizer, and learning rate scheduler.""" + raise NotImplementedError + + def optimizer_zero_grad(self): + """Zero the gradients of the optimizer.""" + raise NotImplementedError + + def optimizer_step(self): + """Perform an optimization step using the optimizer.""" + raise NotImplementedError + + def lr_scheduler_step(self): + """Advance the learning rate scheduler by one step.""" + raise NotImplementedError + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> Any: + """Perform a forward pass and optionally a backward pass on a batch of data.""" + raise NotImplementedError + + def get_per_tensor_param(self) -> tuple[Generator[tuple[str, torch.Tensor], None, None], Optional[dict]]: + """Get a generator that yields per-tensor parameters and optional peft config.""" + raise NotImplementedError + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """Move model parameters, optimizer states, or both to the specified device.""" + raise NotImplementedError +``` + + +### Rollout Engine +As LLM reinforcement learning evolves from single-turn, static tasks to multi-turn, dynamic, and interactive agentic tasks, the legacy SPMD rollout mode previously used by verl has become insufficient. Consequently, in verl v0.7, we have removed the SPMD rollout mode and switched to rollout server mode by default. + +
+ rollout_engine.png +
+ +In the server mode, the LLM server operates as online serving rather than the traditional offline batch inference. Clients send per-sample requests to the server, enabling the engine to utilize dynamic batching. This significantly enhances throughput efficiency for multi-turn conversation. Furthermore, the server-based approach eliminates the need for intrusive modifications to the LLM inference engine, allowing for the seamless integration of modern inference backends such as vLLM, SGLang, and TensorRT-LLM. + +On the client side, verl introduces an extensible **AgentLoop** abstraction designed to define custom agentic task loops. This abstraction manages the cycle of requesting responses from the LLM server and interacting with external environments to obtain feedback. We provide two default implementations: +- **SingleTurnAgentLoop**: Designed for standard single-turn tasks. +- **ToolAgentLoop**: Designed for classic ReAct architectures involving multi-turn tool invocation. + +Users can implement custom AgentLoop logic tailored to their specific needs, such as [SWEAgentLoop](https://github.com/verl-project/verl/pull/4080) or GUIAgentLoop. + +```python +class AgentLoopBase(ABC): + @abstractmethod + async def run(self, sampling_params: dict[str, Any], **kwargs) -> AgentLoopOutput: + """Run agent loop to interact with LLM server and environment. + + Args: + sampling_params (Dict[str, Any]): LLM sampling params. + **kwargs: dataset fields from `verl.utils.dataset.RLHFDataset`. + + Returns: + AgentLoopOutput: Agent loop output. + """ + raise NotImplementedError +``` + +### TransferQueue +As mentioned, verl uses a global single-controller RLTrainer to orchestrate the computation graph. A major limitation in the current implementation is that the RLTrainer handles both control and data flow, creating a bottleneck when dispatching data between components. This issue is amplified by the massive data volumes in multimodal training (images, video, audio) and complex algorithms like router replay, which requires transmitting large tensors per sample. Our earlier attempt to solve this using the Ray object store yielded poor performance due to the lack of tensor optimization and fine-grained column access. + +
+ transfer_queue.png +
+ +In v0.7, we experimentally introduced **TransferQueue** to decouple control flow from data flow. The RLTrainer now only dispatch instructions and metadata, while TransferQueue handles data transmission via reference passing. TransferQueue is specifically optimized for PyTorch tensors (supporting zero-copy and RDMA) and allows for backend extensions like ZeroMQ, NIXL, and Ray RDT. We plan to make this the default transmission method in v0.8. + +```python +# In PPOTrainer +def fit(self): + batch = next(dataloader) + gen_batch: BatchMeta = self.rollout_manager.generate_sequences(batch) + output: BatchMeta = self.actor_rollout_wg.compute_log_prob(gen_batch) + gen_batch = gen_batch.union(output) + output = self.actor_rollout_wg.update_actor(gen_batch) + +# In Worker +def compute_log_prob(self, batch: BatchMeta) -> BatchMeta: + data = tq.get(batch) + output = self.actor.infer_batch(data=data) + return tq.put(output) +``` + +### Checkpoint Engine + +With the increase in LLM context lengths and the evolution of agentic tasks, the "long-tail" problem in rollout has become prominent, limiting the overall efficiency of RL training. + +To mitigate this, a viable strategy is moving from on-policy synchronous training to off-policy asynchronous training, e.g [Laminar](https://arxiv.org/abs/2510.12633), [Areal](https://arxiv.org/abs/2505.24298), [StreamRL](https://arxiv.org/abs/2504.15930), [LlamaRL](https://arxiv.org/pdf/2505.24034), [PipelineRL](https://arxiv.org/abs/2509.19128). This involves separating the rollout and model engines onto different nodes (a disaggregated architecture, as opposed to colocated), with data transmitted via queues. This separation alleviates the rollout long-tail issue and enables rollout elastic scaling, fault tolerance, and heterogeneous hardware. However, it introduces a new challenge: efficient cross-node parameter synchronization. + +
+ checkpoint_engine.png +
+ +To address this, we introduce the Checkpoint Engine: a unified abstraction layer designed to synchronize weights between various training and inference backends. +- It provides three unified APIs to implement the streaming transmission of parameters. +- Users can extend the Transport Layer implementation based on their specific infrastructure requirements (device, network, local cache, etc.). + +Currently, we provide two transport backends: NCCL (for broadcast collective communication) and NIXL (for P2P point-to-point communication). + +```python +class CheckpointEngine(ABC): + @abstractmethod + async def send_weights(self, weights: Generator[tuple[str, torch.Tensor], None, None]): + """Send the weights of the model. + + Args: + weights: A generator that yields the name of the weight tensor and the tensor itself. + """ + raise NotImplementedError + + @abstractmethod + async def receive_weights(self) -> Generator[tuple[str, torch.Tensor], None, None]: + """Receive the weights of the model. + + Yields: + A tuple of the name of the weight tensor and the tensor itself. + """ + raise NotImplementedError +``` + +## verl-trainer +Building upon the four core components provided by verl-core, verl-trainer constructs several RL training pipelines tailored to specific scenarios. These pipelines are designed to address training efficiency challenges across varying scales and requirements: + +**On-policy (Synchronous)** + - Main Features: Executes rollout and training serially, typically sharing GPU resources (Colocate). It strictly adheres to standard on-policy algorithm definitions, where training must wait for all samples to be generated. + - Scenarios: Best for baseline implementations, scenarios where strict algorithmic correctness is prioritized over training throughput. + +**One-step-off-policy (Async)** + - Main Features: Parallelizes generation and training by overlapping the current training step with the next batch's generation. It employs resource isolation and uses parameters from the previous step for rollout to minimize GPU idle time. + - Scenarios: Ideal for scenarios requiring moderate efficiency gains (20%–40%) while maintaining training stability very close to strict on-policy methods. + +**Fully async (Decoupled & Streaming)** + - Main Features: Completely decouples the Trainer and Rollouter onto separate nodes. It utilizes streaming data transfer, staleness control, and partial rollout mechanisms to maximize throughput and mitigate long-tail generation latency. + - Scenarios: Essential for large-scale training (e.g., 128+ GPUs) or complex reasoning tasks (e.g., long chain-of-thought) where generation latency significantly bottlenecks performance. + +
+ fully_async.png +
+ +## roadmap +### v0.7 release + +**Model Engine** +- Integrate Megatron-Bridge and support LoRA/PEFT, see blog post: [How We Build Trillion Parameter Reasoning RL with 10% GPUs](https://macaron.im/mindlab/research/building-trillion-parameter-reasoning-rl-with-10-gpus) +- Support experimental fp8 training for megatron backend +- Support new model for megatron backend: GPT-OSS, Qwen3-Next +- Comprehensive support for new mode engine, FSDP and Megatron engine are production ready. + - Dispatch tensordict with nested tensor instead of padded DataProto + - Add TrainingWorker that resembles Tinker-like API + - Add VLM support for model engine, SFT and RL trainer + - Add model engine based critic model + - Implement ActorRolloutRefWorker by TrainingWorker, support different backend in one worker +- New VeOmni engine added, still in alpha status. + +**Rollout Engine** +- Remove SPMD rollout mode +- Support blockwise fp8 rollout for vllm and sglang; support online quant for vllm with torchao +- Experimental router replay support for vllm +- Optimize multi-modal data fetch and preprocess, support video input +- Upgrade to vllm==0.12.0; sglang==0.5.6 + +**Reward** +- Support hybrid reward scenarios, including generative, discriminative, rule-based rewards, and their combinations. +- Refactor reward models into server mode, supporting both colocated and standalone deployments. +- Introduce new reward managers to handle more complex scenarios, limited mode for request rate control and remote mode for CPU-intensive tasks. + +**Algorithm** +- Add [CISPO](https://arxiv.org/pdf/2506.13585): Clipped IS-weight Policy Optimization +- Add [SAPO](https://arxiv.org/abs/2511.20347): Soft Adaptive Policy Optimization + +**Recipe** +- [NEW] VLA: add experimental support for VLA model +- [NEW] [rhymerl](https://arxiv.org/abs/2508.18588): History Rhymes: Accelerating LLM Reinforcement Learning with RhymeRL +- TransferQueue: support multiple data partition and optimize tensor zero-copy serialization +- One-step-off-policy/Fully async: optimize weight synchronization by checkpoint engine with bucket and pipeline support. + +### v0.8 + +**Model Engine** +- Deprecate DataProto by Tensordict for zero padding transmission +- Switch default to new model engine, mark legacy engine (fsdp_workers.py, megatron_workers.py) as deprecated +- Feature parity between new and legacy model engine: LoRA/PEFT, etc +- Polish VeOmni engine to production ready status +- Support MTP RL training +- Optimize GPU memory for long context: fine-grained activation recompuation/offload +- New model support: DeepSeek V3.2, etc + +**Rollout Engine** +- New rollout engine TensorRT-LLM +- Separate vllm worker from trainer process, update weights by cuda ipc + +**TransferQueue** +- Merge TransferQueue recipe into main +- Optimize e2e image/video vlm training pipeline by TransferQueue +- Optimize router replay transmission by TransferQueue + +**Checkpoint Engine** +- Add checkpoint engine abstract interface +- Add NCCL and NIXL transport backend +- Add more transport backend + +### v0.9 + +**Trainer** +- Merge Full async into main: refactor with verl-core component + +**Model Engine** +- Remove legacy model engine (fsdp_workers.py, megatron_workers.py) +- Support omni-model RL training: Qwen3-Omni, BAGEL, etc + +**Rollout Engine** +- New rollout engine vllm-omni + +**More agentic training recipe** +- SWEAgent +- GUIAgent diff --git a/verl_0720_main/verl/docs/conf.py b/verl_0720_main/verl/docs/conf.py new file mode 100644 index 0000000000000000000000000000000000000000..cbeabbd81b28e97fe0d0e8bcf436ab92f5833743 --- /dev/null +++ b/verl_0720_main/verl/docs/conf.py @@ -0,0 +1,113 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Configuration file for the Sphinx documentation builder. +# +# This file only contains a selection of the most common options. For a full +# list see the documentation: +# https://www.sphinx-doc.org/en/master/usage/configuration.html + +# -- Path setup -------------------------------------------------------------- + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +# +# import os +# import sys +# sys.path.insert(0, os.path.abspath('.')) + + +# -- Project information ----------------------------------------------------- + +project = "verl" +copyright = "2024 ByteDance Seed Foundation MLSys Team" +author = "Guangming Sheng, Chi Zhang, Yanghua Peng, Haibin Lin" + + +# -- General configuration --------------------------------------------------- +# The master toctree document. +master_doc = "index" + +# Add any Sphinx extension module names here, as strings. They can be +# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom +# ones. +extensions = [ + "myst_parser", + "sphinx.ext.autodoc", + "sphinx.ext.autosummary", + "sphinx.ext.autosectionlabel", + "sphinx.ext.napoleon", + "sphinx.ext.viewcode", +] + +# MyST-Parser settings +myst_enable_extensions = [ + "dollarmath", # Enables $...$ and $$...$$ syntax + "amsmath", # Enables amsmath environments +] + +# Use Google style docstrings instead of NumPy docstrings. +napoleon_google_docstring = True +napoleon_numpy_docstring = False + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +source_suffix = { + ".rst": "restructuredtext", + ".md": "markdown", +} + +# Add any paths that contain templates here, relative to this directory. +templates_path = ["_templates"] + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# +# This is also used if you do content translation via gettext catalogs. +# Usually you set "language" from the command line for these cases. +language = "en" + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +# This pattern also affects html_static_path and html_extra_path. +exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"] + + +# -- Options for HTML output ------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +# +html_theme = "sphinx_rtd_theme" + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ["_static"] + +# Add the JavaScript file +html_js_files = [ + "js/runllm-widget.js", + "js/resizable-sidebar.js", +] + +# Add custom CSS file for full-width layout +html_css_files = [ + "custom.css", +] + +exclude_patterns += ["README.md", "README_vllm0.7.md"] + +suppress_warnings = ["ref.duplicate", "ref.myst"] diff --git a/verl_0720_main/verl/docs/contributing/editing-agent-instructions.md b/verl_0720_main/verl/docs/contributing/editing-agent-instructions.md new file mode 100644 index 0000000000000000000000000000000000000000..dc3657b0c347c52743277d6cf2993cdc03bdec8f --- /dev/null +++ b/verl_0720_main/verl/docs/contributing/editing-agent-instructions.md @@ -0,0 +1,95 @@ +# Editing Agent Instructions + +> Read this before modifying `AGENTS.md` or any guide it links to. + +## File Layout + +| Generic | Variants | Audience | Scope | +| ----------- | -------------------------- | --------------- | ------------------------------ | +| `AGENTS.md` | `CLAUDE.md`, ... | Agents | Project-wide instructions. | +| `.agent/` | `.claude/`, `.codex/`, ... | Agents | Agent specification directory. | +| `docs/` | N/A | Humans & Agents | Project documentation. | + +Generic files are framework-agnostic. Variants directly symlink generic files or refer to them and add framework-specific content. + +For skills, keep canonical content under `.agent/skills//SKILL.md`. +Variant trees such as `.codex/skills/` and `.claude/skills/` should be directory symlinks to the canonical `.agent` skill directory, not directories containing only a symlinked `SKILL.md`. +Codex documents support for symlinked skill folders and can skip file-level `SKILL.md` symlinks during discovery. Claude Code discovers the file-level symlink layout in current versions, but directory symlinks match the shared skill-package structure and keep supporting files in sync across frameworks. + +## Token Budget Mindset + +`AGENTS.md` loads on every agent request; domain guides load on entry to a relevant area. +Keep `AGENTS.md` under **200 lines** and each domain guide under **300 lines**. +When a file exceeds its budget, split or prune — do not compress prose to fit. + +## When NOT to Add Content + +Before writing a new rule, ask whether it is actually needed: + +- **Agents already do it.** Test with a prompt first. If the agent behaves correctly without the rule, don't add it. +- **One-off incident.** Prefer a code-level fix (lint rule, CI check, test assertion) over a new doc rule. +- **Hardcoded paths.** File paths change; use "search for X" patterns instead. +- **Upstream docs.** Don't reproduce pytest, ruff, or other tool docs — link to them. +- **Contradicts an existing rule.** Search all linked guides before adding. If two rules conflict, consolidate into one. +- **Already covered elsewhere.** Search `AGENTS.md` and every linked guide for overlapping guidance. + +If any of the above apply, **do not add the content**. + +## Where Content Belongs + +The goal is a lean `AGENTS.md` plus rich domain guides that teach agents what they can't learn from the code alone. + +| Scope | File | +| ------------------------------------------------------------------------------------------------ | ------------ | +| Project-wide invariants (contribution policy, env setup, test/lint commands, commit conventions) | `AGENTS.md` | +| Area-specific knowledge (model patterns, format details, deprecation timelines) | Domain guide | + +**Rules of thumb:** + +- If it only matters for one area, put it in a domain guide. +- If it matters for all areas, consider `AGENTS.md` — but first verify agents don't already do it. +- Create a new domain guide when you have 5 or more non-obvious instructions sharing a coherent scope. + +## What Makes a Good Domain Guide + +Add what agents can't infer from the code or public docs: project-specific +conventions that differ from standard patterns, correct approaches that require +cross-file context, and fixes for repeated mistakes. +Each entry should be short, specific, and actionable — e.g., which files to +touch, what order to change them in, and which tests to run. + +## Keeping Docs Lean + +- Every addition should trigger review of surrounding content for stale or redundant items. +- Refer to existing files (e.g., "follow the PR template") instead of restating their content — keep a single source of truth. +- Prefer examples over explanations — a 3-line snippet beats a paragraph of prose. +- Merge related bullets into one principle instead of listing variants. +- Use `search for X` instead of hardcoded file paths. +- PR references are fine in domain guides for traceability, but avoid them in `AGENTS.md`. + +## Anti-Patterns + +| Pattern | Problem | +| ------------------------- | ----------------------------------------------------------------------- | +| Reactive accumulation | Adding a rule per incident without pruning leads to bloat | +| Copy-paste between guides | Duplicated content drifts apart; keep in one place, link from the other | +| Imperative walls | Long DO NOT lists that agents skim past; consolidate into principles | +| Config snapshots | Show the command to get the value, not the value itself | + +## Change Checklist + +Before submitting changes to any agent instruction file: + +- [ ] **Non-obvious?** Would an agent do the wrong thing without this rule? +- [ ] **No conflicts?** Searched all linked guides for contradictions? +- [ ] **Right file?** Project-wide goes in `AGENTS.md`, area-specific in a domain guide? +- [ ] **Offset the addition?** Removed or consolidated something to compensate? +- [ ] **Under budget?** `AGENTS.md` < 200 lines, domain guides < 300 lines? +- [ ] **No hardcoded paths?** Uses "search for X" where paths may change? +- [ ] **Tested?** Verified that an agent actually follows the new instruction? + +## Acknowledgements + +This guide is adapted from the [vLLM project](https://github.com/vllm-project/vllm)'s [`editing-agent-instructions.md`](https://github.com/vllm-project/vllm/blob/main/docs/contributing/editing-agent-instructions.md). + +Last updated: 05/13/2026 diff --git a/verl_0720_main/verl/docs/data/transfer_queue.md b/verl_0720_main/verl/docs/data/transfer_queue.md new file mode 100644 index 0000000000000000000000000000000000000000..40113afac9b000d67e54d0df99b19272fa12af94 --- /dev/null +++ b/verl_0720_main/verl/docs/data/transfer_queue.md @@ -0,0 +1,342 @@ +# TransferQueue Data System + +Last updated: 06/08/2026. + +This doc introduce [TransferQueue](https://github.com/Ascend/TransferQueue), an asynchronous streaming data management system for efficient post-training. + +🔥 **Now TransferQueue is open-sourced at both [GitHub](https://github.com/Ascend/TransferQueue) and [GitCode](https://gitcode.com/Ascend/TransferQueue). You are welcome to submit contributions or propose new ideas on either platform!** + + +> In the meantime, the early development history remains accessible at [TransferQueue/TransferQueue](https://github.com/TransferQueue/TransferQueue). + +

Overview

+ +TransferQueue is a high-performance data storage and transfer module with panoramic data visibility and streaming scheduling capabilities, optimized for efficient dataflow in post-training workflows. + +

+ +

+ +TransferQueue offers **fine-grained, sub-sample-level** data management and **load-balancing** capabilities. It serves as a data gateway that decouples explicit data dependencies across computational tasks, enabling a divide-and-conquer approach that significantly simplifies algorithm controller design. + +

+ +

+ +

Updates

+ + - **April 15, 2026**: 🔥 TransferQueue has been adopted in [Relax](https://github.com/redai-infra/Relax)! By leveraging the `StreamingDataLoader` abstraction, it schedules training data across the cluster at micro-batch granularity, reducing synchronization barriers in a single-controller setup. + - **April 10, 2026**: 🔥 TransferQueue is now officially integrated into [verl](https://github.com/verl-project/verl/pull/5401)! **We achieved an end-to-end performance gain of 49.1% for multi-modal post-training on a 128 × H100 GPU cluster!** Refer to [our blog](https://www.yuque.com/haomingzi-lfse7/lhp4el/gm8mkpfu83luuhxg?singleDoc#) for more details. + - **Feb 8, 2026**: 🔥 Initialization and usage are greatly simplified by high-level APIs [PR#26](https://github.com/Ascend/TransferQueue/pull/26), [PR#28](https://github.com/Ascend/TransferQueue/pull/28). You can now use a Redis-style API to take advantage of most of the advanced features provided by TransferQueue! + - **Jan 28, 2026**: We experimentally introduce the `StreamingDataLoader` interface for a fully-streamed production-consumption pipeline. Refer to our [tutorials/06_streaming_dataloader.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/06_streaming_dataloader.py) for details. + - **Dec 30, 2025**: **TransferQueue x verl** integration has been tested with the DAPO algorithm at scale **(64 nodes, 1024 cards)**. It significantly optimizes host memory utilization and accelerates data transfers. Stay tuned for more details! + - **Dec 20, 2025**: 🔥 The official [tutorial](https://github.com/Ascend/TransferQueue/tree/main/tutorial) is released! Feel free to check it out. + - **Nov 10, 2025**: We disentangled the data retrieval logic from TransferQueueController [PR#101](https://github.com/TransferQueue/TransferQueue/pull/101). Now you can implement your own `Sampler` to customize data consumption. + - **Nov 5, 2025**: We provide a `KVStorageManager` that simplifies the integration with KV-based storage backends [PR#96](https://github.com/TransferQueue/TransferQueue/pull/96). The first available KV-based backend is [openYuanrong](https://gitcode.com/openeuler/yuanrong-datasystem). + - **Nov 4, 2025**: Data partitioning capability is available in [PR#98](https://github.com/TransferQueue/TransferQueue/pull/98). Now you can define logical data partitions to manage your train/val/test datasets. + - **Oct 25, 2025**: Storage backends are now pluggable in [PR#66](https://github.com/TransferQueue/TransferQueue/pull/66). You can try to integrate your own storage backend with TransferQueue now! + - **Oct 21, 2025**: Early integration with verl is ready [verl/pull/3649](https://github.com/volcengine/verl/pull/3649). Following PRs will optimize the single controller architecture by fully decoupling data & control flows. + - **July 22, 2025**: We published a series of Chinese blog posts on Zhihu 1, 2. + - **July 21, 2025**: We initiated an RFC in the verl community [verl/RFC#2662](https://github.com/volcengine/verl/discussions/2662). + - **July 2, 2025**: We published the paper [AsyncFlow](https://arxiv.org/abs/2507.01663). + +

Components

+ +### Control Plane: Panoramic Data Management + +In the control plane, `TransferQueueController` tracks the **production status** and **consumption status** of each training sample as metadata. Once all required data fields are ready (i.e., written to the `StorageManager`), the data sample can be consumed by downstream tasks. + +We also track the consumption history for each computational task (e.g., `generate_sequences`, `compute_log_prob`, etc.). Therefore, even when different computational tasks require the same data field, they can consume the data independently without interfering with each other. + +

+ +

+ +To make the data retrieval process more customizable, we provide a `Sampler` class that allows users to define their own data retrieval and consumption logic. Refer to the [Customize](#customize) section for details. + +> **load-balancing** capabilities are experimentally supported in the control plane. This design enables us to offload some data management capabilities from single controller. Refer to [#PR70](https://github.com/Ascend/TransferQueue/pull/70) for details. + +### Data Plane: Distributed Data Storage + +In the data plane, we utilize a pluggable design, enabling TransferQueue to integrate with different storage backends based on user requirements. + +Specifically, we provide a `StorageManager` abstraction class that defines the core APIs as follows: + +- `async def put_data(self, data: TensorDict, metadata: BatchMeta) -> None` +- `async def get_data(self, metadata: BatchMeta) -> TensorDict` +- `async def clear_data(self, metadata: BatchMeta) -> None` + +This class encapsulates the core interaction logic within the TransferQueue system. You only need to write a simple subclass to integrate your custom storage backend. Refer to the [Customize](#customize) section for details. + +Currently, we support the following storage backends: + +- SimpleStorage: A basic CPU memory storage with minimal data format constraints and ease of use. +- [Yuanrong](https://gitee.com/openeuler/yuanrong-datasystem) ([usage guide](https://github.com/Ascend/TransferQueue/blob/main/docs/storage_backends/openyuanrong_datasystem.md), beta, [#PR107](https://github.com/TransferQueue/TransferQueue/pull/107), [#PR96](https://github.com/TransferQueue/TransferQueue/pull/96)): An Ascend native data system that provides hierarchical storage interfaces including HBM/DRAM/SSD. +- [MooncakeStore](https://github.com/kvcache-ai/Mooncake) (beta, [#PR162](https://github.com/TransferQueue/TransferQueue/pull/162)): A high-performance, KV-based hierarchical storage that supports RDMA transport between GPU and DRAM. +- [RayRDT](https://docs.ray.io/en/master/ray-core/direct-transport/direct-transport.html) (alpha, [#PR167](https://github.com/TransferQueue/TransferQueue/pull/167)): Ray's new feature that allows Ray to store and pass objects directly between Ray actors. + +Among them, `SimpleStorageUnit` serves as our default storage backend, coordinated by the `AsyncSimpleStorageManager` class. Each storage unit can be deployed on a separate node, allowing for distributed data management. + +`SimpleStorageUnit` employs a 2D data structure as follows: + +- Each row corresponds to a training sample, assigned a unique index within the corresponding global batch. +- Each column represents the input/output data fields for computational tasks. + +This data structure design is motivated by the computational characteristics of the post-training process, where each training sample is generated in a relayed manner across task pipelines. It provides precise addressing capabilities, enabling fine-grained, concurrent data read/write operations in a streaming manner. + +

+ +

+ +### User Interface: High-Level & Low-Level APIs + +| Level | Tier | Style | Fine-Grained Access | Streaming | Sampler | Multiple-Backends | +|---|---|---|---|------------------|---|---| +| High | **KV Interface** ([PR#28](https://github.com/Ascend/TransferQueue/pull/28))| Put/Get/List/Clear | ✓ | ○ | ✗ | ✓ | +| High | **StreamingDataLoader** ([PR#23](https://github.com/Ascend/TransferQueue/pull/23)) | PyTorch DataLoader | ✓ | ✓ | ✓ | ✓ | +| Low | **TransferQueueClient** | Metadata-based | ✓ | ✓ | ✓ | ✓ | + + +#### Key-Value based API + +To simplify the usage of TransferQueue, we provide a Redis-style high-level API that exposes most of its advanced features ([PR#28](https://github.com/Ascend/TransferQueue/pull/28)). + +**Methods** + +- **(async_)kv_put**: Insert/Update a multi-column sample by key, with an optional metadata tag. +- **(async_)kv_batch_put**: Put multiple key-value pairs efficiently in batches. +- **(async_)kv_batch_get**: Retrieve samples (by keys), supporting column selection (by fields). +- **(async_)kv_list**: List keys and tags (metadata) in a partition. +- **(async_)kv_clear**: Remove key-value pairs from storage. + +**Key Features** + +- **Redis-style Semantics**: Familiar KV interface (Put/Get/List) for a zero learning curve. +- **Fine-grained Access**: Update or retrieve specific fields (columns) within a key (row) without requiring a full-row operation. +- **Partition Isolation**: Logical separation of storage namespaces. +- **Metadata Tags**: Lightweight metadata for status tracking. +- **Pluggable Backends**: Supports multiple backends. + +Refer to [tutorials/basic.ipynb](https://github.com/Ascend/TransferQueue/blob/main/tutorial/basic.ipynb) and [tutorials/02_kv_interface.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/02_kv_interface.py) for detailed usage examples. + +#### StreamingDataLoader API + +Designed as a drop-in replacement for the standard PyTorch `DataLoader`, this API allows each rank to automatically consume data without single-controller intervention. + +In this scenario, `TransferQueueController` serves as a side-controller for data dispatching, with a user-defined `Sampler` class to organize the dataflow. +It encapsulates the complex scheduling and data transfer logic required for various parallelism strategies, seamlessly integrating TransferQueue into existing training workflows and simplifying the development of disaggregated frameworks. + +See the [Roadmap](https://github.com/Ascend/TransferQueue/issues/1) and [tutorials/06_streaming_dataloader.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/06_streaming_dataloader.py) for more details. + +#### Low-Level Native API + +The native interfaces of TransferQueue are implemented in `TransferQueueClient`. It offers maximum flexibility through native, atomic operations. + +Developers can leverage `TransferQueueClient` directly to implement advanced features that require fine-grained control and fully streamed data scheduling, as illustrated in the following tutorials: +- [tutorial/03_metadata_concepts.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/03_metadata_concepts.py) +- [tutorial/04_understanding_controller.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/04_understanding_controller.py) +- [tutorial/05_custom_sampler.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/05_custom_sampler.py) + + +

🔥 Showcases

+ +### Collocated Example + +#### verl +The primary motivation for integrating TransferQueue into verl is to **alleviate the data transfer bottleneck of the single controller `RayPPOTrainer`**. Currently, all `DataProto` objects must be routed through `RayPPOTrainer`, resulting in a single-point bottleneck for the entire post-training system. + +

+ +

+ +Official integration with verl is available at [verl/pull/5401](https://github.com/verl-project/verl/pull/5401), with the design doc at [[RFC] PPOTrainer with TransferQueue Integration](https://github.com/verl-project/verl/issues/5400). You may also refer to our [recipe](https://github.com/Ascend/TransferQueue/blob/main/recipe/simple_use_case/single_controller_demo.py), where we mimic verl usage in a high-level manner. + + +### Disaggregated Example + +We have experimentally implemented a **standardized, fully-streamed distributed** workflow via TransferQueue. + +By leveraging the `RankAwareSampler` and `StreamingDataLoader` interfaces, we achieve a **streamlined micro-batch-level producer-consumer pipeline**. This design eliminates the need to manually determine data dispatching logic across varying parallelism strategies—a typical complexity in the single-controller paradigm—thereby greatly simplifying framework design. + +Please refer to our [Roadmap](https://github.com/Ascend/TransferQueue/issues/1) and [tutorials/06_streaming_dataloader.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/06_streaming_dataloader.py) for more details. + +

+ +

+ +

🚀 Quick Start

+ +### Use Python package +```bash +pip install TransferQueue +``` + +### Install from source code + +1. Clone the source code from the GitHub repository + ```bash + git clone https://github.com/Ascend/TransferQueue/ + cd TransferQueue + ``` + +2. Install from source code + ```bash + pip install . + ``` + +### Build wheel package from source code + +1. Clone the source code from the GitHub repository + ```bash + git clone https://github.com/Ascend/TransferQueue/ + cd TransferQueue + ``` + +2. Install dependencies + ```bash + pip install build + ``` + +3. Build and install + ```bash + python -m build --wheel + pip install dist/*.whl + ``` + +

📊 Performance

+ +### Simple Case: Regular Tensor +

+ +

+ +### Complex Case: Regular Tensor + NestedTensor + NonTensor +

+ +

+ +> Note: The openYuanrong benchmark uses only a single NPU, so it doesn't reflect multi-NPU scalability. Additionally, openYuanrong was tested on a different hardware setup than the other backends. + +For detailed performance benchmarks, please refer to [the full benchmark report](https://www.yuque.com/haomingzi-lfse7/lhp4el/mywsxovevynra42u?singleDoc#). + +### Stress Test +Beyond throughput, we also validated stability under high concurrency. We provide a [stress test report](https://www.yuque.com/haomingzi-lfse7/lhp4el/mt0vedqy7c337pgg?singleDoc#) that demonstrates more than **8192 concurrent clients writing 2 TB of data** into TransferQueue across 4 nodes. The system remains stable without any crashes or data loss. + +

🛠️ Customize TransferQueue

+ +### Define your own data retrieval logic +We provide a `BaseSampler` abstraction class, which defines the following interface: + +```python3 +@abstractmethod +def sample( + self, + ready_indexes: list[int], + batch_size: int, + *args: Any, + **kwargs: Any, +) -> tuple[list[int], list[int]]: + """Sample a batch of indices from the ready indices. + + Args: + ready_indexes: List of global indices for which all required fields of the + corresponding samples have been produced, and the samples are not labeled as + consumed in the corresponding task. + batch_size: Number of samples to select + *args: Additional positional arguments for specific sampler implementations + **kwargs: Additional keyword arguments for specific sampler implementations + + Returns: + List of sampled global indices of length batch_size + List of global indices of length batch_size that should be labeled as consumed + (will never be retrieved in the future) + + Raises: + ValueError: If batch_size is invalid or ready_indexes is insufficient + """ + raise NotImplementedError("Subclasses must implement sample") +``` + +In this design, we separate data retrieval and data consumption through the two return values, which enables us to easily control sample replacement. We have implemented two reference designs: `SequentialSampler` and `GRPOGroupNSampler`. + +The `Sampler` class or instance should be passed to the `TransferQueueController` during initialization. During each `get_meta` call, you can provide dynamic sampling parameters to the `Sampler`. + +```python3 +from transfer_queue import TransferQueueController, TransferQueueClient, GRPOGroupNSampler, process_zmq_server_info + +# Option 1: Pass the sampler class to the TransferQueueController +controller = TransferQueueController.remote(GRPOGroupNSampler) + +# Option 2: Pass the sampler instance to the TransferQueueController (if you need custom configuration) +your_own_sampler = YourOwnSampler(config) +controller = TransferQueueController.remote(your_own_sampler) + +# Use the sampler +batch_meta = client.get_meta( + data_fields=["input_ids", "attention_mask"], + batch_size=8, + partition_id="train_0", + task_name="generate_sequences", + sampling_config={"n_samples_per_prompt": 4} # Put the required sampling parameters here +) +``` + +**Refer to [tutorial/05_custom_sampler.py](https://github.com/Ascend/TransferQueue/blob/main/tutorial/05_custom_sampler.py) for more details.** + + +### How to integrate a new storage backend + +The data plane is organized as follows: +```text + transfer_queue/ + ├── storage/ + │ ├── __init__.py + │ │── simple_backend.py # Default distributed storage backend (SimpleStorageUnit) by TQ + │ ├── managers/ # Managers are upper level interfaces that encapsulate the interaction logic with TQ system. + │ │ ├── __init__.py + │ │ ├──base.py # StorageManager, KVStorageManager, StorageManagerFactory + │ │ ├──simple_storage_manager.py # AsyncSimpleStorageManager + │ │ ├──yuanrong_manager.py # YuanrongStorageManager + │ │ └──mooncake_manager.py # MooncakeStorageManager + │ └── clients/ # Clients are lower level interfaces that directly manipulate the target storage backend. + │ │ ├── __init__.py + │ │ ├── base.py # StorageKVClient, StorageClientFactory + │ │ ├── yuanrong_client.py # YuanrongStorageClient + │ │ ├── mooncake_client.py # MooncakeStorageClient + │ │ └── ray_storage_client.py # RayStorageClient +``` + +To integrate TransferQueue with a custom storage backend, start by implementing a subclass that inherits from `StorageManager`. This subclass acts as an adapter between the TransferQueue system and the target storage backend. For KV-based storage backends, you can simply inherit from `KVStorageManager`, which can serve as the general manager for all KV-based backends. + +Distributed storage backends often come with their own native clients serving as the interface of the storage system. In such cases, a low-level adapter for this client can be written, following the examples provided in the `storage/clients` directory. + +Factory classes are provided for both `StorageManager` and `StorageClient` to facilitate easy integration. Adding necessary descriptions of required parameters in the factory class helps enhance the overall user experience. + +

✏️ Contribution Guide

+ +**Contributions are warmly welcome!** + +New ideas, feature suggestions, and user experience feedback are all encouraged—feel free to submit issues or PRs. We will respond as soon as possible. + +We recommend using pre-commit for better code format. + +```bash +# install pre-commit +pip install pre-commit + +# run the following command in your repo folder, then fix the check before committing your code +pre-commit install && pre-commit run --all-files --show-diff-on-failure --color=always +``` + + +

Citation

+Please kindly cite our paper if you find this repo is useful: + +```bibtex +@article{han2025asyncflow, + title={AsyncFlow: An Asynchronous Streaming RL Framework for Efficient LLM Post-Training}, + author={Han, Zhenyu and You, Ansheng and Wang, Haibo and Luo, Kui and Yang, Guang and Shi, Wenqi and Chen, Menglong and Zhang, Sicheng and Lan, Zeshun and Deng, Chunshi and others}, + journal={arXiv preprint arXiv:2507.01663}, + year={2025} +} +``` \ No newline at end of file diff --git a/verl_0720_main/verl/docs/examples/config.rst b/verl_0720_main/verl/docs/examples/config.rst new file mode 100644 index 0000000000000000000000000000000000000000..e8b03a599e6ad5a019f4038a976a3d88b4fbb7dc --- /dev/null +++ b/verl_0720_main/verl/docs/examples/config.rst @@ -0,0 +1,752 @@ +.. _config-explain-page: + +Config Explanation +=================== + +Last updated: 06/18/2025. + +ppo_trainer.yaml for RL FSDP Backend +------------------------------------- + +Data +~~~~ + +.. code:: yaml + + data: + tokenizer: null + train_files: ~/data/rlhf/gsm8k/train.parquet + val_files: ~/data/rlhf/gsm8k/test.parquet + train_max_samples: -1 # set to -1 to use full dataset + val_max_samples: -1 # set to -1 to use full dataset + prompt_key: prompt + max_prompt_length: 512 + max_response_length: 512 + train_batch_size: 1024 + return_raw_input_ids: False # This should be set to true when the tokenizer between policy and rm differs + return_raw_chat: False + return_full_prompt: False + shuffle: True + seed: 42 + filter_overlong_prompts: False + filter_overlong_prompts_workers: 1 + truncation: error + image_key: images + trust_remote_code: True + custom_cls: + path: null + name: null + +- ``data.train_files``: Training set parquet. Can be a list or a single + file. The program will read all files into memory, so it can't be too + large (< 100GB). The path can be either local path or HDFS path. For + HDFS path, we provide utils to download it to DRAM and convert the + HDFS path to local path. +- ``data.val_files``: Validation parquet. Can be a list or a single + file. +- ``data.train_max_samples``: Maximum number of samples to use from the + training dataset. Set to -1 to use the full dataset. +- ``data.val_max_samples``: Maximum number of samples to use from the + validation dataset. Set to -1 to use the full dataset. +- ``data.prompt_key``: The field in the dataset where the prompt is + located. Default is 'prompt'. +- ``data.max_prompt_length``: Maximum prompt length. All prompts will be + left-padded to this length. An error will be reported if the length is + too long +- ``data.max_response_length``: Maximum response length. Rollout in RL + algorithms (e.g. PPO) generates up to this length +- ``data.train_batch_size``: Batch size sampled for one training + iteration of different RL algorithms. +- ``data.return_raw_input_ids``: Whether to return the original + input_ids without adding chat template. This is mainly used to + accommodate situations where the reward model's chat template differs + from the policy. It needs to be decoded first, then apply the RM's + chat template. If using a model-based RM, and the policy and RM + chat_templates are different, this flag needs to be set +- ``data.return_raw_chat``: Whether to return the original chat (prompt) + without applying chat template. +- ``data.return_full_prompt``: Whether to return the full prompt with chat template +- ``data.shuffle``: Whether to shuffle the data in the dataloader. +- ``data.seed``: An integer seed to use when shuffling the data. If not set or set to + `null`, the data shuffling will not be seeded, resulting in a different data order on each run. +- ``data.filter_overlong_prompts``: Default don't filter. +- ``data.filter_overlong_prompts_workers``: For large-scale dataset, filtering + overlong prompts could be timeconsuming. You cat set the ``filter_overlong_prompts_workers`` + to use multiprocessing for speed up. Default to 1. +- ``data.truncation``: Truncate the input_ids or prompt length if they + exceed max_prompt_length. Default is 'error', not allow exceed the + max_prompt_length. The users should increase the max_prompt_length if + throwing the error. You can also set ``left``, ``right`` and ``middle``. + When ``middle`` is selected, the logic splits the allowed max length roughly in half + and keeps the head and tail of the sequence, effectively discarding the middle section. +- ``data.image_key``: The field in the multi-modal dataset where the image is + located. Default is 'images'. +- ``data.trust_remote_code``: If the remote tokenizer has python file, we can use this field to allow + using remote tokenizer. For example: moonshotai/Moonlight-16B-A3B-Instruct + +Customized Dataset +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Customized dataset extension is implemented for the SFT trainer and can be extended to other trainers with similar changes. + +.. code:: yaml + + custom_cls: + path: null + name: null + +- ``data.custom_cls.path``: The path to the file containing your customized dataset class. If not specified, pre-implemented dataset will be used. +- ``data.custom_cls.name``: The name of the dataset class within the specified file. + +Actor/Rollout/Reference Policy +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + actor_rollout_ref: + hybrid_engine: True + model: + path: ~/models/deepseek-llm-7b-chat + external_lib: null + override_config: + attn_implementation: flash_attention_2 # or eager, sdpa - attention implementation override + model_config: {} + moe_config: # Megatron only, can adjust moe configuration + freeze_moe_router: False # Megatron only, can freeze moe router (no grad) + enable_gradient_checkpointing: False + enable_activation_offload: False + trust_remote_code: False + use_remove_padding: False + actor: + strategy: fsdp # This is for backward-compatibility + ppo_mini_batch_size: 256 + ppo_micro_batch_size: null # will be deprecated, use ppo_micro_batch_size_per_gpu + ppo_micro_batch_size_per_gpu: 8 + use_dynamic_bsz: False + ppo_max_token_len_per_gpu: 16384 # n * ${data.max_prompt_length} + ${data.max_response_length} + grad_clip: 1.0 + clip_ratio: 0.2 + entropy_coeff: 0.0 + use_kl_loss: False # True for GRPO + # Rollout Correction (corrects distribution mismatch between rollout and training) + rollout_correction: + rollout_is: token # IS weights + rollout_is_threshold: 2.0 # TIS upper bound, or "0.5_5.0" for IcePop + rollout_rs: null # Rejection sampling + rollout_rs_threshold: null # RS upper threshold + use_torch_compile: True # False to disable torch compile + kl_loss_coef: 0.001 # for grpo + kl_loss_type: low_var_kl # for grpo + ppo_epochs: 1 + data_loader_seed: null + shuffle: False + ulysses_sequence_parallel_size: 1 # sp size + optim: + lr: 1e-6 + lr_warmup_steps: -1 # Prioritized. Negative values mean delegating to lr_warmup_steps_ratio. + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: 0.0 # only used with cosine lr scheduler, default to 0.0 + num_cycles: 0.5 # only used with cosine lr scheduler, default to 0.5 + lr_scheduler_type: constant # select from constant/cosine + total_training_steps: -1 # must be override by program + fsdp_config: + wrap_policy: + # transformer_layer_cls_to_wrap: None + min_num_params: 0 + param_offload: False + optimizer_offload: False + fsdp_size: -1 + checkpoint: + # What to include in saved checkpoints + # 'hf_model' saves the full model in HuggingFace format. For Megatron this requires + # actor.megatron.use_mbridge=True (the default); 'model' and 'hf_model' then produce + # the same HF checkpoint and are deduplicated (saved once). With mbridge disabled, + # only the sharded 'model' is supported -- use verl.model_merger after training to + # convert it to HF format. + save_contents: ['model', 'optimizer', 'extra'] + # For more flexibility, you can specify the contents to load from the checkpoint. + load_contents: ${actor_rollout_ref.actor.checkpoint.save_contents} + ref: + fsdp_config: + param_offload: False + wrap_policy: + # transformer_layer_cls_to_wrap: None + min_num_params: 0 + log_prob_micro_batch_size: null # will be deprecated, use log_prob_micro_batch_size_per_gpu + log_prob_micro_batch_size_per_gpu: 16 + log_prob_use_dynamic_bsz: ${actor_rollout_ref.actor.use_dynamic_bsz} + log_prob_max_token_len_per_gpu: ${actor_rollout_ref.actor.ppo_max_token_len_per_gpu} + ulysses_sequence_parallel_size: ${actor_rollout_ref.actor.ulysses_sequence_parallel_size} # sp size + rollout: + name: vllm + temperature: 1.0 + top_k: -1 # 0 for hf rollout, -1 for vllm rollout + top_p: 1 + prompt_length: ${data.max_prompt_length} # not use for opensource + response_length: ${data.max_response_length} + # for vllm rollout + dtype: bfloat16 # should align with FSDP + gpu_memory_utilization: 0.5 + ignore_eos: False + enforce_eager: True + free_cache_engine: True + load_format: dummy_dtensor + tensor_model_parallel_size: 2 + max_num_batched_tokens: 8192 + max_num_seqs: 1024 + log_prob_micro_batch_size: null # will be deprecated, use log_prob_micro_batch_size_per_gpu + log_prob_micro_batch_size_per_gpu: 16 + log_prob_use_dynamic_bsz: ${actor_rollout_ref.actor.use_dynamic_bsz} + log_prob_max_token_len_per_gpu: ${actor_rollout_ref.actor.ppo_max_token_len_per_gpu} + # for hf rollout + do_sample: True + engine_kwargs: # inference engine parameters, please refer vllm/sglang official doc for detail + vllm: {} + sglang: {} + + n: 1 # for each prompt, sample n responses (i.e. num sample times). set it to values > 1 for grpo, rloo + calculate_log_probs: False # set to True for computing log probs via rollouts + val_kwargs: + # sampling parameters for validation + top_k: -1 # 0 for hf rollout, -1 for vllm rollout + top_p: 1.0 + temperature: 0 + n: 1 + do_sample: False # default eager for validation + + agent: + custom_async_server: # Use custom async server implementation for rollout + path: null + name: null + +**Common config for actor, rollout and reference model** + +- ``actor_rollout_ref.hybrid_engine``: Whether it's a hybrid engine, + currently only supports hybrid engine +- ``actor_rollout_ref.model.path``: Huggingface model path. This can be + either local path or HDFS path. For HDFS path, we provide utils to + download it to DRAM and convert the HDFS path to local path. +- ``actor_rollout_ref.model.external_libs``: Additional Python packages + that need to be imported. Used to register models or tokenizers into + the Huggingface system. +- ``actor_rollout_ref.model.override_config``: Used to override some of + the model's original configurations. Common overrides include: + + - ``attn_implementation``: Override the attention implementation. Default is ``flash_attention_2``. + Supported values: ``flash_attention_2``, ``eager``, ``sdpa``. Use ``eager`` for debugging or + compatibility issues. See :ref:`attention-implementation-override` for detailed usage. + +- ``actor_rollout_ref.model.enable_gradient_checkpointing``: FSDP only, decide + Whether to enable gradient checkpointing for the actor, + Megatron uses recompute options in ``override_transformer_config`` to set this +- ``actor_rollout_ref.model.enable_activation_offload``: Whether to enable + activation offloading for the actor +- ``actor_rollout_ref.model.trust_remote_code``: Whether to enable loading + a remote code model +- ``actor_rollout_ref.model.use_fused_kernels``: Whether to use fused + kernels in the model. If set to True, the following parameters will be + used. + + - ``actor_rollout_ref.model.fused_kernel_options.impl_backend``: The + implementation backend for fused kernels. Options: "triton" or + "torch". Default is "torch". + While in megatron, we only support "triton" as the + implementation backend, so there is no need for this option. + +- ``actor_rollout_ref.model.use_remove_padding``: Whether to use remove + padding in the model. If set to True, the model will remove padding + tokens in the input_ids and response_ids. This helps a lot in improving model running efficiency. + +- ``actor_rollout_ref.model.tiled_mlp``: TiledMLP configuration for memory-efficient + MLP computation. Reduces peak memory by processing MLP forward/backward in tiles. + Only compatible with FSDP2 (requires ``actor_rollout_ref.actor.strategy=fsdp2``). + + - ``actor_rollout_ref.model.tiled_mlp.enabled``: Whether to enable TiledMLP. + Default is False. + - ``actor_rollout_ref.model.tiled_mlp.num_shards``: Number of shards to split + the input. Higher values reduce peak memory but may slightly impact performance. + Default is 4. + +**Actor model** + +- ``actor_rollout_ref.actor.strategy``: fsdp or megatron. In this + example, we use fsdp backend. + +- ``actor_rollout_ref.actor.ppo_mini_batch_size``: One sample is split + into multiple sub-batches with batch_size=ppo_mini_batch_size for PPO + updates. The ppo_mini_batch_size is a global num across all workers/gpus + +- ``actor_rollout_ref.actor.ppo_micro_batch_size``: [Will be deprecated, use ppo_micro_batch_size_per_gpu] + Similar to gradient accumulation, the micro_batch_size_per_gpu for one forward pass, + trading speed for GPU memory. The value represent the global view. + +- ``actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu``: Similar to gradient + accumulation, the micro_batch_size_per_gpu for one forward pass, trading speed + for GPU memory. The value represent the local num per gpu. + +- ``actor_rollout_ref.actor.grad_clip``: Gradient clipping for actor + updates +- ``actor_rollout_ref.actor.use_kl_loss``: to use kl loss in actor. When used, we are not applying KL in the reward function. + +- ``actor_rollout_ref.actor.clip_ratio``: PPO clip ratio + +- ``actor_rollout_ref.actor.use_torch_compile``: Whether to use torch compile in actor + +- ``actor_rollout_ref.actor.entropy_coeff``: The weight of entropy when + calculating PPO loss. The default value is changed to 0.0 since v0.3.x + +- ``actor_rollout_ref.actor.ppo_epochs``: Number of epochs for PPO + updates on one set of sampled data + +- ``actor_rollout_ref.actor.data_loader_seed``: From torch 2.6.0 Megatron backend can get wrong seed generated by pytorch + between cp ranks and cause misalignment between data on these ranks, so we shall manually set the seed to avoid hanging + issue. if ``actor_rollout_ref.actor.shuffle`` is not null, this must be set. + +- ``actor_rollout_ref.actor.shuffle``: Whether to shuffle data when + there are multiple epochs + +- ``actor_rollout_ref.actor.optim``: Actor's optimizer parameters + +- ``actor_rollout_ref.actor.fsdp_config``: FSDP config for actor + training + + - ``wrap_policy``: FSDP wrap policy. By default, it uses Huggingface's + wrap policy, i.e., wrapping by DecoderLayer + + - No need to set transformer_layer_cls_to_wrap, so we comment it. + + - ``*_offload``: Whether to enable parameter, gradient and optimizer + offload + + - Trading speed for GPU memory. + +- ``actor_rollout_ref.actor.use_kl_loss``: Whether to enable kl loss. Default is False. + +- ``actor_rollout_ref.actor.kl_loss_coef``: The coefficient of kl loss. Default is 0.001. + +- ``actor_rollout_ref.actor.kl_loss_type``: Support ``kl`` (``k1``), ``abs``, ``mse`` (``k2``), ``low_var_kl`` (``k3``) and ``full``. Appending ``+`` in the end (e.g., ``k1+`` and ``k3+``) would use straight-through to employ ``k2`` for unbiased gradient estimation, regardless of the kl value estimation (see https://github.com/verl-project/verl/pull/2953#issuecomment-3162113848 for more details). How to calculate the kl divergence between actor and reference policy. For specific options, refer to `kl_penalty()` in `core_algos.py `_ . See this blog post for detailed analysis: http://joschu.net/blog/kl-approx.html + +- ``actor_rollout_ref.actor.checkpoint``: The configurations of checkpoint function in actor + + - ``save_contents``: The contents to save in the checkpoint. Accepts any subset of + ``model``, ``optimizer``, ``extra`` and ``hf_model``. Default is + ``['model', 'optimizer', 'extra']``. The extra information includes RNG states (and the + LR scheduler for FSDP, the ``opt_param_scheduler`` for Megatron). + For Megatron, the meaning of ``model`` depends on the active backend + (``actor.megatron.use_mbridge``): + + - With ``use_mbridge=True`` (default): both ``model`` and ``hf_model`` save the full model + in HuggingFace format under ``${ckpt_path}/model/huggingface/`` via mbridge; if both are + listed, the model is saved once (deduplicated). + - With ``use_mbridge=False``: ``model`` saves Megatron sharded weights via + ``dist_checkpointing`` under ``${ckpt_path}/model/dist_ckpt/``; ``hf_model`` is **not** + supported in this mode -- use ``python -m verl.model_merger merge --backend megatron`` + to convert sharded checkpoints to HF format after training. + + For FSDP, ``hf_model`` saves the full HF model on rank 0 in addition to the sharded + ``model`` shards. + + - ``load_contents``: The contents to load in the checkpoint, you can specify different checkpoint loading contents. By default, it is the same with ``save_checkpoint``. + + - ``save_lora_only`` (bool, default ``False``): When ``True`` and the model has LoRA adapters, + only LoRA/adapter weights are saved instead of the full model state dict. On load, LoRA-only + checkpoints are auto-detected and merged into the base model via ``strict=False``. + Reduces checkpoint size dramatically (e.g. ~150 MiB vs ~54 GiB for a 27B model). + +**Reference Model** + +Reference model will be enabled when ``actor.use_kl_loss`` or/and ``algorithm.use_kl_in_reward`` is/are True. + +- ``actor_rollout_ref.ref``: FSDP config same as actor. **For models + larger than 7B, it's recommended to turn on offload for ref by + default** + +- ``actor_rollout_ref.ref.log_prob_micro_batch_size``: [Will be deprecate, use log_prob_micro_batch_size_per_gpu] + The batch size for one forward pass in the computation of ``ref_log_prob``. The value represent the global num. + +- ``actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu``: The batch size + for one forward pass in the computation of ``ref_log_prob``. The value represent the local num per gpu. + +**Rollout Model** + +- ``actor_rollout_ref.rollout.name``: hf/vllm/sglang. + +- Rollout (Auto-regressive) parameters. The key should be equal to the + property name in vLLM's ``SamplingParams``. + + - ``temperature``, ``top_k``, ``top_p`` and others: Sampling + parameters in ``SamplingParams``. + +- ``actor_rollout_ref.rollout.dtype``: Rollout model parameters type. This should be align with + the actor model parameter type in FSDP/Megatron backend. + +- ``actor_rollout_ref.rollout.gpu_memory_utilization``: + + - For vLLM v0.7.0 and later: The fraction of **total** GPU memory to be used for the vLLM instance. + - For SGLang: Corresponding to ``mem_fraction_static``, the fraction of the free GPU memory used for **static** memory like model weights and KV cache. + +- ``actor_rollout_ref.rollout.tensor_model_parallel_size``: TP size for rollout. Only effective + for vllm. + +- ``actor_rollout_ref.rollout.log_prob_micro_batch_size``: [Will be deprecate, use log_prob_micro_batch_size_per_gpu] + The batch size for one forward pass in the computation of ``log_prob``. The value represent the global num. + +- ``actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu``: Micro batch size per gpu (The batch size for + one forward pass) for recalculating ``log_prob``. The value represent the local num per gpu. + +- ``actor_rollout_ref.rollout.do_sample``: Whether to sample during training rollout. If set to False, the rollout model + will perform greedy sampling. + +- ``actor_rollout_ref.rollout.val_kwargs```: Sampling parameters used specifically during validation. + + - ``top_k``: Top-k sampling parameter. Default to -1 for vLLM rollout or 0 for HF rollout. + - ``top_p``: Top-p sampling parameter. Default is 1.0 (disabled). + - ``temperature``: Sampling temperature. Default is 0 (deterministic greedy). + - ``n``: Number of responses to generate during validation. Default is 1. + - ``do_sample``: Whether to use sampling during validation. Default is False for + deterministic outputs. When set to True, the rollout will use the ``actor_rollout_ref.rollout.val_kwargs`` parameters + (top_k, top_p, temperature) to control the sampling behavior. + +- ``actor_rollout_ref.rollout.engine_kwargs.vllm``: extra vllm engine args, please refer vllm official doc for detail + +- ``actor_rollout_ref.rollout.engine_kwargs.sglang``: extra sglang engine args, please refer sglang official doc for detail + +- ``actor_rollout_ref.rollout.ignore_eos``: Whether to ignore the EOS + token and continue generating tokens after the EOS token is generated. + +- ``actor_rollout_ref.rollout.free_cache_engine``: Offload the KVCache + after rollout generation stage. Default is True. When set to True, + for vllm v0.5.4 and v0.6.3, we need to disable the usage of CUDAGraph + (set ``enforce_eager`` to True.) + +- ``actor_rollout_ref.rollout.enforce_eager``: Whether to use CUDAGraph + in vLLM generation. Default set to True to disable CUDAGraph. + +- ``actor_rollout_ref.rollout.load_format``: Which weight loader to use + to load the actor model weights to the rollout model. + + - ``auto``: Use Megatron weight loader. + - ``megatron``: Use Megatron weight loader. Deployed with Megatron + backend. The input model ``state_dict()`` is already partitioned + along TP dimension and already gathered along PP dimension. This + weight loader requires that the Rollout model and Actor model's + parameters shape and name should be identical. + - ``dtensor``: Default solution when using Huggingface weight loader. + Deployed with FSDP backend and the state_dict_type is + ``StateDictType.SHARDED_STATE_DICT``. Recommend to use this weight + loader + - ``hf``: Use Huggingface weight loader. Deployed with FSDP backend + and the state_dict_type is ``StateDictType.FULL_STATE_DICT``. This + solution doesn't need to rewrite the weight loader for each model + implemented in vLLM but it results in larger peak memory usage. + - ``dummy_hf``, ``dummy_megatron``, ``dummy_dtensor``: Random + initialization. + +.. note:: **NOTED**: In this config field, users only need to select from ``dummy_megatron``, ``dummy_dtensor``, ``dummy_hf`` for rollout initialization and our hybrid engine will select the corresponding weight loader (i.e., ``megatron``, ``dtensor``, ``hf``) during actor/rollout weight synchronization. + + +Megatron Optimizer and Optimizer Parameter Scheduler +____________________________________________________ + +.. code:: yaml + + optim: + optimizer: adam + lr: 1e-6 + clip_grad: 1.0 + total_training_steps: -1 # must be override by program + lr_warmup_init: 0.0 # initial learning rate for warmup, default to 0.0 + lr_warmup_steps: -1 # Prioritized. Negative values mean delegating to lr_warmup_steps_ratio. + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + lr_decay_steps: null + lr_decay_style: constant # select from constant/linear/cosine/inverse_square_root + min_lr: 0.0 # minimum learning rate, default to 0.0 + weight_decay: 0.01 + weight_decay_incr_style: constant # select from constant/linear/cosine + lr_wsd_decay_style: exponential # select from constant/exponential/cosine + lr_wsd_decay_steps: null + use_checkpoint_opt_param_scheduler: False # use checkpoint optimizer parameter scheduler + + +Notice that there are some differences in APIs between Megatron optimizer and FSDP optimizer. + +- Megatron optimizer scheduler names the period after lr_warmup as lr_decay_steps, so the ``lr_scheduler_type`` actually means the style of lr decay after warmup. +- Megatron optimizer also support weight decay decay mechanism +- ``use_checkpoint_opt_param_scheduler`` determines whether to use the checkpoint optimizer parameter scheduler. If set to True, the optimizer parameter scheduler will be saved in the checkpoint and loaded from the checkpoint during resuming training. + +For learning rate decay, original Megatron pretrain default option of ``lr_decay_style`` is ``linear``, +meaning that the learning rate will be linearly decayed from the initial learning rate to ``min_lr`` within the +``lr_decay_steps``. However, in verl, to align with FSDP's default behavior, we set the default +``lr_decay_style`` to ``constant``, meaning that the learning rate will be kept constant after the warmup stage. + + +Critic Model +~~~~~~~~~~~~ + +Most parameters for Critic are similar to Actor Model. + +Reward Model +~~~~~~~~~~~~ + +.. code:: yaml + + reward_model: + enable: False + model: + input_tokenizer: ${actor_rollout_ref.model.path} # set this to null if the chat template is identical + path: ~/models/Anomy-RM-v0.1 + external_lib: ${actor_rollout_ref.model.external_lib} + trust_remote_code: False + fsdp_config: + min_num_params: 0 + param_offload: False + micro_batch_size_per_gpu: 16 + max_length: null + reward_manager: naive + +- ``reward_model.enable``: Whether to enable reward model. If False, we + compute the reward only with the user-defined reward functions. In + GSM8K and Math examples, we disable reward model. For RLHF alignment + example using full_hh_rlhf, we utilize reward model to assess the + responses. If False, the following parameters are not effective. +- ``reward_model.model`` + + - ``input_tokenizer``: Input tokenizer. If the reward model's chat + template is inconsistent with the policy, we need to first decode to + plaintext, then apply the rm's chat_template. Then score with RM. If + chat_templates are consistent, it can be set to null. + - ``path``: RM's HDFS path or local path. Note that RM only supports + AutoModelForSequenceClassification. Other model types need to define + their own RewardModelWorker and pass it from the code. + - ``trust_remote_code``: Whether to enable loading a remote code model, + default to False. +- ``reward_model.reward_manager``: Reward Manager. This defines the mechanism + of computing rule-based reward and handling different reward sources. Default + is ``naive``. If all verification functions are multiprocessing-safe, the reward + manager can be set to ``prime`` for parallel verification. + +Customized Reward Function +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + custom_reward_function: + path: null + name: compute_score + +- ``custom_reward_function.path``: The path to the file containing your customized reward function. If not specified, pre-implemented reward functions will be used. +- ``custom_reward_function.name`` (Optional) : The name of the reward function within the specified file. Default is 'compute_score'. + +Algorithm +~~~~~~~~~ + +.. code:: yaml + + algorithm: + gamma: 1.0 + lam: 1.0 + adv_estimator: gae + use_kl_in_reward: False + kl_penalty: kl # how to estimate kl divergence + kl_ctrl: + type: fixed + kl_coef: 0.005 + horizon: 10000 + target_kl: 0.1 + # Rollout Correction + rollout_correction: + rollout_is: null # IS weights + rollout_is_threshold: 2.0 # Upper threshold for IS weights + rollout_rs: null # Rejection sampling + rollout_rs_threshold: null # RS upper threshold + +- ``gamma``: discount factor +- ``lam``: Trade-off between bias and variance in the GAE estimator +- ``adv_estimator``: Support ``gae``, ``grpo``, ``reinforce_plus_plus``, ``reinforce_plus_plus_baseline``, ``rloo``, ``rloo_vectorized``, ``grpo_vectorized`` +- ``use_kl_in_reward``: Whether to enable in-reward kl penalty. Default is False. +- ``kl_penalty``: Support ``kl``, ``abs``, ``mse``, ``low_var_kl`` and ``full``. How to + calculate the kl divergence between actor and reference policy. For + specific options, refer to `kl_penalty()` in `core_algos.py `_ . +- ``kl_ctrl``: Config for in-reward kl_penalty controller + + - ``kl_coef``: The (initial) coefficient of in-reward kl_penalty. Default is 0.001. + - ``type``: 'fixed' for FixedKLController and 'adaptive' for AdaptiveKLController. + - ``horizon`` and ``target_kl``: See source code of AdaptiveKLController for details. + +- ``rollout_correction``: Rollout Correction configuration (nested dict). Set to ``null`` to disable. + When enabled, contains: + + - ``rollout_is``: IS weights aggregation level, ``null`` to disable IS weights. + - ``rollout_is_threshold``: Upper threshold for IS weights (e.g., 2.0). + - ``rollout_rs``: Rejection sampling mode, ``null`` to disable RS. + - ``rollout_rs_threshold``: RS upper threshold. + + Note: Rollout Correction requires setting ``actor_rollout_ref.rollout.calculate_log_probs=True``. + +Trainer +~~~~~~~ + +.. code:: yaml + + trainer: + total_epochs: 30 + project_name: verl_examples + experiment_name: gsm8k + logger: ['console', 'wandb'] + log_val_generations: 0 + nnodes: 1 + n_gpus_per_node: 8 + save_freq: -1 + val_before_train: True + test_freq: 2 + critic_warmup: 0 + default_hdfs_dir: null # hdfs checkpoint path + default_local_dir: checkpoints/${trainer.project_name}/${trainer.experiment_name} # local checkpoint path + resume_mode: auto # or disable or resume_path if resume_from_path is set + resume_from_path: null + remove_previous_ckpt_in_save: False + del_local_ckpt_after_load: False + ray_wait_register_center_timeout: 300 + +- ``trainer.total_epochs``: Number of epochs in training. +- ``trainer.project_name``: For wandb, swanlab, mlflow +- ``trainer.experiment_name``: For wandb, swanlab, mlflow +- ``trainer.logger``: Support console, wandb, swanlab, mlflow, tensorboard, trackio, and rl_insight. +- ``trainer.log_val_generations``: The number of logged generation during validation (default ``0``) +- ``trainer.nnodes``: Number of nodes used in the training. +- ``trainer.n_gpus_per_node``: Number of GPUs per node. +- ``trainer.save_freq``: The frequency (by iteration) to save checkpoint + of the actor and critic model. +- ``trainer.val_before_train``: Whether to run validation before training. +- ``trainer.test_freq``: The validation frequency (by iteration). +- ``trainer.critic_warmup``: The number of iteration to train the critic + model before actual policy learning. +- ``trainer.resume_mode``: The mode of resuming training. Support + ``disable``, ``auto`` and ``resume_path``. If set to ``auto`` as default, the + program will automatically resume from the latest checkpoint in the + ``default_local_dir``. If set to ``resume_path``, the program will resume + from the path specified in ``resume_from_path``. +- ``trainer.resume_from_path``: The path to resume training from. Only + effective when ``resume_mode`` is set to ``resume_path``. +- ``trainer.remove_previous_ckpt_in_save``: Whether to remove previous + checkpoints in the save directory. Default is False. +- ``trainer.del_local_ckpt_after_load``: Whether to delete local + checkpoints after loading them. Default is False. +- ``trainer.ray_wait_register_center_timeout``: The timeout for waiting + for the ray register center to be ready. Default is 300 seconds. + + +This figure illustrates how the configurations affect the training. + +https://excalidraw.com/#json=pfhkRmiLm1jnnRli9VFhb,Ut4E8peALlgAUpr7E5pPCA + +.. image:: https://github.com/user-attachments/assets/16aebad1-0da6-4eb3-806d-54a74e712c2d + + +evaluation.yaml +--------------- + +Data +~~~~ + +.. code:: yaml + + data: + path: /tmp/math_Qwen2-7B-Instruct.parquet + prompt_key: prompt + response_key: responses + data_source_key: data_source + reward_model_key: reward_model + +- ``data.path``: Path to the dataset file (Parquet format). +- ``data.prompt_key``: The field in the dataset where the prompt is located. Default is 'prompt'. +- ``data.response_key``: The key holds the generated responses. This should be a list of strings representing the responses. Default is 'responses'. +- ``data.data_source_key``: This is used to separate metric calculations for different data sources, ensuring that metrics are calculated independently for each source. +- ``data.reward_model_key``: The key holds the reference answers. These reference answers typically serve as the ground truth or test cases for the task. + +Customized Reward Function +~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + custom_reward_function: + path: null + name: compute_score + +- ``custom_reward_function.path``: The path to the file containing your customized reward function. If not specified, pre-implemented reward functions will be used. +- ``custom_reward_function.name`` (Optional) : The name of the reward function within the specified file. Default is 'compute_score'. + +sft_trainer.yaml for SFT FSDP Backend +-------------------------------------- + + +Optim +~~~~~~~ + +.. code:: yaml + + optim: + optimizer: AdamW + optimizer_impl: torch.optim + lr: 1e-5 + weight_decay: 0.01 + lr_warmup_steps_ratio: 0.1 + clip_grad: 1.0 + lr_scheduler: cosine + override_optimizer_config: null + +- ``optimizer``: Optimizer class name (e.g., ``"AdamW"``, ``"AdamW8bit"``, ``"_AdamW"``). The class name as it appears in the module. +- ``optimizer_impl``: Module path to import optimizer from (e.g., ``"torch.optim"``, ``"torchao.optim"``, ``"bitsandbytes.optim"``). +- ``optim.lr``: Learning rate for the optimizer. +- ``optim.weight_decay``: Weight decay for the optimizer. +- ``optim.lr_warmup_steps_ratio``: Ratio of warmup steps to total training steps. +- ``optim.clip_grad``: Gradient clipping value. +- ``optim.lr_scheduler``: Learning rate scheduler type. Options: + + - ``cosine``: Cosine learning rate scheduler with warmup (default). + - ``wsd``: Warmup-Stable-Decay scheduler that provides a stable learning rate phase between warmup and decay phases. + +- ``override_optimizer_config``: Dictionary of additional optimizer-specific keyword arguments. For example, to use ``torchao.optim``'s ``_AdamW`` with BF16 stochastic rounding: ``{"bf16_stochastic_round": true}`` + +Model +~~~~~~~~~~~~ + +Most parameters for Model are similar to Reward Model. + +.. code:: yaml + + model: + partial_pretrain: ~/models/gemma-1.1-7b-it + fsdp_config: + model_dtype: fp32 + wrap_policy: + min_num_params: 0 + cpu_offload: False + offload_params: False + external_lib: null + enable_gradient_checkpointing: False + trust_remote_code: False + lora_rank: 0 + lora_alpha: 16 + target_modules: all-linear + use_liger: False + +- ``partial_pretrain``: HDFS path or local path for the pretrained model. +- ``fsdp_config`` + + - ``model_dtype``: Model parameters type, default to ``fp32``. + Support: ``bf16``, ``fp16``, ``fp32``. + - ``cpu_offload``: Whether to enable CPU offloading for FSDP. If True, + the offload_params will be used as argument. + - ``offload_params``: Whether to offload parameters to CPU + when not involved in computation. If True, then this offloads gradients + to CPU as well, meaning that the optimizer step runs on CPU. + +- ``lora_rank``: The rank of the LoRA model, default to 0. If ``lora_rank``>0, + we will train LoRA modules instead of tuning the full model. +- ``lora_alpha``: The alpha parameter for LoRA scaling, default to 16. +- ``target_modules``: The names of the modules to apply the adapter to, + default to ``all-linear``. See `peft docs `_ for detail. + +- ``use_liger``: Whether to enable Liger kernel, default to False. If True, + we apply Liger kernel to the model (depends on `liger-kernel`). diff --git a/verl_0720_main/verl/docs/examples/gsm8k_example.rst b/verl_0720_main/verl/docs/examples/gsm8k_example.rst new file mode 100644 index 0000000000000000000000000000000000000000..e653fe065e23d8d38736f4a2c29f705615d70aa9 --- /dev/null +++ b/verl_0720_main/verl/docs/examples/gsm8k_example.rst @@ -0,0 +1,188 @@ +GSM8K Example +============= + +Last updated: 03/25/2025. + +Introduction +------------ + +In this example, we train an LLM to tackle the GSM8k task. + +Paper: https://arxiv.org/pdf/2110.14168 + +Dataset: https://huggingface.co/datasets/openai/gsm8k + +Note that the original paper mainly focuses on training a verifier (a +reward model) to solve math problems via Best-of-N sampling. In this +example, we train an RLHF agent using a rule-based reward model. + +Dataset Introduction +-------------------- + +GSM8k is a math problem dataset. The prompt is an elementary school +problem. The LLM model is required to answer the math problem. + +The training set contains 7473 samples and the test set contains 1319 +samples. + +**An example** + +Prompt + + Katy makes coffee using teaspoons of sugar and cups of water in the + ratio of 7:13. If she used a total of 120 teaspoons of sugar and cups + of water, calculate the number of teaspoonfuls of sugar she used. + +Solution + + The total ratio representing the ingredients she used to make the + coffee is 7+13 = <<7+13=20>>20 Since the fraction representing the + number of teaspoons she used is 7/20, she used 7/20\ *120 = + <<7/20*\ 120=42>>42 #### 42 + +Step 1: Prepare dataset +----------------------- + +.. code:: bash + + cd examples/data_preprocess + python3 gsm8k.py --local_save_dir ~/data/gsm8k + +Step 2: Download Model +---------------------- + +There're three ways to prepare the model checkpoints for post-training: + +- Download the required models from huggingface or modelscope + +.. code:: bash + + hf download deepseek-ai/deepseek-math-7b-instruct --local-dir ~/models/deepseek-math-7b-instruct --local-dir-use-symlinks False + # or + modelscope download --model deepseek-ai/deepseek-math-7b-instruct --local_dir ~/models/deepseek-math-7b-instruct + +- Already store your store model in the local directory or HDFS path. +- Also, you can directly use the model name in huggingface (e.g., + deepseek-ai/deepseek-math-7b-instruct) in + ``actor_rollout_ref.model.path`` and ``critic.model.path`` field in + the run script. You can also download models from modelscope by setting environmental variable ``VERL_USE_MODELSCOPE=True``. + +Noted that users should prepare checkpoints for actor, critic and reward +model. + +[Optional] Step 3: SFT your Model +--------------------------------- + +We provide a SFT Trainer using PyTorch FSDP in +`sft_trainer.py `_. +Users can customize their own SFT +script using our FSDP SFT Trainer. + +We also provide various training scripts for SFT on GSM8K dataset in `gsm8k sft directory `_. + +.. code:: shell + + set -x + + torchrun -m verl.trainer.sft_trainer \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.messages_key=messages \ + data.micro_batch_size_per_gpu=8 \ + model.path=deepseek-ai/deepseek-coder-6.7b-instruct \ + trainer.project_name=gsm8k-sft \ + trainer.experiment_name=gsm8k-sft-deepseek-coder-6.7b-instruct \ + trainer.total_epochs=4 \ + trainer.logger='["console","wandb"]' + + +If you use AMD GPUs (ROCm kernel), you need to add the following environment variables into the run script: + + .. code-block:: bash + + export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + export ROCR_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + export CUDA_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + + +Step 4: Perform PPO training with your model on GSM8K Dataset +------------------------------------------------------------- + +- Prepare your own run.sh script. Here's an example for GSM8k dataset + and deepseek-llm-7b-chat model. +- Users could replace the ``data.train_files`` ,\ ``data.val_files``, + ``actor_rollout_ref.model.path`` and ``critic.model.path`` based on + their environment. +- See :doc:`config` for detailed explanation of each config field. + +**Reward Model/Function** + +We use a rule-based reward model. We force the model to produce a final +answer following 4 “#” as shown in the solution. We extract the final +answer from both the solution and model's output using regular +expression matching. We compare them and assign a reward of 1 to correct +answer, 0.1 to incorrect answer and 0 to no answer. + +**Training Script** + +The training script example for FSDP and Megatron-LM backend are stored in examples/ppo_trainer directory. + +.. code:: bash + + cd ../ppo_trainer + bash run_deepseek_llm_7b_fsdp.sh + +The script of run_deepseek_llm_7b_fsdp.sh + +.. code:: bash + + set -x + + python3 -m verl.trainer.main_ppo \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.train_batch_size=1024 \ + data.max_prompt_length=512 \ + data.max_response_length=512 \ + actor_rollout_ref.model.path=deepseek-ai/deepseek-llm-7b-chat \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.model.enable_gradient_checkpointing=True \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=32 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.5 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=32 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.use_remove_padding=True \ + critic.model.path=deepseek-ai/deepseek-llm-7b-chat \ + critic.model.enable_gradient_checkpointing=True \ + critic.ppo_micro_batch_size_per_gpu=32 \ + critic.fsdp.param_offload=False \ + critic.fsdp.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger='["console","wandb"]' \ + trainer.project_name='verl_example_gsm8k' \ + trainer.experiment_name='deepseek_llm_7b_function_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=1 \ + trainer.total_epochs=15 $@ + + +If you use AMD GPUs (ROCm kernel), you need to add the following environment variables into the run script: + + .. code-block:: bash + + export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + export ROCR_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + export CUDA_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + +If you encounter any issues in using AMD GPUs running VeRL, feel free to contact me - `Yusheng Su `_. \ No newline at end of file diff --git a/verl_0720_main/verl/docs/examples/megatron_fsdp_example.rst b/verl_0720_main/verl/docs/examples/megatron_fsdp_example.rst new file mode 100644 index 0000000000000000000000000000000000000000..6b2cad4b5841bb5ad133aa00c2e70c287bb00fe5 --- /dev/null +++ b/verl_0720_main/verl/docs/examples/megatron_fsdp_example.rst @@ -0,0 +1,73 @@ +Megatron-FSDP Example +======================== + +Last updated: 04/29/2026. + +Introduction +------------ + +In this example, we run SFT and RL training with Megatron-FSDP: + +- Runtime image: ``verlai/verl:vllm011.dev7`` + +Step 1: Prepare +-------------------- + +Download ``Megatron-LM`` and ``Megatron-Bridge``. The required Megatron-FSDP support has already been merged into + ``Megatron-LM`` main + (``) and + ``Megatron-Bridge`` main + (``). + +.. code:: bash + + git clone https://github.com/NVIDIA/Megatron-LM.git + git clone https://github.com/NVIDIA-NeMo/Megatron-Bridge.git + +Step 2: Run Megatron-FSDP SFT +---------------------------- + +Before launch, check and update key fields ``MODEL_PATH`` and ``SAVE_PATH`` in the script. + +.. code:: bash + + bash examples/sft/gsm8k/run_qwen_megatron_fsdp.sh + +Step 3: Run Megatron-FSDP RL +---------------------------- + +Before launch, check and update key fields in +``examples/grpo_trainer/run_qwen2-7b_math_megatron_fsdp.sh``: + +- ``actor_rollout_ref.model.path``: model name or local model path. +- ``train_files`` / ``test_files``: parquet paths for GSM8K and MATH. +- ``trainer.n_gpus_per_node`` and ``trainer.nnodes``: hardware topology. +- ``trainer.project_name`` and ``trainer.experiment_name``: experiment identifiers. + +Then run: + +.. code:: bash + + bash examples/grpo_trainer/run_qwen2-7b_math_megatron_fsdp.sh + +The script launches RL training and enables Megatron-FSDP with: + +- ``actor_rollout_ref.actor.megatron.use_mbridge=True`` +- ``actor_rollout_ref.actor.megatron.vanilla_mbridge=False`` +- ``actor_rollout_ref.actor.megatron.use_megatron_fsdp=True`` + +Checkpoint Notes +---------------- + +Megatron-FSDP checkpoints are saved as DTensor checkpoints under ``dist_ckpt``. +When ``checkpoint.save_contents`` includes ``model``, verl also saves the HuggingFace config and +tokenizer under ``huggingface``; HF weights can also be exported through Megatron-Bridge. + +Current Megatron-FSDP checkpoint examples assume: + +- ``use_distributed_optimizer=True``. +- ``CUDA_DEVICE_MAX_CONNECTIONS`` is unset or greater than ``1``. +- PEFT + Megatron-FSDP checkpoint save/load is not covered by this example yet. +- ``checkpoint.async_save=True`` is not covered for Megatron-FSDP DTensor checkpoints yet. +- Megatron-FSDP checkpoints do not support saving optimizer state by itself; include ``model`` whenever + ``optimizer`` is listed in ``checkpoint.save_contents``. diff --git a/verl_0720_main/verl/docs/examples/multi_modal_example.rst b/verl_0720_main/verl/docs/examples/multi_modal_example.rst new file mode 100644 index 0000000000000000000000000000000000000000..c71b562b4614dae40ea2e204d998307b7db649c8 --- /dev/null +++ b/verl_0720_main/verl/docs/examples/multi_modal_example.rst @@ -0,0 +1,45 @@ +Multi-Modal Example Architecture +================================= + +Last updated: 04/28/2025. + +Introduction +------------ + +Now, verl has supported multi-modal training. You can use fsdp and +vllm/sglang to start a multi-modal RL task. Megatron supports is also +on the way. + +Follow the steps below to quickly start a multi-modal RL task. + +Step 1: Prepare dataset +----------------------- + +.. code:: python + + # it will be saved in the $HOME/data/geo3k folder + python examples/data_preprocess/geo3k.py + +Step 2: Download Model +---------------------- + +.. code:: bash + + # download the model from huggingface + python3 -c "import transformers; transformers.pipeline(model='Qwen/Qwen2.5-VL-7B-Instruct')" + +Step 3: Perform GRPO training with multi-modal model on Geo3K Dataset +--------------------------------------------------------------------- + +.. code:: bash + + # run the task + bash examples/grpo_trainer/run_qwen2_5_vl_7b_fsdp.sh + + + + + + + + diff --git a/verl_0720_main/verl/docs/examples/ppo_code_architecture.rst b/verl_0720_main/verl/docs/examples/ppo_code_architecture.rst new file mode 100644 index 0000000000000000000000000000000000000000..aaa91a7e7cf834fe30243f302b8759cafd363aa8 --- /dev/null +++ b/verl_0720_main/verl/docs/examples/ppo_code_architecture.rst @@ -0,0 +1,206 @@ +PPO Example Architecture +======================== + +Last updated: 02/17/2025. + +Let's start with the Proximal Policy Optimization algorithm, which is +most widely used algorithm in LLM post-training. + +The main entry point of the PPO algorithm example is: +`main_ppo.py `_. +In this tutorial, we will go through the code architecture in `main_ppo.py `_. + +Define the data +--------------- + +Users need to preprocess and store the dataset in parquet files. +And we implement `RLHFDataset` to load and tokenize the parquet files. + +For ``RLHFDataset`` (Default), at least 1 fields are required: + +- ``prompt``: Contains the string prompt + +We already provide some examples of processing the datasets to parquet +files in `data_preprocess directory `_. Currently, we support +preprocess of GSM8k, MATH, HellaSwag, Full_hh_rlhf datasets. See :doc:`../preparation/prepare_data` for +more information. + +Define the reward functions for different datasets +-------------------------------------------------- + +In this main entry point, the users only need to define their own reward +function based on the datasets (or applications) utilized in PPO +training. + +For example, we already provide reward functions for `GSM8k `_ +and `MATH `_ +datasets in the ``_select_rm_score_fn``. In the ``RewardManager``, we +will compute the reward score based on the data_source to select +corresponding reward functions. For some RLHF datasets (e.g., +full_hh_rlhf), the reward model is utilized to assess the responses +without any reward functions. In this case, the ``RewardManager`` will +return the ``rm_score`` computed by the reward model directly. + +See `reward functions `_ for detailed implementation. + +Define worker classes +--------------------- + +verl ships a single, unified model-engine worker implementation. The actor/rollout/ref +policy live in :class:`verl.workers.engine_workers.ActorRolloutRefWorker`, and the +critic/reward-model live in :class:`verl.workers.engine_workers.TrainingWorker`. +The underlying backend (FSDP, FSDP2, Megatron-LM, torchtitan, veomni, ...) is selected +at runtime from ``config.actor_rollout_ref.actor.strategy`` / ``config.critic.strategy``. + +.. code:: python + + from verl.single_controller.ray import RayWorkerGroup + from verl.trainer.ppo.ray_trainer import ResourcePoolManager, Role + from verl.workers.engine_workers import ActorRolloutRefWorker, TrainingWorker + + ray_worker_group_cls = RayWorkerGroup + + role_worker_mapping = { + Role.ActorRollout: ActorRolloutRefWorker, + Role.Critic: TrainingWorker, + Role.RefPolicy: ActorRolloutRefWorker + } + + global_pool_id = 'global_pool' + resource_pool_spec = { + global_pool_id: [config.trainer.n_gpus_per_node] * config.trainer.nnodes, + } + mapping = { + Role.ActorRollout: global_pool_id, + Role.Critic: global_pool_id, + Role.RefPolicy: global_pool_id, + } + +Step 1: Construct the mapping between roles and workers +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +A role represents a group of workers in the same process. We have +pre-defined several roles in `ray_trainer.py `_. + +.. code:: python + + class Role(Enum): + """ + To create more roles dynamically, you can subclass Role and add new members + """ + Actor = 0 # This worker only has Actor + Rollout = 1 # This worker only has Rollout + ActorRollout = 2 # This worker has both actor and rollout, it's a HybridEngine + Critic = 3 # This worker only has critic + RefPolicy = 4 # This worker only has reference policy + RewardModel = 5 # This worker only has reward model + ActorRolloutRef = 6 # This worker contains actor, rollout and reference policy simultaneously + +Step 2: Define the worker class corresponding to this role +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- We have pre-implemented the ``ActorRolloutRefWorker``. Through + different configs, it can be a standalone actor, a standalone rollout, + an ActorRollout HybridEngine, or an ActorRolloutRef HybridEngine. +- The ``TrainingWorker`` is the generic training worker used for + ``Critic`` and ``Reward Model`` roles. +- Backend selection (PyTorch FSDP/FSDP2, Megatron-LM, torchtitan, veomni, ...) + is driven by ``config.actor_rollout_ref.actor.strategy`` and + ``config.critic.strategy`` and handled internally by the model engine. + See `engine workers `_ + and the `model engine package `_ + for more information. + +Step 3: Define resource pool id and resource pool spec +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +- Resource pool is a division of global GPU resources, + ``resource_pool_spec`` is a dict, mapping from id to # of GPUs + + - In the above example, we defined a global resource pool: + global_pool_id, and then put all roles on this one resource pool + with all the GPUs in this post-training task. This refers to + *co-locate* placement where all the models share the same set of + GPUs. + +- See resource pool and placement for advance usage. + +Defining reward model/function +------------------------------ + +.. code:: python + + # we should adopt a multi-source reward function here + # - for rule-based rm, we directly call a reward score + # - for model-based rm, we call a model + # - for code related prompt, we send to a sandbox if there are test cases + # - finally, we combine all the rewards together + # - The reward type depends on the tag of the data + if config.reward_model.enable: + from verl.workers.engine_workers import TrainingWorker + role_worker_mapping[Role.RewardModel] = TrainingWorker + mapping[Role.RewardModel] = global_pool_id + + reward_fn = RewardManager(tokenizer=tokenizer, num_examine=0) + + # Note that we always use function-based RM for validation + val_reward_fn = RewardManager(tokenizer=tokenizer, num_examine=1) + + resource_pool_manager = ResourcePoolManager(resource_pool_spec=resource_pool_spec, mapping=mapping) + +Since not all tasks use model-based RM, users need to define here +whether it's a model-based RM or a function-based RM + +- If it's a model-based RM, directly add the ``RewardModel`` role in the + resource mapping and add it to the resource pool mapping. + + - The default ``TrainingWorker`` handles reward models through the unified + model engine and supports the typical huggingface + ``AutoModelForSequenceClassification`` layout. For custom reward models + you can subclass :class:`verl.workers.engine_workers.TrainingWorker` + or build a dedicated worker on top of the `model engine package + `_. + +- If it's a function-based RM, the users are required to classified the + reward function for each datasets. + +.. code:: python + + def _select_rm_score_fn(data_source): + if data_source == 'openai/gsm8k': + return gsm8k.compute_score + elif data_source == 'lighteval/MATH': + return math.compute_score + else: + raise NotImplementedError + +See reward functions implemented in `directory `_ +for more information. + +Define, init and run the PPO Trainer +------------------------------------ + +.. code:: python + + trainer = RayPPOTrainer(config=config, + tokenizer=tokenizer, + role_worker_mapping=role_worker_mapping, + resource_pool_manager=resource_pool_manager, + ray_worker_group_cls=ray_worker_group_cls, + reward_fn=reward_fn, + val_reward_fn=val_reward_fn) + trainer.init_workers() + trainer.fit() + +- We first initialize the ``RayPPOTrainer`` with user config, tokenizer + and all the above worker mapping, resource pool, worker group and + reward functions +- We first call the ``trainer.init_workers()`` to initialize the models + on the allocated GPUs (in the resource pool) +- The actual PPO training will be executed in ``trainer.fit()`` + +verl can be easily extended to other RL algorithms by reusing the Ray +model workers, resource pool and reward functions. See :doc:`extension<../advance/dpo_extension>` for +more information. + +Details of the ``RayPPOTrainer`` is discussed in :doc:`Ray Trainer<../workers/ray_trainer>`. diff --git a/verl_0720_main/verl/docs/examples/sandbox_fusion_example.rst b/verl_0720_main/verl/docs/examples/sandbox_fusion_example.rst new file mode 100644 index 0000000000000000000000000000000000000000..a141efdfd1e90346de2f8a3f8d8a90e784a4e445 --- /dev/null +++ b/verl_0720_main/verl/docs/examples/sandbox_fusion_example.rst @@ -0,0 +1,52 @@ +Sandbox Fusion Example +============================ + +Last updated: 05/17/2026. + +Introduction +------------ + +Sandbox Fusion is a remote code sandbox service that provides a secure environment for running and evaluating code generated by Large Language Models (LLMs). This example demonstrates how to train an LLM and use Sandbox Fusion to verify generated code, enhancing both security and performance. + +By leveraging a remote code sandbox service with greater CPU resources for concurrent code verification, you can reduce the reward stage time by 10-30%, depending on the quality of the generated code. + +Step 1: Prepare the Dataset +--------------------------- + +We use the Eurus-2-RL-Data dataset for training. This dataset combines math and code questions, making it suitable for LLM training tasks. You can download it from HuggingFace: `Eurus-2-RL-Data Dataset `_. + +Step 2: Set Up the Sandbox Fusion Service +----------------------------------------- + +Sandbox Fusion is a remote code sandbox service designed to securely run and evaluate LLM-generated code. To use it: + +1. **Access Full Documentation**: For detailed setup instructions, refer to the `Sandbox Fusion Documentation `_. +2. **Deploy the Service**: Choose one of the following deployment methods: + + - **Local Deployment**: Follow the guide `here `_. + - **FaaS Instance (Volcengine)**: Create an instance using the `Volcengine Documentation `_. + +After deployment, you will receive an API endpoint in the format: ``https:///run_code``. + +Step 3: Configure the Training Script +------------------------------------- + +To integrate Sandbox Fusion into your training script, configure the following parameters: + +**Key Settings for Sandbox Fusion** + +- ``reward_model.sandbox_fusion.url=''``: Enable Sandbox Fusion by specifying the API endpoint (must end with ``/run_code``). +- ``reward_model.sandbox_fusion.max_concurrent=256``: Set the maximum number of concurrent API requests to the Sandbox Fusion service. +- ``reward_model.sandbox_fusion.memory_limit_mb=1024``: Set the memory limit (in MB) for each sandbox instance. Defaults to 1024MB if not specified. + +**Additional Optimization** + +To further reduce code verification time, enable parallel processing with: + +- ``reward_model.reward_manager=prime``: The Prime reward manager verifies code across multiple subprocesses concurrently. + +**Example Notebook** + +For a practical implementation, refer to the example notebook: + +``examples/tutorial/agent_loop_get_started/agent_loop_tutorial.ipynb`` diff --git a/verl_0720_main/verl/docs/examples/skypilot_examples.rst b/verl_0720_main/verl/docs/examples/skypilot_examples.rst new file mode 100644 index 0000000000000000000000000000000000000000..3834f38842f4f0ff3f766160027c967de603133c --- /dev/null +++ b/verl_0720_main/verl/docs/examples/skypilot_examples.rst @@ -0,0 +1,146 @@ +SkyPilot Examples +================= + +Last updated: 09/04/2025. + +This guide provides examples of running VERL reinforcement learning training on Kubernetes clusters or cloud platforms with GPU nodes using `SkyPilot `_. + +Installation and Configuration +------------------------------- + +Step 1: Install SkyPilot +~~~~~~~~~~~~~~~~~~~~~~~~~ + +Choose the installation based on your target platform: + +.. code-block:: bash + + # For Kubernetes only + pip install "skypilot[kubernetes]" + + # For AWS + pip install "skypilot[aws]" + + # For Google Cloud Platform + pip install "skypilot[gcp]" + + # For Azure + pip install "skypilot[azure]" + + # For multiple platforms + pip install "skypilot[kubernetes,aws,gcp,azure]" + +Step 2: Configure Your Platform +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +See https://docs.skypilot.co/en/latest/getting-started/installation.html + +Step 3: Set Up Environment Variables +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Export necessary API keys for experiment tracking: + +.. code-block:: bash + + # For Weights & Biases tracking + export WANDB_API_KEY="your-wandb-api-key" + + # For HuggingFace gated models (if needed) + export HF_TOKEN="your-huggingface-token" + +Examples +-------- + +All example configurations are available in the `examples/tutorial/skypilot/ `_ directory on GitHub. See the `README `_ for additional details. + +PPO Training +~~~~~~~~~~~~ + +.. code-block:: bash + + sky launch -c verl-ppo verl-ppo.yaml --secret WANDB_API_KEY -y + +Runs PPO training on GSM8K dataset using Qwen2.5-0.5B-Instruct model across 2 nodes with H100 GPUs. Based on examples in ``examples/ppo_trainer/``. + +`View verl-ppo.yaml on GitHub `_ + +GRPO Training +~~~~~~~~~~~~~ + +.. code-block:: bash + + sky launch -c verl-grpo verl-grpo.yaml --secret WANDB_API_KEY -y + +Runs GRPO (Group Relative Policy Optimization) training on MATH dataset using Qwen2.5-7B-Instruct model. Memory-optimized configuration for 2 nodes. Based on examples in ``examples/grpo_trainer/``. + +`View verl-grpo.yaml on GitHub `_ + +Multi-turn Tool Usage Training +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + sky launch -c verl-multiturn verl-multiturn-tools.yaml \ + --secret WANDB_API_KEY --secret HF_TOKEN -y + +Single-node training with 8xH100 GPUs for multi-turn tool usage with Qwen2.5-3B-Instruct. Includes tool and interaction configurations for GSM8K. Based on examples in ``examples/sglang_multiturn/`` but uses vLLM instead of sglang. + +`View verl-multiturn-tools.yaml on GitHub `_ + +Configuration +------------- + +The example YAML files are pre-configured with: + +- **Infrastructure**: Kubernetes clusters (``infra: k8s``) - can be changed to ``infra: aws`` or ``infra: gcp``, etc. +- **Docker Image**: VERL's official Docker image with CUDA 12.6 support +- **Setup**: Automatically clones and installs VERL from source +- **Datasets**: Downloads required datasets during setup phase +- **Ray Cluster**: Configures distributed training across nodes +- **Logging**: Supports Weights & Biases via ``--secret WANDB_API_KEY`` +- **Models**: Supports gated HuggingFace models via ``--secret HF_TOKEN`` + +Launch Command Options +---------------------- + +- ``-c ``: Cluster name for managing the job +- ``--secret KEY``: Pass secrets for API keys (can be used multiple times) +- ``-y``: Skip confirmation prompt + +Monitoring Your Jobs +-------------------- + +Check Cluster Status +~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + sky status + +View Logs +~~~~~~~~~ + +.. code-block:: bash + + sky logs verl-ppo # View logs for the PPO job + +SSH into Head Node +~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + ssh verl-ppo + +Access Ray Dashboard +~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + sky status --endpoint 8265 verl-ppo # Get dashboard URL + +Stop a Cluster +~~~~~~~~~~~~~~ + +.. code-block:: bash + + sky down verl-ppo diff --git a/verl_0720_main/verl/docs/extend_guide.rst b/verl_0720_main/verl/docs/extend_guide.rst new file mode 100644 index 0000000000000000000000000000000000000000..37a8e5a84bb6916c7d169f16074805c5438d1d8a --- /dev/null +++ b/verl_0720_main/verl/docs/extend_guide.rst @@ -0,0 +1,273 @@ +How to Extend verl +=================== + +Last updated: 06/23/2026. + +Author: `Xibin Wu `_ + +RL Researcher +------------- + +How do I extend verl to support my own reward function? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +verl supports different types of reward functions: + +- Rule-based reward: math, code, etc with ground truth +- Discriminative reward model (DisRM) +- Generative reward model (GenRM) +- Hybrid reward: rule-based + GenRM/DisRM + +All types of reward functions are supported to be customized by user, for more details, see: :doc:`Reward Loop`. + +How do I extend verl to support my own tool calls? +++++++++++++++++++++++++++++++++++++++++++++++++++ + +verl provides a built-in ReAct agent loop implementation: `ToolAgentLoop `_. +ToolAgentLoop support two types of tool definitions: + +- Stateless function-based tool: decorate a function with ``@function_tool`` +- Stateful class-based tool: inherit from ``BaseTool`` and implement the ``execute`` method + +After defining your tools, you can set the tool agent loop in config: + +.. code:: bash + + actor_rollout_ref.rollout.agent.default_agent_loop=tool_agent + actor_rollout_ref.rollout.multi_turn.format=hermes # hermes,gpt-oss,qwen3_coder,etc. + actor_rollout_ref.rollout.multi_turn.function_tool_path=path/to/your_tools.py # function-based tool path + actor_rollout_ref.rollout.multi_turn.tool_config_path=path/to/your_tools.yaml # class-based tool path + +For more details, see: + +- :doc:`Multi-turn Rollout Support ` +- :doc:`Agent Loop ` +- `Train ReAct agent with code sandbox `_ + +ToolAgentLoop doesn't meet my requirements, how do I extend verl to support my own agent Loop? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +If ToolAgentLoop doesn't meet your requirements, you can customize your own agent loop by inheriting from ``AgentLoopBase`` and implementing the ``run`` method. + +.. warning:: It's user's responsibility to request LLM server in `TITO(token-in-token-out) `_, be careful to adhere to a golden rule: **never re-encode tokens you’ve decoded**. + +.. code:: python + + class MyAgentLoop(AgentLoopBase): + async def run(self, sampling_params: dict[str, Any], **kwargs) -> AgentLoopOutput: + """Run agent loop to interact with LLM server and environment. + + Args: + sampling_params (Dict[str, Any]): LLM sampling params. + **kwargs: dataset fields from `verl.utils.dataset.RLHFDataset`. + + Returns: + AgentLoopOutput: Agent loop output. + """ + ... + +After defining MyAgentLoop, you can set the agent loop class in config: + +.. code:: bash + + actor_rollout_ref.rollout.agent.agent_loop_config_path=path/to/your_agent.yaml + +For more details, see: :doc:`Agent Loop `. + +I'm doing async training, how do I customize my own replay buffer sampling strategy? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +In async training, the agent framework streams generated trajectories into ``TransferQueue``, and the +trainer uses `ReplayBuffer `_ to sample a batch from TransferQueue for training. + +While we provide a default sampling strategy, it's very common for users to want to customize it to meet their own needs. +To do so, inherit from ``ReplayBuffer`` and implement the ``sample`` method. + +.. code:: python + + class UserCustomReplayBuffer(ReplayBuffer): + def sample(self, global_steps: int, partition_id: str, batch_size: int) -> tuple[KVBatchMeta, dict]: + """Sample a batch of data from the replay buffer. + + Args: + global_steps (int): Global steps of the current training. + partition_id (str): Partition of TransferQueue, e.g. "train" or "val". + batch_size (int, optional): Batch size. + + Returns: + KVBatchMeta: A batch of data. + dict: Auxiliary metrics, e.g. off-policy staleness stats. + """ + ... + +After defining UserCustomReplayBuffer, you can set the custom sampler in config: + +.. code:: bash + + trainer.v1.sampler.custom_sampler.path = "path/to/your/sampler.py" + trainer.v1.sampler.custom_sampler.name = "UserCustomReplayBuffer" + +How do I customize sync/async trainer behavior? ++++++++++++++++++++++++++++++++++++++++++++++++ + +User may want to change the trainer's default behavior, for example: + +- over-sampling: sample more trajectories than the batch size +- dynamic filtering: filter out samples with group responses are all correct or incorrect + +verl `v1 PPO trainer `_ +provides a set of hooks to customize trainer behavior: + +- on_init_end +- on_train_begin +- on_train_end +- on_validate_begin +- on_validate_end +- on_step_begin +- on_step_end +- on_sample_begin +- on_sample_end + +These hooks are also used by the ``sync``, ``colocate_async``, and ``separate_async`` trainers to change model engine, LLM server, and checkpoint engine behavior. + +Agent Framework Developer +------------------------- + +How do I replace verl's AgentLoopManager with my own agent framework? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +AgentLoopManager is a reference implementation of an agent framework and is designed to be fully replaceable by other agent frameworks. +You can plug in your own agent framework, the only requirement is: + +- implement a non-blocking ``generate_sequences`` method +- put trajectory fields(e.g. ``prompt_ids``, ``response_ids``, ``response_mask``, ...) into ``TransferQueue`` once rollout finished + +.. code:: python + + class MyAgentLoopManager: + @classmethod + @auto_await + async def create( + cls, + config: DictConfig, + llm_client: LLMServerClient, + teacher_client: dict[str, LLMServerClient] = None, + reward_loop_worker_handles: list[ray.actor.ActorHandle] = None, + ): + """Create agent loop manager. + + Args: + config (DictConfig): whole config for main entrypoint. + llm_client (LLMServerClient): Client for the LLM server. + teacher_client (dict[str, LLMServerClient]): Client for multiple teacher servers. + reward_loop_worker_handles (List[ray.actor.ActorHandle]): Actor handles for streaming reward computation. + """ + ... + + def generate_sequences(self, prompts: TensorDict) -> None: + """Add batch of prompts to agent framework for rollout without blocking. Agent framework should put trajectory + fields(e.g. prompt_ids, response_ids, response_mask, ...) into TransferQueue once rollout finished. + + Args: + prompts (TensorDict): batch of prompts from train or validation dataset. + """ + ... + +After defining MyAgentLoopManager, you can set the agent loop manager class in config: + +.. code:: bash + + +actor_rollout_ref.rollout.agent.agent_loop_manager_class=my_package.module.MyAgentLoopManager + +I want to train my model with Claude code/Codex/Trae etc, how do I integrate these agent frameworks in blackbox? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +We have launched a sub-project: `verl-project/uni-agent `_, in which we provide an agent gateway: + +- **Message API**: Provide OpenAI ``/v1/chat/completions`` and Anthropic ``/v1/messages`` compatible API +- **Token-in-token-out**: encode ``user,tool`` messages into token ids and request LLM server, decode response ids and parsing tools into ``assistant`` messages +- **Trajectory tracking**: messages prefix matching, spawn a new trajectory if prefix changed +- **Session management**: multiple active sessions management + +For more details, see: + +- `Agent Gateway RFC `_ +- `Agent Gateway Implementation `_ + +Training/Inference Framework Developer +-------------------------------------- + +I'm an inference framework developer, how do I extend verl to support my own inference framework? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +verl provides an environment variable hook ``VERL_USE_EXTERNAL_MODULES`` to load external modules. You can define a register hook in your own module and set the environment variable to dynamically register your own modules. + +- ``RolloutReplica``: custom rollout replica class to define how to launch your own inference server. +- ``ServerAdapter``: custom server adapter class to define how to update weights with your own inference server. + +For example, this is how `verl-project/vexact `_ integrate with verl. vexact define a register hook in `register.py `_: + +.. code:: python + + def _load_vexact_replica(): + """Lazy loader for VeXactReplica to avoid circular imports.""" + from vexact.integrations.verl.async_server import VeXactReplica + + return VeXactReplica + + + # Register VeXact rollout replica (for server mode) + RolloutReplicaRegistry.register("vexact", _load_vexact_replica) + + # Register VeXact rollout base (for hybrid mode with device mesh) + _ROLLOUT_REGISTRY[("vexact", "async")] = "vexact.integrations.verl.rollout.ServerAdapter" + + +And user can set the environment variable to load vexact: + +.. code:: bash + + export VERL_USE_EXTERNAL_MODULES=vexact.integrations.verl.register + +I'm a training framework developer, how do I extend verl to support my own training framework? +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +verl provides a unified training engine abstraction: `BaseEngine `_. +With this abstraction, we provide native support for some popular training frameworks: + +- FSDP: FSDP1/2+SP +- Megatron: DP+TP+CP+EP+PP +- VeOmni: FSDP2+SP+EP +- TorchTitan: FSDP2+TP+CP+EP+PP +- Automodel: FSDP2+TP+CP+EP+PP + +For training framework developer who want to integrate with verl, you can inherit from ``BaseEngine`` and implement all the interfaces. +Then you can register your own training engine in verl with ``VERL_USE_EXTERNAL_MODULES`` same as inference framework. + +For example, this is how FlagOS integrate with verl. FlagOS define a register hook in `__init__.py `_: + +.. code:: python + + from verl_hardware_plugin.engines import register_all_engines + from verl_hardware_plugin.platforms import register_all_platforms + + register_all_platforms() + register_all_engines() + +And user can set the environment variable to load your own training framework: + +.. code:: bash + + export VERL_USE_EXTERNAL_MODULES=verl_hardware_plugin + +For more details, see: :doc:`Model Engine ` + +I'm a hardware vendor, how do I extend verl to support my own chip? ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + +verl provides native support for NVIDIA GPU, Huawei Ascend NPU, AMD GPU in the main branch, and provides a unified plugin system to support other hardware platforms. + +For more details, see: + +- :doc:`Multi-chip Support ` +- `verl-project/verl-hardware-plugin `_: external hardware plugin for MLU, XPU, MetaX, etc. diff --git a/verl_0720_main/verl/docs/faq/faq.rst b/verl_0720_main/verl/docs/faq/faq.rst new file mode 100644 index 0000000000000000000000000000000000000000..901d8077045371551b1b5ea01fdf2211047f9e4b --- /dev/null +++ b/verl_0720_main/verl/docs/faq/faq.rst @@ -0,0 +1,209 @@ +Frequently Asked Questions +==================================== + +Last updated: 09/24/2025. + +Ray related +------------ + +How to add breakpoint for debugging with distributed Ray? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Please checkout the official debugging guide from Ray: https://docs.ray.io/en/latest/ray-observability/ray-distributed-debugger.html + + +"Unable to register worker with raylet" +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +The cause of this issue is due to some system setting, e.g., SLURM added some constraints on how the CPUs are shared on a node. +While `ray.init()` tries to launch as many worker processes as the number of CPU cores of the machine, +some constraints of SLURM restricts the `core-workers` seeing the `raylet` process, leading to the problem. + +To fix this issue, you can set the config term ``ray_init.num_cpus`` to a number allowed by your system. + +Distributed training +------------------------ + +How to run multi-node post-training with Ray? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +You can start a ray cluster and submit a ray job, following the official guide from Ray: https://docs.ray.io/en/latest/ray-core/starting-ray.html + +Then in the configuration, set the ``trainer.nnode`` config to the number of machines for your job. + +How to use verl on a Slurm-managed cluster? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Ray provides users with `this `_ official +tutorial to start a Ray cluster on top of Slurm. We have verified the :doc:`GSM8K example<../examples/gsm8k_example>` +on a Slurm cluster under a multi-node setting with the following steps. + +1. [Optional] If your cluster support `Apptainer or Singularity `_ and you wish +to use it, convert verl's Docker image to an Apptainer image. Alternatively, set up the environment with the package +manager available on your cluster or use other container runtimes (e.g. through `Slurm's OCI support `_) available to you. + +.. code:: bash + + apptainer pull /your/dest/dir/vemlp-th2.4.0-cu124-vllm0.6.3-ray2.10-te1.7-v0.0.3.sif docker://verlai/verl:vemlp-th2.4.0-cu124-vllm0.6.3-ray2.10-te1.7-v0.0.3 + +2. Follow :doc:`GSM8K example<../examples/gsm8k_example>` to prepare the dataset and model checkpoints. + +3. Modify `examples/tutorial/slurm/ray_on_slurm.slurm `_ with your cluster's own information. + +4. Submit the job script to the Slurm cluster with `sbatch`. + +Please note that Slurm cluster setup may vary. If you encounter any issues, please refer to Ray's +`Slurm user guide `_ for common caveats. + +If you changed Slurm resource specifications, please make sure to update the environment variables in the job script if necessary. + + +Install related +------------------------ + +NotImplementedError: TensorDict does not support membership checks with the `in` keyword. +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Detail error information: + +.. code:: bash + + NotImplementedError: TensorDict does not support membership checks with the `in` keyword. If you want to check if a particular key is in your TensorDict, please use `key in tensordict.keys()` instead. + +Cause of the problem: There is no suitable version of tensordict package for the linux-arm64 platform. The confirmation method is as follows: + +.. code:: bash + + pip install tensordict==0.6.2 + +Output example: + +.. code:: bash + + ERROR: Could not find a version that satisfies the requirement tensordict==0.6.2 (from versions: 0.0.1a0, 0.0.1b0, 0.0.1rc0, 0.0.2a0, 0.0.2b0, 0.0.3, 0.1.0, 0.1.1, 0.1.2, 0.8.0, 0.8.1, 0.8.2, 0.8.3) + ERROR: No matching distribution found for tensordict==0.6.2 + +Solution 1st: + Install tensordict from source code: + +.. code:: bash + + pip uninstall tensordict + git clone https://github.com/pytorch/tensordict.git + cd tensordict/ + git checkout v0.6.2 + python setup.py develop + pip install -v -e . + +Solution 2nd: + Temperally modify the error takeplace codes: tensordict_var -> tensordict_var.keys() + + +Illegal memory access +--------------------------------- + +If you encounter the error message like ``CUDA error: an illegal memory access was encountered`` during rollout, please check the vLLM documentation for troubleshooting steps specific to your vLLM version. + +Checkpoints +------------------------ + +If you want to convert the model checkpoint into huggingface safetensor format, please refer to ``verl/model_merger``. + + +Triton ``compile_module_from_src`` error +------------------------------------------------ + +If you encounter triton compilation error similar to the stacktrace below, please set the ``use_torch_compile`` flag according to +https://verl.readthedocs.io/en/latest/examples/config.html to disable just-in-time compilation for fused kernels. + +.. code:: bash + + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/jit.py", line 345, in + return lambda *args, **kwargs: self.run(grid=grid, warmup=False, *args, **kwargs) + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/autotuner.py", line 338, in run + return self.fn.run(*args, **kwargs) + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/jit.py", line 607, in run + device = driver.active.get_current_device() + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/driver.py", line 23, in __getattr__ + self._initialize_obj() + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/driver.py", line 20, in _initialize_obj + self._obj = self._init_fn() + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/driver.py", line 9, in _create_driver + return actives[0]() + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/backends/nvidia/driver.py", line 371, in __init__ + self.utils = CudaUtils() # TODO: make static + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/backends/nvidia/driver.py", line 80, in __init__ + mod = compile_module_from_src(Path(os.path.join(dirname, "driver.c")).read_text(), "cuda_utils") + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/backends/nvidia/driver.py", line 57, in compile_module_from_src + so = _build(name, src_path, tmpdir, library_dirs(), include_dir, libraries) + File "/data/lbh/conda_envs/verl/lib/python3.10/site-packages/triton/runtime/build.py", line 48, in _build + ret = subprocess.check_call(cc_cmd) + File "/data/lbh/conda_envs/verl/lib/python3.10/subprocess.py", line 369, in check_call + raise CalledProcessError(retcode, cmd) + +What is the meaning of train batch size, mini batch size, and micro batch size? +------------------------------------------------------------------------------------------ + +This figure illustrates the relationship between different batch size configurations. + +https://excalidraw.com/#json=pfhkRmiLm1jnnRli9VFhb,Ut4E8peALlgAUpr7E5pPCA + +.. image:: https://github.com/user-attachments/assets/16aebad1-0da6-4eb3-806d-54a74e712c2d + +How to generate ray timeline to analyse performance of a training job? +------------------------------------------------------------------------------------------ + +To generate the ray timeline file, you can set the config term ``ray_init.timeline_json_file`` to a json file path. +For example: + +.. code:: bash + + ray_init.timeline_json_file=/tmp/ray_timeline.json + +The file will be generated in the specified path at the end of a training job. +You can use tools like chrome://tracing or the Perfetto UI and view the ray timeline file. + +This figure shows the ray timeline file generated by from a training job on 1 node with 4 GPUs + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray_timeline.png?raw=true + +How to set proxy only for wandb? +------------------------------------------------------------------------------------------ + +If you need a proxy to access wandb, you can add below config in your training job script. +Comparing to using global https_proxy env variable, this approach won't mess up other http requests, such as ChatCompletionScheduler. + +.. code:: bash + + +trainer.wandb_proxy=http:// + +Missmatch between inference and training sequence (high actor/grad_norm) +------------------------------------------------------------------------------------------ + +If you encounter the issue of actor/grad_norm metric continuously increasing during training, it might be caused by a significant precision mismatching between the inference engine and training. You can use the following parameter to confirm this: + +.. code:: bash + + actor_rollout_ref.rollout.calculate_log_probs=True + +This parameter will add metrics like training/rollout_probs_diff_mean , which can be used to verify if there is a precision difference between inference and training. + +Under normal circumstances, the value of training/rollout_probs_diff_mean should be below 0.005. If you observe this value to be higher than 0.01, it indicates a precision issue from the inference engine. +The precision issue is known to occur under the following conditions: + +1. Using non-Hopper architecture GPUs, such as A100, L20, B200, etc. + +2. Using vLLM `with issue 22103 `_ as the inference engine. + +3. The input and output texts are long, for example, in multi-turn scenarios using reasioning models like Qwen3 for RL training. + +If all three conditions above are met and you observe that rollout_probs_diff_mean is too high, it is recommended to add the following parameter to resolve the precision issue: + +.. code:: bash + + +actor_rollout_ref.rollout.engine_kwargs.vllm.disable_cascade_attn=True + +The root cause of this issue is a bug in the flash attention used by vLLM. Although it has been fixed, the fix has not yet been released in the latest version of vLLM (v0.10.2). +For a more detailed explanation of this issue, please refer to `Fix LSE output error in FA2 kv-split `_. + +Until vLLM releases a new version with this fix, it is recommended to use the configuration above to disable cascade attention as a workaround. diff --git a/verl_0720_main/verl/docs/hardware/multi_chip_support.rst b/verl_0720_main/verl/docs/hardware/multi_chip_support.rst new file mode 100644 index 0000000000000000000000000000000000000000..e037d2ed7e98ca6c47e8ba9b0161cd4ca3f487f0 --- /dev/null +++ b/verl_0720_main/verl/docs/hardware/multi_chip_support.rst @@ -0,0 +1,175 @@ +Multi-Chip Support +================== + +Last updated: 06/03/2026. + +Overview +-------- + +verl supports RL training across multiple hardware platforms through a unified +plugin system. The architecture consists of two main subsystems: + +1. **Platform Plugin System** (``verl.plugin.platform``) — A hardware + abstraction layer with auto-detection and a unified device API. +2. **Engine Plugin System** (``verl.workers.engine.base``) — Training engine + extensions that add chip-specific optimizations on top of existing + FSDP/Megatron engines. + +Hardware Support +---------------- + +**Built-in (verl core):** + +- NVIDIA GPU (CUDA) +- Huawei Ascend NPU + +**Via verl-hardware-plugin (reference implementations):** + +Other hardware platforms are supported through the external +`verl-hardware-plugin `_ +package, which provides reference implementations for vendors to adapt: + +- Intel XPU (Data Center GPU Max / Arc) +- Cambricon MLU (MLU370 / MLU590) +- MetaX (CUDA-compatible) + +.. note:: + + The implementations in verl-hardware-plugin are **examples only**. Full + production support requires collaboration with the respective hardware + vendors. Vendors can use these as templates to build and maintain their own + plugins. + +Design Principles +----------------- + +1. **Plugin Architecture**: Platform backends and engine extensions register + via decorator-based registries (``PlatformRegistry``, ``EngineRegistry``), + requiring no modifications to verl core code. + +2. **Auto-Detection + Manual Override**: The platform auto-detects hardware + type by probing ``is_available(use_smi_check=True)`` on each registered + platform. Can be explicitly overridden via the ``VERL_PLATFORM`` environment + variable. + +3. **Two-Dimensional Engine Lookup**: Engines register with both ``device`` + (torch device type) and ``vendor`` (hardware vendor). Lookup priority: + + - Exact match ``(device, vendor)`` — vendor-specific engine + - Fallback to device-only key — base engine for that device type + - For CUDA-compatible devices, fallback to base CUDA engine + +4. **Backward Compatibility**: The legacy ``verl.utils.device`` API is + preserved as a thin wrapper over the platform plugin system. Existing code + continues to work without modification. + +Architecture Overview +--------------------- + +:: + + +-------------------------------------------------------------------+ + | verl Multi-Chip Architecture | + +-------------------------------------------------------------------+ + | | + | +---------------------------------------------------------+ | + | | Platform Plugin System | | + | | (verl.plugin.platform) | | + | | | | + | | PlatformRegistry | | + | | ├─ "nvidia" → PlatformCUDA (built-in) | | + | | ├─ "huawei" → PlatformNPU (built-in) | | + | | ├─ "intel" → PlatformXPU (plugin) | | + | | ├─ "cambricon" → PlatformMLU (plugin) | | + | | └─ "metax" → PlatformMetaX (plugin) | | + | | | | + | +---------------------------------------------------------+ | + | | + | +---------------------------------------------------------+ | + | | Engine Plugin System | | + | | (verl.workers.engine.base) | | + | | | | + | | EngineRegistry (device, vendor) → Engine class | | + | | | | | + | | +-- ("cuda", None) → FSDPEngineWithLMHead | | + | | +-- ("npu", None) → FSDPNPUEngineWithLMHead | | + | | +-- ("cuda", "metax") → FSDPMetaXEngineWithLMHead | | + | | +-- ("xpu", "intel") → FSDPXPUEngineWithLMHead | | + | | +-- ("mlu","cambricon")→ FSDPMLUEngineWithLMHead | | + | | | | + | +---------------------------------------------------------+ | + | | + +-------------------------------------------------------------------+ + +Plugin Loading +-------------- + +verl discovers plugins through two mechanisms: + +1. **setuptools entry_points** (``verl.plugins`` group) — standard Python + packaging mechanism. After ``pip install``, the plugin is auto-discovered. + +2. **``VERL_USE_EXTERNAL_MODULES``** environment variable — for development + or non-packaged plugins: + + .. code-block:: bash + + export VERL_USE_EXTERNAL_MODULES=verl_hardware_plugin + +Platform Registration +--------------------- + +Each platform class registers via decorator: + +.. code-block:: python + + @PlatformRegistry.register(platform="my_vendor") + class PlatformMyDevice(PlatformBase): + @property + def device_name(self) -> str: + return "my_device" # torch device type + + @property + def vendor_name(self) -> str: + return "my_vendor" # used for engine lookup + +Platform selection priority: + +1. ``VERL_PLATFORM`` environment variable (explicit override) +2. Auto-detection via ``is_available(use_smi_check=True)`` +3. Fallback to ``"nvidia"`` + +Engine Registration +------------------- + +Engine classes register with device and vendor: + +.. code-block:: python + + @EngineRegistry.register( + model_type="language_model", + backend=["fsdp", "fsdp2"], + device="cuda", # torch device type + vendor="my_vendor", # vendor name + ) + class FSDPMyVendorEngineWithLMHead(FSDPEngineWithLMHead): + def initialize(self): + super().initialize() + # vendor-specific initialization + +Engine lookup calls ``get_device_name()`` and ``get_vendor()`` from the active +platform, then resolves the engine by ``(device_name, vendor_name)`` key. + +Environment variable overrides for engine selection: + +- ``VERL_ENGINE_DEVICE`` — override detected device name +- ``VERL_ENGINE_VENDOR`` — override detected vendor name + +Adding New Hardware +------------------- + +For a step-by-step guide on adding support for a new hardware platform, see +the `verl-hardware-plugin Development Guide `_. + +The core platform and engine registry mechanism is implemented in +`PR #6086 `_. diff --git a/verl_0720_main/verl/docs/hybrid_flow.rst b/verl_0720_main/verl/docs/hybrid_flow.rst new file mode 100644 index 0000000000000000000000000000000000000000..1bd1918eba9a3aafd069acdb21c64bc400199b1b --- /dev/null +++ b/verl_0720_main/verl/docs/hybrid_flow.rst @@ -0,0 +1,259 @@ +========================================================= +HybridFlow Programming Guide +========================================================= + +Last updated: 06/02/2025. + +.. _vermouth: https://github.com/vermouth1992 + +Author: `Chi Zhang `_ + +verl is an open source implementation of the paper `HybridFlow `_ [1]_. In this section, we will introduce the basic concepts of HybridFlow, the motivation and how to program with verl APIs. + +Motivation and Design +------------------------ +We use dataflow to represent RL systems. [4]_. + +DataFlow +~~~~~~~~~~~~~~~~~~~~ + +Dataflow is an abstraction of computations. Neural Network training is a typical dataflow. It can be represented by computational graph. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/dataflow.jpeg?raw=true + :alt: The dataflow graph from CS231n 2024 lecture 4 + +This figure [2]_ represents the computation graph of a polynomial function followed by a sigmoid function. In the data flow of neural network computation, each node represents an operator, and each edge represents the direction of forward/backward propagation. The computation graph determines the architecture of the neural network. + +RL as a dataflow problem +++++++++++++++++++++++++++++++++++++++++++++++ + +Reinforcement learning (RL) training can also be represented as a dataflow. Below is the dataflow graph that represents the PPO algorithm used in RLHF [3]_: + +.. image:: https://picx.zhimg.com/70/v2-cb8ab5ee946a105aab6a563e92682ffa_1440w.avis?source=172ae18b&biz_tag=Post + :alt: PPO dataflow graph, credit to Zhihu 低级炼丹师 + +However, the dataflow of RL has fundamental differences compared with dataflow of neural network training as follows: + ++--------------------------+--------------------------------------------------+---------------------+ +| Workload | Node | Edge | ++--------------------------+--------------------------------------------------+---------------------+ +| Neural Network Training | Operator (+/-/matmul/softmax) | Tensor movement | ++--------------------------+--------------------------------------------------+---------------------+ +| Reinforcement Learning | High-level operators (rollout/model forward) | Data Movement | ++--------------------------+--------------------------------------------------+---------------------+ + +In the case of tabular reinforcement learning, each operator is a simple scalar math operation (e.g., bellman update). In deep reinforcement learning(DRL), each operator is a high-level neural network computation such as model inference/update. This makes RL a two-level dataflow problem: + +- Control flow: defines how the high-level operators are executed (e.g., In PPO, we first perform rollout. Then, we perform advantage computation. Finally, we perform training). It expresses the **core logics of RL algorithms**. +- Computation flow: defines the dataflow of **neural network computation** (e.g., model forward/backward/optimizer). + + +Design Choices +~~~~~~~~~~~~~~~~~~~~ +The model size used in DRL before the LLM era is typically small. Thus, the high-level neural network computation can be done in a single process. This enables embedding the computation flow inside the control flow as a single process. + +However, in the LLM era, the computation flow (e.g., training neural network) becomes a multi-process program. This naturally leads to two design choices: + +1. Convert the control flow into a multi-process program as well. Then colocate with computation flow (unified multi-controller) + +- Advantages: + + - Achieves the **optimal performance** under fixed computation flow and control flow as the communication overhead in both training and data transfer is minimized. + +- Disadvantages: + + - The computation and/or control flow is **hard to reuse** from software perspective as computation code is coupled with specific controller code. For example, the training loop of PPO is generic. Say we have an PPO training flow implemented with a specific computation flow such as FSDP. Neither the control flow or computation flow can be reused if we want to switch the computation flow from FSDP to Megatron, due to the coupling of control and computation flows. + - Requires more efforts from the user under flexible and dynamic control flows, due to the multi-process nature of the program. + +2. Separate the flows: single process for the control flow and multi-process for computation flow + +- Advantages: + + - The computation flow defined elsewhere can be **easily reused** after the decoupling. + - The controller runs on a single process. Implementing a new RL algorithm with a **different control flow is simple and easy**. + +- Disadvantages: + + - Additional **data communication overhead** each time the controller process and computatation processes interact. The data has to be sent back and forth. + +In verl, the latter strategy with separate control flow and computation flow is adopted. verl is designed to decouple the control flow of RL algorithms, and the implementation of computation engines. + +Overall Execution Diagram +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Below is a simplified diagram denoting the execution of a reinforcement learning job. In the diagram, the controller runs on a single process, while the generator/actor workers, critic workers run on multiple processes, placed with specific resource groups. For rollout, the controller passes the data to the generator to perform sample generation. When the rollout is done, the data is passed back to controller for the next step of the algorithm. Similar execution is done for other workers. With the hybrid controller design, the data flow and computation is decoupled to provide both efficiency in computation and flexibility in defining algorithm training loops. + +.. figure:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/driver_worker.png?raw=true + :alt: The execution diagram + +Codebase walkthrough (PPO) +------------------------------------------------ + +Entry function +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Code: https://github.com/verl-project/verl/blob/main/verl/trainer/main_ppo.py + +In this file, we define a remote function `main_task` that serves as the controller (driver) process as shown in the above figure. We also define a ``RewardManager``, where users can customize their reward function based on the data source in the dataset. Note that `RewardManager` should return the final token-level reward that is optimized by RL algorithms. Note that users can combine model-based rewards and rule-based rewards. +The ``main_task`` constructs a RayPPOTrainer instance and launch the fit. Note that ``main_task`` **runs as a single process**. + +We highly recommend that the ``main_task`` is NOT scheduled on the head of the ray cluster because ``main_task`` will consume a lot of memory but the head usually contains very few resources. + +Ray trainer +~~~~~~~~~~~~~~~~~~~~ +Code: https://github.com/verl-project/verl/blob/main/verl/trainer/ppo/ray_trainer.py + +The RayPPOTrainer manages + +- Worker and WorkerGroup construction +- Runs the main loop of PPO algorithm + +Note that, the fit function of RayPPOTrainer **runs as a single process**. + +Worker and WorkerGroup construction +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +Each workerGroup manages a list of workers that runs remotely. Note that the worker group runs in the process of its constructor. +Each worker inside the WorkerGroup runs on a GPU. The worker group serves as a proxy for the controller process to interact with a list of workers, in order to perform certain computations. **In order to do so, we have to bind the methods of the worker into the method of the WorkerGroup and define the data dispatch and data collection**. This is done via simple decoration that will be introduced in the Worker definition section. + +For example, in PPO, we define 3 worker groups: + +- ActorRolloutRef: manages actor, rollout and reference policy. ActorRolloutRefWorker can be instantiated as a single actor, a single rollout, a single reference policy, a combined actor/rollout or a combined actor/rollout/ref. This design is aimed for the maximum code reuse in various scenarios. The reason for colocating actor and rollout is for fast weight transfer using nccl. The reason for coloating actor and reference is to implement an efficient lora PPO as the reference policy is simply the base model of PPO in lora. The colocation is done via ``verl.single_controller.ray.base.create_colocated_worker_cls``, where it creates a single ray remote class exposing all class methods from these roles. +- Critic: manages the critic model +- Reward: manages the reward model + +The worker group will be constructed on the resource pool it designates. The resource pool is a set of GPUs in the ray cluster. + +Worker definition +~~~~~~~~~~~~~~~~~~~~ + +.. _ActorRolloutRefWorker: https://github.com/verl-project/verl/blob/main/verl/workers/engine_workers.py + +We take `ActorRolloutRefWorker `_ for an example. +The APIs it should expose to the controller process are: + +- init_model: build the underlying model +- generate_sequences: given prompts, generate responses +- compute_log_prob: compute the log-probability of a generated sequence using actor +- compute_ref_log_prob: compute the log-probability of a generated sequence using reference policy +- save_checkpoint: save the checkpoint + +Note that these methods are defined in the worker that can only be invoked via remote calls. For example, if the controller process wants to initialize the model, it has to call + +.. code-block:: python + + for worker in actor_rollout_ref_wg: + worker.init_model.remote() + +If the controller process wants to generate sequences, it has to call + +.. code-block:: python + + data = xxx + # split the data into dp chunks + data_dp_lst = data.split(dp_size) + output_dp_lst = [] + for i, worker in enumerate(actor_rollout_ref_wg): + output_future = worker.generate_sequences.remote(data_dp_lst[i]) + output_dp_lst.append(output_future) + output = torch.cat(ray.get(output_dp_lst), dim=0) + +We observe that controller process calling worker group methods in general can be divided into 3 parts: + +- Split the data into data parallel sizes +- Dispatch the corresponding data into each worker +- Collect and concatenate the data when the computation finishes + +In verl, we design a syntax sugar to encapsulate the 3 processes into a single call from the controller process. + +.. code-block:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def generate_sequences(data): + ... + + # on the driver + output = actor_rollout_ref_wg.generate_sequences(data) + +We decorate the method of the worker with a ``register`` that explicitly defines how the input data should be split and dispatched to each worker, and how the output data should be collected and concatenated by the controller. For example, ``Dispatch.DP_COMPUTE_PROTO`` splits the input data into dp chunks, dispatch each data to each worker, collect the output and concatenate the results. Note that this function requires the input and output to be a DataProto defined here (https://github.com/verl-project/verl/blob/main/verl/protocol.py). + + +PPO main loop +~~~~~~~~~~~~~~~~~~~~ +With the aforementioned APIs, we can implement the main loop of PPO as if it is a single process program + +.. code-block:: python + + for prompt in dataloader: + output = actor_rollout_ref_wg.generate_sequences(prompt) + old_log_prob = actor_rollout_ref_wg.compute_log_prob(output) + ref_log_prob = actor_rollout_ref_wg.compute_ref_log_prob(output) + values = critic_wg.compute_values(output) + rewards = reward_wg.compute_scores(output) + # compute_advantages is running directly on the control process + advantages = compute_advantages(values, rewards) + output = output.union(old_log_prob) + output = output.union(ref_log_prob) + output = output.union(values) + output = output.union(rewards) + output = output.union(advantages) + # update actor + actor_rollout_ref_wg.update_actor(output) + critic.update_critic(output) + +Takeaways +~~~~~~~~~~~~~~~~~~~~ +- This programming paradigm enables users to use different computation backend without modification of the control process. +- This programming paradigm enables flexible placement (by changing the mapping of WorkerGroup and ResourcePool) without modification of the control process. + +Repository organization +------------------------------------------------ + +Important code files in the repository are organized as below: + +.. code-block:: bash + + verl # the verl package + trainer + main_ppo.py # the entrypoint for RL training + ppo + ray_trainer.py # the training loop for RL algorithms such as PPO + sft_trainer.py # the SFT trainer with FSDP backend + config + generation.yaml # configuration template for rollout + ppo_trainer.yaml # configuration template for the RL trainer + workers + protocol.py # the interface of DataProto + engine_workers.py # unified ActorRolloutRefWorker / TrainingWorker implementations + engine + fsdp # FSDP / FSDP2 actor+critic engine implementations + megatron # Megatron-LM actor+critic engine implementations + torchtitan # torchtitan engine implementation + veomni # veomni engine implementation + mcore # shared mcore helpers + rollout + vllm + vllm_rollout.py # rollout with vllm backend + hf_rollout.py # rollout with huggingface TGI backend + sglang_rollout # rollout with SGLang backend + utils + dataset # datasets for SFT/RM/RL + reward_score # function based reward + gsm8k.py # reward function for gsm8k dataset + math.py # reward function for math dataset + seqlen_balancing.py # the sequence balance optimization + models + llama # Megatron implementation for llama, deepseek, mistral, etc + transformers # ulysses integration with transformer models such as llama, qwen, etc + weight_loader_registery.py # registry of weight loaders for loading hf ckpt into Megatron + third_party + vllm # adaptor for vllm's usage in RL + vllm_spmd # vllm >= v0.7 adaptor + examples # example scripts + tests # integration and unit tests + .github # the configuration of continuous integration tests + + +.. [1] HybridFlow: A Flexible and Efficient RLHF Framework: https://arxiv.org/abs/2409.19256v2 +.. [2] Data flow graph credit to CS231n 2024 lecture 4: https://cs231n.stanford.edu/slides/2024/lecture_4.pdf +.. [3] PPO dataflow graph credit to 低级炼丹师 from Zhihu​: https://zhuanlan.zhihu.com/p/635757674 +.. [4] RLFlow diff --git a/verl_0720_main/verl/docs/index.rst b/verl_0720_main/verl/docs/index.rst new file mode 100644 index 0000000000000000000000000000000000000000..d6322cd5be9270601a4ea16df7b6acdf424a234b --- /dev/null +++ b/verl_0720_main/verl/docs/index.rst @@ -0,0 +1,237 @@ +Welcome to verl's documentation! +================================================ + +verl is a flexible, efficient and production-ready RL training framework designed for large language models (LLMs) post-training. It is an open source implementation of the `HybridFlow `_ paper. + +verl is flexible and easy to use with: + +- **Easy extension of diverse RL algorithms**: The hybrid programming model combines the strengths of single-controller and multi-controller paradigms to enable flexible representation and efficient execution of complex Post-Training dataflows. Allowing users to build RL dataflows in a few lines of code. + +- **Seamless integration of existing LLM infra with modular APIs**: Decouples computation and data dependencies, enabling seamless integration with existing LLM frameworks, such as PyTorch FSDP, Megatron-LM, vLLM and SGLang. Moreover, users can easily extend to other LLM training and inference frameworks. + +- **Flexible device mapping and parallelism**: Supports various placement of models onto different sets of GPUs for efficient resource utilization and scalability across different cluster sizes. + +- Ready integration with popular HuggingFace models + + +verl is fast with: + +- **State-of-the-art throughput**: By seamlessly integrating existing SOTA LLM training and inference frameworks, verl achieves high generation and training throughput. + +- **Efficient actor model resharding with 3D-HybridEngine**: Eliminates memory redundancy and significantly reduces communication overhead during transitions between training and generation phases. + +-------------------------------------------- + +.. _Contents: + +.. toctree:: + :maxdepth: 2 + :caption: Quickstart + + start/install + start/quickstart + start/multinode + start/ray_debug_tutorial + start/more_resources + start/agentic_rl + +.. toctree:: + :maxdepth: 2 + :caption: Programming guide + + extend_guide + hybrid_flow + single_controller + +.. toctree:: + :maxdepth: 1 + :caption: Data Preparation + + preparation/prepare_data + preparation/reward_function + +.. toctree:: + :maxdepth: 2 + :caption: Configurations + + examples/config + +.. toctree:: + :maxdepth: 1 + :caption: PPO Example + + examples/ppo_code_architecture + examples/gsm8k_example + examples/megatron_fsdp_example + examples/multi_modal_example + examples/skypilot_examples + +.. toctree:: + :maxdepth: 1 + :caption: Algorithms + + algo/ppo.md + algo/grpo.md + algo/dapo.md + algo/spin.md + algo/sppo.md + algo/entropy.md + algo/opo.md + algo/baseline.md + algo/gpg.md + algo/rollout_corr.md + algo/rollout_corr_math.md + algo/otb.md + algo/dppo.md + algo/opd.md + +.. toctree:: + :maxdepth: 1 + :caption: PPO Trainer and Workers + + workers/ray_trainer + workers/model_engine + workers/engine_workers + workers/automodel_workers + workers/torchtitan_workers + workers/sglang_worker + workers/trtllm_worker + +.. toctree:: + :maxdepth: 1 + :caption: Performance Tuning Guide + + perf/dpsk.md + perf/best_practices + perf/perf_tuning + perf/rollout_kv_offload.md + README_vllm0.8.md + perf/device_tuning + perf/verl_profiler_system.md + perf/nsight_profiling.md + perf/torch_profiling.md + +.. toctree:: + :maxdepth: 1 + :caption: Adding new models + + advance/fsdp_extension + advance/megatron_extension + advance/deepseek_v4_integration + advance/megatron_lite_backend + +.. toctree:: + :maxdepth: 1 + :caption: Async Training + + advance/one_step_off + advance/delta_weight_sync + advance/fully_async + advance/async-on-policy-distill + advance/dynamic_schedule + +.. toctree:: + :maxdepth: 1 + :caption: Low Precision + + low_precision/fp8.md + low_precision/nvfp4_qat.md + +.. toctree:: + :maxdepth: 1 + :caption: Advanced Features + + advance/checkpoint + advance/rope + advance/attention_implementation + advance/ppo_lora.rst + sglang_multiturn/multiturn.rst + advance/placement + advance/dpo_extension + examples/sandbox_fusion_example + advance/rollout_trace.rst + advance/rl_insight.md + advance/skip_manager.rst + advance/agent_loop + advance/reward_loop + data/transfer_queue.md + advance/grafana_prometheus.md + advance/mtp.md + advance/determinism.md + +.. toctree:: + :maxdepth: 1 + :caption: Hardware Support + + hardware/multi_chip_support + amd_tutorial/index.rst + ascend_tutorial/index.rst + +.. toctree:: + :maxdepth: 1 + :caption: API References + + api/data + api/single_controller.rst + api/trainer.rst + api/utils.rst + +.. toctree:: + :maxdepth: 1 + :caption: Blog + + blog/v0.7.md + +.. toctree:: + :maxdepth: 2 + :caption: FAQ + + faq/faq + +.. toctree:: + :maxdepth: 1 + :caption: Contributing + + contributing/editing-agent-instructions.md + +.. toctree:: + :maxdepth: 1 + :caption: Development Notes + + sglang_multiturn/sandbox_fusion.rst + +Contribution +------------- + +verl is free software; you can redistribute it and/or modify it under the terms +of the Apache License 2.0. We welcome contributions. +Join us on `GitHub `_, `Slack `_ and `Wechat `_ for discussions. + +Contributions from the community are welcome! Please check out our `project roadmap `_ and `good first issues `_ to see where you can contribute. + +Code Linting and Formatting +^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +We use pre-commit to help improve code quality. To initialize pre-commit, run: + +.. code-block:: bash + + pip install pre-commit + pre-commit install + +To resolve CI errors locally, you can also manually run pre-commit by: + +.. code-block:: bash + + pre-commit run + +Adding CI tests +^^^^^^^^^^^^^^^^^^^^^^^^ + +If possible, please add CI test(s) for your new feature: + +1. Find the most relevant workflow yml file, which usually corresponds to a ``hydra`` default config (e.g. ``ppo_trainer``, ``ppo_megatron_trainer``, ``sft_trainer``, etc). +2. Add related path patterns to the ``paths`` section if not already included. +3. Minimize the workload of the test script(s) (see existing scripts for examples). + +We are HIRING! Send us an `email `_ if you are interested in internship/FTE opportunities in MLSys/LLM reasoning/multimodal alignment. diff --git a/verl_0720_main/verl/docs/low_precision/fp8.md b/verl_0720_main/verl/docs/low_precision/fp8.md new file mode 100644 index 0000000000000000000000000000000000000000..cd03102e048a31bd4707e347d49990062e1743e6 --- /dev/null +++ b/verl_0720_main/verl/docs/low_precision/fp8.md @@ -0,0 +1,230 @@ +# FP8 RL in verl + +Last updated: 03/05/2026 + +verl supports two FP8 modes for accelerating RL training: + +| Mode | Training Precision | Rollout Precision | +|------|-------------------|-------------------| +| **FP8 Rollout Only** | BF16 | FP8 | +| **FP8 End-to-End** | FP8 (Megatron) | FP8 (vLLM) | + +> [!TIP] +> For ready-to-run scripts, see the [low-precision recipe directory](https://github.com/verl-project/verl-recipe/low_precision). + +--- + +## FP8 Rollout Only + +FP8 rollout-only mode keeps training in BF16 and quantizes rollout inference to FP8. This reduces GPU memory during generation and speeds up rollout without affecting training precision. + +### Implementation + +We monkey patch several vLLM functions to enable FP8 rollout for reinforcement learning: + +1. **Quantize weights**: Quantize model weights on-the-fly from higher-precision formats to FP8. +2. **Process weights after loading**: For vLLM, we replace the `vllm.model_executor.layers.quantization.fp8.Fp8LinearMethod.process_weights_after_loading` function to handle weight processing after quantization. For SGLang, this patch is not needed as it natively supports loading quantized weights. + +### Support Matrix + +- FP8 blockwise quantization for rollout + - Used in Deepseek, which is 1x128 quantization for activations and 128x128 quantization for model weights +- Dense models and MoE models +- Async rollout interfaces +- vLLM 0.10.x & vLLM 0.11 & vLLM 0.12 & SGLang 0.5.5 +- FSDP and Megatron training backends + +### Usage + +Enable in config file: + +```yaml +rollout: + quantization: "fp8" +``` + +Or via command line: + +```bash +actor_rollout_ref.rollout.quantization=fp8 +``` + +#### Skipping layers in SGLang FP8 rollout + +When using SGLang FP8 rollout, you can skip FP8 weight quantization for +selected modules. Skipped modules stay in the rollout model dtype instead +of being converted to FP8. This is useful for layers that are not +compatible with block-wise FP8 weight quantization, or for modules that +you prefer to keep in higher precision. + +Set `SGLANG_FP8_IGNORED_LAYERS` before starting training: + +```bash +SGLANG_FP8_IGNORED_LAYERS=linear_attn \ +python3 -m verl.trainer.main_ppo \ + actor_rollout_ref.rollout.name=sglang \ + actor_rollout_ref.rollout.quantization=fp8 \ + ... +``` + +Multiple entries can be separated by commas: + +```bash +SGLANG_FP8_IGNORED_LAYERS=linear_attn,visual +``` + +You can also use the model `quantization_config`: + +```json +{ + "quantization_config": { + "ignored_layers": ["re:.*linear_attn.*"] + } +} +``` + +Plain module names, full module paths, and `re:` regex patterns are +supported. verl applies the same ignored-layer rules when launching +SGLang and when syncing updated actor weights into the rollout engine. + +### Experiments and Outcomes + +#### Qwen3-8B-Base Dense Model + +**Configuration** +- DAPO recipe. AIME24 online validation. +- vLLM(FP8 spmd rollout) + FSDP + - Note that SPMD rollout has been deprecated, so we removed the FP8 SPMD rollout. +- Prompt batch size 32, n=16. +- Rollout batch size: 32\*3*16 +- Train_batch_size & ppo_mini_batch_size 32 +- Max response length 20K +- Token-level TIS, C=2 +- 8*H100 +- vLLM 0.10.0+CUDA 12.6 vs vLLM 0.11.0+CUDA 12.9 + +**Accuracy** +![Qwen3-8b-base_fp8_acc]( +https://github.com/Agoniii/verl/blob/xueh/fp8_pr_images/docs/advance/images/Qwen3-8b-base_fp8_acc.png?raw=true) +*dark green: BF16, orange: FP8 rollout + token-level TIS, light green: FP8 rollout without TIS* + +Results and observations: +- With TIS, FP8 rollout aligns with BF16 +- Obvious accuracy drop when TIS is not enabled +- Higher mismatch kl but within acceptable range throughout the training + + +**Performance** + +![Qwen3-8b-base_fp8_rollout_perf]( +https://github.com/Agoniii/verl/blob/xueh/fp8_pr_images/docs/advance/images/Qwen3-8b-base_fp8_rollout_perf.png?raw=true) +*green: BF16, orange: FP8 rollout + CUDA12.6 + DeepGemm, purple: FP8 rollout + CUDA 12.9 + DeepGemm* + +Results and observations: +- FP8 rollout leads to around ~12% rollout speedup with CUDA 12.6 + DeepGemm +- When upgrading to CUDA 12.9, speedup can be up to ~18% + +#### Qwen3-30B-A3B-Base MoE Model + +**Configuration** +- DAPO recipe. AIME24 online validation. +- FP8 async rollout, vLLM+FSDP +- Prompt batch size 32 +- Rollout batch size: 32\*3*16 +- Train_batch_size & ppo_mini_batch_size 32 +- Max response length 20K +- Token-level TIS, C=2 +- 2\*8*H100 +- vLLM 0.10.0+CUDA 12.6 + +**Accuracy** +![Qwen3-30b-a3b_fp8_acc]( +https://github.com/Agoniii/verl/blob/xueh/fp8_pr_images/docs/advance/images/Qwen3-30b-a3b_fp8_acc.png?raw=true) +*grey: BF16 + token-level TIS, red: FP8 rollout + token-level TIS* + +Results and observations: +- Rollout & training distribution mismatch is in general higher for MoE +- Rollout correction required even for BF16 +- FP8 rollout with token-level TIS aligns with BF16 + + +**Performance** + +![Qwen3-30b-a3b_fp8_perf]( +https://github.com/Agoniii/verl/blob/xueh/fp8_pr_images/docs/advance/images/Qwen3-30b-a3b_fp8_perf.png?raw=true) +*grey: BF16 + token-level TIS, red: FP8 rollout + token-level TIS​* + +Results and observations: +- FP8 rollout : over 35% rollout speedup +- Expecting more perf gain with CUDA 12.9 + +--- + +## FP8 End-to-End (Training + Rollout) + +FP8 E2E applies FP8 to the entire RL pipeline: forward/backward passes via Transformer Engine, FP8 optimizer states, and FP8 rollout inference via vLLM. This maximizes memory savings and throughput. + +### Requirements + +- **CUDA 12.9+** (required for block-wise FP8 scaling) +- **Transformer Engine** with block-wise FP8 support +- Environment variable: `NVTE_FP8_BLOCK_SCALING_FP32_SCALES=1` + +### Key Configuration + +```yaml +# FP8 training via Transformer Engine +actor_rollout_ref.actor.megatron.override_transformer_config: + fp8: "hybrid" # FP8 forward + backward; also supports "e4m3" + fp8_recipe: "blockwise" # block-wise scaling + +# FP8 optimizer +actor_rollout_ref.actor.optim.override_optimizer_config: + fp8_recipe: "blockwise" + +# FP8 rollout inference (vLLM) +actor_rollout_ref.rollout: + quantization: fp8 +``` + +### Support Matrix + +- Megatron training backend (via Megatron-Bridge) +- Verified on Qwen3-30B-A3B and Qwen3-8B +- Block-wise FP8 scaling (`fp8_recipe: "blockwise"`) + +### Experiments and Results + +#### Qwen3-30B-A3B MoE Model + +**Configuration** +- DAPO recipe. AIME24 online validation. +- Megatron + Megatron-Bridge, FP8 async rollout with vLLM +- MoE router in BF16 for both vLLM and Megatron-Core +- Prompt batch size 128, n=16 +- Max response length 20K +- Token-level TIS, C=2 +- 2\*8*H100, CUDA 12.9 + +![Qwen3-30b-a3b_fp8_e2e](https://github.com/user-attachments/assets/70fb1396-ec73-40d7-9a43-1d48553c0ad9) +*Orange: BF16, Green: FP8 E2E, Red: FP8 rollout + BF16 training* + +Results and observations: +- FP8 E2E achieves comparable accuracy to the BF16 baseline, with the two curves closely aligned throughout training. +- The training/inference precision mismatch (measured by KL divergence) follows the ordering: FP8 rollout-only > FP8 E2E > BF16 E2E. This is expected, as FP8 E2E maintains consistent precision across both training and inference, resulting in lower distribution mismatch than the FP8 rollout-only setting where training remains in BF16. + +--- + +## Citation + +For more extensive experiments, ablation studies, and analysis on FP8 reinforcement learning, please refer to our technical report: + +```bibtex +@article{qiu2026fp8rl, + title={FP8-RL: A Practical and Stable Low-Precision Stack for LLM Reinforcement Learning}, + author={Qiu, Zhaopeng and Yu, Shuang and Zhang, Jingqi and Zhang, Shuai and Huang, Xue and Yang, Jingyi and Lai, Junjie}, + journal={arXiv preprint arXiv:2601.18150}, + year={2026}, + url={https://arxiv.org/abs/2601.18150} +} +``` diff --git a/verl_0720_main/verl/docs/low_precision/nvfp4_qat.md b/verl_0720_main/verl/docs/low_precision/nvfp4_qat.md new file mode 100644 index 0000000000000000000000000000000000000000..f7c2f19a23e934d9471f2b2a44c013eb1e065539 --- /dev/null +++ b/verl_0720_main/verl/docs/low_precision/nvfp4_qat.md @@ -0,0 +1,87 @@ +# NVFP4 QAT (Quantization-Aware Training) in verl + +Last updated: 04/02/2026 + +verl supports NVFP4 Quantization-Aware Training (QAT), which applies fake quantization during training so the model learns to tolerate NVFP4 quantization error. At rollout time, weights are packed into real NVFP4 format for vLLM inference. This closes the precision gap between training and inference, preventing KL divergence explosion. + +| Training Backend | Training Precision | Rollout Precision | vLLM Quant Method | +|---|---|---|---| +| **FSDP** | BF16 + fake quantization | NVFP4 W4A16 | `compressed-tensors` | +| **Megatron** | BF16 + fake quantization | NVFP4 W4A16 | `modelopt` | + +> [!TIP] +> For ready-to-run scripts, environment setup, and experimental results, see the [QAT recipe](https://github.com/verl-project/verl-recipe/tree/main/qat). + +--- + +## Key Configuration + +### FSDP Backend + +Configured under `actor_rollout_ref.actor.fsdp_config.qat`: + +```yaml +actor_rollout_ref: + actor: + fsdp_config: + qat: + enable: true + mode: "w4a16" + group_size: 16 + ignore_patterns: + - "lm_head" + - "embed_tokens" + - "re:.*mlp.gate$" + quantization_config_path: "recipe/qat/config/nvfp4_w4a16.json" +``` + +| Parameter | Description | Default | +|-----------|-------------|---------| +| `fsdp_config.qat.enable` | Enable QAT | `False` | +| `fsdp_config.qat.mode` | Quantization mode | `"w4a16"` | +| `fsdp_config.qat.group_size` | Quantization group size | `16` | +| `fsdp_config.qat.ignore_patterns` | Layers to skip. Supports `re:` prefix for regex, otherwise substring match | `["lm_head", "embed_tokens", "re:.*mlp.gate$"]` | +| `fsdp_config.qat.quantization_config_path` | vLLM quantization config JSON path | Required | + +### Megatron Backend + +Configured under `actor_rollout_ref.actor.megatron.qat`: + +```yaml +actor_rollout_ref: + actor: + megatron: + qat: + enable: true + mode: "w4a16" + group_size: 16 + ignore_patterns: + - "lm_head" + - "*mlp.gate" + quantization_config_path: "recipe/qat/config/nvfp4_w4a16_megatron.json" +``` + +| Parameter | Description | Default | +|-----------|-------------|---------| +| `megatron.qat.enable` | Enable QAT | `False` | +| `megatron.qat.mode` | Quantization mode | `"w4a16"` | +| `megatron.qat.group_size` | Quantization group size | `16` | +| `megatron.qat.ignore_patterns` | Layers to skip. Uses `fnmatch` glob syntax | `["lm_head", "*mlp.gate"]` | +| `megatron.qat.quantization_config_path` | vLLM quantization config JSON path | Required | + +--- + +## Support Matrix + +- NVFP4 W4A16 (weight-only FP4 quantization) +- Dense models and MoE models +- FSDP and Megatron training backends +- Full quantization and FFN-only quantization strategies +- Verified on Qwen3-8B-Base and Qwen3-30B-A3B-Base + +--- + +## Notes + +- FSDP backend has scalability limitations for very large models. For large-scale training, use the Megatron backend. +- FSDP uses `re:` prefix regex for `ignore_patterns`, while Megatron uses `fnmatch` glob syntax. The two are not interchangeable. diff --git a/verl_0720_main/verl/docs/perf/best_practices.rst b/verl_0720_main/verl/docs/perf/best_practices.rst new file mode 100644 index 0000000000000000000000000000000000000000..da3cc9cf25fac25cc79bcdcbbdeddd47f407c1c2 --- /dev/null +++ b/verl_0720_main/verl/docs/perf/best_practices.rst @@ -0,0 +1,251 @@ +Verl LLM Best Practices (DAPO + Qwen3-235B) +=========================================== + +Last updated: 11/03/2025. + +Purpose +------- + +This guide uses DAPO training on Qwen3-235B as a concrete example. We unpack every parameter that appears in the optimization objective, map it to Verl configuration entries, and share field-tested recommendations so you can derive sensible settings for your own workloads. + +.. note:: + + 1. The guide only covers the subset of parameters required to reproduce the DAPO experiments discussed here. For the full list, refer to the ``config`` components in the Verl source tree: https://github.com/verl-project/verl/tree/main/verl/trainer/config + 2. PPO and GRPO introduce KL-constrained policies. We therefore include that setup in the explanations below. You can treat all configurations mentioned here as a DAPO pipeline augmented with a KL penalty. + +Optimization Objectives +----------------------- + +DAPO objective +~~~~~~~~~~~~~~ + +.. math:: + + \begin{aligned} + \mathcal{J}_{\mathrm{DAPO}}(\theta)= & \mathbb{E}_{(q, a) \sim \mathcal{D},\left\{o_i\right\}_{i=1}^G \sim \pi_{\theta_{\text {old }}}(\cdot \mid q)} \ + {\left[\frac{1}{\sum_{i=1}^G\left|o_i\right|} \sum_{i=1}^G \sum_{t=1}^{\left|o_i\right|} \min \left(r_{i, t}(\theta) \hat{A}_{i, t}, \operatorname{clip}\left(r_{i, t}(\theta), 1-\varepsilon_{\text {low }}, 1+\varepsilon_{\text {high }}\right) \hat{A}_{i, t}\right)\right] } \\ + \end{aligned} + +.. math:: + \text { s.t. } \quad 0<\mid\left\{o_i \mid \text { is_equivalent }\left(a, o_i\right)\right\} \mid`_ and ``hf_model`` in ``save_contents`` is + deduplicated against ``model``. With ``False``, model weights go through Megatron's + native ``dist_checkpointing`` and ``hf_model`` in ``save_contents`` is rejected + (use ``verl.model_merger`` after training instead). Optimizer / LR-scheduler / RNG + states always go through ``dist_checkpointing`` regardless of this flag. + The legacy alias ``actor_rollout_ref.actor.megatron.use_dist_checkpointing=True`` + still works and is equivalent to ``use_mbridge=False``. + See :ref:`checkpoint-page` for the full save/load behaviour matrix. + - ``actor_rollout_ref.actor.megatron.vanilla_mbridge``: + ``False`` (default) uses `Megatron-Bridge `_. + ``True`` selects the legacy `mbridge `_, + which is deprecated and will be removed in a future release. + +:math:`\pi` + - ``actor_rollout_ref.rollout.name``: + Rollout backend. Verl currently supports ``vllm`` and ``sglang``—benchmark and tune according to your infrastructure. + - ``actor_rollout_ref.rollout.response_length`` / ``data.max_response_length``: + Maximum generated tokens (rollout setting takes precedence). Larger values improve quality but consume more memory and latency. Monitor ``clip_ratio``; values above 0.1 often mean you are truncating too much. + - ``actor_rollout_ref.rollout.gpu_memory_utilization``: + Target GPU memory usage during rollout. Push it as high as possible without triggering OOM; with parameter/gradient/optimizer offload enabled, 0.8–0.9 is common. + - ``actor_rollout_ref.rollout.tensor_model_parallel_size``: + Tensor parallel degree for the inference engine. Ensure ``(memory_per_gpu * gpu_memory_utilization * TP) > 2 * model_parameters`` (bf16/fp16). Increase TP gradually to expand KV cache capacity while watching communication cost—especially once TP > 8. + - ``actor_rollout_ref.rollout.temperature`` / ``top_p`` / ``top_k``: + Sampling knobs for rollout. Keep enough randomness; ``temperature=1.0``, ``top_p=1.0``, ``top_k=-1`` are good defaults. + - ``actor_rollout_ref.rollout.val_kwargs.temperature`` / ``top_p`` / ``top_k`` / ``do_sample`` / ``n``: + Sampling options for validation. Set ``temperature > 0`` to prevent repetitive thinking chains. For small test sets (e.g., AIME24) raise ``n`` (64 is a common choice) to reduce variance. A practical starting point is ``temperature=1.0``, ``top_p=0.7``, ``top_k=-1``, ``do_sample=True``, ``n=1`` and then increase ``n`` as needed. + - ``+actor_rollout_ref.rollout.engine_kwargs.vllm.*`` / ``+actor_rollout_ref.rollout.engine_kwargs.sglang.*``: + Extra backend options injected via the ``+`` syntax. Consult backend docs for exact semantics. Some switches (for example ``pipeline_parallel_size``) may not be supported yet; when TP=32, ``enable_expert_parallel=True`` can even slow down DeepSeek-V3 rollout, so benchmark carefully. + +:math:`\pi_\theta` + - ``data.train_batch_size``: + Total batch size per training iteration. Each rollout produces ``train_batch_size * n`` samples. Larger values reduce the number of rollouts but increase off-policy drift. + - ``actor_rollout_ref.actor.ppo_mini_batch_size``: + Mini-batch size per optimization step. Tune it the same way you would for standard deep learning workloads. + - ``actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu``: + Samples processed per forward pass on one GPU group (a Megatron group contains TP * PP * CP GPUs). Keep it ≤ ``ppo_mini_batch_size`` and as large as memory allows. + - ``actor_rollout_ref.actor.use_dynamic_bsz``: + Enable dynamic batch sizing to adapt to sequence length and improve throughput. + - ``actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu``: + Maximum tokens per GPU when computing log probabilities under dynamic batching. Set it to at least a multiple of ``max_prompt_length + max_response_length`` to prevent truncation. + - Megatron parallelism parameters (``pipeline_model_parallel_size`` / ``tensor_model_parallel_size`` / ``expert_model_parallel_size`` / ``expert_tensor_parallel_size`` / ``context_parallel_size``): + Balance PP/TP/EP/ETP/CP to match memory and network constraints. In bf16/fp16, each parameter consumes roughly ``2 / TP`` bytes; if you keep FP32 master weights or skip optimizer offload, reserve another 4–8 bytes for Adam. Activations scale with ``micro_batch_size × sequence_length × hidden_size`` and can be mitigated with gradient checkpointing, dynamic batches, or offload. Prefer increasing TP first, add PP when necessary, extend sequence capacity with CP, align EP/ETP with TP for MoE models, and keep DP minimal on constrained clusters while combining with offload. Always align the setup with hardware topology and communication cost. + - ``actor_rollout_ref.model.use_fused_kernels``: + Enable Verl’s fused kernels for supported models to squeeze out additional performance. + +:math:`\hat{A}_{i,t}` + - ``algorithm.adv_estimator``: + Advantage estimator. Set to ``grpo`` for DAPO/GRPO. + +:math:`R_i` + - ``reward_model.reward_manager``: + Reward aggregation strategy. Use ``dapo`` for DAPO and ``naive`` for GRPO. + +:math:`D_{KL}` + - ``algorithm.use_kl_in_reward``: + Whether to add a KL term to the reward. ``True`` for PPO, ``False`` for GRPO and DAPO. + - ``actor_rollout_ref.actor.use_kl_loss``: + Whether to include a KL loss term. ``False`` for PPO, ``True`` for GRPO, ``False`` for DAPO. + +:math:`\beta` + - ``actor_rollout_ref.actor.kl_loss_coef``: + Weight of the KL loss. Start around 0.001. Larger values curb reward hacking but reduce exploration. + - ``algorithm.kl_ctrl.kl_coef``: + KL coefficient applied within the reward. Adjust to match your tolerance for divergence. + +:math:`\pi_{old}` + - ``actor_rollout_ref.rollout.log_prob_use_dynamic_bsz``: + Enable dynamic batching when the old policy computes log-probabilities. Recommended. + +:math:`\pi_{ref}` + - ``actor_rollout_ref.ref.log_prob_use_dynamic_bsz``: + Enable dynamic batching for the reference policy. Recommended. + - Reference Megatron parallelism: + Keep ``pipeline_model_parallel_size``, ``tensor_model_parallel_size``, ``expert_model_parallel_size``, ``expert_tensor_parallel_size``, and ``context_parallel_size`` in sync with the actor. + - ``actor_rollout_ref.ref.megatron.param_offload``: + Offload reference parameters to CPU when the actor does so. Even without gradients or optimizer states, parity helps with capacity planning. + +:math:`o_i` / :math:`|o_i|` + - ``actor_rollout_ref.actor.loss_agg_mode``: + Loss aggregation mode. Token-level ``token-mean`` matches the recommendations from Dr.GRPO and DAPO; use ``seq-mean-token-mean`` to reproduce the original GRPO behavior. + +:math:`\pi_\theta(o_{i,t} \mid q_i,o_{i,`_ + - `SimonHuang `_ + +1.5B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2.5-1.5B + - GRPO-LoRA + - 1*H100 + - 128 + - fsdp + - vllm0.8.3 + - `qwen2-1.5b_grpo-lora_1_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +3B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2.5-3B + - GRPO-LoRA + - 1*H100 + - 62 + - fsdp + - vllm0.8.3 + - `qwen2-3b_grpo-lora_1_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +7B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2-7B + - GRPO + - 2*H800 + - \ + - fsdp + - vllm0.8.2 + - `qwen2-7b_grpo_2_h800_fsdp_vllm `_ + - `Xiangyongan `_ + * - MIN + - Qwen2.5-7B + - GRPO-LoRA + - 1*H100 + - 16 + - fsdp + - vllm0.8.3 + - `qwen2-7b_grpo-lora_1_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +14B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2-14B + - GRPO + - 4*H800 + - \ + - fsdp + - vllm0.8.2 + - `qwen2-14b_grpo_4_h800_fsdp_vllm `_ + - `Xiangyongan `_ + * - MIN + - Qwen2.5-14B + - GRPO-LoRA + - 2*H100 + - 116 + - fsdp + - vllm0.8.3 + - `qwen2-14b_grpo-lora_2_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +32B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2-32B + - GRPO + - 8*H20 + - \ + - megatron + - vllm0.8.2 + - `qwen2-32b_grpo_8_h20_megatron_vllm `_ + - `Xiangyongan `_ + * - MIN + - Qwen2.5-32B + - GRPO-LoRA + - 4*H100 + - 180 + - fsdp + - vllm0.8.3 + - `qwen2-32b_grpo-lora_4_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +70B +~~~ + +.. list-table:: + :widths: auto + :header-rows: 1 + + * - Tag + - Model + - Task + - Resource + - MaxBatch + - Train + - Infer + - Link + - Contributor + * - MIN + - Qwen2-70B + - GRPO + - 32*H20 + - \ + - fsdp + - vllm0.8.2 + - `qwen2-70b_grpo_32_h20_fsdp_vllm `_ + - `Xiangyongan `_ + * - MIN + - Qwen2-70B + - GRPO + - 32*H800 + - \ + - fsdp + - vllm0.8.3 + - `qwen2-70b_grpo_32_h800_fsdp_vllm `_ + - `Xiangyongan `_ + * - MIN + - Qwen2.5-72B + - GRPO-LoRA + - 8*H100 + - 176 + - fsdp + - vllm0.8.3 + - `qwen2-72b_grpo-lora_8_h100_fsdp_vllm.sh `_ + - `SimonHuang `_ + +405B +~~~~ + +.. table:: + :widths: auto + + ====== ====== ====== ======== ======== ====== ====== ====== + tag model task resource MaxBatch train infer link + ====== ====== ====== ======== ======== ====== ====== ====== + \ \ \ \ \ \ \ + ====== ====== ====== ======== ======== ====== ====== ====== + +671B +~~~~ + +.. table:: + :widths: auto + + ====== ====== ====== ======== ======== ====== ====== ====== + tag model task resource MaxBatch train infer link + ====== ====== ====== ======== ======== ====== ====== ====== + \ \ \ \ \ \ \ + ====== ====== ====== ======== ======== ====== ====== ====== diff --git a/verl_0720_main/verl/docs/perf/dpsk.md b/verl_0720_main/verl/docs/perf/dpsk.md new file mode 100644 index 0000000000000000000000000000000000000000..400066cfc0b67e39c34acb0713384f24677ea165 --- /dev/null +++ b/verl_0720_main/verl/docs/perf/dpsk.md @@ -0,0 +1,88 @@ +# Training DeepSeek 671b + +Last updated: 08/20/2025. + +verl integrates Megatron to support large MoE models such as `Qwen3-235B-A22B` and `deepseek-ai/DeepSeek-V3`. This is an ongoing community effort. + +In the journey the community added the following features and optimizations that enable verl with larger models: +- per tensor weight resharding between rollout and training +- context parallelism and expert parallelism enabled via megatron +- dynamic batch size (sequence balance) for megatron +- reduced ray-related serialization overhead +- optimizer offloading, recomputation, and efficient kernels +- various debugging metrics and utils +- hybrid optimizer + +and the megatron backend now has a wider list of models supported: +- DeepSeek-V3 +- Moonlight +- Qwen3 +- Qwen2.5-VL (to be merged soon) +- Qwen2 +- Mixtral + +## Getting Started + +### preparation +The recommended image with pre-built Megatron dependency is `verlai/verl:app-verl0.4-vllm0.8.5-mcore0.13.0-preview`, which is built using the Dockerfile at [docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.13.preview](https://github.com/verl-project/verl/blob/main/docker/verl0.4-cu124-torch2.6-fa2.7.4/Dockerfile.app.vllm.mcore0.13.preview). + +The image is build in Hopper GPUs with DeepEP. It does not support None-Hopper GPUs, such as A100. You may need to reinstall DeepEP to work with A100. + +With `OFFLOAD_FRACTION=1`, the system's minimum requirements are lowered. It can run on as few as 96 H20 (96GB) GPUs for DeepSeek-V3, and on as few as 32 H20 (96GB) GPUs for Qwen3-235B-A22B. However, this configuration will use 1.6TB CPU memory per node. If you run out of CPU memory or require faster training speed, you can add more nodes. + +### DeepSeek 671b + +For DeepSeek-V3 671b, please refer to [examples/grpo_trainer/run_deepseek_v3_671b_megatron.sh](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_deepseek_v3_671b_megatron.sh). + +MTP and quantilization is disabled during RL training. + +To train your project, configure the following environment variables based on the number of available GPUs. These are recommended settings and can be adjusted based on your specific hardware. +| num gpus | NNODES | TP | PP | EP | OFFLOAD_FRACTION | OFFLOAD_OPTIM | LAST_LAYER | +| -- | -- | -- | -- | -- | -- | -- | -- | +| 96 | 12 | 8 | 12 | 8 | 1. | False | 6 | +| 128 | 16 | 8 | 16 | 8 | 0.5 | True | 1 | +| 256 | 32 | 8 | 16 | 8 | 0. | True | 1 | +| 512 | 64 | 1 | 16 | 32 | 0 | True | 1 | + +### Qwen3 235b + +For Qwen3-235b, please refer to [examples/grpo_trainer/run_qwen3_235b_a22b_megatron.sh](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_235b_a22b_megatron.sh). + +To train your project, configure the following environment variables based on the number of available GPUs. These are recommended settings and can be adjusted based on your specific hardware. +| num gpus | NNODES | TP | PP | EP | OFFLOAD_FRACTION | OFFLOAD_OPTIM | LAST_LAYER | +| -- | -- | -- | -- | -- | -- | -- | -- | +| 32 | 4 | 4 | 8 | 4 | 1. | False | 6 | +| 64 | 8 | 4 | 8 | 4 | 0.5 | True | 6 | +| 128 | 16 | 4 | 8 | 4 | 0 | True | 6 | +| 256 | 32 | 4 | 8 | 4 | 0 | True | 6 | + +### Benchmark +Here are some benchmark results for DeepSeek / Qwen3-235B. All configurations match the recommended settings based on the number of GPUs. + +| model | num gpus | mean response length | rollout time(s) | GPU memory(GB) | CPU memory(GB) | MFU | step time(s) | +| -- | -- | -- | -- | -- | -- | -- | -- | +| DeepSeek 671b | 96 | 1960 | 1050 | 66 | 1500 | 0.19 | 1700 | + +### Qwen3-30B-A3B MOE + +For Qwen3-30b, please refer to [examples/grpo_trainer/run_qwen3_30b_a3b_megatron.sh](https://github.com/verl-project/verl/blob/main/examples/grpo_trainer/run_qwen3_30b_a3b_megatron.sh). + +To train your project, configure the following environment variables based on the number of available GPUs. These are recommended settings and can be adjusted based on your specific hardware. +| num gpus | NNODES | TP | PP | EP | OFFLOAD_FRACTION | OFFLOAD_OPTIM | MFU | +| -- | -- | -- | -- | -- | -- | -- | -- | +| 8 | 1 | 1 | 1 | 8 | 1. | True | 0.4 | +| 16 | 2 | 1 | 1 | 8 | 1. | True | 0.37 | +| 32 | 4 | 1 | 1 | 8 | 1. | True | 0.31 | + + +## Upcoming Optimizations + +The community continue to optimize large MoE models further, ongoing efforts include: +- further optimizing memory consumption, and provide recommended/tuned configurations with various machine types +- optimizing long context RL training performance +- performance improvement with SGLang x Megatron + +We invite the community to try and improve verl together. Get connected with us on [slack](https://join.slack.com/t/verlgroup/shared_invite/zt-2w5p9o4c3-yy0x2Q56s_VlGLsJ93A6vA)/[wechat](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/refs/heads/main/WeChat.JPG)/[Github issues](https://github.com/verl-project/verl/issues/708)! + +## Acknowledgement +@vermouth1992 @ISEEKYAN @ETOgaosion @yzlnew @ShareLer @BearBiscuit05 @ccclyu @ann-qin-lu @SwordFaith @zzong2006 @zhaochenyang20 @ocss884 @eric-haibin-lin @chenhaiq @techkang diff --git a/verl_0720_main/verl/docs/perf/nsight_profiling.md b/verl_0720_main/verl/docs/perf/nsight_profiling.md new file mode 100644 index 0000000000000000000000000000000000000000..490de5e7e4f7b6ba6c0e372eb7c0c3bfce2a77b9 --- /dev/null +++ b/verl_0720_main/verl/docs/perf/nsight_profiling.md @@ -0,0 +1,94 @@ +# NVIDIA Nsight Systems profiling in verl + +Last updated: 06/20/2025. + +This guide explains how to use NVIDIA Nsight Systems for profiling verl training runs. + +## Configuration + +Profiling in verl can be configured through several parameters in the trainer configuration file (ppo_trainer.yaml or other files like dapo_trainer.yaml): + +### Prerequisites + +Nsight Systems version is important, please reference `docker/Dockerfile.vllm.sglang.megatron` for the version we used. + +### Global profiling control + +verl has one single controller process and multiple worker processes. Both controller and worker processes can be profiled. Since the controller process can be executed in any nodes in the cluster, there is a message printed in the logging to indicate the controller process node hostname and process id. + +In `global_profiler`, three new config entries control the profiler behaviors: + +* **`global_profiler.steps`**. List of step numbers at which profiling should be performed. For example: [1, 2, 5] will profile steps 1, 2, and 5. And ``null`` means no profiling. + +* **`global_profiler.profile_continuous_steps`**. If true, and the following `global_profiler.discrete==False`, then the continuous steps in `global_profiler.steps` will be combined into one database. For example the above step 1 and 2 are in one database, and 5 in another. If false, every step occupies at least one database. The reason for this config is to observe the program behaviors between steps. + +Nsys options in controller nodes and worker nodes are configured in `global_profiler.global_tool_config.nsys`: + +* **`global_profiler.global_tool_config.nsys.controller_nsight_options`**. This config group is for the single controller. All fields in this config group will be just sent to Nsight Systems when Ray starts the controller process. `ppo_trainer.yaml` provides a workable example. Users can reference [Nsight Systems manual](https://docs.nvidia.com/nsight-systems/UserGuide/index.html) and [Ray user guide](https://docs.ray.io/en/latest/ray-observability/user-guides/profiling.html) for more details. +* **`global_profiler.global_tool_config.nsys.worker_nsight_options`**. This config group is for the worker processes. Similarly all fields in this config group will be just sent to Nsight Systems when Ray starts the controller process. Capture range is used to control the profiler when to start and stop. So `capture-range: "cudaProfilerApi"` is fixed and does not change it. Users can change `capture-range-end` with some accurate calculation or just leave it `null`. + +### Worker process profiling + +Verl manages mulitiple RL roles, _Actor_, _Ref_, _Rollout_, _Critic_, _Reward_, which are implemented in different Worker classes. And these workers can be combined into one Ray Actor, running in a process group. Each RL role has its own profiling config group, `profiler`, which consists of three fields: + +* **`all_ranks` and `ranks`**. When `all_ranks` is set `True` then all ranks will be profiled; when set `False`, `ranks` will be profiled. By default, verl profiles the whole training process in a series ` worker_process_..nsys-rep` files for each process rank. PID is the process ID; RID is the capture range ID. +* **`discrete`**. When set `False`, all the roles actions in one training step will be dumped in one database. When set `True`, the actions annotated by `DistProfiler.annotate` will be dumped into a discrete database. In this case, each role's action occupies one ``. +* **Verl collocate mode**. Verl can combine two Worker sub classes to one Worker Actor. In this case, the user should take care that the combined Workers have consistent `discrete`. The Nsight Systems profiler uses a `torch.cuda.profiler.start()` and `stop()` pair to dump a `` database anyway. + +### where to find the profiling data + +By default the `*.nsys-rep` files are saved in the directory `/tmp/ray/session_latest/logs/nsight/` at each node. According to the Ray manual, this default directory is not changeable. ["however, Ray preserves the `--output` option of the default config"](https://docs.ray.io/en/latest/ray-observability/user-guides/profiling.html). + +Some users may think it is not convenient, but it is understandable that Ray may start hundreds of processes and it would be a big network file system pressure if we save the files in one central place. + +## Usage Example + +To enable profiling for specific components and steps, modify your ppo_trainer.yaml like this: + +### Disable profiler + +```yaml + profiler: + steps: null # disable profile +``` + +### Enable profiler and one database for one training step + +```yaml + global_profiler: + steps: [1, 2, 5] + discrete: False + actor_rollout_ref: + actor: + profiler: + enable: True + all_ranks: True + # rollout & ref follow actor settings + critic: + profiler: + enable: True + all_ranks: True + reward_model: + profiler: + enable: True + all_ranks: True +``` + +### Enable profiler and multiple databases for one training step + +```yaml + profiler: + steps: [1, 2, 5] + discrete: True +``` + +## Profiling Output + +When profiling is enabled, verl will generate Nsight Systems profiles for the specified components and steps. The profiles will include: + +- CUDA kernel execution +- Memory operations +- CPU-GPU synchronization +- NVTX markers for key operations + +Nsight Systems supports multi-report view, to open multiple databases together. In this mode, different processes and steps can be aligned in one time line for better analysis. diff --git a/verl_0720_main/verl/docs/perf/perf_tuning.rst b/verl_0720_main/verl/docs/perf/perf_tuning.rst new file mode 100644 index 0000000000000000000000000000000000000000..cf25e9e9c627ada3c2caf63e479955e590684bfd --- /dev/null +++ b/verl_0720_main/verl/docs/perf/perf_tuning.rst @@ -0,0 +1,224 @@ +Performance Tuning Guide +============================== + +Last updated: 07/17/2025. + +Author: `Guangming Sheng `_, `Jiali Zheng `_ + +In this section, we will discuss how to tune the performance of all the stages in verl, including: + +1. Rollout generation throughput. + +2. Enable ``use_remove_padding=True`` for sequence packing (i.e., data packing and remove padding). + +3. Batch size tuning for forward and backward computation + +4. Enable ``use_dynamic_bsz=True`` for higher throughput. + +5. Utilize Ulysses Sequence Parallel for Long Context Training + +6. LigerKernel for SFT performance optimization + +7. Forward prefetch in FSDP training backend + +8. Memory optimization for entropy calculation from logits + +Rollout Generation Tuning +-------------------------- + +verl currently supports two rollout backends: vLLM and TGI (with SGLang support coming soon). + +Below are key factors for tuning vLLM-based rollout. Before tuning, we recommend setting ``actor_rollout_ref.rollout.disable_log_stats=False`` so that rollout statistics are logged. + +- Increase ``gpu_memory_utilization``. + + - For vLLM v0.7.0 and later, the vLLM instance will only use gpu_memory_utilization of the **total** memory. + - For SGLang, it's the fraction of the free GPU memory used for **static** memory like model weights and KV cache. However, the remaining (1-gpu_memory_utilization) will also be used during inference. + + However, if model parameters and optimizer states are not offloaded, using too high a fraction can lead to OOM. + A value between 0.5 and 0.7 often strikes a good balance between high throughput and avoiding OOM. + + Note: since the definition of ``gpu_memory_utilization`` varies across inference engines, a value that works well for one engine may cause OOM for another. + +- Adjust ``max_num_seqs`` or ``max_num_batched_tokens``. + If the GPU cache utilization is relatively low in the log, increase ``max_num_seqs`` or ``max_num_batched_tokens`` + can enlarge the effective batch size in the decoding stage, allowing more concurrent requests per batch. + We recommend setting ``max_num_batched_tokens > 2048`` for higher throughput. + +- Use a smaller ``tensor_parallel_size``. + When GPU resources allow, a smaller tensor parallel size spawns more vLLM replicas. + Data parallelism (DP) can yield higher throughput than tensor parallelism (TP), but also increases KVCache consumption. + Carefully balance the trade-off between more replicas and higher memory usage. + Our experiment in Sec. 8.4 of `HybridFlow paper `_ evaluate this trade-off. + +- Balance performance and memory using ``cudagraph_capture_sizes``. + If ``cudagraph_capture_sizes`` is set, vLLM will try to capture the model execution graph for different batch sizes. + Since cudagraph memory can not be offloaded to cpu, The memory stay in gpu when update actor is running. + Using smaller batch sizes can avoid OOM but slightly reduce throughput. + Must to set ``enforce_eager=False`` to use ``cudagraph_capture_sizes``. + +More tuning details such as dealing with Preemption and Chunked-prefill +can be found in `vLLM official tuning guide `_ + +For optimal performance, we recommend using vLLM v0.8.3 or later. See https://github.com/verl-project/verl/blob/main/docs/README_vllm0.8.md for details. + +Enable remove padding (sequence packing) +----------------------------------------- + +Currently, for llama, mistral, gemma1 and qwen based models, users can enable `use_remove_padding=True` to utilize the +sequence packing implementation provided by transformers library. + +For other models, transformers library may also support it but we haven't tested it yet. +Users can add the desired model config to the `test_transformer.py `_ file. +And test its functionality by running the following command: + +.. code-block:: bash + + pytest -s tests/models/test_transformer.py + +If the test passes, you can add your desired model into the model `registry.py `_ file. +Then, you can enjoy the performance boost of sequence packing +and welcome to PR your tested model to verl! + + +Batch Size Tuning +----------------- + +To achieve higher throughput in experience preparation (i.e., model fwd) and model update (i.e., actor/critic fwd/bwd), +users may need to tune the ``*micro_batch_size_per_gpu`` for different computation. + +In verl, the core principle for setting batch sizes is: + +- **Algorithmic metrics** (train batch size, PPO mini-batch size) are *global* (from a single-controller perspective), + normalized in each worker. See the `normalization code `_. + +- **Performance-related parameters** (micro batch size, max token length for dynamic batch size) are *local* parameters that define the per-GPU data allocations. + See the `normalization code `_. + +.. note:: In your training script, please use ``*micro_batch_size_per_gpu`` instead of ``*micro_batch_size``. + So that you don't need to consider the normalization of the ``micro_batch_size`` and ``micro_batch_size`` will be deprecated. + +Batch Size Tuning tips +"""""""""""""""""""""" + +Therefore, users may need to tune the ``*micro_batch_size_per_gpu`` to accelerate training. Here're some tips: + +1. **Enable gradient checkpointing**: + Set ``actor_rollout_ref.model.enable_gradient_checkpointing=True`` and ``critic.model.enable_gradient_checkpointing=True``. + This often allows for larger micro-batch sizes and will be beneficial for large mini-batch training. + +2. Increase the ``*micro_batch_size_per_gpu`` as much as possible till equals to normalized ``mini_batch_size``. + +3. **Use larger forward-only parameters**: + Forward only parameter, such as ``actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu``, + ``actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu``, ``critic.forward_micro_batch_size_per_gpu`` could be larger (e.g., 2x) than training related micro batch sizes, + such as ``actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu``, ``critic.ppo_micro_batch_size_per_gpu``. + +4. **Allow larger micro-batch sizes for Critic and Reward models**: + micro batch size of Critic and Reward model could be larger than Actor model. This is because the actor model has much larger vocab size in the final layer. + +5. **Enable activation offloading**: + Set ``actor_rollout_ref.model.enable_activation_offload=True`` and ``critic.model.enable_activation_offload=True``. + This often works together with gradient checkpointing to get larger micro-batch sizes and it's only available in FSDP backend now. + +Tuning for Dynamic Batch Size +----------------------------- + +Dynamic batch size is a technique that allows the model to process similar number of tokens in a single forward pass (with different actual batch sizes). +This can significantly improve the training efficiency and reduce the memory usage. + +To utilize this technique, users can set ``use_dynamic_bsz=True`` in actor, ref, critic and reward models. +With ``use_dynamic_bsz=True``, users don't need to tune ``*micro_batch_size_per_gpu``. +Instead, users should tune the following parameters: + +- ``actor_rollout_ref.actor.ppo_max_token_len_per_gpu``, ``critic.ppo_max_token_len_per_gpu``: + The maximum number of tokens to be processed in fwd and bwd of ``update_policy`` and ``update_critic``. + +- ``actor_rollout_ref.ref.log_prob_max_token_len_per_gpu`` and ``actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu``: + The maximum number of tokens to be processed in a the fwd computation of ``compute_log_prob`` and ``compute_ref_log_prob``. + +- ``critic.forward_micro_batch_size_per_gpu``, ``reward_model.forward_micro_batch_size_per_gpu``: + The maximum number of tokens to be processed in a the fwd computation of ``compute_values``, ``compute_rm_score``. + +Dynamic Batch Size Tuning tips +"""""""""""""""""""""""""""""" + +Here're some tips to tune the above parameters: + +1. **Increase** ``actor_rollout_ref.actor.ppo_max_token_len_per_gpu`` + Make it at least 2 x (max_prompt_length + max_response_length). See `run_qwen3_8b_fsdp.sh `_ for an example. + Try to increase it to get higher throughput. + +2. **Forward-only parameters can be larger**: + Similar to the non-dynamic-batch scenario, forward-only token limits can exceed those used in forward/backward operations. + +3. **Use larger limits for Critic and Reward models**: + Critic and Reward parameters can be set at least 2× the Actor’s limits. See + `run_qwen3_8b_fsdp.sh `_ for an example. + +.. :math:`\text{critic.ppo_max_token_len_per_gpu} = 2 \times \text{actor.ppo_max_token_len_per_gpu})`. + +Ulysses Sequence Parallel for Long Context Training +---------------------------------------------------- + +To utilize this technique, users can set ``ulysses_sequence_parallel_size>1`` in actor, ref, critic and reward models. + +We support different model utilize different ulysses_sequence_parallel_size sizes. + +To train long sequence (>32k), users may need to decrease the ``*micro_batch_size_per_gpu`` and ``*max_token_len_per_gpu`` to avoid OOM. + +LigerKernel for training performance +-------------------------------------- + +LigerKernel provides fused Triton kernels (RMSNorm, SwiGLU, RoPE) that can improve training throughput. It works with both SFT and RL (PPO/GRPO) training, including vision-language models. + +1. Install liger-kernel via ``pip3 install liger-kernel``. Set ``use_liger`` in your configuration: + + .. code-block:: yaml + + model: + use_liger: True # Enable LigerKernel + +2. The default value is ``False``. When enabled, verl applies Liger's fused RMSNorm, SwiGLU, and RoPE kernels to the model. The ``fused_linear_cross_entropy`` optimization is disabled because verl computes log-probabilities via its own path. + +3. ``use_liger`` is compatible with ``use_fused_kernels`` — they operate at different levels (Liger optimizes model internals, fused kernels optimize the output head). Using both together gives the best speed-memory tradeoff. + +Forward prefetch in FSDP training backend +---------------------- + +During the training phase, users can enable forward prefetching in FSDP by setting ``fsdp_config.forward_prefetch=True``. For example, ``actor_rollout_ref.actor.fsdp_config.forward_prefetch=True``. This configuration prefetches the next forward-pass all-gather operation before completing the current forward computation, overlapping communication with computation and improving efficiency. For further details, refer to the `FSDP forward_prefetch `_ documentation. + +.. note:: + Backward prefetch is unsupported because the ``BACKWARD_POST`` policy may prefetch incorrectly in nested-module cases. For details, see the `FSDP documentation `_ + +Migrating to FSDP2 +---------------------- + +FSDP2 offers notable improvements over FSDP1. According to `PyTorch TorchTitan benchmarks `_: + +- 7% lower GPU memory usage on average +- 1.5% throughput improvement with BF16 training +- Better composability with DTensor and per-parameter sharding + +**Enabling FSDP2 in VERL:** + + .. code-block:: python + + # Enable FSDP2 in actor configuration + actor_rollout_ref.actor.strategy="fsdp2" + +.. note:: + FSDP2 requires PyTorch 2.1+ and is recommended for models with transformer architecture. + +Memory optimization for entropy calculation from logits +---------------------- + +The ``logits`` tensor (typically of shape ``[bsz*seq_len, voc]``) can consume significant memory. When using ``compute_entropy_from_logits``, memory usage reaches approximately ``[bsz*seq_len, voc] × (4 bytes (float32) + 2 bytes (autocast for softmax+logsumexp) + 1 byte (softmax output))``. + +To reduce this memory peak, enable chunked computation by setting: +``actor_rollout_ref.ref.entropy_from_logits_with_chunking = True`` +This processes the tensor in chunks of shape ``[chunk_size, voc]`` (e.g., 2048) rather than the full sequence length, exclusively during the model's forward pass. + +Additionally, during training, standard gradient checkpointing (``enable_gradient_checkpointing=True``) does not apply to entropy calculations. To reduce memory peaks in this context, set: +``actor_rollout_ref.actor.entropy_checkpointing = True`` +This enables entropy recomputation specifically for the entropy calculation, lowering memory usage during training. diff --git a/verl_0720_main/verl/docs/perf/rollout_kv_offload.md b/verl_0720_main/verl/docs/perf/rollout_kv_offload.md new file mode 100644 index 0000000000000000000000000000000000000000..a6011cf46d13e3c9be17564b9fa18889e4e11d9b --- /dev/null +++ b/verl_0720_main/verl/docs/perf/rollout_kv_offload.md @@ -0,0 +1,54 @@ +# Rollout KV Cache Offload via Mooncake-Store + +Last updated: 05/27/2026. + +Offload prefix KV blocks from the vLLM rollout engine to a shared +[Mooncake](https://github.com/kvcache-ai/Mooncake) store so long shared +prefixes (system prompt, agentic tool history, `rollout.n` samples per prompt) +get deduplicated across requests and rollout replicas. This also helps +long-tail load balancing: when work migrates to idle rollout replicas, shared +prefix KV reduces the re-prefill cost. + +## Setup Mooncake + vLLM + +Follow vLLM's official guide for installing the Mooncake client, starting a +master, and writing the JSON config: +**** + +The verl side only consumes whatever that doc produces — no extra steps. + +## Enable in verl + +verl forwards `engine_kwargs.vllm.*` straight to `vllm serve` as CLI flags. +To attach the Mooncake connector, set `kv_transfer_config`: + +```yaml +actor_rollout_ref: + rollout: + engine_kwargs: + vllm: + kv_transfer_config: |- + { + "kv_connector": "MooncakeStoreConnector", + "kv_role": "kv_both", + "kv_connector_extra_config": { + "mooncake_config_path": "/path/to/mooncake_config.json" + } + } +``` + +Or as a Hydra CLI override: + +```bash ++actor_rollout_ref.rollout.engine_kwargs.vllm.kv_transfer_config.kv_connector=MooncakeStoreConnector \ ++actor_rollout_ref.rollout.engine_kwargs.vllm.kv_transfer_config.kv_role=kv_both \ ++actor_rollout_ref.rollout.engine_kwargs.vllm.kv_transfer_config.kv_connector_extra_config.mooncake_config_path=/path/to/mooncake_config.json +``` + +## RL correctness: hard reset on every weight update + +verl clears both local and Mooncake KV caches at every weight update boundary +to avoid reusing KV from the previous policy. + +**Required vLLM version**: use vLLM 0.22 or newer. Older builds may leave stale +KV in the Mooncake master after a weight update. diff --git a/verl_0720_main/verl/docs/perf/torch_profiling.md b/verl_0720_main/verl/docs/perf/torch_profiling.md new file mode 100644 index 0000000000000000000000000000000000000000..8a121faf71398ab5a43c4fbd376c9589f4d83fa1 --- /dev/null +++ b/verl_0720_main/verl/docs/perf/torch_profiling.md @@ -0,0 +1,118 @@ +# PyTorch Profiling in verl + +Last updated: 01/13/2026. + +This guide explains how to use the native [PyTorch Profiler](https://pytorch.org/tutorials/recipes/recipes/profiler_recipe.html) for profiling verl training runs. + +## Configuration + +Profiling in verl can be configured through parameters in the trainer configuration file (e.g., `ppo_trainer.yaml`). + +### Global Profiling Control + +In `global_profiler`, you can control when and how profiling occurs globally: + +* **`global_profiler.steps`**: List of step numbers to profile. E.g., `[1, 2, 5]` profiles steps 1, 2, and 5. Set to `null` to disable. +* **`global_profiler.save_path`**: Directory to save the profiling results. Default is `outputs/profile`. + +### Role Profiling Control + +Each RL role (Actor, Critic, etc.) has its own `profiler` configuration: + +* **`enable`**: Whether to enable profiling for this role. +* **`all_ranks`**: If `True`, profiles all ranks. +* **`ranks`**: List of specific ranks to profile if `all_ranks` is `False`. +* **`tool_config.torch`**: Configuration specific to the PyTorch Profiler. + +#### PyTorch Profiler Options (`tool_config.torch`) + +You can customize the PyTorch Profiler behavior using the following fields under `tool_config.torch`: + +* **`contents`**: List of contents to profile. + * **`cpu`**: Profile CPU activities. + * **`cuda`**: Profile CUDA activities. + * **`memory`**: Track tensor memory allocation/free. + * **`shapes`**: Record shapes of operator inputs. + * **`stack`**: Record source code file and line number. +* **`profile_token_start`**: Effective only for the rollout role; defines the start response-token index for rollout decoding collection. It is applied only when valid (0-based, `profile_token_end > profile_token_start`, and within response length). +* **`profile_token_end`**: Effective only for the rollout role; defines the stop response-token index (exclusive) for rollout decoding collection. It is applied only when valid (0-based, `profile_token_end > profile_token_start`, and within response length). +* **`schedule`**: (Advanced) configuration for `wait`, `warmup`, `active`, `repeat` cycles. + +## Examples + +### 1. End-to-End Collection + +Collects performance data for all steps in a single trace file. + +```yaml +global_profiler: + steps: [1, 2, 5] + save_path: ./outputs/profile + +actor_rollout_ref: + actor: + profiler: + enable: True + all_ranks: True + tool_config: + torch: + discrete: False + contents: [cpu, cuda] + # rollout & ref follow actor settings +``` + +### 2. Discrete Mode Collection + +Discrete mode saves separate trace files for each step. This is useful for detailed analysis and is **mandatory** when using Agent Loop. + +**Configuration Example** + +This configuration supports profiling both Training (Actor) and Inference (Rollout). You can enable/disable them independently. + +```yaml +actor_rollout_ref: + actor: + profiler: + enable: True # Set to True to profile training + all_ranks: False + ranks: [0] # Global Rank 0 + tool_config: + torch: + discrete: True + contents: [cpu, cuda] + rollout: + profiler: + enable: True # Set to True to profile inference + all_ranks: False + ranks: [0] # In Agent Loop, this is the Replica Rank (e.g. 0-th instance) + tool_config: + torch: + discrete: True # REQUIRED + # Optional response-token window for rollout engine side collection. + # If start/stop are not set, the entire rollout stage is collected. + # Collect tokens in [12, 46), i.e. token index 12~45. + profile_token_start: 12 + profile_token_end: 46 + # ref follow actor settings +``` + +**Agent Loop Mode Description** + +When Rollout runs in [Agent Loop](../advance/agent_loop.rst) mode, performance data for the Rollout phase **must be collected using discrete mode**. In this case, the Profiler is triggered by the inference engine backend. + +1. Rank Definition: ranks in the Rollout configuration refers to Replica Rank (inference instance index), not Global Rank. + +2. Inference Engine Support: Currently, vLLM and SGLang engines are supported without additional settings. Specific details are as follows: + + * **vLLM Engine**: Automatically collects AsyncLLM scheduling stacks and inference process performance data. + * **SGLang Engine**: Automatically collects inference process performance data. Does not support the memory option in contents. + +## Visualization + +Collected trace files (usually `.json` or `.json.gz`) are stored in the configured `save_path`. + +You can visualize them using: + +1. **Chrome Tracing**: Open `chrome://tracing` in a Chrome browser and load the JSON file. +2. **Perfetto**: Open [ui.perfetto.dev](https://ui.perfetto.dev/) and load the file (recommended for large traces). +3. **TensorBoard**: If using the TensorBoard plugin for PyTorch Profiler. diff --git a/verl_0720_main/verl/docs/perf/verl_profiler_system.md b/verl_0720_main/verl/docs/perf/verl_profiler_system.md new file mode 100644 index 0000000000000000000000000000000000000000..fc7ecc38eed92ca5e05274e23f40b6f1ce7033b0 --- /dev/null +++ b/verl_0720_main/verl/docs/perf/verl_profiler_system.md @@ -0,0 +1,36 @@ +# verl Profiler System + +Last updated: 08/18/2025. + +## Architecture + +The architecture of verl profiler system is like below: + +![verl-profiler-arch](https://raw.githubusercontent.com/eric-haibin-lin/verl-community/2bc7ed0ba2f37f21707bfac3b241eca4b86d1bc6/docs/verl_profiler_arch.png) + +There is a global profiler and tool configuration to set some common config in single controller level, deciding + +- `tool`: which tool to use +- `steps`: which steps to profile +- `save_path`: results saving path + +When some tool need to profile behavior of each role, configurations in role-level is needed: + +- `tool`: which tool to use +- `enable`: whether enable profiling on this role +- rank info: `all_ranks` and `rank` to decide which rank to profile or log output + +For tool config in role-level, there are some detailed behavior needed to control, like the `discrete` mode in nsys profiler. + +Every role has a profiler config, and by default, rollout/ref/reward models follow the Actor's behavior. + +## To Add a new profiling tool + +New added profiling tool shall reuse the current APIs as much as possible. + +1. The logic of **whether to use the tool**: `tool == [new tool]`. +2. Add the global and local tool config to `ppo_trainer.yaml`/`ppo_megatron_trainer.yaml` and each `[role].yaml`, under `global_tool_config.[new tool]` and `tool_config.[new tool]` +3. The tool config should be implemented in `verl/utils/profiler/config.py`, inherit the `BaseConfig` class. +4. Implement profiling tool initialization logic using configurations in `global_profiler.global_tool_config.[new tool]` and the results saving logics (can also save in role-level profile) +5. For role function-level profiling, please follow the nsys profiler way in `nvtx_profiler.py`, implement a profiler class inherit `DistProfiler` and import new profiler in `verl/utils/profiler/__init__.py` +6. Add unit test and examples for others to use in convinience. \ No newline at end of file diff --git a/verl_0720_main/verl/docs/preparation/prepare_data.rst b/verl_0720_main/verl/docs/preparation/prepare_data.rst new file mode 100644 index 0000000000000000000000000000000000000000..c429e4b167967652a0c3fb52d9e0029f1b9899d4 --- /dev/null +++ b/verl_0720_main/verl/docs/preparation/prepare_data.rst @@ -0,0 +1,128 @@ +Prepare Data for Post-Training +======================================== + +Last updated: 02/09/2025. + +Before starting the post-training job, we need to prepare the data for +the policy training. The data should be stored in the parquet format. + +We provide several data preprocess scripts for different datasets, +including GSM8K, MATH, HelloSwag, Full_hh_rlhf. To prepare other datasets, we need +to follow the following steps: The data preprocess script can be divided +into two parts: + +1. The first part is the common part, which loads the dataset from + huggingface's ``datasets`` package. Then preprocess the datasets with + the ``make_map_fn`` and then store in the parquet format. + +.. code:: python + + import re + import os + import datasets + + from verl.utils.hdfs_io import copy, makedirs + import argparse + + # To extract the solution for each prompts in the dataset + # def extract_solution(solution_str): + # ... + + + if __name__ == '__main__': + parser = argparse.ArgumentParser() + parser.add_argument('--local_dir', default='/opt/tiger/gsm8k') + parser.add_argument('--hdfs_dir', default=None) + + args = parser.parse_args() + + num_few_shot = 5 + data_source = 'openai/gsm8k' + + dataset = datasets.load_dataset(data_source, 'main') + + train_dataset = dataset['train'] + test_dataset = dataset['test'] + + # Construct a `def make_map_fn(split)` for the corresponding datasets. + # ... + + train_dataset = train_dataset.map(function=make_map_fn('train'), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn('test'), with_indices=True) + + local_dir = args.local_dir + hdfs_dir = args.hdfs_dir + + train_dataset.to_parquet(os.path.join(local_dir, 'train.parquet')) + test_dataset.to_parquet(os.path.join(local_dir, 'test.parquet')) + + makedirs(hdfs_dir) + + copy(src=local_dir, dst=hdfs_dir) + +2. The users are required to implement the ``make_map_fn()`` function + (as well as the ``extract_solution``) on their own to support + different datasets or tasks. + +We already implemented the data preprocess of GSM8k, MATH, Hellaswag and Full_hh_rlhf +datasets. And we take the GSM8k dataset as an example: + +**GSM8K** + +In the ``make_map_fn``, each data field should consist of the following +5 fields: + +1. ``data_source``: The name of the dataset. To index the corresponding + reward function in the ``RewardModel`` +2. ``prompt``: This field should be constructed in the format of + huggingface chat_template. The tokenizer in ``RLHFDataset`` will + apply chat template and tokenize the prompt. +3. ``ability``: Define the task category. +4. ``reward_model``: Currently, we only utilize the ``ground_truth`` + field during evaluation. The ``ground_truth`` is computed by the + ``extract_solution`` function. **NOTED** that the implementation of + the corresponding reward function should align with this extracted + ``ground_truth``. +5. ``extra_info``: Record some information of the current prompt. Not + use for now. + +.. code:: python + + def extract_solution(solution_str): + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) # extract the solution after #### + assert solution is not None + final_solution = solution.group(0) + final_solution = final_solution.split('#### ')[1].replace(',', '') + return final_solution + + instruction_following = "Let's think step by step and output the final answer after \"####\"." + + # add a row to each data item that represents a unique id + def make_map_fn(split): + + def process_fn(example, idx): + question = example.pop('question') + + question = question + ' ' + instruction_following + + answer = example.pop('answer') + solution = extract_solution(answer) + data = { + "data_source": data_source, + "prompt": [{ + "role": "user", + "content": question + }], + "ability": "math", + "reward_model": { + "style": "rule", + "ground_truth": solution + }, + "extra_info": { + 'split': split, + 'index': idx + } + } + return data + + return process_fn diff --git a/verl_0720_main/verl/docs/preparation/reward_function.rst b/verl_0720_main/verl/docs/preparation/reward_function.rst new file mode 100644 index 0000000000000000000000000000000000000000..760898cd98c3956b9624c0ad99c29a9dfc838dcf --- /dev/null +++ b/verl_0720_main/verl/docs/preparation/reward_function.rst @@ -0,0 +1,71 @@ +Implement Reward Function for Dataset +====================================== + +Last updated: 06/02/2025. + +For each dataset, we need to implement a reward function or utilize a reward model to compute the rewards for the generated responses. +We already pre-implemented some reward functions in `reward_score directory `_. +You can also use customized reward functions. + +Currently, we support reward functions for GSM8k and MATH datasets. For RLHF datasets (e.g., +full_hh_rlhf) and Code Generation (e.g., APPS), we utilize reward model +and SandBox (will opensource soon) for evaluation respectively. + +RewardManager +------------- + +In the entrypoint of the PPO Post-Training script `main_ppo.py `_, +we implement a ``RewardManager`` that utilize pre-implemented reward functions to compute the scores for each response. + +In the ``RewardManager``, we implemented a ``__call__`` function to +compute the score for each response. +All the reward functions are executed by ``compute_score_fn``. +The input is a ``DataProto``, which includes: + +- ``input_ids``, ``attention_mask``: ``input_ids`` and ``attention_mask`` after applying + chat_template, including prompt and response +- ``responses``: response tokens +- ``ground_truth``: The ground truth string of the current prompt. + Stored in ``non_tensor_batch`` in the ``DataProto``, which should be + preprocessed in the parquet files. +- ``data_source``: The dataset name of the current prompt. Stored in + ``non_tensor_batch`` in the ``DataProto``, which should be + preprocessed in the parquet files. + +After detokenize the responses, the responses string and the ground +truth string will be input to the ``compute_score_fn`` to compute the +score for each response. + +Reward Functions +---------------- + +Pre-implemented +~~~~~~~~~~~~~~~ + +We already pre-implemented some reward functions in `reward_score directory `_. + +- In the `GSM8k example `_, we + force the response to output the final answer after four ####, then + use string matching to compare with the ground truth. If completely + correct, score 1 point; if the format is correct, score 0.1 points; if + the format is incorrect, score 0 points. +- In the `MATH example `_, we follow + the implementation in `lm-evaluation-harness repository `_. + +Customized +~~~~~~~~~~ + +You can implement customized reward functions in a separate file and specify them using ``custom_reward_function.path`` and ``custom_reward_function.name``. For the set of them, please refer to :ref:`config-explain-page`. + +The parameters of your reward function should be ``data_source``, ``solution_str``, ``ground_truth``, and ``extra_info``. +For example: + +.. code:: python + + def my_reward_fn(data_source, solution_str, ground_truth, extra_info=None): + return len(solution_str)/100 + +If you are testing only a single customized reward function, you can simply name it 'compute_score' and leave ``custom_reward_function.name`` unset. + +To run multiple tests with different customized reward functions, you can modify both ``custom_reward_function.path`` and ``custom_reward_function.name`` for each trial. +For instance, you might create a single `my_reward.py` file and implement multiple reward functions within it. This way, for different trials, you only need to adjust ``custom_reward_function.name``, making it more convenient to conduct multiple tests within scripts. diff --git a/verl_0720_main/verl/docs/requirements-docs.txt b/verl_0720_main/verl/docs/requirements-docs.txt new file mode 100644 index 0000000000000000000000000000000000000000..55ccdb8f7149bd6b774b592dca068e63e87256db --- /dev/null +++ b/verl_0720_main/verl/docs/requirements-docs.txt @@ -0,0 +1,13 @@ +# markdown support +recommonmark +myst_parser +# markdown table support +sphinx-markdown-tables + +# theme default rtd + +# crate-docs-theme +sphinx-rtd-theme + +# pin tokenizers version to avoid env_logger version req +tokenizers==0.21 diff --git a/verl_0720_main/verl/docs/sglang_multiturn/multiturn.rst b/verl_0720_main/verl/docs/sglang_multiturn/multiturn.rst new file mode 100644 index 0000000000000000000000000000000000000000..b40418b1ad559ffd2013f68754c84464337c23a8 --- /dev/null +++ b/verl_0720_main/verl/docs/sglang_multiturn/multiturn.rst @@ -0,0 +1,341 @@ +Multi-turn Rollout Support +========================== + +Last updated: 05/09/2026. + +Basic Configuration +~~~~~~~~~~~~~~~~~~~ + +To enable multi-turn rollout, make sure to configure the following fields in your rollout configuration: + +.. code-block:: yaml + + actor_rollout_ref: + rollout: + multi_turn: True + name: "sglang" + +These configuration activates the sglang engine for multi-turn interaction during rollout. + +Custom Tool Configuration +~~~~~~~~~~~~~~~~~~~~~~~~~ + +For custom environment interaction tools, you can implement your own tools based on ``verl.tools.base_tool.BaseTool``. Then, specify your tool configurations in a YAML file: + +.. code-block:: yaml + + tools: + - class_name: "" + config: + type: native + tool_schema: + +For stateless tools that don't need ``BaseTool``'s ``create``/``release`` lifecycle, see the `Function Tool Configuration`_ section below for the simpler ``@function_tool`` API. + +Finally, set the ``tools_config_file`` in your rollout config: + +.. code-block:: yaml + + actor_rollout_ref: + rollout: + tool_kwargs: + tools_config_file: + +This allows integration of customized tool behaviors during actor rollout steps. + +If your tool creates multi-modal inputs, you should return a list of multi-modal inputs in your tool.execute() implementation. + +Image and video should be processed before returning. For example, if you are using Qwen2.5-VL, you can use the following code to get the representations: + +.. code-block:: python + + async def create(self, ...) -> tuple[str, ToolResponse]: + ... + from verl.utils.dataset.vision_utils import process_image, process_video + + img1 = process_image(img1) + video1 = process_video(video1) + + # due to the (image | video) key is ("image" | "video") instead of ("images" | "videos") in vllm, we need to use ("image" | "video") to specify list of images/videos + # link: https://github.com/vllm-project/vllm/blob/3c545c0c3b98ee642373a308197d750d0e449403/vllm/multimodal/parse.py#L205 + return instance_id, ToolResponse(image=[img1, ...], video=[video1, ...], text="...") + + async def execute(self, ...) -> Tuple[str | Dict[str, Any], float, dict]: + ... + from verl.utils.dataset.vision_utils import process_image, process_video + + img1 = process_image(img1) + video1 = process_video(video1) + + # due to the (image | video) key is ("image" | "video") instead of ("images" | "videos") in vllm, we need to use ("image" | "video") to specify list of images/videos + # link: https://github.com/vllm-project/vllm/blob/3c545c0c3b98ee642373a308197d750d0e449403/vllm/multimodal/parse.py#L205 + return ToolResponse(image=[img1, ...], video=[video1, ...], text="..."), 0, {} + +remeber to set ``return_multi_modal_inputs: False`` in your dataset config in order to process the multi-modal inputs in the rollout correctly. +Refer to the `Handling Multi-Modal Inputs in Datasets`_ section for more details. + +Function Tool Configuration +~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +For stateless tools, defining a full ``BaseTool`` subclass plus a yaml schema is overkill. The ``@function_tool`` decorator lets you register a plain Python function as a tool; verl delegates schema inference to :func:`transformers.utils.get_json_schema`, which reads the function signature and a Google-style docstring. + +A typical configuration: + +.. code-block:: yaml + + actor_rollout_ref: + rollout: + mode: async + multi_turn: + enable: True + format: hermes # or any other format your model's chat template supports + function_tool_path: path/to/your_tools.py + agent: + default_agent_loop: tool_agent + +Define your tools in ``path/to/your_tools.py``. The decorator works either bare or with an explicit name; the function name is used as the tool name when bare: + +.. code-block:: python + + from verl.tools.function_tool import function_tool + + @function_tool + def get_weather(city: str) -> dict: + """Get the current weather for a city. + + Args: + city: The city to look up, e.g. "Tokyo" or "San Francisco". + """ + return {"temperature_c": 17.3, "condition": "drizzle"} + + @function_tool("calculator") # explicit name overrides the function name + def calculator(expression: str) -> str: + """Evaluate a Python-style arithmetic expression. + + Args: + expression: A Python-style arithmetic expression, e.g. "(3+4)*5". + """ + return str(eval(expression, {"__builtins__": {}}, {})) + +A few notes on the inferred schema: + +- Parameter types come from the function's type annotations. Beyond the primitives (``str``, ``int``, ``float``, ``bool``), generic and union forms work too: ``list[T]`` / ``dict[K, V]``, ``Optional[X]`` / ``X | None`` (yields ``nullable``), ``int | float`` (yields the JSON ``["integer", "number"]`` type), ``Literal["a", "b"]`` (yields ``enum``). +- Per-argument descriptions come from the ``Args:`` section of the docstring. +- Parameters without a default value are marked ``required``. +- The function may be sync or async; sync functions are dispatched through ``asyncio.to_thread`` so they don't block the event loop. +- ``*args`` / ``**kwargs`` are not representable in JSON Schema and will be rejected at registration time. Use a ``param: list[T]`` argument instead for variable-length inputs. +- Pass ``schema=`` to the decorator if you want to bypass inference entirely and supply your own ``OpenAIFunctionToolSchema`` (or a dict with the same shape). + +If the function violates ``transformers.get_json_schema``'s contract -- no docstring, missing type hint on a parameter, or a parameter that isn't documented in ``Args:`` -- registration raises a ``DocstringParsingException`` or ``TypeHintParsingException`` that points at the offending function. Fix the function rather than catching the exception. + +Return values are normalised the same way for every function tool: + +- ``str`` → wrapped as ``ToolResponse(text=...)`` +- ``dict`` → JSON-serialised into ``ToolResponse(text=...)`` +- ``ToolResponse`` → passed through unchanged +- ``(response, reward)`` or ``(response, reward, metrics)`` tuples are accepted, with ``None`` reward / metrics treated as ``0.0`` / ``{}`` + +``function_tool_path`` and ``tool_config_path`` can be set together. ``AgentLoopWorker`` merges both into one registry once on startup; name collisions across the two paths raise an error. ``RLHFDataset`` shares the same loader so prompt-length filtering sees the exact tool schemas the rollout will. + +The function-tool path intentionally exposes no framework-level lifecycle hooks: each call is just ``fn(**parameters)``, with no ``create``/``release`` or per-trajectory ``instance_id``. For *per-trajectory* state that must be torn down between rollouts (sandbox VMs, scratch directories, DB rows) or that needs ``tools_kwargs`` injected from the dataset, keep using ``BaseTool`` via ``tool_config_path``. + +Multi-turn Tokenization +~~~~~~~~~~~~~~~~~~~~~~~ + +Tokenizing multi-turn rollouts poses a challenge: after applying the chat template and tokenizing the full message list, it's hard to identify which tokens belong to assistant messages. Since the token list is flat, it lacks direct alignment with the message roles. + +To address this, we adopt a **delta-based tokenization** strategy. Each time the LLM generates a new message, we: + +1. Apply the chat template to all prior messages (`messages[:i]`). +2. Apply the chat template again including the latest message (`messages[:i+1]`). +3. Tokenize only the *delta* between these two serialized message strings. + +This ensures that only tokens generated by the assistant are included in the loss mask. + +.. code-block:: python + + # When using tokenizer + # Exclude the assistant prompt (e.g., "<|im_start|>assistant") from the loss by setting add_generation_prompt=True + prev = tokenizer.apply_chat_template(messages[:i], add_generation_prompt=True, tokenize=False) + curr = tokenizer.apply_chat_template(messages[:i+1], add_generation_prompt=False, tokenize=False) + token_ids += tokenizer.encode(curr[len(prev):], add_special_tokens=False) + loss_mask += [1] * len(token_ids) # Mask only the new assistant tokens + +.. code-block:: python + + # When using processor + # Exclude the assistant prompt (e.g., "<|im_start|>assistant") from the loss by setting add_generation_prompt=True + prev = processor.apply_chat_template(messages[:i], add_generation_prompt=True, tokenize=False) + prev_model_inputs = processor(text=prev, images=images, videos=videos, return_tensors="pt")[0].tolist() + curr = processor.apply_chat_template(messages[:i+1], add_generation_prompt=False, tokenize=False) + curr_model_inputs = processor(text=curr, images=images, videos=videos, return_tensors="pt")[0].tolist() + token_ids += curr_model_inputs["input_ids"][len(prev_model_inputs["input_ids"]):] + loss_mask += [1] * len(token_ids) # Mask only the new assistant tokens + +While we've validated this produces consistent results with full message tokenization, future models' chat template could break compatibility. To guard against silent inconsistencies, we compare the delta-based tokenization with full-tokenization results by default at the end of each rollout. + +If you see the following warning, you can check the mismatched substring in the log: + +.. code-block:: + + Inconsistent training and inference tokenization detected. This may lead to unexpected behavior during training. Please review your chat template to determine if this is intentional. For more information, refer to the multiturn README.md. + +The tokenization sanity check mode can be configured using the ``actor_rollout_ref.rollout.multi_turn.tokenization_sanity_check_mode`` parameter, which accepts the following values: + +- ``strict`` (default): Performs strict comparison between delta-based and full tokenization results, raising warnings for any differences. + +- ``ignore_strippable``: Ignores differences in whitespace characters (``\n``, ``\t``, ``\r``, spaces) while still checking for meaningful text mismatches. This is useful when debugging chat template issues where whitespace variations are expected and acceptable. + +- ``disable``: Completely disables the tokenization sanity check. Only use this if you have thoroughly validated that tokenization discrepancies are expected and won't impact training. + +Example configuration: + +.. code-block:: yaml + + actor_rollout_ref: + rollout: + multi_turn: + tokenization_sanity_check_mode: "ignore_strippable" # Choose from: "disable", "ignore_strippable", "strict" + +Handling Multi-Modal Inputs in Datasets +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If your dataset includes multi-modal inputs (such as images or videos), you can control whether these are pre-processed and included in each sample by setting the return_multi_modal_inputs flag in your dataset config (used by RLHFDataset). + +- ``return_multi_modal_inputs: True`` (default): The dataset will pre-process and include a multi_modal_inputs dictionary for each sample. This dict contains the model-ready representations (e.g., image tensors, video tensors, etc.) as produced by your processor. This is useful for single-turn or SFT-style training, where the model expects all modalities to be present in the batch. + +- ``return_multi_modal_inputs: False``: The dataset will not include the multi_modal_inputs field. This is recommended for multi-turn RL or tool-augmented rollouts, where the model may generate new multi-modal inputs dynamically during rollout, and you want to avoid conflicts or redundant data in the batch. + + +Special Cases +^^^^^^^^^^^^^ + +Some models (e.g., Qwen/QwQ-32B and Qwen3 series) remove internal reasoning content during chat template rendering. As a result, the message content can vary across turns, making the delta-based tokenization inaccurate. + +For example, for the following conversation: + +.. code-block:: python + + messages = [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What is 2 + 2?"}, + {"role": "assistant", "content": "user asked about a simple math question. 2 + 2 = 4."}, + {"role": "user", "content": "Explain why."}, + {"role": "assistant", "content": "user wants to know the reasoning behind the answer. Search for a good explanation", + "tool_calls": [{"id": "tool1", "type": "search", "arguments": {"query": "Why is 2 + 2 = 4?"}}]}, + {"role": "tool", "content": "The sum of two and two is four because it is a basic arithmetic operation."}, + {"role": "assistant", "content": "The tool provided a good explanation.The sum of two and two is four because it is a basic arithmetic operation."} + ] + +1. Qwen/QwQ-32B will remove all reasoning content except the last assistant message after applying the chat template. + +.. code-block:: text + + <|im_start|>system + You are a helpful assistant.<|im_end|> + <|im_start|>user + What is 2 + 2?<|im_end|> + <|im_start|>assistant + 2 + 2 = 4.<|im_end|> + <|im_start|>user + Explain why.<|im_end|> + <|im_start|>assistant + + {"name": "", "arguments": {"query": "Why is 2 + 2 = 4?"}} + <|im_end|> + <|im_start|>user + + The sum of two and two is four because it is a basic arithmetic operation. + <|im_end|> + <|im_start|>assistant + The tool provided a good explanation. The sum of two and two is four because it is a basic arithmetic operation.<|im_end|> + +2. Qwen3 series will remove all reasoning content before the last user message. + +.. code-block:: text + + <|im_start|>system + You are a helpful assistant.<|im_end|> + <|im_start|>user + What is 2 + 2?<|im_end|> + <|im_start|>assistant + 2 + 2 = 4.<|im_end|> + <|im_start|>user + Explain why.<|im_end|> + <|im_start|>assistant + + user wants to know the reasoning behind the answer. Search for a good explanation + + + + {"name": "", "arguments": {"query": "Why is 2 + 2 = 4?"}} + <|im_end|> + <|im_start|>user + + The sum of two and two is four because it is a basic arithmetic operation. + <|im_end|> + <|im_start|>assistant + + The tool provided a good explanation. + + + The sum of two and two is four because it is a basic arithmetic operation.<|im_end|> + +To handle this, we fall back to a **fixed base conversation** containing only a single system and user message. Since this base doesn't include assistant messages or reasoning content, it remains consistent across turns. + +.. code-block:: python + + BASE_CHAT_HISTORY = [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "I am a user."} + ] + prev = tokenizer.apply_chat_template(BASE_CHAT_HISTORY, add_generation_prompt=True, tokenize=False) + curr = tokenizer.apply_chat_template([*BASE_CHAT_HISTORY, messages[i]], add_generation_prompt=False, tokenize=False) + token_ids += tokenizer.encode(curr[len(prev):], add_special_tokens=False) + loss_mask += [1] * len(token_ids) + +This method works well for Qwen3 series. However, Qwen/QwQ-32B currently has a bug in its chat template. A fix_ has been proposed but not yet adopted. Until then, use the following command to download the fixed model revision: + +.. code-block:: bash + + pip install huggingface_hub + hf download Qwen/QwQ-32B --revision refs/pr/81 + +.. _fix: https://huggingface.co/Qwen/QwQ-32B/discussions/81 + +Discrepancy Between Training and Inference Templates +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Although the above approach fixes the delta mismatch issue, the removal of reasoning content in the inference-time chat template introduces a new discrepancy: training uses the full reasoning content, while inference does not. + +This mismatch can affect model performance in unpredictable ways. To avoid it, we default to using the full response (including reasoning) for both training and rollout. + +However, this approach comes with trade-offs: + +1. Long reasoning contents can easily exceed the model's context window, especially in multi-turn rollout. +2. There's a mismatch between rollout and production environment now—models will not have reasoning content from past turns if you use the default chat template in production. + +We are still evaluating the impact of these issues. If you experience context length problems or prefer rollouts that match production (i.e., exclude reasoning), you can enable: + +``actor_rollout_ref.rollout.multi_turn.use_inference_chat_template = True`` + +GSM8K Multi-turn Training Performance +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +See the training performance of multi-turn rollout on the GSM8K task HERE_. + +.. _HERE: https://wandb.ai/zhaochenyang20/gsm8k_async_rl/runs/1ro1r7om?nw=nwuserzhaochenyang20 + +.. _function_tool_examples: https://github.com/verl-project/verl/blob/main/tests/experimental/agent_loop/function_tool_examples.py + +Search Tool Integration +~~~~~~~~~~~~~~~~~~~~~~~ + +.. toctree:: + :maxdepth: 1 + + search_tool_example + +Code Walkthrough +~~~~~~~~~~~~~~~~~~~~~~~ +If you want to learn more in depth about the code execution flow, please read https://github.com/zhaochenyang20/Awesome-ML-SYS-Tutorial/tree/main/rlhf/verl/multi-turn/code-walk-through diff --git a/verl_0720_main/verl/docs/sglang_multiturn/sandbox_fusion.rst b/verl_0720_main/verl/docs/sglang_multiturn/sandbox_fusion.rst new file mode 100644 index 0000000000000000000000000000000000000000..995635627ed24d2e172f9913189a9eefcd869d79 --- /dev/null +++ b/verl_0720_main/verl/docs/sglang_multiturn/sandbox_fusion.rst @@ -0,0 +1,312 @@ +=============================== +Sandbox Fusion Tool Integration +=============================== + +Last updated: 05/09/2026. + +.. note:: + + The in-tree ``verl.tools.sandbox_fusion_tools.SandboxFusionTool`` reference + implementation discussed in this guide has been removed. The integration + pattern below remains accurate for users implementing their own + :class:`verl.tools.base_tool.BaseTool` subclass; for the previous + implementation see git history at commit ``f5e21df6`` and earlier. + +Motivations +=========== + +- As users of verl, we want to allow the model to call certain tools during Actor rollout, incorporating the results into the training process. +- A colleague from ByteDance proposed a paper aimed at enhancing model capability through code execution tools. +- We aim to support tool-calling capabilities of inference engines using `sandbox-fusion` as the code execution system, providing the community with a reimplementation of `retools`. + +Reward Compute with Sandbox Fusion + FaaS Integration +===================================================== + +- In current datasets and tasks, similar work already exists (e.g., Prime), which uses local processes as runners to execute model-generated code for reward computation. +- On this basis, #1429 has advanced the design by integrating FaaS as the runner for reward computation. + +Goals +===== + +- Adapt to the `sglang` tool-calling protocol and define tools for sandbox fusion. +- Integrate with the `async-rollout` process, ensuring sandbox fusion tools follow asyncIO conventions. +- Design and implement a basic rate limiter to prevent issues such as 429 errors. + +Non-Goals +========= + +- Training effectiveness is out of scope. +- Observability metrics are not considered. +- Distributed failover and component fault tolerance are not addressed. + +Design Details +============== + +Tool Schema Definition +---------------------- + +- Currently, only code execution is considered, requiring a `code` field in the JSON from the model. +- Only Python code is supported for now, so no `language` parameter is defined. + +.. code-block:: python + + OpenAIFunctionToolSchema( + type="function", + function=OpenAIFunctionSchema( + name="code_interpreter", + description="A tool for executing code.", + parameters=OpenAIFunctionParametersSchema( + type="object", + properties={ + "code": OpenAIFunctionPropertySchema( + type="string", + description="The code to execute.", + enum=None, + ) + }, + required=["code"], + ), + strict=False, + ) + ) + +Configuration Parameters +-------------------------- + ++----------------------------+--------------------------------------------------------------+ +| Parameter Name | Description | ++============================+==============================================================+ +| `num_workers` | Number of worker threads/processes per DP to request runner. | ++----------------------------+--------------------------------------------------------------+ +| `rate_limit` | Global limit of concurrent code executions. Default: 10 | ++----------------------------+--------------------------------------------------------------+ +| `default_timeout` | Timeout (in seconds) for each code execution. Default: 30 | ++----------------------------+--------------------------------------------------------------+ +| `default_language` | Default programming language. Default: "python" | ++----------------------------+--------------------------------------------------------------+ +| `enable_global_rate_limit` | Whether to enable global rate limiting. Default: True | ++----------------------------+--------------------------------------------------------------+ +| `sandbox_fusion_url` | URL for the veFaas sandbox execution service | ++----------------------------+--------------------------------------------------------------+ + +Rate Limiting Design +----------------------- + +Objective: + +- Limit the number of inflight requests using a token bucket model. + +- Ensure ordered submission to code runners to avoid starvation due to backoff. + +Design Highlights: + +- Use Ray Global Actor as a singleton distributed counter at cluster level. + +- Semaphore used for counting, with `acquire` and `release` in separate thread pools to preserve order. + +- Use Ray’s cloud-pickle to serialize functions for decoupled `ExecutionWorker`. + +.. code-block:: python + + @ray.remote(concurrency_groups={"acquire": 1,"release": 10}) + class TokenBucketWorker: + def __init__(self, rate_limit: int): + self.rate_limit = rate_limit + self.current_count = 0 + self._semaphore = threading.Semaphore(rate_limit) + + @ray.method(concurrency_group="acquire") + def acquire(self): + self._semaphore.acquire() + self.current_count += 1 + + @ray.method(concurrency_group="release") + def release(self): + self._semaphore.release() + self.current_count -= 1 + + def get_current_count(self): + return self.current_count + + class ExecutionWorker: + def __init__(self, enable_global_rate_limit=True, rate_limit=10): + self.rate_limit_worker = self._init_rate_limit(rate_limit) if enable_global_rate_limit else None + + def _init_rate_limit(self, rate_limit): + return TokenBucketWorker.options(name="rate-limiter", get_if_exists=True).remote(rate_limit) + + def execute(self, fn: Callable[..., T], *fn_args, **fn_kwargs) -> T: + with ExitStack() as stack: + stack.callback(self.rate_limit_worker.release.remote) + ray.get(self.rate_limit_worker.acquire.remote()) + try: + return fn(*fn_args, **fn_kwargs) + except Exception as e: + logger.warning(f"Error when executing code: {e}") + + def init_execution_pool(num_workers: int, enable_global_rate_limit=True, rate_limit=10, mode: PoolMode=PoolMode.ThreadMode): + if mode == PoolMode.ThreadMode: + return ray.remote(ExecutionWorker).options(max_concurrency=num_workers).remote( + enable_global_rate_limit=enable_global_rate_limit, + rate_limit=rate_limit + ) + else: + raise NotImplementedError("Process mode is not implemented yet") + +Tool Implementation +------------------- + +- Use `instance_id` to identify requests across multiple dialogue rounds. + +- Use `execution_pool` to implement async invocation. + +- Cleanup state after rollout completion. + +.. code-block:: python + + class SandboxFusionTool(BaseTool): + def __init__(self, config: dict, tool_schema: OpenAIFunctionToolSchema): + ... + self.execution_pool = init_execution_pool(...) + ... + + async def create(self, instance_id: Optional[str] = None, ...): + ... + + async def execute(self, instance_id: str, parameters: dict[str, Any], **kwargs) -> Tuple[str, float, dict]: + code = parameters.get("code", "") + timeout = parameters.get("timeout", self.default_timeout) + language = parameters.get("language", self.default_language) + if not isinstance(code, str): + code = str(code) + + result = await self.execution_pool.execute.remote(self.execute_code,instance_id,code,timeout,language) + self._instance_dict[instance_id]["reward"].append(result.strip()) + + return result, result, {} + + def execute_code(self,instance_id,code,timeout=30,language="python"): + result_status, metadata = _process_single_case(0, None, None,self.sandbox_fusion_url, code, timeout, language) + # we should always expect this since we don't have correct answer + if metadata["run_status"] == "Finished": + actual_output = metadata["stdout"] if metadata["stdout"] is not None else "" + return actual_output + else: + return "no stdout here" + + async def calc_reward(self, instance_id: str, ...): + ... + + async def release(self, instance_id: str, ...): + ... + +Test Plan +========= + +Unit Tests +---------- + +- **test_tools_registration**: Test tool registration and initialization. +- **test_rollout_req_creation**: Validate that `AsyncRolloutReq` is built correctly. +- **test_over_size_case**: Ensure rollout terminates early when exceeding `max_seq_len`. +- **test_tool_call_basic_case**: Mock `sglang` output, validate tool call and result. +- **test_tool_call_batch_case**: Test batch processing of tool calls. +- **test_basic_multi_process_init**: Validate Ray global actor behaves as singleton. +- **TestSingleNodeRateLimiterCase**: Verify rate limiter works in single-node mode. +- **test_rotten_execution**: Ensure rate limiter recovers from function errors. +- **TestMultiNodeRateLimiterCase**: Verify behavior in multi-node environments. + +e2e Tests +---------- +we provide e2e test scripts in `tests/special_e2e` folder, named `tests/special_e2e/run_gsm8k_fsdp_sgl_multiturn_sf_tool.sh` + +by setting 'trainer.rollout_data_dir' you can dump the rollout data to local disk. here is an sample taken from the rollout data: + +.. code-block:: python + + { + "input": " + + system\nYou are a math expert. You are given a question and you need to solve it step by step. Reasoning step by step before any tool call. You should use the `calc_gsm8k_reward` tool after step by step solving the question, before generate final answer at least once and refine your answer if necessary. Put your final answer in the format of `#### `.\n\n# Tools\n\nYou may call one or more functions to assist with the user query.\n\nYou are provided with function signatures within XML tags:\n\n{\"type\": \"function\", \"function\": {\"name\": \"code_interpreter\", \"description\": \"A tool for executing code.\", \"parameters\": {\"type\": \"object\", \"properties\": {\"code\": {\"type\": \"string\", \"description\": \"The code to execute.\", \"enum\": null}}, \"required\": [\"code\"]}, \"strict\": false}}\n\n\nFor each function call, return a json object with function name and arguments within XML tags:\n\n{\"name\": , \"arguments\": }\n\n + + user\nJohn gets a bonus that's the same percentage every year. Last year he made $100,000 and got a $10,000 bonus. This year he makes $200,000. How much will John make this year when adding both his total pay and bonus together? Let's think step by step and output the final answer after `####`.\n + + assistant\n", + "output": "assistant\n\nOkay, let's see. John gets a bonus that's the same percentage every year. Last year, he made $100,000 and got a $10,000 bonus. This year, he makes $200,000. The question is asking how much he'll make this year when adding both his total pay and bonus together.\n\nFirst, I need to figure out the percentage bonus he gets. Since the bonus is the same percentage each year, if I can find that percentage based on last year's details, I can apply it to this year's earnings to find this year's bonus. Then, I'll add that bonus to his total pay this year.\n\nLast year, he earned $100,000 and got a $10,000 bonus. To find the percentage, I can divide the bonus by his total earnings. So, 10,000 divided by 100,000. Let me calculate that.\n\n10,000 / 100,000 equals 0.1, which is 10% as a percentage. So, his bonus is 10% of his pay each year.\n\nThis year, he makes $200,000. So, the bonus this year would be 10% of 200,000. Let me compute that using the code interpreter to ensure accuracy.\n + + \n{\"name\": \"code_interpreter\", \"arguments\": {\"code\": \"total_pay_this_year = 200000\\nbonus_percentage = 10 / 100\\nbonus_this_year = total_pay_this_year * bonus_percentage\\ntotal_income_this_year = total_pay_this_year + bonus_this_year\\nprint(total_income_this_year)\", \"executes\": \"True\"}}\n\n + + tool\n220000.0\n\n + + assistant\n\nSo the code shows that John's total income this year would be $220,000. This is consistent with the 10% bonus on last year's pay of $100,000, which was $10,000. Therefore, this year's bonus of $20,000 added to his total pay of $200,000 results in a total income of $220,000.\n\nI need to verify that there are no other factors at play. The problem states it's a constant percentage bonus each year, so there's no mention of changing percentages or other variables. Therefore, applying a straightforward percentage of 10% on this year's partial salary amount would result in $20,000. Adding that to his total pay gives $220,000. The calculation is correct and aligns with the question's parameters.\n\n\n#### 220000.0", + "score": 0, + "step": 1 + } + +here is the readable format version: + +.. code-block:: python + + [system] + + You are a math expert. You are given a question and you need to solve it step by step. Reasoning step by step before any tool call. You should use the `calc_gsm8k_reward` tool after step by step solving the question, before generate final answer at least once and refine your answer if necessary. Put your final answer in the format of `#### `. + + # Tools + + You may call one or more functions to assist with the user query. + + You are provided with function signatures within XML tags: + + {"type": "function", "function": {"name": "code_interpreter", "description": "A tool for executing code.", "parameters": {"type": "object", "properties": {"code": {"type": "string", "description": "The code to execute.", "enum": null}}, "required": ["code"]}, "strict": false}} + + + For each function call, return a json object with function name and arguments within XML tags: + + {"name": , "arguments": } + + + [user] + + John gets a bonus that's the same percentage every year. Last year he made $100,000 and got a $10,000 bonus. This year he makes $200,000. How much will John make this year when adding both his total pay and bonus together? Let's think step by step and output the final answer after `####`. + + [assistant] + + + Okay, let's see. John gets a bonus that's the same percentage every year. Last year, he made $100,000 and got a $10,000 bonus. This year, he makes $200,000. The question is asking how much he'll make this year when adding both his total pay and bonus together. + + First, I need to figure out the percentage bonus he gets. Since the bonus is the same percentage each year, if I can find that percentage based on last year's details, I can apply it to this year's earnings to find this year's bonus. Then, I'll add that bonus to his total pay this year. + + Last year, he earned $100,000 and got a $10,000 bonus. To find the percentage, I can divide the bonus by his total earnings. So, 10,000 divided by 100,000. Let me calculate that. + + 10,000 / 100,000 equals 0.1, which is 10% as a percentage. So, his bonus is 10% of his pay each year. + + This year, he makes $200,000. So, the bonus this year would be 10% of 200,000. Let me compute that using the code interpreter to ensure accuracy. + + {"name": "code_interpreter", "arguments": {"code": "total_pay_this_year = 200000\nbonus_percentage = 10 / 100\nbonus_this_year = total_pay_this_year * bonus_percentage\ntotal_income_this_year = total_pay_this_year + bonus_this_year\nprint(total_income_this_year)", "executes": "True"}} + + + [tool] + + 220000.0 + + [assistant] + + + So the code shows that John's total income this year would be $220,000. This is consistent with the 10% bonus on last year's pay of $100,000, which was $10,000. Therefore, this year's bonus of $20,000 added to his total pay of $200,000 results in a total income of $220,000. + + I need to verify that there are no other factors at play. The problem states it's a constant percentage bonus each year, so there's no mention of changing percentages or other variables. Therefore, applying a straightforward percentage of 10% on this year's partial salary amount would result in $20,000. Adding that to his total pay gives $220,000. The calculation is correct and aligns with the question's parameters. + + + #### 220000.0 + + +You can also use the `RolloutViewer` TUI tool to view the dumped rollout data: + + +.. code-block:: bash + + python scripts/rollout_viewer.py ${trainer.rollout_data_dir} + + +.. image:: https://github.com/user-attachments/assets/e34e5157-2880-4a21-afb2-73885d0dfb11 + :alt: RolloutViewer screenshot \ No newline at end of file diff --git a/verl_0720_main/verl/docs/sglang_multiturn/search_tool_example.rst b/verl_0720_main/verl/docs/sglang_multiturn/search_tool_example.rst new file mode 100644 index 0000000000000000000000000000000000000000..13dfeebdfe3489c66ea45150bccce5edfb9d7ab3 --- /dev/null +++ b/verl_0720_main/verl/docs/sglang_multiturn/search_tool_example.rst @@ -0,0 +1,272 @@ +======================= +Search Tool Integration +======================= + +Last updated: 05/09/2026. + +.. note:: + + The in-tree ``verl.tools.search_tool.SearchTool`` reference implementation + used throughout this guide has been removed. The end-to-end recipe + (config, retrieval server, training script) still works as documentation + of the integration pattern, but the ``SearchTool`` class must now be + provided by users (see :class:`verl.tools.base_tool.BaseTool`). + +Introduction +------------ +- We have added a search tool calling function to Multi-Turn RL, enabling the model to initiate retrieval requests during Actor rollout and directly use retrieval results for training. **We support using a local dense retriever as the retrieval tool, as well as integrating with your own local retrieval engine.** + + + +Quick Reproduction +------------------ + +Create a New Docker Container +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + docker run \ + -it \ + --shm-size 32g \ + --gpus all \ + -v {Huggingface-Cache-Path}:/root/.cache \ + --ipc=host \ + --network=host \ + --privileged \ + --name sglang_{your-name} \ + lmsysorg/sglang:dev \ + /bin/zsh + +If you need to restart after exiting the container: + +.. code:: bash + + docker start -i sglang_{your-name} + +Update Python and Configure the Virtual Environment using uv +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + apt update + apt install -y python3.10 python3.10-venv + + # Create a virtual environment + python3 -m venv ~/.python/verl-multiturn-rollout + + # Activate the virtual environment + source ~/.python/verl-multiturn-rollout/bin/activate + + # Install uv + python3 -m pip install uv + +Install verl Upstream +~~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + cd ~ + git clone https://github.com/verl-project/verl.git + cd verl + + # Install verl + python3 -m uv pip install . + python3 -m uv pip install -r ./requirements_sglang.txt + + # Manually install flash-attn + python3 -m uv pip install wheel + python3 -m uv pip install packaging + python3 -m uv pip install flash-attn --no-build-isolation --no-deps + +Set Up a Local Retrieval Engine +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +If you are using your own local retrieval service, you can skip this +step. We chose the local dense retriever provided in the search-R1 +example; detailed instructions are in the `searchR1 +docs `__. +In brief: + +- The GPU version offers higher accuracy and speed; each GPU uses about + 5–7 GB of memory. +- The CPU version can be used for simple testing but has lower + retrieval precision, which will degrade training performance. See the + `retriever + documentation `__ + in search-R1 for details. +- Recommend using Conda to install faiss-gpu=1.8.0; venv may cause errors. + +**Note**: To start both the training process and the local retrieval +service, we launch two separate Python environments. The training uses +uv in the verl-multiturn-rollout environment, while the retriever uses +conda to install ``faiss-gpu``. + +.. code:: bash + + # Download the Miniconda installer script + wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh + + # Install to $HOME/miniconda3 in batch mode + bash ~/miniconda.sh -b -p $HOME/miniconda3 + + # Activate conda (only in the current shell) + eval "$($HOME/miniconda3/bin/conda shell.bash hook)" + + # (Optional) Add conda to your default shell startup + conda init + + # Reload shell config + source ~/.bashrc + + # Create and activate the retriever environment with Python 3.10 + conda create -n retriever python=3.10 -y + conda activate retriever + + # Install PyTorch (with GPU support) and related libraries + conda install pytorch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 pytorch-cuda=12.1 -c pytorch -c nvidia -y + + # Install other Python packages + pip install transformers datasets pyserini huggingface_hub + + # Install the GPU version of faiss + conda install faiss-gpu=1.8.0 -c pytorch -c nvidia -y + + # Install the API service framework + pip install uvicorn fastapi + +Download the Indexing and Corpus +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The local retrieval files are large—prepare sufficient disk space. +Downloading is about 60–70 GB, and uncompressed takes about 132 GB: + +.. code:: bash + + conda activate retriever + + save_path=/the/path/to/save + python examples/sglang_multiturn/search_r1_like/local_dense_retriever/download.py --save_path $save_path + cat $save_path/part_* > $save_path/e5_Flat.index + gzip -d $save_path/wiki-18.jsonl.gz + +Start the Local flat e5 Retrieval Server +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. The first startup will download models and load the index. +2. Apart from the download, startup takes about 1–2 minutes. +3. After startup, each GPU uses about 5–7 GB of memory, leaving the rest + for multi-turn RL training. + +.. code:: bash + + conda activate retriever + + index_file=$save_path/e5_Flat.index + corpus_file=$save_path/wiki-18.jsonl + retriever_name=e5 + retriever_path=intfloat/e5-base-v2 + + python examples/sglang_multiturn/search_r1_like/local_dense_retriever/retrieval_server.py \ + --index_path $index_file \ + --corpus_path $corpus_file \ + --topk 3 \ + --retriever_name $retriever_name \ + --retriever_model $retriever_path \ + --faiss_gpu + +Set Up WANDB_API_KEY +~~~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + export WANDB_API_KEY={YOUR_WANDB_API_KEY} + + # Define a timestamp function + function now() { + date '+%Y-%m-%d-%H-%M' + } + +**Preprocess the Dataset** +~~~~~~~~~~~~~~~~~~~~~~~~~~ + + **Note:** The following data processing and training commands must be + run in the verl-multiturn-rollout environment. + +.. code:: bash + + python3 examples/data_preprocess/preprocess_search_r1_dataset.py + +Testing on 8 x H20 +~~~~~~~~~~~~~~~~~~ + +.. code:: bash + + # Ensure the now() function is defined + # Create a logs directory + mkdir -p logs + + # Set GPUs and run with a suitable log path + export CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + + nohup bash examples/sglang_multiturn/search_r1_like/run_qwen2_5_3b_search_multiturn_fsdp.sh \ + trainer.experiment_name=qwen2.5-3b-it_rm-searchR1-like-sgl-multiturn-$(now) \ + > logs/searchR1-like$(now).log 2>&1 & + +Custom Search Configuration +--------------------------- + +To enable multi-turn reasoning, set the following fields in your config: + +.. code:: yaml + + actor_rollout_ref: + rollout: + name: "sglang" + multi_turn: + enable: True + +You must specify ``retrieval_service_url`` in ``examples/sglang_multiturn/config/tool_config/search_tool_config.yaml``, and properly configure concurrency. For more details on concurrency, refer to the Sandbox Fusion example: + +.. code:: yaml + + tools: + - class_name: verl.tools.search_tool.SearchTool + config: + retrieval_service_url: http://127.0.0.1:8000/retrieve + num_workers: 120 + rate_limit: 120 + timeout: 30 + +The retriever input/output formats are as follows. If your service +parameters match, only modify ``retrieval_service_url``. You can also +customize in ``search_r1_like_utils.py``. + +.. code:: python + + Input format: + { + "queries": ["What is Python?", "Tell me about neural networks."], + "topk": 3, + "return_scores": true + } + + Output format (when return_scores=True, similarity scores are returned): + { + "result": [ + [ # Results for each query + { + "document": doc, "score": score + }, + # ... more documents + ], + # ... results for other queries + ] + } + +Notes +----- + +1. The total training time is about 27 hours; meanwhile, the validation + dataset is very large (51 k), and each validation takes about 6000 s. + (Therefore, ``val_before_train=False`` by default) diff --git a/verl_0720_main/verl/docs/single_controller.rst b/verl_0720_main/verl/docs/single_controller.rst new file mode 100644 index 0000000000000000000000000000000000000000..03cde0f4b1a4cb6ed748290ca1de0f709b41eb33 --- /dev/null +++ b/verl_0720_main/verl/docs/single_controller.rst @@ -0,0 +1,336 @@ +The Design of ``verl.single_controller`` +============================================== + +Last updated: 05/21/2025. + +**Author:**\ `Wang Zhang `__ + +Preface +------- + +We prepared this document for developers of ``verl``, particularly those +interested in understanding or contributing to the +``verl.single_controller`` module. It is not intended for end users, but +for contributors seeking to understand the architectural rationale and +internal mechanics. + +-------------- + +Origin +------ + +The ``single_controller`` module originated from a request I received — +to adapt a toy single-process RLHF script into a distributed system with +minimal changes, while maintaining ease of debugging. + +Common practice — such as using PyTorch’s Distributed Data Parallel +(DDP) — typically involves wrapping ``nn.Module`` and launching multiple +processes that execute the same function under different ranks. However, +this approach presents two main limitations in the context of +distributed RLHF: - Difficulty representing multiple DAGs as required by +PPO; - Difficulty inspecting intermediate tensors during training. + +To maintain debuggability, we opted for a different approach — breaking +the training loop into well-defined stages like ``generate_sequences``, +``compute_advantages``, and so on. + +We selected `Ray `__ as the initial backend for +``verl`` due to its ability to expose Python class methods as RPC +endpoints. However, Ray’s default model only supports **one method call, +one RPC**, while training LLMs typically requires coordination across +multiple processes. + +To hide this multi-Ray actors invocation for a single method from users, +we introduced the following components: + +- ``WorkerGroup`` – manages a group of remote workers and provides + a unified interface for multi-process distributed computation; +- ``ResourcePool`` – binds computational resources to worker + processes; +- ``ClassWithArgs`` – enables delayed remote instantiation with + specified initialization arguments. + +-------------- + +A Running Example: ``generate_sequences`` +----------------------------------------- + +To illustrate the design, we walk through how the ``generate_sequences`` +method in the ``ActorRolloutRefWorker`` class is registered and invoked +across distributed workers. + +-------------- + +Step 1: Register with a Decorator +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The first step is to define the ``generate_sequences`` and decorate it +with ``@register`` as it will be called in driver script. + +**Source:** +`engine_workers.py `__ + +.. code:: python + + class ActorRolloutRefWorker(Worker): + ... + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def generate_sequences(self, prompts: DataProto): + prompts = prompts.to(torch.cuda.current_device()) + ... + +The ``@register`` decorator adds metadata to the ``generate_sequences`` +method. Currently, it doesn’t alter functionality, but attaches +attributes via a magic key (``MAGIC_ATTR``): + +**Source:** +`decorator.py `__ + +.. code:: python + + def register(dispatch_mode=Dispatch.ALL_TO_ALL, execute_mode=Execute.ALL, blocking=True, materialize_futures=True): + ... + def decorator(func): + @wraps(func) + def inner(*args, **kwargs): + if materialize_futures: + args, kwargs = _materialize_futures(*args, **kwargs) + return func(*args, **kwargs) + + attrs = {"dispatch_mode": dispatch_mode, "execute_mode": execute_mode, "blocking": blocking} + setattr(inner, MAGIC_ATTR, attrs) + return inner + + return decorator + +As the code shows, values of ``dispatch_mode``, ``execute_mode`` and +``blocking`` is attached the ``generate_sequences`` method. + +-------------- + +Step 2: Binding During Initialization +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +These attached attributes are extracted and utilized when +``ActorRolloutRefWorker``, wrapped in a ``RayClassWithArgs``, is passed +into a ``RayWorkerGroup``. + +**Source:** +`main_generation.py `__ + +.. code:: python + + ray_cls_with_init = RayClassWithInitArgs(cls=ray.remote(ActorRolloutRefWorker), config=config, role="rollout") + resource_pool = RayResourcePool(process_on_nodes=[config.trainer.n_gpus_per_node] * config.trainer.nnodes) + wg = RayWorkerGroup(resource_pool=resource_pool, ray_cls_with_init=ray_cls_with_init) + +During the +`initialization `__ +of ``RayWorkerGroup``, two key steps occur: + +1. Worker instances (Ray actors) are created: + `RayWorkerGroup._init_with_resource_pool `__ +2. Methods decorated with ``@register`` are bound to ``RayWorkerGroup``: + `RayWorkerGroup._bind_worker_method `__ + +.. figure:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/worker_group_init.png?raw=true + :alt: initialization_and_binding_of_worker_group + + initialization_and_binding_of_worker_group + +The binding procedure is the heart of ``verl.single_controller``. + +**Key function:** +`WorkerGroup._bind_worker_method `__ + +.. code:: python + + def _bind_worker_method(self, user_defined_cls, func_generator): + ... + for method_name in dir(user_defined_cls): + try: + method = getattr(user_defined_cls, method_name) + assert callable(method) + except Exception: + continue # Skip properties + <<>> + +When a method has the ``MAGIC_ATTR``, the attributes set by +``@register`` are extracted: + +.. code:: python + + <<>> + if hasattr(method, MAGIC_ATTR): + attribute = getattr(method, MAGIC_ATTR) + dispatch_mode = attribute["dispatch_mode"] + execute_mode = attribute["execute_mode"] + blocking = attribute["blocking"] + + <<>> + +As show in the flow chart above, these attributes are fed into +``func_generator``. However, ``func_generator`` takes ``method_name``, +``dispatch_fn``, ``collect_fn``, ``execute_fn``, ``blocking``. We need +to find the corresponding ``dispatch_fn`` and ``collect_fn`` associated +with the ``dispatch_mode`` (``DP_COMPUTE_PROTO``) from +`DISPATCH_MODE_FN_REGISTRY `__: + +.. code:: python3 + + DISPATCH_MODE_FN_REGISTRY = { + Dispatch.ONE_TO_ALL: { + "dispatch_fn": dispatch_one_to_all, + "collect_fn": collect_all_to_all, + }, + ... + Dispatch.DP_COMPUTE_PROTO: { + "dispatch_fn": dispatch_dp_compute_data_proto, + "collect_fn": collect_dp_compute_data_proto, + }, + ... + } + +Similarly, the ``execute_fn`` is selected by ``execute_mode`` and +extracted by: + +.. code:: python + + <<>> + # get execute_fn_name + execute_mode = get_predefined_execute_fn(execute_mode=execute_mode) + wg_execute_fn_name = execute_mode["execute_fn_name"] + + # get execute_fn from string + try: + execute_fn = getattr(self, wg_execute_fn_name) + assert callable(execute_fn), "execute_fn must be callable" + except Exception: + print(f"execute_fn {wg_execute_fn_name} is invalid") + raise + <<>> + +In this ``generate_sequences`` cases: - +``dispatch_mode = Dispatch.DP_COMPUTE_PROTO`` - +``dispatch_fn = dispatch_dp_compute_data_proto`` - +``collect_fn = collect_dp_compute_data_proto`` - +``execute_fn = RayWorkerGroup.execute_all`` + +ONE_TO_ALL v.s. DP_COMPUTE_PROTO +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +``dispatch_mode`` is associated with a ``dispatch_fn`` and a +``collect_fn``. As the name implies, ``dispatch_fn`` processes the input +arguments in ``WorkerGroup`` and generate a batch (list) of input +arguments, each of which will be fed into a worker attached to the +``WorkerGroup``. + +``dispatch_fn`` of ``ONE_TO_ALL`` is +`dispatch_one_to_all `__, +which just duplicates all the input arguments into N replicas, where N +equals the number of Workers attached to the ``worker_group``: + +.. code:: python + + def dispatch_one_to_all(worker_group, *args, **kwargs): + args = tuple([arg] * worker_group.world_size for arg in args) + kwargs = {k: [v] * worker_group.world_size for k, v in kwargs.items()} + return args, kwargs + +``dispatch_fn`` of ``DP_COMPUTE_PROTO`` is +`dispatch_dp_compute_data_proto `__, +which uses ``DataProto.chunk`` to split a large ``DataProto`` into N +smaller ``DataProto``, where N equals the world_size (number of the +workers) of the ``worker_group``: + +.. code:: python + + def dispatch_dp_compute_data_proto(worker_group, *args, **kwargs): + from verl.single_controller.base.worker_group import WorkerGroup + + assert isinstance(worker_group, WorkerGroup) + # Note: enable auto padding for dp compute DatapProto + splitted_args, splitted_kwargs = _split_args_kwargs_data_proto_with_auto_padding( + worker_group.world_size, + *args, + **kwargs, + ) + return splitted_args, splitted_kwargs + +The ``collect_fn`` follows the same pattern and process a batch (list) +of returned value from all workers of a ``WorkerGroup`` and merge it +into a list as ``collect_all_to_all`` does or a large ``DataProto`` as +``collect_dp_compute_data_proto`` does. + +Finally, a new method is dynamically generated using ``func_generator`` +and added to the ``WorkerGroup`` instance: + +.. code:: python + + <<>> + # bind a new method to the RayWorkerGroup + func = func_generator( + self, + method_name, + dispatch_fn=dispatch_fn, + collect_fn=collect_fn, + execute_fn=execute_fn, + blocking=blocking, + ) + + try: + setattr(self, method_name, func) + method_names.append(method_name) + except Exception as e: + raise ValueError(f"Fail to set method_name {method_name}") from e + +This makes the method invocable via the ``WorkerGroup`` interface. + +-------------- + +Step 3: Call Chain +~~~~~~~~~~~~~~~~~~ + +All the machinery above ensures that distributed calls feel identical to +single-process ones. In the original single-process script, the code +looks like: + +.. code:: python + + rollout = Rollout() + rollout.generate_sequences(batch) + +With ``verl``, the multiprocess program becomes: + +.. code:: python + + rollout = RayWorkerGroup(resource_pool=[4], RayClassWithArgs(Rollout)) + rollout.generate_sequences(batch) + +.. figure:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/call_generate_sequences.png?raw=true + :alt: call_chain_of_generate_sequences + + call_chain_of_generate_sequences + +Behind this simple call: - ``dispatch_fn`` splits input across workers - +``execute_fn`` performs the actual remote invocation - ``collect_fn`` +gathers the results + +All of this is abstracted away, enabling developers to write distributed +code with minimal changes to their existing logic. + +-------------- + +Beyond RL Post-Training: Generalizing ``verl.single_controller`` +---------------------------------------------------------------- + +The ``verl.single_controller`` module generalizes well beyond +reinforcement learning. It provides a clean abstraction to batch-process +remote method calls, with automatic input/output handling. + +By minimizing the gap between single-process and multi-process scripts, +``verl.single_controller`` opens the door to distributed computing in +broader domains — not limited to RL post-training. + +We hope this design inspires more examples and extensions from the +community. diff --git a/verl_0720_main/verl/docs/start/agentic_rl.rst b/verl_0720_main/verl/docs/start/agentic_rl.rst new file mode 100644 index 0000000000000000000000000000000000000000..46ca53d447fabcec7702138c878ecc4dc5084504 --- /dev/null +++ b/verl_0720_main/verl/docs/start/agentic_rl.rst @@ -0,0 +1,133 @@ +Agentic RL Training +=================== + +Last updated: 07/15/2025. + +Overview +---------- +The goal of Agentic RL is to improve the performance of backend models from reinforcement learning to the Agent. During the training process, a series of features are developed: + +1. Server-based asynchronous rollout +2. Multi-turn conversations and tool calls +3. LangGraph-based Agent + + +This document explains the system principles and usage involved to help users implement Agentic RL. + + +Server-based Asynchronous Rollout +--------------------------------- + +Since Agents need to interact with the environment through various tool calls, in order to avoid GPU idling while waiting for tool call return results, an asyncio based co-routing mechanism is utilized to execute each rollout requests asynchronously, thereby improving training performance. To support asynchronous rollout, the inference engine (server) and the agent (client) are architecturally separated, implementing a server-based system with the following objectives: + +1. Enabling load balancing mechanisms to balance loads across multiple GPUs and reduce the impact of long-tail requests on performance. For this purpose, scheduling capabilities in stream mode (recipe\stream_mode) are implemented as a recipe. +2. Preventing agent specific features such as tracing from affecting the inference engine. + +System Architecture +~~~~~~~~~~~~~~~~~~~ + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/agent_loop.png?raw=true + +For more detail on internal design, please refer to :doc:`Agent Loop<../advance/agent_loop>`. + +System Components +~~~~~~~~~~~~~~~~~ + ++--------------------------+----------------------------------------------------------------------------+ +| Component | Role | ++==========================+============================================================================+ +| AgentLoop | Client, implements Agent functions | ++--------------------------+----------------------------------------------------------------------------+ +| LLMServerClient | Inference gateway, provides generate interface for AgentLoop | ++--------------------------+----------------------------------------------------------------------------+ +| AsyncServer | Server, each instance is connected to one DP group of the inference engine | ++--------------------------+----------------------------------------------------------------------------+ + +**"generate" Interface** + +The "generate" function based on ray actor is used between the Client and Server instead of the standard chat completion API. This is because the conversion between tokens and text can be irreversible. For example, the token converted from "" will be different from that generated by the LLM. During the training phase, it is necessary to strictly use the tokens generated by LLM inference to avoid inaccurate in computing advantage, which may affect model performance. Having the Server provide a token-based API helps the Client maintain the relationship between the text generated by tool calls and the tokens returned by the LLM, so as to output correct tokens for training. + + +**Inference Engine Adaptation** +AsyncServer uniformly provides a generate function to the upper layer, with separate implementations for SGLang and vLLM to hide underlying differences: + +1. The SGLang AsyncServer uses the async_generate interface of the SGLang engine, which is located on the first GPU of each TP group. Therefore, AsyncServer needs to remotely call async_generate through ray actor. +2. The vLLM AsyncServer uses the generate interface of the vLLM engine, which can communicate with the GPUs in the TP group through ZMQ and can be directly called in AsyncServer. + + +Usage Example +~~~~~~~~~~~~~ + +Follow :doc:`GSM8K example<../examples/gsm8k_example>` to prepare the dataset and model checkpoints. + +There are two options required to use agent loop: + +- `data.return_raw_chat=True` +- `actor_rollout_ref.rollout.mode=async` + +This example uses the sglang inference engine by default, and you can also modify rollout_name to use vllm. + +.. code-block:: bash + + bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh + + +Multi-turn Conversations and Tool Calls +--------------------------------------- + +Follow :doc:`Multi-turn Rollout Support<../sglang_multiturn/multiturn>` to prepare tool and configuration files. + +The Tool Agent Loop has an additional requirement: adding an "agent_name" field to the dataset. During rollout, it will choose to use tool_agent_loop or single_turn_agent (default) based on this field. + +Usage Example +~~~~~~~~~~~~~ + +.. code-block:: bash + + # install mlflow to view toolcall and llm trace + pip install mlflow + + # This will download and preprocess the GSM8K dataset into ~/data/gsm8k/ and add the "agent_name" field. + python examples/data_preprocess/gsm8k_tool_agent_loop.py + + # Start training with tool calls and enabled mlflow based trace helping to debug the rollout details + bash examples/sglang_multiturn/run_qwen2_5_3b_gsm8k_tool_agent_mlflow_fsdp.sh + + # When training is done, start a mlflow server to view trace + mlflow ui -h 0.0.0.0 -p 5000 --backend-store-uri sqlite:////tmp/mlruns.db + + # then you can open http://:5000 from browser to view trace + + +Note: During training, because the model may sometimes fail to generate correct toolcall tags, an error message "Failed to decode tool call" will be output to the console, which does not indicate an abnormality in training. + + +Follow :doc:`Rollout trace<../advance/rollout_trace>` to known more about trace feature. + + + +Agent Framework +--------------- + +System Architecture +~~~~~~~~~~~~~~~~~~~ + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/langgraph_agent.png?raw=true + +System Components +~~~~~~~~~~~~~~~~~ + ++--------------------------+-----------------------------------------------------------------------------------------------+ +| Component | Role | ++==========================+===============================================================================================+ +| ChatModel | LLM object of LangChain, used to adapt to the “generate” api provided by LLMServerClient | ++--------------------------+-----------------------------------------------------------------------------------------------+ +| ReactAgentLoop | Agent adaptation layer, which by default supports a naive LangGraph Agentic. | +| | New classes can be derived to support user-defined Agents, and the run function needs to be | +| | implemented to complete Agent calls. | ++--------------------------+-----------------------------------------------------------------------------------------------+ +| AsyncServer | Server, each instance is connected to one DP group of the inference engine. | ++--------------------------+-----------------------------------------------------------------------------------------------+ + + +Follow doc "recipe/langgraph_agent/example/README.md" for more details. \ No newline at end of file diff --git a/verl_0720_main/verl/docs/start/install.rst b/verl_0720_main/verl/docs/start/install.rst new file mode 100644 index 0000000000000000000000000000000000000000..4b0f3a166615ab6be9acb4cb9a66c3b5e0fb921b --- /dev/null +++ b/verl_0720_main/verl/docs/start/install.rst @@ -0,0 +1,317 @@ +Installation +============ + +Requirements +------------ + +- **Python**: Version >= 3.10 +- **CUDA**: Version >= 12.8 + +verl supports various backends. Currently, the following configurations are available: + +- **FSDP** and **Megatron-LM** (optional) for training. +- **SGLang**, **vLLM** and **TGI** for rollout generation. + +Choices of Backend Engines +---------------------------- + +1. Training: + +We recommend using the **FSDP / FSDP2** backend to investigate, research and prototype different models, datasets and RL algorithms. For users who pursue better scalability, we recommend the **Megatron-LM** backend. Currently, we support `Megatron-LM v0.13.1 `_. Both backends are served through the same unified worker layer – see :doc:`Engine Workers<../workers/engine_workers>` for the worker-level API and :doc:`Model Engine<../workers/model_engine>` for the engine-level design. + + +2. Inference: + +For inference, vllm 0.8.3 and later versions have been tested for stability. We recommend turning on env var `VLLM_USE_V1=1` for optimal performance. + +For SGLang, refer to the :doc:`SGLang Backend<../workers/sglang_worker>` for detailed installation and usage instructions. SGLang rollout is under extensive development and offers many advanced features and optimizations. We encourage users to report any issues or provide feedback via the `SGLang Issue Tracker `_. + +For huggingface TGI integration, it is usually used for debugging and single GPU exploration. + +Install from docker image +------------------------- + +Start from v0.6.0, we use vllm and sglang release image as our base image. + +Base Image +:::::::::: + +- vLLM: https://hub.docker.com/r/vllm/vllm-openai +- SGLang: https://hub.docker.com/r/lmsysorg/sglang + +Application Image +::::::::::::::::: + +Upon base image, the following packages are added: + +- flash_attn +- Megatron-LM +- Apex +- TransformerEngine +- DeepEP + +Latest docker file: + +- `Dockerfile.stable.vllm `_ +- `Dockerfile.stable.sglang `_ + +All pre-built images are available in dockerhub: `verlai/verl `_. For example, ``verlai/verl:sgl055.latest``, ``verlai/verl:vllm011.latest``. + +You can find the latest images used for development and ci in our github workflows: + +- `.github/workflows/vllm.yml `_ +- `.github/workflows/sgl.yml `_ + + +Installation from Docker +:::::::::::::::::::::::: + +After pulling the desired Docker image and installing desired inference and training frameworks, you can run it with the following steps: + +1. Launch the desired Docker image and attach into it: + +.. code:: bash + + docker create --runtime=nvidia --gpus all --net=host --shm-size="10g" --cap-add=SYS_ADMIN -v .:/workspace/verl --name verl sleep infinity + docker start verl + docker exec -it verl bash + + +2. If you use the images provided, you only need to install verl itself without dependencies: + +.. code:: bash + + # install the nightly version (recommended) + git clone https://github.com/verl-project/verl && cd verl + pip3 install --no-deps -e . + +[Optional] If you hope to switch between different frameworks, you can install verl with the following command: + +.. code:: bash + + # install the nightly version (recommended) + git clone https://github.com/verl-project/verl && cd verl + pip3 install -e ".[vllm]" + pip3 install -e ".[sglang]" + + +Install from custom environment +--------------------------------------------- + +We recommend to use docker images for convenience. However, if your environment is not compatible with the docker image, you can also install verl in a python environment. + +.. note:: + + - Dockerfile provides more details than this installation instructions. You can find examples in each Dockerfile, for example `verl0.6-cu128-torch2.8.0-fa2.7.4 Dockerfile.base `_ . + + +Pre-requisites +:::::::::::::: + +For training and inference engines to utilize better and faster hardware support, CUDA/cuDNN and other dependencies are required, +and some of the dependencies are easy to be overridden when installing other packages, +so we put them in the :ref:`Post-installation` step. + +.. note:: + + - The installation steps below are recommended configurations for the latest version of verl. + + If you are trying to customize your own environment, please ignore the strict constraints. + +We need to install the following pre-requisites: + +- **CUDA**: Version >= 12.8 +- **cuDNN**: Version >= 9.10.0 +- **Apex** + +CUDA above 12.8 is recommended to use as the docker image, +please refer to `NVIDIA's official website `_ for other version of CUDA. + +.. code:: bash + + # change directory to anywher you like, in verl source code directory is not recommended + wget https://developer.download.nvidia.com/compute/cuda/12.8.1/local_installers/cuda-repo-ubuntu2204-12-8-local_12.8.1-570.124.06-1_amd64.deb + dpkg -i cuda-repo-ubuntu2204-12-8-local_12.8.1-570.124.06-1_amd64.deb + cp /var/cuda-repo-ubuntu2204-12-8-local/cuda-*-keyring.gpg /usr/share/keyrings/ + apt-get update + apt-get -y install cuda-toolkit-12-8 + update-alternatives --set cuda /usr/local/cuda-12-8 + + +cuDNN can be installed via the following command, +please refer to `NVIDIA's official website `_ for other version of cuDNN. + +.. code:: bash + + # change directory to anywher you like, in verl source code directory is not recommended + wget https://developer.download.nvidia.com/compute/cudnn/9.10.2/local_installers/cudnn-local-repo-ubuntu2204-9.10.2_1.0-1_amd64.deb + dpkg -i cudnn-local-repo-ubuntu2204-9.10.2_1.0-1_amd64.deb + cp /var/cudnn-local-repo-ubuntu2204-9.10.2/cudnn-*-keyring.gpg /usr/share/keyrings/ + apt-get update + apt-get -y install cudnn-cuda-12 + +Install dependencies +:::::::::::::::::::: + +.. note:: + + We recommend to use a fresh new conda environment to install verl and its dependencies. + + **Notice that the inference frameworks often strictly limit your pytorch version and will directly override your installed pytorch if not paying enough attention.** + + As a countermeasure, it is recommended to install inference frameworks first with the pytorch they needed. For vLLM, if you hope to use your existing pytorch, + please follow their official instructions + `Use an existing PyTorch installation `_ . + + +1. First of all, to manage environment, we recommend using conda: + +.. code:: bash + + conda create -n verl python==3.12 + conda activate verl + + +2. Then, execute the ``install.sh`` script that we provided in verl: + +.. code:: bash + + # Make sure you have activated verl conda env + # If you need to run with megatron + bash scripts/install_vllm_sglang_mcore.sh + # Or if you simply need to run with FSDP + USE_MEGATRON=0 bash scripts/install_vllm_sglang_mcore.sh + + +If you encounter errors in this step, please check the script and manually follow the steps in the script. + +[Optional] NVIDIA Apex is recommended for Megatron-LM training, but it's not needed if you only use FSDP backend. +You can install it via the following command, but notice that this steps can take a very long time. +It is recommended to set the ``MAX_JOBS`` environment variable to accelerate the installation process, +but do not set it too large, otherwise the memory will be overloaded and your machines may hang. + +.. code:: bash + + # change directory to anywher you like, in verl source code directory is not recommended + git clone https://github.com/NVIDIA/apex.git && \ + cd apex && \ + MAX_JOB=32 pip install -v --disable-pip-version-check --no-cache-dir --no-build-isolation --config-settings "--build-option=--cpp_ext" --config-settings "--build-option=--cuda_ext" ./ + +Install verl +:::::::::::: + +For installing the latest version of verl, the best way is to clone and +install it from source. Then you can modify our code to customize your +own post-training jobs. + +.. code:: bash + + git clone https://github.com/verl-project/verl.git + cd verl + pip install --no-deps -e . + + +Post-installation +::::::::::::::::: + +Please make sure that the installed packages are not overridden during the installation of other packages. + +The packages worth checking are: + +- **torch** and torch series +- **vLLM** +- **SGLang** +- **pyarrow** +- **tensordict** +- **nvidia-cudnn-cu12**: For Magetron backend + +If you encounter issues about package versions during running verl, please update the outdated ones. + + +Install with AMD GPUs - ROCM kernel support +------------------------------------------------------------------ + +When you run on AMD GPUs (MI300) with ROCM platform, you cannot use the previous quickstart to run verl. You should follow the following steps to build a docker and run it. +If you encounter any issues in using AMD GPUs running verl, feel free to contact me - `Yusheng Su `_. + +Find the docker for AMD ROCm: `docker/Dockerfile.rocm `_ +:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: + +.. code-block:: bash + + # Build the docker in the repo dir: + # docker build -f docker/Dockerfile.rocm -t verl-rocm:03.04.2015 . + # docker images # you can find your built docker + FROM rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 + + # Set working directory + # WORKDIR $PWD/app + + # Set environment variables + ENV PYTORCH_ROCM_ARCH="gfx90a;gfx942" + + # Install vllm + RUN pip uninstall -y vllm && \ + rm -rf vllm && \ + git clone -b v0.6.3 https://github.com/vllm-project/vllm.git && \ + cd vllm && \ + MAX_JOBS=$(nproc) python3 setup.py install && \ + cd .. && \ + rm -rf vllm + + # Copy the entire project directory + COPY . . + + # Install dependencies + RUN pip install "tensordict<0.6" --no-deps && \ + pip install accelerate \ + codetiming \ + datasets \ + dill \ + hydra-core \ + liger-kernel \ + numpy \ + pandas \ + datasets \ + peft \ + "pyarrow>=15.0.0" \ + pylatexenc \ + "ray[data,train,tune,serve]" \ + torchdata \ + transformers \ + wandb \ + orjson \ + pybind11 && \ + pip install -e . --no-deps + +Build the image +:::::::::::::::::::::::: + +.. code-block:: bash + + docker build -t verl-rocm . + +Launch the container +:::::::::::::::::::::::::::: + +.. code-block:: bash + + docker run --rm -it \ + --device /dev/dri \ + --device /dev/kfd \ + -p 8265:8265 \ + --group-add video \ + --cap-add SYS_PTRACE \ + --security-opt seccomp=unconfined \ + --privileged \ + -v $HOME/.ssh:/root/.ssh \ + -v $HOME:$HOME \ + --shm-size 128G \ + -w $PWD \ + verl-rocm \ + /bin/bash + +If you do not want to root mode and require assign yourself as the user, +Please add ``-e HOST_UID=$(id -u)`` and ``-e HOST_GID=$(id -g)`` into the above docker launch script. + +verl with AMD GPUs currently supports FSDP as the training engine, vLLM and SGLang as the inference engine. We will support Megatron in the future. diff --git a/verl_0720_main/verl/docs/start/more_resources.rst b/verl_0720_main/verl/docs/start/more_resources.rst new file mode 100644 index 0000000000000000000000000000000000000000..aa8cb2a62b46579ee4bef2880d7f62485175495e --- /dev/null +++ b/verl_0720_main/verl/docs/start/more_resources.rst @@ -0,0 +1,7 @@ +More Resources +============== + +Last updated: 06/30/2025. + +- Introduction to verl (`Slides `_) +- verl Code Walkthrough (`Slides `_, `Talk in Chinese `_) diff --git a/verl_0720_main/verl/docs/start/multinode.rst b/verl_0720_main/verl/docs/start/multinode.rst new file mode 100644 index 0000000000000000000000000000000000000000..8148da24bd7e9bb489a648941d241745529ff226 --- /dev/null +++ b/verl_0720_main/verl/docs/start/multinode.rst @@ -0,0 +1,821 @@ +Multinode Training +================== + +Last updated: 06/10/2025. + +.. _wuxibin89: https://github.com/wuxibin89 + +Author: `Xibin Wu `_, `Yusheng Su `_. + +Option 1: Launch Manually +------------------------------ + +Set up multinode ray cluster +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +1. Start head node with ``ray start --head --dashboard-host=0.0.0.0``, there're 2 address you should care about: + +- GCS address: ``ray start --address=
``, where worker node should connect to. +- Dashboard address: ``
:8265``, where you should submit job to the cluster. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/head.png?raw=true + +2. Start worker node with ``ray start --address=
`` you get above. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/worker.png?raw=true + +3. Now you should see the cluster have 2 nodes with ``ray status``. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/status.png?raw=true + +4. Additionally, you can access dashboard in the browser with the address you get above. + +*Firewall rules maybe need configure to access the dashboard, if there's any trouble, please contact your network administrator.* + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/overview.png?raw=true + +Submit job to ray cluster +~~~~~~~~~~~~~~~~~~~~~~~~~ +1. Submit ray job to cluster with the dashboard address you get above. + +.. code-block:: bash + + ray job submit --address="http://127.0.0.1:8265" \ + --runtime-env=verl/trainer/runtime_env.yaml \ + --no-wait \ + -- \ + python3 -m verl.trainer.main_ppo \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=2 \ + ... + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/submit.png?raw=true + +2. Then you can check the job status with the following commands: + +- ray job list: list all jobs submitted to the cluster. +- ray job logs : query the logs of the job. +- ray job status : query the status of the job. +- ray job stop : request the job to be stopped. +- ray job list | grep submission_id | grep JobStatus | grep RUNNING | grep -oP 'raysubmit_[^'\''"]+' | head -n 1: get the latest job submission ID of the running job. +- ray job logs --follow: added ``--follow`` parameter to ray job logs command to enable continuous log streaming. + +3. You can also access driver/task/actor logs in ``/tmp/ray/session_latest/logs/``, driver log is ``job-driver-raysubmit_.log``. + +4. We strongly recommend you to view job detail from dashboard in multinode training, because it provide more structure way to view the job information. + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/job.png?raw=true +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/job_detail.png?raw=true + +Option 2: Launch via SkyPilot on Kubernetes or clouds +------------------------------------------------------ + +.. note:: + Ready-to-use SkyPilot example configurations are available in the `examples/tutorial/skypilot/ `_ directory: + + - ``verl-ppo.yaml`` - PPO training with GSM8K dataset + - ``verl-grpo.yaml`` - GRPO training with MATH dataset + - ``verl-multiturn-tools.yaml`` - Multi-turn tool usage training + + See the `SkyPilot examples README `_ for detailed usage instructions. + +Step 1: Setup SkyPilot +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +SkyPilot can support different clouds, here we use GCP as example. `install skypilot `_ + +.. code-block:: bash + + conda create -y -n sky python=3.10 + conda activate sky + pip install "skypilot[gcp]" + + conda install -c conda-forge google-cloud-sdk + gcloud init + + # Run this if you don't have a credential file. + # This will generate ~/.config/gcloud/application_default_credentials.json. + gcloud auth application-default login + + # Check if the GCP credential is correctly setup. + sky check gcp + +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/setup_skypilot.png?raw=true + +Step 2: Prepare dataset +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + git clone https://github.com/verl-project/verl.git + cd examples/data_preprocess + python3 gsm8k.py --local_save_dir ~/data/gsm8k + + +Step 3: Submit a job with SkyPilot +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +1. Create a SkyPilot YAML ``verl-cluster.yml`` with the following content: + +.. parsed-literal:: workdir: . will sync all the data in the current dir to the remote cluster. + +.. code-block:: yaml + + resources: + accelerators: L4:1 # every node has 1 L4 GPU + image_id: docker:verlai/verl:base-verl0.5-cu126-cudnn9.8-torch2.7.0-fa2.7.4 + memory: 64+ # every node has 64 GB memory + ports: 8265 # expose port for ray dashboard + + num_nodes: 2 # cluster size + + # --------------- Work Directory Synchronization (workdir) --------------- + # Defines the local working directory to be synchronized to the remote cluster. + # Here, '.' means synchronizing the directory where the sky submit command is currently run. + workdir: . + + # --------------- (secrets) --------------- + secrets: + ## your wandb api key ## + WANDB_API_KEY: null + + # --------------- File Mounts/Data Upload (file_mounts) --------------- + # If your dataset (gsm8k folder) is local, it needs to be uploaded to the remote cluster. + file_mounts: + # Remote path (relative to remote user's home directory): Local path + # /remote/dir1/file: /local/dir1/file + data/gsm8k: ~/data/gsm8k + + # --------------- Environment Setup (setup) --------------- + # Commands run on each node of the remote cluster to set up the environment (e.g., install dependencies). These are run directly inside Docker. + setup: | + rm -rf verl + git clone https://github.com/verl-project/verl.git + cd verl + pip3 install -v -e .[vllm] + + # --------------- Run Command (run) --------------- + # The actual task commands to be executed on the remote cluster. + # This script will first start the Ray cluster (different ray start commands are executed on Head and Worker nodes). + # Then, your training script will only be run on the Head node (SKYPILOT_NODE_RANK == 0). + run: | + # Get the Head node's IP and total number of nodes (environment variables injected by SkyPilot). + head_ip=`echo "$SKYPILOT_NODE_IPS" | head -n1` + num_nodes=`echo "$SKYPILOT_NODE_IPS" | wc -l` # Here num_nodes should be equal to 2. + + # login wandb + python3 -c "import wandb; wandb.login(relogin=True, key='$WANDB_API_KEY')" + + # Start Ray based on node role (Head=0, Worker>0). + # This logic is a standard Ray cluster startup script. + if [ "$SKYPILOT_NODE_RANK" == "0" ]; then + # Head node starts Ray Head. + echo "Starting Ray head node..." + # Check if a Ray Head is already running to avoid duplicate starts. + ps aux | grep ray | grep 6379 &> /dev/null || ray start --head --disable-usage-stats \ + --port=6379 \ + --dashboard-host=0.0.0.0 \ + --dashboard-port=8265 + + # Wait for all worker nodes to join the cluster. + while [ $(ray nodes | grep NODE_ID | wc -l) -lt $num_nodes ]; do + echo "Waiting for all nodes to join... ($(ray nodes | grep NODE_ID | wc -l)/$num_nodes)" + sleep 5 + done + + # Head node executes the training script. + echo "Executing training script on head node..." + + python3 -m verl.trainer.main_ppo \ + data.train_files=data/gsm8k/train.parquet \ + data.val_files=data/gsm8k/test.parquet \ + data.train_batch_size=256 \ + data.max_prompt_length=512 \ + data.max_response_length=256 \ + actor_rollout_ref.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=4 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=4 \ + critic.optim.lr=1e-5 \ + critic.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + critic.ppo_micro_batch_size_per_gpu=4 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.logger=['console','wandb'] \ + trainer.val_before_train=False \ + trainer.default_hdfs_dir=null \ + trainer.n_gpus_per_node=1 \ + trainer.nnodes=2 \ + trainer.save_freq=20 \ + trainer.test_freq=20 \ + trainer.total_epochs=2 \ + trainer.project_name=verl_examples \ + trainer.experiment_name=experiment_name_gsm8k + + else + # Wait for Ray Head to start. + sleep 10 # Increase waiting time to ensure Head finishes starting. + # Worker node starts Ray Worker. + echo "Starting Ray worker node..." + + # Check if a Ray Worker is already running to avoid duplicate starts. + ps aux | grep ray | grep $head_ip:6379 &> /dev/null || ray start --address $head_ip:6379 --disable-usage-stats + + # Add sleep to after `ray start` to give ray enough time to daemonize + sleep 5 # Ensure Worker successfully connects to Head. + fi + + # No commands are added to the Worker node here; the Worker's main task is to start Ray and wait for the Head node to assign tasks. + echo "Node setup and Ray start script finished for rank $SKYPILOT_NODE_RANK." + + +.. code-block:: bash + + export WANDB_API_KEY= + sky launch -c verl --secret WANDB_API_KEY verl-cluster.yml + +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/running_job.png?raw=true +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/running_job_1.png?raw=true +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/finished.png?raw=true + +**Check the cluster on GCP** + +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/gcp_instances.png?raw=true + +**Check Ray Dashboard** + +We can see the cluster on the RAY Dashboard with the GCP head node: + +```console +$ sky status --endpoint 8265 verl +1.2.3.4:8265 +``` + +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/ray_dashboard_overview.png?raw=true +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/ray_dashboard_jobs.png?raw=true +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/ray_dashboard_cluster.png?raw=true + + +**Check the checkpoint of model** + +.. code-block:: bash + + # login the head node + ssh verl + # The global step will vary. Find the correct path from the training logs. + cd ~/sky_workdir/checkpoints/verl_examples/gsm8k/ + # Then list contents to find the checkpoint, e.g.: + ls -R . + +.. image:: https://github.com/yottalabsai/open-source/blob/main/static/verl/saved_model.png?raw=true + + +Option 3: Launch via Slurm +------------------------------ + +Ray provides users with `this `_ official +tutorial to start a Ray cluster on top of Slurm. We have verified the :doc:`GSM8K example<../examples/gsm8k_example>` +on a Slurm cluster under a multi-node setting with the following steps. + +1. [Optional] If your cluster support `Apptainer or Singularity `_ and you wish +to use it, convert verl's Docker image to an Apptainer image. Alternatively, set up the environment with the package +manager available on your cluster or use other container runtimes (e.g. through `Slurm's OCI support `_) available to you. + +.. code:: bash + + apptainer pull /your/dest/dir/vemlp-th2.4.0-cu124-vllm0.6.3-ray2.10-te1.7-v0.0.3.sif docker://verlai/verl:vemlp-th2.4.0-cu124-vllm0.6.3-ray2.10-te1.7-v0.0.3 + +2. Follow :doc:`GSM8K example<../examples/gsm8k_example>` to prepare the dataset and model checkpoints. + +3. Modify `examples/tutorial/slurm/ray_on_slurm.slurm `_ with your cluster's own information. + +4. Submit the job script to the Slurm cluster with `sbatch`. + +Please note that Slurm cluster setup may vary. If you encounter any issues, please refer to Ray's +`Slurm user guide `_ for common caveats. + +If you changed Slurm resource specifications, please make sure to update the environment variables in the job script if necessary. + + +Option 4: Launch via dstack +------------------------------ + +`dstackai/dstack `_ is an open-source container orchestrator that simplifies distributed training across cloud providers and on-premises environments +without the need to use K8S or Slurm. + +Prerequisite +~~~~~~~~~~~~ +Once dstack is `installed `_, initialize the directory as a repo with ``dstack init``. + +.. code-block:: bash + + mkdir myproject && cd myproject + dstack init + +**Create a fleet** + +Before submitting distributed training jobs, create a `dstack` `fleet `_. + +Run a Ray cluster task +~~~~~~~~~~~~~~~~~~~~~~ + +Once the fleet is created, define a Ray cluster task, e.g. in ``ray-cluster.dstack.yml``: + +.. code-block:: yaml + + type: task + name: ray-verl-cluster + + nodes: 2 + + env: + - WANDB_API_KEY + - PYTHONUNBUFFERED=1 + - CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + + image: verlai/verl:app-verl0.6-transformers4.56.1-sglang0.5.2-mcore0.13.0-te2.2 + commands: + - git clone https://github.com/verl-project/verl + - cd verl + - pip install --no-deps -e . + - pip install hf_transfer hf_xet + - | + if [ $DSTACK_NODE_RANK = 0 ]; then + python3 examples/data_preprocess/gsm8k.py --local_save_dir ~/data/gsm8k + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2.5-7B-Instruct')" + ray start --head --port=6379; + else + ray start --address=$DSTACK_MASTER_NODE_IP:6379 + fi + + # Expose Ray dashboard port + ports: + - 8265 + + resources: + gpu: 80GB:8 + shm_size: 128GB + + # Save checkpoints on the instance + volumes: + - /checkpoints:/checkpoints + +Now, if you run this task via `dstack apply`, it will automatically forward the Ray's dashboard port to `localhost:8265`. + +.. code-block:: bash + + dstack apply -f ray-cluster.dstack.yml + +As long as the `dstack apply` is attached, you can use `localhost:8265` to submit Ray jobs for execution + +Submit Ray jobs +~~~~~~~~~~~~~~~ + +Before you can submit Ray jobs, ensure to install `ray` locally: + +.. code-block:: shell + + pip install ray + +Now you can submit the training job to the Ray cluster which is available at ``localhost:8265``: + +.. code-block:: shell + + $ RAY_ADDRESS=http://localhost:8265 + $ ray job submit \ + -- python3 -m verl.trainer.main_ppo \ + data.train_files=/root/data/gsm8k/train.parquet \ + data.val_files=/root/data/gsm8k/test.parquet \ + data.train_batch_size=256 \ + data.max_prompt_length=512 \ + data.max_response_length=256 \ + actor_rollout_ref.model.path=Qwen/Qwen2.5-7B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=4 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=4 \ + critic.optim.lr=1e-5 \ + critic.model.path=Qwen/Qwen2.5-7B-Instruct \ + critic.ppo_micro_batch_size_per_gpu=4 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.project_name=ppo_training \ + trainer.experiment_name=qwen-2.5-7B \ + trainer.val_before_train=False \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=2 \ + trainer.default_local_dir=/checkpoints \ + trainer.save_freq=10 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 2>&1 | tee verl_demo.log \ + trainer.resume_mode=disable + + +For more details on how `dstack` works, check out its `documentation `_. + +How to debug? +--------------------- + + +Ray Distributed Debugger VSCode Extension (Recommended) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. Starting with Ray 2.39, Anyscale has introduced the `Ray Distributed Debugger `_ VSCode extension. Follow the extension’s installation instructions, then add your cluster using the dashboard URL you obtained earlier. + + .. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/debugger.png?raw=true + :alt: Ray Distributed Debugger VSCode extension screenshot + +2. Prerequisites. + + Ensure the following are installed (see the extension README for more detail): + + - Visual Studio Code + - `ray[default]` >= 2.9.1 + - `debugpy` >= 1.8.0 + + .. image:: https://github.com/aoshen524/verl/blob/main/docs/start/c7098b755ff689859837773a916c857.png?raw=true + :alt: VSCode with Ray prerequisites + +3. Environment Variables. + + To enable post‑mortem debugging, set: + + .. code-block:: bash + + export RAY_DEBUG_POST_MORTEM=1 + + .. admonition:: Note + :class: important + + Be sure to remove any legacy flags before starting Ray: + + - `RAY_DEBUG=legacy` + - `--ray-debugger-external` + +4. Configuring BreakpointsSet up breakpoint() in your code, and submit job to cluster. Then the extension will show the breakpoint information. + + + 1. Insert `breakpoint()` calls into your remote functions. + 2. Submit your job to the cluster. + + The extension will detect active breakpoints and display them in VSCode. + + .. image:: https://github.com/aoshen524/verl/blob/main/docs/start/4ddad74395c79a1402331c0ce73316f.png?raw=true + :alt: Detected breakpoint in VSCode + + **Note:** Breakpoints are only supported inside functions decorated with `@ray.remote`. + +5. Launching the Debugger. + + Run your job directly from the command line (do not use a `launch.json`): + + .. code-block:: bash + + python job.py + +6. Attaching to a Breakpoint. + + Once the process hits the first `breakpoint()`, click the Ray Distributed Debugger icon in the VSCode sidebar to attach the debugger. + + .. image:: https://github.com/aoshen524/verl/blob/main/docs/start/4ddad74395c79a1402331c0ce73316f.png?raw=true + :alt: Attaching VSCode debugger to Ray process + +7. Debugging With Multiple breakpoint(). + + For each subsequent task, first disconnect the current debugger session, then click the extension icon again to attach to the next breakpoint. + + .. image:: https://github.com/aoshen524/verl/blob/main/docs/start/6e83c910a62c82fecb89c6619e001cd.png?raw=true + :alt: Disconnecting and reconnecting the debugger + +Legacy Ray Debugger +~~~~~~~~~~~~~~~~~~~ +1. Ray has a builtin legacy `debugger `_ that allows you to debug your distributed applications. To enable debugger, start ray cluster with ``RAY_DEBUG=legacy`` and ``--ray-debugger-external``. + +.. code-block:: bash + + # start head node + RAY_DEBUG=legacy ray start --head --dashboard-host=0.0.0.0 --ray-debugger-external + # start worker node + RAY_DEBUG=legacy ray start --address='10.124.46.192:6379' --ray-debugger-external + +2. Set up breakpoint in your code, and submit job to cluster. Then run ``ray debug`` to wait breakpoint: + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/legacy.png?raw=true + + +Multi-node training on AMD clusters +--------------------------------------------------------------------------------------- + +If you want to run multi-node training with slurm with Docker/Podman container on AMD Cluster, you can use the following script. + +If you encounter any issues in using AMD GPUs running verl, please contact `Yusheng Su `_. + +.. note:: + 1. You need to use ``podman`` or ``docker`` in the following script. We will release the apptainer script later. + 2. If you want to use ``podman``, you just replace ``docker`` with ``podman`` in the following script. + +The script includes the following steps: + +1. SLURM Configuration +2. Environment Setup +3. Docker/Podman Container Setup +4. Ray Cluster Initialization +5. Data Preprocessing +6. Model Setup +7. Training Launch + + +slurm_script.sh +~~~~~~~~~~~~~~~~~~~~ + +.. code-block:: bash + + #!/bin/bash + + #SBATCH --job-name=verl-ray-on-slurm + #SBATCH --nodes=2 + #SBATCH --ntasks-per-node=2 + #SBATCH --mem=200G + #SBATCH --time=30-00:00:00 + #SBATCH --gpus-per-node=8 + #SBATCH --cpus-per-task=28 + #SBATCH --output=../verl_log/slurm-%j.out + #SBATCH --error=../verl_log/slurm-%j.err + #SBATCH --nodelist=gpu-[0,1] + + + # load necessary modules + ### Run this setup + # [Cluster]: Use docker + # docker pull docker.io/rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 + + + ########################################################################## + ###The following setting should be set in different project and cluster### + ########################################################################## + + ### Project + CONTAINER_NAME="multinode_verl_training" + IMG="verl.rocm" + DOCKERFILE="docker/Dockerfile.rocm" + # echo $PWD + verl_workdir="${HOME}/projects/verl_upstream" + export TRANSFORMERS_CACHE="${HOME}/.cache/huggingface" + export HF_HOME=$TRANSFORMERS_CACHE + + ### Cluster Network Setting + export NCCL_DEBUG=TRACE + export GPU_MAX_HW_QUEUES=2 + export TORCH_NCCL_HIGH_PRIORITY=1 + export NCCL_CHECKS_DISABLE=1 + # export NCCL_IB_HCA=rdma0,rdma1,rdma2,rdma3,rdma4,rdma5,rdma6,rdma7 + export NCCL_IB_HCA=mlx5_0,mlx5_1,mlx5_2,mlx5_3,mlx5_4,mlx5_5,mlx5_8,mlx5_9 + export NCCL_IB_GID_INDEX=3 + export NCCL_CROSS_NIC=0 + export CUDA_DEVICE_MAX_CONNECTIONS=1 + export NCCL_PROTO=Simple + export RCCL_MSCCL_ENABLE=0 + export TOKENIZERS_PARALLELISM=false + export HSA_NO_SCRATCH_RECLAIM=1 + ########################################################################## + + ### For rocm and training script + export HIP_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 + export ROCR_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + export CUDA_VISIBLE_DEVICES=$HIP_VISIBLE_DEVICES + + + # Build and launch the Docker container + srun bash -c " + # Exit on any error + set -e + + # Clean up dangling images (images with tag) + docker image prune -f + + # Need to pull the docker first + docker pull docker.io/rocm/vllm:rocm6.2_mi300_ubuntu20.04_py3.9_vllm_0.6.4 + + if ! docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "${IMG}"; then + echo \"Building ${IMG} image...\" + docker build -f \"${DOCKERFILE}\" -t \"${IMG}\" . + else + echo \"${IMG} image already exists, skipping build\" + fi + + # Removing old container if exists + docker rm \"${CONTAINER_NAME}\" 2>/dev/null || true + + # Checking network devices + ibdev2netdev + + # Launch the docker + docker run --rm -d \ + -e HYDRA_FULL_ERROR=1 \ + -e HIP_VISIBLE_DEVICES=${HIP_VISIBLE_DEVICES} \ + -e ROCR_VISIBLE_DEVICES=${ROCR_VISIBLE_DEVICES} \ + -e CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES} \ + -e NCCL_DEBUG=${NCCL_DEBUG} \ + -e GPU_MAX_HW_QUEUES=${GPU_MAX_HW_QUEUES} \ + -e TORCH_NCCL_HIGH_PRIORITY=${TORCH_NCCL_HIGH_PRIORITY} \ + -e NCCL_CHECKS_DISABLE=${NCCL_CHECKS_DISABLE} \ + -e NCCL_IB_HCA=${NCCL_IB_HCA} \ + -e NCCL_IB_GID_INDEX=${NCCL_IB_GID_INDEX} \ + -e NCCL_CROSS_NIC=${NCCL_CROSS_NIC} \ + -e CUDA_DEVICE_MAX_CONNECTIONS=${CUDA_DEVICE_MAX_CONNECTIONS} \ + -e NCCL_PROTO=${NCCL_PROTO} \ + -e RCCL_MSCCL_ENABLE=${RCCL_MSCCL_ENABLE} \ + -e TOKENIZERS_PARALLELISM=${TOKENIZERS_PARALLELISM} \ + -e HSA_NO_SCRATCH_RECLAIM=${HSA_NO_SCRATCH_RECLAIM} \ + -e TRANSFORMERS_CACHE=${TRANSFORMERS_CACHE} \ + -e HF_HOME=${HF_HOME} \ + --network host \ + --device /dev/dri \ + --device /dev/kfd \ + --device /dev/infiniband \ + --group-add video \ + --cap-add SYS_PTRACE \ + --security-opt seccomp=unconfined \ + --privileged \ + -v \${HOME}:\${HOME} \ + -v \${HOME}/.ssh:/root/.ssh \ + -w "${verl_workdir}" \ + --shm-size 128G \ + --name \"${CONTAINER_NAME}\" \ + \"${IMG}\" \ + tail -f /dev/null + + echo \"Container setup completed\" + " + # (Optional): If you do not want to root mode and require assign yuorself as the user + # Please add `-e HOST_UID=$(id -u)` and `-e HOST_GID=$(id -g)` into the above docker launch script. + + + + + + ### Ray launch the nodes before training + + # Getting the node names + nodes_array=($(scontrol show hostnames "$SLURM_JOB_NODELIST" | tr '\n' ' ')) + + head_node=${nodes_array[0]} + head_node_ip=$(srun --nodes=1 --ntasks=1 -w "$head_node" hostname --ip-address) + + # if we detect a space character in the head node IP, we'll + # convert it to an ipv4 address. This step is optional. + if [[ "$head_node_ip" == *" "* ]]; then + IFS=' ' read -ra ADDR <<<"$head_node_ip" + if [[ ${#ADDR[0]} -gt 16 ]]; then + head_node_ip=${ADDR[1]} + else + head_node_ip=${ADDR[0]} + fi + echo "IPV6 address detected. We split the IPV4 address as $head_node_ip" + fi + + port=6379 + ip_head=$head_node_ip:$port + export ip_head + echo "IP Head: $ip_head" + + # make sure we set environment variables before Ray initialization + + # Print out all env variables + printenv + + echo "Starting HEAD at $head_node" + srun --nodes=1 --ntasks=1 -w "$head_node" \ + docker exec "${CONTAINER_NAME}" \ + ray start --head --node-ip-address="$head_node_ip" --port=$port \ + --dashboard-port=8266 \ + --num-cpus "${SLURM_CPUS_PER_TASK}" --num-gpus "${SLURM_GPUS_PER_NODE}" --block & + # optional, though may be useful in certain versions of Ray < 1.0. + sleep 10 + + # number of nodes other than the head node + worker_num=$((SLURM_JOB_NUM_NODES - 1)) + + for ((i = 1; i <= worker_num; i++)); do + node_i=${nodes_array[$i]} + echo "Debug: Starting worker on node_i = ${node_i}" + if [ -z "$node_i" ]; then + echo "Error: Empty node name for worker $i" + continue + fi + echo "Starting WORKER $i at $node_i" + srun --nodes=1 --ntasks=1 -w "$node_i" \ + docker exec "${CONTAINER_NAME}" \ + ray start --address "$ip_head" --num-cpus "${SLURM_CPUS_PER_TASK}" --num-gpus "${SLURM_GPUS_PER_NODE}" --block & + sleep 5 + done + + + + + # Ray initlization test (See whether any error in the above execution) + echo "Testing Ray initialization in the slurm nodes..." + docker exec "${CONTAINER_NAME}" python3 -c ' + import ray + try: + ray.init(address="auto") + print("\n=== Ray Cluster Status ===") + print(f"Number of nodes: {len(ray.nodes())}") + for node in ray.nodes(): + print("Node: {}, Status: {}".format(node["NodeManagerHostname"], node["Alive"])) + # print(f"Node: {node}") + ray.shutdown() + print("Ray initialization successful!") + except Exception as e: + print(f"Ray initialization failed: {str(e)}") + ' + echo "=== Ray test completed ===" + ###### + + + + # Run data preprocessing + + echo "Starting data preprocessing..." + docker exec "${CONTAINER_NAME}" \ + python3 "examples/data_preprocess/gsm8k.py" "--local_save_dir" "../data/gsm8k" + + echo "Starting data preprocessing..." + docker exec "${CONTAINER_NAME}" \ + python3 "examples/data_preprocess/math_dataset.py" "--local_dir" "../data/math" + + train_files="../data/gsm8k/train.parquet" + val_files="../data/gsm8k/test.parquet" + + # Download and test model + echo "Loading model..." + docker exec "${CONTAINER_NAME}" \ + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2-7B-Instruct')" + MODEL_PATH="Qwen/Qwen2-7B-Instruct" + + # Set model path after pipeline test + MODEL_PATH="Qwen/Qwen2.5-0.5B-Instruct" + + echo "== Data and model loading Done ==" + + echo "Start to train..." + + docker exec "${CONTAINER_NAME}" \ + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2-7B-Instruct')" + MODEL_PATH="Qwen/Qwen2-7B-Instruct" + + + PYTHONUNBUFFERED=1 srun --overlap --nodes=${SLURM_NNODES} --ntasks=1 -w "$head_node" \ + docker exec "${CONTAINER_NAME}" \ + python3 -m verl.trainer.main_ppo \ + data.train_files=$train_files \ + data.val_files=$val_files \ + data.train_batch_size=1024 \ + data.max_prompt_length=1024 \ + data.max_response_length=1024 \ + actor_rollout_ref.model.path=$MODEL_PATH \ + actor_rollout_ref.model.enable_gradient_checkpointing=False \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.model.enable_gradient_checkpointing=True \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.9 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.use_remove_padding=True \ + critic.model.path=$MODEL_PATH \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size_per_gpu=8 \ + critic.fsdp.param_offload=False \ + critic.fsdp.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.0001 \ + trainer.critic_warmup=0 \ + trainer.logger='["console","wandb"]' \ + trainer.project_name='verl_example' \ + trainer.experiment_name='Qwen2.5-32B-Instruct_function_rm' \ + trainer.n_gpus_per_node=${SLURM_GPUS_PER_NODE} \ + trainer.val_before_train=False \ + trainer.nnodes=${SLURM_NNODES} \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 + + +Run multi-node training with above slurm_script.sh +~~~~~~~~~~~~~~~~~~~~ +Just sbatch your slurm_script.sh + +.. code-block:: bash + + sbatch slurm_script.sh + diff --git a/verl_0720_main/verl/docs/start/quickstart.rst b/verl_0720_main/verl/docs/start/quickstart.rst new file mode 100644 index 0000000000000000000000000000000000000000..492fded9cafcd4452ca8fad336676f58d274cebd --- /dev/null +++ b/verl_0720_main/verl/docs/start/quickstart.rst @@ -0,0 +1,151 @@ +.. _quickstart: + +========================================================= +Quickstart: PPO training on GSM8K dataset +========================================================= + +Post-train a LLM using GSM8K dataset. + +Introduction +------------ + +.. _hf_dataset_gsm8k: https://huggingface.co/datasets/openai/gsm8k + +In this example, we train an LLM to tackle the `GSM8k `_ task with function-based rewards. [1]_ + +Prerequisite: + +- the latest version of ``verl`` and its dependencies installed following the installation guide. Using the docker image is recommended. + +- a GPU with at least 24 GB HBM + + +Dataset Introduction +-------------------- + +GSM8k is a math problem dataset. The prompt is an elementary school +problem. The LLM model is asked to solve the math problem. Below is an example: + +Prompt + + Katy makes coffee using teaspoons of sugar and cups of water in the + ratio of 7:13. If she used a total of 120 teaspoons of sugar and cups + of water, calculate the number of teaspoonfuls of sugar she used. + +Solution + + The total ratio representing the ingredients she used to make the + coffee is 7+13 = <<7+13=20>>20 Since the fraction representing the + number of teaspoons she used is 7/20, she used 7/20\ *120 = + <<7/20*\ 120=42>>42 #### 42 + +Step 1: Prepare the dataset +---------------------------- + +We preprocess the dataset in parquet format so that (1) it contains necessary fields for computing RL rewards and (2) is faster to read. + +.. code-block:: bash + + python3 examples/data_preprocess/gsm8k.py --local_save_dir ~/data/gsm8k + +Step 2: Download a model for post-training +------------------------------------------- + +In this example, we start with the ``Qwen2.5-0.5B-Instruct`` model. + +If you want to perform SFT before RL, refer to the :doc:`Complete GSM8K Example<../examples/gsm8k_example>`, the `sft directory `_ and `SFT Trainer `_ for further details. + +.. code-block:: bash + + python3 -c "import transformers; transformers.pipeline('text-generation', model='Qwen/Qwen2.5-0.5B-Instruct')" + +Step 3: Perform PPO training with the instruct model +---------------------------------------------------------------------- + +**Reward Model/Function** + +We use a pre-defined rule-based reward model. We force the model to produce a final +answer following 4 “#” as shown in the solution. We extract the final +answer from both the solution and model's output using regular +expression matching. We assign a reward of 1 to correct +answer, 0.0 to incorrect answer and 0 to no answer. + +For more details, please refer to `verl/utils/reward_score/gsm8k.py `_. + +**Training Script** + +Now let's run PPO training with the dataset and model above. [2]_ + + +Set the ``data.train_files`` ,\ ``data.val_files``, ``actor_rollout_ref.model.path`` and ``critic.model.path`` based on your dataset and model names or paths. +You may set ``VERL_USE_MODELSCOPE=True`` to download models from `modelscope `_ instead of `huggingface `_. + +.. code-block:: bash + + PYTHONUNBUFFERED=1 python3 -m verl.trainer.main_ppo \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.train_batch_size=256 \ + data.max_prompt_length=512 \ + data.max_response_length=512 \ + actor_rollout_ref.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=4 \ + critic.optim.lr=1e-5 \ + critic.model.path=Qwen/Qwen2.5-0.5B-Instruct \ + critic.ppo_micro_batch_size_per_gpu=4 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.logger=console \ + trainer.val_before_train=False \ + trainer.n_gpus_per_node=1 \ + trainer.nnodes=1 \ + trainer.save_freq=10 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 2>&1 | tee verl_demo.log + +You are expected to see the following logs, indicating training in progress. The key metric ``val/test_score/openai/gsm8k`` is computed every ``trainer.test_freq`` steps: + +.. code-block:: bash + + step:0 - timing/gen:21.470 - timing/ref:4.360 - timing/values:5.800 - actor/reward_kl_penalty:0.000 - actor/reward_kl_penalty_coeff:0.001 - timing/adv:0.109 - timing/update_critic:15.664 - critic/vf_loss:14.947 - critic/vf_clipfrac:0.000 - critic/vpred_mean:-2.056 - critic/grad_norm:1023.278 - critic/lr(1e-4):0.100 - timing/update_actor:20.314 - actor/entropy_loss:0.433 - actor/pg_loss:-0.005 - actor/pg_clipfrac:0.000 - actor/ppo_kl:0.000 - actor/grad_norm:1.992 - actor/lr(1e-4):0.010 - critic/score/mean:0.004 - critic/score/max:1.000 - critic/score/min:0.000 - critic/rewards/mean:0.004 - critic/rewards/max:1.000 - critic/rewards/min:0.000 - critic/advantages/mean:-0.000 - critic/advantages/max:2.360 - critic/advantages/min:-2.280 - critic/returns/mean:0.003 - critic/returns/max:0.000 - critic/returns/min:0.000 - critic/values/mean:-2.045 - critic/values/max:9.500 - critic/values/min:-14.000 - response_length/mean:239.133 - response_length/max:256.000 - response_length/min:77.000 - prompt_length/mean:104.883 - prompt_length/max:175.000 - prompt_length/min:68.000 + step:1 - timing/gen:23.020 - timing/ref:4.322 - timing/values:5.953 - actor/reward_kl_penalty:0.000 - actor/reward_kl_penalty:0.001 - timing/adv:0.118 - timing/update_critic:15.646 - critic/vf_loss:18.472 - critic/vf_clipfrac:0.384 - critic/vpred_mean:1.038 - critic/grad_norm:942.924 - critic/lr(1e-4):0.100 - timing/update_actor:20.526 - actor/entropy_loss:0.440 - actor/pg_loss:0.000 - actor/pg_clipfrac:0.002 - actor/ppo_kl:0.000 - actor/grad_norm:2.060 - actor/lr(1e-4):0.010 - critic/score/mean:0.000 - critic/score/max:0.000 - critic/score/min:0.000 - critic/rewards/mean:0.000 - critic/rewards/max:0.000 - critic/rewards/min:0.000 - critic/advantages/mean:0.000 - critic/advantages/max:2.702 - critic/advantages/min:-2.616 - critic/returns/mean:0.000 - critic/returns/max:0.000 - critic/returns/min:0.000 - critic/values/mean:-2.280 - critic/values/max:11.000 - critic/values/min:-16.000 - response_length/mean:232.242 - response_length/max:256.000 - response_length/min:91.000 - prompt_length/mean:102.398 - prompt_length/max:185.000 - prompt_length/min:70.000 + +Checkout ``Algorithm Baselines`` page for full training and validation logs for reference. + +The checkpoint is saved at the following dir by default: ``checkpoints/${trainer.project_name}/${trainer.experiment_name}``. You can merge the saved checkpoints to huggingface model using ``verl.model_merger`` module, for example: + +.. code-block:: bash + + python3 -m verl.model_merger merge \ + --backend fsdp \ + --local_dir checkpoints/${trainer.project_name}/${trainer.experiment_name}/global_step_1/actor \ + --target_dir checkpoints/${trainer.project_name}/${trainer.experiment_name}/global_step_1/actor/huggingface + +For more details about checkpoint and model merging, please refer to :ref:`checkpoint-page`. + +To enable ``wandb`` for experiment tracking, set the following configs: + +.. code-block:: bash + + trainer.logger='["console","wandb"]' \ + trainer.project_name=$YOUR_PROJECT_NAME \ + trainer.experiment_name=$YOUR_RUN_NAME \ + +If you encounter out of memory issues with HBM less than 32GB, enable the following configs would help: + +.. code-block:: bash + + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 \ + critic.ppo_micro_batch_size_per_gpu=1 \ + +For the full set of configs, please refer to :ref:`config-explain-page` for detailed explanation and performance tuning. + + +.. [1] The original paper (https://arxiv.org/pdf/2110.14168) mainly focuses on training a verifier (a reward model) to solve math problems via Best-of-N sampling. In this example, we train an RL agent using a rule-based reward model. +.. [2] More training script examples for FSDP and Megatron-LM backend are stored in `examples/ppo_trainer `_ directory. diff --git a/verl_0720_main/verl/docs/start/ray_debug_tutorial.rst b/verl_0720_main/verl/docs/start/ray_debug_tutorial.rst new file mode 100644 index 0000000000000000000000000000000000000000..9e7c87dfaee0c04f24bdb6921717b8068d1ee6a2 --- /dev/null +++ b/verl_0720_main/verl/docs/start/ray_debug_tutorial.rst @@ -0,0 +1,96 @@ +Ray Debug Tutorial +================== + +Last updated: 04/23/2025 + + +.. _wuxibin89: https://github.com/wuxibin89 + +Author: `Ao Shen `_. + +How to debug? +--------------------- + + +Ray Distributed Debugger VSCode Extension (Recommended) +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +1. Starting with Ray 2.39, Anyscale has introduced the `Ray Distributed Debugger `_ VSCode extension. Follow the extension’s installation instructions, then add your cluster using the dashboard URL you obtained earlier. + + .. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/debugger.png?raw=true + :alt: Ray Distributed Debugger VSCode extension screenshot + +2. Prerequisites. + + Ensure the following are installed (see the extension README for more detail): + + - Visual Studio Code + - `ray[default]` >= 2.9.1 + - `debugpy` >= 1.8.0 + + .. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/readme.png?raw=true + :alt: VSCode with Ray prerequisites + +3. Environment Variables. + + To enable post‑mortem debugging, set: + + .. code-block:: bash + + export RAY_DEBUG_POST_MORTEM=1 + + .. admonition:: Note + :class: important + + Be sure to remove any legacy flags before starting Ray: + + - `RAY_DEBUG=legacy` + - `--ray-debugger-external` + +4. Configuring BreakpointsSet up breakpoint() in your code, and submit job to cluster. Then the extension will show the breakpoint information. + + + 1. Insert `breakpoint()` calls into your remote functions. + 2. Submit your job to the cluster. + + The extension will detect active breakpoints and display them in VSCode. + + **Note:** Breakpoints are only supported inside functions decorated with `@ray.remote`. + +5. Launching the Debugger. + + Run your job directly from the command line (do not use a `launch.json`): + + .. code-block:: bash + + python job.py + +6. Attaching to a Breakpoint. + + Once the process hits the first `breakpoint()`, click the Ray Distributed Debugger icon in the VSCode sidebar to attach the debugger. + + .. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/launch.png?raw=true + :alt: Attaching VSCode debugger to Ray process + +7. Debugging With Multiple breakpoint(). + + For each subsequent task, first disconnect the current debugger session, then click the extension icon again to attach to the next breakpoint. + + .. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/disconnect.png?raw=true + :alt: Disconnecting and reconnecting the debugger + +Legacy Ray Debugger +~~~~~~~~~~~~~~~~~~~ +1. Ray has a builtin legacy `debugger `_ that allows you to debug your distributed applications. To enable debugger, start ray cluster with ``RAY_DEBUG=legacy`` and ``--ray-debugger-external``. + +.. code-block:: bash + + # start head node + RAY_DEBUG=legacy ray start --head --dashboard-host=0.0.0.0 --ray-debugger-external + # start worker node + RAY_DEBUG=legacy ray start --address='10.124.46.192:6379' --ray-debugger-external + +2. Set up breakpoint in your code, and submit job to cluster. Then run ``ray debug`` to wait breakpoint: + +.. image:: https://github.com/eric-haibin-lin/verl-community/blob/main/docs/ray/legacy.png?raw=true + diff --git a/verl_0720_main/verl/docs/workers/automodel_workers.rst b/verl_0720_main/verl/docs/workers/automodel_workers.rst new file mode 100644 index 0000000000000000000000000000000000000000..e12bee5270469ee4f41d8e4174a7739b17a4e582 --- /dev/null +++ b/verl_0720_main/verl/docs/workers/automodel_workers.rst @@ -0,0 +1,65 @@ +Automodel Backend +================= + +Last updated: 03/07/2026. + +We support the Automodel (nemo_automodel) backend by implementing the +``AutomodelEngine`` and ``AutomodelEngineWithLMHead`` engine classes. +The Automodel backend delegates model building, parallelization, optimizer +sharding, LR scheduling, gradient clipping, and checkpointing to +nemo_automodel's infrastructure while using verl's training loop, +data pipeline, and loss function. + +**Requirements** + +- Automodel r0.3.0 +- transformers v5.0.0 + +**Pros** + +- Supports FSDP2 and TP distributed strategies out of + the box. + +- Native support for Mixture-of-Experts (MoE) models with Expert + Parallelism (EP) via DeepEP. + +- TransformerEngine (TE) integration for optimized attention, linear + layers, and RMSNorm. + +- Readily supports any HuggingFace model without checkpoint conversion. + +**Cons** + +- Pipeline parallelism is not yet supported. + + +SFT Examples +------------ + +We provide example SFT training scripts using the Automodel backend in +`examples/sft/gsm8k/ `_. + +Basic: Qwen2.5-0.5B with FSDP2 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A minimal example using ``Qwen/Qwen2.5-0.5B-Instruct`` with FSDP2 and +no parallelism: + +.. code:: shell + + bash examples/sft/gsm8k/run_qwen2_5_0_5b_automodel.sh 4 /tmp/automodel_sft_test + +See `run_qwen2_5_0_5b_automodel.sh `_. + +Advanced: Qwen3-30B MoE with Expert Parallelism +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +A larger-scale example using ``Qwen/Qwen3-30B-A3B-Base`` (MoE model) +with Expert Parallelism (EP=8), DeepEP, TransformerEngine backend, and +torch_mm experts backend: + +.. code:: shell + + bash examples/sft/gsm8k/run_qwen3_30b_automodel.sh 8 /tmp/automodel_sft_30b + +See `run_qwen3_30b_automodel.sh `_. diff --git a/verl_0720_main/verl/docs/workers/engine_workers.rst b/verl_0720_main/verl/docs/workers/engine_workers.rst new file mode 100644 index 0000000000000000000000000000000000000000..fa4f0fdbbc7d2d3e55e9d01efaec8fe7a3e5af1f --- /dev/null +++ b/verl_0720_main/verl/docs/workers/engine_workers.rst @@ -0,0 +1,249 @@ +Engine Workers +============== + +Last updated: 04/20/2026. + +:mod:`verl.workers.engine_workers` provides the worker-layer classes that +``RayWorkerGroup`` instantiates for PPO / GRPO / SFT style RL training. +They are **engine agnostic** – FSDP, FSDP2, Megatron-LM, Automodel, +TorchTitan and VeOmni are all wired in through the same entry points. +The specific backend is selected at runtime from ``actor.strategy`` / +``critic.strategy`` and resolved by +:class:`verl.workers.engine.EngineRegistry`. + +For the engine-layer design (how ``BaseEngine`` subclasses implement +``forward_step``, parallelism, checkpointing, weight export, etc.) see +:doc:`model_engine`. + +Class Hierarchy +--------------- + +:: + + ActorRolloutRefWorker # hybrid worker, co-locates actor + rollout + optional ref + ├── self.actor : TrainingWorker (built if role contains "actor") + ├── self.ref : TrainingWorker (built if role contains "ref") + ├── self.rollout: BaseRollout (vLLM / SGLang, built if role contains "rollout") + └── self.checkpoint_engine (built if role contains "actor") + + TrainingWorker # generic "one engine + optimizer + profiler" worker + └── self.engine : BaseEngine (fsdp / fsdp2 / megatron / automodel / veomni / torchtitan) + +``TrainingWorker`` is also used standalone for the critic, reference +model, reward model and SFT / DPO training – it's essentially a +Ray-wrapped ``BaseEngine`` with a Tinker-like API +(https://thinkingmachines.ai/tinker/) exposed as RPCs. + +ActorRolloutRefWorker +--------------------- + +:class:`verl.workers.engine_workers.ActorRolloutRefWorker` is the +hybrid worker used for actor, rollout and (optional) reference policy. +The ``role`` argument selects which sub-workers are constructed: + +========================= =========================================================================== +role What is built inside ``init_model`` +========================= =========================================================================== +``actor`` ``self.actor`` (``TrainingWorker``) + checkpoint engine +``rollout`` ``self.rollout`` (``BaseRollout``) +``ref`` ``self.ref`` (``TrainingWorker`` with ``forward_only`` engine config) +``actor_rollout`` actor + rollout + checkpoint engine (most common for colocated PPO) +``actor_rollout_ref`` all three +========================= =========================================================================== + +Key RPCs +^^^^^^^^ + +1. ``init_model`` + + .. code:: python + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + + ``ONE_TO_ALL``: the driver calls ``init_model`` and the same routine + runs on every worker. It builds the ``TrainingWorker`` (which in turn + builds the ``BaseEngine`` via ``EngineRegistry.new``), the rollout + engine, and the checkpoint engine used for trainer→rollout weight + sync. + +2. ``compute_log_prob`` / ``compute_ref_log_prob`` + + .. code:: python + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="actor")) + def compute_log_prob(self, data: TensorDict) -> TensorDict: + return self.actor.infer_batch(data) + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="ref")) + def compute_ref_log_prob(self, data: TensorDict) -> TensorDict: + return self.ref.infer_batch(data) + + ``TrainingWorker.infer_batch`` drives ``BaseEngine.infer_batch`` (eval + mode + ``no_grad``). The n-d dispatch function is built from the + engine's actual parallel topology, so Megatron's PP dimension is + surfaced as an extra DP dimension to the single controller without + needing a backend-specific dispatch mode. + +3. ``update_actor`` + + .. code:: python + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="actor")) + def update_actor(self, data: TensorDict) -> TensorDict: + return self.actor.train_mini_batch(data=data) + + ``train_mini_batch`` splits the batch into mini-batches, iterates + over PPO epochs, and calls ``TrainingWorker.train_batch`` for each + mini-batch (one optimizer step per mini-batch). The PPO loss + or distillation loss is wired by ``init_model`` via + ``TrainingWorker.set_loss_fn``. + +4. ``update_weights`` + + .. code:: python + + @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) + async def update_weights(self, global_steps: int = None): + + Push the freshest trainer weights to the rollout engine. + + - For **colocated sync training** (``checkpoint_engine.backend == + "naive"``): export per-tensor parameters via + ``engine.get_per_tensor_param`` and call ``rollout.update_weights`` + directly. LoRA adapters are merged into base weights up-front when + ``model.lora.merge=True``. + - For **disaggregated async training**: send the weights through + ``self.checkpoint_engine.send_weights`` instead. The transport is chosen by + ``checkpoint_engine.backend`` (e.g. ``nccl`` for a full-weight broadcast, or + ``delta`` to broadcast only the parameters that changed since the previous + sync — see :doc:`../advance/one_step_off`). + +5. ``save_checkpoint`` / ``load_checkpoint`` + + Both delegate to the actor ``TrainingWorker``, which in turn calls + ``BaseEngine.save_checkpoint`` / ``load_checkpoint``. The backend + engine is responsible for sharded model + optimizer + scheduler state + (and HuggingFace export when applicable). + +TrainingWorker +-------------- + +:class:`verl.workers.engine_workers.TrainingWorker` is the generic +worker for a single engine + optimizer + profiler. It is used: + +- As ``self.actor`` / ``self.ref`` inside ``ActorRolloutRefWorker``. +- As the critic / reward worker (via ``add_critic_worker`` / + ``add_reward_model_worker`` in ``verl/trainer/main_ppo.py``). +- Standalone for SFT / DPO training. + +Construction takes a single +:class:`verl.workers.config.TrainingWorkerConfig` which bundles the +``model_config``, ``engine_config``, ``optimizer_config``, +``checkpoint_config`` and ``profiler_config``. The backend is chosen +from ``engine_config.strategy`` (``fsdp``, ``fsdp2``, ``megatron``, +``automodel``, ``veomni``, ``torchtitan``). + +Key RPCs +^^^^^^^^ + +- ``reset()`` – first call initializes the engine; subsequent calls + reload weights and reset optimizer / scheduler state. +- ``to(device, model=True, optimizer=True, grad=True)`` – manual + load/offload control. ``device`` must be either ``"cpu"`` or + ``"device"`` (which is mapped to the actual accelerator name). +- ``set_loss_fn(loss_fn)`` – install the loss closure (PPO loss, + distillation loss, or any custom callable that + accepts ``(model_output, batch)``). +- ``train_mini_batch(data)`` – mini-batch + PPO-epoch loop; one + optimizer step per mini-batch; allgather metrics across DP. +- ``train_batch(data)`` – single mini-batch train step. Usually invoked + indirectly via ``train_mini_batch``. +- ``infer_batch(data)`` – forward-only step used for log-prob / value / + reward / distillation-teacher computation. Supports + ``no_lora_adapter=True`` to temporarily disable the adapter at + inference. +- ``save_checkpoint`` / ``load_checkpoint`` – delegate to + ``BaseEngine``. + +Backend Selection +----------------- + +Set the ``strategy`` field on ``actor.engine`` / ``critic.engine`` / +``ref.engine`` in your Hydra config: + +.. code-block:: yaml + + actor_rollout_ref: + actor: + strategy: fsdp2 # or: fsdp, megatron, automodel, veomni, torchtitan + engine: + strategy: fsdp2 + param_offload: False + # ... + +The ``EngineRegistry`` dispatches on ``(model_type, backend, device)`` – +for example ``(language_model, fsdp2, cuda)`` or +``(language_model, megatron, npu)``: + +===================== ====================== ===================== ===================================================================== +model_type backend device Engine class +===================== ====================== ===================== ===================================================================== +``language_model`` ``fsdp`` / ``fsdp2`` ``cuda`` / ``npu`` ``verl.workers.engine.fsdp.FSDPEngineWithLMHead`` +``language_model`` ``megatron`` ``cuda`` ``verl.workers.engine.megatron.MegatronEngineWithLMHead`` +``language_model`` ``megatron`` ``npu`` ``verl.workers.engine.mindspeed.MindspeedEngineWithLMHead`` +``language_model`` ``mindspeed_megatron`` ``npu`` ``verl.workers.engine.mindspeed.MindSpeedMegatronEngineWithLMHead`` +``language_model`` ``automodel`` ``cuda`` ``verl.workers.engine.automodel.AutomodelEngineWithLMHead`` +``language_model`` ``veomni`` ``cuda`` / ``npu`` ``verl.workers.engine.veomni.VeOmniEngineWithLMHead`` +``language_model`` ``torchtitan`` ``cuda`` / ``npu`` ``verl.workers.engine.torchtitan.TorchTitanEngineWithLMHead`` +``value_model`` ``fsdp`` / ``fsdp2`` ``cuda`` / ``npu`` ``verl.workers.engine.fsdp.FSDPEngineWithValueHead`` +``value_model`` ``megatron`` ``cuda`` ``verl.workers.engine.megatron.MegatronEngineWithValueHead`` +===================== ====================== ===================== ===================================================================== + +Migrating from Legacy Workers +----------------------------- + +The legacy ``verl.workers.fsdp_workers`` / ``verl.workers.megatron_workers`` +modules (together with ``verl.workers.actor`` / ``verl.workers.critic`` +/ ``verl.workers.sharding_manager`` / ``verl.workers.legacy``) have been +removed. The table below summarises the equivalent entry points: + +============================================================== ========================================================================= +Legacy (removed) Current (``verl.workers.engine_workers``) +============================================================== ========================================================================= +``verl.workers.fsdp_workers.ActorRolloutRefWorker`` ``ActorRolloutRefWorker`` (``strategy=fsdp``/``fsdp2``) +``verl.workers.megatron_workers.ActorRolloutRefWorker`` ``ActorRolloutRefWorker`` (``strategy=megatron``) +``verl.workers.fsdp_workers.CriticWorker`` ``TrainingWorker`` (with critic config + value-model engine) +``verl.workers.megatron_workers.CriticWorker`` ``TrainingWorker`` (with critic config + value-model engine) +``verl.workers.actor.DataParallelPPOActor`` ``FSDPEngineWithLMHead`` + ``TrainingWorker`` +``verl.workers.actor.MegatronPPOActor`` ``MegatronEngineWithLMHead`` + ``TrainingWorker`` +``verl.workers.critic.DataParallelPPOCritic`` ``FSDPEngineWithValueHead`` + ``TrainingWorker`` +``verl.workers.critic.MegatronPPOCritic`` ``MegatronEngineWithValueHead`` + ``TrainingWorker`` +``verl.workers.sharding_manager.FSDPUlyssesShardingManager`` ``verl.utils.ulysses.FSDPUlyssesShardingManager`` +``Dispatch.MEGATRON_PP_AS_DP_PROTO`` ``make_nd_compute_dataproto_dispatch_fn(mesh_name=...)`` (derived from engine) +``use_legacy_worker_impl: True`` (removed; only the unified engine is available) +============================================================== ========================================================================= + +Extending +--------- + +To add a new backend, implement a ``BaseEngine`` subclass under +``verl/workers/engine//`` and register it with +``@EngineRegistry.register(model_type=..., backend=...)``. The worker +layer (``TrainingWorker`` / ``ActorRolloutRefWorker``) is already +engine-agnostic and will pick up the new backend as soon as +``engine_config.strategy`` is set accordingly. See :doc:`model_engine` +for the detailed extension guide and the test harness under +``tests/special_e2e/sft/``. + +Source +------ + +- :mod:`verl.workers.engine_workers` – + `engine_workers.py `__ +- :mod:`verl.workers.engine` – + `engine/ `__ +- :mod:`verl.workers.rollout` – + `rollout/ `__ +- Driver-side PPO glue – + `verl/trainer/main_ppo.py `__ diff --git a/verl_0720_main/verl/docs/workers/model_engine.rst b/verl_0720_main/verl/docs/workers/model_engine.rst new file mode 100644 index 0000000000000000000000000000000000000000..53653e581444cc61bc5f8dc9f9e28869d8859273 --- /dev/null +++ b/verl_0720_main/verl/docs/workers/model_engine.rst @@ -0,0 +1,125 @@ +Model Engine +============ + +.. _vermouth: https://github.com/vermouth1992 + +Author: `Chi Zhang `_ + +Last updated: 09/25/2025. + +Current Support Matrix +---------------------- + ++----------+-----------+--------------+-------------+--------------------------+ +| Backends | Model | Scalability | Model | Pain points | +| | Supported | | Definition | | +| | | | | | ++==========+===========+==============+=============+==========================+ +| FSDP | Day 1 | - Dense is OK| Huggingface | Monkey patch can be | +| + | support | | + monkey | easily impacted by | +| ulysses | HF model | - MoE is bad | patch | transformers version | ++----------+-----------+--------------+-------------+--------------------------+ +| MCore | Limited | Best | GPTModel | Supporting new models is | +| | | | (One model | difficult | +| | | | for all) | | ++----------+-----------+--------------+-------------+--------------------------+ + +- We monkey patch attention function to support ulysses +- We monkey patch VLM models to support FSDP with mixed data with and + without images + +Class Hierarchy +--------------- + +Note that all the workers and trainers run in **SPMD** mode. SFT/DPO/RM +trainer is directly invoked by ``torchrun``. The Actor/Critic worker can +also be invoked by a RayWorkerGroup and provides APIs to a single +controller. + +- Base Engine level: implement model init, optimizer init, lr scheduler + init, sharding, checkpoint manager. +- Full Engine level: subclass base engine and implement + ``forward_step``. +- Worker/SPMD trainer level: **engine agnostic**, implement training + logics using abstract engine APIs + +RL trainer utilizes workers to construct HybridFlow program. This is out +of the scope of model engine. + +Existing Model Types +-------------------- + +========== ====================== ====================== +Model type Language model Value model +========== ====================== ====================== +Input text/image/video/audio text/image/video/audio +Output logits for next token logits as value +========== ====================== ====================== + +Currently, we have two model types: language model and value model. We +expect to expand the category to include Qwen-Omni family (output both +text and audio) and VLA models. + +Data Format +----------- + +Currently, verl adopts left-right padding data format in RL trainer. +This creates massive padding when the discrepancy between response +length is large. We will start to implement no-padding format throughout +the whole system. + +.. image:: https://github.com/vermouth1992/verl-data/blob/master/images/data_format.png?raw=true + :alt: Data Format + +Here is the migration plan: +- Implement no-padding format in engine +- Add a transformation layer in Actor/Critic worker. +- Replace Actor/Critic Worker in RL trainer +- Implement no-padding throughput system + +Checkpoint System +----------------- + +.. image:: https://github.com/vermouth1992/verl-data/blob/master/images/verl-ckpt.png?raw=true + :alt: Model Engine Checkpoint System + +The engine constructs the model using huggingface config, then load +weights from huggingface checkpoint. If the engine directly uses +huggingface model definition, it can use function provided by +``transformers``. Otherwise, each engine has to write their own +checkpoint load logic (e.g., +`mbridge `__). During model +training, each engine has to implement save_checkpoint and +load_checkpoint that save/load intermediate sharded checkpoint including +model, optimizer and lr scheduler states. Each engine has to implement a +checkpoint merge script, that merges the intermediate sharded checkpoint +back to huggingface format. + +API +--- + +A tentative model engine API can be found: +https://github.com/verl-project/verl/blob/main/verl/workers/engine/base.py#L24 + +Extension +--------- + +Add a new backend +~~~~~~~~~~~~~~~~~ + +- Start a new folder under ``verl/workers/engine``. Then, implement + ``transformer_impl.py``. If you want to implement a non-transformer + model, please contact us in advance. +- Add the engine config to the GSM8k SFT trainer script: + https://github.com/verl-project/verl/blob/main/tests/special_e2e/sft/run_sft_engine_gsm8k.sh +- Invoke the tests with your backend: + https://github.com/verl-project/verl/blob/main/tests/special_e2e/sft/test_sft_engine_all.sh. + This test script will run various backends and various + configurations, and compare the loss and grad norm of the first step + to make sure they are close. + +Add a new model type +~~~~~~~~~~~~~~~~~~~~ + +- This is mainly reserved for models whose the output is not just text + (e.g., Qwen3-Omni). Please discuss with us before you proceed. diff --git a/verl_0720_main/verl/docs/workers/ray_trainer.rst b/verl_0720_main/verl/docs/workers/ray_trainer.rst new file mode 100644 index 0000000000000000000000000000000000000000..5b085ce59068fc2f45a055b1cce25145614c551b --- /dev/null +++ b/verl_0720_main/verl/docs/workers/ray_trainer.rst @@ -0,0 +1,241 @@ +PPO Ray Trainer +=============== + +Last updated: 02/12/2025. + +We implement the RayPPOTrainer, which is a trainer runs on the driver +process on a single CPU/GPU node (default is CPU). + +The PPORayTrainer include 3 core functions for data preparation, +WorkerGroup initialization and PPO training loop. + +Data Preparation +---------------- + +The ``PPORayTrainer``, as a single process, is responsible for loading a +complete batch of samples (prompts) from the dataset and then dispatch +to different worker_groups running on different GPUs. + +To generalize the data loading, we implement the ``RLHFDataset`` class +to load the preprocessed parquet files, apply chat templates to the +prompts, add padding, truncate prompts that exceed max prompt length and +then tokenize. + +.. code:: python + + self.train_dataset = RLHFDataset(data_files=self.config.data.train_files, + tokenizer=self.tokenizer, + config=self.config.data) + +Then, the dataloader will iterate the dataset under PPO mini batch size. + +WorkerGroup Initialization +-------------------------- + +We first introduce a basic implementation of initializing the +``WorkerGroup`` of the actor model on a given set of GPUs. + +.. code:: python + + # max_colocate_count means the number of WorkerGroups (i.e. processes) in each RayResourcePool + # For FSDP backend, we recommend using max_colocate_count=1 that merge all WorkerGroups into one. + # For Megatron backend, we recommend using max_colocate_count>1 that can utilize different WorkerGroup for differnt models + resource_pool = RayResourcePool(process_on_nodes=[config.trainer.n_gpus_per_node] * config.trainer.nnodes, + use_gpu=True, + max_colocate_count=1) + # define actor rollout cls to be init on remote + actor_rollout_cls = RayClassWithInitArgs(cls=ActorRolloutWorker) + # define actor_rollout worker group + actor_rollout_worker_group = MegatronRayWorkerGroup(resource_pool=resource_pool, + ray_cls_with_init=actor_rollout_cls, + default_megatron_kwargs=config.actor_rollout.megatron) + +Different WorkerGroups, like ``actor_rollout_worker_group`` , +``critic_worker_group`` and ``ref_worker_group`` lies on a separate +process in the above implementation. + +The driver process can then call the distributed compute function within +the ``actor_rollout_worker_group`` and other roles to construct the RL +training loop. + +For models colocated in the same set of GPUs, we further provide a +fine-grain optimization, which merge the ``worker_group`` of different roles +in the same process. This optimization can save the redundant +CUDA/distributed context in different processes. + +.. code:: python + + # initialize WorkerGroup + # NOTE: if you want to use a different resource pool for each role, which can support different parallel size, + # you should not use `create_colocated_worker_cls`. Instead, directly pass different resource pool to different worker groups. + # See TODO(url) for more information. + all_wg = {} + for resource_pool, class_dict in self.resource_pool_to_cls.items(): + worker_dict_cls = create_colocated_worker_cls(class_dict=class_dict) + wg_dict = self.ray_worker_group_cls(resource_pool=resource_pool, ray_cls_with_init=worker_dict_cls) + spawn_wg = wg_dict.spawn(prefix_set=class_dict.keys()) + all_wg.update(spawn_wg) + + if self.use_critic: + self.critic_wg = all_wg['critic'] + self.critic_wg.init_model() + + if self.use_reference_policy: + self.ref_policy_wg = all_wg['ref'] + self.ref_policy_wg.init_model() + + if self.use_rm: + self.rm_wg = all_wg['rm'] + self.rm_wg.init_model() + + # we should create rollout at the end so that vllm can have a better estimation of kv cache memory + self.actor_rollout_wg = all_wg['actor_rollout'] + self.actor_rollout_wg.init_model() + +.. note:: For megatron backend, if we merge the ``worker_groups`` into the same processes, all the roles will utilize the same 3D parallel size. To optimize this, we may need to maintain several 3D process groups for each role in the same distributed context. If you want to use different 3D parallel size for different roles, please follow the similar architecture of the first code block to initialize each role's ``worker_group`` + + +PPO Training Loop +----------------- + +We implement the PPO training loop by calling the functions in +worker_group of each role. The input and output data of each function is +a ``DataProto`` object implemented in `protocol.py `_. In the training +loop, trainer will dispatch/collect the data to/from different GPUs +following the transfer protocols wrapped in the workers' functions. The +computation of PPO micro batches is processed in ``update_actor`` and +``update_critic`` functions. + +To extend to other RLHF algorithms, such as DPO, GRPO, please refer to +:doc:`../advance/dpo_extension`. + +.. code:: python + + def fit(self): + """ + The training loop of PPO. + The driver process only need to call the compute functions of the worker group through RPC to construct the PPO dataflow. + The light-weight advantage computation is done on the driver process. + """ + from verl.utils.tracking import Tracking + from omegaconf import OmegaConf + + logger = Tracking(project_name=self.config.trainer.project_name, + experiment_name=self.config.trainer.experiment_name, + default_backend=self.config.trainer.logger, + config=OmegaConf.to_container(self.config, resolve=True)) + + global_steps = 0 + + # perform validation before training + # currently, we only support validation using the reward_function. + if self.val_reward_fn is not None: + val_metrics = self._validate() + pprint(f'Initial validation metrics: {val_metrics}') + + for epoch in range(self.config.trainer.total_epochs): + for batch_dict in self.train_dataloader: + metrics = {} + + batch: DataProto = DataProto.from_single_dict(batch_dict) + # batch = batch.to('cuda') + + # pop those keys for generation + gen_batch = batch.pop(batch_keys=['input_ids', 'attention_mask', 'position_ids']) + + # generate a batch + with Timer(name='gen', logger=None) as timer: + gen_batch_output = self.actor_rollout_wg.generate_sequences(gen_batch) + metrics['timing/gen'] = timer.last + + batch = batch.union(gen_batch_output) + + if self.use_reference_policy: + # compute reference log_prob + with Timer(name='ref', logger=None) as timer: + ref_log_prob = self.ref_policy_wg.compute_ref_log_prob(batch) + batch = batch.union(ref_log_prob) + metrics['timing/ref'] = timer.last + + # compute values + with Timer(name='values', logger=None) as timer: + values = self.critic_wg.compute_values(batch) + batch = batch.union(values) + metrics['timing/values'] = timer.last + + with Timer(name='adv', logger=None) as timer: + # compute scores. Support both model and function-based. + # We first compute the scores using reward model. Then, we call reward_fn to combine + # the results from reward model and rule-based results. + if self.use_rm: + # we first compute reward model score + reward_tensor = self.rm_wg.compute_rm_score(batch) + batch = batch.union(reward_tensor) + + # we combine with rule-based rm + reward_tensor = self.reward_fn(batch) + batch.batch['token_level_scores'] = reward_tensor + + # compute rewards. apply_kl_penalty if available + batch, kl_metrics = apply_kl_penalty(batch, + kl_ctrl=self.kl_ctrl_in_reward, + kl_penalty=self.config.algorithm.kl_penalty) + metrics.update(kl_metrics) + + # compute advantages, executed on the driver process + batch = compute_advantage(batch, + self.config.algorithm.gamma, + self.config.algorithm.lam, + adv_estimator=self.config.algorithm.adv_estimator) + metrics['timing/adv'] = timer.last + + # update critic + if self.use_critic: + with Timer(name='update_critic', logger=None) as timer: + critic_output = self.critic_wg.update_critic(batch) + metrics['timing/update_critic'] = timer.last + critic_output_metrics = reduce_metrics(critic_output.meta_info['metrics']) + metrics.update(critic_output_metrics) + + # implement critic warmup + if self.config.trainer.critic_warmup <= global_steps: + # update actor + with Timer(name='update_actor', logger=None) as timer: + actor_output = self.actor_rollout_wg.update_actor(batch) + metrics['timing/update_actor'] = timer.last + actor_output_metrics = reduce_metrics(actor_output.meta_info['metrics']) + metrics.update(actor_output_metrics) + + # validate + if self.val_reward_fn is not None and (global_steps + 1) % self.config.trainer.test_freq == 0: + with Timer(name='testing', logger=None) as timer: + val_metrics: dict = self._validate() + val_metrics = {f'val/{key}': val for key, val in val_metrics.items()} + metrics['timing/testing'] = timer.last + metrics.update(val_metrics) + + # collect metrics + data_metrics = compute_data_metrics(batch=batch) + metrics.update(data_metrics) + + # TODO: make a canonical logger that supports various backend + logger.log(data=metrics, step=global_steps) + + if self.config.trainer.save_freq > 0 and (global_steps + 1) % self.config.trainer.save_freq == 0: + actor_local_path = os.path.join(self.config.trainer.default_local_dir, 'actor', + f'global_step_{global_steps}') + actor_remote_path = os.path.join(self.config.trainer.default_hdfs_dir, 'actor') + self.actor_rollout_wg.save_checkpoint(actor_local_path, actor_remote_path) + + if self.use_critic: + critic_local_path = os.path.join(self.config.trainer.default_local_dir, 'critic', + f'global_step_{global_steps}') + critic_remote_path = os.path.join(self.config.trainer.default_hdfs_dir, 'critic') + self.critic_wg.save_checkpoint(critic_local_path, critic_remote_path) + + global_steps += 1 + + # perform validation after training + if self.val_reward_fn is not None: + val_metrics = self._validate() + pprint(f'Final validation metrics: {val_metrics}') diff --git a/verl_0720_main/verl/docs/workers/sglang_worker.rst b/verl_0720_main/verl/docs/workers/sglang_worker.rst new file mode 100644 index 0000000000000000000000000000000000000000..6edb8871730d3e9bd99d6a04e21f725baa2a3a74 --- /dev/null +++ b/verl_0720_main/verl/docs/workers/sglang_worker.rst @@ -0,0 +1,237 @@ +SGLang Backend +============== + +Last updated: 05/31/2025. + +**Authored By SGLang RL Team and listed alphabetically by last name** + +`Jingyi Chen `_, `Yitong Guan `_, `Zhuobin Huang `_, `Jiajun Li `_, `Ji Li `_, `Shenggui Li `_, `Junrong Lin `_, `Xiang Long `_, `Rui Lu `_, `Jin Pan `_, `Shuai Shi `_, `Yushen Su `_, `Xinyuan Tong `_, `Chendong Wang `_, `Hanchen Zhang `_, `Haoran Wang `_, `Yongan Xiang `_, `Chengxing Xie `_, `Yuhao Yang `_, `Jinwei Yao `_, `Qiaolin Yu `_, `Yuzhen Zhou `_, `Chenyang Zhao `_ + + + +Introduction +------------ +`SGLang `_ is an open-source state-of-the-art inference service engine, fully adopted by xAI to support all inference needs of Grok during research and serving processes. + +Currently, verl fully supports using SGLang as the inference engine during the rollout phase. As a rollout engine, SGLang provides the same feature coverage as vLLM., including memory saving and multi-node rollout features. After installing verl and SGLang, simply add ``actor_rollout_ref.rollout.name=sglang`` at startup script to seamlessly switch between the two inference frameworks. + +In addition, the SGLang team is actively working on supporting features such as Multi-Turn Agentic RL, VLM RLHF, Server-Based RLHF, and Partial Rollout. You can track the related development progress in the `Tracking Roadmap `_. + +Installation +------------ +Please always follow the following command to install SGLang with verl. + +.. code-block:: bash + + pip install --upgrade pip + # Currently 0.4.8, subject to updates at any time, please refer to the latest version specified in `setup.py` + pip install -e ".[sglang]" + +You can check the following dependencies are in your environment: + +.. note:: + + - **PyTorch**: 2.6.0+cu124 + - **CUDA**: 12.4 + - **flashinfer-python**: 0.2.5+cu124torch2.6 + - **SGLang**: 0.4.6.post5 + - **sgl-kernel**: 0.1.4 + +Using SGLang as the Inference Backend for PPO Training on a Single Machine +------------------------------------------------------------------------- +We use Qwen/Qwen2-7B-Instruct on the gsm8k dataset for a simple test. + +1. Run the following command to prepare the gsm8k dataset: + +.. code-block:: bash + + python3 examples/data_preprocess/gsm8k.py + +2. Run the following script to conduct a PPO experiment on a single machine with 4 GPUs: + +.. code-block:: bash + + export SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK=True + PYTHONUNBUFFERED=1 python3 -m verl.trainer.main_ppo \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.train_batch_size=4096 \ + data.max_prompt_length=4096 \ + data.max_response_length=4096 \ + actor_rollout_ref.rollout.name=sglang \ + actor_rollout_ref.model.path=Qwen/Qwen2-7B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=4 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + 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.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=4 \ + critic.optim.lr=1e-5 \ + critic.model.path=Qwen/Qwen2-7B-Instruct \ + critic.ppo_micro_batch_size_per_gpu=4 \ + critic.fsdp.param_offload=True \ + critic.fsdp.optimizer_offload=True \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.logger=console \ + trainer.val_before_train=False \ + trainer.n_gpus_per_node=4 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 2>&1 | tee verl_demo.log + +Why export SGL_DISABLE_TP_MEMORY_INBALANCE_CHECK? +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +1. ``verl`` initializes a ``SGLangRollout`` module during rollout, which is used to evaluate/generate samples. + +2. ``SGLangRollout`` will initialize ``Engine``, and further initialize a ``torch.distributed.DeviceMesh``, used to support Tensor Parallel (TP). + +3. ``DeviceMesh.init()`` internally checks the free GPU memory of all participating devices. If the difference is too large (more than ~10%), it directly reports an error to avoid initialization failures or deadlocks. + +Why might there be inconsistent GPU memory? +""""""""""""""""""""""""""""""""""""""""""" + +**1. Ray Distributed Actor loads the model at different times** + +``verl`` uses Ray-based multi-process, multi-GPU concurrent training. Each ``WorkerDict`` may be called at different times: + +.. code-block:: python + + self.rollout = SGLangRollout(...) + +Different workers initialize the model at different times → different memory usage. + +**2. Delayed initialization causes memory bias** + +Some workers start model loading/inference (e.g., ``generate_sequences()``, ``compute_log_prob()``) earlier than others. +Early workers already use up GPU memory → late workers still have empty memory → memory difference appears. + +**3. SGLang's TP init uses "all-device broadcast", but there's no uniform release timing** + +Although ``SGLangRollout`` may only involve subset of GPUs, its ``Engine`` initialization calls ``torch.distributed.init_process_group()`` and broadcasts weights, so: + +- Non-rollout GPUs also join the communication. +- Later on, ``DeviceMesh`` init will fail due to "inconsistent memory". + +**4. Different FSDP/TP loading behaviors also lead to mismatch** + +If using: + +.. code-block:: bash + + actor.fsdp_config.param_offload=True + ref.fsdp_config.param_offload=True + +Then some workers keep params on CPU while others already sharded to GPU → leads to asymmetric memory layout. + +Using SGLang as the Inference Backend for PPO Training Across Multiple Machines +------------------------------------------------------------------------------ +SGLang also supports running verl's RAY-based cross-machine inference in IPv4 and IPv6 scenarios. In the script below, we use TP=16 for cross-machine inference. Suppose we have two interconnected machines: node0 with IP 10.94.16.4 and node1 with IP 10.94.16.5. + +1. Start Ray on node0: + +.. code-block:: bash + + ray start --head --dashboard-host=0.0.0.0 + +You will see the following prompt: + +.. code-block:: bash + + Usage stats collection is enabled. To disable this, add `--disable-usage-stats` to the command that starts the cluster, or run the following command: `ray disable-usage-stats` before starting the cluster. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details. + + Local node IP: 10.94.16.4 + + -------------------- + Ray runtime started. + -------------------- + + Next steps + To add another node to this Ray cluster, run + ray start --address='10.94.16.4:6379' + +2. Have node1 join the Ray cluster: + +Run the following command on node1: + +.. code-block:: bash + + ray start --address='10.94.16.4:6379' + +Run the following command to confirm that the Ray cluster now has two nodes: + +.. code-block:: bash + + ray status + +You can see that the cluster has two nodes with 16 GPUs: + +.. code-block:: bash + + ======== Autoscaler status: 2025-04-09 09:25:37.694016 ======== + Node status + --------------------------------------------------------------- + Active: + 1 node_ef382ffd687d8f6b060c1b68e63ada7341b936fe5b1901dd04de1027 + 1 node_1eb4d7d07e793114c23a89d1a41f1f76acf6ef5b35af844a4ee8e4ba + Pending: + (no pending nodes) + Recent failures: + (no failures) + + Resources + --------------------------------------------------------------- + Usage: + 0.0/360.0 CPU + 0.0/16.0 GPU + 0B/3.39TiB memory + 0B/372.53GiB object_store_memory + +3. Run the following script to train meta-llama/Llama-3.1-8B-Instruct with TP=16 across 2 machines using 16 GPUs: + +.. code-block:: bash + + DATA_DIR=$HOME/data/gsm8k + + python3 -m verl.trainer.main_ppo \ + actor_rollout_ref.rollout.name=sglang \ + data.train_files=$DATA_DIR/train.parquet \ + data.val_files=$DATA_DIR/test.parquet \ + data.train_batch_size=4096 \ + data.max_prompt_length=4096 \ + data.max_response_length=4096 \ + actor_rollout_ref.model.path=meta-llama/Llama-3.1-8B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=16 \ + 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.rollout.log_prob_micro_batch_size_per_gpu=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=16 \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.8 \ + actor_rollout_ref.rollout.free_cache_engine=True \ + actor_rollout_ref.ref.log_prob_micro_batch_size=16 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.use_remove_padding=True \ + critic.model.path=meta-llama/Llama-3.1-8B-Instruct \ + critic.model.enable_gradient_checkpointing=True \ + critic.ppo_micro_batch_size=16 \ + critic.fsdp.param_offload=True \ + critic.fsdp.optimizer_offload=True \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=console \ + trainer.val_before_train=True \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=2 \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 2>&1 | tee verl_demo.log diff --git a/verl_0720_main/verl/docs/workers/torchtitan_workers.rst b/verl_0720_main/verl/docs/workers/torchtitan_workers.rst new file mode 100644 index 0000000000000000000000000000000000000000..c1590afe5176e16ed178f4fc8eb410b0db3ee9ac --- /dev/null +++ b/verl_0720_main/verl/docs/workers/torchtitan_workers.rst @@ -0,0 +1,129 @@ +TorchTitan Backend +================== + +Last updated: 07/08/2026. + +We support the `TorchTitan `_ backend by +implementing the ``TorchTitanEngine`` and ``TorchTitanEngineWithLMHead`` engine +classes. The TorchTitan backend delegates model building, parallelization +(FSDP2 / TP / CP / EP), optimizer construction and sharding, LR scheduling, +gradient clipping, and checkpointing to TorchTitan's infrastructure, while using +verl's own training loop (``forward_backward_batch``), data pipeline, and loss +function. Pipeline parallelism is not yet supported by the engine. + +Enable it with ``model_engine=torchtitan``. + +**Requirements** + +- A recent TorchTitan **nightly** (the engine uses TorchTitan's ``Trainer``, + ``ParallelismConfig.spmd_backend``, and ``activation_checkpoint`` APIs). + TorchTitan declares no ``torch`` dependency, so its date can float freely. +- A matching PyTorch **nightly** recent enough to support the ``spmd_types`` + SPMD backend (verified with ``torch>=2.14.0.dev20260625``; the DTensor / + ``fully_shard`` fixes it depends on landed around then). +- **Use ABI-compatible nightly builds of the torch-compiled packages.** + ``torchvision`` and (with the vLLM rollout backend) ``vllm`` ship extensions + ABI-locked to ``torch``, so install them from the PyTorch nightly index at close + build dates. ``vllm`` is the binding constraint: it must be old enough for + verl's rollout API yet built for a nightly ``torch``. The e2e CI test uses this + known-good set: + + .. code:: text + + vllm 1.0.0.dev20260620+cu130 # newest nightly-torch vLLM verl's rollout supports + torch 2.14.0.dev20260625+cu130 # >= spmd_types fix floor; ABI-compatible with vLLM + torchvision 0.29.0.dev20260626+cu130 # pins torch dev0625 exactly (0-day ABI gap) + torchtitan 0.1.0.dev20260701+cu130 # no torch dep; date can float + +- Attention-backend-specific requirements: + + - ``flex`` — no extra dependency (torch built-in FlexAttention). + - ``flex_flash`` — FlexAttention FLASH kernel; Hopper/Blackwell (CUDA + capability >= 9.0) only. + - ``varlen`` — torch built-in variable-length attention; uses FA3 on Hopper + (SM 9.0), FA2 on older GPUs. + +**Pros** + +- N-D parallelism out of the box: FSDP2 (with HSDP replicate), Tensor + Parallelism (TP), Context Parallelism (CP), and Expert Parallelism (EP) for + MoE models — combinable in a single run. + +- ``torch.compile`` support for higher training throughput. + +- Selective or full activation checkpointing, configurable per run for + memory/compute tradeoffs. + +- Multiple attention backends: FlexAttention (with a FLASH kernel on + Hopper/Blackwell) and variable-length attention. + +- Parameter and optimizer-state offload to CPU to fit larger models. + + +**Cons** + +- Pipeline parallelism is not yet supported (``pipeline_parallel_size`` is + accepted by the config but ``model_forward_step`` raises ``NotImplementedError``). + + +Installation +------------ + +TorchTitan and its matching PyTorch build are **nightly-only** (the +``spmd_types`` APIs are not in any stable PyPI release yet), and both come from +the PyTorch nightly index rather than PyPI. Install them together, choosing the +index that matches your CUDA version (``cu130`` shown here; use ``cu126`` etc. +as appropriate): + +.. code:: shell + + # 1. Install matching nightly torch + torchtitan from the PyTorch nightly index + uv pip install --pre torch torchtitan \ + --index-url https://download.pytorch.org/whl/nightly/cu130 + + # 2. Install verl (its other deps resolve from PyPI as usual) + uv pip install -e . + +The commands below are the recommended settings, tested in verl's e2e CI. Install +order matters: vLLM pins an older ``torch``, so it goes first and +``torch``/``torchvision`` are bumped afterward with ``--no-deps``: + +.. code:: shell + + INDEX=https://download.pytorch.org/whl/nightly/cu130 + uv pip install --pre vllm==1.0.0.dev20260620+cu130 --extra-index-url $INDEX + uv pip install --pre torchtitan==0.1.0.dev20260701+cu130 --extra-index-url $INDEX + uv pip install --pre --no-deps \ + torch==2.14.0.dev20260625+cu130 \ + torchvision==0.29.0.dev20260626+cu130 \ + --extra-index-url $INDEX + + +PPO Example +----------- + +An end-to-end GRPO example on GSM8K with the TorchTitan engine is provided at +`tests/special_e2e/run_ppo_trainer_torchtitan.sh `_. + +Basic: Qwen3-0.6B with FSDP2 + spmd_types +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Qwen3-0.6B, pure FSDP across 4 GPUs. ``flex`` attention, the ``spmd_types`` +backend, and selective activation checkpointing are the script defaults: + +.. code:: shell + + NUM_GPUS=4 FSDP_SIZE=4 bash tests/special_e2e/run_ppo_trainer_torchtitan.sh + +The script also exposes ``TP_SIZE``, ``EP_SIZE``, ``ATTN_TYPE``, +``SPMD_BACKEND``, and ``AC_MODE`` as environment variables to override those +defaults. + +Adding tensor parallelism +^^^^^^^^^^^^^^^^^^^^^^^^^^ + +To mirror ``FSDP_SIZE=2 TP_SIZE=2`` on 4 GPUs: + +.. code:: shell + + NUM_GPUS=4 FSDP_SIZE=2 TP_SIZE=2 bash tests/special_e2e/run_ppo_trainer_torchtitan.sh diff --git a/verl_0720_main/verl/docs/workers/trtllm_worker.rst b/verl_0720_main/verl/docs/workers/trtllm_worker.rst new file mode 100644 index 0000000000000000000000000000000000000000..752b02bd1a01b0ab6da5aaba8ff2d9047e4d6ff9 --- /dev/null +++ b/verl_0720_main/verl/docs/workers/trtllm_worker.rst @@ -0,0 +1,74 @@ +TensorRT-LLM Backend +==================== + +Last updated: 5/6/2026. + +**Authored By NVIDIA TensorRT-LLM Team** + +Introduction +------------ +`TensorRT-LLM `_ is a high-performance LLM inference engine with state-of-the-art optimizations for NVIDIA GPUs. +The verl integration of TensorRT-LLM is based on TensorRT-LLM's `Ray orchestrator `_, with more features and performance optimizations to come. + +- For **synchronous training**, the TensorRT-LLM rollout adopts a mixed design combining aspects of the hybrid engine and colocated mode, instead of relying purely on standard colocated mode. +- For **asynchronous training**, the TensorRT-LLM rollout follows other rollout backends and uses standalone mode for trainer and rollout placement. + +TensorRT-LLM rollout supports the following key features, primarily tested on Qwen3 dense and MoE variants: + +- Synchronous training (GRPO, DAPO, etc.) +- Cross-node inference +- FP8 refit +- Asynchronous training (further optimizations planned) +- Preliminary support for VLM + +You can track our roadmap and share feedback at the `TensorRT-LLM rollout roadmap `_. + + +Installation +------------ +We recommend using `docker/Dockerfile.stable.trtllm `_ for building a docker image with TensorRT-LLM pre-installed. The verl integration is supported from ``nvcr.io/nvidia/tensorrt-llm/release:1.2.0rc6``, and you can choose other TensorRT-LLM versions via ``TRTLLM_BASE_IMAGE`` from the `NGC Catalog `_. The image is updated periodically to track TensorRT-LLM's weekly releases. + +Alternatively, refer to the `TensorRT-LLM installation guide `_ for compatible environments if you want to build your own. + +Install verl with TensorRT-LLM: + +.. code-block:: bash + + pip install --upgrade pip + pip install -e ".[trtllm]" + +.. note:: + + Using the TensorRT-LLM rollout requires setting the following environment variables before launching the Ray cluster. These have been included in all the example scripts: + + .. code-block:: bash + + # Clean all SLURM/MPI/PMIx env to avoid PMIx mismatch error. + for v in $(env | awk -F= '/^(PMI|PMIX|MPI|OMPI|SLURM)_/{print $1}'); do + unset "$v" + done + +Using TensorRT-LLM rollout for GRPO +------------------------------------ + +.. code-block:: bash + + ## For FSDP training engine + INFER_BACKEND=trtllm bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh + ## For Megatron-Core training engine + INFER_BACKEND=trtllm bash examples/grpo_trainer/run_qwen3_8b_megatron.sh + +Using TensorRT-LLM rollout for DAPO with FP8 +--------------------------------------------- + +.. code-block:: bash + + # For Megatron-Core training engine with FP8 rollout + INFER_BACKEND=trtllm ROLLOUT_QUANTIZATION=fp8 bash examples/grpo_trainer/run_qwen3_30b_a3b_megatron.sh + +Using TensorRT-LLM rollout in fully async with GRPO +---------------------------------------------------- +.. code-block:: bash + + # Fully async policy with Megatron-Core training engine + bash verl/experimental/fully_async_policy/shell/grpo_30b_a3b_base_math_megatron_4_4_mis_trtllm.sh diff --git a/verl_0720_main/verl/examples/README.md b/verl_0720_main/verl/examples/README.md new file mode 100644 index 0000000000000000000000000000000000000000..f38ca231f8eb44edc0ee40733fdf038a5cce16ff --- /dev/null +++ b/verl_0720_main/verl/examples/README.md @@ -0,0 +1,143 @@ +# verl Examples + +This directory hosts curated, minimal-dependency examples that drive +`verl.trainer.main_ppo` with the current Hydra API. Algorithm-specific +extensions, research baselines, and non-trivial entry points live under +`recipe/`; prefer that directory if you need a custom loss or reward beyond +what these examples show. + +## Conventions + +All run scripts follow the same shape: + +1. Canonical filename: + + ``` + run__.sh + ``` + + - ``: a single canonical size per model family. E.g. + `qwen3_8b`, `qwen3_30b_a3b`, `qwen3_235b_a22b`, `qwen3_vl_8b`, + `deepseek_v3`, `mimo_7b`, `nemotron_nano_v3`. + - ``: one of `fsdp`, `fsdp2`, `megatron`, + `megatron_lite`, `mindspeed`, `automodel`, or `veomni`. **Must be the + final suffix before `.sh`**. + + Nothing follows ``. Per-example *features* — including + the inference backend (`vllm`/`sglang`/`trtllm`), the platform + (`DEVICE=gpu|npu`), the GPU machine type (`MACHINE=gb200`/`b200`/ + `blackwell`), Liger kernel, LoRA, FP8 quantization, sequence parallel + size, server vs sync rollout, etc. — do **not** show up in filenames. + They are exposed as env-var toggles inside the one canonical script. + Do not add `_npu`, `_amd`, `_vllm`, `_sglang`, `_trtllm`, or `_fp8` + script variants. For example, `sft/gsm8k/run_qwen2_5_0_5b_fsdp.sh` + covers plain SFT and its `USE_LIGER=1`, `SP_SIZE=2`, `USE_PEFT=1` + variants via env vars; `grpo_trainer/run_qwen3_8b_fsdp.sh` covers vLLM, + SGLang, and TRT-LLM rollouts, CUDA/NPU platforms, and `MACHINE=gb200` + (Blackwell) via toggles. + + This naming rule is enforced by the `check-example-naming` pre-commit + hook (see `tests/special_sanity/check_example_naming.py`). + +2. Every script exposes its important knobs in a user-adjustable region near + the top. Derived defaults and device/backend-specific details belong below + the "no user adjustment needed below" / "derived defaults" boundary. + Use uppercase env vars for user-facing knobs, e.g. + + ```bash + # ---- user-adjustable ---- + DEVICE=${DEVICE:-gpu} + INFER_BACKEND=${INFER_BACKEND:-vllm} + MODEL_PATH=${MODEL_PATH:-Qwen/Qwen3-8B} + NNODES=${NNODES:-1} + NDEVICES_PER_NODE=${NDEVICES_PER_NODE:-} + TRAIN_BATCH_SIZE=${TRAIN_BATCH_SIZE:-1024} + ROLLOUT_TP=${ROLLOUT_TP:-2} + ROLLOUT_N=${ROLLOUT_N:-5} + PROJECT_NAME=${PROJECT_NAME:-verl_grpo_gsm8k_math} + EXPERIMENT_NAME=${EXPERIMENT_NAME:-qwen3_8b_grpo_vllm_fsdp} + # ---- end user-adjustable ---- + ... + ``` + + Override anything you care about on the command line: + + ```bash + DEVICE=npu MODEL_PATH=/my/local/qwen3-8b NDEVICES_PER_NODE=4 bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh + ``` + + GPU and NPU paths should share the same `PROJECT_NAME` / + `EXPERIMENT_NAME` form. Do not append `_npu` to project or experiment + names just because `DEVICE=npu` is selected. + +3. Defaults (unless a directory explicitly documents otherwise): + + - `data.train_files` + `data.val_files` = GSM8K + MATH for text LLMs + (`geo3k` for vision, `dapo-math-17k` / `aime-2024` for scale-demo 235B / + 671B scripts). + - `actor_rollout_ref.actor.use_dynamic_bsz=True` + - `trainer.balance_batch=True` + - `trainer.logger=["console","wandb"]`. + +4. No deprecated Hydra knobs: + + - `ppo_megatron_trainer.yaml` → use `actor_rollout_ref.actor.model_engine=megatron`. + - `actor_rollout_ref.rollout.mode=async` → removed; async rollout is no + longer selected this way in example scripts. + - `actor_rollout_ref.hybrid_engine=True` → removed; the trainer now + enforces the supported hybrid-engine path internally. + - `ppo_micro_batch_size` / `log_prob_micro_batch_size` → use the + `_per_gpu` suffix. + - `data.val_batch_size` → removed. + - Top-level `reward_model.*` → use `reward_model.reward_model.*` / + `reward.reward_model.*` as applicable. + - `actor.ulysses_sequence_parallel_size` → use + `actor_rollout_ref.actor.fsdp_config.ulysses_sequence_parallel_size`. + +## Directory layout + +### Algorithm trainers + +Each directory holds a canonical recipe for one training algorithm. Adding a +new algorithm? If it needs its own trainer entry point or reward code, put it +under `recipe/` instead. + +| Dir | Algorithm | `algorithm.adv_estimator` / `policy_loss.loss_mode` | +|------------------------------------|--------------------------------|-----------------------------------------------------| +| `ppo_trainer/` | PPO (actor + critic) | `adv_estimator=gae` | +| `grpo_trainer/` | GRPO | `adv_estimator=grpo` | +| `rloo_trainer/` | RLOO | `adv_estimator=rloo` | +| `remax_trainer/` | ReMax | `adv_estimator=remax` | +| `reinforce_plus_plus_trainer/` | REINFORCE++ / baseline | `adv_estimator=reinforce_plus_plus[_baseline]` | +| `cispo_trainer/` | CISPO | `loss_mode=cispo` | +| `dppo_trainer/` | DPPO (TV / KL variants) | `loss_mode=dppo_tv \| dppo_kl` | +| `gdpo_trainer/` | GDPO | `adv_estimator=gdpo` | +| `gmpo_trainer/` | GMPO | `loss_mode=geo_mean` | +| `gpg_trainer/` | GPG | `adv_estimator=gpg`, `loss_mode=gpg` | +| `gspo_trainer/` | GSPO | `loss_mode=gspo` | +| `sapo_trainer/` | SAPO | `loss_mode=sapo` | +| `otb_trainer/` | OTB | `adv_estimator=optimal_token_baseline` | +| `mtp_trainer/` | DAPO + MTP (MiMo-7B) | `adv_estimator=grpo`, MTP flags | +| `on_policy_distillation_trainer/` | on-policy distillation | GRPO + distillation loss | +| `flowgrpo_trainer/` | Flow-GRPO (diffusion) | image-gen specific | + +### Feature / infra + +| Dir | Purpose | +|----------------------|------------------------------------------------------------------------------------------| +| `tuning/` | LoRA (`tuning/lora/`) and scaling demos (`tuning/scaling/`). | +| `profile/` | NPU profiler / torch-memory profiler runs. | +| `sft/` | Supervised fine-tuning examples. | +| `generation/` | Rollout-only inference launches. | +| `vllm_omni/` | vLLM omni backend examples. | +| `data_preprocess/` | Scripts that produce the `$HOME/data//*.parquet` layout the run scripts expect. | +| `prefix_grouper/` | Prefix-grouped rollout examples. | +| `rollout_correction/`| Rollout correction examples. | +| `router_replay/` | Router replay examples. | +| `tutorial/` | Tutorials and cluster launchers (`ray/`, `slurm/`, `skypilot/`, `agent_loop_get_started/`). | + +### Where are the algorithm research variants? + +`recipe/` — e.g. `recipe/dapo`, `recipe/prime`, `recipe/retool`, +`recipe/r1`, `recipe/spin`, `recipe/gvpo`, `recipe/flowrl`, ... +They ship their own trainer entry points and reward code. diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_235b_256k_megatron.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_235b_256k_megatron.sh new file mode 100644 index 0000000000000000000000000000000000000000..7674b06288e68e4e61b9894b6afc913fda5b8084 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_235b_256k_megatron.sh @@ -0,0 +1,207 @@ +#!/bin/bash +# set -xeuo pipefail + +## !!!!!!!supplement!!!!!! +## This script can be used for inference in 256 K and 128 K + +ulimit -n 32768 + +# Project Configuration +project_name='GRPO-Qwen3-235B-A22B-Instruct-MATH' +exp_name='GRPO-Qwen3-235B-A22B-Instruct-Megatron-vLLM' + +# Node Info +NNODES=${NNODES:-16} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +# Model Weights Paths +# MODEL_PATH=/mnt/weight/Qwen3-235B-A22B +MODEL_PATH=${WORK_DIR}/Qwen3-235B-A22B-Instruct-2507 +MCORE_MODEL_PATH=${WORK_DIR}/Qwen3-235B-A22B-Instruct-2507-Mcore +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + +# File System Paths +TRAIN_FILE=${WORK_DIR}/gsm8k/train.parquet +TEST_FILE=${WORK_DIR}/gsm8k/test.parquet + +# Data Configuration +max_prompt_length=$((1024 * 1)) +max_response_length=$((1024 * 255)) + +# Algorithm Configuration +adv_estimator=grpo +use_kl_in_reward=False +kl_coef=0.0 +use_kl_loss=True +kl_loss_coef=0.001 + +# Training Batch Configuration +train_prompt_bsz=4 +n_resp_per_prompt=4 +train_prompt_mini_bsz=4 + +# Performance and Memory Related Configuration +all_offload=True +use_dynamic_bsz=False +actor_ppo_max_token_len=$((max_prompt_length + max_response_length)) +infer_ppo_max_token_len=$((max_prompt_length + max_response_length)) +optimizer_offload_fraction=1 + +# Megatron Configuration +train_tp=2 +train_ep=16 +train_etp=1 +train_pp=16 +train_cp=8 + +# vLLM Configuration +gen_tp=4 +gen_dp=32 +gen_ep=128 +gpu_memory_utilization=0.7 +max_model_len=$((max_prompt_length + max_response_length)) +max_num_batched_tokens=2048 + +# Pipeline Layer Configuration +first_layer=5 +last_layer=5 + +# Data Configuration +DATA_ARGS=( + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + data.prompt_key=prompt + data.train_batch_size=${train_prompt_bsz} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=False + data.truncation='left' +) + +# Model Configuration +MODEL_ARGS=( + actor_rollout_ref.model.path="${MODEL_PATH}" + actor_rollout_ref.model.use_remove_padding=True +) + +# RL Algorithm Configuration +ALGORITHM_ARGS=( + algorithm.adv_estimator=${adv_estimator} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_ctrl.kl_coef=${kl_coef} +) + +# Actor Model Configuration +ACTOR_ARGS=( + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.entropy_coeff=0 + actor_rollout_ref.actor.ppo_epochs=1 + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} + actor_rollout_ref.actor.ppo_mini_batch_size=${train_prompt_mini_bsz} + actor_rollout_ref.actor.kl_loss_type=low_var_kl + actor_rollout_ref.actor.optim.clip_grad=1.0 + actor_rollout_ref.actor.optim.lr_warmup_steps=10 + actor_rollout_ref.actor.optim.weight_decay=0.1 + actor_rollout_ref.actor.optim.lr=1e-6 + +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_offload_fraction=${optimizer_offload_fraction} + +actor_rollout_ref.actor.optim.override_optimizer_config.use_precision_aware_optimizer=True + +actor_rollout_ref.actor.optim.override_optimizer_config.optimizer_cpu_offload=True + actor_rollout_ref.actor.megatron.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.actor.megatron.context_parallel_size=${train_cp} + actor_rollout_ref.actor.megatron.expert_model_parallel_size=${train_ep} + actor_rollout_ref.actor.megatron.expert_tensor_parallel_size=${train_etp} + actor_rollout_ref.actor.megatron.param_offload=${all_offload} + actor_rollout_ref.actor.megatron.optimizer_offload=${all_offload} + actor_rollout_ref.actor.megatron.grad_offload=${all_offload} + actor_rollout_ref.actor.megatron.use_dist_checkpointing=True + actor_rollout_ref.actor.megatron.use_mbridge=True + actor_rollout_ref.actor.megatron.dist_checkpointing_path=${MCORE_MODEL_PATH} + +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method=uniform + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity=full + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers=1 + +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_size=${train_cp} + +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_first_pipeline_stage=${first_layer} + +actor_rollout_ref.actor.megatron.override_transformer_config.num_layers_in_last_pipeline_stage=${last_layer} + +actor_rollout_ref.actor.megatron.override_transformer_config.normalization=RMSNorm + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rmsnorm=True + +actor_rollout_ref.actor.megatron.override_transformer_config.swiglu=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_distributed_optimizer=True + +actor_rollout_ref.actor.megatron.override_transformer_config.sequence_parallel=True +) + +# Reference Model Configuration +REF_ARGS=( + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + actor_rollout_ref.ref.megatron.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.ref.megatron.context_parallel_size=${train_cp} + actor_rollout_ref.ref.megatron.expert_model_parallel_size=${train_ep} + actor_rollout_ref.ref.megatron.expert_tensor_parallel_size=${train_etp} + actor_rollout_ref.ref.megatron.param_offload=${all_offload} + actor_rollout_ref.ref.megatron.use_mbridge=True + actor_rollout_ref.ref.megatron.use_dist_checkpointing=True + actor_rollout_ref.ref.megatron.dist_checkpointing_path=${MCORE_MODEL_PATH} + ++actor_rollout_ref.ref.megatron.override_transformer_config.use_flash_attn=True + +actor_rollout_ref.ref.megatron.override_transformer_config.sequence_parallel=True +) + +# Rollout Configuration +ROLLOUT_ARGS=( + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.max_num_seqs=16 + actor_rollout_ref.rollout.top_p=1.0 + actor_rollout_ref.rollout.top_k=-1 + actor_rollout_ref.rollout.temperature=1.0 + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + actor_rollout_ref.rollout.gpu_memory_utilization=${gpu_memory_utilization} + actor_rollout_ref.rollout.max_num_batched_tokens=${max_num_batched_tokens} + actor_rollout_ref.rollout.max_model_len=${max_model_len} + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} + actor_rollout_ref.rollout.data_parallel_size=${gen_dp} + actor_rollout_ref.rollout.expert_parallel_size=${gen_ep} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.enable_prefix_caching=True + actor_rollout_ref.rollout.enforce_eager=True + actor_rollout_ref.rollout.free_cache_engine=True +) + +# Trainer Configuration +TRAINER_ARGS=( + trainer.logger='["console","tensorboard"]' + trainer.project_name="${project_name}" + trainer.experiment_name="${exp_name}" + trainer.nnodes="${NNODES}" + trainer.n_gpus_per_node="${NPUS_PER_NODE}" + trainer.device='npu' + trainer.total_epochs=15 + trainer.val_before_train=False + trainer.test_freq=-1 + trainer.save_freq=-1 + trainer.default_local_dir="${CKPTS_DIR}" +) + +python3 -m verl.trainer.main_ppo \ + --config-path=config \ + --config-name='ppo_megatron_trainer.yaml' \ + "${DATA_ARGS[@]}" \ + "${MODEL_ARGS[@]}" \ + "${ACTOR_ARGS[@]}" \ + "${REF_ARGS[@]}" \ + "${ROLLOUT_ARGS[@]}" \ + "${ALGORITHM_ARGS[@]}" \ + "${TRAINER_ARGS[@]}" \ + "$@" | tee logs/run_qwen3moe-wy_235b_grpo_megatron_vllm_npu_$(date +%Y%m%d_%H%M%S).log \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_megatron.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_megatron.sh new file mode 100644 index 0000000000000000000000000000000000000000..6ec4d5a2848665e6224f19acb7e512ac4cf2ce1d --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_megatron.sh @@ -0,0 +1,236 @@ +#!/bin/bash +set -xeuo pipefail +# Project Configuration +project_name='DAPO-Qwen3-30b-A3B-BASE-MATH' +exp_name='DAPO-Qwen3-30B-A3B-BASE-Megatron-SGLang' + +# Necessary env +export HCCL_CONNECT_TIMEOUT=1500 +export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 +export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 +export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + +export DISABLE_L2_CACHE=1 +export TASK_QUEUE_ENABLE=1 + +# Node Info +NNODES=${NNODES:-1} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +# Model Weights Paths +MODEL_PATH=Qwen/Qwen3-30B-A3B +MCORE_MODEL_PATH=Qwen/Qwen3-30B-A3B-mcore +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + +# File System Paths +TRAIN_FILE=$RAY_DATA_HOME/dataset/dapo-math-17k.parquet +TEST_FILE=$RAY_DATA_HOME/dataset/aime-2024.parquet +# Data Length Configuration +max_prompt_length=$((1024 * 2)) +max_response_length=$((1024 * 8)) + +# Training Batch Configuration +train_prompt_bsz=16 +train_prompt_mini_bsz=16 +n_resp_per_prompt=8 + +# Algorithm Configuration +adv_estimator=grpo +use_kl_in_reward=False +kl_coef=0.0 +use_kl_loss=True +kl_loss_coef=0.001 + +# Performance and Memory Management Configuration +all_offload=True +use_dynamic_bsz=False +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length))) +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length))) + +# Megatron Parallelism Configuration +train_tp=4 +train_ep=4 +train_etp=4 +train_pp=1 +train_cp=1 + +# SGLang Generation Configuration +gen_tp=4 +gen_dp=1 +gen_ep=1 +gpu_memory_utilization=0.5 +max_model_len=$((max_prompt_length + max_response_length)) +max_num_batched_tokens=$(((max_prompt_length + max_response_length) * 1)) + +# Data Configuration +DATA_CONFIG=( + # File Paths + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + # Data Structure + data.prompt_key=prompt + # Batch and Length Configuration + data.train_batch_size=${train_prompt_bsz} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + # Preprocessing + data.filter_overlong_prompts=False + data.truncation='left' +) + +# Model Configuration +MODEL_CONFIG=( + # Model Path + actor_rollout_ref.model.path="${MODEL_PATH}" + # Model Processing + actor_rollout_ref.model.use_remove_padding=True +) + +# Reinforcement Learning Algorithm Configuration +ALGORITHM_CONFIG=( + # Advantage Estimation + algorithm.adv_estimator=${adv_estimator} + # KL Divergence Control + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_ctrl.kl_coef=${kl_coef} +) + +ACTOR_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} + # Loss Function Configuration + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.entropy_coeff=0 + # PPO Training Parameters + actor_rollout_ref.actor.ppo_epochs=1 + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} + actor_rollout_ref.actor.ppo_mini_batch_size=${train_prompt_mini_bsz} + # Optimizer Settings + actor_rollout_ref.actor.optim.lr=1e-6 + # Megatron Parallelism Strategy + actor_rollout_ref.actor.megatron.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.actor.megatron.context_parallel_size=${train_cp} + actor_rollout_ref.actor.megatron.expert_model_parallel_size=${train_ep} + actor_rollout_ref.actor.megatron.expert_tensor_parallel_size=${train_etp} + # Memory Optimization + actor_rollout_ref.actor.megatron.param_offload=${all_offload} + actor_rollout_ref.actor.megatron.optimizer_offload=${all_offload} + actor_rollout_ref.actor.megatron.grad_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.actor.megatron.dist_checkpointing_path=${MCORE_MODEL_PATH} + actor_rollout_ref.actor.megatron.use_dist_checkpointing=True + actor_rollout_ref.actor.megatron.use_mbridge=True + # Transformer Architecture Optimizations + +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method=uniform + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity=full + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers=1 +) + +REF_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.ref.use_torch_compile=False + # Log Probability Inference + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Megatron Parallelism Strategy + actor_rollout_ref.ref.megatron.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.ref.megatron.context_parallel_size=${train_cp} + actor_rollout_ref.ref.megatron.expert_model_parallel_size=${train_ep} + actor_rollout_ref.ref.megatron.expert_tensor_parallel_size=${train_etp} + # Memory Optimization + actor_rollout_ref.ref.megatron.param_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.ref.megatron.dist_checkpointing_path=${MCORE_MODEL_PATH} + actor_rollout_ref.ref.megatron.use_dist_checkpointing=True + actor_rollout_ref.ref.megatron.use_mbridge=True +) + +ROLLOUT_CONFIG=( + # Rollout Engine + actor_rollout_ref.rollout.name=sglang + +actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend="ascend" + # Generation Parameters + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.top_p=1.0 + actor_rollout_ref.rollout.top_k=-1 + actor_rollout_ref.rollout.temperature=1.0 + # Log Probability Inference + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Memory Management + actor_rollout_ref.rollout.gpu_memory_utilization=${gpu_memory_utilization} + # Parallelism Strategy + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} + actor_rollout_ref.rollout.data_parallel_size=${gen_dp} + actor_rollout_ref.rollout.expert_parallel_size=${gen_ep} + +actor_rollout_ref.rollout.engine_kwargs.sglang.enable_dp_attention=False + # Performance Optimization + +actor_rollout_ref.rollout.engine_kwargs.sglang.chunked_prefill_size=-1 + actor_rollout_ref.rollout.enforce_eager=False + # Validation Generation + actor_rollout_ref.rollout.val_kwargs.n=1 + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.top_p=1.0 + actor_rollout_ref.rollout.val_kwargs.top_k=-1 + actor_rollout_ref.rollout.val_kwargs.temperature=1.0 +) + +TRAINER_CONFIG=( + # Logger Configuration + trainer.logger='["console"]' + # Project Settings + trainer.project_name="${project_name}" + trainer.experiment_name="${exp_name}" + # Hardware Configuration + trainer.nnodes="${NNODES}" + trainer.n_gpus_per_node="${NPUS_PER_NODE}" + trainer.device='npu' + # Training Schedule + trainer.total_epochs=15 + trainer.val_before_train=False + trainer.test_freq=-1 + trainer.save_freq=-1 + # Checkpoint Directory + trainer.default_local_dir="${CKPTS_DIR}" +) + +# profiling configuration +PROF_CONFIG=( + global_profiler.tool=npu + global_profiler.steps=null + global_profiler.save_path=/profpath + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.ranks="[0]" + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True + actor_rollout_ref.actor.profiler.tool_config.npu.contents=['npu','cpu'] + actor_rollout_ref.actor.profiler.tool_config.npu.level=level0 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=True + actor_rollout_ref.rollout.profiler.enable=True + actor_rollout_ref.rollout.profiler.ranks="[0]" + actor_rollout_ref.rollout.profiler.all_ranks=False +) + +python3 -m verl.trainer.main_ppo \ + --config-path=config \ + --config-name='ppo_megatron_trainer.yaml' \ + "${DATA_CONFIG[@]}" \ + "${MODEL_CONFIG[@]}" \ + "${ACTOR_CONFIG[@]}" \ + "${REF_CONFIG[@]}" \ + "${ROLLOUT_CONFIG[@]}" \ + "${ALGORITHM_CONFIG[@]}" \ + "${TRAINER_CONFIG[@]}" \ + "${PROF_CONFIG[@]}" \ + "$@" \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_mindspeed.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_mindspeed.sh new file mode 100644 index 0000000000000000000000000000000000000000..19418e1a2a3a889a8da5f7b22af217ac8720eaa4 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_30b_a3b_mindspeed.sh @@ -0,0 +1,248 @@ +#!/bin/bash +set -xeuo pipefail +# Project Configuration +project_name='GRPO-Qwen3-30b-A3B-BASE-MATH' +exp_name='GRPO-Qwen3-30B-A3B-BASE-MindSpeedLLM-SGLang' + +# Necessary env +export HCCL_CONNECT_TIMEOUT=1500 +export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 +export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 +export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + +export DISABLE_L2_CACHE=1 +export TASK_QUEUE_ENABLE=1 + +#For CANN versions 8.5.0 and above, using mbridge, set this ENV +export HCCL_OP_EXPANSION_MODE="AIV" + +# Node Info +NNODES=${NNODES:-1} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +# Model Weights Paths +MODEL_PATH=Qwen/Qwen3-30B-A3B +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + +# File System Paths +TRAIN_FILE=$RAY_DATA_HOME/gsm8k/train.parquet +TEST_FILE=$RAY_DATA_HOME/gsm8k/test.parquet +# Data Length Configuration +max_prompt_length=$((1024 * 2)) +max_response_length=$((1024 * 2)) + +# Training Batch Configuration +train_prompt_bsz=16 +train_prompt_mini_bsz=16 +n_resp_per_prompt=8 +micro_batch_size=1 + +# Algorithm Configuration +adv_estimator=grpo +use_kl_in_reward=False +kl_coef=0.0 +use_kl_loss=True +kl_loss_coef=0.001 + +# Performance and Memory Management Configuration +all_offload=True +use_dynamic_bsz=False +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length))) +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length))) + +# Megatron Parallelism Configuration +train_tp=4 +train_ep=4 +train_etp=1 +train_pp=4 +train_cp=1 + +# SGLang Generation Configuration +gen_tp=4 +gen_dp=1 +gen_ep=1 +gpu_memory_utilization=0.5 +max_model_len=$((max_prompt_length + max_response_length)) +max_num_batched_tokens=$(((max_prompt_length + max_response_length) * 1)) + +# Data Configuration +DATA_CONFIG=( + # File Paths + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + # Data Structure + data.prompt_key=prompt + # Batch and Length Configuration + data.train_batch_size=${train_prompt_bsz} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + # Preprocessing + data.filter_overlong_prompts=False + data.truncation='left' +) + +# Model Configuration +MODEL_CONFIG=( + # Model Path + actor_rollout_ref.model.path="${MODEL_PATH}" + # Model Processing + actor_rollout_ref.model.use_remove_padding=True +) + +# Reinforcement Learning Algorithm Configuration +ALGORITHM_CONFIG=( + # Advantage Estimation + algorithm.adv_estimator=${adv_estimator} + # KL Divergence Control + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_ctrl.kl_coef=${kl_coef} +) + +ACTOR_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} + # Loss Function Configuration + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.entropy_coeff=0 + # PPO Training Parameters + actor_rollout_ref.actor.ppo_epochs=1 + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} + actor_rollout_ref.actor.ppo_mini_batch_size=${train_prompt_mini_bsz} + # Optimizer Settings + actor_rollout_ref.actor.optim.lr=1e-6 + # Megatron Parallelism Strategy + actor_rollout_ref.actor.mindspeed.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.actor.mindspeed.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.actor.mindspeed.context_parallel_size=${train_cp} + actor_rollout_ref.actor.mindspeed.expert_model_parallel_size=${train_ep} + actor_rollout_ref.actor.mindspeed.expert_tensor_parallel_size=${train_etp} + # Memory Optimization + actor_rollout_ref.actor.mindspeed.param_offload=${all_offload} + actor_rollout_ref.actor.mindspeed.optimizer_offload=${all_offload} + actor_rollout_ref.actor.mindspeed.grad_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.actor.mindspeed.use_mbridge=True + actor_rollout_ref.actor.mindspeed.vanilla_mbridge=True + # Transformer Architecture Optimizations + actor_rollout_ref.actor.mindspeed.strategy=mindspeed_megatron + actor_rollout_ref.actor.mindspeed.mcore_kwargs.spec='[mindspeed_llm.tasks.models.spec.qwen3_spec, layer_spec]' + actor_rollout_ref.actor.mindspeed.mcore_kwargs.seq_length=${max_model_len} + actor_rollout_ref.actor.mindspeed.mcore_kwargs.micro_batch_size=${micro_batch_size} + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.num_query_groups=4 + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_method=uniform + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_granularity=full + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_num_layers=1 + # MOE + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.moe_router_load_balancing_type=aux_loss + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.moe_permutation_async_comm=True + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.moe_token_dispatcher_type=alltoall + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.moe_aux_loss_coeff=0.001 + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.moe_grouped_gemm=True +) + +REF_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.ref.use_torch_compile=False + # Log Probability Inference + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Megatron Parallelism Strategy + actor_rollout_ref.ref.mindspeed.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.ref.mindspeed.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.ref.mindspeed.context_parallel_size=${train_cp} + actor_rollout_ref.ref.mindspeed.expert_model_parallel_size=${train_ep} + actor_rollout_ref.ref.mindspeed.expert_tensor_parallel_size=${train_etp} + # Memory Optimization + actor_rollout_ref.ref.mindspeed.param_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.ref.mindspeed.use_mbridge=True + actor_rollout_ref.ref.mindspeed.vanilla_mbridge=True +) + +ROLLOUT_CONFIG=( + # Rollout Engine + actor_rollout_ref.rollout.name=sglang + +actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend="ascend" + # Generation Parameters + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.top_p=1.0 + actor_rollout_ref.rollout.top_k=-1 + actor_rollout_ref.rollout.temperature=1.0 + # Log Probability Inference + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Memory Management + actor_rollout_ref.rollout.gpu_memory_utilization=${gpu_memory_utilization} + # Parallelism Strategy + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} + actor_rollout_ref.rollout.data_parallel_size=${gen_dp} + actor_rollout_ref.rollout.expert_parallel_size=${gen_ep} + +actor_rollout_ref.rollout.engine_kwargs.sglang.enable_dp_attention=False + # Performance Optimization + +actor_rollout_ref.rollout.engine_kwargs.sglang.chunked_prefill_size=-1 + actor_rollout_ref.rollout.enforce_eager=False + # Validation Generation + actor_rollout_ref.rollout.val_kwargs.n=1 + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.top_p=1.0 + actor_rollout_ref.rollout.val_kwargs.top_k=-1 + actor_rollout_ref.rollout.val_kwargs.temperature=1.0 +) + +TRAINER_CONFIG=( + # Logger Configuration + trainer.logger='["console"]' + # Project Settings + trainer.project_name="${project_name}" + trainer.experiment_name="${exp_name}" + # Hardware Configuration + trainer.nnodes="${NNODES}" + trainer.n_gpus_per_node="${NPUS_PER_NODE}" + trainer.device='npu' + # Training Schedule + trainer.total_epochs=15 + trainer.val_before_train=False + trainer.test_freq=-1 + trainer.save_freq=-1 + # Checkpoint Directory + trainer.default_local_dir="${CKPTS_DIR}" +) + +# profiling configuration +PROF_CONFIG=( + global_profiler.tool=npu + global_profiler.steps=null + global_profiler.save_path=/profpath + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.ranks="[0]" + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True + actor_rollout_ref.actor.profiler.tool_config.npu.contents=['npu','cpu'] + actor_rollout_ref.actor.profiler.tool_config.npu.level=level0 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=True + actor_rollout_ref.rollout.profiler.enable=True + actor_rollout_ref.rollout.profiler.ranks="[0]" + actor_rollout_ref.rollout.profiler.all_ranks=False +) + +python3 -m verl.trainer.main_ppo \ + --config-path=config \ + --config-name='ppo_trainer.yaml' \ + model_engine=mindspeed \ + "${DATA_CONFIG[@]}" \ + "${MODEL_CONFIG[@]}" \ + "${ACTOR_CONFIG[@]}" \ + "${REF_CONFIG[@]}" \ + "${ROLLOUT_CONFIG[@]}" \ + "${ALGORITHM_CONFIG[@]}" \ + "${TRAINER_CONFIG[@]}" \ + "${PROF_CONFIG[@]}" \ + "$@" \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_fsdp.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_fsdp.sh new file mode 100644 index 0000000000000000000000000000000000000000..6384a564a6afd19dcd690cea86f8104306096412 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_fsdp.sh @@ -0,0 +1,59 @@ +set -x + +project_name='GRPO-Qwen3' +exp_name='GRPO-Qwen3-32b-npu' +gen_tp=4 +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +MODEL_PATH=${MODEL_PATH:-"${RAY_DATA_HOME}/models/Qwen3-32B"} +TRAIN_FILE=${TRAIN_FILE:-"${RAY_DATA_HOME}/data/gsm8k/train.parquet"} +TEST_FILE=${TEST_FILE:-"${RAY_DATA_HOME}/data/gsm8k/test.parquet"} + +python3 -m verl.trainer.main_ppo \ + algorithm.adv_estimator=grpo \ + data.train_files="${TRAIN_FILE}" \ + data.val_files="${TEST_FILE}" \ + data.train_batch_size=1024 \ + data.max_prompt_length=2048 \ + data.max_response_length=2048 \ + data.filter_overlong_prompts=True \ + data.truncation='error' \ + data.shuffle=False \ + actor_rollout_ref.model.path=${MODEL_PATH} \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.actor.ulysses_sequence_parallel_size=4 \ + +actor_rollout_ref.actor.fsdp_config.mixed_precision.param_dtype=bf16 \ + +actor_rollout_ref.actor.fsdp_config.mixed_precision.reduce_dtype=bf16 \ + +actor_rollout_ref.actor.fsdp_config.mixed_precision.buffer_dtype=fp32 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.actor.use_kl_loss=True \ + actor_rollout_ref.actor.entropy_coeff=0 \ + actor_rollout_ref.actor.kl_loss_coef=0.001 \ + actor_rollout_ref.actor.kl_loss_type=low_var_kl \ + 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=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.7 \ + actor_rollout_ref.rollout.n=4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=8 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + actor_rollout_ref.actor.use_torch_compile=False \ + actor_rollout_ref.ref.use_torch_compile=False \ + actor_rollout_ref.rollout.enable_chunked_prefill=True \ + actor_rollout_ref.rollout.max_num_batched_tokens=32768 \ + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=4096 \ + algorithm.use_kl_in_reward=False \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tensorboard'] \ + trainer.project_name="${project_name}" \ + trainer.experiment_name="${exp_name}" \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=4 \ + trainer.resume_from_path=checkpoints/ \ + trainer.save_freq=500 \ + trainer.test_freq=50 \ + trainer.total_epochs=50 $@ \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_mindspeed.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_mindspeed.sh new file mode 100644 index 0000000000000000000000000000000000000000..a387648cd680c813fe4d85eb5f7fea5dd6fc27df --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_32b_mindspeed.sh @@ -0,0 +1,236 @@ +#!/bin/bash +set -xeuo pipefail +# Project Configuration +project_name='GRPO-Qwen3-32B-BASE-MATH' +exp_name='GRPO-Qwen3-32B-BASE-MindSpeedLLM-SGLang' + +# Necessary env +export HCCL_CONNECT_TIMEOUT=1500 +export HCCL_HOST_SOCKET_PORT_RANGE=60000-60050 +export HCCL_NPU_SOCKET_PORT_RANGE=61000-61050 + +export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1 +export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 + +export DISABLE_L2_CACHE=1 +export TASK_QUEUE_ENABLE=1 + +#For CANN versions 8.5.0 and above, using mbridge, set this ENV +export HCCL_OP_EXPANSION_MODE="AIV" + +# Node Info +NNODES=${NNODES:-1} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +# Model Weights Paths +MODEL_PATH=Qwen/Qwen3-32B +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${project_name}/${exp_name}"} + +# File System Paths +TRAIN_FILE=$RAY_DATA_HOME/gsm8k/train.parquet +TEST_FILE=$RAY_DATA_HOME/gsm8k/test.parquet +# Data Length Configuration +max_prompt_length=$((1024 * 2)) +max_response_length=$((1024 * 2)) + +# Training Batch Configuration +train_prompt_bsz=16 +train_prompt_mini_bsz=16 +n_resp_per_prompt=8 +micro_batch_size=1 + +# Algorithm Configuration +adv_estimator=grpo +use_kl_in_reward=False +kl_coef=0.0 +use_kl_loss=True +kl_loss_coef=0.001 + +# Performance and Memory Management Configuration +all_offload=True +use_dynamic_bsz=False +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length))) +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length))) + +# Megatron Parallelism Configuration +train_tp=4 +train_pp=4 +train_cp=1 + +# SGLang Generation Configuration +gen_tp=4 +gen_dp=1 +gpu_memory_utilization=0.5 +max_model_len=$((max_prompt_length + max_response_length)) +max_num_batched_tokens=$(((max_prompt_length + max_response_length) * 1)) + +# Data Configuration +DATA_CONFIG=( + # File Paths + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + # Data Structure + data.prompt_key=prompt + # Batch and Length Configuration + data.train_batch_size=${train_prompt_bsz} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + # Preprocessing + data.filter_overlong_prompts=False + data.truncation='left' +) + +# Model Configuration +MODEL_CONFIG=( + # Model Path + actor_rollout_ref.model.path="${MODEL_PATH}" + # Model Processing + actor_rollout_ref.model.use_remove_padding=True +) + +# Reinforcement Learning Algorithm Configuration +ALGORITHM_CONFIG=( + # Advantage Estimation + algorithm.adv_estimator=${adv_estimator} + # KL Divergence Control + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_ctrl.kl_coef=${kl_coef} +) + +ACTOR_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} + # Loss Function Configuration + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.entropy_coeff=0 + # PPO Training Parameters + actor_rollout_ref.actor.ppo_epochs=1 + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} + actor_rollout_ref.actor.ppo_mini_batch_size=${train_prompt_mini_bsz} + # Optimizer Settings + actor_rollout_ref.actor.optim.lr=1e-6 + # Megatron Parallelism Strategy + actor_rollout_ref.actor.mindspeed.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.actor.mindspeed.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.actor.mindspeed.context_parallel_size=${train_cp} + # Memory Optimization + actor_rollout_ref.actor.mindspeed.param_offload=${all_offload} + actor_rollout_ref.actor.mindspeed.optimizer_offload=${all_offload} + actor_rollout_ref.actor.mindspeed.grad_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.actor.mindspeed.use_mbridge=True + actor_rollout_ref.actor.mindspeed.vanilla_mbridge=True + # Transformer Architecture Optimizations + actor_rollout_ref.actor.mindspeed.strategy=mindspeed_megatron + actor_rollout_ref.actor.mindspeed.mcore_kwargs.spec='[mindspeed_llm.tasks.models.spec.qwen3_spec, layer_spec]' + actor_rollout_ref.actor.mindspeed.mcore_kwargs.seq_length=${max_model_len} + actor_rollout_ref.actor.mindspeed.mcore_kwargs.micro_batch_size=${micro_batch_size} + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.num_query_groups=8 + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_method=uniform + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_granularity=full + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.recompute_num_layers=1 + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.overlap_grad_reduce=True + +actor_rollout_ref.actor.mindspeed.mcore_kwargs.overlap_param_gather=True +) + +REF_CONFIG=( + # Core Runtime Settings + actor_rollout_ref.ref.use_torch_compile=False + # Log Probability Inference + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Megatron Parallelism Strategy + actor_rollout_ref.ref.mindspeed.tensor_model_parallel_size=${train_tp} + actor_rollout_ref.ref.mindspeed.pipeline_model_parallel_size=${train_pp} + actor_rollout_ref.ref.mindspeed.context_parallel_size=${train_cp} + # Memory Optimization + actor_rollout_ref.ref.mindspeed.param_offload=${all_offload} + # Model Weights Management + actor_rollout_ref.ref.mindspeed.use_mbridge=True + actor_rollout_ref.ref.mindspeed.vanilla_mbridge=True +) + +ROLLOUT_CONFIG=( + # Rollout Engine + actor_rollout_ref.rollout.name=sglang + +actor_rollout_ref.rollout.engine_kwargs.sglang.attention_backend="ascend" + # Generation Parameters + actor_rollout_ref.rollout.n=${n_resp_per_prompt} + actor_rollout_ref.rollout.top_p=1.0 + actor_rollout_ref.rollout.top_k=-1 + actor_rollout_ref.rollout.temperature=1.0 + # Log Probability Inference + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=${micro_batch_size} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} + # Memory Management + actor_rollout_ref.rollout.gpu_memory_utilization=${gpu_memory_utilization} + # Parallelism Strategy + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} + actor_rollout_ref.rollout.data_parallel_size=${gen_dp} + +actor_rollout_ref.rollout.engine_kwargs.sglang.enable_dp_attention=False + # Performance Optimization + +actor_rollout_ref.rollout.engine_kwargs.sglang.chunked_prefill_size=-1 + actor_rollout_ref.rollout.enforce_eager=False + # Validation Generation + actor_rollout_ref.rollout.val_kwargs.n=1 + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.top_p=1.0 + actor_rollout_ref.rollout.val_kwargs.top_k=-1 + actor_rollout_ref.rollout.val_kwargs.temperature=1.0 +) + +TRAINER_CONFIG=( + # Logger Configuration + trainer.logger='["console"]' + # Project Settings + trainer.project_name="${project_name}" + trainer.experiment_name="${exp_name}" + # Hardware Configuration + trainer.nnodes="${NNODES}" + trainer.n_gpus_per_node="${NPUS_PER_NODE}" + trainer.device='npu' + # Training Schedule + trainer.total_epochs=15 + trainer.val_before_train=False + trainer.test_freq=-1 + trainer.save_freq=-1 + # Checkpoint Directory + trainer.default_local_dir="${CKPTS_DIR}" +) + +# profiling configuration +PROF_CONFIG=( + global_profiler.tool=npu + global_profiler.steps=null + global_profiler.save_path=/profpath + actor_rollout_ref.actor.profiler.enable=True + actor_rollout_ref.actor.profiler.ranks="[0]" + actor_rollout_ref.actor.profiler.all_ranks=False + actor_rollout_ref.actor.profiler.tool_config.npu.discrete=True + actor_rollout_ref.actor.profiler.tool_config.npu.contents=['npu','cpu'] + actor_rollout_ref.actor.profiler.tool_config.npu.level=level0 + actor_rollout_ref.actor.profiler.tool_config.npu.analysis=True + actor_rollout_ref.rollout.profiler.enable=True + actor_rollout_ref.rollout.profiler.ranks="[0]" + actor_rollout_ref.rollout.profiler.all_ranks=False +) + +python3 -m verl.trainer.main_ppo \ + --config-path=config \ + --config-name='ppo_trainer.yaml' \ + model_engine=mindspeed \ + "${DATA_CONFIG[@]}" \ + "${MODEL_CONFIG[@]}" \ + "${ACTOR_CONFIG[@]}" \ + "${REF_CONFIG[@]}" \ + "${ROLLOUT_CONFIG[@]}" \ + "${ALGORITHM_CONFIG[@]}" \ + "${TRAINER_CONFIG[@]}" \ + "${PROF_CONFIG[@]}" \ + "$@" \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_5_122b_a10b_32k_megatron.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_5_122b_a10b_32k_megatron.sh new file mode 100644 index 0000000000000000000000000000000000000000..817db30a76167b39a3f893f93232065d3e4fa975 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_5_122b_a10b_32k_megatron.sh @@ -0,0 +1,174 @@ +#!/usr/bin/env bash +set -xeuo pipefail + +########################### Environment ########################### + +export VLLM_USE_V1=${VLLM_USE_V1:-1} +export VLLM_ALLREDUCE_USE_SYMM_MEM=${VLLM_ALLREDUCE_USE_SYMM_MEM:-0} +export VLLM_ASCEND_ENABLE_PREFETCH_MLP=${VLLM_ASCEND_ENABLE_PREFETCH_MLP:-1} +export VLLM_ASCEND_ENABLE_TOPK_OPTIMIZE=${VLLM_ASCEND_ENABLE_TOPK_OPTIMIZE:-1} +export VLLM_ASCEND_ENABLE_FLASHCOMM1=${VLLM_ASCEND_ENABLE_FLASHCOMM1:-1} +export CPU_AFFINITY_CONF=${CPU_AFFINITY_CONF:-1} + +########################### Quick Config ########################### + +# Node Info +NNODES=${NNODES:-4} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +# ---- user-adjustable ---- + +project_name=${project_name:-verl_grpo_qwen3_5_122b_geo3k} +exp_name=${exp_name:-qwen3_5_122b_megatron_npu_4k_32k} +adv_estimator=${adv_estimator:-grpo} +rollout_name=${rollout_name:-vllm} + +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +HF_MODEL_PATH=${HF_MODEL_PATH:-"${RAY_DATA_HOME}/models/Qwen3.5-122B-A10B"} +train_path=${train_path:-"${RAY_DATA_HOME}/datasets/geo3k/train.parquet"} +test_path=${test_path:-"${RAY_DATA_HOME}/datasets/geo3k/test.parquet"} + +TP=${TP:-2} +PP=${PP:-4} +CP=${CP:-4} +EP=${EP:-16} +ETP=${ETP:-1} +GEN_TP=${GEN_TP:-16} + +ALL_OFFLOAD=${ALL_OFFLOAD:-True} + +# ---- end user-adjustable ---- + +########################### Parameter Arrays ########################### + +DATA=( + data.train_files=${train_path} + data.val_files=${test_path} + data.train_batch_size=16 + data.max_prompt_length=$((1024 * 4)) + data.max_response_length=$((1024 * 32)) + data.truncation='error' + data.filter_overlong_prompts=True +) + +MODEL=( + actor_rollout_ref.model.path=${HF_MODEL_PATH} + actor_rollout_ref.model.trust_remote_code=True + actor_rollout_ref.model.use_remove_padding=False +) + +ALGORITHM=( + algorithm.adv_estimator=${adv_estimator} + algorithm.use_kl_in_reward=False +) + +ACTOR=( + actor_rollout_ref.actor.use_dynamic_bsz=False + actor_rollout_ref.actor.use_kl_loss=True + actor_rollout_ref.actor.kl_loss_coef=0.01 + actor_rollout_ref.actor.kl_loss_type=low_var_kl + actor_rollout_ref.actor.entropy_coeff=0 + actor_rollout_ref.actor.ppo_mini_batch_size=16 + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 + + actor_rollout_ref.actor.optim.lr=1e-6 + +actor_rollout_ref.actor.optim.override_optimizer_config.overlap_cpu_optimizer_d2h_h2d=True + + actor_rollout_ref.actor.checkpoint.strict=False + actor_rollout_ref.actor.checkpoint.save_contents="['model']" + + actor_rollout_ref.actor.megatron.use_mbridge=True + actor_rollout_ref.actor.megatron.vanilla_mbridge=False + actor_rollout_ref.actor.megatron.use_remove_padding=False + actor_rollout_ref.actor.megatron.tensor_model_parallel_size=${TP} + actor_rollout_ref.actor.megatron.pipeline_model_parallel_size=${PP} + actor_rollout_ref.actor.megatron.context_parallel_size=${CP} + actor_rollout_ref.actor.megatron.expert_model_parallel_size=${EP} + actor_rollout_ref.actor.megatron.expert_tensor_parallel_size=${ETP} + actor_rollout_ref.actor.megatron.param_offload=${ALL_OFFLOAD} + actor_rollout_ref.actor.megatron.optimizer_offload=${ALL_OFFLOAD} + actor_rollout_ref.actor.megatron.grad_offload=${ALL_OFFLOAD} + actor_rollout_ref.actor.megatron.dtype=bfloat16 + + ++actor_rollout_ref.actor.megatron.override_transformer_config.attention_backend=auto + +actor_rollout_ref.actor.megatron.override_transformer_config.context_parallel_algo=kvallgather_cp_algo + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_method=uniform + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_granularity=full + +actor_rollout_ref.actor.megatron.override_transformer_config.recompute_num_layers=1 + +actor_rollout_ref.actor.megatron.override_transformer_config.use_flash_attn=True + +actor_rollout_ref.actor.megatron.override_transformer_config.sequence_parallel=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_rmsnorm=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_fused_swiglu=True + +actor_rollout_ref.actor.megatron.override_transformer_config.use_naive_l2norm=True + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_aux_loss_coeff=0.01 + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_z_loss_coeff=0.001 + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_permute_fusion=True + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_grouped_gemm=True + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_token_dispatcher_type=alltoall + +actor_rollout_ref.actor.megatron.override_transformer_config.moe_router_dtype=fp32 +) + +REF=( + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=2 + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=False + actor_rollout_ref.ref.megatron.tensor_model_parallel_size=${TP} + actor_rollout_ref.ref.megatron.pipeline_model_parallel_size=${PP} + actor_rollout_ref.ref.megatron.context_parallel_size=${CP} + actor_rollout_ref.ref.megatron.expert_model_parallel_size=${EP} + actor_rollout_ref.ref.megatron.expert_tensor_parallel_size=${ETP} + actor_rollout_ref.ref.megatron.param_offload=${ALL_OFFLOAD} +) + +ROLLOUT=( + actor_rollout_ref.rollout.name=${rollout_name} + actor_rollout_ref.rollout.tensor_model_parallel_size=${GEN_TP} + actor_rollout_ref.rollout.expert_parallel_size=${EP} + actor_rollout_ref.rollout.gpu_memory_utilization=0.6 + actor_rollout_ref.rollout.n=5 + actor_rollout_ref.rollout.dtype=bfloat16 + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=2 + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=False + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.ignore_eos=False + actor_rollout_ref.rollout.enforce_eager=False + + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=4096 + + +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_mode="FULL_DECODE_ONLY" + +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_capture_sizes="[4,12,24,48,64]" + ++actor_rollout_ref.rollout.engine_kwargs.vllm.additional_config.enable_cpu_binding=True + ++actor_rollout_ref.rollout.engine_kwargs.vllm.async_scheduling=True +) + +TRAINER=( + trainer.critic_warmup=0 + trainer.logger='["console"]' + trainer.project_name=${project_name} + trainer.experiment_name=${exp_name} + trainer.n_gpus_per_node=${NPUS_PER_NODE} + trainer.nnodes="${NNODES}" + trainer.save_freq=15 + trainer.val_before_train=False + trainer.test_freq=-1 + trainer.total_training_steps=100 + trainer.total_epochs=15 +) + +EXTRA=( + model_engine=megatron +) + +########################### Launch ########################### + +mkdir -p logs + +python3 -m verl.trainer.main_ppo \ + "${DATA[@]}" \ + "${ALGORITHM[@]}" \ + "${MODEL[@]}" \ + "${ROLLOUT[@]}" \ + "${ACTOR[@]}" \ + "${REF[@]}" \ + "${TRAINER[@]}" \ + "${EXTRA[@]}" \ + "$@" diff --git a/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_next_80b_fsdp.sh b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_next_80b_fsdp.sh new file mode 100644 index 0000000000000000000000000000000000000000..c106696652e7dba11aa57cdcb3e5ed2dea1f7251 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/grpo_trainer/run_qwen3_next_80b_fsdp.sh @@ -0,0 +1,175 @@ +#!/usr/bin/env bash +set -xeuo pipefail + +project_name="verl_grpo_qwen3-next-80b" +experiment_name="Qwen3_Next_80B_Instruct" + +# Paths +WORK_DIR=${WORK_DIR:-"${HOME}/verl"} +MODEL_PATH=${WORK_DIR}/Qwen3-Next-80B-A3B-Instruct +TRAIN_FILE=${WORK_DIR}/datasets/dapo-math-17k/dapo-math-17k.parquet +TEST_FILE=${WORK_DIR}/datasets/aime/aime-2024.parquet + +# algorithm +adv_estimator=grpo + +use_kl_in_reward=False +kl_coef=0.0 +use_kl_loss=True +kl_loss_coef=0.001 + +clip_ratio_low=0.2 +clip_ratio_high=0.28 + +temperature=1.0 +top_p=1.0 +top_k=-1 # 0 for HF rollout, -1 for vLLM rollout +val_top_p=0.7 + +# batch +train_batch_size=16 +rollout_n=16 +ppo_mini_batch_size=8 + +# length +max_prompt_length=$((1024 * 2)) +max_response_length=$((1024 * 20)) + +# algorithm +learning_rate=1e-6 +warmup_steps=0 +# enable_filter_groups=True + +# performance +sp_size=8 +gen_tp=8 +use_dynamic_bsz=True +actor_ppo_max_token_len=$(((max_prompt_length + max_response_length) / sp_size)) +infer_ppo_max_token_len=$(((max_prompt_length + max_response_length) / sp_size)) +offload=True + +DATA=( + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.truncation='error' +) + +ACTOR=( + actor_rollout_ref.actor.strategy=fsdp2 + actor_rollout_ref.nccl_timeout=14400 + + # fsdp + actor_rollout_ref.actor.fsdp_config.use_orig_params=True + actor_rollout_ref.actor.fsdp_config.model_dtype=bfloat16 + actor_rollout_ref.actor.fsdp_config.param_offload=${offload} + actor_rollout_ref.actor.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.actor.fsdp_config.forward_prefetch=False + actor_rollout_ref.actor.fsdp_config.fsdp_size=-1 + +actor_rollout_ref.actor.fsdp_config.mixed_precision.reduce_dtype=bf16 + + # optimizer + actor_rollout_ref.actor.optim.lr=${learning_rate} + actor_rollout_ref.actor.optim.lr_warmup_steps=${warmup_steps} + + # ppo config + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${actor_ppo_max_token_len} + actor_rollout_ref.actor.ulysses_sequence_parallel_size=${sp_size} + + # entropy + actor_rollout_ref.actor.entropy_checkpointing=True + actor_rollout_ref.actor.entropy_from_logits_with_chunking=True + + actor_rollout_ref.actor.use_kl_loss=${use_kl_loss} + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=low_var_kl + actor_rollout_ref.actor.clip_ratio_low=${clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${clip_ratio_high} + actor_rollout_ref.actor.clip_ratio_c=10.0 + + actor_rollout_ref.actor.use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.actor.use_torch_compile=False +) + +ROLLOUT=( + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.n=${rollout_n} + actor_rollout_ref.rollout.tensor_model_parallel_size=${gen_tp} + actor_rollout_ref.rollout.gpu_memory_utilization=0.6 + actor_rollout_ref.rollout.load_format=auto + actor_rollout_ref.rollout.enforce_eager=True + actor_rollout_ref.rollout.max_num_batched_tokens=$((max_prompt_length + max_response_length)) + actor_rollout_ref.rollout.calculate_log_probs=True + + actor_rollout_ref.rollout.temperature=${temperature} + actor_rollout_ref.rollout.top_p=${top_p} + actor_rollout_ref.rollout.top_k=${top_k} + actor_rollout_ref.rollout.val_kwargs.temperature=${temperature} + actor_rollout_ref.rollout.val_kwargs.top_p=${val_top_p} + actor_rollout_ref.rollout.val_kwargs.top_k=${top_k} + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.n=1 + + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} +) + +REF=( + actor_rollout_ref.ref.ulysses_sequence_parallel_size=${sp_size} + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.ref.fsdp_config.param_offload=${offload} + actor_rollout_ref.ref.fsdp_config.optimizer_offload=${offload} + actor_rollout_ref.ref.fsdp_config.forward_prefetch=False + + actor_rollout_ref.ref.entropy_checkpointing=True + actor_rollout_ref.ref.entropy_from_logits_with_chunking=True + + actor_rollout_ref.ref.log_prob_micro_batch_size_per_gpu=1 + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=${use_dynamic_bsz} + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${infer_ppo_max_token_len} +) + +TRAINER=( + trainer.logger='["console"]' + trainer.project_name="${project_name}" + trainer.experiment_name="${experiment_name}" + trainer.n_gpus_per_node=16 + trainer.nnodes=4 + trainer.val_before_train=False + trainer.save_freq=5 + trainer.test_freq=-1 + trainer.total_epochs=1 + trainer.device=npu +) + +MODEL=( + actor_rollout_ref.model.path=${MODEL_PATH} + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_activation_offload=${offload} +) + +ALGORITHM=( + algorithm.adv_estimator=${adv_estimator} + algorithm.use_kl_in_reward=${use_kl_in_reward} + algorithm.kl_ctrl.kl_coef=${kl_coef} +) + +# ========================================================= +echo "Starting Training with:" +echo "Project: ${project_name}, Exp: ${experiment_name}" +echo "Rollout N: ${rollout_n}, Batch Size: ${train_batch_size}, LR: ${learning_rate}" + + +python3 -m verl.trainer.main_ppo \ + "${DATA[@]}" \ + "${ACTOR[@]}" \ + "${ROLLOUT[@]}" \ + "${REF[@]}" \ + "${TRAINER[@]}" \ + "${ALGORITHM[@]}" \ + "${MODEL[@]}" \ No newline at end of file diff --git a/verl_0720_main/verl/examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh b/verl_0720_main/verl/examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh new file mode 100644 index 0000000000000000000000000000000000000000..7782a56dcb708c94cc975fbb7edd84f1a9e4c41b --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/gspo_trainer/run_qwen3_32b_fsdp.sh @@ -0,0 +1,170 @@ +#!/usr/bin/env bash +# GSPO | Qwen3-32B | DAPO-Math-17k | vLLM rollout | FSDP2 training | Ascend NPU +# Reference: verl/docs/ascend_tutorial/model_support/examples/gspo_optimization_practice.md + +set -xeuo pipefail + +mkdir -p logs +ulimit -n 32768 + +########################### NPU environment ########################### +export RAY_DEDUP_LOGS=0 +export HYDRA_FULL_ERROR=1 +export TASK_QUEUE_ENABLE=1 +export HCCL_EXEC_TIMEOUT=3600 +export HCCL_CONNECT_TIMEOUT=3600 +export HCCL_ASYNC_ERROR_HANDLING=0 +export CPU_AFFINITY_CONF=1 +export VLLM_USE_V1=1 +export VLLM_ATTENTION_BACKEND=XFORMERS +export VLLM_ASCEND_ENABLE_FLASHCOMM=1 +export VLLM_ASCEND_ENABLE_PREFETCH_MLP=1 +export VLLM_ASCEND_ENABLE_DENSE_OPTIMIZE=1 +# Optional: enable jemalloc after installation (see gspo_optimization_practice.md) +# export LD_PRELOAD=/usr/local/lib/libjemalloc.so.2 + +########################### user-adjustable ########################### +PROJECT_NAME=${PROJECT_NAME:-GSPO-Qwen3-32B-BASE-MATH} +EXPERIMENT_NAME=${EXPERIMENT_NAME:-GSPO-Qwen3-32B-BASE-FSDP-vLLM} + +NNODES=${NNODES:-4} +NPUS_PER_NODE=${NPUS_PER_NODE:-16} + +RAY_DATA_HOME=${RAY_DATA_HOME:-"${HOME}/verl"} +MODEL_PATH=${MODEL_PATH:-Qwen/Qwen3-32B} +CKPTS_DIR=${CKPTS_DIR:-"${RAY_DATA_HOME}/ckpts/${PROJECT_NAME}/${EXPERIMENT_NAME}"} + +TRAIN_FILE=${TRAIN_FILE:-"${RAY_DATA_HOME}/dataset/dapo-math-17k.parquet"} +TEST_FILE=${TEST_FILE:-"${RAY_DATA_HOME}/dataset/aime-2024.parquet"} + +MAX_PROMPT_LENGTH=${MAX_PROMPT_LENGTH:-$((1024 * 2))} +MAX_RESPONSE_LENGTH=${MAX_RESPONSE_LENGTH:-$((1024 * 8))} + +TRAIN_BATCH_SIZE=${TRAIN_BATCH_SIZE:-256} +PPO_MINI_BATCH_SIZE=${PPO_MINI_BATCH_SIZE:-64} +ROLLOUT_N=${ROLLOUT_N:-16} + +SP_SIZE=${SP_SIZE:-4} +ROLLOUT_TP=${ROLLOUT_TP:-4} +ROLLOUT_GPU_MEM_UTIL=${ROLLOUT_GPU_MEM_UTIL:-0.7} + +CLIP_RATIO_LOW=${CLIP_RATIO_LOW:-3e-4} +CLIP_RATIO_HIGH=${CLIP_RATIO_HIGH:-4e-4} +ACTOR_LR=${ACTOR_LR:-1e-6} + +TOTAL_EPOCHS=${TOTAL_EPOCHS:-10} +SAVE_FREQ=${SAVE_FREQ:--1} +TEST_FREQ=${TEST_FREQ:--1} +########################### end user-adjustable ########################### + +seq_len=$((MAX_PROMPT_LENGTH + MAX_RESPONSE_LENGTH)) +ppo_max_token_len_per_gpu=$((seq_len / SP_SIZE)) + +DATA=( + algorithm.adv_estimator=grpo + algorithm.use_kl_in_reward=False + data.train_files="${TRAIN_FILE}" + data.val_files="${TEST_FILE}" + data.prompt_key=prompt + data.train_batch_size=${TRAIN_BATCH_SIZE} + data.max_prompt_length=${MAX_PROMPT_LENGTH} + data.max_response_length=${MAX_RESPONSE_LENGTH} + data.truncation='left' +) + +MODEL=( + actor_rollout_ref.model.path="${MODEL_PATH}" + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True +) + +ACTOR=( + actor_rollout_ref.actor.strategy=fsdp2 + actor_rollout_ref.actor.use_torch_compile=False + actor_rollout_ref.actor.policy_loss.loss_mode=gspo + actor_rollout_ref.actor.loss_agg_mode=seq-mean-token-mean + actor_rollout_ref.actor.clip_ratio_low=${CLIP_RATIO_LOW} + actor_rollout_ref.actor.clip_ratio_high=${CLIP_RATIO_HIGH} + actor_rollout_ref.actor.clip_ratio_c=10.0 + actor_rollout_ref.actor.use_kl_loss=False + actor_rollout_ref.actor.kl_loss_coef=0.0 + actor_rollout_ref.actor.entropy_coeff=0 + actor_rollout_ref.actor.grad_clip=1.0 + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE} + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} + actor_rollout_ref.actor.optim.lr=${ACTOR_LR} + actor_rollout_ref.actor.optim.lr_warmup_steps=10 + actor_rollout_ref.actor.optim.weight_decay=0.1 + actor_rollout_ref.actor.fsdp_config.fsdp_size=-1 + actor_rollout_ref.actor.fsdp_config.ulysses_sequence_parallel_size=${SP_SIZE} + actor_rollout_ref.actor.fsdp_config.param_offload=True + actor_rollout_ref.actor.fsdp_config.optimizer_offload=True + actor_rollout_ref.actor.fsdp_config.reshard_after_forward=True + actor_rollout_ref.actor.fsdp_config.forward_prefetch=True + actor_rollout_ref.actor.entropy_checkpointing=True + actor_rollout_ref.actor.entropy_from_logits_with_chunking=True +) + +REF=( + actor_rollout_ref.ref.strategy=fsdp2 + actor_rollout_ref.ref.use_torch_compile=False + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} + actor_rollout_ref.ref.fsdp_config.ulysses_sequence_parallel_size=${SP_SIZE} + actor_rollout_ref.ref.fsdp_config.param_offload=True + actor_rollout_ref.ref.fsdp_config.reshard_after_forward=True + actor_rollout_ref.ref.entropy_from_logits_with_chunking=True +) + +ROLLOUT=( + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.calculate_log_probs=True + actor_rollout_ref.rollout.n=${ROLLOUT_N} + actor_rollout_ref.rollout.top_p=1.0 + actor_rollout_ref.rollout.top_k=-1 + actor_rollout_ref.rollout.temperature=1.0 + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} + actor_rollout_ref.rollout.gpu_memory_utilization=${ROLLOUT_GPU_MEM_UTIL} + actor_rollout_ref.rollout.max_num_batched_tokens=${seq_len} + actor_rollout_ref.rollout.tensor_model_parallel_size=${ROLLOUT_TP} + actor_rollout_ref.rollout.enable_chunked_prefill=True + actor_rollout_ref.rollout.enforce_eager=False + actor_rollout_ref.rollout.free_cache_engine=True + actor_rollout_ref.rollout.cudagraph_capture_sizes="[8, 16, 32, 64, 128, 192, 256]" + +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_mode="FULL_DECODE_ONLY" + actor_rollout_ref.rollout.val_kwargs.n=1 + actor_rollout_ref.rollout.val_kwargs.do_sample=True + actor_rollout_ref.rollout.val_kwargs.top_p=0.7 + actor_rollout_ref.rollout.val_kwargs.top_k=-1 + actor_rollout_ref.rollout.val_kwargs.temperature=1.0 + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=4096 +) + +TRAINER=( + trainer.logger='["console"]' + trainer.project_name="${PROJECT_NAME}" + trainer.experiment_name="${EXPERIMENT_NAME}" + trainer.nnodes="${NNODES}" + trainer.n_gpus_per_node="${NPUS_PER_NODE}" + trainer.device=npu + trainer.total_epochs=${TOTAL_EPOCHS} + trainer.val_before_train=False + trainer.test_freq=${TEST_FREQ} + trainer.save_freq=${SAVE_FREQ} + trainer.default_local_dir="${CKPTS_DIR}" + trainer.resume_mode=auto + trainer.balance_batch=True + trainer.critic_warmup=0 +) + +########################### launch ########################### +python3 -m verl.trainer.main_ppo \ + "${DATA[@]}" \ + "${MODEL[@]}" \ + "${ACTOR[@]}" \ + "${REF[@]}" \ + "${ROLLOUT[@]}" \ + "${TRAINER[@]}" \ + "$@" | tee "logs/run_qwen3_32b_gspo_fsdp_npu.log" diff --git a/verl_0720_main/verl/examples/ascend_extras/ppo_trainer/run_qwen3_8b_fsdp.sh b/verl_0720_main/verl/examples/ascend_extras/ppo_trainer/run_qwen3_8b_fsdp.sh new file mode 100644 index 0000000000000000000000000000000000000000..8d105ef61219c0cef49527a36d2eb94348ff1224 --- /dev/null +++ b/verl_0720_main/verl/examples/ascend_extras/ppo_trainer/run_qwen3_8b_fsdp.sh @@ -0,0 +1,57 @@ +set -x + +MODEL_PATH="Qwen/Qwen3-8B" + +python3 -m verl.trainer.main_ppo \ + algorithm.adv_estimator=gae \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.train_batch_size=64 \ + data.max_prompt_length=2000 \ + data.max_response_length=2000 \ + data.shuffle=False \ + actor_rollout_ref.model.path="${MODEL_PATH}" \ + actor_rollout_ref.model.use_remove_padding=True \ + actor_rollout_ref.model.enable_gradient_checkpointing=True \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=64 \ + actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=1 \ + actor_rollout_ref.actor.fsdp_config.param_offload=True \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=True \ + actor_rollout_ref.actor.use_kl_loss=False \ + actor_rollout_ref.actor.ulysses_sequence_parallel_size=2 \ + actor_rollout_ref.actor.use_dynamic_bsz=True \ + actor_rollout_ref.actor.use_torch_compile=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=1 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.9 \ + actor_rollout_ref.rollout.max_num_batched_tokens=4000 \ + actor_rollout_ref.rollout.max_num_seqs=64 \ + actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes=4096 \ + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True \ + actor_rollout_ref.rollout.enable_chunked_prefill=True \ + actor_rollout_ref.rollout.enforce_eager=False \ + actor_rollout_ref.rollout.calculate_log_probs=False \ + critic.optim.lr=1e-5 \ + critic.model.use_remove_padding=True \ + critic.model.path="${MODEL_PATH}" \ + critic.model.enable_gradient_checkpointing=True \ + critic.ppo_micro_batch_size_per_gpu=1 \ + critic.ulysses_sequence_parallel_size=2 \ + critic.fsdp.param_offload=True \ + critic.fsdp.optimizer_offload=True \ + critic.use_dynamic_bsz=True \ + trainer.critic_warmup=0 \ + trainer.logger=console \ + trainer.project_name='verl_example_ppo_gsm8k' \ + trainer.experiment_name='qwen3_8b_fsdp' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=-1 \ + trainer.val_before_train=False \ + trainer.max_actor_ckpt_to_keep=1 \ + trainer.max_critic_ckpt_to_keep=1 \ + +actor_rollout_ref.rollout.engine_kwargs.vllm.compilation_config.cudagraph_mode="FULL_DECODE_ONLY" \ + trainer.total_training_steps=15 2>&1 | tee ppo_qwen3-8b_fsdp_npu.log diff --git a/verl_0720_main/verl/examples/cispo_trainer/README.md b/verl_0720_main/verl/examples/cispo_trainer/README.md new file mode 100644 index 0000000000000000000000000000000000000000..5ed4afd45e95c233287ec5e467aff4970d4cdfed --- /dev/null +++ b/verl_0720_main/verl/examples/cispo_trainer/README.md @@ -0,0 +1,17 @@ +# CISPO + +CISPO (Clipped IS-weight Policy Optimization) is a policy-loss variant that decouples the lower/upper clip ratios to stabilize IS-ratio-weighted updates, used in MiniMax-M1. + +Reference: [MiniMax-M1: Scaling Test-Time Compute Efficiently with Lightning Attention](https://arxiv.org/abs/2506.13585). + +## Canonical Scripts + +| Script | Infer | Train | Platform | +|--------------------------------------|-------|-------|----------| +| `run_qwen3_8b_fsdp.sh` | vLLM | FSDP | NVIDIA | + +## Key Flags + +- `actor_rollout_ref.actor.policy_loss.loss_mode=cispo` +- `actor_rollout_ref.actor.clip_ratio_low=10` (effectively unclamped on lower side) +- `actor_rollout_ref.actor.clip_ratio_high=0.2` diff --git a/verl_0720_main/verl/examples/cispo_trainer/run_qwen3_8b_fsdp.sh b/verl_0720_main/verl/examples/cispo_trainer/run_qwen3_8b_fsdp.sh new file mode 100644 index 0000000000000000000000000000000000000000..5d03fd5f92e35d8664b34b8b6abe4c4fc2279deb --- /dev/null +++ b/verl_0720_main/verl/examples/cispo_trainer/run_qwen3_8b_fsdp.sh @@ -0,0 +1,120 @@ +#!/usr/bin/env bash +# CISPO | text | vLLM rollout | FSDP training | NVIDIA GPUs +# CISPO is a policy-loss variant on top of GRPO that decouples high/low clip ratios. + +set -xeuo pipefail + +# ---- user-adjustable ---- +MODEL_PATH=${MODEL_PATH:-Qwen/Qwen3-8B} +NNODES=${NNODES:-1} +NGPUS_PER_NODE=${NGPUS_PER_NODE:-8} + +train_batch_size=${TRAIN_BATCH_SIZE:-1024} +ppo_mini_batch_size=${PPO_MINI_BATCH_SIZE:-256} +max_prompt_length=${MAX_PROMPT_LENGTH:-1024} +max_response_length=${MAX_RESPONSE_LENGTH:-2048} +ppo_max_token_len_per_gpu=${PPO_MAX_TOKEN_LEN_PER_GPU:-24576} + +actor_lr=${ACTOR_LR:-1e-6} +kl_loss_coef=${KL_LOSS_COEF:-0.001} +entropy_coeff=${ENTROPY_COEFF:-0} + +clip_ratio_low=${CLIP_RATIO_LOW:-10} +clip_ratio_high=${CLIP_RATIO_HIGH:-0.2} + +rollout_tp=${ROLLOUT_TP:-2} +rollout_gpu_mem_util=${ROLLOUT_GPU_MEM_UTIL:-0.6} +rollout_n=${ROLLOUT_N:-5} + +total_epochs=${TOTAL_EPOCHS:-15} +save_freq=${SAVE_FREQ:-20} +test_freq=${TEST_FREQ:-5} + +project_name=${PROJECT_NAME:-verl_cispo_gsm8k_math} +experiment_name=${EXPERIMENT_NAME:-qwen3_8b_vllm_fsdp} +# ---- end user-adjustable ---- + +gsm8k_train=$HOME/data/gsm8k/train.parquet +gsm8k_test=$HOME/data/gsm8k/test.parquet +math_train=$HOME/data/math/train.parquet +math_test=$HOME/data/math/test.parquet + +train_files="['$gsm8k_train', '$math_train']" +val_files="['$gsm8k_test', '$math_test']" +########################### parameter arrays ########################### + +DATA=( + algorithm.adv_estimator=grpo + algorithm.use_kl_in_reward=False + data.train_files="$train_files" + data.val_files="$val_files" + data.train_batch_size=${train_batch_size} + data.max_prompt_length=${max_prompt_length} + data.max_response_length=${max_response_length} + data.filter_overlong_prompts=True + data.truncation='error' +) + +MODEL=( + actor_rollout_ref.model.path="$MODEL_PATH" + actor_rollout_ref.model.use_remove_padding=True + actor_rollout_ref.model.enable_gradient_checkpointing=True +) + +ACTOR=( + actor_rollout_ref.actor.policy_loss.loss_mode=cispo + actor_rollout_ref.actor.clip_ratio_low=${clip_ratio_low} + actor_rollout_ref.actor.clip_ratio_high=${clip_ratio_high} + actor_rollout_ref.actor.optim.lr=${actor_lr} + actor_rollout_ref.actor.ppo_mini_batch_size=${ppo_mini_batch_size} + actor_rollout_ref.actor.use_dynamic_bsz=True + actor_rollout_ref.actor.ppo_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} + actor_rollout_ref.actor.use_kl_loss=True + actor_rollout_ref.actor.kl_loss_coef=${kl_loss_coef} + actor_rollout_ref.actor.kl_loss_type=low_var_kl + actor_rollout_ref.actor.entropy_coeff=${entropy_coeff} + actor_rollout_ref.actor.fsdp_config.param_offload=False + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False +) + +ROLLOUT=( + actor_rollout_ref.rollout.name=vllm + actor_rollout_ref.rollout.tensor_model_parallel_size=${rollout_tp} + actor_rollout_ref.rollout.gpu_memory_utilization=${rollout_gpu_mem_util} + actor_rollout_ref.rollout.n=${rollout_n} + actor_rollout_ref.rollout.log_prob_use_dynamic_bsz=True + actor_rollout_ref.rollout.log_prob_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} +) + +REF=( + actor_rollout_ref.ref.log_prob_use_dynamic_bsz=True + actor_rollout_ref.ref.log_prob_max_token_len_per_gpu=${ppo_max_token_len_per_gpu} + actor_rollout_ref.ref.fsdp_config.param_offload=True +) + +TRAINER=( + trainer.balance_batch=True + trainer.critic_warmup=0 + trainer.logger='["console","wandb"]' + trainer.project_name=${project_name} + trainer.experiment_name=${experiment_name} + trainer.n_gpus_per_node=${NGPUS_PER_NODE} + trainer.nnodes=${NNODES} + trainer.save_freq=${save_freq} + trainer.test_freq=${test_freq} + trainer.total_epochs=${total_epochs} +) + +EXTRA=( +) + +########################### launch ########################### +python3 -m verl.trainer.main_ppo \ + "${DATA[@]}" \ + "${MODEL[@]}" \ + "${ACTOR[@]}" \ + "${ROLLOUT[@]}" \ + "${REF[@]}" \ + "${TRAINER[@]}" \ + "${EXTRA[@]}" \ + "$@" diff --git a/verl_0720_main/verl/examples/data_preprocess/aime2024_multiturn_w_tool.py b/verl_0720_main/verl/examples/data_preprocess/aime2024_multiturn_w_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..76cdd0576d3801118b160b850bcbd8d2fe6723b1 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/aime2024_multiturn_w_tool.py @@ -0,0 +1,79 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the DAPO-Math-17k dataset to multiturn format +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/retool_aime2024", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_path = "BytedTsinghua-SIA/AIME-2024" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "default") + else: + dataset = datasets.load_dataset(data_path, "default") + + train_dataset = dataset["train"] + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + orig_extra_info = example.pop("extra_info") + extra_info = orig_extra_info.copy() + extra_info["need_tools_kwargs"] = True + extra_info["tools_kwargs"] = { + "code_interpreter": { + "create_kwargs": { + "ground_truth": example["reward_model"]["ground_truth"], + }, + }, + } + example["extra_info"] = extra_info + return example + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/dapo_multiturn_w_tool.py b/verl_0720_main/verl/examples/data_preprocess/dapo_multiturn_w_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..aab356f41bf38e789a31f1ee879ce9beb8b0aa40 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/dapo_multiturn_w_tool.py @@ -0,0 +1,79 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the DAPO-Math-17k dataset to multiturn format +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/retool_dapo", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_path = "BytedTsinghua-SIA/DAPO-Math-17k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "default") + else: + dataset = datasets.load_dataset(data_path, "default") + + train_dataset = dataset["train"] + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + orig_extra_info = example.pop("extra_info") + extra_info = orig_extra_info.copy() + extra_info["need_tools_kwargs"] = True + extra_info["tools_kwargs"] = { + "code_interpreter": { + "create_kwargs": { + "ground_truth": example["reward_model"]["ground_truth"], + }, + }, + } + example["extra_info"] = extra_info + return example + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/full_hh_rlhf.py b/verl_0720_main/verl/examples/data_preprocess/full_hh_rlhf.py new file mode 100644 index 0000000000000000000000000000000000000000..4e8a148df1e322f476cedffe4eadc5ae6ee9b6f1 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/full_hh_rlhf.py @@ -0,0 +1,161 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +- Preprocess data and split the training set into 75% for training RM and 25% for validting RM. +- All the training data is used to train SFT and RL. +- Both chosen and rejected is used to train SFT +""" + +import argparse +import os + +import pandas as pd +from datasets import load_dataset +from tqdm.auto import tqdm + +from verl.utils.fs import copy, makedirs + + +def generate_sft_dataset(target_hdfs_path_dir, local_dir="~/data/full_hh_rlh/sft", local_dataset_path=None): + if local_dataset_path is not None: + dataset = load_dataset(local_dataset_path) + else: + dataset = load_dataset("Dahoas/full-hh-rlhf") + output = {"prompt": [], "response": []} + for data in tqdm(dataset["train"]): + # add chosen + output["prompt"].append(data["prompt"]) + output["response"].append(data["chosen"]) + + # add rejection + output["prompt"].append(data["prompt"]) + output["response"].append(data["rejected"]) + + df = pd.DataFrame(output) + + local_dir = os.path.expanduser(local_dir) + os.makedirs(local_dir, exist_ok=True) + + local_path = os.path.join(local_dir, "train.parquet") + + df.to_parquet(path=local_path) + + if target_hdfs_path_dir is not None: + hdfs_dir = target_hdfs_path_dir + "/" + "train.parquet" + makedirs(hdfs_dir) + + copy(local_path, hdfs_dir) + + +def generate_rm_dataset(target_hdfs_path_dir, local_dir="~/data/full_hh_rlh/rm", local_dataset_path=None): + if local_dataset_path is not None: + train_dataset = load_dataset(local_dataset_path, split="train[:75%]") + test_dataset = load_dataset(local_dataset_path, split="train[-25%:]") + else: + train_dataset = load_dataset("Dahoas/full-hh-rlhf", split="train[:75%]") + test_dataset = load_dataset("Dahoas/full-hh-rlhf", split="train[-25%:]") + + local_dir = os.path.expanduser(local_dir) + os.makedirs(local_dir, exist_ok=True) + + for dataset, name in zip([train_dataset, test_dataset], ["train", "test"], strict=True): + output = {"prompt": [], "chosen": [], "rejected": []} + for data in tqdm(dataset): + # add chosen + output["prompt"].append(data["prompt"]) + output["chosen"].append(data["chosen"]) + output["rejected"].append(data["rejected"]) + + df = pd.DataFrame(output) + + local_path = os.path.join(local_dir, name + ".parquet") + + df.to_parquet(path=local_path) + + if target_hdfs_path_dir is not None: + hdfs_dir = target_hdfs_path_dir + "/" + name + ".parquet" + makedirs(hdfs_dir) + + copy(local_path, hdfs_dir) + + +def generate_rl_dataset(target_hdfs_path_dir, local_dir="~/data/full_hh_rlhf/rl", local_dataset_path=None): + if local_dataset_path is not None: + dataset = load_dataset(local_dataset_path) + else: + dataset = load_dataset("Dahoas/full-hh-rlhf") + train_dataset = dataset["train"] + + data_source = "Dahoas/full-hh-rlhf" + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + prompt = example.pop("prompt") + response = example.pop("response") + + data = { + "data_source": data_source, + "prompt": [{"role": "user", "content": prompt}], + "ability": "alignment", + "reward_model": { + "style": "model", + "ground_truth": response, # should not be used + }, + "extra_info": {"split": split, "index": idx}, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + local_dir = os.path.expanduser(local_dir) + local_path = os.path.join(local_dir, "train.parquet") + train_dataset.to_parquet(local_path) + + if target_hdfs_path_dir is not None: + hdfs_dir = target_hdfs_path_dir + "/" + "train.parquet" + makedirs(hdfs_dir) + + copy(local_path, hdfs_dir) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--split", type=str, choices=["sft", "rm", "rl"], required=True) + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", type=str, required=False, default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", + type=str, + default="~/data/full_hh_rlhf", + help="The save directory for the preprocessed dataset.", + ) + + args = parser.parse_args() + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + if args.split == "sft": + generate_sft_dataset(args.hdfs_dir, os.path.join(local_save_dir, args.split), args.local_dataset_path) + elif args.split == "rm": + generate_rm_dataset(args.hdfs_dir, os.path.join(local_save_dir, args.split), args.local_dataset_path) + elif args.split == "rl": + generate_rl_dataset(args.hdfs_dir, os.path.join(local_save_dir, args.split), args.local_dataset_path) + else: + raise NotImplementedError diff --git a/verl_0720_main/verl/examples/data_preprocess/geo3k.py b/verl_0720_main/verl/examples/data_preprocess/geo3k.py new file mode 100644 index 0000000000000000000000000000000000000000..ba84fd3fc440761a200d0fbdea1535bfe9889b45 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/geo3k.py @@ -0,0 +1,102 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the Geometry3k dataset to parquet format +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None) + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/geo3k", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "hiyouga/geometry3k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset( + local_dataset_path, + ) + else: + dataset = datasets.load_dataset( + data_source, + ) + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = ( + r"You FIRST think about the reasoning process as an internal monologue and then provide the final answer. " + r"The reasoning process MUST BE enclosed within tags. " + r"The final answer MUST BE put in \boxed{}." + ) + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + problem = example.pop("problem") + prompt = problem + " " + instruction_following + answer = example.pop("answer") + images = example.pop("images") + + data = { + "data_source": data_source, + "prompt": [ + { + "role": "user", + "content": prompt, + } + ], + "images": images, + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": answer}, + "extra_info": { + "split": split, + "index": idx, + "answer": answer, + "question": problem, + }, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True, num_proc=8) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True, num_proc=8) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/geo3k_multiturn_w_tool.py b/verl_0720_main/verl/examples/data_preprocess/geo3k_multiturn_w_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..53c7197f9d2d00ccf256aee57e2de1847a926725 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/geo3k_multiturn_w_tool.py @@ -0,0 +1,120 @@ +# Copyright 2023-2025 SGLang Team +# Copyright Amazon.com, Inc. or its affiliates. +# Copyright 2025 Reallm Labs Ltd. or its affiliates +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Preprocess the Geometry3k dataset to parquet format +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", + default="~/data/geo3k_multiturn_w_tool", + help="The save directory for the preprocessed dataset.", + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "hiyouga/geometry3k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path) + else: + dataset = datasets.load_dataset(data_source) + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = ( + r"You FIRST think about the reasoning process as an internal monologue and then provide the final answer. " + r"The reasoning process MUST BE enclosed within tags. " + r"The final answer MUST BE put in \boxed{}." + ) + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + problem = example.pop("problem") + prompt = problem + " " + instruction_following + answer = example.pop("answer") + images = example.pop("images") + data = { + "data_source": data_source, + "prompt": [ + { + "role": "system", + "content": ( + "You are a math expert. You are given a question and you need to solve it step by step. " + "Reasoning step by step before any tool call. " + "You should use the `calc_geo3k_reward` tool after step by step solving the question, " + "before generate final answer at least once and refine your answer if necessary. " + ), + }, + { + "role": "user", + "content": prompt, + }, + ], + "images": images, + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": answer}, + "extra_info": { + "split": split, + "index": idx, + "answer": answer, + "question": problem, + "need_tools_kwargs": True, + "tools_kwargs": { + "calc_geo3k_reward": { + "create_kwargs": {"ground_truth": answer}, + # "execute_kwargs": {}, + # "calc_reward_kwargs": {}, + # "release_kwargs": {}, + }, + }, + }, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True, num_proc=8) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True, num_proc=8) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/gsm8k.py b/verl_0720_main/verl/examples/data_preprocess/gsm8k.py new file mode 100644 index 0000000000000000000000000000000000000000..1656cdbc896a8f14fc7e09705d36335f52165533 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/gsm8k.py @@ -0,0 +1,105 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the GSM8k dataset to parquet format +""" + +import argparse +import os +import re + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + + +def extract_solution(solution_str): + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) + assert solution is not None + final_solution = solution.group(0) + final_solution = final_solution.split("#### ")[1].replace(",", "") + return final_solution + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/gsm8k", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "openai/gsm8k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "main") + else: + dataset = datasets.load_dataset(data_source, "main") + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = 'Let\'s think step by step and output the final answer after "####".' + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + question_raw = example.pop("question") + + question = question_raw + " " + instruction_following + + answer_raw = example.pop("answer") + solution = extract_solution(answer_raw) + data = { + "data_source": data_source, + "prompt": [ + { + "role": "user", + "content": question, + } + ], + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": solution}, + "extra_info": { + "split": split, + "index": idx, + "answer": answer_raw, + "question": question_raw, + }, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_sft.py b/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_sft.py new file mode 100644 index 0000000000000000000000000000000000000000..4589362f933aa95493fdd98ce965eb810180c98a --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_sft.py @@ -0,0 +1,102 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the GSM8k dataset to parquet format +""" + +import argparse +import os +import re + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + + +def extract_solution(solution_str): + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) + assert solution is not None + final_solution = solution.group(0) + final_solution = final_solution.split("#### ")[1].replace(",", "") + return final_solution + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/gsm8k_sft", help="The save directory for the preprocessed dataset." + ) + parser.add_argument("--hdfs_dir", default=None) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "openai/gsm8k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "main") + else: + dataset = datasets.load_dataset(data_source, "main") + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = 'Let\'s think step by step and output the final answer after "####".' + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + question_raw = example.pop("question") + + question = question_raw + " " + instruction_following + + answer_raw = example.pop("answer") + data = { + "messages": [ + { + "role": "user", + "content": question, + }, + { + "role": "assistant", + "content": answer_raw, + }, + ], + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + hdfs_dir = args.hdfs_dir + + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + local_save_dir = os.path.expanduser(local_save_dir) + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_w_tool.py b/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_w_tool.py new file mode 100644 index 0000000000000000000000000000000000000000..8ce02127dfd3dace9bbd3bd4cb12dc12e18db289 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/gsm8k_multiturn_w_tool.py @@ -0,0 +1,125 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the GSM8k dataset to parquet format +""" + +import argparse +import os +import re + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + + +def extract_solution(solution_str): + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) + assert solution is not None + final_solution = solution.group(0) + final_solution = final_solution.split("#### ")[1].replace(",", "") + return final_solution + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/gsm8k", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "openai/gsm8k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "main") + else: + dataset = datasets.load_dataset(data_source, "main") + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = "Let's think step by step and output the final answer after `####`." + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + question_raw = example.pop("question") + + question = question_raw + " " + instruction_following + + answer_raw = example.pop("answer") + solution = extract_solution(answer_raw) + data = { + "data_source": data_source, + "prompt": [ + { + "role": "system", + "content": ( + "You are a math expert. You are given a question and you need to solve it step by step. " + "Reasoning step by step before any tool call. " + "You should use the `calc_gsm8k_reward` tool after step by step solving the question, " + "before generate final answer at least once and refine your answer if necessary. " + "Put your final answer in the format of `#### `." + ), + }, + { + "role": "user", + "content": question, + }, + ], + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": solution}, + "extra_info": { + "split": split, + "index": idx, + "answer": answer_raw, + "question": question_raw, + "need_tools_kwargs": True, + "tools_kwargs": { + "calc_gsm8k_reward": { + "create_kwargs": {"ground_truth": solution}, + # "execute_kwargs": {}, + # "calc_reward_kwargs": {}, + # "release_kwargs": {}, + }, + }, + }, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/gsm8k_tool_agent_loop.py b/verl_0720_main/verl/examples/data_preprocess/gsm8k_tool_agent_loop.py new file mode 100644 index 0000000000000000000000000000000000000000..0bb1d9ef08d055fce3047f9b502c1e6cb84884d6 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/gsm8k_tool_agent_loop.py @@ -0,0 +1,126 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the GSM8k dataset to parquet format +""" + +import argparse +import os +import re + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + + +def extract_solution(solution_str): + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) + assert solution is not None + final_solution = solution.group(0) + final_solution = final_solution.split("#### ")[1].replace(",", "") + return final_solution + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/gsm8k", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "openai/gsm8k" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path, "main") + else: + dataset = datasets.load_dataset(data_source, "main") + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = "Let's think step by step and output the final answer after `####`." + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + question_raw = example.pop("question") + + question = question_raw + " " + instruction_following + + answer_raw = example.pop("answer") + solution = extract_solution(answer_raw) + data = { + "data_source": data_source, + "agent_name": "tool_agent", + "prompt": [ + { + "role": "system", + "content": ( + "You are a math expert. You are given a question and you need to solve it step by step. " + "Reasoning step by step before any tool call. " + "You should use the `calc_gsm8k_reward` tool after step by step solving the question, " + "before generate final answer at least once and refine your answer if necessary. " + "Put your final answer in the format of `#### `." + ), + }, + { + "role": "user", + "content": question, + }, + ], + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": solution}, + "extra_info": { + "split": split, + "index": idx, + "answer": answer_raw, + "question": question_raw, + "need_tools_kwargs": True, + "tools_kwargs": { + "calc_gsm8k_reward": { + "create_kwargs": {"ground_truth": solution}, + # "execute_kwargs": {}, + # "calc_reward_kwargs": {}, + # "release_kwargs": {}, + }, + }, + }, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/hellaswag.py b/verl_0720_main/verl/examples/data_preprocess/hellaswag.py new file mode 100644 index 0000000000000000000000000000000000000000..dc73a810a80570d406bb727099f5524037be2370 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/hellaswag.py @@ -0,0 +1,108 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess Hellaswag dataset. + +""" + +import argparse +import os +import re + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + + +def preprocess(text): + text = text.strip() + # NOTE: Brackets are artifacts of the WikiHow dataset portion of HellaSwag. + text = text.replace(" [title]", ". ") + text = re.sub("\\[.*?\\]", "", text) + text = text.replace(" ", " ") + return text + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None, help="The save directory for the preprocessed dataset.") + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/hellaswag", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "Rowan/hellaswag" + + if local_dataset_path is not None: + dataset = datasets.load_dataset(local_dataset_path) + else: + dataset = datasets.load_dataset(data_source, trust_remote_code=True) + + train_dataset = dataset["train"] + val_dataset = dataset["validation"] + test_dataset = dataset["test"] + + instruction = "Please complete the following sentence.\n" + + def make_map_fn(split): + def process_fn(doc, idx): + ctx = doc["ctx_a"] + " " + doc["ctx_b"].capitalize() + query = preprocess(doc["activity_label"] + ": " + ctx) + choices = [preprocess(ending) for ending in doc["endings"]] + gold = int(doc["label"]) + + data = { + "data_source": data_source, + "prompt": [{"role": "user", "content": query}], + "ability": "nlp", + "reward_model": { + "style": "model", + "eval": "multiple_choice", # using loglikelihood + "ground_truth": gold, + "choices": choices, + }, + "extra_info": {"split": split, "index": idx}, + } + return data + + return process_fn + + # filter data that doesn't have a label + train_dataset = train_dataset.filter(lambda x: len(x["label"]) > 0) + val_dataset = val_dataset.filter(lambda x: len(x["label"]) > 0) + test_dataset = test_dataset.filter(lambda x: len(x["label"]) > 0) + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + val_dataset = val_dataset.map(function=make_map_fn("validation"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + val_dataset.to_parquet(os.path.join(local_save_dir, "validation.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/math_dataset.py b/verl_0720_main/verl/examples/data_preprocess/math_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..b23a032fb1207a47dcd1bc77194a7c1a124aad55 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/math_dataset.py @@ -0,0 +1,106 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the MATH-lighteval dataset to parquet format +""" + +import argparse +import json +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs +from verl.utils.reward_score.math_reward import last_boxed_only_string, remove_boxed + + +def extract_solution(solution_str): + return remove_boxed(last_boxed_only_string(solution_str)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None) + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", default="~/data/math", help="The save directory for the preprocessed dataset." + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + # 'lighteval/MATH' is no longer available on huggingface. + # Use mirror repo: DigitalLearningGmbH/MATH-lighteval + data_source = "DigitalLearningGmbH/MATH-lighteval" + print(f"Loading the {data_source} dataset from huggingface...", flush=True) + if local_dataset_path is not None: + dataset = datasets.load_dataset( + local_dataset_path, + ) + else: + dataset = datasets.load_dataset( + data_source, + ) + + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + instruction_following = "Let's think step by step and output the final answer within \\boxed{}." + + # add a row to each data item that represents a unique id + def make_map_fn(split): + def process_fn(example, idx): + question = example.pop("problem") + + question = question + " " + instruction_following + + answer = example.pop("solution") + solution = extract_solution(answer) + data = { + "data_source": data_source, + "prompt": [{"role": "user", "content": question}], + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": solution}, + "extra_info": {"split": split, "index": idx}, + } + return data + + return process_fn + + train_dataset = train_dataset.map(function=make_map_fn("train"), with_indices=True) + test_dataset = test_dataset.map(function=make_map_fn("test"), with_indices=True) + + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + local_dir = os.path.expanduser(local_save_dir) + hdfs_dir = args.hdfs_dir + + train_dataset.to_parquet(os.path.join(local_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_dir, "test.parquet")) + # Save one example as JSON for reference + example = train_dataset[0] + with open(os.path.join(local_dir, "train_example.json"), "w") as f: + json.dump(example, f, indent=2) + example = test_dataset[0] + with open(os.path.join(local_dir, "test_example.json"), "w") as f: + json.dump(example, f, indent=2) + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/multiturn.py b/verl_0720_main/verl/examples/data_preprocess/multiturn.py new file mode 100644 index 0000000000000000000000000000000000000000..4bf0192b817d3acfc41f7e4a74a4a891d3ae61d6 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/multiturn.py @@ -0,0 +1,125 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates + +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at + +# http://www.apache.org/licenses/LICENSE-2.0 + +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Create a simple multi-turn dataset for testing +""" + +import argparse +import os + +import pandas as pd + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default="~/data/multiturn") + parser.add_argument("--hdfs_dir", default=None) + args = parser.parse_args() + + # Create example conversations + conversations = [] + + # Conversation 1 + conversations.append( + { + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "What is the capital of France?"}, + {"role": "assistant", "content": "The capital of France is Paris."}, + {"role": "user", "content": "And what about Germany?"}, + {"role": "assistant", "content": "The capital of Germany is Berlin."}, + ] + } + ) + + # Conversation 2 + conversations.append( + { + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Can you explain quantum computing?"}, + { + "role": "assistant", + "content": "Quantum computing is a type of computing that uses quantum-mechanical phenomena, " + "such as superposition and entanglement, to perform operations on data.", + }, + {"role": "user", "content": "How is it different from classical computing?"}, + { + "role": "assistant", + "content": "Classical computing uses bits that are either 0 or 1, while quantum computing uses " + "quantum bits or qubits that can exist in multiple states simultaneously due to superposition.", + }, + ] + } + ) + + # Conversation 3 + conversations.append( + { + "messages": [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "Write a simple Python function to calculate factorial."}, + { + "role": "assistant", + "content": ( + "```python\ndef factorial(n):\n if n == 0 or n == 1:\n return 1\n else:\n " + "return n * factorial(n-1)\n```\n\nThis is a recursive function to calculate the " + "factorial of a number." + ), + }, + {"role": "user", "content": "Can you make it iterative instead?"}, + { + "role": "assistant", + "content": ( + "```python\ndef factorial(n):\n result = 1\n for i in range(1, n+1):\n " + "result *= i\n return result\n```\n\nThis is an iterative version of the factorial function." + ), + }, + ] + } + ) + + # Create train and test datasets + train_data = conversations[:2] # First 2 conversations for training + test_data = conversations[2:] # Last conversation for testing + + # Create output directory + local_dir = os.path.expanduser(args.local_dir) + os.makedirs(local_dir, exist_ok=True) + + # Save to parquet files + train_df = pd.DataFrame(train_data) + test_df = pd.DataFrame(test_data) + + train_df.to_parquet(os.path.join(local_dir, "train.parquet")) + test_df.to_parquet(os.path.join(local_dir, "test.parquet")) + + # Handle HDFS if specified + if args.hdfs_dir is not None: + try: + from verl.utils.hdfs_io import copy, makedirs + + makedirs(args.hdfs_dir) + copy(src=local_dir, dst=args.hdfs_dir) + except ImportError: + print("Warning: HDFS support not available. Skipping HDFS copy.") + + # Print statistics + print(f"Train dataset size: {len(train_df)}") + print(f"Test dataset size: {len(test_df)}") + print(f"Data saved to {local_dir}") + + +if __name__ == "__main__": + main() diff --git a/verl_0720_main/verl/examples/data_preprocess/openr1mm.py b/verl_0720_main/verl/examples/data_preprocess/openr1mm.py new file mode 100644 index 0000000000000000000000000000000000000000..30c0c1e30a234a9684be81e0eb8ce2b3add1e736 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/openr1mm.py @@ -0,0 +1,98 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess the lmms-lab/multimodal-open-r1-8k-verified dataset to parquet format. + +Images are kept as raw bytes (no decode, no resize). +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument( + "--local_save_dir", default="~/data/openr1mm", help="The save directory for the preprocessed dataset." + ) + parser.add_argument("--hdfs_dir", default=None) + args = parser.parse_args() + + data_source = "lmms-lab/multimodal-open-r1-8k-verified" + dataset = datasets.load_dataset(data_source) + + instruction = ( + "You FIRST think about the reasoning process as an internal monologue " + "and then provide the final answer. " + "The reasoning process MUST BE enclosed within tags. " + "The final answer MUST BE enclosed within tags." + ) + + def make_map_fn(split): + def process_fn(example, idx): + problem = example.pop("problem") + solution = example.pop("solution") + img = example.pop("image") + + prompt_content = f"\n{problem}\n\n{instruction}" + + # Keep image as raw bytes dict to avoid lossy re-encoding. + # The Qwen VL processor handles resize at runtime. + if isinstance(img, dict) and "bytes" in img: + image_data = img + elif isinstance(img, bytes): + image_data = {"bytes": img} + else: + image_data = img + + data = { + "data_source": data_source, + "prompt": [{"role": "user", "content": prompt_content}], + "images": [image_data], + "ability": "math", + "reward_model": {"style": "rule", "ground_truth": solution}, + "extra_info": { + "split": split, + "index": idx, + "question": problem, + "answer": solution, + }, + } + return data + + return process_fn + + full_dataset = dataset["train"] + full_dataset = full_dataset.cast_column("image", datasets.Image(decode=False)) + split_dataset = full_dataset.train_test_split(test_size=0.1, seed=42) + + train_dataset = split_dataset["train"].map(function=make_map_fn("train"), with_indices=True, num_proc=8) + test_dataset = split_dataset["test"].map(function=make_map_fn("test"), with_indices=True, num_proc=8) + + columns = ["data_source", "prompt", "images", "ability", "reward_model", "extra_info"] + train_dataset = train_dataset.select_columns(columns) + test_dataset = test_dataset.select_columns(columns) + + local_save_dir = os.path.expanduser(args.local_save_dir) + os.makedirs(local_save_dir, exist_ok=True) + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if args.hdfs_dir is not None: + makedirs(args.hdfs_dir) + copy(src=local_save_dir, dst=args.hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/pokemon.py b/verl_0720_main/verl/examples/data_preprocess/pokemon.py new file mode 100644 index 0000000000000000000000000000000000000000..3bbf4d4b46ee98669eaa40a9a9084f918791f50d --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/pokemon.py @@ -0,0 +1,75 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +""" +Preprocess the llamafactory/pokemon-gpt4o-captions dataset to parquet format +""" + +import argparse +import os + +import datasets + +from verl.utils.hdfs_io import copy, makedirs + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("--local_dir", default=None) + parser.add_argument("--hdfs_dir", default=None) + parser.add_argument("--local_dataset_path", default=None, help="The local path to the raw dataset, if it exists.") + parser.add_argument( + "--local_save_dir", + default="~/data/pokemon-gpt4o-captions", + help="The save directory for the preprocessed dataset.", + ) + + args = parser.parse_args() + local_dataset_path = args.local_dataset_path + + data_source = "llamafactory/pokemon-gpt4o-captions" + + if local_dataset_path is not None: + dataset = datasets.load_dataset( + local_dataset_path, + ) + else: + dataset = datasets.load_dataset( + data_source, + ) + + def map_fn(row: dict): + messages = [] + conversation = row.pop("conversations") + for conv in conversation: + if conv["from"] == "gpt": + role = "assistant" + elif conv["from"] == "human": + role = "user" + else: + raise ValueError(f"Unknown role: {conv['from']}") + messages.append( + { + "role": role, + "content": conv["value"], + } + ) + + row["messages"] = messages + return row + + dataset = dataset["train"].map(map_fn, num_proc=16) + dataset = dataset.train_test_split(test_size=0.1) + train_dataset = dataset["train"] + test_dataset = dataset["test"] + + hdfs_dir = args.hdfs_dir + local_save_dir = args.local_dir + if local_save_dir is not None: + print("Warning: Argument 'local_dir' is deprecated. Please use 'local_save_dir' instead.") + else: + local_save_dir = args.local_save_dir + + train_dataset.to_parquet(os.path.join(local_save_dir, "train.parquet")) + test_dataset.to_parquet(os.path.join(local_save_dir, "test.parquet")) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + copy(src=local_save_dir, dst=hdfs_dir) diff --git a/verl_0720_main/verl/examples/data_preprocess/preprocess_search_r1_dataset.py b/verl_0720_main/verl/examples/data_preprocess/preprocess_search_r1_dataset.py new file mode 100644 index 0000000000000000000000000000000000000000..a0c10d59b9c006ae7234ce21f7bdb25562259b23 --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/preprocess_search_r1_dataset.py @@ -0,0 +1,178 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023-2024 SGLang Team +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse +import logging +import os +import tempfile + +import pandas as pd +from huggingface_hub import hf_hub_download +from huggingface_hub.utils import EntryNotFoundError + +from verl.utils.hdfs_io import copy, makedirs + +# Setup logging +logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s") +logger = logging.getLogger(__name__) + +# Configuration constants +DEFAULT_SYSTEM_CONTENT = "You are a helpful and harmless assistant." +DEFAULT_USER_CONTENT_PREFIX = ( + "Answer the given question. You must conduct reasoning inside and " + "first every time you get new information. After reasoning, if you find you lack " + "some knowledge, you can call a search engine by query " + "and it will return the top searched results between and " + ". You can search as many times as your want. If you find no " + "further external knowledge needed, you can directly provide the answer inside " + " and , without detailed illustrations. For example, " + " Beijing . Question: " +) + + +def process_single_row(row, current_split_name, row_index): + """ + Process a single row of data for SearchR1-like format. + + Args: + row: DataFrame row containing the original data + current_split_name: Name of the current split (train/test) + row_index: Index of the row in the DataFrame + + Returns: + pd.Series: Processed row data in the required format + """ + question = row.get("question", "") + + # Build prompt structure + user_content = user_content_prefix.rstrip("\n") + question + prompt = [{"role": "system", "content": system_content}, {"role": "user", "content": user_content}] + + # Extract ground truth from reward_model or fallback to golden_answers + reward_model_data = row.get("reward_model") + if isinstance(reward_model_data, dict) and "ground_truth" in reward_model_data: + ground_truth = reward_model_data.get("ground_truth") + else: + ground_truth = row.get("golden_answers", []) + + # Process data source + data_source_tagged = "searchR1_" + str(row.get("data_source", "")) + + # Build tools kwargs structure + tools_kwargs = { + "search": { + "create_kwargs": {"ground_truth": ground_truth, "question": question, "data_source": data_source_tagged} + } + } + + # Build complete extra_info structure + extra_info = { + "index": row_index, + "need_tools_kwargs": True, + "question": question, + "split": current_split_name, + "tools_kwargs": tools_kwargs, + } + + return pd.Series( + { + "data_source": data_source_tagged, + "prompt": prompt, + "ability": row.get("ability"), + "reward_model": reward_model_data, + "extra_info": extra_info, + "metadata": row.get("metadata"), + } + ) + + +def main(): + local_save_dir = os.path.expanduser(args.local_dir) + os.makedirs(local_save_dir, exist_ok=True) + + processed_files = [] + + # Download and process files using temporary directory + with tempfile.TemporaryDirectory() as tmp_download_dir: + for split in ["train", "test"]: + parquet_filename = f"{split}.parquet" + logger.info(f"Processing {split} split...") + + try: + # Download Parquet file from HuggingFace + logger.info(f"Downloading {parquet_filename} from {args.hf_repo_id}") + local_parquet_filepath = hf_hub_download( + repo_id=args.hf_repo_id, + filename=parquet_filename, + repo_type="dataset", + local_dir=tmp_download_dir, + local_dir_use_symlinks=False, + ) + + # Load and process Parquet file + df_raw = pd.read_parquet(local_parquet_filepath) + logger.info(f"Loaded {len(df_raw)} rows from {parquet_filename}") + + def apply_process_row(row, split_name=split): + return process_single_row(row, current_split_name=split_name, row_index=row.name) + + df_processed = df_raw.apply(apply_process_row, axis=1) + + # Save processed DataFrame + output_file_path = os.path.join(local_save_dir, f"{split}.parquet") + df_processed.to_parquet(output_file_path, index=False) + logger.info(f"Saved {len(df_processed)} processed rows to {output_file_path}") + processed_files.append(output_file_path) + + except EntryNotFoundError: + logger.warning(f"{parquet_filename} not found in repository {args.hf_repo_id}") + except Exception as e: + logger.error(f"Error processing {split} split: {e}") + + if not processed_files: + logger.warning("No data was processed or saved") + return + + logger.info(f"Successfully processed {len(processed_files)} files to {local_save_dir}") + + # Copy to HDFS if specified + if args.hdfs_dir: + try: + makedirs(args.hdfs_dir) + copy(src=local_save_dir, dst=args.hdfs_dir) + logger.info(f"Successfully copied files to HDFS: {args.hdfs_dir}") + except Exception as e: + logger.error(f"Error copying files to HDFS: {e}") + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Download Search-R1 from HuggingFace, process, and save to Parquet.") + parser.add_argument( + "--hf_repo_id", default="PeterJinGo/nq_hotpotqa_train", help="HuggingFace dataset repository ID." + ) + parser.add_argument( + "--local_dir", + default="~/data/searchR1_processed_direct", + help="Local directory to save the processed Parquet files.", + ) + parser.add_argument("--hdfs_dir", default=None, help="Optional HDFS directory to copy the Parquet files to.") + + args = parser.parse_args() + + # System and user content configuration + system_content = DEFAULT_SYSTEM_CONTENT + user_content_prefix = DEFAULT_USER_CONTENT_PREFIX + + main() diff --git a/verl_0720_main/verl/examples/data_preprocess/tinyllava_video_r1.py b/verl_0720_main/verl/examples/data_preprocess/tinyllava_video_r1.py new file mode 100644 index 0000000000000000000000000000000000000000..d89310178bb4ec65e022223b6f7e44197f98ea2d --- /dev/null +++ b/verl_0720_main/verl/examples/data_preprocess/tinyllava_video_r1.py @@ -0,0 +1,191 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Preprocess Zhang199/TinyLLaVA-Video-R1-training-data to verl parquet format. + + - Prompt: " + score = score / 4 + return score + return score + else: + return format_score + + +def compute_score_subem(solution_str, ground_truth, method="strict", format_score=0.0, score=1.0): + """The scoring function for substring exact match (EM). + + Args: + solution_str: the solution text + ground_truth: the ground truth + method: the method to extract the solution, choices are 'strict' and 'flexible' + format_score: the score for the format + score: the score for the correct answer + """ + answer = extract_solution(solution_str=solution_str) + do_print = random.randint(1, 64) == 1 + + if do_print: + print("--------------------------------") + print(f"Golden answers: {ground_truth['target']}") + print(f"Extracted answer: {answer}") + print(f"Solution string: {solution_str}") + + if answer is None: + return 0 + else: + if subem_check(answer, ground_truth["target"]): + return score + else: + return format_score diff --git a/verl_0720_main/verl/verl/utils/rollout_trace.py b/verl_0720_main/verl/verl/utils/rollout_trace.py new file mode 100644 index 0000000000000000000000000000000000000000..91dcefe9bcd0fa0461455eb16cb4eddedd2d32ac --- /dev/null +++ b/verl_0720_main/verl/verl/utils/rollout_trace.py @@ -0,0 +1,445 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import contextlib +import functools +import inspect +import json +import os +from contextvars import ContextVar +from typing import Optional + +from pydantic import BaseModel + +from verl.utils.ray_utils import get_event_loop + +_trace_enabled: ContextVar[bool] = ContextVar("_trace_enabled", default=True) +_trace_attributes: ContextVar[dict | None] = ContextVar("_trace_attributes", default=None) + + +class RolloutTraceConfig: + """Configuration for rollout tracing with various backends. + + Singleton configuration class for managing rollout trace settings across different + tracing backends like Weave, MLflow, and Trackio. + + Args: + backend (Optional[str]): Tracing backend to use ('weave', 'mlflow', or None). + client (Optional[object]): Client instance for the selected backend. + token2text (bool): Whether to convert tokens to text in traces. Defaults to False. + project_name (str): Name of the project for tracing. + experiment_name (str): Name of the experiment for tracing. + max_samples_per_step_per_worker (Optional[int]): Maximum number of unique samples to trace + per worker per step. If None, all samples are traced. If set, each worker will randomly + select up to this many unique samples to trace (including all their rollouts for GRPO). + Total traces = max_samples_per_step_per_worker * num_workers * n_rollouts_per_sample. + """ + + _instance: Optional["RolloutTraceConfig"] = None + backend: str | None = None + client: object | None = None + token2text: bool = False + _initialized: bool = False + project_name: str = None + experiment_name: str = None + max_samples_per_step_per_worker: int | None = None + + def __new__(cls, *args, **kwargs): + if cls._instance is None: + cls._instance = super().__new__(cls) + cls._instance._initialized = False + return cls._instance + + @classmethod + def get_instance(cls) -> "RolloutTraceConfig": + if cls._instance is None: + cls._instance = cls() + return cls._instance + + @classmethod + def init( + cls, + project_name: str, + experiment_name: str, + backend: str, + token2text: bool = False, + max_samples_per_step_per_worker: int | None = None, + ): + config = cls.get_instance() + if config._initialized: + return + + config.backend = backend + config.token2text = token2text + config.project_name = project_name + config.experiment_name = experiment_name + config.max_samples_per_step_per_worker = max_samples_per_step_per_worker + + if backend == "weave": + import weave + + config.client = weave.init(project_name) + elif backend == "mlflow": + import mlflow + + mlflow.config.enable_async_logging() + config.client = mlflow + + MLFLOW_TRACKING_URI = os.environ.get("MLFLOW_TRACKING_URI", "sqlite:////tmp/mlruns.db") + mlflow.set_tracking_uri(MLFLOW_TRACKING_URI) + + mlflow.set_experiment(project_name) + elif backend == "trackio": + import trackio + from trackio import context_vars + + if context_vars.current_run.get() is None: + trackio.init(project=project_name, name=experiment_name, config={"framework": "verl"}) + config.client = trackio + else: + config.client = None + + config._initialized = True + + @classmethod + def get_backend(cls) -> str | None: + return cls.get_instance().backend + + @classmethod + def get_client(cls) -> object | None: + return cls.get_instance().client + + @classmethod + def enable_token2text(cls) -> bool | None: + return cls.get_instance().token2text + + @classmethod + def reset(cls): + cls._instance = None + + +@contextlib.contextmanager +def rollout_trace_attr( + sample_index=None, step=None, rollout_n=None, name="rollout_trace", validate=False, trace: bool = True +): + """A context manager to add attributes to a trace for the configured backend. + + Args: + sample_index: Sample index for the trace. + step: Training step number. + rollout_n: Rollout number (for GRPO with multiple rollouts per sample). + name: Name for the trace span (used by mlflow backend). + validate: Whether this is a validation run. + trace: If False, disables tracing for the duration of the context. + """ + backend = RolloutTraceConfig.get_backend() + + should_skip = backend is not None and not trace + + if should_skip: + token = _trace_enabled.set(False) + try: + yield + finally: + _trace_enabled.reset(token) + return + + # Build attributes for the trace + attributes = {} + if backend: + if sample_index is not None: + attributes["sample_index"] = sample_index + if step is not None: + attributes["step"] = step + if rollout_n is not None: + attributes["rollout_n"] = rollout_n + attributes["validate"] = validate + attributes["experiment_name"] = RolloutTraceConfig.get_instance().experiment_name + + if not attributes or backend is None: + yield + return + + token = _trace_attributes.set(attributes) + if backend == "weave": + import weave + + try: + with weave.attributes(attributes): + yield + finally: + _trace_attributes.reset(token) + elif backend == "mlflow": + import mlflow + + try: + with mlflow.start_span(name=name) as span: + trace_id = span.trace_id + for key, value in attributes.items(): + mlflow.set_trace_tag(trace_id, str(key), str(value)) + yield + finally: + _trace_attributes.reset(token) + else: + try: + yield + finally: + _trace_attributes.reset(token) + + +def _json_trace_content(value): + if isinstance(value, BaseModel): + value = value.model_dump() + return json.dumps(value, default=str, ensure_ascii=False) + + +def _json_trace_metadata(value): + if isinstance(value, BaseModel): + value = value.model_dump() + if value is None or isinstance(value, str | int | float | bool): + return value + if isinstance(value, dict): + return {str(k): _json_trace_metadata(v) for k, v in value.items()} + if isinstance(value, list | tuple): + return [_json_trace_metadata(v) for v in value] + return str(value) + + +def _trackio_message_dict(message): + if not isinstance(message, dict): + return None + role = message.get("role") + if not isinstance(role, str): + return None + return dict(message) + + +def _trackio_output_dict(output): + if isinstance(output, BaseModel): + return output.model_dump() + if isinstance(output, dict): + return output + if hasattr(output, "__dict__"): + return dict(vars(output)) + return None + + +def _trackio_trace_key(op_name): + return "rollout_trace/" + "".join(char if char.isalnum() or char in "._-" else "_" for char in op_name) + + +def _trackio_trace_step(attributes): + step = attributes.get("step") + if step is None: + return None + try: + return int(step) + except (TypeError, ValueError): + return None + + +def _log_trackio_trace(op_name, inputs, output=None, exception=None): + trackio = RolloutTraceConfig.get_client() + attributes = _current_trace_attributes() + metadata_inputs = {key: value for key, value in inputs.items() if key != "messages"} + output_dict = _trackio_output_dict(output) + metadata = { + "op": op_name, + "backend": "trackio", + "experiment_name": RolloutTraceConfig.get_instance().experiment_name, + "inputs": _json_trace_metadata(metadata_inputs), + **{key: _json_trace_metadata(value) for key, value in attributes.items()}, + } + if exception is not None: + metadata["status"] = "error" + metadata["exception_type"] = type(exception).__name__ + else: + metadata["status"] = "success" + metadata["output"] = _json_trace_metadata(output_dict if output_dict is not None else output) + + messages = [] + input_messages = inputs.get("messages") if isinstance(inputs, dict) else None + if isinstance(input_messages, list): + messages = [ + message for message in (_trackio_message_dict(message) for message in input_messages) if message is not None + ] + + if not messages: + messages = [ + {"role": "system", "content": f"verl rollout trace operation: {op_name}"}, + {"role": "user", "content": _json_trace_content({"inputs": inputs})}, + ] + + if exception is not None: + messages.append( + { + "role": "assistant", + "content": _json_trace_content( + { + "exception_type": type(exception).__name__, + "exception": str(exception), + } + ), + } + ) + elif output_dict is not None and output_dict.get("response_text"): + messages.append({"role": "assistant", "content": str(output_dict["response_text"])}) + elif output_dict is not None and output_dict.get("answer"): + messages.append({"role": "assistant", "content": str(output_dict["answer"])}) + else: + messages.append({"role": "assistant", "content": _json_trace_content({"output": output})}) + + trackio.log( + {_trackio_trace_key(op_name): trackio.Trace(messages=messages, metadata=metadata)}, + step=_trackio_trace_step(attributes), + ) + + +def _current_trace_attributes(): + backend = RolloutTraceConfig.get_backend() + if backend == "weave": + from weave.trace.context import call_context + + return {**call_context.call_attributes.get()} + return {**(_trace_attributes.get() or {})} + + +def rollout_trace_op(func): + @functools.wraps(func) + async def async_wrapper(self, *args, **kwargs): + if not _trace_enabled.get(): + return await func(self, *args, **kwargs) + + backend = RolloutTraceConfig.get_backend() + enable_token2text = RolloutTraceConfig.enable_token2text() + if backend is None: + return await func(self, *args, **kwargs) + + sig = inspect.signature(func) + bound_args = sig.bind(self, *args, **kwargs) + bound_args.apply_defaults() + inputs = dict(bound_args.arguments) + del inputs["self"] + + async def add_token2text(self, result): + if hasattr(result, "prompt_ids") and hasattr(self, "tokenizer") and hasattr(self.tokenizer, "decode"): + # Use model_dump() for Pydantic models to get a proper copy, + # otherwise vars() returns a reference to internal __dict__ which + # can cause serialization issues with MLflow + if isinstance(result, BaseModel): + _result = result.model_dump() + else: + _result = dict(vars(result)) + loop = get_event_loop() + if hasattr(result, "prompt_ids"): + prompt_text = await loop.run_in_executor(None, self.tokenizer.decode, result.prompt_ids) + _result["prompt_text"] = prompt_text + + if hasattr(result, "response_ids"): + response_text = await loop.run_in_executor(None, self.tokenizer.decode, result.response_ids) + _result["response_text"] = response_text + return _result + return result + + if backend == "weave": + tracer = RolloutTraceConfig.get_client() + + cur_attributes = _current_trace_attributes() + call = tracer.create_call(op=func.__qualname__, inputs=inputs, attributes=cur_attributes) + try: + result = await func(self, *args, **kwargs) + + if enable_token2text: + _result = await add_token2text(self, result) + tracer.finish_call(call, output=_result) + else: + tracer.finish_call(call, output=result) + + return result + + except Exception as e: + tracer.finish_call(call, exception=e) + raise e + elif backend == "mlflow": + import mlflow + + with mlflow.start_span(name=func.__qualname__) as span: + span.set_inputs(inputs) + result = await func(self, *args, **kwargs) + if enable_token2text: + _result = await add_token2text(self, result) + span.set_outputs(_result) + else: + span.set_outputs(result) + + return result + elif backend == "trackio": + try: + result = await func(self, *args, **kwargs) + if enable_token2text: + _result = await add_token2text(self, result) + _log_trackio_trace(func.__qualname__, inputs, output=_result) + else: + _log_trackio_trace(func.__qualname__, inputs, output=result) + return result + except Exception as e: + _log_trackio_trace(func.__qualname__, inputs, exception=e) + raise e + + else: + return await func(self, *args, **kwargs) + + @functools.wraps(func) + def wrapper(self, *args, **kwargs): + if not _trace_enabled.get(): + return func(self, *args, **kwargs) + + backend = RolloutTraceConfig.get_backend() + if backend is None: + return func(self, *args, **kwargs) + + sig = inspect.signature(func) + bound_args = sig.bind(self, *args, **kwargs) + bound_args.apply_defaults() + inputs = dict(bound_args.arguments) + del inputs["self"] + + if backend == "weave": + tracer = RolloutTraceConfig.get_client() + + cur_attributes = _current_trace_attributes() + call = tracer.create_call(op=func.__qualname__, inputs=inputs, attributes=cur_attributes) + try: + result = func(self, *args, **kwargs) + tracer.finish_call(call, output=result) + return result + except Exception as e: + tracer.finish_call(call, exception=e) + raise e + elif backend == "mlflow": + import mlflow + + return mlflow.trace(func)(self, *args, **kwargs) + elif backend == "trackio": + try: + result = func(self, *args, **kwargs) + _log_trackio_trace(func.__qualname__, inputs, output=result) + return result + except Exception as e: + _log_trackio_trace(func.__qualname__, inputs, exception=e) + raise e + else: + return func(self, *args, **kwargs) + + return async_wrapper if inspect.iscoroutinefunction(func) else wrapper diff --git a/verl_0720_main/verl/verl/utils/seqlen_balancing.py b/verl_0720_main/verl/verl/utils/seqlen_balancing.py new file mode 100644 index 0000000000000000000000000000000000000000..4e172722a896ced6296143ed3d428bc0eaaa6187 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/seqlen_balancing.py @@ -0,0 +1,626 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import copy +import heapq +from itertools import chain + +import torch +from torch import distributed as dist + +from verl.protocol import DataProto +from verl.utils import tensordict_utils as tu +from verl.utils.device import get_device_name + + +def calculate_workload(seqlen_list: torch.Tensor) -> torch.Tensor: + """Calculate approximate computational workload for transformer attention. + + Estimates FLOPs for dense transformer blocks based on sequence length using + the formula: FLOPs ≈ 12 * hidden_size² * seqlen + 2 * hidden_size * seqlen² + + The constants are calibrated for a 7B model (hidden_size=4096), yielding: + workload ∝ 24576 * seqlen + seqlen² + + Args: + seqlen_list: Sequence lengths as a tensor. + + Returns: + torch.Tensor: Estimated workload values proportional to actual FLOPs. + + Note: + The returned values are relative workloads, not actual FLOP counts. + Useful for balancing computation across data parallel ranks. + """ + return 24576 * seqlen_list + seqlen_list**2 + + +def karmarkar_karp(seqlen_list: list[int], k_partitions: int, equal_size: bool) -> list[list[int]]: + """Partition items into k groups using the Karmarkar-Karp differencing method. + + Implements the Largest Differencing Method (LDM) algorithm for balanced + multi-way number partitioning. This heuristic produces near-optimal partitions + by iteratively combining the sets with the largest difference. + + Args: + seqlen_list: Values to partition (typically sequence lengths or workloads). + k_partitions: Number of partitions to create. + equal_size: If True, each partition will have exactly len(seqlen_list) / k_partitions + items. If False, partitions may have different sizes. + + Returns: + list[list[int]]: List of k partitions, each containing indices into seqlen_list. + + See Also: + https://en.wikipedia.org/wiki/Largest_differencing_method + + Note: + When equal_size=True, len(seqlen_list) must be divisible by k_partitions. + """ + + # see: https://en.wikipedia.org/wiki/Largest_differencing_method + class Set: + def __init__(self) -> None: + self.sum = 0 + self.items = [] + + def add(self, idx: int, val: int): + self.items.append((idx, val)) + self.sum += val + + def merge(self, other): + for idx, val in other.items: + self.items.append((idx, val)) + self.sum += val + + def __lt__(self, other): + if self.sum != other.sum: + return self.sum < other.sum + if len(self.items) != len(other.items): + return len(self.items) < len(other.items) + return self.items < other.items + + class State: + def __init__(self, items: list[tuple[int, int]], k: int) -> None: + self.k = k + # sets should always be decreasing order + self.sets = [Set() for _ in range(k)] + assert len(items) in [1, k], f"{len(items)} not in [1, {k}]" + for i, (idx, seqlen) in enumerate(items): + self.sets[i].add(idx=idx, val=seqlen) + self.sets = sorted(self.sets, reverse=True) + + def get_partitions(self): + partitions = [] + for i in range(len(self.sets)): + cur_partition = [] + for idx, _ in self.sets[i].items: + cur_partition.append(idx) + partitions.append(cur_partition) + return partitions + + def merge(self, other): + for i in range(self.k): + self.sets[i].merge(other.sets[self.k - 1 - i]) + self.sets = sorted(self.sets, reverse=True) + + @property + def spread(self) -> int: + return self.sets[0].sum - self.sets[-1].sum + + def __lt__(self, other): + # least heap, let the state with largest spread to be popped first, + # if the spread is the same, let the state who has the largest set + # to be popped first. + if self.spread != other.spread: + return self.spread > other.spread + return self.sets[0] > other.sets[0] + + def __repr__(self) -> str: + repr_str = "[" + for i in range(self.k): + if i > 0: + repr_str += "," + repr_str += "{" + for j, (_, seqlen) in enumerate(self.sets[i].items): + if j > 0: + repr_str += "," + repr_str += str(seqlen) + repr_str += "}" + repr_str += "]" + return repr_str + + sorted_seqlen_list = sorted([(seqlen, i) for i, seqlen in enumerate(seqlen_list)]) + states_pq = [] + if equal_size: + assert len(seqlen_list) % k_partitions == 0, f"{len(seqlen_list)} % {k_partitions} != 0" + for offset in range(0, len(sorted_seqlen_list), k_partitions): + items = [] + for i in range(k_partitions): + seqlen, idx = sorted_seqlen_list[offset + i] + items.append((idx, seqlen)) + heapq.heappush(states_pq, State(items=items, k=k_partitions)) + else: + for seqlen, idx in sorted_seqlen_list: + heapq.heappush(states_pq, State(items=[(idx, seqlen)], k=k_partitions)) + + while len(states_pq) > 1: + state0 = heapq.heappop(states_pq) + state1 = heapq.heappop(states_pq) + # merge states + state0.merge(state1) + heapq.heappush(states_pq, state0) + + final_state = states_pq[0] + partitions = final_state.get_partitions() + if equal_size: + for i, partition in enumerate(partitions): + assert len(partition) * k_partitions == len(seqlen_list), ( + f"{len(partition)} * {k_partitions} != {len(seqlen_list)}" + ) + return partitions + + +def greedy_partition(seqlen_list: list[int], k_partitions: int, equal_size: bool) -> list[list[int]]: + """Partition items into k groups using a greedy assignment strategy. + + Assigns each item to the partition with the smallest current sum, iterating + through items in order. Simpler but typically less optimal than Karmarkar-Karp. + + Args: + seqlen_list: Values to partition (typically sequence lengths or workloads). + k_partitions: Number of partitions to create. + equal_size: If True, adds a bias to ensure equal partition sizes. + Requires len(seqlen_list) to be divisible by k_partitions. + + Returns: + list[list[int]]: List of k partitions, each containing indices into seqlen_list. + + Note: + When equal_size=True, a large bias is added to encourage equal distribution + of items before considering the actual values. + """ + bias = sum(seqlen_list) + 1 if equal_size else 0 + sorted_seqlen = [(seqlen + bias, i) for i, seqlen in enumerate(seqlen_list)] + partitions = [[] for _ in range(k_partitions)] + partition_sums = [0 for _ in range(k_partitions)] + for seqlen, i in sorted_seqlen: + min_idx = None + for j in range(k_partitions): + if min_idx is None or partition_sums[j] < partition_sums[min_idx]: + min_idx = j + partitions[min_idx].append(i) + partition_sums[min_idx] += seqlen + if equal_size: + for i, partition in enumerate(partitions): + assert len(partition) * k_partitions == len(seqlen_list), ( + f"{len(partition)} * {k_partitions} != {len(seqlen_list)}" + ) + return partitions + + +def get_seqlen_balanced_partitions(seqlen_list: list[int], k_partitions: int, equal_size: bool): + """ + Calculates partitions of indices from seqlen_list such that the sum of sequence lengths + in each partition is balanced. Uses the Karmarkar-Karp differencing method. + + This is useful for balancing workload across devices or batches, especially when + dealing with variable sequence lengths. + + Args: + seqlen_list (List[int]): A list of sequence lengths for each item. + k_partitions (int): The desired number of partitions. + equal_size (bool): If True, ensures that each partition has the same number of items. + Requires len(seqlen_list) to be divisible by k_partitions. + If False, partitions can have varying numbers of items, focusing + only on balancing the sum of sequence lengths. + + Returns: + List[List[int]]: A list containing k_partitions lists. Each inner list contains the + original indices of the items assigned to that partition. The indices + within each partition list are sorted. + + Raises: + AssertionError: If len(seqlen_list) < k_partitions. + AssertionError: If equal_size is True and len(seqlen_list) is not divisible by k_partitions. + AssertionError: If any resulting partition is empty. + """ + assert len(seqlen_list) >= k_partitions, f"number of items:[{len(seqlen_list)}] < k_partitions:[{k_partitions}]" + + def _check_and_sort_partitions(partitions): + assert len(partitions) == k_partitions, f"{len(partitions)} != {k_partitions}" + seen_idx = set() + sorted_partitions = [None] * k_partitions + for i, partition in enumerate(partitions): + assert len(partition) > 0, f"the {i}-th partition is empty" + for idx in partition: + seen_idx.add(idx) + sorted_partitions[i] = sorted(partition) + assert seen_idx == set(range(len(seqlen_list))) + return sorted_partitions + + partitions = karmarkar_karp(seqlen_list=seqlen_list, k_partitions=k_partitions, equal_size=equal_size) + return _check_and_sort_partitions(partitions) + + +def log_seqlen_unbalance(seqlen_list: list[int], partitions: list[list[int]], prefix): + """ + Calculate and log metrics related to sequence length imbalance before and after partitioning. + + Args: + seqlen_list (List[int]): A list of sequence lengths for each item. + partitions (List[List[int]]): A list of partitions, where each inner list contains indices + from seqlen_list assigned to that partition. + prefix (str): A prefix to be added to each metric key in the returned dictionary. + + Returns: + dict: A dictionary containing metrics related to sequence length imbalance. + """ + # Get the number of partitions + k_partition = len(partitions) + # assert len(seqlen_list) % k_partition == 0 + batch_size = len(seqlen_list) // k_partition + min_sum_seqlen = None + max_sum_seqlen = None + total_sum_seqlen = 0 + + # Iterate over each batch of sequence lengths + for offset in range(0, len(seqlen_list), batch_size): + cur_sum_seqlen = sum(seqlen_list[offset : offset + batch_size]) + if min_sum_seqlen is None or cur_sum_seqlen < min_sum_seqlen: + min_sum_seqlen = cur_sum_seqlen + if max_sum_seqlen is None or cur_sum_seqlen > max_sum_seqlen: + max_sum_seqlen = cur_sum_seqlen + total_sum_seqlen += cur_sum_seqlen + + balanced_sum_seqlen_list = [] + for partition in partitions: + cur_sum_seqlen_balanced = sum([seqlen_list[i] for i in partition]) + balanced_sum_seqlen_list.append(cur_sum_seqlen_balanced) + # print("balanced_sum_seqlen_list: ", balanced_sum_seqlen_list) + min_sum_seqlen_balanced = min(balanced_sum_seqlen_list) + max_sum_seqlen_balanced = max(balanced_sum_seqlen_list) + + return { + f"{prefix}/min": min_sum_seqlen, + f"{prefix}/max": max_sum_seqlen, + f"{prefix}/minmax_diff": max_sum_seqlen - min_sum_seqlen, + f"{prefix}/balanced_min": min_sum_seqlen_balanced, + f"{prefix}/balanced_max": max_sum_seqlen_balanced, + f"{prefix}/mean": total_sum_seqlen / len(partitions), + } + + +def ceildiv(a: int, b: int) -> int: + """Compute ceiling division of a by b. + + Returns the smallest integer greater than or equal to a/b. + Uses the identity: ceil(a/b) = floor((a + b - 1) / b) = -(-a // b) + + Args: + a: Dividend (numerator). + b: Divisor (denominator), must be non-zero. + + Returns: + int: Ceiling of a divided by b. + + Example: + >>> ceildiv(7, 3) # ceil(7/3) = ceil(2.33) = 3 + 3 + >>> ceildiv(6, 3) # ceil(6/3) = ceil(2.0) = 2 + 2 + """ + return -(a // -b) + + +def roundup_divisible(a: int, b: int) -> int: + """Round up a to the nearest multiple of b. + + Returns the smallest multiple of b that is >= a. + + Args: + a: Value to round up. + b: Divisor to round to (must be positive). + + Returns: + int: Smallest multiple of b that is >= a. + + Example: + >>> roundup_divisible(7, 4) # nearest multiple of 4 >= 7 is 8 + 8 + >>> roundup_divisible(8, 4) # 8 is already a multiple of 4 + 8 + """ + return ((a + b - 1) // b) * b + + +def rearrange_micro_batches( + batch, + max_token_len, + dp_group=None, + num_batches_divided_by=None, + same_micro_num_in_dp=True, + min_num_micro_batch=None, + use_dynamic_bsz_balance=True, + force_group_size=1, +): + """ + Split a batch into micro-batches by total token count, with optional DP sync and padding. + + Args: + batch (TensorDict): must include "attention_mask" (B*S); other fields are sliced similarly. + max_token_len (int): max sum of attention_mask per micro-batch. + dp_group (optional): torch.distributed group for data-parallel sync. + num_batches_divided_by (optional): virtual pipeline parallel size, for megatron. + same_micro_num_in_dp (bool): if True and dp_group set, pad all ranks to the same count. + min_num_micro_batch (int, optional): force at least this many splits (pads empty ones). + use_dynamic_bsz_balance (bool, optional): balance the computational workload between micro-batches + force_group_size (int, optional): force consecutive samples to be in the same micro-batch (for RM training). + + Returns: + List[TensorDict]: the micro-batches. + List[List[int]]: index lists mapping each micro-batch back to original positions. + """ + # this is per local micro_bsz + input_ids = batch["input_ids"] + if input_ids.is_nested: + seq_len_effective: torch.Tensor = input_ids.offsets().diff() + max_seq_len = max(seq_len_effective) + else: + max_seq_len = batch["attention_mask"].shape[-1] + seq_len_effective: torch.Tensor = batch["attention_mask"].sum(dim=1) + + assert max_token_len >= max_seq_len, ( + f"max_token_len must be greater than the sequence length. Got {max_token_len=} and {max_seq_len=}" + ) + + # Validate force_group_size + batch_size = len(seq_len_effective) + assert batch_size % force_group_size == 0, ( + f"Batch size {batch_size} must be divisible by force_group_size {force_group_size}" + ) + + total_seqlen = seq_len_effective.sum().item() + # NOTE: num_microbatches <= batch_size, so take the min of this two. + # When force_group_size > 1, we work with groups instead of individual samples + num_groups = batch_size // force_group_size + num_micro_batches = min(num_groups, ceildiv(total_seqlen, max_token_len)) + if min_num_micro_batch is not None: + # used to support pp + num_micro_batches = max(min_num_micro_batch, num_micro_batches) + if dist.is_initialized() and same_micro_num_in_dp and dp_group is not None: + num_micro_batches = torch.tensor([num_micro_batches], device=get_device_name()) + dist.all_reduce(num_micro_batches, op=dist.ReduceOp.MAX, group=dp_group) + num_micro_batches = num_micro_batches.cpu().item() + if num_batches_divided_by is not None: + num_micro_batches = roundup_divisible(num_micro_batches, num_batches_divided_by) + + assert num_micro_batches <= num_groups + + # upcast to int64 to avoid potential overflow im `calculate_workload` computation. + seq_len_effective = seq_len_effective.long() + + # When force_group_size > 1, aggregate workloads by groups + if force_group_size > 1: + # Calculate workload for each group (sum of workloads of samples in the group) + workloads_per_sample = calculate_workload(seq_len_effective) + workloads_per_sample_grouped = workloads_per_sample.view(num_groups, force_group_size) + group_workloads = workloads_per_sample_grouped.sum(dim=1).cpu().tolist() + + # Partition groups instead of individual samples + micro_bsz_group_idx = get_seqlen_balanced_partitions(group_workloads, num_micro_batches, equal_size=False) + + # Convert group indices back to sample indices + micro_bsz_idx = [] + for group_partition in micro_bsz_group_idx: + sample_partition = [] + for group_idx in group_partition: + start_idx = group_idx * force_group_size + sample_partition.extend(range(start_idx, start_idx + force_group_size)) + micro_bsz_idx.append(sample_partition) + + workloads = group_workloads + else: + # Original logic for force_group_size == 1 + # note that seq_len_effective is a GPU tensor. We need to make it a list to avoid D2H! + workloads = calculate_workload(seq_len_effective).cpu().tolist() + micro_bsz_idx = get_seqlen_balanced_partitions(workloads, num_micro_batches, equal_size=False) + + if use_dynamic_bsz_balance: + # Use the sum of squared sequence lengths to approximate attention computation workload + if force_group_size > 1: + # For grouped samples, use group workloads for sorting + micro_bsz_idx.sort( + key=lambda partition: ( + sum(workloads[idx // force_group_size] for idx in partition[::force_group_size]), + partition[0] if partition else 0, + ), + reverse=True, + ) + else: + micro_bsz_idx.sort( + key=lambda partition: ( + sum(workloads[idx] for idx in partition), + partition[0] if partition else 0, + ), + reverse=True, + ) + # Place smaller micro-batches at both ends to reduce the bubbles exposed during the warm-up and cool-down. + micro_bsz_idx = micro_bsz_idx[::2][::-1] + micro_bsz_idx[1::2] + + micro_batches = [] + + for partition in micro_bsz_idx: + curr_micro_batch = tu.index_select_tensor_dict(batch, partition) + micro_batches.append(curr_micro_batch) + + return micro_batches, micro_bsz_idx + + +def get_reverse_idx(idx_map): + """ + Build the inverse of an index mapping. + + Args: + idx_map (Sequence[int]): Sequence where idx_map[i] = j. + + Returns: + List[int]: Inverse mapping list such that output[j] = i for each i. + """ + reverse_idx_map = copy.deepcopy(idx_map) + + for i, idx in enumerate(idx_map): + reverse_idx_map[idx] = i + + return reverse_idx_map + + +def prepare_dynamic_batch( + data: DataProto, + max_token_len: int, + dp_group=None, + num_batches_divided_by=None, + same_micro_num_in_dp=True, + min_num_micro_batch=None, + use_dynamic_bsz_balance=True, +) -> tuple[list[DataProto], list[list[int]]]: + """ + Prepare a batch for dynamic batching. + + Args: + data (DataProto): The input data. + max_token_len (int): The maximum token length for dynamic batching. + + Returns: + Tuple[List[DataProto], List[List[int]]]: A tuple containing a list of DataProto objects + and a list of index lists. + """ + batch, batch_idx_list = rearrange_micro_batches( + data.batch, + max_token_len=max_token_len, + dp_group=dp_group, + num_batches_divided_by=num_batches_divided_by, + same_micro_num_in_dp=same_micro_num_in_dp, + min_num_micro_batch=min_num_micro_batch, + use_dynamic_bsz_balance=use_dynamic_bsz_balance, + ) + micro_batches = [] + for i, batch_idx in enumerate(batch_idx_list): + tensors = dict(batch[i]) + non_tensors = {key: value[batch_idx] for key, value in data.non_tensor_batch.items()} + meta_info = copy.deepcopy(data.meta_info) + micro_batches.append(DataProto.from_dict(tensors, non_tensors, meta_info=meta_info)) + + return micro_batches, batch_idx_list + + +def restore_dynamic_batch(data: torch.Tensor, batch_idx_list: list[list[int]]) -> torch.Tensor: + """ + Restore a batch from dynamic batching. + + Args: + data (torch.Tensor): The input data. + batch_idx_list (List[List[int]]): The list of index lists. + + Returns: + torch.Tensor: The restored data. + """ + indices = list(chain.from_iterable(batch_idx_list)) + batch_size = data.shape[0] + assert len(indices) == batch_size, f"{len(indices)} vs. {batch_size}" + revert_indices = torch.tensor(get_reverse_idx(indices), dtype=torch.long) + + if data.is_nested: + data_lst = data.unbind() + tensors = [data_lst[i] for i in revert_indices] + reverted_data = torch.nested.as_nested_tensor(tensors, layout=torch.jagged) + else: + reverted_data = data[revert_indices] + + return reverted_data + + +def get_group_balanced_partitions( + seqlen_list: list[int], + uid_list: list, + k_partitions: int, +) -> list[list[int]]: + """ + Partition samples into k groups while keeping samples with the same uid together. + + Args: + seqlen_list: List of sequence lengths for each sample. + uid_list: List of uids identifying which samples share the same prefix. + Samples with the same uid will be kept together. + k_partitions: Number of partitions (typically world_size). + + Returns: + List of k lists, each containing sample indices assigned to that partition. + Samples with the same uid are guaranteed to be in the same partition. + """ + assert len(seqlen_list) == len(uid_list), "seqlen_list and uid_list must have same length" + + # Build groups: each group contains indices of samples with the same uid + # Assumes samples with same uid are contiguous + groups = [] # List of (group_indices, group_total_seqlen) + current_uid = None + current_indices = [] + current_seqlen = 0 + + for i, (seqlen, uid) in enumerate(zip(seqlen_list, uid_list, strict=False)): + if uid != current_uid: + if current_indices: + groups.append((current_indices, current_seqlen)) + current_uid = uid + current_indices = [i] + current_seqlen = seqlen + else: + current_indices.append(i) + current_seqlen += seqlen + + # Don't forget the last group + if current_indices: + groups.append((current_indices, current_seqlen)) + + num_groups = len(groups) + assert num_groups >= k_partitions, ( + f"Number of uid groups ({num_groups}) must be >= k_partitions ({k_partitions}). " + f"Consider reducing world_size or increasing batch_size." + ) + + # Calculate workload for each group (as integers for partitioning) + group_workloads = [] + for indices, total_seqlen in groups: + # Use sum of individual workloads for more accurate estimation + workload = sum(int(calculate_workload(torch.tensor([seqlen_list[i]])).item()) for i in indices) + group_workloads.append(workload) + + # Use Karmarkar-Karp to partition groups + # equal_size=True ensures each partition gets the same number of groups, + # which is required when each group has the same number of samples (rollout.n) + group_partitions = get_seqlen_balanced_partitions( + seqlen_list=group_workloads, + k_partitions=k_partitions, + equal_size=True, + ) + + # Convert group partitions to sample partitions + sample_partitions = [] + for group_partition in group_partitions: + sample_indices = [] + for group_idx in group_partition: + sample_indices.extend(groups[group_idx][0]) + sample_partitions.append(sorted(sample_indices)) + + return sample_partitions diff --git a/verl_0720_main/verl/verl/utils/sglang/__init__.py b/verl_0720_main/verl/verl/utils/sglang/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..3d5aee6bdcdd86a19e72d4a8e96dd3f6b9abc337 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/sglang/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/utils/sglang/sglang_fp8_utils.py b/verl_0720_main/verl/verl/utils/sglang/sglang_fp8_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..887d5dadd6eb457f7154a5ca4c742e41acf00191 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/sglang/sglang_fp8_utils.py @@ -0,0 +1,127 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import re +from collections.abc import Iterable +from typing import Any + +from verl.utils.fp8_utils import FP8QuantizerHelper + + +def _get_config_value(config: Any, key: str, default: Any = None) -> Any: + if config is None: + return default + get_value = getattr(config, "get", None) + if callable(get_value): + return get_value(key, default) + return getattr(config, key, default) + + +def _normalize_ignored_layers(ignored_layers: Any) -> list[str]: + if ignored_layers is None: + return [] + if isinstance(ignored_layers, str): + ignored_layers = ignored_layers.split(",") + elif not isinstance(ignored_layers, Iterable): + ignored_layers = [ignored_layers] + + normalized = [] + for layer in ignored_layers: + layer_name = str(layer).strip() + if layer_name: + normalized.append(layer_name) + return normalized + + +def _dedupe_layers(ignored_layers: Iterable[str]) -> list[str]: + seen = set() + deduped = [] + for layer in ignored_layers: + layer_lower = layer.lower() + if layer_lower in seen: + continue + seen.add(layer_lower) + deduped.append(layer) + return deduped + + +def _get_ignored_layers_from_env() -> list[str]: + return _normalize_ignored_layers(os.getenv("SGLANG_FP8_IGNORED_LAYERS")) + + +def get_sglang_fp8_ignored_layers(quant_config: Any = None) -> list[str]: + ignored_layers = [] + ignored_layers.extend(_normalize_ignored_layers(_get_config_value(quant_config, "ignored_layers"))) + ignored_layers.extend(_normalize_ignored_layers(_get_config_value(quant_config, "modules_to_not_convert"))) + ignored_layers.extend(_get_ignored_layers_from_env()) + return _dedupe_layers(ignored_layers) + + +def _matches_ignored_layer(param_name: str, ignored_layer: str) -> bool: + ignored_layer = ignored_layer.strip() + if not ignored_layer: + return False + + name = param_name.strip(".") + module_name = name[: -len(".weight")] if name.lower().endswith(".weight") else name + if ignored_layer.startswith("re:"): + pattern = ignored_layer[3:] + return any(re.match(pattern, candidate) for candidate in (name, module_name)) + + ignored_layer = ignored_layer.lower().strip(".") + name = name.lower() + module_name = module_name.lower() + for candidate in (name, module_name): + if candidate == ignored_layer: + return True + if candidate.startswith(f"{ignored_layer}."): + return True + if candidate.endswith(f".{ignored_layer}"): + return True + if f".{ignored_layer}." in f".{candidate}.": + return True + return False + + +def build_sglang_fp8_quant_config(hf_config: Any = None, ignored_layers: Any = None) -> dict[str, Any]: + """Build SGLang block-wise FP8 config shared by server init and weight sync.""" + fp8_quant_config = { + "activation_scheme": "dynamic", + "fmt": "e4m3", + "quant_method": "fp8", + "weight_block_size": [128, 128], + } + + hf_quant_config = _get_config_value(hf_config, "quantization_config") + merged_ignored_layers = get_sglang_fp8_ignored_layers(hf_quant_config) + merged_ignored_layers.extend(_normalize_ignored_layers(ignored_layers)) + merged_ignored_layers = _dedupe_layers(merged_ignored_layers) + if merged_ignored_layers: + fp8_quant_config["ignored_layers"] = merged_ignored_layers + + return fp8_quant_config + + +class SGLangFP8QuantizerHelper(FP8QuantizerHelper): + def __init__(self, quant_config): + super().__init__(quant_config) + self.ignored_layers = get_sglang_fp8_ignored_layers(quant_config) + + def should_quantize_param(self, param_name): + for ignored_layer in self.ignored_layers: + if _matches_ignored_layer(param_name, ignored_layer): + return False + return super().should_quantize_param(param_name) diff --git a/verl_0720_main/verl/verl/utils/skip/__init__.py b/verl_0720_main/verl/verl/utils/skip/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..c35af3150d1ae6117ca2845722bb9e259d42ec6b --- /dev/null +++ b/verl_0720_main/verl/verl/utils/skip/__init__.py @@ -0,0 +1,29 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .base_skip import BaseSkip +from .config import SkipManagerConfig + +# register skip +from .rollout_skip import AsyncRolloutSkip, RolloutSkip, RolloutTqSkip +from .skip_manager import SkipManager + +__all__ = [ + "BaseSkip", + "SkipManager", + "SkipManagerConfig", + "RolloutSkip", + "RolloutTqSkip", + "AsyncRolloutSkip", +] diff --git a/verl_0720_main/verl/verl/utils/skip/base_skip.py b/verl_0720_main/verl/verl/utils/skip/base_skip.py new file mode 100644 index 0000000000000000000000000000000000000000..8ee062258b0f0ba23b2f3f323622f618f5da6493 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/skip/base_skip.py @@ -0,0 +1,80 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from enum import Enum +from typing import Callable + + +class SkipAction(Enum): + CACHE = "cache" + # Replace the result with cached data + # ensuring that the output of each step is consistent with the non-skip version from the previous run, + # while relying on cached result in every steps. + REPEAT = "repeat" # repeat the result + # Replace with the latest cached results + # while relying on one cached result at least + RANDOM = "random" # random the result + EMPTY = "empty" # do thing, and return empty + + +class BaseSkip: + """Base class for skip. + + Implementations are shared per role via ``SkipManager.skip_instances`` and may receive + overlapping calls in async pipelines; avoid mutable per-request fields on ``self``. + + Args: + local_config: The local configuration object, refer to verl.utils.skip.SkipManagerConfig. + global_config: The global configuration object. + """ + + support_actions = [] + support_online_step = False + + def __init__(self, local_config, global_config): + self.action = SkipAction(local_config.action) + self.enable = local_config.enable + self.dump_dir = local_config.dump_dir + self.steps = local_config.steps + self.global_config = global_config + if self.action not in self.support_actions: + raise ValueError(f"Unsupported action: {self.action}. Supported actions are: {self.support_actions}") + + def is_enabled(self) -> bool: + return self.enable + + def meet_precondition(self, step: int, func: Callable, *args, **kwargs) -> bool: + raise NotImplementedError("meet_precondition is not implemented") + + def warp_function(self, step: int, func: Callable, *args, **kwargs): + raise NotImplementedError("warp_function is not implemented") + + def prepare_data(self, step: int, result, *args, **kwargs): + raise NotImplementedError("prepare_data is not implemented") + + def extract_step(self, *args, **kwargs): + raise NotImplementedError("extract_step is not implemented") + + +SKIP_REGISTRY: dict[str, type[BaseSkip]] = {} + + +def register_skip( + name: str, +) -> Callable[[type[BaseSkip]], type[BaseSkip]]: + def decorator(cls: type[BaseSkip]) -> type[BaseSkip]: + SKIP_REGISTRY[name] = cls + return cls + + return decorator diff --git a/verl_0720_main/verl/verl/utils/skip/config.py b/verl_0720_main/verl/verl/utils/skip/config.py new file mode 100644 index 0000000000000000000000000000000000000000..bd27f34c4220901c7c780f0c44b2a0800689a008 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/skip/config.py @@ -0,0 +1,77 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dataclasses import dataclass, field + +from verl.base_config import BaseConfig + + +@dataclass +class RolloutSkipConfig(BaseConfig): + """Config for rollout skip behavior.""" + + enable: bool = False + dump_dir: str = "~/.verl/rollout_dump" + steps: list[int] = field(default_factory=list) + action: str = "cache" # cache | repeat | random | empty, refer to SkipAction in base_skip.py + + def __post_init__(self) -> None: + assert isinstance(self.enable, bool), f"`enable` must be bool, got {type(self.enable)}" + assert isinstance(self.dump_dir, str), f"`dump_dir` must be str, got {type(self.dump_dir)}" + assert isinstance(self.steps, list), f"`steps` must be list[int], got {type(self.steps)}" + assert all(isinstance(step, int) for step in self.steps), "`steps` must contain int only" + assert self.action in {"cache", "repeat"}, f"`action` must be one of cache/repeat, got {self.action}" + + +@dataclass +class AsyncRolloutSkipConfig(BaseConfig): + """Config for rollout skip behavior.""" + + enable: bool = False + dump_dir: str = "~/.verl/rollout_dump" + steps: list[int] = field(default_factory=list) + action: str = "cache" # cache | repeat | random | empty, refer to SkipAction in base_skip.py + + def __post_init__(self) -> None: + assert isinstance(self.enable, bool), f"`enable` must be bool, got {type(self.enable)}" + assert isinstance(self.dump_dir, str), f"`dump_dir` must be str, got {type(self.dump_dir)}" + assert isinstance(self.steps, list), f"`steps` must be list[int], got {type(self.steps)}" + assert all(isinstance(step, int) for step in self.steps), "`steps` must contain int only" + assert self.action in {"cache", "repeat"}, f"`action` must be one of cache/repeat, got {self.action}" + + +@dataclass +class RolloutTqSkipConfig(BaseConfig): + """Config for V1 TransferQueue-based rollout skip behavior.""" + + enable: bool = False + dump_dir: str = "~/.verl/rollout_dump" + steps: list[int] = field(default_factory=list) + action: str = "cache" # cache | repeat, refer to SkipAction in base_skip.py + + def __post_init__(self) -> None: + assert isinstance(self.enable, bool), f"`enable` must be bool, got {type(self.enable)}" + assert isinstance(self.dump_dir, str), f"`dump_dir` must be str, got {type(self.dump_dir)}" + assert isinstance(self.steps, list), f"`steps` must be list[int], got {type(self.steps)}" + assert all(isinstance(step, int) for step in self.steps), "`steps` must contain int only" + assert self.action in {"cache", "repeat"}, f"`action` must be one of cache/repeat, got {self.action}" + + +@dataclass +class SkipManagerConfig(BaseConfig): + """Top-level config for skip modules.""" + + rollout: RolloutSkipConfig = field(default_factory=RolloutSkipConfig) + async_rollout: AsyncRolloutSkipConfig = field(default_factory=AsyncRolloutSkipConfig) + rollout_tq: RolloutTqSkipConfig = field(default_factory=RolloutTqSkipConfig) diff --git a/verl_0720_main/verl/verl/utils/skip/rollout_skip.py b/verl_0720_main/verl/verl/utils/skip/rollout_skip.py new file mode 100644 index 0000000000000000000000000000000000000000..8d8f6e8701af5c8407f14c4582e2ee21de8ab99f --- /dev/null +++ b/verl_0720_main/verl/verl/utils/skip/rollout_skip.py @@ -0,0 +1,497 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import json +from pathlib import Path +from typing import Callable + +import torch +from omegaconf import OmegaConf + +from verl.protocol import DataProto +from verl.utils.skip.base_skip import BaseSkip, SkipAction, register_skip + + +@register_skip("rollout") +class RolloutSkip(BaseSkip): + """RolloutSkip skips sequence generation during rollout by attempting to load previously dumped data.""" + + support_actions = [SkipAction.CACHE, SkipAction.REPEAT] + print_mark = "[RolloutSkip()] " + gen_batch_name = "gen_batch.dp" + meta_name = "meta.json" + + def __init__(self, local_config, global_config): + super().__init__(local_config, global_config) + # prepare experiment info + self.exp_name = global_config.trainer.get("experiment_name", "default_experiment_name") + self.project_name = global_config.trainer.get("project_name", "default_project_name") + self.n = int(OmegaConf.select(global_config, "actor_rollout_ref.rollout.n", default=0)) + gen_batch_size = OmegaConf.select(global_config, "data.gen_batch_size") + if gen_batch_size is None: + gen_batch_size = OmegaConf.select(global_config, "data.train_batch_size", default=0) + self.gbs = int(gen_batch_size) + self.response_length = OmegaConf.select(global_config, "data.max_response_length", default=0) + self.prompt_length = OmegaConf.select(global_config, "data.max_prompt_length", default=0) + + def meet_precondition(self, step: int, func: Callable, *args, **kwargs) -> bool: + if self.action == SkipAction.CACHE: + if not self._check_valid_step_path(self._get_step_dump_dir(step)): + print( + f"{self.print_mark}\033[33mNo dumped data found at step {step} " + f"from {self._get_project_dump_dir()}. " + f"The trainer will generate and dump the data for this step.\033[0m", + flush=True, + ) + return False + else: + return True + + elif self.action == SkipAction.REPEAT: + if self._find_latest_step(step) == -1: + print( + f"{self.print_mark}\033[33mNo dumped data found " + f"from {self._get_project_dump_dir()}. " + f"The trainer will generate and dump the data.\033[0m", + flush=True, + ) + return False + return True + return False + + def warp_function(self, step: int, func: Callable, *args, **kwargs): + """Load cached gen batch; ``*args``/``kwargs`` mirror the decorated call (e.g. ``self, prompts``).""" + if self.action == SkipAction.CACHE: + load_step = step + elif self.action == SkipAction.REPEAT: + load_step = self._find_latest_step(step) + if load_step == -1: + raise RuntimeError( + f"{self.print_mark}repeat action expected dumped data for step {step}, " + f"but none was found under {self._get_project_dump_dir()}" + ) + else: + load_step = step + step_dir = self._get_step_dump_dir(load_step) + gen_batch_path = step_dir.joinpath(self.gen_batch_name) + result = DataProto.load_from_disk(gen_batch_path) + print( + f"{self.print_mark}\033[33mLoad generate result at step {load_step} " + f"(request step {step}) from {gen_batch_path}\033[0m", + flush=True, + ) + return result + + def prepare_data(self, step: int, result, *args, **kwargs): + step_dir = self._get_step_dump_dir(step) + try: + step_dir.mkdir(parents=True, exist_ok=True) + result.save_to_disk(step_dir.joinpath(self.gen_batch_name)) + meta_path = step_dir.joinpath(self.meta_name) + meta_path.write_text(json.dumps({"global_steps": step})) + print( + f"{self.print_mark}\033[33mDump generate result at step {step} to {step_dir}\033[0m", + flush=True, + ) + except Exception as e: + print( + f"{self.print_mark}\033[31mFailed to dump generate result at step {step} to {step_dir}: {e}\033[0m", + flush=True, + ) + + def _get_project_dump_dir(self) -> Path: + dumped_dir = Path(self.dump_dir).expanduser().resolve() + sub_dir = ( + f"{self.exp_name}_{self.project_name}" + + f"/GBS{self.gbs}_N{self.n}_in{self.prompt_length}_out{self.response_length}" + ) + dumped_dir = dumped_dir.joinpath(sub_dir).absolute() + return dumped_dir + + def _get_step_dump_dir(self, step) -> Path: + return self._get_project_dump_dir().joinpath(f"{step}").absolute() + + def _check_valid_step_path(self, path: Path) -> bool: + if not path.is_dir(): + return False + gen_batch_path = path.joinpath(self.gen_batch_name) + meta_path = path.joinpath(self.meta_name) + return gen_batch_path.exists() and gen_batch_path.is_file() and meta_path.exists() and meta_path.is_file() + + def _get_available_steps(self) -> list[int]: + result: list[int] = [] + project_dir = self._get_project_dump_dir() + if not project_dir.is_dir(): + return result + for child in project_dir.iterdir(): + if not child.is_dir(): + continue + try: + step = int(child.name) + except ValueError: + continue + if not self._check_valid_step_path(child): + continue + result.append(step) + return sorted(result) + + def _find_latest_step(self, step: int) -> int: + """Prefer exact ready step, else max step < current, else min step > current; -1 if none.""" + if self._check_valid_step_path(self._get_step_dump_dir(step)): + return step + available = self._get_available_steps() + if not available: + return -1 + # try to find the closest step + smaller_steps = [this_step for this_step in available if this_step < step] + if smaller_steps: + return smaller_steps[-1] + larger_steps = [this_step for this_step in available if this_step > step] + if larger_steps: + return larger_steps[0] + return -1 + + +@register_skip("rollout_tq") +class RolloutTqSkip(RolloutSkip): + """Rollout skip for V1 TransferQueue-based trainer (``skip.rollout_tq``). + + Unlike V0's decorator pattern, V1's split architecture (submit prompts to TQ, + then sample trajectories) requires direct method calls from the trainer: + - Phase one (no cache): trainer calls ``prepare_data`` after ``replay_buffer.sample`` + - Phase two (cache exists): trainer calls ``load_dump_data`` in ``_add_batch_to_generate`` + + When ``parameter_sync_step > 1`` (separate async), one global step performs multiple + ``sample`` calls. Each mini-batch is saved to a separate inner sub-directory + ``{step}/{inner_idx}/`` so the full step is captured; on load all inner dirs are + merged. For ``parameter_sync_step == 1`` (sync / colocate async) the directory + structure is (``{step}/{0}/tq_batch.pt``). + """ + + print_mark = "[RolloutTqSkip()] " + tq_batch_name = "tq_batch.pt" + + def __init__(self, local_config, global_config): + super().__init__(local_config, global_config) + self.parameter_sync_step = int( + OmegaConf.select( + global_config, + f"trainer.v1.{global_config.trainer.v1.trainer_mode}.parameter_sync_step", + default=1, + ) + ) + + def _get_v1_inner_dir(self, step: int, inner_idx: int) -> Path: + """Return the dump directory for one mini-batch within a step.""" + return self._get_step_dump_dir(step) / str(inner_idx) + + def _check_valid_v1_step(self, step: int) -> bool: + """Check whether ALL inner dirs for *step* exist (complete cache).""" + return all( + self._check_valid_v1_step_path(self._get_v1_inner_dir(step, i)) for i in range(self.parameter_sync_step) + ) + + def _find_first_missing_inner(self, step: int) -> int: + """Return the first inner index whose dir does not yet exist.""" + for i in range(self.parameter_sync_step): + if not self._check_valid_v1_step_path(self._get_v1_inner_dir(step, i)): + return i + return -1 # all present + + def _check_valid_v1_step_path(self, path: Path) -> bool: + """Check whether a V1-format cached batch (``tq_batch.pt``) exists at *path*.""" + if not path.is_dir(): + return False + return (path / self.tq_batch_name).is_file() and (path / self.meta_name).is_file() + + def _get_available_steps_v1(self) -> list[int]: + """Return sorted list of steps that have V1-format cached data.""" + result: list[int] = [] + project_dir = self._get_project_dump_dir() + if not project_dir.is_dir(): + return result + for child in project_dir.iterdir(): + if not child.is_dir(): + continue + try: + step = int(child.name) + except ValueError: + continue + if not self._check_valid_v1_step(step): + continue + result.append(step) + return sorted(result) + + def _resolve_load_step_v1(self, step: int) -> int: + """Return the actual step to load from for V1 path, or -1 if none available. + + - ``cache``: exact step match + - ``repeat``: closest available step (prefer smaller, then larger) + """ + if self._check_valid_v1_step(step): + return step + if self.action == SkipAction.REPEAT: + available = self._get_available_steps_v1() + if not available: + return -1 + smaller = [s for s in available if s < step] + if smaller: + return smaller[-1] + larger = [s for s in available if s > step] + if larger: + return larger[0] + return -1 + + def has_v1_cache(self, step: int) -> bool: + """V1 phase check: whether cached TQ batch data exists for *step*. + + Returns True -> phase two (load from disk). + Returns False -> phase one (normal rollout + save). + """ + return self._resolve_load_step_v1(step) != -1 + + def meet_precondition(self, step: int, *args, **kwargs) -> bool: + """Phase two: whether cached data exists and should be loaded/injected.""" + return self.has_v1_cache(step) + + def should_save(self, step: int, partition_id: str = "train") -> bool: + """Phase one: whether the sampled batch should be saved to disk.""" + return partition_id == "train" and not self.has_v1_cache(step) + + def maybe_load_and_inject(self, step: int, new_prompt_uids: list[str], partition_id: str = "train") -> bool: + """Phase two: load cached data and inject into TransferQueue if cache exists. + + Convenience wrapper that combines ``meet_precondition`` + ``load_dump_data``. + Returns ``True`` if cached data was injected (caller should skip real rollout), + ``False`` otherwise. + + Args: + step: Current training step. + new_prompt_uids: Freshly generated uids for the current batch. + partition_id: TQ partition (``"train"`` or ``"val"``). + """ + if not self.meet_precondition(step): + print( + f"{self.print_mark}\033[33mNo cached data found for step {step}. " + f"The trainer will generate and dump the data.\033[0m", + flush=True, + ) + return False + self.load_dump_data( + step=step, + new_prompt_uids=new_prompt_uids, + n=self.n, + global_steps=step, + partition_id=partition_id, + ) + return True + + def prepare_data(self, step: int, batch, global_steps: int) -> None: + """Phase one: read batch from TransferQueue and save to disk. + + When ``parameter_sync_step > 1``, each ``sample`` call saves its mini-batch + to ``{step}/{inner_idx}/``. The first missing inner index is used so that + repeated calls within the same step fill successive sub-directories. + + Args: + step: Current training step (used as dump directory name). + batch: :class:`transfer_queue.KVBatchMeta` from ``ReplayBuffer.sample()``. + global_steps: Current ``global_steps`` for metadata. + """ + import transfer_queue as tq + + # Determine which inner index to save to (-1 means all present, shouldn't happen) + inner_idx = self._find_first_missing_inner(step) + if inner_idx == -1: + return + save_dir = self._get_v1_inner_dir(step, inner_idx) + save_dir.mkdir(parents=True, exist_ok=True) + + # Read all trajectory fields from TQ + data = tq.kv_batch_get(keys=batch.keys, partition_id=batch.partition_id) + + save_payload = { + "tensordict": data, + "tags": batch.tags, + "keys": list(batch.keys), + "global_steps": global_steps, + } + torch.save(save_payload, save_dir / self.tq_batch_name) + + meta_path = save_dir / self.meta_name + meta_path.write_text(json.dumps({"global_steps": global_steps, "num_trajectories": len(batch.keys)})) + print( + f"{self.print_mark}\033[33mDump TQ batch at step {step} " + f"({len(batch.keys)} trajectories) to {save_dir}\033[0m", + flush=True, + ) + + def load_dump_data( + self, + step: int, + new_prompt_uids: list[str], + n: int, + global_steps: int, + partition_id: str = "train", + ) -> None: + """Phase two: load cached data from disk and inject into TransferQueue. + + Maps saved trajectories to new prompt uids in order, preserving the + original key structure (``{uid}_{session_id}_{index}``) and GRPO group + composition (each prompt gets *n* trajectories from the same original group). + + Args: + step: Current training step. + new_prompt_uids: Freshly generated uids for the current batch. + n: Number of trajectories per prompt (``rollout.n``). + global_steps: Current ``global_steps``, used to override staleness tags. + partition_id: TQ partition (``"train"`` or ``"val"``). + """ + import transfer_queue as tq + + load_step = self._resolve_load_step_v1(step) + if load_step == -1: + raise FileNotFoundError( + f"{self.print_mark}No dump data found for step {step} under {self._get_project_dump_dir()}" + ) + + # Load all inner dirs and merge (parameter_sync_step == 1 → single dir) + old_keys: list = [] + old_tags: list = [] + data_list: list = [] + for inner_idx in range(self.parameter_sync_step): + inner_dir = self._get_v1_inner_dir(load_step, inner_idx) + payload = torch.load(inner_dir / self.tq_batch_name, weights_only=False) + data_list.append(payload["tensordict"]) + old_keys.extend(payload["keys"]) + old_tags.extend(payload["tags"]) + + if len(data_list) > 1: + from verl.utils.tensordict_utils import concat_tensordict + + data = concat_tensordict(data_list) + else: + data = data_list[0] + + # Group saved trajectories by parent uid. + # Key format: {uid}_{session_id}_{index}, uid is UUID4 (no underscores). + # ReplayBuffer.sample() does NOT guarantee keys are sorted by uid prefix, + # so we use a dict to group regardless of key ordering. + uid_to_indices: dict[str, list[int]] = {} + for idx, key in enumerate(old_keys): + parent_uid = key.split("_")[0] + uid_to_indices.setdefault(parent_uid, []).append(idx) + groups = list(uid_to_indices.values()) + + if not groups: + raise RuntimeError( + f"{self.print_mark}No trajectory groups found in cached data ({len(old_keys)} keys) at step {load_step}" + ) + + num_cached_groups = len(groups) + num_prompts = len(new_prompt_uids) + if num_cached_groups < num_prompts: + print( + f"{self.print_mark}\033[33mCached {num_cached_groups} prompt groups but need {num_prompts} " + f"prompts; will cycle through available groups to fill\033[0m", + flush=True, + ) + elif num_cached_groups > num_prompts: + print( + f"{self.print_mark}\033[33mCached {num_cached_groups} prompt groups but only need {num_prompts} " + f"prompts; using first {num_prompts} groups\033[0m", + flush=True, + ) + + # Build new keys/tags: map each new uid to a cached group. + # If group has fewer than *n* trajectories (some sessions failed), + # cycle within the group to fill *n* trajectories. + # If cached groups < new prompts, cycle through groups (modulo). + new_keys = [] + new_tags = [] + traj_indices: list[int] = [] + for prompt_idx, new_uid in enumerate(new_prompt_uids): + group = groups[prompt_idx % num_cached_groups] + for session_id in range(n): + traj_idx = group[session_id % len(group)] + traj_indices.append(traj_idx) + new_keys.append(f"{new_uid}_{session_id}_0") + tag = dict(old_tags[traj_idx]) + tag["global_steps"] = global_steps + tag["min_global_steps"] = global_steps + tag["max_global_steps"] = global_steps + tag.pop("is_prompt", None) + new_tags.append(tag) + + # NestedTensor (jagged prompts/responses) does not support indexing or slicing + # on dim=0. Use index_select_tensor_dict which unbinds, selects, and rebuilds. + from verl.utils.tensordict_utils import index_select_tensor_dict + + new_fields = index_select_tensor_dict(data, traj_indices) + + # Write trajectory data to TQ + tq.kv_batch_put( + keys=new_keys, + fields=new_fields, + tags=new_tags, + partition_id=partition_id, + ) + + # Mark prompt-level keys as finished so ReplayBuffer can pick them up immediately + prompt_tags = [{"is_prompt": True, "status": "finished", "global_steps": global_steps}] * len(new_prompt_uids) + tq.kv_batch_put( + keys=new_prompt_uids, + partition_id=partition_id, + tags=prompt_tags, + ) + + print( + f"{self.print_mark}\033[33mInjected {len(new_keys)} cached trajectories " + f"from step {load_step} ({len(new_prompt_uids)} prompts x {n}) " + f"into TQ partition '{partition_id}' at current step {step}\033[0m", + flush=True, + ) + + +def parse_async_rollout_sample_step(sample_id: str) -> int: + """Parse the prompt **feed index** embedded in ``uid_sample_{epoch}_{index}`` or ``sample_{epoch}_{index}``. + + The trailing integer is Rollouter ``global_steps`` at feed time: monotonic order in which + prompts are submitted to the async pipeline. It is **not** trainer ``global_steps``, parameter + sync version, or guaranteed completion order under concurrent rollout. + """ + # Strip optional "uid_" prefix (set in non_tensor_batch["uid"]) + if sample_id.startswith("uid_"): + sample_id = sample_id[4:] + parts = sample_id.split("_") + if len(parts) != 3 or parts[0] != "sample": + raise ValueError(f"Invalid async rollout sample_id: {sample_id!r}, expected sample__") + return int(parts[-1]) + + +@register_skip("async_rollout") +class AsyncRolloutSkip(RolloutSkip): + """Rollout skip for fully async policy (``skip.async_rollout``).""" + + support_online_step = True + + def extract_step(self, *args, **kwargs) -> int: + # generate_sequences_single(self, prompts) + # sample_id is embedded in prompts.non_tensor_batch["uid"] + prompts = args[1] if len(args) > 1 else kwargs.get("prompts") + if prompts is None: + raise ValueError("async_rollout extract_step expects prompts as the second argument") + uid_array = prompts.non_tensor_batch.get("uid") + if uid_array is None or len(uid_array) == 0: + raise ValueError("async_rollout extract_step expects uid in prompts.non_tensor_batch") + return parse_async_rollout_sample_step(str(uid_array[0])) diff --git a/verl_0720_main/verl/verl/utils/skip/skip_manager.py b/verl_0720_main/verl/verl/utils/skip/skip_manager.py new file mode 100644 index 0000000000000000000000000000000000000000..3e481ec77ed0238d08c96d7feda0bd10786cd9ee --- /dev/null +++ b/verl_0720_main/verl/verl/utils/skip/skip_manager.py @@ -0,0 +1,190 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import functools +import inspect +from typing import Callable + +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.skip.base_skip import SKIP_REGISTRY +from verl.utils.skip.config import SkipManagerConfig + + +class SkipManager: + """SkipManager is a manager for skip. + + Class attributes default here so code paths (e.g. tests or modules that only import + ``@SkipManager.annotate``) work **before** ``SkipManager.init(config)`` runs — decorators + then no-op until the trainer initializes skip state. + """ + + config: SkipManagerConfig | None = None + step: int = -1 + # This step is shared across all skip_instances + # Different enabled skip_instances (no online step acquisition) shall use the same step definition. + skip_instances: dict = {} # noqa: RUF012 — intentionally mutable class defaults, reset in ``init`` + + @classmethod + def init(cls, config): + cls.config = omega_conf_to_dataclass(config.skip, dataclass_type=SkipManagerConfig) + cls.step = -1 + cls.skip_instances = {} + for name, skip_cls in SKIP_REGISTRY.items(): + local_cfg = getattr(cls.config, name, None) + if local_cfg is None: + continue + instance = skip_cls(local_cfg, config) + cls.skip_instances[name] = instance + + @classmethod + def set_step(cls, step: int): + cls.step = step + + @staticmethod + def _get_prompts_batch(args, kwargs): + """Resolve ``DataProto`` from decorated ``generate_sequences(self, prompts, ...)`` calls.""" + prompts = kwargs.get("prompts") + if prompts is not None: + return prompts + if len(args) > 1: + return args[1] + if len(args) == 1 and hasattr(args[0], "meta_info"): + return args[0] + return None + + @classmethod + def _should_bypass_for_validation(cls, args, kwargs) -> bool: + prompts = cls._get_prompts_batch(args, kwargs) + if prompts is None and len(args) > 1: + return cls._should_bypass_for_validation_tensordict(args, kwargs) + if prompts is None: + return False + if hasattr(prompts, "non_tensor_data"): + return cls._should_bypass_for_validation_tensordict(args, kwargs) + meta_info = getattr(prompts, "meta_info", None) or {} + return bool(meta_info.get("validate", False)) + + @classmethod + def annotate(cls, role: str, **kwargs_outer) -> Callable: + def decorator(func: Callable) -> Callable: + if inspect.iscoroutinefunction(func): + + @functools.wraps(func) + async def async_wrapper(*args, **kwargs_inner): + if cls._should_bypass_for_validation(args, kwargs_inner): + return await func(*args, **kwargs_inner) + skip_instance = cls.skip_instances.get(role) + if skip_instance is None or not skip_instance.is_enabled(): + return await func(*args, **kwargs_inner) + if skip_instance.support_online_step: + step = skip_instance.extract_step(*args, **kwargs_inner) + else: + step = cls.step + if step not in skip_instance.steps: + return await func(*args, **kwargs_inner) + if skip_instance.meet_precondition(step, func, *args, **kwargs_inner): + return skip_instance.warp_function(step, func, *args, **kwargs_inner) + result = await func(*args, **kwargs_inner) + skip_instance.prepare_data(step, result, *args, **kwargs_inner) + return result + + return async_wrapper + + @functools.wraps(func) + def sync_wrapper(*args, **kwargs_inner): + if cls._should_bypass_for_validation(args, kwargs_inner): + return func(*args, **kwargs_inner) + skip_instance = cls.skip_instances.get(role) + if skip_instance is None or not skip_instance.is_enabled(): + return func(*args, **kwargs_inner) + if skip_instance.support_online_step: + step = skip_instance.extract_step(*args, **kwargs_inner) + else: + step = cls.step + if step not in skip_instance.steps: + return func(*args, **kwargs_inner) + if skip_instance.meet_precondition(step, func, *args, **kwargs_inner): + return skip_instance.warp_function(step, func, *args, **kwargs_inner) + result = func(*args, **kwargs_inner) + skip_instance.prepare_data(step, result, *args, **kwargs_inner) + return result + + return sync_wrapper + + return decorator + + @classmethod + def annotate_tq(cls, role: str, phase: str): + """V1 TransferQueue-based skip decorator, unified across both phases. + + V1's split architecture separates "submit prompts" (``_add_batch_to_generate``) + from "sample trajectories" (``ReplayBuffer.sample``). This decorator handles + both, selected by *phase*: + + - ``phase="submit"``: decorate ``_add_batch_to_generate``. The method must be + split into ``_next_train_batch`` (dataloader + uid) and ``_submit_batch_to_rollout`` + (tag registration + generate_sequences). The decorator always calls + ``_next_train_batch`` first (keeping the dataloader aligned even on cache-hit + steps), then checks ``meet_precondition``. On cache-hit it injects cached data + into the TQ and returns; on cache-miss it calls ``_submit_batch_to_rollout``. + + - ``phase="sample"``: decorate ``ReplayBuffer.sample``. After the original + ``sample`` runs, if the skip instance says to save, persist the result to disk + (phase one / cache-miss). When ``parameter_sync_step > 1`` (separate async), + ``sample`` is called multiple times per step; each call saves its mini-batch to + a separate inner sub-directory, and the full set is merged on load. + """ + + def decorator(func: Callable) -> Callable: + @functools.wraps(func) + def wrapper(self, *args, **kwargs): + skip_instance = cls.skip_instances.get(role) + if skip_instance is None or not skip_instance.is_enabled(): + return func(self, *args, **kwargs) + step = cls.step + if step not in skip_instance.steps: + return func(self, *args, **kwargs) + + if phase == "submit": + # Always prepare batch (keeps dataloader aligned on cache-hit steps) + batch = self._next_train_batch() + if skip_instance.maybe_load_and_inject(step, list(batch["uid"])): + return + self._submit_batch_to_rollout(batch) + return + + # phase == "sample": post-save after original sample runs + result = func(self, *args, **kwargs) + # ReplayBuffer.sample returns (batch, off_policy_metrics) + batch = result[0] if isinstance(result, tuple) else result + if skip_instance.should_save(step, batch.partition_id): + skip_instance.prepare_data(step=step, batch=batch, global_steps=step) + return result + + return wrapper + + return decorator + + @staticmethod + def _should_bypass_for_validation_tensordict(args, kwargs) -> bool: + """Check ``validate`` flag in TensorDict's non_tensor_data for V1 path.""" + prompts = kwargs.get("prompts", None) + if prompts is None and len(args) > 1: + prompts = args[1] + if prompts is None: + return False + try: + return bool(getattr(prompts, "non_tensor_data", {}).get("validate", False)) + except Exception: + return False diff --git a/verl_0720_main/verl/verl/utils/tensordict_utils.py b/verl_0720_main/verl/verl/utils/tensordict_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..91d82b15ffd7990c5e50107f3882d83f1558c604 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tensordict_utils.py @@ -0,0 +1,950 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +from typing import Any, Iterable + +import numpy as np +import tensordict +import torch +from packaging.version import parse as parse_version +from tensordict import TensorDict +from tensordict.tensorclass import NonTensorData, NonTensorStack + + +def assign_non_tensor_data(tensor_dict: TensorDict, key, val): + """Assign a single non-tensor value to a TensorDict. + + Wraps the value in NonTensorData so it can be stored alongside tensors + in the TensorDict. Use this for scalar metadata or simple non-tensor values. + + Args: + tensor_dict: The TensorDict to assign to. + key: The key under which to store the value. + val: Any non-tensor value to store (e.g., string, int, dict). + + Raises: + AssertionError: If tensor_dict is not a TensorDict. + + Example: + >>> td = TensorDict({"obs": torch.randn(3, 4)}, batch_size=[3]) + >>> assign_non_tensor_data(td, "experiment_name", "run_001") + """ + assert isinstance(tensor_dict, TensorDict), "input dict must be a TensorDict" + tensor_dict[key] = NonTensorData(val) + + +def assign_non_tensor_stack(tensor_dict: TensorDict, key, val: list): + """Assign a list with potentially nested structures (lists, dicts, etc.) to TensorDict. + + This function handles complex nested data structures like: + - Lists of lists: [[], [0.5, 0.8], [0.9]] + - Lists of dicts: [{"acc": 1.0}, {"acc": 0.0}] + - Lists of lists of dicts: [[{"content": "...", "role": "user"}]] + + These structures are wrapped in NonTensorStack so TensorDict can handle them correctly. + + Args: + tensor_dict: The TensorDict to assign to + key: The key to assign the value under + val: A list containing potentially nested structures + + Example: + >>> td = TensorDict({}, batch_size=[]) + >>> turn_scores = [[], [0.5, 0.8], [0.9]] + >>> assign_non_tensor_stack(td, "turn_scores", turn_scores) + >>> # Now td["turn_scores"] contains the nested data + """ + # Convert list to NonTensorStack to handle nested structures + # This wraps each item in NonTensorData to preserve complex objects + # TODO(petersh6): can convert back to val directly if we are not accessing .data from the NonTensorStack + assert isinstance(tensor_dict, TensorDict), "input dict must be a TensorDict" + tensor_dict[key] = NonTensorStack.from_list([NonTensorData(item) for item in val]) + + +def assign_non_tensor(tensor_dict: TensorDict, **kwargs): + """Assign non-tensor data to a TensorDict. + + Automatically detects if the value is a list with nested structures and uses + the appropriate assignment method (NonTensorData for simple values, + NonTensorStack for lists with nested structures). + + Args: + tensor_dict: The TensorDict to assign to + **kwargs: Key-value pairs where values can be: + - Simple values (stored as NonTensorData) + - Lists with nested structures (stored as NonTensorStack) + + Example: + >>> td = TensorDict({"obs": torch.randn(3, 4)}, batch_size=[3]) + >>> assign_non_tensor( + ... tensor_dict=td, + ... metadata="experiment_1", # Simple value + ... turn_scores=[[], [0.5, 0.8], [0.9]] # Nested list + ... ) + """ + assert isinstance(tensor_dict, TensorDict), "input dict must be a TensorDict" + for key, val in kwargs.items(): + if isinstance(val, (NonTensorData | NonTensorStack)): + tensor_dict[key] = val + elif isinstance(val, list): + # For lists, use NonTensorStack + assign_non_tensor_stack(tensor_dict=tensor_dict, key=key, val=val) + else: + # For non-list values, use NonTensorData + assign_non_tensor_data(tensor_dict=tensor_dict, key=key, val=val) + return tensor_dict + + +def unwrap_non_tensor_data(data): + """Unwrap a NonTensorData object to get the underlying value. + + If the input is a NonTensorData wrapper, extracts and returns the + underlying data. Otherwise, returns the input unchanged. + + Args: + data: Either a NonTensorData object or any other value. + + Returns: + The unwrapped data if input was NonTensorData, otherwise the + original input unchanged. + + Example: + >>> wrapped = NonTensorData("hello") + >>> unwrap_non_tensor_data(wrapped) + 'hello' + >>> unwrap_non_tensor_data(42) # Non-wrapped value + 42 + """ + if isinstance(data, NonTensorData): + return data.data + return data + + +def get_non_tensor_data(data: TensorDict, key: str, default): + """Retrieve and unwrap non-tensor data from a TensorDict. + + Fetches the value for the given key from the TensorDict and automatically + unwraps it if it's stored as NonTensorData. + + Args: + data: The TensorDict to retrieve from. + key: The key to look up. + default: Value to return if the key is not found. + + Returns: + The unwrapped value if the key exists and was wrapped in NonTensorData, + the raw value if it wasn't wrapped, or the default if key not found. + + Example: + >>> td = TensorDict({}, batch_size=[]) + >>> assign_non_tensor_data(td, "config", {"lr": 0.01}) + >>> get_non_tensor_data(td, "config", None) + {'lr': 0.01} + >>> get_non_tensor_data(td, "missing", "default_value") + 'default_value' + """ + output = data.get(key, default) + return unwrap_non_tensor_data(output) + + +def nested_tensor_from_tensor_list(tensors: list[torch.Tensor], ragged_idx: int | None = None) -> torch.Tensor: + assert len(tensors) > 0, "Must provide at least one tensor" + sample_dim = tensors[0].dim() + if ragged_idx is None: + ragged_idx = sample_dim + assert 1 <= ragged_idx <= sample_dim, ( + f"ragged_idx must be in [1, {sample_dim}]. Got {ragged_idx=} and {sample_dim=}" + ) + + if sample_dim == 1: + return torch.nested.as_nested_tensor(tensors, layout=torch.jagged) + + cat_dim = ragged_idx - 1 + values = torch.cat(tensors, dim=cat_dim) + lengths = torch.tensor([tensor.shape[cat_dim] for tensor in tensors], dtype=torch.long, device=values.device) + offsets = torch.zeros(len(tensors) + 1, dtype=torch.long, device=values.device) + torch.cumsum(lengths, dim=0, out=offsets[1:]) + + nested_tensor = torch.nested.nested_tensor_from_jagged(values=values, offsets=offsets) + nested_tensor._ragged_idx = ragged_idx + return nested_tensor + + +def concat_nested_tensors(tensors: list[torch.Tensor]) -> torch.Tensor: + """Concatenate multiple nested tensors along the batch dimension. + + Takes a list of nested tensors with jagged layout and concatenates them + into a single nested tensor. Each input tensor must have 2 or more dimensions and be contiguous. + + Args: + tensors: List of nested tensors to concatenate. All tensors must + be nested, contiguous, and have 2 or more dimensions. + + Returns: + A new nested tensor with jagged layout containing all rows from + the input tensors concatenated along dimension 0. + + Raises: + AssertionError: If any tensor is not nested, not contiguous, or + doesn't have 2 or more dimensions. + + Example: + >>> t1 = torch.nested.as_nested_tensor([torch.randn(3), torch.randn(5)], layout=torch.jagged) + >>> t2 = torch.nested.as_nested_tensor([torch.randn(2), torch.randn(4)], layout=torch.jagged) + >>> result = concat_nested_tensors([t1, t2]) + >>> # result contains 4 rows: lengths [3, 5, 2, 4] + """ + for tensor in tensors: + assert tensor.is_nested and tensor.is_contiguous() + unbind_tensors = [] + for tensor in tensors: + assert len(tensor.shape) >= 2, f"nested tensor must have 2 or more dimensions. Got {tensor.shape}" + unbind_tensor = tensor.unbind(0) + unbind_tensors.extend(list(unbind_tensor)) + + ragged_idx = getattr(tensors[0], "_ragged_idx", tensors[0].dim() - 1) + return nested_tensor_from_tensor_list(unbind_tensors, ragged_idx=ragged_idx) + + +def concat_tensordict_with_none_bsz(data: list[TensorDict]): + """Handle concatenation of TensorDicts with empty batch size. + + For TensorDicts that contain only metadata (NonTensorData) with no batch + dimension, returns the first TensorDict as the concatenation result. + + Args: + data: List of TensorDicts, each with empty batch_size (batch_size=[]). + + Returns: + The first TensorDict from the list, as metadata concatenation + simply preserves the first instance. + + Raises: + AssertionError: If any TensorDict has a non-empty batch_size. + + Note: + This is used internally by concat_tensordict when handling + TensorDicts that contain only non-tensor metadata. + """ + for d in data: + assert len(d.batch_size) == 0 + # directly return the first meta info + return data[0] + + +def concat_tensordict(data: list[TensorDict]) -> TensorDict: + """Concatenate multiple TensorDicts along dimension zero. + + Combines a list of TensorDicts into a single TensorDict by concatenating + all tensors along the batch dimension (dim=0). Handles nested tensors + specially by unbinding and rebinding them. + + Args: + data: List of TensorDicts to concatenate. All TensorDicts must have + the same keys and the same set of nested tensor keys. + + Returns: + A new TensorDict containing concatenated tensors from all inputs. + + Raises: + AssertionError: If data is empty or if TensorDicts have inconsistent + nested tensor keys. + + Note: + - For TensorDicts with empty batch_size, returns the first one + - Nested tensors are handled specially via concat_nested_tensors + - Regular tensors use TensorDict.cat for efficient concatenation + """ + assert len(data) > 0, "Must have at least one tensordict" + + # Find nested tensor keys from the first tensordict + nested_tensor_keys = {key for key, value in data[0].items() if isinstance(value, torch.Tensor) and value.is_nested} + + if not nested_tensor_keys: + if len(data[0].batch_size) == 0: + return concat_tensordict_with_none_bsz(data) + # if batch size is None (only contain NonTensorData) + return TensorDict.cat(data, dim=0) + + # Create a list of tensordicts containing only non-nested tensors for concatenation + regular_tds = [] + for td in data: + current_nested_keys = {k for k, v in td.items() if isinstance(v, torch.Tensor) and v.is_nested} + assert current_nested_keys == nested_tensor_keys, "All tensordicts must have the same set of nested tensors." + + # Create a new TensorDict with non-nested items without modifying the original + regular_items = {k: v for k, v in td.items() if k not in nested_tensor_keys} + regular_tds.append(TensorDict(regular_items, batch_size=td.batch_size, device=td.device)) + + # Concatenate the regular tensordicts + output = TensorDict.cat(regular_tds, dim=0) + + # Concatenate and add nested tensors to the output + for key in nested_tensor_keys: + nested_tensors_to_concat = [td[key] for td in data] + output[key] = concat_nested_tensors(nested_tensors_to_concat) + + return output + + +def chunk_tensordict(td: TensorDict, chunks: int) -> list[TensorDict]: + """Split a TensorDict into equal-sized chunks with special nested tensor handling. + + Divides a TensorDict into the specified number of chunks along the batch + dimension. Handles NestedTensors specially since TensorDict.chunk() doesn't + support jagged tensors. + + Args: + td: The TensorDict to split. + chunks: Number of chunks to create. Must evenly divide len(td). + + Returns: + List of TensorDicts, each containing a portion of the original data. + + Raises: + AssertionError: If td is not a TensorDict or if its length is not + evenly divisible by chunks. + + Note: + PyTorch ``unbind(dim=0)`` on 3D+ jagged NestedTensors has a bug where + ``split_with_sizes`` is applied to the wrong dimension of the internal + ``_values`` tensor. For example, mRoPE ``position_ids`` with per-sample + shape ``(4, seq_len)`` becomes a 3D jagged NestedTensor + ``[B, *(ragged=4), seq_len]``; ``_values`` is ``[B*4, seq_len]`` and + ``unbind`` erroneously splits dimension 1 (``seq_len``) instead of + dimension 0, causing:: + + RuntimeError: split_with_sizes expects split_sizes to sum exactly + to , but got split_sizes=[4, 4, ...] + + 2D jagged NestedTensors (e.g. ``input_ids``, ``loss_mask``) are + unaffected — ``unbind(dim=0)`` works correctly for them. + + The workaround: try ``unbind`` first (fast path for 2D); on failure, + fall back to ``to_padded_tensor`` → ``chunk`` → reconstruct per-chunk + NestedTensors using the original ragged lengths from ``offsets``. + + See https://github.com/pytorch/pytorch/issues/153238 + """ + assert isinstance(td, TensorDict) and len(td) % chunks == 0, ( + f"expecting td with length divisible by chunks, but got {len(td)} and {chunks}" + ) + chunk_size = len(td) // chunks + nested_keys = {key for key, val in td.items() if isinstance(val, torch.Tensor) and val.is_nested} + new_td = TensorDict( + {k: v for k, v in td.items() if k not in nested_keys}, batch_size=td.batch_size, device=td.device + ) + + tds = new_td.chunk(chunks=chunks) + for key in nested_keys: + nt = td[key] + try: + tensors = nt.unbind(dim=0) + except RuntimeError: + padded = nt.to_padded_tensor(0) + padded_chunks = padded.chunk(chunks, dim=0) + offsets = nt.offsets() + lengths = offsets.diff().tolist() + for i, chunk_td in enumerate(tds): + chunk_lengths = lengths[i * chunk_size : (i + 1) * chunk_size] + chunk_tensors = [padded_chunks[i][j, :seq_len] for j, seq_len in enumerate(chunk_lengths)] + chunk_td[key] = nested_tensor_from_tensor_list( + chunk_tensors, ragged_idx=getattr(nt, "_ragged_idx", nt.dim() - 1) + ) + continue + + for i, chunk_td in enumerate(tds): + chunk_td[key] = nested_tensor_from_tensor_list( + list(tensors[i * chunk_size : (i + 1) * chunk_size]), + ragged_idx=getattr(nt, "_ragged_idx", nt.dim() - 1), + ) + + return tds + + +def get_tensordict(tensor_dict: dict[str, torch.Tensor | list], non_tensor_dict: dict = None) -> TensorDict: + """Create a TensorDict from tensors and non-tensor data. + + Automatically handles nested structures in lists by converting them to NonTensorStack. + This enables support for: + - Lists of lists: [[], [0.5, 0.8], [0.9]] + - Lists of dicts: [{"acc": 1.0}, {"acc": 0.0}] + - Lists of lists of dicts: [[{"content": "...", "role": "user"}]] + + Args: + tensor_dict: Dictionary of tensors and lists to include in the TensorDict + non_tensor_dict: Dictionary of metadata to store as NonTensorData + + Returns: + TensorDict with proper handling of nested structures + + Example: + >>> td = get_tensordict( + ... tensor_dict={ + ... "obs": torch.randn(3, 4), + ... "turn_scores": [[], [0.5, 0.8], [0.9]] # Nested list + ... }, + ... non_tensor_dict={"experiment": "test"} + ... ) + """ + tensor_dict = tensor_dict.copy() + if non_tensor_dict is None: + non_tensor_dict = {} + + batch_size = None + + for key, val in tensor_dict.items(): + if isinstance(val, torch.Tensor) and val.is_nested: + assert val.is_contiguous(), "Nested tensors must be contiguous. Try setting layout=torch.jagged" + assert val.layout == torch.jagged, "Nested tensors must be jagged." + + # Skip validation for NonTensorStack as it's already properly formatted + if isinstance(val, NonTensorStack): + if batch_size is None: + batch_size = len(val) + else: + assert len(val) == batch_size, ( + f"Batch size of NonTensorStack {key} is not consistent with other tensors. " + f"Expected {batch_size}, got {len(val)}" + ) + continue + + if isinstance(val, list | np.ndarray): + for v in val: + assert not isinstance(v, torch.Tensor), ( + "Passing a list makes the data NonTensorStack, " + "which doesn't support torch.Tensor. Please convert to numpy first" + ) + # Convert to NonTensorStack to handle nested structures + tensor_dict[key] = NonTensorStack.from_list([NonTensorData(item) for item in val]) + + assert isinstance(val, torch.Tensor | list | np.ndarray), ( + f"{key} -> {type(val)} isn't of 'torch.Tensor | list | np.ndarray' type" + ) + + if batch_size is None: + batch_size = val.size(0) if isinstance(val, torch.Tensor) else len(val) + else: + val_batch_size = val.size(0) if isinstance(val, torch.Tensor) else len(val) + assert val_batch_size == batch_size, ( + f"Batch size of tensor {key} is not consistent with other tensors. " + f"Expected {batch_size}, got {val_batch_size}" + ) + + if batch_size is None: + batch_size = [] + else: + batch_size = [batch_size] + + for key, val in non_tensor_dict.items(): + assert key not in tensor_dict + tensor_dict[key] = NonTensorData(val) + + return TensorDict(source=tensor_dict, batch_size=batch_size) + + +def index_select_tensor_dict(batch: TensorDict, indices: torch.Tensor | list[int]) -> TensorDict: + """Select rows from a TensorDict using indices. + + Creates a new TensorDict containing only the rows specified by indices. + Handles regular tensors, nested tensors, NonTensorStack, and NonTensorData + appropriately. + + Args: + batch: The TensorDict to index into. Can be None. + indices: 1D tensor or list of integers specifying which rows to select. + + Returns: + A new TensorDict containing only the selected rows, or None if + batch was None. + + Raises: + AssertionError: If indices is not 1-dimensional. + + Note: + - Regular tensors are indexed directly + - Nested tensors are unbound, indexed, and rebound + - NonTensorStack is indexed by batch dimension + - NonTensorData (scalar metadata) is preserved unchanged + """ + if isinstance(indices, list): + indices = torch.tensor(indices) + + assert indices.dim() == 1, "indices must be a 1D tensor" + + data_dict = {} + batch_size = indices.shape[0] + + if batch is not None: + for key, tensor in batch.items(): + if isinstance(tensor, torch.Tensor) and not tensor.is_nested: + data_dict[key] = tensor[indices] + elif isinstance(tensor, torch.Tensor) and tensor.is_nested: + tensor_lst = tensor.unbind() # for performance + selected_tensors = [tensor_lst[idx] for idx in indices] + data_dict[key] = nested_tensor_from_tensor_list( + selected_tensors, ragged_idx=getattr(tensor, "_ragged_idx", tensor.dim() - 1) + ) + else: + # This handles NonTensorStack (indexable by batch dim) and NonTensorData (scalar metadata). + if tensor.shape: + data_dict[key] = tensor[indices] + else: + data_dict[key] = tensor + selected_batch = TensorDict(source=data_dict, batch_size=batch_size) + else: + selected_batch = None + + return selected_batch + + +def union_tensor_dict(tensor_dict1: TensorDict, tensor_dict2: TensorDict) -> TensorDict: + """Merge two TensorDicts, adding keys from the second to the first. + + Performs an in-place union of two TensorDicts. Keys from tensor_dict2 + that don't exist in tensor_dict1 are added. Keys that exist in both + must have identical values. + + Args: + tensor_dict1: The base TensorDict to merge into (modified in-place). + tensor_dict2: The TensorDict whose keys will be added to tensor_dict1. + + Returns: + The modified tensor_dict1 containing the union of both TensorDicts. + + Raises: + AssertionError: If batch sizes don't match, or if a key exists in + both TensorDicts with different values. + + Example: + >>> td1 = TensorDict({"a": torch.tensor([1, 2])}, batch_size=[2]) + >>> td2 = TensorDict({"b": torch.tensor([3, 4])}, batch_size=[2]) + >>> result = union_tensor_dict(td1, td2) + >>> list(result.keys()) + ['a', 'b'] + """ + assert tensor_dict1.batch_size == tensor_dict2.batch_size, ( + f"Two tensor dict must have identical batch size. Got {tensor_dict1.batch_size} and {tensor_dict2.batch_size}" + ) + for key in tensor_dict2.keys(): + if key not in tensor_dict1.keys(): + # Note that there is a difference between tensor_dict2[key] and tensor_dict2.get(key) + tensor_dict1[key] = tensor_dict2.get(key) + else: + if isinstance(tensor_dict2[key], torch.Tensor): + assert tensor_dict1[key].equal(tensor_dict2[key]), ( + f"{key} in tensor_dict1 and tensor_dict2 are not the same object" + ) + else: + # non-tensor + assert tensor_dict1[key] == tensor_dict2[key], ( + f"{key} in tensor_dict1 and tensor_dict2 are not the same object" + ) + + return tensor_dict1 + + +def make_iterator(tensordict: TensorDict, mini_batch_size, epochs, seed=None, dataloader_kwargs=None): + """Create an iterator that yields mini-batches from a TensorDict. + + Wraps a TensorDict in a DataLoader-style iterator that yields mini-batches + for the specified number of epochs. Useful for training loops. + + Args: + tensordict: The TensorDict to iterate over. + mini_batch_size: Size of each mini-batch. Must evenly divide the + TensorDict's batch size. + epochs: Number of times to iterate through the entire dataset. + seed: Optional random seed for reproducible shuffling. + dataloader_kwargs: Optional dict of additional kwargs to pass to + the underlying DataLoader (e.g., shuffle=True, num_workers=4). + + Returns: + An iterator that yields TensorDict mini-batches. + + Raises: + AssertionError: If batch size is not divisible by mini_batch_size. + + Example: + >>> td = TensorDict({"obs": torch.randn(100, 4)}, batch_size=[100]) + >>> for batch in make_iterator(td, mini_batch_size=10, epochs=2): + ... # batch is a TensorDict with batch_size=[10] + ... pass + """ + from torch.utils.data import DataLoader + + assert tensordict.batch_size[0] % mini_batch_size == 0, f"{tensordict.batch_size[0]} % {mini_batch_size} != 0" + # we can directly create a dataloader from TensorDict + if dataloader_kwargs is None: + dataloader_kwargs = {} + + if seed is not None: + generator = torch.Generator() + generator.manual_seed(seed) + else: + generator = None + + assert isinstance(dataloader_kwargs, dict) + + idx_lst = torch.arange(tensordict.shape[0]) + + train_dataloader = DataLoader( + dataset=idx_lst, batch_size=mini_batch_size, collate_fn=lambda x: x, generator=generator, **dataloader_kwargs + ) + + def get_data(): + for _ in range(epochs): + for idx in train_dataloader: + yield index_select_tensor_dict(tensordict, idx) + + return iter(get_data()) + + +def assert_tensordict_eq(tensordict1: TensorDict, tensordict2: TensorDict): + """Assert that two TensorDicts are equal. + + Performs a deep equality check between two TensorDicts, verifying that + they have the same keys with identical values. Handles nested tensors + by comparing their unbound components. + + Args: + tensordict1: First TensorDict to compare. + tensordict2: Second TensorDict to compare. + + Raises: + AssertionError: If the TensorDicts differ in keys, value types, or + value contents. The error message indicates what differs. + + Note: + - Regular tensors are compared element-wise + - Nested tensors are unbound and compared component by component + - Non-tensor values are compared with standard equality + """ + tensordict1_key_set = set(tensordict1.keys()) + tensordict2_key_set = set(tensordict2.keys()) + assert tensordict1_key_set == tensordict2_key_set, ( + f"key set diffs. Got {tensordict2_key_set=} vs {tensordict1_key_set=}" + ) + + for key in tensordict1.keys(): + val = tensordict1[key] + val2 = tensordict2[key] + + assert type(val) is type(val2), f"The type of {key} must be the same. Got {type(val)} vs {type(val2)}" + + if isinstance(val, torch.Tensor): + if val.is_nested: + assert val.is_nested and val2.is_nested, ( + f"Both tensors must be nested tensors. {val.is_nested=}, {val2.is_nested=}" + ) + t1, t2 = val.unbind(), val2.unbind() + assert len(t1) == len(t2), f"Nested tensor should have the same lengths. {len(t1)=} vs {len(t2)=}" + for c1, c2 in zip(t1, t2, strict=True): + assert torch.equal(c1, c2), f"Nested tensor components have different values. {c1=} vs {c2=}" + else: + assert torch.all(torch.eq(val, val2)).item() + else: + assert val == val2 + + +def get(tensordict: TensorDict, key: str, default=None) -> Any: + """Get a value from a TensorDict with automatic unwrapping. + + Retrieves a value from the TensorDict and automatically converts it + to a Python-native format: + - Tensors are returned as-is + - NonTensorStack is converted to a Python list + - NonTensorData is unwrapped to its underlying value + + Args: + tensordict: The TensorDict to retrieve from. + key: The key to look up. + default: Value to return if the key doesn't exist. Defaults to None. + + Returns: + The value for the key in its native format, or default if not found. + + Example: + >>> td = get_tensordict({"obs": torch.randn(3, 4), "labels": ["a", "b", "c"]}) + >>> get(td, "obs") # Returns torch.Tensor + >>> get(td, "labels") # Returns ["a", "b", "c"] as a list + >>> get(td, "missing", "default") # Returns "default" + """ + if key not in tensordict: + return default + + output = tensordict.get(key) + if isinstance(output, torch.Tensor): + return output + elif isinstance(output, NonTensorStack): + return output.tolist() + else: + assert isinstance(output, NonTensorData) + return output.data + + +def get_keys(tensordict: TensorDict, keys: Iterable[str]) -> TensorDict: + """Extract a subset of keys from a TensorDict into a new TensorDict. + + Creates a new TensorDict containing only the specified keys. Values + are properly categorized as tensor or non-tensor data. + + Args: + tensordict: The source TensorDict. + keys: Iterable of key names to extract. + + Returns: + A new TensorDict containing only the specified keys with their values. + + Raises: + KeyError: If any key in keys doesn't exist in the tensordict. + + Example: + >>> td = get_tensordict({"a": torch.randn(3), "b": torch.randn(3), "c": torch.randn(3)}) + >>> subset = get_keys(td, ["a", "c"]) + >>> list(subset.keys()) + ['a', 'c'] + """ + tensor_output = {} + non_tensor_output = {} + for key in keys: + if key not in tensordict.keys(): + raise KeyError(f"key {key} not in tensordict") + output = tensordict.get(key) + if isinstance(output, torch.Tensor): + tensor_output[key] = output + elif isinstance(output, NonTensorStack): + tensor_output[key] = output.tolist() + else: + assert isinstance(output, NonTensorData) + non_tensor_output[key] = output.data + + return get_tensordict(tensor_output, non_tensor_output) + + +def pop(tensordict: TensorDict, key: str, default=None) -> Any: + """Remove and return a value from a TensorDict with automatic unwrapping. + + Removes the specified key from the TensorDict and returns its value, + automatically converting to Python-native format (same as get()). + + Args: + tensordict: The TensorDict to pop from. + key: The key to remove and return. + default: Value to return if the key doesn't exist. Defaults to None. + + Returns: + The value for the key in its native format, or default if not found. + The key is removed from the TensorDict. + + Example: + >>> td = get_tensordict({"obs": torch.randn(3, 4), "labels": ["a", "b", "c"]}) + >>> labels = pop(td, "labels") # Returns ["a", "b", "c"], removes from td + >>> "labels" in td.keys() + False + """ + _sentinel = object() + output = tensordict.pop(key, _sentinel) + if output is _sentinel: + return default + + if isinstance(output, torch.Tensor): + return output + elif isinstance(output, NonTensorStack): + return output.tolist() + else: + assert isinstance(output, NonTensorData) + return output.data + + +def pop_keys(tensordict: TensorDict, keys: Iterable[str]) -> TensorDict: + """Remove multiple keys from a TensorDict and return them as a new TensorDict. + + Removes the specified keys from the source TensorDict and creates a new + TensorDict containing those keys and their values. + + Args: + tensordict: The source TensorDict to pop from (modified in-place). + keys: Iterable of key names to remove and return. + + Returns: + A new TensorDict containing the popped keys and their values. + + Raises: + KeyError: If any key in keys doesn't exist in the tensordict. + + Example: + >>> td = get_tensordict({"a": torch.randn(3), "b": torch.randn(3), "c": torch.randn(3)}) + >>> popped = pop_keys(td, ["a", "c"]) + >>> list(td.keys()) # Only 'b' remains + ['b'] + >>> list(popped.keys()) + ['a', 'c'] + """ + tensor_output = {} + non_tensor_output = {} + for key in keys: + if key not in tensordict.keys(): + raise KeyError(f"key {key} not in tensordict") + output = tensordict.get(key) + if isinstance(output, torch.Tensor): + tensor_output[key] = tensordict.pop(key) + elif isinstance(output, NonTensorStack): + tensor_output[key] = tensordict.pop(key).tolist() + else: + assert isinstance(output, NonTensorData) + non_tensor_output[key] = tensordict.pop(key) + + return get_tensordict(tensor_output, non_tensor_output) + + +def pad_to_divisor(data: TensorDict, size_divisor: int): + """Pad a TensorDict's batch dimension to be divisible by a given divisor. + + If the TensorDict's length is not evenly divisible by size_divisor, + pads the batch dimension by repeating elements from the beginning. + Useful for ensuring even distribution across workers in distributed training. + + Args: + data: The TensorDict to pad. + size_divisor: The divisor that the padded length must be divisible by. + + Returns: + tuple: A tuple containing: + - data (TensorDict): The padded TensorDict (or original if no padding needed) + - pad_size (int): Number of elements added as padding (0 if none) + + Raises: + AssertionError: If data is not a TensorDict. + + Example: + >>> td = TensorDict({"obs": torch.randn(10, 4)}, batch_size=[10]) + >>> padded, pad_size = pad_to_divisor(td, 4) + >>> len(padded) # 12 (next multiple of 4 after 10) + 12 + >>> pad_size + 2 + """ + assert isinstance(data, TensorDict), "data must be a TensorDict" + if len(data) % size_divisor != 0: + pad_size = size_divisor - len(data) % size_divisor + padding_protos = [] + remaining_pad = pad_size + while remaining_pad > 0: + take_size = min(remaining_pad, len(data)) + padding_protos.append(data[:take_size]) + remaining_pad -= take_size + data_padded = torch.cat([data] + padding_protos) + else: + if len(data) == 0: + logging.warning("padding a DataProto with no item, no changed made") + pad_size = 0 + data_padded = data + return data_padded, pad_size + + +def unpad(data: TensorDict, pad_size): + """Remove padding from a TensorDict. + + Reverses the effect of pad_to_divisor by removing the specified number + of elements from the end of the TensorDict. + + Args: + data: The padded TensorDict. + pad_size: Number of padding elements to remove. If 0, returns + data unchanged. + + Returns: + The TensorDict with padding removed, equivalent to data[:-pad_size]. + + Example: + >>> td = TensorDict({"obs": torch.randn(12, 4)}, batch_size=[12]) + >>> unpadded = unpad(td, pad_size=2) + >>> len(unpadded) + 10 + """ + if pad_size != 0: + data = data[:-pad_size] + return data + + +def contiguous(data: TensorDict) -> TensorDict: + """Call contiguous on a tensor dict. The contiguous function of tensordict lib will make NonTensorStack. + This function will always return a new tensordict + + Args: + data: The input tensordict + + Returns: + a tensordict that is contiguous + + """ + tensor_dict = {} + non_tensor_dict = {} + + for key in data.keys(): + val = data.get(key) + if isinstance(val, NonTensorData): + non_tensor_dict[key] = val + elif isinstance(val, NonTensorStack): + tensor_dict[key] = val + else: + assert isinstance(val, torch.Tensor), f"Expect val to be a torch.Tensor. Got {type(val)}" + tensor_dict[key] = val.contiguous() + + return get_tensordict(tensor_dict=tensor_dict, non_tensor_dict=non_tensor_dict) + + +def maybe_fix_3d_position_ids(data: TensorDict): + # note for tensordict with pickle/unpickle. nested tensor in tensordict after consolidate and pickle/unpickle + # will incur indexing error for ragged tensor. This only happens when using 3D position ids in VLMs. + # This is likely a bug in tensordict. As a workaround, we manually set _ragged_index. + if "position_ids" in data.keys() and data["position_ids"].dim() == 3 and data["position_ids"].is_nested: + data["position_ids"]._ragged_idx = 2 + + +def list_of_dict_to_tensordict(list_of_dicts: list[dict[str, Any]]) -> TensorDict: + """ + Create a TensorDict from a list of dict of tensors and non_tensors. + Note that this requires tensordict version at least 0.10 + """ + assert parse_version(tensordict.__version__) >= parse_version("0.10"), ( + "Storing non-tensor data in TensorDict at least requires tensordict version 0.10" + ) + + assert len(list_of_dicts) > 0 + + keys = list_of_dicts[0].keys() + dict_of_lists = {key: [d[key] for d in list_of_dicts] for key in keys} + batch_size = len(list_of_dicts) + + final_data = { + key: ( + torch.stack(val_list) + if val_list + and all(isinstance(item, torch.Tensor) for item in val_list) + and all(item.shape == val_list[0].shape for item in val_list) + else ( + torch.nested.as_nested_tensor(val_list, layout=torch.jagged) + if val_list and all(isinstance(item, torch.Tensor) for item in val_list) + else NonTensorStack(*val_list) + ) + ) + for key, val_list in dict_of_lists.items() + } + + td = TensorDict(final_data, batch_size=[batch_size]) + + return td diff --git a/verl_0720_main/verl/verl/utils/tokenizer/__init__.py b/verl_0720_main/verl/verl/utils/tokenizer/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..bc4544c0842861241bcdad46cab15531595fbc32 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tokenizer/__init__.py @@ -0,0 +1,29 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .tokenizer import ( + build_multimodal_processor_inputs, + get_processor_token_id, + hf_processor, + hf_tokenizer, + normalize_token_ids, +) + +__all__ = [ + "hf_tokenizer", + "hf_processor", + "normalize_token_ids", + "build_multimodal_processor_inputs", + "get_processor_token_id", +] diff --git a/verl_0720_main/verl/verl/utils/tokenizer/chat_template.py b/verl_0720_main/verl/verl/utils/tokenizer/chat_template.py new file mode 100644 index 0000000000000000000000000000000000000000..5dc48d238b5cbb20a5305a092058c36c70600a7a --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tokenizer/chat_template.py @@ -0,0 +1,211 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +import logging +import os + +from transformers import PreTrainedTokenizerBase, ProcessorMixin + +from .tokenizer import normalize_token_ids + +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +def initialize_system_prompt(tokenizer, **apply_chat_template_kwargs) -> list[int]: + """ + Initialize system prompt tokens for chat templates that support them. + + Args: + tokenizer: The tokenizer with a chat template + **apply_chat_template_kwargs: Additional arguments for apply_chat_template + + Returns: + List of token IDs for the system prompt, or empty list if not supported + """ + token1 = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": ""}], add_generation_prompt=False, tokenize=True, **apply_chat_template_kwargs + ) + ) + token2 = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": ""}] * 2, + add_generation_prompt=False, + tokenize=True, + **apply_chat_template_kwargs, + ) + ) + # get system prompt tokens + system_prompt = token1[: -(len(token2) - len(token1))] + return system_prompt + + +def initialize_turn_separator(tokenizer, **apply_chat_template_kwargs) -> list[int]: + """Tokens a chat template inserts after a message's closing token, before the next turn. + + Multi-turn agent rollouts build the token sequence incrementally. The model stops at the + assistant close token (e.g. ``<|im_end|>``) and never emits the template's trailing + turn-separator (e.g. ``"\\n"``, id 198 for Qwen). Rendering the following tool/user turn in + isolation also omits that separator, so every turn boundary silently drops it and the rollout + token sequence diverges from ``apply_chat_template`` of the equivalent full conversation. + This returns the separator so callers can restore it at turn boundaries. + + Derivation: rendering the same (user) turn with empty vs non-empty content only differs in the + content region, so the maximal common trailing run is exactly ``[close_token, *separator]``. + A user turn is used deliberately -- probing with an assistant turn would inject reasoning + scaffolding (e.g. Qwen3's ````) that is not part of the separator. The model + emits the close token itself (it is the stop token), so the separator is everything after it. + + Returns an empty list when the template has no turn separator or an unexpected structure, so + callers keep their previous behavior instead of crashing. + """ + # Render two user turns that differ only in body text; the shared trailing run is the separator. + # A bare string ``content`` is rejected by some multimodal processors (they iterate ``content`` + # expecting a list of typed parts), so fall back to the list-of-parts form, and return ``[]`` if + # neither renders. Both probes must use the same form so only the body differs. + empty = filled = None + for as_parts in (False, True): + if as_parts: + body_empty, body_filled = [{"type": "text", "text": ""}], [{"type": "text", "text": "x"}] + else: + body_empty, body_filled = "", "x" + try: + empty = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": body_empty}], + add_generation_prompt=False, + tokenize=True, + **apply_chat_template_kwargs, + ) + ) + filled = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": body_filled}], + add_generation_prompt=False, + tokenize=True, + **apply_chat_template_kwargs, + ) + ) + break + except Exception: + empty = filled = None + if empty is None or filled is None: + return [] + # Maximal common trailing run == the message closing token(s) + inter-turn separator (identical + # regardless of content). + i = 0 + while i < len(empty) and i < len(filled) and empty[-1 - i] == filled[-1 - i]: + i += 1 + suffix = empty[len(empty) - i :] + if not suffix: + return [] + # Split off the closing token the model already emits; the remainder is the dropped separator. + # A processor (VLM path) exposes ``eos_token_id`` on its wrapped tokenizer rather than itself, + # and some tokenizers (e.g. Llama 3) expose it as a list/tuple of ids rather than a single int. + eos_id = getattr(tokenizer, "eos_token_id", None) + if eos_id is None: + eos_id = getattr(getattr(tokenizer, "tokenizer", None), "eos_token_id", None) + eos_ids = {eos_id} if isinstance(eos_id, int) else set(eos_id or []) + last_close = max((i for i, tok_id in enumerate(suffix) if tok_id in eos_ids), default=None) + if last_close is not None: + return suffix[last_close + 1 :] + return suffix[1:] + + +def extract_system_prompt_and_generation(tokenizer, **apply_chat_template_kwargs): + token1 = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": ""}], add_generation_prompt=False, tokenize=True, **apply_chat_template_kwargs + ) + ) + token2 = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": ""}] * 2, + add_generation_prompt=False, + tokenize=True, + **apply_chat_template_kwargs, + ) + ) + # get system prompt tokens + system_prompt = token1[: -(len(token2) - len(token1))] + # get generate prompt tokens + token3 = normalize_token_ids( + tokenizer.apply_chat_template( + [{"role": "user", "content": ""}], add_generation_prompt=True, tokenize=True, **apply_chat_template_kwargs + ) + ) + generate_prompt = token3[len(token1) :] + + return system_prompt, generate_prompt + + +def apply_chat_template( + processor: PreTrainedTokenizerBase | ProcessorMixin, + messages: list[dict], + *, + tokenize: bool = True, + add_generation_prompt: bool = True, + tools=None, + return_dict: bool = False, + **kwargs, +) -> list[int] | str: + """apply_chat_template to messages with special attention to template requiring + at least one user message, e.g. Qwen3.5. + + Args: + processor: tokenizer or processor. + messages: list[dict], messages. + tokenize: bool, whether to tokenize the output. + add_generation_prompt: bool, whether to add generation prompt. + tools: list[dict], tools schema. + return_dict: bool, whether to return a dict. + **kwargs: additional arguments for apply_chat_template. + + Returns: + list[int] | str: tokenized ids or text string. + """ + try: + return processor.apply_chat_template( + messages, + tokenize=tokenize, + add_generation_prompt=add_generation_prompt, + tools=tools, + return_dict=return_dict, + **kwargs, + ) + except Exception: + # Qwen3.5 apply_chat_template needs messages with at least one user message + dummy_user_message = [{"role": "user", "content": [{"type": "text", "text": ""}]}] + dummy_user_prefix = processor.apply_chat_template( + dummy_user_message, + tokenize=tokenize, + add_generation_prompt=False, + tools=tools, + return_dict=return_dict, + **kwargs, + ) + output = processor.apply_chat_template( + dummy_user_message + messages, + tokenize=tokenize, + add_generation_prompt=add_generation_prompt, + tools=tools, + return_dict=return_dict, + **kwargs, + ) + + if not tokenize: # tokenize=False + return output[len(dummy_user_prefix) :] + elif not return_dict: # tokenize=True and return_dict=False + if isinstance(output[0], list): # transformers>=5 + assert len(output) == 1, "output must be a list[int] or list[list[int]]" + dummy_user_prefix = dummy_user_prefix[0] + output = output[0] + return output[len(dummy_user_prefix) :] + else: # tokenize=True and return_dict=True and return_tensors="pt" + dummy_user_prefix = dict(dummy_user_prefix) + output = dict(output) + prefix_len = dummy_user_prefix["input_ids"].shape[1] + output["input_ids"] = output["input_ids"][:, prefix_len:] + output["attention_mask"] = output["attention_mask"][:, prefix_len:] + if "mm_token_type_ids" in output: + output["mm_token_type_ids"] = output["mm_token_type_ids"][:, prefix_len:] + return output diff --git a/verl_0720_main/verl/verl/utils/tokenizer/continuous_token.py b/verl_0720_main/verl/verl/utils/tokenizer/continuous_token.py new file mode 100644 index 0000000000000000000000000000000000000000..cf769452f98a559fe2abdb1f68c45e3e73d5a229 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tokenizer/continuous_token.py @@ -0,0 +1,661 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Continuous Token builder implementations.""" + +from __future__ import annotations + +import logging +from dataclasses import dataclass, field +from typing import Any, Literal + +from .chat_template import apply_chat_template +from .tokenizer import normalize_token_ids + +_SUPPORTED_APPEND_ROLES = frozenset({"tool", "user", "system"}) +_SYNTHETIC_SYSTEM_MESSAGE: dict[str, Any] = {"role": "system", "content": "continuous token synthetic system"} +_SYNTHETIC_USER_MESSAGE: dict[str, Any] = {"role": "user", "content": "continuous token synthetic user"} +_ASSISTANT_REASONING_CONTENT: str = "reasoning" +_DUMMY_TOOL_NAME = "continuous_token_tool" +MergeKind = Literal["assistant", "non_assistant"] + +logger = logging.getLogger(__name__) + + +@dataclass(frozen=True) +class MergeResult: + """Merged runtime tokens plus the edits callers need to align metadata. + + ``token_ids`` is the updated runtime token stream. The other fields describe + how the stream changed at the merge junction: ``inserted_token_ids`` are + CT-created boundary tokens, ``appended_token_count`` counts newly appended + assistant or non-assistant tokens excluding those inserted boundary tokens, + and ``removed_prefix_token_count`` counts stale prefix tokens dropped before + appending. Boundary tokens are not model-generated and therefore must not + carry loss or model logprobs. + """ + + token_ids: list[int] + appended_token_count: int + kind: MergeKind = "non_assistant" + inserted_token_ids: list[int] = field(default_factory=list) + removed_prefix_token_count: int = 0 + + +class ContinuousTokenBuilder: + """Build and update continuous-token runtime prompts for multi-turn rollouts. + + This class exposes two API layers: + + AgentLoop-facing runtime APIs: + ``build_initial_tokens`` renders the first prompt, ``merge_non_assistant_tokens`` + merges append-only tool/user/system messages, ``merge_assistant_tokens`` + appends model-generated assistant tokens, and ``align_response_metadata`` + applies the recorded token edits to masks/logprobs. + + Developer extension APIs: + Model-specific builders should subclass this class and keep the runtime + API contracts above stable. Chat template specific behavior belongs in hooks + such as ``_tokenize_tool_group``, ``_tokenize_single_non_tool``, + ``_tokenize_generation_prompt_delta``, and ``_merge_non_assistant_token_ids``. + ``render_delta_token_id`` is the shared suffix-diff helper those hooks can + reuse. + """ + + allowed_append_roles: frozenset[str] = _SUPPORTED_APPEND_ROLES + + def __init__( + self, + tokenizer: Any, + *, + chat_template_kwargs: dict[str, Any] | None = None, + allowed_append_roles: list[str] | tuple[str, ...] | set[str] | None = None, + ): + self.tokenizer = tokenizer + self.chat_template_kwargs = chat_template_kwargs or {} + if allowed_append_roles is not None: + allowed_roles = frozenset(allowed_append_roles) + unknown_roles = allowed_roles - _SUPPORTED_APPEND_ROLES + if unknown_roles: + raise ValueError(f"Unsupported Continuous Token append roles: {sorted(unknown_roles)}") + self.allowed_append_roles = allowed_roles + + def build_initial_tokens( + self, + messages: list[dict[str, Any]], + *, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + return self._render_tokens(messages, add_generation_prompt=True, tools=tools) + + def tokenize_non_assistant_incremental_messages( + self, + previous_messages: list[dict[str, Any]], + updated_messages: list[dict[str, Any]], + *, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + self._assert_append_only(previous_messages, updated_messages) + appended_messages = updated_messages[len(previous_messages) :] + if not appended_messages: + return [] + incremental_ids: list[int] = [] + + for group in self._iter_append_groups(appended_messages): + role = group[0].get("role") + if role == "tool": + incremental_ids.extend( + self._tokenize_tool_group( + group, + previous_messages=previous_messages, + tools=tools, + ) + ) + elif role in {"user", "system"}: + # System appends can represent retry/control messages; unsupported templates will fail in suffix diff. + if len(group) != 1: + raise ValueError( + f"Continuous Token expects one {role!r} message per append group, got {len(group)}" + ) + incremental_ids.extend(self._tokenize_single_non_tool(group[0], tools=tools)) + else: + raise ValueError(f"Unsupported Continuous Token append role: {role!r}") + + incremental_ids.extend(self._tokenize_generation_prompt_delta(updated_messages, tools=tools)) + return incremental_ids + + def merge_non_assistant_tokens( + self, + previous_messages: list[dict[str, Any]], + updated_messages: list[dict[str, Any]], + runtime_token_ids: list[int], + *, + tools: list[dict[str, Any]] | None = None, + ) -> MergeResult: + appended_ids = self.tokenize_non_assistant_incremental_messages( + previous_messages, updated_messages, tools=tools + ) + return self._merge_non_assistant_token_ids(runtime_token_ids, appended_ids) + + def merge_assistant_tokens(self, runtime_token_ids: list[int], assistant_token_ids: list[int]) -> MergeResult: + """Merge model-generated assistant tokens into the runtime token stream.""" + merged_token_ids = list(runtime_token_ids) + list(assistant_token_ids) + return MergeResult( + token_ids=merged_token_ids, + appended_token_count=len(assistant_token_ids), + kind="assistant", + ) + + def _merge_non_assistant_token_ids( + self, runtime_token_ids: list[int], appended_token_ids: list[int] + ) -> MergeResult: + """Merge runtime prefix tokens and appended non-assistant tokens. + + Model-specific builders usually override this hook for boundary handling, + such as inserting or trimming tokens at the prefix/appended-token junction. + """ + merged_token_ids = list(runtime_token_ids) + list(appended_token_ids) + return MergeResult( + token_ids=merged_token_ids, + appended_token_count=len(appended_token_ids), + kind="non_assistant", + ) + + def _render_tokens( + self, + messages: list[dict[str, Any]], + *, + add_generation_prompt: bool, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + tokenized = apply_chat_template( + self.tokenizer, + messages, + tokenize=True, + add_generation_prompt=add_generation_prompt, + tools=tools, + **self.chat_template_kwargs, + ) + return normalize_token_ids(tokenized) + + def render_delta_token_id( + self, + prefix_messages: list[dict[str, Any]], + appended_messages: list[dict[str, Any]], + *, + add_generation_prompt: bool = False, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + """Render prefix/full prompts as token IDs and return the token-level suffix.""" + prefix_token_ids = self._render_tokens(prefix_messages, add_generation_prompt=False, tools=tools) + full_token_ids = self._render_tokens( + prefix_messages + appended_messages, + add_generation_prompt=add_generation_prompt, + tools=tools, + ) + if full_token_ids[: len(prefix_token_ids)] != prefix_token_ids: + roles = [message.get("role") for message in appended_messages] or ["generation_prompt"] + raise ValueError(f"Continuous Token token-id suffix diff failed for roles: {roles}") + return full_token_ids[len(prefix_token_ids) :] + + def _tokenize_tool_group( + self, + tool_messages: list[dict[str, Any]], + *, + previous_messages: list[dict[str, Any]], + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + synthetic_assistant = self._synthetic_assistant_for_tools(tool_messages) + return self.render_delta_token_id( + [_SYNTHETIC_SYSTEM_MESSAGE, _SYNTHETIC_USER_MESSAGE, synthetic_assistant], + tool_messages, + tools=tools, + ) + + def _tokenize_single_non_tool( + self, + message: dict[str, Any], + *, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + return self.render_delta_token_id( + [_SYNTHETIC_SYSTEM_MESSAGE, _SYNTHETIC_USER_MESSAGE], + [message], + tools=tools, + ) + + def _tokenize_generation_prompt_delta( + self, + updated_messages: list[dict[str, Any]], + *, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + """Tokenize the tokens added only by ``add_generation_prompt=True``.""" + return self.render_delta_token_id(updated_messages, [], add_generation_prompt=True, tools=tools) + + def _iter_append_groups(self, appended_messages: list[dict[str, Any]]) -> list[list[dict[str, Any]]]: + groups: list[list[dict[str, Any]]] = [] + index = 0 + while index < len(appended_messages): + role = appended_messages[index].get("role") + if role == "tool": + end = index + 1 + while end < len(appended_messages) and appended_messages[end].get("role") == "tool": + end += 1 + groups.append(appended_messages[index:end]) + index = end + else: + groups.append([appended_messages[index]]) + index += 1 + return groups + + def _assert_append_only( + self, + previous_messages: list[dict[str, Any]], + updated_messages: list[dict[str, Any]], + ) -> None: + if len(updated_messages) < len(previous_messages): + raise ValueError("Continuous Token messages must be append-only; updated_messages is shorter") + if updated_messages[: len(previous_messages)] != previous_messages: + raise ValueError("Continuous Token messages must be append-only; prefix messages changed") + for message in updated_messages[len(previous_messages) :]: + role = message.get("role") + if role not in self.allowed_append_roles: + raise ValueError( + f"Continuous Token only supports appending roles {sorted(self.allowed_append_roles)}, got {role!r}" + ) + + def _synthetic_assistant_for_tools( + self, + tool_messages: list[dict[str, Any]], + ) -> dict[str, Any]: + tool_calls = [] + for index, tool_message in enumerate(tool_messages): + tool_call = { + "id": _tool_call_id_or_dummy(tool_message, index), + "type": "function", + "function": { + "name": _tool_message_name_or_dummy(tool_message), + "arguments": {}, + }, + } + tool_calls.append(tool_call) + return { + "role": "assistant", + "content": "", + "reasoning_content": _ASSISTANT_REASONING_CONTENT, + "tool_calls": tool_calls, + } + + def align_response_metadata( + self, + merge_result: MergeResult, + response_mask: list[int], + response_logprobs: list[float] | None = None, + *, + assistant_logprobs: list[float] | None = None, + ) -> tuple[list[int], list[float] | None]: + """Align response masks and logprobs after a Continuous Token merge. + + ``MergeResult`` records token edits at the runtime-prefix boundary. This + method applies the same edits to response-side metadata: trimming + metadata for removed prefix tokens, assigning zero mask/logprob to + inserted boundary or non-assistant tokens, and assigning assistant + mask/logprobs to appended assistant tokens. + """ + aligned_mask = list(response_mask) + aligned_logprobs = list(response_logprobs) if response_logprobs is not None else None + if aligned_logprobs is None and assistant_logprobs is not None: + raise ValueError("response_logprobs is required when assistant_logprobs is provided") + + # If merge trimmed tokens from the current prefix, trim their metadata too. + if merge_result.removed_prefix_token_count: + aligned_mask = aligned_mask[: -merge_result.removed_prefix_token_count] + if aligned_logprobs is not None: + aligned_logprobs = aligned_logprobs[: -merge_result.removed_prefix_token_count] + + # Boundary tokens are added by CT itself, so they get mask/logprob 0. + inserted_token_count = len(merge_result.inserted_token_ids) + aligned_mask += [0] * inserted_token_count + if aligned_logprobs is not None: + aligned_logprobs += [0.0] * inserted_token_count + + # Assistant tokens get mask 1 and their logprobs; tool/user/system tokens get mask/logprob 0. + if merge_result.kind == "assistant": + aligned_mask += [1] * merge_result.appended_token_count + if aligned_logprobs is not None: + if assistant_logprobs is None: + if merge_result.appended_token_count: + raise ValueError("assistant_logprobs is required for assistant Continuous Token alignment") + assistant_logprobs = [] + if len(assistant_logprobs) != merge_result.appended_token_count: + raise ValueError( + "assistant_logprobs length must match appended assistant token count, " + f"got {len(assistant_logprobs)} and {merge_result.appended_token_count}" + ) + aligned_logprobs += list(assistant_logprobs) + elif merge_result.kind == "non_assistant": + aligned_mask += [0] * merge_result.appended_token_count + if aligned_logprobs is not None: + aligned_logprobs += [0.0] * merge_result.appended_token_count + else: + raise ValueError(f"Unknown Continuous Token merge kind: {merge_result.kind!r}") + + return aligned_mask, aligned_logprobs + + +class GptOssContinuousTokenBuilder(ContinuousTokenBuilder): + """GPT-OSS tool-response formatting.""" + + def _tokenize_tool_group( + self, + tool_messages: list[dict[str, Any]], + *, + previous_messages: list[dict[str, Any]], + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + del tools + response_text = "".join( + self._format_tool_response( + tool_message, + _resolve_required_tool_name( + tool_message, + index, + tool_messages, + previous_messages, + ), + ) + for index, tool_message in enumerate(tool_messages) + ) + return self.tokenizer.encode(response_text, add_special_tokens=False) + + @staticmethod + def _format_tool_response(tool_message: dict[str, Any], tool_name: str) -> str: + content = _stringify_tool_content(tool_message.get("content", "")) + return f"<|start|>functions.{tool_name} to=assistant<|channel|>commentary<|message|>{content}<|end|>" + + +class QwenContinuousTokenBuilder(ContinuousTokenBuilder): + """Qwen ChatML boundary handling. + + Qwen2.5, Qwen3, and Qwen3.5 templates render ``<|im_end|>\n`` after a turn, + while generation may stop at ``<|im_end|>``. When the runtime prefix ends + there, insert the missing newline before appending non-assistant tokens. + """ + + def __init__(self, tokenizer: Any, **kwargs: Any): + super().__init__(tokenizer, **kwargs) + newline_ids = tokenizer.encode("\n", add_special_tokens=False) + if len(newline_ids) != 1: + raise ValueError(f"Expected Qwen newline to tokenize to one token, got {newline_ids!r}") + self._newline_id = int(newline_ids[0]) + self._im_end_id = _require_token_id(tokenizer, "<|im_end|>") + + def _merge_non_assistant_token_ids( + self, runtime_token_ids: list[int], appended_token_ids: list[int] + ) -> MergeResult: + prefix = list(runtime_token_ids) + inserted_token_ids: list[int] = [] + if prefix and prefix[-1] == self._im_end_id: + prefix.append(self._newline_id) + inserted_token_ids.append(self._newline_id) + return MergeResult( + token_ids=prefix + list(appended_token_ids), + appended_token_count=len(appended_token_ids), + kind="non_assistant", + inserted_token_ids=inserted_token_ids, + ) + + +class MiniMaxContinuousTokenBuilder(ContinuousTokenBuilder): + """MiniMax boundary handling. + + MiniMax templates render ``[e~[\n`` after a turn, while generation may stop + at ``[e~[``. When the runtime prefix ends there, insert the missing newline + before appending non-assistant tokens. + """ + + def __init__(self, tokenizer: Any, **kwargs: Any): + super().__init__(tokenizer, **kwargs) + newline_ids = tokenizer.encode("\n", add_special_tokens=False) + if len(newline_ids) != 1: + raise ValueError(f"Expected MiniMax newline to tokenize to one token, got {newline_ids!r}") + self._newline_id = int(newline_ids[0]) + self._eos_id = _require_token_id(tokenizer, "[e~[") + + def _merge_non_assistant_token_ids( + self, runtime_token_ids: list[int], appended_token_ids: list[int] + ) -> MergeResult: + prefix = list(runtime_token_ids) + inserted_token_ids: list[int] = [] + if prefix and prefix[-1] == self._eos_id: + prefix.append(self._newline_id) + inserted_token_ids.append(self._newline_id) + return MergeResult( + token_ids=prefix + list(appended_token_ids), + appended_token_count=len(appended_token_ids), + kind="non_assistant", + inserted_token_ids=inserted_token_ids, + ) + + +class GLMContinuousTokenBuilder(ContinuousTokenBuilder): + """GLM observation/user boundary handling. + + ``<|observation|>`` and ``<|user|>`` can be both assistant stop tokens and + next-message start tokens. If the runtime prefix ends with either, remove + that token before appending the next non-assistant segment. + """ + + def __init__(self, tokenizer: Any, **kwargs: Any): + super().__init__(tokenizer, **kwargs) + self._observation_id = _require_token_id(tokenizer, "<|observation|>") + self._user_id = _require_token_id(tokenizer, "<|user|>") + self._ambiguous_boundary_ids = {self._observation_id, self._user_id} + + def _merge_non_assistant_token_ids( + self, runtime_token_ids: list[int], appended_token_ids: list[int] + ) -> MergeResult: + prefix = list(runtime_token_ids) + removed_prefix_token_count = 0 + if prefix and prefix[-1] in self._ambiguous_boundary_ids: + prefix = prefix[:-1] + removed_prefix_token_count = 1 + return MergeResult( + token_ids=prefix + list(appended_token_ids), + appended_token_count=len(appended_token_ids), + kind="non_assistant", + removed_prefix_token_count=removed_prefix_token_count, + ) + + +class Gemma4ContinuousTokenBuilder(ContinuousTokenBuilder): + """Gemma4 tool-response boundary handling.""" + + def __init__(self, tokenizer: Any, **kwargs: Any): + super().__init__(tokenizer, **kwargs) + self._tool_response_id = _require_token_id(tokenizer, "<|tool_response>") + + def _tokenize_tool_group( + self, + tool_messages: list[dict[str, Any]], + *, + previous_messages: list[dict[str, Any]], + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + del tools + response_text = "".join( + self._format_tool_response( + tool_message, + _resolve_required_tool_name( + tool_message, + index, + tool_messages, + previous_messages, + ), + ) + for index, tool_message in enumerate(tool_messages) + ) + return self.tokenizer.encode(response_text, add_special_tokens=False) + + @staticmethod + def _format_tool_response(tool_message: dict[str, Any], tool_name: str) -> str: + content = _stringify_tool_content(tool_message.get("content", "")) + return f'<|tool_response>response:{tool_name}{{value:<|"|>{content}<|"|>}}' + + def _tokenize_generation_prompt_delta( + self, + updated_messages: list[dict[str, Any]], + *, + tools: list[dict[str, Any]] | None = None, + ) -> list[int]: + last_message = updated_messages[-1] + if last_message.get("role") not in {"user", "system"}: + return [] + return self.render_delta_token_id( + [_SYNTHETIC_SYSTEM_MESSAGE, _SYNTHETIC_USER_MESSAGE, last_message], + [], + add_generation_prompt=True, + tools=tools, + ) + + def merge_non_assistant_tokens( + self, + previous_messages: list[dict[str, Any]], + updated_messages: list[dict[str, Any]], + runtime_token_ids: list[int], + *, + tools: list[dict[str, Any]] | None = None, + ) -> MergeResult: + appended_token_ids = self.tokenize_non_assistant_incremental_messages( + previous_messages, updated_messages, tools=tools + ) + appended_messages = updated_messages[len(previous_messages) :] + + prefix = list(runtime_token_ids) + inserted_token_ids: list[int] = [] + if appended_messages and prefix[-1:] != [self._tool_response_id]: + prefix.append(self._tool_response_id) + inserted_token_ids.append(self._tool_response_id) + + return MergeResult( + token_ids=prefix + appended_token_ids, + appended_token_count=len(appended_token_ids), + kind="non_assistant", + inserted_token_ids=inserted_token_ids, + ) + + +def _require_token_id(tokenizer: Any, token: str) -> int: + token_id = tokenizer.convert_tokens_to_ids(token) + if token_id is None: + raise ValueError(f"Tokenizer does not define required token {token!r}") + if isinstance(token_id, list): + if len(token_id) != 1: + raise ValueError(f"Tokenizer returned multiple ids for required token {token!r}: {token_id!r}") + token_id = token_id[0] + if not isinstance(token_id, int) or token_id < 0: + raise ValueError(f"Tokenizer returned invalid id for required token {token!r}: {token_id!r}") + return token_id + + +def _stringify_tool_content(content: Any) -> str: + if content is None: + return "" + if isinstance(content, str): + return content + if isinstance(content, list): + return "".join( + item.get("text", "") for item in content if isinstance(item, dict) and item.get("type") == "text" + ) + return str(content) + + +def _tool_message_name_or_dummy(tool_message: dict[str, Any]) -> str: + if tool_message.get("name"): + return str(tool_message["name"]) + return _DUMMY_TOOL_NAME + + +def _tool_call_id_or_dummy(tool_message: dict[str, Any], index: int) -> Any: + if tool_message.get("tool_call_id") is not None: + return tool_message["tool_call_id"] + return f"continuous_token_call_{index}" + + +def _latest_assistant_tool_call_names( + messages: list[dict[str, Any]], +) -> tuple[dict[str, str], list[str | None]]: + tool_names_by_id: dict[str, str] = {} + for message in reversed(messages): + if message.get("role") != "assistant": + continue + tool_calls = message.get("tool_calls") or [] + if not isinstance(tool_calls, list): + return tool_names_by_id, [] + positional_tool_names: list[str | None] = [] + for tool_call in tool_calls: + if not isinstance(tool_call, dict): + positional_tool_names.append(None) + continue + name = _tool_call_function_name(tool_call) + positional_tool_names.append(name) + tool_call_id = tool_call.get("id") + if name is not None and tool_call_id is not None: + tool_names_by_id.setdefault(str(tool_call_id), name) + return tool_names_by_id, positional_tool_names + return tool_names_by_id, [] + + +def _resolve_required_tool_name( + tool_message: dict[str, Any], + index: int, + tool_messages: list[dict[str, Any]], + previous_messages: list[dict[str, Any]], +) -> str: + if tool_message.get("name"): + return str(tool_message["name"]) + + tool_names_by_id, positional_tool_names = _latest_assistant_tool_call_names(previous_messages) + tool_call_id = tool_message.get("tool_call_id") + if tool_call_id is not None and str(tool_call_id) in tool_names_by_id: + return tool_names_by_id[str(tool_call_id)] + + if len(tool_messages) != len(positional_tool_names): + raise ValueError( + "Continuous Token cannot resolve tool name by position: " + f"got {len(tool_messages)} tool response messages but the latest assistant has " + f"{len(positional_tool_names)} tool calls" + ) + if index >= len(positional_tool_names) or positional_tool_names[index] is None: + raise ValueError( + "Continuous Token cannot resolve tool name by position: " + f"assistant tool call at index {index} has no function name" + ) + + # ToolAgentLoop uses asyncio.gather and appends responses in the original + # tool-call order, so positional matching is safe for its full response + # batches. Black-box agent loops may return responses in another order; they + # must provide tool message name or tool_call_id instead of relying on this. + logger.warning( + "Continuous Token is resolving a tool response name by position; this is only safe when " + "tool responses are appended in the same order as the latest assistant tool_calls" + ) + return positional_tool_names[index] + + +def _tool_call_function_name(tool_call: dict[str, Any]) -> str | None: + function = tool_call.get("function") + if isinstance(function, dict) and function.get("name") is not None: + return str(function["name"]) + return None diff --git a/verl_0720_main/verl/verl/utils/tokenizer/continuous_token_wiring.py b/verl_0720_main/verl/verl/utils/tokenizer/continuous_token_wiring.py new file mode 100644 index 0000000000000000000000000000000000000000..fd038da9ec17036d848ffe27f5f955bde21139f9 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tokenizer/continuous_token_wiring.py @@ -0,0 +1,209 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Continuous Token builder factory and model-family resolution.""" + +from __future__ import annotations + +import logging +import re +from enum import StrEnum +from typing import Any + +from .continuous_token import ( + ContinuousTokenBuilder, + Gemma4ContinuousTokenBuilder, + GLMContinuousTokenBuilder, + GptOssContinuousTokenBuilder, + MiniMaxContinuousTokenBuilder, + QwenContinuousTokenBuilder, +) + +logger = logging.getLogger(__name__) + + +class ContinuousTokenModelFamily(StrEnum): + AUTO = "auto" + DEFAULT = "default" + QWEN = "qwen" + QWEN25 = "qwen25" + QWEN3 = "qwen3" + QWEN35 = "qwen35" + MINIMAX = "minimax" + MINIMAX_M2 = "minimaxm2" + MINIMAX_M25 = "minimaxm25" + MINIMAX_M27 = "minimaxm27" + GLM47 = "glm47" + GLM5 = "glm5" + GEMMA4 = "gemma4" + GPTOSS = "gptoss" + + +_CONTINUOUS_TOKEN_BUILDER_REGISTRY: dict[ContinuousTokenModelFamily, type[Any]] = { + ContinuousTokenModelFamily.DEFAULT: ContinuousTokenBuilder, + ContinuousTokenModelFamily.QWEN: QwenContinuousTokenBuilder, + ContinuousTokenModelFamily.QWEN25: QwenContinuousTokenBuilder, + ContinuousTokenModelFamily.QWEN3: QwenContinuousTokenBuilder, + ContinuousTokenModelFamily.QWEN35: QwenContinuousTokenBuilder, + ContinuousTokenModelFamily.MINIMAX: MiniMaxContinuousTokenBuilder, + ContinuousTokenModelFamily.MINIMAX_M2: MiniMaxContinuousTokenBuilder, + ContinuousTokenModelFamily.MINIMAX_M25: MiniMaxContinuousTokenBuilder, + ContinuousTokenModelFamily.MINIMAX_M27: MiniMaxContinuousTokenBuilder, + ContinuousTokenModelFamily.GLM47: GLMContinuousTokenBuilder, + ContinuousTokenModelFamily.GLM5: GLMContinuousTokenBuilder, + ContinuousTokenModelFamily.GEMMA4: Gemma4ContinuousTokenBuilder, + ContinuousTokenModelFamily.GPTOSS: GptOssContinuousTokenBuilder, +} + +CONTINUOUS_TOKEN_BUILDER_FAMILIES = tuple(family.value for family in _CONTINUOUS_TOKEN_BUILDER_REGISTRY) + + +def get_continuous_token_builder_class(model_family: str | ContinuousTokenModelFamily) -> type[Any]: + family = _normalize_model_family(model_family) + try: + return _CONTINUOUS_TOKEN_BUILDER_REGISTRY[family] + except KeyError as exc: + raise ValueError( + f"Unknown Continuous Token builder family {family!r}. " + f"Supported families: {CONTINUOUS_TOKEN_BUILDER_FAMILIES}." + ) from exc + + +def list_continuous_token_builder_families() -> tuple[str, ...]: + return CONTINUOUS_TOKEN_BUILDER_FAMILIES + + +def resolve_continuous_token_model_family( + model_family: str | ContinuousTokenModelFamily, + *, + model_path: str | None = None, + tokenizer: Any | None = None, + tokenizer_name_or_path: str | None = None, +) -> ContinuousTokenModelFamily: + """Resolve ``auto`` to a concrete family, or canonicalize an explicit family.""" + family = _normalize_model_family(model_family) + if family != ContinuousTokenModelFamily.AUTO: + logger.info("Using explicit Continuous Token builder family: %s", family) + return family + + resolved = infer_continuous_token_model_family( + model_path=model_path, + tokenizer=tokenizer, + tokenizer_name_or_path=tokenizer_name_or_path, + ) + logger.info( + "Resolved Continuous Token builder family from auto: %s (model_path=%r, tokenizer_name_or_path=%r)", + resolved, + model_path, + tokenizer_name_or_path or _tokenizer_name_or_path(tokenizer), + ) + return resolved + + +def infer_continuous_token_model_family( + *, + model_path: str | None = None, + tokenizer: Any | None = None, + tokenizer_name_or_path: str | None = None, +) -> ContinuousTokenModelFamily: + """Infer a built-in model family from model/tokenizer names. + + Unknown models intentionally fall back to ``default`` so enabling + ``model_family=auto`` remains conservative. + """ + candidates = [model_path, tokenizer_name_or_path, _tokenizer_name_or_path(tokenizer)] + haystack = " ".join(str(item).lower() for item in candidates if item) + compact = re.sub(r"[^a-z0-9]+", "", haystack) + + if any(marker in haystack for marker in ("glm-5", "glm_5")) or "glm5" in compact: + return ContinuousTokenModelFamily.GLM5 + if any(marker in haystack for marker in ("glm-4.7", "glm_4.7", "glm4.7")) or "glm47" in compact: + return ContinuousTokenModelFamily.GLM47 + if any(marker in haystack for marker in ("gemma-4", "gemma_4")) or any( + marker in compact for marker in ("gemma4", "gemma4unified") + ): + return ContinuousTokenModelFamily.GEMMA4 + if any(marker in haystack for marker in ("gpt-oss", "gpt_oss")) or "gptoss" in compact: + return ContinuousTokenModelFamily.GPTOSS + if "minimaxm27" in compact: + return ContinuousTokenModelFamily.MINIMAX_M27 + if "minimaxm25" in compact: + return ContinuousTokenModelFamily.MINIMAX_M25 + if "minimaxm2" in compact: + return ContinuousTokenModelFamily.MINIMAX_M2 + if "minimax" in compact: + return ContinuousTokenModelFamily.MINIMAX + if any(marker in haystack for marker in ("qwen3.5", "qwen3_5", "qwen3-5")) or "qwen35" in compact: + return ContinuousTokenModelFamily.QWEN35 + if any(marker in haystack for marker in ("qwen2.5", "qwen2_5", "qwen2-5")) or "qwen25" in compact: + return ContinuousTokenModelFamily.QWEN25 + if "qwen3" in compact: + return ContinuousTokenModelFamily.QWEN3 + logger.warning( + "No model-specific Continuous Token builder matched model_path=%r, tokenizer_name_or_path=%r; " + "falling back to the default ContinuousTokenBuilder.", + model_path, + tokenizer_name_or_path or _tokenizer_name_or_path(tokenizer), + ) + return ContinuousTokenModelFamily.DEFAULT + + +def create_continuous_token_builder( + tokenizer: Any, + *, + model_family: str | ContinuousTokenModelFamily, + model_path: str | None = None, + tokenizer_name_or_path: str | None = None, + chat_template_kwargs: dict[str, Any] | None = None, + **builder_kwargs: Any, +) -> Any: + """Instantiate the registered builder selected by config/model metadata.""" + resolved_family = resolve_continuous_token_model_family( + model_family, + model_path=model_path, + tokenizer=tokenizer, + tokenizer_name_or_path=tokenizer_name_or_path, + ) + builder_cls = get_continuous_token_builder_class(resolved_family) + logger.info("Creating Continuous Token builder: family=%s class=%s", resolved_family, builder_cls) + return builder_cls(tokenizer, chat_template_kwargs=chat_template_kwargs, **builder_kwargs) + + +def _normalize_model_family(model_family: str | ContinuousTokenModelFamily) -> ContinuousTokenModelFamily: + if isinstance(model_family, ContinuousTokenModelFamily): + return model_family + if not isinstance(model_family, str) or not model_family: + raise ValueError("Continuous Token model_family must be a non-empty string") + family = model_family.strip().lower() + if not family: + raise ValueError("Continuous Token model_family must be a non-empty string") + family = re.sub(r"[^a-z0-9]+", "", family) + try: + return ContinuousTokenModelFamily(family) + except ValueError as exc: + raise ValueError( + f"Unknown Continuous Token model_family {model_family!r}. " + f"Supported families: {(ContinuousTokenModelFamily.AUTO.value, *CONTINUOUS_TOKEN_BUILDER_FAMILIES)}." + ) from exc + + +def _tokenizer_name_or_path(tokenizer: Any | None) -> str | None: + if tokenizer is None: + return None + name = getattr(tokenizer, "name_or_path", None) + if name: + return str(name) + init_kwargs = getattr(tokenizer, "init_kwargs", None) + if isinstance(init_kwargs, dict) and init_kwargs.get("name_or_path"): + return str(init_kwargs["name_or_path"]) + return None diff --git a/verl_0720_main/verl/verl/utils/tokenizer/tokenizer.py b/verl_0720_main/verl/verl/utils/tokenizer/tokenizer.py new file mode 100644 index 0000000000000000000000000000000000000000..e4baa107d43087d6360e182755c68f113e0a6c38 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tokenizer/tokenizer.py @@ -0,0 +1,248 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Utils for tokenization.""" + +import types +import warnings + +__all__ = [ + "hf_tokenizer", + "hf_processor", + "normalize_token_ids", + "build_multimodal_processor_inputs", + "get_processor_token_id", +] + + +def normalize_token_ids(tokenized_output) -> list[int]: + """Normalize tokenizer outputs into a flat ``list[int]``. + + This handles Transformers 4/5 differences where ``apply_chat_template(tokenize=True)`` + may return either ``list[int]`` or a ``BatchEncoding``/mapping with ``input_ids``. + """ + + token_ids = tokenized_output + if isinstance(tokenized_output, dict): + if "input_ids" in tokenized_output: + token_ids = tokenized_output["input_ids"] + elif hasattr(tokenized_output, "input_ids"): + token_ids = tokenized_output.input_ids + + if hasattr(token_ids, "tolist"): + token_ids = token_ids.tolist() + + if isinstance(token_ids, tuple): + token_ids = list(token_ids) + + if isinstance(token_ids, list) and len(token_ids) == 1 and isinstance(token_ids[0], list | tuple): + token_ids = list(token_ids[0]) + + if not isinstance(token_ids, list): + raise TypeError(f"token_ids must be list-like token ids, got {type(token_ids).__name__}: {token_ids!r}") + + normalized_ids = [] + for idx, token_id in enumerate(token_ids): + if hasattr(token_id, "item"): + token_id = token_id.item() + try: + normalized_ids.append(int(token_id)) + except (TypeError, ValueError) as e: + raise TypeError(f"token_id must be int-convertible, got {type(token_id).__name__}: {token_id!r}") from e + return normalized_ids + + +def get_processor_token_id(processor, token_name: str) -> int | None: + """Resolve a multimodal special token id from a processor. + + Newer processors may expose ``image_token``/``video_token`` strings instead + of ``image_token_id``/``video_token_id`` integers. Fall back to tokenizer + conversion so rollout code can stay processor-agnostic. + """ + + if processor is None: + return None + + token_id_attr = f"{token_name}_token_id" + token_id = getattr(processor, token_id_attr, None) + if token_id is not None: + return int(token_id) + + token_attr = f"{token_name}_token" + token = getattr(processor, token_attr, None) + tokenizer = getattr(processor, "tokenizer", None) + if token is not None and tokenizer is not None: + converted = tokenizer.convert_tokens_to_ids(token) + if converted is not None: + return int(converted) + + return None + + +def _split_videos_and_metadata(videos): + if videos is None: + return None, None + videos = list(videos) + if len(videos) > 0 and isinstance(videos[0], tuple): + video_values, video_metadata = zip(*videos, strict=False) + return list(video_values), list(video_metadata) + return videos, None + + +def build_multimodal_processor_inputs( + processor, + *, + text, + images=None, + videos=None, + audio=None, + mm_processor_kwargs=None, + return_tensors: str = "pt", +): + """Build kwargs for multimodal processor calls. + + This keeps the existing VL flow intact while extending it with audio-aware + paths for processors that accept audio inputs. + """ + processor_kwargs = dict(mm_processor_kwargs or {}) + if audio is not None and "sampling_rate" not in processor_kwargs: + sampling_rate = getattr(getattr(processor, "feature_extractor", None), "sampling_rate", None) + if sampling_rate is not None: + processor_kwargs["sampling_rate"] = int(sampling_rate) + + videos, video_metadata = _split_videos_and_metadata(videos) + processor_kwargs.setdefault("return_tensors", return_tensors) + + if video_metadata is not None: + processor_kwargs.setdefault("video_metadata", video_metadata) + processor_kwargs.setdefault("do_sample_frames", False) + + processor_inputs = {"text": text, "images": images, "videos": videos, **processor_kwargs} + if audio is not None: + processor_inputs["audio"] = audio + + return processor(**processor_inputs) + + +def set_pad_token_id(tokenizer): + """Set pad_token_id to eos_token_id if it is None. + + Args: + tokenizer (transformers.PreTrainedTokenizer): The tokenizer to be set. + + """ + if tokenizer.pad_token_id is None: + tokenizer.pad_token_id = tokenizer.eos_token_id + warnings.warn(f"tokenizer.pad_token_id is None. Now set to {tokenizer.eos_token_id}", stacklevel=1) + if tokenizer.pad_token is None: + tokenizer.pad_token = tokenizer.eos_token + warnings.warn(f"tokenizer.pad_token is None. Now set to {tokenizer.eos_token}", stacklevel=1) + + +def hf_tokenizer(name_or_path, correct_pad_token=True, correct_gemma2=True, **kwargs): + """Create a huggingface pretrained tokenizer which correctness handles eos and pad tokens. + + Args: + + name (str): The name of the tokenizer. + correct_pad_token (bool): Whether to correct the pad token id. + correct_gemma2 (bool): Whether to correct the gemma2 tokenizer. + + Returns: + + transformers.PreTrainedTokenizer: The pretrained tokenizer. + + """ + from transformers import AutoTokenizer + + if correct_gemma2 and isinstance(name_or_path, str) and "gemma-2-2b-it" in name_or_path: + # the EOS token in gemma2 is ambiguious, which may worsen RL performance. + # https://huggingface.co/google/gemma-2-2b-it/commit/17a01657f5c87135bcdd0ec7abb4b2dece04408a + warnings.warn( + "Found gemma-2-2b-it tokenizer. Set eos_token and eos_token_id to and 107.", stacklevel=1 + ) + kwargs["eos_token"] = "" + kwargs["eos_token_id"] = 107 + tokenizer = AutoTokenizer.from_pretrained(name_or_path, **kwargs) + if correct_pad_token: + set_pad_token_id(tokenizer) + return tokenizer + + +def hf_processor(name_or_path, **kwargs): + """Create a huggingface processor to process multimodal data. + + Args: + name_or_path (str): The name of the processor. + + Returns: + Optional[transformers.ProcessorMixin]: The pretrained multimodal processor. + Returns ``None`` for text-only models (including AutoProcessor fallbacks to + tokenizer backends such as ``TokenizersBackend``). + """ + from transformers import AutoConfig, AutoProcessor, PreTrainedTokenizerBase + + try: + processor = AutoProcessor.from_pretrained(name_or_path, **kwargs) + # In newer transformers, AutoProcessor may legitimately fall back to a + # tokenizer backend (e.g. TokenizersBackend) for text-only models. + # Treat it as "no multimodal processor" and let callers use hf_tokenizer. + if isinstance(processor, PreTrainedTokenizerBase): + return None + + config = AutoConfig.from_pretrained(name_or_path, **kwargs) + + # Bind vlm model's get_rope_index method to processor. + processor.config = config + model_class = None + match processor.__class__.__name__: + case "Qwen2VLProcessor": + from transformers.models.qwen2_vl import Qwen2VLModel + + model_class = Qwen2VLModel + case "Qwen2_5_VLProcessor": + from transformers.models.qwen2_5_vl import Qwen2_5_VLModel + + model_class = Qwen2_5_VLModel + case "Qwen3VLProcessor": + from transformers.models.qwen3_vl import Qwen3VLModel + + model_class = Qwen3VLModel + case "Glm4vProcessor": + from transformers.models.glm4v import Glm4vModel + + model_class = Glm4vModel + case "MllamaProcessor": + pass # MllamaProcessor and MllamaModel doesn't have get_rope_index property + case "Gemma4Processor": + # Gemma4 uses standard 1D RoPE -> no get_rope_index to bind. Disable its strict + # per-image-token check (which Qwen's processor lacks). + processor.validate_inputs = lambda *args, **kwargs: None + case _: + raise ValueError(f"Unsupported processor type: {processor.__class__.__name__}") + + if model_class is not None: + processor.get_rope_index = types.MethodType(model_class.get_rope_index, processor) + if hasattr(model_class, "get_vision_position_ids"): + processor.get_vision_position_ids = types.MethodType(model_class.get_vision_position_ids, processor) + except Exception as e: + processor = None + # TODO(haibin.lin): try-catch should be removed after adding transformer version req to setup.py to avoid + # silent failure + warnings.warn(f"Failed to create processor: {e}. This may affect multimodal processing", stacklevel=1) + # Avoid load tokenizer, see: + # https://github.com/huggingface/transformers/blob/v4.49.0/src/transformers/models/auto/processing_auto.py#L344 + if processor is not None and "Processor" not in processor.__class__.__name__: + processor = None + + return processor diff --git a/verl_0720_main/verl/verl/utils/torch_dtypes.py b/verl_0720_main/verl/verl/utils/torch_dtypes.py new file mode 100644 index 0000000000000000000000000000000000000000..f2f445c26140ceeec25c1d3cf5b3df249c6dffb1 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/torch_dtypes.py @@ -0,0 +1,80 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Adapted from Cruise. +""" + +import torch + +HALF_LIST = [16, "16", "fp16", "float16", torch.float16] +FLOAT_LIST = [32, "32", "fp32", "float32", torch.float32] +BFLOAT_LIST = ["bf16", "bfloat16", torch.bfloat16] + + +class PrecisionType: + """Type of precision used. + + >>> PrecisionType.HALF == 16 + True + >>> PrecisionType.HALF in (16, "16") + True + """ + + HALF = "16" + FLOAT = "32" + FULL = "64" + BFLOAT = "bf16" + MIXED = "mixed" + + @staticmethod + def supported_type(precision: str | int) -> bool: + return any(x == precision for x in PrecisionType) + + @staticmethod + def supported_types() -> list[str]: + return [x.value for x in PrecisionType] + + @staticmethod + def is_fp16(precision): + return precision in HALF_LIST + + @staticmethod + def is_fp32(precision): + return precision in FLOAT_LIST + + @staticmethod + def is_bf16(precision): + return precision in BFLOAT_LIST + + @staticmethod + def to_dtype(precision): + if precision in HALF_LIST: + return torch.float16 + elif precision in FLOAT_LIST: + return torch.float32 + elif precision in BFLOAT_LIST: + return torch.bfloat16 + else: + raise RuntimeError(f"unexpected precision: {precision}") + + @staticmethod + def to_str(precision): + if precision == torch.float16: + return "fp16" + elif precision == torch.float32: + return "fp32" + elif precision == torch.bfloat16: + return "bf16" + else: + raise RuntimeError(f"unexpected precision: {precision}") diff --git a/verl_0720_main/verl/verl/utils/torch_functional.py b/verl_0720_main/verl/verl/utils/torch_functional.py new file mode 100644 index 0000000000000000000000000000000000000000..8666bec2d1607c1586c759a1f895ef3c0f0c069f --- /dev/null +++ b/verl_0720_main/verl/verl/utils/torch_functional.py @@ -0,0 +1,1028 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Contain small torch utilities +""" + +import math +from contextlib import contextmanager +from typing import Optional + +import torch +import torch.distributed +import torch.nn.functional as F +from tensordict import TensorDict +from torch import nn +from torch.optim import Optimizer +from torch.optim.lr_scheduler import LambdaLR +from transformers import PreTrainedTokenizer + +from verl.utils.device import get_device_name, get_torch_device + +try: + from flash_attn.ops.triton.cross_entropy import cross_entropy_loss + + FLAH_ATTN_CROSS_ENTROPY_LOSS_AVAILABLE = True +except ImportError: + FLAH_ATTN_CROSS_ENTROPY_LOSS_AVAILABLE = False + + +try: + import torch_npu + + NPU_CROSS_ENTROPY_LOSS_AVAILABLE = hasattr(torch_npu, "npu_cross_entropy_loss") +except ImportError: + NPU_CROSS_ENTROPY_LOSS_AVAILABLE = False + + +def gather_from_labels(data: torch.Tensor, label: torch.Tensor) -> torch.Tensor: + """Gather values from data tensor at positions specified by label indices. + + Selects elements from the last dimension of `data` based on indices in `label`. + Commonly used to extract log-probabilities for specific token IDs from a + vocabulary distribution. + + Args: + data: Input tensor of shape (..., vocab_size) containing values to gather from. + label: Index tensor of shape (...,) with values in range [0, vocab_size). + + Returns: + torch.Tensor: Gathered values with shape (...,), same as label shape. + + Example: + >>> logits = torch.randn(2, 3, 100) # [batch, seq, vocab] + >>> labels = torch.randint(0, 100, (2, 3)) # [batch, seq] + >>> gathered = gather_from_labels(logits, labels) # [batch, seq] + """ + output = torch.gather(data, -1, label.unsqueeze(-1)).squeeze(-1) + return output + + +def logprobs_from_logits(logits, labels, inplace_backward=True): + """ + Compute per-token log-probabilities for the given labels. + + Uses a Flash-Attention–based cross-entropy (if available) for efficient backward, + otherwise falls back to a standard log-softmax+gather approach. + + See: https://github.com/pytorch/pytorch/issues/563#issuecomment-330103591 + + Args: + logits (Tensor): Model outputs of shape (..., vocab_size). + labels (LongTensor): True class indices of shape matching logits[..., :-1]. + inplace_backward (bool): If True and Flash-Attn is available, perform backward in-place. + + Returns: + Tensor: Log-probabilities of the target labels, shape logits.shape[:-1]. + """ + if FLAH_ATTN_CROSS_ENTROPY_LOSS_AVAILABLE: + batch_dim = logits.shape[:-1] + last_dim = logits.shape[-1] + logits = logits.reshape(-1, last_dim) + labels = labels.reshape(-1) + output = logprobs_from_logits_flash_attn(logits, labels, inplace_backward=inplace_backward) + output = output.view(*batch_dim) + elif NPU_CROSS_ENTROPY_LOSS_AVAILABLE: + output = logprobs_from_logits_torch_npu(logits, labels) + else: + output = logprobs_from_logits_v2(logits, labels) + return output + + +def logprobs_from_logits_flash_attn( + logits: torch.Tensor, labels: torch.Tensor, inplace_backward: bool = True +) -> torch.Tensor: + """Compute log-probabilities using Flash Attention's optimized cross-entropy. + + Uses the Flash Attention library's Triton-based cross-entropy implementation + for efficient computation on NVIDIA GPUs. + + Args: + logits: Model output logits of shape (batch_size, vocab_size). + labels: Target token indices of shape (batch_size,). + inplace_backward: If True, perform backward pass in-place for memory efficiency. + + Returns: + torch.Tensor: Log-probabilities for target labels, shape (batch_size,). + + Raises: + AssertionError: If flash-attn version < 2.4.3 (different return format). + """ + output = cross_entropy_loss(logits, labels, inplace_backward=inplace_backward) + assert isinstance(output, tuple), ( + "please make sure flash-attn>=2.4.3 where cross_entropy_loss returns Tuple[losses, z_losses]." + ) + return -output[0] + + +def logprobs_from_logits_torch_npu(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor: + """Compute log-probabilities using Ascend NPU's optimized cross-entropy. + + Uses torch_npu's native cross-entropy implementation for efficient + computation on Huawei Ascend NPU devices. + + Args: + logits: Model output logits of shape (..., vocab_size). + labels: Target token indices of shape (...,). + + Returns: + torch.Tensor: Log-probabilities for target labels, same shape as labels. + """ + batch_dim = logits.shape[:-1] + logits = logits.reshape(-1, logits.shape[-1]) + loss, _, _, _ = torch_npu.npu_cross_entropy_loss(logits, labels.reshape(-1), reduction="none") + return -loss.view(*batch_dim) + + +def logprobs_from_logits_naive(logits: torch.Tensor, labels: torch.Tensor) -> torch.Tensor: + """Compute log-probabilities using standard log-softmax approach. + + Simple implementation using PyTorch's log_softmax followed by gathering. + Less memory-efficient than specialized implementations but works on all devices. + + Args: + logits: Model output logits of shape (..., vocab_size). + labels: Target token indices of shape (...,). + + Returns: + torch.Tensor: Log-probabilities for target labels, same shape as labels. + """ + logp = F.log_softmax(logits, dim=-1) + logpy = gather_from_labels(logp, labels) + return logpy + + +def logprobs_from_logits_v2(logits: torch.FloatTensor, labels: torch.Tensor) -> torch.Tensor: + """Memory-efficient log-probability computation using row-wise processing. + + Computes log-probabilities by processing one row at a time to reduce peak + memory consumption. Uses logsumexp for float32/float64, falls back to + log_softmax for bfloat16 due to numerical stability concerns. + + The mathematical identity used is: log_softmax(x_i) = x_i - logsumexp(x) + + Args: + logits: Model output logits of shape (batch_size, seq_len, vocab_size) + or (batch_size, vocab_size). + labels: Target token indices matching logits shape without vocab dimension. + + Returns: + torch.Tensor: Log-probabilities for target labels. + + Note: + This implementation trades compute for memory by iterating over batch + dimension, making it suitable for large vocabulary sizes. + """ + if logits.dtype in [torch.float32, torch.float64]: + logits_labels = torch.gather(logits, dim=-1, index=labels.unsqueeze(-1)).squeeze(-1) + # loop to reduce peak mem consumption + logsumexp_values = torch.stack([torch.logsumexp(logit, dim=-1) for logit in logits]) + logprobs_labels = logits_labels - logsumexp_values # log_softmax(x_i) = x_i - logsumexp(x) + else: + # logsumexp approach is unstable with bfloat16, fall back to slightly less efficent approach + logprobs_labels = [] + for row_logits, row_labels in zip(logits, labels, strict=True): # loop to reduce peak mem consumption + row_logprobs = F.log_softmax(row_logits, dim=-1) + row_logprobs_labels = row_logprobs.gather(dim=-1, index=row_labels.unsqueeze(-1)).squeeze(-1) + logprobs_labels.append(row_logprobs_labels) + logprobs_labels = torch.stack(logprobs_labels) + return logprobs_labels + + +def clip_by_value(x: torch.Tensor, tensor_min: torch.Tensor, tensor_max: torch.Tensor) -> torch.Tensor: + """Clip tensor values to a range defined by tensor bounds. + + Extension of torch.clamp that supports tensor-valued min/max bounds + instead of only scalar bounds. + + Args: + x: Input tensor to clip. + tensor_min: Minimum bound tensor (broadcastable to x). + tensor_max: Maximum bound tensor (broadcastable to x). + + Returns: + torch.Tensor: Clipped tensor with values in [tensor_min, tensor_max]. + + See Also: + https://github.com/pytorch/pytorch/issues/2793#issuecomment-428784713 + """ + clipped = torch.max(torch.min(x, tensor_max), tensor_min) + return clipped + + +def entropy_from_logits(logits: torch.Tensor) -> torch.Tensor: + """Calculate Shannon entropy from unnormalized logits. + + Computes H(p) = -sum(p * log(p)) using the numerically stable formula: + entropy = logsumexp(logits) - sum(softmax(logits) * logits) + + Args: + logits: Unnormalized log-probabilities of shape (..., vocab_size). + + Returns: + torch.Tensor: Entropy values with shape (...,), one per distribution. + """ + pd = torch.nn.functional.softmax(logits, dim=-1) + entropy = torch.logsumexp(logits, dim=-1) - torch.sum(pd * logits, dim=-1) + return entropy + + +def entropy_from_logits_with_chunking(logits: torch.Tensor, chunk_size: int = 2048) -> torch.Tensor: + """Memory-efficient entropy calculation using chunked processing. + + Computes entropy by processing the batch in chunks to reduce peak memory + usage. Useful for large batch sizes or when memory is constrained. + + Args: + logits: Unnormalized log-probabilities of shape (batch_size, vocab_size). + chunk_size: Number of samples to process at once. Defaults to 2048. + + Returns: + torch.Tensor: Entropy values with shape (batch_size,). + + Note: + Converts chunks to float32 for numerical stability during computation. + """ + entropy = torch.zeros(logits.shape[0], device=logits.device) + for i in range(0, logits.shape[0], chunk_size): + logits_chunk = logits[i : i + chunk_size].float() + pd_chunk = torch.nn.functional.softmax(logits_chunk, dim=-1) + entropy_chunk = torch.logsumexp(logits_chunk, dim=-1) - torch.sum(pd_chunk * logits_chunk, dim=-1) + entropy[i : i + chunk_size] = entropy_chunk + return entropy + + +def masked_sum(values: torch.Tensor, mask: torch.Tensor, axis: int | tuple[int, ...] | None = None) -> torch.Tensor: + """Compute sum of tensor values where mask is True. + + NaN values outside the mask are replaced with zeros to prevent + contaminating the sum. + + Args: + values: Input tensor containing values to sum. + mask: Boolean or numeric mask tensor (same shape as values). + Non-zero values indicate elements to include. + axis: Dimension(s) along which to sum. None sums all elements. + + Returns: + torch.Tensor: Sum of masked values, reduced along specified axis. + """ + # If NaNs exist out of mask, replace NaNs in values with a value that + # won't affect the sum (e.g., 0 for masked regions) + valid_values = torch.where(mask.bool(), values, 0.0) + return (valid_values * mask).sum(axis=axis) + + +def masked_mean(values, mask, axis=None): + """ + Compute the mean of `values` over elements selected by `mask`. + + Args: + values (Tensor): Input tensor. + mask (Tensor): Boolean or numeric mask of the same shape as `values`. + axis (int or tuple of int, optional): Dimension(s) along which to compute the mean. + Defaults to None (over all elements). + + Returns: + Tensor: Masked mean, with shape equal to `values` reduced over `axis`. + """ + s = masked_sum(values, mask, axis) + return s / (mask.sum(axis=axis) + 1e-8) + + +def masked_var(values, mask, unbiased=True): + """Compute variance of tensor with masked values.""" + mean = masked_mean(values, mask) + centered_values = values - mean + variance = masked_mean(centered_values**2, mask) + if unbiased: + mask_sum = mask.sum() + if mask_sum == 0: + raise ValueError("At least one element in the mask has to be 1.") + # note that if mask_sum == 1, then there is a division by zero issue + # to avoid it you just need to use a larger minibatch_size + if mask_sum == 1: + raise ValueError("The sum of the mask is one, which can cause a division by zero.") + bessel_correction = mask_sum / (mask_sum - 1) + variance = variance * bessel_correction + return variance + + +def masked_whiten(values, mask, shift_mean=True): + """ + Whiten `values` by normalizing with mean and variance computed over `mask`. + + Args: + values (torch.Tensor): Input tensor. + mask (torch.Tensor): Boolean tensor of same shape, selects elements for stats. + shift_mean (bool): If True (default), output is zero-mean; + if False, the original mean is re-added after scaling. + + Returns: + torch.Tensor: Whitened tensor of same shape as `values`. + """ + mean, var = masked_mean(values, mask), masked_var(values, mask) + whitened = (values - mean) * torch.rsqrt(var + 1e-8) + if not shift_mean: + whitened += mean + return whitened + + +def get_response_mask(response_id: torch.Tensor, eos_token: int | list[int] = 2, dtype=torch.int64): + """ + end of sentence token can be int or list: 1 or [1, 2] + e.g. + response_id = torch.tensor([[20, 10, 34, 1, 0, 0, 0], + [78, 0, 76, 2, 1, 0, 0], + [23, 98, 1, 0, 0, 0, 0], + [33, 3, 98, 45, 1, 0, 0]]) + #eos_token=1 + response_mask: tensor([[1, 1, 1, 1, 0, 0, 0], + [1, 1, 1, 1, 1, 0, 0], + [1, 1, 1, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 0, 0]]) + #eos_token=[1,2] + response_mask: tensor([[1, 1, 1, 1, 0, 0, 0], + [1, 1, 1, 1, 0, 0, 0], + [1, 1, 1, 0, 0, 0, 0], + [1, 1, 1, 1, 1, 0, 0]]) + """ + eos_mask = torch.isin(response_id, torch.tensor(eos_token, device=response_id.device)).int() + return (eos_mask.cumsum(dim=1) - eos_mask).eq(0).to(dtype) + + +def compute_grad_norm(model: nn.Module) -> float: + """Compute the squared L2 norm of all gradients in a model. + + Sums the squared values of all gradient tensors across all parameters. + Useful for monitoring gradient magnitudes during training. + + Args: + model: PyTorch model with computed gradients. + + Returns: + float: Sum of squared gradient values (not the square root). + + Note: + Returns the squared norm, not the norm itself. To get the actual + L2 norm, take the square root of the returned value. + """ + total_grad_square = 0 + for param in model.parameters(): + if param.grad is not None: + total_grad_square += torch.sum(torch.square(param.grad.detach())).item() + return total_grad_square + + +def broadcast_dict_tensor(tensors: dict[str, torch.Tensor] | TensorDict, src: int, group) -> None: + """Broadcast all tensors in a dictionary from source rank to all ranks. + + Iterates over all tensors in the dictionary and broadcasts each one + from the source rank to all other ranks in the process group. + + Args: + tensors: Dictionary or TensorDict containing tensors to broadcast. + src: Source rank from which to broadcast. + group: Process group for the broadcast operation. + + Note: + This implementation broadcasts tensors one at a time. Could be optimized + to use a single broadcast with packed tensors. + """ + for key in tensors.sorted_keys: + torch.distributed.broadcast(tensors[key], src=src, group=group, async_op=False) + + +def allgather_dict_tensors( + tensors: dict[str, torch.Tensor] | TensorDict, size: int, group, dim: int = 0 +) -> dict[str, torch.Tensor] | TensorDict: + """Gather tensors from all ranks and concatenate them. + + Performs all_gather on each tensor in the dictionary and concatenates + the results along the specified dimension. + + Args: + tensors: Dictionary or TensorDict containing tensors to gather. + size: Number of ranks in the process group. + group: Process group for the all_gather operation. + dim: Dimension along which to concatenate gathered tensors. Defaults to 0. + + Returns: + Dictionary or TensorDict (matching input type) with gathered and + concatenated tensors. Each tensor's size along `dim` is multiplied by `size`. + + Note: + This implementation gathers tensors one at a time synchronously. + Could be optimized using async ops or packed all_gather. + """ + if isinstance(tensors, TensorDict): + is_tensor_dict = True + tensors_as_dict = tensors.to_dict() + else: + tensors_as_dict = tensors + is_tensor_dict = False + + output = {} + sorted_keys = sorted(tensors_as_dict.keys()) + for key in sorted_keys: + val = tensors_as_dict[key] + output[key] = [torch.empty_like(val) for _ in range(size)] + torch.distributed.all_gather(output[key], val, group=group, async_op=False) + output[key] = torch.cat(output[key], dim=dim) + + if is_tensor_dict: + output = TensorDict(source=output, batch_size=tensors.batch_size[0] * size) + + return output + + +def allgather_dict_into_dict(data: dict, group=None) -> dict: + """allgather a dict into a dict of list + + Args: + data: a dict + group: the process group to allgather + + Returns: dict containing a list of the results from allgather + + """ + assert isinstance(data, dict), f"Expect data to be a dictionary, Got {type(data)}" + + group_size = torch.distributed.get_world_size(group=group) + + final_metrics = {} + all_metrics_lst = [None for _ in range(group_size)] + torch.distributed.all_gather_object(all_metrics_lst, data, group=group) + + for all_metrics in all_metrics_lst: + for key, val in all_metrics.items(): + if key not in final_metrics: + final_metrics[key] = [] + final_metrics[key].append(val) + return final_metrics + + +def split_dict_tensor_into_batches(tensors: TensorDict, batch_size) -> list[TensorDict]: + assert tensors.batch_size[0] % batch_size == 0, ( + f"input data batch size: {tensors.batch_size[0]}, split batch size: {batch_size}" + ) + return tensors.split(batch_size) + + +def pad_2d_list_to_length(response, pad_token_id, max_length=None): + """ + pad a 2D list (e.g. responses, logprobs) to a 2D tensor. + """ + response_length = max(len(sub_list) for sub_list in response) + target_length = max_length if max_length is not None and max_length > response_length else response_length + padded_response = [tuple(sub_list) + (pad_token_id,) * (target_length - len(sub_list)) for sub_list in response] + tensor = torch.tensor(padded_response) + return tensor + + +def pad_sequence_to_length(tensors, max_seq_len, pad_token_id, left_pad=False): + """ + pad a 2D tensors (e.g. responses, logprobs) in the last dim to max_seq_length. + input shape: [bs, seq_length] + output shape: [bs, max_seq_length] + """ + if tensors.shape[-1] >= max_seq_len: + return tensors + # (0, max_seq_len - tensors.shape[-1]) means right pad to max_seq_length and no left pad + pad_tuple = (max_seq_len - tensors.shape[-1], 0) if left_pad else (0, max_seq_len - tensors.shape[-1]) + return F.pad(tensors, pad_tuple, "constant", pad_token_id) + + +def postprocess_data( + input_ids: torch.Tensor, + attention_mask: torch.Tensor, + max_length: int, + pad_token_id: int, + left_pad=True, + truncation="error", +): + """Process tokenizer outputs to consistent shapes via padding/truncation. + + Args: + input_ids: Token indices [batch_size, seq_len] + attention_mask: Mask [batch_size, seq_len] + max_length: Target sequence length + pad_token_id: Padding token ID + left_pad: Pad left if True + truncation: "left", "right", "middle" or "error" + + Returns: + (input_ids, attention_mask) padded/truncated to max_length + """ + assert truncation in ["left", "right", "middle", "error"] + assert input_ids.ndim == 2 + + sequence_length = input_ids.shape[-1] + if sequence_length < max_length: + input_ids = pad_sequence_to_length( + input_ids, max_seq_len=max_length, pad_token_id=pad_token_id, left_pad=left_pad + ) + attention_mask = pad_sequence_to_length( + attention_mask, max_seq_len=max_length, pad_token_id=0, left_pad=left_pad + ) + elif sequence_length > max_length: + if truncation == "left": + # actually, left truncation may not be reasonable + input_ids = input_ids[:, -max_length:] + attention_mask = attention_mask[:, -max_length:] + elif truncation == "right": + input_ids = input_ids[:, :max_length] + attention_mask = attention_mask[:, :max_length] + elif truncation == "middle": + left_half = max_length // 2 + right_half = max_length - left_half + input_ids = torch.cat([input_ids[:, :left_half], input_ids[:, -right_half:]], dim=-1) + attention_mask = torch.cat([attention_mask[:, :left_half], attention_mask[:, -right_half:]], dim=-1) + elif truncation == "error": + raise NotImplementedError(f"{sequence_length=} is larger than {max_length=}") + else: + raise NotImplementedError(f"Unknown truncation method {truncation}") + + return input_ids, attention_mask + + +def tokenize_and_postprocess_data( + prompt: str, tokenizer: PreTrainedTokenizer, max_length: int, pad_token_id: int, left_pad=True, truncation="error" +): + """Tokenize text and process outputs to consistent tensor shapes. + + Args: + prompt: Input text to tokenize + tokenizer: HuggingFace tokenizer instance + max_length: Target sequence length + pad_token_id: Padding token ID + left_pad: Pad left if True + truncation: Truncation strategy ("left"/"right"/"error") + + Returns: + Tuple of (input_ids, attention_mask) from postprocess_data + """ + input_data = tokenizer(prompt, return_tensors="pt", add_special_tokens=False) + input_ids = input_data["input_ids"] + attention_mask = input_data["attention_mask"] + + return postprocess_data(input_ids, attention_mask, max_length, pad_token_id, left_pad, truncation) + + +def remove_pad_token(input_ids: torch.Tensor, attention_mask: torch.Tensor): + """Remove the pad token. + + Args: + input_ids shape: [bs, seq_length] + attention_mask shape: [bs, seq_length] + Returns: + no_padding_batch(List[List[int]]): contains the rmpad token ids per query. + """ + no_padding_batch = [] + for ids, mask in zip(input_ids, attention_mask, strict=True): + no_padding_batch.append((ids[len(ids) - mask.sum() :]).cpu().numpy().tolist()) + return no_padding_batch + + +def log_probs_from_logits_response(input_ids, logits, response_length): + """Compute the response log_probs from full logits. Note that logits = model(input_ids) + + Args: + input_ids: [batch_size, seqlen] + logits: [batch_size, seqlen, vocab_size] + + Returns: + response_log_prob: + """ + response_logits = logits[:, -response_length - 1 : -1] + response = input_ids[:, -response_length:] + response_log_prob = logprobs_from_logits(logits=response_logits, labels=response) + return response_log_prob + + +def log_probs_from_logits_response_rmpad(input_ids, attention_mask, logits_rmpad, response_length): + """Compute the log_probs from logits with rmpad logits and pad input. Note that + logits_rmpad = model(input_ids_rmpad). For each sentences, there is a shift between + logits and input_ids. + The reason for this function to is to compute logprobs_from_logits in rmpad mode because it is memory-intensive + for large vocab_size + + Args: + input_ids: [batch_size, seqlen] + attention_mask: [batch_size, seqlen] + logits_rmpad: [total_nnz, vocab_size] + response_length: int + """ + from flash_attn.bert_padding import pad_input, unpad_input + + batch_size, seqlen = input_ids.shape + input_ids_rmpad, indices, *_ = unpad_input(input_ids.unsqueeze(-1), attention_mask=attention_mask) + input_ids_rmpad = input_ids_rmpad.squeeze(-1) + input_ids_rmpad_rolled = torch.roll(input_ids_rmpad, shifts=-1, dims=0) + full_log_probs_rmpad = logprobs_from_logits(logits=logits_rmpad, labels=input_ids_rmpad_rolled) # (total_nnz,) + full_output = pad_input( + hidden_states=full_log_probs_rmpad.unsqueeze(-1), indices=indices, batch=batch_size, seqlen=seqlen + ) + output = full_output.squeeze(-1)[:, -response_length - 1 : -1] # [batch_size, response_length] + return output + + +def log_probs_from_logits_all_rmpad(input_ids_rmpad, logits_rmpad, indices, batch_size, seqlen, response_length): + """Compute the log_probs from logits with rmpad input_ids and logits. Note that + logits_rmpad = model(input_ids_rmpad). For each sentences, there is a shift between + logits and input_ids. + The reason for this function to is to compute logprobs_from_logits in rmpad mode because it is memory-intensive + for large vocab_size + + Args: + input_ids_rmpad: [1, total_nnz] + logits_rmpad: [total_nnz, vocab_size] + indices: [total_nnz] + batch_size: int + seqlen: int + response_length: int + """ + if get_device_name() == "cuda": + from flash_attn.bert_padding import pad_input + elif get_device_name() == "npu": + from verl.utils.attention_utils import pad_input + + input_ids_rmpad = input_ids_rmpad.transpose(0, 1) # transpose back to [total_nnz, 1] + input_ids_rmpad = input_ids_rmpad.squeeze(-1) + input_ids_rmpad_rolled = torch.roll(input_ids_rmpad, shifts=-1, dims=0) + full_log_probs_rmpad = logprobs_from_logits(logits=logits_rmpad, labels=input_ids_rmpad_rolled) # (total_nnz,) + full_output = pad_input( + hidden_states=full_log_probs_rmpad.unsqueeze(-1), indices=indices, batch=batch_size, seqlen=seqlen + ) + output = full_output.squeeze(-1)[:, -response_length - 1 : -1] # [batch_size, response_length] + return output + + +def post_process_logits(input_ids, logits, temperature, top_k, top_p): + if temperature != 1.0: + logits = logits.div_(temperature) # inplace operation to avoid OOM + # TODO: add them back + # if top_k is not None and top_k > 0: + # logits = TopKLogitsWarper(top_k=top_k)(input_ids, logits) + # if top_p is not None and top_p < 1.0 and top_p > 0.0: + # logits = TopPLogitsWarper(top_p=top_p)(input_ids, logits) + return logits + + +def calculate_sum_pi_squared_from_logits(logits: torch.Tensor): + """ + Compute exact sum of squared probabilities from logits. + Formula: Σπ² = exp(logsumexp(2*logits) - 2*logsumexp(logits)) + + Used for optimal baseline variance reduction as described in + "What Matters for Model Merging at Scale?" (arXiv:2410.03617) + + Args: + logits: Logits tensor (..., vocab_size). + + Returns: + Sum of squared probabilities tensor (...). + """ + return torch.exp(torch.logsumexp(2.0 * logits, dim=-1) - 2.0 * torch.logsumexp(logits, dim=-1)) + + +""" +Optimizer related +""" + + +def get_cosine_schedule_with_warmup( + optimizer: Optimizer, + num_warmup_steps: int, + num_training_steps: int, + min_lr_ratio: float = 0.0, + num_cycles: float = 0.5, + last_epoch: int = -1, + init_lr_ratio: float = None, + zero_indexed_step: bool = True, +): + """ + Create a schedule with a learning rate that decreases following the values of the cosine function between the + initial lr set in the optimizer to 0, after a warmup period during which it increases linearly between 0 and the + initial lr set in the optimizer. + Args: + optimizer (:class:`~torch.optim.Optimizer`): + The optimizer for which to schedule the learning rate. + num_warmup_steps (:obj:`int`): + The number of steps for the warmup phase. + num_training_steps (:obj:`int`): + The total number of training steps. + min_lr_ratio (:obj:`float`, `optional`, defaults to 0.0): + The minimum lr ratio w.r.t the maximum. + num_cycles (:obj:`float`, `optional`, defaults to 0.5): + The number of waves in the cosine schedule (the defaults is to just decrease from the max value to 0 + following a half-cosine). + last_epoch (:obj:`int`, `optional`, defaults to -1): + The index of the last epoch when resuming training. + init_lr_ratio (:obj:`float`, `optional`, defaults to None): + The initial lr ratio w.r.t the maximum. + zero_indexed_step (:obj:`bool`, `optional`, defaults to True): + Whether the LR schedule uses 0-indexed steps. If True (default), step counting starts at 0. + If False (used by torchtitan), step counting starts at 1. + Return: + :obj:`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. + """ + min_lr_ratio = 0.0 if min_lr_ratio is None else min_lr_ratio + assert min_lr_ratio >= 0 and min_lr_ratio <= 1.0 + coef = (1 - min_lr_ratio) * 0.5 + intercept = (1 + min_lr_ratio) * 0.5 + + init_lr_ratio = 0.0 if init_lr_ratio is None else init_lr_ratio + assert init_lr_ratio >= 0 and init_lr_ratio <= 1.0 + + def lr_lambda(current_step): + if not zero_indexed_step: + current_step += 1 + if current_step < num_warmup_steps: + return init_lr_ratio + (1.0 - init_lr_ratio) * (float(current_step) / float(max(1, num_warmup_steps))) + progress = float(current_step - num_warmup_steps) / float(max(1, num_training_steps - num_warmup_steps)) + x = math.cos(math.pi * float(num_cycles) * 2.0 * progress) + return max(min_lr_ratio, x * coef + intercept) + + return LambdaLR(optimizer, lr_lambda, last_epoch) + + +def get_constant_schedule_with_warmup( + optimizer: Optimizer, + num_warmup_steps: int, + last_epoch: int = -1, +): + """ + Create a constant LR schedule with a linear warmup phase. + + Args: + optimizer (Optimizer): Wrapped optimizer. + num_warmup_steps (int): Number of steps to ramp up the LR from 0 to initial value. + last_epoch (int, optional): The index of the last epoch when resuming training. Defaults to -1. + + Returns: + LambdaLR: Scheduler that increases LR linearly during warmup, then holds it constant. + """ + + def lr_lambda(current_step): + if current_step < num_warmup_steps: + return float(current_step) / float(max(1.0, num_warmup_steps)) + return 1.0 + + return LambdaLR(optimizer, lr_lambda, last_epoch) + + +def prepare_decoder_attention_mask(attention_mask, input_shape, inputs_embeds): + # create causal mask + # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] + combined_attention_mask = None + if input_shape[-1] > 1: + combined_attention_mask = _make_causal_mask( + input_shape, + inputs_embeds.dtype, + device=inputs_embeds.device, + ) + + if attention_mask is not None: + # [bsz, seq_len] -> [bsz, 1, tgt_seq_len, src_seq_len] + expanded_attn_mask = _expand_mask(attention_mask, inputs_embeds.dtype, tgt_len=input_shape[-1]).to( + inputs_embeds.device + ) + combined_attention_mask = ( + expanded_attn_mask if combined_attention_mask is None else expanded_attn_mask + combined_attention_mask + ) + + return combined_attention_mask + + +# Copied from transformers.models.bart.modeling_bart._make_causal_mask +def _make_causal_mask(input_ids_shape: torch.Size, dtype: torch.dtype, device: torch.device): + """ + Make causal mask used for bi-directional self-attention. + """ + bsz, tgt_len = input_ids_shape + mask = torch.full((tgt_len, tgt_len), torch.finfo(dtype).min, device=device) + mask_cond = torch.arange(mask.size(-1), device=device) + mask.masked_fill_(mask_cond < (mask_cond + 1).view(mask.size(-1), 1), 0) + mask = mask.to(dtype) + return mask[None, None, :, :].expand(bsz, 1, tgt_len, tgt_len) + + +# Copied from transformers.models.bart.modeling_bart._expand_mask +def _expand_mask(mask: torch.Tensor, dtype: torch.dtype, tgt_len: Optional[int] = None): + """ + Expands attention_mask from `[bsz, seq_len]` to `[bsz, 1, tgt_seq_len, src_seq_len]`. + """ + bsz, src_len = mask.size() + tgt_len = tgt_len if tgt_len is not None else src_len + + expanded_mask = mask[:, None, None, :].expand(bsz, 1, tgt_len, src_len).to(dtype) + + inverted_mask = 1.0 - expanded_mask + + return inverted_mask.masked_fill(inverted_mask.to(torch.bool), torch.finfo(dtype).min) + + +def get_unpad_data(attention_mask): + seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32) + indices = torch.nonzero(attention_mask.flatten(), as_tuple=False).flatten() + max_seqlen_in_batch = seqlens_in_batch.max().item() + cu_seqlens = F.pad(torch.cumsum(seqlens_in_batch, dim=0, dtype=torch.int32), (1, 0)) + return ( + indices, + cu_seqlens, + max_seqlen_in_batch, + ) + + +def get_wsd_schedule_with_warmup( + optimizer: Optimizer, + num_warmup_steps: int, + num_training_steps: int, + min_lr_ratio: float = 0.0, + num_cycles: float = 0.5, + last_epoch: int = -1, + stable_ratio: float = 0.9, +): + """ + Create a Warmup-Stable-Decay learning rate scheduler. + + The schedule follows three phases: + 1. Warmup: Learning rate increases linearly from 0 to the initial LR + 2. Stable: Learning rate remains constant at the initial LR + 3. Decay: Learning rate decreases following a cosine curve to min_lr_ratio * initial LR + + Args: + optimizer (:class:`~torch.optim.Optimizer`): + The optimizer for which to schedule the learning rate. + num_warmup_steps (:obj:`int`): + The number of steps for the warmup phase. + num_training_steps (:obj:`int`): + The total number of training steps. + min_lr_ratio (:obj:`float`, `optional`, defaults to 0.0): + The minimum learning rate ratio w.r.t the initial learning rate. + num_cycles (:obj:`float`, `optional`, defaults to 0.5): + The number of waves in the cosine schedule during decay phase. + last_epoch (:obj:`int`, `optional`, defaults to -1): + The index of the last epoch when resuming training. + stable_ratio (:obj:`float`, `optional`, defaults to 0.0): + The ratio of non-warmup steps that should maintain a constant learning rate. + Set to 0.0 to behave exactly like cosine schedule. + + Return: + :obj:`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. + """ + remaining_steps = max(0, num_training_steps - num_warmup_steps) + num_stable_steps = int(remaining_steps * stable_ratio) + num_decay_steps = remaining_steps - num_stable_steps + + def lr_lambda(current_step): + if current_step < num_warmup_steps: + return float(current_step) / float(max(1, num_warmup_steps)) + if current_step < num_warmup_steps + num_stable_steps: + return 1.0 + if current_step < num_training_steps: + progress = float(current_step - num_warmup_steps - num_stable_steps) / float(max(1, num_decay_steps)) + value = max(0.0, 0.5 * (1.0 + math.cos(math.pi * float(num_cycles) * 2.0 * progress))) + return (1.0 - min_lr_ratio) * value + min_lr_ratio + return min_lr_ratio + + return LambdaLR(optimizer, lr_lambda, last_epoch) + + +@contextmanager +def check_device_is_available(): + """ + Some modules must be imported after CUDA is initialized. Such as sglang's sharding manager. + + This context manager checks if CUDA is available and raises an error if it is not. + """ + if not get_torch_device().is_available(): + raise RuntimeError("Device {} must be initialized before importing this module.".format(get_device_name())) + + yield + + +def distributed_mean_max_min_std(local_tensor, compute_max=True, compute_min=True, compute_std=True): + """Compute distributed statistics across all processes. + + Args: + local_tensor: Tensor containing local values + compute_max: Include maximum value calculation + compute_min: Include minimum value calculation + compute_std: Include standard deviation calculation + + Returns: + Tuple containing (mean, max, min, std) in this order. None for disabled metrics. + """ + # Sum the local tensor across all processes + local_sum = torch.sum(local_tensor) + local_num = torch.tensor(torch.numel(local_tensor), device=get_device_name()) + + torch.distributed.all_reduce(local_sum, op=torch.distributed.ReduceOp.SUM) + torch.distributed.all_reduce(local_num, op=torch.distributed.ReduceOp.SUM) + + global_mean = local_sum / local_num + + if compute_max: + local_max = torch.max(local_tensor) + torch.distributed.all_reduce(local_max, op=torch.distributed.ReduceOp.MAX) + else: + local_max = None + + if compute_min: + local_min = torch.min(local_tensor) + torch.distributed.all_reduce(local_min, op=torch.distributed.ReduceOp.MIN) + else: + local_min = None + + if compute_std: + square_diff = torch.sum(torch.pow(local_tensor - global_mean, 2)) + torch.distributed.all_reduce(square_diff, op=torch.distributed.ReduceOp.SUM) + global_std = torch.sqrt(square_diff / (local_num - 1)) + else: + global_std = None + + return global_mean, local_max, local_min, global_std + + +def distributed_masked_mean(local_tensor, local_mask): + """Compute global mean of non-masked elements across distributed processes. + + Args: + local_tensor (torch.Tensor): Input tensor with local values + local_mask (torch.Tensor): Binary mask (1=valid, 0=ignore) matching local_tensor shape + + Returns: + torch.Tensor: Global mean of all valid elements across processes + """ + local_tensor = local_tensor * local_mask + + local_sum = torch.sum(local_tensor) + local_num = torch.sum(local_mask) + + torch.distributed.all_reduce(local_sum, op=torch.distributed.ReduceOp.SUM) + torch.distributed.all_reduce(local_num, op=torch.distributed.ReduceOp.SUM) + + global_mean = local_sum / local_num + return global_mean + + +def expand_as_nested(tensor: torch.Tensor, nested_tensor: torch.Tensor) -> torch.Tensor: + """ + + Args: + tensor: a tensor with shape (bsz,) + nested_tensor: a nested tensor with shape (bsz, xxx) + + Returns: + a tensor with the same shape as nested_tensor + + """ + assert nested_tensor.is_nested, "nested_tensor must be nested" + assert tensor.shape[0] == nested_tensor.shape[0], ( + f"The batch shape must be the same. Got {tensor.shape[0]} vs {nested_tensor.shape[0]}" + ) + assert len(tensor.shape) == 1, "The ndim of tensor must be 1" + assert len(nested_tensor.shape) == 2, "The ndim of nested_tensor must be 2" + + offsets = nested_tensor.offsets() + seqlens = offsets.diff() + output = torch.repeat_interleave(tensor, seqlens, dim=0) + output = torch.nested.nested_tensor_from_jagged(values=output, offsets=offsets) + return output + + +@contextmanager +def use_original_torch_compile(): + """torch.compile might be replaced by mindspeed on NPU, this contextmanager + can revert torch.compile temporarily. + """ + try: + from mindspeed.patch_utils import MindSpeedPatchesManager + + compile_patch = None + for patch in MindSpeedPatchesManager.patches_info.values(): + if patch.orig_module_name == "torch" and patch.orig_func_name == "compile": + if patch.is_applied(): + compile_patch = patch + break + if compile_patch is not None: + compile_patch.remove_patch() + yield + compile_patch.apply_patch() + else: + yield + except Exception: + yield diff --git a/verl_0720_main/verl/verl/utils/tracking.py b/verl_0720_main/verl/verl/utils/tracking.py new file mode 100644 index 0000000000000000000000000000000000000000..e7c9ff273902bd753ed41136c29a6992a679791b --- /dev/null +++ b/verl_0720_main/verl/verl/utils/tracking.py @@ -0,0 +1,728 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +A unified tracking interface that supports logging data to different backend +""" + +import dataclasses +import json +import logging +import os +from contextlib import contextmanager +from enum import Enum +from functools import partial +from pathlib import Path +from typing import Any + +import orjson + +logger = logging.getLogger(__name__) + +MLFLOW_MAX_ATTEMPTS = 3 +MLFLOW_SLEEP_SECONDS = 5 + + +class Tracking: + """A unified tracking interface for logging experiment data to multiple backends. + + This class provides a centralized way to log experiment metrics, parameters, and artifacts + to various tracking backends including WandB, MLflow, SwanLab, TensorBoard, and console. + + Attributes: + supported_backend: List of supported tracking backends. + logger: Dictionary of initialized logger instances for each backend. + """ + + supported_backend = [ + "wandb", + "mlflow", + "swanlab", + "vemlp_wandb", + "tensorboard", + "console", + "clearml", + "trackio", + "file", + "rl_insight", + ] + + def __init__(self, project_name, experiment_name, default_backend: str | list[str] = "console", config=None): + if isinstance(default_backend, str): + default_backend = [default_backend] + for backend in default_backend: + if backend == "tracking": + import warnings + + warnings.warn("`tracking` logger is deprecated. use `wandb` instead.", DeprecationWarning, stacklevel=2) + else: + assert backend in self.supported_backend, f"{backend} is not supported" + + self.logger = {} + + if "tracking" in default_backend or "wandb" in default_backend: + import os + + import wandb + + settings = None + if config and config["trainer"].get("wandb_proxy", None): + settings = wandb.Settings(https_proxy=config["trainer"]["wandb_proxy"]) + entity = os.environ.get("WANDB_ENTITY", None) + wandb.init(project=project_name, name=experiment_name, entity=entity, config=config, settings=settings) + self.logger["wandb"] = wandb + + if "trackio" in default_backend: + import trackio + from trackio import context_vars + + if context_vars.current_run.get() is None: + trackio.init(project=project_name, name=experiment_name, config=config) + self.logger["trackio"] = _TrackioLoggingAdapter(trackio) + + if "mlflow" in default_backend: + import os + import time + + import mlflow + + for _mlflow_attempt in range(1, MLFLOW_MAX_ATTEMPTS + 1): + try: + MLFLOW_TRACKING_URI = os.environ.get("MLFLOW_TRACKING_URI", "sqlite:////tmp/mlruns.db") + logger.info("Using MLFlow tracking URI: %s", MLFLOW_TRACKING_URI) + mlflow.set_tracking_uri(MLFLOW_TRACKING_URI) + + # Some cloud providers like Azure ML or Databricks automatically set MLFLOW_RUN_ID + # If set, attach to the existing run instead of creating a new one + run_id = os.environ.get("MLFLOW_RUN_ID") + if run_id: + mlflow.start_run(run_id=run_id) + else: + # Project_name is actually experiment_name in MLFlow + # If experiment does not exist, will create a new experiment + experiment = mlflow.set_experiment(project_name) + mlflow.start_run(experiment_id=experiment.experiment_id, run_name=experiment_name) + + mlflow.log_params(_compute_mlflow_params_from_objects(config)) + self.logger["mlflow"] = _MlflowLoggingAdapter() + break # Success + except Exception as e: + logger.warning( + "MLflow initialization attempt %d/%d failed: %s", _mlflow_attempt, MLFLOW_MAX_ATTEMPTS, e + ) + if _mlflow_attempt < MLFLOW_MAX_ATTEMPTS: + time.sleep(MLFLOW_SLEEP_SECONDS) + else: + logger.warning("All MLflow initialization attempts failed. Proceeding without MLflow tracking.") + + if "swanlab" in default_backend: + import os + + import swanlab + + SWANLAB_API_KEY = os.environ.get("SWANLAB_API_KEY", None) + SWANLAB_LOG_DIR = os.environ.get("SWANLAB_LOG_DIR", "swanlog") + SWANLAB_MODE = os.environ.get("SWANLAB_MODE", "cloud") + if SWANLAB_API_KEY: + swanlab.login(SWANLAB_API_KEY) # NOTE: previous login information will be overwritten + + if config is None: + config = {} # make sure config is not None, otherwise **config will raise error + swanlab.init( + project=project_name, + experiment_name=experiment_name, + config={"FRAMEWORK": "verl", **config}, + logdir=SWANLAB_LOG_DIR, + mode=SWANLAB_MODE, + ) + self.logger["swanlab"] = swanlab + + if "vemlp_wandb" in default_backend: + import os + + import volcengine_ml_platform + from volcengine_ml_platform import wandb as vemlp_wandb + + volcengine_ml_platform.init( + ak=os.environ["VOLC_ACCESS_KEY_ID"], + sk=os.environ["VOLC_SECRET_ACCESS_KEY"], + region=os.environ["MLP_TRACKING_REGION"], + ) + + vemlp_wandb.init( + project=project_name, + name=experiment_name, + config=config, + sync_tensorboard=True, + ) + self.logger["vemlp_wandb"] = vemlp_wandb + + if "tensorboard" in default_backend: + self.logger["tensorboard"] = _TensorboardAdapter(project_name, experiment_name) + + if "console" in default_backend: + from verl.utils.logger import LocalLogger + + self.console_logger = LocalLogger(print_to_console=True) + self.logger["console"] = self.console_logger + + if "clearml" in default_backend: + self.logger["clearml"] = ClearMLLogger(project_name, experiment_name, config) + + if "file" in default_backend: + self.logger["file"] = FileLogger(project_name, experiment_name) + + if "rl_insight" in default_backend: + self.logger["rl_insight"] = RLInsightLogger(project_name, experiment_name, config) + + def log(self, data, step, backend=None): + for default_backend, logger_instance in self.logger.items(): + if backend is None or default_backend in backend: + logger_instance.log(data=data, step=step) + + def __del__(self): + if "wandb" in self.logger: + self.logger["wandb"].finish(exit_code=0) + if "swanlab" in self.logger: + self.logger["swanlab"].finish() + if "vemlp_wandb" in self.logger: + self.logger["vemlp_wandb"].finish(exit_code=0) + if "tensorboard" in self.logger: + self.logger["tensorboard"].finish() + if "clearml" in self.logger: + self.logger["clearml"].finish() + if "trackio" in self.logger: + self.logger["trackio"].finish() + if "file" in self.logger: + self.logger["file"].finish() + if "rl_insight" in self.logger: + self.logger["rl_insight"].finish() + + +class RLInsightLogger: + """Logger backend that exports scalar metrics and rl-insight runtime signals.""" + + ENABLE_ENV = "VERL_RL_INSIGHT_ENABLE" + _init_done = False + _rl_insight_module = None + _registered_metrics: set[tuple[str | None, tuple[str, ...], str | None]] = set() + + def __init__(self, project_name, experiment_name, config=None): + self.init(project_name=project_name, experiment_name=experiment_name, config=config) + + @classmethod + def _get_rl_insight(cls): + if cls._rl_insight_module is None: + import rl_insight + + cls._rl_insight_module = rl_insight + return cls._rl_insight_module + + @classmethod + def enabled(cls) -> bool: + """Return whether rl-insight is globally enabled in this process.""" + return os.getenv(cls.ENABLE_ENV) == "1" + + @classmethod + def init(cls, project_name=None, experiment_name=None, config=None): + if not cls.enabled() or cls._init_done: + return + + rl_insight_config = {} + if config is not None: + try: + rl_insight_config = config.get("trainer", {}).get("rl_insight", {}) or {} + except (AttributeError, KeyError, TypeError): + pass + cls._get_rl_insight().init(project=project_name, experiment_name=experiment_name, config=rl_insight_config) + cls._init_done = True + if config is not None: + cls.register_transfer_queue_metrics(config) + + @classmethod + def log(cls, data, step): + if not cls.enabled(): + return + if not cls._init_done: + cls._get_rl_insight().init() + cls._init_done = True + metric_gauge = cls._get_rl_insight().metric_gauge + + for key, value in data.items(): + try: + scalar = float(value) + except (TypeError, ValueError): + continue + metric_gauge(str(key).replace("/", "_"), scalar) + + @classmethod + def finish(cls): + if not cls._init_done: + return + + cls._get_rl_insight().finish() + cls._init_done = False + cls._registered_metrics.clear() + + @classmethod + @contextmanager + def trace_state( + cls, + state_name: str, + *, + state_lane_id: str | int | None = None, + **labels: Any, + ): + if not cls.enabled(): + yield + return + + if not cls._init_done: + cls._get_rl_insight().init() + cls._init_done = True + with cls._get_rl_insight().trace_state(state_name, state_lane_id=state_lane_id, **labels): + yield + + @classmethod + def register_rollout_metrics( + cls, + server_addresses: list[str], + rollout_name: str | None, + labels: list[dict[str, Any] | None] | None = None, + ) -> None: + cls.register_metrics(server_addresses, rollout_name, labels) + + @classmethod + def register_transfer_queue_metrics(cls, config) -> None: + if not (config or {}).get("transfer_queue", {}).get("metrics", {}).get("enabled", False): + return + + try: + import transfer_queue as tq + + endpoint = tq.get_metrics_endpoint() + except Exception: + logger.exception("[rl-insight] Failed to get transfer_queue metrics endpoint") + return + + if endpoint: + cls.register_metrics([endpoint], "transfer_queue", None) + + @classmethod + def register_metrics( + cls, + server_addresses: list[str], + job_name: str | None = None, + labels: list[dict[str, Any] | None] | None = None, + ) -> None: + if not cls.enabled(): + return + + metric_key = (job_name, tuple(server_addresses), repr(labels)) + if metric_key in cls._registered_metrics: + return + + try: + cls._get_rl_insight().update_prometheus_config(server_addresses, job_name, labels) + cls._registered_metrics.add(metric_key) + except Exception: + logger.exception("[rl-insight] Failed to register metrics endpoint") + + +class ClearMLLogger: + def __init__(self, project_name: str, experiment_name: str, config): + self.project_name = project_name + self.experiment_name = experiment_name + + import clearml + + self._task: clearml.Task = clearml.Task.init( + task_name=experiment_name, + project_name=project_name, + continue_last_task=True, + output_uri=False, + ) + + self._task.connect_configuration(config, name="Hyperparameters") + + def _get_logger(self): + return self._task.get_logger() + + def log(self, data, step): + import numpy as np + import pandas as pd + + # logs = self._rewrite_logs(data) + logger = self._get_logger() + for k, v in data.items(): + title, series = k.split("/", 1) + + if isinstance(v, int | float | np.floating | np.integer): + logger.report_scalar( + title=title, + series=series, + value=v, + iteration=step, + ) + elif isinstance(v, pd.DataFrame): + logger.report_table( + title=title, + series=series, + table_plot=v, + iteration=step, + ) + else: + logger.warning( + f'Trainer is attempting to log a value of "{v}" of type {type(v)} for key "{k}". This ' + f"invocation of ClearML logger's function is incorrect so this attribute was dropped. " + ) + + def finish(self): + self._task.close() + + +class _TrackioLoggingAdapter: + def __init__(self, trackio): + self.trackio = trackio + + def log(self, data, step): + self.trackio.log(data, step=step) + + def finish(self): + from trackio import context_vars + + if context_vars.current_run.get() is not None: + self.trackio.finish() + + +class FileLogger: + def __init__(self, project_name: str, experiment_name: str): + self.project_name = project_name + self.experiment_name = experiment_name + + self.filepath = os.getenv("VERL_FILE_LOGGER_PATH", None) + if self.filepath is None: + root_path = os.path.expanduser(os.getenv("VERL_FILE_LOGGER_ROOT", ".")) + directory = os.path.join(root_path, self.project_name) + os.makedirs(directory, exist_ok=True) + self.filepath = os.path.join(directory, f"{self.experiment_name}.jsonl") + print(f"Creating file logger at {os.path.abspath(self.filepath)}") + self.fp = open(self.filepath, "wb", buffering=0) + + def log(self, data, step): + data = {"step": step, "data": data} + self.fp.write(orjson.dumps(data, option=orjson.OPT_SERIALIZE_NUMPY) + b"\n") + + def finish(self): + self.fp.close() + + +class _TensorboardAdapter: + def __init__(self, project_name, experiment_name): + import os + + from torch.utils.tensorboard import SummaryWriter + + tensorboard_dir = os.environ.get("TENSORBOARD_DIR", f"tensorboard_log/{project_name}/{experiment_name}") + os.makedirs(tensorboard_dir, exist_ok=True) + print(f"Saving tensorboard log to {tensorboard_dir}.") + self.writer = SummaryWriter(tensorboard_dir) + + def log(self, data, step): + for key in data: + self.writer.add_scalar(key, data[key], step) + + def finish(self): + self.writer.close() + + +class _MlflowLoggingAdapter: + def __init__(self): + import logging + import re + + self.logger = logging.getLogger(__name__) + # Suppress noisy "Found credentials from IAM Role" on every MLflow request + logging.getLogger("botocore.credentials").setLevel(logging.WARNING) + # MLflow metric key validation logic: + # https://github.com/mlflow/mlflow/blob/master/mlflow/utils/validation.py#L157C12-L157C44 + # Only characters allowed: slashes, alphanumerics, underscores, periods, dashes, colons, + # and spaces. + self._invalid_chars_pattern = re.compile( + r"[^/\w.\- :]" + ) # Allowed: slashes, alphanumerics, underscores, periods, dashes, colons, and spaces. + self._consecutive_slashes_pattern = re.compile(r"/+") + self._sanitized_key_cache = {} + + def _sanitize_key(self, key): + if key in self._sanitized_key_cache: + return self._sanitized_key_cache[key] or key + # First replace @ with _at_ for backward compatibility + sanitized = key.replace("@", "_at_") + # Replace consecutive slashes with a single slash (MLflow treats them as file paths) + sanitized = self._consecutive_slashes_pattern.sub("/", sanitized) + # Then replace any other invalid characters with _ + sanitized = self._invalid_chars_pattern.sub("_", sanitized) + if sanitized == key: + self._sanitized_key_cache[key] = None + else: + self.logger.warning("[MLflow] Metric key '%s' sanitized to '%s' due to invalid characters.", key, sanitized) + self._sanitized_key_cache[key] = sanitized + return sanitized + + def log(self, data, step): + import mlflow + + results = {self._sanitize_key(k): v for k, v in data.items()} + for _attempt in range(MLFLOW_MAX_ATTEMPTS): + try: + mlflow.log_metrics(metrics=results, step=step) + return + except Exception as error: + # No sleep between retries — this runs per training step, so we avoid blocking. + msg = "mlflow.log_metrics failed (attempt %d/%d): %s" + args = (_attempt + 1, MLFLOW_MAX_ATTEMPTS, error) + if _attempt < MLFLOW_MAX_ATTEMPTS - 1: + self.logger.info(msg, *args) + else: + self.logger.warning(msg, *args) + + +def _compute_mlflow_params_from_objects(params) -> dict[str, Any]: + if params is None: + return {} + + return _flatten_dict(_transform_params_to_json_serializable(params, convert_list_to_dict=True), sep="/") + + +def _transform_params_to_json_serializable(x, convert_list_to_dict: bool): + _transform = partial(_transform_params_to_json_serializable, convert_list_to_dict=convert_list_to_dict) + + if dataclasses.is_dataclass(x): + return _transform(dataclasses.asdict(x)) + if isinstance(x, dict): + return {k: _transform(v) for k, v in x.items()} + if isinstance(x, list): + if convert_list_to_dict: + return {"list_len": len(x)} | {f"{i}": _transform(v) for i, v in enumerate(x)} + else: + return [_transform(v) for v in x] + if isinstance(x, Path): + return str(x) + if isinstance(x, Enum): + return x.value + + return x + + +def _flatten_dict(raw: dict[str, Any], *, sep: str) -> dict[str, Any]: + import pandas as pd + + ans = pd.json_normalize(raw, sep=sep).to_dict(orient="records")[0] + assert isinstance(ans, dict) + return ans + + +@dataclasses.dataclass +class ValidationGenerationsLogger: + project_name: str = None + experiment_name: str = None + + def log(self, loggers, samples, step): + if "wandb" in loggers: + self.log_generations_to_wandb(samples, step) + if "swanlab" in loggers: + self.log_generations_to_swanlab(samples, step) + if "mlflow" in loggers: + self.log_generations_to_mlflow(samples, step) + if "trackio" in loggers: + self.log_generations_to_trackio(samples, step) + + if "clearml" in loggers: + self.log_generations_to_clearml(samples, step) + if "tensorboard" in loggers: + self.log_generations_to_tensorboard(samples, step) + + if "vemlp_wandb" in loggers: + self.log_generations_to_vemlp_wandb(samples, step) + + def log_generations_to_vemlp_wandb(self, samples, step): + from volcengine_ml_platform import wandb as vemlp_wandb + + self._log_generations_to_wandb(samples, step, vemlp_wandb) + + def log_generations_to_wandb(self, samples, step): + import wandb + + self._log_generations_to_wandb(samples, step, wandb) + + def _log_generations_to_wandb(self, samples, step, wandb): + """Log samples to wandb as a table""" + + # Create column names for all samples + columns = ["step"] + sum( + [[f"input_{i + 1}", f"output_{i + 1}", f"score_{i + 1}"] for i in range(len(samples))], [] + ) + + if not hasattr(self, "validation_table"): + # Initialize the table on first call + self.validation_table = wandb.Table(columns=columns) + + # Create a new table with same columns and existing data + # Workaround for https://github.com/wandb/wandb/issues/2981#issuecomment-1997445737 + new_table = wandb.Table(columns=columns, data=self.validation_table.data) + + # Add new row with all data + row_data = [] + row_data.append(step) + for sample in samples: + row_data.extend(sample) + + new_table.add_data(*row_data) + + # Update reference and log + if wandb.run is not None: + wandb.log({"val/generations": new_table}, step=step) + self.validation_table = new_table + + def log_generations_to_swanlab(self, samples, step): + """Log samples to swanlab as text""" + import swanlab + + swanlab_table = swanlab.echarts.Table() + + # Create column names + headers = ["step", "input", "output", "score"] + + swanlab_row_list = [[step, *sample] for sample in samples] + swanlab_table.add(headers=headers, rows=swanlab_row_list) + + # Log to swanlab + swanlab.log({"val/generations": swanlab_table}, step=step) + + def log_generations_to_mlflow(self, samples, step): + """Log validation generation to mlflow as artifacts""" + # https://mlflow.org/docs/latest/api_reference/python_api/mlflow.html?highlight=log_artifact#mlflow.log_artifact + + import tempfile + + import mlflow + + try: + with tempfile.TemporaryDirectory() as tmp_dir: + validation_gen_step_file = Path(tmp_dir, f"val_step{step}.json") + row_data = [] + for sample in samples: + data = {"input": sample[0], "output": sample[1], "score": sample[2]} + row_data.append(data) + with open(validation_gen_step_file, "w") as file: + json.dump(row_data, file) + mlflow.log_artifact(validation_gen_step_file) + except Exception as e: + print(f"WARNING: save validation generation file to mlflow failed with error {e}") + + def log_generations_to_trackio(self, samples, step): + """Log validation generations to trackio as traces.""" + import trackio + + traces = [] + for sample_index, sample in enumerate(samples): + if len(sample) >= 3: + input_text, output_text, score = sample[0], sample[1], sample[2] + else: + input_text, output_text, score = sample, "", None + + traces.append( + trackio.Trace( + messages=[ + {"role": "user", "content": str(input_text)}, + {"role": "assistant", "content": str(output_text)}, + ], + metadata={ + "source": "validation_generations", + "sample_index": sample_index, + "score": score, + }, + ) + ) + + if traces: + trackio.log({"val/generations": traces}, step=step) + + def log_generations_to_clearml(self, samples, step): + """Log validation generation to clearml as table""" + + import clearml + import pandas as pd + + task: clearml.Task | None = clearml.Task.current_task() + if task is None: + return + + table = [ + { + "step": step, + "input": sample[0], + "output": sample[1], + "score": sample[2], + } + for sample in samples + ] + + logger = task.get_logger() + logger.report_table( + series="Validation generations", + title="Validation", + table_plot=pd.DataFrame.from_records(table), + iteration=step, + ) + + def log_generations_to_tensorboard(self, samples, step): + """Log samples to tensorboard as text""" + # Initialize tensorboard writer if not exists + if not hasattr(self, "writer"): + from torch.utils.tensorboard import SummaryWriter + + # Use the same directory structure as _TensorboardAdapter + if self.project_name and self.experiment_name: + default_dir = os.path.join("tensorboard_log", self.project_name, self.experiment_name) + else: + default_dir = "tensorboard_log" + + tensorboard_dir = os.environ.get("TENSORBOARD_DIR", default_dir) + os.makedirs(tensorboard_dir, exist_ok=True) + self.writer = SummaryWriter(log_dir=tensorboard_dir) + + # Format the samples data into readable text + text_content = f"**Generation Results - Step {step}**\n\n" + + for i, sample in enumerate(samples): + text_content += f"### Sample {i + 1}\n" + + # Assuming sample contains [input, output, score] + if len(sample) >= 3: + input_text, output_text, score = sample[0], sample[1], sample[2] + + text_content += f"**Input:** {input_text}\n\n" + text_content += f"**Output:** {output_text}\n\n" + text_content += f"**Score:** {score}\n\n" + else: + # Handle cases where sample format might be different + text_content += f"**Data:** {sample}\n\n" + + text_content += "---\n\n" + + # Log to tensorboard as text + self.writer.add_text("val/generations", text_content, step) + # Flush to ensure data is written + self.writer.flush() diff --git a/verl_0720_main/verl/verl/utils/transferqueue_utils.py b/verl_0720_main/verl/verl/utils/transferqueue_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..046471995943d7149d72fc41fa900d8f7da4746c --- /dev/null +++ b/verl_0720_main/verl/verl/utils/transferqueue_utils.py @@ -0,0 +1,430 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio +import copy +import functools +import inspect +import logging +import os +import threading +import time +from functools import wraps +from typing import TYPE_CHECKING, Any, Callable + +import torch +from tensordict.tensorclass import NonTensorData, NonTensorStack + +if TYPE_CHECKING: + from verl.single_controller.base.decorator import Dispatch + +from tensordict import TensorDict + +try: + import transfer_queue as tq + from transfer_queue import ( + BatchMeta, + KVBatchMeta, + ) + +except ImportError: + + class BatchMeta: + pass + + class KVBatchMeta: + pass + + # Mock transfer_queue module when not installed + class _MockTQ: + """Mock transfer_queue module that raises RuntimeError on any access.""" + + def __getattr__(self, name: str) -> Any: + def _raise(*args, **kwargs): + raise RuntimeError( + f"transfer_queue is not installed. Cannot use tq.{name}(). " + "Please install it by calling `pip install TransferQueue==0.1.8`" + ) + + return _raise + + tq = _MockTQ() + + +from verl.utils import tensordict_utils as tu + +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +TQ_INITIALIZED = False + + +# TODO (TQ): verl will make all actor async, so this can be cleanup later. +def _run_async_in_temp_loop(async_func: Callable[..., Any], *args, **kwargs) -> Any: + # Use a temporary event loop in a new thread because event + # loop may already exist in server mode + tmp_event_loop = asyncio.new_event_loop() + thread = threading.Thread( + target=tmp_event_loop.run_forever, + name="batchmeta tensordict converter", + daemon=True, + ) + + def run_coroutine(coroutine): + if not thread.is_alive(): + thread.start() + future = asyncio.run_coroutine_threadsafe(coroutine, tmp_event_loop) + return future.result() + + async def stop_loop(): + tmp_event_loop.stop() + + try: + return run_coroutine(async_func(*args, **kwargs)) + finally: + if thread.is_alive(): + asyncio.run_coroutine_threadsafe(stop_loop(), tmp_event_loop) + thread.join() + + +def _find_meta(*args, **kwargs): + for arg in args: + if isinstance(arg, BatchMeta | KVBatchMeta): + return arg + for v in kwargs.values(): + if isinstance(v, BatchMeta | KVBatchMeta): + return v + return None + + +async def _async_meta_to_realdata(meta: BatchMeta | KVBatchMeta) -> TensorDict: + if isinstance(meta, KVBatchMeta): + meta = await async_kv_batch_meta2batch_meta(meta) + meta_info = copy.deepcopy(meta.extra_info) + if meta.size == 0: + empty_td = TensorDict({}, batch_size=(0,)) + tu.assign_non_tensor(empty_td, **meta_info) + return empty_td + + tq_client = tq.get_client() + tensordict = await tq_client.async_get_data(meta) + + for key, val in meta_info.items(): + if isinstance(val, (NonTensorData | NonTensorStack)): + tensordict[key] = val + else: + tu.assign_non_tensor_data(tensor_dict=tensordict, key=key, val=val) + return tensordict + + +def _meta_to_realdata(meta: BatchMeta) -> TensorDict: + return _run_async_in_temp_loop(_async_meta_to_realdata, meta) + + +async def _async_update_meta_with_output(output: TensorDict, meta: BatchMeta, func_name=None) -> BatchMeta: + fields, meta_data = [], {} + for k, v in output.items(): + if isinstance(v, torch.Tensor | NonTensorStack): + fields.append(k) + elif isinstance(v, NonTensorData): + meta_data[k] = v.data + else: + raise ValueError(f"Unsupported type {type(v)} for key {k} in output TensorDict.") + + if fields: + t1 = time.time() + tq_client = tq.get_client() + meta = await tq_client.async_put(data=output.select(*fields), metadata=meta) + t2 = time.time() + + logger.info(f"Task {func_name} (pid={os.getpid()}) is writing to TransferQueue, cost time: {t2 - t1}s)") + meta.extra_info = meta_data + return meta + + +def _update_meta_with_output(output: TensorDict, meta: BatchMeta, func_name=None) -> BatchMeta: + updated_meta = _run_async_in_temp_loop(_async_update_meta_with_output, output, meta, func_name) + return updated_meta + + +def _compute_need_collect(dispatch_mode: "dict | Dispatch", args: list) -> bool: + """Compute whether data collection is needed for the current worker. + + This function determines whether the current worker should collect data based on + the dispatch mode configuration and worker parameters. It's used to optimize + distributed data collection by ensuring only the appropriate rank collects data. + + Args: + dispatch_mode: Controls data collection logic for the current worker. Can be None, + a Dispatch instance, or a dict with 'collect_fn' key. If None or Dispatch, + always returns True (current worker should collect). If dict, checks + collect_fn for lazy compute optimization. + args: List of arguments passed to the function. Should contain a Worker instance + as the first argument when using lazy compute mode. + + Returns: + bool: True if data collection is needed, False otherwise. + + Note: + Only checks worker attributes when dispatch_mode is a dict with 'collect_fn', + the collect_fn is 'collect_lazy_compute_data_proto', and args[0] is a Worker. + Otherwise, returns True. For the lazy compute case, checks the worker's + data parallel rank for the mesh specified in collect_fn.args[0] to determine + if this worker should collect data. + """ + from verl.single_controller.base.decorator import Dispatch + from verl.single_controller.base.worker import Worker + + if dispatch_mode is None or isinstance(dispatch_mode, Dispatch): + return True + + assert "collect_fn" in dispatch_mode.keys(), "collect_fn should be in dispatch_mode." + + collect_fn = dispatch_mode["collect_fn"] + + # Check if collect_fn is a functools.partial and handle gracefully + if isinstance(collect_fn, functools.partial): + collect_fn_name = collect_fn.func.__name__ + if collect_fn_name != "collect_lazy_compute_data_proto" or len(args) < 1 or not isinstance(args[0], Worker): + return True + + collect_mesh_name = collect_fn.args[0] if collect_fn.args else None + if collect_mesh_name is None: + return True + + return args[0].query_collect_info(collect_mesh_name) + else: + # If collect_fn is not a partial, we can't extract mesh_name information + # Fall back to default behavior (collect data) + return True + + +def _postprocess_common(output, put_data, need_collect): + """Common post-processing logic for function outputs in TransferQueue bridge. + + This function handles the final return value based on whether data should be + put into storage (put_data) and whether collection is needed (need_collect). + It ensures proper return types based on the execution context. + + Args: + output: The original output from the decorated function. Can be any type. + put_data: bool, indicating whether the output should be put into TransferQueue. + If True, output will be put to TQ and return the corresponding BatchMeta; + if False, output will not be put into TQ. + need_collect: bool, indicating whether this process needs to collect data. + If False, the output will be replaced by an empty BatchMeta or DataProto + to avoid redundant communication. + + Returns: + - BatchMeta.empty(): When put_data=True but need_collect=False, indicating + no data should be stored but BatchMeta structure is expected. + - DataProto(): When put_data=False, need_collect=False, and output is DataProto, + returning an empty DataProto. + - output: In all other cases, returns the original output unchanged. + + Note: + This function is used in the tqbridge decorator to normalize return values + across different execution paths and avoid redundant data operations in + distributed scenarios. + """ + from verl.protocol import DataProto + + if put_data and not need_collect: + return BatchMeta() + elif not put_data and not need_collect and isinstance(output, DataProto): + return DataProto() + elif not put_data and not need_collect and isinstance(output, TensorDict): + return TensorDict({}, batch_size=(0,)) + else: + return output + + +async def async_kv_batch_meta2batch_meta(meta: KVBatchMeta) -> BatchMeta: + global TQ_INITIALIZED + if not TQ_INITIALIZED: + tq.init() + TQ_INITIALIZED = True + tq_client = tq.get_client() + batch_meta = await tq_client.async_kv_retrieve_meta(keys=meta.keys, partition_id=meta.partition_id, create=False) + fields = meta.fields + if fields is not None: + if isinstance(fields, str): + fields = [fields] + batch_meta = batch_meta.select_fields(fields) + + batch_meta.extra_info = meta.extra_info + return batch_meta + + +def kv_batch_meta2batch_meta(meta: KVBatchMeta): + return _run_async_in_temp_loop(async_kv_batch_meta2batch_meta, meta) + + +async def async_batch_meta2kv_batch_meta(meta: BatchMeta) -> KVBatchMeta: + global TQ_INITIALIZED + if not TQ_INITIALIZED: + tq.init() + TQ_INITIALIZED = True + tq_client = tq.get_client() + partition_id = meta.partition_ids[0] + assert all([partition_id == pid for pid in meta.partition_ids]) + keys = await tq_client.async_kv_retrieve_keys(global_indexes=meta.global_indexes, partition_id=partition_id) + + kv_batch_meta = KVBatchMeta( + keys=keys, + tags=[{}] * meta.size, + partition_id=partition_id, + fields=meta.field_names, + extra_info=meta.extra_info, + ) + return kv_batch_meta + + +def batch_meta2kv_batch_meta(meta: BatchMeta): + return _run_async_in_temp_loop(async_batch_meta2kv_batch_meta, meta) + + +def tqbridge(dispatch_mode: "dict | Dispatch" = None): + """Creates a decorator for bridging KVBatchMeta and TensorDict. + + This decorator automatically handles conversions between `KVBatchMeta` + and `TensorDict` in function parameters, and decides whether to sync function + output back to `KVBatchMeta` based on configuration(`put_data`). It supports + both synchronous and asynchronous functions (async def). When TQ is not enabled, it + simply calls the original function as-is. + + Args: + dispatch_mode: Controls data collection behavior for the current worker. Passed to + _compute_need_collect to determine if current worker should collect data. + If None, _compute_need_collect will return True to fallback default logics. + + + Returns: + A decorator function used to decorate target functions (synchronous or asynchronous). + """ + # TODO: move to the top + from verl.single_controller.base.decorator import _check_dispatch_mode + + if dispatch_mode is not None: + _check_dispatch_mode(dispatch_mode) + + def decorator(func): + pid = os.getpid() + + @wraps(func) + def inner(*args, **kwargs): + batch_meta = _find_meta(*args, **kwargs) + if batch_meta is None: + return func(*args, **kwargs) + else: + global TQ_INITIALIZED + if not TQ_INITIALIZED: + tq.init() + TQ_INITIALIZED = True + + is_kv_batch_meta = isinstance(batch_meta, KVBatchMeta) + if is_kv_batch_meta: + tags = batch_meta.tags + batch_meta = kv_batch_meta2batch_meta(batch_meta) + t1 = time.time() + args = [_meta_to_realdata(arg) if isinstance(arg, BatchMeta | KVBatchMeta) else arg for arg in args] + kwargs = { + k: _meta_to_realdata(v) if isinstance(v, BatchMeta | KVBatchMeta) else v for k, v in kwargs.items() + } + t2 = time.time() + logger.info( + f"Task {func.__name__} (pid={pid}) is getting len_samples={batch_meta.size}, cost time: {t2 - t1}" + ) + + output = func(*args, **kwargs) + + put_data = False + if isinstance(output, TensorDict): + if output.batch_size: + assert output.batch_size[0] == batch_meta.size, ( + f"output batch size {output.batch_size} != meta size {batch_meta.size}" + ) + put_data = True + + if dispatch_mode is not None: + need_collect = _compute_need_collect(dispatch_mode, args) + else: + need_collect = True + if put_data and need_collect: + updated_meta = _update_meta_with_output(output, batch_meta, func.__name__) + if is_kv_batch_meta: + updated_meta = batch_meta2kv_batch_meta(updated_meta) + updated_meta.tags = tags + return updated_meta + return _postprocess_common(output, put_data, need_collect) + + @wraps(func) + async def async_inner(*args, **kwargs): + batch_meta = _find_meta(*args, **kwargs) + if batch_meta is None: + return await func(*args, **kwargs) + else: + global TQ_INITIALIZED + if not TQ_INITIALIZED: + tq.init() + TQ_INITIALIZED = True + + is_kv_batch_meta = isinstance(batch_meta, KVBatchMeta) + if is_kv_batch_meta: + tags = batch_meta.tags + batch_meta = await async_kv_batch_meta2batch_meta(batch_meta) + + t1 = time.time() + args = [ + await _async_meta_to_realdata(arg) if isinstance(arg, BatchMeta | KVBatchMeta) else arg + for arg in args + ] + kwargs = { + k: await _async_meta_to_realdata(v) if isinstance(v, BatchMeta | KVBatchMeta) else v + for k, v in kwargs.items() + } + t2 = time.time() + logger.info( + f"Task {func.__name__} (pid={pid}) is getting len_samples={batch_meta.size}, cost time: {t2 - t1}" + ) + + output = await func(*args, **kwargs) + + put_data = False + if isinstance(output, TensorDict): + if output.batch_size: + assert output.batch_size[0] == batch_meta.size, ( + f"output batch size {output.batch_size} != meta size {batch_meta.size}" + ) + put_data = True + + if dispatch_mode is not None: + need_collect = _compute_need_collect(dispatch_mode, args) + else: + need_collect = True + if put_data and need_collect: + updated_meta = await _async_update_meta_with_output(output, batch_meta, func.__name__) + if is_kv_batch_meta: + updated_meta = await async_batch_meta2kv_batch_meta(updated_meta) + updated_meta.tags = tags + return updated_meta + return _postprocess_common(output, put_data, need_collect) + + wrapper_inner = inner + wrapper_async_inner = async_inner + + wrapper = wrapper_async_inner if inspect.iscoroutinefunction(func) else wrapper_inner + return wrapper + + return decorator diff --git a/verl_0720_main/verl/verl/utils/transformers_compat.py b/verl_0720_main/verl/verl/utils/transformers_compat.py new file mode 100644 index 0000000000000000000000000000000000000000..e7fb659fddbbdd0239a5006da6ba9b7d291b61bf --- /dev/null +++ b/verl_0720_main/verl/verl/utils/transformers_compat.py @@ -0,0 +1,121 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Compatibility utilities for different versions of transformers library. +""" + +import importlib.metadata +from functools import lru_cache +from typing import Optional + +from packaging import version + +# Handle version compatibility for flash_attn_supports_top_left_mask +# This function was added in newer versions of transformers +try: + from transformers.modeling_flash_attention_utils import flash_attn_supports_top_left_mask +except ImportError: + # For older versions of transformers that don't have this function + # Default to False as a safe fallback for older versions + def flash_attn_supports_top_left_mask(): + """Fallback implementation for older transformers versions. + Returns False to disable features that require this function. + """ + return False + + +@lru_cache +def is_transformers_version_in_range(min_version: Optional[str] = None, max_version: Optional[str] = None) -> bool: + try: + # Get the installed version of the transformers library + transformers_version_str = importlib.metadata.version("transformers") + except importlib.metadata.PackageNotFoundError as e: + raise ModuleNotFoundError("The `transformers` package is not installed.") from e + + transformers_version = version.parse(transformers_version_str) + + lower_bound_check = True + if min_version is not None: + lower_bound_check = version.parse(min_version) <= transformers_version + + upper_bound_check = True + if max_version is not None: + upper_bound_check = transformers_version <= version.parse(max_version) + + return lower_bound_check and upper_bound_check + + +@lru_cache +def get_auto_model_for_vision2seq(): + """Return the available VL auto model class across transformers versions.""" + + try: + # Prefer the newer class when available. In transformers 4.x this class has + # a broader mapping than AutoModelForVision2Seq, and AutoModelForVision2Seq + # is deprecated for removal in v5. + from transformers import AutoModelForImageTextToText + except ImportError: + from transformers import AutoModelForVision2Seq + + return AutoModelForVision2Seq + + return AutoModelForImageTextToText + + +def unpack_visual_output(visual_output): + """Unpack the output from the visual encoder, handling both tuple and object return types. + + Newer versions of transformers return an object with `pooler_output` and `deepstack_features` + attributes instead of a plain tuple. + """ + if hasattr(visual_output, "pooler_output"): + # For newer versions(>=5.0.0) of transformers, return the pooler_output and deepstack_features + if hasattr(visual_output, "deepstack_features"): + return visual_output.pooler_output, visual_output.deepstack_features + else: + return visual_output.pooler_output, None + if isinstance(visual_output, tuple): + return visual_output + else: + return visual_output, None + + +def drop_tied_target_keys(state_dict, model, model_config) -> None: + """Drop tied alias keys (e.g. ``lm_head.weight``) from ``state_dict``. + + FSDP gather and the model merger materialise one independent CPU tensor + per state_dict key, which defeats ``save_pretrained``'s storage-pointer- + based dedup. When both keys end up in safetensors, ``transformers>=5`` + silently refuses to re-tie on reload. Detects aliases by ``Parameter`` + identity after ``tie_weights()``. + + Only drops the alias when the canonical name has been confirmed to exist + in ``state_dict``; otherwise the alias is promoted to canonical so we + never accidentally remove every entry for a tied parameter. + """ + if not getattr(model_config, "tie_word_embeddings", False): + return + model.tie_weights() + canonical_by_id: dict[int, str] = {} + for name, param in model.named_parameters(remove_duplicate=False): + pid = id(param) + if pid not in canonical_by_id: + canonical_by_id[pid] = name + continue + if canonical_by_id[pid] in state_dict: + state_dict.pop(name, None) + else: + # Canonical name absent: promote the alias instead of erasing it. + canonical_by_id[pid] = name diff --git a/verl_0720_main/verl/verl/utils/trtllm/__init__.py b/verl_0720_main/verl/verl/utils/trtllm/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..3d5aee6bdcdd86a19e72d4a8e96dd3f6b9abc337 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/trtllm/__init__.py @@ -0,0 +1,14 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/utils/trtllm/trtllm_fp8_utils.py b/verl_0720_main/verl/verl/utils/trtllm/trtllm_fp8_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..43c15351c06057efbdc82b2ab1b3940a3601b106 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/trtllm/trtllm_fp8_utils.py @@ -0,0 +1,21 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from verl.utils.fp8_utils import FP8QuantizerHelper + + +class TRTLLMFP8QuantizerHelper(FP8QuantizerHelper): + def __init__(self, quant_config): + super().__init__(quant_config) diff --git a/verl_0720_main/verl/verl/utils/ulysses.py b/verl_0720_main/verl/verl/utils/ulysses.py new file mode 100644 index 0000000000000000000000000000000000000000..7ee07dd9090f9c6d1e64a9ccefe326b7e907713c --- /dev/null +++ b/verl_0720_main/verl/verl/utils/ulysses.py @@ -0,0 +1,404 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Utilities for DeepSpeed Ulysses Sequence Parallelism. +DeepSpeed Ulysses Paper: https://arxiv.org/abs/2309.14509 +Inspired from: https://github.com/deepspeedai/DeepSpeed/blob/master/deepspeed/sequence/layer.py +""" + +from typing import TYPE_CHECKING, Any, Optional + +import torch +import torch.distributed as dist +from torch import Tensor +from torch.distributed import ProcessGroup +from torch.distributed.device_mesh import DeviceMesh + +if TYPE_CHECKING: + from verl import DataProto + +_ULYSSES_SEQUENCE_PARALLEL_GROUP = None + + +def set_ulysses_sequence_parallel_group(group: dist.ProcessGroup): + """ + Set ulysses sequence parallel process group. + """ + global _ULYSSES_SEQUENCE_PARALLEL_GROUP + _ULYSSES_SEQUENCE_PARALLEL_GROUP = group + + +def get_ulysses_sequence_parallel_group() -> Optional[dist.ProcessGroup]: + """ + Get ulysses sequence parallel process group. + """ + global _ULYSSES_SEQUENCE_PARALLEL_GROUP + return _ULYSSES_SEQUENCE_PARALLEL_GROUP + + +def get_ulysses_sequence_parallel_world_size(group: ProcessGroup = None) -> int: + """ + Get ulysses sequence parallel world size. + """ + group = get_ulysses_sequence_parallel_group() if group is None else group + return dist.get_world_size(group) if group else 1 + + +def get_ulysses_sequence_parallel_rank(group: ProcessGroup = None) -> int: + """ + Get ulysses sequence parallel rank. + """ + group = get_ulysses_sequence_parallel_group() if group is None else group + return dist.get_rank(group) if group else 0 + + +def gather_seq_scatter_heads( + x: Tensor, + seq_dim: int, + head_dim: int, + unpadded_dim_size: int = 0, + group: ProcessGroup = None, +) -> Tensor: + """ + A func to sync embedding input with alltoall in sequence parallel + gather sequence dimension and scatter head dim: + e.g. seq_dim: 1, head_dim: 2 + [bsz, seq/n, h, ...] -> [bsz, seq, h/n, ...] + """ + group = get_ulysses_sequence_parallel_group() if group is None else group + if not group: + return x + sp_world = get_ulysses_sequence_parallel_world_size(group) + x = SeqAllToAll.apply(group, x, head_dim, seq_dim) + if unpadded_dim_size and unpadded_dim_size % sp_world != 0: + padding_size = x.size(seq_dim) - unpadded_dim_size + x = _unpad_tensor(x, seq_dim, padding_size) + return x + + +def gather_heads_scatter_seq(x: Tensor, head_dim: int, seq_dim: int, group: ProcessGroup = None) -> Tensor: + """ + A func to sync attention result with alltoall in sequence parallel + gather head dimension and scatter seq dim: + e.g. seq_dim: 1, head_dim: 2 + [bsz, seq, h/n, ...] -> [bsz, seq/n, h, ...] + """ + group = get_ulysses_sequence_parallel_group() if group is None else group + if not group: + return x + dim_size = x.size(seq_dim) + sp_world = get_ulysses_sequence_parallel_world_size(group) + if dim_size % sp_world != 0: + padding_size = sp_world - (dim_size % sp_world) + x = _pad_tensor(x, seq_dim, padding_size) + return SeqAllToAll.apply(group, x, seq_dim, head_dim, False) + + +def _pad_tensor(x: Tensor, dim: int, padding_size: int) -> Tensor: + shape = list(x.shape) + shape[dim] = padding_size + pad = torch.zeros(shape, dtype=x.dtype, device=x.device) + return torch.cat([x, pad], dim=dim) + + +def _unpad_tensor(x: Tensor, dim: int, padding_size: int) -> Tensor: + slc = [slice(None)] * len(x.shape) + slc[dim] = slice(0, -padding_size) + return x[tuple(slc)] + + +def slice_input_tensor(x: Tensor, dim: int, padding: bool = True, group: ProcessGroup = None) -> Tensor: + group = get_ulysses_sequence_parallel_group() if group is None else group + sp_world_size = dist.get_world_size(group) + sp_rank = get_ulysses_sequence_parallel_rank() + dim_size = x.size(dim) + # pad before slice + if padding and dim_size % sp_world_size: + padding_size = sp_world_size - (dim_size % sp_world_size) + x = _pad_tensor(x, dim, padding_size) + # slice the input tensor + parts = x.size(dim) // sp_world_size + slc = [slice(None)] * len(x.shape) + slc[dim] = slice(sp_rank * parts, (sp_rank + 1) * parts) + return x[tuple(slc)].contiguous() + + +def all_to_all_tensor( + local_input: Tensor, + scatter_dim: int, + gather_dim: int, + group: Optional[dist.ProcessGroup] = None, + async_op: bool = False, +): + group = get_ulysses_sequence_parallel_group() if group is None else group + seq_world_size = dist.get_world_size(group) + input_list = [t.contiguous() for t in torch.tensor_split(local_input, seq_world_size, scatter_dim)] + output_list = [torch.empty_like(input_list[0]) for _ in range(seq_world_size)] + comm = dist.all_to_all(output_list, input_list, group=group, async_op=async_op) + if async_op: + + def wait(): + comm.wait() + return torch.cat(output_list, dim=gather_dim).contiguous() + + return wait + return torch.cat(output_list, dim=gather_dim).contiguous() + + +def all_gather_tensor(local_tensor: Tensor, group: Optional[dist.ProcessGroup] = None, async_op: bool = False): + group = get_ulysses_sequence_parallel_group() if group is None else group + sp_world_size = dist.get_world_size(group=group) + output_shape = list(local_tensor.shape) + output_shape[0] = output_shape[0] * sp_world_size + output = torch.empty(output_shape, dtype=local_tensor.dtype, device=local_tensor.device) + dist.all_gather_into_tensor(output, local_tensor, group=group, async_op=async_op) + return output + + +class SeqAllToAll(torch.autograd.Function): + @staticmethod + def forward( + ctx: Any, + group: dist.ProcessGroup, + local_input: Tensor, + scatter_dim: int, + gather_dim: int, + async_op: bool = False, + ) -> Tensor: + ctx.group = group + ctx.scatter_dim = scatter_dim + ctx.gather_dim = gather_dim + ctx.async_op = async_op + return all_to_all_tensor(local_input, scatter_dim, gather_dim, group, async_op) + + @staticmethod + def backward(ctx: Any, *grad_output: Tensor) -> tuple[None, Tensor, None, None]: + input_t = torch.cat(grad_output[1:], dim=ctx.gather_dim).contiguous() if ctx.async_op else grad_output[0] + return ( + None, + all_to_all_tensor(input_t, ctx.gather_dim, ctx.scatter_dim, ctx.group, False), + None, + None, + None, + None, + ) + + +class Gather(torch.autograd.Function): + @staticmethod + def forward( + ctx: Any, + group: dist.ProcessGroup, + local_tensor: Tensor, + gather_dim: int, + grad_scaler: bool = True, + async_op=False, + ) -> Tensor: + ctx.group = group + ctx.gather_dim = gather_dim + ctx.grad_scaler = grad_scaler + ctx.async_op = async_op + + sp_world_size = dist.get_world_size(group=group) + ctx.sp_world_size = sp_world_size + + sp_rank = dist.get_rank(group=group) + ctx.sp_rank = sp_rank + + local_shape = list(local_tensor.size()) + split_size = local_shape[0] + part_size = local_shape[gather_dim] # store original size + ctx.part_size = part_size + + output = all_gather_tensor(local_tensor, group, async_op) + return torch.cat(output.split(split_size, dim=0), dim=gather_dim) + + @staticmethod + def backward(ctx: Any, grad_output: Tensor) -> Any: + if ctx.grad_scaler: + grad_output = grad_output * ctx.sp_world_size + return ( + None, + grad_output.split(ctx.part_size, dim=ctx.gather_dim)[ctx.sp_rank].contiguous(), + None, + None, + None, + None, + ) + + +def gather_outpus_and_unpad(*args, **kwargs): + raise RuntimeError( + "please use verl.utils.ulysses.gather_outputs_and_unpad instead of verl.utils.ulysses.gather_outpus_and_unpad" + ) + + +def gather_outputs_and_unpad( + x: Tensor, + gather_dim: int, + unpad_dim: int = None, + padding_size: int = 0, + grad_scaler: bool = True, + group: Optional[dist.ProcessGroup] = None, +): + """ + Gather a tensor across a process group and optionally unpad its padded elements. + + Args: + x (Tensor): Input tensor to gather. + gather_dim (int): Dimension along which to gather across ranks. + unpad_dim (int, optional): Dimension from which to remove padding. If None, no unpadding. + padding_size (int): Number of padding elements to remove on `unpad_dim`. Defaults to 0. + grad_scaler (bool): Whether to apply gradient scaling during gather. Defaults to True. + group (ProcessGroup, optional): Process group for gathering. If None, uses + `get_ulysses_sequence_parallel_group()`. If still None, returns `x` unchanged. + + Returns: + Tensor: The gathered tensor, with padding removed if requested. + """ + group = get_ulysses_sequence_parallel_group() if group is None else group + if group is None: + return x + x = Gather.apply(group, x, gather_dim, grad_scaler) + if unpad_dim is not None: + assert isinstance(padding_size, int), "padding size is not given or is not an integer" + if padding_size == 0: + return x + x = _unpad_tensor(x, unpad_dim, padding_size) + return x + + +def ulysses_pad( + input_ids_rmpad: torch.Tensor, position_ids_rmpad: Optional[torch.Tensor] = None, sp_size: int = 1, pad_value=0 +): + if position_ids_rmpad is not None: + assert position_ids_rmpad.size(-2) == 1 + assert input_ids_rmpad.size(-1) == position_ids_rmpad.size(-1) + if sp_size <= 1: + return input_ids_rmpad, position_ids_rmpad, 0 + _, total_seq_len = input_ids_rmpad.shape + pad_size = (sp_size - total_seq_len % sp_size) % sp_size + if pad_size > 0: + input_ids_rmpad = torch.nn.functional.pad(input_ids_rmpad, (0, pad_size), value=pad_value) + if position_ids_rmpad is not None: + pad_pos_ids = torch.arange(pad_size, device=position_ids_rmpad.device).unsqueeze(0) + if position_ids_rmpad.dim() == 3: + pad_pos_ids = pad_pos_ids.unsqueeze(0).repeat(position_ids_rmpad.size(0), 1, 1) + position_ids_rmpad = torch.cat((position_ids_rmpad, pad_pos_ids), dim=-1) + return input_ids_rmpad, position_ids_rmpad, pad_size + + +def ulysses_pad_and_slice_inputs( + input_ids_rmpad: torch.Tensor, + position_ids_rmpad: Optional[torch.Tensor] = None, + sp_size: int = 1, + skip_position_ids_rmpad: bool = False, + pad_value=0, +): + """ + Pad and slice input_ids to be divisible by sp_size + Pad position_ids to be divisible by sp_size. + + Note both input_ids_rmpad and position_ids_rmpad will be padded and sliced. + + The is the utility of pre-forward for ulysses sequence parallelism + + Args: + input_ids_rmpad: shape of [bsz, seqlen] + position_ids_rmpad: shape of [bsz, seqlen], where bsz must be 1 + sp_size (int): ulysses sequence parallelism size + skip_position_ids_rmpad: whether to skip position_ids_rmpad for VeOmniEngine + + Returns: + torch.Tensor: padded and sliced input_ids + torch.Tensor: padded and sliced position_ids + int: pad size + """ + input_ids_rmpad, position_ids_rmpad, pad_size = ulysses_pad( + input_ids_rmpad, position_ids_rmpad, sp_size, pad_value=pad_value + ) + input_ids_rmpad = slice_input_tensor(input_ids_rmpad, dim=1, padding=False) + if position_ids_rmpad is not None and not skip_position_ids_rmpad: + position_ids_rmpad = slice_input_tensor(position_ids_rmpad, dim=1, padding=False) + return input_ids_rmpad, position_ids_rmpad, pad_size + + +def validate_ulysses_config(num_heads, ulysses_sequence_size): + if ulysses_sequence_size > 1: + assert num_heads % ulysses_sequence_size == 0, ( + f"num_heads ({num_heads}) must be divisible by ulysses sequence size({ulysses_sequence_size})" + ) + + +class BaseShardingManager: + """Base sharding manager used for resharding weights/data across parallel groups.""" + + def __init__(self): + self.timing = {} + + def __enter__(self): + pass + + def __exit__(self, exc_type, exc_value, traceback): + pass + + def preprocess_data(self, data: "DataProto") -> "DataProto": + return data + + def postprocess_data(self, data: "DataProto") -> "DataProto": + return data + + +class FSDPUlyssesShardingManager(BaseShardingManager): + """ + Sharding manager to support data resharding when using FSDP + Ulysses sequence parallelism. + """ + + def __init__(self, device_mesh: DeviceMesh): + super().__init__() + self.device_mesh = device_mesh + self.seed_offset = 12345 + + def __enter__(self): + if self.device_mesh is not None: + self.prev_sp_group = get_ulysses_sequence_parallel_group() + set_ulysses_sequence_parallel_group(self.device_mesh["sp"].get_group()) + + def __exit__(self, exc_type, exc_value, traceback): + if self.device_mesh is not None: + set_ulysses_sequence_parallel_group(self.prev_sp_group) + + def preprocess_data(self, data: "DataProto") -> "DataProto": + """ + AllGather data from sp region. + + This is because the data is first sharded along the FSDP dimension as we utilize the DP_COMPUTE. + In Ulysses, we need to make sure the same data is used across a SP group. + """ + if self.device_mesh is not None: + from verl.protocol import all_gather_data_proto + + group = self.device_mesh["sp"].get_group() + all_gather_data_proto(data=data, process_group=group) + return data + + def postprocess_data(self, data: "DataProto") -> "DataProto": + """ + Split the data to follow FSDP partition. + """ + if self.device_mesh is not None: + sp_size = self.device_mesh["sp"].size() + sp_rank = self.device_mesh["sp"].get_local_rank() + data = data.chunk(chunks=sp_size)[sp_rank] + return data diff --git a/verl_0720_main/verl/verl/utils/veomni/__init__.py b/verl_0720_main/verl/verl/utils/veomni/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1ce90c5eb352d85c59105c0dc85b5f1dd576f095 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/veomni/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/utils/veomni/router_replay.py b/verl_0720_main/verl/verl/utils/veomni/router_replay.py new file mode 100644 index 0000000000000000000000000000000000000000..d1850af83e213f343f7dd85a58661ce635edb0f6 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/veomni/router_replay.py @@ -0,0 +1,522 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""VeOmni-flavored MoE router replay. + +Self-contained record/replay controller for verl's VeOmni engine. Tapped +into VeOmni's MoE ``SparseMoeBlock.forward`` via +``veomni.utils.moe_router_replay.set_active_replay`` (a module-level +singleton slot); the patched forward calls +``maybe_replay_indices(module, routing_scores, top_indices)`` on each MoE +layer, which delegates to :meth:`VeOmniRouterReplay.on_router_forward`. + +Lifecycle (per ``forward_backward_batch``):: + + # Outside every micro-batch loop: + replay.begin_record() # R2 compute_log_prob + or replay.begin_replay() # R2 actor update, or R3 everywhere + + # Inside the micro-batch loop, before each forward: + replay.set_microbatch_targets(per_layer_targets) # REPLAY only + + # After every micro-batch finishes: + # (nothing — state carries across recompute-in-backward too) + + # After the whole step: + routed_experts = replay.collect_recorded(...) # RECORD only + replay.clear() + +Layer indexing +-------------- +RR uses **id-keyed positional** indexing. The first time each MoE router +fires, we assign the next position (``len(_id_to_pos)``) to ``id(module)``; +every subsequent call for that module reuses the same position. This is +the key correctness property under **activation checkpointing**: backward +recompute fires the same router modules again (in any order — per-layer +checkpoint segments differentiate from L-1 down to 0), and the id-keyed +lookup gives the correct position regardless of the order. A +monotonically-incremented cursor would walk past ``len(_targets)`` (REPLAY +crash) or grow phantom slots in ``_recorded`` (RECORD corruption). + +Layer position L is learned implicitly from input shapes: + * RECORD: ``len(_recorded)`` after the first full forward establishes L. + * REPLAY: ``set_microbatch_targets`` stashes a list of length L — no + prior id-mapping discovery needed (this is what unblocks R3 step 1, + where REPLAY runs *before* any RECORD has populated the id table). + +Parallelism scope +----------------- +VeOmni uses FSDP2 + optional Ulysses SP, no pipeline parallelism. The +RECORD all-gather and REPLAY pad+slice both go through +``verl.utils.ulysses`` so the SP layout matches what +``super().prepare_model_inputs`` applies to ``input_ids``. +""" + +from __future__ import annotations + +import os +from enum import Enum +from typing import TYPE_CHECKING + +import torch +import torch.distributed as dist + +from verl.utils.ulysses import all_gather_tensor, slice_input_tensor + +if TYPE_CHECKING: + import torch.nn as nn + + +__all__ = ["RouterReplayAction", "VeOmniRouterReplay"] + + +class RouterReplayAction(Enum): + DISABLED = "disabled" + RECORD = "record" + REPLAY = "replay" + + +class VeOmniRouterReplay: + """Router replay controller for VeOmni (FSDP2 + optional Ulysses SP). + + Single self-contained class: state machine, layer discovery, RECORD / + REPLAY forward dispatch, cross-rank aggregation, and the VeOmni-side + install/uninstall hookup all live here. No abstract base — see the + module docstring for why VeOmni and Megatron/mindspeed intentionally + keep separate RR implementations. + """ + + # ------------------------------------------------------------ lifecycle + + def __init__(self, sp_group: dist.ProcessGroup | None = None) -> None: + self._sp_group = sp_group + self._action: RouterReplayAction = RouterReplayAction.DISABLED + # id(router_module) -> position. Populated lazily on first sight + # of each router; stable across the lifetime of the controller + # (FSDP2 / LoRA wrappers don't mutate module identity per forward). + # Re-discovered between RECORD and REPLAY phases (cleared by + # ``begin_record`` / ``begin_replay``); this is just an optimization + # bookkeeping reset — the same model produces the same id table. + self._id_to_pos: dict[int, int] = {} + # RECORD: per-layer-position list of [local_nnz, topk] tensors, one + # per micro-batch. Inner list length == num_micro_batches at collect + # time; outer list grows as new routers fire on the *first* + # micro-batch. Recompute-in-backward (which fires the same routers + # again, in any order under per-layer checkpointing) is detected via + # ``len(_recorded[pos]) == _mb_index + 1`` and skipped. + self._recorded: list[list[torch.Tensor]] = [] + # REPLAY: positional list of target tensors for the *current* + # micro-batch. Re-built per micro-batch by ``set_microbatch_targets``. + self._targets: list[torch.Tensor] = [] + # REPLAY only: optional per-token mask of shape ``[local_nnz]``, + # bool, True where the recorded targets are valid (substitute) and + # False where they are placeholders (fall through to native + # routing). Populated by ``set_microbatch_targets``. R3 needs this + # because the rollout backend only captures response-token routing + # and writes zeros for prompt tokens; substituting all tokens with + # those zeros sends every prompt token's topk slots to expert 0, + # which corrupts the EP all-to-all token distribution. + self._replay_mask: torch.Tensor | None = None + # RECORD only: which micro-batch is currently being recorded. Bumped + # by ``advance_record_microbatch`` between micro-batches; used both + # to detect recompute (slot already filled for this mb) and to + # validate the per-layer slot list is dense (no skipped mbs). + self._mb_index: int = 0 + # Env-gated shape sanity check. + self._debug: bool = os.environ.get("VERL_ROUTER_REPLAY_DEBUG") == "1" + self._installed: bool = False + + @property + def action(self) -> RouterReplayAction: + return self._action + + @property + def num_layers(self) -> int: + """Number of MoE layers discovered so far. RECORD: established by the + first full forward (``len(_recorded)``). REPLAY: known directly from + ``set_microbatch_targets`` input length (``len(_targets)``).""" + return max(len(self._recorded), len(self._targets)) + + # -------------------------------------------------------- install/uninstall + + def install(self, model: nn.Module) -> None: + """Register this controller with VeOmni's global ``set_active_replay`` slot. + + After returning, every MoE router forward in ``model`` should reach + :meth:`on_router_forward` via ``maybe_replay_indices``. + """ + try: + from veomni.utils.moe_router_replay import set_active_replay, validate_model_for_replay # type: ignore + except ImportError as e: + raise RuntimeError( + "router_replay.mode != 'disabled' requires a VeOmni build that " + "exposes `veomni.utils.moe_router_replay.set_active_replay`. " + "Either upgrade VeOmni or set router_replay.mode='disabled'." + ) from e + # Fail fast if this model family has not been wired for replay + # (would otherwise surface as a cryptic mid-forward error from the + # controller's collect/set_microbatch_targets path). + validate_model_for_replay(model) + set_active_replay(self) + self._installed = True + + def uninstall(self) -> None: + """Reverse :meth:`install`. Idempotent.""" + if not self._installed: + return + try: + from veomni.utils.moe_router_replay import set_active_replay # type: ignore + + set_active_replay(None) + except ImportError: + pass + self._installed = False + + # ----------------------------------------------------- router-side entry + + def on_router_forward( + self, + module: nn.Module, + routing_scores: torch.Tensor, + top_indices: torch.Tensor, + ) -> torch.Tensor: + """Called from each MoE router forward via VeOmni's hook surface. + + Indices-only: records ``top_indices`` in RECORD mode or returns + substituted target indices in REPLAY mode. All model-specific + post-topk weight math (gather, renorm, scaling, dtype cast) lives + in the per-family patched ``SparseMoeBlock.forward``, not here — + that keeps the controller model-agnostic across MoE families + (softmax/sigmoid gating, group topk, expert bias, scaling factors, + etc.). ``routing_scores`` is accepted for optional debug inspection + but NOT used to derive weights. + + Position assignment is keyed on ``id(module)`` so backward recompute + under activation checkpointing (which fires routers again, possibly + in non-sequential order under per-layer checkpoint segments) lands + on the same position as the original forward. + """ + mid = id(module) + if mid in self._id_to_pos: + pos = self._id_to_pos[mid] + new_layer = False + else: + pos = len(self._id_to_pos) + self._id_to_pos[mid] = pos + new_layer = True + + if self._debug: + # Cheap shape sanity check — cross-family safe (no weight math). + # Env-gated; off by default. + if routing_scores.dim() != 2 or top_indices.dim() != 2: + raise AssertionError( + f"router_replay: expected 2D tensors, got routing_scores " + f"{tuple(routing_scores.shape)} and top_indices " + f"{tuple(top_indices.shape)}." + ) + if routing_scores.shape[0] != top_indices.shape[0]: + raise AssertionError( + f"router_replay: routing_scores / top_indices row count " + f"mismatch: {routing_scores.shape[0]} vs {top_indices.shape[0]}." + ) + + if self._action is RouterReplayAction.RECORD: + if new_layer: + # First time we've seen this router — grow the slot list. + # Must be on the first micro-batch; later mbs would see the + # router already mapped. + self._recorded.append([]) + slot = self._recorded[pos] + # Recompute detection: this layer has already been captured for + # the current mb. Skip the append (re-recording would duplicate + # an already-deterministic value). Snapshot was via + # ``.detach().clone()`` so the originally captured tensor is + # independent of the autograd graph that produced it. + if len(slot) == self._mb_index + 1: + return top_indices + if len(slot) != self._mb_index: + # Should never happen — would indicate skipped or extra + # micro-batches earlier in the step. + raise RuntimeError( + f"router_replay RECORD invariant violated at layer pos={pos}: " + f"slot has {len(slot)} entries, expected {self._mb_index} " + f"before appending mb {self._mb_index}. Possible cause: " + "router fired in some micro-batches but not others." + ) + slot.append(top_indices.detach().clone()) + return top_indices + + if self._action is RouterReplayAction.REPLAY: + # Strict: every layer position must have a target. There is no + # silent fallback — a missing target indicates a real plumbing + # bug (routed_experts not propagated, layer count mismatch + # between RECORD and REPLAY models, or the engine forgot to call + # set_microbatch_targets before this forward). + if pos >= len(self._targets): + raise RuntimeError( + f"router_replay REPLAY: layer pos={pos} has no target " + f"(only {len(self._targets)} targets set for this " + "micro-batch). Likely cause: model has more MoE layers " + "than the recorded routed_experts tensor describes, or " + "set_microbatch_targets was not called before forward." + ) + target = self._targets[pos] + if self._replay_mask is None: + substituted = target + else: + # Per-token gated substitution: where the mask is True the + # recorded target is valid (substitute); where False the + # target is a placeholder and we must fall through to native + # routing. R3 needs this to skip prompt tokens that the + # rollout backend wrote zeros for. + mask = self._replay_mask + if mask.shape[0] != top_indices.shape[0]: + raise RuntimeError( + f"router_replay REPLAY: replay_mask has {mask.shape[0]} rows " + f"but top_indices has {top_indices.shape[0]}. The mask must " + "be sliced with the same Ulysses SP rule as the targets." + ) + substituted = torch.where(mask.unsqueeze(-1), target, top_indices) + + # Defensive duplicate-detection. + # + # VeOmni's MoE expert dispatch (``permute()`` in + # ``veomni/distributed/moe/moe_utils.py``) builds the permuted + # tensor via ``routing_map.bool().masked_select(...)``, which + # collapses duplicate top-k slots within one token to a single + # entry. ``input_splits`` keeps counting every slot, so the two + # diverge whenever ANY token has duplicate top-k experts and the + # EP all-to-all crashes with + # ``RuntimeError: Split sizes doesn't match total dim 0 size``. + # + # Recorded targets can contain such duplicate rows when the + # rollout backend writes zero placeholders (e.g. left/right pad, + # or capture regions that don't match the response_mask). The + # mask filters most of these, but the exact contract differs + # across backends — fall back to native routing for any row whose + # substituted top-k contains a duplicate. Native indices are + # always distinct top-k choices, so this is correct regardless + # of what the rollout produces. + sorted_sub, _ = substituted.sort(dim=-1) + has_duplicate = (sorted_sub[:, 1:] == sorted_sub[:, :-1]).any(dim=-1) + return torch.where(has_duplicate.unsqueeze(-1), top_indices, substituted) + + return top_indices + + # --------------------------------------------------- engine-side drivers + + def begin_record(self) -> None: + """Enter RECORD mode. Must be called before the micro-batch loop.""" + self._action = RouterReplayAction.RECORD + self._recorded = [] + self._targets = [] + self._replay_mask = None + self._id_to_pos = {} + self._mb_index = 0 + + def begin_replay(self) -> None: + """Enter REPLAY mode. Must be called before the micro-batch loop.""" + self._action = RouterReplayAction.REPLAY + self._targets = [] + self._replay_mask = None + self._id_to_pos = {} + # _mb_index is unused in REPLAY (recompute is detected via id-keyed + # lookup hitting an already-mapped module, not via mb counters). + self._mb_index = 0 + + def advance_record_microbatch(self) -> None: + """Mark the start of a new RECORD micro-batch. + + Call this in the engine driver immediately after the previous + micro-batch's forward+backward returns and before the next one + starts. Bumps ``_mb_index`` so :meth:`on_router_forward` knows which + slot to fill on the next router fire. Recompute-in-backward (which + fires within the *same* micro-batch's backward) does NOT call this — + it's detected via id-keyed lookup hitting a slot whose length already + equals ``_mb_index + 1``. + """ + if self._action is not RouterReplayAction.RECORD: + raise RuntimeError(f"advance_record_microbatch requires RECORD action, got {self._action}") + self._mb_index += 1 + + def set_microbatch_targets( + self, + per_layer_targets: list[torch.Tensor], + replay_mask: torch.Tensor | None = None, + ) -> None: + """Load per-layer target indices for the upcoming micro-batch forward. + + ``per_layer_targets[i]`` is ``[local_nnz, topk]`` int64 on device, + ordered by layer position (matches the order in which routers fire + during forward — established when each router gets its position + assigned in :meth:`on_router_forward`). Strict: REPLAY mode must + already be active. ``L`` is taken from the input list length — no + prior id-mapping discovery is required, which is what unblocks R3 + step 1 where REPLAY runs before any RECORD has populated the table. + + ``replay_mask`` (optional, ``[local_nnz]`` bool): per-token gate. + Where True, substitute with the recorded target. Where False, fall + through to the native router output. Required for R3, where the + rollout backend captures only response-token routing and writes + zeros for prompt tokens — without the mask, those zeros would be + substituted, sending every prompt token's topk slots to expert 0 + and corrupting the EP all-to-all token distribution. + + The mask must be in the rmpad ``[local_nnz]`` layout (same SP + slice rule as ``per_layer_targets``). The engine driver builds it + from ``response_mask`` (strided ``(bs, max_response_len)``) plus + ``input_ids.offsets()`` via per-sample length arithmetic, then + runs it through :meth:`slice_microbatch_replay_mask`. Callers + should not pass the strided ``response_mask`` / ``loss_mask`` + directly — they have neither the right shape nor a valid + ``.values()`` for a strided layout. + + R2 callers should pass ``None`` (uniform substitution): R2 + RECORD captures the actor's full-sequence routing (prompt + + response), so applying a response-only gate would leak prompt- + token routing divergence into the bit-equal forward guarantee. + """ + if self._action is not RouterReplayAction.REPLAY: + raise RuntimeError(f"set_microbatch_targets requires REPLAY action, got {self._action}") + self._targets = list(per_layer_targets) + self._replay_mask = replay_mask.bool() if replay_mask is not None else None + + def clear(self) -> None: + """Reset the state machine between steps.""" + self._action = RouterReplayAction.DISABLED + self._recorded = [] + self._targets = [] + self._replay_mask = None + self._id_to_pos = {} + self._mb_index = 0 + + # ---------------------------------------------------- cross-rank gather + + def _all_gather_recorded(self, local: torch.Tensor) -> torch.Tensor: + """All-gather a ``[local_nnz, L, topk]`` tensor along Ulysses SP. + + Returns ``[nnz_padded, L, topk]`` where + ``nnz_padded = local_nnz * sp_size``. The caller trims the ulysses + pad suffix. + """ + if self._sp_group is None or dist.get_world_size(self._sp_group) == 1: + return local + return all_gather_tensor(local.contiguous(), group=self._sp_group) + + def collect_recorded( + self, + pad_size_per_mb: list[int], + num_micro_batches: int, + ) -> list[torch.Tensor]: + """Aggregate recorded indices across ranks and unpack per micro-batch. + + For each micro-batch: + * Stack per-layer-position [local_nnz, topk] into [local_nnz, L, topk] + * All-gather along Ulysses SP to restore full nnz+pad + * Trim the ulysses pad suffix + + Returns a list of length ``num_micro_batches``, each element a + ``[total_nnz_mb, L, topk]`` int tensor. The caller (engine) is + responsible for (a) reordering micro-batches back to batch order + using the indices returned by ``prepare_micro_batches``, and (b) + converting to the nested/jagged layout expected by the trainer. + """ + if self._action is not RouterReplayAction.RECORD: + raise RuntimeError(f"collect_recorded requires RECORD action, got {self._action}") + if not self._recorded: + raise RuntimeError("collect_recorded called before any router fired.") + if len(pad_size_per_mb) != num_micro_batches: + raise ValueError(f"pad_size_per_mb length {len(pad_size_per_mb)} != {num_micro_batches}") + # Sanity: every recorded layer should have the same number of entries. + for pos, slot in enumerate(self._recorded): + if len(slot) != num_micro_batches: + raise RuntimeError( + f"Router layer pos={pos} recorded {len(slot)} micro-batches, " + f"expected {num_micro_batches}. Possible causes: " + "router skipped on some micro-batches, or forward failed mid-step." + ) + + per_mb: list[torch.Tensor] = [] + for mb in range(num_micro_batches): + per_layer = [slot[mb] for slot in self._recorded] + local = torch.stack(per_layer, dim=1) # [local_nnz, L, topk] + gathered = self._all_gather_recorded(local) + pad = pad_size_per_mb[mb] + if pad > 0: + gathered = gathered[:-pad] + per_mb.append(gathered) + return per_mb + + # ---------------------------------------------------- replay input prep + + def slice_microbatch_replay_targets(self, batch_routed_experts: torch.Tensor) -> list[torch.Tensor]: + """Prepare per-layer replay targets from a micro-batch's routed_experts. + + Reuses :func:`verl.utils.ulysses.slice_input_tensor` so the pad+split + rule matches the one ``super().prepare_model_inputs`` already applies + to ``input_ids``. The SP rank/world_size are derived from + ``self._sp_group`` internally — we never dig into ``parallel_state`` + here. + + Args: + batch_routed_experts: ``[mb_nnz, L, topk]`` int tensor, flattened + across the micro-batch (the ``.values()`` of the nested + jagged ``routed_experts``). + + Returns: + List of length ``L``, each ``[mb_nnz_local, topk]`` int64 tensor, + ready to feed :meth:`set_microbatch_targets`. + """ + if batch_routed_experts.dim() != 3: + raise ValueError(f"routed_experts must be [mb_nnz, L, topk], got {batch_routed_experts.shape}") + idx = batch_routed_experts.to(torch.int64) + if self._sp_group is not None and dist.get_world_size(self._sp_group) > 1: + idx = slice_input_tensor(idx, dim=0, padding=True, group=self._sp_group) + return list(idx.unbind(dim=1)) + + def slice_microbatch_replay_mask(self, batch_mask: torch.Tensor) -> torch.Tensor: + """Prepare a per-token replay mask using the same pad+slice rule as + :meth:`slice_microbatch_replay_targets`. + + Used to feed the optional ``replay_mask`` of + :meth:`set_microbatch_targets`. The input is a flat + ``[mb_nnz]`` bool/int tensor in the same rmpad layout as + ``input_ids.values()`` — it is NOT the engine's + ``response_mask`` directly (which is a strided + ``(bs, max_response_len)`` tensor and has the wrong shape). + The engine driver constructs the flat layout via per-sample + prompt/response length arithmetic (see + ``VeOmniEngineWithLMHead._maybe_push_router_replay_state``) + before calling this helper. + + Padding values (added by ``slice_input_tensor`` to make the tensor + SP-divisible) are filled with zero, matching the "no recorded data" + semantics — pad rows shouldn't be substituted regardless. + """ + if batch_mask.dim() != 1: + raise ValueError(f"replay_mask must be 1-D [mb_nnz], got {batch_mask.shape}") + m = batch_mask.to(torch.int64) + if self._sp_group is not None and dist.get_world_size(self._sp_group) > 1: + m = slice_input_tensor(m, dim=0, padding=True, group=self._sp_group) + return m.bool() + + # --------------------------------------------------------- debug helpers + + def assert_layer_count(self, expected: int) -> None: + """Assert the discovered layer count matches the model config.""" + if self.num_layers != expected: + raise AssertionError( + f"router_replay discovered {self.num_layers} MoE layers, " + f"model config says {expected}. Layer discovery is broken." + ) diff --git a/verl_0720_main/verl/verl/utils/vllm/__init__.py b/verl_0720_main/verl/verl/utils/vllm/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..6b38268f4d9c80c37228d8ce049343fc44e4f485 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/__init__.py @@ -0,0 +1,32 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from .npu_vllm_patch import apply_npu_vllm_patches +from .utils import TensorLoRARequest, VLLMHijack, is_version_ge + +# The contents of vllm/patch.py should not be imported here, because the contents of +# patch.py should be imported after the vllm LLM instance is created. Therefore, +# wait until you actually start using it before importing the contents of +# patch.py separately. + +# Apply NPU-specific vLLM patches when this module is imported. +# Remove this when https://github.com/vllm-project/vllm-ascend/issues/5915 is fixed. +apply_npu_vllm_patches() + +__all__ = [ + "TensorLoRARequest", + "VLLMHijack", + "is_version_ge", +] diff --git a/verl_0720_main/verl/verl/utils/vllm/npu_vllm_patch.py b/verl_0720_main/verl/verl/utils/vllm/npu_vllm_patch.py new file mode 100644 index 0000000000000000000000000000000000000000..6277f09fe8434ba455d9dda059b22a7ca73d6ebf --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/npu_vllm_patch.py @@ -0,0 +1,75 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Copyright 2025 The Qwen Team and The HuggingFace Inc. team +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from functools import wraps + +from verl.utils.device import is_torch_npu_available + + +def vllm_v013_weight_loader_method_wrapper(fn): + @wraps(fn) + def wrapper(self, param, loaded_weight, weight_name, shard_id, expert_id, return_success=False): + if (shard_id in ("w1", "w3") and param.shape[1] == self.hidden_size) or ( + shard_id == "w2" and param.shape[2] == self.hidden_size + ): + param.data = param.data.transpose(1, 2) + return fn(self, param, loaded_weight, weight_name, shard_id, expert_id, return_success) + + return wrapper + + +def patch_vllm013_rotary_emb(): + from vllm.model_executor.layers.rotary_embedding.common import ApplyRotaryEmb + + def vllm013_npu_rotary_embedding_init_impl( + self, + enforce_enable: bool = False, + is_neox_style: bool = True, + enable_fp32_compute: bool = False, + ) -> None: + super(ApplyRotaryEmb, self).__init__() + self.is_neox_style = is_neox_style + self.enable_fp32_compute = enable_fp32_compute + self.apply_rotary_emb_flash_attn = None + + ApplyRotaryEmb.__init__ = vllm013_npu_rotary_embedding_init_impl + + +def apply_npu_vllm_patches() -> None: + """Apply NPU-specific vLLM patches for weight loading and rotary embedding. + + Must be called before the vLLM engine is created. + """ + if not is_torch_npu_available(check_device=False): + return + + import vllm + from packaging import version + + _VLLM_VERSION = version.parse(vllm.__version__) + if _VLLM_VERSION >= version.parse("0.13.0") and _VLLM_VERSION <= version.parse("0.14.0"): + # Disable flash_attn in RotaryEmbedding (NPU) when VLLM >= 0.13 + from vllm.model_executor.layers.fused_moe import FusedMoE + + patch_vllm013_rotary_emb() + FusedMoE.weight_loader = vllm_v013_weight_loader_method_wrapper(FusedMoE.weight_loader) + elif _VLLM_VERSION >= version.parse("0.18.0"): + # Disable flash_attn in RotaryEmbedding (NPU) when VLLM >= 0.18 + from vllm.model_executor.layers.fused_moe import FusedMoE + + patch_vllm013_rotary_emb() + FusedMoE.weight_loader = vllm_v013_weight_loader_method_wrapper(FusedMoE.weight_loader) diff --git a/verl_0720_main/verl/verl/utils/vllm/patch.py b/verl_0720_main/verl/verl/utils/vllm/patch.py new file mode 100644 index 0000000000000000000000000000000000000000..951c5cad3d616b1c0472d64fee2c232c61973968 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/patch.py @@ -0,0 +1,142 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# To support different vLLM versions, we add the model into SUPPORTED_MOE_MODELS separately to avoid triggering +# unsupported issues. +SUPPORTED_MOE_MODELS = [] + +try: + from vllm.model_executor.models.deepseek_v2 import DeepseekV2ForCausalLM, DeepseekV3ForCausalLM + + SUPPORTED_MOE_MODELS.append(DeepseekV2ForCausalLM) + SUPPORTED_MOE_MODELS.append(DeepseekV3ForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.mixtral import MixtralForCausalLM + + SUPPORTED_MOE_MODELS.append(MixtralForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.qwen2_moe import Qwen2MoeForCausalLM + + SUPPORTED_MOE_MODELS.append(Qwen2MoeForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.qwen3_moe import Qwen3MoeForCausalLM + + SUPPORTED_MOE_MODELS.append(Qwen3MoeForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.qwen3_vl_moe import Qwen3MoeLLMForCausalLM + + SUPPORTED_MOE_MODELS.append(Qwen3MoeLLMForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.qwen3_next import Qwen3NextForCausalLM + + SUPPORTED_MOE_MODELS.append(Qwen3NextForCausalLM) +except ImportError: + pass + +try: + from vllm.model_executor.models.kimi_vl import KimiVLForConditionalGeneration + + SUPPORTED_MOE_MODELS.append(KimiVLForConditionalGeneration) +except ImportError: + pass + +try: + from vllm.model_executor.models.qwen3_5 import Qwen3_5MoeForCausalLM + + SUPPORTED_MOE_MODELS.append(Qwen3_5MoeForCausalLM) +except ImportError: + pass + + +def patch_vllm_moe_model_weight_loader(model): + # this is a work around to load the weight of vllm fused moe model + # it is from a bug from vllm 0.8.2 + # all the weights are supposed to have a weight_loader, but the moe weights + # do not have a weight_loader, so we need to patch it + # (True, 'model.embed_tokens.weight') + # (True, 'model.layers.0.self_attn.qkv_proj.weight') + # (True, 'model.layers.0.self_attn.qkv_proj.bias') + # (True, 'model.layers.0.self_attn.o_proj.weight') + # (True, 'model.layers.0.mlp.gate.weight') + # (True, 'model.layers.0.mlp.shared_expert.gate_up_proj.weight') + # (True, 'model.layers.0.mlp.shared_expert.down_proj.weight') + # (False, 'model.layers.0.mlp.shared_expert_gate.weight') use default + # (False, 'model.layers.0.input_layernorm.weight') use default + # (False, 'model.layers.0.post_attention_layernorm.weight') use default + # (False, 'model.layers.0.mlp.experts.w13_weight') use mlp.experts.weight_loader + # (False, 'model.layers.0.mlp.experts.w2_weight') use mlp.experts.weight_loader + + # Early return if no MOE models are supported + if not SUPPORTED_MOE_MODELS: + return + + original_model_type = type(model) + if hasattr(model, "runnable") and "ACLGraphWrapper" in str(original_model_type): + model = model.runnable + original_model_type = type(model) + + # Define MLP attribute mapping for different model types + MLP_ATTR_MAPPING = {} + try: + from vllm.model_executor.models.mixtral import MixtralForCausalLM + + MLP_ATTR_MAPPING[MixtralForCausalLM] = "block_sparse_moe" + except ImportError: + pass + + DEFAULT_MLP_ATTR = "mlp" + + # Get inner model (either model.model or model.language_model) + inner_model = getattr(model, "model", None) or getattr(model, "language_model", None) + if inner_model is None: + raise ValueError("The provided model does not have a valid 'model' or 'language_model' attribute.") + + if not isinstance(model, tuple(SUPPORTED_MOE_MODELS)) and not isinstance(inner_model, tuple(SUPPORTED_MOE_MODELS)): + return + + # TODO(@leisuzz): class Qwen3MoeLLMForCausalLM is not available if VLLM version < 0.11.0, + # will update the 'if statement' with 'isinstance' when verl commonly use VLLM version >= 0.11.0 + if type(inner_model).__name__ in ("Qwen3MoeLLMForCausalLM", "Qwen3_5MoeForCausalLM"): + inner_model = inner_model.model # Reassign inner_model in Qwen3-vl + + for layer_idx, layer in enumerate(inner_model.layers): + mlp_attr = MLP_ATTR_MAPPING.get(original_model_type, DEFAULT_MLP_ATTR) + + mlp = getattr(layer, mlp_attr, None) + if not mlp: + continue + + experts = getattr(mlp, "experts", None) + if not experts or not hasattr(experts, "weight_loader"): + continue + + # Patch the weight loaders + for name, param in mlp.named_parameters(): + if "w13_weight" in name or "w2_weight" in name: + param.weight_loader = experts.weight_loader diff --git a/verl_0720_main/verl/verl/utils/vllm/utils.py b/verl_0720_main/verl/verl/utils/vllm/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..1ac655fcf603b660a28ed56c93f0fd2d4117f0e6 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/utils.py @@ -0,0 +1,128 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from msgspec import field +from packaging import version as vs + +try: + from vllm.lora.lora_model import LoRAModel +except ImportError: + from vllm.lora.models import LoRAModel + +from vllm.lora.request import LoRARequest +from vllm.lora.utils import get_adapter_absolute_path +from vllm.lora.worker_manager import LRUCacheWorkerLoRAManager + +from verl.third_party.vllm import get_version + + +class TensorLoRARequest(LoRARequest): + peft_config: dict = field(default=None) + lora_tensors: dict = field(default=None) + + +class VLLMHijack: + @staticmethod + def hijack(): + def hijack__load_adapter(self, lora_request: TensorLoRARequest) -> LoRAModel: + """ + based on vllm.lora.worker_manager.WorkerLoRAManager._load_adapter, support load adapter with lora tensors + + Reason: + VLLM does not support adding LoRA from tensors directly. It only supports adding LoRA via file paths. + To synchronize the LoRA tensors of the actor model, we need to find a workaround to enable VLLM to + load memory-based LoRA tensors. + """ + try: + supported_lora_modules = self._adapter_manager.supported_lora_modules + packed_modules_mapping = self._adapter_manager.packed_modules_mapping + expected_lora_modules: list[str] = [] + for module in supported_lora_modules: + if module in packed_modules_mapping: + expected_lora_modules.extend(packed_modules_mapping[module]) + else: + expected_lora_modules.append(module) + + expected_lora_modules = list(set(expected_lora_modules)) + + lora_tensors = None + from vllm.lora.peft_helper import PEFTHelper + + if isinstance(lora_request, TensorLoRARequest): + peft_config = lora_request.peft_config + lora_tensors = lora_request.lora_tensors + peft_helper = PEFTHelper.from_dict(peft_config) + else: + lora_path = get_adapter_absolute_path(lora_request.lora_path) + + peft_helper = PEFTHelper.from_local_dir(lora_path, self.max_position_embeddings) + + # Validates the LoRA configuration against requirements before + # loading weights, throwing an exception if validation fails. + peft_helper.validate_legal(self.lora_config) + + # For some models like Qwen2VL, we need to use hf_to_vllm_mapper + # to ensure correct loading of lora weights. + model = self._adapter_manager.model + hf_to_vllm_mapper = None + if hasattr(model, "hf_to_vllm_mapper") and model.hf_to_vllm_mapper is not None: + hf_to_vllm_mapper = model.hf_to_vllm_mapper + + lora_request_kwargs = { + "peft_helper": peft_helper, + "lora_model_id": lora_request.lora_int_id, + "device": "cpu", + "dtype": self.lora_config.lora_dtype, + "weights_mapper": hf_to_vllm_mapper, + } + if hasattr(self, "embedding_padding_modules"): + lora_request_kwargs["embedding_modules"] = self.embedding_modules + lora_request_kwargs["embedding_padding_modules"] = self.embedding_padding_modules + else: + lora_request_kwargs["model_vocab_size"] = self.vocab_size + if hasattr(self.lora_config, "lora_extra_vocab_size"): + lora_request_kwargs["target_embedding_padding"] = ( + self.vocab_size + self.lora_config.lora_extra_vocab_size + ) + if isinstance(lora_request, TensorLoRARequest): + lora = self._lora_model_cls.from_lora_tensors( + tensors=lora_tensors, + **lora_request_kwargs, + ) + else: + lora = self._lora_model_cls.from_local_checkpoint( + lora_path, + expected_lora_modules, + **lora_request_kwargs, + ) + except Exception: + raise + + if getattr(lora, "extra_vocab_size", 0) > getattr(self.lora_config, "lora_extra_vocab_size", 0): + raise ValueError( + f"LoRA added vocab size {lora.extra_vocab_size} is greater than lora_extra_vocab_size " + f"{self.lora_config.lora_extra_vocab_size}." + ) + return lora + + def do_hijack(target_cls, target_method_name, hooking_method): + setattr(target_cls, target_method_name, hooking_method) + + do_hijack(LRUCacheWorkerLoRAManager, "_load_adapter", hijack__load_adapter) + + +def is_version_ge(pkg: str = "vllm", minver: str = "0.7.3"): + """check if the package version is greater than or equal to the minimum version""" + return vs.parse(get_version(pkg)) >= vs.parse(minver) diff --git a/verl_0720_main/verl/verl/utils/vllm/vllm_dsv4_fp8_utils.py b/verl_0720_main/verl/verl/utils/vllm/vllm_dsv4_fp8_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..29602b883fd101f92ad36dbd4d95d2cb0973fa7a --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/vllm_dsv4_fp8_utils.py @@ -0,0 +1,352 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from types import MethodType + +import torch +from vllm.model_executor.layers.fused_moe.layer import FusedMoE +from vllm.model_executor.layers.linear import LinearBase + + +def is_deepseek_v4_model(model): + if model is None: + return False + + for obj in (model, getattr(model, "config", None), getattr(model, "hf_config", None)): + if obj is not None and getattr(obj, "model_type", None) is not None: + return obj.model_type == "deepseek_v4" + + text_config = getattr(getattr(model, "config", None), "text_config", None) + return getattr(text_config, "model_type", None) == "deepseek_v4" + + +def iter_deepseek_v4_weights(weights): + for name, weight in weights: + if ".experts." in name and weight.dtype in (torch.int8, torch.float8_e8m0fnu): + weight = weight.view(torch.uint8) + yield name, weight + + +def _is_mega_moe_module(module): + from vllm.models.deepseek_v4.nvidia.model import DeepseekV4MegaMoEExperts + + return isinstance(module, DeepseekV4MegaMoEExperts) + + +def _is_mxfp4_fused_moe_module(module): + from vllm.model_executor.layers.quantization.mxfp4 import Mxfp4MoEMethod + + return isinstance(module, FusedMoE) and isinstance(module.quant_method, Mxfp4MoEMethod) + + +def _make_mxfp4_moe_param(shape, device, weight_loader, quant_method=None): + param = torch.nn.Parameter(torch.empty(shape, dtype=torch.uint8, device=device), requires_grad=False) + param.weight_loader = weight_loader + if quant_method is not None: + param.quant_method = quant_method + return param + + +def _wrap_vllm_param(custom_param, source_param, copy_param_subclass_attrs): + param = torch.nn.Parameter(custom_param.data, requires_grad=False) + copy_param_subclass_attrs(param, source_param) + copy_param_subclass_attrs(param, custom_param) + return param + + +def _get_param_weight_loader(param): + return getattr(param, "weight_loader", None) or getattr(param, "_weight_loader", None) + + +def _param_parallel_dim(param, public_name, private_name, default): + if hasattr(param, private_name): + return getattr(param, private_name) + return getattr(param, public_name, default) + + +def _copy_loaded_weight(param, loaded_weight): + param.data.copy_(loaded_weight.to(device=param.device, dtype=param.dtype)) + + +def _try_load_column_weight(param, loaded_weight): + tp_size = int(getattr(param, "tp_size", 1)) + tp_rank = int(getattr(param, "tp_rank", 0)) + if tp_size <= 1: + return False + + data = param.data + if loaded_weight.ndim == data.ndim + 1 and loaded_weight.shape == torch.Size((tp_size, *data.shape)): + _copy_loaded_weight(param, loaded_weight.select(0, tp_rank)) + return True + + if data.ndim >= 2 and loaded_weight.ndim == data.ndim - 1: + expected_shape = (tp_size * data.shape[0] * data.shape[1], *data.shape[2:]) + if loaded_weight.shape == torch.Size(expected_shape): + global_shape = (tp_size, data.shape[0], data.shape[1], *data.shape[2:]) + _copy_loaded_weight(param, loaded_weight.reshape(global_shape).select(0, tp_rank)) + return True + + if data.ndim == loaded_weight.ndim and data.ndim > 0: + expected_shape = (tp_size * data.shape[0], *data.shape[1:]) + if loaded_weight.shape == torch.Size(expected_shape): + _copy_loaded_weight(param, loaded_weight.narrow(0, tp_rank * data.shape[0], data.shape[0])) + return True + + return False + + +def _try_load_merged_weight(param, loaded_weight, shard_offset, shard_size, shard_id): + output_dim = int(getattr(param, "output_dim", getattr(param, "_output_dim", 0))) + loaded_dim = loaded_weight.shape[output_dim] + offsets = [] + if isinstance(shard_offset, int): + offsets.append(shard_offset) + if isinstance(shard_size, int) and shard_size > 0: + offsets.append(shard_offset * loaded_dim // shard_size) + if isinstance(shard_id, int): + offsets.append(shard_id * loaded_dim) + + for offset in dict.fromkeys(offsets): + if offset < 0 or offset + loaded_dim > param.shape[output_dim]: + continue + target = param.data.narrow(output_dim, offset, loaded_dim) + if target.shape == loaded_weight.shape: + target.copy_(loaded_weight.to(device=target.device, dtype=target.dtype)) + return True + return False + + +def _attach_weight_loaders(param): + subclass_type = getattr(param, "subclass_type", None) + if subclass_type is None or getattr(param, "_verl_deepseek_v4_loaders", False): + return + + original_column_loader = getattr(subclass_type, "load_column_parallel_weight", None) + if original_column_loader is not None: + + def load_column_parallel_weight(self, *args, **kwargs): + loaded_weight = kwargs.get("loaded_weight", args[0] if args else None) + if loaded_weight is not None: + if self.shape == loaded_weight.shape: + _copy_loaded_weight(self, loaded_weight) + return + if _try_load_column_weight(self, loaded_weight): + return + return original_column_loader(self, *args, **kwargs) + + param.load_column_parallel_weight = MethodType(load_column_parallel_weight, param) + + original_merged_loader = getattr(subclass_type, "load_merged_column_weight", None) + if original_merged_loader is not None: + + def load_merged_column_weight(self, *args, **kwargs): + loaded_weight = kwargs.get("loaded_weight", args[0] if args else None) + shard_id = kwargs.get("loaded_shard_id", kwargs.get("shard_id", args[1] if len(args) > 1 else None)) + shard_offset = kwargs.get("shard_offset", args[2] if len(args) > 2 else None) + shard_size = kwargs.get("shard_size", args[3] if len(args) > 3 else None) + if loaded_weight is not None and _try_load_merged_weight( + self, loaded_weight, shard_offset, shard_size, shard_id + ): + return + return original_merged_loader(self, *args, **kwargs) + + param.load_merged_column_weight = MethodType(load_merged_column_weight, param) + + param._verl_deepseek_v4_loaders = True + + +def _prepare_linear_params_for_loading(model, copy_param_subclass_attrs): + from vllm.model_executor.parameter import BlockQuantScaleParameter, ModelWeightParameter + + for layer in model.modules(): + if not isinstance(layer, LinearBase): + continue + + for name, param_type in ( + ("weight", ModelWeightParameter), + ("weight_scale_inv", BlockQuantScaleParameter), + ("weight_scale", BlockQuantScaleParameter), + ): + param = getattr(layer, name, None) + if not isinstance(param, torch.nn.Parameter): + continue + if not hasattr(param, "subclass_type"): + weight_loader = _get_param_weight_loader(param) + if weight_loader is None: + continue + param = _wrap_vllm_param( + param_type( + data=param.data, + output_dim=_param_parallel_dim(param, "output_dim", "_output_dim", 0), + input_dim=_param_parallel_dim(param, "input_dim", "_input_dim", 1), + weight_loader=weight_loader, + ), + param, + copy_param_subclass_attrs, + ) + setattr(layer, name, param) + + update_param_tp_status = getattr(layer, "update_param_tp_status", None) + if callable(update_param_tp_status): + update_param_tp_status() + + for name in ("weight", "weight_scale_inv", "weight_scale"): + param = getattr(layer, name, None) + if param is not None: + _attach_weight_loaders(param) + + +def _restore_moe_params_for_loading(model): + restored = False + for module in model.modules(): + if _is_mega_moe_module(module): + num_experts = module.num_local_experts + intermediate_size = module.intermediate_size + hidden_size = module.hidden_size + device = module._transformed_l1_weights[0].device + elif _is_mxfp4_fused_moe_module(module): + quant_method = module.quant_method + num_experts = quant_method.num_experts + intermediate_size = quant_method.intermediate_size + hidden_size = quant_method.hidden_size + device = module.w13_weight.device + else: + continue + + weight_loader = module.weight_loader + module.w13_weight = _make_mxfp4_moe_param( + (num_experts, 2 * intermediate_size, hidden_size // 2), device, weight_loader + ) + module.w2_weight = _make_mxfp4_moe_param( + (num_experts, hidden_size, intermediate_size // 2), device, weight_loader + ) + module.w13_weight_scale = _make_mxfp4_moe_param( + (num_experts, 2 * intermediate_size, hidden_size // 32), + device, + weight_loader, + quant_method="block", + ) + module.w2_weight_scale = _make_mxfp4_moe_param( + (num_experts, hidden_size, intermediate_size // 32), + device, + weight_loader, + quant_method="block", + ) + restored = True + return restored + + +def _process_moe_weights_after_loading(model): + for module in model.modules(): + if _is_mega_moe_module(module): + module._transformed_l1_weights = None + module._transformed_l2_weights = None + module.finalize_weights() + elif _is_mxfp4_fused_moe_module(module): + module.quant_method.process_weights_after_loading(module) + + +def prepare_deepseek_v4_weights_for_loading(model, copy_param_subclass_attrs): + _prepare_linear_params_for_loading(model, copy_param_subclass_attrs) + return _restore_moe_params_for_loading(model) + + +def process_deepseek_v4_weights_after_loading(model, moe_params_restored): + if moe_params_restored: + _process_moe_weights_after_loading(model) + reload_deepseek_v4_dense_fp8_scales(model) + + +def _normalize_dim(dim: int, ndim: int) -> int: + return dim + ndim if dim < 0 else dim + + +def _map_weight_name_for_vllm(model, name: str) -> str: + mapper = getattr(model, "hf_to_vllm_mapper", None) + map_name = getattr(mapper, "_map_name", None) + if callable(map_name): + mapped = map_name(name) + if mapped is not None: + return mapped + + mapped = name + if ".shared_experts.w2" in mapped: + mapped = mapped.replace(".shared_experts.w2", ".shared_experts.down_proj", 1) + if mapped.endswith(".scale"): + mapped = mapped[: -len(".scale")] + ".weight_scale_inv" + if mapped.startswith(("layers.", "embed.")): + mapped = "model." + mapped + elif mapped == "head.weight": + mapped = "lm_head.weight" + return mapped + + +def cache_deepseek_v4_dense_fp8_scales(model, weights): + if not is_deepseek_v4_model(model): + return + + scale_dtype = getattr(torch, "float8_e8m0fnu", None) + if scale_dtype is None: + return + + cache = getattr(model, "_verl_dense_fp8_scale_cache", None) + if cache is None: + cache = {} + model._verl_dense_fp8_scale_cache = cache + + for name, tensor in weights: + if name.endswith(".scale") and ".experts." not in name and tensor.dtype == scale_dtype: + cache[_map_weight_name_for_vllm(model, name)] = tensor.detach().clone() + + +def _copy_scale_shard(param: torch.nn.Parameter, loaded_scale: torch.Tensor) -> None: + target = param.data + loaded = loaded_scale.to(device=target.device, dtype=target.dtype) + if target.shape == loaded.shape: + target.copy_(loaded) + return + + if target.ndim != loaded.ndim: + return + + tp_rank = int(getattr(param, "tp_rank", 0)) + tp_size = int(getattr(param, "tp_size", 1)) + candidate_dims = [] + for attr in ("input_dim", "_input_dim", "output_dim", "_output_dim"): + if hasattr(param, attr): + dim = _normalize_dim(int(getattr(param, attr)), target.ndim) + if dim not in candidate_dims: + candidate_dims.append(dim) + + for dim in candidate_dims: + if loaded.shape[:dim] != target.shape[:dim] or loaded.shape[dim + 1 :] != target.shape[dim + 1 :]: + continue + if loaded.shape[dim] != target.shape[dim] * tp_size: + continue + start = tp_rank * target.shape[dim] + target.copy_(loaded.narrow(dim, start, target.shape[dim])) + return + + +def reload_deepseek_v4_dense_fp8_scales(model): + cache = getattr(model, "_verl_dense_fp8_scale_cache", None) + if not cache: + return + + params = dict(model.named_parameters()) + for name, scale in cache.items(): + param = params.get(name) + if param is not None: + _copy_scale_shard(param, scale) diff --git a/verl_0720_main/verl/verl/utils/vllm/vllm_fp8_utils.py b/verl_0720_main/verl/verl/utils/vllm/vllm_fp8_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..fa694096717ee32f1051080bbe891c0798031352 --- /dev/null +++ b/verl_0720_main/verl/verl/utils/vllm/vllm_fp8_utils.py @@ -0,0 +1,786 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import importlib.metadata +import inspect +import logging +from dataclasses import dataclass, field +from unittest.mock import patch + +import torch +from packaging import version + +try: + from vllm.model_executor.layers.fused_moe.layer import FusedMoE + from vllm.model_executor.layers.linear import LinearBase +except ImportError as e: + raise ImportError("FP8 quantization not available") from e + +from verl.utils.kernel.fp8_kernel import scaled_fp8_blockwise +from verl.utils.vllm.vllm_dsv4_fp8_utils import ( + cache_deepseek_v4_dense_fp8_scales, + is_deepseek_v4_model, + iter_deepseek_v4_weights, + prepare_deepseek_v4_weights_for_loading, + process_deepseek_v4_weights_after_loading, + reload_deepseek_v4_dense_fp8_scales, +) + +logger = logging.getLogger(__name__) + + +def _get_vllm_version(): + return version.parse(importlib.metadata.version("vllm")) + + +# Ref: https://github.com/NVIDIA-NeMo/RL/commit/bc24887c72a6e1b2699a228bc87c588546dfe6b7 +@dataclass() +class FP8State: + # A cache of fp8 parameter names, we can check this cache to see if a + # param name corresponds to a fp8 weight + seen_params: set = field(default_factory=lambda: set()) + fp8_param_names: set = field(default_factory=lambda: set()) + vllm_patches: list = field(default_factory=lambda: []) + + +fp8_state: FP8State = FP8State() + + +def _copy_param_subclass_attrs(dst_param, src_param): + if src_param is None: + return + + base_param_attrs = dir(torch.nn.Parameter) + for attr in dir(src_param): + if attr in base_param_attrs or attr.startswith("__"): + continue + try: + setattr(dst_param, attr, getattr(src_param, attr)) + except AttributeError: + pass + + subclass_type = getattr(src_param, "subclass_type", type(src_param)) + if subclass_type is not torch.nn.Parameter: + dst_param.subclass_type = subclass_type + + +def is_fp8_model(vllm_config): + from vllm.model_executor.layers.quantization.fp8 import Fp8Config + + if hasattr(vllm_config, "quant_config"): + if isinstance(vllm_config.quant_config, Fp8Config): + return True + elif is_mxfp8_vllm_ascend(vllm_config.quant_config): + return True + + return False + + +def get_module_from_param_name(model, name: str): + # Split the name into parts (e.g., 'layers', '0', 'self_attn', 'q_proj', 'weight') + # The module path is all but the last part (the parameter's own name) + path_parts = name.split(".") + module_path = path_parts[:-1] + # Replace with the fused model name + packed_modules_mapping = model.packed_modules_mapping + reversed_mapping = { + original_name: fused_name + for fused_name, original_names_list in packed_modules_mapping.items() + for original_name in original_names_list + } + if module_path[-1] in reversed_mapping.keys(): + module_path[-1] = reversed_mapping[module_path[-1]] + + current_module = model + try: + # Traverse the model hierarchy + for part in module_path: + if isinstance(current_module, FusedMoE): + return current_module + elif isinstance(current_module, torch.nn.ModuleList): + current_module = current_module[int(part)] + else: + current_module = getattr(current_module, part) + except (AttributeError, IndexError, ValueError) as e: + print(f"Warning: Could not find module for parameter '{name}'. Error: {e}") + return current_module + + +def is_fp8_weight(name, model): + if name not in fp8_state.seen_params: + fp8_state.seen_params.add(name) + # Filter out bias params + if name.endswith("weight"): + module = get_module_from_param_name(model, name) + # We currently only quantize linear layers + + if (isinstance(module, LinearBase) and module.weight.dtype == torch.float8_e4m3fn) or ( + isinstance(module, FusedMoE) + and module.w13_weight.dtype == torch.float8_e4m3fn + and module.w2_weight.dtype == torch.float8_e4m3fn + ): + fp8_state.fp8_param_names.add(name) + return name in fp8_state.fp8_param_names + + +def is_mxfp8_vllm_ascend(quant_config): + try: + from vllm_ascend.quantization.modelslim_config import AscendModelSlimConfig + + if isinstance(quant_config, AscendModelSlimConfig): + quant_method = quant_config.quant_description.get("quant_method") + return quant_method in ["ascend"] + return False + except ImportError: + # vllm_ascend not installed, so this can't be an Ascend MXFP8 config + return False + + +def restore_mxfp8_weights_for_loading(model): + for name, module in model.named_modules(): + if ( + hasattr(module, "_mxfp8_transformed") + and hasattr(module, "quant_method") + and hasattr(module.quant_method, "quant_method") + and hasattr(module.quant_method.quant_method, "restore_weights_for_rl_loading") + ): + module.quant_method.quant_method.restore_weights_for_rl_loading(module) + + +def apply_mxfp8_transformation_after_loading(model): + """Re-apply MXFP8 transformations after weight loading. + + This function iterates through all linear modules in the model and applies + the MXFP8 transformations (transpose, reshape) that are required for NPU + inference. + + Must be called AFTER model.load_weights() in RL training loops. + """ + try: + from vllm.model_executor.layers.linear import LinearBase + except ImportError: + logger.warning("Could not import LinearBase, skipping MXFP8 transformation") + return + + for name, module in model.named_modules(): + if (isinstance(module, LinearBase) or isinstance(module, FusedMoE)) and hasattr( + module, "_mxfp8_original_shapes" + ): + if hasattr(module, "quant_method") and hasattr(module.quant_method, "process_weights_after_loading"): + logger.debug(f"Applying MXFP8 transformation for module: {name}") + module.quant_method.process_weights_after_loading(module) + + +def quant_weights(weights, model, quant_config, dtype=torch.bfloat16): + """Quantize weights to FP8 format using a memory-efficient generator. + + + Args: + weights: Generator or iterable of (name, tensor) pairs + model: The model to check for FP8 weight names + quant_config: Quantization configuration with weight_block_size + dtype: Data type for intermediate computation (default: bfloat16) + + Yields: + Tuples of (name, tensor) for each weight and its scale + """ + + if is_deepseek_v4_model(model): + yield from iter_deepseek_v4_weights(weights) + return + + fp8_state.seen_params.clear() + fp8_state.fp8_param_names.clear() + is_mxfp8_npu = is_mxfp8_vllm_ascend(quant_config) + if is_mxfp8_npu: + import torch_npu + # vLLM v0.11-v0.12 renamed weight_scale_inv → weight_scale in process_weights_after_loading, + # so load_weights expects "_scale" suffix. v0.14+ keeps weight_scale_inv, so expects "_scale_inv". + vllm_ver = _get_vllm_version() + _use_scale_not_scale_inv = version.parse("0.11.0") <= vllm_ver < version.parse("0.14.0") + + for k, v in weights: + if not is_fp8_weight(k, model): + yield (k, v) + continue + + # Cast the weight into fp8 and its scale factor + if torch.distributed.get_rank() == 0: + logger.debug(f"Quantizing to FP8 blockwise: {k}") + if is_mxfp8_npu: + param_lp, param_scale = torch_npu.npu_dynamic_mx_quant( + v.to(dtype), + axis=-1, + dst_type=torch_npu.float8_e4m3fn, + ) + param_scale = param_scale.flatten(-2, -1) + else: + param_lp, param_scale = scaled_fp8_blockwise( + v.to(dtype), + weight_block_size=quant_config.weight_block_size, + ) + param_scale = param_scale.squeeze(-1) + + # Yield the quantized weight + yield (k, param_lp) + + # Yield the scale with appropriate naming based on vLLM version + if is_mxfp8_npu: + yield (k + "_scale", param_scale) + elif _use_scale_not_scale_inv and "expert" not in k: + yield (k + "_scale", param_scale) + else: + yield (k + "_scale_inv", param_scale) + + # Explicitly delete original tensor reference to help GC + del v, param_lp, param_scale + + +def prepare_quanted_weights_for_loading(model_runner): + model = model_runner.model + if not is_deepseek_v4_model(model): + return False + return prepare_deepseek_v4_weights_for_loading(model, _copy_param_subclass_attrs) + + +def process_quanted_weights_after_loading(model_runner, reload_state): + process_deepseek_v4_weights_after_loading(model_runner.model, reload_state) + + +def load_quanted_weights(weights, model_runner, is_drafter=False, prepare_model=True, process_model=True): + if is_drafter: + drafter = getattr(model_runner, "drafter", None) + model = drafter.model if drafter is not None and hasattr(drafter, "model") else None + assert model is not None, ( + "load_quanted_weights(is_drafter=True) requires model_runner.drafter.model " + "to be present and non-None for FP8 weight loading." + ) + else: + model = model_runner.model + quant_config = model_runner.vllm_config.quant_config + vllm_dtype = model_runner.vllm_config.model_config.dtype + + reload_state = None + if prepare_model: + reload_state = prepare_quanted_weights_for_loading(model_runner) + + is_mxfp8_npu = is_mxfp8_vllm_ascend(quant_config) + if is_mxfp8_npu: + # For MXFP8 on NPU, restore the original shapes expected by weight_loader. + restore_mxfp8_weights_for_loading(model) + + weights = list(weights) + cache_deepseek_v4_dense_fp8_scales(model, weights) + weights_quantized = quant_weights(weights, model, quant_config, dtype=vllm_dtype) + + # Monkey patch the param class to their subclass, as certain models + # will check the param type to call the proper weightloader + for name, param in model.named_parameters(): + if hasattr(param, "subclass_type"): + param.orig_type = param.__class__ + param.__class__ = param.subclass_type + # Finally load the weights into vllm + try: + loaded_params = model.load_weights(weights_quantized) + reload_deepseek_v4_dense_fp8_scales(model) + finally: + # Undo the type change above to the original type + for name, param in model.named_parameters(): + if hasattr(param, "orig_type"): + param.__class__ = param.orig_type + del param.orig_type + + if process_model: + if is_mxfp8_npu: + apply_mxfp8_transformation_after_loading(model) + process_quanted_weights_after_loading(model_runner, reload_state) + + return loaded_params + + +def replace_parameter_preserve_subclass(layer: torch.nn.Module, param_name: str, new_data: torch.Tensor | None): + if new_data is None: + setattr(layer, param_name, None) + return + + if isinstance(new_data, torch.nn.Parameter): + new_data = new_data.data + + old_param = getattr(layer, param_name, None) + param = torch.nn.Parameter(new_data, requires_grad=False) + _copy_param_subclass_attrs(param, old_param) + setattr(layer, param_name, param) + + +def _restore_layer_param_subclass_attrs(layer: torch.nn.Module, old_params: dict[str, torch.nn.Parameter]): + for name, old_param in old_params.items(): + new_param = getattr(layer, name, None) + if isinstance(new_param, torch.nn.Parameter): + _copy_param_subclass_attrs(new_param, old_param) + + +def _make_process_weights_after_loading_for_vllm20(original_fn): + def _patched_process_weights_after_loading(self, layer) -> None: + old_params = dict(layer.named_parameters(recurse=False)) + with patch( + "vllm.model_executor.layers.quantization.fp8.replace_parameter", replace_parameter_preserve_subclass + ): + original_fn(self, layer) + _restore_layer_param_subclass_attrs(layer, old_params) + + return _patched_process_weights_after_loading + + +def process_weights_after_loading_for_vllm10(self, layer) -> None: + """This function is used to process the weights after loading for a Linear layer, it is used for vllm v0.10 + + Compared to the original process_weights_after_loading in vllm, we just avoid creation of + new torch.nn.Parameter objects, because that removes the weight_loader attribute which we need for refit. + """ + logger.debug("Applying patch process_weights_after_loading") + try: + from vllm.model_executor.parameter import ( + BlockQuantScaleParameter, + ModelWeightParameter, + ) + except Exception: + print("error") + from torch.nn import Parameter + + def _create_param_from_subclass_attributes(custom_param): + param = Parameter(custom_param.data, requires_grad=False) + base_param_dir = dir(torch.nn.Parameter) + custom_param_dir = dir(custom_param) + # Find the attributes that are unique to the custom parameter + custom_attributes = [ + attr for attr in custom_param_dir if attr not in base_param_dir and not attr.startswith("__") + ] + # Set the custom attributes into the base parameter object + for attr in custom_attributes: + setattr(param, attr, getattr(custom_param, attr)) + + param.subclass_type = type(custom_param) + return param + + assert self.block_quant and self.quant_config.is_checkpoint_fp8_serialized + assert self.quant_config.activation_scheme == "dynamic" + weight = layer.weight.data + weight_scale_inv = layer.weight_scale_inv.data + weight = self._maybe_pad_weight(weight) + + layer.weight = _create_param_from_subclass_attributes( + ModelWeightParameter( + data=weight, + output_dim=0, + input_dim=1, + weight_loader=layer.weight.weight_loader, + ) + ) + layer.weight_scale_inv = _create_param_from_subclass_attributes( + BlockQuantScaleParameter( + data=weight_scale_inv, + output_dim=0, + input_dim=1, + weight_loader=layer.weight_scale_inv.weight_loader, + ) + ) + + +def process_weights_after_loading_for_vllm11(self, layer) -> None: + """This function is used to process the weights after loading for a Linear layer, it is used for vllm 0.11 + + Compared to the original process_weights_after_loading in vllm, we just avoid creation of + new torch.nn.Parameter objects, because that removes the weight_loader attribute which we need for refit. + """ + from torch.nn import Parameter + from vllm.model_executor.layers.quantization.utils.fp8_utils import ( + maybe_post_process_fp8_weight_block, + process_fp8_weight_block_strategy, + ) + from vllm.model_executor.parameter import ( + BlockQuantScaleParameter, + ModelWeightParameter, + ) + + assert self.block_quant and self.quant_config.is_checkpoint_fp8_serialized + assert self.quant_config.activation_scheme == "dynamic" + + def _create_param_from_subclass_attributes(custom_param): + param = Parameter(custom_param.data, requires_grad=False) + base_param_dir = dir(torch.nn.Parameter) + custom_param_dir = dir(custom_param) + # Find the attributes that are unique to the custom parameter + custom_attributes = [ + attr for attr in custom_param_dir if attr not in base_param_dir and not attr.startswith("__") + ] + # Set the custom attributes into the base parameter object + for attr in custom_attributes: + setattr(param, attr, getattr(custom_param, attr)) + + param.subclass_type = type(custom_param) + return param + + weight_scale = layer.weight_scale_inv if hasattr(layer, "weight_scale_inv") else layer.weight_scale + weight, weight_scale = process_fp8_weight_block_strategy(layer.weight, weight_scale) + + layer.weight = _create_param_from_subclass_attributes( + ModelWeightParameter( + data=weight.data, + output_dim=0, + input_dim=1, + weight_loader=layer.weight.weight_loader, + ) + ) + layer.weight_scale = _create_param_from_subclass_attributes( + BlockQuantScaleParameter( + data=weight_scale.data, + output_dim=0, + input_dim=1, + weight_loader=layer.weight_scale_inv.weight_loader, + ) + ) + + del layer.weight_scale_inv + + if _get_vllm_version() == version.parse("0.11.0"): + maybe_post_process_fp8_weight_block(layer, self.cutlass_block_fp8_supported) + else: + maybe_post_process_fp8_weight_block(layer) + + +def process_weights_after_loading_for_vllm14(self, layer) -> None: + """This function is used to process the weights after loading for a Linear layer, it is used for vllm v0.14-v0.19. + + Compared to the original process_weights_after_loading in vllm, we just avoid creation of + new torch.nn.Parameter objects, because that removes the weight_loader attribute which we need for refit. + """ + from torch.nn import Parameter + from vllm.model_executor.layers.quantization.utils.fp8_utils import ( + maybe_post_process_fp8_weight_block, + process_fp8_weight_block_strategy, + ) + from vllm.model_executor.parameter import ( + BlockQuantScaleParameter, + ModelWeightParameter, + ) + + assert self.block_quant and self.quant_config.is_checkpoint_fp8_serialized + assert self.quant_config.activation_scheme == "dynamic" + + def _create_param_from_subclass_attributes(custom_param): + param = Parameter(custom_param.data, requires_grad=False) + base_param_dir = dir(torch.nn.Parameter) + custom_param_dir = dir(custom_param) + # Find the attributes that are unique to the custom parameter + custom_attributes = [ + attr for attr in custom_param_dir if attr not in base_param_dir and not attr.startswith("__") + ] + # Set the custom attributes into the base parameter object + for attr in custom_attributes: + setattr(param, attr, getattr(custom_param, attr)) + + param.subclass_type = type(custom_param) + return param + + weight, weight_scale_inv = process_fp8_weight_block_strategy(layer.weight, layer.weight_scale_inv) + + layer.weight = _create_param_from_subclass_attributes( + ModelWeightParameter( + data=weight.data, + output_dim=0, + input_dim=1, + weight_loader=layer.weight.weight_loader, + ) + ) + layer.weight_scale_inv = _create_param_from_subclass_attributes( + BlockQuantScaleParameter( + data=weight_scale_inv.data, + output_dim=0, + input_dim=1, + weight_loader=layer.weight_scale_inv.weight_loader, + ) + ) + + # vLLM v0.17 removed the `else: register_parameter("input_scale", None)` from + # create_weights() for dynamic activation, but apply() still accesses layer.input_scale. + # Since block_quant always uses dynamic activation, ensure the attribute exists. + if not hasattr(layer, "input_scale"): + layer.input_scale = None + + maybe_post_process_fp8_weight_block(layer) + + +def process_weights_after_loading_moe_for_vllm10(self, layer) -> None: + """This function is used to process the weights after loading for a FusedMoE layer, it is used for vllm v0.10""" + from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import is_rocm_aiter_moe_enabled + from vllm.model_executor.layers.quantization.fp8 import _is_col_major, _swap_w13_to_w31 + from vllm.model_executor.layers.quantization.utils.fp8_utils import ( + get_col_major_tma_aligned_tensor, + requant_weight_ue8m0_inplace, + ) + from vllm.utils.deep_gemm import is_blackwell_deep_gemm_used + + self.rocm_aiter_moe_enabled = is_rocm_aiter_moe_enabled() + assert self.quant_config.activation_scheme == "dynamic" + if self.flashinfer_moe_enabled: + w13_weight = _swap_w13_to_w31(layer.w13_weight.data) + w13_weight_scale_inv = _swap_w13_to_w31(layer.w13_weight_scale_inv.data) + w2_weight = layer.w2_weight.data + w2_weight_scale_inv = layer.w2_weight_scale_inv.data + else: + w13_weight = layer.w13_weight.data + w13_weight_scale_inv = layer.w13_weight_scale_inv.data + w2_weight = layer.w2_weight + w2_weight_scale_inv = layer.w2_weight_scale_inv + + from torch.nn import Parameter + + def _create_param_from_subclass_attributes(custom_data, custom_weight): + param = Parameter(custom_data, requires_grad=False) + base_param_dir = dir(torch.nn.Parameter) + custom_weight_dir = dir(custom_weight) + # Find the attributes that are unique to the custom parameter + custom_attributes = [ + attr for attr in custom_weight_dir if attr not in base_param_dir and not attr.startswith("__") + ] + # Set the custom attributes into the base parameter object + for attr in custom_attributes: + setattr(param, attr, getattr(custom_weight, attr)) + + return param + + layer.w13_weight = _create_param_from_subclass_attributes(w13_weight, layer.w13_weight) + layer.w13_weight_scale_inv = _create_param_from_subclass_attributes( + w13_weight_scale_inv, layer.w13_weight_scale_inv + ) + layer.w2_weight = _create_param_from_subclass_attributes(w2_weight, layer.w2_weight) + layer.w2_weight_scale_inv = _create_param_from_subclass_attributes(w2_weight_scale_inv, layer.w2_weight_scale_inv) + + # DeepGemm scales need to be transposed and aligned. We try to do + # it ahead of time for performance reasons. + if self.allow_deep_gemm and not is_blackwell_deep_gemm_used(): + # Lazy import to avoid CUDA initialization problems. + if _is_col_major(layer.w13_weight_scale_inv): + layer.w13_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w13_weight_scale_inv).contiguous() + if _is_col_major(layer.w2_weight_scale_inv): + layer.w2_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w2_weight_scale_inv).contiguous() + + if is_blackwell_deep_gemm_used(): + assert layer.weight_block_size is not None + # Re-quantise the expert weights so their scales are UE8M0. + block_sz = tuple(layer.weight_block_size) + requant_weight_ue8m0_inplace( + layer.w13_weight.data, + layer.w13_weight_scale_inv.data, + block_sz, + ) + requant_weight_ue8m0_inplace( + layer.w2_weight.data, + layer.w2_weight_scale_inv.data, + block_sz, + ) + + if _is_col_major(layer.w13_weight_scale_inv): + layer.w13_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w13_weight_scale_inv).contiguous() + if _is_col_major(layer.w2_weight_scale_inv): + layer.w2_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w2_weight_scale_inv).contiguous() + + +def process_weights_after_loading_moe_for_vllm11(self, layer) -> None: + """This function is used to process the weights after loading for a FusedMoE layer, it is used for vllm 0.11""" + from vllm.model_executor.layers.quantization.utils.flashinfer_utils import ( + swap_w13_to_w31, + ) + from vllm.model_executor.layers.quantization.utils.fp8_utils import ( + expert_weight_is_col_major, + requant_weight_ue8m0_inplace, + ) + from vllm.utils.deep_gemm import ( + get_col_major_tma_aligned_tensor, + is_deep_gemm_e8m0_used, + ) + + try: + from vllm.model_executor.layers.fused_moe.rocm_aiter_fused_moe import is_rocm_aiter_moe_enabled + + self.rocm_aiter_moe_enabled = is_rocm_aiter_moe_enabled() + except ImportError: + from vllm._aiter_ops import rocm_aiter_ops + + self.rocm_aiter_moe_enabled = rocm_aiter_ops.is_fused_moe_enabled() + + assert self.block_quant and self.quant_config.is_checkpoint_fp8_serialized + assert self.quant_config.activation_scheme == "dynamic" + + if self.flashinfer_moe_backend is not None: + layer.w13_weight.data = swap_w13_to_w31(layer.w13_weight.data) + layer.w13_weight_scale_inv.data = swap_w13_to_w31(layer.w13_weight_scale_inv.data) + + if self.allow_deep_gemm and not is_deep_gemm_e8m0_used(): + if expert_weight_is_col_major(layer.w13_weight_scale_inv): + layer.w13_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w13_weight_scale_inv) + if expert_weight_is_col_major(layer.w2_weight_scale_inv): + layer.w2_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w2_weight_scale_inv) + + if is_deep_gemm_e8m0_used(): + assert layer.weight_block_size is not None + # Re-quantise the expert weights so their scales are UE8M0. + block_sz = tuple(layer.weight_block_size) + requant_weight_ue8m0_inplace( + layer.w13_weight.data, + layer.w13_weight_scale_inv.data, + block_sz, + ) + requant_weight_ue8m0_inplace( + layer.w2_weight.data, + layer.w2_weight_scale_inv.data, + block_sz, + ) + + # Ensure column-major TMA alignment expected by DeepGEMM. + if expert_weight_is_col_major(layer.w13_weight_scale_inv): + layer.w13_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w13_weight_scale_inv) + if expert_weight_is_col_major(layer.w2_weight_scale_inv): + layer.w2_weight_scale_inv = get_col_major_tma_aligned_tensor(layer.w2_weight_scale_inv) + + +def process_weights_after_loading_moe_for_vllm14(self, layer) -> None: + # removed the reentrancy guard here for refit + from vllm.model_executor.layers.fused_moe.oracle.fp8 import ( + convert_to_fp8_moe_kernel_format, + make_fp8_moe_kernel, + ) + + # Allow for accessing weights and scales in standard way. + w13 = layer.w13_weight + w2 = layer.w2_weight + w13_scale = getattr(layer, f"w13_{self.weight_scale_name}") + w2_scale = getattr(layer, f"w2_{self.weight_scale_name}") + w13_input_scale = layer.w13_input_scale + w2_input_scale = layer.w2_input_scale + + # Shuffle weights to runtime format and setup kernel. + w13, w2, w13_scale, w2_scale = convert_to_fp8_moe_kernel_format( + fp8_backend=self.fp8_backend, + layer=layer, + w13=w13, + w2=w2, + w13_scale=w13_scale, + w2_scale=w2_scale, + w13_input_scale=w13_input_scale, + w2_input_scale=w2_input_scale, + ) + from torch.nn import Parameter + + def _create_param_from_subclass_attributes(custom_data, custom_weight): + param = Parameter(custom_data, requires_grad=False) + base_param_dir = dir(torch.nn.Parameter) + custom_weight_dir = dir(custom_weight) + # Find the attributes that are unique to the custom parameter + custom_attributes = [ + attr for attr in custom_weight_dir if attr not in base_param_dir and not attr.startswith("__") + ] + # Set the custom attributes into the base parameter object + for attr in custom_attributes: + setattr(param, attr, getattr(custom_weight, attr)) + + return param + + # Replace parameters with updated versions. Note that this helper + # function ensures the replacement is compatible with RL weight reloads. + layer.w13_weight = _create_param_from_subclass_attributes(w13, layer.w13_weight) + layer.w2_weight = _create_param_from_subclass_attributes(w2, layer.w2_weight) + layer.w13_weight_scale_inv = _create_param_from_subclass_attributes(w13_scale, layer.w13_weight_scale_inv) + layer.w2_weight_scale_inv = _create_param_from_subclass_attributes(w2_scale, layer.w2_weight_scale_inv) + + self.moe_quant_config = self.get_fused_moe_quant_config(layer) + if self.moe_quant_config: + assert self.experts_cls is not None + + # Check for the new API by inspecting the function signature, which is more + # robust than version string comparison, especially for dev/pre-release versions. + sig = inspect.signature(make_fp8_moe_kernel) + if "routing_tables" in sig.parameters: + # vLLM >= 0.16+: routing_tables/shared_experts added, returns kernel directly + self.moe_kernel = make_fp8_moe_kernel( + moe_quant_config=self.moe_quant_config, + moe_config=self.moe, + fp8_backend=self.fp8_backend, + experts_cls=self.experts_cls, + routing_tables=layer._maybe_init_expert_routing_tables(), + shared_experts=layer.shared_experts, + ) + else: + # vLLM 0.14/0.15: routing_tables/shared_experts not supported, returns (kernel, use_inplace) + self.kernel, self.use_inplace = make_fp8_moe_kernel( + moe_quant_config=self.moe_quant_config, + moe_config=self.moe, + fp8_backend=self.fp8_backend, + experts_cls=self.experts_cls, + ) + + +def apply_vllm_fp8_patches(): + logger.info("Applying vllm fp8 patches for blockwise quantization") + vllm_ver = _get_vllm_version() + if fp8_state.vllm_patches: + logger.debug("vLLM FP8 patches already applied") + return + + func1_path = "vllm.model_executor.layers.quantization.fp8.Fp8LinearMethod.process_weights_after_loading" + func2_path = "vllm.model_executor.layers.quantization.fp8.Fp8MoEMethod.process_weights_after_loading" + + # vLLM 0.20 refactored FP8 post-load handling and removed + # maybe_post_process_fp8_weight_block. Keep its native transformation logic, + # but preserve parameter subclass metadata for RL weight reloads. + if vllm_ver >= version.parse("0.20.0"): + from vllm.model_executor.layers.quantization.fp8 import ( + Fp8LinearMethod, + Fp8MoEMethod, + ) + + patcher1 = patch( + func1_path, + _make_process_weights_after_loading_for_vllm20(Fp8LinearMethod.process_weights_after_loading), + ) + patcher2 = patch( + func2_path, + _make_process_weights_after_loading_for_vllm20(Fp8MoEMethod.process_weights_after_loading), + ) + patcher1.start() + patcher2.start() + fp8_state.vllm_patches.extend([patcher1, patcher2]) + return + + # Linear patch: v0.14+ keeps weight_scale_inv, v0.11-v0.12 renames to weight_scale + if vllm_ver >= version.parse("0.14.0"): + linear_patch_fn = process_weights_after_loading_for_vllm14 + elif vllm_ver >= version.parse("0.11.0"): + linear_patch_fn = process_weights_after_loading_for_vllm11 + else: + linear_patch_fn = process_weights_after_loading_for_vllm10 + patcher1 = patch(func1_path, linear_patch_fn) + patcher1.start() + + # MoE patch + if vllm_ver >= version.parse("0.14.0"): + moe_patch_fn = process_weights_after_loading_moe_for_vllm14 + elif vllm_ver >= version.parse("0.11.0"): + moe_patch_fn = process_weights_after_loading_moe_for_vllm11 + else: + moe_patch_fn = process_weights_after_loading_moe_for_vllm10 + patcher2 = patch(func2_path, moe_patch_fn) + patcher2.start() + fp8_state.vllm_patches.extend([patcher1, patcher2]) diff --git a/verl_0720_main/verl/verl/version/version b/verl_0720_main/verl/verl/version/version new file mode 100644 index 0000000000000000000000000000000000000000..7f9ccdc8db19da5e135e66f32ad96fe8dbc76cac --- /dev/null +++ b/verl_0720_main/verl/verl/version/version @@ -0,0 +1 @@ +0.9.0.dev diff --git a/verl_0720_main/verl/verl/workers/__init__.py b/verl_0720_main/verl/verl/workers/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1ce90c5eb352d85c59105c0dc85b5f1dd576f095 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/workers/config/__init__.py b/verl_0720_main/verl/verl/workers/config/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..b3635a14cf0af7263a65b7a5eaac5f95c61bf9fc --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/__init__.py @@ -0,0 +1,38 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from . import actor, checkpoint, critic, disaggregation, engine, model, optimizer, reward, rollout +from .actor import * # noqa: F401 +from .checkpoint import * # noqa: F401 +from .critic import * # noqa: F401 +from .disaggregation import * # noqa: F401 +from .distillation import * # noqa: F401 +from .engine import * # noqa: F401 +from .model import * # noqa: F401 +from .optimizer import * # noqa: F401 +from .reward import * # noqa: F401 +from .rollout import * # noqa: F401 + +__all__ = ( + actor.__all__ + + critic.__all__ + + reward.__all__ + + engine.__all__ + + optimizer.__all__ + + rollout.__all__ + + model.__all__ + + distillation.__all__ + + disaggregation.__all__ + + checkpoint.__all__ +) diff --git a/verl_0720_main/verl/verl/workers/config/actor.py b/verl_0720_main/verl/verl/workers/config/actor.py new file mode 100644 index 0000000000000000000000000000000000000000..b4111f9be88eabfa07f1a3c3fada1400782d2006 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/actor.py @@ -0,0 +1,420 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dataclasses import dataclass, field +from typing import Any, Optional + +from omegaconf import MISSING + +from verl.base_config import BaseConfig +from verl.trainer.config import CheckpointConfig, RolloutCorrectionConfig +from verl.utils.profiler.config import ProfilerConfig +from verl.utils.qat import QATConfig + +from .checkpoint import McoreCheckpointConfig, MindSpeedCheckpointConfig +from .engine import ( + FSDPEngineConfig, + McoreEngineConfig, + MindSpeedEngineConfig, + TorchtitanEngineConfig, + VeOmniEngineConfig, +) +from .model import HFModelConfig +from .optimizer import OptimizerConfig + +__all__ = [ + "PolicyLossConfig", + "RouterReplayConfig", + "ActorConfig", + "FSDPActorConfig", + "McoreActorConfig", + "VeOmniActorConfig", + "QATConfig", + "TorchTitanActorConfig", + "MindSpeedActorConfig", +] + + +@dataclass +class RouterReplayConfig(BaseConfig): + """Configuration for router replay in MoE models. + + This configuration controls the routing behavior for Mixture of Experts (MoE) models, + allowing for deterministic training through route recording and replay. + + Args: + mode (str): Router replay mode. Options: 'disabled', 'R2', 'R3'. + - 'disabled': No router replay functionality + - 'R2': Use Router Replay routing strategy + - 'R3': Use Rollout Router Replay routing strategy + record_file (Optional[str]): File path to save recorded routing decisions. + Required when mode is 'record', 'R2', or 'R3'. + replay_file (Optional[str]): File path to load recorded routing decisions for replay. + Required when mode is 'replay'. + """ + + mode: str = "disabled" + record_file: Optional[str] = None + replay_file: Optional[str] = None + + def __post_init__(self): + """Validate router replay configuration.""" + valid_modes = ["disabled", "R2", "R3"] + if self.mode not in valid_modes: + raise ValueError(f"Invalid router_replay mode: {self.mode}. Must be one of {valid_modes}") + + +@dataclass +class PolicyLossConfig(BaseConfig): + """Configuration for policy loss computation. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + loss_mode (str): Loss function mode. Options: 'vanilla', 'clip-cov', 'kl-cov', 'gpg'. + clip_cov_ratio (float): Ratio of tokens to be clipped for clip-cov loss. + clip_cov_lb (float): Lower bound for clip-cov loss. + clip_cov_ub (float): Upper bound for clip-cov loss. + kl_cov_ratio (float): Ratio of tokens to be applied KL penalty for kl-cov loss. + ppo_kl_coef (float): KL divergence penalty coefficient. + rollout_correction (RolloutCorrectionConfig): Configuration for rollout correction. + """ + + loss_mode: str = "vanilla" + clip_cov_ratio: float = 0.0002 + clip_cov_lb: float = 1.0 + clip_cov_ub: float = 5.0 + kl_cov_ratio: float = 0.0002 + ppo_kl_coef: float = 0.1 + rollout_correction: RolloutCorrectionConfig = field(default_factory=RolloutCorrectionConfig) + + +@dataclass +class ActorConfig(BaseConfig): + """Configuration for actor model training. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy. Must be specified. + ppo_mini_batch_size (int): Mini-batch size for PPO training. + ppo_micro_batch_size (Optional[int]): Micro-batch size for PPO training. + If None, uses ppo_micro_batch_size_per_gpu. + ppo_micro_batch_size_per_gpu (Optional[int]): Micro-batch size per GPU for PPO training. + use_dynamic_bsz (bool): Whether to use dynamic batch sizing. + ppo_max_token_len_per_gpu (int): Maximum token length per GPU for PPO training. + clip_ratio (float): PPO clipping ratio for policy loss. + clip_ratio_low (float): Lower bound for PPO clipping ratio. + clip_ratio_high (float): Upper bound for PPO clipping ratio. + policy_loss (PolicyLossConfig): Configuration for policy loss computation. + clip_ratio_c (float): Clipping ratio for critic loss. + loss_agg_mode (str): Loss aggregation mode. Options: 'token-mean', 'sample-mean'. + loss_scale_factor (Optional[int]): Scale factor for 'seq-mean-token-sum-norm' loss aggregation mode. + If None, uses response_length. Set to a constant to ensure consistent normalization. + entropy_coeff (float): Entropy coefficient for regularization. + tau_pos (float): Positive tau for SAPO smoothing (>= 1.0 keeps rewards stable). + tau_neg (float): Negative tau for SAPO smoothing (> tau_pos for asymmetry). + use_kl_loss (bool): Whether to use KL divergence loss. + use_torch_compile (bool): Whether to use torch.compile for optimization. + kl_loss_coef (float): KL divergence loss coefficient. + kl_loss_type (str): Type of KL loss to use. + ppo_epochs (int): Number of PPO epochs per training step. + shuffle (bool): Whether to shuffle data during training. + checkpoint (CheckpointConfig): Configuration for checkpointing. + optim (OptimizerConfig): Configuration for optimizer. + use_fused_kernels (bool): Whether to use custom fused kernels (e.g., FlashAttention, fused MLP). + data_loader_seed (int): Seed for data loader. If None, uses global seed. + router_replay (RouterReplayConfig): Configuration for router replay in MoE models. + """ + + _mutable_fields = BaseConfig._mutable_fields | { + "ppo_mini_batch_size", + "ppo_micro_batch_size", + "ppo_micro_batch_size_per_gpu", + "ppo_infer_micro_batch_size_per_gpu", + "engine", + "model_config", + } + + strategy: str = MISSING + ppo_mini_batch_size: int = 256 + ppo_micro_batch_size: Optional[int] = None # deprecate + ppo_micro_batch_size_per_gpu: Optional[int] = None + ppo_infer_micro_batch_size_per_gpu: Optional[int] = None + use_dynamic_bsz: bool = False + ppo_max_token_len_per_gpu: int = 16384 + ppo_infer_max_token_len_per_gpu: int = 16384 + clip_ratio: float = 0.2 + clip_ratio_low: float = 0.2 + clip_ratio_high: float = 0.2 + freeze_vision_tower: bool = False + policy_loss: PolicyLossConfig = field(default_factory=PolicyLossConfig) + clip_ratio_c: float = 3.0 + loss_agg_mode: str = "token-mean" + loss_scale_factor: Optional[int] = None + entropy_coeff: float = 0 + tau_pos: float = 1.0 + tau_neg: float = 1.05 + calculate_entropy: bool = False + calculate_sum_pi_squared: bool = False + use_kl_loss: bool = False + # Whether to enable PrefixGrouper-based shared-prefix forward + use_prefix_grouper: bool = False + use_torch_compile: bool = True + kl_loss_coef: float = 0.001 + kl_loss_type: str = "low_var_kl" + ppo_epochs: int = 1 + shuffle: bool = False + data_loader_seed: int = 42 + checkpoint: CheckpointConfig = field(default_factory=CheckpointConfig) + optim: OptimizerConfig = field(default_factory=OptimizerConfig) + use_fused_kernels: bool = False + profiler: ProfilerConfig = field(default_factory=ProfilerConfig) + engine: BaseConfig = field(default_factory=BaseConfig) + rollout_n: int = MISSING # must be override by sampling config + model_config: HFModelConfig = field(default_factory=BaseConfig) + router_replay: RouterReplayConfig = field(default_factory=RouterReplayConfig) + + # Store global batch info for loss aggregation: + # dp_size: data parallel size + # batch_num_tokens: number of valid tokens in global batch + # global_batch_size: global batch size + global_batch_info: dict = field(default_factory=dict) + qat: QATConfig = field(default_factory=QATConfig) + + def __post_init__(self): + """Validate actor configuration parameters.""" + assert self.strategy != MISSING + assert self.rollout_n != MISSING + if not self.use_dynamic_bsz: + if self.ppo_micro_batch_size is not None and self.ppo_micro_batch_size_per_gpu is not None: + raise ValueError( + "[actor] You have set both 'actor.ppo_micro_batch_size' AND 'actor.ppo_micro_batch_size_per_gpu'. " + "Please remove 'actor.ppo_micro_batch_size' because only '*_ppo_micro_batch_size_per_gpu' is " + "supported (the former is deprecated)." + ) + else: + assert not (self.ppo_micro_batch_size is None and self.ppo_micro_batch_size_per_gpu is None), ( + "[actor] Please set at least one of 'actor.ppo_micro_batch_size' or " + "'actor.ppo_micro_batch_size_per_gpu' if use_dynamic_bsz is not enabled." + ) + + valid_loss_agg_modes = [ + "token-mean", + "seq-mean-token-sum", + "seq-mean-token-mean", + "seq-mean-token-sum-norm", + ] + if self.loss_agg_mode not in valid_loss_agg_modes: + raise ValueError(f"Invalid loss_agg_mode: {self.loss_agg_mode}") + + def validate(self, n_gpus: int, train_batch_size: int, model_config: dict = None): + """Validate actor configuration with runtime parameters.""" + if not self.use_dynamic_bsz: + if train_batch_size < self.ppo_mini_batch_size: + raise ValueError( + f"train_batch_size ({train_batch_size}) must be >= " + f"actor.ppo_mini_batch_size ({self.ppo_mini_batch_size})" + ) + + sp_size = getattr(self, "ulysses_sequence_parallel_size", 1) + if self.ppo_micro_batch_size is not None: + if self.ppo_mini_batch_size % self.ppo_micro_batch_size != 0: + raise ValueError( + f"ppo_mini_batch_size ({self.ppo_mini_batch_size}) must be divisible by " + f"ppo_micro_batch_size ({self.ppo_micro_batch_size})" + ) + if self.ppo_micro_batch_size * sp_size < n_gpus: + raise ValueError( + f"ppo_micro_batch_size ({self.ppo_micro_batch_size}) * " + f"ulysses_sequence_parallel_size ({sp_size}) must be >= n_gpus ({n_gpus})" + ) + + @staticmethod + def _check_mutually_exclusive(mbs, mbs_per_gpu, name: str): + """Validate mutually exclusive micro batch size configuration options.""" + param = "ppo_micro_batch_size" + param_per_gpu = f"{param}_per_gpu" + + if mbs is None and mbs_per_gpu is None: + raise ValueError(f"[{name}] Please set at least one of '{name}.{param}' or '{name}.{param_per_gpu}'.") + + if mbs is not None and mbs_per_gpu is not None: + raise ValueError( + f"[{name}] You have set both '{name}.{param}' AND '{name}.{param_per_gpu}'. Please remove " + f"'{name}.{param}' because only '*_{param_per_gpu}' is supported (the former is deprecated)." + ) + + +@dataclass +class McoreActorConfig(ActorConfig): + """Configuration for Megatron actor models. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy set to 'megatron' for Megatron parallelism. + megatron (dict[str, Any]): Configuration for Megatron parallelism settings. + profile (dict[str, Any]): Configuration for profiling settings. + checkpoint (McoreCheckpointConfig): Megatron-specific checkpoint config + that adds ``mbridge_config`` on top of the base checkpoint fields. + """ + + strategy: str = "megatron" + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + megatron: McoreEngineConfig = field(default_factory=McoreEngineConfig) + profile: dict[str, Any] = field(default_factory=dict) + use_rollout_log_probs: bool = False + checkpoint: McoreCheckpointConfig = field(default_factory=McoreCheckpointConfig) + + def __post_init__(self): + """Validate FSDP actor configuration parameters.""" + super().__post_init__() + self.engine = self.megatron + + +@dataclass +class FSDPActorConfig(ActorConfig): + """Configuration for FSDP actor models. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy set to 'fsdp' for Fully Sharded Data Parallel. + grad_clip (float): Gradient clipping threshold. + ulysses_sequence_parallel_size (int): [DEPRECATED] Ulysses sequence parallel size for long sequences. + entropy_from_logits_with_chunking (bool): Whether to compute entropy from logits + with chunking for memory efficiency. + entropy_checkpointing (bool): Whether to use gradient checkpointing for entropy computation. + fsdp_config (dict[str, Any]): Configuration for FSDP settings. + use_remove_padding (bool): Whether to remove padding tokens in inputs during training + """ + + strategy: str = "fsdp" + grad_clip: float = 1.0 + ulysses_sequence_parallel_size: int = 1 + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + entropy_checkpointing: bool = False + fsdp_config: FSDPEngineConfig = field(default_factory=FSDPEngineConfig) + use_remove_padding: bool = False + use_rollout_log_probs: bool = False + + def __post_init__(self): + """Validate FSDP actor configuration parameters.""" + super().__post_init__() + self.engine = self.fsdp_config + # Sync strategy to engine config so engine_workers can pick the right FSDP version. + # EngineConfig.strategy defaults to None, so without this, engine_workers.py always + # falls back to FSDP1 even when actor.strategy="fsdp2". + object.__setattr__(self.engine, "strategy", self.strategy) + + # backward compatibility + if self.ulysses_sequence_parallel_size > 1: + self.fsdp_config.ulysses_sequence_parallel_size = self.ulysses_sequence_parallel_size + + def validate(self, n_gpus: int, train_batch_size: int, model_config: dict = None): + """Validate FSDP actor configuration with runtime parameters.""" + super().validate(n_gpus, train_batch_size, model_config) + if ( + self.ulysses_sequence_parallel_size > 1 + and model_config + and not model_config.get("use_remove_padding", False) + ): + raise ValueError( + "When using sequence parallelism for actor/ref policy, you must enable `use_remove_padding`." + ) + + +@dataclass +class VeOmniActorConfig(ActorConfig): + """Configuration for VeOmni actor models. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy set to 'veomni' for VeOmni parallelism. + veomni (dict[str, Any]): Configuration for VeOmni settings. + use_remove_padding (bool): Whether to remove padding tokens in inputs during training + """ + + strategy: str = "veomni" + veomni: VeOmniEngineConfig = field(default_factory=VeOmniEngineConfig) + use_remove_padding: bool = False + use_rollout_log_probs: bool = False + + def __post_init__(self): + """Validate VeOmni actor configuration parameters.""" + super().__post_init__() + self.engine = self.veomni + if self.veomni.router_replay.mode != "disabled" and not self.use_remove_padding: + raise RuntimeError( + "router_replay requires use_remove_padding=True. In VeOmni engine, " + "the non-remove-padding path also disables Ulysses SP slicing and " + "the fused-kernel log_probs path, and is not a tested production " + "configuration for MoE routing replay. Set " + "actor.use_remove_padding=True or router_replay.mode='disabled'." + ) + + +@dataclass +class TorchTitanActorConfig(ActorConfig): + """Configuration for TorchTitan actor models. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy set to 'torchtitan' for TorchTitan parallelism. + torchtitan (TorchtitanEngineConfig): Configuration for TorchTitan engine settings. + use_remove_padding (bool): Whether to remove padding tokens in inputs during training + use_rollout_log_probs (bool): Whether to use log probabilities from rollout engine + """ + + strategy: str = "torchtitan" + torchtitan: TorchtitanEngineConfig = field(default_factory=TorchtitanEngineConfig) + use_remove_padding: bool = False + use_rollout_log_probs: bool = False + + def __post_init__(self): + """Validate TorchTitan actor configuration parameters.""" + super().__post_init__() + self.engine = self.torchtitan + + +@dataclass +class MindSpeedActorConfig(ActorConfig): + """Configuration for mindspeed actor models. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Training strategy set to 'mindspeed' for mindspeed parallelism. + mindspeed (dict[str, Any]): Configuration for mindspeed parallelism settings. + profile (dict[str, Any]): Configuration for profiling settings. + use_rollout_log_probs (bool): Whether to use log probabilities from rollout engine. + checkpoint (MindSpeedCheckpointConfig): MindSpeed-specific checkpoint config + (inherits ``mbridge_config`` from :class:`McoreCheckpointConfig`). + """ + + strategy: str = "mindspeed" + mindspeed: MindSpeedEngineConfig = field(default_factory=MindSpeedEngineConfig) + profile: dict[str, Any] = field(default_factory=dict) + use_rollout_log_probs: bool = False + checkpoint: MindSpeedCheckpointConfig = field(default_factory=MindSpeedCheckpointConfig) + + def __post_init__(self): + """Validate MindSpeed actor configuration parameters.""" + super().__post_init__() + self.engine = self.mindspeed diff --git a/verl_0720_main/verl/verl/workers/config/checkpoint.py b/verl_0720_main/verl/verl/workers/config/checkpoint.py new file mode 100644 index 0000000000000000000000000000000000000000..5262a0ddd05b2affa84db97c43d9bb91d0fddb85 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/checkpoint.py @@ -0,0 +1,60 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Backend-specific extensions of :class:`verl.trainer.config.CheckpointConfig`. + +The base :class:`CheckpointConfig` lives in ``verl/trainer/config/config.py`` and +carries only fields that every backend understands (``save_contents``, +``load_contents``, ``async_save``). Anything that is meaningful only to one +training backend (e.g. mbridge options for Megatron) goes into a subclass here, +mirroring how ``ActorConfig`` / ``McoreActorConfig`` are split between +``verl/trainer/config`` and ``verl/workers/config``. +""" + +from dataclasses import dataclass, field +from typing import Any + +from verl.trainer.config import CheckpointConfig + +__all__ = ["McoreCheckpointConfig", "MindSpeedCheckpointConfig"] + + +@dataclass +class McoreCheckpointConfig(CheckpointConfig): + """Checkpoint config for the Megatron-Core backend. + + Adds the mbridge-specific knobs consumed by + :class:`verl.utils.checkpoint.megatron_checkpoint_manager.MegatronCheckpointManager` + when it forwards kwargs to ``bridge.save_weights()``. + + Args: + mbridge_config (dict[str, Any]): Extra kwargs forwarded to + ``bridge.save_weights``. Typical keys include + ``distributed_filesystem`` and ``memory_efficient`` for the + ``vanilla_mbridge`` path. Keys that are not accepted by the active + bridge's ``save_weights`` signature are silently ignored. + """ + + mbridge_config: dict[str, Any] = field(default_factory=dict) + + +@dataclass +class MindSpeedCheckpointConfig(McoreCheckpointConfig): + """Checkpoint config for the MindSpeed backend. + + MindSpeed reuses the Megatron checkpoint manager and therefore inherits the + mbridge knob from :class:`McoreCheckpointConfig`. A dedicated class is kept + so MindSpeed-only fields (should any appear in the future) have a natural + home and so the ``_target_`` in MindSpeed yamls mirrors the backend name. + """ diff --git a/verl_0720_main/verl/verl/workers/config/critic.py b/verl_0720_main/verl/verl/workers/config/critic.py new file mode 100644 index 0000000000000000000000000000000000000000..5180b083ebc690f39a773cea00488dac8f75a91c --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/critic.py @@ -0,0 +1,340 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from dataclasses import dataclass, field +from typing import Optional + +from omegaconf import MISSING + +from verl.base_config import BaseConfig +from verl.trainer.config import BaseModelConfig, CheckpointConfig +from verl.utils.profiler import ProfilerConfig + +from .checkpoint import McoreCheckpointConfig, MindSpeedCheckpointConfig +from .engine import ( + FSDPEngineConfig, + McoreEngineConfig, + MindSpeedEngineConfig, + TorchtitanEngineConfig, + VeOmniEngineConfig, +) +from .model import HFModelConfig +from .optimizer import OptimizerConfig + +__all__ = [ + "CriticConfig", + "FSDPCriticConfig", + "McoreCriticConfig", + "TorchTitanCriticConfig", + "FSDPCriticModelCfg", + "MindSpeedCriticConfig", + "VeOmniCriticConfig", +] + + +@dataclass +class CriticConfig(BaseConfig): + """Configuration for critic model training. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + strategy (str): Strategy used for critic model training (fsdp, fsdp2, megatron). + ppo_micro_batch_size_per_gpu (int): Local per-GPU micro batch size. + rollout_n (int): Number of rollouts per update (mirrors actor rollout_n). + optim (Dict[str, Any]): Optimizer configuration including lr, weight_decay, etc. + model (Dict[str, Any]): Model configuration including path, tokenizer_path, etc. + ppo_mini_batch_size (int): PPO mini-batch size per update. + ppo_micro_batch_size (Optional[int]): Global micro batch size (deprecated). + use_dynamic_bsz (bool): Whether to automatically adjust batch size at runtime. + ppo_max_token_len_per_gpu (int): Max tokens per GPU in one PPO batch. + forward_max_token_len_per_gpu (int): Max token length per GPU in forward pass. + ppo_epochs (int): Number of PPO epochs per batch. + shuffle (bool): Shuffle training data across PPO epochs. + cliprange_value (float): PPO value function clipping range. + loss_agg_mode (str): Loss aggregation mode. + loss_scale_factor (Optional[int]): Scale factor for 'seq-mean-token-sum-norm' loss aggregation mode. + checkpoint (Dict[str, Any]): Checkpoint configuration. + profiler (Dict[str, Any]): Profiler configuration. + enable (Optional[bool]): Whether to enable the critic. + """ + + _mutable_fields = BaseConfig._mutable_fields | { + "ppo_micro_batch_size_per_gpu", + "ppo_mini_batch_size", + "ppo_micro_batch_size", + "engine", + "model", + } + + strategy: str = MISSING + ppo_micro_batch_size_per_gpu: Optional[int] = None + enable: Optional[bool] = None + rollout_n: int = 1 + ppo_mini_batch_size: int = 1 + use_dynamic_bsz: bool = False + ppo_max_token_len_per_gpu: int = 32768 + # deprecate this + forward_max_token_len_per_gpu: int = 32768 + ppo_infer_micro_batch_size_per_gpu: Optional[int] = None + ppo_infer_max_token_len_per_gpu: int = 32768 + ppo_epochs: int = 1 + data_loader_seed: int = 42 + shuffle: bool = True + cliprange_value: float = 0.5 + loss_agg_mode: str = "token-mean" + loss_scale_factor: Optional[int] = None + ppo_micro_batch_size: Optional[int] = None + engine: BaseConfig = field(default_factory=BaseConfig) + optim: OptimizerConfig = field(default_factory=OptimizerConfig) + model: HFModelConfig = None + checkpoint: CheckpointConfig = field(default_factory=CheckpointConfig) + profiler: ProfilerConfig = field(default_factory=ProfilerConfig) + + def __post_init__(self): + """Validate critic configuration parameters.""" + assert self.strategy != MISSING + + if not self.use_dynamic_bsz: + self._check_mutually_exclusive(self.ppo_micro_batch_size, self.ppo_micro_batch_size_per_gpu, "critic") + + if self.ppo_micro_batch_size is not None: + if self.ppo_mini_batch_size % self.ppo_micro_batch_size != 0: + raise ValueError( + f"[critic] ppo_mini_batch_size ({self.ppo_mini_batch_size}) must be divisible by " + f"ppo_micro_batch_size ({self.ppo_micro_batch_size})" + ) + + def validate(self, n_gpus: int, train_batch_size: int): + """Validate critic configuration with runtime parameters. + + Args: + n_gpus: Total number of GPUs available + train_batch_size: Training batch size from data config + """ + if not self.use_dynamic_bsz: + if train_batch_size < self.ppo_mini_batch_size: + raise ValueError( + f"train_batch_size ({train_batch_size}) must be >= " + f"critic.ppo_mini_batch_size ({self.ppo_mini_batch_size})" + ) + + @staticmethod + def _check_mutually_exclusive(mbs, mbs_per_gpu, name: str): + """Validate mutually exclusive micro batch size configuration options. + + Ensures that users don't set both deprecated micro_batch_size and + the new micro_batch_size_per_gpu parameters simultaneously. + + Args: + mbs: Deprecated micro batch size parameter value. + mbs_per_gpu: New micro batch size per GPU parameter value. + name (str): Configuration section name for error messages. + + Raises: + ValueError: If both parameters are set or neither is set. + """ + param = "micro_batch_size" + param_per_gpu = f"{param}_per_gpu" + + if mbs is None and mbs_per_gpu is None: + raise ValueError(f"[{name}] Please set at least one of '{name}.{param}' or '{name}.{param_per_gpu}'.") + + if mbs is not None and mbs_per_gpu is not None: + raise ValueError( + f"[{name}] You have set both '{name}.{param}' AND '{name}.{param_per_gpu}'. Please remove " + f"'{name}.{param}' because only '*_{param_per_gpu}' is supported (the former is deprecated)." + ) + + +@dataclass +class McoreCriticConfig(CriticConfig): + """Configuration for Megatron-based critic model training. + + The inheritance from CriticConfig provides all base critic configuration plus Megatron-specific settings. + + Args: + nccl_timeout (int): NCCL timeout in seconds for distributed operations. + megatron (Dict[str, Any]): Megatron-specific parallelism settings. + checkpoint (McoreCheckpointConfig): Megatron-specific checkpoint config + that adds ``mbridge_config`` on top of the base checkpoint fields. + """ + + strategy: str = "megatron" + nccl_timeout: int = 600 + megatron: McoreEngineConfig = field(default_factory=McoreEngineConfig) + checkpoint: McoreCheckpointConfig = field(default_factory=McoreCheckpointConfig) + + def validate(self, n_gpus: int, train_batch_size: int): + """Validate Megatron critic configuration with runtime parameters.""" + super().validate(n_gpus, train_batch_size) + + def __post_init__(self): + """Validate Megatron critic configuration parameters.""" + super().__post_init__() + self.engine = self.megatron + + +@dataclass +class FSDPCriticConfig(CriticConfig): + """Configuration for FSDP-based critic model training. + + The inheritance from CriticConfig provides all base critic configuration plus FSDP-specific settings. + + Args: + forward_micro_batch_size (int): Forward-only batch size during inference (global). + forward_micro_batch_size_per_gpu (int): Forward-only batch size during inference (per GPU). + ulysses_sequence_parallel_size (int): [DEPRECATED] Ulysses sequence parallel size for long sequences. + grad_clip (float): Gradient clipping for critic updates. + """ + + _mutable_fields = CriticConfig._mutable_fields | { + "forward_micro_batch_size", + "forward_micro_batch_size_per_gpu", + } + + strategy: str = "fsdp" + fsdp: FSDPEngineConfig = field(default_factory=FSDPEngineConfig) + forward_micro_batch_size: int = 1 + forward_micro_batch_size_per_gpu: int = 1 + ulysses_sequence_parallel_size: int = 1 + grad_clip: float = 1.0 + + def __post_init__(self): + """Validate FSDP critic configuration parameters.""" + super().__post_init__() + self.engine = self.fsdp + # Sync strategy to engine config so engine_workers can pick the right FSDP version. + # EngineConfig.strategy defaults to None, so without this, engine_workers.py always + # falls back to FSDP1 even when critic.strategy="fsdp2". + object.__setattr__(self.engine, "strategy", self.strategy) + + def validate(self, n_gpus: int, train_batch_size: int): + """Validate FSDP critic configuration with runtime parameters.""" + super().validate(n_gpus, train_batch_size) + + if not self.use_dynamic_bsz: + sp_size = self.ulysses_sequence_parallel_size + if self.ppo_micro_batch_size is not None: + if self.ppo_micro_batch_size * sp_size < n_gpus: + raise ValueError( + f"critic.ppo_micro_batch_size ({self.ppo_micro_batch_size}) * " + f"ulysses_sequence_parallel_size ({sp_size}) must be >= n_gpus ({n_gpus})" + ) + + +@dataclass +class TorchTitanCriticConfig(CriticConfig): + """Configuration for TorchTitan-based critic model training. + + The inheritance from CriticConfig provides all base critic configuration plus TorchTitan-specific settings. + + Args: + strategy (str): Training strategy set to 'torchtitan' for TorchTitan parallelism. + torchtitan (TorchtitanEngineConfig): Configuration for TorchTitan engine settings. + """ + + strategy: str = "torchtitan" + torchtitan: TorchtitanEngineConfig = field(default_factory=TorchtitanEngineConfig) + + def __post_init__(self): + """Validate TorchTitan critic configuration parameters.""" + super().__post_init__() + self.engine = self.torchtitan + + +@dataclass +class FSDPCriticModelCfg(BaseModelConfig): + """FSDP-enabled critic model configuration. + Inherits base critic settings and adds distributed-memory and LoRA options. + + Args: + use_shm (bool): Whether to use shared memory for loading the model. + enable_activation_offload (bool): Offload activations to CPU to reduce GPU memory usage. + use_remove_padding (bool): Use remove-padding optimization (saves compute). + enable_gradient_checkpointing (bool): Enable gradient checkpointing for memory efficiency. + fsdp_config (FSDPEngineConfig): FSDP-specific configuration block. + lora_rank (int): Set to positive value to enable LoRA (e.g., 32). + lora_alpha (int): LoRA scaling factor. + target_modules (Union[str, List[str]]): LoRA target modules: "all-linear" or list of layer names. + """ + + use_shm: bool = False + enable_activation_offload: bool = False + use_remove_padding: bool = False + enable_gradient_checkpointing: bool = True + fsdp_config: FSDPEngineConfig = field(default_factory=FSDPEngineConfig) + lora_rank: int = 0 + lora_alpha: int = 16 + target_modules: str | list[str] = "all-linear" + # TiledMLP configuration for memory-efficient MLP computation + tiled_mlp: dict = field(default_factory=lambda: {"enabled": False, "num_shards": 4}) + + +@dataclass +class MindSpeedCriticConfig(CriticConfig): + """Configuration for mindspeed-based critic model training. + + The inheritance from CriticConfig provides all base critic configuration plus mindspeed-specific settings. + + Args: + nccl_timeout (int): NCCL timeout in seconds for distributed operations. + mindspeed (Dict[str, Any]): mindspeed-specific parallelism settings. + checkpoint (MindSpeedCheckpointConfig): MindSpeed-specific checkpoint config + (inherits ``mbridge_config`` from :class:`McoreCheckpointConfig`). + """ + + strategy: str = "mindspeed" + nccl_timeout: int = 600 + mindspeed: MindSpeedEngineConfig = field(default_factory=MindSpeedEngineConfig) + checkpoint: MindSpeedCheckpointConfig = field(default_factory=MindSpeedCheckpointConfig) + + def validate(self, n_gpus: int, train_batch_size: int): + """Validate mindspeed critic configuration with runtime parameters.""" + super().validate(n_gpus, train_batch_size) + + +@dataclass +class VeOmniCriticConfig(CriticConfig): + """Configuration for VeOmni-based critic model training. + + Uses VeOmni's FSDP2 + sequence parallelism engine for the value model, + mirroring VeOmniActorConfig but for the critic role. + + Args: + strategy (str): Training strategy set to 'veomni'. + veomni (VeOmniEngineConfig): VeOmni engine configuration. + """ + + strategy: str = "veomni" + veomni: VeOmniEngineConfig = field(default_factory=VeOmniEngineConfig) + grad_clip: float = 1.0 + + def __post_init__(self): + """Set engine to VeOmni config.""" + super().__post_init__() + self.engine = self.veomni + + def validate(self, n_gpus: int, train_batch_size: int): + """Validate VeOmni critic configuration with runtime parameters.""" + super().validate(n_gpus, train_batch_size) + + if not self.use_dynamic_bsz: + sp_size = self.veomni.ulysses_parallel_size + if self.ppo_micro_batch_size is not None: + if self.ppo_micro_batch_size * sp_size < n_gpus: + raise ValueError( + f"critic.ppo_micro_batch_size ({self.ppo_micro_batch_size}) * " + f"veomni.ulysses_parallel_size ({sp_size}) must be >= n_gpus ({n_gpus})" + ) diff --git a/verl_0720_main/verl/verl/workers/config/disaggregation.py b/verl_0720_main/verl/verl/workers/config/disaggregation.py new file mode 100644 index 0000000000000000000000000000000000000000..a98e954d0996789df11f2044128f6dc468a9286b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/disaggregation.py @@ -0,0 +1,60 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from dataclasses import dataclass +from typing import Optional + +from verl.base_config import BaseConfig + +__all__ = ["DisaggregationConfig"] + +_ALLOWED_BACKENDS = ("nixl", "mooncake", "ascend", "mori", "fake") +_ALLOWED_MOONCAKE_PROTOCOLS = ("nvlink", "local", "rdma", "tcp") + + +@dataclass +class DisaggregationConfig(BaseConfig): + """Prefill-Decode disaggregation knobs.""" + + enabled: bool = False + prefill_replicas: int = 1 + decode_replicas: int = 1 + decode_tensor_model_parallel_size: Optional[int] = None + transfer_backend: str = "nixl" + bootstrap_port: Optional[int] = None + ib_device: Optional[str] = None + mooncake_protocol: str = "nvlink" + + def __post_init__(self) -> None: + if not self.enabled: + return + if self.transfer_backend not in _ALLOWED_BACKENDS: + raise ValueError(f"disaggregation.transfer_backend={self.transfer_backend!r} not in {_ALLOWED_BACKENDS}") + if self.prefill_replicas < 1 or self.decode_replicas < 1: + raise ValueError( + f"disaggregation requires >=1 prefill and >=1 decode replica " + f"(got prefill_replicas={self.prefill_replicas}, decode_replicas={self.decode_replicas})" + ) + if self.bootstrap_port is not None and not (0 < self.bootstrap_port < 65536): + raise ValueError(f"bootstrap_port out of range: {self.bootstrap_port}") + if self.transfer_backend == "mooncake" and self.mooncake_protocol not in _ALLOWED_MOONCAKE_PROTOCOLS: + raise ValueError( + f"disaggregation.mooncake_protocol={self.mooncake_protocol!r} not in {_ALLOWED_MOONCAKE_PROTOCOLS}" + ) + + def effective_decode_tp(self, prefill_tp: int) -> int: + """Resolve decode TP (defaults to ``prefill_tp``). Test-only helper; runtime paths + must inline this because OmegaConf/Ray serialization drops dataclass methods.""" + if self.decode_tensor_model_parallel_size is not None: + return self.decode_tensor_model_parallel_size + return prefill_tp diff --git a/verl_0720_main/verl/verl/workers/config/distillation.py b/verl_0720_main/verl/verl/workers/config/distillation.py new file mode 100644 index 0000000000000000000000000000000000000000..b58f8bccffd0d3efaaec0a351f0288de7d857818 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/distillation.py @@ -0,0 +1,320 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os +from dataclasses import dataclass, field +from typing import Optional + +from verl.base_config import BaseConfig +from verl.utils.config import omega_conf_to_dataclass + +from .rollout import RolloutConfig + +__all__ = ["DistillationLossConfig", "DistillationTeacherModelConfig", "DistillationConfig"] + +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +@dataclass +class DistillationLossConfig(BaseConfig): + """Configuration for distillation loss settings. + + loss_mode (str): + Distillation loss function to use. + topk (int, optional): + Number of top tokens to consider for top-k distillation losses. + use_task_rewards (bool): + Whether to include task rewards alongside distillation loss. + distillation_loss_coef (float): + Coefficient for distillation loss when combined with task rewards. + loss_max_clamp (float, optional): + Maximum value to clamp distillation loss. If None, no clamping is applied. + log_prob_min_clamp (float, optional): + Minimum value to clamp log probabilities for stability, e.g., log q - log p where p or q are + very close to zero. If None, no clamping is applied. + use_policy_gradient (bool): + Whether to incorporate distillation loss as a reward, as done + by https://thinkingmachines.ai/blog/on-policy-distillation/. Recommended to use loss_mode=k1. + Otherwise, distillation loss is directly backpropagated as a supervised loss, + as in https://arxiv.org/abs/2306.13649. Recommended to use loss_mode=k3 or forward_kl_topk. + policy_loss_mode (str): + Name of the policy loss to use when use_policy_gradient is true. + clip_ratio (float): + PPO clipping ratio for policy loss. + clip_ratio_low (float): + Lower bound for PPO clipping ratio. + clip_ratio_high (float): + Upper bound for PPO clipping ratio. + loss_settings (DistillationLossSettings, optional): + Runtime-populated settings based on loss_mode. Not set by user. + """ + + loss_mode: str = "k3" + topk: Optional[int] = 128 + use_task_rewards: bool = True + distillation_loss_coef: float = 1.0 + loss_max_clamp: Optional[float] = 10.0 + log_prob_min_clamp: Optional[float] = -10.0 + + # Chunked top-K log-probs (opt-in, avoids [B, T, V] log_softmax buffer + # at long context). Only consumed by ``loss_mode='forward_kl_topk'``. + # Default ``False`` to preserve short-context performance (chunked path + # has ~6x time overhead at N=14K, V=152K). Set ``True`` when hitting OOM + # at long context (>=64K tokens, V=152K) where the baseline path OOMs. + use_chunked_topk: bool = False + # Tokens per chunk along (B*T) when ``use_chunked_topk=True``. Larger + # chunks reduce kernel-launch overhead but increase per-chunk memory; + # smaller chunks reduce per-chunk memory but increase kernel-launch + # overhead (saved-tensor total stays constant either way). + chunked_topk_chunk_size: int = 4096 + + use_policy_gradient: bool = True + policy_loss_mode: str = "vanilla" + clip_ratio: float = 0.2 + clip_ratio_low: float = 0.2 + clip_ratio_high: float = 0.2 + + # Store global batch info for loss aggregation: + # dp_size: data parallel size + # batch_num_tokens: number of valid tokens in global batch + # global_batch_size: global batch size + global_batch_info: dict = field(default_factory=dict) + + # Store distillation loss settings for computing the specified loss_mode + # Not set by user, populated at runtime + loss_settings: Optional[dict] = None + + def __post_init__(self): + self._mutable_fields.add("loss_settings") + from verl.trainer.distillation.losses import DistillationLossSettings, get_distillation_loss_settings + + self.loss_settings: DistillationLossSettings = get_distillation_loss_settings(self.loss_mode) + + if self.policy_loss_mode != "vanilla": + raise NotImplementedError( + f"Only vanilla policy loss is currently supported when use_policy_gradient is True, " + f"but got {self.policy_loss_mode}." + ) + + if self.use_policy_gradient and self.loss_mode == "forward_kl_topk": + print( + "WARNING: forward_kl_topk is most effective as a supervised distillation loss " + "(use_policy_gradient=False). With policy gradient, the update uses only the sampled" + " token's logprob ∇logπ(a), so the top-k distributional signal (how non-sampled logits " + "should move) is largely unused." + ) + + if not self.use_policy_gradient and self.loss_mode == "k1": + raise ValueError( + "Directly backpropagating k1 loss is incorrect since gradient of k1 loss" + " wrt model weights does not depend on teacher log probabilities." + ) + + +@dataclass +class DistillationTeacherModelConfig(BaseConfig): + """Configuration for on-policy distillation teacher. + + key (str, optional): + Identifier to route examples to the teacher model in multi-teacher setting. + model_path (str, optional): + Model path for the teacher model. Can be a local path or a Hugging Face model + inference (RolloutConfig): + Rollout configuration for the teacher model inference during distillation. + num_replicas (int): + Number of inference replicas of this teacher to launch. Each replica occupies + `per_replica_world_size` GPUs (= inference.data_parallel_size * + inference.tensor_model_parallel_size * inference.pipeline_model_parallel_size), + so the teacher's total GPU footprint is + `num_replicas * per_replica_world_size`. + """ + + _mutable_fields = BaseConfig._mutable_fields | {"num_replicas", "key"} + + key: Optional[str] = None + model_path: Optional[str] = None + inference: RolloutConfig = field(default_factory=RolloutConfig) + num_replicas: Optional[int] = 0 + + @property + def per_replica_world_size(self) -> int: + return ( + self.inference.tensor_model_parallel_size + * self.inference.data_parallel_size + * self.inference.pipeline_model_parallel_size + ) + + @property + def world_size(self) -> int: + return self.num_replicas * self.per_replica_world_size + + def check_configured(self): + if self.model_path is None: + raise ValueError("model_path must be specified for distillation teacher model config.") + if self.key is None: + raise ValueError("key must be specified for distillation teacher model config.") + if self.num_replicas is None: + raise ValueError("num_replicas must be specified for distillation teacher model config.") + + def validate_and_prepare_for_distillation(self, use_topk: bool, topk: Optional[int]) -> None: + # Prompt + Response from student are fed into teacher as context + max_model_len = self.inference.max_model_len + student_prompt_length = self.inference.prompt_length + student_response_length = self.inference.response_length + required_context_len = student_prompt_length + student_response_length + 1 + if max_model_len is not None and required_context_len > max_model_len: + raise ValueError( + "Distillation teacher inference requires room for the student prompt, the full student " + f"response, and one generated token, but got {student_prompt_length=}, " + f"{student_response_length=}, {required_context_len=}, {max_model_len=}." + ) + self.inference.prompt_length = self.inference.prompt_length + self.inference.response_length + self.inference.response_length = 1 + self._validate_topk_logprobs(use_topk=use_topk, topk=topk) + + def _validate_topk_logprobs(self, use_topk: bool, topk: Optional[int]) -> None: + if not use_topk: + return + if topk is None: + raise ValueError("topk must be specified when use_topk is True.") + + engine_name = self.inference.name + engine_kwargs = self.inference.engine_kwargs + match engine_name: + case "vllm": + vllm_engine_kwargs = dict(engine_kwargs.get("vllm", {})) + max_logprobs = vllm_engine_kwargs.get("max_logprobs") + if max_logprobs is None: + vllm_engine_kwargs["max_logprobs"] = topk + max_logprobs = topk + if max_logprobs < topk: + raise ValueError( + f"VLLM max_logprobs ({max_logprobs}) must be >= distillation_loss topk " + f"({topk}) to enable distillation loss computation." + ) + engine_kwargs["vllm"] = vllm_engine_kwargs + case "sglang": + # SGLang's top_logprobs_num is a per-request parameter, so there is no + # engine-boot cap to align (unlike vLLM's max_logprobs). The async + # server translates sampling_params["prompt_logprobs"] into + # return_logprob + logprob_start_len=0 + top_logprobs_num at call time. + pass + case _: + raise NotImplementedError( + f"DistillationTeacherModelConfig does not support inference engine {engine_name}" + ) + + +@dataclass +class DistillationConfig(BaseConfig): + """Configuration for on-policy distillation. + + enabled (bool): + Whether on-policy distillation is enabled. + n_gpus_per_node (int): + Number of GPUs per node in the teacher resource pool. + nnodes (int): + Number of nodes in the teacher resource pool. + teacher_models (dict[str, TeacherModelConfig]): + Configurations for teacher models used for multi-teacher distillation. + teacher_key (str): + Key to route examples to the appropriate teacher model in multi-teacher setups. Should correspond to a field in + the data proto, e.g., data_source. + distillation_loss (DistillationLossConfig): + Configuration for distillation loss settings. + + NOTE: The `teacher_model` entry is in the `teacher_models` dict by default. + Since it is popped when other teacher entries are added, using `teacher_model` as + one of several keys silently drops it. For example, the following CLI overrides result + in ONLY `teacher_model2` being used: + + ```bash + distillation.teacher_models.teacher_model.key=openai/gsm8k + distillation.teacher_models.teacher_model.model_path=Qwen/Qwen3-4B + +distillation.teacher_models.teacher_model2.key=hiyouga/geometry3k + +distillation.teacher_models.teacher_model2.model_path=Qwen/Qwen3-VL-4B-Instruct + ``` + Instead, give the first teacher a different name: + + ```bash + +distillation.teacher_models.teacher_model1.key=openai/gsm8k + +distillation.teacher_models.teacher_model1.model_path=Qwen/Qwen3-4B + +distillation.teacher_models.teacher_model2.key=hiyouga/geometry3k + +distillation.teacher_models.teacher_model2.model_path=Qwen/Qwen3-VL-4B-Instruct + ``` + """ + + _mutable_fields = BaseConfig._mutable_fields | {"teacher_models", "n_gpus_per_node", "nnodes"} + + enabled: bool = False + n_gpus_per_node: int = 0 + nnodes: int = 0 + teacher_models: dict[str, DistillationTeacherModelConfig] = field(default_factory=dict) + teacher_key: str = "data_source" + distillation_loss: DistillationLossConfig = field(default_factory=DistillationLossConfig) + + def __post_init__(self): + if not self.enabled: + return + + self.teacher_models = self._resolve_teacher_models() + teacher_world_size_sum = 0 + for teacher_model in self.teacher_models.values(): + teacher_model.validate_and_prepare_for_distillation( + use_topk=self.distillation_loss.loss_settings.use_topk, + topk=self.distillation_loss.topk, + ) + teacher_world_size_sum += teacher_model.world_size + total_pool_size = self.n_gpus_per_node * self.nnodes + if teacher_world_size_sum != total_pool_size: + raise ValueError( + f"Sum of teacher (num_replicas * per_replica_world_size) ({teacher_world_size_sum}) must match " + f"the distillation resource pool size " + f"({self.n_gpus_per_node=} * {self.nnodes=} = {total_pool_size})." + ) + + def _resolve_teacher_models(self) -> dict[str, DistillationTeacherModelConfig]: + assert "teacher_model" in self.teacher_models + if len(self.teacher_models) == 1: + # Single teacher occupies the entire teacher resource pool. + teacher_model = self.teacher_models["teacher_model"] + inference = teacher_model.inference + per_replica = ( + inference.tensor_model_parallel_size + * inference.data_parallel_size + * inference.pipeline_model_parallel_size + ) + pool_size = self.n_gpus_per_node * self.nnodes + if pool_size % per_replica != 0: + raise ValueError( + f"Single teacher's per_replica_world_size ({per_replica}) must divide the distillation " + f"resource pool size ({self.n_gpus_per_node=} * {self.nnodes=} = {pool_size})." + ) + teacher_model.num_replicas = pool_size // per_replica + teacher_model.key = "default" + else: + # Multiple teachers: remove default single teacher config + self.teacher_models.pop("teacher_model") + + # Teacher models dict is keyed by teacher_key instead of YAML entry name + teacher_models = {} + for teacher_config in self.teacher_models.values(): + teacher_config = omega_conf_to_dataclass(teacher_config, dataclass_type=DistillationTeacherModelConfig) + teacher_config.check_configured() + if teacher_config.key in teacher_models: + raise ValueError(f"Duplicate teacher key {teacher_config.key} found in teacher models.") + teacher_models[teacher_config.key] = teacher_config + return teacher_models diff --git a/verl_0720_main/verl/verl/workers/config/engine.py b/verl_0720_main/verl/verl/workers/config/engine.py new file mode 100644 index 0000000000000000000000000000000000000000..4f53cb2e0683e752a91f156d17aaeab4b8d01205 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/engine.py @@ -0,0 +1,650 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os +import warnings +from dataclasses import dataclass, field +from typing import Any, Callable, Literal, Optional + +from verl.base_config import BaseConfig +from verl.trainer.config import CheckpointConfig + +from ...utils.profiler import ProfilerConfig +from .model import HFModelConfig +from .optimizer import OptimizerConfig + +__all__ = [ + "FSDPEngineConfig", + "McoreEngineConfig", + "TrainingWorkerConfig", + "TorchtitanEngineConfig", + "VeOmniEngineConfig", + "AutomodelEngineConfig", + "EngineConfig", + "EngineRouterReplayConfig", + "QATEngineConfig", + "MindSpeedEngineConfig", +] + + +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "INFO")) + + +# TODO: rename to RouterReplayConfig after removing the legacy implementation +@dataclass +class EngineRouterReplayConfig(BaseConfig): + """Configuration for router replay in MoE models. + + This configuration controls the routing behavior for Mixture of Experts (MoE) models, + allowing for deterministic training through route recording and replay. + + Args: + mode (str): Router replay mode. Options: 'disabled', 'R2', 'R3'. + - 'disabled': No router replay functionality + - 'R2': Use Router Replay routing strategy + - 'R3': Use Rollout Router Replay routing strategy + record_file (Optional[str]): File path to save recorded routing decisions. + Required when mode is 'record', 'R2', or 'R3'. + replay_file (Optional[str]): File path to load recorded routing decisions for replay. + Required when mode is 'replay'. + """ + + mode: str = "disabled" + record_file: Optional[str] = None + replay_file: Optional[str] = None + + def __post_init__(self): + """Validate router replay configuration.""" + valid_modes = ["disabled", "R2", "R3"] + if self.mode not in valid_modes: + raise ValueError(f"Invalid router_replay mode: {self.mode}. Must be one of {valid_modes}") + + +@dataclass +class EngineConfig(BaseConfig): + _mutable_fields = BaseConfig._mutable_fields | { + "use_dynamic_bsz", + "max_token_len_per_gpu", + "micro_batch_size_per_gpu", + "infer_max_token_len_per_gpu", + "infer_micro_batch_size_per_gpu", + "use_fused_kernels", + "use_remove_padding", + "forward_only", + "param_offload", + } + # whether to offload param + param_offload: bool = False + # whether to offload optimizer + optimizer_offload: bool = False + # whether to offload grad + grad_offload: bool = False + # whether the engine is forward only (e.g., ref policy) + forward_only: bool = False + # the strategy (backend) + strategy: str = None + # model dtype + dtype: str = "bfloat16" # ["bfloat16", "float16"] + # whether to use dynamic bsz + use_dynamic_bsz: bool = True + # for training + max_token_len_per_gpu: int = None + micro_batch_size_per_gpu: int = None + # for inference + infer_max_token_len_per_gpu: int = None + infer_micro_batch_size_per_gpu: int = None + # whether use fuse lm head kernel + use_fused_kernels: bool = False + # TODO (this may conflict with the one in model config) + use_remove_padding: bool = True + + seed: int = 42 + + full_determinism: bool = False + router_replay: EngineRouterReplayConfig = field(default_factory=EngineRouterReplayConfig) + + def __post_init__(self): + pass + # TODO: turn on this check after we reorg config + # if self.use_dynamic_bsz: + # assert self.max_token_len_per_gpu is not None + # else: + # assert self.micro_batch_size_per_gpu is not None + + +@dataclass +class QATEngineConfig(BaseConfig): + """Configuration for QAT (Quantization-Aware Training) within an engine. + + Args: + enable (bool): Whether to enable QAT, default False + mode (str): Quantization mode, "w4a16" or "w4a4", default "w4a16" + group_size (int): Group size for blockwise quantization, default 16 + ignore_patterns (list[str]): Module name patterns to exclude from quantization + activation_observer (str): Observer strategy for activation global_scale (W4A4 only) + quantization_config_path (Optional[str]): Path to quantization config JSON for vLLM + """ + + enable: bool = False + mode: str = "w4a16" + group_size: int = 16 + ignore_patterns: list[str] = field(default_factory=lambda: ["lm_head", "embed_tokens", "re:.*mlp.gate$"]) + activation_observer: str = "static_minmax" + quantization_config_path: Optional[str] = None + + +@dataclass +class McoreEngineConfig(EngineConfig): + """Configuration for Megatron parallelism. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + param_offload (bool): Whether to offload parameters to CPU. + grad_offload (bool): Whether to offload gradients to CPU. + optimizer_offload (bool): Whether to offload optimizer states to CPU. + tensor_model_parallel_size (int): Tensor model parallel size. + expert_model_parallel_size (int): Expert model parallel size for MoE models. + expert_tensor_parallel_size (Optional[int]): Expert tensor parallel size for MoE models. + pipeline_model_parallel_size (int): Pipeline model parallel size. + virtual_pipeline_model_parallel_size (Optional[int]): Virtual pipeline model parallel size + for interleaved scheduling. + context_parallel_size (int): Context parallel size for long sequences. + dynamic_context_parallel (bool): Whether to enable hybrid context parallelism. + max_seqlen_per_dp_cp_rank (Optional[int]): Maximum sequence length per DPxCP rank. + sequence_parallel (bool): Whether to enable sequence parallelism. + use_distributed_optimizer (bool): Whether to use distributed optimizer. + use_dist_checkpointing (bool): Whether to use distributed checkpointing. + dist_checkpointing_path (Optional[str]): Path for distributed checkpointing. + dist_ckpt_optim_fully_reshardable (bool): Use fully reshardable optimizer checkpoints. + distrib_optim_fully_reshardable_mem_efficient (bool): Use memory-efficient fully reshardable format. + seed (int): Random seed for reproducibility. + override_ddp_config (dict[str, Any]): Override configuration for DDP. + override_transformer_config (dict[str, Any]): Override configuration for transformer. + use_mbridge (bool): Whether to use MBridge for communication. + vanilla_mbridge (bool): Whether to use the deprecated legacy mbridge backend instead of Megatron-Bridge. + use_megatron_fsdp (bool): Whether to use Megatron-FSDP (Zero-3 sharding). + dtype (str): Mixed precision training param dtype, default "bfloat16" + """ + + # sequence_parallel is not listed as a frozen field for auto-correction purpose + _mutable_fields = EngineConfig._mutable_fields | {"sequence_parallel"} + # mcore parallelism + tensor_model_parallel_size: int = 1 + expert_model_parallel_size: int = 1 + expert_tensor_parallel_size: Optional[int] = None + pipeline_model_parallel_size: int = 1 + virtual_pipeline_model_parallel_size: Optional[int] = None + context_parallel_size: int = 1 + dynamic_context_parallel: bool = False + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + max_seqlen_per_dp_cp_rank: Optional[int] = None + sequence_parallel: bool = True + use_distributed_optimizer: bool = True + pad_bshd_to_minibatch_max: bool = True + use_dist_checkpointing: bool = False + dist_checkpointing_path: Optional[str] = None + dist_checkpointing_prefix: str = "" + dist_ckpt_optim_fully_reshardable: bool = False + distrib_optim_fully_reshardable_mem_efficient: bool = False + override_ddp_config: dict[str, Any] = field(default_factory=dict) + override_transformer_config: dict[str, Any] = field(default_factory=dict) + override_mcore_model_config: dict[str, Any] = field(default_factory=dict) + use_mbridge: bool = True + vanilla_mbridge: bool = False + use_megatron_fsdp: bool = False + strategy: str = "megatron" + qat: QATEngineConfig = field(default_factory=QATEngineConfig) + + def __post_init__(self) -> None: + super().__post_init__() + """config validation logics go here""" + assert self.strategy == "megatron" + assert self.dtype in ["bfloat16", "float16"], f"dtype {self.dtype} not supported" + if self.vanilla_mbridge: + warnings.warn( + "The legacy mbridge backend selected by `vanilla_mbridge=True` is deprecated and will be removed " + "in a future release. Use Megatron-Bridge by setting `vanilla_mbridge=False` or removing the option.", + FutureWarning, + stacklevel=2, + ) + if self.tensor_model_parallel_size == 1: + warnings.warn("set sequence parallel to false as TP size is 1", stacklevel=2) + self.sequence_parallel = False + + +@dataclass +class FSDPEngineConfig(EngineConfig): + """Configuration for FSDP (Fully Sharded Data Parallel). + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + wrap_policy (Dict[str, Any]): Configuration for FSDP wrap policy. + param_offload (bool): Whether to offload parameters to CPU, default False + optimizer_offload (bool): Whether to offload optimizer states to CPU, default False + offload_policy (bool): Whether to offload policy model parameters, default False + reshard_after_forward (bool): Whether to reshard parameters after forward pass, default True + fsdp_size (int): FSDP group size. -1 means use all available GPUs. + forward_prefetch (bool): Whether to prefetch parameters for next forward pass, default False + model_dtype (str): Model data type used to initialize the transformers model. default "fp32" + use_orig_params (bool): Whether to use original parameters when initialize FSDP1, default False + seed (int): Random seed for reproducibility. + full_determinism (bool): If true, enable_full_determinism is called to ensure reproducible results + in distributed training. Important: this will negatively impact performance, so only use it for + debugging. + mixed_precision (Optional[dict[str, Any]]): Mixed precision configuration for FSDP, default None + dtype (str): Mixed precision training param dtype, default "bfloat16" + qat (QATEngineConfig): QAT configuration, default disabled + """ + + # ulysses_sequence_parallel_size is mutable for backward compatibility + _mutable_fields = EngineConfig._mutable_fields | {"ulysses_sequence_parallel_size"} + + # fsdp specific flags + wrap_policy: dict[str, Any] = field(default_factory=dict) + offload_policy: bool = False + reshard_after_forward: bool = True + fsdp_size: int = -1 + forward_prefetch: bool = False + model_dtype: str = "fp32" + use_orig_params: bool = False + mixed_precision: Optional[dict[str, Any]] = None + ulysses_sequence_parallel_size: int = 1 + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + use_torch_compile: bool = True + entropy_checkpointing: bool = False + strategy: str = "fsdp" + qat: QATEngineConfig = field(default_factory=QATEngineConfig) + + def __post_init__(self): + super().__post_init__() + assert self.strategy in ["fsdp", "fsdp2"], f"strategy {self.strategy} not supported" + + +@dataclass +class VeOmniEngineConfig(EngineConfig): + """Configuration for VeOmni. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + param_offload (bool): Whether to offload parameters to CPU, default False + optimizer_offload (bool): Whether to offload optimizer states to CPU, default False + fsdp_size (int): FSDP group size. -1 means use all available GPUs, default -1 + ulysses_parallel_size (int): Ulysses sequence parallel size, default 1 + expert_parallel_size (int): Expert parallel size, default 1 + init_device (str): Device to initialize model weights. + 1. `cpu`: Init parameters on CPU in rank0 only. + 2. `cuda`: Init parameters on GPU. + 3. `meta`: Init parameters on meta. + 4. `npu`: Init parameters on Ascend NPU. + default "meta" + enable_full_shard (bool): Enable fully shard for FSDP training (ZeRO-3), default False + enable_fsdp_offload (bool): Enable CPU offload for FSDP1, default False + enable_reentrant (bool): Use reentrant gradient checkpointing, default False + attn_implementation (str): Attention implementation to use. + 1. `eager` + 2. `sdpa` + 3. `flash_attention_2` + 4. `flash_attention_3` + 5. `veomni_flash_attention_2_with_sp` + 6. `veomni_flash_attention_3_with_sp` + 7. `native-sparse` + default "flash_attention_2" + Note: In case VeOmni add more attn_implementation, please check https://github.com/ByteDance-Seed/VeOmni/ + moe_implementation (str): MoE implementation to use. + 1. `eager` + 2. `fused` + default "fused" + Note: In case VeOmni add more moe_implementation, please check https://github.com/ByteDance-Seed/VeOmni/ + cross_entropy_loss_implementation (str): Cross-entropy kernel selected via VeOmni's + ``OpsImplementationConfig``. Common values: ``"eager"`` (default), ``"liger_kernel"``, + ``"npu"``. See VeOmni docs for the full registry. + rms_norm_implementation (str): RMSNorm kernel. ``"eager"`` (HF default), + ``"triton"`` (batch-invariant Triton kernel — required to keep vexact's rollout + and the FSDP actor bitwise-aligned on DeepSeek-V3 / Moonlight), ``"liger_kernel"``, + ``"npu"``. + swiglu_mlp_implementation (str): SwiGLU MLP kernel. ``"eager"`` (default) or + ``"liger_kernel"``. + rotary_pos_emb_implementation (str): RoPE kernel. ``"eager"`` (default), ``"triton"`` + (deterministic Triton bmm — required for bitwise-aligned RoPE on DeepSeek-V3 / + Moonlight), ``"liger_kernel"``, ``"npu"``. + load_balancing_loss_implementation (str): MoE load-balancing loss kernel. + ``"eager"`` (default) or ``"triton"``. + force_use_huggingface (bool): Force loading model from huggingface, default False + activation_gpu_limit (float): When enabling activation offload, `activation_gpu_limit` GB + activations are allowed to reserve on GPU, default 0.0 + basic_modules (list[str]): List of basic modules to use, default None + forward_prefetch (bool): Whether to prefetch parameters for next forward pass, default False + model_dtype (str): Model data type used to initialize the transformers model. default "fp32" + seed (int): Random seed for reproducibility. + full_determinism (bool): If true, enable_full_determinism is called to ensure reproducible results + in distributed training. Important: this will negatively impact performance, so only use it for + debugging. + mixed_precision (Optional[dict[str, Any]]): Mixed precision configuration for FSDP, default None + rms_norm_gated_implementation (str): Gated RMSNorm implementation (Qwen3.5 GatedDeltaNet + ``self.norm``). ``"fla"`` uses fla.modules.FusedRMSNormGated (requires flash-linear-attention, + GPU). ``"eager"`` (default) uses the HuggingFace Qwen3_5RMSNormGated. Qwen3.5 has no NPU + backend today — selecting any non-eager value on NPU raises at OpSlot bind time. + causal_conv1d_implementation (str): Varlen depthwise causal conv1d implementation (Qwen3.5 + GatedDeltaNet pre-mixer). ``"fla"`` uses fla.modules.convolution.causal_conv1d (requires + flash-linear-attention, GPU). ``"eager"`` (default) leaves causal_conv1d_fn unset; the varlen + training path then raises because no torch fallback handles cu_seqlens. Qwen3.5 has no NPU + backend today — selecting any non-eager value on NPU raises at OpSlot bind time. + chunk_gated_delta_rule_implementation (str): Chunk gated delta-rule kernel for Qwen3.5 linear + attention. ``"fla"`` uses fla.ops.gated_delta_rule.chunk_gated_delta_rule (requires + flash-linear-attention, GPU). ``"flash_qla"`` uses QwenLM FlashQLA (requires the optional + flash-qla extra, Hopper SM90 only — no Ampere/Ada below or Blackwell above; SM10x wheels are + WIP upstream). ``"eager"`` (default) uses transformers' torch_chunk_gated_delta_rule, which + does NOT support cu_seqlens; varlen training therefore raises at runtime. Qwen3.5 has no NPU + backend today — selecting any non-eager value on NPU raises at OpSlot bind time. + + """ + + _mutable_fields = EngineConfig._mutable_fields | {"attn_implementation"} + + forward_prefetch: bool = False + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + use_torch_compile: bool = True + entropy_checkpointing: bool = False + strategy: str = "veomni" + fsdp_size: int = -1 + ulysses_parallel_size: int = 1 + expert_parallel_size: int = 1 + seed: int = 42 + full_determinism: bool = False + mixed_precision: bool = False + init_device: str = "meta" + enable_full_shard: bool = False + ckpt_manager: Literal["dcp"] = "dcp" + load_checkpoint_path: Optional[str] = None + enable_fsdp_offload: bool = False + enable_reentrant: bool = False + attn_implementation: str = "flash_attention_2" + moe_implementation: str = "fused" + # Kernel-backend selectors for VeOmni's per-model patches; passed into + # OpsImplementationConfig and consumed by apply_per_model_patches in each + # model's device_patch.py. Defaults match VeOmni's OpsImplementationConfig + # defaults so existing configs see no change. + cross_entropy_loss_implementation: str = "eager" + rms_norm_implementation: str = "eager" + swiglu_mlp_implementation: str = "eager" + rotary_pos_emb_implementation: str = "eager" + load_balancing_loss_implementation: str = "eager" + rms_norm_gated_implementation: str = "eager" + causal_conv1d_implementation: str = "eager" + chunk_gated_delta_rule_implementation: str = "eager" + force_use_huggingface: bool = False + activation_gpu_limit: float = 0.0 + basic_modules: Optional[list[str]] = field(default_factory=list) + + def __post_init__(self): + super().__post_init__() + assert self.strategy in ["veomni"], f"strategy {self.strategy} not supported" + + replacements = { + "flash_attention_2": "veomni_flash_attention_2_with_sp", + "flash_attention_3": "veomni_flash_attention_3_with_sp", + "flash_attention_4": "veomni_flash_attention_4_with_sp", + } + if self.attn_implementation in replacements: + new_impl = replacements[self.attn_implementation] + logger.info(f"Replacing attn_implementation from '{self.attn_implementation}' to '{new_impl}'") + self.attn_implementation = new_impl + + +@dataclass +class TorchtitanEngineConfig(EngineConfig): + """Configuration for Torchtitan. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + wrap_policy (Dict[str, Any]): Configuration for FSDP wrap policy. + reshard_after_forward (Literal["default", "always", "never"]): The policy for applying + `reshard_after_forward` within an FSDP setup, default "default" + forward_prefetch (bool): Whether to prefetch parameters for next forward pass, default False + use_orig_params (bool): Whether to use original parameters when initialize FSDP, default False + mixed_precision (bool): Mixed precision configuration for FSDP, default False + offload_policy (bool): Whether to offload policy model parameters, default False + data_parallel_size (int): Data parallel group size, default 1 + data_parallel_replicate_size (int): Data parallel replicate size, default 1 + data_parallel_shard_size (int): Data parallel shard degree, default 1 + tensor_parallel_size (int): Tensor parallel size, default 1 + expert_parallel_size (int): Expert parallel size, default 1 + expert_tensor_parallel_size (int): Expert tensor parallel size, default 1 + pipeline_parallel_size (int): Pipeline parallel size, default 1 + context_parallel_size (int): Context parallel size, default 1 + attn_type (str): Attention type for torchtitan's model (e.g., "sdpa", "flex", "varlen"), + default "flex" + spmd_backend (str): torchtitan SPMD backend, one of "default", "full_dtensor", "spmd_types", + default "spmd_types" + activation_checkpoint (str): Activation checkpointing mode, one of "selective", "full", "none". + Default "selective" (torchtitan's default). Use "none" under spmd_backend="spmd_types" with + eager: selective/full AC recompute runs on the autograd backward + thread where the thread-local SPMD mesh is inactive, so spmd.assert_type raises + "no current mesh". Compiled runs recompute in-graph and are unaffected. + strategy (str): Strategy to use for distributed training, default "torchtitan" + seed (int): Random seed for reproducibility. + full_determinism (bool): If true, enable_full_determinism is called to ensure reproducible results + in distributed training. Important: this will negatively impact performance, so only use it for + debugging. + + """ + + wrap_policy: dict[str, Any] = field(default_factory=dict) + reshard_after_forward: Literal["default", "always", "never"] = "default" + forward_prefetch: bool = False + use_orig_params: bool = False + mixed_precision: bool = False + offload_policy: bool = False + use_torch_compile: bool = True + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + entropy_checkpointing: bool = False + data_parallel_size: int = 1 + data_parallel_replicate_size: int = 1 + data_parallel_shard_size: int = 1 + tensor_parallel_size: int = 1 + expert_parallel_size: int = 1 + expert_tensor_parallel_size: int = 1 + pipeline_parallel_size: int = 1 + context_parallel_size: int = 1 + attn_type: str = "flex" + spmd_backend: str = "spmd_types" + activation_checkpoint: str = "selective" + max_seq_len: Optional[int] = None + strategy: str = "torchtitan" + seed: int = 42 + full_determinism: bool = False + + def __post_init__(self): + super().__post_init__() + assert self.attn_type in ["flex", "flex_flash", "varlen"], ( + f"attn_type {self.attn_type} not supported (sdpa is not a valid language-model backend)" + ) + assert self.spmd_backend in ["default", "full_dtensor", "spmd_types"], ( + f"spmd_backend {self.spmd_backend} not supported" + ) + assert self.activation_checkpoint in ["selective", "full", "none"], ( + f"activation_checkpoint {self.activation_checkpoint} not supported" + ) + assert self.strategy in ["torchtitan"], f"strategy {self.strategy} not supported" + + +@dataclass +class AutomodelEngineConfig(EngineConfig): + """Configuration for Automodel (nemo_automodel) backend. + + The Automodel backend uses NeMoAutoModelForCausalLM for model loading and + supports FSDP2, MegatronFSDP, and DDP distributed strategies with optional + TP, CP, and EP parallelism. + + Args: + strategy (str): Backend strategy identifier, must be "automodel". + distributed_strategy (str): Distributed training strategy: "fsdp2", "megatron_fsdp", or "ddp". + tp_size (int): Tensor parallel size. + pp_size (int): Pipeline parallel size (only pp_size=1 supported initially). + cp_size (int): Context parallel size. + ep_size (int): Expert parallel size for MoE models. + dp_replicate_size (int): Data-parallel replicate size for HSDP. 1 = pure sharding. + sequence_parallel (bool): Enable sequence parallelism in the TP plan. + defer_fsdp_grad_sync (bool): Defer FSDP gradient sync to the final micro-batch. + activation_checkpointing (bool): Whether to enable activation checkpointing. + enable_fp8 (bool): Whether to enable FP8 training. + enable_compile (bool): Whether to enable torch.compile for the model. + model_dtype (str): Model data type for loading weights. "fp32" loads in float32 + (matching FSDP golden), "auto" uses the dtype from the model config. + attn_implementation (str): Attention implementation to use ("sdpa", "flash_attention_2", "eager", "te"). + + Backend settings (nemo_automodel BackendConfig): + backend_config (dict): Dict of kwargs passed directly to + nemo_automodel.components.models.common.BackendConfig(**backend_config). + Controls how model layers are implemented (TE vs PyTorch) and MoE dispatch. + See automodel.yaml for all predefined keys with defaults. + Key fields: + attn (str): Attention backend. "te" = TransformerEngine fused attention, + "sdpa" = PyTorch scaled dot-product attention. Default: "sdpa". + linear (str): Linear layer backend. "te" = TE fused linear (with FP8 support), + "torch" = standard PyTorch linear. Default: "te". + rms_norm (str): RMSNorm backend. "te" = TE fused RMSNorm, "torch" = PyTorch, + "torch_fp32" = PyTorch in FP32 (better numerical stability for MoE). + Default: "torch_fp32". + rope_fusion (bool): Enable fused RoPE kernel (requires CP=1). Default: true. + experts (str): MoE expert computation backend. + "gmm" = grouped_gemm (requires pip install grouped_gemm), + "torch_mm" = torch._grouped_mm (no external dependency), + "te" = TE GroupedLinear. Default: "gmm". + dispatcher (str): MoE token dispatch strategy. + "torch" = standard all-gather + local compute, + "deepep" = DeepEP optimized all-to-all (higher throughput). + Default: "torch". + Note: "deepep" with experts="gmm" matches the legacy enable_deepep=True behavior. + enable_fsdp_optimizations (bool): Enable FSDP-specific optimizations in Automodel. + Default: false. + enable_hf_state_dict_adapter (bool): Enable HuggingFace state dict adapter for + checkpoint compatibility. Default: true. + fake_balanced_gate (bool): Use fake balanced gating for debugging. Default: false. + fake_gate_noise (float): Noise added to fake balanced gate. Default: 0.0. + gate_precision: Gate computation precision. Default: null (auto). + Full reference: nemo_automodel/components/models/common/backend_config.py + + MoE / Expert Parallelism settings: + moe_config (dict): Dict of kwargs passed directly to + nemo_automodel.components.moe.parallelizer.MoEParallelizerConfig(**moe_config). + Controls MoE parallelization behavior within FSDP2. + See automodel.yaml for all predefined keys with defaults. + Key fields: + ignore_router_for_ac (bool): Exclude router from activation checkpointing. + Default: false. + reshard_after_forward (bool): Reshard expert params after forward pass + (trades compute for memory). Default: false. + lm_head_precision: Precision for the LM head. Default: null (auto). + wrap_outer_model (bool): Whether to FSDP-wrap the outermost model module. + Default: true. + Full reference: nemo_automodel/components/moe/parallelizer.py + + Mixed precision policy (FSDP2): + mp_param_dtype (str): Parameter dtype for FSDP2 mixed precision policy. + mp_reduce_dtype (str): Reduce dtype for FSDP2 mixed precision policy. + mp_output_dtype (str): Output dtype for FSDP2 mixed precision policy. + + Entropy computation: + entropy_from_logits_with_chunking (bool): Whether to use chunked entropy computation. + use_torch_compile (bool): Whether to use torch.compile for entropy computation. + entropy_checkpointing (bool): Whether to use checkpointing for entropy computation. + """ + + strategy: str = "automodel" + distributed_strategy: str = "fsdp2" + # Parallelism sizes + tp_size: int = 1 + pp_size: int = 1 + cp_size: int = 1 + ep_size: int = 1 + dp_replicate_size: int = 1 + sequence_parallel: bool = False + defer_fsdp_grad_sync: bool = True + # Model settings + activation_checkpointing: bool = False + enable_fp8: bool = False + enable_compile: bool = False + model_dtype: str = "fp32" + attn_implementation: str = "flash_attention_2" + # Backend settings + backend_config: dict = field(default_factory=dict) + # MoE settings + moe_config: dict = field(default_factory=dict) + # Mixed precision policy + mp_param_dtype: str = "bf16" + mp_reduce_dtype: str = "fp32" + mp_output_dtype: str = "bf16" + # Entropy computation + entropy_from_logits_with_chunking: bool = False + entropy_from_logits_chunk_size: int = 2048 + use_torch_compile: bool = True + entropy_checkpointing: bool = False + + def __post_init__(self): + super().__post_init__() + assert self.strategy == "automodel", f"strategy must be 'automodel', got {self.strategy}" + assert self.distributed_strategy in ["fsdp2", "megatron_fsdp", "ddp"], ( + f"distributed_strategy {self.distributed_strategy} not supported" + ) + assert self.pp_size == 1, "Pipeline parallelism (pp_size > 1) is not yet supported for automodel backend" + + +@dataclass +class MindSpeedEngineConfig(McoreEngineConfig): + """Configuration for mindspeed parallelism. + + The inheritance from BaseConfig provides omegaconf.DictConfig-like interface for a dataclass config. + + Args: + mcore_kwargs dict[str, Any]: mindspeed_megatron engine kwargs. + fsdp_kwargs dict[str, Any]: mindspeed_fsdp engine kwargs. + """ + + strategy: str = "mindspeed_megatron" + mcore_kwargs: dict[str, Any] = field(default_factory=dict) + fsdp_kwargs: dict[str, Any] = field(default_factory=dict) + + def __post_init__(self) -> None: + """config validation logics go here""" + assert self.strategy in ["mindspeed_megatron", "mindspeed_fsdp"], f"strategy {self.strategy} not supported" + assert self.dtype in ["bfloat16", "float16"], f"dtype {self.dtype} not supported" + if self.tensor_model_parallel_size == 1: + warnings.warn("set sequence parallel to false as TP size is 1", stacklevel=2) + self.sequence_parallel = False + + +@dataclass +class TrainingWorkerConfig(BaseConfig): + model_type: str = None # model type (language_model/value_model) + model_config: HFModelConfig = None + engine_config: EngineConfig = None + optimizer_config: OptimizerConfig = None + checkpoint_config: CheckpointConfig = None + profiler_config: ProfilerConfig = None + # automatically select engine and optimizer function. + # This function takes model config and the device name as parameter. + # Users can pass in a higher-order function to take more parameters + auto_select_engine_optim_fn: Callable[["HFModelConfig", str], tuple["EngineConfig", "OptimizerConfig"]] = None + extra_context: dict = field(default_factory=dict) diff --git a/verl_0720_main/verl/verl/workers/config/megatron_peft.py b/verl_0720_main/verl/verl/workers/config/megatron_peft.py new file mode 100644 index 0000000000000000000000000000000000000000..ccfb5b25dd314d85ce9a139ff655a3ce82dca522 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/megatron_peft.py @@ -0,0 +1,40 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""PEFT configuration of Megatron for verl.""" + + +def get_peft_cls(model_config, bridge, provider, dtype=None): + """Create a Megatron-Bridge PEFT object from ``model_config.lora``.""" + if not hasattr(model_config, "lora"): + return None + + lora_cfg = model_config.lora + if lora_cfg.get("rank", 0) <= 0: + return None + + assert bridge is not None and provider is not None, "LoRA/PEFT only supported via Megatron-Bridge" + + from megatron.bridge.peft.utils import create_peft + + peft_cls = create_peft(lora_cfg, dtype=dtype) + print( + f"Enabling {lora_cfg.get('type', 'lora').upper()} with rank={lora_cfg.get('rank')}, " + f"alpha={lora_cfg.get('alpha')}, dropout={lora_cfg.get('dropout')}" + ) + return peft_cls + + +__all__ = [ + "get_peft_cls", +] diff --git a/verl_0720_main/verl/verl/workers/config/model.py b/verl_0720_main/verl/verl/workers/config/model.py new file mode 100644 index 0000000000000000000000000000000000000000..545ec4682189d3811311cd0ac8fcae3e153bef3b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/model.py @@ -0,0 +1,261 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from dataclasses import dataclass, field +from typing import Any, Optional + +from omegaconf import MISSING +from transformers import AutoConfig + +from verl.base_config import BaseConfig +from verl.utils import hf_processor, hf_tokenizer +from verl.utils.fs import copy_to_local +from verl.utils.import_utils import import_external_libs +from verl.utils.model import get_generation_config, update_model_config + +__all__ = ["HFModelConfig", "MtpConfig"] + + +@dataclass +class MtpConfig(BaseConfig): + """ + Configuration for MTP model. + + enable: Enable loading and saving of MTP parameters, but do not use them + + enable_train: Whether to enable using MTP parameters during training + enable_rollout: Whether to enable using MTP parameters during rollout + + Training parameters: + detach_encoder: Whether to detach encoder parameters during MTP training + mtp_loss_scaling_factor: Loss scaling factor during MTP training + + vLLM rollout parameters: + method: "mtp" + num-speculative-tokens: 1 + + SGLang rollout parameters: + speculative-algorithm: EAGLE + speculative-num-steps: 3 + speculative-eagle-topk: 1 + speculative-num-draft-tokens: 4 + """ + + enable: bool = False + enable_train: bool = False + enable_rollout: bool = False + + detach_encoder: bool = False + mtp_loss_scaling_factor: float = 0.1 + + speculative_algorithm: str = "EAGLE" + speculative_num_steps: int = 3 + speculative_eagle_topk: int = 1 + speculative_num_draft_tokens: int = 4 + + method: str = "mtp" + num_speculative_tokens: int = 1 + + +@dataclass +class HFModelConfig(BaseConfig): + # note that we separate model_path, model_config_path and tokenizer_path in case they are different + _mutable_fields = { + "model_type", + "hf_config_path", + "tokenizer_path", + "hf_config", + "generation_config", + "tokenizer", + "processor", + "local_path", + "architectures", + "local_hf_config_path", + "local_tokenizer_path", + "mtp", + } + + path: str = MISSING + local_path: Optional[str] = None + hf_config_path: Optional[str] = None + local_hf_config_path: Optional[str] = None + tokenizer_path: Optional[str] = None + local_tokenizer_path: Optional[str] = None + + # model type, e.g., "language_model", "value_model" + model_type: str = "language_model" + + # whether to load tokenizer. This is useful when we only want to load model config + load_tokenizer: bool = True + + hf_config: Any = None + generation_config: Any = None + tokenizer: Any = None + processor: Any = None + + # whether to use shared memory + use_shm: bool = False + trust_remote_code: bool = False + + # custom chat template for the model + custom_chat_template: Optional[str] = None + + external_lib: Any = None + + override_config: dict = field(default_factory=dict) + + enable_gradient_checkpointing: bool = True + enable_activation_offload: bool = False + + use_remove_padding: bool = True + + # TODO: unify fsdp and megatron lora config + # fsdp lora related. We may setup a separate config later + lora_rank: int = 0 + lora_alpha: int = 16 + target_modules: Optional[Any] = "all-linear" # allow both "all-linear" and ["q_proj","k_proj"] + target_parameters: Optional[list[str]] = None # for lora adapter on nn.Parameter + + exclude_modules: Optional[str] = None + + # megatron lora config + lora: dict[str, Any] = field(default_factory=dict) + + # path to pre-trained LoRA adapter to load for continued training + lora_adapter_path: Optional[str] = None + use_liger: bool = False + + use_fused_kernels: bool = False + fused_kernel_options: dict = field(default_factory=dict) + + # TiledMLP configuration for memory-efficient MLP computation + tiled_mlp: dict = field(default_factory=lambda: {"enabled": False, "num_shards": 4}) + + architectures: Optional[list[str]] = None + + mtp: MtpConfig = field(default_factory=MtpConfig) + + def __post_init__(self): + import_external_libs(self.external_lib) + + if self.hf_config_path is None: + self.hf_config_path = self.path + if self.tokenizer_path is None: + self.tokenizer_path = self.path + + self.local_path = copy_to_local(self.path, use_shm=self.use_shm) + + # construct tokenizer + if self.load_tokenizer: + self.local_tokenizer_path = copy_to_local(self.tokenizer_path, use_shm=self.use_shm) + self.tokenizer = hf_tokenizer(self.local_tokenizer_path, trust_remote_code=self.trust_remote_code) + self.processor = hf_processor(self.local_tokenizer_path, trust_remote_code=self.trust_remote_code) + + # For base models (e.g. Qwen3.5-2b-Base), the processor may not have a chat_template + # while the tokenizer does. Sync it so that processor.apply_chat_template() works. + if ( + self.processor is not None + and not getattr(self.processor, "chat_template", None) + and getattr(self.tokenizer, "chat_template", None) + ): + self.processor.chat_template = self.tokenizer.chat_template + + if self.custom_chat_template is not None: + if self.processor is not None: + self.processor.chat_template = self.custom_chat_template + else: + self.tokenizer.chat_template = self.custom_chat_template + + self.local_hf_config_path = copy_to_local(self.hf_config_path, use_shm=self.use_shm) + self.generation_config = get_generation_config( + self.local_hf_config_path, trust_remote_code=self.trust_remote_code + ) + + # construct hf_config + attn_implementation = self.override_config.get("attn_implementation", "flash_attention_2") + try: + self.hf_config = AutoConfig.from_pretrained( + self.local_hf_config_path, + trust_remote_code=self.trust_remote_code, + attn_implementation=attn_implementation, + ) + except ValueError as error: + lookup_error = error.__cause__ or error.__context__ + if not isinstance(lookup_error, KeyError) or lookup_error.args != ("deepseek_v4",): + raise + from vllm.transformers_utils.config import get_config + + self.hf_config = get_config( + self.local_hf_config_path, + trust_remote_code=self.trust_remote_code, + attn_implementation=attn_implementation, + ) + + override_config_kwargs = {} + + if self.tokenizer is not None: + override_config_kwargs.update( + { + "bos_token_id": self.tokenizer.bos_token_id, + "eos_token_id": self.tokenizer.eos_token_id, + "pad_token_id": self.tokenizer.pad_token_id, + } + ) + + # TODO: (vermouth1992). self.config.model in megatron differs from that of fsdp in the override_config. + override_config = ( + self.override_config["model_config"] if "model_config" in self.override_config else self.override_config + ) + override_config_kwargs.update(override_config) + update_model_config(self.hf_config, override_config_kwargs=override_config_kwargs) + + self.share_embeddings_and_output_weights = getattr(self.hf_config, "tie_word_embeddings", False) + + # get model architectures + self.architectures = getattr(self.hf_config, "architectures", None) + assert self.architectures is not None and len(self.architectures) == 1, ( + "Expect only one architecture, got {}".format(self.architectures) + ) + + # per model patch + if getattr(self.hf_config, "model_type", None) == "kimi_vl": + self.hf_config.text_config.topk_method = "greedy" + + # When MTP is disabled, zero out MTP layer counts from hf_config so that + # downstream engine/worker code does not need to handle each MTP field format + # individually. Supports both DeepSeek-style (num_nextn_predict_layers) and + # Qwen3.5-style (mtp_num_hidden_layers, possibly nested under text_config). + if not self.mtp.enable: + if hasattr(self.hf_config, "num_nextn_predict_layers"): + self.hf_config.num_nextn_predict_layers = 0 + if hasattr(self.hf_config, "mtp_num_hidden_layers"): + self.hf_config.mtp_num_hidden_layers = 0 + if hasattr(self.hf_config, "text_config") and hasattr(self.hf_config.text_config, "mtp_num_hidden_layers"): + self.hf_config.text_config.mtp_num_hidden_layers = 0 + + # Ensure target_modules is a str or list[str] (only if not None) + if self.target_modules is not None: + if not isinstance(self.target_modules, (str | list)): + raise TypeError( + "target_modules must be a string or a list of strings, " + f"but got {type(self.target_modules).__name__}" + ) + if isinstance(self.target_modules, list): + for x in self.target_modules: + if not isinstance(x, str): + raise TypeError( + f"All elements in target_modules list must be strings, but found {type(x).__name__}" + ) + + def get_processor(self): + return self.processor if self.processor is not None else self.tokenizer diff --git a/verl_0720_main/verl/verl/workers/config/optimizer.py b/verl_0720_main/verl/verl/workers/config/optimizer.py new file mode 100644 index 0000000000000000000000000000000000000000..add9e236c8cb401abf54e8c0de4f24c746a25f7b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/optimizer.py @@ -0,0 +1,295 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import warnings +from dataclasses import dataclass +from typing import Optional + +from omegaconf import MISSING + +from verl.base_config import BaseConfig + +__all__ = [ + "OptimizerConfig", + "FSDPOptimizerConfig", + "McoreOptimizerConfig", + "build_optimizer", + "VeOmniOptimizerConfig", + "TorchtitanOptimizerConfig", + "AutomodelOptimizerConfig", +] + + +@dataclass +class OptimizerConfig(BaseConfig): + """Base optimizer configuration. + + Args: + lr (float): learning rate. Must be specified. + lr_warmup_steps_ratio (float): Warmup steps ratio; total steps will be injected at runtime. + total_training_steps (int): Total training steps (must be overridden at runtime). + weight_decay (float): Weight decay factor. + lr_warmup_steps (Optional[int]): Number of warmup steps; None delegates to lr_warmup_steps_ratio. + """ + + _mutable_fields = {"clip_grad", "total_training_steps", "lr_warmup_steps"} + + lr: float = 1e-3 + lr_warmup_steps_ratio: float = 0.0 + total_training_steps: int = -1 + weight_decay: float = 0.01 + lr_warmup_steps: Optional[int] = -1 + betas: tuple[float, float] = (0.9, 0.999) + clip_grad: float = 1.0 + # deprecate grad_clip + grad_clip: Optional[float] = None + + def __post_init__(self): + assert self.lr != MISSING + if self.grad_clip is not None: + warnings.warn("`grad_clip` is deprecated, use `clip_grad` instead.", DeprecationWarning, stacklevel=2) + self.clip_grad = self.grad_clip + + +@dataclass +class VeOmniOptimizerConfig(OptimizerConfig): + """VeOmni optimizer configuration extending base OptimizerConfig. + + Args: + optimizer (str): Optimizer name; default is "adamw". + lr (float): Learning rate. + lr_min (float): Minimum learning rate. + lr_start (float): Starting learning rate for warmup. + lr_decay_ratio (float): LR decay ratio. + lr_scheduler_type (str): LR scheduler type: "constant" or "cosine". + """ + + _mutable_fields = OptimizerConfig._mutable_fields.copy() + + optimizer: str = "adamw" + lr_min: float = 0.0 + lr_start: float = 0.0 + lr_decay_ratio: float = 1.0 + lr_scheduler_type: str = "constant" + override_optimizer_config: Optional[dict] = None + + +@dataclass +class FSDPOptimizerConfig(OptimizerConfig): + """FSDP optimizer configuration extending base OptimizerConfig. + + Args: + optimizer (str): Optimizer class name (e.g., "AdamW", "AdamW8bit", "_AdamW"). + optimizer_impl (str): Module path to import optimizer from (e.g., "torch.optim", "torchao.optim", + "bitsandbytes.optim"). + lr (float): Learning rate. + min_lr_ratio (Optional[float]): Minimum LR ratio for cosine schedule. + lr_scheduler_type (str): LR scheduler type: "constant" or "cosine". + num_cycles (float): Number of cosine cycles in LR schedule. + zero_indexed_step (bool): Whether the LR schedule uses 0-indexed steps. If True (default), + step counting starts at 0. If False, step counting starts at 1. + """ + + _mutable_fields = OptimizerConfig._mutable_fields.copy() + _mutable_fields.add("lr_scheduler_type") + + optimizer: str = "AdamW" + optimizer_impl: str = "torch.optim" + min_lr_ratio: Optional[float] = None + # deprecate warmup_style + warmup_style: Optional[str] = None + lr_scheduler_type: str = "constant" + num_cycles: float = 0.5 + override_optimizer_config: Optional[dict] = None + zero_indexed_step: bool = True + + def __post_init__(self): + if self.warmup_style is not None: + assert self.warmup_style in ["constant", "cosine"] + warnings.warn( + "`warmup_style` is deprecated, use `lr_scheduler_type` instead.", DeprecationWarning, stacklevel=2 + ) + self.lr_scheduler_type = self.warmup_style + assert self.lr_scheduler_type in ["constant", "cosine"] + return super().__post_init__() + + +@dataclass +class McoreOptimizerConfig(OptimizerConfig): + """Mcore optimizer configuration extending base OptimizerConfig. + + Args: + optimizer (str): Optimizer name; default is "adam". + lr (float): Learning rate. + clip_grad (float): Gradient clipping norm. + lr_warmup_init (float): Initial learning rate for warmup; defaults to 0.0. + lr_decay_steps (Optional[int]): Number of decay steps. + lr_decay_style (str): LR decay style: "constant", "linear", "cosine", or "inverse_square_root". + min_lr (float): Minimum learning rate. + weight_decay_incr_style (str): Weight decay increment style: "constant" or "cosine". + lr_wsd_decay_style (str): Weight-standard-deviation decay style: "constant", "exponential", or "cosine". + lr_wsd_decay_steps (Optional[int]): Number of steps for weight-standard-deviation decay. + use_checkpoint_opt_param_scheduler (bool): Whether to use checkpoint optimizer parameter scheduler. + use_precision_aware_optimizer (bool): Enable Megatron's precision-aware optimizer so the + grad-accumulation buffer and Adam moments can be stored below fp32 (bf16 training only). + Opt-in; default False keeps the fp32 optimizer state and prior numerics. Requires + TransformerEngine's FusedAdam. Mirrors Megatron's ``--use-precision-aware-optimizer``. + main_grads_dtype (str): dtype of the main-grad / grad-accumulation buffer when the + precision-aware optimizer is enabled ("fp32" or "bf16"). Also drives the DDP grad-bucket + dtype so the two stay consistent. Mirrors Megatron's ``--main-grads-dtype``. + exp_avg_dtype (str): dtype of the Adam first moment (m) when the precision-aware optimizer is + enabled ("fp32" or "bf16"). Mirrors Megatron's ``--exp-avg-dtype``. + exp_avg_sq_dtype (str): dtype of the Adam second moment (v) when the precision-aware optimizer + is enabled ("fp32" or "bf16"). Mirrors Megatron's ``--exp-avg-sq-dtype``. + """ + + optimizer: str = "adam" + lr_warmup_init: float = 0.0 + lr_decay_steps: Optional[int] = None + lr_decay_style: str = "linear" + min_lr: float = 0.0 + weight_decay_incr_style: str = "constant" + lr_wsd_decay_style: str = "exponential" + lr_wsd_decay_steps: Optional[int] = None + use_checkpoint_opt_param_scheduler: bool = False + use_precision_aware_optimizer: bool = False + main_grads_dtype: str = "fp32" + exp_avg_dtype: str = "fp32" + exp_avg_sq_dtype: str = "fp32" + override_optimizer_config: Optional[dict] = None + + def __post_init__(self): + allowed_dtypes = {"fp32", "float32", "32", "bf16", "bfloat16"} + for field_name in ("main_grads_dtype", "exp_avg_dtype", "exp_avg_sq_dtype"): + value = getattr(self, field_name) + assert str(value) in allowed_dtypes, ( + f"`{field_name}` must be one of {sorted(allowed_dtypes)}, got {value!r}" + ) + return super().__post_init__() + + +@dataclass +class TorchtitanOptimizerConfig(OptimizerConfig): + """Torchtitan optimizer configuration extending base OptimizerConfig. + + Args: + name (str): Optimizer name; default is "AdamW". + eps (float): Epsilon value for AdamW optimizer, default 1e-8. + decay_type (str): Weight decay type: "linear", "sqrt", or "cosine". + min_lr_factor (float): Minimum learning rate factor. + """ + + name: str = "AdamW" + eps: float = 1e-8 + decay_type: str = "linear" + min_lr_factor: float = 0.0 + + +@dataclass +class AutomodelOptimizerConfig(OptimizerConfig): + """Automodel optimizer configuration extending base OptimizerConfig. + + Uses the same optimizer building mechanism as FSDP (dynamic import from optimizer_impl). + LR scheduling is handled by Automodel's OptimizerParamScheduler. + + Args: + optimizer (str): Optimizer class name (e.g., "AdamW"). + optimizer_impl (str): Module path to import optimizer from (e.g., "torch.optim"). + lr (float): Learning rate (maps to max_lr in OptimizerParamScheduler). + init_lr_ratio (Optional[float]): Initial LR ratio for warmup start (init_lr = lr * init_lr_ratio). + min_lr_ratio (Optional[float]): Minimum LR ratio after decay (min_lr = lr * min_lr_ratio). + lr_scheduler_type (str): LR decay style: "constant", "cosine", "linear", or "inverse-square-root". + wd_incr_style (str): Weight decay increment style: "constant", "linear", or "cosine". + num_cycles (float): Kept for backward compatibility (unused by Automodel scheduler). + zero_indexed_step (bool): Kept for backward compatibility (unused by Automodel scheduler). + """ + + _mutable_fields = OptimizerConfig._mutable_fields.copy() + _mutable_fields.add("lr_scheduler_type") + + optimizer: str = "AdamW" + optimizer_impl: str = "torch.optim" + init_lr_ratio: Optional[float] = 0.1 + min_lr_ratio: Optional[float] = 0.01 + lr_scheduler_type: str = "cosine" + wd_incr_style: str = "constant" + num_cycles: float = 0.5 + zero_indexed_step: bool = True + # Common optimizer kwargs + eps: float = 1e-8 + master_weights: bool = False + store_param_remainders: bool = False + exp_avg_dtype: Optional[str] = None # "fp32", "bf16", "fp16", or "torch.float32" etc. + exp_avg_sq_dtype: Optional[str] = None # "fp32", "bf16", "fp16", or "torch.float32" etc. + master_weight_dtype: Optional[str] = None # "fp32", "bf16", "fp16", or "torch.float32" etc. + override_optimizer_config: Optional[dict] = None + + def __post_init__(self): + assert self.lr_scheduler_type in ["constant", "cosine", "linear", "inverse-square-root"] + return super().__post_init__() + + +def build_optimizer(parameters, config: FSDPOptimizerConfig): + """Build an optimizer based on the configuration. + + Dynamically imports and instantiates an optimizer class from the specified module. + + Args: + parameters: Model parameters to optimize + config: FSDPOptimizerConfig with optimizer settings + + Returns: + Optimizer instance + + Examples: + # PyTorch AdamW + config.optimizer_impl = "torch.optim" + config.optimizer = "AdamW" + + # TorchAO AdamW with bf16 stochastic rounding + config.optimizer_impl = "torchao.optim" + config.optimizer = "_AdamW" + config.override_optimizer_config = {"bf16_stochastic_round": True} + + # BitsAndBytes AdamW 8bit + config.optimizer_impl = "bitsandbytes.optim" + config.optimizer = "AdamW8bit" + """ + import importlib + + optimizer_args = { + "lr": config.lr, + "weight_decay": config.weight_decay, + } + + optimizer_name_lower = config.optimizer.lower() + if "adam" in optimizer_name_lower or "ademamix" in optimizer_name_lower: + optimizer_args["betas"] = config.betas + + if config.override_optimizer_config is not None: + optimizer_args.update(config.override_optimizer_config) + + try: + module = importlib.import_module(config.optimizer_impl) + optimizer_cls = getattr(module, config.optimizer) + except ImportError as e: + raise ImportError( + f"Failed to import module '{config.optimizer_impl}'. Make sure the package is installed. Error: {e}" + ) from e + except AttributeError as e: + raise AttributeError( + f"Optimizer '{config.optimizer}' not found in module '{config.optimizer_impl}'. " + f"Available optimizers: {dir(module)}" + ) from e + + return optimizer_cls(parameters, **optimizer_args) diff --git a/verl_0720_main/verl/verl/workers/config/reward.py b/verl_0720_main/verl/verl/workers/config/reward.py new file mode 100644 index 0000000000000000000000000000000000000000..3c3f4e269d1dc5ca0ab89b245785bd002ac0d00a --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/reward.py @@ -0,0 +1,105 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os +from dataclasses import dataclass, field +from typing import Optional + +from verl.base_config import BaseConfig +from verl.trainer.config.config import ModuleConfig + +from .rollout import RolloutConfig + +__all__ = ["SandboxFusionConfig", "RewardConfig", "RewardModelConfig"] + +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +@dataclass +class RewardManagerConfig(BaseConfig): + """Configuration for reward manager. + + A reward manager defines the mechanism of computing rule-based reward and handling different reward sources. + + Args: + source (str): Source of the reward manager. Options: ``"register"``, ``"importlib"``. Default: ``"register"``. + name (str, optional): + - When ``source`` is ``"register"``, the name is used in `get_reward_manager_cls(name)``. + See ``verl/experimental/reward/reward_manager.py`` for options. Default: ``"naive"``. + - When ``source`` is ``"importlib"``, the name is used in ``getattr(module, name)``, + e.g., ``"DAPORewardManager"``. + module (ModuleConfig, optional): Optional configuration for the external module defining the reward manager, + """ + + source: str = "register" + name: str = "naive" + module: Optional[ModuleConfig] = field(default_factory=ModuleConfig) + + def __post_init__(self): + super().__post_init__() + if self.source == "register": + from verl.experimental.reward_loop.reward_manager.registry import REWARD_MANAGER + + assert self.name in REWARD_MANAGER, ( + f"Reward manager is not registered: {self.name=} ,{REWARD_MANAGER.keys()=}" + ) + elif self.source == "importlib": + # NOTE: The existence is not checked since it depends on which machine the config is initialized on. + assert self.module is not None and self.module.path is not None, ( + "When source is importlib, module.path should be set." + ) + + +@dataclass +class SandboxFusionConfig(BaseConfig): + """Configuration for cloud/local sandbox fusion. + + Args: + url (Optional[str]): Cloud/local function URL for sandbox execution. + max_concurrent (int): Max concurrent requests allowed to sandbox. + memory_limit_mb (int): Max memory limit for each sandbox process in MB. + """ + + url: Optional[str] = None + max_concurrent: int = 64 + memory_limit_mb: int = 1024 + + +@dataclass +class RewardModelConfig(BaseConfig): + _mutable_fields = BaseConfig._mutable_fields + + enable: bool = False + enable_resource_pool: bool = False + n_gpus_per_node: int = 8 + nnodes: int = 0 + model_path: Optional[str] = None + rollout: RolloutConfig = field(default_factory=RolloutConfig) + + +@dataclass +class RewardConfig(BaseConfig): + _mutable_fields = BaseConfig._mutable_fields + + # reward manager args + num_workers: int = 8 + reward_manager: RewardManagerConfig = field(default_factory=RewardManagerConfig) + + # reward model args + reward_model: RewardModelConfig = field(default_factory=RewardModelConfig) + + # sandbox fusion args + sandbox_fusion: SandboxFusionConfig = field(default_factory=SandboxFusionConfig) diff --git a/verl_0720_main/verl/verl/workers/config/rollout.py b/verl_0720_main/verl/verl/workers/config/rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..2fa50c604d346a3a189fcc60cc715a0616f1cad6 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/config/rollout.py @@ -0,0 +1,350 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import warnings +from dataclasses import dataclass, field +from typing import Optional + +from omegaconf import MISSING, DictConfig, OmegaConf + +from verl.base_config import BaseConfig +from verl.utils.profiler import ProfilerConfig +from verl.workers.config.disaggregation import DisaggregationConfig +from verl.workers.config.model import MtpConfig + +__all__ = [ + "SamplingConfig", + "MultiTurnConfig", + "CustomAsyncServerConfig", + "AgentLoopConfig", + "TraceConfig", + "ServerConfig", + "PrometheusConfig", + "RolloutConfig", + "CheckpointEngineConfig", +] + + +@dataclass +class SamplingConfig(BaseConfig): + temperature: float = 1.0 + top_k: int = -1 + top_p: float = 1.0 + min_p: float = 0.0 + presence_penalty: float = 0.0 + frequency_penalty: float = 0.0 + repetition_penalty: float = 1.0 + do_sample: bool = True + n: int = 1 + + +@dataclass +class MultiTurnConfig(BaseConfig): + _mutable_fields = {"max_assistant_turns", "max_assistant_response_length", "max_user_turns"} + + enable: bool = False + max_assistant_turns: Optional[int] = None + max_assistant_response_length: Optional[int] = None + tool_config_path: Optional[str] = None + function_tool_path: Optional[str] = None + max_user_turns: Optional[int] = None + max_parallel_calls: int = 1 + max_tool_response_length: int = 256 + tool_response_truncate_side: str = "middle" + use_inference_chat_template: bool = False + tokenization_sanity_check_mode: str = "strict" + format: str = "hermes" + num_repeat_rollouts: Optional[int] = None + + +@dataclass +class CustomAsyncServerConfig(BaseConfig): + path: Optional[str] = None + name: Optional[str] = None + + +@dataclass +class AgentLoopConfig(BaseConfig): + num_workers: int = 8 + default_agent_loop: str = "single_turn_agent" + agent_loop_config_path: Optional[str] = None + custom_async_server: CustomAsyncServerConfig = field(default_factory=CustomAsyncServerConfig) + # Fully qualified class name for custom AgentLoopManager (e.g., "mypackage.module.MyManager"). + # Security: This class will be dynamically imported via importlib. Only use trusted class paths. + agent_loop_manager_class: Optional[str] = None + + +@dataclass +class TraceConfig(BaseConfig): + project_name: Optional[str] = None + experiment_name: Optional[str] = None + backend: Optional[str] = None + token2text: bool = False + max_samples_per_step_per_worker: Optional[int] = None + + def __post_init__(self): + if self.max_samples_per_step_per_worker is not None and self.max_samples_per_step_per_worker < 0: + raise ValueError("`max_samples_per_step_per_worker` must be a non-negative integer or null.") + + +@dataclass +class ServerConfig(BaseConfig): + """ + Configuration for SGLang server when running in server mode + """ + + timeout: float = 60.0 + max_attempts: int = 3 + retry_delay: float = 2.0 + max_connections: int = 1000 + max_start_wait_time: float = 300.0 + + +@dataclass +class PrometheusConfig(BaseConfig): + """ + Configuration for Prometheus server + """ + + # whether enable prometheus on server mode rollout + enable: bool = False + # Port number that Prometheus listens on, default is 9090 + port: int = 9090 + # Path to Prometheus configuration file + file: str = "/tmp/ray/session_latest/metrics/prometheus/prometheus.yml" + # Specify served_model_name to avoid displaying overly long model paths in Grafana + served_model_name: Optional[str] = None + + +@dataclass +class CheckpointEngineConfig(BaseConfig): + """ + Configuration for checkpoint engine to update weights from trainer to rollout + """ + + _mutable_fields = {"backend"} + + # Backend for checkpoint engine: naive, nccl, nixl, hccl + backend: Optional[str] = "naive" + # Bucket size in MB to transfer multiple weights at one time + update_weights_bucket_megabytes: int = 2048 + # Additional keyword arguments for checkpoint engine + engine_kwargs: dict = field(default_factory=dict) + # If set, this Python module is imported on every worker process before the + # backend is instantiated, allowing custom backends to register themselves + # in CheckpointEngineRegistry. + custom_backend_module: Optional[str] = None + + +@dataclass +class RolloutConfig(BaseConfig): + _mutable_fields = { + "max_model_len", + "load_format", + "engine_kwargs", + "prompt_length", + "response_length", + "expert_parallel_size", + "moe_tensor_parallel_size", + "full_determinism", + "max_num_seqs", + } + + name: Optional[str] = MISSING + mode: str = "async" + nnodes: int = 0 + n_gpus_per_node: int = 8 + + temperature: float = 1.0 + top_k: int = -1 + top_p: float = 1.0 + min_p: float = 0.0 + presence_penalty: float = 0.0 + frequency_penalty: float = 0.0 + do_sample: bool = True + n: int = 1 + repetition_penalty: float = 1.0 + + # Whether to enable full determinism for reproducibility. + full_determinism: bool = False + + # Random seed for rollout. Used as the seed for vLLM sampling and + # enable_full_determinism() when full_determinism is True. + seed: int = 42 + + # Early termination threshold for multi-turn rollout in sglang. + # Abort remaining requests when (1 - over_sample_rate) * total_requests are completed. + over_sample_rate: float = 0.0 + + prompt_length: int = 512 + response_length: int = 512 + + dtype: str = "bfloat16" + gpu_memory_utilization: float = 0.5 + standalone_gpu_memory_utilization: Optional[float] = None + ignore_eos: bool = False + enforce_eager: bool = False + cudagraph_capture_sizes: Optional[list] = None + free_cache_engine: bool = True + data_parallel_size: int = 1 + expert_parallel_size: int = 1 + tensor_model_parallel_size: int = 2 + pipeline_model_parallel_size: int = 1 + moe_tensor_parallel_size: int = 1 + max_num_batched_tokens: int = 8192 + logprobs_mode: Optional[str] = "processed_logprobs" + scheduling_policy: Optional[str] = "fcfs" + + # TODO: enable train_kwargs + # train_sampling_config: SamplingConfig = field(default_factory=SamplingConfig) + + val_kwargs: SamplingConfig = field(default_factory=SamplingConfig) + + max_model_len: Optional[int] = None + max_num_seqs: int = 1024 + + # note that the logprob computation should belong to the actor + log_prob_micro_batch_size: Optional[int] = None + log_prob_micro_batch_size_per_gpu: Optional[int] = None + log_prob_use_dynamic_bsz: bool = False + log_prob_max_token_len_per_gpu: int = 16384 + + disable_log_stats: bool = True + + multi_stage_wake_up: bool = False + engine_kwargs: dict = field(default_factory=dict) + + calculate_log_probs: bool = False + + agent: AgentLoopConfig = field(default_factory=AgentLoopConfig) + + trace: TraceConfig = field(default_factory=TraceConfig) + + multi_turn: MultiTurnConfig = field(default_factory=MultiTurnConfig) + + # Server configuration for sglang server mode + server: ServerConfig = field(default_factory=ServerConfig) + + # Use Prometheus to collect and monitor rollout statistics + prometheus: PrometheusConfig = field(default_factory=PrometheusConfig) + + # Extension point for custom configurations + custom: Optional[dict] = None + + # Fully qualified class name for a custom CheckpointEngineManager. When set, the trainer + # loads this class instead of the built-in CheckpointEngineManager. + checkpoint_manager_class: Optional[str] = None + + # Checkpoint Engine config for update weights from trainer to rollout + checkpoint_engine: CheckpointEngineConfig = field(default_factory=CheckpointEngineConfig) + + profiler: Optional[ProfilerConfig] = None + + enable_chunked_prefill: bool = True + + enable_prefix_caching: bool = True + + load_format: str = "dummy" + + layered_summon: bool = False + + layer_name_map: dict = field(default_factory=dict) + + sglang_engine_mode: str = "local" + + limit_images: Optional[int] = None + + skip_tokenizer_init: bool = True + + quantization: Optional[str] = None + + quantization_config_file: Optional[str] = None + + enable_rollout_routing_replay: bool = False + moe_load_balance_metrics_interval: int = 0 + + enable_sleep_mode: bool = True + + mtp: MtpConfig = field(default_factory=MtpConfig) + + qat: Optional[dict] = None + + disaggregation: DisaggregationConfig = field(default_factory=DisaggregationConfig) + + def __post_init__(self): + """Validate the rollout config""" + # Deprecation warning for mode field - only async mode is supported + if self.mode == "sync": + raise ValueError( + "Rollout mode 'sync' has been removed. Please set " + "`actor_rollout_ref.rollout.mode=async` or remove the mode setting entirely." + ) + if self.mode != "async": + warnings.warn( + f"Unknown rollout mode '{self.mode}'. Only 'async' mode is supported. " + "The 'mode' field is deprecated and will be removed in a future version.", + DeprecationWarning, + stacklevel=2, + ) + + if self.name != "trtllm" and self.expert_parallel_size > 1: + assert self.expert_parallel_size == (self.tensor_model_parallel_size * self.data_parallel_size), ( + "expert_parallel_size must be equal to tensor_model_parallel_size * data_parallel_size" + ) + + if self.moe_tensor_parallel_size is not None and self.moe_tensor_parallel_size > 1: + assert self.name == "trtllm", "moe_tensor_parallel_size is only supported for trtllm" + + if self.name == "trtllm": + # If either expert_parallel_size or moe_tensor_parallel_size is at default 1, + # convert to None so TensorRT-LLM treats it as unspecified. + # When both unspecified: moe_ep_size=1, moe_tp_size=moe_world_size (no EP, all TP). + # When only one set: the other is auto-derived from tensor_model_parallel_size. + if self.expert_parallel_size is not None and self.expert_parallel_size == 1: + self.expert_parallel_size = None + if self.moe_tensor_parallel_size is not None and self.moe_tensor_parallel_size == 1: + self.moe_tensor_parallel_size = None + if self.expert_parallel_size is not None and self.moe_tensor_parallel_size is not None: + assert self.moe_tensor_parallel_size * self.expert_parallel_size == self.tensor_model_parallel_size, ( + "moe_tensor_parallel_size * expert_parallel_size must equal tensor_model_parallel_size " + f"(got {self.moe_tensor_parallel_size} * {self.expert_parallel_size} = " + f"{self.moe_tensor_parallel_size * self.expert_parallel_size}, " + f"tensor_model_parallel_size={self.tensor_model_parallel_size})" + ) + + if self.pipeline_model_parallel_size > 1: + if self.name == "vllm" or self.name == "sglang" or self.name == "trtllm": + raise NotImplementedError( + f"Current rollout {self.name=} not implemented pipeline_model_parallel_size > 1 yet." + ) + + # Hydra passes this as dict/DictConfig; coerce to dataclass so + # downstream .enabled etc. work. BaseConfig is frozen, hence object.__setattr__. + if isinstance(self.disaggregation, dict): + object.__setattr__(self, "disaggregation", DisaggregationConfig(**self.disaggregation)) + elif not isinstance(self.disaggregation, DisaggregationConfig): + if not isinstance(self.disaggregation, DictConfig): + raise TypeError( + f"rollout.disaggregation must be dict, DictConfig, or DisaggregationConfig; " + f"got {type(self.disaggregation).__name__}." + ) + object.__setattr__( + self, + "disaggregation", + DisaggregationConfig(**OmegaConf.to_container(self.disaggregation, resolve=True)), + ) + + if self.disaggregation.enabled and self.name not in ("sglang", "vllm"): + raise ValueError( + f"rollout.disaggregation.enabled=True requires rollout.name in ('sglang', 'vllm'); got {self.name!r}." + ) diff --git a/verl_0720_main/verl/verl/workers/engine/__init__.py b/verl_0720_main/verl/verl/workers/engine/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..247f743b08738196aa03d2137f4bac2f33193c08 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/__init__.py @@ -0,0 +1,64 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from .base import BaseEngine, EngineRegistry +from .fsdp import FSDPEngine, FSDPEngineWithLMHead + +__all__ = [ + "BaseEngine", + "EngineRegistry", + "FSDPEngine", + "FSDPEngineWithLMHead", +] + +try: + from .torchtitan import TorchTitanEngine, TorchTitanEngineWithLMHead + + __all__ += ["TorchTitanEngine", "TorchTitanEngineWithLMHead"] +except ImportError: + TorchTitanEngine = None + TorchTitanEngineWithLMHead = None + +try: + from .veomni import VeOmniEngine, VeOmniEngineWithLMHead + + __all__ += ["VeOmniEngine", "VeOmniEngineWithLMHead"] +except ImportError: + VeOmniEngine = None + VeOmniEngineWithLMHead = None + +try: + from .automodel import AutomodelEngine, AutomodelEngineWithLMHead + + __all__ += ["AutomodelEngine", "AutomodelEngineWithLMHead"] +except ImportError: + AutomodelEngine = None + AutomodelEngineWithLMHead = None + +# Mindspeed must be imported before Megatron to ensure the related monkey patches take effect as expected +try: + from .mindspeed import MindspeedEngineWithLMHead, MindspeedEngineWithValueHead, MindSpeedMegatronEngineWithLMHead + + __all__ += ["MindspeedEngineWithLMHead", "MindspeedEngineWithValueHead", "MindSpeedMegatronEngineWithLMHead"] +except ImportError: + MindspeedEngineWithLMHead = None + MindspeedEngineWithValueHead = None + MindSpeedMegatronEngineWithLMHead = None + +try: + from .megatron import MegatronEngine, MegatronEngineWithLMHead, MegatronEngineWithValueHead + + __all__ += ["MegatronEngine", "MegatronEngineWithLMHead", "MegatronEngineWithValueHead"] +except ImportError: + MegatronEngine = None + MegatronEngineWithLMHead = None diff --git a/verl_0720_main/verl/verl/workers/engine/automodel/__init__.py b/verl_0720_main/verl/verl/workers/engine/automodel/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..a839342706ba5be7fe7f748d2db291ca467ae4c3 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/automodel/__init__.py @@ -0,0 +1,20 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .transformer_impl import AutomodelEngine, AutomodelEngineWithLMHead + +__all__ = [ + "AutomodelEngine", + "AutomodelEngineWithLMHead", +] diff --git a/verl_0720_main/verl/verl/workers/engine/automodel/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/automodel/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..dc9171761e6d258c290d27f24bbb7f251d6b2283 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/automodel/transformer_impl.py @@ -0,0 +1,720 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Automodel (nemo_automodel) engine for verl SFT training. + +This engine delegates model building, parallelization, optimizer sharding, +LR scheduling, gradient clipping, and checkpointing to Automodel's +infrastructure while using verl's training loop, data pipeline, and loss function. +""" + +import gc +import logging +import os +from contextlib import nullcontext +from typing import Any, Callable, Optional + +import torch +import torch.distributed +from huggingface_hub.constants import HF_HUB_CACHE +from nemo_automodel.components.checkpoint.checkpointing import Checkpointer, CheckpointingConfig +from nemo_automodel.components.optim.scheduler import OptimizerParamScheduler +from nemo_automodel.components.training.utils import ( + prepare_for_final_backward, + prepare_for_grad_accumulation, + scale_grads_and_clip_grad_norm, +) +from tensordict import TensorDict +from torch.distributed.tensor import DTensor + +import verl.utils.torch_functional as verl_F +from verl.trainer.config import CheckpointConfig +from verl.utils import tensordict_utils as tu +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.debug import log_gpu_memory_usage +from verl.utils.device import get_device_id, get_device_name +from verl.utils.model import convert_weight_keys, extract_multi_modal_inputs +from verl.utils.torch_functional import logprobs_from_logits +from verl.workers.config import AutomodelEngineConfig, AutomodelOptimizerConfig, HFModelConfig + +from ..base import BaseEngine, BaseEngineCtx, EngineRegistry +from ..utils import enable_full_determinism, postprocess_batch_func, prepare_micro_batches +from .utils import ( + build_automodel_model, + build_distributed_config_from_engine_config, + get_dp_group_size, + get_dp_rank, + get_pp_rank, + get_tp_rank, + load_automodel_model_to_gpu, + load_automodel_optimizer, + maybe_fully_shard_optimizer, + offload_automodel_model_to_cpu, + offload_automodel_optimizer, +) + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +class AutomodelEngine(BaseEngine): + """Engine implementation using Automodel for distributed training.""" + + def __init__( + self, + model_config: HFModelConfig, + engine_config: AutomodelEngineConfig, + optimizer_config: AutomodelOptimizerConfig, + checkpoint_config: CheckpointConfig, + **kwargs, + ): + super().__init__() + + self.model_config = model_config + self.engine_config = engine_config + self.optimizer_config = optimizer_config + self.checkpoint_config = checkpoint_config + + self.mode = None + self.rank = torch.distributed.get_rank() + + # Apply compatibility patches early in the process + from nemo_automodel._transformers.utils import apply_cache_compatibility_patches + from nemo_automodel.shared.te_patches import apply_te_patches + + apply_cache_compatibility_patches() + apply_te_patches() + + world_size = torch.distributed.get_world_size() + self.distributed_config, self.device_mesh, self.moe_mesh = build_distributed_config_from_engine_config( + self.engine_config, world_size + ) + + if self.engine_config.full_determinism: + enable_full_determinism(seed=self.engine_config.seed) + + self._is_offload_param = self.engine_config.param_offload + self._is_offload_optimizer = self.engine_config.optimizer_offload + + if self.engine_config.entropy_from_logits_with_chunking: + entropy_from_logits = verl_F.entropy_from_logits_with_chunking + else: + entropy_from_logits = verl_F.entropy_from_logits + + self.compute_entropy_from_logits = ( + torch.compile(entropy_from_logits, dynamic=True) + if self.engine_config.use_torch_compile + else entropy_from_logits + ) + + @property + def is_param_offload_enabled(self) -> bool: + return self._is_offload_param + + @property + def is_optimizer_offload_enabled(self) -> bool: + return self._is_offload_optimizer + + def initialize(self): + """Build the model, optimizer, LR scheduler, and checkpointer using Automodel infrastructure.""" + self.module = build_automodel_model( + self.model_config, self.engine_config, self.distributed_config, self.device_mesh, self.moe_mesh + ) + log_gpu_memory_usage("After Automodel model build", logger=logger) + + if not self.engine_config.forward_only: + self.optimizer = self._build_optimizer(self.module) + # maybe shard optimizer for MegatronFSDP + maybe_fully_shard_optimizer(self.module, self.optimizer, self.distributed_config) + self.lr_scheduler = self._build_lr_scheduler(self.optimizer) + else: + self.optimizer = None + self.lr_scheduler = None + self._build_checkpointer() + + self.to( + device="cpu", + model=self._is_offload_param, + optimizer=self._is_offload_optimizer, + grad=self._is_offload_param, + ) + + log_gpu_memory_usage("After offload model/optimizer/grad during init", logger=logger) + torch.cuda.empty_cache() + + def _build_optimizer(self, module): + """Build optimizer via Automodel's build_optimizer.""" + from nemo_automodel.components.config.loader import ConfigNode + from nemo_automodel.recipes.llm.train_ft import build_optimizer as automodel_build_optimizer + + config = self.optimizer_config + + opt_dict = { + "_target_": f"{config.optimizer_impl}.{config.optimizer}", + "lr": config.lr, + "weight_decay": config.weight_decay, + "eps": config.eps, + "betas": list(config.betas), + } + + if config.master_weights: + opt_dict["master_weights"] = config.master_weights + if config.store_param_remainders: + opt_dict["store_param_remainders"] = config.store_param_remainders + + _short_to_torch = {"bf16": "torch.bfloat16", "fp32": "torch.float32", "fp16": "torch.float16"} + for attr in ("exp_avg_dtype", "exp_avg_sq_dtype", "master_weight_dtype"): + val = getattr(config, attr, None) + if val is not None: + opt_dict[attr] = _short_to_torch.get(val, val) + + if config.override_optimizer_config: + opt_dict.update(config.override_optimizer_config) + + cfg_opt = ConfigNode(opt_dict) + optimizers = automodel_build_optimizer(module, cfg_opt, self.distributed_config, self.device_mesh) + assert len(optimizers) == 1, f"Expected 1 optimizer, got {len(optimizers)}" + return optimizers[0] + + def _build_lr_scheduler(self, optimizer): + cfg = self.optimizer_config + total_steps = cfg.total_training_steps + num_warmup_steps = cfg.lr_warmup_steps + + if num_warmup_steps <= 0: + num_warmup_steps = int(cfg.lr_warmup_steps_ratio * total_steps) + + base_lr = cfg.lr + init_lr_ratio = cfg.init_lr_ratio if cfg.init_lr_ratio is not None else 0.1 + min_lr_ratio = cfg.min_lr_ratio if cfg.min_lr_ratio is not None else 0.01 + + if self.rank == 0: + print( + f"Automodel LR Scheduler: total_steps={total_steps}, warmup={num_warmup_steps}, " + f"decay_style={cfg.lr_scheduler_type}, init_lr={base_lr * init_lr_ratio:.2e}, " + f"max_lr={base_lr:.2e}, min_lr={base_lr * min_lr_ratio:.2e}" + ) + + scheduler = OptimizerParamScheduler( + optimizer=optimizer, + init_lr=base_lr * init_lr_ratio, + max_lr=base_lr, + min_lr=base_lr * min_lr_ratio, + lr_warmup_steps=num_warmup_steps, + lr_decay_steps=total_steps, + lr_decay_style=cfg.lr_scheduler_type, + start_wd=cfg.weight_decay, + end_wd=cfg.weight_decay, + wd_incr_steps=total_steps, + wd_incr_style=getattr(cfg, "wd_incr_style", "constant"), + ) + return scheduler + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> Any: + batch_num_tokens = data["loss_mask"].sum().to(get_device_id()) + torch.distributed.all_reduce( + batch_num_tokens, op=torch.distributed.ReduceOp.SUM, group=self.get_data_parallel_group() + ) + tu.assign_non_tensor(data, batch_num_tokens=batch_num_tokens.item()) + tu.assign_non_tensor(data, dp_size=self.get_data_parallel_size()) + + micro_batches, indices = prepare_micro_batches( + data=data, dp_group=self.get_data_parallel_group(), same_micro_num_in_dp=True + ) + + output_lst = [] + ctx = torch.no_grad() if forward_only else nullcontext() + + if not forward_only: + prepare_for_grad_accumulation([self.module]) + + # Set MoE aux loss backward scale to counteract FSDP's gradient allreduce. + if self.engine_config.ep_size > 1: + from nemo_automodel.components.moe.megatron.moe_utils import MoEAuxLossAutoScaler + + MoEAuxLossAutoScaler.main_loss_backward_scale = torch.tensor( + float(get_dp_group_size(self.device_mesh, include_cp=True)) + ) + + num_micro_batches = len(micro_batches) + for i, micro_batch in enumerate(micro_batches): + # Signal final backward for MoE + if not forward_only and i == num_micro_batches - 1: + prepare_for_final_backward([self.module]) + + with ctx: + loss, meta_info = self.forward_step(micro_batch, loss_function=loss_function, forward_only=forward_only) + if not forward_only: + loss.backward() + output_lst.append(meta_info) + + return postprocess_batch_func(output_lst=output_lst, indices=indices, data=data) + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + raise NotImplementedError("forward_step must be implemented in subclass") + + def optimizer_zero_grad(self): + self.optimizer.zero_grad() + + def optimizer_step(self): + grad_norm = scale_grads_and_clip_grad_norm( + max_grad_norm=self.optimizer_config.clip_grad, + model_parts=[self.module], + norm_type=2.0, + pp_enabled=False, + device_mesh=self.device_mesh, + moe_mesh=self.moe_mesh, + ep_axis_name="ep" if self.moe_mesh is not None and "ep" in self.moe_mesh.mesh_dim_names else None, + pp_axis_name=None, + foreach=True, + num_label_tokens=None, + dp_group_size=get_dp_group_size(self.device_mesh, include_cp=True), + ) + + if isinstance(grad_norm, torch.Tensor): + grad_norm_val = grad_norm.item() + else: + grad_norm_val = float(grad_norm) + + # If grad_norm is not finite, skip the update + if not torch.isfinite(torch.tensor(grad_norm_val)): + print(f"WARN: grad_norm is not finite: {grad_norm_val}") + self.optimizer.zero_grad() + else: + self.optimizer.step() + if hasattr(self.module, "update_moe_gate_bias"): + self.module.update_moe_gate_bias() + + return grad_norm_val + + def lr_scheduler_step(self): + """Step Automodel's OptimizerParamScheduler and return current LR.""" + self.lr_scheduler.step(increment=1) + lr = self.optimizer.param_groups[0]["lr"] + return lr + + def get_data_parallel_rank(self): + if self.device_mesh is not None: + return self.device_mesh.get_local_rank("dp") + return torch.distributed.get_rank() + + def get_data_parallel_size(self): + if self.device_mesh is not None: + return self.device_mesh["dp"].size() + return torch.distributed.get_world_size() + + def get_data_parallel_group(self): + if self.device_mesh is not None: + return self.device_mesh.get_group(mesh_dim="dp") + return torch.distributed.group.WORLD + + def is_mp_src_rank_with_outputs(self): + if self.device_mesh is not None and "tp" in self.device_mesh.mesh_dim_names: + if self.device_mesh["tp"].size() > 1: + return self.device_mesh.get_local_rank("tp") == 0 + return True + + def train_mode(self, **kwargs): + return AutomodelTrainModeCtx(self, **kwargs) + + def eval_mode(self, **kwargs): + return AutomodelEvalModeCtx(self, **kwargs) + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + super().to(device=device, model=model, optimizer=optimizer, grad=grad) + + if self.engine_config.forward_only: + return + + device_name = get_device_name() + assert device in (device_name, "cpu") + + if device == device_name: + if model: + load_automodel_model_to_gpu(self.module) + if optimizer and self.optimizer is not None: + load_automodel_optimizer(self.optimizer, get_device_id()) + gc.collect() + elif device == "cpu": + if model: + offload_automodel_model_to_cpu(self.module) + if optimizer and self.optimizer is not None: + offload_automodel_optimizer(self.optimizer) + else: + raise ValueError(f"Invalid device type: {device}") + + def _build_checkpointer(self): + ckpt_config = CheckpointingConfig( + enabled=True, + checkpoint_dir="checkpoints/", + model_save_format="safetensors", + model_cache_dir=HF_HUB_CACHE, + model_repo_id=self.model_config.path, + save_consolidated=True, + is_peft=False, + ) + self.checkpointer = Checkpointer( + config=ckpt_config, + dp_rank=get_dp_rank(self.device_mesh, include_cp=True), + tp_rank=get_tp_rank(self.device_mesh), + pp_rank=get_pp_rank(self.device_mesh), + moe_mesh=self.moe_mesh, + ) + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """Save model, optimizer, and LR scheduler using Automodel's Checkpointer.""" + origin_module_device = next(self.module.parameters()).device.type + if self._is_offload_param or origin_module_device == "cpu": + load_automodel_model_to_gpu(self.module) + + # Save model weights + self.checkpointer.save_model(self.module, local_path) + + # Save optimizer and LR scheduler state + if self.optimizer is not None: + scheduler_list = [self.lr_scheduler] if self.lr_scheduler is not None else None + self.checkpointer.save_optimizer(self.optimizer, self.module, local_path, scheduler=scheduler_list) + + torch.distributed.barrier() + if self._is_offload_param: + offload_automodel_model_to_cpu(self.module) + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: int = True, **kwargs + ) -> None: + """Load model, optimizer, and LR scheduler using Automodel's Checkpointer.""" + if self._is_offload_param: + load_automodel_model_to_gpu(self.module) + + model_path = os.path.join(local_path, "model") + if not os.path.isdir(model_path): + model_path = local_path + self.checkpointer.load_model(self.module, model_path) + + if self.optimizer is not None: + scheduler_list = [self.lr_scheduler] if self.lr_scheduler is not None else None + self.checkpointer.load_optimizer(self.optimizer, self.module, local_path, scheduler=scheduler_list) + + torch.distributed.barrier() + if self._is_offload_param: + offload_automodel_model_to_cpu(self.module) + + if self._is_offload_optimizer and self.optimizer is not None: + offload_automodel_optimizer(self.optimizer) + + def get_per_tensor_param(self, **kwargs): + load_automodel_model_to_gpu(self.module) + + params = self.module.state_dict() + params = convert_weight_keys(params, getattr(self.module, "_fsdp_wrapped_module", self.module)) + + if self._is_offload_param: + offload_automodel_model_to_cpu(self.module) + + def param_generator(): + for name, param in params.items(): + unsharded_tensor = param.full_tensor() if isinstance(param, DTensor) else param + yield name, unsharded_tensor + + return param_generator(), None + + +class AutomodelEvalModeCtx(BaseEngineCtx): + def __init__(self, engine: AutomodelEngine, **kwargs): + super().__init__(engine=engine, mode="eval", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, AutomodelEngine) + super().__enter__() + self.engine.module.eval() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, AutomodelEngine) + # Reshard the root FSDP module + if hasattr(self.engine.module, "reshard"): + self.engine.module.reshard() + super().__exit__(exc_type, exc_value, traceback) + + +class AutomodelTrainModeCtx(BaseEngineCtx): + def __init__(self, engine: AutomodelEngine, **kwargs): + super().__init__(engine=engine, mode="train", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, AutomodelEngine) + super().__enter__() + self.engine.module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, AutomodelEngine) + if self.zero_grad_on_exit or exc_type is not None: + self.engine.optimizer_zero_grad() + super().__exit__(exc_type, exc_value, traceback) + + +@EngineRegistry.register(model_type="language_model", backend=["automodel"], device=["cuda"]) +class AutomodelEngineWithLMHead(AutomodelEngine): + """Automodel engine for language model with LM head training.""" + + def prepare_model_inputs(self, micro_batch: TensorDict): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + use_fused_kernels = tu.get_non_tensor_data(data=micro_batch, key="use_fused_kernels", default=False) + temperature = micro_batch["temperature"] + temperature_item = temperature + if use_fused_kernels: + assert not isinstance(temperature, torch.Tensor), ( + "use_fused_kernels does not support per sample temperature yet" + ) + assert pad_mode == DatasetPadMode.NO_PADDING, f"pad_mode {pad_mode} not supported" + + multi_modal_inputs = extract_multi_modal_inputs(micro_batch.get("multi_modal_inputs", [])) + input_ids = micro_batch["input_ids"] + position_ids = micro_batch["position_ids"] + + if not isinstance(temperature, torch.Tensor): + temperature = torch.tensor([temperature] * input_ids.shape[0], device=input_ids.device) + + temperature = temperature.to(torch.float32) + assert temperature.shape[0] == input_ids.shape[0] + + output_args = {} + + if use_remove_padding: + temperature_rmpad = verl_F.expand_as_nested(temperature, input_ids).values() + temperature_rmpad = temperature_rmpad.unsqueeze(0) + + if pad_mode == DatasetPadMode.NO_PADDING: + input_ids_rmpad = input_ids.values().unsqueeze(0) + if position_ids.dim() == 3: + position_ids_rmpad = position_ids.values().unsqueeze(1) + else: + position_ids_rmpad = position_ids.values().unsqueeze(0) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + input_ids_rmpad_rolled = torch.roll(input_ids_rmpad, shifts=-1, dims=1) + + input_ids_rmpad_rolled = input_ids_rmpad_rolled.squeeze(0) + temperature_rmpad = temperature_rmpad.squeeze(0) + output_args["input_ids_rmpad_rolled"] = input_ids_rmpad_rolled + output_args["temperature_rmpad"] = temperature_rmpad + + model_inputs = { + "input_ids": input_ids_rmpad, + "attention_mask": None, + "position_ids": position_ids_rmpad, + } + + # For TE attention backend, pass cu_seqlens + if self.engine_config.attn_implementation == "te": + cu_seqlens = input_ids.offsets().to(torch.int32) + max_seqlen = cu_seqlens.diff().max().item() + model_inputs["qkv_format"] = "thd" + model_inputs["cu_seqlens"] = cu_seqlens.unsqueeze(0) + model_inputs["max_seqlen"] = max_seqlen + + else: + if pad_mode == DatasetPadMode.NO_PADDING: + input_ids = micro_batch["input_ids"] + position_ids = micro_batch["position_ids"] + loss_mask = micro_batch["loss_mask"] + + pad_token_id = tu.get_non_tensor_data(data=micro_batch, key="pad_token_id", default=0) + batch_size = micro_batch.batch_size[0] + seq_len_effective = input_ids.offsets().diff() + max_seq_len = max(seq_len_effective) + + input_ids_rmpad_rolled = torch.roll(input_ids.values(), shifts=-1, dims=0) + output_args["input_ids_rmpad_rolled"] = input_ids_rmpad_rolled + output_args["temperature"] = temperature + + input_ids = torch.nested.to_padded_tensor( + input_ids, padding=pad_token_id, output_size=(batch_size, max_seq_len) + ) + + if position_ids.dim() == 3: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, 4, max_seq_len) + ).transpose(0, 1) + else: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, max_seq_len) + ) + + attention_mask_list = [torch.ones_like(t, dtype=torch.int32) for t in loss_mask] + attention_mask = torch.nested.as_nested_tensor(attention_mask_list, layout=torch.jagged) + attention_mask = torch.nested.to_padded_tensor( + attention_mask, padding=0, output_size=(batch_size, max_seq_len) + ) + + model_inputs = { + "input_ids": input_ids, + "attention_mask": attention_mask, + "position_ids": position_ids, + } + + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + extra_args = {} + if use_fused_kernels: + extra_args["temperature"] = temperature_item + extra_args["return_dict"] = True + + model_inputs.update(multi_modal_inputs) + model_inputs.update(extra_args) + + return model_inputs, output_args + + def prepare_model_outputs(self, output, output_args, micro_batch: TensorDict): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + use_fused_kernels = tu.get_non_tensor_data(data=micro_batch, key="use_fused_kernels", default=False) + calculate_entropy = tu.get_non_tensor_data(data=micro_batch, key="calculate_entropy", default=False) + + if isinstance(output, torch.Tensor): + from types import SimpleNamespace + + output = SimpleNamespace(logits=output) + + model_output = {} + input_ids = micro_batch["input_ids"] + + if use_remove_padding: + input_ids_rmpad_rolled = output_args["input_ids_rmpad_rolled"] + temperature_rmpad = output_args["temperature_rmpad"] + + if use_fused_kernels: + log_probs = output.log_probs.squeeze(0) + entropy_rmpad = output.entropy.squeeze(0) + else: + logits_rmpad = output.logits.squeeze(0) + # With TP, logits are DTensors sharded on vocab dim; gather for log_softmax. + if isinstance(logits_rmpad, DTensor): + logits_rmpad = logits_rmpad.full_tensor() + logits_rmpad = logits_rmpad / temperature_rmpad.clamp(min=1e-8).unsqueeze(-1).to(logits_rmpad.dtype) + + inplace_backward = True + if calculate_entropy: + inplace_backward = False + log_probs = logprobs_from_logits( + logits=logits_rmpad, + labels=input_ids_rmpad_rolled, + inplace_backward=inplace_backward, + ) + + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + if self.engine_config.entropy_from_logits_with_chunking: + entropy_rmpad = self.compute_entropy_from_logits( + logits_rmpad, + chunk_size=self.engine_config.entropy_from_logits_chunk_size, + ) + else: + entropy_rmpad = self.compute_entropy_from_logits(logits_rmpad) + else: + entropy_rmpad = torch.utils.checkpoint.checkpoint( + self.compute_entropy_from_logits, logits_rmpad + ) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + if calculate_entropy: + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + else: + response_length = tu.get_non_tensor_data(data=micro_batch, key="max_response_length", default=1024) + if use_fused_kernels: + log_probs = output.log_probs[:, -response_length - 1 : -1] + entropy = output.entropy[:, -response_length - 1 : -1] + else: + logits = output.logits + # With TP, logits are DTensors sharded on vocab dim; gather for log_softmax. + if isinstance(logits, DTensor): + logits = logits.full_tensor() + temperature = output_args["temperature"] + temperature = temperature.unsqueeze(-1).unsqueeze(-1) + logits = logits / temperature.clamp(min=1e-8).to(logits.dtype) + + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + entropy = verl_F.entropy_from_logits(logits) + else: + entropy = torch.utils.checkpoint.checkpoint(verl_F.entropy_from_logits, logits) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + seq_lengths = cu_seqlens.diff() + starts = torch.zeros_like(seq_lengths, dtype=torch.int64) + logits = torch.nested.narrow(logits, 1, starts, seq_lengths, layout=torch.jagged) + logits_rmpad = torch.cat([t for t in logits.unbind()]) + input_ids_rmpad_rolled = output_args["input_ids_rmpad_rolled"] + log_probs = logprobs_from_logits(logits=logits_rmpad, labels=input_ids_rmpad_rolled) + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + if calculate_entropy: + entropy = torch.nested.narrow(entropy, 1, starts, seq_lengths, layout=torch.jagged) + entropy_rmpad = torch.cat([t for t in entropy.unbind()]) + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + model_output["log_probs"] = log_probs + if calculate_entropy: + model_output["entropy"] = entropy + + return model_output + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + """Run forward pass, compute loss, and return outputs.""" + device_name = get_device_name() + micro_batch = micro_batch.to(get_device_id()) + model_inputs, output_args = self.prepare_model_inputs(micro_batch=micro_batch) + + with torch.autocast(device_type=device_name, dtype=torch.bfloat16): + raw_output = self.module( + **model_inputs, + use_cache=False, + ) + + model_output = self.prepare_model_outputs( + output=raw_output, output_args=output_args, micro_batch=micro_batch + ) + + if loss_function is not None: + loss, metrics = loss_function( + model_output=model_output, data=micro_batch, dp_group=self.get_data_parallel_group() + ) + else: + assert forward_only, "forward_only must be True when loss_function is None" + loss = torch.tensor(1.0, device=device_name) + metrics = {} + + output = { + "model_output": model_output, + "loss": loss.detach().item(), + "metrics": metrics, + } + + return loss, output diff --git a/verl_0720_main/verl/verl/workers/engine/automodel/utils.py b/verl_0720_main/verl/verl/workers/engine/automodel/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..c10cf9a2db2bcd67473a78e5df0ce3f3570984fb --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/automodel/utils.py @@ -0,0 +1,250 @@ +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Utility functions for the Automodel engine integration.""" + +import torch +import torch.distributed + +from verl.utils.device import get_device_id, get_torch_device + + +def get_dp_rank(device_mesh, include_cp=False): + """Get data-parallel rank from device mesh.""" + if device_mesh is None: + return 0 + if include_cp and "cp" in device_mesh.mesh_dim_names and device_mesh["cp"].size() > 1: + return device_mesh.get_local_rank("dp_cp") + return device_mesh.get_local_rank("dp") + + +def get_tp_rank(device_mesh): + """Get tensor-parallel rank from device mesh.""" + if device_mesh is None or "tp" not in device_mesh.mesh_dim_names or device_mesh["tp"].size() == 1: + return 0 + return device_mesh.get_local_rank("tp") + + +def get_pp_rank(device_mesh): + """Get pipeline-parallel rank from device mesh.""" + if device_mesh is None or "pp" not in device_mesh.mesh_dim_names or device_mesh["pp"].size() == 1: + return 0 + return device_mesh.get_local_rank("pp") + + +def get_dp_group_size(device_mesh, include_cp=False): + """Get data-parallel group size from device mesh.""" + if device_mesh is None: + return torch.distributed.get_world_size() + if include_cp and "cp" in device_mesh.mesh_dim_names and device_mesh["cp"].size() > 1: + return device_mesh["dp_cp"].size() + if "dp" in device_mesh.mesh_dim_names: + return device_mesh["dp"].size() + return torch.distributed.get_world_size() + + +def maybe_fully_shard_optimizer(model, optimizer, distributed_config): + """Call fully_shard_optimizer for MegatronFSDP strategy.""" + from nemo_automodel.components.distributed.config import MegatronFSDPConfig + + if isinstance(distributed_config, MegatronFSDPConfig) and torch.distributed.get_world_size() > 1: + from megatron_fsdp.fully_shard import fully_shard_optimizer + + fully_shard_optimizer(model, optimizer) + + +def build_distributed_config_from_engine_config(engine_config, world_size): + """Build v5 distributed config, device_mesh, and moe_mesh from engine config. + + Args: + engine_config: AutomodelEngineConfig instance. + world_size: Total number of processes in the job. + + Returns: + Tuple of (distributed_config, device_mesh, moe_mesh). + """ + from nemo_automodel.components.distributed.config import DDPConfig, FSDP2Config, MegatronFSDPConfig + from nemo_automodel.components.distributed.mesh_utils import create_device_mesh + + strategy = engine_config.distributed_strategy + + if strategy == "fsdp2": + from torch.distributed.fsdp import MixedPrecisionPolicy + + from verl.utils.torch_dtypes import PrecisionType + + mp_policy = MixedPrecisionPolicy( + param_dtype=PrecisionType.to_dtype(engine_config.mp_param_dtype), + reduce_dtype=PrecisionType.to_dtype(engine_config.mp_reduce_dtype), + output_dtype=PrecisionType.to_dtype(engine_config.mp_output_dtype), + cast_forward_inputs=True, + ) + + distributed_config = FSDP2Config( + sequence_parallel=engine_config.sequence_parallel, + mp_policy=mp_policy, + activation_checkpointing=engine_config.activation_checkpointing, + defer_fsdp_grad_sync=engine_config.defer_fsdp_grad_sync, + ) + + elif strategy == "megatron_fsdp": + distributed_config = MegatronFSDPConfig( + activation_checkpointing=engine_config.activation_checkpointing, + ) + + elif strategy == "ddp": + distributed_config = DDPConfig( + activation_checkpointing=engine_config.activation_checkpointing, + ) + + else: + raise ValueError(f"Unsupported distributed_strategy: {strategy}") + + device_mesh, moe_mesh = create_device_mesh( + distributed_config, + tp_size=engine_config.tp_size, + pp_size=engine_config.pp_size, + cp_size=engine_config.cp_size, + ep_size=engine_config.ep_size, + dp_replicate_size=engine_config.dp_replicate_size, + world_size=world_size, + ) + + return distributed_config, device_mesh, moe_mesh + + +def build_automodel_model(model_config, engine_config, distributed_config, device_mesh, moe_mesh): + """Build a model using NeMoAutoModelForCausalLM.from_pretrained(). + + Args: + model_config: HFModelConfig with model path and settings. + engine_config: AutomodelEngineConfig with distributed settings. + distributed_config: FSDP2Config, MegatronFSDPConfig, or DDPConfig instance. + device_mesh: Pre-created device mesh (or None for DDP). + moe_mesh: Pre-created MoE mesh (or None). + + Returns: + A HuggingFace model with Automodel's distributed infrastructure applied. + """ + from nemo_automodel._transformers.auto_model import NeMoAutoModelForCausalLM + + kwargs = {} + + if engine_config.enable_fp8: + from nemo_automodel.components.quantization.fp8 import FP8Config + + kwargs["fp8_config"] = FP8Config() + + if engine_config.enable_compile: + from nemo_automodel.components.utils.compile_utils import CompileConfig + + kwargs["compile_config"] = CompileConfig() + + # Qwen/Llama with ep_size<=1: use HF implementation. + from transformers import AutoConfig + + _cfg = AutoConfig.from_pretrained(model_config.path, trust_remote_code=model_config.trust_remote_code) + _arch = (getattr(_cfg, "architectures", None) or [""])[0].lower() + if engine_config.ep_size <= 1 and ("qwen" in _arch or "llama" in _arch): + kwargs["force_hf"] = True + + if engine_config.backend_config and not kwargs.get("force_hf", False): + from nemo_automodel.components.models.common.utils import BackendConfig + + backend_kwargs = dict(engine_config.backend_config) + kwargs["backend"] = BackendConfig(**backend_kwargs) + + # MoE config for MoEParallelizerConfig + if engine_config.ep_size > 1: + from nemo_automodel.components.moe.config import MoEParallelizerConfig + + moe_kwargs = dict(engine_config.moe_config) if engine_config.moe_config else {} + if hasattr(distributed_config, "mp_policy"): + moe_kwargs.setdefault("mp_policy", distributed_config.mp_policy) + + kwargs["moe_config"] = MoEParallelizerConfig(**moe_kwargs) + + kwargs["attn_implementation"] = engine_config.attn_implementation + + from verl.utils.torch_dtypes import PrecisionType + + kwargs["torch_dtype"] = PrecisionType.to_dtype(engine_config.model_dtype) + + model = NeMoAutoModelForCausalLM.from_pretrained( + pretrained_model_name_or_path=model_config.path, + device_mesh=device_mesh, + moe_mesh=moe_mesh, + distributed_config=distributed_config, + activation_checkpointing=engine_config.activation_checkpointing, + trust_remote_code=model_config.trust_remote_code, + **kwargs, + ) + + return model + + +@torch.no_grad() +def offload_automodel_model_to_cpu(model, empty_cache=True): + """Offload an FSDP2-wrapped model to CPU (reshard, move to CPU, optional cache clear).""" + from torch.distributed.fsdp._fully_shard._fsdp_common import TrainingState + from torch.distributed.fsdp._fully_shard._fsdp_state import _get_module_fsdp_state + + for module in model.modules(): + state = _get_module_fsdp_state(module) + if state is None: + continue + fsdp_param_group = state._fsdp_param_group + + if fsdp_param_group is None: + continue + + fsdp_param_group._training_state = TrainingState.IDLE + + model.reshard() + model.cpu() + if empty_cache: + get_torch_device().empty_cache() + + +@torch.no_grad() +def load_automodel_model_to_gpu(model): + """Load model back to GPU.""" + device = get_device_id() + model.to(device, non_blocking=True) + + +@torch.no_grad() +def offload_automodel_optimizer(optimizer): + """Offload optimizer state to CPU.""" + if not optimizer.state: + return + for param_group in optimizer.param_groups: + for param in param_group["params"]: + state = optimizer.state[param] + for key, value in state.items(): + if isinstance(value, torch.Tensor): + state[key] = value.to("cpu", non_blocking=True) + + +@torch.no_grad() +def load_automodel_optimizer(optimizer, device_id): + """Load optimizer state back to GPU.""" + if not optimizer.state: + return + for param_group in optimizer.param_groups: + for param in param_group["params"]: + state = optimizer.state[param] + for key, value in state.items(): + if isinstance(value, torch.Tensor): + state[key] = value.to(device_id, non_blocking=True) diff --git a/verl_0720_main/verl/verl/workers/engine/base.py b/verl_0720_main/verl/verl/workers/engine/base.py new file mode 100644 index 0000000000000000000000000000000000000000..dcfddb61337f3ae24e3d2c8f6ad75ce503447260 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/base.py @@ -0,0 +1,395 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +The abstract base class defining the interface for model training engines. +""" + +import os +from abc import abstractmethod +from contextlib import nullcontext +from typing import Any, Callable, ContextManager, Generator, Optional + +import torch +from tensordict import TensorDict + +from verl.utils.device import get_device_name, get_vendor +from verl.utils.tensordict_utils import maybe_fix_3d_position_ids + + +class BaseEngine: + """ + Abstract base class defining the interface for model training engines. Interface is subject to + change before release. + + Engine implementations must subclass BaseEngine and provide concrete behavior for all methods. + """ + + def initialize(self): + """ + Instantiate or load the model, optimizer, and learning rate scheduler. + + Should prepare all components necessary for training or evaluation. + """ + raise NotImplementedError + + @property + @abstractmethod + def is_param_offload_enabled(self) -> bool: + """Whether parameter offloading is enabled.""" + raise NotImplementedError + + @property + @abstractmethod + def is_optimizer_offload_enabled(self) -> bool: + """Whether optimizer offloading is enabled.""" + raise NotImplementedError + + def train_mode(self, **kwargs): + """ + Context manager entry for switching the engine and model into training mode. + + Usage: + with engine.train_mode(): + # runs in training mode + """ + raise NotImplementedError + + def eval_mode(self, **kwargs): + """ + Context manager entry for switching the engine and model into evaluation mode. + + Usage: + with engine.eval_mode(): + # runs in evaluation mode + """ + raise NotImplementedError + + def optimizer_zero_grad(self): + """ + Zero the gradients of the optimizer. + """ + raise NotImplementedError + + def optimizer_step(self): + """ + Perform an optimization step using the optimizer. + """ + raise NotImplementedError + + def lr_scheduler_step(self): + """ + Advance the learning rate scheduler by one step. + + Returns: + current_lr (float or list[float]): Updated learning rate(s). + """ + raise NotImplementedError + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> Any: + """ + Perform a forward pass and optionally a backward pass on a batch of data. + + Args: + data: The input data for the forward pass, typically containing tensors and metadata. + loss_function: The loss function to optimize. See `verl.workers.roles.utils.losses` for examples. + forward_only: If True, perform only the forward pass. If False, perform forward and backward pass. + + Returns: + Any: The output of the forward pass, which can be used for loss computation or other purposes. + """ + raise NotImplementedError + + def train_batch(self, data: TensorDict, loss_function: Callable) -> Any: + """ + Perform a training step on a batch of data. + + Args: + data: The input data for training, typically containing tensors and metadata. + loss_function: A function that computes the loss and metrics given a batch and predictions. + + Returns: + dict[str, torch.Tensor]: A dictionary containing the aggregated training metrics for the batch. + """ + maybe_fix_3d_position_ids(data) + + self.optimizer_zero_grad() + outputs = self.forward_backward_batch(data, loss_function, forward_only=False) + grad_norm = self.optimizer_step() + if self.is_mp_src_rank_with_outputs(): + assert "grad_norm" not in outputs["metrics"] + outputs["metrics"]["grad_norm"] = grad_norm + return outputs + + def infer_batch(self, data: TensorDict, loss_function: Optional[Callable] = None) -> Any: + """ + Perform inference on a batch of data. + + Args: + data: The input data for inference, typically containing tensors and metadata. + + Returns: + Any: The output of the inference, which can be used for predictions or other purposes. + """ + # see comments from train_batch + maybe_fix_3d_position_ids(data) + + with torch.no_grad(): + outputs = self.forward_backward_batch(data, loss_function, forward_only=True) + return outputs + + def get_per_tensor_param(self) -> tuple[Generator[tuple[str, torch.Tensor], None, None], Optional[dict]]: + """ + Get a generator that yields per-tensor parameters and optional peft config. + + Returns: + Generator[tuple[str, torch.Tensor]]: A generator that yields tuples of parameter names and tensors. + Optional[dict]: Optional peft config. + """ + raise NotImplementedError + + def get_per_tensor_param_shard(self, **kwargs) -> tuple[Generator, Optional[dict]]: + """ + Like :meth:`get_per_tensor_param`, but yields each rank's *local* parameter shard + instead of all-gathering full tensors. + + Used by checkpoint engines that operate on shards (e.g. the ``delta_sharded`` + backends, which byte-diff each rank's shard locally and only gather the changed + elements), so the export never materializes full tensors on any rank. + + Implementations yield ``(name, local_shard, ShardSpec)`` -- the spec (see + :mod:`verl.workers.engine.spec`) carries all placement knowledge + (offset translation or a dense rebuild callable), so the consuming checkpoint + engine stays trainer-agnostic. + + Returns: + Generator: A generator that yields per-parameter local shards with placement metadata. + Optional[dict]: Optional peft config. + """ + raise NotImplementedError + + def get_data_parallel_size(self): + raise NotImplementedError + + def get_data_parallel_rank(self): + raise NotImplementedError + + def get_data_parallel_group(self): + raise NotImplementedError + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move model parameters, optimizer states, or both to the specified device. + + Args: + device: Target device identifier. + model: If True, move the model. + optimizer: If True, move the optimizer states. + grad: If True, move the gradient buffer. + """ + if grad: + assert model, "Gradient buffers must be moved to device along with model parameters" + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """ + Save model, optimizer, and scheduler states to a checkpoint. + + Args: + local_path: Local filesystem path to save checkpoint. + hdfs_path: Optional HDFS path to copy checkpoint. + global_step: Integer training step number for naming. + max_ckpt_to_keep: Maximum number of recent checkpoints to retain. + **kwargs: Arbitrary keyword arguments. + """ + raise NotImplementedError + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: bool = True, **kwargs + ) -> None: + """ + Load model, optimizer, and scheduler states from a checkpoint. + + Args: + local_path: Local filesystem path of the checkpoint. + hdfs_path: Optional HDFS path where checkpoint is stored. + del_local_after_load: Whether to delete local copy after loading. + **kwargs: Arbitrary keyword arguments. + """ + raise NotImplementedError + + def is_mp_src_rank_with_outputs(self): + """ + Whether the current rank is the first rank in model parallel group that contains model outputs + """ + raise NotImplementedError + + def disable_adapter(self) -> ContextManager: + """ + Disable all adapters temporarily under the context in the model for LoRA + """ + return nullcontext() + + +class BaseEngineCtx: + def __init__(self, engine: BaseEngine, mode, **kwargs): + """Base Engine context that handles load and offload + + Args: + engine: + **kwargs: + """ + self.engine = engine + self.mode = mode + assert self.mode in ("train", "eval") + self.disable_auto_offload = kwargs.pop("disable_auto_offload", False) + self.zero_grad_on_exit = kwargs.pop("zero_grad_on_exit", True) + + def _context_switch(self, device): + if self.disable_auto_offload: + return + if device != "cpu": + if not self.engine.is_param_offload_enabled and not self.engine.is_optimizer_offload_enabled: + return + if self.mode == "eval": + self.engine.to(device=device, model=self.engine.is_param_offload_enabled, optimizer=False, grad=False) + elif self.mode == "train": + self.engine.to( + device=device, + model=self.engine.is_param_offload_enabled, + optimizer=self.engine.is_optimizer_offload_enabled, + grad=self.engine.is_param_offload_enabled, + ) + + def __enter__(self): + self.engine.mode = self.mode + self._context_switch(get_device_name()) + + def __exit__(self, exc_type, exc_val, exc_tb): + self._context_switch("cpu") + self.engine.mode = None + + +class EngineRegistry: + """ + A registry for managing and instantiating different types of training engines. + + This class uses a dictionary to store engine classes, mapping a string key to each class. + It provides a decorator `register` to add new engines to the registry and a `new` method + to create an instance of a registered engine. + """ + + _engines = {} + + @classmethod + def register( + cls, + model_type: str, + backend: list[str] | str, + device: list[str] | str = "cuda", + vendor: list[str] | str | None = None, + ): + """ + A class method decorator that registers an engine class with a given key. + + This allows for dynamic instantiation of engine classes by their registered key. + + Args: + model_type (str): The type of the model + backend (list[str] | str): The backend to use for the model type + device (list[str] | str): The device type (e.g., "cuda", "npu", "cpu") this engine supports, + default is "cuda" + vendor (list[str] | str | None): The hardware vendor (e.g., "nvidia", "metax") this engine + supports. If None, the engine is registered as the default for the device type. + + Returns: + A decorator function that takes an engine class and registers it. + """ + + def decorator(engine_class): + assert issubclass(engine_class, BaseEngine) + if model_type not in cls._engines: + cls._engines[model_type] = {} + + backends = backend if isinstance(backend, list) else [backend] + devices = device if isinstance(device, list) else [device] + vendors = vendor if isinstance(vendor, list) else ([vendor] if vendor else [None]) + for current_backend in backends: + for current_device in devices: + if current_backend not in cls._engines[model_type]: + cls._engines[model_type][current_backend] = {} + for current_vendor in vendors: + key = (current_device, current_vendor) if current_vendor else current_device + assert key not in cls._engines[model_type][current_backend], ( + f"The key(device-vendor: {key}) has been already registed!" + ) + cls._engines[model_type][current_backend][key] = engine_class + + return engine_class + + return decorator + + @classmethod + def get_engine_cls(cls, model_type: str, backend: str): + assert model_type in cls._engines, f"Unknown model_type: {model_type}" + assert backend in cls._engines[model_type], f"Unknown backend: {backend}" + device = get_device_name() + vendor = get_vendor() + # Allow environment variables to override detected device and vendor for engine selection, if set + if os.getenv("VERL_ENGINE_DEVICE"): + device = os.getenv("VERL_ENGINE_DEVICE") + if os.getenv("VERL_ENGINE_VENDOR"): + vendor = os.getenv("VERL_ENGINE_VENDOR") + registry = cls._engines[model_type][backend] + + # Try vendor-specific lookup: (device, vendor) + vendor_key = (device, vendor) + if vendor_key in registry: + return registry[vendor_key] + + # Fallback to device-only key (registered without vendor) + if device in registry: + return registry[device] + + # For cuda-compatible vendors without a specific registration, try nvidia + if device == "cuda" and vendor != "nvidia": + nvidia_key = (device, "nvidia") + if nvidia_key in registry: + return registry[nvidia_key] + + raise ValueError( + f"No engine registered for device={device!r}, vendor={vendor!r}, " + f"model_type={model_type!r}, backend={backend!r}" + ) + + @classmethod + def new(cls, model_type, backend, *args, **kwargs): + """ + Function to create a new training engine instance based on the provided config. + Args: + key: A configuration object containing the engine key and other settings. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + Returns: + engine: An instance of the training engine corresponding to the config. + Raises: + NotImplementedError: If the engine key in the config does not match any known engines. + """ + engine_cls = cls.get_engine_cls(model_type, backend) + return engine_cls(*args, **kwargs) diff --git a/verl_0720_main/verl/verl/workers/engine/fsdp/__init__.py b/verl_0720_main/verl/verl/workers/engine/fsdp/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..a1bdb16b47cec72d684b5c9fbc61ab787e7e81c1 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/fsdp/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from .transformer_impl import FSDPEngine, FSDPEngineWithLMHead + +__all__ = ["FSDPEngine", "FSDPEngineWithLMHead"] diff --git a/verl_0720_main/verl/verl/workers/engine/fsdp/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/fsdp/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..9d2255c795a235f5d9bdd4f9a3c31c3c3d433fa0 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/fsdp/transformer_impl.py @@ -0,0 +1,1525 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +The concrete Engine implementation using PyTorch FullyShardedDataParallel (FSDP) +""" + +import gc +import logging +import os +import warnings +from contextlib import nullcontext +from inspect import signature +from typing import Callable, ContextManager, Optional + +import torch +import torch.distributed +from peft import LoraConfig, TaskType, get_peft_model +from tensordict import TensorDict +from torch.distributed.fsdp import FullyShardedDataParallel as FSDP +from torch.distributed.fsdp.api import FullStateDictConfig, ShardedStateDictConfig, StateDictType +from torch.distributed.tensor import DTensor + +import verl.utils.torch_functional as verl_F +from verl.models.transformers.monkey_patch import apply_monkey_patch +from verl.trainer.config import CheckpointConfig +from verl.utils import tensordict_utils as tu +from verl.utils.activation_offload import enable_activation_offloading +from verl.utils.checkpoint.fsdp_checkpoint_manager import FSDPCheckpointManager +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.debug import log_gpu_memory_usage +from verl.utils.device import get_device_id, get_device_name +from verl.utils.fsdp_utils import ( + CPUOffloadPolicy, + FSDPModule, + MixedPrecisionPolicy, + apply_fsdp2, + collect_lora_params, + fsdp2_clip_grad_norm_, + fsdp2_load_full_state_dict, + fsdp_version, + get_fsdp_wrap_policy, + get_init_weight_context_manager, + init_fn, + load_fsdp_model_to_gpu, + load_fsdp_optimizer, + merged_lora_context, + normalize_peft_param_name, + offload_fsdp_model_to_cpu, + offload_fsdp_optimizer, + replace_lora_wrapper, +) +from verl.utils.model import convert_weight_keys, extract_multi_modal_inputs +from verl.utils.py_functional import convert_to_regular_types +from verl.utils.torch_functional import logprobs_from_logits +from verl.utils.ulysses import ( + gather_outputs_and_unpad, + get_ulysses_sequence_parallel_group, + set_ulysses_sequence_parallel_group, + ulysses_pad, + ulysses_pad_and_slice_inputs, +) +from verl.workers.config import FSDPEngineConfig, FSDPOptimizerConfig, HFModelConfig +from verl.workers.utils.padding import build_attention_mask_from_nested + +from ..base import BaseEngine, BaseEngineCtx, EngineRegistry +from ..utils import enable_full_determinism, postprocess_batch_func, prepare_micro_batches +from .utils import create_device_mesh, get_sharding_strategy + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +device_name = get_device_name() + + +class FSDPEngine(BaseEngine): + """ + Concrete Engine implementation using PyTorch FullyShardedDataParallel (FSDP). + + Supports model sharding, activation/optimizer offloading, LoRA, and sequence parallelism. + """ + + def __init__( + self, + model_config: HFModelConfig, + engine_config: FSDPEngineConfig, + optimizer_config: FSDPOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + """ + Initialize the FSDPEngine. + + Sets up distributed device meshes, LoRA, and offload policies based on config. + + Args: + config: Configuration object with FSDP and model settings. + """ + super().__init__() + + self.model_config = model_config + self.engine_config = engine_config + self.optimizer_config = optimizer_config + self.checkpoint_config = checkpoint_config + + self.mode = None + + self.rank = torch.distributed.get_rank() + + # Apply NPU patches for FSDP backend + from .utils import apply_npu_fsdp_patches + + apply_npu_fsdp_patches(self.model_config) + + # build device mesh for Ulysses Sequence Parallel + + self.use_remove_padding = self.model_config.use_remove_padding + + if self.engine_config.ulysses_sequence_parallel_size > 1 and not self.use_remove_padding: + raise ValueError( + "When using sequence parallelism (ulysses_sequence_parallel_size > 1), " + "you must enable `use_remove_padding`." + ) + + self._init_device_mesh() + + if self.engine_config.full_determinism: + enable_full_determinism(seed=self.engine_config.seed) + + # set FSDP offload params + self._is_offload_param = self.engine_config.param_offload + self._is_offload_optimizer = self.engine_config.optimizer_offload + self._is_lora = self.model_config.lora_rank > 0 + # Set in _build_fsdp_module when FSDP2 CPUOffloadPolicy is configured (see #5995). + self._uses_fsdp2_cpu_offload_policy = False + + # Defaults for mixed-precision state. _build_fsdp_module overrides these when it + # runs; subclasses that bypass _build_fsdp_module (e.g. VeOmniEngine) keep the + # defaults so forward_step / optimizer_step can still read them safely. + self._autocast_dtype = torch.bfloat16 + self.scaler = None + + # QAT (Quantization-Aware Training) + self._qat_config = getattr(self.engine_config, "qat", None) + self._qat_enabled = self._qat_config is not None and getattr(self._qat_config, "enable", False) + if self._qat_enabled: + logger.info(f"QAT enabled: mode={self._qat_config.mode}, group_size={self._qat_config.group_size}") + + if self.engine_config.entropy_from_logits_with_chunking: + entropy_from_logits = verl_F.entropy_from_logits_with_chunking + else: + entropy_from_logits = verl_F.entropy_from_logits + + self.compute_entropy_from_logits = ( + torch.compile(entropy_from_logits, dynamic=True) + if self.engine_config.use_torch_compile # use torch compile by default + else entropy_from_logits + ) + + @property + def is_param_offload_enabled(self) -> bool: + return self._is_offload_param + + @property + def is_optimizer_offload_enabled(self) -> bool: + return self._is_offload_optimizer + + def is_mp_src_rank_with_outputs(self): + if self.ulysses_device_mesh is not None: + is_collect = self.ulysses_device_mesh["sp"].get_local_rank() == 0 + else: + is_collect = True + return is_collect + + def initialize(self): + """ + Build the model, optimizer, and learning rate scheduler under FSDP. + + Applies device, dtype, and precision configurations, including mixed precision. + Sets up checkpoint manager and FLOPs counter. + """ + # This is used to import external_lib into the huggingface systems + self._build_model_optimizer() + + self.checkpoint_manager = FSDPCheckpointManager( + model=self.module, + optimizer=self.optimizer, + lr_scheduler=self.lr_scheduler, + processing_class=self.model_config.get_processor(), + checkpoint_config=self.checkpoint_config, + trust_remote_code=self.model_config.trust_remote_code, + ) + + self.to( + device="cpu", + model=self._is_offload_param, + optimizer=self._is_offload_optimizer, + grad=self._is_offload_param, + ) + + log_gpu_memory_usage("After offload model/optimizer/grad during init", logger=logger) + + def _init_device_mesh(self): + world_size = torch.distributed.get_world_size() + from torch.distributed.device_mesh import init_device_mesh + + fsdp_size = self.engine_config.fsdp_size + + self.device_mesh = create_device_mesh(world_size=world_size, fsdp_size=fsdp_size) + self.ulysses_device_mesh = None + self.ulysses_parallel_group = None + self.ulysses_sequence_parallel_size = self.engine_config.ulysses_sequence_parallel_size + dp_size = self.get_data_parallel_size() + if self.ulysses_sequence_parallel_size > 1: + self.ulysses_device_mesh = init_device_mesh( + device_name, mesh_shape=(dp_size, self.ulysses_sequence_parallel_size), mesh_dim_names=["dp", "sp"] + ) + self.ulysses_parallel_group = self.ulysses_device_mesh["sp"].get_group() + + self.use_ulysses_sp = self.ulysses_sequence_parallel_size > 1 + + def _build_module(self): + from verl.utils.model import get_hf_auto_model_class + from verl.utils.torch_dtypes import PrecisionType + + torch_dtype = self.engine_config.model_dtype + + if torch_dtype is None: + # if it is training, we force torch_dtype to fp32 + torch_dtype = torch.float32 if not self.engine_config.forward_only else torch.bfloat16 + + torch_dtype = PrecisionType.to_dtype(torch_dtype) + + init_context = get_init_weight_context_manager( + use_meta_tensor=not self.model_config.hf_config.tie_word_embeddings, mesh=self.device_mesh + ) + + with init_context(), warnings.catch_warnings(): + warnings.simplefilter("ignore") + + if self.model_config.model_type == "language_model": + auto_class = get_hf_auto_model_class(hf_config=self.model_config.hf_config) + + module = auto_class.from_pretrained( + pretrained_model_name_or_path=self.model_config.local_path, + torch_dtype=torch_dtype, + config=self.model_config.hf_config, + trust_remote_code=self.model_config.trust_remote_code, + ) + + # Strip sub-modules listed in _verl_strip_modules (e.g. + # talker / code2wav for Qwen3-Omni Thinker-only training). + _strip_list = getattr(module, "_verl_strip_modules", []) + for attr in _strip_list: + if hasattr(module, attr): + delattr(module, attr) + logger.info(f"Stripped unused sub-module '{attr}' to reduce memory") + else: + from verl.utils.model import load_valuehead_model + + assert self.model_config.model_type == "value_model", ( + f"Unsupported model type: {self.model_config.model_type}" + ) + self.model_config.hf_config.num_labels = 1 + self.model_config.hf_config.classifier_dropout = 0.0 + self.model_config.hf_config.hidden_dropout = "0" + self.model_config.hf_config.summary_dropout_prob = 0.0 + module = load_valuehead_model( + local_path=self.model_config.local_path, + torch_dtype=torch_dtype, + model_config=self.model_config.hf_config, + trust_remote_code=self.model_config.trust_remote_code, + ) + + use_liger = self.model_config.use_liger + # Apply Liger kernel; disable fused_linear_cross_entropy (conflicts with verl's forward patching) + if use_liger: + from liger_kernel.transformers.monkey_patch import _apply_liger_kernel_to_instance + + _apply_liger_kernel_to_instance( + model=module, + fused_linear_cross_entropy=False, + swiglu=True, + ) + + fused_kernel_options = self.model_config.fused_kernel_options + fused_kernels_backend = ( + fused_kernel_options.get("impl_backend", None) if fused_kernel_options is not None else None + ) + + use_fused_kernels = self.model_config.use_fused_kernels + apply_monkey_patch( + model=module, + use_remove_padding=self.use_remove_padding, + ulysses_sp_size=self.ulysses_sequence_parallel_size, + use_fused_kernels=use_fused_kernels, + fused_kernels_backend=fused_kernels_backend, + ) + + # some parameters may not in torch_dtype + module.to(torch_dtype) + + if self.model_config.enable_gradient_checkpointing: + module.gradient_checkpointing_enable(gradient_checkpointing_kwargs={"use_reentrant": False}) + return module + + def _build_lora_module(self, module): + module.enable_input_require_grads() + + lora_adapter_path = getattr(self.model_config, "lora_adapter_path", None) + if lora_adapter_path is not None: + from peft import PeftModel + + from verl.utils.fs import copy_to_local + + print(f"Loading pre-trained LoRA adapter to from: {lora_adapter_path}") + # Copy adapter to local if needed + local_adapter_path = copy_to_local(lora_adapter_path, use_shm=self.model_config.use_shm) + + module = PeftModel.from_pretrained(module, local_adapter_path, is_trainable=True) + peft_config = module.peft_config["default"] + # Ensure task_type is TaskType enum, not string + if isinstance(peft_config.task_type, str): + peft_config.task_type = TaskType.CAUSAL_LM + else: + # Convert config to regular Python types before creating PEFT model + lora_config = { + "task_type": TaskType.CAUSAL_LM, + "r": self.model_config.lora_rank, + "lora_alpha": self.model_config.lora_alpha, + "target_modules": convert_to_regular_types(self.model_config.target_modules), + "target_parameters": convert_to_regular_types(self.model_config.target_parameters), + "exclude_modules": convert_to_regular_types(self.model_config.exclude_modules), + "bias": "none", + } + module = get_peft_model(module, LoraConfig(**lora_config)) + + # FSDP requires all params in a flat group to share dtype: cast a + # fp32 adapter to the bf16 base dtype only when they actually differ. + base_dtype = next((p.dtype for p in module.parameters() if not p.requires_grad), None) + if base_dtype is not None: + mismatched = [p for p in module.parameters() if p.requires_grad and p.dtype != base_dtype] + if mismatched: + logger.info( + f"Casting {len(mismatched)} LoRA adapter params from " + f"{mismatched[0].dtype} to {base_dtype} to match base." + ) + for param in mismatched: + param.data = param.data.to(base_dtype) + + return module + + def _build_fsdp_module(self, module): + # TODO(ziheng): need to improve + from torch.distributed.fsdp import CPUOffload, MixedPrecision + + from verl.utils.torch_dtypes import PrecisionType + + mixed_precision_config = self.engine_config.mixed_precision + if mixed_precision_config is not None: + param_dtype = PrecisionType.to_dtype(mixed_precision_config.get("param_dtype", "bf16")) + reduce_dtype = PrecisionType.to_dtype(mixed_precision_config.get("reduce_dtype", "fp32")) + buffer_dtype = PrecisionType.to_dtype(mixed_precision_config.get("buffer_dtype", "fp32")) + else: + param_dtype = torch.bfloat16 + reduce_dtype = torch.float32 + buffer_dtype = torch.float32 + + mixed_precision = MixedPrecision(param_dtype=param_dtype, reduce_dtype=reduce_dtype, buffer_dtype=buffer_dtype) + + self._autocast_dtype = param_dtype + # fp16 training requires loss scaling to avoid gradient underflow. Mirror the pattern + # landed in #4036 for the legacy dp_actor path. bf16 / fp32 do not need a scaler. + if param_dtype == torch.float16: + from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler + + self.scaler = ShardedGradScaler(growth_interval=400) + else: + self.scaler = None + + auto_wrap_policy = get_fsdp_wrap_policy( + module=module, + config=self.engine_config.wrap_policy, + is_lora=self.model_config.lora_rank > 0, + ) + + fsdp_mesh = self.device_mesh + sharding_strategy = get_sharding_strategy(fsdp_mesh, zero3_enable=self.engine_config.reshard_after_forward) + + # Note: We force turn off CPUOffload because it causes incorrect results when using grad accumulation + if self.engine_config.strategy == "fsdp": + # cpu_offload: + # - actor: None + # - critic: None + # - ref: CPUOffload(offload_params=True) + + # We force reference policy to use CPUOffload to save memory. + # We force turn off CPUOffload for actor because it causes incorrect results when using grad accumulation + cpu_offload = None + if self.engine_config.forward_only: + cpu_offload = CPUOffload(offload_params=True) + self._is_offload_param = False + self._is_offload_optimizer = False + + module = FSDP( + module, + param_init_fn=init_fn, + auto_wrap_policy=auto_wrap_policy, + device_id=get_device_id(), + sharding_strategy=sharding_strategy, + mixed_precision=mixed_precision, + sync_module_states=True, + device_mesh=self.device_mesh, + forward_prefetch=self.engine_config.forward_prefetch, + use_orig_params=self.engine_config.use_orig_params, + cpu_offload=cpu_offload, + ) + elif self.engine_config.strategy == "fsdp2": + # - actor: offload_policy + # - critic: offload_policy + # - ref: CPUOffloadPolicy(pin_memory=True) + assert CPUOffloadPolicy is not None, "PyTorch version >= 2.4 is required for using fully_shard API (FSDP2)" + mp_policy = MixedPrecisionPolicy( + param_dtype=param_dtype, reduce_dtype=reduce_dtype, cast_forward_inputs=True + ) + offload_policy = None + if self.engine_config.offload_policy or self.engine_config.forward_only: + self._is_offload_param = False + self._is_offload_optimizer = False + offload_policy = CPUOffloadPolicy(pin_memory=True) + self._uses_fsdp2_cpu_offload_policy = True + + fsdp_kwargs = { + "mesh": fsdp_mesh, + "mp_policy": mp_policy, + "offload_policy": offload_policy, + "reshard_after_forward": self.engine_config.reshard_after_forward, + } + full_state = module.state_dict() + apply_fsdp2(module, fsdp_kwargs, self.engine_config) + fsdp2_load_full_state_dict(module, full_state, fsdp_mesh, offload_policy) + else: + raise NotImplementedError(f"Unknown strategy {self.engine_config.strategy}") + + if self.model_config.enable_activation_offload: + enable_gradient_checkpointing = self.model_config.enable_gradient_checkpointing + enable_activation_offloading(module, self.engine_config.strategy, enable_gradient_checkpointing) + + if torch.distributed.get_world_size() == 1 and fsdp_version(module) == 1: + FSDP.set_state_dict_type( + module, + state_dict_type=StateDictType.FULL_STATE_DICT, + state_dict_config=FullStateDictConfig(), + ) + elif fsdp_version(module) == 1: + FSDP.set_state_dict_type( + module, + state_dict_type=StateDictType.SHARDED_STATE_DICT, + state_dict_config=ShardedStateDictConfig(), + ) + + return module + + def _build_optimizer(self, module): + from verl.workers.config.optimizer import build_optimizer + + optimizer = build_optimizer(module.parameters(), self.optimizer_config) + + return optimizer + + def _build_lr_scheduler(self, optimizer): + from verl.utils.torch_functional import get_constant_schedule_with_warmup, get_cosine_schedule_with_warmup + + optim_config = self.optimizer_config + + total_steps = optim_config.total_training_steps + num_warmup_steps = optim_config.lr_warmup_steps + lr_scheduler_type = optim_config.lr_scheduler_type + min_lr_ratio = optim_config.min_lr_ratio + num_cycles = optim_config.num_cycles + zero_indexed_step = optim_config.zero_indexed_step + if num_warmup_steps <= 0: + num_warmup_steps_ratio = optim_config.lr_warmup_steps_ratio + num_warmup_steps = int(num_warmup_steps_ratio * total_steps) + + if self.rank == 0: + print(f"Total steps: {total_steps}, num_warmup_steps: {num_warmup_steps}") + + if lr_scheduler_type == "constant": + lr_scheduler = get_constant_schedule_with_warmup(optimizer=optimizer, num_warmup_steps=num_warmup_steps) + elif lr_scheduler_type == "cosine": + lr_scheduler = get_cosine_schedule_with_warmup( + optimizer=optimizer, + num_warmup_steps=num_warmup_steps, + num_training_steps=total_steps, + min_lr_ratio=min_lr_ratio, + num_cycles=num_cycles, + zero_indexed_step=zero_indexed_step, + ) + else: + raise NotImplementedError(f"LR scheduler type {lr_scheduler_type} is not supported") + return lr_scheduler + + def _apply_qat(self, module): + """Apply QAT transformations to the model before FSDP wrapping.""" + from verl.utils.qat.core import apply_qat, enable_qat_fuse + + module = apply_qat( + module, + { + "enable": self._qat_config.enable, + "mode": self._qat_config.mode, + "group_size": self._qat_config.group_size, + "ignore_patterns": list(self._qat_config.ignore_patterns), + "activation_observer": self._qat_config.activation_observer, + }, + ) + enable_qat_fuse(module) + + if self._qat_config.mode == "w4a4": + self._restore_w4a4_input_scales(module, self.model_config.local_path) + + return module + + def _restore_w4a4_input_scales(self, model, model_path): + """Restore input_global_scale and input_amax from checkpoint for W4A4 mode.""" + import glob + + from safetensors import safe_open + + safetensor_files = glob.glob(f"{model_path}/model*.safetensors") + loaded_count = 0 + + for sf_path in safetensor_files: + with safe_open(sf_path, framework="pt") as f: + for key in f.keys(): + if "input_global_scale" in key: + module_path = key.replace(".input_global_scale", "") + amax_key = f"{module_path}.input_amax" + + module = model + for part in module_path.split("."): + module = module[int(part)] if part.isdigit() else getattr(module, part) + + scale_val = f.get_tensor(key) + val = scale_val.item() if scale_val.numel() == 1 else scale_val.max().item() + module.input_global_scale.fill_(val) + + amax_val = f.get_tensor(amax_key) + amax = amax_val.item() if amax_val.numel() == 1 else amax_val.max().item() + module.input_amax.fill_(amax) + loaded_count += 1 + + logger.info(f"[QAT W4A4] Restored {loaded_count} input_global_scale/input_amax from {model_path}") + + def _build_model_optimizer(self): + from verl.utils.model import print_model_size + + # Load base model with specified configuration and dtype + module = self._build_module() + try: + self.pass_packed_cu_seqlens = "cu_seqlens" in signature(module.forward).parameters + except (TypeError, ValueError): + self.pass_packed_cu_seqlens = False + # Apply LoRA adapters if low-rank adaptation is enabled + if self._is_lora: + module = self._build_lora_module(module) + + # Apply QAT before FSDP wrapping (training only) + if self._qat_enabled and not self.engine_config.forward_only: + module = self._apply_qat(module) + + # Synchronize all distributed processes before proceeding + torch.distributed.barrier() + if self.rank == 0: + print_model_size(module) + log_gpu_memory_usage("After init model from HF AutoModel", logger=logger) + + # Wrap model with FSDP for distributed training (sharding, mixed precision, etc.) + log_gpu_memory_usage("Before FSDP", logger=None) + module = self._build_fsdp_module(module) + log_gpu_memory_usage("After FSDP", logger=None) + + if not self.engine_config.forward_only: + # Initialize optimizer with model parameters and config settings + optimizer = self._build_optimizer(module) + # Create learning rate scheduler with warmup and decay settings + lr_scheduler = self._build_lr_scheduler(optimizer) + else: + optimizer = None + lr_scheduler = None + + self.module = module + self.optimizer = optimizer + self.lr_scheduler = lr_scheduler + + def train_mode(self, **kwargs): + """ + Return a context manager that switches to training mode with FSDP-specific handling. + + Includes parameter and optimizer offload entry/exit. + """ + return EngineTrainModeCtx(self, **kwargs) + + def eval_mode(self, **kwargs): + """ + Return a context manager that switches to evaluation mode with FSDP-specific handling. + + Includes activation offload entry/exit. + """ + return EngineEvalModeCtx(self, **kwargs) + + def get_data_parallel_rank(self): + if self.ulysses_device_mesh is not None: + return self.ulysses_device_mesh["dp"].get_local_rank() + else: + return torch.distributed.get_rank() + + def get_data_parallel_size(self): + return torch.distributed.get_world_size() // self.ulysses_sequence_parallel_size + + def get_data_parallel_group(self): + if self.ulysses_device_mesh is not None: + return self.ulysses_device_mesh.get_group(mesh_dim="dp") + else: + return torch.distributed.group.WORLD + + def get_model_parallel_group(self): + raise NotImplementedError + + def get_context_parallel_group(self): + raise NotImplementedError + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> list[TensorDict]: + # note that the global_batch_size should include data on all the dp + tu.assign_non_tensor(data, sp_size=self.ulysses_sequence_parallel_size) + + # compute num_tokens in global batch for loss normalization + batch_num_tokens = data["loss_mask"].sum().to(get_device_id()) + torch.distributed.all_reduce( + batch_num_tokens, op=torch.distributed.ReduceOp.SUM, group=self.get_data_parallel_group() + ) + tu.assign_non_tensor(data, batch_num_tokens=batch_num_tokens.item()) + tu.assign_non_tensor(data, dp_size=self.get_data_parallel_size()) + + micro_batches, indices = prepare_micro_batches( + data=data, dp_group=self.get_data_parallel_group(), same_micro_num_in_dp=True + ) + + output_lst = [] + + ctx = torch.no_grad() if forward_only else nullcontext() + + # getattr fallback: some subclasses (e.g. VeOmniEngine) bypass FSDPEngine.__init__ + # and _build_fsdp_module, so self.scaler may not be set. + scaler = getattr(self, "scaler", None) + + for micro_batch in micro_batches: + with ctx: + loss, meta_info = self.forward_step(micro_batch, loss_function=loss_function, forward_only=forward_only) + + if not forward_only: + if scaler is not None: + scaler.scale(loss).backward() + else: + loss.backward() + # Training discards model_output (train_batch pops it); keeping it accumulates + # full-length nested tensors across the mini-batch (∝ ppo_mini_batch * rollout_n) → OOM. + meta_info.pop("model_output", None) + + output_lst.append(meta_info) + + # postprocess and return + return postprocess_batch_func(output_lst=output_lst, indices=indices, data=data) + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + raise NotImplementedError("forward_step must be implemented in subclass") + + def optimizer_zero_grad(self): + """ + Zero gradients and enforce FSDP grad-clipping logic. + """ + self.optimizer.zero_grad() + + def optimizer_step(self): + """ + Clip gradients, skip update if non-finite, and step optimizer. + + Returns: + grad_norm (float): Norm of gradients before clipping. + """ + assert self.optimizer_config.clip_grad is not None + + # getattr fallback: some subclasses (e.g. VeOmniEngine) bypass FSDPEngine.__init__. + scaler = getattr(self, "scaler", None) + + # Unscale gradients before clip so the clip threshold is applied to true gradient + # magnitudes, not scaled ones. scaler.step() will skip the update if any grad is inf/nan. + if scaler is not None: + scaler.unscale_(self.optimizer) + + if isinstance(self.module, FSDP): + grad_norm = self.module.clip_grad_norm_(self.optimizer_config.clip_grad) + elif isinstance(self.module, FSDPModule): + grad_norm = fsdp2_clip_grad_norm_(self.module.parameters(), max_norm=self.optimizer_config.clip_grad) + else: + grad_norm = torch.nn.utils.clip_grad_norm_( + self.module.parameters(), max_norm=self.optimizer_config.clip_grad + ) + + if isinstance(grad_norm, DTensor): + grad_norm = grad_norm.full_tensor() + + if scaler is not None: + # scaler handles inf/nan skipping internally via _check_inf_per_device. + scaler.step(self.optimizer) + scaler.update() + else: + # if grad_norm is not finite, skip the update + if not torch.isfinite(grad_norm): + print(f"WARN: grad_norm is not finite: {grad_norm}") + self.optimizer.zero_grad() + else: + self.optimizer.step() + + if self._qat_enabled: + from verl.utils.qat.core import invalidate_all_scales + + invalidate_all_scales(self.module) + + return grad_norm.item() + + def lr_scheduler_step(self): + """ + Advance FSDP scheduler and return updated learning rate. + """ + self.lr_scheduler.step() + lr = self.lr_scheduler.get_last_lr()[0] # only return the first group + return lr + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move FSDP model and/or optimizer to CPU or GPU with offload support. + Note that this function executes irrespective of offload config. It serves as manual control + """ + super().to(device=device, model=model, optimizer=optimizer, grad=grad) + + if self.engine_config.forward_only: + # force cpu_offload + return + + device_name = get_device_name() + + assert device in (device_name, "cpu") + if device == device_name: + if model: + load_fsdp_model_to_gpu(self.module) + if optimizer and self.optimizer is not None: + load_fsdp_optimizer(self.optimizer, device) + gc.collect() + elif device == "cpu": + if model: + offload_fsdp_model_to_cpu(self.module) + if optimizer and self.optimizer is not None: + offload_fsdp_optimizer(self.optimizer) + else: + raise ValueError(f"Invalid device type: {device}") + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """ + Save FSDP checkpoint, handling parameter offload as needed. + """ + origin_module_device = next(self.module.parameters()).device.type + if (self._is_offload_param or origin_module_device == "cpu") and not getattr( + self, "_uses_fsdp2_cpu_offload_policy", False + ): + load_fsdp_model_to_gpu(self.module) + + self.checkpoint_manager.save_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, global_step=global_step, max_ckpt_to_keep=max_ckpt_to_keep + ) + + torch.distributed.barrier() + if self._is_offload_param: + offload_fsdp_model_to_cpu(self.module) + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: int = True, **kwargs + ) -> None: + """ + Load FSDP checkpoint, restoring parameters and optimizer state. + """ + import torch + + if self._is_offload_param: + load_fsdp_model_to_gpu(self.module) + + self.checkpoint_manager.load_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, del_local_after_load=del_local_after_load + ) + + torch.distributed.barrier() + if self._is_offload_param: + offload_fsdp_model_to_cpu(self.module) + + if self._is_offload_optimizer: + offload_fsdp_optimizer(self.optimizer) + + def get_per_tensor_param_shard(self, **kwargs): + """Like :meth:`get_per_tensor_param`, but yields each rank's *local* FSDP shard + instead of all-gathering the full tensor -- used by the ``delta_sharded`` + checkpoint engine, which diffs on shards and gathers only the changes. + + Yields ``(name, local_flat_shard_bf16, within_param_flat_offset, full_numel, + full_shape, contributes)``. Non-LoRA base path only. + """ + + # FSDP1's (SHARDED_)STATE_DICT export runs through the unshard machinery and + # asserts flat params are GPU-resident; FSDP2 state_dict() only collects + # DTensor refs and the generator below stages each shard lazily. + _needs_staging = fsdp_version(self.module) == 1 + if _needs_staging and not self._uses_fsdp2_cpu_offload_policy: + load_fsdp_model_to_gpu(self.module) + params = self.module.state_dict() + params = convert_weight_keys(params, getattr(self.module, "_fsdp_wrapped_module", self.module)) + if _needs_staging and self._is_offload_param: + offload_fsdp_model_to_cpu(self.module) + + device = get_device_id() + + from ..spec import ShardSpec + + def _gen(): + for name, param in params.items(): + spec = ShardSpec.from_param(param) + p = param.to(device, non_blocking=True) + if p.is_floating_point(): + p = p.to(torch.bfloat16, non_blocking=True) + local = p.to_local() if hasattr(p, "to_local") else p + yield name, local.reshape(-1), spec + + return _gen(), None + + def get_per_tensor_param(self, layered_summon=False, base_sync_done=False, **kwargs): + log_gpu_memory_usage("Before load_fsdp_model_to_gpu", logger=logger) + + # FSDP2 CPUOffloadPolicy owns CPU<->GPU placement; calling model.to(device) here + # leaves the module half-moved and crashes state_dict() below (#5995). The + # per-DTensor .to(device).full_tensor() below still produces GPU tensors. + # + # FSDP2 state_dict() only collects DTensor refs and the generator below already + # stages each shard lazily via .to(device).full_tensor(), so the whole-shard + # round trip is only needed for FSDP1 (state_dict unshards on-device) and LoRA + # (adapter merge does real weight math on the module). + _is_peft = hasattr(getattr(self.module, "_fsdp_wrapped_module", self.module), "peft_config") + _skip_staging = fsdp_version(self.module) == 2 and not _is_peft + if not self._uses_fsdp2_cpu_offload_policy and not _skip_staging: + load_fsdp_model_to_gpu(self.module) + + log_gpu_memory_usage("After load_fsdp_model_to_gpu", logger=logger) + + peft_config = None + merge_lora = self.model_config.lora.get("merge", False) + + peft_model = getattr(self.module, "_fsdp_wrapped_module", self.module) + if hasattr(peft_model, "peft_config"): # LoRA + if not merge_lora: + peft_config = peft_model.peft_config.get("default", None) + params = collect_lora_params( + module=self.module, + layered_summon=layered_summon, + base_sync_done=base_sync_done, + ) + if not base_sync_done: + params = {replace_lora_wrapper(k, peft_config): v for k, v in params.items()} + else: # merge lora + # state_dict() aliases the live parameter storage and merged_lora_context + # restores the un-merged base weights on exit, so tensors must be + # materialized while the context is still open (inside the generator). + # Materializing after exit silently sends base weights without adapters. + return self._merged_lora_per_tensor_param(), None + else: + params = self.module.state_dict() + + params = convert_weight_keys(params, getattr(self.module, "_fsdp_wrapped_module", self.module)) + + log_gpu_memory_usage("Before offload_fsdp_model_to_cpu", logger=logger) + if self._is_offload_param and not _skip_staging: + offload_fsdp_model_to_cpu(self.module) + log_gpu_memory_usage("After offload_fsdp_model_to_cpu", logger=logger) + + if peft_config is not None and base_sync_done: + per_tensor_param = params.items() + else: + device = get_device_id() # used when fsdp2 set cpu_offload_policy + # TODO: cast fp32 to bf16 to reduce weight sync overhead, need more fine-grained control, e.g MoE gate + per_tensor_param = ( + ( + name, + param.to(device, non_blocking=True).full_tensor().to(torch.bfloat16, non_blocking=True) + if isinstance(param, DTensor) + else param, + ) + for name, param in params.items() + ) + + if self._qat_enabled: + from verl.utils.qat.quantizer import QATQuantizer + from verl.utils.torch_dtypes import PrecisionType + + mixed_precision_config = self.engine_config.mixed_precision + if mixed_precision_config is not None: + param_dtype = PrecisionType.to_dtype(mixed_precision_config.get("param_dtype", "bf16")) + else: + param_dtype = torch.bfloat16 + + quantizer = QATQuantizer( + mode=self._qat_config.mode, + group_size=self._qat_config.group_size, + ignore_patterns=list(self._qat_config.ignore_patterns), + device=torch.device(get_device_id()), + param_dtype=param_dtype, + ) + per_tensor_param = quantizer.quantize_with_fusion( + per_tensor_param, + target_device=torch.device("cpu"), + ) + + peft_config_dict = peft_config.to_dict() if peft_config is not None else None + return per_tensor_param, peft_config_dict + + def _merged_lora_per_tensor_param(self): + """Stream merged (base + LoRA) weights for rollout weight sync. + + ``state_dict()`` returns tensors that alias the live FSDP parameter + storage, and ``merged_lora_context`` restores the un-merged base + weights when it exits. The context therefore must stay open until the + consumer has materialized every tensor: ``DTensor.full_tensor()`` + produces a copy, so yielded tensors remain valid after the restore. + Consuming a state_dict captured inside the context after the context + has exited would silently send base weights without the adapters. + """ + device = get_device_id() + try: + with merged_lora_context(self.module, backup_adapters=True): + params = normalize_peft_param_name(self.module.state_dict()) + params = convert_weight_keys(params, getattr(self.module, "_fsdp_wrapped_module", self.module)) + for name, param in params.items(): + yield ( + name, + param.to(device, non_blocking=True).full_tensor().to(torch.bfloat16, non_blocking=True) + if isinstance(param, DTensor) + # clone: plain tensors also alias module storage, and bucketed + # senders may flush after the restore has already run + else param.detach().clone(), + ) + finally: + log_gpu_memory_usage("Before offload_fsdp_model_to_cpu", logger=logger) + if self._is_offload_param: + offload_fsdp_model_to_cpu(self.module) + log_gpu_memory_usage("After offload_fsdp_model_to_cpu", logger=logger) + + def disable_adapter(self) -> ContextManager: + return self.module.disable_adapter() + + +class EngineEvalModeCtx(BaseEngineCtx): + def __init__(self, engine: FSDPEngine, **kwargs): + super().__init__(engine=engine, mode="eval", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, FSDPEngine) + super().__enter__() + self.prev_sp_group = get_ulysses_sequence_parallel_group() + set_ulysses_sequence_parallel_group(self.engine.ulysses_parallel_group) + self.engine.module.eval() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, FSDPEngine) + set_ulysses_sequence_parallel_group(self.prev_sp_group) + + # https://pytorch.org/docs/stable/notes/fsdp.html#fsdp-notes + # unshard the root FSDP module + if self.engine.engine_config.fsdp_size > 1: + if fsdp_version(self.engine.module) == 1: + self.engine.module._handle.reshard(True) + elif fsdp_version(self.engine.module) == 2: + self.engine.module.reshard() + + super().__exit__(exc_type, exc_value, traceback) + + +class EngineTrainModeCtx(BaseEngineCtx): + def __init__(self, engine: FSDPEngine, **kwargs): + super().__init__(engine=engine, mode="train", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, FSDPEngine) + super().__enter__() + self.prev_sp_group = get_ulysses_sequence_parallel_group() + set_ulysses_sequence_parallel_group(self.engine.ulysses_parallel_group) + self.engine.module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, FSDPEngine) + set_ulysses_sequence_parallel_group(self.prev_sp_group) + if self.zero_grad_on_exit or exc_type is not None: + self.engine.optimizer_zero_grad() + super().__exit__(exc_type, exc_value, traceback) + + +@EngineRegistry.register(model_type="language_model", backend=["fsdp", "fsdp2"], device=["cuda", "npu"]) +class FSDPEngineWithLMHead(FSDPEngine): + def prepare_model_inputs(self, micro_batch: TensorDict): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + use_fused_kernels = tu.get_non_tensor_data(data=micro_batch, key="use_fused_kernels", default=False) + temperature = micro_batch["temperature"] + temperature_item = temperature + if use_fused_kernels: + assert not isinstance(temperature, torch.Tensor), ( + "use_fused_kernels does not support per sample temperature yet" + ) + assert pad_mode == DatasetPadMode.NO_PADDING, f"pad_mode {pad_mode} not supported" + + multi_modal_inputs = extract_multi_modal_inputs(micro_batch.get("multi_modal_inputs", [])) + input_ids = micro_batch["input_ids"] + position_ids = micro_batch["position_ids"] + pass_packed_cu_seqlens = getattr(self, "pass_packed_cu_seqlens", False) + + if not isinstance(temperature, torch.Tensor): + temperature = torch.tensor([temperature] * input_ids.shape[0], device=input_ids.device) + + temperature = temperature.to(torch.float32) + assert temperature.shape[0] == input_ids.shape[0] + + # args used to get outputs + output_args = {} + + if use_remove_padding: + # support per sample temperature + # temperature (bsz,) + # input_ids (bsz, j1) + temperature_rmpad = verl_F.expand_as_nested(temperature, input_ids).values() # (total_nnz,) + temperature_rmpad = temperature_rmpad.unsqueeze(0) # (1, total_nnz) + packed_cu_seqlens = None + + if pad_mode == DatasetPadMode.NO_PADDING: + input_ids_rmpad = input_ids.values().unsqueeze(0) # (1, total_nnz) + packed_cu_seqlens = input_ids.offsets().to(device=input_ids_rmpad.device, dtype=torch.long) + if position_ids.dim() == 3: + position_ids_rmpad = position_ids.values().unsqueeze(1) # (4, 1, total_nnz) + else: + position_ids_rmpad = position_ids.values().unsqueeze(0) # (1, total_nnz) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + # for compute the log_prob + input_ids_rmpad_rolled = torch.roll(input_ids_rmpad, shifts=-1, dims=1) # (1, total_nnz) + + # pad and slice the inputs if sp > 1 + sp_pad_size = 0 + is_vlm_model = False + if self.use_ulysses_sp: + is_vlm_model = hasattr(getattr(self.module, "module", self.module).config, "vision_config") + if is_vlm_model: + # vlm model's inputs will be sliced after embedding + input_ids_rmpad, position_ids_rmpad, pad_size = ulysses_pad( + input_ids_rmpad, + position_ids_rmpad=position_ids_rmpad, + sp_size=self.ulysses_sequence_parallel_size, + ) + else: + input_ids_rmpad, position_ids_rmpad, pad_size = ulysses_pad_and_slice_inputs( + input_ids_rmpad, + position_ids_rmpad=position_ids_rmpad, + sp_size=self.ulysses_sequence_parallel_size, + skip_position_ids_rmpad=getattr(self, "_veomni_handles_position_ids", False), + ) + input_ids_rmpad_rolled, _, _ = ulysses_pad_and_slice_inputs( + input_ids_rmpad_rolled, + position_ids_rmpad=None, + sp_size=self.ulysses_sequence_parallel_size, + ) + + temperature_rmpad, _, _ = ulysses_pad_and_slice_inputs( + temperature_rmpad, position_ids_rmpad=None, sp_size=self.ulysses_sequence_parallel_size, pad_value=1 + ) + + output_args["pad_size"] = pad_size + sp_pad_size = pad_size + + input_ids_rmpad_rolled = input_ids_rmpad_rolled.squeeze(0) # ((total_nnz / sp) + pad) + temperature_rmpad = temperature_rmpad.squeeze(0) + output_args["input_ids_rmpad_rolled"] = input_ids_rmpad_rolled + output_args["temperature_rmpad"] = temperature_rmpad + + # only pass input_ids and position_ids to enable flash_attn_varlen + + model_inputs = { + "input_ids": input_ids_rmpad, + "attention_mask": None, + "position_ids": position_ids_rmpad, + } + if packed_cu_seqlens is not None and pass_packed_cu_seqlens: + model_cu_seqlens = packed_cu_seqlens + if self.use_ulysses_sp and sp_pad_size: + padded_total = int(model_cu_seqlens[-1].item()) + int(sp_pad_size) + model_cu_seqlens = torch.cat( + [ + model_cu_seqlens, + model_cu_seqlens.new_tensor([padded_total]), + ] + ) + model_inputs["cu_seqlens"] = model_cu_seqlens + model_inputs["cu_seqlens_cpu"] = model_cu_seqlens.cpu() + + else: + if pad_mode == DatasetPadMode.NO_PADDING: + input_ids = micro_batch["input_ids"] + position_ids = micro_batch["position_ids"] + pad_token_id = tu.get_non_tensor_data(data=micro_batch, key="pad_token_id", default=0) + batch_size = micro_batch.batch_size[0] + seq_len_effective = input_ids.offsets().diff() + max_seq_len = int(seq_len_effective.max().item()) + + input_ids_rmpad_rolled = torch.roll(input_ids.values(), shifts=-1, dims=0) + output_args["input_ids_rmpad_rolled"] = input_ids_rmpad_rolled + # we store the per sample temperature + output_args["temperature"] = temperature + + input_ids = torch.nested.to_padded_tensor( + input_ids, padding=pad_token_id, output_size=(batch_size, max_seq_len) + ) + + if position_ids.dim() == 3: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, 4, max_seq_len) + ).transpose(0, 1) # (4, batch_size, max_seq_len) + else: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, max_seq_len) + ) + + attention_mask = build_attention_mask_from_nested( + input_ids=micro_batch["input_ids"], max_seq_len=max_seq_len + ) + + model_inputs = { + "input_ids": input_ids, + "attention_mask": attention_mask, + "position_ids": position_ids, + } + + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + extra_args = {} + if use_fused_kernels: + extra_args["temperature"] = temperature_item + extra_args["return_dict"] = True + if use_remove_padding: + # We have already computed `input_ids_rmpad_rolled` from the *full* + # global sequence and (when SP>1) SP-sliced it. Pass it into the model + # so the fused forward uses these labels verbatim instead of redoing + # `torch.roll` on the local SP shard, which would wrap around the + # shard boundary rather than the global sequence (issue #6068). This + # mirrors what the veomni engine already does for fused kernels. + extra_args["shift_labels"] = output_args["input_ids_rmpad_rolled"].unsqueeze(0) + + model_inputs.update(multi_modal_inputs) + model_inputs.update(extra_args) + + return model_inputs, output_args + + def prepare_model_outputs(self, output, output_args, micro_batch: TensorDict, logits_processor_func): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + use_fused_kernels = tu.get_non_tensor_data(data=micro_batch, key="use_fused_kernels", default=False) + calculate_entropy = tu.get_non_tensor_data(data=micro_batch, key="calculate_entropy", default=False) + calculate_sum_pi_squared = tu.get_non_tensor_data( + data=micro_batch, key="calculate_sum_pi_squared", default=False + ) + distillation_use_topk = tu.get_non_tensor_data(data=micro_batch, key="distillation_use_topk", default=False) + distillation_only = tu.get_non_tensor_data(data=micro_batch, key="distillation_only", default=False) + + if calculate_sum_pi_squared and use_fused_kernels: + raise NotImplementedError( + "calculate_sum_pi_squared=True is not supported with use_fused_kernels=True: " + "fused kernels do not materialize the full logits tensor needed for Σπ²." + ) + + model_output = {} + + input_ids = micro_batch["input_ids"] + + if use_remove_padding: + input_ids_rmpad_rolled = output_args["input_ids_rmpad_rolled"] + temperature_rmpad = output_args["temperature_rmpad"] + + if use_fused_kernels: + # temperature is singleton + log_probs = None + if not distillation_only: + log_probs = output.log_probs.squeeze(0) # (total_nnz,) + entropy_rmpad = output.entropy.squeeze(0) if calculate_entropy else None # (total_nnz,) + + # When the fused kernel also computed top-K distillation + # (veomni's chunk_topk_distill path), extract the per-token + # distillation outputs and store them as nested tensors — + # same model_output keys as the eager logit-processor path. + if distillation_use_topk: + aux_outputs = getattr(output, "fused_linear_aux", None) + if aux_outputs is not None and aux_outputs.distillation_losses is not None: + cu_seqlens = input_ids.offsets() + for field_name in ("distillation_losses", "student_mass", "teacher_mass"): + v = getattr(aux_outputs, field_name).squeeze(0) + if self.use_ulysses_sp: + pad_size = output_args["pad_size"] + v = gather_outputs_and_unpad(v, gather_dim=0, unpad_dim=0, padding_size=pad_size) + model_output[field_name] = torch.nested.nested_tensor_from_jagged(v, cu_seqlens) + else: + logits_rmpad = output.logits.squeeze(0) # (total_nnz, vocab_size) + # With TP, logits are DTensors sharded on vocab dim; gather for log_softmax. + if isinstance(logits_rmpad, DTensor): + logits_rmpad = logits_rmpad.full_tensor() + logits_rmpad = logits_rmpad / temperature_rmpad.clamp(min=1e-8).unsqueeze(-1).to(logits_rmpad.dtype) + + log_probs = None + + if calculate_sum_pi_squared: + sum_pi_squared_rmpad = verl_F.calculate_sum_pi_squared_from_logits(logits_rmpad) + + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + if self.engine_config.entropy_from_logits_with_chunking: + entropy_rmpad = self.compute_entropy_from_logits( + logits_rmpad, + chunk_size=self.engine_config.entropy_from_logits_chunk_size, + ) # ((total_nnz / sp) + pad) + else: + entropy_rmpad = self.compute_entropy_from_logits(logits_rmpad) + else: + entropy_rmpad = torch.utils.checkpoint.checkpoint( + self.compute_entropy_from_logits, logits_rmpad + ) + + # logits_processor_func return tensors with shape (1, total_nnz/sp_size) + if distillation_use_topk: + outputs = logits_processor_func(student_logits=logits_rmpad.unsqueeze(0), data=micro_batch) + cu_seqlens = input_ids.offsets() + for k, v in outputs.items(): + v = v.squeeze(0) + assert v.shape == (logits_rmpad.shape[0],), ( + f"logits_rmpad len: {logits_rmpad.shape[0]}, {k} shape: {v.shape}" + ) + if self.use_ulysses_sp: + pad_size = output_args["pad_size"] + v = gather_outputs_and_unpad(v, gather_dim=0, unpad_dim=0, padding_size=pad_size) + model_output[k] = torch.nested.nested_tensor_from_jagged(v, cu_seqlens) + + if not distillation_only: + # if use_sp: ((total_nnz / sp) + pad) ; if not use_sp: (batch, seqlen) + inplace_backward = True + if calculate_entropy: + inplace_backward = False + log_probs = logprobs_from_logits( + logits=logits_rmpad, + labels=input_ids_rmpad_rolled, + inplace_backward=inplace_backward, + ) + + # gather log_prob if sp > 1 + if self.use_ulysses_sp: + pad_size = output_args["pad_size"] + + # gather and unpad for the ulysses sp + if not distillation_only: + log_probs = gather_outputs_and_unpad( + log_probs, + gather_dim=0, + unpad_dim=0, + padding_size=pad_size, + ) + if calculate_entropy: + entropy_rmpad = gather_outputs_and_unpad( + entropy_rmpad, + gather_dim=0, + unpad_dim=0, + padding_size=pad_size, + ) + if calculate_sum_pi_squared: + sum_pi_squared_rmpad = gather_outputs_and_unpad( + sum_pi_squared_rmpad, + gather_dim=0, + unpad_dim=0, + padding_size=pad_size, + ) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + # (bsz, j1), for each sample, is the length of each sample: [real_prompt length + real_response length] + if not distillation_only: + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + if calculate_entropy: + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + if calculate_sum_pi_squared: + sum_pi_squared = torch.nested.nested_tensor_from_jagged(sum_pi_squared_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + else: # not using rmpad and no ulysses sp + if use_fused_kernels: + if pad_mode == DatasetPadMode.NO_PADDING: + # Re-wrap dense fused (bsz, seqlen) outputs as nested/jagged: mask out the + # padding columns per sequence and pack the valid tokens to (total_nnz,). + cu_seqlens = input_ids.offsets() + seq_lengths = cu_seqlens.diff() + arange = torch.arange(output.log_probs.shape[1], device=output.log_probs.device) + mask = arange < seq_lengths.unsqueeze(1) + + log_probs = None + if not distillation_only: + log_probs = output.log_probs[mask] + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + + if calculate_entropy: + entropy = output.entropy[mask] + entropy = torch.nested.nested_tensor_from_jagged(entropy, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + else: + logits = output.logits # (bsz, response_length, vocab_size) + temperature = output_args["temperature"] # (bsz,) + temperature = temperature.unsqueeze(-1).unsqueeze(-1) + # With TP, logits are DTensors sharded on vocab dim; gather for log_softmax. + if isinstance(logits, DTensor): + logits = logits.full_tensor() + logits = logits / temperature.clamp(min=1e-8).to(logits.dtype) + + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + entropy = verl_F.entropy_from_logits(logits) + else: + entropy = torch.utils.checkpoint.checkpoint(verl_F.entropy_from_logits, logits) + + if calculate_sum_pi_squared: + sum_pi_squared = verl_F.calculate_sum_pi_squared_from_logits(logits) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + seq_lengths = cu_seqlens.diff() + starts = torch.zeros_like(seq_lengths, dtype=torch.int64) + logits = torch.nested.narrow(logits, 1, starts, seq_lengths, layout=torch.jagged) + logits_rmpad = torch.cat([t for t in logits.unbind()]) + input_ids_rmpad_rolled = output_args["input_ids_rmpad_rolled"] + + # Mirror the use_remove_padding=True branch (see verl#6293). + # No Ulysses SP gather here: this branch is the no-SP path + # (log_probs is also not gathered) and pad_size is only + # populated in output_args along the use_remove_padding=True + # path of prepare_model_inputs. + if distillation_use_topk: + outputs = logits_processor_func(student_logits=logits_rmpad.unsqueeze(0), data=micro_batch) + for k, v in outputs.items(): + v = v.squeeze(0) + assert v.shape == (logits_rmpad.shape[0],), ( + f"logits_rmpad len: {logits_rmpad.shape[0]}, {k} shape: {v.shape}" + ) + model_output[k] = torch.nested.nested_tensor_from_jagged(v, cu_seqlens) + + log_probs = None + if not distillation_only: + log_probs = logprobs_from_logits(logits=logits_rmpad, labels=input_ids_rmpad_rolled) + + # (bsz, j1), for each sample, length of each sample: [real_prompt_length + real_response_length] + if not distillation_only: + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + if calculate_entropy: + entropy = torch.nested.narrow(entropy, 1, starts, seq_lengths, layout=torch.jagged) + entropy_rmpad = torch.cat([t for t in entropy.unbind()]) + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + if calculate_sum_pi_squared: + sum_pi_squared = torch.nested.narrow( + sum_pi_squared, 1, starts, seq_lengths, layout=torch.jagged + ) + sum_pi_squared_rmpad = torch.cat([t for t in sum_pi_squared.unbind()]) + sum_pi_squared = torch.nested.nested_tensor_from_jagged(sum_pi_squared_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + if not distillation_only: + model_output["log_probs"] = log_probs + if calculate_entropy: + model_output["entropy"] = entropy + if calculate_sum_pi_squared: + model_output["sum_pi_squared"] = sum_pi_squared + + return model_output + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + device_name = get_device_name() + # actually, we should avoid assigning like this... + micro_batch = micro_batch.to(get_device_id()) + model_inputs, output_args = self.prepare_model_inputs(micro_batch=micro_batch) + + # Honor mixed_precision.param_dtype resolved during FSDP setup. When dtype is fp32, + # autocast is a no-op at best and a footgun at worst, so skip it entirely. + # getattr fallback: some subclasses (e.g. VeOmniEngine) bypass FSDPEngine.__init__ + # and _build_fsdp_module, so self._autocast_dtype may not be set. + autocast_dtype = getattr(self, "_autocast_dtype", torch.bfloat16) + autocast_ctx: ContextManager = ( + nullcontext() + if autocast_dtype == torch.float32 + else torch.autocast(device_type=device_name, dtype=autocast_dtype) + ) + with autocast_ctx: + raw_output = self.module( + **model_inputs, + use_cache=False, + ) # prevent model thinks we are generating + + model_output = self.prepare_model_outputs( + output=raw_output, output_args=output_args, micro_batch=micro_batch, logits_processor_func=loss_function + ) + + if loss_function is not None: + loss, metrics = loss_function( + model_output=model_output, data=micro_batch, dp_group=self.get_data_parallel_group() + ) + else: + assert forward_only, "forward_only must be True when loss_function is None" + loss = torch.tensor(1.0, device=device_name) + metrics = {} + + # Detach model outputs before they are appended to forward_backward_batch's + # output_lst: they are only consumed for metrics/postprocessing after backward, + # and keeping their grad_fn alive retains part of every micro-batch's autograd + # graph until the whole batch finishes. With PEFT (enable_input_require_grads) + # this pins the checkpointed embedding output plus its gradient buffer per + # micro-batch (~2 x [total_nnz, hidden] for long sequences), which accumulates + # across micro-batches and OOMs the actor update. + model_output = { + key: value.detach() if torch.is_tensor(value) and value.grad_fn is not None else value + for key, value in model_output.items() + } + output = { + "model_output": model_output, + "loss": loss.detach().item(), + "metrics": metrics, + } + + return loss, output + + +@EngineRegistry.register(model_type="value_model", backend=["fsdp", "fsdp2"], device=["cuda", "npu"]) +class FSDPEngineWithValueHead(FSDPEngineWithLMHead): + """ + The only difference between critic and actor is how the raw model output is processed + """ + + def prepare_model_outputs(self, output, output_args, micro_batch: TensorDict, logits_processor_func): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + + input_ids = micro_batch["input_ids"] + if use_remove_padding: + if hasattr(self.module, "v_head"): + # For trl.AutoModelForCausalLMWithValueHead + values_rmpad = output[2].squeeze(0) + else: + values_rmpad = output.logits + values_rmpad = values_rmpad.squeeze(0) # (total_nnz, 1) + # critic model arch is like Qwen3ForTokenClassfication and num_labels=1 + # so we squeeze the last dimension here to get the value for each token + values_rmpad = values_rmpad.squeeze(-1) + + # gather output if sp > 1 + if self.use_ulysses_sp: + pad_size = output_args["pad_size"] + values_rmpad = gather_outputs_and_unpad(values_rmpad, gather_dim=0, unpad_dim=0, padding_size=pad_size) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + # (bsz, j1), for each sample, is the length of each sample: [real_prompt length + real_response length] + values = torch.nested.nested_tensor_from_jagged(values_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + else: + if hasattr(self.module, "v_head"): + # For trl.AutoModelForCausalLMWithValueHead + values = output[2] + else: + values = output.logits.squeeze(-1) + + if pad_mode == DatasetPadMode.NO_PADDING: + cu_seqlens = input_ids.offsets() + seq_lengths = cu_seqlens.diff() + starts = torch.zeros_like(seq_lengths, dtype=torch.int64) + values = torch.nested.narrow(values, 1, starts, seq_lengths, layout=torch.jagged) + values_rmpad = torch.cat([t for t in values.unbind()]) + # (bsz, j1), for each sample, length of each sample: [real_prompt_length + real_response_length] + values = torch.nested.nested_tensor_from_jagged(values_rmpad, cu_seqlens) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + return {"values": values} diff --git a/verl_0720_main/verl/verl/workers/engine/fsdp/utils.py b/verl_0720_main/verl/verl/workers/engine/fsdp/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..4ddde00cc2170ae8ed29e1e769d70a090b52eca9 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/fsdp/utils.py @@ -0,0 +1,87 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import logging +import os + +from torch.distributed.device_mesh import init_device_mesh + +from verl.utils.device import get_device_name, is_npu_available + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +def apply_npu_fsdp_patches(model_config=None): + """Apply NPU patches for FSDP backend if NPU is available.""" + if is_npu_available: + if model_config is not None and model_config.get("use_liger", False): + return + from verl.models.transformers.npu_patch import apply_npu_patches + + apply_npu_patches() + + +def create_device_mesh(world_size, fsdp_size): + """ + Create a device mesh for distributed training based on the world size and FSDP size. + + Args: + world_size (int): Total number of processes in the distributed training setup. + fsdp_size (int): Size of the Fully Sharded Data Parallel (FSDP) group. + + Returns: + torch.distributed.device_mesh.DeviceMesh: The initialized device mesh. + """ + device_name = get_device_name() + if fsdp_size < 0 or fsdp_size >= world_size: + device_mesh = init_device_mesh(device_name, mesh_shape=(world_size,), mesh_dim_names=["fsdp"]) + else: + device_mesh = init_device_mesh( + device_name, mesh_shape=(world_size // fsdp_size, fsdp_size), mesh_dim_names=["ddp", "fsdp"] + ) + return device_mesh + + +def get_sharding_strategy(device_mesh, zero3_enable=True): + """ + Determine the appropriate sharding strategy based on the number of dimensions of the device mesh. + + Args: + device_mesh (torch.distributed.device_mesh.DeviceMesh): The device mesh used for distributed training. + + Returns: + torch.distributed.fsdp.ShardingStrategy: The sharding strategy to be used with FSDP. + + Raises: + NotImplementedError: If the number of dimensions of the device mesh is neither 1 nor 2. + """ + + from torch.distributed.fsdp import ShardingStrategy + + if zero3_enable: + fsdp_strategy = ShardingStrategy.FULL_SHARD + hsdp_strategy = ShardingStrategy.HYBRID_SHARD + else: + fsdp_strategy = ShardingStrategy.SHARD_GRAD_OP + hsdp_strategy = ShardingStrategy._HYBRID_SHARD_ZERO2 + + if device_mesh.ndim == 1: + sharding_strategy = ShardingStrategy.FULL_SHARD + sharding_strategy = fsdp_strategy + elif device_mesh.ndim == 2: + sharding_strategy = ShardingStrategy.HYBRID_SHARD + sharding_strategy = hsdp_strategy + else: + raise NotImplementedError(f"Get device mesh ndim={device_mesh.ndim}, but only support 1 or 2") + return sharding_strategy diff --git a/verl_0720_main/verl/verl/workers/engine/megatron/__init__.py b/verl_0720_main/verl/verl/workers/engine/megatron/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..51d3a1949e9170f5ff2d0997b55076d97b0c4918 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/megatron/__init__.py @@ -0,0 +1,29 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# HACK Avoid cpu worker trigger cuda jit error +import os + +from verl.utils.device import is_cuda_available + +if not is_cuda_available and "TORCH_CUDA_ARCH_LIST" not in os.environ: + os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0" + +from .transformer_impl import MegatronEngine, MegatronEngineWithLMHead, MegatronEngineWithValueHead # noqa: E402 + +if not is_cuda_available: + del os.environ["TORCH_CUDA_ARCH_LIST"] + +__all__ = ["MegatronEngine", "MegatronEngineWithLMHead", "MegatronEngineWithValueHead"] diff --git a/verl_0720_main/verl/verl/workers/engine/megatron/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/megatron/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..9256982565b4db5de140a6c57c5e2df6b2d9572e --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/megatron/transformer_impl.py @@ -0,0 +1,1229 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import inspect +import logging +import os +from functools import partial +from typing import Any, Callable, ContextManager, Iterator, Optional + +import torch +import torch.distributed +from megatron.core import parallel_state as mpu +from megatron.core.package_info import __version__ +from megatron.core.pipeline_parallel import get_forward_backward_func +from omegaconf import OmegaConf +from tensordict import TensorDict + +import verl.utils.torch_functional as verl_F +from verl.models.mcore import get_mcore_weight_converter +from verl.trainer.config import CheckpointConfig +from verl.utils import tensordict_utils as tu +from verl.utils.checkpoint.megatron_checkpoint_manager import MegatronCheckpointManager +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.debug import log_gpu_memory_usage +from verl.utils.device import get_device_id, get_device_name, get_torch_device +from verl.utils.megatron.pipeline_parallel import make_batch_generator +from verl.utils.megatron.router_replay_patch import RouterReplay, RouterReplayAction, apply_router_replay_patch +from verl.utils.megatron.router_replay_utils import ( + RouterReplayHelper, + build_r3_replay_mask, + merge_router_topk_indices, + pp_gather, + reorder_and_merge_vpp_layers, + set_router_replay_data, +) +from verl.utils.megatron.tensor_parallel import ( + vocab_parallel_entropy, + vocab_parallel_entropy_with_chunking, + vocab_parallel_log_probs_from_logits, + vocab_parallel_sum_pi_squared, +) +from verl.utils.megatron_peft_utils import add_base_layer_suffix, build_peft_config_for_vllm +from verl.utils.megatron_utils import ( + check_mtp_config, + get_megatron_module_device, + get_megatron_mtp_loss, + load_megatron_model_to_gpu, + load_megatron_optimizer, + offload_megatron_model_to_cpu, + offload_megatron_optimizer, + patch_engine_mtp, + register_megatron_training_hooks, + unwrap_model, +) +from verl.utils.model import extract_multi_modal_inputs, load_mcore_dist_weights +from verl.utils.seqlen_balancing import restore_dynamic_batch +from verl.workers.config import HFModelConfig, McoreEngineConfig, McoreOptimizerConfig + +from ..base import BaseEngine, BaseEngineCtx, EngineRegistry +from ..utils import postprocess_batch_func, prepare_micro_batches +from .utils import set_random_seed + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +class MegatronEngine(BaseEngine): + def __init__( + self, + model_config: HFModelConfig, + engine_config: McoreEngineConfig, + optimizer_config: McoreOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + super().__init__() + + self.model_config = model_config + self.engine_config = engine_config + self.optimizer_config = optimizer_config + self.checkpoint_config = checkpoint_config + assert self.engine_config.use_mbridge, "use_mbridge must be True" + self._init_device_mesh() + + set_random_seed(seed=self.engine_config.seed) + + self._is_offload_param = self.engine_config.param_offload + self._is_offload_grad = self.engine_config.grad_offload + self._is_offload_optimizer = self.engine_config.optimizer_offload + + self.mode = None + + self.layer_name_mapping = { + "qkv_layer_name": "self_attention.linear_qkv.", + "gate_proj_layer_name": "linear_fc1.", + } + self.weight_converter = None + + # QAT configuration + self._qat_config = getattr(self.engine_config, "qat", None) + self._qat_enabled = self._qat_config is not None and getattr(self._qat_config, "enable", False) + if self._qat_enabled: + if self.engine_config.vanilla_mbridge: + raise ValueError( + "QAT requires non-vanilla Megatron bridge. " + "Please set 'use_mbridge=True' and 'vanilla_mbridge=False'." + ) + logger.info(f"QAT enabled in MegatronEngine: mode={self._qat_config.mode}") + + # Router replay configuration for MoE models + self.enable_routing_replay = self.engine_config.router_replay.mode != "disabled" + logger.info(f"enable_routing_replay in MegatronEngine: {self.enable_routing_replay}") + if self.enable_routing_replay: + apply_router_replay_patch() + self.mini_layer_topk_idx_list = [] + # Apply checkpoint patch for MoE models + from verl.utils.device import is_cuda_available, is_npu_available + + if is_npu_available and __version__ >= "0.16.0": + from verl.models.mcore.patch import apply_mtp_inference_patch + + apply_mtp_inference_patch() + + if is_cuda_available: + from verl.models.mcore.patch import apply_patch_megatron_recomputation_backward + + apply_patch_megatron_recomputation_backward() + + def _init_device_mesh(self): + # TODO: set different parallelism for actor, critic, ref + if mpu.is_initialized(): + return + + extra_args = dict() + + if self.engine_config.dynamic_context_parallel: + assert "dynamic_context_parallel" in inspect.signature(mpu.initialize_model_parallel).parameters, ( + "dynamic_context_parallel is not supported in your megatron version, " + + "please update your megatron version to the latest version" + ) + assert self.engine_config.max_seqlen_per_dp_cp_rank is not None, ( + "max_seqlen_per_dp_cp_rank is required when dynamic_context_parallel is enabled" + ) + extra_args["dynamic_context_parallel"] = self.engine_config.dynamic_context_parallel + + mpu.initialize_model_parallel( + tensor_model_parallel_size=self.engine_config.tensor_model_parallel_size, + pipeline_model_parallel_size=self.engine_config.pipeline_model_parallel_size, + virtual_pipeline_model_parallel_size=self.engine_config.virtual_pipeline_model_parallel_size, + use_sharp=False, + context_parallel_size=self.engine_config.context_parallel_size, + expert_model_parallel_size=self.engine_config.expert_model_parallel_size, + expert_tensor_parallel_size=self.engine_config.expert_tensor_parallel_size, + nccl_communicator_config_path=None, + **extra_args, + ) + + def _build_tf_config(self): + from verl.utils.megatron_utils import mapping_string_to_attn_backend + from verl.utils.torch_dtypes import PrecisionType + + self.is_value_model = self.model_config.model_type == "value_model" + self.share_embeddings_and_output_weights = self.model_config.share_embeddings_and_output_weights + + check_mtp_config(self.model_config, self.engine_config) + + self.param_dtype = PrecisionType.to_dtype(self.engine_config.dtype) + self.dtype = PrecisionType.to_dtype(self.param_dtype) + + override_transformer_config = mapping_string_to_attn_backend({**self.engine_config.override_transformer_config}) + if self.is_value_model: + # A value head cannot share weights with the vocabulary embedding. This must + # be set before either bridge creates and finalizes its Megatron config. + self.model_config.hf_config.tie_word_embeddings = False + self.share_embeddings_and_output_weights = False + override_transformer_config["share_embeddings_and_output_weights"] = False + if self.engine_config.dynamic_context_parallel: + override_transformer_config["max_seqlen_per_dp_cp_rank"] = self.engine_config.max_seqlen_per_dp_cp_rank + # note(baiyan): we must set the transformer_config.dynamic_context_parallel to False + # because of the bad coupling design in Megatron-LM + # https://github.com/xiaoyao0115/Megatron-LM/blob/88733ab6614e3e91b9d095172f41e7d8b5d8e9d4/megatron/core/pipeline_parallel/dynamic_cp_schedule.py#L552-L553 + # but it does not affect the functionality of dynamic CP, so we can use it to avoid the coupling. + override_transformer_config["dynamic_context_parallel"] = False + override_transformer_config["context_parallel_size"] = mpu.get_data_parallel_world_size() + self.provider = None + self.vanilla_bridge = self.engine_config.vanilla_mbridge + + if self.vanilla_bridge: + from verl.models.mcore.mbridge import AutoBridge + + bridge = AutoBridge.from_config(self.model_config.hf_config, dtype=self.param_dtype) + bridge.set_extra_args(**override_transformer_config) + tf_config = bridge.config + tf_config.fp16 = self.param_dtype == torch.float16 + tf_config.bf16 = self.param_dtype == torch.bfloat16 + else: + from verl.models.mcore.bridge import AutoBridge + + # Use Megatron-Bridge to convert HF config to Megatron config + bridge = AutoBridge.from_hf_pretrained( + self.model_config.local_path, trust_remote_code=self.model_config.trust_remote_code + ) + # Get Megatron provider and configure it + provider = bridge.to_megatron_provider(load_weights=False) + + # Match verl implementation (need variable_seq_lengths) + from megatron.core.transformer.enums import AttnBackend + + virtual_pipeline_model_parallel_size = self.engine_config.virtual_pipeline_model_parallel_size + provider_overrides = { + "tensor_model_parallel_size": self.engine_config.tensor_model_parallel_size, + "pipeline_model_parallel_size": self.engine_config.pipeline_model_parallel_size, + "expert_model_parallel_size": self.engine_config.expert_model_parallel_size, + "expert_tensor_parallel_size": self.engine_config.expert_tensor_parallel_size, + "virtual_pipeline_model_parallel_size": virtual_pipeline_model_parallel_size, + "context_parallel_size": self.engine_config.context_parallel_size, + "sequence_parallel": self.engine_config.sequence_parallel, + "overlap_p2p_comm": ( + virtual_pipeline_model_parallel_size is not None and virtual_pipeline_model_parallel_size > 1 + ), + "batch_p2p_comm": False, + "variable_seq_lengths": True, + "attention_backend": AttnBackend.flash, + "moe_token_dispatcher_type": "alltoall", + "moe_router_load_balancing_type": "none", + } + for key, value in override_transformer_config.items(): + provider_overrides[key] = value + if ( + self.model_config.hf_config.model_type == "deepseek_v4" + and not self.model_config.mtp.enable + and getattr(provider, "mtp_num_layers", 0) + ): + provider_overrides["mtp_num_layers"] = 0 + csa_compress_ratios = getattr(provider, "csa_compress_ratios", None) + if csa_compress_ratios is not None: + provider_overrides["csa_compress_ratios"] = csa_compress_ratios[: provider.num_layers] + if self.enable_routing_replay: + if hasattr(provider, "moe_enable_routing_replay"): + provider_overrides["moe_enable_routing_replay"] = True + else: + provider_overrides["enable_routing_replay"] = True + + if self._qat_enabled: + from megatron.bridge.models.gpt_provider import modelopt_transformer_layer_spec + + provider.transformer_layer_spec = modelopt_transformer_layer_spec + + provider.apply_overrides_and_finalize( + dtype=self.param_dtype, + overrides=provider_overrides, + ) + self.provider = provider + tf_config = None # Will be set after model creation + self.bridge = bridge + + if not self.bridge: + self.weight_converter = get_mcore_weight_converter(self.model_config.hf_config, self.dtype) + + # Set router replay directly on tf_config instead of passing through + # override_transformer_config, because dataclass subclasses like MLATransformerConfig + # generate their own __init__ and may not accept compatibility kwargs. + if self.enable_routing_replay and tf_config is not None: + if hasattr(tf_config, "moe_enable_routing_replay"): + tf_config.moe_enable_routing_replay = True + else: + tf_config.enable_routing_replay = True + + if torch.distributed.get_rank() == 0: + if tf_config is not None: + print(f"TF config: {tf_config}") + self.tf_config = tf_config + + from verl.workers.config.megatron_peft import get_peft_cls + + self.peft_cls = get_peft_cls( + model_config=self.model_config, bridge=self.bridge, provider=self.provider, dtype=self.param_dtype + ) + + def _resolve_override_ddp_config(self): + """Keep the DDP grad-bucket dtype consistent with the optimizer's grad buffer. + + When the precision-aware optimizer is opted into with a sub-fp32 + ``main_grads_dtype``, the DDP grad bucket must reduce grads in the same + dtype, so inject ``grad_reduce_in_fp32=False`` unless the user set it + explicitly via ``override_ddp_config``. Default (opt-out) leaves the fp32 + grad bucket untouched, preserving prior behavior. + """ + from verl.utils.torch_dtypes import PrecisionType + + override_ddp_config = dict(self.engine_config.override_ddp_config or {}) + opt_cfg = self.optimizer_config + if ( + opt_cfg is not None + and getattr(opt_cfg, "use_precision_aware_optimizer", False) + and PrecisionType.to_dtype(getattr(opt_cfg, "main_grads_dtype", "fp32")) != torch.float32 + and "grad_reduce_in_fp32" not in override_ddp_config + ): + override_ddp_config["grad_reduce_in_fp32"] = False + return override_ddp_config + + def _build_megatron_module(self): + from verl.utils.megatron_utils import McoreModuleWrapperConfig, make_megatron_module + from verl.utils.model import print_model_size + + if self.engine_config.forward_only: + wrap_with_ddp = False + else: + wrap_with_ddp = True + + wrap_config = McoreModuleWrapperConfig( + is_value_model=self.is_value_model, + wrap_with_ddp=wrap_with_ddp, + use_distributed_optimizer=self.engine_config.use_distributed_optimizer, + use_megatron_fsdp=self.engine_config.use_megatron_fsdp, + ) + override_ddp_config = self._resolve_override_ddp_config() + + module, updated_tf_config = make_megatron_module( + wrap_config=wrap_config, + tf_config=self.tf_config, + hf_config=self.model_config.hf_config, + bridge=self.bridge, + provider=self.provider, + override_model_config=self.engine_config.override_mcore_model_config, + override_ddp_config=override_ddp_config, + peft_cls=self.peft_cls, + peft_config=self.model_config.get("lora", None), + ) + self.tf_config = updated_tf_config + print(f"module: {len(module)}") + + if self.engine_config.use_dist_checkpointing: + load_mcore_dist_weights( + module, self.engine_config.dist_checkpointing_path, is_value_model=self.is_value_model + ) + else: + if self.vanilla_bridge: + self.bridge.load_weights(module, self.model_config.local_path) + else: + allowed_mismatched_params = [] + if self.is_value_model: + allowed_mismatched_params = ["output_layer.weight"] + self.bridge.load_hf_weights( + module, self.model_config.local_path, allowed_mismatched_params=allowed_mismatched_params + ) + + if torch.distributed.get_rank() == 0: + print_model_size(module[0]) + + if self.enable_routing_replay: + print(f"routing replay layers: {len(RouterReplay.router_instances)}") + + return module + + def _maybe_enable_fused_kernels(self): + if not self.engine_config.use_fused_kernels: + return + + if self.is_value_model or self.model_config.mtp.enable: + logger.warning_once( + "Fused kernels are not supported for value models or when MTP is enabled in Megatron engine; disabling." + ) + self.engine_config.use_fused_kernels = False + return + + from verl.models.mcore.model_forward_fused import patch_fused_forward + + for model in self.module: + patch_fused_forward(model) + + def _build_optimizer(self): + from verl.utils.megatron.optimizer import get_megatron_optimizer, init_megatron_optim_config + + optim_config_megatron = init_megatron_optim_config( + self.optimizer_config, + use_distributed_optimizer=self.engine_config.use_distributed_optimizer, + fp16=self.param_dtype == torch.float16, + bf16=self.param_dtype == torch.bfloat16, + ) + optimizer = get_megatron_optimizer(model=self.module, config=optim_config_megatron) + register_megatron_training_hooks(self.module, optimizer) + return optimizer + + def _build_lr_scheduler(self): + from verl.utils.megatron.optimizer import get_megatron_optimizer_param_scheduler + + optimizer_scheduler = get_megatron_optimizer_param_scheduler( + optimizer=self.optimizer, config=self.optimizer_config + ) + return optimizer_scheduler + + @property + def is_param_offload_enabled(self) -> bool: + return self._is_offload_param + + @property + def is_optimizer_offload_enabled(self) -> bool: + return self._is_offload_optimizer + + def is_mp_src_rank_with_outputs(self): + return ( + mpu.get_tensor_model_parallel_rank() == 0 + and mpu.get_pipeline_model_parallel_rank() == mpu.get_pipeline_model_parallel_world_size() - 1 + and mpu.get_context_parallel_rank() == 0 + ) + + def initialize(self): + self._build_tf_config() + + self.module = self._build_megatron_module() + + if self._qat_enabled and not self.engine_config.forward_only: + from verl.utils.modelopt import apply_qat_to_modules + + self.module = apply_qat_to_modules(self.module, self._qat_config) + + self._maybe_enable_fused_kernels() + + if self.model_config.mtp.enable: + patch_engine_mtp(self.module, self.model_config) + elif ( + self.engine_config.forward_only + and self.engine_config.override_transformer_config.get("mtp_num_layers") == 0 + ): + from verl.models.mcore.mtp_patch import patch_postprocess + + for model in self.module: + patch_postprocess(model) + + # For forward_only, we don't need optimizer, lr_scheduler, checkpoint_mananager + if self.engine_config.forward_only: + self.optimizer = None + self.lr_scheduler = None + self.to(device="cpu", model=self._is_offload_param, optimizer=False, grad=False) + log_gpu_memory_usage("After offload model during init (forward_only)", logger=logger) + return + + self.optimizer = self._build_optimizer() + self.lr_scheduler = self._build_lr_scheduler() + + full_reshardable = self.engine_config.dist_ckpt_optim_fully_reshardable + mem_eff = self.engine_config.distrib_optim_fully_reshardable_mem_efficient + + tmp_config = OmegaConf.create( + { + "model": {"path": self.model_config.local_path}, + "megatron": { + "dist_ckpt_optim_fully_reshardable": full_reshardable, + "distrib_optim_fully_reshardable_mem_efficient": mem_eff, + }, + } + ) + + role = "actor" if not self.is_value_model else "critic" + + self.checkpoint_mananager = MegatronCheckpointManager( + config=tmp_config, + checkpoint_config=self.checkpoint_config, + model_config=self.model_config.hf_config, + transformer_config=self.tf_config, + role=role, + model=self.module, + arch=self.model_config.architectures[0], + hf_config=self.model_config.hf_config, + param_dtype=self.param_dtype, + share_embeddings_and_output_weights=self.share_embeddings_and_output_weights, + processing_class=self.model_config.get_processor(), + optimizer=self.optimizer, + optimizer_scheduler=self.lr_scheduler, + use_distributed_optimizer=self.engine_config.use_distributed_optimizer, + use_checkpoint_opt_param_scheduler=self.optimizer_config.use_checkpoint_opt_param_scheduler, + use_dist_checkpointing=self.engine_config.use_dist_checkpointing, + bridge=self.bridge, + provider=self.provider, + peft_cls=self.peft_cls, + use_megatron_fsdp=self.engine_config.use_megatron_fsdp, + ) + + self.to( + device="cpu", + model=self._is_offload_param, + optimizer=self._is_offload_optimizer, + grad=self._is_offload_param, + ) + + log_gpu_memory_usage("After offload model/optimizer/grad during init", logger=logger) + + def train_mode(self, **kwargs): + """ + Context manager entry for switching the engine and model into training mode. + + Usage: + with engine.train_mode(): + # runs in training mode + """ + return EngineTrainModeCtx(self, **kwargs) + + def eval_mode(self, **kwargs): + """ + Context manager entry for switching the engine and model into evaluation mode. + + Usage: + with engine.eval_mode(): + # runs in evaluation mode + """ + return EngineEvalModeCtx(self, **kwargs) + + def optimizer_zero_grad(self): + """ + Zero out gradients of all parameters before starting a new backward pass. + """ + self.optimizer.zero_grad() + # use use_contiguous_buffers_in_local_ddp and no overlap_dp_param_comm + for chunk in self.module: + # if use distributed optimizer, zero grad buffer will be handled by optimizer + chunk.zero_grad_buffer() + + def optimizer_step(self): + """ + Perform an optimization step to update model parameters based on accumulated gradients. + + Returns: + grad_norm (float): The norm of the gradients before clipping or update. + """ + # forward_kl_topk leaves large fp32 vocab tensors until backward ends; + # free cached blocks before grad-norm all_reduce to reduce OOM on tight VRAM. + if getattr(self, "_distillation_use_topk_active", False): + get_torch_device().empty_cache() + update_successful, grad_norm, num_zeros_in_grad = self.optimizer.step() + + if update_successful: + # allgather already execute in optimizer.step in new megatron + pass + else: + raise NotImplementedError("Megatron optimizer step failed. This should not happen") + + return grad_norm + + def lr_scheduler_step(self): + """ + Advance the learning rate scheduler by one step. + + Returns: + current_lr (float or list[float]): Updated learning rate(s). + """ + from verl.utils.megatron.optimizer import get_megatron_last_lr + + self.lr_scheduler.step(1) + return get_megatron_last_lr(self.optimizer) + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move model parameters, optimizer states, or both to the specified device. + Note that this function executes irrespective of offload config. It serves as manual control + + Args: + device: Target device identifier. + model: If True, move the model. + optimizer: If True, move the optimizer states. + """ + super().to(device=device, model=model, optimizer=optimizer, grad=grad) + + device_name = get_device_name() + + assert device in (device_name, "cpu") + if device == device_name: + if model: + load_megatron_model_to_gpu(self.module, load_grad=grad) + if optimizer and self.optimizer is not None: + load_megatron_optimizer(self.optimizer) + elif device == "cpu": + if model: + offload_megatron_model_to_cpu(self.module) + if optimizer and self.optimizer is not None: + offload_megatron_optimizer(self.optimizer) + else: + raise ValueError(f"Invalid device type: {device}") + + def get_data_parallel_rank(self): + if self.engine_config.dynamic_context_parallel: + # in order to let every dp-cp group has full data to split, we set dp=1 + return 0 + return mpu.get_data_parallel_rank() + + def get_data_parallel_size(self): + if self.engine_config.dynamic_context_parallel: + # in order to let every dp-cp group has full data to split, we set dp=1 + return 1 + return mpu.get_data_parallel_world_size() + + def get_data_parallel_group(self): + return mpu.get_data_parallel_group() + + def get_model_parallel_group(self): + return mpu.get_model_parallel_group() + + def get_context_parallel_group(self): + return mpu.get_context_parallel_group() + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """ + Save model, optimizer, and scheduler states to a checkpoint. + + Args: + local_path: Local filesystem path to save checkpoint. + hdfs_path: Optional HDFS path to copy checkpoint. + global_step: Integer training step number for naming. + max_ckpt_to_keep: Maximum number of recent checkpoints to retain. + """ + origin_module_device = get_megatron_module_device(self.module) + if self._is_offload_param or origin_module_device == "cpu": + load_megatron_model_to_gpu(self.module, load_grad=True) + self.checkpoint_mananager.save_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, global_step=global_step, max_ckpt_to_keep=max_ckpt_to_keep + ) + torch.distributed.barrier() + if self._is_offload_param: + offload_megatron_model_to_cpu(self.module) + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: bool = True, **kwargs + ) -> None: + """ + Load model, optimizer, and scheduler states from a checkpoint. + + Args: + local_path: Local filesystem path of the checkpoint. + hdfs_path: Optional HDFS path where checkpoint is stored. + del_local_after_load: Whether to delete local copy after loading. + """ + if self._is_offload_param: + load_megatron_model_to_gpu(self.module) + self.checkpoint_mananager.load_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, del_local_after_load=del_local_after_load + ) + if self._is_offload_param: + offload_megatron_model_to_cpu(self.module) + if self._is_offload_optimizer: + offload_megatron_optimizer(self.optimizer) + + def _routed_num_tokens(self, data: TensorDict) -> torch.Tensor: + """Real (unpadded) tokens fed to the MoE router: attention_mask in the padded RL + path, else the packed input_ids count in the no-padding SFT path. Not loss_mask, + which counts response tokens only and would under-normalize the router loss.""" + attention_mask = data.get("attention_mask", None) + if attention_mask is not None: + return attention_mask.sum() + input_ids = data["input_ids"] + if input_ids.is_nested: + return input_ids.offsets()[-1] + return torch.tensor(input_ids.numel(), device=input_ids.device) + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> Any: + self._distillation_use_topk_active = tu.get_non_tensor_data(data, key="distillation_use_topk", default=False) + tu.assign_non_tensor(data, sp_size=self.engine_config.context_parallel_size) + + # compute num_tokens in global batch for loss normalization + batch_num_tokens = data["loss_mask"].sum().to(get_device_id()) + torch.distributed.all_reduce( + batch_num_tokens, op=torch.distributed.ReduceOp.SUM, group=self.get_data_parallel_group() + ) + tu.assign_non_tensor(data, batch_num_tokens=batch_num_tokens.item()) + tu.assign_non_tensor(data, dp_size=self.get_data_parallel_size()) + + # Global routed-token count for the per-token-loss regime (consumed in + # postprocess_micro_batch_func). Real tokens are CP-replicated, so a single + # all-reduce over the DP group gives the global value. + if self.tf_config is not None and self.tf_config.calculate_per_token_loss: + routed_num_tokens = self._routed_num_tokens(data).to(get_device_id()) + torch.distributed.all_reduce( + routed_num_tokens, op=torch.distributed.ReduceOp.SUM, group=self.get_data_parallel_group() + ) + tu.assign_non_tensor(data, routed_num_tokens=routed_num_tokens.item()) + + # BSHD path only: pad every micro-batch to the mini-batch's global max seq_len so the + # padded `s_q` is shared -> cuDNN plan built once per shape. Raw (unaligned) + # max; TP/CP/FP8 alignment is applied inside preprocess_bshd_engine. + pad_bshd_to_minibatch_max = self.engine_config.pad_bshd_to_minibatch_max + global_max_seqlen = None + if pad_bshd_to_minibatch_max and not self.engine_config.use_remove_padding and "input_ids" in data.keys(): + input_ids_for_max = data["input_ids"] + if input_ids_for_max.is_nested: + global_max_seqlen = int(input_ids_for_max.offsets().diff().max().item()) + + vpp_size = mpu.get_virtual_pipeline_model_parallel_world_size() + if vpp_size is not None and vpp_size > 1: + num_batches_divided_by = self.tf_config.microbatch_group_size_per_vp_stage + else: + num_batches_divided_by = None + + micro_batches, indices = prepare_micro_batches( + data=data, + dp_group=self.get_data_parallel_group(), + num_batches_divided_by=num_batches_divided_by, + same_micro_num_in_dp=True, + min_num_micro_batch=None, + ) + + if num_batches_divided_by is not None: + assert len(micro_batches) % num_batches_divided_by == 0, ( + f"micro_batches {micro_batches} must be divisible by num_batches_divided_by " + f"{num_batches_divided_by} for megatron backend" + ) + + # compute input shapes for pp stages + n_micro_batch = len(micro_batches) + + for micro_batch in micro_batches: + tu.assign_non_tensor(micro_batch, num_micro_batch=n_micro_batch) + if global_max_seqlen is not None: + tu.assign_non_tensor(micro_batch, forced_max_seqlen=global_max_seqlen) + + forward_backward_func = get_forward_backward_func() + + postprocess_micro_batch_func = partial( + self.postprocess_micro_batch_func, + forward_only=forward_only, + loss_function=loss_function, + ) + + tu.assign_non_tensor(data, num_micro_batch=n_micro_batch) + + forward_step = partial( + self.forward_step, + logits_processor_func=loss_function, + postprocess_micro_batch_func=postprocess_micro_batch_func, + ) + + enable_routing_replay = tu.get_non_tensor_data(data, key="enable_routing_replay", default=False) + + if enable_routing_replay: + # Set to REPLAY mode: for R3 mode or actor update phase in R2 mode + RouterReplay.set_global_router_replay_action(RouterReplayAction.REPLAY_FORWARD) + if forward_only and self.engine_config.router_replay.mode == "R2": + # In R2 mode, forward_only calls (e.g., compute_log_probs) need to record routing information + RouterReplay.set_global_router_replay_action(RouterReplayAction.RECORD) + + # batch should be a list of batches inside micro-batches + batch_generator = make_batch_generator(micro_batches, vpp_size=len(self.module)) + + # TODO: we may use the new schedule instead + # for flash-attn: (seq_len, batch_size, hidden_size) = (mbs*seq_len, 1, hidden_size) + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.module, + num_microbatches=n_micro_batch, + seq_length=1, # the communication shape is obtained via p2p comm + micro_batch_size=1, # the communication shape is obtained via p2p comm + forward_only=forward_only, + ) + + if self.model_config.mtp.enable and mpu.is_pipeline_last_stage(ignore_virtual=True): + # All CP ranks must participate in the all_reduce inside get_megatron_mtp_loss, + # because save_loss_to_tracker uses avg_group=DP+CP group. + # Only collect metrics on the src rank afterward. + metrics = get_megatron_mtp_loss(n_micro_batch) + if self.is_mp_src_rank_with_outputs(): + if "metrics" not in losses_reduced[0]: + losses_reduced[0]["metrics"] = {} + losses_reduced[0]["metrics"].update(metrics) + + if RouterReplayHelper.is_r2_record_action(self.tf_config): + if self.tf_config.virtual_pipeline_model_parallel_size is not None: + # config = self.actor_module[0].module.module.config + vp_size = len(self.module) + microbatch_group_size_per_vp_stage = self.tf_config.microbatch_group_size_per_vp_stage + bs = n_micro_batch + topk_idx_td = reorder_and_merge_vpp_layers( + self.mini_layer_topk_idx_list, bs, vp_size, microbatch_group_size_per_vp_stage + ) + else: + tensors = [tensor for nt in self.mini_layer_topk_idx_list for tensor in nt.unbind()] + topk_idx_td = torch.nested.as_nested_tensor(tensors, layout=torch.jagged) + self.mini_layer_topk_idx_list = [] + + layers_topk_idx = pp_gather(topk_idx_td.to(torch.uint8), self.tf_config) + use_dynamic_bsz = tu.get_non_tensor_data(data=data, key="use_dynamic_bsz", default=True) + if use_dynamic_bsz and indices is not None: + layers_topk_idx = restore_dynamic_batch(layers_topk_idx, indices) + + output = {} + if mpu.is_pipeline_last_stage(ignore_virtual=True): + output = postprocess_batch_func(output_lst=losses_reduced, indices=indices, data=data) + if RouterReplayHelper.is_r2_record_action(self.tf_config): + output["model_output"]["routed_experts"] = layers_topk_idx + if enable_routing_replay: + RouterReplay.clear_global_indices() + RouterReplay.clear_global_router_replay_action() + return output + + def get_per_tensor_param(self, base_sync_done=False, **kwargs): + peft_config = None + non_merge_lora_sync = self.peft_cls is not None and not self.model_config.lora.get("merge", False) + adapter_only = base_sync_done and non_merge_lora_sync + if non_merge_lora_sync: + peft_config = build_peft_config_for_vllm(self.model_config.lora) + # when lora adapter only, we only load adapter weights when base sync is done, otherwise load all weights + load_megatron_model_to_gpu(self.module, load_grad=False, load_frozen_params=not adapter_only) + if self.vanilla_bridge: + per_tensor_param = self.bridge.export_weights(self.module) + elif adapter_only: + per_tensor_param = self.bridge.export_adapter_weights(self.module) + else: + per_tensor_param = ( + self.bridge.export_hf_weights(self.module, merge_adapter_weights=False) + if non_merge_lora_sync + else self.bridge.export_hf_weights(self.module) + ) + if non_merge_lora_sync: + per_tensor_param = add_base_layer_suffix( + per_tensor_param, model_type=self.model_config.hf_config.model_type + ) + + # QAT: process weights through QATWeightExporter for quantized weight sync to vLLM + if self._qat_enabled: + from verl.utils.modelopt import export_qat_weights + + per_tensor_param = export_qat_weights(per_tensor_param, self.module, self._qat_config.mode, self.bridge) + + return per_tensor_param, peft_config + + def disable_adapter(self) -> ContextManager: + return self.peft_cls.disable_adapter(self.module) + + def forward_step(self, batch_iter, model, logits_processor_func, postprocess_micro_batch_func): + raise NotImplementedError("forward_step must be implemented in subclass") + + def postprocess_micro_batch_func(self, output, data: TensorDict, forward_only: bool, loss_function): + raise NotImplementedError("postprocess_micro_batch_func must be implemented in subclass") + + +class EngineEvalModeCtx(BaseEngineCtx): + def __init__(self, engine: MegatronEngine, **kwargs): + super().__init__(engine=engine, mode="eval", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, MegatronEngine) + super().__enter__() + # mcore module is a list of model chunk in each vpp stage + for module in self.engine.module: + module.eval() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, MegatronEngine) + super().__exit__(exc_type, exc_value, traceback) + + +class EngineTrainModeCtx(BaseEngineCtx): + def __init__(self, engine: MegatronEngine, **kwargs): + super().__init__(engine=engine, mode="train", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, MegatronEngine) + super().__enter__() + # mcore module is a list of model chunk in each vpp stage + for module in self.engine.module: + module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, MegatronEngine) + if self.zero_grad_on_exit or exc_type is not None: + self.engine.optimizer_zero_grad() + super().__exit__(exc_type, exc_value, traceback) + + +@EngineRegistry.register(model_type="language_model", backend="megatron") +class MegatronEngineWithLMHead(MegatronEngine): + def prepare_model_inputs(self, batch: TensorDict): + input_ids = batch["input_ids"] + loss_mask = batch["loss_mask"].to(bool) + multi_modal_inputs = extract_multi_modal_inputs(batch.get("multi_modal_inputs", [])) + + routed_experts = batch.get("routed_experts", None) + + return { + "input_ids": input_ids, + "attention_mask": batch.get("attention_mask", None), + "loss_mask": loss_mask, + "multi_modal_inputs": multi_modal_inputs, + "routed_experts": routed_experts, + } + + def prepare_model_outputs(self, output: dict, data: TensorDict): + return output + + def _lm_head_logits_processor( + self, + logits, + label, + temperature, + *, + calculate_sum_pi_squared: bool, + calculate_entropy: bool, + distillation_use_topk: bool, + distillation_only: bool, + logits_processor_func: Callable, + batch: TensorDict, + data_format: str, + ): + assert logits.shape[:2] == label.shape[:2] + # avoid non-positive temperature such as padding + temperature[temperature <= 0] = 1e-8 + assert torch.all(temperature > 0).item(), f"temperature tensor must be positive. Got {temperature}" + logits.div_(temperature.unsqueeze(dim=-1).to(logits.dtype)) + ret = {} + # sum_pi_squared is non-destructive — must run before vocab_parallel_entropy. + if calculate_sum_pi_squared: + ret["sum_pi_squared"] = vocab_parallel_sum_pi_squared(logits) + if calculate_entropy: + logits_bak = logits.clone() + # # disable the hint until the fused_kernel is optimized for triton>=3.3 + # if torch.distributed.get_rank() == 0: + # logger.warning_once( + # "For memory-efficient computation, enable fused kernels via " + # "`actor_rollout_ref.model.use_fused_kernels=True`. " + # "The current `clone()` operation ensures correctness but increases memory usage." + # ) + if self.engine_config.entropy_from_logits_with_chunking: + entropy = vocab_parallel_entropy_with_chunking( + logits, + chunk_size=self.engine_config.entropy_from_logits_chunk_size, + ) + else: + entropy = vocab_parallel_entropy(logits) + + ret["entropy"] = entropy + else: + logits_bak = logits + + # logits_processor_func return tensors with shape (1, total_nnz/cp_size) + if distillation_use_topk: + ret.update(logits_processor_func(student_logits=logits_bak, data=batch, data_format=data_format)) + if not distillation_only: + ret["log_probs"] = vocab_parallel_log_probs_from_logits(logits_bak, label) + + return ret + + def forward_step( + self, batch_iter: Iterator[TensorDict], model, logits_processor_func, postprocess_micro_batch_func + ): + batch: TensorDict = next(batch_iter) + + if self.engine_config.dynamic_context_parallel: + # split the batch and give the sub-batches to each dp-cp group + from verl.utils.megatron_utils import dynamic_cp_split_batch + + batch = dynamic_cp_split_batch( + batch=batch, + engine_config=self.engine_config, + dp_size=mpu.get_data_parallel_world_size(), + dp_rank=mpu.get_data_parallel_rank(), + ) + + batch = batch.to(get_device_id()) + use_fused_kernels = tu.get_non_tensor_data(batch, key="use_fused_kernels", default=False) + calculate_entropy = tu.get_non_tensor_data(batch, key="calculate_entropy", default=False) + calculate_sum_pi_squared = tu.get_non_tensor_data(batch, key="calculate_sum_pi_squared", default=False) + distillation_use_topk = tu.get_non_tensor_data(batch, key="distillation_use_topk", default=False) + distillation_only = tu.get_non_tensor_data(batch, key="distillation_only", default=False) + + if calculate_sum_pi_squared and use_fused_kernels: + raise NotImplementedError( + "calculate_sum_pi_squared=True is not supported with use_fused_kernels=True: " + "fused kernels do not materialize the full logits tensor needed for Σπ²." + ) + pad_mode = tu.get_non_tensor_data(batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + temperature = batch["temperature"] + model_inputs = self.prepare_model_inputs(batch) + input_ids = model_inputs["input_ids"] + attention_mask = model_inputs["attention_mask"] + multi_modal_inputs = model_inputs["multi_modal_inputs"] + local_cp_size = tu.get_non_tensor_data(data=batch, key="local_cp_size", default=None) + loss_mask = model_inputs["loss_mask"] + + unwrapped_model = unwrap_model(model) + if hasattr(unwrapped_model, "vp_stage"): + vp_rank = unwrapped_model.vp_stage + else: + vp_rank = 0 + + if RouterReplayHelper.is_replay_backward_action(self.tf_config, vp_rank): + router_instance_list = RouterReplayHelper.get_micro_batch_router_list(self.tf_config, vp_rank) + for router in router_instance_list: + router.set_router_replay_action(RouterReplayAction.REPLAY_FORWARD) + + if RouterReplayHelper.is_replay_forward_action(self.tf_config, vp_rank): + layers_topk_idx = model_inputs["routed_experts"] + replay_mask = None + if self.engine_config.router_replay.mode == "R3": + replay_mask = build_r3_replay_mask(input_ids, batch["response_mask"]) + set_router_replay_data( + layers_topk_idx, + None, + self.tf_config, + vp_rank, + replay_mask=replay_mask, + ) + + if pad_mode == DatasetPadMode.NO_PADDING: + label = input_ids.clone() + else: + raise NotImplementedError(f"Pad mode {pad_mode} is not supported for megatron engine") + + if use_fused_kernels: + if not self.engine_config.use_remove_padding: + logger.warning_once( + "Fused kernels require `use_remove_padding=True` for Megatron engine. Falling back to non-fused." + ) + use_fused_kernels = False + elif isinstance(temperature, torch.Tensor): + if temperature.numel() != 1: + logger.warning_once( + "Fused kernels do not support per-sample temperature. Falling back to non-fused." + ) + use_fused_kernels = False + else: + temperature_value = float(temperature.item()) + else: + temperature_value = float(temperature) + + if use_fused_kernels: + from verl.models.mcore import get_mcore_forward_fused_model_engine_fn + + fused_forward_fn = get_mcore_forward_fused_model_engine_fn(self.model_config.hf_config) + output = fused_forward_fn( + model=model, + input_ids=input_ids, + labels=label, + multi_modal_inputs=multi_modal_inputs, + temperature=temperature_value, + calculate_entropy=calculate_entropy, + pad_token_id=self.model_config.tokenizer.pad_token_id, + ) + else: + if not isinstance(temperature, torch.Tensor): + temperature = torch.tensor([temperature] * input_ids.shape[0], device=input_ids.device) + + temperature = temperature.to(torch.float32) + assert temperature.shape[0] == input_ids.shape[0] + temperature = verl_F.expand_as_nested(temperature, input_ids) # (bsz, j1) + from verl.models.mcore import get_mcore_engine_forward_fn + + forward_fn = get_mcore_engine_forward_fn(self.model_config.hf_config) + data_format = "thd" if self.engine_config.use_remove_padding else "bshd" + + logits_processor = partial( + self._lm_head_logits_processor, + calculate_sum_pi_squared=calculate_sum_pi_squared, + calculate_entropy=calculate_entropy, + distillation_use_topk=distillation_use_topk, + distillation_only=distillation_only, + logits_processor_func=logits_processor_func, + batch=batch, + data_format=data_format, + ) + + response_attention_mask = None + if attention_mask is not None and not loss_mask.is_nested: + response_attention_mask = attention_mask[:, -loss_mask.shape[-1] :] + logits_processor_args = { + "label": label, + "temperature": temperature, + "loss_mask": loss_mask, + "response_attention_mask": response_attention_mask, + } + + output = forward_fn( + model, + input_ids, + multi_modal_inputs, + logits_processor=logits_processor, + logits_processor_args=logits_processor_args, + vision_model=hasattr(self.model_config.hf_config, "vision_config"), + pad_token_id=self.model_config.tokenizer.pad_token_id, + data_format=data_format, + mtp_enable_train=self.model_config.mtp.enable and self.model_config.mtp.enable_train, + local_cp_size=local_cp_size, + forced_max_seqlen=tu.get_non_tensor_data(data=batch, key="forced_max_seqlen", default=None), + ) + + # Router replay: record routing decisions for R2 mode + if RouterReplayHelper.is_r2_record_action(self.tf_config, vp_rank): + merge_router_topk_indices(None, input_ids, self.mini_layer_topk_idx_list, self.tf_config, vp_rank) + + # Router replay: switch to backward replay mode for next backward pass + if RouterReplayHelper.is_replay_forward_action(self.tf_config, vp_rank): + router_instance_list = RouterReplayHelper.get_micro_batch_router_list(self.tf_config, vp_rank) + for router in router_instance_list: + router.set_router_replay_action(RouterReplayAction.REPLAY_BACKWARD) + + return output, partial(postprocess_micro_batch_func, data=batch, local_cp_size=local_cp_size) + + def postprocess_micro_batch_func( + self, output, data: TensorDict, forward_only: bool, loss_function, local_cp_size=None + ): + # For memory efficiency + # We move calculation of entropy to compute_log_probs, forward_only == True + device = data["input_ids"].device + model_output = self.prepare_model_outputs(output, data) + + if loss_function is not None: + # TODO(baiyan): How to support hybrid context parallel with dp_group, + # now the dp_group is not used, so just leave it as is, but what if we need to use it? + loss, metrics = loss_function(model_output=model_output, data=data, dp_group=self.get_data_parallel_group()) + # scale loss by num_micro_batch because megatron will scale loss + # by n_micro_batch inside pp schedule + scaled_loss = loss * data["num_micro_batch"] + else: + assert forward_only, "forward_only must be True when loss_function is None" + loss = torch.tensor(1.0, device=device) + scaled_loss = loss + metrics = {} + if local_cp_size is not None: + # aggregate model_output by DP-CP groups + from verl.utils.megatron_utils import dynamic_cp_merge_output + + model_output = dynamic_cp_merge_output( + model_output, + dp_size=mpu.get_data_parallel_world_size(), + dp_rank=mpu.get_data_parallel_rank(), + local_cp_size=local_cp_size, + ) + + output = { + "model_output": model_output, + "loss": loss.detach().item(), + "metrics": metrics, + } + + # calculate_per_token_loss=True (auto-enabled by Megatron-Bridge at CP>1) puts + # Megatron in its per-token regime: loss_func must return (loss_sum, num_tokens, + # output), and finalize_model_grads divides every gradient by the accumulated + # total_num_tokens. That division is what cancels the MoE router's pre-multiplication + # of the aux/z loss by num_tokens; a 2-tuple leaves total_num_tokens=0, so the factor + # is never cancelled (the ~1e4 grad_norm blow-up at CP>1). + if self.tf_config is not None and self.tf_config.calculate_per_token_loss and loss_function is not None: + # seq-mean-token-mean is the one incompatible agg mode: its per-sequence 1/n_s + # uses CP-local shard counts that diverge from the global normalization. The + # other modes compose correctly across CP shards. + if hasattr(loss_function, "keywords") and "config" in loss_function.keywords: + _agg_mode = getattr(loss_function.keywords["config"], "loss_agg_mode", None) + if _agg_mode == "seq-mean-token-mean": + raise ValueError( + "loss_agg_mode='seq-mean-token-mean' is incompatible with " + "calculate_per_token_loss=True (auto-enabled by Megatron-Bridge " + "under CP>1). The per-sequence inner division by n_s requires " + "local-shard counts that diverge from global under CP. Use one " + "of: 'token-mean', 'seq-mean-token-sum', 'seq-mean-token-sum-norm'." + ) + # verl never passes a router padding_mask, so the MoE router normalizes the + # aux/z loss by logits.shape[0]. THD packs padding out -> that equals the real + # token count; BSHD leaves it at B*S (padding-inclusive), while gradients are + # divided by the real token count -> a padding-ratio mis-normalization. + if not self.engine_config.use_remove_padding: + raise ValueError( + "calculate_per_token_loss=True requires use_remove_padding=True. " + "verl does not pass a padding_mask to the MoE router, so in BSHD it " + "normalizes the aux/z loss by the padding-inclusive token count (B*S) " + "while gradients are divided by the real token count. Use THD " + "(use_remove_padding=True) or disable CP." + ) + # finalize_model_grads all-reduces the returned token count over the DP*CP group + # and divides every gradient by it. Real tokens are CP-replicated across the CP + # ranks, so report the per-CP-rank share (/cp_size); otherwise that DP*CP sum + # over-counts by cp_size and every gradient comes out 1/cp_size too small. + cp_size = self.engine_config.context_parallel_size + local_num_tokens = (self._routed_num_tokens(data) // cp_size).to(torch.int) + # n_i is the global routed-token count (all-reduced in forward_backward_batch); + # scaling loss by the same value makes Sum(L_i)/Sum(n_i) recover the loss. Falls + # back to local counts when not plumbed (single-rank / tests). + routed_num_tokens = data["routed_num_tokens"] if "routed_num_tokens" in data.keys() else None + if routed_num_tokens is None: + routed_num_tokens = self._routed_num_tokens(data) + dp_size = data["dp_size"] if "dp_size" in data.keys() else 1 + local_sum = loss * routed_num_tokens / dp_size + return local_sum, local_num_tokens, output + + # return loss and stats + return scaled_loss, output + + +@EngineRegistry.register(model_type="value_model", backend="megatron") +class MegatronEngineWithValueHead(MegatronEngineWithLMHead): + # for value head + def forward_step(self, batch_iter, model, logits_processor_func, postprocess_micro_batch_func): + batch: TensorDict = next(batch_iter) + batch = batch.to(get_device_id()) + model_inputs = self.prepare_model_inputs(batch) + input_ids = model_inputs["input_ids"] + multi_modal_inputs = model_inputs["multi_modal_inputs"] + + from verl.models.mcore import get_mcore_engine_forward_fn + + forward_fn = get_mcore_engine_forward_fn(self.model_config.hf_config) + + output = forward_fn( + model, + input_ids, + multi_modal_inputs, + value_model=True, + vision_model=hasattr(self.model_config.hf_config, "vision_config"), + pad_token_id=self.model_config.tokenizer.pad_token_id, + data_format="thd" if self.engine_config.use_remove_padding else "bshd", + forced_max_seqlen=tu.get_non_tensor_data(data=batch, key="forced_max_seqlen", default=None), + ) + + return output, partial(postprocess_micro_batch_func, data=batch) + + def prepare_model_outputs(self, output: dict | torch.Tensor, data: TensorDict): + return {"values": output} diff --git a/verl_0720_main/verl/verl/workers/engine/megatron/utils.py b/verl_0720_main/verl/verl/workers/engine/megatron/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..0d9f3b8aadfffe82e17c04094805baa3751c4e7c --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/megatron/utils.py @@ -0,0 +1,35 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +from verl.utils.device import get_torch_device + + +def set_random_seed(seed): + import random + + import numpy as np + import torch + + torch.manual_seed(seed) + np.random.seed(seed) + random.seed(seed) + if get_torch_device().device_count() > 0: + from megatron.core import tensor_parallel + + tensor_parallel.model_parallel_cuda_manual_seed(seed) + # FIXME: torch cumsum not support deterministic (used in vllm sampler), + # https://github.com/pytorch/pytorch/issues/89492 + # torch.use_deterministic_algorithms(True, warn_only=True) + # os.environ['CUBLAS_WORKSPACE_CONFIG'] = ':4096:8' diff --git a/verl_0720_main/verl/verl/workers/engine/mindspeed/__init__.py b/verl_0720_main/verl/verl/workers/engine/mindspeed/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..4b3cfa1304ec3b6cb6219cdedca8c2f861b22c28 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/mindspeed/__init__.py @@ -0,0 +1,17 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .transformer_impl import MindspeedEngineWithLMHead, MindspeedEngineWithValueHead, MindSpeedMegatronEngineWithLMHead + +__all__ = ["MindspeedEngineWithLMHead", "MindspeedEngineWithValueHead", "MindSpeedMegatronEngineWithLMHead"] diff --git a/verl_0720_main/verl/verl/workers/engine/mindspeed/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/mindspeed/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..6fa78d901b38acdc7ef608edce9470ad43de3d6b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/mindspeed/transformer_impl.py @@ -0,0 +1,173 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import logging +import os + +try: + from mindspeed.megatron_adaptor import repatch +except ImportError: + repatch = None + +from verl.trainer.config import CheckpointConfig +from verl.utils.megatron.router_replay_patch import RouterReplay +from verl.utils.model import print_model_size +from verl.workers.config import ( + HFModelConfig, + McoreEngineConfig, + McoreOptimizerConfig, + MindSpeedEngineConfig, +) + +from ..base import EngineRegistry +from ..megatron import MegatronEngineWithLMHead, MegatronEngineWithValueHead +from .utils import ( + apply_patch, + gpt_model_provider, + reset_fp8_reuse_quantized_weight, +) + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +def _mindspeed_repatch(engine_config): + if repatch is not None: + from verl.utils.megatron_utils import mapping_string_to_attn_backend + + repatch_config = mapping_string_to_attn_backend(dict(engine_config.get("override_transformer_config", {}))) + # flash-attn-npu batch-invariant replaces DotProductAttention.forward; fusion attention + # registers the same patch when use_flash_attn=True and causes "the patch of forward exist". + if repatch_config.get("use_flash_attn_npu_batch_invariant"): + repatch_config["use_flash_attn"] = False + else: + repatch_config.setdefault("use_flash_attn", True) + if engine_config.context_parallel_size > 1: + repatch_config["context_parallel_size"] = engine_config.context_parallel_size + repatch(repatch_config) + + +@EngineRegistry.register(model_type="language_model", backend="megatron", device="npu") +class MindspeedEngineWithLMHead(MegatronEngineWithLMHead): + def __init__( + self, + model_config: HFModelConfig, + engine_config: McoreEngineConfig, + optimizer_config: McoreOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + super().__init__(model_config, engine_config, optimizer_config, checkpoint_config) + + def _init_device_mesh(self): + # repatch must happen before initialize_model_parallel so that + # initialize_model_parallel_cp_wrapper is in effect when the call is made. + # The initial MindSpeed patch pass sees context_parallel_size=1 (default) because + # verl passes CP size via hydra config rather than --context-parallel-size CLI arg, + # so the CP ring-rank initialization wrapper is not registered on the first pass. + _mindspeed_repatch(self.engine_config) + super()._init_device_mesh() + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move model parameters, optimizer states, or both to the specified device. + Note that this function executes irrespective of offload config. It serves as manual control + + Args: + device: Target device identifier. + model: If True, move the model. + optimizer: If True, move the optimizer states. + """ + reset_fp8_reuse_quantized_weight(self, device, model, optimizer, grad) + super().to(device=device, model=model, optimizer=optimizer, grad=grad) + + +@EngineRegistry.register(model_type="value_model", backend="megatron", device="npu") +class MindspeedEngineWithValueHead(MegatronEngineWithValueHead): + def __init__( + self, + model_config: HFModelConfig, + engine_config: McoreEngineConfig, + optimizer_config: McoreOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + super().__init__(model_config, engine_config, optimizer_config, checkpoint_config) + + def _init_device_mesh(self): + # repatch must happen before initialize_model_parallel so that + # initialize_model_parallel_cp_wrapper is in effect when the call is made. + # The initial MindSpeed patch pass sees context_parallel_size=1 (default) because + # verl passes CP size via hydra config rather than --context-parallel-size CLI arg, + # so the CP ring-rank initialization wrapper is not registered on the first pass. + _mindspeed_repatch(self.engine_config) + super()._init_device_mesh() + + +@EngineRegistry.register(model_type="language_model", backend="mindspeed_megatron", device="npu") +class MindSpeedMegatronEngineWithLMHead(MegatronEngineWithLMHead): + def __init__( + self, + model_config: HFModelConfig, + engine_config: MindSpeedEngineConfig, + optimizer_config: McoreOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + super().__init__(model_config, engine_config, optimizer_config, checkpoint_config) + + def _init_device_mesh(self): + apply_patch(self.model_config, self.engine_config, self.optimizer_config) + super()._init_device_mesh() + + def _build_megatron_module(self): + is_value_model = ( + "ForTokenClassification" in self.model_config.architectures[0] + or "ForSequenceClassification" in self.model_config.architectures[0] + ) + + self.is_value_model = is_value_model + + import torch.distributed + from megatron.core.enums import ModelType + from megatron.training.training import get_model + + # For forward_only, we don't need optimizer, lr_scheduler, checkpoint_mananager + if self.engine_config.forward_only: + module = get_model(gpt_model_provider, ModelType.encoder_or_decoder, wrap_with_ddp=False) + else: + module = get_model(gpt_model_provider, ModelType.encoder_or_decoder, wrap_with_ddp=True) + + if self.vanilla_bridge: + self.bridge.load_weights(module, self.model_config.local_path) + else: + raise ValueError(f"vanilla_bridge should be true now, but got {self.vanilla_bridge}") + + if torch.distributed.get_rank() == 0: + print_model_size(module[0]) + + if self.enable_routing_replay: + print(f"routing replay layers: {len(RouterReplay.router_instances)}") + + return module + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move model parameters, optimizer states, or both to the specified device. + Note that this function executes irrespective of offload config. It serves as manual control + + Args: + device: Target device identifier. + model: If True, move the model. + optimizer: If True, move the optimizer states. + """ + reset_fp8_reuse_quantized_weight(self, device, model, optimizer, grad) + super().to(device=device, model=model, optimizer=optimizer, grad=grad) diff --git a/verl_0720_main/verl/verl/workers/engine/mindspeed/utils.py b/verl_0720_main/verl/verl/workers/engine/mindspeed/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..42e949d42079ee336cc1489457da9a6014963acf --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/mindspeed/utils.py @@ -0,0 +1,240 @@ +# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import argparse + +import torch + +from verl.workers.config import HFModelConfig, McoreOptimizerConfig, MindSpeedEngineConfig + + +def get_base_mcore_config_from_model_config(model_config: HFModelConfig) -> dict: + """ + Create a base TransformerConfig with common parameters across different model architectures. + + Args: + model_config: HuggingFace model configuration + + Returns: + TransformerConfig with common parameters + """ + + from verl.models.mcore.config_converter import get_hf_rope_theta + + hf_config = model_config.hf_config + base_config = { + "num_layers": hf_config.num_hidden_layers, + "hidden_size": hf_config.hidden_size, + "num_attention_heads": hf_config.num_attention_heads, + "num_query_groups": hf_config.num_key_value_heads, + "ffn_hidden_size": hf_config.intermediate_size, + "attention_dropout": hf_config.attention_dropout, + "hidden_dropout": getattr(hf_config, "hidden_dropout", 0.0), + "kv_channels": getattr(hf_config, "head_dim", None), + "norm_topk_prob": getattr(hf_config, "norm_topk_prob", False), + "layernorm_epsilon": hf_config.rms_norm_eps, + "max_position_embeddings": hf_config.max_position_embeddings, + "tie_word_embeddings": hf_config.tie_word_embeddings, + "torch_dtype": hf_config.torch_dtype, + "bf16": hf_config.dtype is torch.bfloat16, + "rotary_base": get_hf_rope_theta(hf_config), + "num_experts": getattr(hf_config, "num_experts", None), + "moe_router_topk": getattr(hf_config, "num_experts_per_tok", None), + "moe_ffn_hidden_size": getattr(hf_config, "moe_intermediate_size", None), + "padded_vocab_size": hf_config.vocab_size, + "make_vocab_size_divisible_by": 1, + "untie_embeddings_and_output_weights": True, + } + + tokenizer_config = { + "tokenizer_name_or_path": model_config.tokenizer_path, + "tokenizer_type": "PretrainedFromHF", + } + base_config.update(tokenizer_config) + return base_config + + +def get_base_mcore_config_from_engine_config(engine_config: MindSpeedEngineConfig) -> dict: + """ + Create a base TransformerConfig with common parameters across different model architectures. + + Args: + engine_config: mindspeed engine configuration + + Returns: + TransformerConfig with common parameters + """ + + base_config = { + "tensor_model_parallel_size": engine_config.tensor_model_parallel_size, + "expert_model_parallel_size": engine_config.expert_model_parallel_size, + "expert_tensor_parallel_size": engine_config.expert_tensor_parallel_size, + "pipeline_model_parallel_size": engine_config.pipeline_model_parallel_size, + "virtual_pipeline_model_parallel_size": engine_config.virtual_pipeline_model_parallel_size, + "context_parallel_size": engine_config.context_parallel_size, + "sequence_parallel": engine_config.sequence_parallel, + "use_distributed_optimizer": engine_config.use_distributed_optimizer, + "seed": engine_config.seed, + } + + base_config.update(engine_config.mcore_kwargs) + return base_config + + +def get_base_mcore_config_from_optim_config(optim_config: McoreOptimizerConfig) -> dict: + """ + Create a base TransformerConfig with common parameters across different model architectures. + + Args: + optim_config: megatron optimizer configuration + + Returns: + TransformerConfig with common parameters + """ + + base_config = { + "lr": optim_config.lr, + "lr_decay_style": optim_config.lr_decay_style, + "min_lr": optim_config.min_lr, + "weight_decay": optim_config.weight_decay, + "lr_warmup_fraction": optim_config.lr_warmup_steps_ratio, + "clip_grad": optim_config.clip_grad, + "adam_beta1": optim_config.betas[0], + "adam_beta2": optim_config.betas[1], + } + + base_config.update(optim_config.override_optimizer_config) + return base_config + + +def set_global_config(config): + from megatron.training.arguments import parse_args, validate_args + from megatron.training.global_vars import set_global_variables + + args = parse_args(ignore_unknown_args=True) + for key, value in config.items(): + setattr(args, key, value) + + validate_args(args) + try: + set_global_variables(args) + except AssertionError: + print("megatron args already set") + + +def add_mcore_arguments(all_config: dict) -> dict: + mcore_config_dict = {} + mcore_config_list = [] + for key, value in all_config.items(): + if value is None: + continue + mcore_config_dict[key] = value + if isinstance(value, bool): + mcore_config_list.append(f"--{key.replace('_', '-')}") + + from megatron.training.arguments import add_megatron_arguments + + parser = argparse.ArgumentParser(description="Megatron-LM Arguments", allow_abbrev=False) + parser = add_megatron_arguments(parser) + args, _ = parser.parse_known_args(mcore_config_list) + return {**vars(args), **mcore_config_dict} + + +def apply_patch(model_config, engine_config, optimizer_config): + model_config = get_base_mcore_config_from_model_config(model_config) + optimizer_config = get_base_mcore_config_from_optim_config(optimizer_config) + engine_config = get_base_mcore_config_from_engine_config(engine_config) + all_config = {**model_config, **optimizer_config, **engine_config} + mcore_config = add_mcore_arguments(all_config) + from mindspeed_llm.tasks.megatron_adaptor_v2 import repatch + + repatch(mcore_config) + set_global_config(mcore_config) + + +def gpt_model_provider(pre_process=True, post_process=True): + """ + Builds the model. + + If you set the use_mcore_models to True, it will return the mcore GPT model and if not the legacy GPT model. + + Args: + pre_process (bool, optional): Set to true if you need to compute embedings. Defaults to True. + post_process (bool, optional): Set to true if you need to want to compute output logits/loss. + Defaults to True. + + + Returns: + Union[GPTModel, megatron.legacy.model.GPTModel]: The returned model + """ + from megatron.core.models.gpt import GPTModel + from megatron.core.models.gpt.gpt_layer_specs import ( + get_gpt_layer_local_spec, + get_gpt_layer_with_transformer_engine_spec, + ) + from megatron.core.transformer.spec_utils import import_module + from megatron.training import get_args + from megatron.training.arguments import core_transformer_config_from_args + + args = get_args() + use_te = args.transformer_impl == "transformer_engine" + # Experimental loading arguments from configs + config = core_transformer_config_from_args(args) + + if args.spec is not None: + transformer_layer_spec = import_module(args.spec) + else: + if use_te: + transformer_layer_spec = get_gpt_layer_with_transformer_engine_spec( + args.num_experts, args.moe_grouped_gemm, qk_layernorm=args.qk_layernorm + ) + else: + transformer_layer_spec = get_gpt_layer_local_spec( + args.num_experts, args.moe_grouped_gemm, qk_layernorm=args.qk_layernorm + ) + + model = GPTModel( + config=config, + transformer_layer_spec=transformer_layer_spec, + vocab_size=args.padded_vocab_size, + max_sequence_length=args.max_position_embeddings, + pre_process=pre_process, + post_process=post_process, + fp16_lm_cross_entropy=args.fp16_lm_cross_entropy, + parallel_output=True, + share_embeddings_and_output_weights=not args.untie_embeddings_and_output_weights, + position_embedding_type=args.position_embedding_type, + rotary_percent=args.rotary_percent, + seq_len_interpolation_factor=args.rotary_seq_len_interpolation_factor, + ) + + return model + + +def reset_fp8_reuse_quantized_weight(engine, device: str, model: bool, optimizer: bool, grad: bool): + override_config = getattr(engine.engine_config, "override_transformer_config", None) + if override_config and override_config.get("fp8_reuse_quantized_weight", False): + from mindspeed.te.pytorch.fp8.reuse import ( + clear_weight_quantization_reuse_cache, + set_weight_release_enabled, + ) + + # clear quantized weights on NPU + clear_weight_quantization_reuse_cache(release_storage=True) + + # enable release high-precision weights only when all modules are in training mode. For ref model, + # we need to keep its high-precision weights for offloading. For actor_update model, the high-precision + # weights will be released if possible, and then recovered before optimizer step + set_weight_release_enabled(getattr(engine, "mode", None) == "train") diff --git a/verl_0720_main/verl/verl/workers/engine/spec.py b/verl_0720_main/verl/verl/workers/engine/spec.py new file mode 100644 index 0000000000000000000000000000000000000000..17921e0fb89e2761d737af1d133b512aa0789d6e --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/spec.py @@ -0,0 +1,113 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""The shard-export contract between training engines and the sharded delta engine. + +``BaseEngine.get_per_tensor_param_shard`` yields ``(name, local_shard, ShardSpec)`` +per local parameter, in an order identical on every rank. The spec describes the +parameter's distribution declaratively with torch's own vocabulary -- a +:class:`~torch.distributed.device_mesh.DeviceMesh` plus +:class:`~torch.distributed.tensor.placement_types.Placement` per mesh dim -- and +the engine derives everything else (this rank's flat offset, the gather group, +whether this rank contributes) via ``compute_local_shape_and_global_offset``. +DTensor-based trainers (FSDP, veomni, ...) pass ``param.device_mesh`` / +``param.placements`` verbatim; ``mesh=None`` means the local tensor already is +the whole parameter (replicated / unsharded). + +``to_hf`` is reserved for trainers whose logical parameter differs from the HF +tensor(s) (e.g. Megatron fused qkv): a pure-permutation callable mapping the +gather group's dense shards to ``[(hf_name, hf_tensor)]``. It is None for +DTensor trainers and lands with the Megatron follow-up PR. +""" + +from __future__ import annotations + +from dataclasses import dataclass +from typing import Callable, Optional + +import torch +from torch.distributed.tensor import DTensor +from torch.distributed.tensor._utils import compute_local_shape_and_global_offset + +__all__ = ["ShardSpec", "derive_placement"] + + +@dataclass +class ShardSpec: + """Declarative placement descriptor for one exported local parameter shard.""" + + # Logical (full) tensor shape; the distribution facts below refer to it. + full_shape: tuple + # Distribution: torch DeviceMesh + per-mesh-dim Placement. None = unsharded. + mesh: Optional[object] = None + placements: Optional[tuple] = None + # Reserved (Megatron follow-up): pure-permutation shards -> [(hf_name, hf_tensor)]. + to_hf: Optional[Callable[[list[torch.Tensor]], list[tuple[str, torch.Tensor]]]] = None + + @classmethod + def from_param(cls, param: torch.Tensor) -> ShardSpec: + if isinstance(param, DTensor): + return cls(full_shape=tuple(param.shape), mesh=param.device_mesh, placements=tuple(param.placements)) + return cls(full_shape=tuple(param.shape)) + + +def _prod(xs) -> int: + n = 1 + for x in xs: + n *= int(x) + return n + + +def derive_placement(spec: ShardSpec): + """Derive ``(flat_offset, contributes, gather_group)`` for THIS rank from the spec. + + * unsharded (``mesh is None``): offset 0; only global rank 0 contributes; no group + (the local tensor is already the full parameter). + * ``Shard(0)`` over one mesh dim (+ any number of ``Replicate`` dims): the flat + offset comes from ``compute_local_shape_and_global_offset`` (pure math, no + collective); only ranks at coordinate 0 of every Replicate dim contribute; the + gather group is the Shard dim's subgroup. + + Other shard dims raise -- same scope as the export that produces the spec. + """ + import torch.distributed as dist + + if spec.mesh is None: + return 0, (dist.get_rank() == 0 if dist.is_initialized() else True), None + + placements = spec.placements + shard_dims = [d for d, p in enumerate(placements) if p.is_shard()] + for d in shard_dims: + if placements[d].dim != 0: + raise NotImplementedError( + f"sharded delta only supports Shard(0) (FSDP2 default); got placements={placements}" + ) + assert len(shard_dims) <= 1, f"at most one Shard dim is supported; got placements={placements}" + + coord = spec.mesh.get_coordinate() + contributes = True + if coord is not None: + for d, p in enumerate(placements): + if p.is_replicate() and coord[d] != 0: + contributes = False + break + + if not shard_dims: + # replicated across every mesh dim: full tensor on each rank, no gather + return 0, contributes, None + + _, global_offset = compute_local_shape_and_global_offset(spec.full_shape, spec.mesh, list(placements)) + inner = _prod(spec.full_shape[1:]) + offset = int(global_offset[0]) * inner + group = spec.mesh.get_group(mesh_dim=shard_dims[0]) + return offset, contributes, group diff --git a/verl_0720_main/verl/verl/workers/engine/torchtitan/__init__.py b/verl_0720_main/verl/verl/workers/engine/torchtitan/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..345757277af1504e14f8921143b0afc04d897554 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/torchtitan/__init__.py @@ -0,0 +1,16 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from .transformer_impl import TorchTitanEngine, TorchTitanEngineWithLMHead + +__all__ = ["TorchTitanEngine", "TorchTitanEngineWithLMHead"] diff --git a/verl_0720_main/verl/verl/workers/engine/torchtitan/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/torchtitan/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..af03afb36a53bf63d03013c4f137ba13bb109960 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/torchtitan/transformer_impl.py @@ -0,0 +1,766 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +The concrete Engine implementation using PyTorch TorchTitan parallelism (FSDP2 + TP + PP) +""" + +import gc +import importlib +import logging +import os +import re +from contextlib import nullcontext +from typing import Any, Callable, Optional + +import torch +import torch.distributed +from tensordict import TensorDict +from torch.distributed.tensor import DTensor +from torchtitan.components.checkpoint import CheckpointManager +from torchtitan.components.loss import CrossEntropyLoss +from torchtitan.components.lr_scheduler import LRSchedulersContainer +from torchtitan.components.optimizer import OptimizersContainer, ParamGroupConfig +from torchtitan.config import CompileConfig, ParallelismConfig, TrainingConfig +from torchtitan.distributed import utils as dist_utils +from torchtitan.distributed.activation_checkpoint import FullAC, SelectiveAC +from torchtitan.distributed.context_parallel import prepare_context_parallel_input +from torchtitan.distributed.parallel_dims import ParallelDims +from torchtitan.train import Trainer + +import verl.utils.torch_functional as verl_F +from verl.trainer.config import CheckpointConfig +from verl.utils import tensordict_utils as tu +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.debug import log_gpu_memory_usage +from verl.utils.device import get_device_id, get_device_name +from verl.utils.fsdp_utils import ( + load_fsdp_model_to_gpu, + load_fsdp_optimizer, + offload_fsdp_model_to_cpu, + offload_fsdp_optimizer, +) +from verl.utils.model import extract_multi_modal_inputs +from verl.utils.torch_functional import logprobs_from_logits +from verl.workers.config import HFModelConfig, TorchtitanEngineConfig, TorchtitanOptimizerConfig +from verl.workers.engine.torchtitan.utils import ( + NoOpDataLoader, + derive_torchtitan_name_and_flavor, + enable_fsdp_gradient_division, + get_attention_masks, + iter_per_tensor_params_ep, +) + +from ..base import BaseEngine, BaseEngineCtx, EngineRegistry +from ..utils import enable_full_determinism, postprocess_batch_func, prepare_micro_batches + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +device_name = get_device_name() + + +class TorchTitanEngine(BaseEngine): + """ + Concrete Engine implementation using PyTorch TorchTitan parallelism. + + Supports model sharding with FSDP2, tensor parallelism, activation/optimizer offloading, + LoRA, and sequence parallelism following the TorchTitan design. + """ + + def __init__( + self, + model_config: HFModelConfig, + engine_config: TorchtitanEngineConfig, + optimizer_config: TorchtitanOptimizerConfig, + checkpoint_config: CheckpointConfig, + ): + """ + Initialize the TorchTitanEngine. + + Sets up distributed device meshes for tensor and data parallelism, LoRA, and offload policies. + + Args: + model_config: Configuration for HuggingFace model. + engine_config: Configuration for FSDP/TorchTitan engine (uses FSDP2). + optimizer_config: Configuration for optimizer. + checkpoint_config: Configuration for checkpointing. + """ + super().__init__() + + self.model_config = model_config + self.engine_config = engine_config + self.optimizer_config = optimizer_config + self.checkpoint_config = checkpoint_config + + # Derive torchtitan model name and flavor from HF config + torchtitan_name, torchtitan_flavor = derive_torchtitan_name_and_flavor(self.model_config.hf_config) + + # Get ModelSpec from model registry + model_module = importlib.import_module(f"torchtitan.models.{torchtitan_name}") + model_spec = model_module.model_registry(torchtitan_flavor, attn_backend=self.engine_config.attn_type) + + optimizer = OptimizersContainer.Config( + param_groups=[ + ParamGroupConfig( + pattern=r".*", + optimizer_name=self.optimizer_config.name, + optimizer_kwargs={ + "lr": self.optimizer_config.lr, + "eps": self.optimizer_config.eps, + "betas": (self.optimizer_config.betas[0], self.optimizer_config.betas[1]), + "weight_decay": self.optimizer_config.weight_decay, + }, + ) + ], + ) + + total_steps = self.optimizer_config.total_training_steps + lr_warmup_steps = self.optimizer_config.lr_warmup_steps + if lr_warmup_steps is None or lr_warmup_steps <= 0: + lr_warmup_steps = int(self.optimizer_config.lr_warmup_steps_ratio * total_steps) + + lr_scheduler = LRSchedulersContainer.Config( + warmup_steps=lr_warmup_steps, + decay_type=self.optimizer_config.decay_type, + min_lr_factor=self.optimizer_config.min_lr_factor, + ) + parallelism = ParallelismConfig( + data_parallel_replicate_degree=self.engine_config.data_parallel_replicate_size, + data_parallel_shard_degree=self.engine_config.data_parallel_shard_size, + fsdp_reshard_after_forward=self.engine_config.reshard_after_forward, + tensor_parallel_degree=self.engine_config.tensor_parallel_size, + pipeline_parallel_degree=self.engine_config.pipeline_parallel_size, + context_parallel_degree=self.engine_config.context_parallel_size, + expert_parallel_degree=self.engine_config.expert_parallel_size, + spmd_backend=self.engine_config.spmd_backend, + ) + checkpoint = CheckpointManager.Config( + enable=True, + initial_load_in_hf=True, + initial_load_model_only=True, + initial_load_path=model_config.path, + ) + compile_config = CompileConfig(enable=self.engine_config.use_torch_compile) + training_kwargs = {} + if self.engine_config.max_seq_len is not None: + training_kwargs["seq_len"] = self.engine_config.max_seq_len + if self.engine_config.offload_policy or self.engine_config.forward_only: + training = TrainingConfig(enable_cpu_offload=True, **training_kwargs) + else: + training = TrainingConfig(**training_kwargs) + + # Activation checkpointing mode. Note: under spmd_backend="spmd_types" with + # eager execution (use_torch_compile=False), selective/full AC recompute runs + # on the autograd backward thread where the thread-local SPMD mesh is inactive, + # so spmd.assert_type() raises "no current mesh". Set activation_checkpoint="none" + # (or enable torch.compile, which recomputes in-graph) in that configuration. + activation_checkpoint = { + "selective": SelectiveAC.Config, + "full": FullAC.Config, + "none": lambda: None, + }[self.engine_config.activation_checkpoint]() + + # Construct Torchtitan's Trainer.Config + self.config = Trainer.Config( + model_spec=model_spec, + hf_assets_path=self.model_config.path, + optimizer=optimizer, + lr_scheduler=lr_scheduler, + parallelism=parallelism, + checkpoint=checkpoint, + compile=compile_config, + training=training, + activation_checkpoint=activation_checkpoint, + # Use a no-op dataloader since verl has its own data loading + dataloader=NoOpDataLoader.Config(), + # Provide a concrete loss so Trainer.__init__ can build it; + # verl uses its own loss function and ignores this one. + loss=CrossEntropyLoss.Config(), + ) + self.trainer = Trainer(self.config) + + self._init_device_mesh() + + # Re-enable FSDP's gradient division for verl's loss scaling. + # TorchTitan disables gradient division by default (for global token normalization), + # but verl's loss function multiplies by dp_size to compensate for gradient averaging. + if self.engine_config.data_parallel_shard_size > 1: + dp_size = self.get_data_parallel_size() + for model_part in self.trainer.model_parts: + enable_fsdp_gradient_division(model_part, dp_size) + + if self.engine_config.full_determinism: + enable_full_determinism(seed=self.engine_config.seed) + + # set FSDP offload params + self._is_offload_param = self.engine_config.param_offload + self._is_offload_optimizer = self.engine_config.optimizer_offload + + if self.engine_config.entropy_from_logits_with_chunking: + entropy_from_logits = verl_F.entropy_from_logits_with_chunking + else: + entropy_from_logits = verl_F.entropy_from_logits + + self.compute_entropy_from_logits = ( + torch.compile(entropy_from_logits, dynamic=True) + if self.engine_config.use_torch_compile + else entropy_from_logits + ) + + @property + def is_param_offload_enabled(self) -> bool: + return self._is_offload_param + + @property + def is_optimizer_offload_enabled(self) -> bool: + return self._is_offload_optimizer + + def is_mp_src_rank_with_outputs(self): + """ + Whether the current rank is the first rank in model parallel group that contains model outputs + """ + is_collect = True + # TP: outputs are on TP rank 0 + if self.parallel_dims.tp > 1: + tp_mesh = self.parallel_dims.get_optional_mesh("tp") + is_collect = is_collect and (tp_mesh.get_local_rank() == 0) + # PP: outputs are on the last PP rank + if self.parallel_dims.pp > 1: + pp_mesh = self.parallel_dims.get_optional_mesh("pp") + is_collect = is_collect and (pp_mesh.get_local_rank() == self.parallel_dims.pp - 1) + # CP: outputs are on CP rank 0 + if self.parallel_dims.cp > 1: + cp_mesh = self.parallel_dims.get_optional_mesh("cp") + is_collect = is_collect and (cp_mesh.get_local_rank() == 0) + return is_collect + + def initialize(self): + """ + Build the model, optimizer, and learning rate scheduler with TorchTitan parallelism. + + Applies device, dtype, and precision configurations, including mixed precision. + Sets up checkpoint manager. + """ + self.module = self.trainer.model_parts + self.checkpointer = self.trainer.checkpointer + # load initial HF weights + self.checkpointer.load() + + if not self.engine_config.forward_only: + self.optimizer = self.trainer.optimizers + self.lr_scheduler = self.trainer.lr_schedulers + else: + self.optimizer = None + self.lr_scheduler = None + + self.to( + device="cpu", + model=self._is_offload_param, + optimizer=self._is_offload_optimizer, + grad=self._is_offload_param, + ) + + log_gpu_memory_usage("After offload model/optimizer/grad during init", logger=logger) + + def _init_device_mesh(self): + """Initialize the device mesh for TorchTitan style parallelism.""" + world_size = torch.distributed.get_world_size() + self.parallel_dims = ParallelDims( + dp_shard=self.engine_config.data_parallel_shard_size, + dp_replicate=self.engine_config.data_parallel_replicate_size, + cp=self.engine_config.context_parallel_size, + tp=self.engine_config.tensor_parallel_size, + pp=self.engine_config.pipeline_parallel_size, + ep=self.engine_config.expert_parallel_size, + world_size=world_size, + ) + self.device_mesh = self.parallel_dims.build_mesh() + + # Mirror torchtitan's init_distributed (which verl bypasses): disable autograd + # multithreading so backward-thread activation-checkpoint recompute can access the + # thread-local SPMD mesh / process groups (e.g. current_spmd_mesh().get_group(...)). + torch.autograd.set_multithreading_enabled(False) + + def train_mode(self, **kwargs): + """Return a context manager for training mode.""" + return EngineTrainModeCtx(self, **kwargs) + + def eval_mode(self, **kwargs): + """Return a context manager for evaluation mode.""" + return EngineEvalModeCtx(self, **kwargs) + + def get_data_parallel_rank(self): + mesh = self._get_data_parallel_mesh() + return 0 if mesh is None else mesh.get_local_rank() + + def get_data_parallel_size(self): + return self.engine_config.data_parallel_shard_size * self.engine_config.data_parallel_replicate_size + + def get_data_parallel_group(self): + mesh = self._get_data_parallel_mesh() + if mesh is not None: + return mesh.get_group() + # If world_size == dp_size (e.g. single GPU, or all ranks are DP), + # return WORLD so that collective ops in _postprocess_output + # (allgather_dict_into_dict, all_reduce) still run and produce the + # correct metric aggregation format. + if torch.distributed.get_world_size() == self.get_data_parallel_size(): + return torch.distributed.group.WORLD + return None + + def get_model_parallel_group(self): + raise NotImplementedError + + def get_context_parallel_group(self): + raise NotImplementedError + + def _get_data_parallel_mesh(self): + """Get the data parallel mesh, handling hybrid/fully/replicate shard modes.""" + mesh = self.parallel_dims.get_optional_mesh(["dp_replicate", "fsdp"]) + if mesh is None: + mesh = self.parallel_dims.get_optional_mesh("fsdp") + if mesh is None: + mesh = self.parallel_dims.get_optional_mesh("dp_replicate") + return mesh + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False): + """Perform forward and optionally backward pass on a batch.""" + tu.assign_non_tensor(data, sp_size=self.engine_config.tensor_parallel_size) + + # Compute num_tokens in global batch for loss normalization + batch_num_tokens = data["loss_mask"].sum().to(get_device_id()) + dp_group = self.get_data_parallel_group() + if dp_group is not None: + torch.distributed.all_reduce(batch_num_tokens, op=torch.distributed.ReduceOp.SUM, group=dp_group) + tu.assign_non_tensor(data, batch_num_tokens=batch_num_tokens.item()) + tu.assign_non_tensor(data, dp_size=self.get_data_parallel_size()) + + micro_batches, indices = prepare_micro_batches( + data=data, + dp_group=self.get_data_parallel_group(), + same_micro_num_in_dp=True, + ) + + output_lst = [] + + ctx = torch.no_grad() if forward_only else nullcontext() + + # train_context activates the (thread-local) SPMD mesh required by spmd_types; it must + # span backward too, since activation-checkpoint recompute re-runs the forward there. + for micro_batch in micro_batches: + with self.trainer.train_context(), ctx: + loss, output = self.forward_step(micro_batch, loss_function=loss_function, forward_only=forward_only) + if not forward_only: + loss.backward() + output_lst.append(output) + + return postprocess_batch_func(output_lst=output_lst, indices=indices, data=data) + + def model_forward_step( + self, + *, + inputs: torch.Tensor, + extra_inputs: dict[str, torch.Tensor] | None = None, + extra_kwargs: dict[str, torch.Tensor] | None = None, + ) -> torch.Tensor: + """ + Perform a forward pass through the trainer model without backward. + """ + model_parts = self.module + parallel_dims = self.parallel_dims + + if parallel_dims.pp_enabled: + raise NotImplementedError( + "Pipeline parallelism is not yet supported in model_forward_step. " + "This will be implemented in a follow-up PR." + ) + else: + # Non-PP forward. train_context (SPMD mesh) is set by the caller. + assert len(model_parts) == 1 + pred = model_parts[0](inputs, **extra_inputs, **extra_kwargs) + + if isinstance(pred, DTensor): + pred = pred.full_tensor() + return pred + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + raise NotImplementedError("forward_step must be implemented in subclass") + + def optimizer_zero_grad(self): + """Zero gradients.""" + self.optimizer.zero_grad() + + def optimizer_step(self): + """Perform optimizer step with gradient clipping.""" + grad_norm = dist_utils.clip_grad_norm_( + [p for m in self.module for p in m.parameters()], + self.config.training.max_norm, + foreach=True, + pp_mesh=self.parallel_dims.get_optional_mesh("pp"), + ep_enabled=self.parallel_dims.ep_enabled, + ) + + # if grad_norm is not finite, skip the update + if not torch.isfinite(grad_norm): + logger.warning(f"grad_norm is not finite: {grad_norm}") + self.optimizer.zero_grad() + else: + self.optimizer.step() + return grad_norm.item() + + def lr_scheduler_step(self): + """Advance learning rate scheduler.""" + self.lr_scheduler.step() + lr = self.lr_scheduler.schedulers[0].get_last_lr()[0] + return lr + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """Move model and/or optimizer to CPU or GPU.""" + super().to(device=device, model=model, optimizer=optimizer, grad=grad) + + if self.engine_config.forward_only: + return + + device_name = get_device_name() + assert device in (device_name, "cpu") + if device == device_name: + if model: + for module in self.module: + load_fsdp_model_to_gpu(module) + if optimizer and self.optimizer is not None: + load_fsdp_optimizer(self.optimizer, device) + gc.collect() + elif device == "cpu": + if model: + for module in self.module: + offload_fsdp_model_to_cpu(module) + if optimizer and self.optimizer is not None: + offload_fsdp_optimizer(self.optimizer) + else: + raise ValueError(f"Invalid device type: {device}") + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """Save checkpoint.""" + if self._is_offload_param: + for module in self.module: + load_fsdp_model_to_gpu(module) + + # Override TorchTitan's folder to use verl's path + parent_dir = os.path.dirname(local_path) + self.checkpointer.folder = parent_dir + + if max_ckpt_to_keep is not None: + self.checkpointer.keep_latest_k = max_ckpt_to_keep + + self.checkpointer.save(curr_step=global_step) + + torch.distributed.barrier() + if self._is_offload_param: + for module in self.module: + offload_fsdp_model_to_cpu(module) + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: int = True, **kwargs + ) -> None: + """Load checkpoint.""" + if self._is_offload_param: + for module in self.module: + load_fsdp_model_to_gpu(module) + + # Override TorchTitan's folder to use verl's path + parent_dir = os.path.dirname(local_path) + self.checkpointer.folder = parent_dir + + # Extract step number from path (verl uses global_step_N format) + match = re.search(r"global_step_(\d+)", local_path) + if match: + step = int(match.group(1)) + self.checkpointer.load(step=step) + else: + # Fallback to latest + self.checkpointer.load(step=-1) + + torch.distributed.barrier() + if self._is_offload_param: + for module in self.module: + offload_fsdp_model_to_cpu(module) + + if self._is_offload_optimizer: + offload_fsdp_optimizer(self.optimizer) + + def get_per_tensor_param(self, **kwargs): + for module in self.module: + load_fsdp_model_to_gpu(module) + + params = {} + for module in self.module: + module_params = module.state_dict() + params.update(module_params) + + if self._is_offload_param: + for module in self.module: + offload_fsdp_model_to_cpu(module) + + # Convert TorchTitan key names to HuggingFace key names (expected by vLLM) + sd_adapter = self.checkpointer.sd_adapter + if sd_adapter is not None: + params = sd_adapter.to_hf(params) + + # When weight tying is enabled, the sd_adapter skips lm_head.weight during + # to_hf() conversion (since it's the same tensor as embed_tokens.weight in + # the torchtitan model). But vLLM needs lm_head.weight explicitly, so we + # add it back as a reference to embed_tokens.weight. + if "model.embed_tokens.weight" in params and "lm_head.weight" not in params: + params["lm_head.weight"] = params["model.embed_tokens.weight"] + + device = get_device_id() # used when fsdp2 set cpu_offload_policy + + # When Expert Parallel (EP) is used, sd_adapter.to_hf() only produces + # individual expert weights for the locally-owned experts (e.g., 16 out of + # 128 with EP=8). vLLM needs ALL experts. We gather the missing experts + # by all-gathering each expert weight across the EP process group. + if self.parallel_dims.ep_enabled: + ep_mesh = self.parallel_dims.get_optional_mesh("ep") + ep_group = ep_mesh.get_group() + ep_size = self.parallel_dims.ep + per_tensor_param = iter_per_tensor_params_ep(params, device, ep_group, ep_size) + else: + # TODO: cast fp32 to bf16 to reduce weight sync overhead, need more fine-grained control, e.g MoE gate + per_tensor_param = ( + ( + name, + param.to(device, non_blocking=True).full_tensor().to(torch.bfloat16, non_blocking=True) + if isinstance(param, DTensor) + else param, + ) + for name, param in params.items() + ) + # TODO: support Torchtitan PEFT + return per_tensor_param, None + + +class EngineEvalModeCtx(BaseEngineCtx): + def __init__(self, engine: TorchTitanEngine, **kwargs): + super().__init__(engine=engine, mode="eval", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, TorchTitanEngine) + super().__enter__() + for module in self.engine.module: + module.eval() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, TorchTitanEngine) + + # Reshard the root FSDP module + if self.engine.engine_config.data_parallel_shard_size > 1: + for module in self.engine.module: + module.reshard() + + super().__exit__(exc_type, exc_value, traceback) + + +class EngineTrainModeCtx(BaseEngineCtx): + def __init__(self, engine: TorchTitanEngine, **kwargs): + super().__init__(engine=engine, mode="train", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, TorchTitanEngine) + super().__enter__() + for module in self.engine.module: + module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, TorchTitanEngine) + if self.zero_grad_on_exit or exc_type is not None: + self.engine.optimizer_zero_grad() + super().__exit__(exc_type, exc_value, traceback) + + +@EngineRegistry.register(model_type="language_model", backend=["torchtitan"], device=["cuda", "npu"]) +class TorchTitanEngineWithLMHead(TorchTitanEngine): + """TorchTitan engine implementation for language models with LM head.""" + + def prepare_model_inputs(self, micro_batch: TensorDict): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + assert pad_mode == DatasetPadMode.NO_PADDING, f"pad_mode {pad_mode} not supported" + + multi_modal_inputs = extract_multi_modal_inputs(micro_batch.get("multi_modal_inputs", [])) + input_ids = micro_batch["input_ids"] + position_ids = micro_batch["position_ids"] + output_args = {} + + if use_remove_padding: + input_ids = input_ids.values().unsqueeze(0) + if position_ids.dim() == 3: + position_ids = position_ids.values().unsqueeze(1) + else: + position_ids = position_ids.values().unsqueeze(0) + + labels = torch.roll(input_ids, shifts=-1, dims=1) + attn_type = self.engine_config.attn_type + attention_mask = get_attention_masks( + input_batch=input_ids, + positions=position_ids, + attn_type=attn_type, + ) + else: + loss_mask = micro_batch["loss_mask"] + pad_token_id = tu.get_non_tensor_data(data=micro_batch, key="pad_token_id", default=0) + batch_size = micro_batch.batch_size[0] + max_seq_len = max(input_ids.offsets().diff()) + + labels = torch.roll(input_ids.values(), shifts=-1, dims=0) + input_ids = torch.nested.to_padded_tensor( + input_ids, padding=pad_token_id, output_size=(batch_size, max_seq_len) + ) + + if position_ids.dim() == 3: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, 4, max_seq_len) + ).transpose(0, 1) + else: + position_ids = torch.nested.to_padded_tensor( + position_ids, padding=0, output_size=(batch_size, max_seq_len) + ) + + attention_mask_list = [torch.ones_like(t, dtype=torch.int32) for t in loss_mask] + attention_mask = torch.nested.as_nested_tensor(attention_mask_list, layout=torch.jagged) + attention_mask = torch.nested.to_padded_tensor( + attention_mask, padding=0, output_size=(batch_size, max_seq_len) + ) + + extra_inputs = { + "positions": position_ids, + } + # For arguments, like attention_masks, we have to put them in a separate + # dict as extra_inputs are not forwarded to other stages in PP, but + # extra_kwargs are. + extra_kwargs: dict[str, Any] = {"attention_masks": attention_mask} + if self.parallel_dims.cp_enabled: + input_ids, labels, extra_kwargs = prepare_context_parallel_input( + input_ids, + labels, + extra_kwargs, + self.parallel_dims.get_mesh("cp"), + self.trainer.device, + self.trainer.config.parallelism.context_parallel_load_balancer, + ) + + # TODO(jessicazhong): multimodal is not yet supported for Torchtitan engine + extra_inputs.update(multi_modal_inputs) + output_args["labels"] = labels + return input_ids, extra_inputs, extra_kwargs, output_args + + def prepare_model_outputs(self, logits, output_args, micro_batch: TensorDict): + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + pad_mode = tu.get_non_tensor_data(data=micro_batch, key="pad_mode", default=DatasetPadMode.NO_PADDING) + assert pad_mode == DatasetPadMode.NO_PADDING, f"pad_mode {pad_mode} not supported" + + temperature = micro_batch["temperature"] + calculate_entropy = tu.get_non_tensor_data(data=micro_batch, key="calculate_entropy", default=False) + labels = output_args["labels"] + model_output = {} + + input_ids = micro_batch["input_ids"] + cu_seqlens = input_ids.offsets() + if use_remove_padding: + labels = labels.squeeze(0) + logits_rmpad = logits.squeeze(0) + # PyTorch's autograd doesn't allow in-place modification of views when gradients need to flow back + logits_rmpad = logits_rmpad / temperature + + inplace_backward = True + if calculate_entropy: + inplace_backward = False + log_probs = logprobs_from_logits( + logits=logits_rmpad, + labels=labels, + inplace_backward=inplace_backward, + ) + + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + if self.engine_config.entropy_from_logits_with_chunking: + entropy_rmpad = self.compute_entropy_from_logits( + logits_rmpad, + chunk_size=self.engine_config.entropy_from_logits_chunk_size, + ) # ((total_nnz / sp) + pad) + else: + entropy_rmpad = self.compute_entropy_from_logits(logits_rmpad) + else: + entropy_rmpad = torch.utils.checkpoint.checkpoint(self.compute_entropy_from_logits, logits_rmpad) + + log_probs = torch.nested.nested_tensor_from_jagged(log_probs.squeeze(0), cu_seqlens) + if calculate_entropy: + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + else: + logits.div_(temperature) + if calculate_entropy: + if not self.engine_config.entropy_checkpointing: + entropy = verl_F.entropy_from_logits(logits) + else: + entropy = torch.utils.checkpoint.checkpoint(verl_F.entropy_from_logits, logits) + + seq_lengths = cu_seqlens.diff() + starts = torch.zeros_like(seq_lengths, dtype=torch.int64) + logits = torch.nested.narrow(logits, 1, starts, seq_lengths, layout=torch.jagged) + logits_rmpad = torch.cat([t for t in logits.unbind()]) + log_probs = logprobs_from_logits(logits=logits_rmpad, labels=output_args["labels"]) + log_probs = torch.nested.nested_tensor_from_jagged(log_probs, cu_seqlens) + if calculate_entropy: + entropy = torch.nested.narrow(entropy, 1, starts, seq_lengths, layout=torch.jagged) + entropy_rmpad = torch.cat([t for t in entropy.unbind()]) + entropy = torch.nested.nested_tensor_from_jagged(entropy_rmpad, cu_seqlens) + + model_output["log_probs"] = log_probs + if calculate_entropy: + model_output["entropy"] = entropy + + return model_output + + def forward_step(self, micro_batch: TensorDict, loss_function, forward_only): + device_name = get_device_name() + micro_batch = micro_batch.to(get_device_id()) + input_ids, extra_inputs, extra_kwargs, output_args = self.prepare_model_inputs(micro_batch=micro_batch) + + with torch.autocast(device_type=device_name, dtype=torch.bfloat16): + logits = self.model_forward_step(inputs=input_ids, extra_inputs=extra_inputs, extra_kwargs=extra_kwargs) + + model_output = self.prepare_model_outputs(logits=logits, output_args=output_args, micro_batch=micro_batch) + + if loss_function is not None: + loss, metrics = loss_function( + model_output=model_output, data=micro_batch, dp_group=self.get_data_parallel_group() + ) + else: + assert forward_only, "forward_only must be True when loss_function is None" + loss = torch.tensor(1.0, device=device_name) + metrics = {} + + output = { + "model_output": model_output, + "loss": loss.detach().item(), + "metrics": metrics, + } + + return loss, output diff --git a/verl_0720_main/verl/verl/workers/engine/torchtitan/utils.py b/verl_0720_main/verl/verl/workers/engine/torchtitan/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..f6c9cc13e3f8c3ce215974f47f5135db4e1419a7 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/torchtitan/utils.py @@ -0,0 +1,359 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import importlib +import logging +import re +from collections import defaultdict +from collections.abc import Generator, Iterator +from dataclasses import dataclass +from typing import Any + +import torch +import torch.distributed +import torch.nn as nn +from torch.distributed._composable.fsdp import FSDPModule +from torch.distributed.tensor import DTensor +from torch.nn.attention.flex_attention import _mask_mod_signature, and_masks +from torchtitan.components.dataloader import BaseDataLoader +from torchtitan.models.common.attention import ( + AttentionMasksType, + VarlenMetadata, + create_attention_mask, + get_causal_mask_mod, +) + +logger = logging.getLogger(__name__) + + +class NoOpDataLoader(BaseDataLoader): + """A no-op dataloader for use when verl manages its own data loading. + + Satisfies the BaseDataLoader interface required by torchtitan's Trainer + but does nothing. Its __iter__ yields nothing, and state_dict / + load_state_dict are no-ops. + """ + + @dataclass(kw_only=True, slots=True) + class Config(BaseDataLoader.Config): + pass + + def __init__(self, **kwargs): + pass + + def __iter__(self) -> Iterator[tuple[dict[str, torch.Tensor], torch.Tensor]]: + return iter([]) + + def state_dict(self): + return {} + + def load_state_dict(self, state_dict): + pass + + +# Mapping from HuggingFace model_type to torchtitan model name. +# Torchtitan models not mapped here: +# - flux: diffusion model, not applicable to verl's RL/SFT workflows +# - llama3_ft: fault-tolerant variant of llama3, same HF models (mapped via "llama") +_HF_MODEL_TYPE_TO_TORCHTITAN_NAME = { + "qwen2": "qwen3", + "qwen3": "qwen3", + "qwen2_moe": "qwen3", + "qwen3_moe": "qwen3", + "llama": "llama3", + "llama4": "llama4", + "deepseek_v3": "deepseek_v3", + "gpt_oss": "gpt_oss", +} + + +def derive_torchtitan_name_and_flavor(hf_config) -> tuple[str, str]: + """Derive torchtitan model name and flavor from a HuggingFace config. + + The name is mapped from ``hf_config.model_type``. The flavor is found by + matching architecture parameters (dim, n_layers, vocab_size) against the + known flavors registered in the torchtitan model package. + + Args: + hf_config: A HuggingFace AutoConfig object. + + Returns: + A ``(name, flavor)`` tuple. + + Raises: + ValueError: If model_type is unsupported or no matching flavor is found. + """ + model_type = getattr(hf_config, "model_type", None) + if model_type is None: + raise ValueError("HuggingFace config does not have 'model_type' field") + + name = _HF_MODEL_TYPE_TO_TORCHTITAN_NAME.get(model_type) + if name is None: + raise ValueError( + f"Cannot derive torchtitan model name from HF model_type '{model_type}'. " + f"Supported types: {list(_HF_MODEL_TYPE_TO_TORCHTITAN_NAME.keys())}." + ) + + # Import the model package and use model_registry to build each flavor's config. + # model_registry has sensible defaults for all optional params (attn_backend, etc.). + model_module = importlib.import_module(f"torchtitan.models.{name}") + model_registry = model_module.model_registry + + # The configs dict name isn't derivable from the model name + # (e.g. gpt_oss -> gptoss_configs), so we find it by convention. + flavor_names = None + for attr, obj in vars(model_module).items(): + if attr.endswith("_configs") and isinstance(obj, dict): + flavor_names = list(obj.keys()) + break + + if flavor_names is None: + raise ValueError( + f"Could not find model configs dict in torchtitan.models.{name}. " + f"Expected a dict attribute ending with '_configs'." + ) + + hidden_size = hf_config.hidden_size + num_layers = hf_config.num_hidden_layers + vocab_size = hf_config.vocab_size + + for flavor_name in flavor_names: + cfg = model_registry(flavor_name).model + n_layers = getattr(cfg, "n_layers", None) or len(getattr(cfg, "layers", [])) + if ( + getattr(cfg, "dim", None) == hidden_size + and n_layers == num_layers + and getattr(cfg, "vocab_size", None) == vocab_size + ): + logger.info( + f"Auto-derived torchtitan name='{name}', flavor='{flavor_name}' from HF model_type='{model_type}'" + ) + return name, flavor_name + + raise ValueError( + f"No matching torchtitan flavor found for model_type='{model_type}' " + f"(hidden_size={hidden_size}, num_hidden_layers={num_layers}, " + f"vocab_size={vocab_size}). " + f"Available flavors for '{name}': {flavor_names}." + ) + + +def enable_fsdp_gradient_division(model: nn.Module, dp_size: int) -> None: + """ + Re-enable FSDP's automatic gradient division. + + TorchTitan calls disable_fsdp_gradient_division() which sets gradient_divide_factor=1.0. + This re-enables it by setting the factor to the specified dp_size, so gradients are + averaged across FSDP ranks. This is needed for verl's loss scaling (loss * dp_size) + to work correctly. + + Args: + model: The model (or model part) to enable gradient division on. + dp_size: The data parallel size to use as the gradient divide factor. + """ + + for module in model.modules(): + if isinstance(module, FSDPModule): + module.set_gradient_divide_factor(float(dp_size)) + + +def get_attention_masks( + input_batch: torch.Tensor, + positions: torch.Tensor, + attn_type: str, +) -> AttentionMasksType: + match attn_type: + case "flex" | "flex_flash": + # flex_flash is FlexAttention with the FLASH kernel backend; it uses + # the same BlockMask as flex. + return _get_flex_attention_masks( + input_batch, + positions, + ) + case "varlen": + return _create_varlen_metadata_for_document( + input_batch, + positions, + ) + case _: + raise TypeError("Only varlen and flex attn masks are supported") + + +def _get_document_mask_mod(positions: torch.Tensor) -> _mask_mod_signature: + # Detect boundaries from position resets + first_dummy_value = positions[:, :1] - 1 + position_diff = torch.diff(positions, prepend=first_dummy_value, dim=-1) + sequence_indices = (position_diff != 1).cumsum(-1) # [batch, seq] + + def document_mask(b: torch.Tensor, h: torch.Tensor, q_idx: torch.Tensor, kv_idx: torch.Tensor) -> torch.Tensor: + return sequence_indices[b, q_idx] == sequence_indices[b, kv_idx] + + return document_mask + + +def _get_flex_attention_masks( + input_batch: torch.Tensor, + positions: torch.Tensor, +) -> AttentionMasksType: + mask_mods = [get_causal_mask_mod()] + B = input_batch.shape[0] + mask_mods.append(_get_document_mask_mod(positions=positions)) + return create_attention_mask(and_masks(*mask_mods), B, None, input_batch.shape[1], input_batch.shape[1]) + + +def _create_varlen_metadata_for_document(input_batch: torch.Tensor, positions: torch.Tensor) -> VarlenMetadata: + """ + Creates cumulative sequence length indices needed for variable length attention + + Args: + input_batch: Input token IDs with shape [batch, seq]. + positions: Position IDs with shape [batch, seq]. Boundaries detected where + position diff != 1 (i.e., position resets). + + Returns: + VarlenMetadata containing cumulative sequence length indices for q, k, and max_seq_len + """ + batch_size, seq_len = input_batch.shape + device = input_batch.device + + # Detect boundaries from position resets (where diff != 1) + first_dummy_value = positions[:, :1] - 1 + position_diff = torch.diff(positions, prepend=first_dummy_value, dim=-1) + # boundary_mask[b, i] is True if position i starts a new document + boundary_mask = position_diff != 1 # [batch, seq] + boundary_mask[:, 0] = True + + cu_seqlens_list, all_seq_lengths = [], [] + offset = 0 + + for b in range(batch_size): + # Find positions where new documents start + boundary_positions = boundary_mask[b].nonzero(as_tuple=True)[0].to(torch.int32) + sample_cu_seqlens = torch.cat( + [ + boundary_positions, + torch.tensor([seq_len], dtype=torch.int32, device=device), + ] + ) + sample_cu_seqlens = torch.unique_consecutive(sample_cu_seqlens) + + seq_lengths = torch.diff(sample_cu_seqlens) + all_seq_lengths.append(seq_lengths) + + cu_seqlens_adjusted = sample_cu_seqlens[:-1] + offset + cu_seqlens_list.append(cu_seqlens_adjusted) + + offset += seq_len + + packed_cu_seqlens = torch.cat(cu_seqlens_list + [torch.tensor([offset], dtype=torch.int32, device=device)]) + + max_seqlen = 0 + if len(all_seq_lengths) > 0: + all_seq_lengths = torch.cat(all_seq_lengths) + # device to host sync but only done once per model forward + max_seqlen = all_seq_lengths.max().item() + + return VarlenMetadata( + cu_seq_q=packed_cu_seqlens, + cu_seq_k=packed_cu_seqlens, + max_q=max_seqlen, + max_k=max_seqlen, + ) + + +# Regex to parse: model.layers.{L}.mlp.experts.{E}.{weight_suffix} +_EXPERT_PATTERN = re.compile(r"\.layers\.(\d+)\..*\.experts\.(\d+)\.(.*)") + + +def _parse_expert_name(name: str) -> tuple[int, int, str] | None: + """Parse layer_id, expert_id, weight_suffix from expert param name.""" + match = _EXPERT_PATTERN.search(name) + if match: + return int(match.group(1)), int(match.group(2)), match.group(3) + return None + + +def _make_expert_name_template(name: str) -> str: + """Convert 'model.layers.0.mlp.experts.3.w1' -> 'model.layers.0.mlp.experts.{}.w1'""" + return _EXPERT_PATTERN.sub(lambda m: f".layers.{m.group(1)}.mlp.experts.{{}}.{m.group(3)}", name) + + +def iter_per_tensor_params_ep( + params: dict[str, Any], + device: int, + ep_group: torch.distributed.ProcessGroup, + ep_size: int, +) -> Generator[tuple[str, torch.Tensor], None, None]: + """Yield (name, tensor) pairs for weight sync with Expert Parallel. + + Gathers expert weights across EP ranks one (layer, weight_type) group + at a time to avoid OOM from materializing all experts simultaneously. + + Non-expert params are yielded first (with FSDP full_tensor() if needed), + then expert params are all-gathered per group and yielded individually. + + Args: + params: HF-format state dict with per-expert keys. Expert keys must + follow the pattern ``model.layers.{L}.mlp.experts.{E}.{suffix}``. + device: device ID to place tensors on. + ep_group: The EP process group for all-gather. + ep_size: Number of EP ranks. + """ + expert_params: dict[tuple[int, str], dict[int, tuple[str, Any]]] = defaultdict(dict) + non_expert_params: list[tuple[str, Any]] = [] + + for name, param in params.items(): + parsed = _parse_expert_name(name) if "mlp.experts." in name else None + if parsed is None: + non_expert_params.append((name, param)) + else: + layer_id, expert_id, weight_suffix = parsed + expert_params[(layer_id, weight_suffix)][expert_id] = (name, param) + + params.clear() + + # Yield non-expert params + for name, param in non_expert_params: + if isinstance(param, DTensor): + yield name, param.to(device, non_blocking=True).full_tensor().to(torch.bfloat16, non_blocking=True) + else: + yield name, param + del non_expert_params + + # Yield expert params with all-gather + for (layer_id, weight_suffix), experts_dict in sorted(expert_params.items()): + sorted_expert_ids = sorted(experts_dict.keys()) + + # Stack local expert weights + local_weights = [] + for eid in sorted_expert_ids: + _, param = experts_dict[eid] + if isinstance(param, DTensor): + param = param.to(device, non_blocking=True).full_tensor() + else: + param = param.to(device, non_blocking=True) + local_weights.append(param) + + name_template = _make_expert_name_template(experts_dict[sorted_expert_ids[0]][0]) + local_stacked = torch.stack(local_weights, dim=0) + + # All-gather across EP ranks + gathered_list = [torch.empty_like(local_stacked) for _ in range(ep_size)] + torch.distributed.all_gather(gathered_list, local_stacked, group=ep_group) + all_experts = torch.cat(gathered_list, dim=0) + + for expert_id in range(all_experts.shape[0]): + yield name_template.format(expert_id), all_experts[expert_id].to(torch.bfloat16).clone() + + del local_weights, local_stacked, gathered_list, all_experts + torch.cuda.empty_cache() diff --git a/verl_0720_main/verl/verl/workers/engine/utils.py b/verl_0720_main/verl/verl/workers/engine/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..29c43ce7a2e2072676357e506405ccf4417e1934 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/utils.py @@ -0,0 +1,159 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import os +import random + +import numpy as np +import torch +from tensordict import TensorDict + +from verl.utils import tensordict_utils as tu +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.device import is_npu_available +from verl.utils.device import manual_seed as device_manual_seed +from verl.utils.device import manual_seed_all as device_manual_seed_all +from verl.utils.py_functional import append_to_dict +from verl.utils.seqlen_balancing import rearrange_micro_batches, restore_dynamic_batch + + +def enable_full_determinism(seed: int): + """ + Helper function for reproducibility in distributed training. + See https://pytorch.org/docs/stable/notes/randomness.html for details. + """ + + os.environ["PYTHONHASHSEED"] = str(seed) + os.environ["CUBLAS_WORKSPACE_CONFIG"] = ":16:8" + os.environ["FLASH_ATTENTION_DETERMINISTIC"] = "1" + if is_npu_available: + # The environment variable required to enable deterministic mode on Ascend NPUs. + os.environ["HCCL_DETERMINISTIC"] = "true" + os.environ["CLOSE_MATMUL_K_SHIFT"] = "1" + + random.seed(seed) + np.random.seed(seed) + torch.manual_seed(seed) + device_manual_seed(seed) + device_manual_seed_all(seed) + torch.use_deterministic_algorithms(True, warn_only=True) + # Enable CUDNN deterministic mode + torch.backends.cudnn.deterministic = True + torch.backends.cudnn.benchmark = False + torch.backends.cudnn.enabled = False + + +def prepare_micro_batches( + data: TensorDict, + dp_group=None, + num_batches_divided_by=None, + same_micro_num_in_dp=True, + min_num_micro_batch=None, + use_dynamic_bsz_balance=True, +): + """ + Prepare micro batches from data. + """ + use_dynamic_bsz = tu.get_non_tensor_data(data=data, key="use_dynamic_bsz", default=True) + sp_size = tu.get_non_tensor_data(data=data, key="sp_size", default=1) + + force_group_size = tu.get_non_tensor_data(data=data, key="force_group_size", default=1) + + if use_dynamic_bsz: + assert "max_token_len_per_gpu" in data.keys(), "max_token_len_per_gpu must be set when use_dynamic_bsz is True" + max_token_len_per_gpu = data["max_token_len_per_gpu"] + max_token_len = max_token_len_per_gpu * sp_size + micro_batches, batch_idx_list = rearrange_micro_batches( + data, + max_token_len=max_token_len, + dp_group=dp_group, + num_batches_divided_by=num_batches_divided_by, + same_micro_num_in_dp=same_micro_num_in_dp, + min_num_micro_batch=min_num_micro_batch, + use_dynamic_bsz_balance=use_dynamic_bsz_balance, + force_group_size=force_group_size, + ) + else: + total_data_size = len(data) + micro_batch_size_per_gpu = data["micro_batch_size_per_gpu"] + assert total_data_size % (force_group_size * micro_batch_size_per_gpu) == 0, ( + "data size must be divisible by force_group_size * micro_batch_size_per_gpu" + ) + micro_batches = tu.chunk_tensordict(data, total_data_size // (micro_batch_size_per_gpu * force_group_size)) + batch_idx_list = None + return micro_batches, batch_idx_list + + +def postprocess_batch_func(output_lst, indices, data: TensorDict): + """postprocess the output of a forward_backward_batch. + output_lst is a list of dict containing outputs for each micro-batch + reorder entropy and outputs. Return None for other pp ranks + only on last rank. It should be on every tp rank + + each losses_reduced contains 1. model_output, 2. loss, 3. metrics. + """ + + use_dynamic_bsz = tu.get_non_tensor_data(data=data, key="use_dynamic_bsz", default=True) + pad_mode = tu.get_non_tensor_data(data=data, key="pad_mode", default=DatasetPadMode.NO_PADDING) + assert pad_mode == DatasetPadMode.NO_PADDING, "postprocess_batch_func only support NO_PADDING pad_mode" + + # losses_reduced is a list of dict containing outputs for each micro-batch + # reorder entropy and outputs. Return None for other pp ranks + # only on last rank. It should be on every tp rank + + # losses_reduced contains 1. model_output, 2. loss, 3. metrics. + # We perform reverse + + model_output = {} + losses = [] + aggregated_metrics = {} + + # model output + for o in output_lst: + if "model_output" in o: + for key, val in o["model_output"].items(): + if key not in model_output: + model_output[key] = [] + model_output[key].append(val) + + # concat results from micro batches + for key, val in model_output.items(): + if pad_mode == DatasetPadMode.NO_PADDING: + tensors = [tensor for nt in model_output[key] for tensor in nt.unbind()] + model_output[key] = torch.nested.as_nested_tensor(tensors, layout=torch.jagged) + else: + raise NotImplementedError(f"pad_mode {pad_mode} not implemented") + + # reverse with dynamic bsz + if use_dynamic_bsz: + model_output[key] = restore_dynamic_batch(model_output[key], indices) + + # loss + for o in output_lst: + if "loss" in o: + losses.append(o["loss"]) + + # metrics + for o in output_lst: + if "metrics" in o: + metrics = o["metrics"] + append_to_dict(aggregated_metrics, metrics) + + output = { + "model_output": model_output, + "loss": losses, + "metrics": aggregated_metrics, + } + + return output diff --git a/verl_0720_main/verl/verl/workers/engine/veomni/__init__.py b/verl_0720_main/verl/verl/workers/engine/veomni/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..409226f77c39d7cc34d6b0f71d4e86a40d7d05b5 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/veomni/__init__.py @@ -0,0 +1,17 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .transformer_impl import VeOmniEngine, VeOmniEngineWithLMHead, VeOmniEngineWithValueHead + +__all__ = ["VeOmniEngine", "VeOmniEngineWithLMHead", "VeOmniEngineWithValueHead"] diff --git a/verl_0720_main/verl/verl/workers/engine/veomni/transformer_impl.py b/verl_0720_main/verl/verl/workers/engine/veomni/transformer_impl.py new file mode 100644 index 0000000000000000000000000000000000000000..d25fdf207e94a8b24131a98198f848f3cc7f027b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/veomni/transformer_impl.py @@ -0,0 +1,997 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import logging +from dataclasses import dataclass, field +from typing import Any, Callable, Optional, Sequence + +import torch +import torch.distributed as dist +from tensordict import TensorDict +from torch.distributed.tensor import DTensor +from veomni.arguments import MixedPrecisionConfig, OpsImplementationConfig +from veomni.distributed import parallel_state +from veomni.distributed.offloading import build_activation_offloading_context +from veomni.distributed.torch_parallelize import build_parallelize_model +from veomni.models.auto import build_foundation_model +from veomni.optim import build_lr_scheduler, build_optimizer +from veomni.utils.seqlen_pos_transform_utils import prepare_fa_kwargs_from_position_ids + +import verl.utils.torch_functional as verl_F +from verl.trainer.config import CheckpointConfig +from verl.utils import tensordict_utils as tu +from verl.utils.checkpoint.fsdp_checkpoint_manager import FSDPCheckpointManager +from verl.utils.device import get_device_id, get_device_name +from verl.utils.fsdp_utils import fsdp_version +from verl.utils.model import convert_weight_keys +from verl.utils.profiler import log_gpu_memory_usage +from verl.utils.ulysses import ( + get_ulysses_sequence_parallel_group, + set_ulysses_sequence_parallel_group, +) +from verl.utils.veomni.router_replay import RouterReplayAction, VeOmniRouterReplay +from verl.workers.config import HFModelConfig, VeOmniEngineConfig, VeOmniOptimizerConfig + +from ..base import BaseEngineCtx, EngineRegistry +from ..fsdp.transformer_impl import FSDPEngine, FSDPEngineWithLMHead, FSDPEngineWithValueHead +from ..utils import enable_full_determinism, postprocess_batch_func, prepare_micro_batches +from .utils import ( + MOE_PARAM_HANDERS, + VL_TYPE2INDEX, + default_moe_param_handler, + load_veomni_model_to_gpu, + load_veomni_optimizer, + offload_veomni_model_to_cpu, + offload_veomni_optimizer, +) + +logger = logging.getLogger(__file__) + + +class VeOmniEngine(FSDPEngine): + _veomni_handles_position_ids = True + + def _apply_veomni_input_transforms(self, model_inputs: dict, micro_batch: TensorDict): + """Apply VeOmni-specific input transforms shared by LM and value heads. + + Handles vision-language model masks, sequence parallel sharding, + and flash attention kwargs from position_ids. + """ + input_ids_rmpad = model_inputs["input_ids"] + sp_enabled = parallel_state.get_parallel_state().sp_enabled + sp_shard_collator = OmniSequenceShardCollator() if sp_enabled else None + + if self.module.config.model_type in VL_TYPE2INDEX.keys(): + image_mask = input_ids_rmpad == VL_TYPE2INDEX[self.module.config.model_type]["IMAGE_INPUT_INDEX"] + video_mask = input_ids_rmpad == VL_TYPE2INDEX[self.module.config.model_type]["VIDEO_INPUT_INDEX"] + model_inputs.update({"image_mask": image_mask, "video_mask": video_mask}) + + if sp_enabled: + sp_shard_collator(model_inputs) + + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + if use_remove_padding and model_inputs.get("position_ids", None) is not None: + model_inputs.update(_prepare_veomni_flash_attention_kwargs(model_inputs["position_ids"])) + if sp_enabled: + model_inputs["position_ids"] = sp_shard_collator.sp_slice(model_inputs["position_ids"], dim=-1) + + def __init__( + self, + model_config: HFModelConfig, + engine_config: VeOmniEngineConfig, + optimizer_config: VeOmniOptimizerConfig, + checkpoint_config: CheckpointConfig, + **kwargs, + ): + """ + Initialize the VeOmniEngine. + + Sets up distributed device meshes, LoRA, and offload policies based on config. + + Args: + config: Configuration object with VeOmni and model settings. + """ + + self.model_config = model_config + self.engine_config = engine_config + self.optimizer_config = optimizer_config + self.checkpoint_config = checkpoint_config + # VeOmniEngine only supports fsdp2. + self.data_parallel_mode = "fsdp2" + self.rank = dist.get_rank() + + fsdp_size = self.engine_config.fsdp_size + world_size = dist.get_world_size() + dp_size = world_size // self.engine_config.ulysses_parallel_size + + if fsdp_size < 0 or fsdp_size >= dp_size: + data_parallel_replicate_size = 1 + data_parallel_shard_size = dp_size + else: + if dp_size % fsdp_size != 0: + raise ValueError( + f"Data parallel size ({dp_size}) must be divisible by fsdp_size ({fsdp_size}). " + "Please adjust your parallel configuration." + ) + data_parallel_replicate_size = dp_size // fsdp_size + data_parallel_shard_size = fsdp_size + + parallel_state.init_parallel_state( + dp_size=dp_size, + dp_replicate_size=data_parallel_replicate_size, + dp_shard_size=data_parallel_shard_size, + extra_parallel_sizes=(self.engine_config.expert_parallel_size,), + ulysses_size=self.engine_config.ulysses_parallel_size, + dp_mode=self.data_parallel_mode, + ) + + if self.engine_config.full_determinism: + enable_full_determinism(seed=self.engine_config.seed) + + self.use_remove_padding = self.model_config.use_remove_padding + + self._is_offload_param = self.engine_config.param_offload + self._is_offload_optimizer = self.engine_config.optimizer_offload + self._is_lora = self.model_config.lora_rank > 0 + + self.use_ulysses_sp = parallel_state.get_parallel_state().sp_enabled + self.ulysses_sequence_parallel_size = self.engine_config.ulysses_parallel_size + + if self.use_ulysses_sp: + self.ulysses_parallel_group = parallel_state.get_parallel_state().device_mesh["sp"].get_group() + else: + self.ulysses_parallel_group = None + + if self.engine_config.entropy_from_logits_with_chunking: + entropy_from_logits = verl_F.entropy_from_logits_with_chunking + else: + entropy_from_logits = verl_F.entropy_from_logits + + self.compute_entropy_from_logits = ( + torch.compile(entropy_from_logits, dynamic=True) + if self.engine_config.use_torch_compile # use torch compile by default + else entropy_from_logits + ) + + # Router replay (R2 / R3) for MoE models. Controller is attached in + # initialize() after the model is built; here we only record intent. + self._router_replay_mode: str = self.engine_config.router_replay.mode + self.enable_routing_replay: bool = self._router_replay_mode != "disabled" + self._router_replay: VeOmniRouterReplay | None = None + if self.enable_routing_replay: + logger.info("VeOmniEngine: router_replay enabled, mode=%s", self._router_replay_mode) + + def initialize(self): + """ + Build the model, optimizer, and learning rate scheduler under VeOmni. + + Applies device, dtype, and precision configurations, including mixed precision. + Sets up checkpoint manager and FLOPs counter. + """ + self._build_model_optimizer() + + if self.enable_routing_replay: + # Defense in depth: the VeOmniActorConfig check is the primary + # fail-fast point and runs *before* engine init. By the time we get here, + # ``_build_model_optimizer()`` has already finished — this + # second check exists to catch direct ``VeOmniEngine`` + # instantiation paths that bypass the worker (e.g. unit tests, + # standalone debug scripts) so the user gets a typed config + # error instead of an opaque mid-step ``AttributeError`` on + # ``input_ids.offsets()``. + if not self.engine_config.use_remove_padding: + raise RuntimeError( + "router_replay requires use_remove_padding=True. In VeOmni engine, " + "the non-remove-padding path also disables Ulysses SP slicing and " + "the fused-kernel log_probs path, and is not a tested production " + "configuration for MoE routing replay. Set " + "actor.model.use_remove_padding=True or " + "router_replay.mode='disabled'." + ) + self._router_replay = VeOmniRouterReplay(sp_group=self.ulysses_parallel_group) + # Fails loudly if the VeOmni build in the environment does not + # export `set_active_replay` yet (plan requires upgrading VeOmni + # or disabling router_replay). + self._router_replay.install(self.module) + + self.checkpoint_manager = FSDPCheckpointManager( + model=self.module, + optimizer=self.optimizer, + lr_scheduler=self.lr_scheduler, + processing_class=self.model_config.get_processor(), + checkpoint_config=self.checkpoint_config, + trust_remote_code=self.model_config.trust_remote_code, + ) + + self.to( + device="cpu", + model=self._is_offload_param, + optimizer=self._is_offload_optimizer, + grad=self._is_offload_optimizer, + ) + + log_gpu_memory_usage("After offload model/optimizer/grad during init", logger=logger) + + def _build_optimizer(self, module): + optimizer = build_optimizer( + module, + lr=self.optimizer_config.lr, + betas=self.optimizer_config.betas, + weight_decay=self.optimizer_config.weight_decay, + optimizer_type=self.optimizer_config.optimizer, + ) + get_optimizer_pre_hook = getattr(module, "get_optimizer_pre_hook", None) + if get_optimizer_pre_hook is not None: + optimizer_pre_hook = get_optimizer_pre_hook(module, module.config, self.data_parallel_mode) + optimizer.register_step_pre_hook(optimizer_pre_hook) + + return optimizer + + def _build_lr_scheduler(self, optimizer): + optim_config = self.optimizer_config + lr_scheduler = build_lr_scheduler( + optimizer, + train_steps=optim_config.total_training_steps, + lr=optim_config.lr, + lr_min=optim_config.lr_min, + lr_decay_style=optim_config.lr_scheduler_type, + lr_decay_ratio=optim_config.lr_decay_ratio, + lr_warmup_ratio=optim_config.lr_warmup_steps_ratio, + lr_start=optim_config.lr_start, + ) + + return lr_scheduler + + def _get_model_config_path(self): + """Return the config path (or PretrainedConfig) for build_foundation_model. + + Subclasses can override to modify the HF config before model construction + (e.g. VeOmniEngineWithValueHead rewrites architectures to ForTokenClassification). + """ + return self.model_config.local_hf_config_path + + def _build_model_optimizer(self): + # build_foundation_model runs apply_ops_config(ops_implementation) + # before constructing the model, so per-model device_patch files see + # the resolved kernel backends. + ops_implementation = OpsImplementationConfig( + attn_implementation=self.engine_config.attn_implementation, + moe_implementation=self.engine_config.moe_implementation, + cross_entropy_loss_implementation=self.engine_config.cross_entropy_loss_implementation, + rms_norm_implementation=self.engine_config.rms_norm_implementation, + swiglu_mlp_implementation=self.engine_config.swiglu_mlp_implementation, + rotary_pos_emb_implementation=self.engine_config.rotary_pos_emb_implementation, + load_balancing_loss_implementation=self.engine_config.load_balancing_loss_implementation, + rms_norm_gated_implementation=self.engine_config.rms_norm_gated_implementation, + causal_conv1d_implementation=self.engine_config.causal_conv1d_implementation, + chunk_gated_delta_rule_implementation=self.engine_config.chunk_gated_delta_rule_implementation, + ) + + veomni_mixed_precision_config = MixedPrecisionConfig(enable=self.engine_config.mixed_precision) + + # Load base model with specified configuration and dtype + module = build_foundation_model( + config_path=self._get_model_config_path(), + weights_path=self.model_config.local_path, + torch_dtype="float32" if veomni_mixed_precision_config.enable else "bfloat16", + attn_implementation=self.engine_config.attn_implementation, + ops_implementation=ops_implementation, + init_device=self.engine_config.init_device, + ) + log_gpu_memory_usage("After load base model", logger=logger) + + # Applies parallel strategies to the model. + log_gpu_memory_usage("Before parallelize model", logger=logger) + module = build_parallelize_model( + module, + init_device=self.engine_config.init_device, + weights_path=self.model_config.local_path, + enable_full_shard=self.engine_config.enable_full_shard, + mixed_precision=veomni_mixed_precision_config, + enable_gradient_checkpointing=self.model_config.enable_gradient_checkpointing, + enable_fsdp_offload=self.engine_config.enable_fsdp_offload, + basic_modules=list( + set(getattr(module, "_no_split_modules", None) or []) | set(self.engine_config.basic_modules) + ), + enable_reentrant=self.engine_config.enable_reentrant, + enable_forward_prefetch=self.engine_config.forward_prefetch, + ) + log_gpu_memory_usage("After parallelize model", logger=logger) + + if not self.engine_config.forward_only: + # Initialize optimizer with model parameters and config settings + optimizer = self._build_optimizer(module) + # Create learning rate scheduler with warmup and decay settings + lr_scheduler = self._build_lr_scheduler(optimizer) + else: + optimizer = None + lr_scheduler = None + + self.module = module + self.optimizer = optimizer + self.lr_scheduler = lr_scheduler + self.model_fwd_context, self.model_bwd_context = build_activation_offloading_context( + self.model_config.enable_activation_offload, + self.model_config.enable_gradient_checkpointing, + self.engine_config.activation_gpu_limit, + ) + + def optimizer_step(self): + """ + Perform an optimization step using the optimizer. + """ + if hasattr(self.module, "clip_grad_norm_"): + grad_norm = self.module.clip_grad_norm_(self.optimizer_config.clip_grad) + else: + grad_norm = torch.nn.utils.clip_grad_norm_(self.module.parameters(), self.optimizer_config.clip_grad) + + if isinstance(grad_norm, DTensor): + grad_norm = grad_norm.full_tensor() + + # if grad_norm is not finite, skip the update + if not torch.isfinite(grad_norm): + print(f"WARN: grad_norm is not finite: {grad_norm}") + self.optimizer.zero_grad() + else: + self.optimizer.step() + return grad_norm.item() + + def forward_backward_batch(self, data: TensorDict, loss_function: Callable, forward_only=False) -> Any: + """ + Perform a forward pass and optionally a backward pass on a batch of data. + + Args: + data: The input data for the forward pass, typically containing tensors and metadata. + loss_function: The loss function to optimize. See `verl.workers.roles.utils.losses` for examples. + forward_only: If True, perform only the forward pass. If False, perform forward and backward pass. + + Returns: + Any: The output of the forward pass, which can be used for loss computation or other purposes. + """ + tu.assign_non_tensor(data, sp_size=parallel_state.get_parallel_state().ulysses_size) + + # compute num_tokens in global batch for loss normalization + batch_num_tokens = data["loss_mask"].sum().to(get_device_id()) + torch.distributed.all_reduce( + batch_num_tokens, op=torch.distributed.ReduceOp.SUM, group=self.get_data_parallel_group() + ) + tu.assign_non_tensor(data, batch_num_tokens=batch_num_tokens.item()) + tu.assign_non_tensor(data, dp_size=self.get_data_parallel_size()) + + micro_batches, indices = prepare_micro_batches( + data=data, dp_group=self.get_data_parallel_group(), same_micro_num_in_dp=True + ) + + # Router replay state machine: decide RECORD vs REPLAY for this step. + # RECORD: R2 compute_log_prob (forward_only=True). + # REPLAY: R2 actor update, or R3 always (forward_only=True and False). + rr_active = self.enable_routing_replay and tu.get_non_tensor_data(data, "enable_routing_replay", default=False) + if rr_active: + assert self._router_replay is not None + if self._router_replay_mode == "R2" and forward_only: + self._router_replay.begin_record() + else: + self._router_replay.begin_replay() + + # Wrap the per-step body in try/finally so the controller is always + # reset to DISABLED even if forward / backward / postprocess raises. + # Without this, an exception leaves _recorded / _targets pinned + # (GPU memory) until the next successful step's begin_record/replay + # clears them, which may never happen if the caller (Ray actor) tears + # down the worker after the failure. + try: + output_lst = [] + # Per-microbatch metadata for RECORD aggregation (pad_size for SP pad trim, + # cu_seqlens for per-sample split). Collected via side-channel on the + # micro_batch TensorDict during prepare_model_inputs. + pad_size_per_mb: list[int] = [] + cu_seqlens_per_mb: list[torch.Tensor] = [] + + for micro_batch in micro_batches: + if rr_active: + # Singular form: stash the entire list as one NonTensorData. + # The plural ``assign_non_tensor`` auto-dispatches lists to a + # NonTensorStack (batch_size=[len(list)]), which mutates the + # lazy-stacked micro_batch's batch_size and is rejected. + tu.assign_non_tensor_data(micro_batch, "_router_replay_pad_size_out", pad_size_per_mb) + tu.assign_non_tensor_data(micro_batch, "_router_replay_cu_seqlens_out", cu_seqlens_per_mb) + + with self.model_fwd_context: + loss, meta_info = self.forward_step( + micro_batch, loss_function=loss_function, forward_only=forward_only + ) + if not forward_only: + with self.model_bwd_context: + loss.backward() + + output_lst.append(meta_info) + + # Advance the per-mb counter on the controller. RECORD bumps + # ``_mb_index`` so the next micro-batch's first router fire writes + # into the next slot (recompute-in-backward of *this* mb has + # already finished by here, so its detection window is closed). + # Skip the bump after the last micro-batch: collect_recorded + # cross-checks that every layer has exactly num_micro_batches + # entries, so we must not advance past the last one. + if rr_active and self._router_replay.action is RouterReplayAction.RECORD: + if len(output_lst) < len(micro_batches): + self._router_replay.advance_record_microbatch() + + if rr_active and self._router_replay.action is RouterReplayAction.RECORD: + # ``collect_recorded`` already checks pad_size_per_mb internally; + # cu_seqlens_per_mb is engine-local, so we cross-check here for + # a clear error if anything (e.g. a deep-copying TD op) breaks + # the by-reference side-channel contract. + n_mb = len(micro_batches) + if not (len(pad_size_per_mb) == len(cu_seqlens_per_mb) == n_mb): + raise RuntimeError( + f"router_replay RECORD aggregation: side-channel lengths " + f"diverge — pad_size_per_mb={len(pad_size_per_mb)}, " + f"cu_seqlens_per_mb={len(cu_seqlens_per_mb)}, " + f"num_micro_batches={n_mb}." + ) + # Per-microbatch recorded indices -> per-sample nested tensors, + # attached to each output_lst entry so postprocess_batch_func can + # unbind + restore the original batch order, matching log_probs / + # entropy flow. + per_mb_flat = self._router_replay.collect_recorded( + pad_size_per_mb=pad_size_per_mb, + num_micro_batches=n_mb, + ) + for i, (flat, cu) in enumerate(zip(per_mb_flat, cu_seqlens_per_mb, strict=True)): + output_lst[i].setdefault("model_output", {})["routed_experts"] = ( + torch.nested.nested_tensor_from_jagged(flat, offsets=cu) + ) + + result = postprocess_batch_func(output_lst=output_lst, indices=indices, data=data) + return result + finally: + if rr_active: + self._router_replay.clear() + + def get_data_parallel_rank(self): + return parallel_state.get_parallel_state().device_mesh.get_local_rank("dp") + + def get_data_parallel_size(self): + return torch.distributed.get_world_size() // parallel_state.get_parallel_state().ulysses_size + + def get_data_parallel_group(self): + if parallel_state.get_parallel_state().ulysses_size > 1: + return parallel_state.get_parallel_state().device_mesh.get_group(mesh_dim="dp") + else: + return torch.distributed.group.WORLD + + def get_model_parallel_group(self): + raise NotImplementedError + + def get_context_parallel_group(self): + raise NotImplementedError + + def is_mp_src_rank_with_outputs(self): + """ + Whether the current rank is the first rank in model parallel group that contains model outputs + """ + if parallel_state.get_parallel_state().ulysses_size > 1: + is_collect = parallel_state.get_parallel_state().device_mesh["ulysses"].get_local_rank() == 0 + else: + is_collect = True + return is_collect + + def train_mode(self, **kwargs): + """ + Return a context manager that switches to training mode with VeOmni-specific handling. + + Includes parameter and optimizer offload entry/exit. + """ + return EngineTrainModeCtx(self, **kwargs) + + def eval_mode(self, **kwargs): + """ + Return a context manager that switches to evaluation mode with VeOmni-specific handling. + + Includes activation offload entry/exit. + """ + return EngineEvalModeCtx(self, **kwargs) + + def to(self, device: str, model: bool = True, optimizer: bool = True, grad: bool = True): + """ + Move model parameters, optimizer states, or both to the specified device. + Note that this function executes irrespective of offload config. It serves as manual control. + + Args: + device: Target device identifier. + model: If True, move the model. + optimizer: If True, move the optimizer states. + """ + super(FSDPEngine, self).to(device=device, model=model, optimizer=optimizer, grad=grad) + + device_name = get_device_name() + + assert device in (device_name, "cpu") + if device == device_name: + if model: + load_veomni_model_to_gpu(self.module) + if optimizer and self.optimizer is not None: + load_veomni_optimizer(self.optimizer, device) + elif device == "cpu": + if model: + offload_veomni_model_to_cpu(self.module) + if optimizer and self.optimizer is not None: + offload_veomni_optimizer(self.optimizer) + else: + raise ValueError(f"Invalid device type: {device}") + + def save_checkpoint( + self, + local_path: str, + hdfs_path: Optional[str] = None, + global_step: int = 0, + max_ckpt_to_keep: Optional[int] = None, + **kwargs, + ) -> None: + """ + Save VeOmni checkpoint, handling parameter offload as needed. + """ + origin_module_device = next(self.module.parameters()).device.type + if self._is_offload_param or origin_module_device == "cpu": + load_veomni_model_to_gpu(self.module) + + self.checkpoint_manager.save_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, global_step=global_step, max_ckpt_to_keep=max_ckpt_to_keep + ) + + torch.distributed.barrier() + if self._is_offload_param: + offload_veomni_model_to_cpu(self.module) + + def load_checkpoint( + self, local_path: str, hdfs_path: Optional[str] = None, del_local_after_load: int = True, **kwargs + ) -> None: + """ + Load VeOmni checkpoint, restoring parameters and optimizer state. + """ + if self._is_offload_param: + load_veomni_model_to_gpu(self.module) + + self.checkpoint_manager.load_checkpoint( + local_path=local_path, hdfs_path=hdfs_path, del_local_after_load=del_local_after_load + ) + + torch.distributed.barrier() + if self._is_offload_param: + offload_veomni_model_to_cpu(self.module) + + if self._is_offload_optimizer: + offload_veomni_optimizer(self.optimizer) + + def get_per_tensor_param(self, **kwargs): + load_veomni_model_to_gpu(self.module) + + params = self.module.state_dict() + params = convert_weight_keys(params, getattr(self.module, "_fsdp_wrapped_module", self.module)) + + if self._is_offload_param: + offload_veomni_model_to_cpu(self.module) + + ps = parallel_state.get_parallel_state() + model_type = getattr(self.module.config, "model_type", "default") + process_func = MOE_PARAM_HANDERS.get(model_type, default_moe_param_handler) + + def param_generator(): + for name, param in params.items(): + unsharded_tensor = param.full_tensor() if isinstance(param, DTensor) else param + + is_expert_layer = "mlp.experts." in name + is_proj = any(p in name for p in ["down_proj", "gate_proj", "up_proj", "gate_up_proj"]) + + if is_expert_layer and is_proj and ps.ep_enabled: + ep_rank, ep_size = ps.ep_rank, ps.ep_size + buffer = torch.empty_like(unsharded_tensor) # [num_experts/ep_size, H, I] + for src_ep_rank in range(ep_size): + tensor = unsharded_tensor if src_ep_rank == ep_rank else buffer + torch.distributed.broadcast(tensor, group_src=src_ep_rank, group=ps.ep_group) + yield from process_func(name, tensor, ep_rank=src_ep_rank) + + else: + if is_expert_layer: + yield from process_func(name, unsharded_tensor, ep_rank=0) + else: + yield name, unsharded_tensor + + # TODO: support VeOmni LoRA + return param_generator(), None + + +class EngineEvalModeCtx(BaseEngineCtx): + def __init__(self, engine: VeOmniEngine, **kwargs): + super().__init__(engine=engine, mode="eval", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, VeOmniEngine) + super().__enter__() + self.prev_sp_group = get_ulysses_sequence_parallel_group() + set_ulysses_sequence_parallel_group(self.engine.ulysses_parallel_group) + self.engine.module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, VeOmniEngine) + set_ulysses_sequence_parallel_group(self.prev_sp_group) + + # https://pytorch.org/docs/stable/notes/fsdp.html#fsdp-notes + # unshard the root FSDP module + if parallel_state.get_parallel_state().dp_shard_size > 1: + if fsdp_version(self.engine.module) == 1: + self.engine.module._handle.reshard(True) + elif fsdp_version(self.engine.module) == 2: + self.engine.module.reshard() + + super().__exit__(exc_type, exc_value, traceback) + + +class EngineTrainModeCtx(BaseEngineCtx): + def __init__(self, engine: VeOmniEngine, **kwargs): + super().__init__(engine=engine, mode="train", **kwargs) + + def __enter__(self): + assert isinstance(self.engine, VeOmniEngine) + super().__enter__() + self.prev_sp_group = get_ulysses_sequence_parallel_group() + set_ulysses_sequence_parallel_group(self.engine.ulysses_parallel_group) + # TODO: Switch to eval mode after Integrating the CI environment + # VeOmni (ref: https://github.com/ByteDance-Seed/VeOmni/pull/421) + self.engine.module.train() + + def __exit__(self, exc_type, exc_value, traceback): + assert isinstance(self.engine, VeOmniEngine) + set_ulysses_sequence_parallel_group(self.prev_sp_group) + if self.zero_grad_on_exit or exc_type is not None: + self.engine.optimizer_zero_grad() + super().__exit__(exc_type, exc_value, traceback) + + +@dataclass +class OmniSequenceShardCollator: + """ + Data collator to chunk inputs along the sequence length. + """ + + # features to slice sequence dimension + sp_slice_features: dict[str, int] = field( + default_factory=lambda: { + "input_ids": -1, + "labels": -1, + "pixel_values": 0, + "pixel_values_videos": 0, + }, + metadata={"help": "features to slice sequence dimension."}, + ) + + # features to padding sequence dimension + padding_features: dict[str, int] = field( + default_factory=lambda: { + "pixel_values": 0, + "pixel_values_videos": 0, + }, + metadata={"help": "features to padding sequence dimension."}, + ) + + # padding scale for padding features + padding_scale: dict[str, int] = field( + default_factory=lambda: {"pixel_values": 4, "pixel_values_videos": 4}, + metadata={"help": "padding scale for padding features."}, + ) + + def __post_init__(self): + self.sp_size = parallel_state.get_parallel_state().sp_size + self.sp_rank = parallel_state.get_parallel_state().sp_rank + + def sp_slice(self, feature: torch.Tensor, dim: int = -1) -> dict[str, "torch.Tensor"]: + seq_length = feature.size(dim) + sp_chunk_size = (seq_length + self.sp_size - 1) // self.sp_size + return feature.narrow(dim, self.sp_rank * sp_chunk_size, sp_chunk_size) + + def sp_padding( + self, tensor: "torch.Tensor", dim: int = -1, pad_value: int = 0, pad_scale: int = 1 + ) -> "torch.Tensor": + """ + Pads a tensor with pad_length to aligns tensor with sp size. + """ + seq_length = tensor.size(dim) + scale_sp_size = self.sp_size * pad_scale + + sp_chunk_size = (seq_length + scale_sp_size - 1) // scale_sp_size + pad_size = sp_chunk_size * scale_sp_size - seq_length + if pad_size == 0: + return tensor + + pad_shape = list(tensor.shape) + pad_shape[dim] = pad_size + pad = torch.full(pad_shape, fill_value=pad_value, dtype=tensor.dtype, device=tensor.device) + return torch.cat((tensor, pad), dim=dim) + + def __call__(self, batch: Sequence[dict[str, "torch.Tensor"]]) -> dict[str, "torch.Tensor"]: + for key in batch.keys(): + if key in self.padding_features.keys(): + batch[key] = self.sp_padding( + batch[key], + dim=self.sp_slice_features.get(key, -1), + pad_value=self.padding_features[key], + pad_scale=self.padding_scale.get(key, 1), + ) + + # sp slice + for key in batch.keys(): + if key in self.sp_slice_features.keys(): + batch[key] = self.sp_slice(batch[key], dim=self.sp_slice_features[key]) + + return batch + + +def _prepare_veomni_flash_attention_kwargs(position_ids: torch.Tensor) -> dict[str, torch.Tensor | int]: + """Normalize packed position_ids layout and derive varlen FlashAttention kwargs. + + Supported formats for use_remove_padding=true: + - 2D: (1, total_nnz) - standard packed format + - 3D: (rope_dim, 1, total_nnz) - VeRL mRoPE packed format + """ + if position_ids.dim() == 2: + # (1, total_nnz) - standard packed format + fa_position_ids = position_ids + elif position_ids.dim() == 3: + # (rope_dim, 1, total_nnz) - VeRL mRoPE packed format + if position_ids.shape[1] == 1: + fa_position_ids = position_ids[0] + else: + raise ValueError( + f"Unsupported 3D position_ids shape: {tuple(position_ids.shape)}, expected (rope_dim, 1, total_nnz)" + ) + else: + raise ValueError( + f"Unsupported position_ids rank: {position_ids.dim()}, " + f"expected 2 (1, total_nnz) or 3 (rope_dim, 1, total_nnz)" + ) + + (cu_seq_lens_q, cu_seq_lens_k), (max_length_q, max_length_k) = prepare_fa_kwargs_from_position_ids(fa_position_ids) + return { + "cu_seq_lens_q": cu_seq_lens_q, + "cu_seq_lens_k": cu_seq_lens_k, + "max_length_q": max_length_q, + "max_length_k": max_length_k, + } + + +@EngineRegistry.register(model_type="language_model", backend=["veomni"], device=["cuda", "npu"]) +class VeOmniEngineWithLMHead(VeOmniEngine, FSDPEngineWithLMHead): + def prepare_model_inputs(self, micro_batch: TensorDict): + model_inputs, output_args = super().prepare_model_inputs(micro_batch) + self._apply_veomni_input_transforms(model_inputs, micro_batch) + + # Activate VeOmni's chunk_logprobs path: ForCausalLMLoss short-circuits + # to per-token log_probs/entropy on return_log_probs=True. Pass the + # already-rolled labels as shift_labels so chunk_logprobs skips its + # internal causal shift and the output seq length matches the input — + # prepare_model_outputs().squeeze(0) then lands at (total_nnz,). + use_remove_padding = tu.get_non_tensor_data(data=micro_batch, key="use_remove_padding", default=True) + use_fused_kernels = tu.get_non_tensor_data(data=micro_batch, key="use_fused_kernels", default=False) + if use_fused_kernels and use_remove_padding: + input_ids_rmpad = model_inputs["input_ids"] + shift_labels = output_args["input_ids_rmpad_rolled"].unsqueeze(0) + model_inputs["labels"] = input_ids_rmpad + model_inputs["shift_labels"] = shift_labels + model_inputs["return_log_probs"] = True + + # Pass teacher top-K tensors so ForCausalLMLoss routes to + # chunk_topk_distill_function for fused distillation. TD keys + # teacher_ids / teacher_logprobs are populated by verl's native + # distillation pipeline (see verl/trainer/distillation/losses.py). + distillation_use_topk = tu.get_non_tensor_data(data=micro_batch, key="distillation_use_topk", default=False) + if distillation_use_topk and "teacher_ids" in micro_batch.keys(): + if "teacher_logprobs" not in micro_batch.keys(): + raise ValueError( + "teacher_ids present without teacher_logprobs; " + "both must be provided together for fused top-K distillation." + ) + # Kernel kwarg names follow veomni's chunk_topk_distill_function API. + teacher_topk_ids = micro_batch["teacher_ids"].values().unsqueeze(0) + teacher_topk_log_probs = micro_batch["teacher_logprobs"].values().unsqueeze(0) + # SP-slice along seqlen (dim=1); teacher tensors are 3D + # (1, total_nnz, K) so use slice_input_tensor directly — + # ulysses_pad_and_slice_inputs hardcodes 2D. + if self.use_ulysses_sp: + from verl.utils.ulysses import slice_input_tensor + + teacher_topk_ids = slice_input_tensor(teacher_topk_ids, dim=1, padding=True) + teacher_topk_log_probs = slice_input_tensor(teacher_topk_log_probs, dim=1, padding=True) + model_inputs["teacher_topk_ids"] = teacher_topk_ids + model_inputs["teacher_topk_log_probs"] = teacher_topk_log_probs + + # Router replay plumbing. Two responsibilities: + # (1) snapshot the ulysses pad_size for this micro-batch so + # forward_backward_batch can trim it during RECORD aggregation; + # (2) during REPLAY, slice this micro-batch's routed_experts along + # the same pad+SP rule that super().prepare_model_inputs used + # for input_ids, then feed per-layer targets to the controller. + self._maybe_push_router_replay_state(micro_batch, output_args) + + return model_inputs, output_args + + def _maybe_push_router_replay_state(self, micro_batch: TensorDict, output_args: dict) -> None: + rr = self._router_replay + if rr is None: + return + + # RR's pack/unpack rule assumes ``input_ids`` is a jagged NestedTensor + # built by ``left_right_2_no_padding`` — both the cu_seqlens snapshot + # below and ``slice_microbatch_replay_targets`` rely on the same + # pad+slice rule that ``super().prepare_model_inputs`` applies to it. + # The engine-init guard already rejects ``use_remove_padding=False``, + # but this check defends against future callers that bypass the guard + # (debug tools, sub-engine instantiations) and converts an opaque + # ``AttributeError: 'Tensor' object has no attribute 'offsets'`` into + # a clear invariant failure. + input_ids = micro_batch["input_ids"] + if not (isinstance(input_ids, torch.Tensor) and input_ids.is_nested): + raise RuntimeError( + "router_replay: micro_batch['input_ids'] must be a jagged " + "NestedTensor (produced by left_right_2_no_padding). Got " + f"type={type(input_ids).__name__}, is_nested=" + f"{getattr(input_ids, 'is_nested', False)}. RR currently " + "supports only the use_remove_padding=True data path." + ) + + pad_sink = tu.get_non_tensor_data(micro_batch, "_router_replay_pad_size_out", default=None) + if pad_sink is not None: + pad_sink.append(int(output_args.get("pad_size", 0))) + + # Snapshot cu_seqlens for per-sample split during RECORD aggregation. + cu_sink = tu.get_non_tensor_data(micro_batch, "_router_replay_cu_seqlens_out", default=None) + if cu_sink is not None: + cu_sink.append(input_ids.offsets().clone()) + + if rr.action is RouterReplayAction.REPLAY: + routed = micro_batch.get("routed_experts", None) + if routed is None: + # Strict: no silent fallback. A missing routed_experts in + # REPLAY means the trainer-side plumbing (compute_log_prob -> + # update_actor for R2, or rollout -> compute_log_prob for R3) + # has dropped the field somewhere upstream — silently running + # the actor update on native router decisions would re-introduce + # exactly the floating-point divergence RR is meant to remove. + raise RuntimeError( + "router_replay REPLAY: micro_batch missing 'routed_experts'. " + "Verify that compute_log_prob (R2) or the rollout path (R3) " + "attached routed_experts to the batch before this engine " + "call, and that left_right_2_no_padding preserved it." + ) + # Nested-jagged [bs, seq, L, topk] → rmpad values [mb_nnz, L, topk]. + # slice_microbatch_replay_targets reuses slice_input_tensor to + # mirror the exact pad+slice rule super().prepare_model_inputs + # already applied to input_ids. + flat = routed.values() if hasattr(routed, "values") else routed + per_layer = rr.slice_microbatch_replay_targets(flat) + + # Per-token replay mask — R3 only. + # + # R2 RECORD captures the actor's full-sequence routing + # (prompt + response) in compute_log_prob, so REPLAY + # substitutes uniformly. Applying a response-only mask + # would let prompt tokens fall through to a fresh native + # call — atomic-add nondeterminism in the fused MoE + # experts means that call may pick different indices than + # RECORD, breaking the bit-equal forward guarantee R2 + # exists for. The divergence then propagates through + # attention KV into response logits and gradients. + # + # R3 RECORD runs at the rollout backend, which only + # captures response-token routing during generation; + # prefill is not instrumented and prompt-token positions + # carry zero placeholders. Substituting those zeros sends + # every prompt token's topk slots to expert 0, corrupting + # the EP all-to-all token distribution. R3 must mask + # prompt tokens out and let them go through native routing. + replay_mask = None + if self._router_replay_mode == "R3": + response_mask = micro_batch.get("response_mask", None) + if response_mask is None: + raise RuntimeError( + "router_replay R3: micro_batch missing 'response_mask'. " + "R3 needs the response_mask to know which tokens have " + "real recorded routing (response) vs. zero placeholders " + "(prompt). Verify left_right_2_no_padding preserved it." + ) + # Build a per-rmpad-token bool mask in the SAME [total_nnz] + # layout as ``input_ids.values()`` (also matches + # ``routed_experts.values()`` since they share the same + # ``index_first_axis(unpad_input(input_ids).indices)`` + # transform): per sample i, ``prompt_lens[i]`` zeros + # followed by ``response_lens[i]`` ones. + # + # We CANNOT use ``micro_batch['loss_mask']`` directly — + # after ``left_right_2_no_padding`` it's still a strided + # ``(bs, max_response_len)`` tensor (not nested), which + # neither has the right shape nor a valid ``.values()`` + # for a strided layout. + total_lens = input_ids.offsets().diff() # (bs,) + response_lens = response_mask.sum(dim=-1).to(total_lens.dtype) # (bs,) + prompt_lens = total_lens - response_lens # (bs,) + # Defensive: response_lens > total_lens means the + # response_mask describes more tokens than the input has, + # which is data corruption. Failing here surfaces a clear + # message instead of letting repeat_interleave silently + # produce a malformed mask. + if torch.any(prompt_lens < 0): + raise RuntimeError( + f"router_replay R3: response_mask sum exceeds total token " + f"count for some samples — prompt_lens={prompt_lens.tolist()}. " + "Likely cause: response_mask was not aligned with the " + "input_ids the actor sees (rollout/trainer plumbing bug)." + ) + bs = total_lens.size(0) + # values=[0, 1, 0, 1, ...] (length 2*bs), counts=[p_0, r_0, p_1, r_1, ...] + values = torch.tensor([False, True], dtype=torch.bool, device=total_lens.device).repeat(bs) + counts = torch.stack([prompt_lens, response_lens], dim=1).flatten() + mask_flat = torch.repeat_interleave(values, counts) + # Defensive: the constructed mask must align with + # routed_experts at the rmpad layer, otherwise the + # downstream ``torch.where(mask, target, native)`` would + # silently misalign and the EP all-to-all would still + # blow up. Fail-fast here with a clearer message. + if mask_flat.numel() != flat.size(0): + raise RuntimeError( + f"router_replay R3: constructed replay_mask has " + f"{mask_flat.numel()} entries but routed_experts.values() " + f"has {flat.size(0)}. response_mask + input_ids.offsets() " + "do not describe the same total token count." + ) + # Mirror the same pad+slice rule used for routed_experts. + replay_mask = rr.slice_microbatch_replay_mask(mask_flat) + + rr.set_microbatch_targets(per_layer, replay_mask=replay_mask) + + +@EngineRegistry.register(model_type="value_model", backend=["veomni"], device=["cuda", "npu"]) +class VeOmniEngineWithValueHead(VeOmniEngine, FSDPEngineWithValueHead): + """Value model engine using VeOmni's FSDP2 + sequence parallelism. + + Combines VeOmniEngine (model init, parallel state, activation offloading) + with FSDPEngineWithValueHead (TokenClassification output -> per-token values). + """ + + def _get_model_config_path(self): + """Return a modified HF config that loads ForTokenClassification(num_labels=1). + + Uses HF's AutoModelForTokenClassification model mapping to resolve the + canonical ForTokenClassification class name for this model family, then + sets config.architectures so VeOmni's MODELING_REGISTRY dispatches to it. + """ + from transformers import AutoModelForTokenClassification + from veomni.models.auto import build_config + + config = build_config(self.model_config.local_hf_config_path) + config.num_labels = 1 + config.classifier_dropout = 0.0 + config.hidden_dropout = "0" + config.summary_dropout_prob = 0.0 + config.tie_word_embeddings = False + token_cls = AutoModelForTokenClassification._model_mapping.get(type(config), None) + if token_cls is None: + raise ValueError(f"No ForTokenClassification class in transformers for {type(config).__name__}.") + config.architectures = [token_cls.__name__] + return config + + def prepare_model_inputs(self, micro_batch: TensorDict): + model_inputs, output_args = super().prepare_model_inputs(micro_batch) + self._apply_veomni_input_transforms(model_inputs, micro_batch) + return model_inputs, output_args diff --git a/verl_0720_main/verl/verl/workers/engine/veomni/utils.py b/verl_0720_main/verl/verl/workers/engine/veomni/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..3a1fe722c55c54d301f1f539b2a747044fc1ae1a --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine/veomni/utils.py @@ -0,0 +1,134 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch + +from verl.utils.device import get_device_id, get_torch_device + +VL_TYPE2INDEX = { + "qwen2_5_vl": { + "IMAGE_INPUT_INDEX": 151655, + "VIDEO_INPUT_INDEX": 151656, + }, + "qwen3_vl": { + "IMAGE_INPUT_INDEX": 151655, + "VIDEO_INPUT_INDEX": 151656, + }, + "qwen3_vl_moe": { + "IMAGE_INPUT_INDEX": 151655, + "VIDEO_INPUT_INDEX": 151656, + }, + "qwen3_5": { + "IMAGE_INPUT_INDEX": 248056, + "VIDEO_INPUT_INDEX": 248057, + }, + "qwen3_5_moe": { + "IMAGE_INPUT_INDEX": 248056, + "VIDEO_INPUT_INDEX": 248057, + }, +} + + +@torch.no_grad() +def offload_veomni_model_to_cpu(model, empty_cache: bool = True): + from torch.distributed.fsdp._fully_shard._fsdp_common import TrainingState + from torch.distributed.fsdp._fully_shard._fsdp_state import _get_module_fsdp_state + + for module in model.modules(): + state = _get_module_fsdp_state(module) + if state is None: + continue + fsdp_param_group = state._fsdp_param_group + + if fsdp_param_group is None: + continue + + fsdp_param_group._training_state = TrainingState.IDLE + + model.reshard() + model.cpu() + if empty_cache: + get_torch_device().empty_cache() + + +@torch.no_grad() +def load_veomni_model_to_gpu(model): + device = get_device_id() + model.to(device) + + +@torch.no_grad() +def offload_veomni_optimizer(optimizer): + optimizers = [] + # Check if this is a MultiOptimizer (for ep and non-ep parameters when ep+fsdp2 is enabled) + if hasattr(optimizer, "_is_multi_optimizer") and optimizer._is_multi_optimizer: + optimizers.extend(optimizer.optimizers_dict.values()) + else: + optimizers.append(optimizer) + + for opt in optimizers: + if not opt.state: + continue + for param_group in opt.param_groups: + for param in param_group["params"]: + state = opt.state[param] + for key, value in state.items(): + if isinstance(value, torch.Tensor): + state[key] = value.to("cpu", non_blocking=True) + + +@torch.no_grad() +def load_veomni_optimizer(optimizer, device_id): + optimizers = [] + # Check if this is a MultiOptimizer (for ep and non-ep parameters when ep+fsdp2 is enabled) + if hasattr(optimizer, "_is_multi_optimizer") and optimizer._is_multi_optimizer: + optimizers.extend(optimizer.optimizers_dict.values()) + else: + optimizers.append(optimizer) + + for opt in optimizers: + if not opt.state: + continue + for param_group in opt.param_groups: + for param in param_group["params"]: + state = opt.state[param] + for key, value in state.items(): + if isinstance(value, torch.Tensor): + state[key] = value.to(device_id, non_blocking=True) + + +def _map_moe_params_common(name, tensor, ep_rank): + num_experts_per_rank = tensor.size(0) + for i in range(num_experts_per_rank): + idx = ep_rank * num_experts_per_rank + i + new_key = name.replace("mlp.experts.", f"mlp.experts.{idx}.") + ".weight" + yield new_key, tensor[i].to(get_device_id(), non_blocking=True) + + +def default_moe_param_handler(name, tensor, ep_rank): + if "gate_up_proj" in name: + gate, up = tensor.chunk(2, dim=1) + params = { + name.replace("gate_up_proj", "gate_proj"): gate, + name.replace("gate_up_proj", "up_proj"): up, + } + else: + params = {name: tensor} + + for key, value in params.items(): + yield from _map_moe_params_common(key, value, ep_rank) + + +# This is used to override the default mapping of MoE parameters +MOE_PARAM_HANDERS = {} diff --git a/verl_0720_main/verl/verl/workers/engine_workers.py b/verl_0720_main/verl/verl/workers/engine_workers.py new file mode 100644 index 0000000000000000000000000000000000000000..ab9ca0e9ba46cc48e75da498b0f5e31d79dc0333 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine_workers.py @@ -0,0 +1,767 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import functools +import logging +import os +from contextlib import nullcontext +from copy import deepcopy +from functools import partial +from itertools import chain +from typing import Optional + +import psutil +import torch +from codetiming import Timer +from omegaconf import DictConfig, open_dict +from tensordict import NonTensorData, TensorDict +from torch.distributed.device_mesh import init_device_mesh + +from verl.checkpoint_engine import CheckpointEngineRegistry +from verl.single_controller.base import Worker +from verl.single_controller.base.decorator import Dispatch, make_nd_compute_dataproto_dispatch_fn, register +from verl.trainer.distillation import distillation_ppo_loss, is_distillation_enabled +from verl.utils import tensordict_utils as tu +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.device import get_device_name, get_torch_device, set_expandable_segments +from verl.utils.distributed import initialize_global_process_group_ray, set_numa_affinity +from verl.utils.flops_counter import FlopsCounter +from verl.utils.import_utils import import_external_libs +from verl.utils.memory_utils import aggressive_empty_cache +from verl.utils.metric.utils import Metric +from verl.utils.profiler import DistProfiler, DistProfilerExtension, ProfilerConfig, log_gpu_memory_usage +from verl.utils.py_functional import append_to_dict +from verl.utils.tensordict_utils import maybe_fix_3d_position_ids +from verl.utils.torch_functional import allgather_dict_into_dict +from verl.workers.config import ( + ActorConfig, + DistillationConfig, + HFModelConfig, + MtpConfig, + RolloutConfig, + TrainingWorkerConfig, +) +from verl.workers.rollout.base import BaseRollout, get_rollout_class +from verl.workers.utils.losses import ppo_loss + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +def _with_routing_replay_flag(enabled: bool): + """Decorator to set 'enable_routing_replay' flag on the data TensorDict.""" + + def decorator(func): + @functools.wraps(func) + def wrapper(self, data: TensorDict, *args, **kwargs): + if self.enable_routing_replay: + tu.assign_non_tensor_data(data, "enable_routing_replay", enabled) + return func(self, data, *args, **kwargs) + + return wrapper + + return decorator + + +class TrainingWorker(Worker, DistProfilerExtension): + """ + TrainingWorker provides a Tinker-like API (https://thinkingmachines.ai/tinker/) as a RayWorkerGroup + to a single controller. Currently, we only provide more coarse grained APIs, + and do not provide exact APIs as Tinker does. But this can be added in the future. + """ + + def __init__(self, config: TrainingWorkerConfig): + Worker.__init__(self) + + from verl.workers.engine import BaseEngine, EngineRegistry + + initialize_global_process_group_ray(timeout_second=None) + + set_numa_affinity() + + self.config = config + self.model_config = self.config.model_config + self.engine_config = self.config.engine_config + self.optimizer_config = self.config.optimizer_config + self.checkpoint_config = self.config.checkpoint_config + self.device_name = get_device_name() + + if self.engine_config is None: + assert self.optimizer_config is None + if self.config.auto_select_engine_optim_fn is None: + raise ValueError( + "engine_config is not provided and auto_select_engine_optim_fn is not set. " + "Cannot determine engine backend." + ) + # Support automatically select engine backend given model config + self.engine_config, self.optimizer_config = self.config.auto_select_engine_optim_fn( + self.model_config, self.device_name + ) + + # we use the one defined in model + # TODO: this is not elegant and should refactor later + self.engine_config.use_remove_padding = self.model_config.get("use_remove_padding", False) + self.engine_config.use_fused_kernels = self.model_config.get("use_fused_kernels", False) + + self.profiler_config = self.config.profiler_config + if self.profiler_config is not None: + self.profiler_tool_config = self.profiler_config.tool_config.get(self.profiler_config.tool, {}) + else: + self.profiler_tool_config = None + + DistProfilerExtension.__init__( + self, DistProfiler(rank=self.rank, config=self.profiler_config, tool_config=self.profiler_tool_config) + ) + + self.model_config.model_type = self.config.model_type + self.engine: BaseEngine = EngineRegistry.new( + model_type=self.config.model_type, + backend=self.engine_config.strategy, + model_config=self.model_config, + engine_config=self.engine_config, + optimizer_config=self.optimizer_config, + checkpoint_config=self.checkpoint_config, + ) + + # build dispatch info + self._register_dispatch_collect_info( + mesh_name="train", + dp_rank=self.engine.get_data_parallel_rank(), + is_collect=self.engine.is_mp_src_rank_with_outputs(), + ) + + if hasattr(self.model_config, "hf_config"): + self.flops_counter = FlopsCounter(self.model_config.hf_config) + else: + self.flops_counter = None + + self.loss_fn = None + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def to(self, device, model=True, optimizer=True, grad=True): + """Manual control of load/offload""" + assert device in ["cpu", "device"] + + if device == "device": + device = get_device_name() + + self.engine.to(device=device, model=model, optimizer=optimizer, grad=grad) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def set_loss_fn(self, loss_fn): + self.loss_fn = loss_fn + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def reset(self): + """ + Reset the model engine to the initial state. If the engine is not initialized, + we initialize it. Otherwise, reload ckpt and reset states + """ + self.engine.initialize() + + def _postprocess_output(self, output, *, global_token_num, delta_time, forward_only, images_seqlens): + """ + + Args: + output: a dictionary containing loss, model_outputs and metrics + + Returns: + + """ + + metrics: dict = output.pop("metrics") + # perform all gather in dp group to ensure that it's correct. + # Here each metric in metrics can be a list (micro-batch metrics) or a singleton + # we should always sum the loss of each micro-batch as we scale by global_bsz/global_token + loss = torch.sum(torch.tensor(output.pop("loss"), device=self.device_name)) + dp_group = self.engine.get_data_parallel_group() + if dp_group is not None: + torch.distributed.all_reduce(loss, op=torch.distributed.ReduceOp.AVG, group=dp_group) + loss = loss.item() + + # For grad_norm, we do not perform all reduce because it is already been done when clipping grad + grad_norm = metrics.pop("grad_norm", None) + if isinstance(grad_norm, torch.Tensor): + grad_norm = grad_norm.detach().item() + lr = metrics.pop("lr", None) + + # For other metrics, we perform all gather in dp group (only if DP > 1) + if dp_group is not None: + final_metrics = allgather_dict_into_dict(data=metrics, group=dp_group) + else: + final_metrics = metrics + final_metrics["loss"] = loss + if grad_norm is not None: + final_metrics["grad_norm"] = grad_norm + if lr is not None: + final_metrics["lr"] = lr + + # log memory + final_metrics["perf/max_memory_allocated_gb"] = get_torch_device().max_memory_allocated() / (1024**3) + final_metrics["perf/max_memory_reserved_gb"] = get_torch_device().max_memory_reserved() / (1024**3) + final_metrics["perf/cpu_memory_used_gb"] = psutil.virtual_memory().used / (1024**3) + + # TODO: confirm the mtp loss IS same across dp + for k, v in final_metrics.items(): + if k.startswith("mtp_losses"): + flatten_v = [sublist[0] for sublist in v] # sublist should be single element + final_metrics[k] = sum(flatten_v) / len(flatten_v) + # compute mfu + if global_token_num is not None and self.flops_counter is not None: + estimated_flops, promised_flops = self.flops_counter.estimate_flops( + global_token_num, delta_time, images_seqlens=images_seqlens + ) + final_metrics["mfu"] = estimated_flops / promised_flops / torch.distributed.get_world_size() + if forward_only: + final_metrics["mfu"] /= 3.0 + # model outputs + model_output = output.pop("model_output", {}) + # We only return final_metrics + final_output = tu.get_tensordict(tensor_dict=model_output, non_tensor_dict={"metrics": final_metrics}) + return final_output + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="train"), blocking=False) + def train_mini_batch(self, data: TensorDict) -> TensorDict: + """Split a batch into N mini-batches run for multiple epochs + + Args: + data: + + Returns: + + """ + maybe_fix_3d_position_ids(data) + batch_size_per_dp = data.shape[0] + disable_auto_offload = tu.pop(data, key="disable_auto_offload", default=False) + mini_batch_size = tu.pop(data, key="mini_batch_size", default=None) + num_mini_batch = tu.pop(data, key="num_mini_batch", default=None) + epochs = tu.pop(data, key="epochs", default=1) + seed = tu.pop(data, key="seed", default=42) + dataloader_kwargs = tu.pop(data, key="dataloader_kwargs", default={}) + + assert mini_batch_size is not None or num_mini_batch is not None + + if mini_batch_size is None: + assert batch_size_per_dp % num_mini_batch == 0, f"Got {batch_size_per_dp=} and {num_mini_batch=}" + mini_batch_size_per_gpu = batch_size_per_dp // num_mini_batch + else: + assert mini_batch_size % self.engine.get_data_parallel_size() == 0, ( + f"Got {mini_batch_size=} and {self.engine.get_data_parallel_size()=}" + ) + mini_batch_size_per_gpu = mini_batch_size // self.engine.get_data_parallel_size() + + # make iterator + dataloader = tu.make_iterator( + data, + mini_batch_size=mini_batch_size_per_gpu, + epochs=epochs, + seed=seed + self.engine.get_data_parallel_rank(), + dataloader_kwargs=dataloader_kwargs, + ) + + with ( + self.engine.train_mode(disable_auto_offload=disable_auto_offload), + Timer(name="train_batch", logger=None), + ): + # update + output_lst = [] + total_num_iterations = data.shape[0] // mini_batch_size_per_gpu * epochs + + for batch_idx, mini_batch_td in enumerate(dataloader): + maybe_fix_3d_position_ids(mini_batch_td) + # add global token num + if "input_ids" in mini_batch_td: + global_token_num = mini_batch_td["input_ids"].offsets().diff().tolist() # (total_nnz,) + # allgather from dp rank + global_token_num_output = [None] * torch.distributed.get_world_size( + self.engine.get_data_parallel_group() + ) + torch.distributed.all_gather_object( + global_token_num_output, global_token_num, self.engine.get_data_parallel_group() + ) + global_token_num = [x for xs in global_token_num_output for x in xs] + else: + global_token_num = None + + tu.assign_non_tensor( + mini_batch_td, + global_token_num=NonTensorData(global_token_num), + update_lr_scheduler=batch_idx == total_num_iterations - 1, + disable_auto_offload=True, + ) + actor_output = self.train_batch(mini_batch_td) + output_lst.append(actor_output) + + if self.engine.is_mp_src_rank_with_outputs(): + actor_output = [tu.get(output, "metrics") for output in output_lst] + metrics = {} + for output in actor_output: + for key, val in output.items(): + # flattn dp and micro batch + if isinstance(val, list): + output[key] = ( + Metric.aggregate_dp(val) + if isinstance(val[0], Metric) + else list(chain.from_iterable(val)) + ) + append_to_dict(metrics, output) + + output = tu.get_tensordict(tensor_dict={}, non_tensor_dict={"metrics": metrics}).cpu() + else: + output = None + return output + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="train"), blocking=False) + @DistProfiler.annotate(color="red", role="train_batch") + def train_batch(self, data: TensorDict) -> TensorDict: + assert self.loss_fn is not None, "loss function can't be None when calling train_batch" + assert not self.engine_config.forward_only, "Can't run `train_batch` when forward_only is in the engine config." + # global_token_num should be a list of number of tokens of each seq in this batch + global_token_num = tu.get(data, key="global_token_num") + disable_auto_offload = tu.get(data, key="disable_auto_offload", default=False) + images_seqlens = tu.get(data, key="images_seqlens", default=None) + + # inject engineering parameters if not specified + default_keys = dict( + use_remove_padding=self.model_config.get("use_remove_padding", False), + use_dynamic_bsz=self.engine_config.use_dynamic_bsz, + max_token_len_per_gpu=self.engine_config.max_token_len_per_gpu, + micro_batch_size_per_gpu=self.engine_config.micro_batch_size_per_gpu, + use_fused_kernels=self.engine_config.use_fused_kernels, + ) + + for key, val in default_keys.items(): + if key not in data.keys(): + tu.assign_non_tensor(data, **{key: val}) + + with ( + self.engine.train_mode(disable_auto_offload=disable_auto_offload), + Timer(name="train_batch", logger=None) as timer, + ): + output = self.engine.train_batch(data, loss_function=self.loss_fn) + # containing loss, model_output and metrics + # for training, we only care about loss and metrics + delta_time = timer.last + + update_lr_scheduler = tu.get(data, key="update_lr_scheduler", default=False) + # update lr scheduler + if update_lr_scheduler: + lr = self.engine.lr_scheduler_step() + else: + lr = None + + if self.engine.is_mp_src_rank_with_outputs(): + # we don't need model_output in training. Maybe we change out mind later + output.pop("model_output") + if lr is not None: + output["metrics"]["lr"] = lr + final_output = self._postprocess_output( + output, + global_token_num=global_token_num, + delta_time=delta_time, + forward_only=False, + images_seqlens=images_seqlens, + ).cpu() + else: + final_output = None + + return final_output + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="train"), blocking=False) + def infer_batch(self, data: TensorDict) -> TensorDict: + # add mfu calculator + global_token_num = tu.get(data, key="global_token_num") + compute_loss = tu.get(data, key="compute_loss", default=True) + disable_auto_offload = tu.get(data, key="disable_auto_offload", default=False) + no_lora_adapter = tu.pop(data, key="no_lora_adapter", default=False) + images_seqlens = tu.get(data, key="images_seqlens", default=None) + + default_keys = dict( + use_remove_padding=self.model_config.get("use_remove_padding", False), + use_dynamic_bsz=self.engine_config.use_dynamic_bsz, + max_token_len_per_gpu=self.engine_config.infer_max_token_len_per_gpu, + micro_batch_size_per_gpu=self.engine_config.infer_micro_batch_size_per_gpu, + use_fused_kernels=self.engine_config.use_fused_kernels, + ) + + for key, val in default_keys.items(): + if key not in data.keys(): + tu.assign_non_tensor(data, **{key: val}) + + # for sft training, we need to compute loss in eval + loss_function = self.loss_fn if compute_loss else None + + with ( + self.engine.eval_mode(disable_auto_offload=disable_auto_offload), + Timer(name="eval_batch", logger=None) as timer, + ): + adapter_ctx = self.engine.disable_adapter() if no_lora_adapter else nullcontext() + with adapter_ctx: + output = self.engine.infer_batch(data, loss_function=loss_function) + delta_time = timer.last + + if self.engine.is_mp_src_rank_with_outputs(): + final_output = self._postprocess_output( + output, + global_token_num=global_token_num, + delta_time=delta_time, + forward_only=True, + images_seqlens=images_seqlens, + ).cpu() + else: + final_output = None + + return final_output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, local_path, hdfs_path=None, global_step=0, max_ckpt_to_keep=None): + return self.engine.save_checkpoint(local_path, hdfs_path, global_step, max_ckpt_to_keep) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def load_checkpoint(self, local_path, hdfs_path=None, del_local_after_load=False): + return self.engine.load_checkpoint(local_path, hdfs_path, del_local_after_load) + + +class ActorRolloutRefWorker(Worker, DistProfilerExtension): + """Hybrid worker that includes actor model, rollout and optional ref model. + For standalone actor or rollout, use ActorWorker or BaseRollout respectively. + + NOTE: ActorRolloutRefWorker no longer support spmd mode and run native server mode. + """ + + actor_worker_cls = TrainingWorker + ref_worker_cls = TrainingWorker + + def __init__( + self, config: DictConfig, role: str, distillation_config: Optional[DistillationConfig] = None, **kwargs + ): + Worker.__init__(self) + self.config = config + self.distillation_config = distillation_config + self.distillation_enabled = is_distillation_enabled(distillation_config) + self.role = role + self.actor: TrainingWorker | None = None + self.ref: TrainingWorker | None = None + self.rollout: BaseRollout = None + assert self.role in ["actor", "rollout", "ref", "actor_rollout", "actor_rollout_ref"] + self._is_actor = self.role in ["actor", "actor_rollout", "actor_rollout_ref"] + self._is_rollout = self.role in ["rollout", "actor_rollout", "actor_rollout_ref"] + self._is_ref = self.role in ["ref", "actor_rollout_ref"] + + if self._is_actor: + omega_profiler_config = config.actor.get("profiler", {}) + elif self._is_rollout: + # NOTE: In colocation mode, rollout config may not take effect (follow the actor config) + # This is for extendability in AsyncRL cases + omega_profiler_config = config.rollout.get("profiler", {}) + else: + omega_profiler_config = config.ref.get("profiler", {}) + + profiler_config = omega_conf_to_dataclass(omega_profiler_config, dataclass_type=ProfilerConfig) + if omega_profiler_config.get("tool", None) in ["npu", "nsys", "torch", "torch_memory", "precision_debugger"]: + tool_config = omega_conf_to_dataclass( + omega_profiler_config.get("tool_config", {}).get(omega_profiler_config.get("tool")) + ) + else: + tool_config = None + + # Router replay is supported on the megatron engine and on the veomni + # engine. Both expose `router_replay` on their per-strategy engine + # config (the field lives on the shared `EngineConfig` base). + actor_strategy = self.config.actor.strategy + if actor_strategy == "megatron": + rr_mode = self.config.actor.megatron.router_replay.mode + elif actor_strategy == "veomni": + rr_mode = self.config.actor.veomni.router_replay.mode + else: + rr_mode = "disabled" + self.enable_routing_replay = rr_mode != "disabled" + + DistProfilerExtension.__init__( + self, DistProfiler(rank=self.rank, config=profiler_config, tool_config=tool_config) + ) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def set_loss_fn(self, loss_fn): + self.actor.set_loss_fn(loss_fn=loss_fn) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def to(self, device, model=True, optimizer=True, grad=True): + """Manual control of load/offload""" + self.actor.to(device=device, model=model, optimizer=optimizer, grad=grad) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + model_config: HFModelConfig = omega_conf_to_dataclass(self.config.model) + + # 1. build reference model + if "ref" in self.role: + # TODO: align ref config with actor config + with open_dict(self.config.ref): + self.config.ref.ppo_mini_batch_size = self.config.actor.ppo_mini_batch_size + self.config.ref.ppo_micro_batch_size = self.config.ref.pop("log_prob_micro_batch_size", None) + self.config.ref.ppo_micro_batch_size_per_gpu = self.config.ref.pop( + "log_prob_micro_batch_size_per_gpu", None + ) + self.config.ref.use_dynamic_bsz = self.config.ref.pop("log_prob_use_dynamic_bsz", False) + self.config.ref.ppo_max_token_len_per_gpu = self.config.ref.pop("log_prob_max_token_len_per_gpu", None) + ref_config: ActorConfig = omega_conf_to_dataclass(self.config.ref) + + # The ref model does not need to enable MTP; force it to false. + ref_config.model_config = deepcopy(model_config) + ref_config.model_config.mtp = MtpConfig(enable=False) + + # construct TrainingWorkerConfig + ref_training_config = TrainingWorkerConfig( + model_type=ref_config.model_config.get("model_type", "language_model"), + model_config=ref_config.model_config, + engine_config=ref_config.engine, + optimizer_config=ref_config.optim, + checkpoint_config=ref_config.checkpoint, + ) + + # assign engine configs + ref_training_config.engine_config.use_dynamic_bsz = self.config.ref.use_dynamic_bsz + ref_training_config.engine_config.infer_max_token_len_per_gpu = self.config.ref.ppo_max_token_len_per_gpu + ref_training_config.engine_config.infer_micro_batch_size_per_gpu = ( + self.config.ref.ppo_micro_batch_size_per_gpu + ) + ref_training_config.engine_config.use_remove_padding = model_config.get("use_remove_padding", False) + + self.ref = self.ref_worker_cls(config=ref_training_config) + self.ref.reset() + self.set_dispatch_collect(mesh_name="ref", **self.ref.get_dispatch_collect()) + + # 2. build actor model + if "actor" in self.role: + actor_config: ActorConfig = omega_conf_to_dataclass(self.config.actor) + actor_config.model_config = model_config + distillation_config: Optional[DistillationConfig] = ( + omega_conf_to_dataclass(self.distillation_config) if self.distillation_enabled else None + ) + + actor_training_config = TrainingWorkerConfig( + model_type=actor_config.model_config.get("model_type", "language_model"), + model_config=actor_config.model_config, + engine_config=actor_config.engine, + optimizer_config=actor_config.optim, + checkpoint_config=actor_config.checkpoint, + ) + + assert self.config.actor.use_dynamic_bsz == self.config.rollout.log_prob_use_dynamic_bsz + + # assign engine configs + actor_training_config.engine_config.use_dynamic_bsz = self.config.actor.use_dynamic_bsz + actor_training_config.engine_config.infer_max_token_len_per_gpu = ( + self.config.rollout.log_prob_max_token_len_per_gpu + ) + actor_training_config.engine_config.infer_micro_batch_size_per_gpu = ( + self.config.rollout.log_prob_micro_batch_size_per_gpu + ) + actor_training_config.engine_config.max_token_len_per_gpu = self.config.actor.ppo_max_token_len_per_gpu + actor_training_config.engine_config.micro_batch_size_per_gpu = ( + self.config.actor.ppo_micro_batch_size_per_gpu + ) + actor_training_config.engine_config.use_remove_padding = model_config.get("use_remove_padding", False) + + if self.config.actor.use_dynamic_bsz: + assert self.config.rollout.log_prob_max_token_len_per_gpu is not None + assert self.config.actor.ppo_max_token_len_per_gpu is not None + else: + assert self.config.rollout.log_prob_micro_batch_size_per_gpu is not None + assert self.config.actor.ppo_micro_batch_size_per_gpu is not None + if self.distillation_enabled: + self.loss_fn = partial( + distillation_ppo_loss, config=actor_config, distillation_config=distillation_config + ) + else: + self.loss_fn = partial(ppo_loss, config=actor_config) + self.actor = self.actor_worker_cls(config=actor_training_config) + self.actor.reset() + self.actor.set_loss_fn(self.loss_fn) + self.set_dispatch_collect(mesh_name="actor", **self.actor.get_dispatch_collect()) + + # 3. build rollout engine + if "rollout" in self.role: + rollout_config: RolloutConfig = omega_conf_to_dataclass(self.config.rollout) + + # TODO: move rollout_device_mesh into ServerAdapter + # 3.1 build rollout device mesh (sglang need only) + infer_tp = rollout_config.tensor_model_parallel_size * rollout_config.data_parallel_size + infer_pp = rollout_config.pipeline_model_parallel_size + infer_world_size = infer_tp * infer_pp + dp = self.world_size // infer_world_size + assert self.world_size % infer_world_size == 0, ( + f"rollout world_size: {self.world_size} is not divisible by infer_world_size: {infer_world_size}" + ) + rollout_device_mesh = init_device_mesh( + get_device_name(), mesh_shape=(dp, infer_tp, infer_pp), mesh_dim_names=["dp", "infer_tp", "infer_pp"] + ) + + # 3.2 initialize rollout engine + rollout_cls: type[BaseRollout] = get_rollout_class(rollout_config.name, rollout_config.mode) + self.rollout = rollout_cls( + config=rollout_config, model_config=model_config, device_mesh=rollout_device_mesh + ) + + # used for LoRA (base_sync_done is unused in merge-only mode but kept for Phase 2 adapter path) + self.base_sync_done: bool = "dummy" not in self.config.rollout.load_format + self.layered_summon = self.config.rollout.get("layered_summon", False) + self.peft_merge: bool = model_config.lora.get("merge", False) + + # 4. build checkpoint engine + if "actor" in self.role: + checkpoint_engine_config = omega_conf_to_dataclass(self.config.rollout.checkpoint_engine) + backend = checkpoint_engine_config.backend + bucket_size = checkpoint_engine_config.update_weights_bucket_megabytes << 20 + engine_kwargs = checkpoint_engine_config.engine_kwargs.get(backend, {}) + # If custom_backend_module is set, import it so plugins can register + # in CheckpointEngineRegistry before the backend is instantiated. + import_external_libs(checkpoint_engine_config.custom_backend_module or None) + self.checkpoint_engine = CheckpointEngineRegistry.new( + backend, is_master=(torch.distributed.get_rank() == 0), bucket_size=bucket_size, **engine_kwargs + ) + + # Free cached GPU memory so colocated vLLM processes can see it via cudaMemGetInfo + aggressive_empty_cache(force_sync=True) + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="ref")) + @DistProfiler.annotate(color="olive", role="ref_compute_log_prob") + @_with_routing_replay_flag(enabled=False) + def compute_ref_log_prob(self, data: TensorDict) -> TensorDict: + output = self.ref.infer_batch(data=data) + return output.cpu() if output is not None else None + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="actor")) + @DistProfiler.annotate(color="blue", role="actor_compute_log_prob") + @_with_routing_replay_flag(enabled=True) + def compute_log_prob(self, data: TensorDict) -> TensorDict: + output = self.actor.infer_batch(data) + + return output.cpu() if output is not None else None + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="actor")) + @DistProfiler.annotate(color="red", role="actor_update") + @_with_routing_replay_flag(enabled=True) + def update_actor(self, data: TensorDict) -> TensorDict: + output = self.actor.train_mini_batch(data=data) + return output.cpu() if output is not None else None + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def load_checkpoint(self, local_path, hdfs_path=None, del_local_after_load=False): + assert "actor" in self.role, "load_checkpoint only support actor role" + self.actor.load_checkpoint(local_path, hdfs_path, del_local_after_load) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, local_path, hdfs_path=None, global_step=0, max_ckpt_to_keep=None): + assert "actor" in self.role, "save_checkpoint only support actor role" + self.actor.save_checkpoint(local_path, hdfs_path, global_step, max_ckpt_to_keep) + + @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) + async def update_weights(self, global_steps: int = None, mode: str = "auto"): + """Update weights from trainer to rollout. + + 1. For sync training with colocated trainer and rollout, update rollout directly from model engine. + - before update_weights: rollout should be in sleep mode. + - after update_weights: rollout should be in wake_up mode. + 2. For async training with disaggregated trainer and rollout, send_weights only by checkpoint engine. + + LoRA handling: when model.lora.merge=True (peft_merge), LoRA is merged into + base weights before sync. The engine returns full HF-keyed params with + peft_config=None, so the rollout receives a standard weight update. + + Args: + global_steps: Current global training step count, passed to rollout for logging/tracking. + mode: Weight update strategy. Supported values: + - ``"auto"``: Automatically resolve to the backend configured in + ``config.rollout.checkpoint_engine.backend`` (default). + - ``"naive"``: Direct in-process weight sync between colocated trainer + and rollout. Used for synchronous training where both share the same + process. Rollout must be in sleep mode before this call. + - Any other value: Delegates to + :meth:`checkpoint_engine.send_weights` for asynchronous weight + transfer via checkpoint engine, suitable for disaggregated + trainer/rollout deployments. + """ + + # Resolve mode: "auto" falls back to config, explicit values take precedence + effective_mode = mode if mode != "auto" else self.config.rollout.checkpoint_engine.backend + + # 0. send_weights only for async training with disaggregated trainer and rollout + if effective_mode != "naive": + # The sharded delta engine diffs each rank's local FSDP shard (no all-gather), + # so it consumes the sharded param generator instead of the full-tensor one. + if effective_mode == "delta_sharded": + per_tensor_param, _ = self.actor.engine.get_per_tensor_param_shard() + else: + per_tensor_param, _ = self.actor.engine.get_per_tensor_param() + metrics = await self.checkpoint_engine.send_weights(per_tensor_param, global_steps=global_steps) + return metrics or {} + + set_expandable_segments(False) + log_gpu_memory_usage("Before resume weights", logger=logger) + + # 1. resume rollout memory (weights were released during sleep) + if self.config.rollout.free_cache_engine: + await self.rollout.resume(tags=["weights"]) + log_gpu_memory_usage("After resume weights", logger=logger) + + # 2. determine if we need a base weight sync (adapter path only) + per_tensor_param, peft_config = self.actor.engine.get_per_tensor_param( + layered_summon=self.layered_summon, base_sync_done=True + ) + + do_lora_base_sync = False + if not self.peft_merge and peft_config is not None: + self.rollout.sleep_level = 1 + do_lora_base_sync = not self.base_sync_done + + # 3. sync weights: For SGLang, we need base first (when needed), then adapter/merged + if do_lora_base_sync: + per_tensor_param_base, peft_config = self.actor.engine.get_per_tensor_param( + layered_summon=self.layered_summon, base_sync_done=False + ) + await self.rollout.update_weights( + per_tensor_param_base, peft_config=peft_config, base_sync_done=False, global_steps=global_steps + ) + + await self.rollout.update_weights( + per_tensor_param, peft_config=peft_config, base_sync_done=True, global_steps=global_steps + ) + + log_gpu_memory_usage("After update_weights", logger=logger) + + # 3. offload model to cpu + if self.actor.engine.is_param_offload_enabled: + self.actor.engine.to("cpu", model=True, optimizer=False, grad=False) + aggressive_empty_cache(force_sync=True) + + # 4. resume kv_cache + if self.config.rollout.free_cache_engine: + await self.rollout.resume(tags=["kv_cache"]) + log_gpu_memory_usage("After resume kv_cache", logger=logger) + + self.base_sync_done = True + set_expandable_segments(True) + + @register(dispatch_mode=Dispatch.DP_COMPUTE, blocking=False) + def execute_checkpoint_engine(self, method: str, *args, **kwargs): + """Execute checkpoint engine method. + + Args: + method (str): Checkpoint engine method name. + *args: Variable length argument list. + **kwargs: Arbitrary keyword arguments. + + """ + return getattr(self.checkpoint_engine, method)(*args, **kwargs) diff --git a/verl_0720_main/verl/verl/workers/engine_workers_tinker.py b/verl_0720_main/verl/verl/workers/engine_workers_tinker.py new file mode 100644 index 0000000000000000000000000000000000000000..7414589d229a56271b923e8bdd76cf2401f68b02 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/engine_workers_tinker.py @@ -0,0 +1,204 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from typing import TypedDict + +from codetiming import Timer +from tensordict import TensorDict + +from verl.single_controller.base.decorator import Dispatch, make_nd_compute_dataproto_dispatch_fn, register +from verl.utils import tensordict_utils as tu +from verl.utils.profiler import DistProfiler +from verl.utils.tensordict_utils import maybe_fix_3d_position_ids +from verl.workers.engine_workers import ActorRolloutRefWorker, TrainingWorker + + +class OptimStepParams(TypedDict, total=False): + """Runtime param-group override for Tinker-style optimizer steps. + + This payload is only consumed by ``TinkerTrainingWorker.optimizer_step``. It is + not part of the generic engine API, does not participate in optimizer + construction, and does not configure LR scheduler behavior. Tinker callers + that own scheduling should pass the resulting optimizer values here. + + The current implementation applies each provided key to all optimizer param + groups before one explicit optimizer step. For VeOmni MultiOptimizer, the + Tinker worker flattens the wrapped optimizers' param groups and applies the + same global override. Optimizer-specific or group-specific overrides require a + different payload shape. + """ + + lr: float + eps: float + betas: tuple[float, float] + weight_decay: float + + +def _iter_optimizer_param_groups(optimizer): + """Return a flat list of param groups, including VeOmni MultiOptimizer children.""" + if getattr(optimizer, "_is_multi_optimizer", False): + optimizers = optimizer.optimizers_dict.values() + else: + optimizers = [optimizer] + + param_groups = [] + for opt in optimizers: + opt_param_groups = getattr(opt, "param_groups", None) + if opt_param_groups is None: + raise NotImplementedError( + f"{type(opt).__name__} does not expose param_groups for per-step optimizer params" + ) + param_groups.extend(opt_param_groups) + return param_groups + + +def _apply_optim_step_params(optimizer, optim_step_params: OptimStepParams | None) -> None: + """Apply a Tinker step-time override to every optimizer param group. + + The override is intentionally global: every provided key must exist with the + same value type on all param groups. This keeps mixed optimizers such as + VeOmni Muon+AdamW fail-fast for optimizer-specific keys like ``betas`` while + still allowing shared keys such as ``lr``. + """ + if optim_step_params is None: + return + + if hasattr(optim_step_params, "to_dict"): + optim_step_params = optim_step_params.to_dict() + if not isinstance(optim_step_params, dict): + raise TypeError(f"optim_step_params must be a dict, got {type(optim_step_params)}") + + normalized_params = {key: value for key, value in optim_step_params.items() if value is not None} + if not normalized_params: + return + + param_groups = _iter_optimizer_param_groups(optimizer) + if not param_groups: + raise ValueError(f"{type(optimizer).__name__} does not have param_groups") + + for key, value in normalized_params.items(): + if key not in param_groups[0]: + raise ValueError(f"{type(optimizer).__name__} does not support optim_step_params key: {key!r}") + + expected_type = type(param_groups[0][key]) + if not isinstance(value, expected_type): + raise TypeError( + f"optim_step_params type mismatch for {type(optimizer).__name__}: " + f"{key!r} got {type(value).__name__}, expected {expected_type.__name__}" + ) + + for param_group in param_groups: + if key not in param_group: + raise ValueError(f"{type(optimizer).__name__} has inconsistent param_group key: {key!r}") + if not isinstance(param_group[key], expected_type): + raise TypeError(f"{type(optimizer).__name__} has inconsistent param_group type for {key!r}") + + for param_group in param_groups: + param_group.update(normalized_params) + + +class TinkerTrainingWorker(TrainingWorker): + """ + Training worker exposing Tinker-style split training primitives. + + Unlike TrainingWorker.train_batch(), these APIs let a caller explicitly separate gradient + clearing, forward/backward, and optimizer stepping. + """ + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def optimizer_zero_grad(self) -> None: + with self.engine.train_mode(zero_grad_on_exit=False): + self.engine.optimizer_zero_grad() + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="train"), blocking=False) + @DistProfiler.annotate(color="red", role="forward_backward") + def forward_backward(self, data: TensorDict) -> TensorDict: + assert self.loss_fn is not None, "loss function can't be None when calling forward_backward" + assert not self.engine_config.forward_only, ( + "Can't run `forward_backward` when forward_only is in the engine config." + ) + global_token_num = tu.get(data, key="global_token_num") + disable_auto_offload = tu.get(data, key="disable_auto_offload", default=False) + images_seqlens = tu.get(data, key="images_seqlens", default=None) + + default_keys = dict( + use_remove_padding=self.model_config.get("use_remove_padding", False), + use_dynamic_bsz=self.engine_config.use_dynamic_bsz, + max_token_len_per_gpu=self.engine_config.max_token_len_per_gpu, + micro_batch_size_per_gpu=self.engine_config.micro_batch_size_per_gpu, + use_fused_kernels=self.engine_config.use_fused_kernels, + ) + + for key, val in default_keys.items(): + if key not in data.keys(): + tu.assign_non_tensor(data, **{key: val}) + + maybe_fix_3d_position_ids(data) + + with ( + self.engine.train_mode( + disable_auto_offload=disable_auto_offload, + zero_grad_on_exit=False, + ), + Timer(name="forward_backward", logger=None) as timer, + ): + output = self.engine.forward_backward_batch(data, loss_function=self.loss_fn, forward_only=False) + delta_time = timer.last + + if self.engine.is_mp_src_rank_with_outputs(): + output.pop("model_output") + final_output = self._postprocess_output( + output, + global_token_num=global_token_num, + delta_time=delta_time, + forward_only=False, + images_seqlens=images_seqlens, + ).cpu() + else: + final_output = None + + return final_output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def optimizer_step(self, optim_step_params: OptimStepParams | None = None) -> dict: + """Run one Tinker optimizer step with an optional global param-group override.""" + with self.engine.train_mode(zero_grad_on_exit=True): + _apply_optim_step_params(self.engine.optimizer, optim_step_params) + grad_norm = self.engine.optimizer_step() + + metrics = {} + if grad_norm is not None and self.engine.is_mp_src_rank_with_outputs(): + metrics["grad_norm"] = grad_norm + return metrics + + +class TinkerActorRolloutRefWorker(ActorRolloutRefWorker): + """Actor-rollout-ref worker exposing Tinker-style split training primitives for the actor.""" + + actor_worker_cls = TinkerTrainingWorker + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def optimizer_zero_grad(self) -> None: + assert "actor" in self.role, "optimizer_zero_grad only support actor role" + return self.actor.optimizer_zero_grad() + + @register(dispatch_mode=make_nd_compute_dataproto_dispatch_fn(mesh_name="actor"), blocking=False) + def forward_backward(self, data: TensorDict) -> TensorDict: + assert "actor" in self.role, "forward_backward only support actor role" + output = self.actor.forward_backward(data=data) + return output.cpu() if output is not None else None + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def optimizer_step(self, optim_step_params: OptimStepParams | None = None) -> dict: + assert "actor" in self.role, "optimizer_step only support actor role" + return self.actor.optimizer_step(optim_step_params=optim_step_params) diff --git a/verl_0720_main/verl/verl/workers/reward_manager/__init__.py b/verl_0720_main/verl/verl/workers/reward_manager/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..06b693697f0383ff9bc1ace9a24a914b0fa1096c --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/__init__.py @@ -0,0 +1,37 @@ +# Copyright 2024 PRIME team and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .registry import get_reward_manager_cls, register # noqa: I001 +from .batch import BatchRewardManager +from .dapo import DAPORewardManager +from .naive import NaiveRewardManager +from .prime import PrimeRewardManager + +# Note(haibin.lin): no need to include all reward managers here in case of complicated dependencies +__all__ = [ + "BatchRewardManager", + "DAPORewardManager", + "NaiveRewardManager", + "PrimeRewardManager", + "register", + "get_reward_manager_cls", +] + +# Import experimental reward managers to ensure they are registered +try: + from verl.experimental.reward_loop.reward_manager.limited import RateLimitedRewardManager # noqa: F401 + + __all__.append("RateLimitedRewardManager") +except ImportError: + pass # Optional dependency, may not be available diff --git a/verl_0720_main/verl/verl/workers/reward_manager/abstract.py b/verl_0720_main/verl/verl/workers/reward_manager/abstract.py new file mode 100644 index 0000000000000000000000000000000000000000..494c87c5597e1e427712410e72bd040fd50204cf --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/abstract.py @@ -0,0 +1,72 @@ +# Copyright 2023-2025 SGLang Team +# Copyright Amazon.com, Inc. or its affiliates. +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from abc import ABC, abstractmethod +from typing import Any, Callable + +import torch + +from verl.protocol import DataProto + +RawRewardFn = Callable[..., Any] + + +class AbstractRewardManager(ABC): + @abstractmethod + def __init__( + self, + tokenizer: Any, + num_examine: int, + compute_score: RawRewardFn | None, + reward_fn_key: str = "data_source", + **kwargs: Any, + ): + pass + + @abstractmethod + def __call__( + self, + data: DataProto, + return_dict: bool = False, + ) -> torch.Tensor | dict[str, Any]: + pass + + def _extract_reward_from_rm_scores( + self, data: DataProto, return_dict: bool = False + ) -> torch.Tensor | dict[str, Any] | None: + """ + Extract reward from already-computed rm_scores if available. + This has been deprecated. + + Args: + data: DataProto object containing the batch data + return_dict: Whether to return a dictionary with reward_tensor and reward_extra_info + + Returns: + If rm_scores exists: + - If return_dict=True: dict with "reward_tensor" and "reward_extra_info" + - If return_dict=False: torch.Tensor of rm_scores + If rm_scores doesn't exist: None + """ + if "rm_scores" not in data.batch.keys(): + return None + + if return_dict: + reward_extra_keys = data.meta_info.get("reward_extra_keys", []) + reward_extra_info = {key: data.non_tensor_batch[key] for key in reward_extra_keys} + return {"reward_tensor": data.batch["rm_scores"], "reward_extra_info": reward_extra_info} + else: + return data.batch["rm_scores"] diff --git a/verl_0720_main/verl/verl/workers/reward_manager/batch.py b/verl_0720_main/verl/verl/workers/reward_manager/batch.py new file mode 100644 index 0000000000000000000000000000000000000000..078e301d45f93c4e4b1092245bd6c9c55476b8e0 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/batch.py @@ -0,0 +1,128 @@ +# Copyright 2025 Individual Contributor: Mert Unsal +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from collections import defaultdict +from typing import Any + +import torch + +from verl import DataProto +from verl.workers.reward_manager import register +from verl.workers.reward_manager.abstract import AbstractRewardManager, RawRewardFn + + +@register("batch") +class BatchRewardManager(AbstractRewardManager): + """ + A batch reward manager that computes rewards for a batch of data. + + Args: + tokenizer (Tokenizer): The tokenizer to use for decoding the responses. + num_examine (int): The number of responses to examine. + compute_score (callable): The function to compute the rewards. + reward_fn_key (str): The key to use for the reward function. + reward_kwargs (dict): The keyword arguments to pass to the reward function. + """ + + def __init__( + self, tokenizer, num_examine, compute_score: RawRewardFn, reward_fn_key="data_source", **reward_kwargs + ): + self.tokenizer = tokenizer + self.num_examine = num_examine + self.compute_score = compute_score + self.reward_fn_key = reward_fn_key + self.reward_kwargs = reward_kwargs + + def verify(self, data): + prompt_ids = data.batch["prompts"] + response_ids = data.batch["responses"] + attention_mask = data.batch["attention_mask"] + + prompt_len = prompt_ids.shape[-1] + valid_response_lengths = attention_mask[:, prompt_len:].sum(dim=-1) + + responses_str = [] + for i in range(len(data)): + valid_len = valid_response_lengths[i] + valid_response_ids = response_ids[i][:valid_len] + response_str = self.tokenizer.decode(valid_response_ids, skip_special_tokens=True) + responses_str.append(response_str) + + ground_truths = [item.non_tensor_batch["reward_model"].get("ground_truth", None) for item in data] + data_sources = data.non_tensor_batch[self.reward_fn_key] + rollout_reward_scores = data.non_tensor_batch.get("reward_scores", [{} for _ in range(len(data))]) + extras = data.non_tensor_batch.get("extra_info", [{} for _ in range(len(data))]) + + for i in range(len(data)): + extras[i]["rollout_reward_scores"] = rollout_reward_scores[i] + + scores = self.compute_score( + data_sources=data_sources, + solution_strs=responses_str, + ground_truths=ground_truths, + extra_infos=extras, + **self.reward_kwargs, + ) + + return scores + + def __call__(self, data: DataProto, return_dict: bool = False) -> torch.Tensor | dict[str, Any]: + # If there is rm score, we directly return rm score. Otherwise, we compute via rm_score_fn + reward_from_rm_scores = self._extract_reward_from_rm_scores(data, return_dict) + if reward_from_rm_scores is not None: + return reward_from_rm_scores + + reward_tensor = torch.zeros_like(data.batch["responses"], dtype=torch.float32) + reward_extra_info = defaultdict(list) + prompt_ids = data.batch["prompts"] + prompt_len = prompt_ids.shape[-1] + attention_mask = data.batch["attention_mask"] + valid_response_lengths = attention_mask[:, prompt_len:].sum(dim=-1) + data_sources = data.non_tensor_batch[self.reward_fn_key] + + scores = self.verify(data) + rewards = [] + already_printed: dict[str, Any] = {} + + for i in range(len(data)): + length = valid_response_lengths[i].item() + score = scores[i] + + if isinstance(score, dict): + reward = score["score"] + for key, value in score.items(): + reward_extra_info[key].append(value) + else: + reward = score + + rewards.append(reward) + reward_tensor[i, length - 1] = reward + + data_source = data_sources[i] + if already_printed.get(data_source, 0) < self.num_examine: + response_str = self.tokenizer.decode(data.batch["responses"][i][:length], skip_special_tokens=True) + prompt_str = self.tokenizer.decode(data.batch["prompts"][i], skip_special_tokens=True) + ground_truth = data[i].non_tensor_batch["reward_model"].get("ground_truth", None) + print("[prompt]", prompt_str) + print("[response]", response_str) + print("[ground_truth]", ground_truth) + print("[score]", scores[i]) + already_printed[data_source] = already_printed.get(data_source, 0) + 1 + + data.batch["acc"] = torch.tensor(rewards, dtype=torch.float32, device=prompt_ids.device) + + if return_dict: + return {"reward_tensor": reward_tensor, "reward_extra_info": reward_extra_info} + else: + return reward_tensor diff --git a/verl_0720_main/verl/verl/workers/reward_manager/dapo.py b/verl_0720_main/verl/verl/workers/reward_manager/dapo.py new file mode 100644 index 0000000000000000000000000000000000000000..7d1ef219436f0e90e2223bda90887990655b31ea --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/dapo.py @@ -0,0 +1,154 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from collections import defaultdict + +import torch + +from verl import DataProto +from verl.utils.reward_score import default_compute_score +from verl.workers.reward_manager import register +from verl.workers.reward_manager.abstract import AbstractRewardManager + + +@register("dapo") +class DAPORewardManager(AbstractRewardManager): + """The reward manager.""" + + def __init__( + self, + tokenizer, + num_examine, + compute_score=None, + reward_fn_key="data_source", + max_resp_len=None, + overlong_buffer_cfg=None, + ) -> None: + self.tokenizer = tokenizer + self.num_examine = num_examine # the number of batches of decoded responses to print to the console + self.compute_score = compute_score or default_compute_score + self.reward_fn_key = reward_fn_key + self.overlong_buffer_cfg = overlong_buffer_cfg + self.max_resp_len = max_resp_len + + if self.overlong_buffer_cfg is not None and self.overlong_buffer_cfg.enable: + assert self.max_resp_len is not None, ( + f"max_resp_len must be provided if {overlong_buffer_cfg=}, but got None" + ) + assert self.max_resp_len >= self.overlong_buffer_cfg.len, ( + "max_resp_len must be larger than overlong_buffer.len" + ) + assert self.overlong_buffer_cfg.len > 0, ( + "overlong_buffer.len must be positive when overlong penalty is enabled," + f"but got {self.overlong_buffer_cfg.len}." + "To disable the overlong penalty, set overlong_buffer.enable = False" + ) + + def __call__(self, data: DataProto, return_dict: bool = False): + """We will expand this function gradually based on the available datasets""" + + # If there is rm score, we directly return rm score. Otherwise, we compute via rm_score_fn + reward_from_rm_scores = self._extract_reward_from_rm_scores(data, return_dict) + if reward_from_rm_scores is not None: + return reward_from_rm_scores + + reward_tensor = torch.zeros_like(data.batch["responses"], dtype=torch.float32) + reward_extra_info = defaultdict(list) + + already_print_data_sources = {} + + for i in range(len(data)): + data_item = data[i] # DataProtoItem + + prompt_ids = data_item.batch["prompts"] + + prompt_length = prompt_ids.shape[-1] + + valid_prompt_length = data_item.batch["attention_mask"][:prompt_length].sum() + valid_prompt_ids = prompt_ids[-valid_prompt_length:] + + response_ids = data_item.batch["responses"] + valid_response_length = data_item.batch["attention_mask"][prompt_length:].sum() + valid_response_ids = response_ids[:valid_response_length] + + # decode + prompt_str = self.tokenizer.decode(valid_prompt_ids, skip_special_tokens=True) + response_str = self.tokenizer.decode(valid_response_ids, skip_special_tokens=True) + eos_token = self.tokenizer.eos_token + if response_str.endswith(eos_token): + response_str = response_str[: -len(eos_token)] + + ground_truth = data_item.non_tensor_batch["reward_model"]["ground_truth"] + + data_source = data_item.non_tensor_batch[self.reward_fn_key] + + extra_info = data_item.non_tensor_batch.get("extra_info", {}) + + rollout_reward_scores = data_item.non_tensor_batch.get("reward_scores", {}) + + extra_info["rollout_reward_scores"] = rollout_reward_scores + + result = self.compute_score( + data_source=data_source, + solution_str=response_str, + ground_truth=ground_truth, + extra_info=extra_info, + ) + + score: float + if isinstance(result, dict): + score = result["score"] + # Store the information including original reward + for key, value in result.items(): + reward_extra_info[key].append(value) + else: + score = result + reward_extra_info["acc"].append(score) + + reward = score + + if self.overlong_buffer_cfg is not None and self.overlong_buffer_cfg.enable: + overlong_buffer_len = self.overlong_buffer_cfg.len + expected_len = self.max_resp_len - overlong_buffer_len + exceed_len = valid_response_length - expected_len + overlong_penalty_factor = self.overlong_buffer_cfg.penalty_factor + overlong_reward = min(-exceed_len / overlong_buffer_len * overlong_penalty_factor, 0) + reward += overlong_reward + if self.overlong_buffer_cfg.log: + reward_extra_info["overlong_reward"].append(overlong_reward) + reward_extra_info["overlong"].append(overlong_reward < 0) + + reward_tensor[i, valid_response_length - 1] = reward + + if data_source not in already_print_data_sources: + already_print_data_sources[data_source] = 0 + + if already_print_data_sources[data_source] < self.num_examine: + already_print_data_sources[data_source] += 1 + print("[prompt]", prompt_str) + print("[response]", response_str) + print("[ground_truth]", ground_truth) + if isinstance(result, dict): + for key, value in result.items(): + print(f"[{key}]", value) + else: + print("[score]", score) + + if return_dict: + return { + "reward_tensor": reward_tensor, + "reward_extra_info": reward_extra_info, + } + else: + return reward_tensor diff --git a/verl_0720_main/verl/verl/workers/reward_manager/naive.py b/verl_0720_main/verl/verl/workers/reward_manager/naive.py new file mode 100644 index 0000000000000000000000000000000000000000..a00de0dd302541ecaf0fa1bc32ea41b7d9e36760 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/naive.py @@ -0,0 +1,175 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import signal +from collections import defaultdict +from contextlib import contextmanager +from typing import Any, Optional + +import torch + +from verl import DataProto +from verl.utils.reward_score import default_compute_score +from verl.workers.reward_manager import register +from verl.workers.reward_manager.abstract import AbstractRewardManager + + +@contextmanager +def _score_timeout(seconds: Optional[float]): + """Raise ``TimeoutError`` if the wrapped block runs longer than ``seconds``. + + A single ``compute_score`` call on a pathological model output (e.g. a long, + degenerate RL rollout that triggers catastrophic regex backtracking or a slow + symbolic comparison) can run for a very long time. Because ``NaiveRewardManager`` + scores samples serially in the driver process, one such call blocks the entire + training loop. This guard bounds the wall-clock time of each call. + + Implemented with ``SIGALRM``, so it only takes effect on the main thread. If a + handler cannot be installed (e.g. called from a non-main thread), it degrades to + a no-op so reward computation still runs -- just without the timeout. + """ + if not seconds or seconds <= 0: + yield + return + + def _handler(signum, frame): + raise TimeoutError(f"compute_score timed out after {seconds}s") + + try: + old_handler = signal.signal(signal.SIGALRM, _handler) + except ValueError: + # Not in the main thread -> SIGALRM is unavailable; run without a timeout. + yield + return + + signal.setitimer(signal.ITIMER_REAL, seconds) + try: + yield + finally: + signal.setitimer(signal.ITIMER_REAL, 0) + signal.signal(signal.SIGALRM, old_handler) + + +@register("naive") +class NaiveRewardManager(AbstractRewardManager): + """The reward manager.""" + + def __init__( + self, tokenizer, num_examine, compute_score=None, reward_fn_key="data_source", compute_score_timeout=None + ) -> None: + """ + Initialize the NaiveRewardManager instance. + + Args: + tokenizer: The tokenizer used to decode token IDs into text. + num_examine: The number of batches of decoded responses to print to the console for debugging purpose. + compute_score: A function to compute the reward score. If None, `default_compute_score` will be used. + reward_fn_key: The key used to access the data source in the non-tensor batch data. Defaults to + "data_source". + compute_score_timeout: Optional per-sample timeout (in seconds) for `compute_score`. If set, a single + scoring call that exceeds this limit is aborted, assigned a reward of 0.0, and training continues, + instead of blocking the whole loop. Defaults to None (no timeout, unchanged behavior). + """ + self.tokenizer = tokenizer # Store the tokenizer for decoding token IDs + self.num_examine = num_examine # the number of batches of decoded responses to print to the console + self.compute_score = compute_score or default_compute_score + self.reward_fn_key = reward_fn_key # Store the key for accessing the data source + self.compute_score_timeout = compute_score_timeout # per-sample timeout (seconds) for compute_score + + def __call__(self, data: DataProto, return_dict: bool = False) -> torch.Tensor | dict[str, Any]: + """We will expand this function gradually based on the available datasets""" + + # If there is rm score, we directly return rm score. Otherwise, we compute via rm_score_fn + reward_from_rm_scores = self._extract_reward_from_rm_scores(data, return_dict) + if reward_from_rm_scores is not None: + return reward_from_rm_scores + + reward_tensor = torch.zeros_like(data.batch["responses"], dtype=torch.float32) + reward_extra_info = defaultdict(list) + + already_print_data_sources = {} + + for i in range(len(data)): + data_item = data[i] # DataProtoItem + + prompt_ids = data_item.batch["prompts"] + + prompt_length = prompt_ids.shape[-1] + + valid_prompt_length = data_item.batch["attention_mask"][:prompt_length].sum() + valid_prompt_ids = prompt_ids[-valid_prompt_length:] + + response_ids = data_item.batch["responses"] + valid_response_length = data_item.batch["attention_mask"][prompt_length:].sum() + valid_response_ids = response_ids[:valid_response_length] + + # decode + prompt_str = self.tokenizer.decode(valid_prompt_ids, skip_special_tokens=True) + response_str = self.tokenizer.decode(valid_response_ids, skip_special_tokens=True) + + ground_truth = data_item.non_tensor_batch["reward_model"]["ground_truth"] + data_source = data_item.non_tensor_batch[self.reward_fn_key] + extra_info = data_item.non_tensor_batch.get("extra_info", {}) + num_turns = data_item.non_tensor_batch.get("__num_turns__", None) + rollout_reward_scores = data_item.non_tensor_batch.get("reward_scores", {}) + extra_info["num_turns"] = num_turns + extra_info["rollout_reward_scores"] = rollout_reward_scores + + try: + with _score_timeout(self.compute_score_timeout): + score = self.compute_score( + data_source=data_source, + solution_str=response_str, + ground_truth=ground_truth, + extra_info=extra_info, + ) + except TimeoutError: + print( + f"[NaiveRewardManager] compute_score exceeded " + f"{self.compute_score_timeout}s for data_source={data_source}; " + f"assigning reward 0.0 and continuing." + ) + score = 0.0 + + if isinstance(score, dict): + reward = score["score"] + # Store the information including original reward + for key, value in score.items(): + reward_extra_info[key].append(value) + else: + reward = score + + reward_tensor[i, valid_response_length - 1] = reward + + if data_source not in already_print_data_sources: + already_print_data_sources[data_source] = 0 + + if already_print_data_sources[data_source] < self.num_examine: + already_print_data_sources[data_source] += 1 + print("[prompt]", prompt_str) + print("[response]", response_str) + print("[ground_truth]", ground_truth) + if isinstance(score, dict): + for key, value in score.items(): + print(f"[{key}]", value) + else: + print("[score]", score) + + if return_dict: + return { + "reward_tensor": reward_tensor, + "reward_extra_info": reward_extra_info, + } + else: + return reward_tensor diff --git a/verl_0720_main/verl/verl/workers/reward_manager/prime.py b/verl_0720_main/verl/verl/workers/reward_manager/prime.py new file mode 100644 index 0000000000000000000000000000000000000000..b15ed7c3fcb3931e8860b6fe32898ebb2cc5386c --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/prime.py @@ -0,0 +1,189 @@ +# Copyright 2024 PRIME team and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import asyncio +from concurrent.futures import ProcessPoolExecutor +from functools import partial +from typing import Any, Callable, Optional + +import psutil +import torch +from transformers import PreTrainedTokenizer + +from verl import DataProto +from verl.utils.ray_utils import get_event_loop +from verl.utils.reward_score import default_compute_score +from verl.workers.reward_manager import register +from verl.workers.reward_manager.abstract import AbstractRewardManager + + +async def single_compute_score(evaluation_func, completion, reference, task, task_extra_info, executor, timeout=300.0): + loop = get_event_loop() + try: + # Ensure process_completion is called properly + future = loop.run_in_executor(executor, partial(evaluation_func, task, completion, reference, task_extra_info)) + return await asyncio.wait_for(future, timeout=timeout) + except asyncio.TimeoutError: + print(f"[Timeout] Task timeout: {completion}") + return None # Default value for timed-out rows + except Exception as e: + print(f"[Error] Task failed: {e}, completion: {completion[:80]}") + return None # Default value for failed rows + + +async def parallel_compute_score_async( + evaluation_func, completions, references, tasks, extra_info=None, num_processes=64 +): + if extra_info is None: + extra_info = [None] * len(tasks) + scores = [] + with ProcessPoolExecutor(max_workers=num_processes) as executor: + # to prevent very occasional starvation caused by some anomalous programs ( like infinite loop ), the + # exceptions in async programs will instantly halt the evaluation, and all summoned processes will be killed. + try: + # Create tasks for all rows + tasks_async = [ + single_compute_score(evaluation_func, c, r, t, ei, executor, timeout=300.0) + for c, r, t, ei in zip(completions, references, tasks, extra_info, strict=True) + ] + results = await asyncio.gather(*tasks_async, return_exceptions=False) + except Exception as e: + print(f"[Exception] async gather failed: {e}") + raise + finally: + terminated_count = 0 + for pid, proc in executor._processes.items(): + try: + p = psutil.Process(pid) + p.terminate() + try: + p.wait(timeout=5) + except psutil.TimeoutExpired: + p.kill() + terminated_count += 1 + except Exception: + pass + print(f"[Shutdown] {terminated_count} subprocess(es) terminated.") + + # Process results + for result, completion, reference, task in zip(results, completions, references, tasks, strict=True): + if isinstance(result, Exception) or result is None: + # Handle failed or timed-out tasks + scores.append(0.0) + elif isinstance(result, int | float | bool): + scores.append(float(result)) + else: + scores.append(float(result[0])) + return scores + + +def run_reward_scoring(evaluation_func, completions, references, tasks, extra_info=None, num_processes=64): + loop = asyncio.new_event_loop() + asyncio.set_event_loop(loop) + try: + return loop.run_until_complete( + parallel_compute_score_async(evaluation_func, completions, references, tasks, extra_info, num_processes) + ) + finally: + loop.close() + + +@register("prime") +class PrimeRewardManager(AbstractRewardManager): + """ + The Reward Manager used in https://github.com/PRIME-RL/PRIME + """ + + def __init__( + self, + tokenizer: PreTrainedTokenizer, + num_examine: int, + compute_score: Optional[Callable] = None, + reward_fn_key: str = "data_source", + ) -> None: + self.tokenizer = tokenizer + self.num_examine = num_examine # the number of batches of decoded responses to print to the console + self.compute_score = compute_score or default_compute_score + self.reward_fn_key = reward_fn_key + + def verify(self, data): + """ + verify the batch and save as ``acc`` tensor + """ + # batched scoring + prompt_ids = data.batch["prompts"] + + response_ids = data.batch["responses"] + sequences_str = self.tokenizer.batch_decode(response_ids, skip_special_tokens=True) + ground_truth = [data_item.non_tensor_batch["reward_model"]["ground_truth"] for data_item in data] + data_sources = data.non_tensor_batch[self.reward_fn_key] + extra_info = data.non_tensor_batch.get("extra_info", None) + + assert len(sequences_str) == len(ground_truth) == len(data_sources) + try: + scores = run_reward_scoring( + self.compute_score, + completions=sequences_str, + references=ground_truth, + tasks=data_sources, + extra_info=extra_info, + num_processes=64, + ) + except asyncio.TimeoutError: + print("[Timeout] Global reward scoring timed out. Setting all as 0.") + scores = [0.0 for _ in range(len(sequences_str))] + except Exception as e: + print(f"[Error] Unexpected error during scoring. Setting all as 0. {e}") + scores = [0.0 for _ in range(len(sequences_str))] + data.batch["acc"] = torch.tensor(scores, dtype=torch.float32, device=prompt_ids.device) + return scores + + def __call__(self, data: DataProto, return_dict: bool = False) -> torch.Tensor | dict[str, Any]: + """We will expand this function gradually based on the available datasets""" + + # If there is rm score, we directly return rm score. Otherwise, we compute via rm_score_fn + reward_from_rm_scores = self._extract_reward_from_rm_scores(data, return_dict) + if reward_from_rm_scores is not None: + return reward_from_rm_scores + + reward_tensor = torch.zeros_like(data.batch["responses"], dtype=torch.float32) + + already_print_data_sources = {} + + # batched scoring + prompt_ids = data.batch["prompts"] + prompt_length = prompt_ids.shape[-1] + + response_ids = data.batch["responses"] + valid_response_length = data.batch["attention_mask"][:, prompt_length:].sum(dim=-1) + sequences_str = self.tokenizer.batch_decode(response_ids, skip_special_tokens=True) + data_sources = data.non_tensor_batch["data_source"] + + scores = self.verify(data) + + for i in range(len(data)): + data_source = data_sources[i] + reward_tensor[i, valid_response_length[i].item() - 1] = scores[i] + + if data_source not in already_print_data_sources: + already_print_data_sources[data_source] = 0 + + if already_print_data_sources[data_source] < self.num_examine: + already_print_data_sources[data_source] += 1 + print(sequences_str) + + if return_dict: + return {"reward_tensor": reward_tensor} + else: + return reward_tensor diff --git a/verl_0720_main/verl/verl/workers/reward_manager/registry.py b/verl_0720_main/verl/verl/workers/reward_manager/registry.py new file mode 100644 index 0000000000000000000000000000000000000000..4e255d8ac8cdc9467f33ba4a63c2f5ff27f44d33 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/reward_manager/registry.py @@ -0,0 +1,55 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from typing import Callable + +from verl.workers.reward_manager.abstract import AbstractRewardManager + +__all__ = ["register", "get_reward_manager_cls"] + +REWARD_MANAGER_REGISTRY: dict[str, type[AbstractRewardManager]] = {} + + +def register(name: str) -> Callable[[type[AbstractRewardManager]], type[AbstractRewardManager]]: + """Decorator to register a reward manager class with a given name. + + Args: + name: `(str)` + The name of the reward manager. + """ + + def decorator(cls: type[AbstractRewardManager]) -> type[AbstractRewardManager]: + if name in REWARD_MANAGER_REGISTRY and REWARD_MANAGER_REGISTRY[name] != cls: + raise ValueError( + f"Reward manager {name} has already been registered: {REWARD_MANAGER_REGISTRY[name]} vs {cls}" + ) + REWARD_MANAGER_REGISTRY[name] = cls + return cls + + return decorator + + +def get_reward_manager_cls(name: str) -> type[AbstractRewardManager]: + """Get the reward manager class with a given name. + + Args: + name: `(str)` + The name of the reward manager. + + Returns: + `(type)`: The reward manager class. + """ + if name not in REWARD_MANAGER_REGISTRY: + raise ValueError(f"Unknown reward manager: {name}") + return REWARD_MANAGER_REGISTRY[name] diff --git a/verl_0720_main/verl/verl/workers/rollout/__init__.py b/verl_0720_main/verl/verl/workers/rollout/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..f6bd6c28b770fd5996bd23936796ef374ccb8ec1 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/__init__.py @@ -0,0 +1,20 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .base import BaseRollout, get_rollout_class +from .hf_rollout import HFRollout +from .naive import NaiveRollout +from .replica import RolloutReplica + +__all__ = ["BaseRollout", "NaiveRollout", "HFRollout", "get_rollout_class", "RolloutReplica"] diff --git a/verl_0720_main/verl/verl/workers/rollout/__pycache__/llm_server.cpython-310.pyc b/verl_0720_main/verl/verl/workers/rollout/__pycache__/llm_server.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..e8e93191bbe8f1894428ed043a86861b7810738b Binary files /dev/null and b/verl_0720_main/verl/verl/workers/rollout/__pycache__/llm_server.cpython-310.pyc differ diff --git a/verl_0720_main/verl/verl/workers/rollout/base.py b/verl_0720_main/verl/verl/workers/rollout/base.py new file mode 100644 index 0000000000000000000000000000000000000000..5375a1b5f2a77e17f8a4b4165457513b92d39b5b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/base.py @@ -0,0 +1,109 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import importlib +from abc import ABC, abstractmethod +from typing import Generator + +import torch +from torch.distributed.device_mesh import DeviceMesh + +from verl import DataProto +from verl.utils.config import omega_conf_to_dataclass +from verl.workers.config import HFModelConfig, RolloutConfig + +__all__ = ["BaseRollout"] + + +class BaseRollout(ABC): + """Base class for rollout.""" + + def __init__( + self, + config: RolloutConfig, + model_config: HFModelConfig, + device_mesh: DeviceMesh, + *args, + **kwargs, + ): + self.config = omega_conf_to_dataclass(config) + self.model_config: HFModelConfig = omega_conf_to_dataclass(model_config) + self.device_mesh = device_mesh + + @abstractmethod + async def resume(self, tags: list[str]): + """Resume rollout weights or kv cache in GPU memory. + + Args: + tags: weights or kv_cache. + """ + pass + + @abstractmethod + async def update_weights( + self, + weights: Generator[tuple[str, torch.Tensor], None, None], + wire_format: str = "named_tensors", + **kwargs, + ): + """Update the weights of the rollout model. + + Args: + weights: A generator that yields the name of the weight tensor and the tensor itself. + wire_format: How the generator packages weights -- "named_tensors" (full + tensors, the default) or "delta_flush" (per-flush sparse payloads from + the delta checkpoint engine; sglang only). Implementations must consume + this explicitly and never forward it to engine-specific extensions. + """ + pass + + @abstractmethod + async def release(self): + """Release weights and kv cache in GPU memory.""" + pass + + def generate_sequences(self, prompts: DataProto) -> DataProto: + """Batch generate sequences in sync mode. + + Args: + prompts: The input prompts. + + Returns: + The output sequences. + """ + raise NotImplementedError + + +_ROLLOUT_REGISTRY = { + ("vllm", "async"): "verl.workers.rollout.vllm_rollout.ServerAdapter", + ("sglang", "async"): "verl.workers.rollout.sglang_rollout.sglang_rollout.ServerAdapter", + ("trtllm", "async"): "verl.workers.rollout.trtllm_rollout.trtllm_rollout.ServerAdapter", +} + + +def get_rollout_class(rollout_name: str, mode: str = "async") -> type[BaseRollout]: + """Get the rollout class by name. + + Args: + rollout_name: The name of the rollout. + mode: The mode of the rollout, async: server mode. + + Returns: + The rollout class. + """ + assert (rollout_name, mode) in _ROLLOUT_REGISTRY, f"Rollout {rollout_name} with mode {mode} not found" + fqdn = _ROLLOUT_REGISTRY[(rollout_name, mode)] + module_name, class_name = fqdn.rsplit(".", 1) + rollout_module = importlib.import_module(module_name) + return getattr(rollout_module, class_name) diff --git a/verl_0720_main/verl/verl/workers/rollout/hf_rollout.py b/verl_0720_main/verl/verl/workers/rollout/hf_rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..b60f275e2051523aee7805ecab3af19d1ebf6dc5 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/hf_rollout.py @@ -0,0 +1,177 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Rollout with huggingface models. +TODO: refactor this class. Currently, it will hang when using FSDP HybridShard. We should actually create a single +GPU model. Then, get full state_dict and bind the state_dict to the single GPU model. Then, use the single GPU model +to perform generation. +""" + +import contextlib + +import torch +import torch.distributed +from tensordict import TensorDict +from torch import nn +from torch.distributed.fsdp import FullyShardedDataParallel as FSDP +from transformers import GenerationConfig + +from verl import DataProto +from verl.utils.device import get_device_name, get_torch_device +from verl.utils.torch_functional import get_response_mask + +from .base import BaseRollout + +__all__ = ["HFRollout"] + + +class HFRollout(BaseRollout): + def __init__(self, module: nn.Module, config): + super().__init__() + self.config = config + self.module = module + + def generate_sequences(self, prompts: DataProto) -> DataProto: + batch_size = prompts.batch.batch_size[0] + num_chunks = max(batch_size // self.config.get("micro_batch_size", batch_size), 1) + batch_prompts = prompts.chunk(chunks=num_chunks) + output = [self._generate_minibatch(p) for p in batch_prompts] + output = DataProto.concat(output) + return output + + @torch.no_grad() + def _generate_minibatch(self, prompts: DataProto) -> DataProto: + # make sampling args can be overridden by inputs + do_sample = prompts.meta_info.get("do_sample", self.config.do_sample) + is_validate = prompts.meta_info.get("validate", False) + + temperature = prompts.meta_info.get("temperature", self.config.temperature) + response_length = prompts.meta_info.get("response_length", self.config.response_length) + top_p = prompts.meta_info.get("top_p", self.config.get("top_p", 1.0)) + top_k = max(0, prompts.meta_info.get("top_k", self.config.get("top_k", 0))) # to be compatible with vllm + + if not do_sample: + # do_sample==False -> greedy decoding + kwargs = { + "do_sample": False, + "num_beams": 1, + } + elif is_validate: + # do validate and do sample -> use val_kwargs + kwargs = { + "do_sample": True, + "num_beams": 1, + "top_k": max(0, self.config.val_kwargs.top_k), # to be compatible with vllm + "top_p": self.config.val_kwargs.top_p, + "temperature": self.config.val_kwargs.temperature, + "num_return_sequences": 1, # if validate, already repeat in ray_trainer + } + else: + # do_sample -> use rollout config + kwargs = { + "do_sample": True, + "num_beams": 1, + "top_p": top_p, + "top_k": top_k, + "temperature": temperature, + # already repeat in ray_trainer + # https://github.com/verl-project/verl/blob/2fdfbdcba6f2e076f64bc47922d8fe6cf7dc7da5/verl/trainer/ppo/ray_trainer.py#L1117 + "num_return_sequences": 1, + } + + # make config according to generate mode + generation_config = GenerationConfig(**kwargs) + + idx = prompts.batch["input_ids"] # (bs, prompt_length) + prompt_length = idx.size(1) + attention_mask = prompts.batch["attention_mask"] # left-padded attention_mask + position_ids = prompts.batch["position_ids"] + + # used to construct attention_mask + eos_token_id = prompts.meta_info["eos_token_id"] + pad_token_id = prompts.meta_info["pad_token_id"] + + self.module.eval() + param_ctx = contextlib.nullcontext() + + if isinstance(self.module, FSDP): + # recurse need to set to False according to https://github.com/pytorch/pytorch/issues/100069 + param_ctx = FSDP.summon_full_params(self.module, writeback=False, recurse=False) + with param_ctx, torch.autocast(device_type=get_device_name(), dtype=torch.bfloat16): + output = self.module.generate( + input_ids=idx, + attention_mask=attention_mask, + position_ids=position_ids, + do_sample=do_sample, + max_new_tokens=response_length, + eos_token_id=eos_token_id, + pad_token_id=pad_token_id, + generation_config=generation_config, + output_scores=False, # this is potentially very large + return_dict_in_generate=True, + use_cache=True, + ) + + # TODO: filter out the seq with no answers like ds-chat + seq = output.sequences + generated_batch_size = seq.size(0) # bs * num_return_sequences + + # huggingface generate will stop generating when all the batch reaches [EOS]. + # We have to pad to response_length + sequence_length = prompt_length + self.config.response_length + delta_length = sequence_length - seq.shape[1] + + if delta_length > 0: + delta_tokens = torch.ones(size=(generated_batch_size, delta_length), device=seq.device, dtype=seq.dtype) + delta_tokens = pad_token_id * delta_tokens + seq = torch.cat((seq, delta_tokens), dim=1) + assert seq.shape[1] == sequence_length + + # make necessary reputations if num_return_sequences > 1 + num_return_sequences = kwargs.get("num_return_sequences", 1) + if num_return_sequences > 1: + position_ids = position_ids.repeat_interleave(num_return_sequences, dim=0) + attention_mask = attention_mask.repeat_interleave(num_return_sequences, dim=0) + + prompt = seq[:, :prompt_length] # (generated_batch_size, prompt_length) + response = seq[:, prompt_length:] # (generated_batch_size, response_length) + + response_length = response.size(1) + delta_position_id = torch.arange(1, response_length + 1, device=position_ids.device) + delta_position_id = delta_position_id.unsqueeze(0).repeat(generated_batch_size, 1) + + response_position_ids = position_ids[:, -1:] + delta_position_id + position_ids = torch.cat([position_ids, response_position_ids], dim=-1) + + response_attention_mask = get_response_mask( + response_id=response, eos_token=eos_token_id, dtype=attention_mask.dtype + ) + attention_mask = torch.cat((attention_mask, response_attention_mask), dim=-1) + + batch = TensorDict( + { + "prompts": prompt, + "responses": response, + "input_ids": seq, + "attention_mask": attention_mask, + "position_ids": position_ids, + }, + batch_size=generated_batch_size, + ) + + # empty cache before compute old_log_prob + get_torch_device().empty_cache() + + self.module.train() + return DataProto(batch=batch) diff --git a/verl_0720_main/verl/verl/workers/rollout/llm_server.py b/verl_0720_main/verl/verl/workers/rollout/llm_server.py new file mode 100644 index 0000000000000000000000000000000000000000..205bef4f33cd744e737ca8f482ffdf96fbc1ac03 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/llm_server.py @@ -0,0 +1,596 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Utility classes for manage and request LLM servers: +- LLMServerManager: manage life-cycle of LLM servers, including launch, tear-down replicas. +- LLMServerClient: proxy client to request LLM servers, used by AgentLoopWorker. +- GlobalRequestLoadBalancer: global load balancer for LLMServerClient. +""" + +import asyncio +import logging +import os +from typing import Any, Optional +from uuid import uuid4 + +import ray +import torch +from cachetools import LRUCache +from omegaconf import DictConfig + +from verl.single_controller.ray.base import RayResourcePool, RayWorkerGroup +from verl.utils import normalize_token_ids +from verl.utils.ray_utils import auto_await +from verl.utils.rollout_trace import rollout_trace_op +from verl.utils.tracking import RLInsightLogger +from verl.workers.rollout.replica import RolloutReplica, TokenOutput, get_rollout_replica_class +from verl.workers.rollout.utils import update_prometheus_config + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +DEFAULT_ROUTING_CACHE_SIZE = 10000 + + +@ray.remote +class GlobalRequestLoadBalancer: + """Global sticky-session + in-flight load balancer shared by all AgentLoopWorkers. + + When a sticky session points to a removed server, the cache entry is + automatically invalidated and a new server is selected. + + Key features: + - **Atomic acquire**: ``acquire_server()`` returns ``(server_id, handle)`` + - **Sticky Session**: Uses LRUCache to map request_id → server_id, ensuring + multi-turn conversations route to the same server. + - **Least-loaded Selection**: When no sticky session exists, selects the + server with the fewest in-flight requests. + - **Deterministic Routing**: When ``full_determinism=True``, tie-breaking + among equally-loaded servers uses ``hash(request_id)`` so the same + request always routes to the same server across runs. + - **Dynamic Server Management**: Supports add/remove servers at runtime + for hybrid scaling. + """ + + def __init__( + self, + servers: dict[str, ray.actor.ActorHandle], + max_cache_size: int = DEFAULT_ROUTING_CACHE_SIZE, + full_determinism: bool = False, + ): + # Allow empty initial servers: in dynamic-resource-scheduling mode all + # replicas are hybrid and will be registered later via add_servers(). + + self._servers: dict[str, ray.actor.ActorHandle] = dict(servers) + self._inflight_requests: dict[str, int] = {sid: 0 for sid in servers} + self._request_id_to_server: LRUCache = LRUCache(maxsize=max_cache_size) + self._full_determinism = full_determinism + + def acquire_server(self, request_id: str) -> tuple[str, ray.actor.ActorHandle]: + """Acquire a server for the given request (sticky + least-loaded). + + Returns: + A tuple of ``(server_id, actor_handle)`` in a single atomic call. + """ + # Try sticky session first + if request_id in self._request_id_to_server: + server_id = self._request_id_to_server[request_id] + # Check if server is still in the active pool + if server_id in self._inflight_requests: + self._inflight_requests[server_id] += 1 + return server_id, self._servers[server_id] + # Server was removed, clear stale cache entry and re-select + del self._request_id_to_server[request_id] + + # Select new server (least-loaded among available) + if not self._inflight_requests: + raise RuntimeError("No available servers in load balancer") + + min_count = min(self._inflight_requests.values()) + candidates = [sid for sid, count in self._inflight_requests.items() if count == min_count] + if len(candidates) == 1: + server_id = candidates[0] + elif self._full_determinism: + # Deterministic tie-breaking: same request_id → same server across runs + server_id = candidates[hash(request_id) % len(candidates)] + else: + server_id = candidates[0] + self._request_id_to_server[request_id] = server_id + self._inflight_requests[server_id] += 1 + return server_id, self._servers[server_id] + + def release_server(self, server_id: str) -> None: + """Release a server after a request completes.""" + if server_id not in self._inflight_requests: + return + if self._inflight_requests[server_id] > 0: + self._inflight_requests[server_id] -= 1 + + def add_servers(self, servers: dict[str, ray.actor.ActorHandle]) -> None: + """Atomically add multiple servers to the load balancer pool. + + This is more efficient than calling :meth:`add_server` in a loop + because it performs a single bulk update on the internal state. + + Args: + servers: Dict mapping server_id → actor_handle for all servers + to register. + """ + for sid, handle in servers.items(): + self._inflight_requests[sid] = 0 + self._servers[sid] = handle + logger.info(f"[GlobalLoadBalancer] added {len(servers)} servers") + + def remove_servers(self, server_ids: list[str]) -> None: + """Atomically remove multiple servers from the load balancer pool. + + More efficient than calling :meth:`remove_server` in a loop. + + Args: + server_ids: List of server identifiers to remove. + """ + for sid in server_ids: + self._inflight_requests.pop(sid, None) + self._servers.pop(sid, None) + logger.info(f"[GlobalLoadBalancer] removed {len(server_ids)} servers") + + def get_inflight_count(self, server_id: str) -> int: + """Get number of in-flight requests for a server.""" + return self._inflight_requests.get(server_id, 0) + + def get_all_servers(self) -> list[str]: + """Get list of all active server IDs.""" + return list(self._inflight_requests.keys()) + + def clear_sticky_cache(self) -> dict: + """Clear the sticky-session cache to force request redistribution. + + After clearing, all subsequent ``acquire_server()`` calls will select + the least-loaded server (based on ``_inflight_requests``), which + naturally balances load across all active replicas — including newly + added ones with zero in-flight requests. + + Returns: + A dict with ``cleared_entries`` (number of cache entries dropped) + and ``server_loads`` (current per-server inflight counts for + diagnostics). + """ + cleared = len(self._request_id_to_server) + self._request_id_to_server.clear() + logger.info( + f"[GlobalLoadBalancer] Sticky cache cleared: {cleared} entries dropped. " + f"Server loads: {dict(self._inflight_requests)}" + ) + return { + "cleared_entries": cleared, + "server_loads": dict(self._inflight_requests), + } + + def get_status(self) -> dict: + """Return current load balancer state for debugging.""" + return { + "servers": dict(self._inflight_requests), + "total_inflight": sum(self._inflight_requests.values()), + "active_servers": len(self._inflight_requests), + "registered_handles": list(self._servers.keys()), + } + + def get_total_inflight(self) -> int: + """Return the sum of in-flight requests across all currently registered servers.""" + return sum(self._inflight_requests.values()) + + +class LLMServerClient: + """ + A class to manage multiple OpenAI compatible LLM servers. This class provides + - Load balance: least in-flight requests load balancing via global coordination + - Sticky session: send multi-turn chat completions to same server for automatic prefix caching + """ + + def __init__( + self, + config: DictConfig, + load_balancer_handle: ray.actor.ActorHandle = None, + **kwargs, + ): + """Initialize the LLMServerClient. + + Args: + config (DictConfig): whole config for main entrypoint. + load_balancer_handle (ray.actor.ActorHandle): shared global load balancer actor + that also holds the server-handle registry. Optional; subclasses that + manage server routing externally can pass None. + """ + self.config = config + self._load_balancer = load_balancer_handle + + async def _acquire_server(self, request_id: str) -> tuple[str, ray.actor.ActorHandle]: + # Atomic acquire: returns (server_id, handle) in one Ray RPC. + server_id, handle = await self._load_balancer.acquire_server.remote(request_id=request_id) + return server_id, handle + + def _release_server(self, server_id: str) -> None: + # Fire-and-forget: release is just a counter decrement, no need to await. + # Awaiting here risks blocking the finally clause if the LB actor is unresponsive. + self._load_balancer.release_server.remote(server_id=server_id) + + @rollout_trace_op + async def generate( + self, + request_id, + *, + prompt_ids: list[int], + sampling_params: dict[str, Any], + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + audio_data: Optional[list[Any]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + **kwargs: Any, + ) -> TokenOutput: + """Generate tokens from prompt ids. + + Args: + request_id (str): request id for sticky session. + prompt_ids (List[int]): List of prompt token ids. + sampling_params (Dict[str, Any]): Sampling parameters for the chat completion. + + Returns: + TokenOutput | DiffusionOutput: token or diffusion output + """ + server_id, server = await self._acquire_server(request_id) + try: + multimodal_kwargs = {} + if audio_data is not None: + multimodal_kwargs["audio_data"] = audio_data + if mm_processor_kwargs: + multimodal_kwargs["mm_processor_kwargs"] = mm_processor_kwargs + # priority is only supported by vLLM rollout server. + priority = kwargs.pop("priority", 0) + priority_kwargs = ( + {"priority": priority} if priority != 0 and self.config.actor_rollout_ref.rollout.name == "vllm" else {} + ) + output: TokenOutput = await server.generate.remote( + request_id=uuid4().hex, # use new request_id for each turn + prompt_ids=prompt_ids, + sampling_params=sampling_params, + image_data=image_data, + video_data=video_data, + **multimodal_kwargs, + **priority_kwargs, + **kwargs, + ) + global_steps = output.extra_fields.get("global_steps") + # setdefault does not replace an existing None value. Some custom + # rollout adapters preserve a stable extra_fields schema with these + # keys initialized to None, so fill them explicitly when unset. + if output.extra_fields.get("min_global_steps") is None: + output.extra_fields["min_global_steps"] = global_steps + if output.extra_fields.get("max_global_steps") is None: + output.extra_fields["max_global_steps"] = global_steps + return output + finally: + self._release_server(server_id) + + +class FullyAsyncLLMServerClient(LLMServerClient): + """FullyLLMServerClient supports resume generation on partial rollout, making rollout interruption + invisible to the AgentLoop. + """ + + def __init__( + self, + config: DictConfig, + load_balancer_handle: ray.actor.ActorHandle = None, + only_hybrid: bool = False, + **kwargs, + ): + """Initialize the FullyAsyncLLMServerClient. + + Args: + config (DictConfig): whole config for main entrypoint. + load_balancer_handle (ray.actor.ActorHandle): shared global load balancer actor + that also holds the server-handle registry. + only_hybrid (bool): When ``True``, hybrid replicas are the *only* rollout + resource. If the load balancer is temporarily empty (e.g. during + weight synchronisation) :meth:`_acquire_server` will keep retrying + every 1 second instead of raising immediately. + """ + super().__init__(config=config, load_balancer_handle=load_balancer_handle, **kwargs) + self._only_hybrid = only_hybrid + + async def _acquire_server(self, request_id: str) -> tuple[str, ray.actor.ActorHandle]: + # Atomic acquire: returns (server_id, handle) in one Ray RPC. + # When only_hybrid is True, hybrid replicas are the sole rollout resource and + # the LB may be temporarily empty during weight sync / scaling transitions. + # In that case keep retrying every 1 s until a server becomes available. + # Otherwise raise immediately so callers see the error right away. + while True: + try: + return await super()._acquire_server(request_id) + except RuntimeError as e: + if "No available servers in load balancer" in str(e) and self._only_hybrid: + await asyncio.sleep(1) + else: + raise + + @rollout_trace_op + async def generate( + self, + request_id, + *, + prompt_ids: list[int], + sampling_params: dict[str, Any], + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + audio_data: Optional[list[Any]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + **kwargs: Any, + ) -> TokenOutput: + """Generate tokens from prompt ids. + + Args: + request_id (str): request id for sticky session. + prompt_ids (List[int]): List of prompt token ids. + sampling_params (Dict[str, Any]): Sampling parameters for the chat completion. + image_data (Optional[List[Any]]): Image data for the chat completion. + video_data (Optional[List[Any]]): Video data for the chat completion. + audio_data (Optional[List[Any]]): Audio data for the chat completion. + mm_processor_kwargs (Optional[Dict[str, Any]]): Multimodal processor kwargs. + + Returns: + TokenOutput: token output + """ + prompt_ids = normalize_token_ids(prompt_ids) + + limit_key = None + if "max_tokens" in sampling_params: + limit_key = "max_tokens" + elif "max_new_tokens" in sampling_params: + limit_key = "max_new_tokens" + original_max_tokens = sampling_params.get(limit_key) if limit_key else None + + final_output = TokenOutput( + token_ids=[], + log_probs=[], + num_preempted=0, + ) + min_global_steps, max_global_steps = None, None + + while True: + # 1. generate tokens + output = await super().generate( + request_id=request_id, + prompt_ids=prompt_ids + final_output.token_ids, + sampling_params=sampling_params, + image_data=image_data, + video_data=video_data, + audio_data=audio_data, + mm_processor_kwargs=mm_processor_kwargs, + **kwargs, + ) + + # 2. merge output into final_output + final_output.token_ids.extend(output.token_ids) + if output.log_probs is not None: + final_output.log_probs.extend(output.log_probs) + # On partial rollout resume the model version may differ, so keep + # existing routing and only append routing for newly generated tokens. + if output.routed_experts is not None and len(output.token_ids) > 0: + if final_output.routed_experts is None: + final_output.routed_experts = output.routed_experts + else: + final_output.routed_experts = torch.cat( + [final_output.routed_experts, output.routed_experts[-len(output.token_ids) :]], + dim=0, + ) + if output.num_preempted is not None: + final_output.num_preempted += output.num_preempted + final_output.stop_reason = output.stop_reason + + # update model weights version + global_steps = output.extra_fields.get("global_steps", None) + if min_global_steps is None: + min_global_steps = global_steps + max_global_steps = global_steps + + # 3. update max_new_tokens + if original_max_tokens is not None: + sampling_params[limit_key] = original_max_tokens - len(final_output.token_ids) + if len(final_output.token_ids) >= original_max_tokens: + final_output.stop_reason = "length" + break + + # 4. check stop reason + # If partial rollout not enable, aborted samples will be dropped. + # For v1 trainer, should_retry is always True. Since self.config.async_training is not exist. + should_retry = True + if hasattr(self.config, "async_training") and not self.config.async_training.partial_rollout: + should_retry = False + if output.stop_reason not in ("aborted", "abort") or not should_retry: + break + + await asyncio.sleep(1) + + final_output.extra_fields["global_steps"] = global_steps + final_output.extra_fields["min_global_steps"] = min_global_steps + final_output.extra_fields["max_global_steps"] = max_global_steps + return final_output + + +class LLMServerManager: + """LLMServerManager is responsible for: + - Launch server replicas + - Launch global load balancer + - Elastic launch/tear-down new replicas + + Args: + config (DictConfig): Config for the trainer entrypoint. + worker_group (RayWorkerGroup): Worker group for the server replicas. If not none, init hybrid server, + else init standalone server with a new resource pool. + rollout_resource_pool (RayResourcePool): Resource pool for the server replicas, only needed for TensorRT-LLM. + start_rank (int): First ``replica_rank`` to assign. Defaults to 0. + """ + + def __init__( + self, + config: DictConfig, + worker_group: RayWorkerGroup = None, + rollout_resource_pool: RayResourcePool = None, + start_rank: int = 0, + ): + self.config = config + self.rollout_config = config.actor_rollout_ref.rollout + self.model_config = config.actor_rollout_ref.model + self.worker_group = worker_group + self.rollout_resource_pool = rollout_resource_pool + self.start_rank = start_rank + + assert worker_group is not None or self.rollout_config.nnodes > 0, "nnodes must be > 0 in standalone mode" + + # for recipe to change + if not hasattr(self, "rollout_replica_class"): + self.rollout_replica_class = get_rollout_replica_class( + self.rollout_config.name, + disaggregation_enabled=self.rollout_config.disaggregation.enabled, + ) + + @classmethod + @auto_await + async def create(cls, *args, **kwargs): + """Create the LLMServerManager.""" + instance = cls(*args, **kwargs) + await instance._initialize_llm_servers() + await instance._init_global_load_balancer() + return instance + + async def _initialize_llm_servers(self, start_rank: int = None): + """Initialize the LLM server replicas. + + Args: + start_rank: First ``replica_rank`` to assign. Defaults to ``self.start_rank`` + so standalone replicas can avoid Ray named-actor collisions with hybrid + replicas (which start at 0) when both coexist (e.g. separate async). + """ + if start_rank is None: + start_rank = self.start_rank + rollout_world_size = ( + self.rollout_config.tensor_model_parallel_size + * self.rollout_config.data_parallel_size + * self.rollout_config.pipeline_model_parallel_size + ) + # PD inflates per-replica footprint; miss this and init_hybrid slices + # past worker_group → empty workers on replica_rank>=1. + disagg = getattr(self.rollout_config, "disaggregation", None) + if disagg is not None and getattr(disagg, "enabled", False): + prefill_tp = self.rollout_config.tensor_model_parallel_size + # Inline decode_tp default: OmegaConf/Ray serialization drops dataclass methods. + decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else prefill_tp + ) + rollout_world_size = ( + (prefill_tp * disagg.prefill_replicas + decode_tp * disagg.decode_replicas) + * self.rollout_config.data_parallel_size + * self.rollout_config.pipeline_model_parallel_size + ) + world_size = ( + self.worker_group.world_size + if self.worker_group + else self.rollout_config.n_gpus_per_node * self.rollout_config.nnodes + ) + num_replicas = world_size // rollout_world_size + + self.rollout_replicas = [ + self.rollout_replica_class( + replica_rank=start_rank + replica_rank, + config=self.rollout_config, + model_config=self.model_config, + gpus_per_node=self.rollout_config.n_gpus_per_node, + ) + for replica_rank in range(num_replicas) + ] + + if self.worker_group and self.rollout_config.name != "trtllm": + await asyncio.gather(*[server.init_hybrid(self.worker_group) for server in self.rollout_replicas]) + # TODO: unify trtllm to init_hybrid + elif self.worker_group and self.rollout_config.name == "trtllm": + await asyncio.gather( + *[ + server.init_hybrid_colocated(self.worker_group, self.rollout_resource_pool) + for server in self.rollout_replicas + ] + ) + else: + await asyncio.gather(*[server.init_standalone() for server in self.rollout_replicas]) + + self.server_handles = [server._server_handle for server in self.rollout_replicas] + self.server_addresses = [server._server_address for server in self.rollout_replicas] + print(f"LLMServerManager: {self.server_addresses}") + + # Update Prometheus / rl-insight metrics with server addresses + needs_metrics = self.rollout_config.prometheus.enable or RLInsightLogger.enabled() + if self.rollout_config.disable_log_stats: + if needs_metrics: + raise ValueError("Metrics monitoring requires disable_log_stats=False, but it is currently True.") + if not self.rollout_config.disable_log_stats: + if self.rollout_config.prometheus.enable: + update_prometheus_config( + self.rollout_config.prometheus, self.server_addresses, self.rollout_config.name + ) + if RLInsightLogger.enabled(): + RLInsightLogger.register_rollout_metrics( + self.server_addresses, + self.rollout_config.name, + labels=[{"replica": server.replica_rank} for server in self.rollout_replicas], + ) + + async def _init_global_load_balancer(self) -> None: + self.global_load_balancer = GlobalRequestLoadBalancer.remote( + servers=dict(zip(self.server_addresses, self.server_handles, strict=True)), + max_cache_size=DEFAULT_ROUTING_CACHE_SIZE, + full_determinism=getattr(self.rollout_config, "full_determinism", False), + ) + + def get_client(self, client_cls=LLMServerClient, **kwargs) -> LLMServerClient: + """Get the LLMServerClient to request LLM server replicas. + + Args: + client_cls: The client class to instantiate (default: ``LLMServerClient``). + Pass ``FullyAsyncLLMServerClient`` for abort-resume support. + **kwargs: Forwarded to the client constructor. + """ + return client_cls( + config=self.config, + load_balancer_handle=self.global_load_balancer, + **kwargs, + ) + + def get_addresses(self) -> list[str]: + """Get the OpenAI chat completion API http addresses of the LLM server replicas.""" + return self.server_addresses + + def get_replicas(self) -> list[RolloutReplica]: + """Get the LLM server replicas.""" + return self.rollout_replicas + + @auto_await + async def start_profile(self, **kwargs): + """Start profiling on all rollout replicas.""" + await asyncio.gather(*[replica.start_profile(**kwargs) for replica in self.rollout_replicas]) + + @auto_await + async def stop_profile(self): + """Stop profiling on all rollout replicas.""" + await asyncio.gather(*[replica.stop_profile() for replica in self.rollout_replicas]) diff --git a/verl_0720_main/verl/verl/workers/rollout/naive/__init__.py b/verl_0720_main/verl/verl/workers/rollout/naive/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..cb6c23bf4327ef199ea9b454f00be88cbaa27967 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/naive/__init__.py @@ -0,0 +1,17 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from .naive_rollout import NaiveRollout + +__all__ = ["NaiveRollout"] diff --git a/verl_0720_main/verl/verl/workers/rollout/naive/naive_rollout.py b/verl_0720_main/verl/verl/workers/rollout/naive/naive_rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..fe56dc4c929b05aa2279b7e1b46e6d9a74e1b175 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/naive/naive_rollout.py @@ -0,0 +1,120 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +In single GPU rollout, the sequences are generated directly by sampling from the model. +The output will contain +1. output_ids +2. attention_masks (left padding) +3. eos_masks +4. log_probs +""" + +import torch +import torch.nn.functional as F +from tensordict import TensorDict +from torch import nn + +from verl import DataProto +from verl.utils.torch_functional import logprobs_from_logits + +from ..base import BaseRollout + +__all__ = ["NaiveRollout"] + + +class NaiveRollout(BaseRollout): + def __init__(self, module: nn.Module, config): + """A naive rollout. It requires the module to be compatible with huggingface APIs. That is: + The module should define __call__ to receive input_ids, attention_mask and position_ids. + It outputs a structure that contains logits field. + + Args: + module: module here follows huggingface APIs + config: DictConfig + """ + super().__init__() + self.config = config + self.module = module + + @torch.no_grad() + def generate_sequences(self, prompts: DataProto) -> DataProto: + """Generate sequences""" + idx = prompts.batch["input_ids"] # (bs, prompt_length) + attention_mask = prompts.batch["attention_mask"] # left-padded attention_mask + position_ids = prompts.batch["position_ids"] + + # used to construct attention_mask + eos_token_id = prompts.meta_info["eos_token_id"] + + batch_size = idx.size(0) + prompt_length = idx.size(1) + + self.module.eval() + + prev_attention_mask = torch.ones(size=(batch_size, 1), dtype=attention_mask.dtype, device=attention_mask.device) + + logits_lst = [] + for _ in range(self.config.response_length): + # if the sequence context is growing too long we must crop it at block_size + # idx_cond = idx if idx.size(1) <= self.config.block_size else idx[:, -self.config.block_size:] + idx_cond = idx + # forward the model to get the logits for the index in the sequence + # we use huggingface APIs here + output = self.module(input_ids=idx_cond, attention_mask=attention_mask, position_ids=position_ids) + logits = output.logits + # pluck the logits at the final step and scale by desired temperature + logits = logits[:, -1, :] / self.config.temperature # (bs, vocab_size) + # optionally crop the logits to only the top k options + if self.config.top_k is not None: + v, _ = torch.topk(logits, min(self.config.top_k, logits.size(-1))) + logits[logits < v[:, [-1]]] = -float("Inf") + # apply softmax to convert logits to (normalized) probabilities + probs = F.softmax(logits, dim=-1) + # sample from the distribution + if self.config.do_sample: + idx_next = torch.multinomial(probs, num_samples=1) + else: + idx_next = torch.argmax(probs, dim=-1, keepdim=True) + + attention_mask = torch.cat((attention_mask, prev_attention_mask), dim=-1) + + for token_id in eos_token_id: + prev_attention_mask = torch.logical_and(idx_next != token_id, prev_attention_mask.bool()) + prev_attention_mask.to(attention_mask.dtype) + + position_ids = torch.cat((position_ids, position_ids[:, -1:] + 1), dim=-1) + + # append sampled index to the running sequence and continue + idx = torch.cat((idx, idx_next), dim=1) + logits_lst.append(logits) + + logits = torch.stack(logits_lst, dim=1) # (bs, response_length, vocab_size) + prompts = idx[:, :prompt_length] # (bs, prompt_length) + response = idx[:, prompt_length:] # (bs, response_length) + log_probs = logprobs_from_logits(logits=logits, labels=response) + batch = TensorDict( + { + "input_ids": prompts, + "responses": response, + "sequences": idx, + "old_log_probs": log_probs, + "attention_mask": attention_mask, + "position_ids": position_ids, + }, + batch_size=batch_size, + ) + + self.module.train() + + return DataProto(batch=batch) diff --git a/verl_0720_main/verl/verl/workers/rollout/replica.py b/verl_0720_main/verl/verl/workers/rollout/replica.py new file mode 100644 index 0000000000000000000000000000000000000000..c01cdd79126ffa51ddcb42cdc3ff6529d5c6ecbe --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/replica.py @@ -0,0 +1,408 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import asyncio +import logging +import os +from abc import ABC, abstractmethod +from enum import Enum +from typing import Any, Callable, Optional + +import ray +from omegaconf import DictConfig +from pydantic import BaseModel +from ray.actor import ActorHandle + +from verl.single_controller.ray import RayClassWithInitArgs, RayResourcePool, RayWorkerGroup, ResourcePoolManager +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.device import is_torch_npu_available +from verl.workers.config import HFModelConfig, RolloutConfig + +logger = logging.getLogger(__file__) + + +# Max number of concurrent calls to the methods of Rollout, +# excluding calls to generate method. +CONTROL_METHOD_CONCURRENCY = 16 + + +class TokenOutput(BaseModel): + token_ids: list[int] + """response token ids""" + log_probs: Optional[list[float]] = None + """logprobs of response token ids""" + routed_experts: Optional[Any] = None + """routed experts of response token ids""" + stop_reason: Optional[str] = None + """stop reason: 'completed', 'aborted', or None for unknown""" + num_preempted: Optional[int] = None + """number of preempted times for metric calculation""" + extra_fields: dict[str, Any] = {} + """Extra fields for dynamic addition.""" + + +class RolloutMode(Enum): + # Rollout engine and training engine(fsdp/megatron) fused in same process + # Rollout and trainer share GPUs, switch context with weight synchronization. + # Usage scenarios: on-policy training. + HYBRID = "hybrid" + + # Rollout engine colocated with hybrid engine in same ray placement group but in separate process. + # Rollout and hybrid processes share GPUs, switch context without weight synchronization. + # Usage scenarios: GRM (LLM as a judge). + COLOCATED = "colocated" + + # Standalone rollout server with separate GPU resource, disaggregated architecture. + # Usage scenarios: off-policy training. + STANDALONE = "standalone" + + +class RolloutReplica(ABC): + """Rollout replica is an individual server instance, which may be deployed on single or multiple nodes. + It is equivalent to launch server in each node with command line: + + SGLang: + ``` + python -m sglang.launch_server --node-rank 0 --nnode 2 ... + python -m sglang.launch_server --node-rank 1 --nnode 2 ... + ``` + + vLLM: + ``` + vllm serve --data-parallel-size 16 --data-parallel-size-local 8 --data-parallel-start-rank 0 ... + vllm serve --data-parallel-size 16 --data-parallel-size-local 8 --data-parallel-start-rank 8 ... + ``` + + Args: + replica_rank: int, rank of this rollout replica. + config: RolloutConfig, full config. + model_config: DictConfig, model config. + gpus_per_node: int, number of gpus per node. + """ + + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: DictConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + name_suffix: str = "", + ) -> None: + self.replica_rank = replica_rank + self.config: RolloutConfig = omega_conf_to_dataclass(config) + self.model_config: HFModelConfig = model_config + + self.world_size = ( + self.config.tensor_model_parallel_size + * self.config.data_parallel_size + * self.config.pipeline_model_parallel_size + ) + self.gpus_per_node = gpus_per_node + self.gpus_per_replica_node = min(gpus_per_node, self.world_size) + assert self.world_size % self.gpus_per_replica_node == 0, ( + f"world_size {self.world_size} must be divisible by gpus_per_node {self.gpus_per_replica_node}" + ) + self.nnodes = self.world_size // self.gpus_per_replica_node + self.is_reward_model = is_reward_model + self.is_teacher_model = is_teacher_model + self.name_suffix = f"_{name_suffix}" if name_suffix else "" + + self.rollout_mode: RolloutMode = None + self.workers: list[ActorHandle] = [] + self.resource_pool: RayResourcePool = None + self.bundle_indices: list[int] = [] + + self.servers: list[ActorHandle] = [] + self._server_address: str = None + self._server_handle: ActorHandle = None + + async def init_hybrid(self, worker_group: RayWorkerGroup): + """Init hybrid rollout server, rollout engine and training engine(fsdp/megatron) fused in same process. + + Args: + worker_group: RayWorkerGroup, fused workers where training engine(fsdp/megatron) have been initialized. + """ + self.rollout_mode = RolloutMode.HYBRID + self.workers = worker_group.workers[ + self.world_size * self.replica_rank : self.world_size * (self.replica_rank + 1) + ] + await self.launch_servers() + + async def init_hybrid_colocated(self, worker_group: RayWorkerGroup, resource_pool: RayResourcePool): + """Init hybrid rollout server, rollout engine and training engine(fsdp/megatron) fused in same process. + + Args: + worker_group: RayWorkerGroup, fused workers where training engine(fsdp/megatron) have been initialized. + resource_pool: RayResourcePool, ray placement group where hybrid engine processes have been launched. + bundle_indices: list[int], bundle indices for this rollout replica. + """ + self.rollout_mode = RolloutMode.HYBRID + self.workers = worker_group.workers[ + self.world_size * self.replica_rank : self.world_size * (self.replica_rank + 1) + ] + self.resource_pool = resource_pool + self.bundle_indices = [self.replica_rank * self.world_size + idx for idx in range(self.world_size)] + await self.launch_servers() + + # TODO(sgm): this should be the default solution, but need to make the RolloutMode more clear. + async def init_colocated(self, resource_pool: RayResourcePool): + """Init colocated rollout server, rollout engine and hybrid engine colocated in same ray placement group + but in separate processes. + + Args: + resource_pool: RayResourcePool, ray placement group where hybrid engine processes have been launched. + """ + self.rollout_mode = RolloutMode.COLOCATED + self.resource_pool = resource_pool + use_gpu = self.rollout_worker_use_gpu() + + if self.is_reward_model: + name_prefix = f"rollout_reward_colocate_{self.replica_rank}{self.name_suffix}" + elif self.is_teacher_model: + name_prefix = f"rollout_teacher_colocate_{self.replica_rank}{self.name_suffix}" + else: + name_prefix = f"rollout_colocate_{self.replica_rank}{self.name_suffix}" + + worker_group = RayWorkerGroup( + resource_pool=self.resource_pool, + ray_cls_with_init=self.get_ray_class_with_init_args(), + bin_pack=False, + name_prefix=name_prefix, + use_gpu=use_gpu, + device_name="cuda" if not is_torch_npu_available(check_device=False) else "npu", + ) + self.workers = worker_group.workers + await self.launch_servers() + + async def init_standalone(self): + """Init standalone rollout server, create new resource pool for this rollout.""" + # create resource pool for this rollout + self.rollout_mode = RolloutMode.STANDALONE + if self.is_reward_model: + resource_pool_name = f"rollout_pool_reward_{self.replica_rank}{self.name_suffix}" + elif self.is_teacher_model: + resource_pool_name = f"rollout_pool_teacher_{self.replica_rank}{self.name_suffix}" + else: + resource_pool_name = f"rollout_pool_{self.replica_rank}{self.name_suffix}" + resource_pool_spec = { + resource_pool_name: [self.gpus_per_replica_node] * self.nnodes, + } + resource_pool_manager = ResourcePoolManager( + resource_pool_spec=resource_pool_spec, + mapping=None, + max_colocate_count=2, + ) + resource_pool_manager.create_resource_pool() + self.resource_pool = resource_pool_manager.resource_pool_dict[resource_pool_name] + + # create worker group for this rollout + if self.is_reward_model: + name_prefix = f"rollout_reward_standalone_{self.replica_rank}{self.name_suffix}" + elif self.is_teacher_model: + name_prefix = f"rollout_teacher_standalone_{self.replica_rank}{self.name_suffix}" + else: + name_prefix = f"rollout_standalone_{self.replica_rank}{self.name_suffix}" + worker_group = RayWorkerGroup( + resource_pool=self.resource_pool, + ray_cls_with_init=self.get_ray_class_with_init_args(), + bin_pack=False, + name_prefix=name_prefix, + use_gpu=True, + device_name="cuda" if not is_torch_npu_available(check_device=False) else "npu", + ) + self.workers = worker_group.workers + await self.launch_servers() + + def get_ray_class_with_init_args(self) -> RayClassWithInitArgs: + """Get rollout worker actor class for colocated and standalone mode.""" + from verl.checkpoint_engine.base import CheckpointEngineWorker + + rollout_worker_actor_cls = ray.remote(CheckpointEngineWorker) + + return RayClassWithInitArgs( + cls=rollout_worker_actor_cls, + rollout_config=self.config, + model_config=self.model_config, + replica_rank=self.replica_rank, + ) + + @abstractmethod + async def launch_servers(self): + """Launch http server in each node.""" + raise NotImplementedError + + @property + def server_address(self) -> str: + """Get rollout server address for OpenAI chat completion.""" + return self._server_address + + @property + def server_handle(self) -> ActorHandle: + """Get rollout server handle for Token-in-token-out generation.""" + return self._server_handle + + @property + def max_concurrency(self) -> int: + # 1000 is Ray's default max_concurrency for async execution. + # Add some margin to account for control method call. + return max(1000, self.config.max_num_seqs + CONTROL_METHOD_CONCURRENCY) + + def rollout_worker_use_gpu(self) -> bool: + return True + + async def wake_up(self): + """Wake up each rollout server.""" + await asyncio.gather(*[server.wake_up.remote() for server in self.servers]) + + async def sleep(self): + """Sleep each rollout server.""" + await asyncio.gather(*[server.sleep.remote() for server in self.servers]) + + async def abort_all_requests(self): + """Partial rollout: abort and save all unfinished requests in each rollout server.""" + await asyncio.gather(*[server.abort_all_requests.remote() for server in self.servers]) + + async def resume_generation(self): + """Resume generation on all servers after abort_all_requests.""" + await asyncio.gather(*[server.resume_generation.remote() for server in self.servers]) + + async def clear_kv_cache(self): + """reset kv cache in each rollout server.""" + await asyncio.gather(*[server.clear_kv_cache.remote() for server in self.servers]) + + async def release_kv_cache(self): + """Release only the kv_cache GPU memory, keeping model weights in place.""" + await asyncio.gather(*[server.release_kv_cache.remote() for server in self.servers]) + + async def resume_kv_cache(self): + """Restore the kv_cache GPU memory after a weight sync.""" + await asyncio.gather(*[server.resume_kv_cache.remote() for server in self.servers]) + + async def start_profile(self, **kwargs): + """Start profiling on the replica.""" + await asyncio.gather(*[server.start_profile.remote(**kwargs) for server in self.servers]) + + async def stop_profile(self): + """Stop profiling on the replica.""" + await asyncio.gather(*[server.stop_profile.remote() for server in self.servers]) + + +class RolloutReplicaRegistry: + """Factory for managing rollout replica implementations.""" + + _registry: dict[str, Callable[[], type[RolloutReplica]]] = {} + + @classmethod + def register(cls, name: str, loader: Callable[[], type[RolloutReplica]]) -> None: + """Register a new rollout replica type.""" + cls._registry[name] = loader + + @classmethod + def get(cls, name: str) -> type[RolloutReplica]: + """Get a rollout replica class by name.""" + if name not in cls._registry: + raise ValueError(f"Unknown rollout mode: {name}. Available: {list(cls._registry.keys())}") + return cls._registry[name]() + + +# Loader functions for built-in types +def _load_vllm(): + from verl.workers.rollout.vllm_rollout.vllm_async_server import vLLMReplica + + return vLLMReplica + + +def _load_sglang(): + os.environ["SGLANG_USE_CPU_ENGINE"] = "1" + + try: + import vllm # noqa: F401 + except ImportError: + import sys + import types + from unittest.mock import Mock + + mock_vllm = types.ModuleType("vllm") + + mock_custom_ops = types.ModuleType("vllm._custom_ops") + mock_custom_ops.scaled_fp8_quant = Mock() + mock_vllm._custom_ops = mock_custom_ops + + mock_model_executor = types.ModuleType("vllm.model_executor") + mock_layers = types.ModuleType("vllm.model_executor.layers") + mock_activation = types.ModuleType("vllm.model_executor.layers.activation") + + class GeluAndMul: # noqa: N801 + pass + + class SiluAndMul: # noqa: N801 + pass + + mock_activation.GeluAndMul = GeluAndMul + mock_activation.SiluAndMul = SiluAndMul + mock_layers.activation = mock_activation + mock_model_executor.layers = mock_layers + mock_vllm.model_executor = mock_model_executor + + sys.modules["vllm"] = mock_vllm + sys.modules["vllm._custom_ops"] = mock_custom_ops + sys.modules["vllm.model_executor"] = mock_model_executor + sys.modules["vllm.model_executor.layers"] = mock_layers + sys.modules["vllm.model_executor.layers.activation"] = mock_activation + + from verl.workers.rollout.sglang_rollout.async_sglang_server import SGLangReplica + + del os.environ["SGLANG_USE_CPU_ENGINE"] + return SGLangReplica + + +def _load_trtllm(): + from verl.workers.rollout.trtllm_rollout.trtllm_async_server import TRTLLMReplica + + return TRTLLMReplica + + +# Register built-in types +RolloutReplicaRegistry.register("vllm", _load_vllm) +RolloutReplicaRegistry.register("sglang", _load_sglang) +RolloutReplicaRegistry.register("trtllm", _load_trtllm) + + +def get_rollout_replica_class(rollout: str, disaggregation_enabled: bool = False) -> type[RolloutReplica]: + """Resolve a replica class by backend name. + + PD-disaggregated rollouts reuse the base backend name (``sglang`` / + ``vllm``); the dispatch here picks the PD class only when the caller + asserts ``disaggregation_enabled=True`` (sourced from + ``RolloutConfig.disaggregation.enabled``). Validation in + ``RolloutConfig.__post_init__`` rejects the flag for backends without a + PD class. + """ + if disaggregation_enabled: + if rollout == "sglang": + # _load_sglang side-effect: installs vllm mocks needed by SGLangPDReplica's + # transitive imports. Cheap if already installed. + RolloutReplicaRegistry.get("sglang") + from verl.workers.rollout.sglang_rollout.sglang_pd_replica import SGLangPDReplica + + return SGLangPDReplica + if rollout == "vllm": + from verl.workers.rollout.vllm_rollout.vllm_pd_replica import vLLMPDReplica + + return vLLMPDReplica + raise NotImplementedError( + f"PD disaggregation is only supported with rollout in ('sglang', 'vllm'); got {rollout!r}." + ) + return RolloutReplicaRegistry.get(rollout) diff --git a/verl_0720_main/verl/verl/workers/rollout/schemas.py b/verl_0720_main/verl/verl/workers/rollout/schemas.py new file mode 100644 index 0000000000000000000000000000000000000000..0b48c94f108b87e29af6ea7a6af068859f763e1d --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/schemas.py @@ -0,0 +1,713 @@ +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import difflib +import logging +import os +from enum import Enum +from typing import Any, Optional + +import torch +from pydantic import BaseModel, ConfigDict, model_validator +from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast, ProcessorMixin + +from verl.tools.schemas import OpenAIFunctionToolCall, OpenAIFunctionToolSchema, ToolResponse +from verl.utils.model import compute_position_id_with_mask +from verl.utils.tokenizer import build_multimodal_processor_inputs + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +BASE_CHAT_HISTORY = [ + {"role": "system", "content": "You are a helpful assistant."}, + {"role": "user", "content": "I am a user."}, +] + + +class FinishReasonTypeEnum(str, Enum): + """The enum for finish reason type.""" + + LENGTH = "length" + STOP = "stop" + TOOL_CALL = "tool_calls" + + @classmethod + def from_str(cls, value: str) -> "FinishReasonTypeEnum": + if value == "stop": + return cls.STOP + elif value == "length": + return cls.LENGTH + elif value == "tool_calls": + return cls.TOOL_CALL + else: + raise ValueError(f"Unsupported finish reason type: {value}") + + +class Message(BaseModel): + role: str + content: str | dict[str, Any] | list[dict[str, Any]] | ToolResponse + tool_calls: Optional[list[OpenAIFunctionToolCall]] = None + + +class AsyncRolloutRequestStateEnum(str, Enum): + """The enum for async rollout request state.""" + + PENDING = "pending" + RUNNING = "running" + COMPLETED = "completed" + FAILED = "failed" + TOOL_CALLING = "tool_calling" + + +class TokenizationSanityCheckModeEnum(str, Enum): + """The enum for tokenization sanity check mode.""" + + DISABLE = "disable" + STRICT = "strict" + IGNORE_STRIPPABLE = "ignore_strippable" + + +class AsyncRolloutRequest(BaseModel): + """The data model for async rollout.""" + + model_config = ConfigDict(arbitrary_types_allowed=True) + + batch_data_id: int = 0 + rollout_offset: int = 0 + request_id: str + state: AsyncRolloutRequestStateEnum + messages: list[Message] + multi_modal_keys: Optional[list[str]] = None + multi_modal_data: Optional[dict[str, Any]] = None + multi_modal_inputs: Optional[dict[str, torch.Tensor]] = None + mm_processor_kwargs: Optional[dict[str, Any]] = None + tool_schemas: Optional[list[OpenAIFunctionToolSchema]] = None + tools_kwargs: dict[str, Any] = {} + input_ids: Optional[torch.Tensor] = None + prompt_ids: Optional[torch.Tensor] = None + response_ids: Optional[torch.Tensor] = None + attention_mask: Optional[torch.Tensor] = None + prompt_attention_mask: Optional[torch.Tensor] = None + response_attention_mask: Optional[torch.Tensor] = None + position_ids: Optional[torch.Tensor] = None + prompt_position_ids: Optional[torch.Tensor] = None + response_position_ids: Optional[torch.Tensor] = None + loss_mask: Optional[torch.Tensor] = None + prompt_loss_mask: Optional[torch.Tensor] = None + response_loss_mask: Optional[torch.Tensor] = None + reward_scores: dict[str, float] + max_prompt_len: int + max_response_len: int = 8192 + max_model_len: int = 32768 + metrics: dict[str, list[Any]] = {} + output_token_ids: torch.Tensor | None = None + rollout_log_probs: torch.Tensor | None = None + + use_inference_chat_template: bool + tokenization_sanity_check_mode: TokenizationSanityCheckModeEnum + generation_prompt_ids: Optional[torch.Tensor] = None + base_conv_wo_gen_prompt_end_pos: int + base_conv_with_gen_prompt_end_pos: int + + @model_validator(mode="before") + @classmethod + def initialize_request(cls, values): + if not (messages := values.get("messages")): + raise ValueError("messages is required for AsyncRolloutRequest initialization") + if not (max_prompt_len := values.get("max_prompt_len")): + raise ValueError("max_prompt_len is required for AsyncRolloutRequest initialization") + if not (processing_class := values.pop("processing_class", None)): + raise ValueError("processing_class is required for AsyncRolloutRequest initialization") + + values["messages"] = [Message.model_validate(msg) for msg in messages] + + # If there is no multi_modal_keys, we assume the multi-modal data is image, video and audio. + if not values.get("multi_modal_keys"): + values["multi_modal_keys"] = ["image", "video", "audio"] + if not values.get("multi_modal_data"): + values["multi_modal_data"] = {key: [] for key in values["multi_modal_keys"]} + else: + # check if all multi_modal_keys are in multi_modal_data + for key in values["multi_modal_keys"]: + if key not in values["multi_modal_data"]: + values["multi_modal_data"][key] = [] + if not values.get("multi_modal_inputs"): + values["multi_modal_inputs"] = {} + if not values.get("mm_processor_kwargs"): + values["mm_processor_kwargs"] = {} + + tools = ( + [tool.model_dump() for tool in tool_schemas] if (tool_schemas := values.get("tool_schemas", [])) else None + ) + + multi_modal_data = values["multi_modal_data"] + mm_processor_kwargs = values["mm_processor_kwargs"] + tokens_without_prompt = cls._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data=multi_modal_data, + mm_processor_kwargs=mm_processor_kwargs, + tools=tools, + add_generation_prompt=False, + tokenize=True, + ) + if ( + values.get("input_ids") is None + or values.get("attention_mask") is None + or values.get("position_ids") is None + ): + tokenization_dict_with_prompt = cls._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data=multi_modal_data, + mm_processor_kwargs=mm_processor_kwargs, + tools=tools, + add_generation_prompt=True, + tokenize=True, + return_dict=True, + ) + + values["input_ids"], values["attention_mask"] = ( + tokenization_dict_with_prompt["input_ids"], + tokenization_dict_with_prompt["attention_mask"], + ) + if values["input_ids"].shape[-1] > max_prompt_len: + # Only log the warning to avoid truncating in the middle of generation prompt. Consider raising an + # error for this case in the future. + # Ensure batch_data_id exists with default value if not provided + if "batch_data_id" not in values: + values["batch_data_id"] = cls.model_fields["batch_data_id"].default + logger.warning( + f"Prompt {values['batch_data_id']} has length {values['input_ids'].shape[-1]} " + f"which is greater than max_prompt_len {max_prompt_len} after applied chat template with tools." + ) + + # Process multi_modal_inputs + multi_modal_inputs = tokenization_dict_with_prompt.copy() + multi_modal_inputs.pop("input_ids", None) + multi_modal_inputs.pop("attention_mask", None) + values["multi_modal_inputs"] = multi_modal_inputs + + values["position_ids"] = values["prompt_position_ids"] = cls._get_position_ids( + processing_class, + values["input_ids"], + values["attention_mask"], + multi_modal_inputs, + mm_processor_kwargs=mm_processor_kwargs, + ) + + values["prompt_ids"], values["prompt_attention_mask"] = values["input_ids"], values["attention_mask"] + values["loss_mask"] = values["prompt_loss_mask"] = torch.zeros_like(values["input_ids"], dtype=torch.bool) + values["generation_prompt_ids"] = values["input_ids"][..., tokens_without_prompt.shape[-1] :] + values["base_conv_wo_gen_prompt_end_pos"] = cls._handle_apply_chat_template( + processing_class, + BASE_CHAT_HISTORY, + multi_modal_data=multi_modal_data, + mm_processor_kwargs=mm_processor_kwargs, + tools=tools, + add_generation_prompt=False, + tokenize=True, + ).shape[-1] + + values["base_conv_with_gen_prompt_end_pos"] = cls._handle_apply_chat_template( + processing_class, + BASE_CHAT_HISTORY, + multi_modal_data=multi_modal_data, + mm_processor_kwargs=mm_processor_kwargs, + tools=tools, + add_generation_prompt=True, + tokenize=True, + ).shape[-1] + + return values + + @staticmethod + def _handle_apply_chat_template( + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + messages: list[Message], + multi_modal_data: dict[str, Any], + mm_processor_kwargs: Optional[dict[str, Any]] = None, + tools: Optional[list[OpenAIFunctionToolSchema]] = None, + add_generation_prompt: bool = False, + tokenize: bool = False, + return_dict: bool = False, + ): + raw_prompt = processing_class.apply_chat_template( + messages, tools=tools, add_generation_prompt=add_generation_prompt, tokenize=False + ) + if not tokenize: + return raw_prompt + + if isinstance(processing_class, PreTrainedTokenizer) or isinstance(processing_class, PreTrainedTokenizerFast): + if any(len(values) > 0 for values in multi_modal_data.values()): + logger.warning( + "There is multi_modal_data but you are not using a processor. Multi-modal data will be ignored." + ) + model_inputs = processing_class(text=[raw_prompt], return_tensors="pt") + elif isinstance(processing_class, ProcessorMixin): + images = images if len(images := multi_modal_data.get("image", [])) > 0 else None + videos = videos if len(videos := multi_modal_data.get("video", [])) > 0 else None + audio = audio if len(audio := multi_modal_data.get("audio", [])) > 0 else None + model_inputs = build_multimodal_processor_inputs( + processing_class, + text=[raw_prompt], + images=images, + videos=videos, + audio=audio, + mm_processor_kwargs=mm_processor_kwargs, + ) + else: + raise ValueError(f"Unsupported processing class type: {type(processing_class)}") + + if hasattr(model_inputs, "convert_to_tensors"): + model_inputs = model_inputs.convert_to_tensors("pt") + model_inputs = dict(model_inputs) + if return_dict: + return model_inputs + else: + return model_inputs["input_ids"] + + @staticmethod + def _get_position_ids( + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + input_ids: torch.Tensor, + attention_mask: torch.Tensor, + multi_modal_inputs: Optional[dict[str, torch.Tensor]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + ) -> torch.Tensor: + # special case for qwen2vl + is_qwen2vl = ( + hasattr(processing_class, "image_processor") + and "Qwen2VLImageProcessor" in processing_class.image_processor.__class__.__name__ + ) + if is_qwen2vl: + from verl.models.transformers.qwen2_vl import get_rope_index + + image_grid_thw = video_grid_thw = second_per_grid_ts = None + if multi_modal_inputs: + image_grid_thw = multi_modal_inputs.get("image_grid_thw") + video_grid_thw = multi_modal_inputs.get("video_grid_thw") + second_per_grid_ts = multi_modal_inputs.get("second_per_grid_ts") + + assert input_ids.dim() == 2 and input_ids.shape[0] == 1, ( + f"input_ids should be 2D with batch size 1, but got shape {input_ids.shape}" + ) + assert attention_mask.dim() == 2 and attention_mask.shape[0] == 1, ( + f"attention_mask should be 2D with batch size 1, but got shape {attention_mask.shape}" + ) + new_position_ids = get_rope_index( + processing_class, + input_ids=input_ids.squeeze(0), + image_grid_thw=image_grid_thw, + video_grid_thw=video_grid_thw, + second_per_grid_ts=second_per_grid_ts, + attention_mask=attention_mask.squeeze(0), + ) + return new_position_ids # (3, seq_len) + else: + return compute_position_id_with_mask(attention_mask) # (1, seq_len) + + def _update_input_ids( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + new_input_ids: torch.Tensor, + attention_mask: bool, + loss_mask: bool, + new_multi_modal_inputs: Optional[dict[str, torch.Tensor]] = None, + ) -> None: + """ + Update the input_ids, attention_mask, position_ids, and loss_mask of the request in additive manner. + """ + self.input_ids = torch.cat([self.input_ids, new_input_ids], dim=-1) + attention_mask = torch.ones_like(new_input_ids) * int(attention_mask) + self.attention_mask = torch.cat([self.attention_mask, attention_mask], dim=-1) + loss_mask = torch.ones_like(new_input_ids) * int(loss_mask) + self.loss_mask = torch.cat([self.loss_mask, loss_mask], dim=-1) + + if new_multi_modal_inputs: + self._update_multi_modal_inputs(new_multi_modal_inputs) + + new_position_ids = self._get_position_ids( + processing_class, + new_input_ids, + attention_mask, + new_multi_modal_inputs, + mm_processor_kwargs=self.mm_processor_kwargs, + ) + + last_pos = self.position_ids[..., -1:] + new_position_ids = new_position_ids + (last_pos + 1) + + self.position_ids = torch.cat([self.position_ids, new_position_ids], dim=-1) + + assert ( + self.input_ids.shape[-1] + == self.attention_mask.shape[-1] + == self.position_ids.shape[-1] + == self.loss_mask.shape[-1] + ), f"""Request {self.request_id} has different length of {self.input_ids.shape[-1]=}, + {self.attention_mask.shape[-1]=}, {self.position_ids.shape[-1]=}, {self.loss_mask.shape[-1]=}""" + + def _update_multi_modal_inputs(self, new_multi_modal_inputs: dict[str, torch.Tensor]) -> None: + """ + Update the multi_modal_inputs of the request in additive manner. + """ + for key in new_multi_modal_inputs: + input_tensor = new_multi_modal_inputs[key] + self.multi_modal_inputs[key] = ( + torch.cat([self.multi_modal_inputs[key], input_tensor], dim=0) + if key in self.multi_modal_inputs + else input_tensor + ) + + def get_generation_prompt_ids( + self, processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin + ) -> list[int]: + """ + Get the generation prompt ids for rollout engine. + + Because rollout engine(SGLang) requires the ids to be a list, we need to convert the tensor to a list. + """ + generation_prompt_ids = ( + None + if self.input_ids[..., -self.generation_prompt_ids.shape[-1] :].eq(self.generation_prompt_ids).all() + else self.generation_prompt_ids + ) + if generation_prompt_ids is not None: + self._update_input_ids(processing_class, generation_prompt_ids, attention_mask=True, loss_mask=False) + + if self.use_inference_chat_template: + messages = [msg.model_dump() for msg in self.messages] + tools = [tool.model_dump() for tool in self.tool_schemas] if self.tool_schemas else None + generation_prompt_ids = self._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data=self.multi_modal_data, + mm_processor_kwargs=self.mm_processor_kwargs, + tools=tools, + add_generation_prompt=True, + tokenize=True, + ) + return generation_prompt_ids.squeeze(0).tolist() + else: + return self.input_ids.squeeze(0).tolist() + + def add_user_message( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + content: str, + ) -> None: + self.messages.append(Message(role="user", content=content)) + messages = [*BASE_CHAT_HISTORY, self.messages[-1]] + tools = [tool.model_dump() for tool in self.tool_schemas] if self.tool_schemas else None + + # We don't need to pass multi_modal_data here because we don't have any multi-modal data from Engine + # Inference, it is pure text. + content_ids = self._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data={}, + mm_processor_kwargs={}, + tools=tools, + add_generation_prompt=False, + tokenize=True, + )[..., self.base_conv_wo_gen_prompt_end_pos :] + self._update_input_ids(processing_class, content_ids, attention_mask=True, loss_mask=False) + + def add_assistant_message( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + content: str, + content_ids: Optional[torch.Tensor] = None, + tool_calls: Optional[list[OpenAIFunctionToolCall]] = None, + ) -> None: + self.messages.append(Message(role="assistant", content=content, tool_calls=tool_calls)) + if content_ids is None: + messages = [*BASE_CHAT_HISTORY, self.messages[-1]] + tools = [tool.model_dump() for tool in self.tool_schemas] if self.tool_schemas else None + + # We don't need to pass multi_modal_data here because we don't have any multi-modal data from Engine + # Inference, it is pure text. + content_ids = self._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data={}, + mm_processor_kwargs={}, + tools=tools, + add_generation_prompt=False, + tokenize=True, + )[..., self.base_conv_with_gen_prompt_end_pos :] + self._update_input_ids(processing_class, content_ids, attention_mask=True, loss_mask=True) + + def add_tool_response_messages( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + contents: list[ToolResponse], + ) -> None: + if not contents or all(content.is_empty() for content in contents): + return + # We also handle the case when tool returns image + # We require the processing of the image and video to be done at tool.execute() level + delta_multi_modal_data = {key: [] for key in self.multi_modal_keys} + for content in contents: + if content.is_text_only(): + self.messages.append(Message(role="tool", content=content.text)) + else: + content_list = [] + # When we update multi_model_keys, we also need to update this logic + if content.image: + content_list.extend([{"type": "image"} for _ in content.image]) + delta_multi_modal_data["image"].extend(content.image) + if content.video: + content_list.extend([{"type": "video"} for _ in content.video]) + delta_multi_modal_data["video"].extend(content.video) + if content.text: + content_list.append({"type": "text", "text": content.text}) + self.messages.append(Message(role="tool", content=content_list)) + + messages = [*BASE_CHAT_HISTORY, *self.messages[-len(contents) :]] + tools = [tool.model_dump() for tool in self.tool_schemas] if self.tool_schemas else None + + for key in self.multi_modal_keys: + if len(delta_multi_modal_data[key]) > 0: + self.multi_modal_data[key].extend(delta_multi_modal_data[key]) + + # We just passed the new multi-modal data to the chat template to update the input_ids. + content_info = self._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data=delta_multi_modal_data, + mm_processor_kwargs=self.mm_processor_kwargs, + tools=tools, + add_generation_prompt=False, + tokenize=True, + return_dict=True, + ) + content_ids = content_info["input_ids"][..., self.base_conv_wo_gen_prompt_end_pos :] + + # process multi_modal_inputs + multi_modal_inputs = content_info.copy() + multi_modal_inputs.pop("input_ids", None) + multi_modal_inputs.pop("attention_mask", None) + + # chat templates include generation prompt tokens (e.g., "assistant\n") + # So when tool response is added, we need to explicitly remove these tokens. + self._remove_generation_prompt_ids_if_present() + + self._update_input_ids( + processing_class, + content_ids, + attention_mask=True, + loss_mask=False, + new_multi_modal_inputs=multi_modal_inputs, + ) + + def update_metrics(self, metrics: Any, tool_id: str) -> None: + """ + metrics: should be a dict of tools_name -> Any + """ + if self.metrics.get(tool_id) is None: + self.metrics[tool_id] = [] + self.metrics[tool_id].append(metrics) + + def _get_prompt_diffs( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + full_prompt_ids: torch.Tensor, + current_prompt_ids: torch.Tensor, + diff_surrounding_chars: int = 10, + ) -> list[dict[str, Any]]: + """Get differences between full prompt and current prompt with surrounding context. + + This function helps debug tokenization mismatches by showing the differences between + full prompt and current prompt with surrounding context. Instead of just showing + the exact diff, it includes additional tokens before and after to help locate + the issue in the chat template. + + For example, if the actual diff is a newline change from "\n\n" to "\n", with + diff_surrounding_chars the output might look like: + + full_prompt_chunk: "<|im_start|>assistant\n\nI think..." + current_prompt_chunk: "<|im_start|>assistant\nI think..." + + This context makes it much easier to identify where in the chat template the + mismatch occurs. + + Args: + processing_class: The processing class to use for decoding the token IDs + full_prompt_ids: Token IDs from applying chat template to all messages at once + current_prompt_ids: Token IDs from incremental chat template application + diff_surrounding_chars: Number of surrounding characters to include for context (default: 10) + + Returns: + List of dicts containing the differing chunks with context and their indices + """ + full_prompt_ids = full_prompt_ids.squeeze(0) + current_prompt_ids = current_prompt_ids.squeeze(0) + full_prompt = processing_class.decode(full_prompt_ids, skip_special_tokens=False) + current_prompt = processing_class.decode(current_prompt_ids, skip_special_tokens=False) + s = difflib.SequenceMatcher(None, full_prompt, current_prompt, autojunk=False) + diffs = [] + for tag, i1, i2, j1, j2 in s.get_opcodes(): + if tag == "equal": + continue + + # Get the surrounding context for better readability + start_i = max(0, i1 - diff_surrounding_chars) + end_i = min(len(full_prompt), i2 + diff_surrounding_chars) + start_j = max(0, j1 - diff_surrounding_chars) + end_j = min(len(current_prompt), j2 + diff_surrounding_chars) + + diffs.append( + { + "full_prompt_chunk": full_prompt[start_i:end_i], + "current_prompt_chunk": current_prompt[start_j:end_j], + "indices": (start_i, end_i, start_j, end_j), + } + ) + return diffs + + def _remove_generation_prompt_ids_if_present(self) -> None: + """ + Remove generation prompt IDs from input tensors if they are present at the end. + """ + if self.input_ids[..., -self.generation_prompt_ids.shape[-1] :].eq(self.generation_prompt_ids).all(): + self.input_ids = self.input_ids[..., : -self.generation_prompt_ids.shape[-1]] + self.attention_mask = self.attention_mask[..., : -self.generation_prompt_ids.shape[-1]] + self.position_ids = self.position_ids[..., : -self.generation_prompt_ids.shape[-1]] + self.loss_mask = self.loss_mask[..., : -self.generation_prompt_ids.shape[-1]] + + def finalize( + self, + processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin, + reward_scores: dict[str, list[float]], + finish_reason_type: FinishReasonTypeEnum = FinishReasonTypeEnum.STOP, + ) -> None: + self.state = AsyncRolloutRequestStateEnum.COMPLETED + self.reward_scores = reward_scores + + # In case we failed to generate the assistant message and the generation prompt ids were already added to + # input_ids, remove them from the end of input_ids + self._remove_generation_prompt_ids_if_present() + + self.response_ids = self.input_ids[..., self.prompt_ids.shape[-1] :] + + if self.tokenization_sanity_check_mode != TokenizationSanityCheckModeEnum.DISABLE: + # When there is a diff, we log the diffs with diff_surrounding_chars context + diff_surrounding_chars = 10 + + messages = [msg.model_dump() for msg in self.messages] + tools = [tool.model_dump() for tool in self.tool_schemas] if self.tool_schemas else None + full_prompt_info = self._handle_apply_chat_template( + processing_class, + messages, + multi_modal_data=self.multi_modal_data, + mm_processor_kwargs=self.mm_processor_kwargs, + tools=tools, + add_generation_prompt=False, + tokenize=True, + return_dict=True, + ) + full_prompt_ids = full_prompt_info["input_ids"] + + # We must use dict(full_prompt_info) to convert BatchFeature values to a new dict + # because np.array() only keeps the keys for BatchFeature. + full_prompt_multi_modal_inputs = full_prompt_info.copy() + full_prompt_multi_modal_inputs.pop("input_ids", None) + full_prompt_multi_modal_inputs.pop("attention_mask", None) + + for multi_modal_inputs_key in self.multi_modal_inputs: + if multi_modal_inputs_key in full_prompt_multi_modal_inputs: + if ( + not self.multi_modal_inputs[multi_modal_inputs_key] + .eq(full_prompt_multi_modal_inputs[multi_modal_inputs_key]) + .all() + ): + logger.warning( + f"Multi-modal data {multi_modal_inputs_key} is not consistent. " + f"This may lead to unexpected behavior during training. " + f"Please review your multi_modal_inputs logic." + ) + else: + logger.warning( + f"Multi-modal inputs key {multi_modal_inputs_key} is not found in the multi_modal_inputs. " + f"This may lead to unexpected behavior during training." + f"Please review your multi_modal_inputs logic." + ) + + if diffs := self._get_prompt_diffs( + processing_class, full_prompt_ids, self.input_ids, diff_surrounding_chars=diff_surrounding_chars + ): + log_warning = False + if self.tokenization_sanity_check_mode == TokenizationSanityCheckModeEnum.STRICT: + log_warning = True + elif self.tokenization_sanity_check_mode == TokenizationSanityCheckModeEnum.IGNORE_STRIPPABLE: + non_strippable_diffs_exist = any( + d["full_prompt_chunk"].strip() or d["current_prompt_chunk"].strip() for d in diffs + ) + if non_strippable_diffs_exist: + log_warning = True + + if log_warning: + mode_str = f" ({self.tokenization_sanity_check_mode.value})" + logger.warning( + f"Inconsistent training and inference tokenization detected{mode_str}. This may lead to " + f"unexpected behavior during training. Please review your chat template to determine if this " + f"is intentional. For more information, refer to the multiturn README.md." + ) + logger.warning( + f"Showing {diff_surrounding_chars} characters before and after the diffs for context and " + f"better readability." + ) + diff_details_list = [] + for d in diffs: + i1, i2, j1, j2 = d["indices"] + diff_details_list.append( + f"idx {i1}:{i2} -> {j1}:{j2} | full_prompt_chunk: {repr(d['full_prompt_chunk'])} | " + f"current_prompt_chunk: {repr(d['current_prompt_chunk'])}" + ) + diff_details = "\n".join(diff_details_list) + logger.warning(f"Found differences:\n{diff_details}") + + if finish_reason_type == FinishReasonTypeEnum.STOP: + pass + elif finish_reason_type == FinishReasonTypeEnum.LENGTH: + pass + else: + raise ValueError(f"Unsupported finalize finish reason type: {finish_reason_type}") + self.truncate_output_ids(processing_class) + + assert ( + self.input_ids.shape[-1] + == self.attention_mask.shape[-1] + == self.position_ids.shape[-1] + == self.loss_mask.shape[-1] + ), f"""Request {self.request_id} has different length of {self.input_ids.shape[-1]=}, + {self.attention_mask.shape[-1]=}, {self.position_ids.shape[-1]=}, {self.loss_mask.shape[-1]=}""" + + def truncate_output_ids( + self, processing_class: PreTrainedTokenizer | PreTrainedTokenizerFast | ProcessorMixin + ) -> None: + self.input_ids = self.input_ids[..., : self.max_model_len] + self.attention_mask = self.attention_mask[..., : self.max_model_len] + self.position_ids = self.position_ids[..., : self.max_model_len] + self.loss_mask = self.loss_mask[..., : self.max_model_len] + self.response_ids = self.input_ids[..., self.prompt_ids.shape[-1] :][..., : self.max_response_len] + self.response_attention_mask = self.attention_mask[..., self.prompt_attention_mask.shape[-1] :][ + ..., : self.max_response_len + ] + self.response_position_ids = self.position_ids[..., self.prompt_position_ids.shape[-1] :][ + ..., : self.max_response_len + ] + self.response_loss_mask = self.loss_mask[..., self.prompt_loss_mask.shape[-1] :][..., : self.max_response_len] diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/__init__.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..337ec74bce1922b8af69a9879fcbf0f2b382c555 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/__init__.py @@ -0,0 +1,12 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/async_sglang_server.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/async_sglang_server.py new file mode 100644 index 0000000000000000000000000000000000000000..dc930036dbd21f96be653c15e982fcac887751a6 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/async_sglang_server.py @@ -0,0 +1,867 @@ +# Copyright 2023-2024 SGLang Team +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import asyncio +import dataclasses +import json +import logging +import os +import secrets +from pathlib import Path +from typing import Any, Optional + +import ray +import sglang +import sglang.srt.entrypoints.engine +import torch +from packaging import version +from ray.actor import ActorHandle +from sglang.srt.entrypoints.http_server import ( + ServerArgs, + _GlobalState, + app, + set_global_state, +) +from sglang.srt.managers.io_struct import ( + ContinueGenerationReqInput, + GenerateReqInput, + PauseGenerationReqInput, + ReleaseMemoryOccupationReqInput, + ResumeMemoryOccupationReqInput, +) +from sglang.srt.managers.tokenizer_manager import ServerStatus + +from verl.plugin.platform import get_platform +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.device import get_visible_devices_keyword +from verl.utils.net_utils import get_free_port, is_valid_ipv6_address +from verl.utils.profiler import DistProfiler, build_sglang_profiler_args +from verl.utils.tracking import RLInsightLogger +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.replica import RolloutMode, RolloutReplica, TokenOutput +from verl.workers.rollout.sglang_rollout.sglang_rollout import _set_envs_and_config +from verl.workers.rollout.sglang_rollout.utils import SGLANG_LORA_NAME +from verl.workers.rollout.utils import get_max_position_embeddings, run_uvicorn + +logger = logging.getLogger(__file__) +logger.setLevel(logging.INFO) + +visible_devices_keyword = get_visible_devices_keyword() + + +def _extract_prompt_logprobs_sglang( + meta_info: dict, + num_prompt_logprobs: int, + sequence_length: int, + result_dict: dict[str, list], +) -> None: + """Shape SGLang input-logprobs into the vLLM ``extract_prompt_logprobs`` contract. + Populates ``result_dict`` with two ``[sequence_length, max(num_prompt_logprobs, 1)]`` + lists — ``prompt_ids`` and ``prompt_logprobs`` — so the distillation teacher + consumer in ``teacher_manager.AsyncTeacherLLMServerManager`` can treat vLLM and + SGLang teachers interchangeably. + SGLang returns input logprobs with length ``S == len(input_ids)`` whose first + entry has ``logprob=None`` (no predicting context). That matches the vLLM + convention, so we skip entry 0 and append a trailing dummy row to keep the + total length equal to the consumer's ``len(sequence_ids)`` assertion. + """ + input_token_logprobs = meta_info.get("input_token_logprobs") or [] + if num_prompt_logprobs > 0: + input_top_logprobs = meta_info.get("input_top_logprobs") or [] + prompt_ids_ls: list[list[int]] = [] + prompt_logprobs_ls: list[list[float]] = [] + # Entry 0 has logprob=None (no predicting context); skip it, matching vLLM. + for position in range(1, len(input_token_logprobs)): + if num_prompt_logprobs == 0: + logprob, token_id, _ = input_token_logprobs[position] + prompt_ids_ls.append([int(token_id)]) + prompt_logprobs_ls.append([float(logprob)]) + else: + top_entries = input_top_logprobs[position] + # SGLang returns ranked best-first; we preserve that ordering so rank + # 0 is the top-1 token, matching the vLLM extractor's rank-1 slot. + ids = [int(tok_id) for _, tok_id, _ in top_entries] + logprobs = [float(logprob) for logprob, _, _ in top_entries] + assert len(ids) == num_prompt_logprobs, ( + f"SGLang returned {len(ids)} top logprobs at position {position}, expected {num_prompt_logprobs}." + ) + prompt_ids_ls.append(ids) + prompt_logprobs_ls.append(logprobs) + # Trailing dummy row so total length == len(sequence_ids), matching vLLM. + pad_width = max(num_prompt_logprobs, 1) + prompt_ids_ls.append([0] * pad_width) + prompt_logprobs_ls.append([0.0] * pad_width) + assert len(prompt_ids_ls) == sequence_length, ( + f"SGLang prompt_logprobs length ({len(prompt_ids_ls)}) does not match " + f"sequence length ({sequence_length}); check logprob_start_len=0 invariant." + ) + result_dict["prompt_ids"] = prompt_ids_ls + result_dict["prompt_logprobs"] = prompt_logprobs_ls + + +class SGLangHttpServer: + """SGLang http server in single node, this is equivalent to launch server with command line: + ``` + python -m sglang.launch_server --node-rank 0 --nnode 1 ... + ``` + + Args: + config (DictConfig): full config. + rollout_mode (RolloutMode): rollout mode. + replica_rank (int): replica rank, a replica may contain multiple nodes. + node_rank (int): node rank. + nnodes (int): number of nodes. + cuda_visible_devices (str): cuda visible devices. + """ + + def __init__( + self, + config: RolloutConfig, + model_config: HFModelConfig, + rollout_mode: RolloutMode, + workers: list[ActorHandle], + replica_rank: int, + node_rank: int, + nnodes: int, + cuda_visible_devices: str, + base_gpu_id: int, + disaggregation_role: str = "null", + disaggregation_bootstrap_port: Optional[int] = None, + ): + print( + f"SGLang http server: {rollout_mode=}, {replica_rank=}, {node_rank=}, " + f"{nnodes=}, {cuda_visible_devices=}, role={disaggregation_role}" + ) + os.environ[visible_devices_keyword] = cuda_visible_devices + + assert disaggregation_role in ("null", "prefill", "decode"), ( + f"disaggregation_role must be 'null'|'prefill'|'decode', got {disaggregation_role!r}" + ) + self._disaggregation_role = disaggregation_role + self._disaggregation_bootstrap_port = disaggregation_bootstrap_port + + self.config: RolloutConfig = omega_conf_to_dataclass(config) + self.model_config: HFModelConfig = omega_conf_to_dataclass(model_config, dataclass_type=HFModelConfig) + max_position_embeddings = get_max_position_embeddings(self.model_config.hf_config) + if self.config.max_model_len is None: + self.config.max_model_len = max_position_embeddings + else: + if self.config.max_model_len > max_position_embeddings: + raise ValueError( + f"max_model_len ({self.config.max_model_len}) should be less than or equal to " + f"max_position_embeddings ({max_position_embeddings})" + ) + self.rollout_mode = rollout_mode + self.workers = workers + + self.replica_rank = replica_rank + self.node_rank = node_rank + self.nnodes = nnodes + self.base_gpu_id = base_gpu_id + # model weights version, set by ServerAdapter when update weights. + self.global_steps = None + + # PD peer linkage populated post-launch by SGLangPDReplica.set_pd_peer. + self._pd_decode_peers: list[ActorHandle] = [] + self._pd_bootstrap_host: Optional[str] = None + + if self.rollout_mode != RolloutMode.HYBRID and self.config.load_format == "dummy": + logger.warning(f"rollout mode is {self.rollout_mode}, load_format is dummy, set to auto") + self.config.load_format = "auto" + + # used for http server + self._server_address = ray.util.get_node_ip_address().strip("[]") + self._server_port = None + + # used for controlling sglang server profiler + profiler_config = self.config.profiler + tool_config = None + if profiler_config is not None: + if profiler_config.tool in ["torch", "npu"]: + tool_config = omega_conf_to_dataclass((profiler_config.tool_config or {}).get(profiler_config.tool)) + else: + logger.warning(f"agent loop only support torch and npu profiler, got {profiler_config.tool}") + profiler_config = None + self.profiler_controller = DistProfiler(self.replica_rank, config=profiler_config, tool_config=tool_config) + + # For multi-node, we need dist_init_addr so nodes can coordinate NCCL init. + # For single-node, let SGLang handle port selection internally via nccl_port, + # which also avoids port conflicts. + self._master_address = None + self._master_port = None + self._master_sock = None + if self.nnodes > 1 and self.node_rank == 0: + self._master_address = self._server_address + self._master_port, self._master_sock = get_free_port(self._server_address, with_alive_sock=True) + logger.info( + f"SGLangHttpServer, replica_rank: {self.replica_rank}, " + f"master address: {self._master_address}, port: {self._master_port}" + ) + + def get_master_address(self): + """Get master address and port for init NCCL process group.""" + return self._master_address, self._master_port + + def get_server_address(self): + """Get http server address and port.""" + assert self._server_port is not None, "http server is not launched, port is None" + return self._server_address, self._server_port + + async def set_pd_peer(self, decode_peers: list, bootstrap_host: str): + assert isinstance(decode_peers, list) and decode_peers + self._pd_decode_peers = list(decode_peers) + self._pd_bootstrap_host = bootstrap_host + + def _prepend_cu12_lib_to_ld_library_path(self) -> None: + """Ray runtime_env.pip installs cu12 into a transient venv, not the usual + site-packages. NIXL's UCX plugin dlopens libcudart.so.12 from + LD_LIBRARY_PATH; wrong path ⇒ scheduler subprocess dies with SIGABRT.""" + try: + import nvidia.cuda_runtime as cu12_mod + except ImportError as e: + logger.warning( + f"nvidia.cuda_runtime not importable: {e}. " + f"NIXL may fail with 'libcudart.so.12: cannot open shared object'." + ) + return + cu12_lib = str(Path(cu12_mod.__file__).parent / "lib") + if not os.path.isdir(cu12_lib): + return + existing = os.environ.get("LD_LIBRARY_PATH", "") + if cu12_lib in existing.split(":"): + return + os.environ["LD_LIBRARY_PATH"] = f"{cu12_lib}:{existing}" if existing else cu12_lib + logger.info(f"Prepended {cu12_lib} to LD_LIBRARY_PATH for NIXL/UCX dlopen.") + + async def launch_server(self, master_address: str = None, master_port: int = None): + if self._disaggregation_role != "null": + self._prepend_cu12_lib_to_ld_library_path() + + if self.nnodes > 1: + if self.node_rank != 0: + assert master_address and master_port, "non-master node should provide master address and port" + self._master_address = master_address + self._master_port = master_port + + engine_kwargs = self.config.get("engine_kwargs", {}).get("sglang", {}) or {} + attention_backend = engine_kwargs.pop("attention_backend", None) + mm_attention_backend = engine_kwargs.pop("mm_attention_backend", None) + # Delta checkpoint engines apply sparse weight updates in place through SGLang's + # custom-weight-loader hook; register the verl loader so the update requests' + # load_format resolves inside the TP workers. + custom_weight_loader = list(engine_kwargs.pop("custom_weight_loader", None) or []) + ce_backend = str((self.config.get("checkpoint_engine", None) or {}).get("backend", "")) + if ce_backend == "delta_sharded": + from verl.workers.rollout.sglang_rollout.delta_loader import LOADER_FQN + + if LOADER_FQN not in custom_weight_loader: + custom_weight_loader.append(LOADER_FQN) + if attention_backend is None: + if torch.version.hip is not None: + attention_backend = "aiter" + elif version.parse(sglang.__version__) >= version.parse("0.5.12"): + # FA3 CUDA-graph capture is broken on sglang>=0.5.12 (#22800); + # default to flashinfer (users can opt into fa4 via engine_kwargs). + attention_backend = "flashinfer" + else: + attention_backend = "fa3" + # mm_attention_backend uses a different name space than attention_backend + # (e.g. text "flashinfer" vs vision "flashinfer_cudnn"), so don't mirror it. + # Leave None to let sglang's VisionAttention auto-pick per device + # (triton_attn on Ada, fa3 on Hopper, fa4 on Blackwell). + quantization = self.config.get("quantization", None) + if quantization is not None: + if quantization == "fp8": + from verl.utils.sglang.sglang_fp8_utils import build_sglang_fp8_quant_config + + assert version.parse(sglang.__version__) >= version.parse("0.5.5"), ( + "sglang>=0.5.5 is required for FP8 quantization" + ) + fp8_block_quant_kwargs = build_sglang_fp8_quant_config(self.model_config.hf_config) + else: + raise ValueError(f"Currently only support fp8 quantization, got: {quantization}") + infer_tp = self.config.tensor_model_parallel_size * self.config.data_parallel_size + args = { + "model_path": self.model_config.local_path, + "dtype": self.config.dtype, + "mem_fraction_static": self.config.gpu_memory_utilization, + "disable_cuda_graph": self.config.enforce_eager, + "enable_memory_saver": True, + "base_gpu_id": self.base_gpu_id, + "gpu_id_step": 1, + "tp_size": infer_tp, + "dp_size": self.config.data_parallel_size, + "ep_size": self.config.expert_parallel_size, + "node_rank": self.node_rank, + "load_format": self.config.load_format, + "nnodes": self.nnodes, + "trust_remote_code": self.model_config.trust_remote_code, + "max_running_requests": self.config.get("max_num_seqs", None), + "log_level": "error", + "mm_attention_backend": mm_attention_backend, + "attention_backend": attention_backend, + "skip_tokenizer_init": self.config.skip_tokenizer_init, + "skip_server_warmup": True, + "quantization": quantization, + "json_model_override_args": json.dumps({"quantization_config": fp8_block_quant_kwargs}) + if quantization == "fp8" + else json.dumps({}), + "custom_weight_loader": custom_weight_loader or None, + **engine_kwargs, + } + + # update lora-related args + if self.model_config.lora_rank > 0: + args.update( + { + "enable_lora": True, + "max_lora_rank": self.model_config.lora_rank, + "lora_target_modules": self.model_config.target_modules, + } + ) + # Only set dist_init_addr for multi-node; for single-node, let SGLang + # handle port selection internally via nccl_port to avoid conflicts. + if self.nnodes > 1: + dist_init_addr = ( + f"[{self._master_address}]:{self._master_port}" + if is_valid_ipv6_address(self._master_address) + else f"{self._master_address}:{self._master_port}" + ) + args["dist_init_addr"] = dist_init_addr + + if self.config.prometheus.enable or RLInsightLogger.enabled(): + if self.config.prometheus.served_model_name: + # Extract model name from path if it's a full path + served_model_name = self.config.prometheus.served_model_name + if "/" in served_model_name: + # If it's a full path, extract the last part as model name + served_model_name = served_model_name.split("/")[-1] + args["served_model_name"] = served_model_name + + # start sglang metrics + args["enable_metrics"] = True + + # enable_weights_cpu_backup is supported in sglang>=0.5.3 + if "enable_weights_cpu_backup" in [f.name for f in dataclasses.fields(ServerArgs)]: + # HYBRID mode also needs CPU weight backup so that: + # 1. sleep() can release GPU weights to free memory for the training engine. + # 2. naive update_weights() can call resume(tags=["weights"]) to reload weights + # from CPU before applying the latest trainer weights via IPC. + # Without this, sleep() releases GPU memory but update_weights() cannot restore + # the weight buffers, causing OOM when training tries to use the freed memory. + enable_weights_cpu_backup = ( + True + if self.rollout_mode in (RolloutMode.COLOCATED, RolloutMode.HYBRID) or self.model_config.lora_rank > 0 + else False + ) + args["enable_weights_cpu_backup"] = enable_weights_cpu_backup + + if self._disaggregation_role != "null": + disagg = self.config.disaggregation + args["disaggregation_mode"] = self._disaggregation_role + args["disaggregation_transfer_backend"] = disagg.transfer_backend + # Bind HTTP + bootstrap to the routable node IP; default 127.0.0.1 + # makes decode-to-prefill bootstrap connection fail across nodes. + args["host"] = self._server_address + if self._disaggregation_bootstrap_port is not None: + args["disaggregation_bootstrap_port"] = self._disaggregation_bootstrap_port + if disagg.decode_tensor_model_parallel_size is not None: + args["disaggregation_decode_tp"] = disagg.decode_tensor_model_parallel_size + if disagg.ib_device is not None: + args["disaggregation_ib_device"] = disagg.ib_device + + if self.config.enable_rollout_routing_replay: + args.update({"enable_return_routed_experts": True}) + + # mtp + if self.config.mtp is not None and self.config.mtp.enable and self.config.mtp.enable_rollout: + # Enable weights CPU backup for sglang >= 0.5.6 + if version.parse(sglang.__version__) < version.parse("0.5.6"): + raise ValueError(f"sglang version {sglang.__version__} is not supported for MTP rollout") + + args["speculative_algorithm"] = self.config.mtp.speculative_algorithm + args["speculative_num_steps"] = self.config.mtp.speculative_num_steps + args["speculative_eagle_topk"] = self.config.mtp.speculative_eagle_topk + args["speculative_num_draft_tokens"] = self.config.mtp.speculative_num_draft_tokens + + args["enable_weights_cpu_backup"] = True + args["enable_draft_weights_cpu_backup"] = True + + # NOTE: We can't directly call SGLang's launch_server since it's not an async function. + # https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/http_server.py + sglang.srt.entrypoints.engine._set_envs_and_config = _set_envs_and_config + os.environ["SGLANG_BLOCK_NONZERO_RANK_CHILDREN"] = "0" + server_args = ServerArgs(**args) + # For SGLang main branch or version >= 0.5.10 + # The latest main branch of SGLang has wrapped the _launch_subprocesses function inside the Engine class + if version.parse(sglang.__version__) >= version.parse("0.5.10"): + from sglang.srt.entrypoints.http_server import Engine + + self.tokenizer_manager, self.template_manager, self.scheduler_info, *_ = Engine._launch_subprocesses( + server_args=server_args, + init_tokenizer_manager_func=sglang.srt.entrypoints.engine.init_tokenizer_manager, + run_scheduler_process_func=sglang.srt.entrypoints.engine.run_scheduler_process, + run_detokenizer_process_func=sglang.srt.entrypoints.engine.run_detokenizer_process, + ) + elif version.parse(sglang.__version__) >= version.parse("0.5.7"): + from sglang.srt.entrypoints.http_server import _launch_subprocesses + + self.tokenizer_manager, self.template_manager, self.scheduler_info, *_ = _launch_subprocesses( + server_args=server_args, + init_tokenizer_manager_func=sglang.srt.entrypoints.engine.init_tokenizer_manager, + run_scheduler_process_func=sglang.srt.entrypoints.engine.run_scheduler_process, + run_detokenizer_process_func=sglang.srt.entrypoints.engine.run_detokenizer_process, + ) + else: + from sglang.srt.entrypoints.http_server import _launch_subprocesses + + self.tokenizer_manager, self.template_manager, self.scheduler_info, *_ = _launch_subprocesses( + server_args=server_args + ) + + # In multi-node cases, non-zero rank nodes should not launch http server. + if self.node_rank > 0: + return + + set_global_state( + _GlobalState( + tokenizer_manager=self.tokenizer_manager, + template_manager=self.template_manager, + scheduler_info=self.scheduler_info, + ) + ) + app.is_single_tokenizer_mode = True + + # Set warmup_thread_{kw}args to avoid AttributeError in lifespan function + app.server_args = server_args + app.warmup_thread_kwargs = {"server_args": server_args} + app.warmup_thread_args = (server_args, None, None) + + # Manually add Prometheus middleware before starting server + # This ensures /metrics endpoint is available immediately + if server_args.enable_metrics: + from sglang.srt.utils.common import add_prometheus_middleware + + add_prometheus_middleware(app) + + self._server_port, self._server_task = await run_uvicorn(app, server_args, self._server_address) + self.tokenizer_manager.server_status = ServerStatus.Up + + async def wake_up(self): + if self.node_rank != 0: + return + + if self.rollout_mode == RolloutMode.HYBRID: + # In hybrid mode, rollout is wake up in `update_weights` + raise ValueError(f"wake_up not support rollout_mode {self.rollout_mode}") + elif self.rollout_mode == RolloutMode.COLOCATED: + # Directly call engine to wake up without sync weights. + obj = ResumeMemoryOccupationReqInput(tags=["kv_cache", "weights"]) + await self.tokenizer_manager.resume_memory_occupation(obj, None) + await self.tokenizer_manager.flush_cache() + elif self.rollout_mode == RolloutMode.STANDALONE: + # In standalone mode, resume kv_cache if free_cache_engine is enabled + obj = ResumeMemoryOccupationReqInput(tags=["kv_cache"]) + await self.tokenizer_manager.resume_memory_occupation(obj, None) + await self.tokenizer_manager.flush_cache() + + @property + def lora_as_adapter(self) -> bool: + return ( + self.model_config.lora_rank > 0 or self.model_config.lora.get("rank", 0) > 0 + ) and not self.model_config.lora.get("merge", False) + + async def sleep(self): + if self.node_rank != 0 or not self.config.free_cache_engine: + return + + # When using LoRA as adapter (merge=False), only release kv_cache — + # keep base weights in GPU so we only need to sync adapter deltas. + # Mirrors the vLLM sleep() pattern in vllm_async_server.py. + if self.lora_as_adapter: + tags = ["kv_cache"] + else: + tags = ["kv_cache", "weights"] + + if self.rollout_mode == RolloutMode.HYBRID: + obj = ReleaseMemoryOccupationReqInput(tags=tags) + await self.tokenizer_manager.release_memory_occupation(obj, None) + elif self.rollout_mode == RolloutMode.COLOCATED: + obj = ReleaseMemoryOccupationReqInput(tags=tags) + await self.tokenizer_manager.release_memory_occupation(obj, None) + elif self.rollout_mode == RolloutMode.STANDALONE: + # In standalone mode, resume kv_cache if free_cache_engine is enabled + obj = ReleaseMemoryOccupationReqInput(tags=["kv_cache"]) + await self.tokenizer_manager.release_memory_occupation(obj, None) + + async def clear_kv_cache(self): + if self.node_rank == 0: + await self.tokenizer_manager.flush_cache() + + async def release_kv_cache(self): + """Release only kv_cache GPU memory, keeping model weights intact.""" + if self.node_rank != 0 or not self.config.free_cache_engine: + return + obj = ReleaseMemoryOccupationReqInput(tags=["kv_cache"]) + await self.tokenizer_manager.release_memory_occupation(obj, None) + + async def resume_kv_cache(self): + """Restore kv_cache GPU memory after a weight sync. Counterpart to release_kv_cache().""" + if self.node_rank != 0 or not self.config.free_cache_engine: + return + obj = ResumeMemoryOccupationReqInput(tags=["kv_cache"]) + await self.tokenizer_manager.resume_memory_occupation(obj, None) + await self.tokenizer_manager.flush_cache() + + async def generate( + self, + prompt_ids: torch.Tensor, + sampling_params: dict[str, Any], + request_id: str, + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + bootstrap_host: Optional[str] = None, + bootstrap_port: Optional[int] = None, + bootstrap_room: Optional[int] = None, + ) -> TokenOutput: + # PD top-level dispatch: prefill mints a bootstrap_room and fans out + # paired local-prefill + remote-decode calls; decode returns the tokens + # (prefill only materialises KV and pushes via NIXL). Random peer + # choice avoids systematic skew from heavy-tailed RL prompt lengths. + if self._disaggregation_role == "prefill" and self._pd_decode_peers and bootstrap_room is None: + room = secrets.randbits(63) + decode_peer = self._pd_decode_peers[secrets.randbelow(len(self._pd_decode_peers))] + prefill_coro = self.generate( + prompt_ids, + dict(sampling_params), + f"{request_id}_P", + image_data=image_data, + video_data=video_data, + bootstrap_host=self._pd_bootstrap_host, + bootstrap_port=self._disaggregation_bootstrap_port, + bootstrap_room=room, + ) + decode_coro = decode_peer.generate.remote( + prompt_ids, + dict(sampling_params), + f"{request_id}_D", + image_data=image_data, + video_data=video_data, + bootstrap_host=self._pd_bootstrap_host, + bootstrap_port=self._disaggregation_bootstrap_port, + bootstrap_room=room, + ) + _, decode_output = await asyncio.gather(prefill_coro, decode_coro) + return decode_output + + # TODO(@wuxibin): switch to `/generate` http endpoint once multi-modal support ready. + max_possible_tokens = self.config.max_model_len - len(prompt_ids) - 1 + + if max_possible_tokens < 0: + raise ValueError( + f"Prompt length ({len(prompt_ids)}) exceeds the model's maximum context length " + f"({self.config.max_model_len})." + ) + + if "max_new_tokens" in sampling_params: + max_new_tokens = sampling_params.pop("max_new_tokens") + elif "max_tokens" in sampling_params: + # support vllm-style 'max_tokens' param + max_new_tokens = sampling_params.pop("max_tokens") + else: + # Cap max_tokens by response_length to ensure tensor alignment, + # and by remaining budget to prevent OOM in multi-turn rollouts. + max_new_tokens = min( + self.config.response_length, self.config.prompt_length + self.config.response_length - len(prompt_ids) + ) + + # Clamp max_new_tokens to the valid range [0, max_possible_tokens] + max_new_tokens = max(0, min(max_new_tokens, max_possible_tokens)) + + assert max_new_tokens <= max_possible_tokens, ( + f"max_new_tokens {max_new_tokens} exceeds available context space {max_possible_tokens}" + ) + sampling_params["max_new_tokens"] = max_new_tokens + return_logprob = sampling_params.pop("logprobs", False) + + # vLLM-style "prompt_logprobs=K" from the distillation teacher: request + # input-token logprobs for every position (top-K when K>0, sampled-token + # logprob only when K==0). Translate to SGLang's per-request logprob API. + prompt_logprobs = sampling_params.pop("prompt_logprobs", None) + if prompt_logprobs is not None: + return_logprob = True + + request = { + "rid": request_id, + "input_ids": prompt_ids, + "sampling_params": sampling_params, + "return_logprob": return_logprob, + "image_data": image_data, + # TODO: support video input for sglang + # video_data=video_data, + } + + if prompt_logprobs is not None: + request["logprob_start_len"] = 0 + if prompt_logprobs > 0: + request["top_logprobs_num"] = prompt_logprobs + + if self.config.enable_rollout_routing_replay: + request.update({"return_routed_experts": True}) + + # SGLang's scheduler rejects disagg-mode requests without bootstrap_room. + if bootstrap_room is not None: + request["bootstrap_host"] = bootstrap_host + request["bootstrap_port"] = bootstrap_port + request["bootstrap_room"] = bootstrap_room + + generate_request = GenerateReqInput(**request) + + # Add lora request + if self.model_config.lora_rank > 0: + generate_request.lora_path = SGLANG_LORA_NAME + + with RLInsightLogger.trace_state("sglang_generate", state_lane_id=f"replica_{self.replica_rank}"): + output = await self.tokenizer_manager.generate_request(generate_request, None).__anext__() + meta_info = output.get("meta_info", {}) + finish_reason = meta_info.get("finish_reason") + finish_reason = finish_reason["type"] if finish_reason else None + if return_logprob: + token_ids = list(output.get("output_ids", [])) + output_token_logprobs = meta_info.get("output_token_logprobs") or [] + if output_token_logprobs and len(output_token_logprobs) == len(token_ids): + log_probs = [float(log_prob) for log_prob, _, _ in output_token_logprobs] + else: + # SGLang may return mismatched lengths (e.g. max_new_tokens=0 + # produces a phantom logprob entry with empty output_ids), or + # an abort may leave an empty logprob payload. + if len(output_token_logprobs) != len(token_ids): + logger.error( + f"output_token_logprobs length ({len(output_token_logprobs)}) != " + f"output_ids length ({len(token_ids)}) for request {request_id}" + ) + token_ids = [] + log_probs = [] + else: + token_ids = output["output_ids"] + log_probs = None + + routed_experts = None + if self.config.enable_rollout_routing_replay: + if self.config.skip_tokenizer_init: + routed_experts = output.get("meta_info", {}).get("routed_experts", None) + else: + from sglang.srt.layers.moe.routed_experts_capturer import extract_routed_experts_from_meta_info + + hf_config = self.model_config.hf_config + if not hasattr(hf_config, "num_hidden_layers") or not hasattr(hf_config, "num_experts_per_tok"): + raise AttributeError( + "enable_rollout_routing_replay is set, but hf_config is missing " + "'num_hidden_layers' or 'num_experts_per_tok'. This feature requires an MoE model " + "configuration that defines these attributes." + ) + routed_experts = extract_routed_experts_from_meta_info(output).reshape( + -1, hf_config.num_hidden_layers, hf_config.num_experts_per_tok + ) + + extra_fields = {"global_steps": self.global_steps} + if prompt_logprobs is not None: + _extract_prompt_logprobs_sglang( + meta_info=meta_info, + num_prompt_logprobs=prompt_logprobs, + sequence_length=len(prompt_ids), + result_dict=extra_fields, + ) + + # Re-key backend spec-decoding stats to the rollout-common names. + if self.config.mtp is not None and self.config.mtp.enable and self.config.mtp.enable_rollout: + extra_fields["spec_num_draft_tokens"] = int( + meta_info.get("spec_draft_token_num", self.config.mtp.speculative_num_draft_tokens) + ) + extra_fields["spec_num_accepted_tokens"] = int(meta_info.get("spec_accept_token_num", 0)) + extra_fields["spec_num_verify_steps"] = int(meta_info.get("spec_verify_ct", 0)) + + return TokenOutput( + token_ids=token_ids, + log_probs=log_probs, + routed_experts=routed_experts, + stop_reason=finish_reason, + extra_fields=extra_fields, + ) + + async def set_global_steps(self, global_steps: int): + """Set the global steps of the model weights.""" + self.global_steps = global_steps + + async def abort_all_requests(self): + if self.node_rank != 0: + return + await self.tokenizer_manager.pause_generation(PauseGenerationReqInput(mode="abort")) + + async def resume_generation(self): + if self.node_rank != 0: + return + await self.tokenizer_manager.continue_generation(ContinueGenerationReqInput()) + + async def start_profile(self, **kwargs): + if ( + self.profiler_controller.check_enable() + and self.profiler_controller.check_this_rank() + and self.profiler_controller.is_discrete_mode() + ): + profile_args = build_sglang_profiler_args( + self.profiler_controller.config, self.profiler_controller.tool_config, self.replica_rank + ) + tokenizer_manager = getattr(self, "tokenizer_manager", None) + if tokenizer_manager is None: + return + await tokenizer_manager.start_profile(**profile_args) + + async def stop_profile(self): + if ( + self.profiler_controller.check_enable() + and self.profiler_controller.check_this_rank() + and self.profiler_controller.is_discrete_mode() + ): + tokenizer_manager = getattr(self, "tokenizer_manager", None) + if tokenizer_manager is None: + return + await tokenizer_manager.stop_profile() + + +class SGLangReplica(RolloutReplica): + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: HFModelConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + name_suffix: str = "", + ): + super().__init__( + replica_rank, config, model_config, gpus_per_node, is_reward_model, is_teacher_model, name_suffix + ) + self.server_class = ray.remote(SGLangHttpServer) + + async def launch_servers(self): + """Launch http server in each node.""" + assert len(self.workers) == self.world_size, ( + f"worker number {len(self.workers)} not equal to world size {self.world_size}" + ) + + # get (node_id, CUDA_VISIBLE_DEVICES) of all workers + worker_infos = await asyncio.gather( + *[ + worker.__ray_call__.remote( + lambda self: (ray.get_runtime_context().get_node_id(), os.environ[visible_devices_keyword]) + ) + for worker in self.workers + ] + ) + worker_cuda_visible_devices = [worker_info[1] for worker_info in worker_infos] + worker_node_ids = [worker_info[0] for worker_info in worker_infos] + base_gpu_id = 0 + infer_tp = self.config.tensor_model_parallel_size * self.config.data_parallel_size + replica_world_size = infer_tp * self.config.pipeline_model_parallel_size + if os.environ.get(f"RAY_EXPERIMENTAL_NOSET_{visible_devices_keyword}", None): + logger.warning(f"RAY_EXPERIMENTAL_NOSET_{visible_devices_keyword} is set True!") + base_gpu_id = (0 + self.replica_rank * replica_world_size) % self.gpus_per_node + # create server actor in each node with node affinity and cuda visible devices + for node_rank in range(self.nnodes): + workers = self.workers[ + node_rank * self.gpus_per_replica_node : (node_rank + 1) * self.gpus_per_replica_node + ] + node_cuda_visible_devices_set = worker_cuda_visible_devices[ + node_rank * self.gpus_per_replica_node : (node_rank + 1) * self.gpus_per_replica_node + ] + node_cuda_visible_devices = ",".join( + map( + str, + sorted( + set( + int(device) + for worker_devices_set in node_cuda_visible_devices_set + for device in worker_devices_set.split(",") + if device.strip() + ) + ), + ) + ) + + node_id = worker_node_ids[node_rank * self.gpus_per_replica_node] + if self.is_reward_model: + name = f"sglang_server_reward_{self.replica_rank}_{node_rank}{self.name_suffix}" + elif self.is_teacher_model: + name = f"sglang_server_teacher_{self.replica_rank}_{node_rank}{self.name_suffix}" + else: + name = f"sglang_server_{self.replica_rank}_{node_rank}{self.name_suffix}" + server = self.server_class.options( + scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy( + node_id=node_id, + soft=False, + ), + runtime_env={ + "env_vars": { + **{var: "1" for var in get_platform().ray_noset_envvars()}, + **get_platform().rollout_env_vars(), + } + }, + name=name, + max_concurrency=self.max_concurrency, + ).remote( + config=self.config, + model_config=self.model_config, + rollout_mode=self.rollout_mode, + workers=workers, + replica_rank=self.replica_rank, + node_rank=node_rank, + nnodes=self.nnodes, + cuda_visible_devices=node_cuda_visible_devices, + base_gpu_id=base_gpu_id, + ) + self.servers.append(server) + + # launch http server in each node + master_address, master_port = None, None + if self.nnodes > 1: + master_address, master_port = await self.servers[0].get_master_address.remote() + await asyncio.gather( + *[ + server.launch_server.remote(master_address=master_address, master_port=master_port) + for server in self.servers + ] + ) + + # get http server address from first server + server_address, server_port = await self.servers[0].get_server_address.remote() + self._server_handle = self.servers[0] + self._server_address = ( + f"[{server_address}]:{server_port}" + if is_valid_ipv6_address(server_address) + else f"{server_address}:{server_port}" + ) + + async def abort_all_requests(self): + """Abort all ongoing generation requests on the primary server. + + SGLang control RPCs are only served by the node-rank 0 server for a + multi-node replica, so avoid broadcasting this call to every server. + """ + await self.servers[0].abort_all_requests.remote() + + async def resume_generation(self): + """Resume generation on the primary server after abort_all_requests.""" + await self.servers[0].resume_generation.remote() diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/delta_loader.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/delta_loader.py new file mode 100644 index 0000000000000000000000000000000000000000..fa4cb79b70f706e22cc46e4b47ed288ed226ec7b --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/delta_loader.py @@ -0,0 +1,160 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""In-process sparse delta apply for SGLang, loaded via ``--custom-weight-loader``. + +SGLang's ``update_weights_from_tensor`` supports pluggable loaders: when the +request's ``load_format`` names an import path registered in +``--custom-weight-loader``, SGLang ``dynamic_import``s it **inside every TP +worker process** and calls ``loader(model, named_tensors)``. That is exactly +the hook a sparse delta needs: the delta payload is decoded and applied *in +place* onto SGLang's live weights (masked overwrite of only the changed +positions), so the receiver never stages a full-model mirror anywhere — +peak memory is one decode chunk, independent of model size — and no SGLang +fork or patch is required. + +Wire contract (what the delta checkpoint engine sends per flush): + +* ``__delta_spec__`` — uint8 tensor holding a JSON manifest + ``{"encoding", "params": [DeltaParam-dict...], "checksum"}``. +* ``__positions__`` — uint8 blob of packed positions (per-param slices are + byte offsets ``pos_start:pos_end``; ``indices`` packs little-endian int32 + absolute positions). +* ``__values__`` — the changed values in the flush's (uniform) dtype; + per-param slices are element offsets ``val_start:val_end``. + +Register at server launch (verl config):: + + +actor_rollout_ref.rollout.engine_kwargs.sglang.custom_weight_loader='["verl.workers.rollout.sglang_rollout.delta_loader.apply_delta"]' +""" + +from __future__ import annotations + +import json +import math +from contextlib import contextmanager + +import torch + +# Cap on the densified tensors handed to one model.load_weights call, matching +# SGLang's own delta-apply chunking default. +CHUNK_BYTES = 512 << 20 + +# Import path callers pass as both --custom-weight-loader and load_format. +LOADER_FQN = "verl.workers.rollout.sglang_rollout.delta_loader.apply_delta" + + +def apply_delta(model, named_tensors) -> None: + """Decode one sparse delta flush and masked-apply it onto ``model`` in place.""" + from verl.checkpoint_engine.delta_sync.encode import checksum as _checksum + + tensors = dict(named_tensors) + spec = json.loads(bytes(tensors["__delta_spec__"].cpu().numpy().tobytes()).decode()) + values = tensors["__values__"] + positions = tensors.get("__positions__") + if positions is None: # dense flush (first sync) carries values only + positions = torch.empty(0, dtype=torch.uint8, device=values.device) + + got = _checksum(positions, values) + if got != int(spec["checksum"]): + raise RuntimeError( + f"delta checksum mismatch in sglang loader: got {got}, expected {spec['checksum']}; " + "indicates corruption between sender encode and receiver apply" + ) + + if spec["encoding"] == "dense": + _apply_dense(model, spec["params"], values) + return + + encoding = spec["encoding"] + with _masked_copy(): + chunk: list[tuple[str, torch.Tensor]] = [] + chunk_bytes = 0 + for p in spec["params"]: + t = _decode_one(encoding, positions, values, p) + nbytes = t.numel() * t.element_size() + if chunk and chunk_bytes + nbytes > CHUNK_BYTES: + model.load_weights(chunk) + chunk, chunk_bytes = [], 0 + chunk.append((p["name"], t)) + chunk_bytes += nbytes + if chunk: + model.load_weights(chunk) + + +def _apply_dense(model, params: list[dict], values: torch.Tensor) -> None: + """Apply a dense (full-coverage) flush: plain chunked load, no masking needed.""" + chunk: list[tuple[str, torch.Tensor]] = [] + chunk_bytes = 0 + for p in params: + dtype = getattr(torch, p["dtype"]) + t = values[p["val_start"] : p["val_end"]].to(dtype).view(p["shape"]) + nbytes = t.numel() * t.element_size() + if chunk and chunk_bytes + nbytes > CHUNK_BYTES: + model.load_weights(chunk) + chunk, chunk_bytes = [], 0 + chunk.append((p["name"], t)) + chunk_bytes += nbytes + if chunk: + model.load_weights(chunk) + + +def _decode_one(encoding: str, positions: torch.Tensor, values: torch.Tensor, p: dict) -> torch.Tensor: + """Densify one param's sparse delta into a full-shape NaN-masked tensor. + + ``indices`` positions are reinterpreted via an int32 view (8 B/element + transient) rather than a per-byte int64 unpack (32 B/element), so even a + full-seed flush of a large embedding stays within a few GiB. + """ + numel = math.prod(p["shape"]) + dtype = getattr(torch, p["dtype"]) + flat = torch.full((numel,), float("nan"), dtype=dtype, device=values.device) + vals = values[p["val_start"] : p["val_end"]] + if vals.numel() == 0: + return flat.view(p["shape"]) + + pos_b = positions[p["pos_start"] : p["pos_end"]] + if encoding == "indices": + # pos_start is always int32-aligned (each entry packs nnz * 4 bytes). + idx = pos_b.view(torch.int32).to(torch.int64) + else: + raise ValueError(f"unsupported delta encoding: {encoding!r}") + + flat.index_copy_(0, idx, vals.to(dtype)) + return flat.view(p["shape"]) + + +@contextmanager +def _masked_copy(): + """Temporarily make ``Tensor.copy_`` skip NaN positions in the source. + + SGLang's per-model ``load_weights`` ultimately lands on ``param.copy_(loaded)`` + (possibly on a narrowed TP slice). Under this context a NaN-masked source + overwrites only the changed positions; fully dense sources (e.g. the first + full-seed flush) take the original fast path untouched. + """ + orig_copy = torch.Tensor.copy_ + + def masked_copy_(self, src, *args, **kwargs): + if isinstance(src, torch.Tensor) and src.is_floating_point() and self.shape == src.shape: + mask = ~torch.isnan(src) + if not bool(mask.all()): + self[mask] = src[mask].to(self.dtype) + return self + return orig_copy(self, src, *args, **kwargs) + + torch.Tensor.copy_ = masked_copy_ + try: + yield + finally: + torch.Tensor.copy_ = orig_copy diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/http_server_engine.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/http_server_engine.py new file mode 100644 index 0000000000000000000000000000000000000000..63034857ce7074e83f57fee0024f5a8b8be996ac --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/http_server_engine.py @@ -0,0 +1,973 @@ +# Copyright 2025 z.ai +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# This file is adapted from multiple sources: +# 1. THUDM/slime project +# Original source: https://github.com/THUDM/slime/blob/main/slime/backends/sglang_utils/http_server_engine.py +# Copyright 2025 z.ai +# Licensed under the Apache License, Version 2.0 +# 2. SGLang project +# Original source: https://github.com/sgl-project/sglang/blob/main/python/sglang/srt/entrypoints/http_server_engine.py +# Copyright 2023-2024 SGLang Team +# Licensed under the Apache License, Version 2.0 +# +# Modifications made by z.ai and ModelBest Inc. include but are not limited to: +# - Enhanced error handling and retry logic +# - Added async support with connection pooling +# - Extended functionality for distributed weight updates +# - Improved logging and monitoring capabilities +# - Additional configuration options and optimizations + +"""HTTP Server Engine Adapter for SGLang. + +This module provides HTTP-based adapters for SGLang engines, allowing communication +with SGLang servers through HTTP requests instead of direct engine calls. + +Classes: + HttpServerAdapter: Synchronous HTTP adapter for SGLang engines + AsyncHttpServerAdapter: Asynchronous HTTP adapter for SGLang engines + +Functions: + launch_server_process: Launch and initialize an SGLang HTTP server process +""" + +import asyncio +import logging +import multiprocessing +import os +import time +from contextlib import asynccontextmanager +from typing import Any, Callable, Optional + +import aiohttp +import requests +from sglang.srt.entrypoints.EngineBase import EngineBase +from sglang.srt.entrypoints.http_server import launch_server +from sglang.srt.managers.io_struct import UpdateWeightsFromTensorReqInput +from sglang.srt.server_args import ServerArgs +from sglang.srt.utils import kill_process_tree + +# Configure logger +logger = logging.getLogger(__name__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +# Default configuration constants +DEFAULT_TIMEOUT = 60.0 +DEFAULT_MAX_ATTEMPTS = 3 +DEFAULT_RETRY_DELAY = 2.0 +DEFAULT_MAX_CONNECTIONS = 2000 +DEFAULT_MAX_WAIT_TIME = 300.0 + + +def _read_response(response: requests.Response): + if response.status_code == 204 or not response.content: + return {} + try: + return response.json() + except ValueError: + return { + "content_type": response.headers.get("Content-Type", ""), + "text": response.text, + } + + +async def _read_async_response(resp: aiohttp.ClientResponse) -> dict[str, Any]: + if resp.status == 204 or (resp.content_length == 0): + return {} + + try: + return await resp.json(content_type=None) + except Exception: + try: + text = await resp.text() + except Exception: + return {} + return { + "content_type": (resp.headers.get("Content-Type") or ""), + "text": text, + } + + +def launch_server_process( + server_args: ServerArgs, + timeout: float = DEFAULT_TIMEOUT, + max_wait_time=DEFAULT_MAX_WAIT_TIME, + first_rank_in_node=False, +) -> multiprocessing.Process: + """Launch an SGLang HTTP server process and wait for it to be ready. + + This function starts a new process running an SGLang HTTP server, then waits + for the server to become ready by polling its health endpoints. It ensures + the server is fully operational before returning. + + Args: + server_args (ServerArgs): Server configuration arguments including host, port, and other settings + timeout (float, optional): Timeout for individual HTTP requests during health checks. + Defaults to DEFAULT_TIMEOUT. + + Returns: + multiprocessing.Process: The launched multiprocessing.Process instance + + Raises: + RuntimeError: If the server process terminates unexpectedly during startup or cache flush + TimeoutError: If server fails to become ready within reasonable time (300 seconds) + requests.RequestException: If health check requests fail repeatedly + + Note: + This function will return immediately for non-master nodes (node_rank != 0), + but the process will still be started and returned. + This is for consistency; except for the process obtained by node_rank = 0, + other processes have no actual effect. + """ + p = multiprocessing.Process(target=launch_server, args=(server_args,)) + if server_args.node_rank != 0 or not first_rank_in_node: + logger.info(f"Server process started with PID {p.pid} for node rank {server_args.node_rank}", flush=True) + return p + + p.start() + + base_url = server_args.url() + headers = { + "Content-Type": "application/json; charset=utf-8", + "Authorization": f"Bearer {server_args.api_key}", + } + + # Health check with overall timeout + start_time = time.time() + + with requests.Session() as session: + while time.time() - start_time < max_wait_time: + if not p.is_alive(): + raise RuntimeError("Server process terminated unexpectedly during startup") + + try: + if server_args.is_embedding: + response = session.get(f"{base_url}/health", headers=headers, timeout=timeout) + else: + response = session.get(f"{base_url}/health_generate", headers=headers, timeout=timeout) + if response.status_code == 200: + break + except requests.RequestException as e: + logger.debug(f"Health check failed: {e}") + + time.sleep(2) + else: + p.terminate() + logger.error(f"Server in {base_url} failed to become healthy within timeout period") + raise TimeoutError("Server failed to become healthy within timeout period") + + # Ensure cache is ready + while time.time() - start_time < max_wait_time: + if not p.is_alive(): + raise RuntimeError("Server process terminated unexpectedly during cache flush") + + try: + response = session.get(f"{base_url}/flush_cache", headers=headers, timeout=timeout) + if response.status_code == 200: + break + except requests.RequestException as e: + logger.debug(f"Cache flush check failed: {e}") + + time.sleep(2) + else: + p.terminate() + raise TimeoutError("Server cache flush failed within timeout period") + + return p + + +class HttpServerAdapter(EngineBase): + """HTTP-based adapter for SGLang engines. + + This adapter allows interaction with SGLang engines through HTTP requests + instead of direct engine calls. It launches an HTTP server process and + provides methods to communicate with it via REST API calls. + + You can use this class to launch a server from a HttpServerAdapter instance. + We recommend using this class only when you need to use http server. + Otherwise, you can use Engine directly. + + Attributes: + router_ip (Optional[str]): IP address of the router for worker registration + router_port (Optional[int]): Port of the router for worker registration + server_args (ServerArgs): Server configuration arguments + node_rank (int): Rank of this node in distributed setup + process (multiprocessing.Process): The launched server process + timeout (float): HTTP request timeout in seconds + max_attempts (int): Maximum number of attempts for requests + retry_delay (float): Base delay between retries in seconds + """ + + def __init__( + self, + router_ip: Optional[str] = None, + router_port: Optional[int] = None, + timeout: float = DEFAULT_TIMEOUT, + max_attempts: int = DEFAULT_MAX_ATTEMPTS, + retry_delay: float = DEFAULT_RETRY_DELAY, + first_rank_in_node: bool = False, + max_start_wait_time: float = DEFAULT_MAX_WAIT_TIME, + launch_server: bool = True, + **kwargs: Any, + ) -> None: + """Initialize the HTTP server engine adapter. + + Args: + router_ip (Optional[str], optional): IP address of router for worker registration. + Defaults to None. + router_port (Optional[int], optional): Port of router for worker registration. + Defaults to None. + timeout (float, optional): HTTP request timeout in seconds. + Defaults to DEFAULT_TIMEOUT. + max_attempts (int, optional): Maximum number of retry attempts for failed requests. + Defaults to DEFAULT_MAX_ATTEMPTS. + retry_delay (float, optional): Base delay between retries in seconds. + Defaults to DEFAULT_RETRY_DELAY. + launch_server (bool, optional): Whether to launch the server process. + Defaults to True. + **kwargs (Any): Additional arguments passed to ServerArgs + + Note: + TODO: @ChangyiYang Enable SGLang router for this http server engine + If both router_ip and router_port are provided and this is the master node + (node_rank == 0), the adapter will automatically register with the router. + """ + self.router_ip: Optional[str] = router_ip + self.router_port: Optional[int] = router_port + self.timeout: float = timeout + self.max_attempts: int = max_attempts + self.retry_delay: float = retry_delay + self.server_args: ServerArgs = ServerArgs(**kwargs) + self.node_rank: int = self.server_args.node_rank + self.max_start_wait_time: float = max_start_wait_time + + logger.info( + f"Launch HttpServerAdapter at: {self.server_args.host}:{self.server_args.port} with {first_rank_in_node}" + ) + if launch_server: + self.process: multiprocessing.Process = launch_server_process( + self.server_args, self.timeout, self.max_start_wait_time, first_rank_in_node + ) + + if self.node_rank == 0 and self.router_ip and self.router_port: + self._register_with_router() + + def _register_with_router(self) -> None: + """Register worker with router with error handling. + + This method attempts to register the current worker with a router service. + If registration fails, it logs an error but does not raise an exception, + allowing the server to continue operating without router integration. + + Raises: + Does not raise exceptions - all errors are logged and handled gracefully. + """ + try: + url = f"http://{self.router_ip}:{self.router_port}/add_worker" + params = {"url": f"http://{self.server_args.host}:{self.server_args.port}"} + response = requests.post(url, params=params, timeout=self.timeout) + response.raise_for_status() + logger.info("Successfully registered with router") + except Exception as e: + logger.error(f"Failed to register with router: {e}") + # Don't raise here - server can still work without router + + def _make_request( + self, + endpoint: str, + payload: Optional[dict[str, Any]] = None, + method: str = "POST", + timeout: float = DEFAULT_TIMEOUT, + only_master: bool = True, + ) -> dict[str, Any]: + """Make a HTTP request with retry logic and consistent error handling. + + Args: + endpoint (str): The API endpoint to call (without leading slash) + payload (Optional[Dict[str, Any]], optional): The JSON payload to send. + Defaults to empty dict if None. + method (str, optional): HTTP method to use. Defaults to "POST". + + Returns: + Dict[str, Any]: The JSON response from the server + + Raises: + requests.HTTPError: If the HTTP request fails with a client/server error + RuntimeError: If all retry attempts are exhausted + + Note: + - For non-master nodes (node_rank != 0), returns empty dict immediately + - Uses exponential backoff for retries + - Logs warnings for timeout and connection errors, errors for HTTP errors + """ + if only_master and self.node_rank != 0: + return {} + + url = f"http://{self.server_args.host}:{self.server_args.port}/{endpoint}" + + for attempt in range(self.max_attempts): + try: + if method.upper() == "GET": + response = requests.get(url, timeout=self.timeout) + else: + response = requests.post(url, json=payload or {}, timeout=self.timeout) + + response.raise_for_status() + return _read_response(response) + + except requests.exceptions.Timeout: + logger.warning(f"Request to {endpoint} timed out (attempt {attempt + 1})") + except requests.exceptions.ConnectionError: + logger.warning(f"Connection error for {endpoint} (attempt {attempt + 1})") + except requests.exceptions.HTTPError as e: + logger.error(f"HTTP error for {endpoint}: {e}") + raise + except Exception as e: + logger.error(f"Unexpected error for {endpoint}: {e}") + if attempt == self.max_attempts - 1: + raise + + if attempt < self.max_attempts - 1: + time.sleep(self.retry_delay * (2**attempt)) + + raise RuntimeError(f"Failed to complete request to {endpoint} after {self.max_attempts} attempts") + + def update_weights_from_tensor(self, req: UpdateWeightsFromTensorReqInput) -> dict[str, Any]: + """Update model weights from tensor data. + + The HTTP server will only post meta data, and the real weights will be + copied directly from GPUs. + + Args: + serialized_named_tensors (List[str]): List of serialized tensor data + load_format (Optional[str], optional): Format specification for loading weights. + Defaults to None. + flush_cache (bool, optional): Whether to flush cache after updating weights. + Defaults to False. + + Returns: + Dict[str, Any]: Server response containing update status + + Note: + The model should be on GPUs rather than CPU for this functionality to work properly. + If you encounter issues, ensure your model is loaded on GPU devices rather than CPU. + """ + import base64 + + named_tensors = req.serialized_named_tensors + load_format = req.load_format + flush_cache = req.flush_cache + + if named_tensors: + serialized_named_tensors = [ + base64.b64encode(named_tensor).decode("utf-8") for named_tensor in named_tensors + ] + else: + serialized_named_tensors = [] + + return self._make_request( + "update_weights_from_tensor", + { + "serialized_named_tensors": serialized_named_tensors, + "load_format": load_format, + "flush_cache": flush_cache, + }, + ) + + def shutdown(self) -> None: + """Shutdown the HTTP server and clean up resources. + + This method performs the following cleanup operations: + 1. Unregisters the worker from the router (if configured) + 2. Terminates the server process tree + + All operations are performed with error handling to ensure graceful shutdown + even if individual steps fail. + + Note: + This method should be called when the adapter is no longer needed + to ensure proper cleanup of resources and processes. + """ + # Unregister from router + if self.router_ip and self.router_port: + try: + url = f"http://{self.router_ip}:{self.router_port}/remove_worker" + params = {"url": f"http://{self.server_args.host}:{self.server_args.port}"} + requests.post(url, params=params, timeout=5.0) # Short timeout for shutdown + logger.info("Successfully unregistered from router") + except Exception as e: + logger.warning(f"Failed to unregister from router: {e}") + + # Kill server process + if hasattr(self, "process") and self.process is not None: + try: + kill_process_tree(self.process.pid) + logger.info("Server process terminated") + except Exception as e: + logger.error(f"Failed to terminate server process: {e}") + + def generate( + self, + prompt: Optional[str] = None, + sampling_params: Optional[dict[str, Any]] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + return_logprob: bool = False, + logprob_start_len: Optional[int] = None, + top_logprobs_num: Optional[int] = None, + token_ids_logprob: Optional[list[int]] = None, + lora_path: Optional[str] = None, + custom_logit_processor: Optional[Callable] = None, + ) -> dict[str, Any]: + """Generate text using the SGLang server. + + Args: + prompt (Optional[str], optional): Text prompt for generation. Defaults to None. + sampling_params (Optional[Dict[str, Any]], optional): Parameters controlling + text generation sampling. Defaults to None. + input_ids (Optional[List[int]], optional): Alternative to prompt, direct token IDs input. + Defaults to None. + image_data (Optional[Any], optional): Image data for multimodal generation. + Defaults to None. + return_logprob (bool, optional): Whether to return log probabilities. + Defaults to False. + logprob_start_len (Optional[int], optional): Starting length for log probability calculation. + Defaults to None. + top_logprobs_num (Optional[int], optional): Number of top log probabilities to return. + Defaults to None. + token_ids_logprob (Optional[List[int]], optional): Specific token IDs for + log probability calculation. Defaults to None. + lora_path (Optional[str], optional): Path to LoRA adapter weights. Defaults to None. + custom_logit_processor (Optional[Callable], optional): Custom logit processing function. + Defaults to None. + + Returns: + Dict[str, Any]: Generated text and associated metadata from the server + + Note: + Either prompt or input_ids should be provided, but not both. + The response format depends on the server configuration and parameters. + """ + payload = { + "text": prompt, + "sampling_params": sampling_params, + "input_ids": input_ids, + "image_data": image_data, + "return_logprob": return_logprob, + "logprob_start_len": logprob_start_len, + "top_logprobs_num": top_logprobs_num, + "token_ids_logprob": token_ids_logprob, + "lora_path": lora_path, + "custom_logit_processor": custom_logit_processor, + } + # Filter out None values + payload = {k: v for k, v in payload.items() if v is not None} + + return self._make_request("generate", payload, only_master=False) + + def reward_score( + self, + prompt: Optional[str] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + lora_path: Optional[str] = None, + ) -> dict[str, Any]: + assert self.server_args.is_embedding, "Score is only supported for embedding models" + payload = { + "text": prompt, + "input_ids": input_ids, + "image_data": image_data, + "lora_path": lora_path, + } + # Filter out None values + payload = {k: v for k, v in payload.items() if v is not None} + + return self._make_request("classify", payload, only_master=False) + + def flush_cache(self) -> dict[str, Any]: + """Flush the cache of the server. + + This method repeatedly attempts to flush the server cache until successful. + The flush operation will not return status 200 when there are pending requests. + + Returns: + Dict[str, Any]: Server response indicating cache flush status. + For non-master nodes, returns empty dict. + + Note: + Uses retry logic with limited attempts (max_attempts * 2) to avoid infinite loops. + Each retry includes a delay to allow pending requests to complete. + """ + if self.node_rank != 0: + return {} + + # Use retry logic with limited attempts to avoid infinite loops + for attempt in range(self.max_attempts * 2): # Allow more retries for cache flush + try: + response = requests.get( + f"http://{self.server_args.host}:{self.server_args.port}/flush_cache", timeout=self.timeout + ) + if response.status_code == 200: + return _read_response(response) + except Exception as e: + logger.warning(f"Error flushing cache (attempt {attempt + 1}): {e}") + + time.sleep(self.retry_delay) + + logger.error("Failed to flush cache after maximum attempts") + return {} + + def release_memory_occupation(self, tags: Optional[list[str]] = None) -> dict[str, Any]: + """Release GPU memory occupation temporarily. + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to release. + If None, releases all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory release status + """ + return self._make_request("release_memory_occupation", {"tags": tags}) + + def resume_memory_occupation(self, tags: Optional[list[str]] = None) -> dict[str, Any]: + """Resume GPU memory occupation. + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to resume. + If None, resumes all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory resume status + """ + return self._make_request("resume_memory_occupation", {"tags": tags}) + + def abort_request(self, rid: str = "", abort_all: bool = False) -> dict[str, Any]: + """Abort a request. + + Args: + rid (str): The ID of the request to abort + abort_all (bool, optional): Whether to abort all requests. Defaults to False. + + Returns: + Dict[str, Any]: Server response indicating abort status + """ + return self._make_request("abort_request", {"rid": rid, "abort_all": abort_all}) + + +class AsyncHttpServerAdapter(HttpServerAdapter): + """Asynchronous HTTP-based adapter for SGLang engines. + + This class inherits from HttpServerAdapter and adds async capabilities + for non-blocking HTTP requests to the SGLang server. It provides the same + functionality as the synchronous version but with async/await support. + + The async adapter is useful when you need to make multiple concurrent requests + or integrate with async frameworks. It uses aiohttp for efficient async HTTP + communication and maintains connection pooling for better performance. + + Attributes: + max_connections (int): Maximum number of connections in the connection pool + """ + + def __init__( + self, + router_ip: Optional[str] = None, + router_port: Optional[int] = None, + timeout: float = DEFAULT_TIMEOUT, + max_attempts: int = DEFAULT_MAX_ATTEMPTS, + retry_delay: float = DEFAULT_RETRY_DELAY, + max_connections: int = DEFAULT_MAX_CONNECTIONS, + first_rank_in_node: bool = False, + launch_server: bool = True, + **kwargs: Any, + ) -> None: + """Initialize the async HTTP server engine adapter. + + Args: + router_ip (Optional[str], optional): IP address of router for worker registration. + Defaults to None. + router_port (Optional[int], optional): Port of router for worker registration. + Defaults to None. + timeout (float, optional): HTTP request timeout in seconds. + Defaults to DEFAULT_TIMEOUT. + max_attempts (int, optional): Maximum number of retry attempts for failed requests. + Defaults to DEFAULT_MAX_ATTEMPTS. + retry_delay (float, optional): Base delay between retries in seconds. + Defaults to DEFAULT_RETRY_DELAY. + max_connections (int, optional): Maximum number of connections in the connection pool. + Defaults to DEFAULT_MAX_CONNECTIONS. + launch_server (bool, optional): Whether to launch the server process. + Defaults to True. + **kwargs (Any): Additional arguments passed to ServerArgs + """ + super().__init__( + router_ip, + router_port, + timeout, + max_attempts, + retry_delay, + first_rank_in_node, + launch_server=launch_server, + **kwargs, + ) + self.max_connections: int = max_connections + + @asynccontextmanager + async def _get_session(self) -> aiohttp.ClientSession: + """Context manager for safe session access with proper connection pooling. + + Yields: + aiohttp.ClientSession: Session instance for making HTTP requests + + Note: + This method creates a new session for each request to avoid resource competition + while still maintaining proper connection pooling through the shared connector. + """ + # Create a new session for each request to avoid resource competition + connector = aiohttp.TCPConnector( + limit=self.max_connections, + limit_per_host=self.max_connections // 4, + ttl_dns_cache=300, + use_dns_cache=True, + ) + timeout = aiohttp.ClientTimeout(total=self.timeout) + session = aiohttp.ClientSession(connector=connector, timeout=timeout) + + try: + yield session + finally: + # Always close the session to free up resources + if not session.closed: + await session.close() + + async def _make_async_request( + self, + endpoint: str, + payload: Optional[dict[str, Any]] = None, + method: str = "POST", + timeout: float = DEFAULT_TIMEOUT, + only_master: bool = True, + ) -> dict[str, Any]: + """Make an async HTTP request with retry logic and consistent error handling. + + Args: + endpoint (str): The API endpoint to call (without leading slash) + payload (Optional[Dict[str, Any]], optional): The JSON payload to send. + Defaults to empty dict if None. + method (str, optional): HTTP method to use. Defaults to "POST". + + Returns: + Dict[str, Any]: The JSON response from the server + + Raises: + aiohttp.ClientResponseError: If the HTTP request fails with a client/server error + RuntimeError: If all retry attempts are exhausted + + Note: + - For non-master nodes (node_rank != 0), returns empty dict immediately + - Uses exponential backoff for retries + - Logs warnings for timeout and connection errors, errors for HTTP errors + """ + if only_master and self.node_rank != 0: + return {} + + url = f"http://{self.server_args.host}:{self.server_args.port}/{endpoint}" + + for attempt in range(self.max_attempts): + try: + async with self._get_session() as session: + if method.upper() == "GET": + async with session.get(url, timeout=timeout) as response: + response.raise_for_status() + return await _read_async_response(response) + else: + async with session.post(url, json=payload or {}, timeout=timeout) as response: + response.raise_for_status() + return await _read_async_response(response) + + except asyncio.TimeoutError: + logger.warning(f"Async request to {endpoint} timed out (attempt {attempt + 1})") + except aiohttp.ClientConnectorError: + logger.warning(f"Connection error for {endpoint} (attempt {attempt + 1})") + except aiohttp.ClientResponseError as e: + logger.error(f"HTTP error for {endpoint}: {e}") + raise + except Exception as e: + logger.error(f"Unexpected error for {endpoint}: {e}") + if attempt == self.max_attempts - 1: + raise + + if attempt < self.max_attempts - 1: + await asyncio.sleep(self.retry_delay * (2**attempt)) + + raise RuntimeError(f"Failed to complete async request to {endpoint} after {self.max_attempts} attempts") + + async def release_memory_occupation(self, tags: Optional[list[str]] = None) -> dict[str, Any]: + """Release GPU memory occupation temporarily (async version). + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to release. + If None, releases all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory release status + """ + return await self._make_async_request("release_memory_occupation", {"tags": tags}) + + async def resume_memory_occupation(self, tags: Optional[list[str]] = None) -> dict[str, Any]: + """Resume GPU memory occupation (async version). + + Similar to AsyncEngine, this method handles first-time weight reloading + by calling release_memory_occupation if needed. + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to resume. + If None, resumes all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory resume status + """ + return await self._make_async_request("resume_memory_occupation", {"tags": tags}) + + async def update_weights_from_tensor( + self, + req: UpdateWeightsFromTensorReqInput, + ) -> dict[str, Any]: + """Update model weights from tensor data asynchronously. + + Args: + serialized_named_tensors (List[str]): List of serialized tensor data + load_format (Optional[str], optional): Format specification for loading weights. + Defaults to None. + flush_cache (bool, optional): Whether to flush cache after updating weights. + Defaults to True. + + Returns: + Dict[str, Any]: Server response containing update status + """ + import base64 + + named_tensors = req.serialized_named_tensors + load_format = req.load_format + flush_cache = req.flush_cache + + serialized_named_tensors = [base64.b64encode(named_tensor).decode("utf-8") for named_tensor in named_tensors] + return await self._make_async_request( + "update_weights_from_tensor", + { + "serialized_named_tensors": serialized_named_tensors, + "load_format": load_format, + "flush_cache": flush_cache, + }, + ) + + async def load_lora_adapter_from_tensor(self, req): + return await self._make_async_request( + "load_lora_adapter_from_tensors", + { + "lora_name": req.lora_name, + "config_dict": req.config_dict, + "serialized_tensors": req.serialized_tensors, + }, + ) + + async def unload_lora_adapter(self, lora_name: str): + return await self._make_async_request( + "unload_lora_adapter", + { + "lora_name": lora_name, + }, + ) + + async def available_models(self): + return await self._make_async_request(endpoint="v1/models", method="GET") + + async def flush_cache(self) -> dict[str, Any]: + """Flush the cache of the server asynchronously. + + Similar to the sync version, this method retries until the cache + is successfully flushed. It uses async sleep between retries. + + Returns: + Dict[str, Any]: Server response indicating cache flush status. + For non-master nodes, returns empty dict. + + Note: + Uses retry logic with limited attempts (max_attempts * 4) to avoid infinite loops. + Each retry includes an async delay to allow pending requests to complete. + """ + if self.node_rank != 0: + return {} + + # Use retry logic with limited attempts to avoid infinite loops + for attempt in range(self.max_attempts * 4): # Allow more retries for cache flush + try: + async with self._get_session() as session: + url = f"http://{self.server_args.host}:{self.server_args.port}/flush_cache" + async with session.get(url) as response: + if response.status == 200: + return await _read_async_response(response) + except Exception as e: + logger.warning(f"Error flushing cache (attempt {attempt + 1}): {e}") + + await asyncio.sleep(self.retry_delay) + + logger.error("Failed to flush cache after maximum attempts") + return {} + + async def generate( + self, + prompt: Optional[str] = None, + sampling_params: Optional[dict[str, Any]] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + return_logprob: bool = False, + logprob_start_len: Optional[int] = None, + top_logprobs_num: Optional[int] = None, + token_ids_logprob: Optional[list[int]] = None, + lora_path: Optional[str] = None, + custom_logit_processor: Optional[Callable] = None, + ) -> dict[str, Any]: + """Generate text using the SGLang server asynchronously.""" + logger.info("generate() started") + + payload = { + "text": prompt, + "sampling_params": sampling_params, + "input_ids": input_ids, + "image_data": image_data, + "return_logprob": return_logprob, + "logprob_start_len": logprob_start_len, + "top_logprobs_num": top_logprobs_num, + "token_ids_logprob": token_ids_logprob, + "lora_path": lora_path, + "custom_logit_processor": custom_logit_processor, + } + + # Filter out None values + payload = {k: v for k, v in payload.items() if v is not None} + + # Send request + response = await self._make_async_request("generate", payload, timeout=self.timeout, only_master=False) + + return response + + async def async_generate( + self, + prompt: Optional[str] = None, + sampling_params: Optional[dict[str, Any]] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + return_logprob: bool = False, + logprob_start_len: Optional[int] = None, + top_logprobs_num: Optional[int] = None, + token_ids_logprob: Optional[list[int]] = None, + lora_path: Optional[str] = None, + custom_logit_processor: Optional[Callable] = None, + ) -> dict[str, Any]: + """Async generate method that mirrors AsyncEngine.async_generate interface. + + This method provides compatibility with AsyncEngine's async_generate method + by forwarding the call to the generate method. It ensures API consistency + between direct engine usage and HTTP-based engine usage. + + Args: + prompt (Optional[str], optional): Text prompt for generation. Defaults to None. + sampling_params (Optional[Dict[str, Any]], optional): Parameters controlling + text generation sampling. Defaults to None. + input_ids (Optional[List[int]], optional): Alternative to prompt, direct token IDs input. + Defaults to None. + image_data (Optional[Any], optional): Image data for multimodal generation. + Defaults to None. + return_logprob (bool, optional): Whether to return log probabilities. + Defaults to False. + logprob_start_len (Optional[int], optional): Starting length for log probability calculation. + Defaults to None. + top_logprobs_num (Optional[int], optional): Number of top log probabilities to return. + Defaults to None. + token_ids_logprob (Optional[List[int]], optional): Specific token IDs for + log probability calculation. Defaults to None. + lora_path (Optional[str], optional): Path to LoRA adapter weights. Defaults to None. + custom_logit_processor (Optional[Callable], optional): Custom logit processing function. + Defaults to None. + + Returns: + Dict[str, Any]: Generated text and associated metadata from the server + + Note: + This method is provided for API compatibility with AsyncEngine. + It forwards all calls to the generate method. + """ + return await self.generate( + prompt=prompt, + sampling_params=sampling_params, + input_ids=input_ids, + image_data=image_data, + return_logprob=return_logprob, + logprob_start_len=logprob_start_len, + top_logprobs_num=top_logprobs_num, + token_ids_logprob=token_ids_logprob, + lora_path=lora_path, + custom_logit_processor=custom_logit_processor, + ) + + async def reward_score( + self, + prompt: Optional[str] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + lora_path: Optional[str] = None, + ) -> dict[str, Any]: + logger.info("reward_score() started") + payload = { + "text": prompt, + "input_ids": input_ids, + "image_data": image_data, + "lora_path": lora_path, + } + # Filter out None values + payload = {k: v for k, v in payload.items() if v is not None} + + # Send request + response = await self._make_async_request("classify", payload, timeout=self.timeout, only_master=False) + + return response + + async def async_reward_score( + self, + prompt: Optional[str] = None, + input_ids: Optional[list[int]] = None, + image_data: Optional[Any] = None, + lora_path: Optional[str] = None, + ) -> dict[str, Any]: + return await self.reward_score( + prompt=prompt, + input_ids=input_ids, + image_data=image_data, + lora_path=lora_path, + ) + + async def abort_request(self, rid: str = "", abort_all: bool = False) -> dict[str, Any]: + """Abort a request asynchronously. + + Args: + rid (str): The ID of the request to abort + abort_all (bool, optional): Whether to abort all requests. Defaults to False. + + Returns: + Dict[str, Any]: Server response indicating abort status + """ + return await self._make_async_request("abort_request", {"rid": rid, "abort_all": abort_all}) diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_pd_replica.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_pd_replica.py new file mode 100644 index 0000000000000000000000000000000000000000..cd649c3c97497f494835e8b50265ee2221319571 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_pd_replica.py @@ -0,0 +1,229 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""SGLang PD-disaggregated replica: 1 prefill + N decode servers per replica, +asymmetric TP supported. MVP: prefill_replicas=1, whole replica on one node.""" + +import asyncio +import logging +import os +from dataclasses import replace as _dc_replace +from typing import Optional + +import ray +from omegaconf import DictConfig +from ray.actor import ActorHandle + +from verl.utils.device import is_torch_npu_available +from verl.utils.net_utils import get_free_port, is_valid_ipv6_address +from verl.workers.config import RolloutConfig +from verl.workers.rollout.sglang_rollout.async_sglang_server import ( + SGLangReplica, + visible_devices_keyword, +) + +logger = logging.getLogger(__file__) +logger.setLevel(logging.INFO) + + +class SGLangPDReplica(SGLangReplica): + """Replica that runs SGLang in prefill-decode disaggregated mode.""" + + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: DictConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + ): + super().__init__( + replica_rank, + config, + model_config, + gpus_per_node, + is_reward_model, + is_teacher_model, + ) + disagg = self.config.disaggregation + assert disagg.enabled, "SGLangPDReplica requires rollout.disaggregation.enabled=True" + + if disagg.prefill_replicas != 1: + raise NotImplementedError(f"prefill_replicas=1 only (got {disagg.prefill_replicas})") + self._n_prefill = disagg.prefill_replicas + self._n_decode = disagg.decode_replicas + + self._prefill_tp = self.config.tensor_model_parallel_size + # Inline decode_tp default: OmegaConf/Ray serialization drops dataclass methods. + self._decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else self._prefill_tp + ) + + pd_world_size = self._prefill_tp + self._n_decode * self._decode_tp + if pd_world_size > gpus_per_node: + raise NotImplementedError( + f"PD replica needs {pd_world_size} GPUs but gpus_per_node={gpus_per_node}; " + f"use more replicas to span nodes" + ) + + if self.config.data_parallel_size != 1: + raise NotImplementedError(f"data_parallel_size=1 only (got {self.config.data_parallel_size})") + self.world_size = pd_world_size + self.gpus_per_replica_node = min(self.gpus_per_node, self.world_size) + assert self.world_size % self.gpus_per_replica_node == 0 + self.nnodes = self.world_size // self.gpus_per_replica_node + + self._prefill_servers: list[ActorHandle] = [] + self._decode_servers: list[ActorHandle] = [] + self._prefill_server_address: Optional[str] = None + self._decode_server_addresses: list[str] = [] + self._bootstrap_port: Optional[int] = None + + async def launch_servers(self): + assert len(self.workers) == self.world_size + assert not is_torch_npu_available(check_device=False), "PD on NPU not validated" + + worker_infos = await asyncio.gather( + *[ + worker.__ray_call__.remote( + lambda self: ( + ray.get_runtime_context().get_node_id(), + os.environ[visible_devices_keyword], + ) + ) + for worker in self.workers + ] + ) + + # Hold the bootstrap socket open until prefill binds it; closing earlier + # opens a TOCTOU window where another process can grab the port. + bootstrap_port = self.config.disaggregation.bootstrap_port + self._bootstrap_sock = None + if bootstrap_port is None: + prefill_host_ip = ray.util.get_node_ip_address().strip("[]") + bootstrap_port, self._bootstrap_sock = get_free_port(prefill_host_ip, with_alive_sock=True) + self._bootstrap_port = bootstrap_port + + prefill_end = self._prefill_tp + prefill_workers = self.workers[0:prefill_end] + prefill_node_id = worker_infos[0][0] + prefill_devs = self._collect_cuda_devices(worker_infos[0:prefill_end]) + + if self._bootstrap_sock is not None: + self._bootstrap_sock.close() + self._bootstrap_sock = None + + [prefill_server] = await self._launch_one( + role="prefill", + workers=prefill_workers, + node_id=prefill_node_id, + cuda_visible_devices=prefill_devs, + bootstrap_port=self._bootstrap_port, + tp=self._prefill_tp, + actor_name=f"sglang_server_{self.replica_rank}_0", + ) + self._prefill_servers = [prefill_server] + + prefill_address, prefill_port = await prefill_server.get_server_address.remote() + + def _fmt(addr, port): + return f"[{addr}]:{port}" if is_valid_ipv6_address(addr) else f"{addr}:{port}" + + self._prefill_server_address = _fmt(prefill_address, prefill_port) + + self._decode_servers = [] + self._decode_server_addresses = [] + for i in range(self._n_decode): + start = self._prefill_tp + i * self._decode_tp + end = start + self._decode_tp + workers_i = self.workers[start:end] + node_id_i = worker_infos[start][0] + devs_i = self._collect_cuda_devices(worker_infos[start:end]) + + [decode_server] = await self._launch_one( + role="decode", + workers=workers_i, + node_id=node_id_i, + cuda_visible_devices=devs_i, + bootstrap_port=self._bootstrap_port, + tp=self._decode_tp, + actor_name=f"sglang_server_decode_{self.replica_rank}_{i}", + ) + self._decode_servers.append(decode_server) + + d_addr, d_port = await decode_server.get_server_address.remote() + self._decode_server_addresses.append(_fmt(d_addr, d_port)) + + self._server_address = self._prefill_server_address + self._server_handle = prefill_server + self.servers = list(self._prefill_servers) + list(self._decode_servers) + + await prefill_server.set_pd_peer.remote(list(self._decode_servers), prefill_address) + + logger.info( + f"SGLangPDReplica rank={self.replica_rank} launched: " + f"prefill={self._prefill_server_address}, " + f"decodes=[{', '.join(self._decode_server_addresses)}], " + f"bootstrap_port={self._bootstrap_port}" + ) + + @staticmethod + def _collect_cuda_devices(worker_infos) -> str: + devs = set() + for _, dev_str in worker_infos: + for d in dev_str.split(","): + if d.strip(): + devs.add(int(d)) + return ",".join(str(d) for d in sorted(devs)) + + async def _launch_one( + self, + role: str, + workers: list[ActorHandle], + node_id: str, + cuda_visible_devices: str, + bootstrap_port: int, + tp: int, + actor_name: str, + ) -> list[ActorHandle]: + base_gpu_id = 0 + if os.environ.get(f"RAY_EXPERIMENTAL_NOSET_{visible_devices_keyword}", None): + base_gpu_id = (0 + self.replica_rank * self.world_size) % self.gpus_per_node + + pool_config = _dc_replace(self.config, tensor_model_parallel_size=tp) + + server = self.server_class.options( + scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy( + node_id=node_id, soft=False + ), + runtime_env={"env_vars": {f"RAY_EXPERIMENTAL_NOSET_{visible_devices_keyword}": "1"}}, + name=actor_name, + max_concurrency=self.max_concurrency, + ).remote( + config=pool_config, + model_config=self.model_config, + rollout_mode=self.rollout_mode, + workers=workers, + replica_rank=self.replica_rank, + node_rank=0, + nnodes=1, + cuda_visible_devices=cuda_visible_devices, + base_gpu_id=base_gpu_id, + disaggregation_role=role, + disaggregation_bootstrap_port=bootstrap_port, + ) + await server.launch_server.remote(master_address=None, master_port=None) + return [server] diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_rollout.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..07e30d07c806b146454eff2b2fd0d10cc6c9c30d --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/sglang_rollout.py @@ -0,0 +1,454 @@ +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import annotations + +import logging +import multiprocessing as mp +import os +from dataclasses import asdict +from typing import Generator + +import ray +import sglang.srt.entrypoints.engine +import torch +from peft import LoraConfig +from sglang.srt.server_args import ServerArgs +from sglang.srt.utils import ( + MultiprocessingSerializer, + assert_pkg_version, + is_cuda, + set_prometheus_multiproc_dir, + set_ulimit, +) +from sglang.srt.weight_sync.utils import _preprocess_tensor_for_update_weights +from sglang.srt.weight_sync.utils import update_weights as sgl_update_weights +from torch.distributed.device_mesh import DeviceMesh, init_device_mesh + +from verl.utils.net_utils import is_valid_ipv6_address +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.base import BaseRollout +from verl.workers.rollout.sglang_rollout.http_server_engine import AsyncHttpServerAdapter +from verl.workers.rollout.sglang_rollout.utils import ( + SGLANG_LORA_NAME, + get_named_tensor_buckets, +) + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +# patch to avoid issue https://github.com/sgl-project/sglang/issues/6723 +def _set_envs_and_config(server_args: ServerArgs): + # Set global environments + os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" + os.environ["NCCL_CUMEM_ENABLE"] = "0" + os.environ["NCCL_NVLS_ENABLE"] = str(int(server_args.enable_nccl_nvls)) + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + os.environ["CUDA_DEVICE_MAX_CONNECTIONS"] = "4" + os.environ["CUDA_MODULE_LOADING"] = "AUTO" + # Enable faulthandler in subprocesses + os.environ["PYTHONFAULTHANDLER"] = "1" + + # Set prometheus env vars + if server_args.enable_metrics: + set_prometheus_multiproc_dir() + + # Set ulimit + set_ulimit() + + # Check flashinfer version + if server_args.attention_backend == "flashinfer": + assert_pkg_version( + "flashinfer_python", + "0.2.5", + "Please uninstall the old version and reinstall the latest version by following the instructions at https://docs.flashinfer.ai/installation.html.", + ) + if is_cuda(): + try: + # For sglang 0.5.12 and sglang_kernel > 0.4.2, naming is sglang_kernel + assert_pkg_version( + "sglang_kernel", + "0.1.1", + "Please reinstall the latest version with `pip install follow https://sgl-project.github.io/get_started/install.html#for-cuda-13`", + ) + except Exception: + assert_pkg_version( + "sgl_kernel", + "0.1.1", + "Please reinstall the latest version with `pip install sgl-kernel --force-reinstall`", + ) + + # Set mp start method + mp.set_start_method("spawn", force=True) + + +sglang.srt.entrypoints.engine._set_envs_and_config = _set_envs_and_config + + +# because chatCompletion is an async method, it makes the whole ray actor be an async actor +# which can not call loop.run_until_complete. So we need to make the engine to be an async class +class ServerAdapter(BaseRollout): + """SGLang server adapter used in native http server mode, serve as http client to request SGLang server + to resume/release/update weights and kv_cache. + + - hybrid mode: reside in each hybrid worker to sync weights between training engine and SGLang server. + - standalone/colocated mode: just a dummy placeholder to occupy the GPU to prevent ray scheduling new GPU actor. + """ + + def __init__( + self, + config: RolloutConfig, + model_config: HFModelConfig, + device_mesh: DeviceMesh, + replica_rank: int = -1, + ): + super().__init__(config, model_config, device_mesh) + if self.config.get("quantization", None) == "fp8": + import sglang + from packaging import version + + from verl.utils.sglang.sglang_fp8_utils import build_sglang_fp8_quant_config + + assert version.parse(sglang.__version__) >= version.parse("0.5.5"), ( + "sglang>=0.5.5 is required for FP8 quantization" + ) + fp8_block_quant_kwargs = build_sglang_fp8_quant_config(self.model_config.hf_config) + self.model_config.hf_config.quantization_config = fp8_block_quant_kwargs + self._engine: AsyncHttpServerAdapter = None + + rank = int(os.environ["RANK"]) + local_world_size = int(os.environ["RAY_LOCAL_WORLD_SIZE"]) + # PD asymmetric layout inflates per-replica footprint; must match + # agent_loop.py:_initialize_llm_servers or trainer-to-replica mapping breaks. + disagg = getattr(self.config, "disaggregation", None) + prefill_tp = self.config.tensor_model_parallel_size + if disagg is not None and getattr(disagg, "enabled", False): + # Inline decode_tp default: OmegaConf/Ray serialization drops dataclass methods. + decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else prefill_tp + ) + rollout_world_size = ( + (prefill_tp * disagg.prefill_replicas + decode_tp * disagg.decode_replicas) + * self.config.data_parallel_size + * self.config.pipeline_model_parallel_size + ) + else: + rollout_world_size = prefill_tp * self.config.data_parallel_size * self.config.pipeline_model_parallel_size + if replica_rank == -1: + self.replica_rank = rank // rollout_world_size + else: + self.replica_rank = replica_rank + self.rollout_rank = rank % rollout_world_size + self.node_rank = self.rollout_rank // local_world_size + self.local_rank = self.rollout_rank % local_world_size + + # Map each trainer rank to its co-located SGLang server so weight-update + # IPC handles stay on the GPU where they were created. Offset math + # assumes prefill_replicas == 1 (enforced by SGLangPDReplica); if that + # ever lifts, update both this block and SGLangPDReplica.launch_servers. + self._pd_role = None + self._pd_server_index = None + self._pd_tp_local_rank = None + if disagg is not None and getattr(disagg, "enabled", False): + decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else prefill_tp + ) + # Modulo by single-group footprint so if DP>1 is ever enabled, + # each DP group's ranks resolve to the same role offsets. + footprint = prefill_tp + disagg.decode_replicas * decode_tp + local = self.rollout_rank % footprint + if local < prefill_tp: + self._pd_role = "prefill" + self._pd_server_index = 0 + self._pd_tp_local_rank = local + else: + off = local - prefill_tp + self._pd_role = "decode" + self._pd_server_index = off // decode_tp + self._pd_tp_local_rank = off % decode_tp + self._has_server = (disagg is None or not getattr(disagg, "enabled", False)) or (self._pd_role is not None) + + # sleep_level controls what gets released during sleep/release: + # 2 (default) = release weights + kv_cache (full sleep, merge path) + # 1 = release kv_cache only (keep base weights, adapter path) + # Set by engine_workers.update_weights() when lora.merge=False. + self.sleep_level = 2 + + async def _init_server_adapter(self): + if self._engine is not None: + return + + if not self._has_server: + return + + # device_mesh is needed to gather cuda ipc handle to update weights. + if self.device_mesh is None: + assert torch.distributed.is_initialized(), "torch distributed must be initialized" + infer_tp = self.config.tensor_model_parallel_size * self.config.data_parallel_size + infer_pp = self.config.pipeline_model_parallel_size + infer_world_size = infer_tp * infer_pp + dp = torch.distributed.get_world_size() // infer_world_size + self.device_mesh = init_device_mesh( + "cpu", mesh_shape=(dp, infer_tp, infer_pp), mesh_dim_names=["dp", "infer_tp", "infer_pp"] + ) + + # Only the role's TP-rank-0 builds an adapter; others participate in + # FSDP collectives but skip HTTP dispatch. + if self._pd_role is not None: + if self._pd_tp_local_rank != 0: + return + else: + if self.device_mesh["infer_tp"].get_local_rank() != 0: + return + + if self._pd_role == "prefill": + actor_name = f"sglang_server_{self.replica_rank}_0" + timeout_kwargs = {} + elif self._pd_role == "decode": + actor_name = f"sglang_server_decode_{self.replica_rank}_{self._pd_server_index}" + # Decode init on long-prompt workloads can stall past the default + # (60s × 12); shorter timeout + fewer attempts avoids trainer lockup. + timeout_kwargs = {"timeout": 10.0, "max_attempts": 2} + else: + actor_name = f"sglang_server_{self.replica_rank}_{self.node_rank}" + timeout_kwargs = {} + + self.server_actor = ray.get_actor(actor_name) + server_address, server_port = await self.server_actor.get_server_address.remote() + host = f"[{server_address}]" if is_valid_ipv6_address(server_address) else server_address + logger.info( + f"ServerAdapter {self._pd_role or 'colocated'}: " + f"replica_rank={self.replica_rank}, rollout_rank={self.rollout_rank}, " + f"server={host}:{server_port}, actor={actor_name}" + ) + + self._engine = AsyncHttpServerAdapter( + model_path=self.model_config.local_path, + host=host, + port=server_port, + launch_server=False, + trust_remote_code=self.model_config.trust_remote_code, + **timeout_kwargs, + ) + + def _is_server_tp_leader(self) -> bool: + """True if this rank is TP-rank-0 of its server's group. + + In PD, the role's TP (prefill_tp or decode_tp) may differ from the + config-level TP that device_mesh was built with, so use + _pd_tp_local_rank when PD is active. + """ + if self._pd_role is not None: + return self._pd_tp_local_rank == 0 + return self.device_mesh["infer_tp"].get_local_rank() == 0 + + async def resume(self, tags: list[str]): + """Resume rollout weights or kv cache in GPU memory. + + Args: + tag: weights or kv_cache. + """ + await self._init_server_adapter() + if self._engine is None: + return + if self._is_server_tp_leader() and self.config.free_cache_engine: + await self._engine.resume_memory_occupation(tags=tags) + + async def release(self): + """Release weights and kv cache in GPU memory. + + When sleep_level=1 (LoRA adapter mode), only releases kv_cache + to keep base weights alive across training iterations. + When sleep_level=2 (default/merge mode), releases everything. + """ + await self._init_server_adapter() + if self._engine is None: + return + if self._is_server_tp_leader() and self.config.free_cache_engine: + if self.sleep_level == 1: + tags = ["kv_cache"] + else: + tags = ["kv_cache", "weights"] + await self._engine.release_memory_occupation(tags=tags) + + async def update_weights( + self, + weights: Generator[tuple[str, torch.Tensor], None, None], + global_steps: int = None, + wire_format: str = "named_tensors", + **kwargs, + ): + """ + Update model weights using tensor buckets, similar to THUDM/slime's implementation. + + Notes: + - For the best performance of `rebuild_cuda_tensor`, it is recommended to: + 1. Enable `RAY_EXPERIMENTAL_NOSET_CUDA_VISIBLE_DEVICES`. + 2. Manually set `CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7` + when using Tensor Parallelism (TP >= 8). + - See reference implementations in SLIME: + - Main logic: https://github.com/THUDM/slime/blob/fb7605cc5fb09af0f9369d37f7192f12bddee577/slime/ray/ppo_actor.py#L452 + - runtime envs: https://github.com/THUDM/slime/blob/fb7605cc5fb09af0f9369d37f7192f12bddee577/slime/ray/ppo_actor.py#L39 + """ + await self._init_server_adapter() + + # Delta checkpoint-engine wire (standalone replicas): the generator yields + # per-flush sparse payloads, applied in place through the verl custom + # weight loader. Hybrid replicas pass full (name, tensor) pairs with no + # wire_format kwarg and take the bucketed path below. + if wire_format == "delta_flush": + await self._update_weights_delta(weights, global_steps=global_steps) + return + + # All ranks MUST iterate the weights generator below — DTensor.full_tensor() + # all_gather's across the FSDP group and skipping deadlocks the others. + # Only HTTP dispatch is gated on self._engine. + + peft_config, base_sync_done = kwargs.get("peft_config", None), kwargs.get("base_sync_done", False) + if peft_config and base_sync_done: + if self.device_mesh["infer_tp"].get_local_rank() == 0: + # unload lora + models_result = await self._engine.available_models() + exists = any(item["id"] == SGLANG_LORA_NAME for item in models_result["data"]) + if exists: + await self._engine.unload_lora_adapter(SGLANG_LORA_NAME) + + # load lora by tensor + serialize_peft_config, serialize_named_tensors = self.wrap_lora_params(peft_config, weights) + from sglang.srt.managers.io_struct import LoadLoRAAdapterFromTensorsReqInput + + req = LoadLoRAAdapterFromTensorsReqInput( + lora_name=SGLANG_LORA_NAME, + config_dict=serialize_peft_config, + serialized_tensors=serialize_named_tensors, + ) + # send http request + await self._engine.load_lora_adapter_from_tensor(req) + else: + update_weights_bucket_bytes = int(self.config.checkpoint_engine.update_weights_bucket_megabytes) << 20 + if self.config.get("quantization", None) == "fp8": + from verl.utils.sglang.sglang_fp8_utils import SGLangFP8QuantizerHelper + + logger.info("Convert bf16 weights to fp8 format before loading") + fp8_quantizer_helper = SGLangFP8QuantizerHelper(self.model_config.hf_config.quantization_config) + weights = fp8_quantizer_helper.quant_weights_by_name( + weights, + dtype=self.model_config.hf_config.dtype, + ) + else: + weights = weights + + async for params_batch in get_named_tensor_buckets(weights, update_weights_bucket_bytes): + await sgl_update_weights( + engine=self._engine, + params_batch=params_batch, + device_mesh_key="infer_tp", + device_mesh=self.device_mesh, + ) + + if self._engine is not None and self._is_server_tp_leader(): + await self._engine.flush_cache() + if global_steps is not None: + await self.server_actor.set_global_steps.remote(global_steps) + + async def _update_weights_delta(self, flushes, global_steps: int = None) -> None: + """Apply the delta engine's sparse flushes in place. + + ``flushes`` is ``CheckpointEngine.receive_weights``'s generator of + ``(named_tensors, is_last)`` items: one sentinel-encoded flush at a time, + already resident on this worker's GPU. Every rank of the replica consumes + the generator (the gather below is collective); TP0 posts one + ``update_weights_from_tensor`` per flush with ``load_format`` pointing at + the verl delta loader, and the radix cache is flushed only on the last + flush of the stream. + """ + assert self._pd_role is None, "delta checkpoint engine does not support PD disaggregation" + applied = 0 + for named, is_last in flushes: + await self._update_weights_delta_flush(named, flush_cache=is_last) + applied += 1 + del named + if self._engine is not None and self._is_server_tp_leader() and global_steps is not None: + await self.server_actor.set_global_steps.remote(global_steps) + logger.info("delta apply v=%s flushes=%d (in-place via sglang loader)", global_steps, applied) + + async def _update_weights_delta_flush(self, params_batch, flush_cache: bool) -> None: + """Hand one sparse flush to the colocated SGLang server via same-GPU CUDA IPC. + + SPMD across the replica's CheckpointEngineWorkers: every rank serializes its + local GPU copy, TP0 gathers the IPC handles and posts one + ``update_weights_from_tensor`` with ``load_format`` pointing at the verl + loader. Same flow as ``sglang.srt.weight_sync.utils.update_weights``, inlined + so the radix cache is flushed only on the stream's last flush instead of on + every bucket. Checksum is re-verified inside each SGLang worker (fail loud). + """ + import torch.distributed as dist + from sglang.srt.managers.io_struct import UpdateWeightsFromTensorReqInput + from sglang.srt.model_executor.model_runner import LocalSerializedTensor + from sglang.srt.utils import MultiprocessingSerializer + from sglang.srt.utils.patch_torch import monkey_patch_torch_reductions + + from verl.workers.rollout.sglang_rollout.delta_loader import LOADER_FQN + + monkey_patch_torch_reductions() + mesh = self.device_mesh["infer_tp"] + tp_size = mesh.mesh.size()[0] + serialized = [(name, MultiprocessingSerializer.serialize(t.detach())) for name, t in params_batch] + + gathered = [None for _ in range(tp_size)] if mesh.get_local_rank() == 0 else None + dist.gather_object( + obj=serialized, + object_gather_list=gathered, + dst=mesh.mesh.tolist()[0], + group=mesh.get_group(), + ) + if mesh.get_local_rank() != 0: + return + + named_tensors = [ + (group[0][0], LocalSerializedTensor(values=[part[1] for part in group])) + for group in zip(*gathered, strict=True) + ] + req = UpdateWeightsFromTensorReqInput( + serialized_named_tensors=[MultiprocessingSerializer.serialize(named_tensors) for _ in range(tp_size)], + load_format=LOADER_FQN, + flush_cache=flush_cache, + ) + await self._engine.update_weights_from_tensor(req) + + def wrap_lora_params(self, peft_config: LoraConfig, weights: Generator[tuple[str, torch.Tensor]]): + # peft config + peft_config_json = asdict(peft_config) + peft_config_json["task_type"] = peft_config_json["task_type"].value + peft_config_json["peft_type"] = peft_config_json["peft_type"].value + peft_config_json["target_modules"] = list(peft_config_json["target_modules"]) + + # lora weights + processed_weights: dict[str, torch.Tensor] = { + name: _preprocess_tensor_for_update_weights(tensor.detach()) for name, tensor in weights + } + + infer_tp_size = self.device_mesh["infer_tp"].mesh.size()[0] + serialized_named_tensors = [] + for i in range(infer_tp_size): + serialized_tensors = MultiprocessingSerializer.serialize(processed_weights, output_str=True) + serialized_named_tensors.append(serialized_tensors) + + return peft_config_json, serialized_named_tensors diff --git a/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/utils.py b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..0119c7d0a5f24168c659cafc13540e579d141748 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/sglang_rollout/utils.py @@ -0,0 +1,129 @@ +# Copyright 2023-2024 SGLang Team +# Copyright 2025 ModelBest Inc. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import pickle +from typing import Any, Iterator, Optional + +import numpy as np +import torch +import torch.distributed as dist + +from verl.utils.device import get_device_name +from verl.workers.rollout.utils import ensure_async_iterator + +SGLANG_LORA_NAME = "verl_actor_lora_name" + + +def broadcast_pyobj( + data: list[Any], + rank: int, + dist_group: Optional[torch.distributed.ProcessGroup] = None, + src: int = 0, + force_cpu_device: bool = False, +): + """from https://github.com/sgl-project/sglang/blob/844e2f227ab0cce6ef818a719170ce37b9eb1e1b/python/sglang/srt/utils.py#L905 + + Broadcast inputs from src rank to all other ranks with torch.dist backend. + The `rank` here refer to the source rank on global process group (regardless + of dist_group argument). + """ + device = torch.device(get_device_name() if not force_cpu_device else "cpu") + + if rank == src: + if len(data) == 0: + tensor_size = torch.tensor([0], dtype=torch.long, device=device) + dist.broadcast(tensor_size, src=src, group=dist_group) + else: + serialized_data = pickle.dumps(data) + size = len(serialized_data) + + tensor_data = torch.ByteTensor(np.frombuffer(serialized_data, dtype=np.uint8)).to(device) + tensor_size = torch.tensor([size], dtype=torch.long, device=device) + + dist.broadcast(tensor_size, src=src, group=dist_group) + dist.broadcast(tensor_data, src=src, group=dist_group) + return data + else: + tensor_size = torch.tensor([0], dtype=torch.long, device=device) + dist.broadcast(tensor_size, src=src, group=dist_group) + size = tensor_size.item() + + if size == 0: + return [] + + tensor_data = torch.empty(size, dtype=torch.uint8, device=device) + dist.broadcast(tensor_data, src=src, group=dist_group) + + serialized_data = bytes(tensor_data.cpu().numpy()) + data = pickle.loads(serialized_data) + return data + + +def _compact_for_bucket(tensor: torch.Tensor) -> torch.Tensor: + """Return a tensor safe to retain in a weight-sync bucket without pinning extra memory. + + ``get_named_tensor_buckets`` keeps every tensor alive until its bucket is flushed. A tensor + that is a *view* into a larger backing buffer would therefore keep that whole buffer resident + (and ship the whole buffer downstream), so such views must be compacted with ``clone()``. + + However the weights synced here come from ``DTensor.full_tensor()`` (a fresh all-gather) and + already own tight, contiguous storage. Cloning those allocates a second full-size buffer and + transiently doubles the tensor's footprint -- which OOMs on multi-GiB fused MoE weights + (e.g. ``[num_experts, ...]`` ``gate_up_proj``/``qkv``) while the actor params and rollout + weights are both already resident. Skip the clone when the tensor already owns its storage. + """ + if tensor.is_contiguous() and tensor.untyped_storage().nbytes() == tensor.numel() * tensor.element_size(): + return tensor + return tensor.clone() + + +async def get_named_tensor_buckets( + iterable: Iterator[tuple[str, torch.Tensor]], bucket_bytes: int +) -> Iterator[list[tuple[str, torch.Tensor]]]: + """ + Group tensors into buckets based on a specified size in megabytes. + + Args: + iterable: An iterator of tuples containing tensor names and tensors. + bucket_bytes: The maximum size of each bucket in bytes. + + Yields: + Lists of tuples, where each tuple contains a tensor name and its corresponding tensor. + + Example: + >>> tensors = [('tensor1', torch.randn(1000, 1000)), ('tensor2', torch.randn(2000, 2000))] + >>> for bucket in get_named_tensor_buckets(tensors, bucket_size_mb=10): + ... print(bucket) + [('tensor1', tensor(...)), ('tensor2', tensor(...))] + + """ + if bucket_bytes <= 0: + raise ValueError(f"bucket_bytes must be greater than 0, got {bucket_bytes}") + + current_bucket = [] + current_size = 0 + async for name, tensor in ensure_async_iterator(iterable): + tensor_size = tensor.element_size() * tensor.numel() + if current_size + tensor_size > bucket_bytes: + if current_bucket: + yield current_bucket + current_bucket = [(name, _compact_for_bucket(tensor))] + current_size = tensor_size + else: + current_bucket.append((name, _compact_for_bucket(tensor))) + current_size += tensor_size + + if current_bucket: + yield current_bucket diff --git a/verl_0720_main/verl/verl/workers/rollout/tokenizer.py b/verl_0720_main/verl/verl/workers/rollout/tokenizer.py new file mode 100644 index 0000000000000000000000000000000000000000..1e1212e50dce4785767cdd52c3dcc6288d08fa02 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/tokenizer.py @@ -0,0 +1,163 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +The base tokenizer class, required for any hybrid engine based rollout or inference with vLLM. +""" + +from abc import ABC, abstractmethod + +import numpy as np +import torch + +__all__ = ["HybridEngineBaseTokenizer"] + + +class HybridEngineBaseTokenizer(ABC): + """the tokenizer property and function name should align with HF's to meet vllm requirement""" + + @property + @abstractmethod + def vocab_size(self): + """ + `int`: Size of the base vocabulary (without the added tokens). + """ + pass + + @property + @abstractmethod + def pad_token_id(self): + """ + `Optional[int]`: Id of the padding token in the vocabulary. Returns `None` if the token has not been set. + """ + pass + + @property + @abstractmethod + def eos_token_id(self): + """ + `Optional[int]`: Id of the end of sentence token in the vocabulary. Returns `None` if the token has not been + set. + """ + pass + + @property + @abstractmethod + def all_special_ids(self) -> list[int]: + """ + `List[int]`: List the ids of the special tokens(`''`, `''`, etc.) mapped to class attributes. + """ + pass + + @property + @abstractmethod + def all_special_tokens(self) -> list[str]: + """ + `List[str]`: A list of the unique special tokens (`''`, `''`, ..., etc.). + + Convert tokens of `tokenizers.AddedToken` type to string. + """ + pass + + @abstractmethod + def encode(self, text): + """ + Converts a string to a sequence of ids (integer), using the tokenizer and vocabulary. + + Args: + text (`str`, `List[str]` or `List[int]`): + The first sequence to be encoded. This can be a string, a list of strings (tokenized string using the + `tokenize` method) or a list of integers. + + text_pair (`str`, `List[str]` or `List[int]`, *optional*): + Optional second sequence to be encoded. This can be a string, a list of strings (tokenized string using + the `tokenize` method) or a list of integers. + """ + pass + + @abstractmethod + def decode( + self, + token_ids: int | list[int] | np.ndarray | torch.Tensor, + skip_special_tokens: bool = False, + clean_up_tokenization_spaces: bool = None, + **kwargs, + ) -> str: + """ + Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special + tokens and clean up tokenization spaces. + + Similar to doing `self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))`. + + Args: + token_ids (`Union[int, List[int], np.ndarray, torch.Tensor]`): + List of tokenized input ids. Can be obtained using the `__call__` method. + skip_special_tokens (`bool`, *optional*, defaults to `False`): + Whether or not to remove special tokens in the decoding. + clean_up_tokenization_spaces (`bool`, *optional*): + Whether or not to clean up the tokenization spaces. If `None`, will default to + `self.clean_up_tokenization_spaces`. + kwargs (additional keyword arguments, *optional*): + Will be passed to the underlying model specific decode method. + + Returns: + `str`: The decoded sentence. + """ + pass + + @abstractmethod + def convert_ids_to_tokens(self, ids: int | list[int], skip_special_tokens: bool = False) -> str | list[str]: + """ + Converts a single index or a sequence of indices in a token or a sequence of tokens, using the vocabulary and + added tokens. + + Args: + ids (`int` or `List[int]`): + The token id (or token ids) to convert to tokens. + skip_special_tokens (`bool`, *optional*, defaults to `False`): + Whether or not to remove special tokens in the decoding. + + Returns: + `str` or `List[str]`: The decoded token(s). + """ + pass + + @abstractmethod + def get_added_vocab(self) -> dict[str, int]: + """ + Returns the added tokens in the vocabulary as a dictionary of token to index. Results might be different from + the fast call because for now we always add the tokens even if they are already in the vocabulary. This is + something we should change. + + Returns: + `Dict[str, int]`: The added tokens. + """ + pass + + @abstractmethod + def convert_tokens_to_string(self, tokens: list[str]) -> str: + """ + Converts a sequence of tokens in a single string. The most simple way to do it is `" ".join(tokens)` but we + often want to remove sub-word tokenization artifacts at the same time. + + Args: + tokens (`List[str]`): The token to join in a string. + + Returns: + `str`: The joined tokens. + """ + pass + + @property + def is_fast(self): + return False diff --git a/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/__init__.py b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..d828409b82e82a2f9aae138e1150608cf891a667 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_rollout.md b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_rollout.md new file mode 100644 index 0000000000000000000000000000000000000000..ebb1d21516e37b43a84e625cdb62bbd6bdeb6d1c --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_rollout.md @@ -0,0 +1,291 @@ +# Running VeRL with TensorRT-LLM Rollout + +We provide initial support for [TensorRT-LLM](https://github.com/NVIDIA/TensorRT-LLM) as an asynchronous rollout engine in VERL's reinforcement learning pipeline. It covers key features such as distributed inference with Ray-based orchestration, dynamic weight updates via IPC (Inter-Process Communication), and efficient GPU memory management for GRPO training. + +TRT-LLM rollout uses hybrid engine colocate mode, where training and inference workers are colocated on the same GPUs. Memory is managed via `resume()`/`release()` APIs to enable GPU sharing between training and inference workloads. + +While the current design factors in multi-node use cases, more extensive multi-node testing and functionality will be delivered in the near future. Current focus is on FSDP and Megatron backend support for Qwen model variants. + +--- + +## 1. Quick Start + + +```bash +# GRPO with FSDP training engine and TP1 +>> INFER_BACKEND=trtllm ROLLOUT_TP=1 bash examples/grpo_trainer/run_qwen3_8b_fsdp.sh +``` + +Note that using the TRT-LLM rollout requires setting the following environment variables before launching the Ray cluster, as included in the above script. + +```bash +# Clean all SLURM/MPI/PMIx env to avoid pmix mismatch error. +for v in $(env | awk -F= '/^(PMI|PMIX|MPI|OMPI|SLURM)_/{print $1}'); do + unset "$v" +done +``` + +## 2. Architecture Design + +### 2.1 High-Level Component Diagram + +```mermaid +%%{init: {'theme':'base', 'themeVariables': { 'fontSize':'18px', 'edgeLabelBackground':'#eeeeee'}}}%% +flowchart TB + space1[" "] + style space1 fill:none,stroke:none + + subgraph VERL["VERL Training Pipeline"] + subgraph Workers["Training Workers"] + Actor["Actor Worker"] + Critic["Critic Worker"] + RefModel["Ref Model Worker"] + end + + Actor -->|Weight Updates
IPC
| Rollout["TensorRT-LLM Rollout"] + + subgraph RayCluster["Rollout Workers
(Ray Cluster)
"] + space2[" "] + style space2 fill:none,stroke:none + + subgraph AsyncRollout["ServerAdapter
(per DP rank)
"] + DPLeader["• DP Leader coordination"] + IPCMgmt["• IPC handle management"] + HTTPAdapter["• HTTP adapter for server communication"] + end + + AsyncRollout -->|HTTP/REST API| HTTPServer + + subgraph HTTPServer["TRTLLMHttpServer
(Ray Actor per Replica)
"] + OpenAI["• OpenAI Server wrapper"] + EngMgmt["• AsyncLLM engine management"] + MemMgmt["• Memory management (resume/release)"] + end + + HTTPServer --> AsyncLLM + + subgraph AsyncLLM["TensorRT-LLM
AsyncLLM Engine
"] + GPUWorkers["• GPU workers (Tensor Parallel)"] + KVCache["• KV Cache management"] + CUDAGraph["• CUDA Graph optimization"] + end + end + end + + space1 ~~~ VERL + + style VERL fill:#e1f5ff + style RayCluster fill:#fff4e6 + style AsyncRollout fill:#f3e5f5 + style HTTPServer fill:#e8f5e9 + style AsyncLLM fill:#fce4ec +``` + +### 2.2 Agent Loop Architecture + +TRT-LLM rollout follows the same Agent Loop architecture described in the [VERL documentation](https://verl.readthedocs.io/en/latest/advance/agent_loop.html). + +With TensorRT-LLM rollout, the AsyncLLM engine runs in the same process as the TRTLLMHttpServer (Ray actor). The engine spawns Ray workers as ModelRunner through Ray's native orchestration with placement groups. + +AsyncLLM engine communicates with Ray workers through TensorRT-LLM's internal communication layer. When the server receives a request, it directly calls the AsyncLLM engine to generate response_ids. The Ray workers are separate processes from FSDP/Megatron-LM workers but are co-located on the same GPUs in hybrid engine mode. + +The diagram below illustrates TRT-LLM's implementation in hybrid engine mode (Ray Workers and FSDP workers share GPUs): + +```mermaid +flowchart TB + generate[generate] + + generate --> Server + + Server[TRTLLMHttpServer
AsyncLLM Engine] + + Server --> Workers + + subgraph Workers["TRT-LLM group (TP4)"] + direction LR + subgraph W0[ ] + RW0[Ray Worker-0] + F0[FSDP-0] + end + subgraph W1[ ] + RW1[Ray Worker-1] + F1[FSDP-1] + end + subgraph W2[ ] + RW2[Ray Worker-2] + F2[FSDP-2] + end + subgraph W3[ ] + RW3[Ray Worker-3] + F3[FSDP-3] + end + end + + style Server fill:#ffb6c1 + style RW0 fill:#ffffe0 + style RW1 fill:#ffffe0 + style RW2 fill:#ffffe0 + style RW3 fill:#ffffe0 + style F0 fill:#ffb6c1 + style F1 fill:#ffb6c1 + style F2 fill:#ffb6c1 + style F3 fill:#ffb6c1 + style W0 fill:#d3d3d3 + style W1 fill:#d3d3d3 + style W2 fill:#d3d3d3 + style W3 fill:#d3d3d3 + style Workers fill:#f5f5f5 +``` + + +### 2.3 Ray Placement Group Architecture + +1. **Placement APIs & GPU Assignment**: TRT-LLM rollout leverages TRT-LLM's Ray-based APIs (`placement_groups`, `placement_bundle_indices`, `per_worker_gpu_share`) to control GPU placement. Each replica (corresponding to one `TRTLLMHttpServer`) is assigned GPU bundles from placement groups based on its replica rank and TP size. + +2. **Server Placement**: `TRTLLMHttpServer` is pinned to the same node as its first bundle using `NodeAffinitySchedulingStrategy`, ensuring efficient communication between the HTTP server and its Ray workers. + +3. **GPU Sharing**: In hybrid engine mode, training and inference workers share GPUs. Memory is managed via `resume()`/`release()` APIs. The resource pool uses `max_colocate_count=3` internally to support colocation of ActorRollout, RewardModel, and Critic workers. + +4. **Multi-Node Design**: The placement group slicing algorithm supports spanning multiple placement groups for multi-node deployments. **Note**: Formal multi-node testing and functionality will be delivered in subsequent MRs. + +The following diagram shows an example of TP=4 and DP=2. Replica 0 takes bundles 0-3 and Replica 1 takes bundles 4-7 from the same placement group, with each replica managing TP workers across its assigned bundles: + +```mermaid +flowchart TB + subgraph RayCluster["Ray Cluster Resource Pool"] + subgraph PG0["Placement Group 0 (Node 0)"] + B0_0["Bundle 0: GPU 0"] + B0_1["Bundle 1: GPU 1"] + B0_2["Bundle 2: GPU 2"] + B0_3["Bundle 3: GPU 3"] + B0_4["Bundle 4: GPU 4"] + B0_5["Bundle 5: GPU 5"] + B0_6["Bundle 6: GPU 6"] + B0_7["Bundle 7: GPU 7"] + end + + subgraph PG1["Placement Group 1 (Node 1)"] + B1_0["Bundle 0: GPU 0"] + B1_1["Bundle 1: GPU 1"] + B1_2["Bundle 2: GPU 2"] + B1_3["Bundle 3: GPU 3"] + B1_4["Bundle 4: GPU 4"] + B1_5["Bundle 5: GPU 5"] + B1_6["Bundle 6: GPU 6"] + B1_7["Bundle 7: GPU 7"] + end + + PG0 --> Assignment + PG1 --> Assignment + + Assignment["Assigned to TRTLLMReplica"] + + Assignment --> Replica0 + Assignment --> Replica1 + + Replica0["Replica 0
(bundles 0-3 from PG0)
TP=4, DP=2"] + Replica1["Replica 1
(bundles 4-7 from PG0)
TP=4, DP=2"] + end + + style PG0 fill:#e3f2fd + style PG1 fill:#e3f2fd + style Replica0 fill:#c8e6c9 + style Replica1 fill:#c8e6c9 +``` + +--- + +## 3. Core Components + +### 3.1 `TRTLLMHttpServer` + +**Purpose**: Ray actor that wraps TensorRT-LLM's AsyncLLM engine and exposes an OpenAI-compatible HTTP API. + +**Key Responsibilities**: +- Initialize and manage AsyncLLM engine with placement group constraints +- Wrap AsyncLLM with OpenAIServer to expose HTTP endpoints +- Handle HTTP server lifecycle (launch, shutdown) +- Process generation requests with sampling parameters +- Coordinate memory management (wake_up/sleep) for GPU sharing with training workers + + +### 3.2 `TRTLLMReplica` + +**Purpose**: Manages the mapping between replicas and Ray placement groups, orchestrating server deployment. + +**Key Responsibilities**: +- Calculate placement group and bundle index assignments per replica +- Pin TRTLLMHttpServer to specific nodes using NodeAffinitySchedulingStrategy +- Launch and coordinate HTTP servers across distributed nodes +- Validate placement group configurations + + +### 3.3 `ServerAdapter` + +**Purpose**: Rollout worker that handles weight updates, memory management, and generation via HTTP adapter. + +Each DP rank has one leader (the first TP rank within that DP group), and that leader coordinates weight updates to the corresponding TRTLLMHttpServer replica. + +**Key Responsibilities**: +- Act as DP leader for weight synchronization across exclude_dp mesh +- Convert PyTorch tensors to IPC handles for zero-copy weight updates +- Stream weight updates in chunks to avoid memory exhaustion +- Coordinate resume/release operations for memory management +- Initialize HTTP adapter for server communication + + +### 3.4 `AsyncTRTLLMHttpAdapter` + +**Purpose**: HTTP client for communicating with TRTLLMHttpServer. + +**Key Features**: +- Async request handling with retry logic +- Connection pooling for high throughput +- Exponential backoff on failures +- Timeout management + +--- + +## 4. Data Flow Diagrams + +### 4.1 Generation Request Flow + +```mermaid +sequenceDiagram + participant Client as Client/Actor + participant Rollout as ServerAdapter + participant Adapter as AsyncHttpAdapter + participant Server as TRTLLMHttpServer + participant AsyncLLM as AsyncLLM Engine + + Client->>Rollout: generate(prompts) + + rect rgb(240, 248, 255) + Note over Rollout: Init adapter if needed + end + + Rollout->>Adapter: POST /v1/completions
{prompt_ids, sampling_params} + + rect rgb(255, 250, 240) + Note over Adapter: Retry loop with backoff + end + + Adapter->>Server: HTTP POST + + rect rgb(245, 255, 245) + Note over Server: Parse request
Validate params + end + + Server->>AsyncLLM: generate_async() + + rect rgb(255, 245, 245) + Note over AsyncLLM: Schedule to execution queue + Note over AsyncLLM: Run inference (TP workers)
- Forward pass
- Sample tokens
- Update KV cache + end + + AsyncLLM-->>Server: Output (token_ids, log_probs) + + Server-->>Adapter: JSON response + Adapter-->>Rollout: TokenOutput + Rollout-->>Client: Results +``` diff --git a/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_server.py b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_server.py new file mode 100644 index 0000000000000000000000000000000000000000..1be9b4b8f940aaecb6d6b7f3763dfa5cbff1da57 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_async_server.py @@ -0,0 +1,631 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import asyncio +import logging +import os +from typing import Any, Optional + +import ray +import torch +from omegaconf import DictConfig +from ray.actor import ActorHandle +from ray.util import placement_group_table +from ray.util.placement_group import PlacementGroup + +from verl.plugin.platform import get_platform +from verl.single_controller.ray import SubRayResourcePool +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.net_utils import is_valid_ipv6_address +from verl.utils.profiler import DistProfiler +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.replica import RolloutMode, RolloutReplica, TokenOutput +from verl.workers.rollout.utils import get_max_position_embeddings, qwen2_5_vl_dedup_image_tokens, run_uvicorn + +logger = logging.getLogger(__file__) +logger.setLevel(logging.INFO) + + +def _resolve_chat_stop_tokens(model_config) -> tuple[int, list[int]]: + """Return (end_id, stop_token_ids) for TorchSampler. + + Both TRTLLM's samplers stops only on end_id. For chat-format prompts the model + naturally ends each assistant turn with a chat-end token (e.g. <|im_end|> + for Qwen, <|eot_id|> for Llama-3) that is *different* from the base-model + eos_token_id. If end_id is set to the base eos the sampler ignores the + chat-end token and the model loops into a second turn, inflating response + lengths until max_tokens is hit. + + For models without a distinct chat-end token the return values are + identical to the current default (end_id = hf_config.eos_token_id). + """ + eos_token_id = model_config.hf_config.eos_token_id + all_stop_ids: list[int] = list(eos_token_id) if isinstance(eos_token_id, list) else [eos_token_id] + + if model_config.generation_config is not None: + gen_eos = model_config.generation_config.eos_token_id + if gen_eos is not None: + for t in gen_eos if isinstance(gen_eos, list) else [gen_eos]: + if t not in all_stop_ids: + all_stop_ids.append(t) + + chat_end_id = None + if model_config.tokenizer is not None: + _chat_stop_strings = ["<|im_end|>", "<|eot_id|>", "<|end_of_turn|>"] + _added_vocab = model_config.tokenizer.get_added_vocab() + for stop_str in _chat_stop_strings: + if stop_str in _added_vocab: + tid = _added_vocab[stop_str] + if tid not in all_stop_ids: + all_stop_ids.append(tid) + if chat_end_id is None: + chat_end_id = tid + + primary_end_id = chat_end_id if chat_end_id is not None else eos_token_id + logger.warning(f"TRT-LLM stop token IDs: {all_stop_ids}, end_id: {primary_end_id}") + return primary_end_id, all_stop_ids + + +@ray.remote +class TRTLLMHttpServer: + """TensorRT LLM HTTP server in single node. + + Args: + config (DictConfig): full config. + model_config (HFModelConfig): model config. + is_reward_model (bool): whether this is a reward model. + rollout_mode (RolloutMode): rollout mode. + workers (list[ActorHandle]): list of rollout workers. + replica_rank (int): replica rank, a replica may contain multiple nodes. + max_colocate_count (int): max colocate count. + pgs (list[PlacementGroup]): placement groups. + bundle_indices (list[list[int]]): bundle indices. + """ + + def __init__( + self, + config: RolloutConfig, + model_config: HFModelConfig, + is_reward_model: bool, + rollout_mode: RolloutMode, + workers: list[ActorHandle], + replica_rank: int, + max_colocate_count: int, + pgs: list[PlacementGroup] = None, + bundle_indices: list[list[int]] = None, + ): + os.environ["TRT_LLM_DISABLE_LOAD_WEIGHTS_IN_PARALLEL"] = "1" + assert torch.cuda.is_available(), "TRTLLM http server should run on GPU node" + + self.config: RolloutConfig = omega_conf_to_dataclass(config) + self.model_config: HFModelConfig = omega_conf_to_dataclass(model_config, dataclass_type=HFModelConfig) + self.is_reward_model = is_reward_model + max_position_embeddings = get_max_position_embeddings(self.model_config.hf_config) + if self.config.max_model_len is None: + self.config.max_model_len = max_position_embeddings + else: + if self.config.max_model_len > max_position_embeddings: + raise ValueError( + f"max_model_len ({self.config.max_model_len}) should be less than or equal to " + f"max_position_embeddings ({max_position_embeddings})" + ) + self.rollout_mode = rollout_mode + self.workers = workers + self.replica_rank = replica_rank + self.max_colocate_count = max_colocate_count + self.pgs = pgs + self.bundle_indices = bundle_indices + # model weights version, set by ServerAdapter when update weights. + self.global_steps = None + # Set when generation is allowed; cleared during weight sync to block new requests. + self._generation_allowed = asyncio.Event() + self._generation_allowed.set() + + self.profiler_controller = self._init_profiler_controller() + + # Non-HYBRID with load_format=dummy normally needs to load from disk (auto). + # Exception: FP8 has no on-disk ckpt; weights are filled during first sync (keep dummy). + if ( + self.rollout_mode != RolloutMode.HYBRID + and self.config.load_format == "dummy" + and self.config.quantization != "fp8" + ): + logger.warning(f"rollout mode is {self.rollout_mode}, load_format is dummy, set to auto") + self.config.load_format = "auto" + + self.is_vlm_model = ( + self.model_config.hf_config is not None and hasattr(self.model_config.hf_config, "vision_config") + ) or hasattr(self.model_config, "vision_config") + + # used for http server + self._server_address = ray.util.get_node_ip_address().strip("[]") + self._server_port = None + + logger.info(f"TRTLLMHttpServer, replica_rank: {self.replica_rank}") + + _end_id, _stop_ids = _resolve_chat_stop_tokens(self.model_config) + + logger.info(f"TRT-LLM resolved end_id={_end_id}, stop_ids={_stop_ids}") + + self._use_torch_sampler = bool(int(os.environ.get("TLLM_USE_TORCHSAMPLER", "0"))) + + if self._use_torch_sampler: + self.sampling_args = { + "detokenize": True, + "end_id": _end_id, + "stop_token_ids": _stop_ids, + "pad_id": self.model_config.hf_config.pad_token_id, + "include_stop_str_in_output": True, + } + else: + self.sampling_args = { + "detokenize": False, + "end_id": -1, + "pad_id": self.model_config.hf_config.pad_token_id, + "stop_token_ids": _stop_ids, + "include_stop_str_in_output": True, + } + logger.info(f"use_torch_sampler={self._use_torch_sampler}, sampling_args={self.sampling_args}") + + def get_server_address(self): + """Get http server address and port.""" + assert self._server_port is not None, "http server is not launched, port is None" + return self._server_address, self._server_port + + async def launch_server(self): + from tensorrt_llm import AsyncLLM + from tensorrt_llm.llmapi import CapacitySchedulerPolicy, CudaGraphConfig, KvCacheConfig, SchedulerConfig + + try: + from tensorrt_llm.llmapi.llm_args import ExecutorMemoryType, SleepConfig + except ImportError: + ExecutorMemoryType = None + SleepConfig = None + from tensorrt_llm.serve import OpenAIServer + + assert self.config.pipeline_model_parallel_size == 1, "pipeline_model_parallel_size > 1 is not supported yet" + + engine_kwargs = self.config.get("engine_kwargs", {}).get("trtllm", {}) or {} + # Pop kv_cache_config from engine_kwargs to merge into KvCacheConfig constructor, + # otherwise **engine_kwargs unpacking in llm_kwargs would overwrite the entire + # KvCacheConfig object, losing free_gpu_memory_fraction and enable_block_reuse. + kv_cache_overrides = engine_kwargs.pop("kv_cache_config", {}) + kv_cache_kwargs = { + "enable_block_reuse": self.config.enable_prefix_caching, + "free_gpu_memory_fraction": self.config.gpu_memory_utilization, + **kv_cache_overrides, + } + kv_cache_config = KvCacheConfig(**kv_cache_kwargs) + + per_worker_gpu_share = 1.0 / self.max_colocate_count + + quantization = self.config.quantization + if quantization is not None: + if quantization == "fp8": + FP8_BLOCK_QUANT_KWARGS = { + "activation_scheme": "dynamic", + "fmt": "e4m3", + "quant_method": "fp8", + "weight_block_size": [128, 128], + } + engine_kwargs["model_kwargs"] = {"quantization_config": FP8_BLOCK_QUANT_KWARGS} + if self.config.load_format != "dummy": + raise ValueError("FP8 quantization is only supported for dummy load format") + else: + raise ValueError(f"Currently only support fp8 quantization, got: {quantization}") + + llm_kwargs = { + "model": self.model_config.local_path, + "backend": "pytorch", + "dtype": self.config.dtype, + "enable_chunked_prefill": self.config.enable_chunked_prefill, + "skip_tokenizer_init": self.config.skip_tokenizer_init, + "orchestrator_type": "ray", + "kv_cache_config": kv_cache_config, + "max_seq_len": self.config.max_model_len, + "max_batch_size": self.config.max_num_seqs, + "max_num_tokens": self.config.max_num_batched_tokens, + "tensor_parallel_size": self.config.tensor_model_parallel_size, + "pipeline_parallel_size": self.config.pipeline_model_parallel_size, + "moe_expert_parallel_size": self.config.expert_parallel_size, + "moe_tensor_parallel_size": self.config.moe_tensor_parallel_size, + "load_format": self.config.load_format, + "trust_remote_code": self.model_config.trust_remote_code, + "placement_groups": self.pgs, + "placement_bundle_indices": self.bundle_indices, + "per_worker_gpu_share": per_worker_gpu_share, + "sleep_config": SleepConfig( + restore_modes={ + ExecutorMemoryType.MODEL_WEIGHTS_MAIN: "NONE", + ExecutorMemoryType.KV_CACHE: "NONE", + } + ) + if self.config.enable_sleep_mode and SleepConfig is not None + else None, + "allreduce_strategy": "NCCL", + "sampler_type": "TorchSampler" if self._use_torch_sampler else "TRTLLMSampler", + **engine_kwargs, + } + + self_defined_extension = { + "ray_worker_extension_cls": "verl.workers.rollout.trtllm_rollout.trtllm_worker_extension.WorkerExtension", + } + if self.is_vlm_model: + llm_kwargs.update(self_defined_extension) + else: + # TODO: once TRT-LLM WorkerExtension includes wait_for_engine_idle, + # replace with "tensorrt_llm.llmapi.rlhf_utils.WorkerExtension" directly. + llm_kwargs.update( + { + "ray_worker_extension_cls": ( + "verl.workers.rollout.trtllm_rollout.trtllm_worker_extension.RlhfWorkerExtension" + ), + } + ) + + if self.is_reward_model: + llm_kwargs.update( + { + "cuda_graph_config": None, + "disable_overlap_scheduler": True, + } + ) + else: + llm_kwargs.update( + { + "cuda_graph_config": CudaGraphConfig( + enable_padding=True, + batch_sizes=self.config.cudagraph_capture_sizes, + max_batch_size=0 if self.config.cudagraph_capture_sizes else self.config.max_num_seqs, + ), + "scheduler_config": SchedulerConfig( + capacity_scheduler_policy=CapacitySchedulerPolicy.MAX_UTILIZATION, + ), + } + ) + + self.llm = await AsyncLLM(**llm_kwargs) + import inspect + + init_params = inspect.signature(OpenAIServer.__init__).parameters + if "generator" in init_params: + trtllm_server = OpenAIServer( + generator=self.llm, + model=self.model_config.local_path, + tool_parser=None, + server_role=None, + metadata_server_cfg=None, + ) + else: + trtllm_server = OpenAIServer( + llm=self.llm, + model=self.model_config.local_path, + tool_parser=None, + server_role=None, + metadata_server_cfg=None, + ) + + app = trtllm_server.app + self._server_port, self._server_task = await run_uvicorn(app, None, self._server_address) + + async def generate( + self, + prompt_ids: str | list[int], + sampling_params: dict[str, Any], + request_id: str, + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + audio_data: Optional[list[Any]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + ) -> TokenOutput: + from tensorrt_llm.llmapi import SamplingParams + + max_tokens = min( + self.config.response_length, + self.config.prompt_length + self.config.response_length - len(prompt_ids), + ) + max_tokens = max(0, min(max_tokens, self.config.max_model_len - len(prompt_ids))) + sampling_params["max_tokens"] = max_tokens + # TorchSampler: logprobs=0 means sampled-token logprob; TRTLLMSampler: logprobs=1 + _want_logprobs = sampling_params.pop("logprobs", False) + if self._use_torch_sampler: + sampling_params["logprobs"] = 0 if _want_logprobs else None + else: + sampling_params["logprobs"] = 1 if _want_logprobs else None + if sampling_params["top_k"] == -1: + sampling_params["top_k"] = 0 + sampling_params.update(self.sampling_args) + + trt_llm_sampling_params = SamplingParams(**sampling_params) + if audio_data is not None: + raise NotImplementedError("TRT-LLM rollout does not support audio inputs yet.") + + await self._generation_allowed.wait() + if self.is_vlm_model and (image_data or video_data): + deduped_ids = qwen2_5_vl_dedup_image_tokens(prompt_ids, self.model_config.processor) + org_prompt = self.llm.tokenizer.decode(deduped_ids) + input_dict = { + "prompt": org_prompt, + "multi_modal_data": {}, + "mm_processor_kwargs": dict(mm_processor_kwargs or {}), + } + if image_data: + input_dict["multi_modal_data"]["image"] = image_data + if video_data: + input_dict["multi_modal_data"]["video"] = video_data + + outputs = await self.llm.generate_async( + inputs=input_dict, + sampling_params=trt_llm_sampling_params, + ) + else: + outputs = await self.llm.generate_async( + inputs=prompt_ids, + sampling_params=trt_llm_sampling_params, + ) + token_ids = outputs.outputs[0].token_ids + log_probs = None + if outputs.outputs[0].logprobs is not None: + # When logprobs=1, TRT-LLM returns only the sampled token's logprob at each position. + # Extract log_probs before checking finish_reason so cancelled (partial) requests also + # return log_probs for their already-generated tokens. + log_probs = [list(d.values())[0].logprob for d in outputs.outputs[0].logprobs] + if outputs.outputs[0].finish_reason == "cancelled": + return TokenOutput( + token_ids=token_ids, + log_probs=log_probs, + stop_reason="aborted", + extra_fields={"global_steps": self.global_steps}, + ) + return TokenOutput(token_ids=token_ids, log_probs=log_probs, extra_fields={"global_steps": self.global_steps}) + + async def set_global_steps(self, global_steps: int): + """Set the global steps of the model weights.""" + self.global_steps = global_steps + + async def abort_all_requests(self): + """Abort all in-flight requests and block new ones. Call resume_generation() to unblock.""" + self._generation_allowed.clear() + await self.llm.pause_generation() + # TODO: remove once TRT-LLM is upgraded to a version where pause_generation() + # drains internally (https://github.com/NVIDIA/TensorRT-LLM/pull/13784). + await self.llm.collective_rpc("wait_for_engine_idle") + if self.config.enable_prefix_caching: + await self.llm.collective_rpc("reset_prefix_cache") + + async def resume_generation(self): + """Unblock new generation requests after abort_all_requests().""" + await self.llm.resume_generation() + self._generation_allowed.set() + + async def clear_kv_cache(self): + """Invalidate prefix cache entries after weight update.""" + await self.llm.collective_rpc("reset_prefix_cache") + + async def release_kv_cache(self): + """Release only kv_cache GPU memory, keeping model weights intact. + + This is used during weight sync to free GPU memory for new weights. + """ + if not self.config.free_cache_engine: + return + await self.llm.release(tags=["kv_cache"]) + + async def resume_kv_cache(self): + """Restore kv_cache GPU memory after a weight sync. Counterpart to release_kv_cache().""" + await self.llm.resume(tags=["kv_cache"]) + + async def wake_up(self): + from verl.workers.rollout.trtllm_rollout.trtllm_rollout import ServerAdapter + + if self.rollout_mode == RolloutMode.HYBRID: + # In hybrid mode, rollout is wake up in `update_weights` + raise ValueError(f"wake_up not support rollout_mode {self.rollout_mode}") + if self.rollout_mode == RolloutMode.COLOCATED: + await self.llm.resume(tags=ServerAdapter.get_full_tags()) + elif self.rollout_mode == RolloutMode.STANDALONE: + logger.info("skip wake_up in standalone mode") + + async def sleep(self): + from verl.workers.rollout.trtllm_rollout.trtllm_rollout import ServerAdapter + + if not self.config.free_cache_engine: + return + + if self.rollout_mode == RolloutMode.HYBRID: + await self.llm.release(tags=ServerAdapter.get_full_tags()) + elif self.rollout_mode == RolloutMode.COLOCATED: + await self.llm.release(tags=ServerAdapter.get_full_tags()) + elif self.rollout_mode == RolloutMode.STANDALONE: + logger.info("skip sleep in standalone mode") + + async def report_device_ids(self) -> list[str]: + """Report GPU device UUIDs from TRT-LLM workers.""" + return await self.llm.collective_rpc( + "report_device_id", + unique_reply_rank=0, + ) + + async def start_profile(self, **kwargs): + if self.profiler_controller.check_enable() and self.profiler_controller.check_this_rank(): + await self.llm.collective_rpc("start_profile") + + async def stop_profile(self): + if self.profiler_controller.check_enable() and self.profiler_controller.check_this_rank(): + await self.llm.collective_rpc("stop_profile") + + def _init_profiler_controller(self) -> DistProfiler: + profiler_config = self.config.profiler + tool_config = None + if profiler_config is not None: + if profiler_config.tool in ["torch", "npu"]: + tool_config = omega_conf_to_dataclass((profiler_config.tool_config or {}).get(profiler_config.tool)) + elif profiler_config.tool == "nsys": + # nsys config lives in global_tool_config, not tool_config + from verl.utils.profiler.config import NsightToolConfig + + raw = (profiler_config.global_tool_config or {}).get("nsys") + tool_config = omega_conf_to_dataclass(raw) if raw is not None else NsightToolConfig() + elif profiler_config.tool is not None: + logger.warning(f"trtllm rollout: unsupported profiler tool '{profiler_config.tool}', disabling") + profiler_config = None + return DistProfiler(self.replica_rank, config=profiler_config, tool_config=tool_config) + + +class TRTLLMReplica(RolloutReplica): + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: DictConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + name_suffix: str = "", + ) -> None: + if is_teacher_model: + raise NotImplementedError("TRTLLMReplica doesn't support teacher model yet.") + super().__init__( + replica_rank, config, model_config, gpus_per_node, is_reward_model, is_teacher_model, name_suffix + ) + self.node_ip = ray.util.get_node_ip_address().strip("[]") + + def rollout_worker_use_gpu(self) -> bool: + return False + + def get_pgs_and_bundle_indices(self) -> tuple[list[PlacementGroup], list[list[int]]]: + """Get placement groups and bundle indices for the replica.""" + + start_pg_index = 0 + local_bundle_index = 0 + + # For SubRayResourcePool, the replica is assigned sub pool specific for this replica. + if isinstance(self.resource_pool, SubRayResourcePool): + assert self.resource_pool.subgroup_world_size == self.world_size, ( + "Subgroup world size must be equal to world size" + ) + local_bundle_index = self.resource_pool.start_bundle_index + # For RayResourcePool, the replica is assigned to entire resource pool. + # We need to find start pg index and local bundle index based on replica rank. + else: + # In standalone mode, init_standalone() creates a per-replica RayResourcePool + # that contains only world_size bundles for this replica. Start at bundle 0. + # In colocated/hybrid mode, the shared pool spans all replicas, so offset by rank. + if self.rollout_mode == RolloutMode.STANDALONE: + local_bundle_index = 0 + else: + local_bundle_index = self.world_size * self.replica_rank + + while ( + start_pg_index < len(self.resource_pool.pgs) + and local_bundle_index >= self.resource_pool.pgs[start_pg_index].bundle_count + ): + local_bundle_index -= self.resource_pool.pgs[start_pg_index].bundle_count + start_pg_index += 1 + assert ( + start_pg_index < len(self.resource_pool.pgs) + and local_bundle_index < self.resource_pool.pgs[start_pg_index].bundle_count + ), "Start pg index or local bundle index out of range" + + # Global Bundle View for Replica x 2 & TP=4: + # ┌───────────────────┬───────────────────┐ + # │ Placement Group 0 │ Placement Group 1 │ + # ├────┬────┬────┬────┼────┬────┬────┬────┤ + # │ 0 │ 1 │ 2 │ 3 │ 0 │ 1 │ 2 │ 3 │ + # └────┴────┴────┴────┴────┴────┴────┴────┘ + # └───────────────┘ └───────────────┘ + # Replica 0 Replica 1 + # (4 GPUs) (4 GPUs) + + left_bundle_count = self.world_size + + pgs = [] + bundle_indices = [] + + for pg in self.resource_pool.pgs[start_pg_index:]: + if left_bundle_count == 0: + break + + left_bundle_count_in_pg = min(left_bundle_count, pg.bundle_count - local_bundle_index) + pg_bundle_indices = [local_bundle_index + idx for idx in range(left_bundle_count_in_pg)] + pgs.append(pg) + bundle_indices.append(pg_bundle_indices) + left_bundle_count -= left_bundle_count_in_pg + local_bundle_index = 0 + + assert left_bundle_count == 0, "all bundle indices should be assigned" + + return pgs, bundle_indices + + async def launch_servers(self): + assert self.resource_pool.pgs is not None, "placement groups are not initialized" + + pgs, bundle_indices = self.get_pgs_and_bundle_indices() + + # Check server process should be launched on the same node as first bundle of first pg. + first_pg_data = placement_group_table(pgs[0]) + node_id = first_pg_data["bundles_to_node_id"][bundle_indices[0][0]] + print(f"TRTLLMReplica: {self.replica_rank}") + print(f"pg node_id: {node_id}") + print(f"pgs: {pgs}") + print(f"bundle_indices: {bundle_indices}") + + # TRTLLMReplica is a 1:1 map from replica to TRTLLMHttpServer. + name = ( + f"trtllm_server_{self.replica_rank}{self.name_suffix}" + if not self.is_reward_model + else f"trtllm_server_reward_{self.replica_rank}{self.name_suffix}" + ) + _server_env_vars = {var: "1" for var in get_platform().ray_noset_envvars()} + _server_env_vars.update(get_platform().rollout_env_vars()) + # Propagate profiling env vars to the Ray actor so that RayExecutor + # (instantiated inside TRTLLMHttpServer) picks them up for inner workers. + for _prof_var in ( + "TLLM_ENABLE_NSYS", + "TLLM_NSYS_OUTPUT_DIR", + "TLLM_USE_TORCHSAMPLER", + ): + if _val := os.environ.get(_prof_var): + _server_env_vars[_prof_var] = _val + server = TRTLLMHttpServer.options( + scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy( + node_id=node_id, + soft=False, + ), + runtime_env={"env_vars": _server_env_vars}, + name=name, + max_concurrency=self.max_concurrency, + ).remote( + config=self.config, + model_config=self.model_config, + is_reward_model=self.is_reward_model, + rollout_mode=self.rollout_mode, + workers=self.workers, + replica_rank=self.replica_rank, + max_colocate_count=self.resource_pool.max_colocate_count, + pgs=pgs, + bundle_indices=bundle_indices, + ) + self.servers.append(server) + + # launch http server in each node + await asyncio.gather(*[server.launch_server.remote() for server in self.servers]) + + # get http server address from first server + server_address, server_port = await self.servers[0].get_server_address.remote() + self._server_handle = self.servers[0] + self._server_address = ( + f"[{server_address}]:{server_port}" + if is_valid_ipv6_address(server_address) + else f"{server_address}:{server_port}" + ) diff --git a/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_rollout.py b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..0d694937f27eabe38becdef2294d32600278037d --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_rollout.py @@ -0,0 +1,544 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +from __future__ import annotations + +import asyncio +import base64 +import contextlib +import gc +import logging +import os +import pickle +import threading +from contextlib import asynccontextmanager +from typing import Any, AsyncGenerator, Optional + +import aiohttp +import pynvml +import ray +import torch +import torch.distributed as dist + +try: + from tensorrt_llm.llmapi.llm_args import ExecutorMemoryType +except (ImportError, RuntimeError): + # RuntimeError: FlashInfer's check_cuda_arch() crashes on CPU-only actors + ExecutorMemoryType = None +from torch.distributed.device_mesh import DeviceMesh, init_device_mesh +from torch.multiprocessing.reductions import reduce_tensor + +from verl.utils.device import get_torch_device +from verl.utils.net_utils import is_valid_ipv6_address +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.base import BaseRollout +from verl.workers.rollout.utils import ensure_async_iterator + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +# Default configuration constants +DEFAULT_TIMEOUT = 60.0 +DEFAULT_MAX_ATTEMPTS = 3 +DEFAULT_RETRY_DELAY = 2.0 +DEFAULT_MAX_CONNECTIONS = 2000 +DEFAULT_MAX_WAIT_TIME = 300.0 + + +@contextlib.contextmanager +def nvml_context(): + """Context manager for NVML initialization and shutdown. + + Raises: + RuntimeError: If NVML initialization fails + """ + try: + pynvml.nvmlInit() + yield + except pynvml.NVMLError as e: + raise RuntimeError(f"Failed to initialize NVML: {e}") from e + finally: + try: + pynvml.nvmlShutdown() + except pynvml.NVMLError: + pass + + +_NVML_INITIALIZED = False +_NVML_LOCK = threading.Lock() + + +def get_device_uuid(id: str | int) -> str: + """Get the UUID of a CUDA device using NVML.""" + id = int(id) # pynvml expects int; ray.get_gpu_ids() may return str + global _NVML_INITIALIZED + with _NVML_LOCK: + if not _NVML_INITIALIZED: + try: + pynvml.nvmlInit() + _NVML_INITIALIZED = True + except pynvml.NVMLError as e: + raise RuntimeError(f"Failed to initialize NVML: {e}") from e + + # Get the device handle and UUID + try: + handle = pynvml.nvmlDeviceGetHandleByIndex(id) + uuid = pynvml.nvmlDeviceGetUUID(handle) + # Ensure the UUID is returned as a string, not bytes + if isinstance(uuid, bytes): + return uuid.decode("utf-8") + elif isinstance(uuid, str): + return uuid + else: + raise RuntimeError(f"Unexpected UUID type: {type(uuid)} for device {id} (global index: {id})") + except pynvml.NVMLError as e: + raise RuntimeError(f"Failed to get device UUID for device {id} (global index: {id}): {e}") from e + + +async def _read_async_response(resp: aiohttp.ClientResponse) -> dict[str, Any]: + if resp.status == 204 or (resp.content_length == 0): + return {} + + try: + return await resp.json(content_type=None) + except Exception: + try: + text = await resp.text() + except Exception: + return {} + return { + "content_type": (resp.headers.get("Content-Type") or ""), + "text": text, + } + + +class AsyncTRTLLMHttpAdapter: + def __init__( + self, + host: str, + port: int, + timeout: float = DEFAULT_TIMEOUT, + max_attempts: int = DEFAULT_MAX_ATTEMPTS, + retry_delay: float = DEFAULT_RETRY_DELAY, + max_connections: int = DEFAULT_MAX_CONNECTIONS, + ): + self.host = host + self.port = port + self.timeout = timeout + self.max_attempts = max_attempts + self.retry_delay = retry_delay + self.max_connections = max_connections + + @asynccontextmanager + async def _get_session(self) -> aiohttp.ClientSession: + """Context manager for safe session access with proper connection pooling. + + Yields: + aiohttp.ClientSession: Session instance for making HTTP requests + + Note: + This method creates a new session for each request to avoid resource competition + while still maintaining proper connection pooling through the shared connector. + """ + # Create a new session for each request to avoid resource competition + connector = aiohttp.TCPConnector( + limit=self.max_connections, + limit_per_host=self.max_connections // 4, + ttl_dns_cache=300, + use_dns_cache=True, + ) + timeout = aiohttp.ClientTimeout(total=self.timeout) + session = aiohttp.ClientSession(connector=connector, timeout=timeout) + + try: + yield session + finally: + # Always close the session to free up resources + if not session.closed: + await session.close() + + async def _make_async_request( + self, + endpoint: str, + payload: Optional[dict[str, Any]] = None, + timeout: float = DEFAULT_TIMEOUT, + method: str = "POST", + return_status: bool = False, + ) -> dict[str, Any] | int: + """Make an async HTTP request with retry logic and consistent error handling. + + Args: + endpoint (str): The API endpoint to call (without leading slash) + payload (Optional[Dict[str, Any]], optional): The JSON payload to send. + Defaults to empty dict if None. + method (str, optional): HTTP method to use. Defaults to "POST". + + Returns: + Dict[str, Any]: The JSON response from the server + + Raises: + aiohttp.ClientResponseError: If the HTTP request fails with a client/server error + RuntimeError: If all retry attempts are exhausted + + Note: + - Uses exponential backoff for retries + - Logs warnings for timeout and connection errors, errors for HTTP errors + """ + + url = f"http://{self.host}:{self.port}/{endpoint}" + + for attempt in range(self.max_attempts): + try: + async with self._get_session() as session: + if method.upper() == "GET": + async with session.get(url, timeout=timeout) as response: + response.raise_for_status() + return response.status if return_status else await _read_async_response(response) + else: + async with session.post(url, json=payload or {}, timeout=timeout) as response: + response.raise_for_status() + return response.status if return_status else await _read_async_response(response) + + except asyncio.TimeoutError: + logger.warning(f"Async request to {endpoint} timed out (attempt {attempt + 1})") + except aiohttp.ClientConnectorError: + logger.warning(f"Connection error for {endpoint} (attempt {attempt + 1})") + except aiohttp.ClientResponseError as e: + logger.error(f"HTTP error for {endpoint}: {e}") + raise + except Exception as e: + logger.error(f"Unexpected error for {endpoint}: {e}") + if attempt == self.max_attempts - 1: + raise + + if attempt < self.max_attempts - 1: + await asyncio.sleep(self.retry_delay * (2**attempt)) + + raise RuntimeError(f"Failed to complete async request to {endpoint} after {self.max_attempts} attempts") + + async def resume_memory_occupation(self, tags: list[str]): + """Resume GPU memory occupation (async version). + + Similar to AsyncEngine, this method handles first-time weight reloading + by calling release_memory_occupation if needed. + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to resume. + If None, resumes all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory resume status + """ + return await self._make_async_request("resume_memory", {"tags": tags}) + + async def release_memory_occupation(self, tags: list[str]): + """Release GPU memory occupation temporarily (async version). + + Args: + tags (Optional[List[str]], optional): List of tags to specify which memory to release. + If None, releases all memory. Defaults to None. ["weights", "kv_cache"] + + Returns: + Dict[str, Any]: Server response indicating memory release status + """ + return await self._make_async_request("release_memory", {"tags": tags}) + + async def update_weights(self, weights: dict[str, str]): + """Update model weights from tensor data asynchronously. + + Args: + weights: A dictionary that maps the device uuid of the weight handles. + + Returns: + Dict[str, Any]: Server response containing update status + """ + return await self._make_async_request("update_weights", {"weights": weights}) + + +class ServerAdapter(BaseRollout): + # All releasable/resumable weight tags: every ExecutorMemoryType except kv_cache + # (handled separately) and internal tags prefixed with "_". + # Fallback to hard-coded list for trtllm versions that don't export ExecutorMemoryType. + _WEIGHTS_TAGS = ( + [t.value for t in ExecutorMemoryType if t is not ExecutorMemoryType.KV_CACHE and not t.value.startswith("_")] + if ExecutorMemoryType is not None + else [ + "sampler", + "drafter", + "guided_decoder", + "spec_resource_manager", + "model_extra", + "executor_extra", + "model", + "draft_model", + ] + ) + + @staticmethod + def get_full_tags() -> list[str]: + return ServerAdapter._WEIGHTS_TAGS + ["kv_cache"] + + def __init__( + self, config: RolloutConfig, model_config: HFModelConfig, device_mesh: DeviceMesh, replica_rank: int = -1 + ): + super().__init__(config, model_config, device_mesh) + if self.config.quantization == "fp8" and self.model_config.hf_config is not None: + FP8_BLOCK_QUANT_KWARGS = { + "activation_scheme": "dynamic", + "fmt": "e4m3", + "quant_method": "fp8", + "weight_block_size": [128, 128], + } + self.model_config.hf_config.quantization_config = dict(FP8_BLOCK_QUANT_KWARGS) + self._adapter = None + self.hybrid_device_mesh = None + self.gpu_id = None + self.is_leader_rank = None + self.replica_rank = None + self.is_dp_rank = None + self._supports_partial_loading = None + + # hybrid mode + if self.device_mesh is not None: + assert device_mesh.mesh_dim_names.index("dp") == 0, "DP dim should always be the first dimension" + + # Clone a new device mesh for CPU backend only (used for internal ranks communication) + device_mesh_kwargs = dict( + mesh_shape=device_mesh.mesh.shape, + mesh_dim_names=device_mesh.mesh_dim_names, + ) + self.hybrid_device_mesh = init_device_mesh("cpu", **device_mesh_kwargs) + + self.hybrid_device_mesh[self.hybrid_device_mesh.mesh_dim_names[1:]]._flatten(mesh_dim_name="exclude_dp") + self.is_leader_rank = self.hybrid_device_mesh["exclude_dp"].get_local_rank() == 0 + logger.info(f"is_dp_leader: {self.is_leader_rank}") + logger.info(f"exclude_dp_rank = {self.hybrid_device_mesh['exclude_dp'].get_local_rank()}") + logger.info(f"exclude_dp_size = {self.hybrid_device_mesh['exclude_dp'].size()}") + self.gpu_id = ray.get_gpu_ids()[0] + self.replica_rank = self.hybrid_device_mesh["dp"].get_local_rank() + assert len(ray.get_gpu_ids()) == 1, "ServerAdapter should run on a single GPU node" + else: + rank = int(os.environ["RANK"]) + self.replica_rank = replica_rank + self.is_leader_rank = rank == 0 + # Required for CUDA IPC handle creation during weight sync for Async RL. + # Reward/ref models skip weight sync so this can be None. + self.gpu_id = ray.get_gpu_ids()[0] + + # Below is required for all modes. + assert self.replica_rank >= 0, "replica_rank is not set" + assert self.is_leader_rank is not None, "is_leader_rank is not set" + + self.node_ip = ray.util.get_node_ip_address().strip("[]") + + async def get_supports_partial_loading(self) -> bool: + """Query and cache whether the model supports partial weight loading.""" + if self._supports_partial_loading is not None: + return self._supports_partial_loading + + await self._init_server_adapter() + try: + self._supports_partial_loading = await self.server_actor.supports_partial_loading.remote() + except Exception as e: + logger.warning(f"Failed to query partial loading support: {e}, defaulting to False") + self._supports_partial_loading = False + + logger.info(f"Model supports partial loading: {self._supports_partial_loading}") + return self._supports_partial_loading + + async def _init_server_adapter(self): + if self._adapter is not None: + return + + # Standalone mode: lazily build the CPU device mesh from the gloo process group + # (initialized by initialize_global_process_group_ray before ServerAdapter construction). + # Reward/ref models that never call resume(), release(), or update_weights() will never build the mesh. + if self.hybrid_device_mesh is None and self.device_mesh is None: + assert dist.is_initialized(), "gloo process group must be initialized before building device mesh" + infer_tp = self.config.tensor_model_parallel_size + infer_pp = getattr(self.config, "pipeline_model_parallel_size", 1) + world_size = dist.get_world_size() + dp = world_size // (infer_tp * infer_pp) + self.hybrid_device_mesh = init_device_mesh( + "cpu", mesh_shape=(dp, infer_tp, infer_pp), mesh_dim_names=["dp", "infer_tp", "infer_pp"] + ) + self.hybrid_device_mesh[self.hybrid_device_mesh.mesh_dim_names[1:]]._flatten(mesh_dim_name="exclude_dp") + self.is_leader_rank = self.hybrid_device_mesh["exclude_dp"].get_local_rank() == 0 + + # Lazy init http server adapter because http server is launched after hybrid engine. + self.server_actor = ray.get_actor(f"trtllm_server_{self.replica_rank}") + server_address, server_port = await self.server_actor.get_server_address.remote() + assert server_address == self.node_ip, f"server address: {server_address} != node_ip: {self.node_ip}" + + logger.debug(f"replica_rank={self.replica_rank}, server address: {server_address}, port: {server_port}") + host = f"[{server_address}]" if is_valid_ipv6_address(server_address) else server_address + self._adapter = AsyncTRTLLMHttpAdapter( + host=host, + port=server_port, + timeout=self.config.server.timeout, + max_attempts=self.config.server.max_attempts, + retry_delay=self.config.server.retry_delay, + max_connections=self.config.server.max_connections, + ) + + async def resume(self, tags: list[str]): + """Resume rollout weights or kv cache in GPU memory. + + Args: + tag: weights or kv_cache. + """ + # Synchronize all ranks before resuming KV cache to ensure non-leader ranks + # have completed actor offloading to CPU, preventing OOM issue. + if "kv_cache" in tags and self.config.free_cache_engine: + group = self.hybrid_device_mesh["exclude_dp"].get_group() if self.hybrid_device_mesh is not None else None + await asyncio.to_thread(dist.barrier, group=group) + if self.is_leader_rank and self.config.free_cache_engine: + if "weights" in tags: + tags = self._WEIGHTS_TAGS + elif "kv_cache" in tags: + tags = ["kv_cache"] + else: + raise ValueError(f"Invalid tag: {tags}") + await self._init_server_adapter() + await self._adapter.resume_memory_occupation(tags=tags) + + async def release(self): + """Release weights and kv cache in GPU memory.""" + if self.is_leader_rank and self.config.free_cache_engine: + await self._init_server_adapter() + tags = self._WEIGHTS_TAGS + ["kv_cache"] + await self._adapter.release_memory_occupation(tags=tags) + + async def update_weights_from_ipc_handles(self, device_handles): + """Update weights from IPC handles.""" + if self.hybrid_device_mesh is not None: + world_size = self.hybrid_device_mesh["exclude_dp"].size() + group = self.hybrid_device_mesh["exclude_dp"].get_group() + else: + world_size = dist.get_world_size() + group = None + + if self.is_leader_rank: + gathered_handles = [None for _ in range(world_size)] + else: + gathered_handles = None + + await asyncio.to_thread( + dist.gather_object, + obj=device_handles, + object_gather_list=gathered_handles, + group_dst=0, + group=group, + ) + + if self.is_leader_rank: + all_handles = {k: v for d in gathered_handles for k, v in d.items()} + await self._adapter.update_weights(all_handles) + + await asyncio.to_thread(dist.barrier, group=group) + + async def update_weights( + self, + weights: AsyncGenerator[tuple[str, torch.Tensor], None], + global_steps: int = None, + wire_format: str = "named_tensors", + **kwargs, + ): + """Update the weights of the rollout model. + + Args: + weights: A generator that yields the name of the weight tensor and the tensor itself. + """ + assert wire_format == "named_tensors", ( + f"TensorRT-LLM rollout only consumes full named tensors; got wire_format={wire_format!r}" + ) + if self.is_leader_rank: + await self._init_server_adapter() + + total_available_bytes = int(self.config.checkpoint_engine.update_weights_bucket_megabytes) * 1024 * 1024 + + if self.config.get("quantization", None) == "fp8": + from verl.utils.trtllm.trtllm_fp8_utils import TRTLLMFP8QuantizerHelper + + fp8_quantizer_helper = TRTLLMFP8QuantizerHelper(self.model_config.hf_config.quantization_config) + weights = fp8_quantizer_helper.quant_weights_by_name( + weights, + dtype=self.model_config.hf_config.dtype, + ) + + try: + device_uuid = get_device_uuid(int(self.gpu_id)) + except Exception as e: + logger.error(f"Failed to get device UUID in update_weights(): {e}") + device_uuid = None + raise e + + cur_available_bytes = total_available_bytes + cur_handles = [] + + async def flush(): + nonlocal cur_available_bytes, cur_handles + if not cur_handles: + return + serialized_device_handles = {device_uuid: base64.b64encode(pickle.dumps(cur_handles)).decode("utf-8")} + await self.update_weights_from_ipc_handles(serialized_device_handles) + cur_available_bytes = total_available_bytes + cur_handles = [] + + # For non-VLM, always use partial loading. For VLM, leader queries and broadcasts to all + # ranks in the DP replica; use get_global_rank(group, 0) since each replica has a different leader. + is_vlm = self.model_config.hf_config is not None and hasattr(self.model_config.hf_config, "vision_config") + if not is_vlm: + supports_partial_loading = True + else: + exclude_dp_group = self.hybrid_device_mesh["exclude_dp"].get_group() + spl_tensor = torch.zeros(1, dtype=torch.int32) + if self.is_leader_rank: + supports_partial_loading = await self.get_supports_partial_loading() + spl_tensor[0] = int(supports_partial_loading) + leader_global_rank = dist.get_global_rank(exclude_dp_group, 0) + await asyncio.to_thread(dist.broadcast, spl_tensor, src=leader_global_rank, group=exclude_dp_group) + supports_partial_loading = bool(spl_tensor.item()) + + async for name, param in ensure_async_iterator(weights): + if supports_partial_loading: + size_in_bytes = param.element_size() * param.numel() + if size_in_bytes > cur_available_bytes: + await flush() + + assert cur_available_bytes >= size_in_bytes, ( + f"cur_available_bytes: {cur_available_bytes:,} size_in_bytes: {size_in_bytes:,} name: {name}" + ) + cur_available_bytes -= size_in_bytes + + # Clone so the IPC handle owns the data to avoid buffer reuse + # before TRT-LLM reads it, which will silently corrupt weights. + handle = reduce_tensor(param.detach().clone()) + cur_handles.append((name, handle)) + + await flush() + + if self.is_leader_rank: + # Finalize update weights + await self._adapter.update_weights(None) + if global_steps is not None: + await self.server_actor.set_global_steps.remote(global_steps) + group = self.hybrid_device_mesh["exclude_dp"].get_group() if self.hybrid_device_mesh is not None else None + await asyncio.to_thread(dist.barrier, group=group) + + del weights + gc.collect() + get_torch_device().empty_cache() + + def _get_attribute(self, name: str): + return getattr(self, name) diff --git a/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_worker_extension.py b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_worker_extension.py new file mode 100644 index 0000000000000000000000000000000000000000..5bcada65746781ff6460236d2f8f8ec0fbae9dcf --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/trtllm_rollout/trtllm_worker_extension.py @@ -0,0 +1,198 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import base64 +import inspect +from typing import Optional + +# Defer tensorrt_llm imports to avoid FlashInfer's check_cuda_arch() crash +# when this module is loaded on CPU-only Ray actors. The module is normally +# loaded only on GPU workers via string path in trtllm_async_server.py, but +# guard defensively in case of transitive imports. +try: + from tensorrt_llm import serialization + from tensorrt_llm._ray_utils import control_action_decorator + from tensorrt_llm._torch.modules.fused_moe.moe_load_balancer import MoeLoadBalancer + from tensorrt_llm._torch.utils import get_device_uuid + from tensorrt_llm.llmapi.rlhf_utils import WorkerExtension as TrtllmWorkerExtension + from tensorrt_llm.logger import logger +except (ImportError, RuntimeError): + # On CPU actors without CUDA, these imports may fail. + # The class below won't be usable, but the module can be imported safely. + serialization = None + control_action_decorator = lambda f: f # noqa: E731 — identity fallback + MoeLoadBalancer = None + get_device_uuid = None + TrtllmWorkerExtension = object + logger = None + + +class WorkerExtension(TrtllmWorkerExtension): + def __init__(self): + pass + + @control_action_decorator + def supports_partial_loading(self) -> bool: + """Check if the model supports partial weight loading.""" + try: + model = self.engine.model_engine.model + load_weights_args = inspect.getfullargspec(model.load_weights).args + return "allow_partial_loading" in load_weights_args + except Exception as e: + logger.warning(f"Failed to check partial loading support: {e}") + return False + + @control_action_decorator + def update_weights(self, ipc_handles: Optional[dict] = None): + try: + if not hasattr(self.engine.model_engine.model, "first_pre_reload_weights"): + for module in self.engine.model_engine.model.modules(): + if hasattr(module, "pre_reload_weights") and not getattr(module, "_weights_removed", False): + module.pre_reload_weights() + self.engine.model_engine.model.first_pre_reload_weights = True + + if ipc_handles is not None: + logger.info("Update weights from IPC handles") + device_uuid = get_device_uuid(self.device_id) + + if device_uuid not in ipc_handles: + raise ValueError(f"Device UUID {device_uuid} not found in ipc_handles") + + weights = {} + + serialized_handles = ipc_handles[device_uuid] + if isinstance(serialized_handles, str): + # Data is base64-encoded pickled bytes - deserialize it + # using restricted unpickler from tensorrt_llm.serialization + logger.info("Deserializing base64-encoded weight handles") + decoded_data = base64.b64decode(serialized_handles) + # Allow basic builtins and torch tensor reconstruction classes + approved_imports = { + "builtins": [ + "list", + "tuple", + "str", + "int", + "float", + "bool", + "bytes", + "dict", + "NoneType", + "type", + ], + "torch": [ + "Tensor", + "FloatTensor", + "DoubleTensor", + "HalfTensor", + "BFloat16Tensor", + "IntTensor", + "LongTensor", + "ShortTensor", + "CharTensor", + "ByteTensor", + "BoolTensor", + "Size", + "dtype", + "device", + "float32", + "float16", + "bfloat16", + "int32", + "int64", + "int16", + "int8", + "uint8", + "bool", + ], + "torch.multiprocessing.reductions": [ + "rebuild_cuda_tensor", + "rebuild_tensor", + ], + "torch._utils": [ + "_rebuild_tensor_v2", + ], + "torch.storage": [ + "_load_from_bytes", + "_TypedStorage", + "UntypedStorage", + "TypedStorage", + ], + } + all_handles = serialization.loads( + decoded_data, + approved_imports=approved_imports, + ) + + # Verify the result is a list as expected + if not isinstance(all_handles, list): + raise ValueError(f"Deserialized data must be a list, got {type(all_handles).__name__} instead") + else: + # Data is already in the correct format (backward compatibility) + all_handles = serialized_handles + + for param_name, tensor_handle in all_handles: + func, args = tensor_handle + list_args = list(args) + list_args[6] = self.device_id + tensor = func(*list_args) + weights[param_name] = tensor + + logger.info(f"weights key size: {len(weights.keys())}") + + # Check if model supports partial loading and use appropriate strategy + model = self.engine.model_engine.model + load_weights_args = inspect.getfullargspec(model.load_weights).args + supports_partial_loading = "allow_partial_loading" in load_weights_args + + if supports_partial_loading: + self.engine.model_engine.model_loader.reload(model, weights, allow_partial_loading=True) + else: + self.engine.model_engine.model_loader.reload(model, weights, allow_partial_loading=False) + else: + logger.info("Finalize update weights") + for module in self.engine.model_engine.model.modules(): + if hasattr(module, "process_weights_after_loading") and not getattr( + module, "_weights_removed", False + ): + module.process_weights_after_loading() + if hasattr(module, "post_load_weights") and not getattr(module, "_weights_removed", False): + module.post_load_weights() + moe_load_balancer = getattr(self.engine.model_engine, "moe_load_balancer", None) + if isinstance(moe_load_balancer, MoeLoadBalancer): + moe_load_balancer.register_weight_slots_after_to_cuda() + logger.info("moe_load_balancer finalizing model...") + moe_load_balancer.finalize_model() + logger.info("moe_load_balancer finalize model done") + self.engine.reset_prefix_cache() + delattr(self.engine.model_engine.model, "first_pre_reload_weights") + + except Exception as e: + logger.error("Encountered an error in update_weights") + raise e + + def reset_prefix_cache(self) -> None: + """Invalidate the KV cache prefix reuse state after weight updates.""" + self.engine.reset_prefix_cache() + + +# TODO: remove this class and revert the non-VLM path in trtllm_async_server.py +# to use "tensorrt_llm.llmapi.rlhf_utils.WorkerExtension" once verl's TRT-LLM version +# is bumped to include https://github.com/NVIDIA/TensorRT-LLM/pull/13784. +class RlhfWorkerExtension(TrtllmWorkerExtension): + """Minimal extension of TRT-LLM's WorkerExtension for non-VLM RLHF models.""" + + @control_action_decorator + def wait_for_engine_idle(self) -> None: + """Block until the engine has no active or queued requests.""" + pass diff --git a/verl_0720_main/verl/verl/workers/rollout/utils.py b/verl_0720_main/verl/verl/workers/rollout/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..06a1daefeef62249390a5d5c8bb1d4fa7b376788 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/utils.py @@ -0,0 +1,223 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import asyncio +import logging +import os + +import numpy as np +import ray +import uvicorn +import yaml +from fastapi import FastAPI + +from verl.utils.tokenizer import get_processor_token_id +from verl.workers.config.rollout import PrometheusConfig + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + + +def get_max_position_embeddings(hf_config) -> int: + max_len = getattr(hf_config, "max_position_embeddings", None) + if max_len is None: + text_config = getattr(hf_config, "text_config", None) + if text_config is not None: + max_len = getattr(text_config, "max_position_embeddings", None) + + if max_len is None: + raise ValueError("max_position_embeddings not found in HFModelConfig!") + return int(max_len) + + +class _UvicornServerAutoPort(uvicorn.Server): + """Uvicorn Server that reports the system-assigned port when port=0.""" + + def __init__(self, config: uvicorn.Config) -> None: + super().__init__(config) + self.actual_port: int | None = None + self._startup_done: asyncio.Event = asyncio.Event() + + async def startup(self, sockets=None) -> None: + try: + await super().startup(sockets=sockets) + if self.servers and self.config.port == 0: + sock = self.servers[0].sockets[0] + self.actual_port = sock.getsockname()[1] + else: + self.actual_port = self.config.port + finally: + self._startup_done.set() + + async def get_port(self) -> int | None: + await self._startup_done.wait() + return self.actual_port + + +async def run_uvicorn(app: FastAPI, server_args, server_address) -> tuple[int, asyncio.Task]: + app.server_args = server_args + config = uvicorn.Config(app, host=server_address, port=0, log_level="warning") + server = _UvicornServerAutoPort(config) + server_task = asyncio.create_task(server.serve()) + server_port = await server.get_port() + if server_port is None: + # server.startup() failed. await the task to re-raise exception from server.serve() + await server_task + + # Fails on unexpected situation. + raise RuntimeError("Unexpected: HTTP server started without reporting listened port") + logger.info(f"HTTP server started on port {server_port}") + return server_port, server_task + + +async def ensure_async_iterator(iterable): + """Convert an iterable to an async iterator.""" + if hasattr(iterable, "__aiter__"): + async for item in iterable: + yield item + else: + for item in iterable: + yield item + + +def qwen2_5_vl_dedup_image_tokens(prompt_ids: list[int], processor): + """Deduplicate consecutive image tokens in prompt_ids for Qwen2.5-VL, since vLLM will replicate the + <|image_pad|> and <|video_pad|> token by image_data. + For example, + ``` + <|vision_start|><|image_pad|><|image_pad|>...<|image_pad|><|vision_end|> + => + <|vision_start|><|image_pad|><|vision_end|> + ``` + """ + if ( + processor is not None + and hasattr(processor, "image_processor") + and "Qwen2VLImageProcessor" in processor.image_processor.__class__.__name__ + ): + prompt_ids = np.array(prompt_ids) + mask = np.ones(len(prompt_ids), dtype=bool) + is_value = (prompt_ids == processor.image_token_id) | (prompt_ids == processor.video_token_id) + mask[1:] &= ~(is_value[1:] & is_value[:-1]) + return prompt_ids[mask].tolist() + else: + return prompt_ids + + +def get_vision_placeholder_token_ids(processor) -> list[int]: + """Vision placeholder token ids the policy must never sample. + + An `<|image_pad|>` or `<|video_pad|>` token only means something when a real image or video + sits behind it: the k-th run of placeholders pairs with the k-th row of `image_grid_thw` / + `video_grid_thw`. Nothing stops the policy from sampling one anyway -- it is an ordinary entry + of the vocabulary -- and then that pairing silently breaks. Every downstream consumer trusts + it, so each one has its own way of dying: `get_rope_index` walks off the end of the grid + iterator, and `merge_multimodal_embeddings` scatters into a mask that no longer matches the + embedding count. Both take the whole training job down with them. + + A tool that returns an image is unaffected: that image arrives in the next turn's prompt, not + in what the model generates. + + Returns an empty list for text-only models, leaving sampling untouched. + """ + token_ids = [] + for modality in ("image", "video"): + token_id = get_processor_token_id(processor, modality) + if token_id is not None: + token_ids.append(token_id) + return token_ids + + +def update_prometheus_config(config: PrometheusConfig, server_addresses: list[str], rollout_name: str | None = None): + """ + Update Prometheus configuration file with server addresses and reload on first node. + + server_addresses: vllm or sglang server addresses + + rollout_name: name of the rollout backend (e.g., "vllm", "sglang") + """ + + if not server_addresses: + logger.warning("No server addresses available to update Prometheus config") + return + + try: + # Get Prometheus config file path from environment or use default + prometheus_config_json = { + "global": {"scrape_interval": "10s", "evaluation_interval": "10s"}, + "scrape_configs": [ + { + "job_name": "ray", + "file_sd_configs": [{"files": ["/tmp/ray/prom_metrics_service_discovery.json"]}], + }, + {"job_name": "rollout", "static_configs": [{"targets": server_addresses}]}, + ], + } + + # Write configuration file to all nodes + @ray.remote(num_cpus=0) + def write_config_file(config_data, config_path): + os.makedirs(os.path.dirname(config_path), exist_ok=True) + with open(config_path, "w") as f: + yaml.dump(config_data, f, default_flow_style=False, indent=2) + return True + + # Reload Prometheus on all nodes. Only master node should succeed, skip errors on other nodes. + @ray.remote(num_cpus=0) + def reload_prometheus(port): + import socket + import subprocess + + hostname = socket.gethostname() + ip_address = socket.gethostbyname(hostname) + + reload_url = f"http://{ip_address}:{port}/-/reload" + + try: + subprocess.run(["curl", "-X", "POST", reload_url], capture_output=True, text=True, timeout=10) + print(f"Reloading Prometheus on node: {reload_url}") + except Exception: + # Skip errors on non-master nodes + pass + + # Get all available nodes and schedule tasks on each node + nodes = ray.nodes() + alive_nodes = [node for node in nodes if node["Alive"]] + + # Write config files on all nodes + write_tasks = [] + for node in alive_nodes: + node_ip = node["NodeManagerAddress"] + task = write_config_file.options( + resources={"node:" + node_ip: 0.001} # Schedule to specific node + ).remote(prometheus_config_json, config.file) + write_tasks.append(task) + + ray.get(write_tasks) + + server_type = rollout_name.upper() if rollout_name else "rollout" + print(f"Updated Prometheus configuration at {config.file} with {len(server_addresses)} {server_type} servers") + + # Reload Prometheus on all nodes + reload_tasks = [] + for node in alive_nodes: + node_ip = node["NodeManagerAddress"] + task = reload_prometheus.options( + resources={"node:" + node_ip: 0.001} # Schedule to specific node + ).remote(config.port) + reload_tasks.append(task) + + ray.get(reload_tasks) + + except Exception as e: + logger.error(f"Failed to update Prometheus configuration: {e}") diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/__init__.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..2ecf113c8394c651655e80fb3780837cd85288c3 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/__init__.py @@ -0,0 +1,42 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import os +from importlib.metadata import PackageNotFoundError, version + +from .vllm_rollout import ServerAdapter # noqa: F401 + + +def get_version(pkg): + try: + return version(pkg) + except PackageNotFoundError: + return None + + +vllm_package_name = "vllm" +vllm_package_version = get_version(vllm_package_name) +if vllm_package_version is None: + raise PackageNotFoundError( + "To use vllm rollout, please ensure the 'vllm' package is properly installed. See " + "https://verl.readthedocs.io/en/latest/start/install.html for more details" + ) + +if "ROCM_PATH" in os.environ: + import re + + match = re.match(r"(\d+\.\d+\.?\d*)", vllm_package_version) + if match: + vllm_package_version = match.group(1) + else: + raise ValueError(f"Warning: Could not parse version format: {vllm_package_version}") diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/bucketed_weight_transfer.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/bucketed_weight_transfer.py new file mode 100644 index 0000000000000000000000000000000000000000..7e6540105a0247fa8f05168db2ffc93876ca5088 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/bucketed_weight_transfer.py @@ -0,0 +1,335 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +Bucketed weight transfer via ZMQ + IPC (or shared memory fallback). + +Not recommended depending on vllm for this file. +""" + +import gc +import logging +import os +from multiprocessing import shared_memory +from typing import Callable, TypedDict + +import torch +import zmq +from torch.multiprocessing.reductions import reduce_tensor + +from verl.utils.device import get_device_id, get_device_name, get_torch_device + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "INFO")) + + +class TensorMetadata(TypedDict): + name: str + shape: torch.Size + dtype: torch.dtype + offset: int + handle: tuple + + +# copy from https://github.com/vllm-project/vllm/blob/main/examples/offline_inference/rlhf_utils.py +def rebuild_ipc(handle: tuple[Callable, tuple], device_id: int | None = None) -> torch.Tensor: + func, args = handle + list_args = list(args) + if device_id is not None: + # the key is to change device id to the current device id + # in case two processes have different CUDA_VISIBLE_DEVICES + list_args[6] = device_id + buffer = func(*list_args) + return buffer + + +def create_shared_memory(size: int, name: str): + """Create shared memory for weight transfer. If already exists, attach to it.""" + try: + shm = shared_memory.SharedMemory(name=name, create=True, size=size) + except FileExistsError: + shm = shared_memory.SharedMemory(name=name) + assert shm.size >= size, f"Stale shm segment '{name}': expected {size} bytes, got {shm.size}" + return shm + + +def rebuild_shared_memory(name: str, size: int, dtype=torch.uint8): + """Rebuild tensor from shared memory.""" + shm = shared_memory.SharedMemory(name=name) + tensor = torch.frombuffer(shm.buf[:size], dtype=dtype) + + return tensor, shm + + +class BucketedWeightSender: + """ + Send model weights via bucketed IPC transfer over ZMQ. + + Packs weight tensors into a fixed-size communication buffer and sends them + in buckets to the receiver. Supports CUDA IPC and shared memory fallback. + + Args: + zmq_handle: ZMQ IPC socket path (e.g., "ipc:///tmp/rl-colocate-zmq-.sock") + bucket_size_mb: Communication buffer size in MB + use_shm: Use shared memory instead of CUDA IPC (for NPU compatibility) + """ + + def __init__( + self, + zmq_handle: str, + bucket_size_mb: int = 512, + use_shm: bool = False, + ): + self.zmq_handle = zmq_handle + self.bucket_size_mb = bucket_size_mb + self.bucket_size = int(bucket_size_mb) << 20 + self.use_shm = use_shm + + self.zmq_context = zmq.Context.instance() + self.socket = None + self.buffer = None + self.shm = None + + async def async_send_weights(self, weights): + """ + Send weights to the receiver. Accepts a sync generator or async iterator. + + Args: + weights: Generator or async iterator yielding (name, tensor) pairs + """ + from verl.workers.rollout.utils import ensure_async_iterator + + try: + self._init_socket() + self._init_buffer() + + # send bucket weights + offset = 0 + bucket_meta: dict[str, TensorMetadata] = {} + # dtype = PrecisionType.to_dtype(self.config.dtype) + async for name, weight in ensure_async_iterator(weights): + # model parameters are in fp32 full precision + # (vermouth1992) we should not force cast weight here because some parameters + # (such as moe gate) have to keep fp32 precision. If a weight is bf16 in the rollout side, + # the rollout should automatically cast on demand. However, this would incur a higher weight + # transfer volume. + # weight = weight.to(dtype, non_blocking=True) + + # fill the tensor bucket + if offset + weight.nbytes > self.bucket_size and len(bucket_meta) > 0: + get_torch_device().synchronize() + self.socket.send_pyobj({"bucket_meta": bucket_meta, "is_last": False}) + self.socket.recv() + bucket_meta = {} + offset = 0 + + if offset + weight.nbytes > self.bucket_size: + assert not self.use_shm, ( + f"Weight {name}({weight.shape}, {weight.dtype}) is too large to fit in the bucket." + f"Please increase rollout.update_weights_bucket_megabytes({self.bucket_size_mb} MB)." + ) + self._direct_send_large_weight(name, weight) + continue + + bucket_meta[name] = { + "name": name, + "shape": weight.shape, + "dtype": weight.dtype, + "offset": offset, + "handle": None, + } + self.buffer[offset : offset + weight.nbytes].view(dtype=weight.dtype).view(weight.shape).copy_( + weight, non_blocking=True + ) + offset += weight.nbytes + + # send the last bucket + get_torch_device().synchronize() + self.socket.send_pyobj({"bucket_meta": bucket_meta, "is_last": True}) + self.socket.recv() + finally: + self._cleanup() + + def _init_socket(self): + """Initialize ZMQ REQ socket and bind.""" + if self.zmq_handle.startswith("ipc://"): + ipc_path = self.zmq_handle[len("ipc://") :] + try: + os.remove(ipc_path) + except OSError: + pass + self.socket = self.zmq_context.socket(zmq.REQ) + self.socket.bind(self.zmq_handle) + + def _init_buffer(self): + """build communication buffer""" + buffer, shm = None, None + if not self.use_shm: + buffer = torch.empty(self.bucket_size, dtype=torch.uint8, device=f"{get_device_name()}:{get_device_id()}") + handle = reduce_tensor(buffer) + self.socket.send_pyobj(handle) + else: + import uuid + + # Create unique name for shared memory + shm_name = f"verl_weights_{uuid.uuid4().hex}" + shm = create_shared_memory(self.bucket_size, shm_name) + buffer = torch.frombuffer(shm.buf, dtype=torch.uint8) + + comm_metadata = {"name": shm_name, "size": self.bucket_size} + self.socket.send_pyobj(comm_metadata) + + self.socket.recv() + self.buffer = buffer + self.shm = shm + + def _cleanup(self): + """clean up""" + if self.socket is not None: + self.socket.close() + self.socket = None + if self.zmq_handle.startswith("ipc://"): + ipc_path = self.zmq_handle[len("ipc://") :] + try: + os.remove(ipc_path) + except OSError: + pass + del self.buffer + self.buffer = None + if self.shm is not None: + self.shm.close() + self.shm.unlink() + del self.shm + self.shm = None + gc.collect() + get_torch_device().ipc_collect() + get_torch_device().empty_cache() + + def _direct_send_large_weight(self, name: str, weight: torch.Tensor): + """Send a weight larger than the bucket size via cuda ipc or share memory.""" + logger.debug(f"Direct sending large weight {name}({weight.shape}, {weight.dtype})") + # TODO: support fallback to shared memory + handle = reduce_tensor(weight) + bucket_meta: dict[str, TensorMetadata] = {} + bucket_meta[name] = { + "name": name, + "shape": weight.shape, + "dtype": weight.dtype, + "offset": 0, + "handle": handle, + } + self.socket.send_pyobj({"bucket_meta": bucket_meta, "is_last": False}) + self.socket.recv() + + +class BucketedWeightReceiver: + """ + Receive model weights via bucketed IPC transfer over ZMQ. + + Receives weight tensors from BucketedWeightSender and passes each + bucket to a callback for processing (e.g., loading into the model). + + Args: + zmq_handle: ZMQ IPC socket path (must match sender) + device: Target device for received tensors + use_shm: Use shared memory instead of CUDA IPC + """ + + def __init__( + self, + zmq_handle: str, + device: torch.device, + use_shm: bool = False, + ): + self.zmq_handle = zmq_handle + self.device = device + self.use_shm = use_shm + + self.zmq_context = zmq.Context.instance() + self.socket = None + self.buffer = None + self.shm = None + + def receive_weights(self, on_bucket_received: callable): + """ + Receive weights from sender and process each bucket via callback. + + Args: + on_bucket_received: Callback function(weights: list[(name, tensor)]) called per bucket. + """ + try: + self._init_socket() + self._init_buffer() + + # receive bucket and update weights + while True: + metadata = self.socket.recv_pyobj() + weights, tensor = [], None + for name, meta in metadata["bucket_meta"].items(): + shape, dtype, offset, handle = meta["shape"], meta["dtype"], meta["offset"], meta["handle"] + if handle is not None: + tensor = rebuild_ipc(handle, self.device.index) + weights.append((name, tensor)) + continue + size = dtype.itemsize * shape.numel() + tensor = self.buffer[offset : offset + size].view(dtype=dtype).view(shape) + if self.use_shm: + tensor = tensor.to(self.device) + weights.append((name, tensor)) + on_bucket_received(weights) + get_torch_device().synchronize() + self.socket.send(b"") + del weights, tensor + if metadata["is_last"]: + break + finally: + self._cleanup() + + def _init_socket(self): + """Initialize ZMQ REP socket and connect.""" + self.socket = self.zmq_context.socket(zmq.REP) + self.socket.connect(self.zmq_handle) + + def _init_buffer(self): + """Receive and rebuild communication buffer from sender.""" + comm_metadata = self.socket.recv_pyobj() + buffer, shm = None, None + if not self.use_shm: + handle = comm_metadata + buffer = rebuild_ipc(handle, self.device.index) + assert buffer.dtype == torch.uint8 + else: + shm_name = comm_metadata["name"] + shm_size = comm_metadata["size"] + buffer, shm = rebuild_shared_memory(shm_name, shm_size, dtype=torch.uint8) + self.socket.send(b"") + self.buffer = buffer + self.shm = shm + + def _cleanup(self): + """clean up""" + if self.socket is not None: + self.socket.close() + self.socket = None + # Synchronize before releasing the buffer to ensure all async ops + # referencing it (e.g. clone, .to()) have completed. + get_torch_device().synchronize() + del self.buffer + self.buffer = None + if self.shm is not None: + self.shm.close() + del self.shm + self.shm = None + gc.collect() + get_torch_device().ipc_collect() + get_torch_device().empty_cache() diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/utils.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..077188022ff804cad9b7c5452e73b5c604f5f409 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/utils.py @@ -0,0 +1,517 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import ctypes +import json +import logging +import os +import platform +import signal +import threading +from collections.abc import Mapping +from types import MethodType +from typing import Any, Literal, Optional, get_args + +import torch +from vllm.outputs import RequestOutput + +from verl.plugin.platform import get_platform +from verl.utils.device import is_npu_available +from verl.utils.vllm import TensorLoRARequest, VLLMHijack +from verl.utils.vllm.patch import patch_vllm_moe_model_weight_loader +from verl.utils.vllm.vllm_fp8_utils import apply_vllm_fp8_patches, is_fp8_model, load_quanted_weights +from verl.workers.rollout.vllm_rollout.weight_update_utils import apply_buffer_updates, split_buffer_updates + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) + +# magic numbers that ensure we are using the same LoRA adapter during the rollout and training process +VLLM_LORA_INT_ID = 123 +VLLM_LORA_NAME = "123" +VLLM_LORA_PATH = "simon_lora_path" + +VLLM_ASCEND_REQUIRED_ENV_VARS = {"VLLM_ALL2ALL_BACKEND": "flashinfer_all2allv", "VLLM_ASCEND_ENABLE_NZ": "0"} + + +def _resolve_vllm_weight_sync_local_rank(worker_local_rank: int, parallel_config: Any) -> int: + worker_local_rank = int(worker_local_rank) + if parallel_config is None: + return worker_local_rank + + tp_size = max(int(getattr(parallel_config, "tensor_parallel_size", 1) or 1), 1) + dp_size = int(getattr(parallel_config, "data_parallel_size", 1) or 1) + dp_local_size = int(getattr(parallel_config, "data_parallel_size_local", 1) or 1) + if dp_size <= 1 and dp_local_size <= 1: + return worker_local_rank + + dp_local_rank = getattr(parallel_config, "data_parallel_rank_local", None) + if dp_local_rank is None: + dp_rank = getattr(parallel_config, "data_parallel_rank", None) + if dp_rank is None: + dp_rank = getattr(parallel_config, "data_parallel_index", None) + if dp_rank is not None and dp_local_size > 0: + dp_local_rank = int(dp_rank) % dp_local_size + + if dp_local_rank is None: + return worker_local_rank + + tp_rank = worker_local_rank % tp_size + return int(dp_local_rank) * tp_size + tp_rank + + +def set_death_signal(): + """Kill the current process when the parent process exits.""" + if platform.system() != "Linux": + return + libc = ctypes.CDLL("libc.so.6") + libc.prctl(1, signal.SIGKILL) + if os.getppid() == 1: + os.kill(os.getpid(), signal.SIGKILL) + + +def get_device_uuid(device_id: int) -> str: + from vllm.platforms import current_platform + + # Convert torch.npu.current_device to its corresponding ASCEND_RT_VISIBLE_DEVICES. + if is_npu_available: + if os.getenv("ASCEND_RT_VISIBLE_DEVICES") is not None: + npu_visible_devices = os.environ["ASCEND_RT_VISIBLE_DEVICES"].split(",") + assert device_id < len(npu_visible_devices), f"device_id {device_id} must less than {npu_visible_devices}" + return "NPU-" + npu_visible_devices[device_id] + else: + return f"NPU-{device_id}" + else: + try: + return current_platform.get_device_uuid(device_id) + except Exception: + return get_platform().get_device_uuid(device_id=device_id) + + +def get_vllm_max_lora_rank(lora_rank: int): + """ + For vLLM, automatically adjusts the `max_lora_rank` to the nearest allowed value. + The allowed values are retrieved from vLLM's MaxLoRARanks type definition. + """ + assert lora_rank > 0, f"lora_rank must be greater than 0, get {lora_rank}" + + try: + from vllm.config.lora import MaxLoRARanks + except Exception: + # FIXME: migrate vllm version https://github.com/vllm-project/vllm/blob/main/vllm/config/lora.py#L25 + MaxLoRARanks = Literal[1, 8, 16, 32, 64, 128, 256, 320, 512] + + vllm_max_lora_ranks = sorted(get_args(MaxLoRARanks)) + if lora_rank > vllm_max_lora_ranks[-1]: + raise ValueError(f"lora_rank must be less than or equal to {vllm_max_lora_ranks[-1]}, but got {lora_rank}") + + for rank in vllm_max_lora_ranks: + if lora_rank <= rank: + return rank + + +# https://github.com/vllm-project/vllm/issues/13175 +def monkey_patch_compute_logits(model, vocab_size: int, banned_token_ids: Optional[list[int]] = None): + """Mask the tokens the sampler must never pick. + + Beyond the out-of-vocabulary tail, `banned_token_ids` covers tokens that live *inside* the + vocabulary yet are still illegal to generate: the vision placeholders, which are meaningless + unless a real image or video sits behind them. See `get_vision_placeholder_token_ids`. + """ + original_compute_logits = model.compute_logits + + def compute_logits( + self, + *args, + **kwargs, + ) -> torch.Tensor: + logits = original_compute_logits(*args, **kwargs) + logits[..., vocab_size:] = float("-inf") + if banned_token_ids: + logits[..., banned_token_ids] = float("-inf") + return logits + + model.compute_logits = MethodType(compute_logits, model) + + +class vLLMColocateWorkerExtension: + """ + The class for vLLM's worker to inherit from, in the colocate setting. + By defining an extension class, the code can work no matter what is + the underlying worker class. This way, the code can be compatible + with both vLLM V0 and V1. + NOTE: we define this class in a separate module, and the main module + should pass the full qualified name as `worker_extension_cls` argument. + + Feature support: + 1. LoRA + 2. Online FP8 quantization + """ + + def __new__(cls, **kwargs): + set_death_signal() + + if os.environ.get("VERL_FULL_DETERMINISM", "0") == "1": + from verl.workers.engine.utils import enable_full_determinism + + # VERL_SEED is set by vLLMHttpServer.__init__ only when the + # rollout config has full_determinism=true. Worker sub-processes + # inherit their parent's env, so rollout workers will see it but + # RM workers (whose parent vLLMHttpServer does not set it) won't. + # If VERL_SEED is missing, skip — RM doesn't need the determinism + # patch, only rollout does. + verl_seed = os.environ.get("VERL_SEED") + if verl_seed is not None: + enable_full_determinism(seed=int(verl_seed)) + + # 1. patch for Lora + VLLMHijack.hijack() + vllm_config = kwargs.get("vllm_config") + # 2. patch online fp8 quant. Some models, including DeepSeek-V4, get + # fp8 from the HF config rather than an explicit rollout quantization arg. + if os.environ.get("VERL_VLLM_FP8_QUANT_ENABLED", "0") == "1" or is_fp8_model(vllm_config): + apply_vllm_fp8_patches() + # 3. patch QAT (compressed-tensors NVFP4) for dynamic weight loading + quant_config = getattr(vllm_config, "quant_config", None) if vllm_config else None + _is_qat_model = getattr(quant_config, "quant_format", None) == "nvfp4-pack-quantized" + _is_modelopt_qat = type(quant_config).__name__ == "ModelOptNvFp4Config" + if _is_qat_model: + from verl.utils.qat import apply_qat_patches + + apply_qat_patches() + logger.info("Applied QAT (compressed-tensors) patches in vLLM worker subprocess") + elif _is_modelopt_qat: + from verl.utils.modelopt import apply_modelopt_nvfp4_patches + + apply_modelopt_nvfp4_patches() + logger.info("Applied ModelOpt NVFP4 patches in vLLM worker subprocess") + + # TODO: For ascend NPU, when the corresponding vllm-ascend version is upgraded to v0.13.0, + # please remove the VLLM_ASCEND_REQUIRED_ENV_VARS variable replacement action. + # This is only a fix for vllm version < v0.13.0. + if is_npu_available: + for k in VLLM_ASCEND_REQUIRED_ENV_VARS: + if k not in os.environ: + os.environ[k] = VLLM_ASCEND_REQUIRED_ENV_VARS[k] + + instance = super().__new__(cls) + instance._is_qat_model = _is_qat_model + instance._is_modelopt_qat = _is_modelopt_qat + return instance + + def _get_drafter_model(self): + """Return the drafter's model object, or None if unavailable.""" + drafter = getattr(self.model_runner, "drafter", None) + return drafter.model if drafter is not None and hasattr(drafter, "model") else None + + def _get_draft_model_config(self): + """Return the draft model config from speculative_config, or None.""" + spec = self.model_runner.vllm_config.speculative_config + return spec.draft_model_config if spec is not None and spec.draft_model_config is not None else None + + def _use_mtp_drafter_weight_sync(self): + """Return whether the vLLM MTP drafter should receive actor weights.""" + spec = self.model_runner.vllm_config.speculative_config + return spec is not None and spec.method == "mtp" and self._get_drafter_model() is not None + + def _iter_all_models(self): + """Yield models that need weight updates. + + Only vLLM MTP drafter sync is supported for now. Independent non-MTP + draft models are not compatible with actor weight loading through this path. + """ + yield self.model_runner.model + if self._use_mtp_drafter_weight_sync(): + yield self._get_drafter_model() + + def _iter_all_models_with_config(self): + """Yield (model, model_config) for models that need post-processing.""" + yield self.model_runner.model, self.model_runner.vllm_config.model_config + if self._use_mtp_drafter_weight_sync(): + draft_cfg = self._get_draft_model_config() + if draft_cfg is not None: + yield self._get_drafter_model(), draft_cfg + + def monkey_patch_model(self, vocab_size: int, banned_token_ids: Optional[list[int]] = None): + for model in self._iter_all_models(): + # patch compute_logits to avoid sampling OOV and other illegal tokens + monkey_patch_compute_logits(model, vocab_size, banned_token_ids) + # patch weight loader to support MoE model + patch_vllm_moe_model_weight_loader(model) + + def update_weights_from_ipc(self, peft_config: dict = None, base_sync_done=False, use_shm: bool = False): + """Update the weights of the rollout model.""" + from vllm.platforms import current_platform + + from verl.workers.rollout.vllm_rollout.bucketed_weight_transfer import BucketedWeightReceiver + + if current_platform.device_type == "npu" and self.device is None: + self.device = torch.device(f"npu:{self.local_rank}") + + # In async mode, make sure the old lora is removed before adding the new one + if peft_config and base_sync_done: + self.remove_lora(VLLM_LORA_INT_ID) + + use_standard_weight_load = not (peft_config and base_sync_done) and not is_fp8_model( + self.model_runner.vllm_config + ) + + if self._is_qat_model: + # QAT (compressed-tensors): Prepare for weight loading BEFORE receiving any buckets + from verl.utils.qat import prepare_qat_for_load_weights + + for model in self._iter_all_models(): + prepare_qat_for_load_weights(model, device=self.device) + logger.info("QAT: prepare_qat_for_load_weights completed") + elif self._is_modelopt_qat: + from verl.utils.modelopt.vllm_modelopt_patch import prepare_modelopt_for_weight_reload + + prepare_modelopt_for_weight_reload(self.model_runner.model, device=self.device) + logger.info("ModelOpt: prepare_modelopt_for_weight_reload completed") + elif use_standard_weight_load: + # Re-apply here because async IPC weight sync can happen long after init and lose MoE weight_loader attrs. + for model in self._iter_all_models(): + patch_vllm_moe_model_weight_loader(model) + + assert self.device is not None + quant_reload_state = False + if is_fp8_model(self.model_runner.vllm_config) and not (peft_config and base_sync_done): + from verl.utils.vllm.vllm_fp8_utils import prepare_quanted_weights_for_loading + + quant_reload_state = prepare_quanted_weights_for_loading(self.model_runner) + + receiver = BucketedWeightReceiver( + zmq_handle=self._get_zmq_handle(), + device=self.device, + use_shm=use_shm, + ) + receiver.receive_weights( + on_bucket_received=lambda weights: self._update_weights( + weights, + peft_config=peft_config, + base_sync_done=base_sync_done, + quant_prepared=bool(quant_reload_state), + ) + ) + + if self._is_qat_model: + # QAT (compressed-tensors): call process_weights_after_loading AFTER all buckets are received + from verl.utils.qat import manual_process_weights_after_loading + + for model in self._iter_all_models(): + manual_process_weights_after_loading(model) + logger.info("QAT: process_weights_after_loading completed") + elif self._is_modelopt_qat: + from verl.utils.modelopt.vllm_modelopt_patch import modelopt_process_weights_after_loading + + modelopt_process_weights_after_loading(self.model_runner.model) + logger.info("ModelOpt QAT: process_weights_after_loading completed") + elif quant_reload_state: + from verl.utils.vllm.vllm_fp8_utils import process_quanted_weights_after_loading + + process_quanted_weights_after_loading(self.model_runner, quant_reload_state) + logger.info("FP8/MXFP4: process_weights_after_loading completed") + elif use_standard_weight_load: + # Some post-load transforms are non-idempotent; run once after all buckets. + from vllm.model_executor.model_loader.utils import process_weights_after_loading + + for model, model_config in self._iter_all_models_with_config(): + process_weights_after_loading(model, model_config, self.device) + + def _apply_buffer_updates_all_models(self, buffer_updates, main_named_buffers): + """Apply buffer updates to the main model and any synced MTP drafter. + + The main model (yielded first) reuses the prebuilt ``named_buffers`` map; + the drafter builds its own. Returns buffers applied to the main model. + """ + models = list(self._iter_all_models()) + loaded = apply_buffer_updates(models[0], buffer_updates, named_buffers=main_named_buffers) + for model in models[1:]: + apply_buffer_updates(model, buffer_updates) + return loaded + + def _update_weights( + self, + weights: list[tuple[str, torch.Tensor]], + peft_config: dict, + base_sync_done: bool, + quant_prepared: bool = False, + ): + if peft_config and base_sync_done: + # Clone out of the receiver's reused IPC bucket buffer: add_lora keeps these tensors + # past this callback, so views into the freed/overwritten buffer crash later (#6454). + weights = {name: tensor.clone() for name, tensor in weights} + lora_request = TensorLoRARequest( + lora_name=VLLM_LORA_NAME, + lora_int_id=VLLM_LORA_INT_ID, + lora_path=VLLM_LORA_PATH, + peft_config=peft_config, + lora_tensors=weights, + ) + self.add_lora(lora_request) + logger.info(f"vLLM load weights, loaded_params: {len(weights)}") + else: + param_updates, buffer_updates, named_buffers = split_buffer_updates(self.model_runner.model, weights) + # Add the FP8 related logic here as sharding manager has been deprecated. + # Check if FP8 quantization is enabled and apply appropriate weight loading + if is_fp8_model(self.model_runner.vllm_config): + logger.info(f"FP8 model detected (async): {self.model_runner.vllm_config.quant_config}") + # Convert bf16 weights to fp8 format before loading + reload_kwargs = {"prepare_model": not quant_prepared, "process_model": not quant_prepared} + loaded_params = ( + load_quanted_weights(param_updates, self.model_runner, **reload_kwargs) if param_updates else [] + ) + # Keep the draft model in sync when present. + if self._use_mtp_drafter_weight_sync() and param_updates: + load_quanted_weights(param_updates, self.model_runner, is_drafter=True, **reload_kwargs) + loaded_buffers = self._apply_buffer_updates_all_models(buffer_updates, named_buffers) + logger.info( + f"FP8 weights loaded (async), loaded_params: {len(loaded_params)}, loaded_buffers: {loaded_buffers}" + ) + else: + if param_updates: + for model in self._iter_all_models(): + model.load_weights(param_updates) + loaded_buffers = self._apply_buffer_updates_all_models(buffer_updates, named_buffers) + logger.info( + f"Loading standard weights (non-FP8, async), " + f"loaded_params: {len(param_updates)}, loaded_buffers: {loaded_buffers}" + ) + + def _get_zmq_handle(self) -> str: + """Get ZMQ handle for communication. + + Uses Ray job id + replica_rank + rollout-local rank to match the sender + side and avoid cross-job collisions on shared hosts. + In PD mode, each engine actor's local ranks start at 0; the optional + VERL_ZMQ_BASE_TRAINER_RANK offset maps them back to trainer ranks. + """ + replica_rank = os.environ.get("VERL_REPLICA_RANK", "0") + job_id = os.environ.get("VERL_RAY_JOB_ID", "0") + vllm_config = getattr(self.model_runner, "vllm_config", None) + parallel_config = getattr(vllm_config, "parallel_config", None) + local_rank = _resolve_vllm_weight_sync_local_rank(self.local_rank, parallel_config) + trainer_rank_base = os.environ.get("VERL_ZMQ_BASE_TRAINER_RANK") + trainer_rank = int(trainer_rank_base) + local_rank if trainer_rank_base is not None else local_rank + return f"ipc:///tmp/rl-colocate-zmq-{job_id}-replica-{replica_rank}-rank-{trainer_rank}.sock" + + +class SuppressSignalInThread: + def __enter__(self): + self.original_signal = signal.signal + + def no_op_signal(sig, action): + if threading.current_thread() is not threading.main_thread(): + print(f"Ignored signal {sig} in thread {threading.current_thread().name}") + return + return self.original_signal(sig, action) + + signal.signal = no_op_signal + return self + + def __exit__(self, exc_type, exc_val, exc_tb): + signal.signal = self.original_signal + + +def build_cli_args_from_config(config: dict[str, Any]) -> list[str]: + """ + Convert a config dictionary to CLI arguments for vLLM server. + + Handles different value types appropriately: + - None: skipped + - bool True: adds '--key' + - bool False: skipped + - list: expands to '--key item1 item2 ...' + - empty list: skipped (vLLM uses nargs="+" which requires at least one value) + - dict: JSON serialized + - other: string converted + + Args: + config: Dictionary of configuration key-value pairs + + Returns: + List of CLI argument strings + """ + cli_args = [] + for k, v in config.items(): + if v is None: + continue + if isinstance(v, bool): + if v: + cli_args.append(f"--{k}") + elif isinstance(v, list): + if not v: + # Skip empty lists - vLLM uses nargs="+" which requires at least one value + continue + # Lists need to be expanded as multiple separate arguments + # e.g., --cuda-graph-sizes 1 2 4 8 becomes ['--cuda-graph-sizes', '1', '2', '4', '8'] + cli_args.append(f"--{k}") + cli_args.extend([str(item) for item in v]) + else: + cli_args.append(f"--{k}") + # Use json.dumps for dict to ensure valid JSON format + cli_args.append(json.dumps(v) if isinstance(v, dict) else str(v)) + return cli_args + + +def build_mtp_speculative_config( + method: str, num_speculative_tokens: int, engine_speculative_config: Any = None +) -> dict[str, Any]: + """Build vLLM's MTP speculative config, applying rollout engine overrides.""" + if engine_speculative_config is None: + engine_speculative_config = {} + if isinstance(engine_speculative_config, str): + engine_speculative_config = json.loads(engine_speculative_config) + if not isinstance(engine_speculative_config, Mapping): + raise TypeError("rollout.engine_kwargs.vllm.speculative_config must be a mapping when MTP rollout is enabled") + + return { + "method": method, + "num_speculative_tokens": num_speculative_tokens, + **{key: val for key, val in engine_speculative_config.items() if val is not None}, + } + + +def extract_prompt_logprobs(output: RequestOutput, num_prompt_logprobs: Optional[int], result_dict: dict[str, list]): + """Extract prompt log probabilities from generation output.""" + if num_prompt_logprobs is None: + return + + prompt_logprobs_ls, prompt_ids_ls = [], [] + # NOTE: logprob of first prompt token is None. + for logprobs_dict in output.prompt_logprobs[1:]: + if num_prompt_logprobs == 0: + token_id_str = list(logprobs_dict.keys())[0] + logprob = logprobs_dict[token_id_str].logprob + prompt_logprobs_ls.append([logprob]) + prompt_ids_ls.append([int(token_id_str)]) + else: + prompt_ids = [None] * num_prompt_logprobs + prompt_logprobs = [None] * num_prompt_logprobs + # We get either top-k logprobs or top-k plus the sampled logprob (if sampled token is not in top-k) + assert len(logprobs_dict) in [num_prompt_logprobs, num_prompt_logprobs + 1], len(logprobs_dict) + for token_id_str, token_logprob in logprobs_dict.items(): + rank = token_logprob.rank + if rank > num_prompt_logprobs: + continue # the sampled token is not in the top-k + logprob = token_logprob.logprob + prompt_ids[rank - 1] = int(token_id_str) + prompt_logprobs[rank - 1] = logprob + prompt_logprobs_ls.append(prompt_logprobs) + prompt_ids_ls.append(prompt_ids) + + # NOTE: pad a dummy prompt logprob for last prompt token. + prompt_logprobs_ls.append([0.0] * max(num_prompt_logprobs, 1)) + prompt_ids_ls.append([0] * max(num_prompt_logprobs, 1)) + + result_dict["prompt_ids"] = prompt_ids_ls + result_dict["prompt_logprobs"] = prompt_logprobs_ls diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_async_server.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_async_server.py new file mode 100644 index 0000000000000000000000000000000000000000..6d8773a6cb1ddd5b34ebae587e230c847e4198ba --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_async_server.py @@ -0,0 +1,1326 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +import argparse +import asyncio +import inspect +import json +import logging +import os +import uuid +from pprint import pprint +from typing import Any, Callable, Optional + +import ray +import vllm.entrypoints.cli.serve +from packaging import version +from ray.actor import ActorHandle +from vllm import SamplingParams +from vllm.engine.arg_utils import AsyncEngineArgs +from vllm.entrypoints.cli.serve import run_headless +from vllm.entrypoints.openai.api_server import build_app, init_app_state +from vllm.inputs import TokensPrompt +from vllm.lora.request import LoRARequest +from vllm.outputs import RequestOutput +from vllm.usage.usage_lib import UsageContext +from vllm.v1.engine.async_llm import AsyncLLM + +from verl.plugin.platform import get_platform +from verl.utils.config import omega_conf_to_dataclass +from verl.utils.device import get_resource_name, get_visible_devices_keyword, is_torch_npu_available +from verl.utils.net_utils import get_free_port, is_valid_ipv6_address +from verl.utils.profiler import DistProfiler, build_vllm_profiler_args +from verl.utils.tokenizer import normalize_token_ids +from verl.utils.tracking import RLInsightLogger +from verl.utils.vllm.vllm_fp8_utils import apply_vllm_fp8_patches +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.replica import RolloutMode, RolloutReplica, TokenOutput +from verl.workers.rollout.utils import ( + get_max_position_embeddings, + get_vision_placeholder_token_ids, + qwen2_5_vl_dedup_image_tokens, + run_uvicorn, +) +from verl.workers.rollout.vllm_rollout.utils import ( + VLLM_LORA_INT_ID, + VLLM_LORA_NAME, + VLLM_LORA_PATH, + SuppressSignalInThread, + build_cli_args_from_config, + build_mtp_speculative_config, + extract_prompt_logprobs, + get_vllm_max_lora_rank, +) + +_VLLM_VERSION = version.parse(vllm.__version__) +_RESET_PREFIX_CACHE_KWARGS = {} +if _VLLM_VERSION >= version.parse("0.13.0"): + _RESET_PREFIX_CACHE_KWARGS["reset_connector"] = True + + +if _VLLM_VERSION > version.parse("0.11.0"): + from vllm.utils.argparse_utils import FlexibleArgumentParser + + if _VLLM_VERSION == version.parse("0.12.0"): + from vllm.entrypoints.harmony_utils import get_encoding + + elif _VLLM_VERSION >= version.parse("0.13.0"): + from vllm.entrypoints.openai.parser.harmony_utils import get_encoding + + else: + get_encoding = None + + if get_encoding is not None and os.getenv("VERL_USE_GPT_OSS", "0") == "1": + get_encoding() +else: + from vllm.utils import FlexibleArgumentParser + + +logger = logging.getLogger(__file__) +logger.setLevel(logging.INFO) + + +class vLLMHttpServer: + """vLLM http server in single node, this is equivalent to launch server with command line: + ``` + vllm serve --tensor-parallel-size=8 ... + ``` + """ + + def __init__( + self, + config, + model_config, + rollout_mode: RolloutMode, + workers: list[ActorHandle], + replica_rank: int, + node_rank: int, + gpus_per_node: int, + nnodes: int, + cuda_visible_devices: str, + disaggregation_role: str = "null", + disaggregation_kv_transfer_config: Optional[dict] = None, + ): + """ + Args: + config (RolloutConfig): full config. + model_config (HFModelConfig): model config. + rollout_mode (RolloutMode): rollout mode. + replica_rank (int): replica rank, a replica may contain multiple nodes. + node_rank (int): node rank. + gpus_per_node (int): number of gpus per node. + nnodes (int): number of nodes. + cuda_visible_devices (str): cuda visible devices. + disaggregation_role: PD role, or ``"null"`` for normal rollout. + disaggregation_kv_transfer_config: vLLM KVTransferConfig dict for PD. + """ + if disaggregation_role not in ("null", "prefill", "decode"): + raise ValueError(f"disaggregation_role must be 'null'|'prefill'|'decode', got {disaggregation_role!r}") + if disaggregation_role != "null" and disaggregation_kv_transfer_config is None: + raise ValueError( + f"disaggregation_role={disaggregation_role!r} requires disaggregation_kv_transfer_config to be set" + ) + self._disaggregation_role = disaggregation_role + self._disaggregation_kv_transfer_config = disaggregation_kv_transfer_config + # Filled by vLLMPDReplica.set_pd_peer for prefill-side routing. + self._pd_decode_peers: list[ActorHandle] = [] + self._pd_prefill_side_channel_port: Optional[int] = None + self._pd_prefill_engine_id: Optional[str] = None + self._pd_peer_idx: int = 0 + + os.environ[get_visible_devices_keyword()] = cuda_visible_devices + os.environ["VERL_REPLICA_RANK"] = str(replica_rank) + # Forward the Ray job id into the vLLM worker subprocess so the + # colocated weight-transfer IPC socket path is unique per Ray job. + # Without this, two concurrent verl jobs on the same node both bind + # the same /tmp/rl-colocate-zmq-replica-0-rank-0.sock and one fails + # with EADDRINUSE; a stale socket from a crashed run trips the same + # error on restart. + os.environ["VERL_RAY_JOB_ID"] = ray.get_runtime_context().get_job_id() + + self.config = self._init_config(config) + self.model_config = self._init_model_config(model_config) + self._validate_configs() + + if self.config.full_determinism: + from verl.workers.engine.utils import enable_full_determinism + + rollout_seed = replica_rank + self.config.seed + enable_full_determinism(seed=rollout_seed) + os.environ["VERL_FULL_DETERMINISM"] = "1" + os.environ["VERL_SEED"] = str(rollout_seed) + os.environ["VLLM_BATCH_INVARIANT"] = "1" + + self.rollout_mode = rollout_mode + self.workers = workers + + self.replica_rank = replica_rank + self.node_rank = node_rank + self.gpus_per_node = gpus_per_node + self.nnodes = nnodes + # model weights version, set by ServerAdapter when update weights. + self.global_steps = None + self._warned_missing_spec_decode_stats = False + + if self.rollout_mode != RolloutMode.HYBRID and self.config.load_format == "dummy": + logger.warning(f"rollout mode is {self.rollout_mode}, load_format is dummy, set to auto") + self.config.load_format = "auto" + + # used for http server + self._server_address = ray.util.get_node_ip_address().strip("[]") + self._server_port = None + + # used for controlling vllm server profiler + profiler_config = self.config.profiler + tool_config = None + if profiler_config is not None: + if profiler_config.tool in ["torch", "npu"]: + tool_config = omega_conf_to_dataclass((profiler_config.tool_config or {}).get(profiler_config.tool)) + else: + logger.warning(f"agent loop only support torch and npu profiler, got {profiler_config.tool}") + profiler_config = None + self.profiler_controller = DistProfiler(self.replica_rank, config=profiler_config, tool_config=tool_config) + + # used for data parallel: --data-parallel-address, --data-parallel-rpc-port + if self.node_rank == 0: + self._master_address = self._server_address + # used for torch.distributed.init_process_group + self._master_port, self._master_sock = get_free_port(self._server_address, with_alive_sock=True) + # used for data parallel: --data-parallel-address, --data-parallel-rpc-port + self._dp_rpc_port, self._dp_rpc_sock = get_free_port(self._server_address, with_alive_sock=True) + self._dp_master_port, self._dp_master_sock = get_free_port(self._server_address, with_alive_sock=True) + else: + self._master_address = None + self._master_port = None + self._dp_rpc_port = None + self._dp_master_port = None + + self._post_init(cuda_visible_devices) + + def get_master_address(self): + """Get master address and port for data parallel. + Returns: + tuple: (master_address, master_port, dp_rpc_port) + """ + return self._master_address, self._master_port, self._dp_rpc_port + + def get_server_address(self): + """Get http server address and port.""" + assert self._server_port is not None, "http server is not launched, port is None" + return self._server_address, self._server_port + + @property + def lora_as_adapter(self) -> bool: + return ( + self.model_config.lora_rank > 0 or self.model_config.lora.get("rank", 0) > 0 + ) and not self.model_config.lora.get("merge", False) + + async def collective_rpc( + self, + method: str | Callable, + timeout: float | None = None, + args: tuple = (), + kwargs: dict[str, Any] | None = None, + ): + await self.engine.collective_rpc( + method=method, + timeout=timeout, + args=args, + kwargs=kwargs, + ) + + async def set_pd_peer( + self, + decode_peers: list, + prefill_side_channel_port: int, + prefill_engine_id: str, + ) -> None: + assert self._disaggregation_role == "prefill", ( + f"set_pd_peer must be called on the prefill server (got role={self._disaggregation_role!r})" + ) + assert isinstance(decode_peers, list) and decode_peers, "decode_peers must be a non-empty list" + self._pd_decode_peers = list(decode_peers) + self._pd_prefill_side_channel_port = prefill_side_channel_port + self._pd_prefill_engine_id = prefill_engine_id + + async def launch_server(self, master_address: str = None, master_port: int = None, dp_rpc_port: int = None): + if self.node_rank != 0: + assert master_address and master_port and dp_rpc_port, ( + "non-master node should provide master_address, master_port and dp_rpc_port" + ) + self._master_address = master_address + self._master_port = master_port + self._dp_rpc_port = dp_rpc_port + + # 1. setup vllm serve cli args + engine_kwargs = self.config.get("engine_kwargs", {}).get(self._get_engine_kwargs_key(), {}) or {} + engine_kwargs = {key: val for key, val in engine_kwargs.items() if val is not None} + if self.config.get("limit_images", None): # support for multi-image data + engine_kwargs["limit_mm_per_prompt"] = {"image": self.config.get("limit_images")} + if self.config.cudagraph_capture_sizes and _VLLM_VERSION <= version.parse("0.11.0"): + engine_kwargs["cuda_graph_sizes"] = self.config.cudagraph_capture_sizes + + self._preprocess_engine_kwargs(engine_kwargs) + + # Override default generation config from hugging face model config, + # user can still override them by passing kwargs in each request. + override_generation_config = self._get_override_generation_config() + logger.info(f"override_generation_config: {override_generation_config}") + + logger.info(f"enable_sleep_mode: {self.config.enable_sleep_mode}") + if not self.config.enable_sleep_mode: + from verl.utils.device import set_expandable_segments + + set_expandable_segments(True) + + quantization, hf_overrides = self._apply_quantization() + + compilation_config = engine_kwargs.pop("compilation_config", None) or {} + if isinstance(compilation_config, str): + compilation_config = json.loads(compilation_config) + compilation_config.setdefault("cudagraph_mode", "FULL_AND_PIECEWISE") + + # FULL cuda graph is not yet supported with DCP, downgrade to PIECEWISE + dcp_size = engine_kwargs.get("decode_context_parallel_size", 1) or 1 + if dcp_size > 1 and compilation_config["cudagraph_mode"] == "FULL_AND_PIECEWISE": + logger.warning( + "FULL cuda graph is not supported with DCP (decode_context_parallel_size=%d), " + "downgrading cudagraph_mode to PIECEWISE.", + dcp_size, + ) + compilation_config["cudagraph_mode"] = "PIECEWISE" + if self.config.cudagraph_capture_sizes and _VLLM_VERSION > version.parse("0.11.0"): + compilation_config["cudagraph_capture_sizes"] = self.config.cudagraph_capture_sizes + + compilation_config = json.dumps(compilation_config) + args = { + "dtype": self.config.dtype, + "load_format": self.config.load_format, + "skip_tokenizer_init": False, + "distributed_executor_backend": "mp", + "worker_extension_cls": self._get_worker_extension_cls(), + "trust_remote_code": self.model_config.trust_remote_code, + "max_model_len": self.config.max_model_len, + "max_num_seqs": self.config.max_num_seqs, + "enable_chunked_prefill": self.config.enable_chunked_prefill, + "max_num_batched_tokens": self.config.max_num_batched_tokens, + "enable_prefix_caching": self.config.enable_prefix_caching, + "enable_sleep_mode": self.config.enable_sleep_mode, + "logprobs_mode": self.config.logprobs_mode, + "enforce_eager": self.config.enforce_eager, + "gpu_memory_utilization": self.config.gpu_memory_utilization, + "disable_log_stats": self.config.disable_log_stats, + "tensor_parallel_size": self.config.tensor_model_parallel_size, + "seed": self.replica_rank + self.config.seed, + "override_generation_config": json.dumps(override_generation_config), + "quantization": quantization, + "hf_overrides": hf_overrides, + "scheduling_policy": self.config.scheduling_policy, + "compilation_config": compilation_config, + **engine_kwargs, + } + + # update profiler args + profiler_args = build_vllm_profiler_args( + self.profiler_controller.config, self.profiler_controller.tool_config, self.replica_rank + ) + if _VLLM_VERSION >= version.parse("0.13.0"): + # vLLM >= 0.13.0 supports profiler config via CLI args; env vars still work but will be deprecated + args.update(profiler_args) + + if self.config.prometheus.enable: + if self.config.prometheus.served_model_name: + # Extract model name from path if it's a full path + served_model_name = self.config.prometheus.served_model_name + if "/" in served_model_name: + # If it's a full path, extract the last part as model name + served_model_name = served_model_name.split("/")[-1] + args["served_model_name"] = served_model_name + + if self.config.mtp is not None and self.config.mtp.enable and self.config.mtp.enable_rollout: + args["speculative_config"] = build_mtp_speculative_config( + self.config.mtp.method, + self.config.mtp.num_speculative_tokens, + args.get("speculative_config"), + ) + + if self.config.data_parallel_size > 1: + assert self.gpus_per_node % self.config.tensor_model_parallel_size == 0, ( + "gpus_per_node should be divisible by tensor_model_parallel_size" + ) + data_parallel_size_local = self.gpus_per_node // self.config.tensor_model_parallel_size + assert len(self.workers) == data_parallel_size_local * self.config.tensor_model_parallel_size, ( + f"num workers ({len(self.workers)}) should be equal to " + f"dp_size_local ({data_parallel_size_local}) * tp_size ({self.config.tensor_model_parallel_size})" + ) + dp_args = { + "data_parallel_size": self.config.data_parallel_size, + "data_parallel_size_local": data_parallel_size_local, + "data_parallel_start_rank": self.node_rank * data_parallel_size_local, + "data_parallel_address": self._master_address, + "data_parallel_rpc_port": self._dp_rpc_port, + } + args.update(dp_args) + + args.update({"enable_expert_parallel": self.config.expert_parallel_size > 1}) + + # used for torch.distributed.init_process_group + if self.nnodes > 1: + args.update( + { + "master_addr": self._master_address, + "master_port": self._master_port, + "node_rank": self.node_rank, + "nnodes": self.nnodes, + "data_parallel_address": self._master_address, + "data_parallel_rpc_port": self._dp_rpc_port, + } + ) + + # update lora-related args + lora_rank = self.model_config.lora.get("rank", 0) + if lora_rank <= 0: + lora_rank = ( + self.model_config.lora_rank + ) # FIXME: fallback to lora_rank for now, we should unify lora settings. + + if self.model_config.lora.get("merge", False): + lora_rank = 0 + + if lora_rank > 0: + lora_args = { + "enable_lora": True, + "max_loras": 1, + "max_lora_rank": get_vllm_max_lora_rank(lora_rank), + } + if self.model_config.lora.get("fully_sharded_loras", False): + lora_args["fully_sharded_loras"] = True + args.update(lora_args) + + if self.config.enable_rollout_routing_replay: + # R3 (Rollout Router Replay) relies on vLLM's ``enable_return_routed_experts`` + # path (RoutedExpertsManager / RoutedExpertsCapturer), which is only correct + # for hybrid-attention MoE models (e.g. Qwen3.5, whose linear + full attention + # layout produces >1 KV-cache group) starting from vLLM 0.22.0. Earlier + # releases either lack the feature or under-size the routed-experts host + # buffer and crash with an IndexError. Fail fast with an actionable message + # instead of surfacing an opaque runtime error deep inside vLLM. + if _VLLM_VERSION < version.parse("0.22.0"): + raise RuntimeError( + "rollout.enable_rollout_routing_replay=True requires vLLM >= 0.22.0 " + f"(installed: {vllm.__version__}). Upgrade vLLM (e.g. `pip install -U " + "'vllm>=0.22.0'`) or disable enable_rollout_routing_replay." + ) + args.update({"enable_return_routed_experts": True}) + + if self._disaggregation_role != "null": + args["kv_transfer_config"] = json.dumps(self._disaggregation_kv_transfer_config) + + server_args = ["serve", self.model_config.local_path] + build_cli_args_from_config(args) + + if self.replica_rank == 0: + pprint(server_args) + + CMD_MODULES = self._get_cli_modules() + parser = FlexibleArgumentParser(description=self._get_cli_description()) + subparsers = parser.add_subparsers(required=False, dest="subparser") + cmds = {} + for cmd_module in CMD_MODULES: + new_cmds = cmd_module.cmd_init() + for cmd in new_cmds: + cmd.subparser_init(subparsers).set_defaults(dispatch_function=cmd.cmd) + cmds[cmd.name] = cmd + server_args = parser.parse_args(args=server_args) + server_args.model = server_args.model_tag + if server_args.subparser in cmds: + cmds[server_args.subparser].validate(server_args) + + # 3. launch server + if self.node_rank == 0: + await self.run_server(server_args) + else: + await self.run_headless(server_args) + + async def run_server(self, args: argparse.Namespace): + engine_args = AsyncEngineArgs.from_cli_args(args) + usage_context = UsageContext.OPENAI_API_SERVER + vllm_config = engine_args.create_engine_config(usage_context=usage_context) + vllm_config.parallel_config.data_parallel_master_port = self._dp_master_port + + fn_args = set(dict(inspect.signature(AsyncLLM.from_vllm_config).parameters).keys()) + kwargs = {} + if "enable_log_requests" in fn_args: + kwargs["enable_log_requests"] = engine_args.enable_log_requests + if "disable_log_stats" in fn_args: + kwargs["disable_log_stats"] = engine_args.disable_log_stats + + engine_client = AsyncLLM.from_vllm_config(vllm_config=vllm_config, usage_context=usage_context, **kwargs) + + # Don't keep the dummy data in memory + await engine_client.reset_mm_cache() + # A sampled <|image_pad|>/<|video_pad|> has no image behind it, and every consumer of the + # sequence assumes it does. Mask them out with the OOV tail, so the policy cannot pick one. + await engine_client.collective_rpc( + method="monkey_patch_model", + kwargs={ + "vocab_size": len(self.model_config.tokenizer), + "banned_token_ids": get_vision_placeholder_token_ids(self.model_config.processor), + }, + ) + + build_app_sig = inspect.signature(build_app) + supported_tasks: tuple[Any, ...] = () + build_app_kwargs: dict[str, Any] = {} + if "supported_tasks" in build_app_sig.parameters: + supported_tasks = await engine_client.get_supported_tasks() + build_app_kwargs["supported_tasks"] = supported_tasks + # vLLM >= 0.20.0 requires `model_config` to register pooling API routes + # (e.g. ``/classify``, ``/embed``); see + # ``register_pooling_api_routers`` in vllm/entrypoints/pooling/factories.py + # which short-circuits when ``model_config`` is ``None``. + if "model_config" in build_app_sig.parameters: + build_app_kwargs["model_config"] = engine_client.model_config + app = build_app(args, **build_app_kwargs) + + init_app_sig = inspect.signature(init_app_state) + if "vllm_config" in init_app_sig.parameters: + await init_app_state(engine_client, vllm_config, app.state, args) + elif "supported_tasks" in init_app_sig.parameters: + await init_app_state(engine_client, app.state, args, supported_tasks) + else: + await init_app_state(engine_client, app.state, args) + if self.replica_rank == 0 and self.node_rank == 0: + logger.info(f"Initializing a V1 LLM engine with config: {vllm_config}") + + self.engine = engine_client + self._server_port, self._server_task = await run_uvicorn(app, args, self._server_address) + + async def run_headless(self, args: argparse.Namespace): + """Run headless server in a separate thread.""" + args.api_server_count = 0 + + def run_headless_wrapper(): + with SuppressSignalInThread(): + run_headless(args) + + def on_run_headless_done(future: asyncio.Future): + try: + exc = future.exception() + if exc: + logger.exception(f"run_headless failed with exception: {exc}") + else: + logger.warning("run_headless completed successfully, but it's not expected.") + except Exception as e: + logger.exception(f"get result from run_headless failed: {e}") + finally: + os._exit(1) + + self.task = asyncio.create_task(asyncio.to_thread(run_headless_wrapper)) + self.task.add_done_callback(on_run_headless_done) + + async def generate( + self, + prompt_ids: list[int], + sampling_params: dict[str, Any], + request_id: str, + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + audio_data: Optional[list[Any]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + priority: int = 0, + kv_transfer_params: Optional[dict] = None, + ) -> TokenOutput: + """Generate sequence with token-in-token-out. + + Args: + kv_transfer_params: vLLM KV-transfer payload for PD requests. + """ + if self._disaggregation_role == "prefill" and self._pd_decode_peers and kv_transfer_params is None: + return await self._pd_dispatch( + prompt_ids, + sampling_params, + request_id, + image_data=image_data, + video_data=video_data, + audio_data=audio_data, + mm_processor_kwargs=mm_processor_kwargs, + priority=priority, + ) + + prompt_ids = normalize_token_ids(prompt_ids) + + # Calculate the maximum possible new tokens based on available context space + # This serves as a safety upper bound. vLLM v0.20+ rejects `max_tokens < 1` + # (see vllm.sampling_params.SamplingParams._verify_args), so we require at + # least one token of headroom to be able to generate at all. + max_possible_tokens = self.config.max_model_len - len(prompt_ids) + if max_possible_tokens < 1: + raise ValueError( + f"Prompt length ({len(prompt_ids)}) leaves no room to generate within the " + f"model's maximum context length ({self.config.max_model_len}); need at least " + f"1 token of headroom." + ) + + # Determine max_tokens from sampling_params or use configured response_length as default + if "max_tokens" in sampling_params: + max_tokens = sampling_params.pop("max_tokens") + elif "max_new_tokens" in sampling_params: + # support sglang-style 'max_new_tokens' param + max_tokens = sampling_params.pop("max_new_tokens") + else: + # Default to a calculation that considers configured lengths + # Cap max_tokens by response_length to ensure tensor alignment, + # and by remaining budget to prevent OOM in multi-turn rollouts. + max_tokens = min( + self.config.response_length, self.config.prompt_length + self.config.response_length - len(prompt_ids) + ) + + # Clamp max_tokens to the valid range [1, max_possible_tokens]. The lower bound + # is 1 because vLLM v0.20+ raises VLLMValidationError when max_tokens < 1. + max_tokens = max(1, min(max_tokens, max_possible_tokens)) + + assert 1 <= max_tokens <= max_possible_tokens, ( + f"max_tokens {max_tokens} not in valid range [1, {max_possible_tokens}]" + ) + sampling_params["logprobs"] = 0 if sampling_params.pop("logprobs", False) else None + sampling_params.setdefault("repetition_penalty", self.config.get("repetition_penalty", 1.0)) + sampling_params.setdefault("ignore_eos", self.config.get("ignore_eos", False)) + # Inject per-request seed for deterministic sampling when full_determinism is enabled. + if self.config.full_determinism: + sampling_params.setdefault("seed", self.replica_rank + self.config.seed) + + if kv_transfer_params is not None: + extra_args = dict(sampling_params.pop("extra_args", None) or {}) + extra_args["kv_transfer_params"] = kv_transfer_params + sampling_params["extra_args"] = extra_args + + sampling_params = SamplingParams(max_tokens=max_tokens, **sampling_params) + prompt_ids = qwen2_5_vl_dedup_image_tokens(prompt_ids, self.model_config.processor) + multi_modal_data = {} + if image_data is not None: + multi_modal_data["image"] = image_data + if video_data is not None: + multi_modal_data["video"] = video_data + if audio_data is not None: + multi_modal_data["audio"] = audio_data + + prompt_kwargs = {"prompt_token_ids": prompt_ids, "multi_modal_data": multi_modal_data} + if mm_processor_kwargs: + prompt_kwargs["mm_processor_kwargs"] = mm_processor_kwargs + try: + prompt = TokensPrompt(**prompt_kwargs) + except TypeError: + prompt = prompt_kwargs + + # Add lora request + lora_request = None + if self.lora_as_adapter: + # Make sure we also check that the lora is already loaded in the engine + lora_loaded = VLLM_LORA_INT_ID in await self.engine.list_loras() + if lora_loaded: + lora_request = LoRARequest( + lora_name=VLLM_LORA_NAME, lora_int_id=VLLM_LORA_INT_ID, lora_path=VLLM_LORA_PATH + ) + + with RLInsightLogger.trace_state("vllm_generate", state_lane_id=f"replica_{self.replica_rank}"): + generator = self.engine.generate( + prompt=prompt, + sampling_params=sampling_params, + request_id=request_id, + lora_request=lora_request, + priority=priority, + ) + + # Get final response + final_res: Optional[RequestOutput] = None + async for output in generator: + final_res = output + assert final_res is not None + + extra_fields = {"global_steps": self.global_steps} + # Handle abort case: when the request is aborted by pause_generation(abort), + # outputs may be empty. Return empty results with stop_reason="aborted" + # instead of crashing with "IndexError: list index out of range". + if not final_res.outputs: + return TokenOutput( + token_ids=[], + log_probs=None, + routed_experts=None, + stop_reason="aborted", + extra_fields=extra_fields, + ) + + extract_prompt_logprobs( + output=final_res, + num_prompt_logprobs=sampling_params.prompt_logprobs, + result_dict=extra_fields, + ) + token_ids = final_res.outputs[0].token_ids + log_probs = None + if sampling_params.logprobs is not None: + log_probs = [logprobs[token_ids[i]].logprob for i, logprobs in enumerate(final_res.outputs[0].logprobs)] + + routed_experts = None + if self.config.enable_rollout_routing_replay: + routed_experts = final_res.outputs[0].routed_experts + + # Determine stop reason from finish_reason + finish_reason = final_res.outputs[0].finish_reason + if finish_reason == "abort": + stop_reason = "aborted" + elif finish_reason in ("stop", "length"): + stop_reason = "completed" + else: + stop_reason = finish_reason # for more stop reason in the future + + num_preempted = None + + if hasattr(final_res.outputs[0], "num_preempted"): + num_preempted = final_res.outputs[0].num_preempted + + response_kv_transfer_params = getattr(final_res, "kv_transfer_params", None) + if response_kv_transfer_params is not None: + extra_fields["kv_transfer_params"] = response_kv_transfer_params + + # Re-key backend spec-decoding stats to the rollout-common names. + if self.config.mtp is not None and self.config.mtp.enable and self.config.mtp.enable_rollout: + spec_decode_stats = getattr(final_res.metrics, "request_spec_decode_stats", None) + if spec_decode_stats is None: + if not self._warned_missing_spec_decode_stats: + logger.warning( + "vLLM MTP rollout metrics do not include request_spec_decode_stats; " + "speculative decoding acceptance metrics will be skipped." + ) + self._warned_missing_spec_decode_stats = True + else: + extra_fields["spec_num_draft_tokens"] = spec_decode_stats.num_draft_tokens + extra_fields["spec_num_accepted_tokens"] = spec_decode_stats.num_accepted_tokens + extra_fields["spec_num_verify_steps"] = spec_decode_stats.num_verify_steps + return TokenOutput( + token_ids=token_ids, + log_probs=log_probs, + routed_experts=routed_experts, + stop_reason=stop_reason, + num_preempted=num_preempted, + extra_fields=extra_fields, + ) + + def _select_decode_peer(self) -> ActorHandle: + """Round-robin across decode peers.""" + peer = self._pd_decode_peers[self._pd_peer_idx % len(self._pd_decode_peers)] + self._pd_peer_idx += 1 + return peer + + async def _pd_dispatch( + self, + prompt_ids: list[int], + sampling_params: dict[str, Any], + request_id: str, + image_data: Optional[list[Any]] = None, + video_data: Optional[list[Any]] = None, + audio_data: Optional[list[Any]] = None, + mm_processor_kwargs: Optional[dict[str, Any]] = None, + priority: int = 0, + ) -> TokenOutput: + """Run prefill locally, then decode on a selected peer.""" + decode_peer = self._select_decode_peer() + connector = (self._disaggregation_kv_transfer_config or {}).get("kv_connector", "") + is_mooncake = connector == "MooncakeConnector" + + # Prefill only materializes KV; discard its single generated token. + prefill_sp = dict(sampling_params) + prefill_sp.pop("max_tokens", None) + prefill_sp.pop("max_new_tokens", None) + prefill_sp["max_tokens"] = 1 + transfer_id = uuid.uuid4().hex + prefill_kv_params = { + "do_remote_decode": True, + "do_remote_prefill": False, + "transfer_id": transfer_id, + } + + prefill_out = await self.generate( + prompt_ids, + prefill_sp, + f"{request_id}_P", + image_data=image_data, + video_data=video_data, + audio_data=audio_data, + mm_processor_kwargs=mm_processor_kwargs, + priority=priority, + kv_transfer_params=prefill_kv_params, + ) + if is_mooncake: + # Mooncake does not return decode params from the prefill leg. + decode_kv_params = { + "do_remote_decode": False, + "do_remote_prefill": True, + "remote_engine_id": self._pd_prefill_engine_id, + # Single-node PD uses Mooncake's local bootstrap address. + "remote_bootstrap_addr": f"http://127.0.0.1:{self._pd_prefill_side_channel_port}", + "transfer_id": transfer_id, + } + else: + decode_kv_params = prefill_out.extra_fields.get("kv_transfer_params") + if decode_kv_params is None: + raise RuntimeError(f"PD prefill leg returned no kv_transfer_params (request_id={request_id})") + + return await decode_peer.generate.remote( + prompt_ids, + dict(sampling_params), + f"{request_id}_D", + image_data=image_data, + video_data=video_data, + audio_data=audio_data, + mm_processor_kwargs=mm_processor_kwargs, + priority=priority, + kv_transfer_params=decode_kv_params, + ) + + async def wake_up(self, tags: list[str] | None = None): + if self.node_rank != 0: + return + + if self.rollout_mode == RolloutMode.HYBRID: + # engine.wake_up() broadcasts via the DP coordinator to ALL EngineCore + # processes across all DP shards (unlike collective_rpc which only reaches + # TP workers within a single shard). + await self.engine.wake_up(tags=tags or self._get_wake_up_tags()) + await self.engine.reset_prefix_cache(**_RESET_PREFIX_CACHE_KWARGS) + elif self.rollout_mode == RolloutMode.COLOCATED: + # Directly call engine to wake up without sync weights. + await self.engine.wake_up(tags=self._get_wake_up_tags()) + # reset_connector=True drops any attached external KV store + # (e.g. MooncakeStoreConnector) whose entries were computed + # against the previous weights. No-op success when no connector + # is configured (vLLM scheduler treats it as such). + await self.engine.reset_prefix_cache(**_RESET_PREFIX_CACHE_KWARGS) + elif self.rollout_mode == RolloutMode.STANDALONE: + logger.info("skip wake_up in standalone mode") + + async def sleep(self): + if self.node_rank != 0 or not self.config.free_cache_engine: + return + + if self.rollout_mode == RolloutMode.HYBRID: + await self._sleep_hybrid() + elif self.rollout_mode == RolloutMode.COLOCATED: + await self.engine.sleep(level=1) + elif self.rollout_mode == RolloutMode.STANDALONE: + logger.info("skip sleep in standalone mode") + + async def clear_kv_cache(self): + if self.node_rank == 0: + # reset_connector=True drops any attached external KV store + # (e.g. MooncakeStoreConnector) whose entries were computed + # against the previous model weights. With no connector it + # is a no-op success, so we can pass it unconditionally. + await self.engine.reset_prefix_cache(**_RESET_PREFIX_CACHE_KWARGS) + + if _VLLM_VERSION >= version.parse("0.9.0"): + await self.engine.reset_mm_cache() + if _VLLM_VERSION >= version.parse("0.16.0"): + await self.engine.reset_encoder_cache() + + async def release_kv_cache(self): + """Release only kv_cache GPU memory, keeping model weights intact. + # TODO: support true release of kv_cache + """ + if self.node_rank != 0 or not self.config.free_cache_engine: + return + + async def resume_kv_cache(self): + """Restore kv_cache GPU memory after a weight sync. Counterpart to release_kv_cache().""" + if self.node_rank != 0: + return + + async def start_profile(self, **kwargs): + if self.node_rank != 0: + return + if ( + self.profiler_controller.check_enable() + and self.profiler_controller.check_this_rank() + and self.profiler_controller.is_discrete_mode() + ): + await self.engine.start_profile(**kwargs) + + async def stop_profile(self): + if self.node_rank != 0: + return + if ( + self.profiler_controller.check_enable() + and self.profiler_controller.check_this_rank() + and self.profiler_controller.is_discrete_mode() + ): + await self.engine.stop_profile() + + async def set_global_steps(self, global_steps: int): + """Set the global steps of the model weights.""" + self.global_steps = global_steps + + async def wait_for_requests_to_drain(self): + await self.engine.wait_for_requests_to_drain() + + async def abort_all_requests(self, reset_prefix_cache: bool = True) -> dict[str, Any]: + """Abort all ongoing generation requests. + + On vLLM >= 0.12.0, uses AsyncLLM.pause_generation() to abort in-flight + requests, drain, and clear caches. The engine remains paused after this + call — use resume_generation() to accept new requests (e.g. before + validation). + + On vLLM < 0.12.0, manually aborts each request and resets prefix cache. + + Returns: + dict[str, Any]: Dictionary containing: + - aborted_count: Number of requests aborted + - request_ids: List of aborted request IDs + """ + try: + if _VLLM_VERSION >= version.parse("0.12.0"): + # Snapshot request IDs before pausing for reporting + request_ids = list(self.engine.output_processor.request_states.keys()) + + # pause_generation with wait_for_inflight_requests=False will: + # 1. Set engine to paused state (blocks new generate calls) + # 2. Abort all in-flight requests + # 3. Wait for requests to drain + # 4. Clear prefix and mm caches if clear_cache=True. + # EngineCore._reset_caches defaults reset_connector=True + # on this path, so any attached external KV store (e.g. + # MooncakeStoreConnector) is invalidated along with the + # local prefix cache — RL-correct hard-reset at every + # weight update boundary, no extra kwargs needed. + await self.engine.pause_generation( + wait_for_inflight_requests=False, + clear_cache=reset_prefix_cache, + ) + else: + # Take an atomic snapshot to avoid race conditions with the vLLM engine thread + request_states_snapshot = list(self.engine.output_processor.request_states.items()) + request_ids = [req_id for req_id, _ in request_states_snapshot] + + if not request_ids: + return {"aborted_count": 0, "request_ids": []} + + # For each request, create an abort output and put it to its queue + # This allows the generator to receive the aborted result + from vllm.v1.engine import FinishReason + + for _, req_state in request_states_snapshot: + request_output = req_state.make_request_output( + [], pooling_output=None, finish_reason=FinishReason.ABORT, stop_reason=None + ) + req_state.queue.put(request_output) + + # Abort requests in the output processor and engine core + self.engine.output_processor.abort_requests(request_ids) + await self.engine.engine_core.abort_requests_async(request_ids) + + # Try to reset prefix cache to ensure clean state + if reset_prefix_cache: + await self.clear_kv_cache() + logger.info("Prefix cache reset after abort") + + logger.info(f"Aborted {len(request_ids)} requests: {request_ids}") + return {"aborted_count": len(request_ids), "request_ids": request_ids} + + except Exception as e: + logger.error(f"Error aborting requests: {e}") + return {"aborted_count": 0, "request_ids": [], "error": str(e)} + + async def resume_generation(self): + """Resume generation after abort_all_requests (pause_generation). + + Only effective on vLLM >= 0.12.0 where pause_generation is used. + No-op on older versions. + """ + if self.node_rank != 0: + return + if _VLLM_VERSION >= version.parse("0.12.0"): + await self.engine.resume_generation() + + async def abort_request(self, request_id: str, reset_prefix_cache: bool = True) -> dict[str, Any]: + """Abort a specific generation request. + + Args: + request_id: The ID of the request to abort. + + Returns: + dict[str, Any]: Dictionary containing abort result. + """ + try: + request_states = self.engine.output_processor.request_states + req_state = request_states.get(request_id) + + if req_state is None: + return {"aborted": False, "error": f"Request {request_id} not found"} + + # Create abort output and put it to the queue + from vllm.v1.engine import FinishReason + + request_output = req_state.make_request_output( + [], pooling_output=None, finish_reason=FinishReason.ABORT, stop_reason=None + ) + req_state.queue.put(request_output) + + # Abort in output processor and engine core + self.engine.output_processor.abort_requests([request_id]) + await self.engine.engine_core.abort_requests_async([request_id]) + + # Try to reset prefix cache to ensure clean state + if reset_prefix_cache: + await self.clear_kv_cache() + logger.info(f"Prefix cache reset after abort request {request_id}") + + logger.info(f"Aborted request: {request_id}") + return {"aborted": True, "request_id": request_id} + + except Exception as e: + logger.error(f"Error aborting request {request_id}: {e}") + return {"aborted": False, "request_id": request_id, "error": str(e)} + + # ----------------------------------------------------------------------- + # Hook methods for subclass overrides + # ----------------------------------------------------------------------- + + def _init_config(self, config): + """Initialise config. Override when a specific dataclass_type is needed.""" + return omega_conf_to_dataclass(config) + + def _init_model_config(self, model_config): + """Initialise model_config. Override when a specific dataclass_type is needed.""" + return omega_conf_to_dataclass(model_config, dataclass_type=HFModelConfig) + + def _validate_configs(self) -> None: + """Validate config/model_config after initialisation.""" + max_position_embeddings = get_max_position_embeddings(self.model_config.hf_config) + if self.config.max_model_len is None: + self.config.max_model_len = max_position_embeddings + else: + if self.config.max_model_len > max_position_embeddings: + raise ValueError( + f"max_model_len ({self.config.max_model_len}) should be less than or equal to " + f"max_position_embeddings ({max_position_embeddings})" + ) + + def _post_init(self, cuda_visible_devices: str) -> None: + """Called at the end of __init__. Default logs server metadata.""" + logger.info( + f"{self.__class__.__name__}, replica_rank: {self.replica_rank}, node_rank: {self.node_rank}, " + f"{get_visible_devices_keyword()}: {cuda_visible_devices}, " + f"master_address: {self._master_address}, master_port: {self._master_port}, " + f"data_parallel_rpc_port: {self._dp_rpc_port}, data_parallel_master_port: {self._dp_master_port}" + ) + + def _get_engine_kwargs_key(self) -> str: + """Return the key under config.engine_kwargs for this engine (e.g. 'vllm').""" + return "vllm" + + def _preprocess_engine_kwargs(self, engine_kwargs: dict) -> None: + """Mutate engine_kwargs in-place before the CLI args dict is built.""" + if _VLLM_VERSION < version.parse("0.22.0"): + # Work around multimodal processor cache desync across pause/resume. + # See: https://github.com/vllm-project/vllm/pull/43001/ + engine_kwargs.setdefault("mm_processor_cache_gb", 0) + + def _get_override_generation_config(self) -> dict: + """Return the override_generation_config dict.""" + # Override default generation config from hugging face model config, + # user can still override them by passing kwargs in each request. + return dict( + temperature=self.config.temperature, + top_k=self.config.top_k, + top_p=self.config.top_p, + repetition_penalty=1.0, + max_new_tokens=self.config.response_length, + ) + + def _apply_quantization(self) -> tuple[Optional[str], dict]: + """Process quantization config. Returns (quantization_str, hf_overrides).""" + quantization = self.config.quantization + hf_overrides = {} + + # Handle QAT (Quantization-Aware Training) configuration + qat_config_dict = getattr(self.config, "qat", {}) or {} + if qat_config_dict.get("enable", False): + from verl.utils.qat import QATConfig, load_quantization_config + + qat_config = QATConfig(**qat_config_dict) + quantization_config_dict = load_quantization_config(qat_config) + quant_method = quantization_config_dict.get("quant_method", None) + + if quant_method == "modelopt": + from verl.utils.modelopt import apply_modelopt_nvfp4_patches + + apply_modelopt_nvfp4_patches() + quantization = "modelopt" + elif quant_method == "compressed-tensors": + from verl.utils.qat import apply_qat_patches + + apply_qat_patches() + quantization = "compressed-tensors" + else: + raise ValueError(f"Unsupported quant_method: {quant_method}") + + logger.info(f"QAT quantization config injected (quant_method={quant_method})") + hf_overrides["quantization_config"] = quantization_config_dict + elif quantization is not None: + # Handle other quantization methods (fp8, torchao) + _SUPPORTED_QUANTIZATION = ["fp8", "torchao", "ascend"] + if quantization not in _SUPPORTED_QUANTIZATION: + raise ValueError(f"Currently only support {_SUPPORTED_QUANTIZATION} quantization, got: {quantization}") + + if quantization == "fp8": + # Ignore MoE router layers for FP8 quantization + all_mlp_gate_layers = [] + for layer in range(self.model_config.hf_config.num_hidden_layers): + all_mlp_gate_layers.append(f"model.layers.{layer}.mlp.gate") + + FP8_BLOCK_QUANT_KWARGS = { + "activation_scheme": "dynamic", + "fmt": "e4m3", + "quant_method": "fp8", + "weight_block_size": [128, 128], + "ignored_layers": all_mlp_gate_layers, + } + hf_overrides["quantization_config"] = dict(FP8_BLOCK_QUANT_KWARGS) + # Apply vllm fp8 patches + # Will remove the patch after vllm support on-the-fly quant for rollout natively. + apply_vllm_fp8_patches() + # for subprocesses patching + os.environ["VERL_VLLM_FP8_QUANT_ENABLED"] = "1" + + model_quantization_config = getattr(self.model_config.hf_config, "quantization_config", {}) or {} + if quantization is None and model_quantization_config.get("quant_method") == "fp8": + apply_vllm_fp8_patches() + os.environ["VERL_VLLM_FP8_QUANT_ENABLED"] = "1" + + if quantization is not None and self.config.quantization_config_file is not None: + hf_overrides["quantization_config_file"] = self.config.quantization_config_file + + return quantization, hf_overrides + + def _get_worker_extension_cls(self) -> str: + """Return the fully-qualified colocate worker extension class name.""" + return "verl.workers.rollout.vllm_rollout.utils.vLLMColocateWorkerExtension" + + def _get_cli_modules(self) -> list: + """Return the list of CLI command modules used for argument parsing.""" + return [vllm.entrypoints.cli.serve] + + def _get_cli_description(self) -> str: + """Return the description string for the CLI argument parser.""" + return "vLLM CLI" + + def _get_wake_up_tags(self) -> list[str]: + """Return the tags passed to engine.wake_up(). Default includes kv_cache.""" + return ["kv_cache", "weights"] + + async def _sleep_hybrid(self): + """HYBRID sleep: adapters and MTP need level=1; full weights need level=2. + + Uses engine.sleep() instead of engine.collective_rpc("sleep") to ensure + that sleep is properly propagated to all data-parallel worker processes. + collective_rpc only reaches the TP workers within a single DP shard, + leaving other DP shards' weights unreleased, which causes OOM during + FSDP training backward when DP > 1. + """ + mtp_config = getattr(self.config, "mtp", None) + mtp_rollout_enabled = ( + mtp_config is not None + and getattr(mtp_config, "enable", False) + and getattr(mtp_config, "enable_rollout", False) + ) + # MTP drafter-only weights are initialized by vLLM and are not guaranteed + # to be restored by actor weight sync after level 2 sleep discards them. + # lora only update adapter weights, so set sleep level to 1 + # vllm_ascend not support sleep_level now. Enabling EP during training may lead to accuracy issues. + if mtp_rollout_enabled or self.lora_as_adapter or is_torch_npu_available(check_device=False): + sleep_level = 1 + else: + sleep_level = 2 + await self.engine.sleep(level=sleep_level) + if _VLLM_VERSION >= version.parse("0.17.0"): + await self.engine.reset_encoder_cache() + + +class vLLMReplica(RolloutReplica): + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: HFModelConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + name_suffix: str = "", + ): + super().__init__( + replica_rank, config, model_config, gpus_per_node, is_reward_model, is_teacher_model, name_suffix + ) + self.server_class = ray.remote(vLLMHttpServer) + + async def launch_servers(self): + """Launch http server in each node.""" + assert len(self.workers) == self.world_size, ( + f"worker number {len(self.workers)} not equal to world size {self.world_size}" + ) + + self._validate_launch_requirements() + + # get (node_id, CUDA_VISIBLE_DEVICES) of all workers + worker_infos = await asyncio.gather( + *[ + worker.__ray_call__.remote( + lambda self: ( + ray.get_runtime_context().get_node_id(), + ray.get_runtime_context().get_accelerator_ids()[get_resource_name()][0], + ) + ) + for worker in self.workers + ] + ) + worker_cuda_visible_devices = [worker_info[1] for worker_info in worker_infos] + worker_node_ids = [worker_info[0] for worker_info in worker_infos] + + # create server actor in each node with node affinity and cuda visible devices + nnodes, gpus_per_replica_node = self.nnodes, self.gpus_per_replica_node + for node_rank in range(nnodes): + workers = self.workers[node_rank * gpus_per_replica_node : (node_rank + 1) * gpus_per_replica_node] + node_cuda_visible_devices = ",".join( + worker_cuda_visible_devices[node_rank * gpus_per_replica_node : (node_rank + 1) * gpus_per_replica_node] + ) + node_id = worker_node_ids[node_rank * gpus_per_replica_node] + prefix = self._get_server_name_prefix() + if self.is_reward_model: + name = f"{prefix}server_reward_{self.replica_rank}_{node_rank}{self.name_suffix}" + elif self.is_teacher_model: + name = f"{prefix}server_teacher_{self.replica_rank}_{node_rank}{self.name_suffix}" + else: + name = f"{prefix}server_{self.replica_rank}_{node_rank}{self.name_suffix}" + env_vars = { + **{var: "1" for var in get_platform().ray_noset_envvars()}, + **get_platform().rollout_env_vars(), + } + + server = self.server_class.options( + scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy( + node_id=node_id, + soft=False, + ), + runtime_env={"env_vars": env_vars}, + name=name, + max_concurrency=self.max_concurrency, + ).remote( + config=self.config, + model_config=self.model_config, + rollout_mode=self.rollout_mode, + workers=workers, + replica_rank=self.replica_rank, + node_rank=node_rank, + gpus_per_node=gpus_per_replica_node, + nnodes=nnodes, + cuda_visible_devices=node_cuda_visible_devices, + ) + self.servers.append(server) + + # launch http server in each node + master_address, master_port, dp_rpc_port = await self.servers[0].get_master_address.remote() + await asyncio.gather( + *[ + server.launch_server.remote( + master_address=master_address, master_port=master_port, dp_rpc_port=dp_rpc_port + ) + for server in self.servers + ] + ) + + # get http server address from first server + server_address, server_port = await self.servers[0].get_server_address.remote() + self._server_handle = self.servers[0] + self._server_address = ( + f"[{server_address}]:{server_port}" + if is_valid_ipv6_address(server_address) + else f"{server_address}:{server_port}" + ) + + async def sleep(self): + """Sleep each rollout server.""" + # Drain DP engines for safe sleep. + await self.servers[0].wait_for_requests_to_drain.remote() + await asyncio.gather(*[server.sleep.remote() for server in self.servers]) + + async def abort_all_requests(self) -> dict[str, Any]: + """Abort all ongoing generation requests across all servers. + + Returns: + dict[str, Any]: Combined abort results from all servers. + """ + results = await asyncio.gather(*[server.abort_all_requests.remote() for server in self.servers]) + + total_aborted = sum(r.get("aborted_count", 0) for r in results) + all_request_ids = [] + for r in results: + all_request_ids.extend(r.get("request_ids", [])) + + return { + "aborted_count": total_aborted, + "request_ids": all_request_ids, + "server_results": results, + } + + async def resume_generation(self): + """Resume generation on all servers after abort_all_requests.""" + await asyncio.gather(*[server.resume_generation.remote() for server in self.servers]) + + async def abort_request(self, request_id: str) -> dict[str, Any]: + """Abort a specific request. Tries all servers since we don't know which one has it. + + Args: + request_id: The ID of the request to abort. + + Returns: + dict[str, Any]: Abort result. + """ + # TODO(petersh6): we should only abort on the server that has the request. + results = await asyncio.gather(*[server.abort_request.remote(request_id) for server in self.servers]) + + for r in results: + if r.get("aborted", False): + return r + + return {"aborted": False, "request_id": request_id, "error": "Request not found on any server"} + + async def release_kv_cache(self): + # Drain all in-flight requests so that vLLM worker threads go idle + # before we touch engine.release_kv_cache() + await self.servers[0].wait_for_requests_to_drain.remote() + await asyncio.gather(*[server.release_kv_cache.remote() for server in self.servers]) + + # ----------------------------------------------------------------------- + # Hook methods for subclass overrides + # ----------------------------------------------------------------------- + + def _validate_launch_requirements(self) -> None: + """Validate requirements before launching. Override in subclasses.""" + # NOTE: We always use MP Executor backend whether it's single-node or multi-node. + # For multi-node without DP (e.g TP=16), need vllm>=0.11.1, https://github.com/vllm-project/vllm/pull/23691 + if self.config.data_parallel_size == 1 and self.nnodes > 1: + assert _VLLM_VERSION >= version.parse("0.11.1"), ( + "For multi-node MP Executor, either (1) set data_parallel_size > 1 or (2) upgrade vLLM to >= 0.11.1" + ) + + def _get_server_name_prefix(self) -> str: + """Return the Ray actor name prefix (e.g. 'vllm_').""" + return "vllm_" diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_pd_replica.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_pd_replica.py new file mode 100644 index 0000000000000000000000000000000000000000..5a3846716d0440e733454fed42f458cac76f7789 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_pd_replica.py @@ -0,0 +1,305 @@ +# Copyright 2026 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""vLLM PD-disaggregated replica: 1 prefill + N decode servers per replica, +asymmetric TP supported. MVP: prefill_replicas=1, single-node only.""" + +import asyncio +import logging +import os +import uuid +from dataclasses import replace as _dc_replace +from typing import Optional + +import ray +from ray.actor import ActorHandle + +from verl.utils.device import get_device_name, get_resource_name, is_torch_npu_available +from verl.utils.net_utils import get_free_port, is_valid_ipv6_address +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.vllm_rollout.vllm_async_server import vLLMReplica + +logger = logging.getLogger(__file__) +logger.setLevel(logging.INFO) + + +class vLLMPDReplica(vLLMReplica): + """Replica that runs vLLM in prefill-decode disaggregated mode.""" + + def __init__( + self, + replica_rank: int, + config: RolloutConfig, + model_config: HFModelConfig, + gpus_per_node: int = 8, + is_reward_model: bool = False, + is_teacher_model: bool = False, + name_suffix: str = "", + ): + super().__init__( + replica_rank, + config, + model_config, + gpus_per_node, + is_reward_model, + is_teacher_model, + name_suffix, + ) + + disagg = self.config.disaggregation + assert disagg.enabled, "vLLMPDReplica requires rollout.disaggregation.enabled=True" + + if disagg.transfer_backend not in ("nixl", "mooncake"): + raise NotImplementedError( + f"vLLMPDReplica supports transfer_backend in ('nixl', 'mooncake') in this " + f"revision; got {disagg.transfer_backend!r}. mori/ascend/fake are reserved " + f"in DisaggregationConfig and will land in follow-ups." + ) + if disagg.prefill_replicas != 1: + raise NotImplementedError(f"prefill_replicas=1 only (got {disagg.prefill_replicas})") + self._n_prefill = disagg.prefill_replicas + self._n_decode = disagg.decode_replicas + + self._prefill_tp = self.config.tensor_model_parallel_size + # Inline decode_tp default: OmegaConf/Ray serialization drops dataclass methods. + self._decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else self._prefill_tp + ) + + pd_world_size = self._prefill_tp + self._n_decode * self._decode_tp + if pd_world_size > gpus_per_node: + raise NotImplementedError( + f"PD replica needs {pd_world_size} GPUs but gpus_per_node={gpus_per_node}; " + f"single-node only in this revision (use more replicas to span nodes once " + f"multi-node lands)" + ) + if self.config.data_parallel_size != 1: + raise NotImplementedError(f"data_parallel_size=1 only (got {self.config.data_parallel_size})") + if self.config.pipeline_model_parallel_size != 1: + raise NotImplementedError( + f"pipeline_model_parallel_size=1 only " + f"(got {self.config.pipeline_model_parallel_size}); PD path does not model PP yet" + ) + + self.world_size = pd_world_size + self.gpus_per_replica_node = min(self.gpus_per_node, self.world_size) + assert self.world_size % self.gpus_per_replica_node == 0 + self.nnodes = self.world_size // self.gpus_per_replica_node + + self._prefill_servers: list[ActorHandle] = [] + self._decode_servers: list[ActorHandle] = [] + + async def launch_servers(self): + assert len(self.workers) == self.world_size, ( + f"worker count {len(self.workers)} != PD world size {self.world_size}" + ) + assert not is_torch_npu_available(check_device=False), "vLLM PD on NPU not validated" + + worker_infos = await asyncio.gather( + *[ + worker.__ray_call__.remote( + lambda self: ( + ray.get_runtime_context().get_node_id(), + ray.get_runtime_context().get_accelerator_ids()[get_resource_name()][0], + ray.util.get_node_ip_address().strip("[]"), + ) + ) + for worker in self.workers + ] + ) + + # Bind the side-channel on the prefill worker's node. + prefill_host_ip = worker_infos[0][2] + prefill_engine_id = uuid.uuid4().hex + + prefill_end = self._prefill_tp + prefill_workers = self.workers[0:prefill_end] + prefill_node_id = worker_infos[0][0] + prefill_devs = self._collect_cuda_devices(worker_infos[0:prefill_end]) + + # Keep side-channel sockets reserved until all actors bind. + reserved_socks = [] + prefill_side_channel_port, prefill_sock = get_free_port(prefill_host_ip, with_alive_sock=True) + reserved_socks.append(prefill_sock) + try: + prefill_kv_cfg = self._build_kv_transfer_config( + role="prefill", + engine_id=prefill_engine_id, + transfer_backend=self.config.disaggregation.transfer_backend, + mooncake_protocol=self.config.disaggregation.mooncake_protocol, + ) + self._prefill_servers = [ + self._spawn_pd_server( + role="prefill", + workers=prefill_workers, + node_id=prefill_node_id, + cuda_visible_devices=prefill_devs, + tp=self._prefill_tp, + kv_transfer_config=prefill_kv_cfg, + side_channel_host=prefill_host_ip, + side_channel_port=prefill_side_channel_port, + mooncake_bootstrap_port=prefill_side_channel_port, + actor_name=f"vllm_server_{self.replica_rank}_0{self.name_suffix}", + zmq_base_trainer_rank=0, + ) + ] + + for i in range(self._n_decode): + start = self._prefill_tp + i * self._decode_tp + end = start + self._decode_tp + workers_i = self.workers[start:end] + node_id_i = worker_infos[start][0] + devs_i = self._collect_cuda_devices(worker_infos[start:end]) + + decode_side_channel_port, decode_sock = get_free_port(prefill_host_ip, with_alive_sock=True) + reserved_socks.append(decode_sock) + decode_kv_cfg = self._build_kv_transfer_config( + role="decode", + engine_id=uuid.uuid4().hex, + transfer_backend=self.config.disaggregation.transfer_backend, + mooncake_protocol=self.config.disaggregation.mooncake_protocol, + ) + self._decode_servers.append( + self._spawn_pd_server( + role="decode", + workers=workers_i, + node_id=node_id_i, + cuda_visible_devices=devs_i, + tp=self._decode_tp, + kv_transfer_config=decode_kv_cfg, + side_channel_host=prefill_host_ip, + side_channel_port=decode_side_channel_port, + mooncake_bootstrap_port=prefill_side_channel_port, + actor_name=f"vllm_server_decode_{self.replica_rank}_{i}{self.name_suffix}", + zmq_base_trainer_rank=start, + ) + ) + + await asyncio.gather( + *[ + server.launch_server.remote(master_address=None, master_port=None, dp_rpc_port=None) + for server in self._prefill_servers + self._decode_servers + ] + ) + finally: + for sock in reserved_socks: + sock.close() + + await self._prefill_servers[0].set_pd_peer.remote( + self._decode_servers, + prefill_side_channel_port, + prefill_engine_id, + ) + + self.servers = list(self._prefill_servers) + list(self._decode_servers) + prefill_address, prefill_port = await self._prefill_servers[0].get_server_address.remote() + self._server_handle = self._prefill_servers[0] + self._server_address = ( + f"[{prefill_address}]:{prefill_port}" + if is_valid_ipv6_address(prefill_address) + else f"{prefill_address}:{prefill_port}" + ) + + logger.info( + "vLLMPDReplica rank=%s launched: prefill=%s (engine_id=%s, side_channel=%s:%d), decodes=%d", + self.replica_rank, + self._server_address, + prefill_engine_id, + prefill_host_ip, + prefill_side_channel_port, + len(self._decode_servers), + ) + + @staticmethod + def _collect_cuda_devices(worker_infos) -> str: + return ",".join(worker_info[1] for worker_info in worker_infos) + + @staticmethod + def _build_kv_transfer_config( + role: str, + engine_id: str, + transfer_backend: str, + mooncake_protocol: Optional[str] = None, + ) -> dict: + """Assemble vLLM's ``--kv-transfer-config`` payload.""" + role_to_kv_role = { + "prefill": "kv_producer", + "decode": "kv_consumer", + } + connector = { + "nixl": "NixlConnector", + "mooncake": "MooncakeConnector", + }[transfer_backend] + cfg: dict = { + "kv_connector": connector, + "kv_role": role_to_kv_role[role], + "engine_id": engine_id, + "kv_buffer_device": get_device_name(), + } + if transfer_backend == "mooncake" and mooncake_protocol: + cfg["kv_connector_extra_config"] = {"mooncake_protocol": mooncake_protocol} + return cfg + + def _spawn_pd_server( + self, + role: str, + workers: list[ActorHandle], + node_id: str, + cuda_visible_devices: str, + tp: int, + kv_transfer_config: dict, + side_channel_host: str, + side_channel_port: int, + mooncake_bootstrap_port: int, + actor_name: str, + zmq_base_trainer_rank: int = 0, + ) -> ActorHandle: + """Construct one PD ``vLLMHttpServer`` actor.""" + per_role_config = _dc_replace(self.config, tensor_model_parallel_size=tp) + + env_vars = { + "RAY_EXPERIMENTAL_NOSET_CUDA_VISIBLE_DEVICES": "1", + "RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES": "1", + "NCCL_CUMEM_ENABLE": "0", + "VLLM_NIXL_SIDE_CHANNEL_HOST": side_channel_host, + "VLLM_NIXL_SIDE_CHANNEL_PORT": str(side_channel_port), + "VLLM_MOONCAKE_BOOTSTRAP_PORT": str(mooncake_bootstrap_port), + # Avoid Mooncake TCP port exhaustion under validation concurrency. + "MC_TCP_ENABLE_CONNECTION_POOL": os.environ.get("MC_TCP_ENABLE_CONNECTION_POOL", "1"), + "VERL_ZMQ_BASE_TRAINER_RANK": str(zmq_base_trainer_rank), + "VERL_RAY_JOB_ID": ray.get_runtime_context().get_job_id(), + } + + return self.server_class.options( + scheduling_strategy=ray.util.scheduling_strategies.NodeAffinitySchedulingStrategy( + node_id=node_id, + soft=False, + ), + runtime_env={"env_vars": env_vars}, + name=actor_name, + max_concurrency=self.max_concurrency, + ).remote( + config=per_role_config, + model_config=self.model_config, + rollout_mode=self.rollout_mode, + workers=workers, + replica_rank=self.replica_rank, + node_rank=0, + gpus_per_node=self.gpus_per_replica_node, + nnodes=1, + cuda_visible_devices=cuda_visible_devices, + disaggregation_role=role, + disaggregation_kv_transfer_config=kv_transfer_config, + ) diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_rollout.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_rollout.py new file mode 100644 index 0000000000000000000000000000000000000000..4d99d93ebb2c74fb86f3154698d88800e6aa13c6 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/vllm_rollout.py @@ -0,0 +1,327 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +""" +The vllm_rollout that can be applied in different backend +When working with FSDP: +- Use DTensor weight loader (recommended) or HF weight loader +- Utilize state_dict from the FSDP to synchronize the weights among tp ranks in vLLM +When working with Megatron: +- Use Megatron weight loader +- During training, only the current pp stage holds the parameters +- Before inference, broadcast the parameters of the current pp rank + to all other pp ranks (all pp ranks holds all the parameters) +- Bind the parameters to the inference engine +- Do inference in tp. pp is treated as additional dp +- After inference, all the parameters that doesn't belong to this pp rank is freed. +""" + +import logging +import os +import time +from typing import Any, Generator, Optional + +import ray +import torch +from packaging import version as vs +from torch.distributed.device_mesh import DeviceMesh + +from verl import DataProto +from verl.third_party.vllm import VLLM_SLEEP_LEVEL, get_version +from verl.utils.device import get_device_id, is_support_ipc +from verl.workers.config import HFModelConfig, RolloutConfig +from verl.workers.rollout.base import BaseRollout +from verl.workers.rollout.vllm_rollout.bucketed_weight_transfer import BucketedWeightSender +from verl.workers.rollout.vllm_rollout.utils import get_device_uuid + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "INFO")) + + +def _check_vllm_version_for_sleep_level(): + # https://github.com/vllm-project/vllm/issues/25171 + minver = "0.11.0" + current_version = get_version("vllm") + if not current_version: + logger.warning("Could not determine vLLM version, assuming an older version for sleep_level configuration.") + return False + return vs.parse(current_version) >= vs.parse(minver) + + +def _should_expand_vllm_moe_params() -> bool: + current_version = get_version("vllm") + if not current_version: + return False + + try: + return vs.parse(current_version) <= vs.parse("0.24.0") + except vs.InvalidVersion: + return False + + +async def _iter_vllm_compatible_moe_params(weights): + """Expand Transformers 5 packed MoE expert tensors to vLLM checkpoint keys. + + Transformers 5 stores Qwen-style MoE experts as packed 3D parameters: + ``mlp.experts.gate_up_proj`` with shape + ``[num_experts, 2 * intermediate_size, hidden_size]`` and + ``mlp.experts.down_proj`` with shape + ``[num_experts, hidden_size, intermediate_size]``. vLLM's Qwen MoE reload + path still accepts the original per-expert checkpoint keys during live + weight sync, so stream those keys without materializing a full dict. + """ + from verl.workers.rollout.utils import ensure_async_iterator + + async for name, tensor in ensure_async_iterator(weights): + if name.endswith(".mlp.experts.gate_up_proj") and tensor.dim() == 3: + gate, up = tensor.chunk(2, dim=1) + base = name.removesuffix(".gate_up_proj") + for expert_id in range(tensor.size(0)): + yield f"{base}.{expert_id}.gate_proj.weight", gate[expert_id].contiguous() + yield f"{base}.{expert_id}.up_proj.weight", up[expert_id].contiguous() + continue + + if name.endswith(".mlp.experts.down_proj") and tensor.dim() == 3: + base = name.removesuffix(".down_proj") + for expert_id in range(tensor.size(0)): + yield f"{base}.{expert_id}.down_proj.weight", tensor[expert_id].contiguous() + continue + + yield name, tensor + + +class ServerAdapter(BaseRollout): + """ + vLLM server adapter used in native async mode, serve as a client to request vLLM server + to resume/release/update weights and kv_cache. + """ + + def __init__( + self, + config: RolloutConfig, + model_config: HFModelConfig, + device_mesh: DeviceMesh, + replica_rank: int = -1, + ): + super().__init__(config, model_config, device_mesh) + self.server_handle: ray.actor.ActorHandle = None + + rank = int(os.environ["RANK"]) + local_world_size = int(os.environ["RAY_LOCAL_WORLD_SIZE"]) + # PD asymmetric layout inflates per-replica footprint; must match + # llm_server.py:_initialize_llm_servers or trainer-to-replica mapping breaks. + prefill_tp = self.config.tensor_model_parallel_size + disagg = getattr(self.config, "disaggregation", None) + if disagg is not None and getattr(disagg, "enabled", False): + decode_tp = ( + disagg.decode_tensor_model_parallel_size + if disagg.decode_tensor_model_parallel_size is not None + else prefill_tp + ) + per_replica = prefill_tp * disagg.prefill_replicas + decode_tp * disagg.decode_replicas + else: + decode_tp = prefill_tp + per_replica = prefill_tp + rollout_world_size = per_replica * self.config.data_parallel_size * self.config.pipeline_model_parallel_size + if replica_rank == -1: + self.replica_rank = rank // rollout_world_size + else: + self.replica_rank = replica_rank + self.rollout_rank = rank % rollout_world_size + self.node_rank = self.rollout_rank // local_world_size + + # Map each trainer rank to its co-located vLLM server so weight-update + # IPC handles stay on the GPU where they were created. Offset math + # assumes prefill_replicas == 1 (enforced by vLLMPDReplica); if that + # ever lifts, update both this block and vLLMPDReplica.launch_servers. + self._pd_role: Optional[str] = None + self._pd_server_index: Optional[int] = None + self._pd_tp_local_rank: Optional[int] = None + if disagg is not None and getattr(disagg, "enabled", False): + footprint = prefill_tp + disagg.decode_replicas * decode_tp + local = self.rollout_rank % footprint + if local < prefill_tp: + self._pd_role = "prefill" + self._pd_server_index = 0 + self._pd_tp_local_rank = local + else: + off = local - prefill_tp + self._pd_role = "decode" + self._pd_server_index = off // decode_tp + self._pd_tp_local_rank = off % decode_tp + # Each role's TP-rank-0 owns the adapter (vs colocated where every + # rollout_rank=0 owns it). One log line per PD rank at startup so + # a deadlock report can be traced back to the role mapping. + self._has_server = self._pd_tp_local_rank == 0 + logger.info( + "vllm PD ServerAdapter: rank=%d replica=%d rollout=%d role=%s server_idx=%s tp_local=%s has_server=%s", + rank, + self.replica_rank, + self.rollout_rank, + self._pd_role, + self._pd_server_index, + self._pd_tp_local_rank, + self._has_server, + ) + else: + self._has_server = self.rollout_rank == 0 + + if config.layered_summon or (config.expert_parallel_size > 1 and not _check_vllm_version_for_sleep_level()): + logger.warning("Setting the sleep level to 1 may cause a memory overflow.") + self.sleep_level = 1 + else: + self.sleep_level = VLLM_SLEEP_LEVEL + + self.device_uuid = get_device_uuid(get_device_id()) + # Use replica_rank + node-local rank to form ZMQ handle instead of GPU UUID, + # because CheckpointEngineWorker and vLLM worker may see different GPU UUIDs + # when CUDA_VISIBLE_DEVICES differs between processes (common on ROCm/AMD). + # Must use node-local rank (not rollout_rank) so it matches vLLM worker's + # local_rank on every node. Include replica_rank to avoid collisions when + # multiple replicas share a node, and the Ray job id so two independent + # verl jobs on the same host (or a new run after a crashed one with a + # stale socket file) cannot collide on the shared /tmp namespace. + local_rank = self.rollout_rank % local_world_size + job_id = ray.get_runtime_context().get_job_id() + self.zmq_handle = f"ipc:///tmp/rl-colocate-zmq-{job_id}-replica-{self.replica_rank}-rank-{local_rank}.sock" + + self.use_shm = not is_support_ipc() + if self.use_shm: + logger.warning( + "IPC is not supported on your devices. Falling back to shared memory for weight transfer, " + "which may cause performance degradation. If you are using Ascend NPUs, please ensure that " + "your software and CANN toolkit versions meet the requirements for IPC support. (Ascend HDK version " + ">= 25.3.rc1 and CANN toolkit version >= 8.3.RC1)" + ) + + def _ensure_server_handle(self) -> bool: + """Lazy-init server handle. Returns False if this rank should not proceed.""" + if not self._has_server: + return False + # Lazy init http server adapter because http server is launched after hybrid engine. + if self.server_handle is None: + prefix = self._get_server_name_prefix() + if self._pd_role == "prefill": + actor_name = f"{prefix}server_{self.replica_rank}_0" + elif self._pd_role == "decode": + actor_name = f"{prefix}server_decode_{self.replica_rank}_{self._pd_server_index}" + else: + actor_name = f"{prefix}server_{self.replica_rank}_{self.node_rank}" + self.server_handle = ray.get_actor(actor_name) + return True + + async def _execute_method( + self, + method: str, + non_block: bool = False, + timeout: Optional[float] = None, + args: tuple = (), + kwargs: Optional[dict] = None, + ) -> Any: + """Execute method on inference engine via ray. + + Args: + method: The method name to execute on the server. + non_block: If True, execute the method asynchronously and return immediately. + timeout: Timeout for the collective_rpc call. + args: Positional arguments for the method. + kwargs: Keyword arguments for the method. + + Returns: + The result of the method execution, or None if non_block=True. + """ + if not self._ensure_server_handle(): + return None + + future = self.server_handle.collective_rpc.remote(method, timeout=timeout, args=args, kwargs=kwargs) + return future if non_block else await future + + async def resume(self, tags: list[str]): + """Resume rollout weights or kv cache in GPU memory. + + Args: + tags: weights or kv_cache. + """ + if self.config.free_cache_engine and self._ensure_server_handle(): + await self.server_handle.wake_up.remote(tags=tags) + + async def release(self): + """Release weights and kv cache in GPU memory.""" + if self.config.free_cache_engine and self._ensure_server_handle(): + await self.server_handle.sleep.remote() + + @torch.no_grad() + async def update_weights( + self, + weights: Generator[tuple[str, torch.Tensor], None, None], + global_steps: int = None, + wire_format: str = "named_tensors", + **kwargs, + ): + """Update model weights via CUDA IPC (fallback to shared memory if IPC not supported) to inference workers.""" + assert wire_format == "named_tensors", ( + f"vLLM rollout only consumes full named tensors; got wire_format={wire_format!r}" + ) + start_time = time.time() + + future = await self._execute_method( + "update_weights_from_ipc", + non_block=True, + kwargs={**kwargs, "use_shm": self.use_shm}, + ) + + bucket_size_mb = self.config.checkpoint_engine.update_weights_bucket_megabytes + sender = BucketedWeightSender( + zmq_handle=self.zmq_handle, + bucket_size_mb=bucket_size_mb, + use_shm=self.use_shm, + ) + if _should_expand_vllm_moe_params() and not ( + kwargs.get("peft_config") is not None and kwargs.get("base_sync_done", False) + ): + weights = _iter_vllm_compatible_moe_params(weights) + await sender.async_send_weights(weights) + + if future is not None: + await future + + # reset caches after updating weights + if self._has_server: + await self.server_handle.clear_kv_cache.remote() + if global_steps is not None: + await self.server_handle.set_global_steps.remote(global_steps) + + if self.replica_rank == 0 and self.rollout_rank == 0: + logger.info(f"update_weights done, time cost: {time.time() - start_time:.2f}s") + + def _get_server_name_prefix(self) -> str: + """Return the Ray actor name prefix matching the rollout type (e.g. 'vllm_').""" + return f"{self.config.get('name', 'vllm')}_" + + def generate_sequences(self, prompts: DataProto) -> DataProto: + """Batch generate sequences in sync mode. + + Note: ServerAdapter uses async server mode and does not support synchronous + generation. Since SPMD mode was retired (PR #4411), the generation workflow + should use the async server interface instead. + + Raises: + NotImplementedError: Always raised as sync generation is not supported. + """ + raise NotImplementedError( + "ServerAdapter does not support synchronous generate_sequences(). " + "The vLLM SPMD mode was retired in PR #4411. For batch generation, " + "please use the async server interface via vLLMReplica and LLMServerClient, " + "or use HFRollout for synchronous generation. " + "See https://github.com/verl-project/verl/issues/4682 for more details." + ) diff --git a/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/weight_update_utils.py b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/weight_update_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..463a39c05a17a540f9f6fb0c0b4250d6cfbb6bb7 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/rollout/vllm_rollout/weight_update_utils.py @@ -0,0 +1,65 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch + +WeightUpdate = tuple[str, torch.Tensor] + + +def split_buffer_updates( + model: torch.nn.Module, weights: list[WeightUpdate] +) -> tuple[list[WeightUpdate], list[WeightUpdate], dict[str, torch.Tensor]]: + """Split incoming weight updates into parameter and buffer updates. + + Returns the parameter updates, the buffer updates, and the model's + ``named_buffers`` map so callers can reuse it without re-iterating. + """ + named_buffers = dict(model.named_buffers()) + param_updates, buffer_updates = [], [] + for name, tensor in weights: + if name in named_buffers: + buffer_updates.append((name, tensor)) + else: + param_updates.append((name, tensor)) + return param_updates, buffer_updates, named_buffers + + +@torch.no_grad() +def apply_buffer_updates( + model: torch.nn.Module, + buffer_updates: list[WeightUpdate], + named_buffers: dict[str, torch.Tensor] | None = None, +) -> int: + """Copy updated buffer tensors into the target model in-place.""" + if not buffer_updates: + return 0 + + if named_buffers is None: + named_buffers = dict(model.named_buffers()) + loaded = 0 + for name, tensor in buffer_updates: + if name not in named_buffers: + continue + + target = named_buffers[name] + if target.shape != tensor.shape: + raise ValueError( + f"Buffer shape mismatch for {name}: expected {tuple(target.shape)}, got {tuple(tensor.shape)}" + ) + + source = tensor.to(device=target.device, dtype=target.dtype, non_blocking=False) + target.copy_(source, non_blocking=False) + loaded += 1 + + return loaded diff --git a/verl_0720_main/verl/verl/workers/utils/__init__.py b/verl_0720_main/verl/verl/workers/utils/__init__.py new file mode 100644 index 0000000000000000000000000000000000000000..1cd1e8433dffa0b3ba420be3e346f4f5cd062014 --- /dev/null +++ b/verl_0720_main/verl/verl/workers/utils/__init__.py @@ -0,0 +1,13 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. diff --git a/verl_0720_main/verl/verl/workers/utils/losses.py b/verl_0720_main/verl/verl/workers/utils/losses.py new file mode 100644 index 0000000000000000000000000000000000000000..ebaa4e4d8b759db980f62bf96f83be305f54847e --- /dev/null +++ b/verl_0720_main/verl/verl/workers/utils/losses.py @@ -0,0 +1,205 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +import torch +from tensordict import TensorDict + +from verl.trainer.ppo.core_algos import agg_loss, compute_value_loss, get_policy_loss_fn, kl_penalty +from verl.utils import tensordict_utils as tu +from verl.utils.dataset.dataset_utils import DatasetPadMode +from verl.utils.metric import AggregationType, Metric +from verl.utils.torch_functional import masked_mean, masked_sum +from verl.workers.config import ActorConfig, CriticConfig +from verl.workers.utils.padding import no_padding_2_padding + + +def sft_loss(config: ActorConfig, model_output, data: TensorDict, dp_group=None): + pad_mode = tu.get_non_tensor_data(data=data, key="pad_mode", default=DatasetPadMode.NO_PADDING) + dp_size = data["dp_size"] + batch_num_tokens = data["batch_num_tokens"] + + log_prob = model_output["log_probs"] + + if pad_mode == DatasetPadMode.NO_PADDING: + # log_prob and loss mask are nested tensors of shape [bsz, j1] + # for each sample, loss mask shape is [1, prompt_length + response_length] + loss_mask = data["loss_mask"] + + log_prob_flatten = log_prob.values() + loss_mask_flatten = loss_mask.values() + + # left-shift the loss mask by one token to align with log_prob + loss_mask_flatten = torch.roll(loss_mask_flatten, shifts=-1, dims=0) + + # NOTE: loss is averaged over all tokens in the batch across all data parallel groups, + # For FSDP backend, the loss is directly used for backward; while for Megatron backend, + # the loss should be scaled by `num_microbatches` for pp schedule. + loss = -masked_sum(log_prob_flatten, loss_mask_flatten) / batch_num_tokens * dp_size + else: + response_mask = data["response_mask"].to(bool) + loss = -masked_sum(log_prob, response_mask) / batch_num_tokens * dp_size + + return loss, {} + + +def ppo_loss(config: ActorConfig, model_output, data: TensorDict, dp_group=None): + """Computes ppo loss from model output (log_prob, entropy, values, etc. ) and old_log_probs from data.""" + log_prob = no_padding_2_padding(model_output["log_probs"], data) + entropy = model_output.get("entropy", None) + if entropy is not None: + entropy = no_padding_2_padding(entropy, data) + + # global batch info for loss aggregation + config.global_batch_info["dp_size"] = data["dp_size"] + config.global_batch_info["batch_num_tokens"] = data["batch_num_tokens"] + config.global_batch_info["global_batch_size"] = data["global_batch_size"] + config.global_batch_info["loss_scale_factor"] = config.loss_scale_factor + + # assumes that if any of the global batch info is set, the policy_loss_fn will + # normalize using dp_size/global_bsz/global_token; in this case, metric aggregation should be SUM + # to reflect the mean loss over the global batch + if ( + data["dp_size"] > 1 + or data["batch_num_tokens"] is not None + or data["global_batch_size"] is not None + or config.loss_scale_factor is not None + ): + metric_aggregation = AggregationType.SUM + else: + metric_aggregation = AggregationType.MEAN + + metrics = {} + + # select fields and convert to padded tensor + fields = ["response_mask", "old_log_probs", "advantages"] + if "rollout_is_weights" in data: + fields.append("rollout_is_weights") + if "ref_log_prob" in data: + fields.append("ref_log_prob") + data = data.select(*fields).to_padded_tensor() + + response_mask = data["response_mask"].to(bool) + # compute policy loss + old_log_prob = data["old_log_probs"] + advantages = data["advantages"] + rollout_is_weights = data.get("rollout_is_weights", None) + + loss_agg_mode = config.loss_agg_mode + + loss_mode = config.policy_loss.get("loss_mode", "vanilla") + + policy_loss_fn = get_policy_loss_fn(loss_mode) + pg_loss, pg_metrics = policy_loss_fn( + old_log_prob=old_log_prob, + log_prob=log_prob, + advantages=advantages, + response_mask=response_mask, + loss_agg_mode=loss_agg_mode, + config=config, + rollout_is_weights=rollout_is_weights, + ) + + # AggregationType.MEAN for pg metrics: assumes policy_loss_fn normalizes by local_bsz/local_tokens + # Ex: in compute_policy_loss_vanilla, pg_metrics are pg_clipfrac, ppo_kl, pg_clipfrac_lower + pg_metrics = Metric.from_dict(pg_metrics, aggregation=AggregationType.MEAN) + + metrics.update(pg_metrics) + metrics["actor/pg_loss"] = Metric(value=pg_loss, aggregation=metric_aggregation) + policy_loss = pg_loss + + # add entropy loss + if entropy is not None: + entropy_loss = agg_loss( + loss_mat=entropy, loss_mask=response_mask, loss_agg_mode=loss_agg_mode, **config.global_batch_info + ) + entropy_coeff = config.entropy_coeff + policy_loss -= entropy_coeff * entropy_loss + metrics["actor/entropy_loss"] = Metric(value=entropy_loss, aggregation=metric_aggregation) + + # add kl loss + if config.use_kl_loss: + ref_log_prob = data["ref_log_prob"] + # compute kl loss + kld = kl_penalty(logprob=log_prob, ref_logprob=ref_log_prob, kl_penalty=config.kl_loss_type) + kl_loss = agg_loss( + loss_mat=kld, loss_mask=response_mask, loss_agg_mode=config.loss_agg_mode, **config.global_batch_info + ) + + policy_loss += kl_loss * config.kl_loss_coef + metrics["kl_loss"] = Metric(value=kl_loss, aggregation=metric_aggregation) + metrics["kl_coef"] = config.kl_loss_coef + + return policy_loss, metrics + + +def value_loss(config: CriticConfig, model_output, data: TensorDict, dp_group=None): + """value loss + + Args: + config: CriticConfig + model_output: model output from the model + data: the input to the model + dp_group: data paralle group + + Returns: + value loss + """ + vpreds = no_padding_2_padding(model_output["values"], data) # (bsz, response_length) + + # Normalize the value loss over the global mini-batch (dp_size / batch_num_tokens / + # global_batch_size) instead of the local micro-batch, so the accumulated critic gradient is + # invariant to how the mini-batch is split into micro-batches (as the actor's ppo_loss does). + dp_size = data["dp_size"] + batch_num_tokens = data["batch_num_tokens"] + global_batch_size = data["global_batch_size"] + + # When the loss is normalized over the global batch, each micro-batch contributes a partial sum, + # so the loss metric must be aggregated with SUM to reflect the global-batch mean. + if ( + dp_size > 1 + or batch_num_tokens is not None + or global_batch_size is not None + or config.loss_scale_factor is not None + ): + metric_aggregation = AggregationType.SUM + else: + metric_aggregation = AggregationType.MEAN + + # select fields and convert to padded tensor + data = data.select("values", "returns", "response_mask").to_padded_tensor() + values = data["values"] + returns = data["returns"] + response_mask = data["response_mask"].to(bool) + + vf_loss, vf_clipfrac = compute_value_loss( + vpreds=vpreds, + values=values, + returns=returns, + response_mask=response_mask, + cliprange_value=config.cliprange_value, + loss_agg_mode=config.loss_agg_mode, + dp_size=dp_size, + batch_num_tokens=batch_num_tokens, + global_batch_size=global_batch_size, + loss_scale_factor=config.loss_scale_factor, + ) + + metrics = { + "critic/vf_loss": Metric(value=vf_loss, aggregation=metric_aggregation), + "critic/vf_clipfrac": vf_clipfrac.detach().item(), + "critic/vpred_mean": masked_mean(vpreds, response_mask).detach().item(), + } + + return vf_loss, metrics diff --git a/verl_0720_main/verl/verl/workers/utils/padding.py b/verl_0720_main/verl/verl/workers/utils/padding.py new file mode 100644 index 0000000000000000000000000000000000000000..ed8d145520571f60452579a85ffba981dd8d64ec --- /dev/null +++ b/verl_0720_main/verl/verl/workers/utils/padding.py @@ -0,0 +1,231 @@ +# Copyright 2025 Bytedance Ltd. and/or its affiliates +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import torch +import torch.nn.functional as F +from tensordict import TensorDict + +from verl.utils import tensordict_utils as tu +from verl.utils.attention_utils import index_first_axis, unpad_input + + +def left_right_2_no_padding(data: TensorDict) -> TensorDict: + """ + Convert TensorDict from left-right padding to no-padding format. + + Args: + data: TensorDict with "input_ids", "attention_mask", "response_mask", "position_ids" + + Returns: + data: TensorDict with + - Tensor includes NestedTensors like "input_ids", "loss_mask", "position_ids" + - NonTensorData includes "max_seq_len", "max_response_len", "indices" + + Note: + 1. the return input_ids/position_ids/loss_mask are nested tensor. + 2. we will remove "attention_mask", "response" in the return data, but "response_mask" is kept. + """ + assert "input_ids" in data, "input_ids is required in left-right padding data" + assert "attention_mask" in data, "attention_mask is required in left-right padding data" + assert "response_mask" in data, "response_mask is required in left-right padding data" + assert "position_ids" in data, "position_ids is required in left-right padding data" + + input_ids = data.pop("input_ids") + attention_mask = data["attention_mask"] + response_mask = data["response_mask"] + position_ids = data["position_ids"] # (bs, seq_len) or # (bs, 4, seq_len) + + max_seq_len, max_response_len = input_ids.shape[1], response_mask.shape[1] + tu.assign_non_tensor_data(data, "max_seq_len", max_seq_len) + tu.assign_non_tensor_data(data, "max_response_len", max_response_len) + + input_ids_rmpad, indices, cu_seqlens, *_ = unpad_input(input_ids.unsqueeze(-1), attention_mask) + tu.assign_non_tensor_data(data, "indices", indices) + + input_ids_nested = torch.nested.nested_tensor_from_jagged(input_ids_rmpad.squeeze(-1), offsets=cu_seqlens) + + position_ids_list = [] + for i in range(attention_mask.shape[0]): + curr_mask = attention_mask[i].bool() + curr_pos_ids = position_ids[i] + if curr_pos_ids.dim() == 1: # (seq_len,) + valid_ids = curr_pos_ids[curr_mask] + else: # (4, seq_len) + valid_ids = curr_pos_ids[:, curr_mask] + position_ids_list.append(valid_ids) + position_ids_nested = torch.nested.as_nested_tensor(position_ids_list, layout=torch.jagged) + + data["input_ids"] = input_ids_nested + data["position_ids"] = position_ids_nested + data["loss_mask"] = data["response_mask"] + + routed_experts = data.get("routed_experts", None) + if routed_experts is not None and not routed_experts.is_nested: + if routed_experts.max() <= 255: + routed_experts = routed_experts.to(torch.uint8) + routed_experts_rmpad = index_first_axis(routed_experts.unsqueeze(-1).flatten(0, 1), indices) + routed_experts_nested = torch.nested.nested_tensor_from_jagged( + routed_experts_rmpad.squeeze(-1), offsets=cu_seqlens + ) + data["routed_experts"] = routed_experts_nested + + # (bsz, seqlen, topk) + teacher_logprobs = data.get("teacher_logprobs", None) + teacher_ids = data.get("teacher_ids", None) + if teacher_logprobs is not None and teacher_ids is not None: + teacher_logprobs_rmpad = index_first_axis(teacher_logprobs.unsqueeze(-1).flatten(0, 1), indices) + teacher_ids_rmpad = index_first_axis(teacher_ids.unsqueeze(-1).flatten(0, 1), indices) + teacher_logprobs_nested = torch.nested.nested_tensor_from_jagged( + teacher_logprobs_rmpad.squeeze(-1), offsets=cu_seqlens + ) + teacher_ids_nested = torch.nested.nested_tensor_from_jagged(teacher_ids_rmpad.squeeze(-1), offsets=cu_seqlens) + data["teacher_logprobs"] = teacher_logprobs_nested + data["teacher_ids"] = teacher_ids_nested + + return data + + +def no_padding_2_padding(tensor: torch.Tensor, data: TensorDict) -> torch.Tensor: + """Slice response from unpad model output. + + Args: + tensor: a nested tensor or a tensor of shape (total_nnz,*), + total_nnz is the total number of tokens across all sequences in the batch + + data: TensorDict with "prompts", "responses", "attention_mask" + + Returns: + tensor: sliced response tensor of shape [bsz, max_response_len, *] + """ + values = tensor.values() if tensor.is_nested else tensor + prompt_ids = data["prompts"] + response_ids = data["responses"] + + max_response_len = tu.get_non_tensor_data(data=data, key="max_response_len", default=-1) + + if prompt_ids.is_nested: + prompt_lens = prompt_ids.offsets().diff() + response_lens = response_ids.offsets().diff() + if max_response_len < 0: + max_response_len = response_lens.max().item() + else: + attention_mask = data["attention_mask"] + assert not attention_mask.is_nested + prompt_lens = attention_mask[:, : prompt_ids.shape[1]].sum(dim=1) + response_lens = attention_mask[:, prompt_ids.shape[1] :].sum(dim=1) + max_response_len = response_ids.shape[1] + + sequence_lens = prompt_lens + response_lens + sequence_offsets = sequence_lens.cumsum(dim=0) + assert sequence_offsets[-1].item() == values.shape[0] + assert not prompt_lens.eq(0).any(), f"seq_offset - resp_len - 1 assumes prompt_len > 0. Got {prompt_lens}" + + response_list = [] + # Skip padding dimensions after sequence dimensions, if any. + skip_padding = (0, 0) * (values.ndim - 1) + for resp_len, seq_offset in zip(response_lens, sequence_offsets, strict=True): + pad_size = max_response_len - resp_len + # left-shift model output by one token for log_probs/values + response_list.append(F.pad(values[seq_offset - resp_len - 1 : seq_offset - 1], (*skip_padding, 0, pad_size))) + + output = torch.stack(response_list, dim=0) + return output + + +def build_attention_mask_from_nested(input_ids: torch.Tensor, max_seq_len: int | None = None) -> torch.Tensor: + """Build a padded full-sequence attention mask from nested input ids.""" + assert input_ids.is_nested, "input_ids must be a nested tensor" + device = input_ids.values().device + seq_lens = input_ids.offsets().diff().to(device=device) + if max_seq_len is None: + max_seq_len = int(seq_lens.max().item()) + positions = torch.arange(max_seq_len, device=device).unsqueeze(0) + return (positions < seq_lens.unsqueeze(1)).to(torch.int32) + + +def embeds_padding_2_no_padding(data: TensorDict) -> TensorDict: + """ + Convert TensorDict from prompt embeds with padding to no-padding format. + + Currently we expect the prompt embedding mask to be [1111000...] format, + which means the valid tokens are continuous and start from the left. + + Args: + data: TensorDict with "prompt_embeds", "prompt_embeds_mask", + "negative_prompt_embeds", "negative_prompt_embeds_mask" + + Returns: + data: TensorDict with + - Tensor includes NestedTensors "prompt_embeds", "prompt_embeds_mask", + "negative_prompt_embeds", "negative_prompt_embeds_mask" + """ + + def _to_nested(embeds: torch.Tensor, mask: torch.Tensor): + """Strip padding from (bs, seq_len, dim) embeds using the boolean mask and return nested tensors.""" + embeds_list, mask_list = [], [] + for i in range(mask.shape[0]): + curr_mask = mask[i].bool() + embeds_list.append(embeds[i, curr_mask, :]) + mask_list.append(curr_mask[curr_mask]) + return ( + torch.nested.as_nested_tensor(embeds_list, layout=torch.jagged), + torch.nested.as_nested_tensor(mask_list, layout=torch.jagged), + ) + + data["prompt_embeds"], data["prompt_embeds_mask"] = _to_nested(data["prompt_embeds"], data["prompt_embeds_mask"]) + + if isinstance(data.get("negative_prompt_embeds", None), torch.Tensor): + data["negative_prompt_embeds"], data["negative_prompt_embeds_mask"] = _to_nested( + data["negative_prompt_embeds"], data["negative_prompt_embeds_mask"] + ) + + return data + + +def response_from_nested(tensor: torch.Tensor, response_mask: torch.Tensor) -> torch.Tensor: + """Extract response from nested model output. + + Args: + tensor: a nested tensor with shape (bsz, prompt_len + response_len) + response_mask: a nested tensor with shape (bsz, response_len) + + Returns: + tensor: a nested tensor with shape (bsz, response_len) + """ + values, offsets = tensor.values(), tensor.offsets() + response_lens = response_mask.offsets().diff() + response_list = [] + for resp_len, seq_offset in zip(response_lens, offsets[1:], strict=True): + # left-shift model output by one token for log_probs/values + response_list.append(values[seq_offset - resp_len - 1 : seq_offset - 1]) + return torch.nested.as_nested_tensor(response_list, layout=torch.jagged) + + +def response_to_nested(tensor: torch.Tensor, response_mask: torch.Tensor) -> torch.Tensor: + """Convert padded response tensor to nested tensor. + + Args: + tensor: a tensor with shape (bsz, response_len) + response_mask: a nested tensor with shape (bsz, response_len) + + Returns: + tensor: a nested tensor with shape (bsz, response_len) + """ + assert response_mask.is_nested + response_lens = response_mask.offsets().diff() + response_list = [] + for i in range(tensor.shape[0]): + response_list.append(tensor[i, : response_lens[i]]) + + return torch.nested.as_nested_tensor(response_list, layout=torch.jagged)