From 30911f133aa300ae9d8e341dba8e63192335705e Mon Sep 17 00:00:00 2001 From: shengguangming Date: Thu, 31 Oct 2024 14:29:44 +0800 Subject: [PATCH] [init] feat: upload first open source version of verl --- .gitignore | 110 ++ .readthedocs.yaml | 16 + .style.yapf | 5 + LICENSE | 202 ++++ Notice.txt | 1 + README.md | 168 ++++ docs/Makefile | 20 + docs/README.md | 19 + docs/_static/logo.png | Bin 0 -> 84701 bytes docs/advance/dpo_extension.rst | 271 +++++ docs/advance/fsdp_extension.rst | 94 ++ docs/advance/megatron_extension.rst | 25 + docs/advance/placement.rst | 11 + docs/conf.py | 67 ++ docs/examples/config.rst | 339 +++++++ docs/examples/gsm8k_example.rst | 165 +++ docs/examples/ppo_code_architecture.rst | 207 ++++ docs/index.rst | 73 ++ docs/preparation/install.rst | 81 ++ docs/preparation/prepare_data.rst | 126 +++ docs/preparation/reward_function.rst | 46 + docs/requirements-docs.txt | 9 + docs/workers/fsdp_workers.rst | 142 +++ docs/workers/megatron_workers.rst | 200 ++++ docs/workers/ray_trainer.rst | 243 +++++ examples/data_preprocess/full_hh_rlhf.py | 146 +++ examples/data_preprocess/gsm8k.py | 93 ++ examples/data_preprocess/hellaswag.py | 102 ++ examples/data_preprocess/math.py | 89 ++ .../generation/run_deepseek_v2_lite_math.sh | 16 + examples/ppo_trainer/run_deepseek7b_llm.sh | 38 + .../ppo_trainer/run_deepseek_full_hh_rlhf.sh | 41 + .../run_deepseek_math_gsm8k_megatron.sh | 40 + examples/ppo_trainer/run_deepseek_megatron.sh | 31 + examples/ppo_trainer/run_qwen2-7b.sh | 47 + examples/ppo_trainer/run_qwen2-7b_rm.sh | 54 + examples/ppo_trainer/run_qwen2.5-32b.sh | 48 + examples/ray/tutorial.ipynb | 949 ++++++++++++++++++ examples/sft/gsm8k/run_deepseek_6b7.sh | 16 + examples/sft/gsm8k/run_gemma_2b.sh | 18 + examples/sft/gsm8k/run_gemma_7b.sh | 16 + patches/megatron_v4.patch | 568 +++++++++++ requirements.txt | 12 + setup.py | 62 ++ single_controller/__init__.py | 20 + single_controller/base/__init__.py | 16 + single_controller/base/decorator.py | 410 ++++++++ single_controller/base/dp.py | 47 + single_controller/base/megatron/__init__.py | 13 + single_controller/base/megatron/worker.py | 39 + .../base/megatron/worker_group.py | 51 + .../base/register_center/__init__.py | 13 + single_controller/base/register_center/ray.py | 29 + single_controller/base/worker.py | 181 ++++ single_controller/base/worker_group.py | 196 ++++ single_controller/ray/__init__.py | 16 + single_controller/ray/base.py | 455 +++++++++ single_controller/ray/decorator.py | 65 ++ .../ray/dist_data_pass_protocol.py | 23 + single_controller/ray/dp.py | 93 ++ single_controller/ray/megatron.py | 62 ++ single_controller/version/version | 1 + verl/__init__.py | 27 + verl/models/README.md | 35 + verl/models/__init__.py | 13 + verl/models/llama/megatron/__init__.py | 24 + .../megatron/checkpoint_utils/__init__.py | 13 + .../megatron/checkpoint_utils/llama_loader.py | 446 ++++++++ .../megatron/checkpoint_utils/llama_saver.py | 449 +++++++++ verl/models/llama/megatron/layers/__init__.py | 18 + .../megatron/layers/parallel_attention.py | 418 ++++++++ .../llama/megatron/layers/parallel_decoder.py | 146 +++ .../llama/megatron/layers/parallel_linear.py | 74 ++ .../llama/megatron/layers/parallel_mlp.py | 74 ++ .../llama/megatron/layers/parallel_rmsnorm.py | 46 + .../llama/megatron/modeling_llama_megatron.py | 663 ++++++++++++ verl/models/registry.py | 50 + verl/models/weight_loader_registry.py | 23 + verl/protocol.py | 487 +++++++++ verl/third_party/__init__.py | 13 + verl/third_party/vllm/__init__.py | 45 + .../third_party/vllm/vllm_v_0_3_1/__init__.py | 13 + .../vllm/vllm_v_0_3_1/arg_utils.py | 228 +++++ verl/third_party/vllm/vllm_v_0_3_1/config.py | 578 +++++++++++ verl/third_party/vllm/vllm_v_0_3_1/llm.py | 275 +++++ .../vllm/vllm_v_0_3_1/llm_engine_sp.py | 765 ++++++++++++++ .../vllm/vllm_v_0_3_1/model_loader.py | 275 +++++ .../vllm/vllm_v_0_3_1/model_runner.py | 285 ++++++ .../vllm/vllm_v_0_3_1/parallel_state.py | 147 +++ .../vllm/vllm_v_0_3_1/tokenizer.py | 72 ++ .../vllm/vllm_v_0_3_1/weight_loaders.py | 95 ++ verl/third_party/vllm/vllm_v_0_3_1/worker.py | 314 ++++++ .../third_party/vllm/vllm_v_0_4_2/__init__.py | 13 + .../vllm/vllm_v_0_4_2/arg_utils.py | 320 ++++++ verl/third_party/vllm/vllm_v_0_4_2/config.py | 201 ++++ .../vllm_v_0_4_2/dtensor_weight_loaders.py | 269 +++++ .../vllm/vllm_v_0_4_2/hf_weight_loader.py | 91 ++ verl/third_party/vllm/vllm_v_0_4_2/llm.py | 306 ++++++ .../vllm/vllm_v_0_4_2/llm_engine_sp.py | 285 ++++++ .../vllm_v_0_4_2/megatron_weight_loaders.py | 348 +++++++ .../vllm/vllm_v_0_4_2/model_loader.py | 265 +++++ .../vllm/vllm_v_0_4_2/model_runner.py | 281 ++++++ .../vllm/vllm_v_0_4_2/parallel_state.py | 294 ++++++ .../vllm/vllm_v_0_4_2/spmd_gpu_executor.py | 218 ++++ .../vllm/vllm_v_0_4_2/tokenizer.py | 77 ++ verl/third_party/vllm/vllm_v_0_4_2/worker.py | 292 ++++++ .../third_party/vllm/vllm_v_0_5_4/__init__.py | 13 + .../vllm/vllm_v_0_5_4/arg_utils.py | 453 +++++++++ verl/third_party/vllm/vllm_v_0_5_4/config.py | 246 +++++ .../vllm_v_0_5_4/dtensor_weight_loaders.py | 340 +++++++ .../vllm/vllm_v_0_5_4/hf_weight_loader.py | 42 + verl/third_party/vllm/vllm_v_0_5_4/llm.py | 239 +++++ .../vllm/vllm_v_0_5_4/llm_engine_sp.py | 348 +++++++ .../vllm_v_0_5_4/megatron_weight_loaders.py | 307 ++++++ .../vllm/vllm_v_0_5_4/model_loader.py | 302 ++++++ .../vllm/vllm_v_0_5_4/model_runner.py | 150 +++ .../vllm/vllm_v_0_5_4/parallel_state.py | 304 ++++++ .../vllm/vllm_v_0_5_4/spmd_gpu_executor.py | 253 +++++ .../vllm/vllm_v_0_5_4/tokenizer.py | 77 ++ verl/third_party/vllm/vllm_v_0_5_4/worker.py | 323 ++++++ verl/trainer/__init__.py | 13 + verl/trainer/config/evaluation.yaml | 6 + verl/trainer/config/generation.yaml | 35 + verl/trainer/config/ppo_megatron_trainer.yaml | 145 +++ verl/trainer/config/ppo_trainer.yaml | 131 +++ verl/trainer/config/sft_trainer.yaml | 38 + verl/trainer/fsdp_sft_trainer.py | 359 +++++++ verl/trainer/main_eval.py | 69 ++ verl/trainer/main_generation.py | 136 +++ verl/trainer/main_ppo.py | 186 ++++ verl/trainer/ppo/__init__.py | 13 + verl/trainer/ppo/actor/__init__.py | 18 + verl/trainer/ppo/actor/base.py | 66 ++ verl/trainer/ppo/actor/dp_actor.py | 167 +++ verl/trainer/ppo/actor/megatron_actor.py | 368 +++++++ verl/trainer/ppo/core_algos.py | 216 ++++ verl/trainer/ppo/critic/__init__.py | 18 + verl/trainer/ppo/critic/base.py | 40 + verl/trainer/ppo/critic/dp_critic.py | 134 +++ verl/trainer/ppo/critic/megatron_critic.py | 297 ++++++ verl/trainer/ppo/hybrid_engine/__init__.py | 32 + verl/trainer/ppo/hybrid_engine/base.py | 33 + verl/trainer/ppo/hybrid_engine/fsdp_vllm.py | 105 ++ .../ppo/hybrid_engine/megatron_vllm.py | 428 ++++++++ verl/trainer/ppo/ray_trainer.py | 526 ++++++++++ verl/trainer/ppo/reward_model/__init__.py | 15 + verl/trainer/ppo/reward_model/base.py | 45 + .../ppo/reward_model/megatron/__init__.py | 15 + .../ppo/reward_model/megatron/reward_model.py | 275 +++++ verl/trainer/ppo/rollout/__init__.py | 19 + verl/trainer/ppo/rollout/base.py | 37 + verl/trainer/ppo/rollout/hf_rollout.py | 140 +++ verl/trainer/ppo/rollout/megatron/__init__.py | 17 + verl/trainer/ppo/rollout/naive/__init__.py | 15 + .../ppo/rollout/naive/naive_rollout.py | 119 +++ verl/trainer/ppo/rollout/tokenizer.py | 162 +++ .../ppo/rollout/vllm_rollout/__init__.py | 15 + .../ppo/rollout/vllm_rollout/vllm_rollout.py | 216 ++++ verl/trainer/ppo/workers/__init__.py | 13 + verl/trainer/ppo/workers/fsdp_workers.py | 809 +++++++++++++++ verl/trainer/ppo/workers/megatron_workers.py | 728 ++++++++++++++ verl/trainer/runtime_env.yaml | 5 + verl/utils/__init__.py | 13 + verl/utils/config.py | 23 + verl/utils/dataset/README.md | 16 + verl/utils/dataset/__init__.py | 17 + verl/utils/dataset/rl_dataset.py | 180 ++++ verl/utils/dataset/rm_dataset.py | 151 +++ verl/utils/dataset/sft_dataset.py | 160 +++ verl/utils/debug/__init__.py | 15 + verl/utils/debug/performance.py | 30 + verl/utils/debug/trajectory_tracker.py | 108 ++ verl/utils/distributed.py | 28 + verl/utils/fs.py | 88 ++ verl/utils/fsdp_utils.py | 122 +++ verl/utils/hdfs_io.py | 142 +++ verl/utils/import_utils.py | 48 + verl/utils/logger/__init__.py | 13 + verl/utils/logger/aggregate_logger.py | 43 + verl/utils/logging_utils.py | 22 + verl/utils/megatron/__init__.py | 13 + verl/utils/megatron/memory.py | 41 + verl/utils/megatron/optimizer.py | 92 ++ verl/utils/megatron/optimizer_config.py | 129 +++ verl/utils/megatron/pipeline_parallel.py | 51 + verl/utils/megatron/sequence_parallel.py | 54 + verl/utils/megatron/tensor_parallel.py | 185 ++++ verl/utils/megatron_utils.py | 254 +++++ verl/utils/memory_buffer.py | 214 ++++ verl/utils/model.py | 332 ++++++ verl/utils/py_functional.py | 56 ++ verl/utils/ray_utils.py | 43 + verl/utils/rendezvous/__init__.py | 13 + verl/utils/rendezvous/ray_backend.py | 77 ++ verl/utils/reward_score/__init__.py | 13 + verl/utils/reward_score/gsm8k.py | 52 + verl/utils/reward_score/math.py | 227 +++++ verl/utils/torch_dtypes.py | 82 ++ verl/utils/torch_functional.py | 465 +++++++++ verl/utils/tracking.py | 46 + verl/version/version | 1 + 201 files changed, 29407 insertions(+) create mode 100644 .gitignore create mode 100644 .readthedocs.yaml create mode 100644 .style.yapf create mode 100644 LICENSE create mode 100644 Notice.txt create mode 100644 README.md create mode 100644 docs/Makefile create mode 100644 docs/README.md create mode 100644 docs/_static/logo.png create mode 100644 docs/advance/dpo_extension.rst create mode 100644 docs/advance/fsdp_extension.rst create mode 100644 docs/advance/megatron_extension.rst create mode 100644 docs/advance/placement.rst create mode 100644 docs/conf.py create mode 100644 docs/examples/config.rst create mode 100644 docs/examples/gsm8k_example.rst create mode 100644 docs/examples/ppo_code_architecture.rst create mode 100644 docs/index.rst create mode 100644 docs/preparation/install.rst create mode 100644 docs/preparation/prepare_data.rst create mode 100644 docs/preparation/reward_function.rst create mode 100644 docs/requirements-docs.txt create mode 100644 docs/workers/fsdp_workers.rst create mode 100644 docs/workers/megatron_workers.rst create mode 100644 docs/workers/ray_trainer.rst create mode 100644 examples/data_preprocess/full_hh_rlhf.py create mode 100644 examples/data_preprocess/gsm8k.py create mode 100644 examples/data_preprocess/hellaswag.py create mode 100644 examples/data_preprocess/math.py create mode 100644 examples/generation/run_deepseek_v2_lite_math.sh create mode 100644 examples/ppo_trainer/run_deepseek7b_llm.sh create mode 100644 examples/ppo_trainer/run_deepseek_full_hh_rlhf.sh create mode 100644 examples/ppo_trainer/run_deepseek_math_gsm8k_megatron.sh create mode 100644 examples/ppo_trainer/run_deepseek_megatron.sh create mode 100644 examples/ppo_trainer/run_qwen2-7b.sh create mode 100644 examples/ppo_trainer/run_qwen2-7b_rm.sh create mode 100644 examples/ppo_trainer/run_qwen2.5-32b.sh create mode 100644 examples/ray/tutorial.ipynb create mode 100644 examples/sft/gsm8k/run_deepseek_6b7.sh create mode 100644 examples/sft/gsm8k/run_gemma_2b.sh create mode 100644 examples/sft/gsm8k/run_gemma_7b.sh create mode 100644 patches/megatron_v4.patch create mode 100644 requirements.txt create mode 100644 setup.py create mode 100644 single_controller/__init__.py create mode 100644 single_controller/base/__init__.py create mode 100644 single_controller/base/decorator.py create mode 100644 single_controller/base/dp.py create mode 100644 single_controller/base/megatron/__init__.py create mode 100644 single_controller/base/megatron/worker.py create mode 100644 single_controller/base/megatron/worker_group.py create mode 100644 single_controller/base/register_center/__init__.py create mode 100644 single_controller/base/register_center/ray.py create mode 100644 single_controller/base/worker.py create mode 100644 single_controller/base/worker_group.py create mode 100644 single_controller/ray/__init__.py create mode 100644 single_controller/ray/base.py create mode 100644 single_controller/ray/decorator.py create mode 100644 single_controller/ray/dist_data_pass_protocol.py create mode 100644 single_controller/ray/dp.py create mode 100644 single_controller/ray/megatron.py create mode 100644 single_controller/version/version create mode 100644 verl/__init__.py create mode 100644 verl/models/README.md create mode 100644 verl/models/__init__.py create mode 100644 verl/models/llama/megatron/__init__.py create mode 100644 verl/models/llama/megatron/checkpoint_utils/__init__.py create mode 100644 verl/models/llama/megatron/checkpoint_utils/llama_loader.py create mode 100644 verl/models/llama/megatron/checkpoint_utils/llama_saver.py create mode 100644 verl/models/llama/megatron/layers/__init__.py create mode 100644 verl/models/llama/megatron/layers/parallel_attention.py create mode 100644 verl/models/llama/megatron/layers/parallel_decoder.py create mode 100644 verl/models/llama/megatron/layers/parallel_linear.py create mode 100644 verl/models/llama/megatron/layers/parallel_mlp.py create mode 100644 verl/models/llama/megatron/layers/parallel_rmsnorm.py create mode 100644 verl/models/llama/megatron/modeling_llama_megatron.py create mode 100644 verl/models/registry.py create mode 100644 verl/models/weight_loader_registry.py create mode 100644 verl/protocol.py create mode 100644 verl/third_party/__init__.py create mode 100644 verl/third_party/vllm/__init__.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/__init__.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/arg_utils.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/config.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/llm.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/llm_engine_sp.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/model_loader.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/model_runner.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/parallel_state.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/tokenizer.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/weight_loaders.py create mode 100644 verl/third_party/vllm/vllm_v_0_3_1/worker.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/__init__.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/arg_utils.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/config.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/dtensor_weight_loaders.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/hf_weight_loader.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/llm.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/llm_engine_sp.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/megatron_weight_loaders.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/model_loader.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/model_runner.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/parallel_state.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/spmd_gpu_executor.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/tokenizer.py create mode 100644 verl/third_party/vllm/vllm_v_0_4_2/worker.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/__init__.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/arg_utils.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/config.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/dtensor_weight_loaders.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/hf_weight_loader.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/llm.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/llm_engine_sp.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/megatron_weight_loaders.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/model_loader.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/model_runner.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/parallel_state.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/spmd_gpu_executor.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/tokenizer.py create mode 100644 verl/third_party/vllm/vllm_v_0_5_4/worker.py create mode 100644 verl/trainer/__init__.py create mode 100644 verl/trainer/config/evaluation.yaml create mode 100644 verl/trainer/config/generation.yaml create mode 100644 verl/trainer/config/ppo_megatron_trainer.yaml create mode 100644 verl/trainer/config/ppo_trainer.yaml create mode 100644 verl/trainer/config/sft_trainer.yaml create mode 100644 verl/trainer/fsdp_sft_trainer.py create mode 100644 verl/trainer/main_eval.py create mode 100644 verl/trainer/main_generation.py create mode 100644 verl/trainer/main_ppo.py create mode 100644 verl/trainer/ppo/__init__.py create mode 100644 verl/trainer/ppo/actor/__init__.py create mode 100644 verl/trainer/ppo/actor/base.py create mode 100644 verl/trainer/ppo/actor/dp_actor.py create mode 100644 verl/trainer/ppo/actor/megatron_actor.py create mode 100644 verl/trainer/ppo/core_algos.py create mode 100644 verl/trainer/ppo/critic/__init__.py create mode 100644 verl/trainer/ppo/critic/base.py create mode 100644 verl/trainer/ppo/critic/dp_critic.py create mode 100644 verl/trainer/ppo/critic/megatron_critic.py create mode 100644 verl/trainer/ppo/hybrid_engine/__init__.py create mode 100644 verl/trainer/ppo/hybrid_engine/base.py create mode 100644 verl/trainer/ppo/hybrid_engine/fsdp_vllm.py create mode 100644 verl/trainer/ppo/hybrid_engine/megatron_vllm.py create mode 100644 verl/trainer/ppo/ray_trainer.py create mode 100644 verl/trainer/ppo/reward_model/__init__.py create mode 100644 verl/trainer/ppo/reward_model/base.py create mode 100644 verl/trainer/ppo/reward_model/megatron/__init__.py create mode 100644 verl/trainer/ppo/reward_model/megatron/reward_model.py create mode 100644 verl/trainer/ppo/rollout/__init__.py create mode 100644 verl/trainer/ppo/rollout/base.py create mode 100644 verl/trainer/ppo/rollout/hf_rollout.py create mode 100644 verl/trainer/ppo/rollout/megatron/__init__.py create mode 100644 verl/trainer/ppo/rollout/naive/__init__.py create mode 100644 verl/trainer/ppo/rollout/naive/naive_rollout.py create mode 100644 verl/trainer/ppo/rollout/tokenizer.py create mode 100644 verl/trainer/ppo/rollout/vllm_rollout/__init__.py create mode 100644 verl/trainer/ppo/rollout/vllm_rollout/vllm_rollout.py create mode 100644 verl/trainer/ppo/workers/__init__.py create mode 100644 verl/trainer/ppo/workers/fsdp_workers.py create mode 100644 verl/trainer/ppo/workers/megatron_workers.py create mode 100644 verl/trainer/runtime_env.yaml create mode 100644 verl/utils/__init__.py create mode 100644 verl/utils/config.py create mode 100644 verl/utils/dataset/README.md create mode 100644 verl/utils/dataset/__init__.py create mode 100644 verl/utils/dataset/rl_dataset.py create mode 100644 verl/utils/dataset/rm_dataset.py create mode 100644 verl/utils/dataset/sft_dataset.py create mode 100644 verl/utils/debug/__init__.py create mode 100644 verl/utils/debug/performance.py create mode 100644 verl/utils/debug/trajectory_tracker.py create mode 100644 verl/utils/distributed.py create mode 100644 verl/utils/fs.py create mode 100644 verl/utils/fsdp_utils.py create mode 100644 verl/utils/hdfs_io.py create mode 100644 verl/utils/import_utils.py create mode 100644 verl/utils/logger/__init__.py create mode 100644 verl/utils/logger/aggregate_logger.py create mode 100644 verl/utils/logging_utils.py create mode 100644 verl/utils/megatron/__init__.py create mode 100644 verl/utils/megatron/memory.py create mode 100644 verl/utils/megatron/optimizer.py create mode 100644 verl/utils/megatron/optimizer_config.py create mode 100644 verl/utils/megatron/pipeline_parallel.py create mode 100644 verl/utils/megatron/sequence_parallel.py create mode 100644 verl/utils/megatron/tensor_parallel.py create mode 100644 verl/utils/megatron_utils.py create mode 100644 verl/utils/memory_buffer.py create mode 100644 verl/utils/model.py create mode 100644 verl/utils/py_functional.py create mode 100644 verl/utils/ray_utils.py create mode 100644 verl/utils/rendezvous/__init__.py create mode 100644 verl/utils/rendezvous/ray_backend.py create mode 100644 verl/utils/reward_score/__init__.py create mode 100644 verl/utils/reward_score/gsm8k.py create mode 100644 verl/utils/reward_score/math.py create mode 100644 verl/utils/torch_dtypes.py create mode 100644 verl/utils/torch_functional.py create mode 100644 verl/utils/tracking.py create mode 100644 verl/version/version diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..54b93e43a --- /dev/null +++ b/.gitignore @@ -0,0 +1,110 @@ +**/*.pt +**/checkpoints +**/wget-log +**/_build/ +**/*.ckpt +**/outputs +**/*.tar.gz +**/playground +**/wandb + +# 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/ +*.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/ + +# 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/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +# vscode +.vscode + +# Mac +.DS_Store + +# output logs +tests/e2e/toy_examples/deepspeed/synchronous/output.txt diff --git a/.readthedocs.yaml b/.readthedocs.yaml new file mode 100644 index 000000000..2df37965f --- /dev/null +++ b/.readthedocs.yaml @@ -0,0 +1,16 @@ +# 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.8" + +sphinx: + configuration: docs/conf.py + +python: + install: + - requirements: docs/requirements-docs.txt \ No newline at end of file diff --git a/.style.yapf b/.style.yapf new file mode 100644 index 000000000..a09a27c31 --- /dev/null +++ b/.style.yapf @@ -0,0 +1,5 @@ +[style] +based_on_style = google +column_limit = 120 +indent_width = 4 +split_arguments_when_comma_terminated: true \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 000000000..d64569567 --- /dev/null +++ b/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/Notice.txt b/Notice.txt new file mode 100644 index 000000000..ade439da5 --- /dev/null +++ b/Notice.txt @@ -0,0 +1 @@ +Copyright 2023-2024 Bytedance Ltd. and/or its affiliates \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 000000000..71be0b7f5 --- /dev/null +++ b/README.md @@ -0,0 +1,168 @@ +
+ +
+ +

veRL: Volcano Engine Reinforcement Learning for LLM

+ +veRL (HybridFlow) is a flexible, efficient and industrial-level RL(HF) training framework designed for large language models (LLMs). veRL is the open-source version of [HybridFlow](https://arxiv.org/abs/2409.19256v2) paper. + +veRL is flexible and easy to use with: + +- **Easy to support diverse RL(HF) 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 API design**: Decouples computation and data dependencies, enabling seamless integration with existing LLM frameworks, such as PyTorch FSDP, Megatron-LM and vLLM. Moreover, users can easily extend to other LLM training and inference frameworks. + +- **Flexible device mapping**: Supports various placement of models onto different sets of GPUs for efficient resource utilization and scalability across different cluster sizes. + +- Readily integration with popular Hugging Face 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. + + +

+| Documentation | Paper | + +

+ + + +## Installation + +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. + +```bash +# install verl together with some lightweight dependencies in setup.py +git clone https://github.com/volcengine/verl.git +cd verl +pip3 install -e . +``` + +You can also install veRL using `pip3 install` + +```bash +# directly install from pypi +pip3 install verl +``` + +### Dependencies + +veRL requires Python >= 3.9 and CUDA >= 12.1. + +veRL support various backend, we currently release FSDP and Megatron-LM for actor training and vLLM for rollout generation. + +To install the dependencies, we recommend using conda: + +```bash +conda create -n verl python==3.9 +conda activate verl +``` + +The following dependencies are required for all backends. + +```bash +# install torch [or you can skip this step and let vllm to install the correct version for you] +pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu121 + +# install vllm +pip3 install vllm==0.5.4 +pip3 install ray==2.10 # other version may have bug + +# flash attention 2 +pip3 install flash-attn --no-build-isolation +``` + +**FSDP** + +We recommend using FSDP backend to investigate, research and prototype different models, datasets and RL algorithms. + +The pros, cons and extension guide for using FSDP backend can be found in fsdp.md + +**Megatron-LM** + +For users who pursue better scalability, we recommend using Megatron-LM backend. Please install the above dependencies first. + +Currently, we support Megatron-LM@core_v0.4.0 and we fix some internal issues of Megatron-LM. Here's the additional installation guide. + +The pros, cons and extension guide for using Megatron-LM backend can be found in megatron.md. + +```bash +# FOR Megatron-LM Backend +# apex +pip3 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 + +# transformer engine +pip3 install git+https://github.com/NVIDIA/TransformerEngine.git@v1.7 + +# megatron core v0.4.0 +cd .. +git clone -b core_v0.4.0 https://github.com/NVIDIA/Megatron-LM.git +cd Megatron-LM +cp ../verl/patches/megatron_v4.patch . +git apply megatron_v4.patch +pip3 install -e . +export PYTHONPATH=$PYTHONPATH:$(pwd) +``` + +## Getting Started +Visit our [documentation](https://verl-doc.readthedocs.io/en/latest/index.html) to learn more. + +**Running an PPO example should follow:** +- Preparation + - [Installation](https://verl-doc.readthedocs.io/en/latest/preparation/install.html) + - [Prepare Data (Parquet) for Post-Training](https://verl-doc.readthedocs.io/en/latest/preparation/prepare_data.html) + - [Implement Reward Function for Dataset](https://verl-doc.readthedocs.io/en/latest/preparation/reward_function.html) +- PPO Example (Run an example) + - [PPO Example Architecture](https://verl-doc.readthedocs.io/en/latest/examples/ppo_code_architecture.html) + - [Config Explanation](https://verl-doc.readthedocs.io/en/latest/examples/config.html) + - [Run GSM8K Example](https://verl-doc.readthedocs.io/en/latest/examples/gsm8k_example.html) + +**For code explanation and advance usage (extension):** +- PPO Trainer and Workers + - [PPO Ray Trainer](https://verl-doc.readthedocs.io/en/latest/workers/ray_trainer.html) + - [PyTorch FSDP Backend](https://verl-doc.readthedocs.io/en/latest/workers/fsdp_workers.html) + - [Megatron-LM Backend](https://verl-doc.readthedocs.io/en/latest/index.html) +- Advance Usage and Extension + - [Ray API Design Tutorial](https://verl-doc.readthedocs.io/en/latest/advance/placement.html) + - [Extend to other RL(HF) algorithms](https://verl-doc.readthedocs.io/en/latest/advance/dpo_extension.html) + - [Add models to FSDP backend](https://verl-doc.readthedocs.io/en/latest/advance/fsdp_extension.html) + - [Add models to Megatron-LM backend](https://verl-doc.readthedocs.io/en/latest/advance/megatron_extension.html) + + +## Contribution +### Code formatting +We use yapf (Google style) to enforce strict code formatting when reviewing MRs. To reformat you code locally, make sure you installed `yapf` +```bash +pip3 install yapf +``` +Then, make sure you are at top level of verl repo and run +```bash +yapf -ir -vv --style ./.style.yapf verl single_controller examples +``` + + + +## Citation + +```tex +@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} +} + +@inproceedings{zhang2024framework, + title={A Framework for Training Large Language Models for Code Generation via Proximal Policy Optimization}, + author={Zhang, Chi and Sheng, Guangming and Liu, Siyao and Li, Jiahao and Feng, Ziyuan and Liu, Zherui and Liu, Xin and Jia, Xiaoying and Peng, Yanghua and Lin, Haibin and Wu, Chuan}, + booktitle={In NL2Code Workshop of ACM KDD}, + year={2024} +} +``` + diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 000000000..8bda904a9 --- /dev/null +++ b/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/docs/README.md b/docs/README.md new file mode 100644 index 000000000..ee325d86a --- /dev/null +++ b/docs/README.md @@ -0,0 +1,19 @@ +# veRL documents + +## Build the docs + +```bash +# Install dependencies. +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 open localhost:8000. \ No newline at end of file diff --git a/docs/_static/logo.png b/docs/_static/logo.png new file mode 100644 index 0000000000000000000000000000000000000000..6a3b61308d73eb72071eaec8c95544ab36cd3970 GIT binary patch literal 84701 zcmZ^~1ymeOvp>8LWP!!q-EDFA;4Z=43GVI|Jh;0%3GVLh?oMz>kRTt=`(FF*`QJTf zc4|txx@LaUJzd=$p`;**2#*I3000oBr9P_w0FVU#96*>4PUoAu&&LVMLPTB!0QeDy z@L~l0aZO?(r6Laicu@fW0U-du^9L*71ORXa0{~}+003_q0D$d~)uzn6#(jAIsky+#~A?lln3$Ont71_DTO4+ z`}E)RKZ-T_?WiA>Kd@5MbkUTT<2A9jWim3gH#TGPuyy!H0l@FU`$5{8xfqdn*xJ}R z^Lhx7{Y!)QgZ_ujOh)oA6&Gs(GEI3U5;1!xGZIcFFcX+e5T1mDgx|^3oLA+u#DAE7 zTnUg_y0|#-GBdloyED17G1)s=FthOR@Gyf}nORvGKQtJfJ?&hKJQ(eq$^Tu*f0pyv z%-O`r%E86T-j3v-a*d4bU0npo$o_HkU%!9X)5XgCzdhMG|3_FK0W$w1VP;_hGylKL z%sj0A53_$H|2F$qT>o~*|4(7OV)nKUPG-)|AJr0M;s2L|{~zzatM_kx6*Fgh8`poL z_+n?}BFOq5CjX25zpWIVtjs=c%RgrS<>i0!{&03xQ6msv;@4q$9b-_#E^tFM#12R6Io<4VfV8OI%Sv z095Wbv_T89@&gi{;PbvCkNt`4_rDcw=3ZWX*{y5>^K8yvo9%wPXlP_V(`5^~o~b34 zsX}1{wtdcHo_z>ZINdPz@bDnVa3Hj@%UPpf)k2Gim|I{MjF*b;e^q#NdsLrpCb228 z4-%8Qa_`-Sdl%Ea<9e+--Upi8xA1(cBZjLIZ9((Htw|->Bh#H8zS9URdS9(6scF8} z$tKGKF5p7;_7+Y>mJTXIogqVj;eQ!oZKG!ON-DL*uqgWjcD3PfVL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3`hZg|G=ZliIhS~or$&RyC`QI|tAgsf0sJBXJL_uOlfAYqE z@K(8Wp_*`o1r@o>V9`q9f>jeE>xzXxh%NI59u4t@6!y<10N*8e2vFQbXs}L7T0xMR z(Q+)j-KYnU97vecvs*873Ja$lwA*v{BRmo*ZzoBQi-UE|phxB=Wqm zNS#NfyTU~N(>8V@B6?A=9cDge#f>POcuzmkKoeCQX;9{JbyVBSTAk!Z^ajV>-LSil zaKVB^iOw5?SVtdm!G>YX;xfDwbDgrB!s}N|&Gm$*u;moZ&Rs?NFMcv34I48 z7)-$NV0DH(@ll)-(k`R=Y6DQ_K}I}4*i<{$yqj^8!`CTDlV?wl_t_xTIGfSnFi$aFkRcX zE)buko(#r(QHJJjG2lOM$H*Ed6auvOUcn1)$^&nPMX6@WmYjsUk3-GlSX zIR64phV98%Ii@CI#1JrtW5gQF%pKzK=hsE-k%kZf52QDA|nDu9G}O6Op3t z5FX7s0UXyFoQmIo=bP1P_2QYC8GBR`0%-AxN1)j?H1Pl#0F#XX<`8Z)Jrn1tIFG=& z9;aP50AbvZ790+eJc#p$IJw#LLvT2_0n`pdzz`@vzy{6&P%O(3C_?}a$T~cJIRh8Y zb3pSboV(!c=sN0%smw6T?>++Rabyj}ocnP80O!Bp{4gAhtqAQf1Pp-|5Xdg27Kn|+ z5HJMt2yg`i-~2cYUx0iLW~0ZVbWfbgw?Fc0o@) zRxpOJoy??>LBPhmk!dP3bW{+46LK(CL0$$2Ya=J5018FmVWQKL{%QOq@@} z`DmO6;^a3$_(;aCO@Pq&ptBJ=9688Xz(*o{74=q}-@|z&&VRLi7eK)dLtp?1sBXVPz}}5z~q+gtg9O`YFw?j zACPQ8h{qu{d?I}6D~Nv`js+H#>@Wljfnh7Im)Wz&D1SSFjI3#RAuwhL{C_-K5NcX~510S|002ovPDHLkV1ks06^j4> literal 0 HcmV?d00001 diff --git a/docs/advance/dpo_extension.rst b/docs/advance/dpo_extension.rst new file mode 100644 index 000000000..fb7754c58 --- /dev/null +++ b/docs/advance/dpo_extension.rst @@ -0,0 +1,271 @@ +Extend to other RL(HF) algorithms +================================= + +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 single_controller.base import Worker + from 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/trainer/ppo/rollout/vllm_rollout/vllm_rollout.py and we won’t +go into details here. + +**ReferencePolicy inference** + +API: compute reference log probability + +.. code:: python + + from 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 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 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 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/docs/advance/fsdp_extension.rst b/docs/advance/fsdp_extension.rst new file mode 100644 index 000000000..3441aa5a9 --- /dev/null +++ b/docs/advance/fsdp_extension.rst @@ -0,0 +1,94 @@ + +Add models to FSDP backend +=========================== + +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 `_. +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 `_.: + +- ``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) + 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/docs/advance/megatron_extension.rst b/docs/advance/megatron_extension.rst new file mode 100644 index 000000000..de2d25642 --- /dev/null +++ b/docs/advance/megatron_extension.rst @@ -0,0 +1,25 @@ +Add models to Megatron-LM backend +=========== + +Model +----------- + +The most challenging aspect to use Megatron-LM backend is implementing +the models for training. Currently, we implement Llama model that +support data parallelism, tensor parallelism, pipeline parallelism (also +vPP) and sequence parallelism. We also implement remove padding on Llama +model, which can be found in `modeling_llama_megatron.py `_. + +To support other model, users are required to implement: + +1. Implemnt a model similar to ``modeling_llama_megatron.py`` that satisfy the + parallelism requirements of Megatron-LM. Then register your model in + the `registry.py `_. +2. Checkpoint utils that can load full checkpoint (e.g. huggingface + checkpoint) to partitioned models during the runtime. Then register + your loader to ``weight_loader_registry`` in `weight_loader_registry.py `_. +3. Weight loader that synchronize the weight from Megatron to rollout + (vLLM) model. Note that both the actor model and rollout model are + partitioned during runtime. So, it’s advisable to map the model name + in actor model implementation. Otherwise, you may need an additional + name mapping and even weight transformation. \ No newline at end of file diff --git a/docs/advance/placement.rst b/docs/advance/placement.rst new file mode 100644 index 000000000..a98caa116 --- /dev/null +++ b/docs/advance/placement.rst @@ -0,0 +1,11 @@ +Ray API Design Tutorial +======================================= + +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/docs/conf.py b/docs/conf.py new file mode 100644 index 000000000..e22a4199f --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,67 @@ +# 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 = u'veRL' +# pylint: disable=W0622 +copyright = u'2024 ByteDance Seed Foundation MLSys Team' +author = u'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 = ['recommonmark', + 'sphinx.ext.autosectionlabel', +] + +# The suffix(es) of source filenames. +# You can specify multiple suffix as a list of string: +source_suffix = ['.rst', 'rest', '.md'] + +# 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 = u'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'] \ No newline at end of file diff --git a/docs/examples/config.rst b/docs/examples/config.rst new file mode 100644 index 000000000..0051f6578 --- /dev/null +++ b/docs/examples/config.rst @@ -0,0 +1,339 @@ +Config Explaination +=================== + +ppo_trainer.yaml for FSDP Backend +--------------------------------- + +Data +~~~~ + +.. code:: yaml + + data: + tokenizer: null + train_files: ~/data/rlhf/gsm8k/train.parquet + val_files: ~/data/rlhf/gsm8k/test.parquet + prompt_key: prompt + max_prompt_length: 512 + max_response_length: 512 + train_batch_size: 1024 + val_batch_size: 1312 + return_raw_input_ids: False # This should be set to true when the tokenizer between policy and rm differs + return_raw_chat: False + +- ``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.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.val_batch_size``: Batch size sampled for one validation + iteration. +- ``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``: +- ``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. + +V1: + +- data.prompt_id_key: The field in the dataset where the prompt_id is + located +- data.max_prompt_id_length: In data processing, the prompt_id will be + tokenized using the tokenizer and packaged with the prompt. This + specifies the maximum length of the tokenized prompt_id. **An error + will be reported if it’s not long enough** + +Actor/Rollout/Reference Policy +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. code:: yaml + + actor_rollout_ref: + hybrid_engine: True + model: + path: ~/models/deepseek-llm-7b-chat + external_lib: null + override_config: {} + enable_gradient_checkpointing: False + actor: + strategy: fsdp # This is for backward-compatibility + ppo_mini_batch_size: 256 + ppo_micro_batch_size: 64 + grad_clip: 1.0 + clip_ratio: 0.2 + entropy_coeff: 0.001 + ppo_epochs: 1 + shuffle: True + optim: + lr: 1e-6 + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: null # only useful for warmup with cosine + warmup_style: 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 + grad_offload: False + optimizer_offload: False + ref: + fsdp_config: + param_offload: False + wrap_policy: + # transformer_layer_cls_to_wrap: None + min_num_params: 0 + log_prob_micro_batch_size: 128 + rollout: + name: vllm + temperature: 1.0 + top_k: -1 # 0 for hf rollout, -1 for vllm rollout + top_p: 1 + 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 # or dummy_hf or dummy_megatron + tensor_model_parallel_size: 2 + max_num_batched_tokens: 8192 + max_num_seqs: 1024 + log_prob_micro_batch_size: 128 + # for vllm and hf rollout + do_sample: True + +**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, mainly dropout +- ``actor_rollout_ref.model.enable_gradient_checkpointing``: Whether to + enable gradient checkpointing for the actor + +**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 + +- ``actor_rollout_ref.actor.ppo_micro_batch_size``: Similar to gradient + accumulation, the micro_batch_size for one forward pass, trading speed + for GPU memory + +- ``actor_rollout_ref.actor.grad_clip``: Gradient clipping for actor + updates + +- ``actor_rollout_ref.actor.clip_ratio``: PPO clip ratio + +- ``actor_rollout_ref.actor.entropy_coeff``: The weight of entropy when + calculating PPO loss + +- ``actor_rollout_ref.actor.ppo_epochs``: Number of epochs for PPO + updates on one set of sampled data + +- ``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. + +**Reference Model** + +- ``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``: The batch size + for one forward pass in the computation of ``ref_log_prob``. + +**Rollout Model** + +- ``actor_rollout_ref.rollout.name``: hf/vllm. We use vLLM by default + because it’s much efficient and our hybrid engine is implemented with + vLLM. + +- 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``. + +- ``dtype``: Rollout model parameters type. This should be align with + the actor model parameter type in FSDP/Megatron backend. + +- ``gpu_memory_utilization``: The proportion of the remaining GPU memory + allocated for kv cache after other models have initialized when using + vllm. + +- ``tensor_model_parallel_size``: TP size for rollout. Only effective + for vllm. + +- ``log_prob_micro_batch_size``: Micro_batch_size (The batch size for + one forward pass) for recalculating log_prob. + +- ``do_sample``: Whether to sample. If set to False, the rollout model + will perform greedy sampling. We disable ``do_sample`` during + validation. + +- ``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, 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. + +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} + fsdp_config: + min_num_params: 0 + param_offload: False + micro_batch_size: 64 + max_length: null + +- ``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. + +Algorithm +~~~~~~~~~ + +.. code:: yaml + + algorithm: + gamma: 1.0 + lam: 1.0 + adv_estimator: gae + kl_penalty: kl # how to estimate kl divergence + kl_ctrl: + type: fixed + kl_coef: 0.005 + +- ``gemma``: discount factor +- ``lam``: Trade-off between bias and variance in the GAE estimator +- ``adv_estimator``: gae. Currently only supports gae, will support GRPO + in the future +- ``kl_penalty``\ :Support ``kl``, ``abs``, ``mse`` and ``full``.How to + calculate the kl divergence between actor and reference policy. For + specific options, refer to `core_algos.py `_ . + +Trainer +~~~~~~~ + +.. code:: yaml + + trainer: + total_epochs: 30 + project_name: verl_examples + experiment_name: gsm8k + logger: ['console', 'tracking'] + nnodes: 1 + n_gpus_per_node: 8 + save_freq: -1 + test_freq: 2 + critic_warmup: 0 + default_hdfs_dir: ~/experiments/gsm8k/ppo/${trainer.experiment_name} # hdfs checkpoint path + default_local_dir: checkpoints/${trainer.project_name}/${trainer.experiment_name} # local checkpoint path + +- ``trainer.total_epochs``: Number of epochs in training. +- ``trainer.project_name``: For wandb +- ``trainer.experiment_name``: For wandb +- ``trainer.logger``: Support console and tracking. For tracking, we + will initialize a wandb +- ``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.test_freq``: The validation frequency (by iteration). +- ``trainer.critic_warmup``: The number of iteration to train the critic + model before actual policy learning. diff --git a/docs/examples/gsm8k_example.rst b/docs/examples/gsm8k_example.rst new file mode 100644 index 000000000..90b61e06f --- /dev/null +++ b/docs/examples/gsm8k_example.rst @@ -0,0 +1,165 @@ +GSM8K Example +============= + +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/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_dir ~/data/gsm8k + +Step 2: Download Model +---------------------- + +There’re three ways to prepare the model checkpoints for post-training: + +- Download the required models from hugging face + +.. code:: bash + + huggingface-cli download deepseek-ai/deepseek-math-7b-instruct --local-dir ~/models/deepseek-math-7b-instruct --local-dir-use-symlinks False + +- 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. + +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 +`fsdp_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.fsdp_sft_trainer \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.prompt_key=question \ + data.response_key=answer \ + data.micro_batch_size=8 \ + model.partial_pretrain=deepseek-ai/deepseek-coder-6.7b-instruct \ + trainer.default_hdfs_dir=hdfs://user/verl/experiments/gsm8k/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','tracking'] + +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 explaination 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_deepseek7b_llm.sh + +The script of run_deepseek7b_llm.sh + +.. code:: bash + + set -x + + python3 -m verl.trainer.main_ppo \ + data.train_files=~/data/rlhf/gsm8k/train.parquet \ + data.val_files=~/data/rlhf/gsm8k/test.parquet \ + data.train_batch_size=1024 \ + data.val_batch_size=1312 \ + data.max_prompt_length=512 \ + data.max_response_length=512 \ + actor_rollout_ref.model.path=~/models/deepseek-llm-7b-chat \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=64 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.grad_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.micro_batch_size=256 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=128 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=128 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.path=~/models/deepseek-llm-7b-chat \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=64 \ + critic.model.fsdp_config.param_offload=False \ + critic.model.fsdp_config.grad_offload=False \ + critic.model.fsdp_config.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + 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.total_epochs=15 diff --git a/docs/examples/ppo_code_architecture.rst b/docs/examples/ppo_code_architecture.rst new file mode 100644 index 000000000..bd247a2bc --- /dev/null +++ b/docs/examples/ppo_code_architecture.rst @@ -0,0 +1,207 @@ +PPO Example Architecture +======================== + +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, Hellasage, 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 +--------------------- + +.. code:: python + + if config.actor_rollout_ref.actor.strategy == 'fsdp': # for FSDP backend + assert config.actor_rollout_ref.actor.strategy == config.critic.strategy + from verl.trainer.ppo.workers.fsdp_workers import ActorRolloutRefWorker, CriticWorker + from single_controller.ray import RayWorkerGroup + ray_worker_group_cls = RayWorkerGroup + + elif config.actor_rollout_ref.actor.strategy == 'megatron': # for Megatron backend + assert config.actor_rollout_ref.actor.strategy == config.critic.strategy + from verl.trainer.ppo.workers.megatron_workers import ActorRolloutRefWorker, CriticWorker + from single_controller.ray.megatron import NVMegatronRayWorkerGroup + ray_worker_group_cls = NVMegatronRayWorkerGroup # Ray worker class for Megatron-LM + + else: + raise NotImplementedError + + from verl.trainer.ppo.ray_trainer import ResourcePoolManager, Role + + role_worker_mapping = { + Role.ActorRollout: ActorRolloutRefWorker, + Role.Critic: CriticWorker, + 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 +- We also pre-implemented workers for ``Actor``, ``Rollout``, + ``Critic``, ``Reward Model`` and ``Reference model`` on two different + backend: PyTorch FSDP + and Megatron-LM. + See `FSDP Workers `_ + and `Megatron-LM Workers `_ + 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.trainer.ppo.workers.fsdp_workers import RewardModelWorker + role_worker_mapping[Role.RewardModel] = RewardModelWorker + 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. + + - Note that the pre-defined ``RewardModelWorker`` only supports models + with the structure of huggingface + ``AutoModelForSequenceClassification``. If it’s not this model, you + need to define your own RewardModelWorker in `FSDP Workers `_ + and `Megatron-LM Workers `_. + +- 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/docs/index.rst b/docs/index.rst new file mode 100644 index 000000000..0cadb743b --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,73 @@ +Welcome to veRL/HybridFlow's documentation! +================================================ + +veRL (HybridFlow) is a flexible, efficient and industrial-level RL(HF) training framework designed for large language models (LLMs) Post-Training. + +veRL is flexible and easy to use with: + +- **Easy to support diverse RL(HF) 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 API design**: Decouples computation and data dependencies, enabling seamless integration with existing LLM frameworks, such as PyTorch FSDP, Megatron-LM and vLLM. Moreover, users can easily extend to other LLM training and inference frameworks. + +- **Flexible device mapping**: Supports various placement of models onto different sets of GPUs for efficient resource utilization and scalability across different cluster sizes. + +- Readily integration with popular Hugging Face 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: 5 + :caption: Preparation + :titlesonly: + :numbered: + + preparation/install + preparation/prepare_data + preparation/reward_function + +.. toctree:: + :maxdepth: 2 + :caption: PPO Example + :titlesonly: + :numbered: + + examples/ppo_code_architecture + examples/config + examples/gsm8k_example + +.. toctree:: + :maxdepth: 1 + :caption: PPO Trainer and Workers + + workers/ray_trainer + workers/fsdp_workers + workers/megatron_workers + +.. toctree:: + :maxdepth: 1 + :caption: Advance Usage and Extension + + advance/placement + advance/dpo_extension + advance/fsdp_extension + advance/megatron_extension + + +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 `_ . + +.. and check out our +.. :doc:`contribution guidelines `. diff --git a/docs/preparation/install.rst b/docs/preparation/install.rst new file mode 100644 index 000000000..1c623c162 --- /dev/null +++ b/docs/preparation/install.rst @@ -0,0 +1,81 @@ +Installation +============ + +To install the veRL, we recommend using conda: + +.. code:: bash + + conda create -n verl python==3.9 + conda activate 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 + + # install verl together with some lightweight dependencies in setup.py + git clone https://github.com/volcengine/verl.git + cd verl + pip3 install -e . + +You can also install veRL using ``pip3 install`` + +.. code:: bash + + # directly install from pypi + pip3 install verl + +Dependencies +------------ + +veRL requires Python >= 3.9 and CUDA >= 12.1. + +veRL support various backend, we currently release FSDP and Megatron-LM +for actor training and vLLM for rollout generation. + +The following dependencies are required for all backends, PyTorch FSDP and Megatron-LM. + +The pros, cons and extension guide for using PyTorch FSDP backend can be +found in :doc:`FSDP Workers<../workers/fsdp_workers>`. + +.. code:: bash + + # install torch [or you can skip this step and let vllm to install the correct version for you] + pip install torch==2.4.0 torchvision==0.19.0 torchaudio==2.4.0 --index-url https://download.pytorch.org/whl/cu121 + + # install vllm + pip3 install vllm==0.5.4 + pip3 install ray==2.10 # other version may have bug + + # flash attention 2 + pip3 install flash-attn --no-build-isolation + +For users who pursue better scalability, we recommend using Megatron-LM +backend. Please install the above dependencies first. + +Currently, we support Megatron-LM\@core_v0.4.0 and we fix some internal +issues of Megatron-LM. Here’s the additional installation guide. + +The pros, cons and extension guide for using Megatron-LM backend can be +found in :doc:`Megatron-LM Workers<../workers/megatron_workers>`. + +.. code:: bash + + # FOR Megatron-LM Backend + # apex + pip3 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 + + # transformer engine + pip3 install git+https://github.com/NVIDIA/TransformerEngine.git@v1.7 + + # megatron core v0.4.0 + cd .. + git clone -b core_v0.4.0 https://github.com/NVIDIA/Megatron-LM.git + cd Megatron-LM + cp ../verl/patches/megatron_v4.patch . + git apply megatron_v4.patch + pip3 install -e . + export PYTHONPATH=$PYTHONPATH:$(pwd) diff --git a/docs/preparation/prepare_data.rst b/docs/preparation/prepare_data.rst new file mode 100644 index 000000000..ca2111c8c --- /dev/null +++ b/docs/preparation/prepare_data.rst @@ -0,0 +1,126 @@ +Prepare Data (Parquet) for Post-Training +======================================== + +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='hdfs://haruna/home/byte_data_seed/lf_lq/user/zhangchi.usc1992/data/rlhf') + + 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 ``RewardModule`` +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/docs/preparation/reward_function.rst b/docs/preparation/reward_function.rst new file mode 100644 index 000000000..b58917bcd --- /dev/null +++ b/docs/preparation/reward_function.rst @@ -0,0 +1,46 @@ +Implment Reward Function for Dataset +======================= + +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 `_. + +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 utilze 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 +---------------- +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 `_. diff --git a/docs/requirements-docs.txt b/docs/requirements-docs.txt new file mode 100644 index 000000000..439f85ef8 --- /dev/null +++ b/docs/requirements-docs.txt @@ -0,0 +1,9 @@ +# markdown suport +recommonmark +# markdown table suport +sphinx-markdown-tables + +# theme default rtd + +# crate-docs-theme +sphinx-rtd-theme \ No newline at end of file diff --git a/docs/workers/fsdp_workers.rst b/docs/workers/fsdp_workers.rst new file mode 100644 index 000000000..e54860f0f --- /dev/null +++ b/docs/workers/fsdp_workers.rst @@ -0,0 +1,142 @@ +PyTorch FSDP Backend +============ + +We support PyTorch FSDP Backend by implementing various workers for +actor, critic, reference, rollout and reward models. We also implement +the ``FSDPVLLMShardingManager`` that reshard weight between FSDP and +vLLM in `fsdp_vllm.py `_. + +**Pros** + +- Readily support various models. + + - Users only need to implement the corresponding + ``dtensor_weight_loader`` for weight synchronization between FSDP + and vLLM. While for ``hf_weight_loader``, users can directly apply + any models supported both in HF and vLLM without any code change. + +- Easy to organize the forward and backward computation for each model. + +**Cons** + +- Poor scalability when it comes to large-scale models (e.g. Llama 70B + and 405B) +- The resharding overhead between actor and rollout could be larger than + Megatron-LM backend. + +Due to the simplicity, we recommend using FSDP backend for algorithm +research and prototyping. + +FSDP Workers +------------ + +ActorRolloutRefWorker +^^^^^^^^^^^^^^^^^^^^^ + +Actor/Rollout HybridEngine +'''''''''''''''''''''''''' + +1. HybridEngine, Actor and Rollout initialization API. + +.. code:: python + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + +``ONE_TO_ALL``: when calling the ``init_model`` function from the driver +process, each worker (on a GPU) will execute the following model +initialization process. + +The initialization details of HybridEngine, Actor and Rollout are +highlighted below: + +1. ``DataParallelPPOActor`` implements the simple PPO computation logics + when the model is built with FSDP, including compute log prob, model + update. +2. ``vLLMRollout`` support generation with vLLM. We modify the vLLM + Engine and make it executed under SPMD to fit into our + ``WorkerGroup`` design. +3. ``FSDPVLLMShardingManager`` a context manager to perform actual + resharding between actor and rollout. + +See `source code `_. for more information. + +1. Generate sequence and recompute log prob + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def generate_sequences(self, prompts: DataProto): + +- ``Dispatch.DP_COMPUTE_PROTO``: The data will be dispatched and + collected along the DP dimension + +- In this function, the rollout model will perform auto-regressive + generation and the actor model will recompute the old log prob for the + generetad response. + +3. Update actor model + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def update_actor(self, data: DataProto): + +- Update the actor model weight using PPO & entropy loss. + +ReferenceModel +'''''''''''''' + +1. Reference model initialization + +The reference model is initialized using the same function as the actor +model without initializing the HybridEngine and Optimizer. Then the +actor model is also wrapped by the ``DataParallelPPOActor``. + +2. Compute reference log prob + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_ref_log_prob(self, data: DataProto): + +- In this function, the reference model will call the compute log prob + function in ``DataParallelPPOActor`` to compute the reference log + prob. + +CriticWorker and RewardWorker +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +1. Model initialization + +Quite similar to reference model. The CriticWorker will perform +additional initialization for the Optimizer. + +2. Compute Values for CriticWorker + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_values(self, data: DataProto): + +3. Update Critic + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def update_critic(self, data: DataProto): + +4. Compute Reward + +.. code:: python + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_rm_score(self, data: DataProto): + + +HybridShard +------------ + +We didn’t support FSDP `HybridShard`. To support this, we may need to +construct a 2D device mesh and test the corresponding +``dtensor_weight_loader`` and ``hf_weight_loader`` for each model. \ No newline at end of file diff --git a/docs/workers/megatron_workers.rst b/docs/workers/megatron_workers.rst new file mode 100644 index 000000000..5a8f5ed41 --- /dev/null +++ b/docs/workers/megatron_workers.rst @@ -0,0 +1,200 @@ +Megatron-LM Backend +================ + +We support Megatron Backend by implementing various workers for actor, +critic, reference, rollout and reward models. We also implement the +``3DHybridEngine`` using Megatron-LM and vLLM in `megatron_vllm.py `_. + +**Pros** + +- Support 3D parallelism and sequence parallelism for best scalablility + and throughput. +- 3D HybridEngine can significantly reduce peak memory usage and reduce + weight synchronize overhead between actor and rollout. + +**Cons** + +- Users should implement their own models for Megatron-LM +- Users should implement the corresponding weight_loader to + + - synchronize the model weight between actor (in Megatron) and rollout + (in vLLM). + - load weights from checkpoints to corresponding model in Megatron-LM + +Megatron Workers +---------------- + +MegatronWorker +^^^^^^^^^^^^^^ + +``MegatronWorker`` is the base class of different megatron worker +classes. In this class, ``get_megatron_global_info`` and +``get_megatron_rank_info`` function to retrive the 3D parallel world +size and rank of each ``Worker`` running on specific GPU. These information +will be used in transfer protocol for Megatron Backend. + +The following ``Worker`` class for different models will be utilized to +construct the ``WorkerGroup`` . + +We implement various of APIs for each ``Worker`` class decorated by the +``@register(dispatch_mode=)`` . These APIs can be called by the ray +driver process. The data can be correctly collect and dispatch following +the ``dispatch_mode`` on each function. The supported dispatch_model +(i.e., transfer protocols) can be found in `decorator.py `_. + +ActorRolloutRefWorker +^^^^^^^^^^^^^^^^^^^^^ + +This class is implemented for Actor/Rollout HybridEngine or for the +reference model to initialize their model and perform computation. + +Actor/Rollout HybridEngine +'''''''''''''''''''''''''' + +1. HybridEngine, Actor and Rollout initialization API. + +.. code:: python + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + +``ONE_TO_ALL``: when calling the ``init_model`` function from the driver +process, each worker (on a GPU) will execute the following model +initialization process. + +The initialization details of HybridEngine, Actor and Rollout are +highlighted below: + +1. ``AllGatherPPModel`` holds memory buffer for both Actor and Rollout + and support weight resharding between actor and rollout. +2. ``MegatronPPOActor`` implements the simple PPO computation logics + when the model is built with Megatron, including compute log prob, + model update. +3. ``vLLMRollout`` support generation with vLLM. We modify the vLLM + Engine and make it executed under SPMD to fit into our + ``WorkerGroup`` design. +4. ``MegatronVLLMShardingManager`` a context manager to perform actual + resharding between actor and rollout. + +See `source code `_ for more information. + +.. code:: python + + # Initialize the 3D HybridEngine + hybrid_engine = AllGatherPPModel(model_provider=megatron_actor_model_provider) + # Fetch the model at current rank + actor_module = hybrid_engine.this_rank_models + ... + + # build actor model + self.actor = MegatronPPOActor(config=self.config.actor, + model_config=self.actor_model_config, + megatron_config=megatron_config, + actor_module=self.actor_module, + actor_optimizer=self.actor_optimizer, + actor_optimizer_config=self.actor_optim_config) + + # build rollout + # rollout initialization + rollout = vLLMRollout(actor_module=params, + config=self.config.rollout, + tokenizer=self.tokenizer, + model_hf_config=self.actor_model_config, + train_tp=mpu.get_tensor_model_parallel_world_size()) + # perform weight resharding between actor and rollout + sharding_manager = MegatronVLLMShardingManager(module=self.hybrid_engine, + inference_engine=rollout.inference_engine, + model_config=self.actor_model_config, + layer_name_mapping=layer_name_mapping) + ... + +2. Generate sequence and recompute log prob + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_PP_AS_DP_PROTO) + def generate_sequences(self, prompts: DataProto): + +- ``Dispatch.MEGATRON_PP_AS_DP_PROTO``: The PP dimension of the actor + model will be regarded as DP dimension. Then the driver process will + dispatch and collect the data according to this reorganization. This + is because, in HybridEngine, the actor weight, which usually applied + larger 3D parallel sizes, will be gathered along the PP dimension and + TP dimension. Therefore, the corresponding data should be dispatched + and collected through the 3D parallel group of the rollout model, + rather than the actor model. However, the world_size and rank + information can only be retrived from ``get_megatron_global_info`` and + ``get_megatron_rank_info``, which records the 3D information for the + actor model. Moreover, the data resharding inside TP dimension will be + processed within the HybridEngine. + +- In this function, the rollout model will perform auto-regressive + generation and the actor model will recompute the old log prob for the + generetad response. + +3. Update actor model + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def update_actor(self, data: DataProto): + +- ``Dispatch.MEGATRON_COMPUTE_PROTO``: User passes the data partitioned + by DP dimension. The data is dispatched to all tp/pp ranks within the + same dp group, and ultimately only collects output data from tp=0 and + the last pp. +- Update the actor model weight using PPO & entropy loss. + +ReferenceModel +'''''''''''''' + +1. Reference model initialization + +The reference model is initialized using the same function as the actor +model without initializing the HybridEngine and Optimizer. Then the +actor model is also wrapped by the ``MegatronPPOActor``. + +2. Compute reference log prob + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_ref_log_prob(self, data: DataProto): + +- In this function, the reference model will call the compute log prob + function in ``MegatronPPOActor`` to compute the reference log prob. + +CriticWorker and RewardWorker +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +1. Model initialization + +Quite similar to reference model. The CriticWorker will perform +additional initialization for the Optimizer. + +2. Compute Values for CriticWorker + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_values(self, data: DataProto): + +3. Update Critic + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def update_critic(self, data: DataProto): + +4. Compute Reward + +.. code:: python + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_rm_score(self, data: DataProto): + +Context Parallel +---------------- + +This require the developer/contributor to implement the context parallel +both in Megatron-LM and models. diff --git a/docs/workers/ray_trainer.rst b/docs/workers/ray_trainer.rst new file mode 100644 index 000000000..9470bb8f8 --- /dev/null +++ b/docs/workers/ray_trainer.rst @@ -0,0 +1,243 @@ +PPO Ray Trainer +=============== + +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 runnning 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(parquet_files=self.config.data.train_files, + tokenizer=self.tokenizer, + prompt_key=self.config.data.prompt_key, + max_prompt_length=self.config.data.max_prompt_length, + filter_prompts=True, + return_raw_chat=self.config.data.get('return_raw_chat', False), + truncation='error') + +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 + + # Due to the Ray issue, we can only support max_colocate_count=1 for now. + # This means that each GPU can only have one process. + # We can support max_colocate > 1 when applying this pull request: https://github.com/ray-project/ray/pull/44385 + 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, + 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/examples/data_preprocess/full_hh_rlhf.py b/examples/data_preprocess/full_hh_rlhf.py new file mode 100644 index 000000000..daaa59cfd --- /dev/null +++ b/examples/data_preprocess/full_hh_rlhf.py @@ -0,0 +1,146 @@ +# 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'): + 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'): + 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']): + 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'): + 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', type=str, default='~/data/full_hh_rlhf') + parser.add_argument('--hdfs_dir', type=str, required=False, default=None) + + args = parser.parse_args() + + if args.split == 'sft': + generate_sft_dataset(args.hdfs_dir, os.path.join(args.local_dir, args.split)) + elif args.split == 'rm': + generate_rm_dataset(args.hdfs_dir, os.path.join(args.local_dir, args.split)) + elif args.split == 'rl': + generate_rl_dataset(args.hdfs_dir, os.path.join(args.local_dir, args.split)) + else: + raise NotImplementedError diff --git a/examples/data_preprocess/gsm8k.py b/examples/data_preprocess/gsm8k.py new file mode 100644 index 000000000..b3f491bb7 --- /dev/null +++ b/examples/data_preprocess/gsm8k.py @@ -0,0 +1,93 @@ +# 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 re +import os +import datasets + +from verl.utils.hdfs_io import copy, makedirs +import argparse + + +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='~/data/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'] + + 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 + + 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')) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_dir, dst=hdfs_dir) diff --git a/examples/data_preprocess/hellaswag.py b/examples/data_preprocess/hellaswag.py new file mode 100644 index 000000000..39c8e8f55 --- /dev/null +++ b/examples/data_preprocess/hellaswag.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 Hellaswag dataset. + +""" + +import re +import os +import datasets + +from verl.utils.hdfs_io import copy, makedirs +import argparse + + +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='/opt/tiger/hellaswag') + parser.add_argument('--hdfs_dir', default=None) + + args = parser.parse_args() + + data_source = 'Rowan/hellaswag' + + 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) + + local_dir = args.local_dir + hdfs_dir = args.hdfs_dir + + train_dataset.to_parquet(os.path.join(local_dir, 'train.parquet')) + val_dataset.to_parquet(os.path.join(local_dir, 'validation.parquet')) + test_dataset.to_parquet(os.path.join(local_dir, 'test.parquet')) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_dir, dst=hdfs_dir) diff --git a/examples/data_preprocess/math.py b/examples/data_preprocess/math.py new file mode 100644 index 000000000..6443431dc --- /dev/null +++ b/examples/data_preprocess/math.py @@ -0,0 +1,89 @@ +# 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 os +import datasets + +from verl.utils.hdfs_io import copy, makedirs +import argparse + +from verl.utils.reward_score.math import remove_boxed, last_boxed_only_string + + +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='~/data/math') + parser.add_argument('--hdfs_dir', default=None) + + args = parser.parse_args() + + data_source = 'lighteval/MATH' + + dataset = datasets.load_dataset(data_source, trust_remote_code=True) + + 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_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')) + + if hdfs_dir is not None: + makedirs(hdfs_dir) + + copy(src=local_dir, dst=hdfs_dir) diff --git a/examples/generation/run_deepseek_v2_lite_math.sh b/examples/generation/run_deepseek_v2_lite_math.sh new file mode 100644 index 000000000..216b33d4c --- /dev/null +++ b/examples/generation/run_deepseek_v2_lite_math.sh @@ -0,0 +1,16 @@ +python3 -m verl.trainer.main_generation \ + trainer.nnodes=1 \ + trainer.n_gpus_per_node=8 \ + data.path=~/data/rlhf/gsm8k/test.parquet \ + data.prompt_key=prompt \ + data.n_samples=1 \ + data.output_path=~/data/rlhf/math/deepseek_v2_lite_gen_test.parquet \ + model.path=deepseek-ai/deepseek-llm-7b-chat \ + +model.trust_remote_code=True \ + rollout.temperature=1.0 \ + rollout.top_k=50 \ + rollout.top_p=0.7 \ + rollout.prompt_length=2048 \ + rollout.response_length=1024 \ + rollout.tensor_model_parallel_size=2 \ + rollout.gpu_memory_utilization=0.8 diff --git a/examples/ppo_trainer/run_deepseek7b_llm.sh b/examples/ppo_trainer/run_deepseek7b_llm.sh new file mode 100644 index 000000000..f5beea652 --- /dev/null +++ b/examples/ppo_trainer/run_deepseek7b_llm.sh @@ -0,0 +1,38 @@ +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.val_batch_size=1312 \ + 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.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=32 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.grad_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=128 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=128 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.path=deepseek-ai/deepseek-llm-7b-chat \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=32 \ + critic.model.fsdp_config.param_offload=False \ + critic.model.fsdp_config.grad_offload=False \ + critic.model.fsdp_config.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + 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.total_epochs=15 \ No newline at end of file diff --git a/examples/ppo_trainer/run_deepseek_full_hh_rlhf.sh b/examples/ppo_trainer/run_deepseek_full_hh_rlhf.sh new file mode 100644 index 000000000..2990c5169 --- /dev/null +++ b/examples/ppo_trainer/run_deepseek_full_hh_rlhf.sh @@ -0,0 +1,41 @@ +set -x + +train_files=$HOME/data/full_hh_rlhf/rl/train.parquet +test_files=$HOME/data/full_hh_rlhf/rl/train.parquet # no use + +python3 -m verl.trainer.main_ppo --config-path=./config --config-name='ppo_megatron_trainer'\ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.train_batch_size=512 \ + data.val_batch_size=128 \ + data.max_prompt_length=128 \ + data.max_response_length=128 \ + actor_rollout_ref.model.path=deepseek-ai/deepseek-llm-7b-chat \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=128 \ + actor_rollout_ref.actor.ppo_micro_batch_size=16 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=16 \ + actor_rollout_ref.ref.param_offload=False \ + critic.optim.lr=1e-5 \ + critic.model.path=deepseek-ai/deepseek-llm-7b-chat \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=16 \ + reward_model.enable=True \ + reward_model.megatron.tensor_model_parallel_size=4 \ + reward_model.model.path=deepseek-ai/deepseek-llm-7b-chat \ + reward_model.micro_batch_size=16 \ + reward_model.param_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_megatron_full_hh_rlhf_examples' \ + trainer.experiment_name='deepseek_llm_7b_model_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=5 \ + trainer.total_epochs=100 \ No newline at end of file diff --git a/examples/ppo_trainer/run_deepseek_math_gsm8k_megatron.sh b/examples/ppo_trainer/run_deepseek_math_gsm8k_megatron.sh new file mode 100644 index 000000000..42c658519 --- /dev/null +++ b/examples/ppo_trainer/run_deepseek_math_gsm8k_megatron.sh @@ -0,0 +1,40 @@ +set -x + +gsm8k_train_path=$HOME/data/gsm8k/train.parquet +gsm8k_test_path=$HOME/data/gsm8k/test.parquet +math_train_path=$HOME/data/math/train.parquet +math_test_path=$HOME/data/math/test.parquet + +train_files="['$gsm8k_train_path', '$math_train_path']" +test_files="['$gsm8k_test_path', '$math_test_path']" + +python3 -m verl.trainer.main_ppo --config-path=./config --config-name='ppo_megatron_trainer'\ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.train_batch_size=1024 \ + data.val_batch_size=6312 \ + data.max_prompt_length=1024 \ + data.max_response_length=512 \ + actor_rollout_ref.model.path=deepseek-ai/deepseek-coder-6.7b-instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=32 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=32 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=32 \ + critic.optim.lr=1e-5 \ + critic.model.path=deepseek-ai/deepseek-coder-6.7b-instruct \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=32 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_megatron_math_gsm8k_examples' \ + trainer.experiment_name='deepseek_llm_7b_function_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=5 \ + trainer.total_epochs=100 \ No newline at end of file diff --git a/examples/ppo_trainer/run_deepseek_megatron.sh b/examples/ppo_trainer/run_deepseek_megatron.sh new file mode 100644 index 000000000..9e8a60aa8 --- /dev/null +++ b/examples/ppo_trainer/run_deepseek_megatron.sh @@ -0,0 +1,31 @@ +set -x + +python3 -m verl.trainer.main_ppo --config-path=./config --config-name='ppo_megatron_trainer'\ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.train_batch_size=1024 \ + data.val_batch_size=1312 \ + data.max_prompt_length=512 \ + data.max_response_length=512 \ + actor_rollout_ref.model.path=deepseek-ai/deepseek-coder-6.7b-instruct \ + actor_rollout_ref.actor.optim.lr=2e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=64 \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=64 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.5 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=128 \ + critic.optim.lr=2e-5 \ + critic.model.path=deepseek-ai/deepseek-coder-6.7b-instruct \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=64 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_megatron_gsm8k_examples' \ + trainer.experiment_name='deepseek_llm_7b_function_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.total_epochs=15 \ No newline at end of file diff --git a/examples/ppo_trainer/run_qwen2-7b.sh b/examples/ppo_trainer/run_qwen2-7b.sh new file mode 100644 index 000000000..c77549488 --- /dev/null +++ b/examples/ppo_trainer/run_qwen2-7b.sh @@ -0,0 +1,47 @@ +set -x + +gsm8k_train_path=$HOME/data/gsm8k/train.parquet +gsm8k_test_path=$HOME/data/gsm8k/test.parquet +math_train_path=$HOME/data/math/train.parquet +math_test_path=$HOME/data/math/test.parquet + +train_files="['$gsm8k_train_path', '$math_train_path']" +test_files="['$gsm8k_test_path', '$math_test_path']" + +python3 -m verl.trainer.main_ppo \ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.train_batch_size=1024 \ + data.val_batch_size=6312 \ + data.max_prompt_length=1024 \ + data.max_response_length=512 \ + 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=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=16 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.grad_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=2 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.4 \ + 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.path=Qwen/Qwen2-7B-Instruct \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=16 \ + critic.model.fsdp_config.param_offload=False \ + critic.model.fsdp_config.grad_offload=False \ + critic.model.fsdp_config.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_example' \ + trainer.experiment_name='Qwen2-7B-Instruct_function_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 \ No newline at end of file diff --git a/examples/ppo_trainer/run_qwen2-7b_rm.sh b/examples/ppo_trainer/run_qwen2-7b_rm.sh new file mode 100644 index 000000000..6132fa796 --- /dev/null +++ b/examples/ppo_trainer/run_qwen2-7b_rm.sh @@ -0,0 +1,54 @@ +set -x +# Discliamer: the model used in the script is only for academic example, +gsm8k_train_path=$HOME/data/gsm8k/train.parquet +gsm8k_test_path=$HOME/data/gsm8k/test.parquet +math_train_path=$HOME/data/math/train.parquet +math_test_path=$HOME/data/math/test.parquet + +train_files="['$gsm8k_train_path', '$math_train_path']" +test_files="['$gsm8k_test_path', '$math_test_path']" + +python3 -m verl.trainer.main_ppo \ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.train_batch_size=1024 \ + data.val_batch_size=6312 \ + data.max_prompt_length=1024 \ + data.max_response_length=512 \ + data.return_raw_chat=True \ + actor_rollout_ref.model.path=Qwen/Qwen2-7B-Instruct \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.optim.lr_warmup_steps_ratio=0.1 \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=16 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.grad_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=16 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=1 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.6 \ + 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.optim.lr_warmup_steps_ratio=0.05 \ + critic.model.path=Qwen/Qwen2-7B-Instruct \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=16 \ + critic.model.fsdp_config.param_offload=False \ + critic.model.fsdp_config.grad_offload=False \ + critic.model.fsdp_config.optimizer_offload=False \ + reward_model.enable=True \ + reward_model.model.path=sfairXC/FsfairX-Gemma2-RM-v0.1\ + reward_model.model.fsdp_config.param_offload=True \ + reward_model.micro_batch_size=16 \ + algorithm.kl_ctrl.kl_coef=0.001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_example' \ + trainer.experiment_name='Qwen2-7B-Instruct_hybrid_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=1 \ + trainer.save_freq=-1 \ + trainer.test_freq=5 \ + trainer.total_epochs=15 \ No newline at end of file diff --git a/examples/ppo_trainer/run_qwen2.5-32b.sh b/examples/ppo_trainer/run_qwen2.5-32b.sh new file mode 100644 index 000000000..358ea366a --- /dev/null +++ b/examples/ppo_trainer/run_qwen2.5-32b.sh @@ -0,0 +1,48 @@ +set -x + +gsm8k_train_path=$HOME/data/gsm8k/train.parquet +gsm8k_test_path=$HOME/data/gsm8k/test.parquet +math_train_path=$HOME/data/math/train.parquet +math_test_path=$HOME/data/math/test.parquet + +train_files="['$gsm8k_train_path', '$math_train_path']" +test_files="['$gsm8k_test_path', '$math_test_path']" + +python3 -m verl.trainer.main_ppo \ + data.train_files="$train_files" \ + data.val_files="$test_files" \ + data.train_batch_size=1024 \ + data.val_batch_size=6304 \ + data.max_prompt_length=1024 \ + data.max_response_length=1024 \ + actor_rollout_ref.model.path=Qwen/Qwen2.5-32B-Instruct \ + actor_rollout_ref.model.enable_gradient_checkpointing=False \ + actor_rollout_ref.actor.optim.lr=1e-6 \ + actor_rollout_ref.actor.ppo_mini_batch_size=256 \ + actor_rollout_ref.actor.ppo_micro_batch_size=16 \ + actor_rollout_ref.actor.fsdp_config.param_offload=False \ + actor_rollout_ref.actor.fsdp_config.grad_offload=False \ + actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \ + actor_rollout_ref.rollout.log_prob_micro_batch_size=128 \ + actor_rollout_ref.rollout.tensor_model_parallel_size=4 \ + actor_rollout_ref.rollout.name=vllm \ + actor_rollout_ref.rollout.gpu_memory_utilization=0.3 \ + actor_rollout_ref.ref.log_prob_micro_batch_size=128 \ + actor_rollout_ref.ref.fsdp_config.param_offload=True \ + critic.optim.lr=1e-5 \ + critic.model.path=Qwen/Qwen2.5-32B-Instruct \ + critic.model.enable_gradient_checkpointing=False \ + critic.ppo_micro_batch_size=32 \ + critic.model.fsdp_config.param_offload=False \ + critic.model.fsdp_config.grad_offload=False \ + critic.model.fsdp_config.optimizer_offload=False \ + algorithm.kl_ctrl.kl_coef=0.0001 \ + trainer.critic_warmup=0 \ + trainer.logger=['console','tracking'] \ + trainer.project_name='verl_example' \ + trainer.experiment_name='Qwen2.5-32B-Instruct_function_rm' \ + trainer.n_gpus_per_node=8 \ + trainer.nnodes=4 \ + trainer.save_freq=-1 \ + trainer.test_freq=10 \ + trainer.total_epochs=15 \ No newline at end of file diff --git a/examples/ray/tutorial.ipynb b/examples/ray/tutorial.ipynb new file mode 100644 index 000000000..415233182 --- /dev/null +++ b/examples/ray/tutorial.ipynb @@ -0,0 +1,949 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "ed65145e-1dcc-4fad-85cd-8cc683fde91b", + "metadata": {}, + "source": [ + "# VeRL Ray API Tutorial" + ] + }, + { + "cell_type": "markdown", + "id": "5f90dfa8-3285-41e4-ba44-12abcb76b3ce", + "metadata": { + "tags": [] + }, + "source": [ + "## Chapter 1: Ray Basics" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "id": "cafc9be5-614b-4380-9b69-31525ea4e73a", + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "\n", + "# turn off Megatron timer\n", + "os.environ['MEGATRON_USE_CUDA_TIMER'] = '0'\n", + "os.environ['MEGATRON_START_PROCESS_TIMER'] = 'False'\n", + "os.environ['NCCL_DEBUG'] = 'WARN'" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "3fb65288-4410-43b4-9ffc-8538197ae039", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "import ray\n", + "import torch" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "ca7f0778-f676-4f67-8c1f-9f6c8eee74e2", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2024-01-22 14:52:10,398\tINFO worker.py:1655 -- Started a local Ray instance. View the dashboard at \u001b[1m\u001b[32m127.0.0.1:8265 \u001b[39m\u001b[22m\n" + ] + }, + { + "data": { + "text/html": [ + "
\n", + "
\n", + "

Ray

\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "\n", + "\n", + "
Python version:3.9.2
Ray version: 2.3.0
Dashboard:http://127.0.0.1:8265
\n", + "
\n", + "
\n" + ], + "text/plain": [ + "RayContext(dashboard_url='127.0.0.1:8265', python_version='3.9.2', ray_version='2.3.0', ray_commit='{{RAY_COMMIT_SHA}}', address_info={'node_ip_address': '10.122.229.29', 'raylet_ip_address': '10.122.229.29', 'redis_address': None, 'object_store_address': '/tmp/ray/session_2024-01-22_14-52-08_389091_1470202/sockets/plasma_store', 'raylet_socket_name': '/tmp/ray/session_2024-01-22_14-52-08_389091_1470202/sockets/raylet', 'webui_url': '127.0.0.1:8265', 'session_dir': '/tmp/ray/session_2024-01-22_14-52-08_389091_1470202', 'metrics_export_port': 46547, 'gcs_address': '10.122.229.29:57281', 'address': '10.122.229.29:57281', 'dashboard_agent_listen_port': 52365, 'node_id': 'ec81d96ec29985ce0b98441aabb46fc8dcb49b0e1dec1088f973977f'})" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489193)\u001b[0m rank 0, value: tensor([1.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489357)\u001b[0m rank 2, value: tensor([3.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489358)\u001b[0m rank 3, value: tensor([4.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489356)\u001b[0m rank 1, value: tensor([2.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489837)\u001b[0m rank 0, value: tensor([2.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489999)\u001b[0m rank 3, value: tensor([5.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489998)\u001b[0m rank 2, value: tensor([4.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1489997)\u001b[0m rank 1, value: tensor([3.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490000)\u001b[0m rank 0, value: tensor([3.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490633)\u001b[0m rank 3, value: tensor([6.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490635)\u001b[0m rank 5, value: tensor([8.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490636)\u001b[0m rank 6, value: tensor([9.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490631)\u001b[0m rank 1, value: tensor([4.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490637)\u001b[0m rank 7, value: tensor([10.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490634)\u001b[0m rank 4, value: tensor([7.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulator pid=1490632)\u001b[0m rank 2, value: tensor([5.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491761)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491761)\u001b[0m rank 0, value: tensor([10.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491923)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491922)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491921)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491924)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491919)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491925)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491920)\u001b[0m 10\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491923)\u001b[0m rank 5, value: tensor([15.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491921)\u001b[0m rank 3, value: tensor([13.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491924)\u001b[0m rank 7, value: tensor([17.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491925)\u001b[0m rank 6, value: tensor([16.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491920)\u001b[0m rank 2, value: tensor([12.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491919)\u001b[0m rank 1, value: tensor([11.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(GPUAccumulatorDecorator pid=1491922)\u001b[0m rank 4, value: tensor([14.], device='cuda:0')\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument timing_log_level:2 with timing_log_level:0\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument tensor_model_parallel_size:1 with tensor_model_parallel_size:4\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument use_distributed_optimizer:False with use_distributed_optimizer:True\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument weight_decay:0.01 with weight_decay:0.0\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument sgd_momentum:0.9 with sgd_momentum:0.0\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument bf16:False with bf16:True\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument clip_grad:1.0 with clip_grad:0.0\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m WARNING: overriding default argument sequence_parallel:False with sequence_parallel:True\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m using world size: 4, data-parallel-size: 1, tensor-model-parallel size: 4, pipeline-model-parallel size: 1 \n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m setting global batch size to 1\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m accumulate gradients in fp32 and all-reduce gradients in fp32 for bfloat16 data type. \n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m using torch.bfloat16 for parameters ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m setting number of micro-batches to constant 1\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [2024-01-22 14:53:47] > initializing torch distributed ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group1]\tnew_group dp\thigh_stream False\tranks: [0]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group2]\tnew_group dp\thigh_stream False\tranks: [1]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group3]\tnew_group dp\thigh_stream False\tranks: [2]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group4]\tnew_group dp\thigh_stream False\tranks: [3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group5]\tnew_group mp\thigh_stream False\tranks: [0, 1, 2, 3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group6]\tnew_group tp\thigh_stream False\tranks: [0, 1, 2, 3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group8]\tnew_group pp\thigh_stream False\tranks: [0]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group9]\tnew_group emb\thigh_stream False\tranks: [0]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group10]\tnew_group posemb\thigh_stream False\tranks: [0]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group11]\tnew_group pp\thigh_stream False\tranks: [1]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group12]\tnew_group emb\thigh_stream False\tranks: [1]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group13]\tnew_group posemb\thigh_stream False\tranks: [1]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group14]\tnew_group pp\thigh_stream False\tranks: [2]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group15]\tnew_group emb\thigh_stream False\tranks: [2]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group16]\tnew_group posemb\thigh_stream False\tranks: [2]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group17]\tnew_group pp\thigh_stream False\tranks: [3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group18]\tnew_group emb\thigh_stream False\tranks: [3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [rank0]\t[group19]\tnew_group posemb\thigh_stream False\tranks: [3]\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m NCCL version 2.18.6+cuda12.1\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m this torch version does not have hires nccl profiling. nccl profiling is not enabled this time. this is only a kindly notice, not a error message\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [2024-01-22 14:53:52] > initialized tensor model parallel with size 4\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [2024-01-22 14:53:52] > initialized pipeline model parallel with size 1\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m > setting random seeds to 1234 ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m > compiling dataset index builder ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m already compiled, skip compiling\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m >>> done with dataset index builder. Compilation time: 0.005 seconds\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m > compiling and loading fused kernels ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m will compile object fused_mix_prec_layer_norm_cuda with extra cuda flags ['-maxrregcount=50', '-U__CUDA_NO_HALF_OPERATORS__', '-U__CUDA_NO_HALF_CONVERSIONS__']\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m [2024-01-22 14:53:52] >>> done with compiling and loading fused kernels. Compilation time: 0.039 seconds\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m this torch version does not have hires nccl profiling. nccl profiling is not enabled this time. this is only a kindly notice, not a error message\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m > compiling and loading fused kernels ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m will compile object fused_mix_prec_layer_norm_cuda with extra cuda flags ['-maxrregcount=50', '-U__CUDA_NO_HALF_OPERATORS__', '-U__CUDA_NO_HALF_CONVERSIONS__']\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m [2024-01-22 14:53:52] >>> done with compiling and loading fused kernels. Compilation time: 0.044 seconds\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m this torch version does not have hires nccl profiling. nccl profiling is not enabled this time. this is only a kindly notice, not a error message\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m > compiling and loading fused kernels ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m will compile object fused_mix_prec_layer_norm_cuda with extra cuda flags ['-maxrregcount=50', '-U__CUDA_NO_HALF_OPERATORS__', '-U__CUDA_NO_HALF_CONVERSIONS__']\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m [2024-01-22 14:53:52] >>> done with compiling and loading fused kernels. Compilation time: 0.044 seconds\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m this torch version does not have hires nccl profiling. nccl profiling is not enabled this time. this is only a kindly notice, not a error message\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m > compiling and loading fused kernels ...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m will compile object fused_mix_prec_layer_norm_cuda with extra cuda flags ['-maxrregcount=50', '-U__CUDA_NO_HALF_OPERATORS__', '-U__CUDA_NO_HALF_CONVERSIONS__']\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m [2024-01-22 14:53:52] >>> done with compiling and loading fused kernels. Compilation time: 0.045 seconds\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m No modifications detected for re-loaded extension module fused_mix_prec_layer_norm_cuda, skipping build step...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m Loading extension module fused_mix_prec_layer_norm_cuda...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m No modifications detected for re-loaded extension module fused_mix_prec_layer_norm_cuda, skipping build step...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m Loading extension module fused_mix_prec_layer_norm_cuda...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m No modifications detected for re-loaded extension module fused_mix_prec_layer_norm_cuda, skipping build step...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m Loading extension module fused_mix_prec_layer_norm_cuda...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m No modifications detected for re-loaded extension module fused_mix_prec_layer_norm_cuda, skipping build step...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m Loading extension module fused_mix_prec_layer_norm_cuda...\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493490)\u001b[0m Using `is_flash_attn_available` is deprecated and will be removed in v4.38. Please use `is_flash_attn_2_available` instead.\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493654)\u001b[0m Using `is_flash_attn_available` is deprecated and will be removed in v4.38. Please use `is_flash_attn_2_available` instead.\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493653)\u001b[0m Using `is_flash_attn_available` is deprecated and will be removed in v4.38. Please use `is_flash_attn_2_available` instead.\n", + "\u001b[2m\u001b[36m(MLPLayerWorker pid=1493652)\u001b[0m Using `is_flash_attn_available` is deprecated and will be removed in v4.38. Please use `is_flash_attn_2_available` instead.\n" + ] + } + ], + "source": [ + "# Build a local ray cluster. The head node and worker node are on this machine\n", + "ray.init()" + ] + }, + { + "cell_type": "markdown", + "id": "a0d35eb2-cf28-411a-b39f-f45e97c2edba", + "metadata": { + "tags": [] + }, + "source": [ + "Implement an Accumulator class." + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "86c98e9c-26a5-44d0-bbe9-064f8809708c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "@ray.remote\n", + "class Accumulator:\n", + " def __init__(self):\n", + " self.value = 0\n", + " \n", + " def add(self, x):\n", + " self.value += x\n", + " \n", + " def get_value(self):\n", + " return self.value" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "id": "9d5aa406-23c3-4270-a6f8-d4af20769ed9", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Instantiate an accumulator. Accumulator can be viewed as a process, acting as an RPC service.\n", + "accumulator = Accumulator.remote()" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "56850d3a-3803-4395-a73e-a27e99dc6905", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "value_ref = accumulator.get_value.remote() # Check the current value. Note that this function returns immediately and does not actually wait for the remote execution to complete.\n", + "# Get the value\n", + "value = ray.get(value_ref)\n", + "print(value)" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "932807f1-792b-42af-aa44-6a6202718919", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "10\n" + ] + } + ], + "source": [ + "# Accumulate, then check the result.\n", + "accumulator.add.remote(10) # Similarly, the 'add' here will return immediately.\n", + "new_value = ray.get(accumulator.get_value.remote())\n", + "print(new_value)" + ] + }, + { + "cell_type": "markdown", + "id": "ca31e450-e02c-44d0-86a1-a3f9eeadd311", + "metadata": {}, + "source": [ + "## Chapter 2: Resource Pool and RayWorkerGroup\n", + "In the previous example, it was a simple single-process worker. \n", + "In this example, we implement a worker with a GPU and form a RayWorkerGroup. Within this RayWorkerGroup, we implement a simple operation of an accumulator." + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "84727b3d-1351-4550-9009-48526d18dc2c", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from single_controller.ray.base import RayResourcePool, RayClassWithInitArgs, RayWorkerGroup, merge_resource_pool\n", + "from single_controller.base import Worker" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "aab5eeb7-6485-4911-94cc-3f5ca52b6634", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "resource_pool = RayResourcePool([4], use_gpu=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "54e4d531-ca50-43e9-ab79-d24bb66ffe0e", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "@ray.remote\n", + "class GPUAccumulator(Worker):\n", + "\n", + " def __init__(self) -> None:\n", + " super().__init__()\n", + " # The initial value of each rank is the same as the rank\n", + " self.value = torch.zeros(size=(1,), device='cuda') + self.rank\n", + "\n", + " def add(self, x):\n", + " self.value += x\n", + " print(f'rank {self.rank}, value: {self.value}')\n", + " return self.value.cpu()\n" + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "id": "73e0658b-a6a7-4a86-a53f-5442c8cd56fe", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tensor([1.]), tensor([2.]), tensor([3.]), tensor([4.])]\n" + ] + } + ], + "source": [ + "# Each worker's initial value is its rank, and then each rank's value is incremented by 1, so the values obtained on each rank are [1, 2, 3, 4]\n", + "class_with_args = RayClassWithInitArgs(cls=GPUAccumulator)\n", + "worker_group = RayWorkerGroup(resource_pool, class_with_args)\n", + "print(worker_group.execute_all_sync('add', x=[1,1,1,1]))" + ] + }, + { + "cell_type": "markdown", + "id": "e36f73e8-4f38-459f-a09b-f6ffb95fd42b", + "metadata": {}, + "source": [ + "The principle of parameter passing: The input parameter is a list of length world_size, where each element in the list is dispatched respectively to each worker in the RayWorkerGroup. \n", + "The return parameter is also a list, corresponding to the return value of each worker." + ] + }, + { + "cell_type": "markdown", + "id": "3873383b-e89d-4283-ae3e-af367e6160a5", + "metadata": {}, + "source": [ + "### GPU Resource Sharing" + ] + }, + { + "cell_type": "markdown", + "id": "cf62933e-143e-4a15-90c3-3d0d0568d89c", + "metadata": { + "tags": [] + }, + "source": [ + "RayWorkerGroups mapped to the same resource pool share the GPU. In this example, we implement three resource pools: the first occupies 4 GPUs, the second also occupies 4 GPUs, and the last occupies all 8 GPUs. Among them, the first resource pool reuses the resource pool mentioned above." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "100e75f1-6f2b-44b6-b4e1-f703eae8e5bf", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Create a new resource pool and then merge the newly created resource pool with the previous one.\n", + "resource_pool_1 = RayResourcePool([4], use_gpu=True, name_prefix='a')\n", + "resource_pool_merge = merge_resource_pool(resource_pool, resource_pool_1)" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "59393509-037b-475a-800a-5e6de9ea7b66", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Establish a RayWorkerGroup on the newly created resource pool.\n", + "worker_group_1 = RayWorkerGroup(resource_pool_1, class_with_args)\n", + "worker_group_merge = RayWorkerGroup(resource_pool_merge, class_with_args)" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "558dc5bf-75aa-4675-b8de-99944e088522", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tensor([2.]), tensor([3.]), tensor([4.]), tensor([5.])]\n" + ] + } + ], + "source": [ + "# Run 'add' on the second set of 4 GPUs; the result should be [2, 3, 4, 5].\n", + "output_1 = worker_group_1.execute_all_sync('add', x=[2,2,2,2])\n", + "print(output_1)" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "id": "30228667-f8ef-448c-a2cc-44241ede86d9", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tensor([3.]), tensor([4.]), tensor([5.]), tensor([6.]), tensor([7.]), tensor([8.]), tensor([9.]), tensor([10.])]\n" + ] + } + ], + "source": [ + "# Run 'add' on the merged set of 8 GPUs; the result should be [3, 4, 5, 6, 7, 8, 9, 10].\n", + "output_merge = worker_group_merge.execute_all_sync('add', x=[3,3,3,3,3,3,3,3])\n", + "print(output_merge)" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "041329de-0e27-4e31-ac11-f0dd54f4f4b3", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 4 8\n" + ] + } + ], + "source": [ + "print(worker_group.world_size, worker_group_1.world_size, worker_group_merge.world_size)" + ] + }, + { + "cell_type": "markdown", + "id": "8cb9fc59-6035-4b72-9863-67bd4d48f3fe", + "metadata": {}, + "source": [ + "## Chapter 3: Data Dispatch, Execution and Collection" + ] + }, + { + "cell_type": "markdown", + "id": "0e1fedea-16d1-4dec-b269-c402747e11e8", + "metadata": {}, + "source": [ + "In the above example, we used the `execute_all_sync` function in the RayWorkerGroup to dispatch data from the driver to each worker. This is very inconvenient for coding. \n", + "In this chapter, we use the form of function decorators to allow RayWorkerGroup to directly call functions written in the Worker, and to greatly simplify parameter passing." + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "c70d1b33-030a-4681-aeb2-16ab48b6445b", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from single_controller.ray.decorator import register, Dispatch, Execute" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "id": "bb6b76c1-8caf-4749-b20d-c3f842751aa4", + "metadata": {}, + "outputs": [], + "source": [ + "@ray.remote\n", + "class GPUAccumulatorDecorator(Worker):\n", + "\n", + " def __init__(self) -> None:\n", + " super().__init__()\n", + " # The initial value of each rank is the same as the rank\n", + " self.value = torch.zeros(size=(1,), device='cuda') + self.rank\n", + " \n", + " # map from a single input to all the worker\n", + " @register(Dispatch.ONE_TO_ALL)\n", + " def add(self, x):\n", + " print(x)\n", + " self.value = self.value + x\n", + " print(f'rank {self.rank}, value: {self.value}')\n", + " return self.value.cpu()" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "2d2bb460-4e9d-4fc5-a767-e7c8b9a4d7fe", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "class_with_args = RayClassWithInitArgs(cls=GPUAccumulatorDecorator)\n", + "gpu_accumulator_decorator = RayWorkerGroup(resource_pool_merge, class_with_args)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "id": "e4db17f7-8896-4ea6-a732-2146400ff2da", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[tensor([10.]), tensor([11.]), tensor([12.]), tensor([13.]), tensor([14.]), tensor([15.]), tensor([16.]), tensor([17.])]\n" + ] + } + ], + "source": [ + "# As we can see, 10 is automatically dispatched to each Worker in this RayWorkerGroup.\n", + "print(gpu_accumulator_decorator.add(x=10))" + ] + }, + { + "cell_type": "markdown", + "id": "f8b54ccb-0064-4d06-b74b-fbf39f8c5faf", + "metadata": {}, + "source": [ + "### Custom Dispatch, Collection\n", + "Users can customize `dispatch` and `collection` function. You only need to write the `dispatch_fn` and `collect_fn` functions yourself. We also support executing RPC only on rank_zero, with specific examples provided below." + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "id": "9ad68e0f-372e-4792-b4db-c2406f211113", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from single_controller.ray.decorator import register, Dispatch, collect_all_to_all, Execute" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "id": "74f91242-3176-4ea8-adce-104a58d21874", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "def two_to_all_dispatch_fn(worker_group, *args, **kwargs):\n", + " \"\"\"\n", + " Assume the input is a list of 2. Duplicate the input interleaved and pass to each worker.\n", + " \"\"\"\n", + " for arg in args:\n", + " assert len(arg) == 2\n", + " for i in range(worker_group.world_size - 2):\n", + " arg.append(arg[i % 2])\n", + " for k, v in kwargs.items():\n", + " assert len(v) == 2\n", + " for i in range(worker_group.world_size - 2):\n", + " v.append(v[i % 2])\n", + " return args, kwargs\n", + "\n", + "\n", + "@ray.remote\n", + "class TestActor(Worker):\n", + " # TODO: pass *args and **kwargs is bug prone and not very convincing\n", + " def __init__(self, x) -> None:\n", + " super().__init__()\n", + " self._x = x\n", + "\n", + " def foo(self, y):\n", + " return self._x + y\n", + "\n", + " @register(dispatch_mode=Dispatch.ALL_TO_ALL, execute_mode=Execute.RANK_ZERO)\n", + " def foo_rank_zero(self, x, y):\n", + " return self._x + y + x\n", + "\n", + " @register(dispatch_mode={'dispatch_fn': two_to_all_dispatch_fn, 'collect_fn': collect_all_to_all})\n", + " def foo_custom(self, x, y):\n", + " return self._x + y + x" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "id": "00f386a8-c5f7-49c4-87ec-6e2ab3547bf1", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "class_with_args = RayClassWithInitArgs(cls=TestActor, x=2)\n", + "worker_group = RayWorkerGroup(resource_pool, class_with_args)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "45ae935c-4f92-4c28-9e35-add439dcd2a0", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "output_ref = worker_group.foo_custom(x=[1, 2], y=[5, 6])\n", + "assert output_ref == [8, 10, 8, 10]\n", + "\n", + "output_ref = worker_group.foo_rank_zero(x=1, y=2)\n", + "assert output_ref == 5" + ] + }, + { + "cell_type": "markdown", + "id": "165ae826-7b20-4305-a5c6-d7f236eab410", + "metadata": {}, + "source": [ + "## Chapter 4: MegatronRayWorkerGroup" + ] + }, + { + "cell_type": "markdown", + "id": "d0855a6b-41d7-41bb-95e6-d9395b175d81", + "metadata": {}, + "source": [ + "Finally, we implement a `MegatronRayWorkerGroup`, within which we create a Megatron and then run a tensor parallel (tp) split Llama mlp layer. Here, we use a complex dispatch mode, `Megatron_COMPUTE`. This dispatch mode assumes that user passes the data partitioned by DP dimension. The data is dispatched to all tp/pp ranks within the same dp group, and ultimately only collects output data from tp=0 and the last pp. In this way, for users that only write code on the driver, the Megatron behind the RPC becomes transparent." + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "d36b4606-0e28-4fd6-8e04-498408ca161a", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "from single_controller.ray import MegatronRayWorkerGroup\n", + "from single_controller.megatron.worker import MegatronWorker\n", + "from omegaconf import OmegaConf" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "430fa41d-7874-49fd-b7f0-2c173a6c849b", + "metadata": {}, + "outputs": [], + "source": [ + "@ray.remote\n", + "class MLPLayerWorker(MegatronWorker):\n", + " @register(Dispatch.ONE_TO_ALL)\n", + " def init_model(self, config):\n", + " from omegaconf import OmegaConf\n", + " from verl.models.llama.megatron.layers import ParallelLlamaMLP\n", + " megatron_config = OmegaConf.create({'sequence_parallel_enabled': False})\n", + " self.parallel_layer = ParallelLlamaMLP(config=config, megatron_config=megatron_config)\n", + " \n", + " @register(Dispatch.ONE_TO_ALL)\n", + " def get_weights(self):\n", + " output = {}\n", + " for key, val in self.parallel_layer.named_parameters():\n", + " output[key] = val\n", + " return output\n", + " \n", + " @register(Dispatch.MEGATRON_COMPUTE)\n", + " def run_layer(self, x):\n", + " x = x.to('cuda')\n", + " y = self.parallel_layer(x)\n", + " return y" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "2b749128-4190-454f-96a2-72477a7a77bd", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "layer_cls = RayClassWithInitArgs(cls=MLPLayerWorker)\n", + "layer_worker_group = MegatronRayWorkerGroup(resource_pool=resource_pool,\n", + " ray_cls_with_init=layer_cls,\n", + " default_megatron_kwargs={\n", + " 'tensor_model_parallel_size': 4,\n", + " 'pipeline_model_parallel_size': 1,\n", + " 'num_layers_per_virtual_pipeline_stage': None\n", + " })\n" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "f134f99e-678a-4b6f-8d70-fce2a5c9ac3e", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 4 1 1\n" + ] + } + ], + "source": [ + "print(layer_worker_group.world_size, layer_worker_group.tp_size, layer_worker_group.pp_size, layer_worker_group.dp_size)" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "204ce866-868a-4b0b-900b-0dda6164a995", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "ffn_hidden_size = 11008\n", + "batch_size = 16\n", + "seq_len = 2048\n", + "hidden_size = 4096\n", + "\n", + "config = OmegaConf.create({\n", + " 'hidden_size': hidden_size,\n", + " 'intermediate_size': ffn_hidden_size,\n", + " 'hidden_act': 'silu',\n", + " 'pretraining_tp': 1,\n", + " 'tp': layer_worker_group.tp_size,\n", + "})" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "d35a0216-7787-4ea7-86ab-c87f8e98f13d", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "x = torch.rand(size=(seq_len, batch_size, hidden_size), dtype=torch.float32)" + ] + }, + { + "cell_type": "code", + "execution_count": 31, + "id": "0f84eaf8-adcd-4f89-a1ce-800db07df242", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[None, None, None, None]" + ] + }, + "execution_count": 31, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "layer_worker_group.init_model(config)" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "5203bb40-f831-4898-832c-5f65596ba0f6", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "torch.Size([2048, 16, 4096])\n" + ] + } + ], + "source": [ + "output = layer_worker_group.run_layer([x]) # This must be a list of size 1, ensuring that the input equals the data parallel (dp).\n", + "print(output[0].shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "6a159884-7409-4dfe-a367-3c31090b09a1", + "metadata": { + "tags": [] + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "0\n" + ] + } + ], + "source": [ + "print(gpu_accumulator_decorator.world_size)" + ] + }, + { + "cell_type": "code", + "execution_count": 33, + "id": "806c36c3-a6a6-4c99-88fb-f2e2468b12db", + "metadata": { + "tags": [] + }, + "outputs": [], + "source": [ + "# Shutdown ray cluster\n", + "ray.shutdown()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ea54e858-5221-493e-b53a-4765b918a0fa", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "fileId": "e862b5a9-13b2-48bf-9eda-96bd0c76e37c", + "kernelspec": { + "display_name": "Python 3.9.2 64-bit", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.9.2" + }, + "vscode": { + "interpreter": { + "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" + } + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/examples/sft/gsm8k/run_deepseek_6b7.sh b/examples/sft/gsm8k/run_deepseek_6b7.sh new file mode 100644 index 000000000..8a28cc7d6 --- /dev/null +++ b/examples/sft/gsm8k/run_deepseek_6b7.sh @@ -0,0 +1,16 @@ +set -x + +hdfs_path=hdfs://user/verl/experiments/gsm8k/deepseek-coder-6.7b-instruct/ # replace to your own hdfs/local path + +TORCHRUN -m verl.trainer.fsdp_sft_trainer \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.prompt_key=question \ + data.response_key=answer \ + data.micro_batch_size=8 \ + model.partial_pretrain=deepseek-ai/deepseek-coder-6.7b-instruct \ + trainer.default_hdfs_dir=$hdfs_path \ + trainer.project_name=gsm8k-sft \ + trainer.experiment_name=gsm8k-sft-deepseek-coder-6.7b-instruct \ + trainer.total_epochs=4 \ + trainer.logger=['console','tracking'] \ No newline at end of file diff --git a/examples/sft/gsm8k/run_gemma_2b.sh b/examples/sft/gsm8k/run_gemma_2b.sh new file mode 100644 index 000000000..fb1b987f4 --- /dev/null +++ b/examples/sft/gsm8k/run_gemma_2b.sh @@ -0,0 +1,18 @@ +# Tested in 4 GPUs + +set -x + +hdfs_path=hdfs://user/verl/experiments/gsm8k/gemma-2b-it/ # replace to your own hdfs/local path + +TORCHRUN -m verl.trainer.fsdp_sft_trainer \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.prompt_key=question \ + data.response_key=answer \ + data.micro_batch_size=32 \ + model.partial_pretrain=google/gemma-2b-it \ + trainer.default_hdfs_dir=$hdfs_path \ + trainer.project_name=gsm8k-sft \ + trainer.experiment_name=gsm8k-sft-gemma-2b-it \ + trainer.total_epochs=3 \ + trainer.logger=['console','tracking'] \ No newline at end of file diff --git a/examples/sft/gsm8k/run_gemma_7b.sh b/examples/sft/gsm8k/run_gemma_7b.sh new file mode 100644 index 000000000..4955c078c --- /dev/null +++ b/examples/sft/gsm8k/run_gemma_7b.sh @@ -0,0 +1,16 @@ +set -x + +hdfs_path=hdfs://user/verl/experiments/gsm8k/gemma-1.1-7b-it/ # replace to your own hdfs/local path + +TORCHRUN -m verl.trainer.fsdp_sft_trainer \ + data.train_files=$HOME/data/gsm8k/train.parquet \ + data.val_files=$HOME/data/gsm8k/test.parquet \ + data.prompt_key=question \ + data.response_key=answer \ + data.micro_batch_size=8 \ + model.partial_pretrain=google/gemma-1.1-7b-it \ + trainer.default_hdfs_dir=$hdfs_path \ + trainer.project_name=gsm8k-sft \ + trainer.experiment_name=gsm8k-sft-gemma-1.1-7b-it \ + trainer.total_epochs=4 \ + trainer.logger=['console','tracking'] \ No newline at end of file diff --git a/patches/megatron_v4.patch b/patches/megatron_v4.patch new file mode 100644 index 000000000..17bceecc1 --- /dev/null +++ b/patches/megatron_v4.patch @@ -0,0 +1,568 @@ +diff --git a/.gitignore b/.gitignore +index 5955b349..ade0cd51 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -7,3 +7,5 @@ build + slurm* + logs + .vscode ++tests/* ++examples/* +diff --git a/build.sh b/build.sh +new file mode 100644 +index 00000000..49d5361f +--- /dev/null ++++ b/build.sh +@@ -0,0 +1,4 @@ ++#! /bin/bash ++ ++export PYTHONPATH=$PYTHONPATH:$(pwd) ++pip3 install regex ninja +diff --git a/megatron/__init__.py b/megatron/__init__.py +index c35de282..60896b47 100644 +--- a/megatron/__init__.py ++++ b/megatron/__init__.py +@@ -2,7 +2,7 @@ + + import torch + +-from .global_vars import get_args, get_retro_args ++from .global_vars import get_args, update_args, fork_args_namespace, get_retro_args + from .global_vars import get_current_global_batch_size + from .global_vars import get_num_microbatches + from .global_vars import get_signal_handler +diff --git a/megatron/arguments.py b/megatron/arguments.py +index 0ca8776e..9ef67624 100644 +--- a/megatron/arguments.py ++++ b/megatron/arguments.py +@@ -59,6 +59,16 @@ def parse_args(extra_args_provider=None, ignore_unknown_args=False): + return args + + def validate_args(args, defaults={}): ++ # Set input defaults. ++ for key in defaults: ++ if getattr(args, key, None) is not None: ++ if args.rank == 0 and defaults[key] != getattr(args, key): ++ print('WARNING: overriding default argument {key}:{v2} \ ++ with {key}:{v}'.format(key=key, v=defaults[key], ++ v2=getattr(args, key)), ++ flush=True) ++ ++ setattr(args, key, defaults[key]) + # Tensor model parallel size. + args.tensor_model_parallel_size = min( + args.tensor_model_parallel_size, args.world_size) +@@ -125,19 +135,19 @@ def validate_args(args, defaults={}): + args.recompute_granularity = 'selective' + del args.recompute_activations + +- # Set input defaults. +- for key in defaults: +- # For default to be valid, it should not be provided in the +- # arguments that are passed to the program. We check this by +- # ensuring the arg is set to None. +- if getattr(args, key, None) is not None: +- if args.rank == 0: +- print('WARNING: overriding default arguments for {key}:{v} \ +- with {key}:{v2}'.format(key=key, v=defaults[key], +- v2=getattr(args, key)), +- flush=True) +- else: +- setattr(args, key, defaults[key]) ++ # # Set input defaults. ++ # for key in defaults: ++ # # For default to be valid, it should not be provided in the ++ # # arguments that are passed to the program. We check this by ++ # # ensuring the arg is set to None. ++ # if getattr(args, key, None) is not None: ++ # if args.rank == 0: ++ # print('WARNING: overriding default arguments for {key}:{v} \ ++ # with {key}:{v2}'.format(key=key, v=defaults[key], ++ # v2=getattr(args, key)), ++ # flush=True) ++ # else: ++ # setattr(args, key, defaults[key]) + + # Batch size. + assert args.micro_batch_size is not None +diff --git a/megatron/core/pipeline_parallel/p2p_communication.py b/megatron/core/pipeline_parallel/p2p_communication.py +index 29ee34df..fa590b16 100644 +--- a/megatron/core/pipeline_parallel/p2p_communication.py ++++ b/megatron/core/pipeline_parallel/p2p_communication.py +@@ -130,32 +130,28 @@ def _batched_p2p_ops( + send_prev_op = torch.distributed.P2POp( + torch.distributed.isend, + tensor_send_prev, +- get_pipeline_model_parallel_prev_rank(), +- group, ++ get_pipeline_model_parallel_prev_rank() + ) + ops.append(send_prev_op) + if tensor_recv_prev is not None: + recv_prev_op = torch.distributed.P2POp( + torch.distributed.irecv, + tensor_recv_prev, +- get_pipeline_model_parallel_prev_rank(), +- group, ++ get_pipeline_model_parallel_prev_rank() + ) + ops.append(recv_prev_op) + if tensor_send_next is not None: + send_next_op = torch.distributed.P2POp( + torch.distributed.isend, + tensor_send_next, +- get_pipeline_model_parallel_next_rank(), +- group, ++ get_pipeline_model_parallel_next_rank() + ) + ops.append(send_next_op) + if tensor_recv_next is not None: + recv_next_op = torch.distributed.P2POp( + torch.distributed.irecv, + tensor_recv_next, +- get_pipeline_model_parallel_next_rank(), +- group, ++ get_pipeline_model_parallel_next_rank() + ) + ops.append(recv_next_op) + if len(ops) > 0: +diff --git a/megatron/core/pipeline_parallel/schedules.py b/megatron/core/pipeline_parallel/schedules.py +index 992da781..2eb78d52 100644 +--- a/megatron/core/pipeline_parallel/schedules.py ++++ b/megatron/core/pipeline_parallel/schedules.py +@@ -78,6 +78,8 @@ def get_forward_backward_func(): + transformer, this is the encoder's sequence length. This is ignored if variable_seq_lengths + in the config is True. Otherwise, each microbatch in the current global batch size must use + this sequence length. ++ ++ hidden_size (int, required): hidden size of the model + + micro_batch_size (int, required): The number of sequences in a microbatch. + +@@ -287,6 +289,7 @@ def forward_backward_no_pipelining( + model: Union[torch.nn.Module, List[torch.nn.Module]], + num_microbatches: int, + seq_length: int, # unused ++ hidden_size: int, # unused + micro_batch_size: int, # unused + decoder_seq_length: int = None, # unused + forward_only: bool = False, +@@ -370,8 +373,10 @@ def forward_backward_pipelining_with_interleaving( + data_iterator: Union[Iterator, List[Iterator]], + model: Union[torch.nn.Module, List[torch.nn.Module]], + num_microbatches: int, +- seq_length: int, +- micro_batch_size: int, ++ seq_length: int = None, ++ hidden_size: int = None, ++ micro_batch_size: int = None, ++ input_shapes: list = None, + decoder_seq_length: int = None, + forward_only: bool = False, + collect_non_loss_data: bool = False, +@@ -457,7 +462,7 @@ def forward_backward_pipelining_with_interleaving( + "Interleaving is not supported with a different decoder sequence length." + ) + +- tensor_shape = [seq_length, micro_batch_size, config.hidden_size] ++ tensor_shape = [seq_length, micro_batch_size, hidden_size] + if config.sequence_parallel: + tensor_shape[0] = tensor_shape[0] // parallel_state.get_tensor_model_parallel_world_size() + +@@ -944,6 +949,7 @@ def get_tensor_shapes( + rank: int, + model_type: ModelType, + seq_length: int, ++ hidden_size: int, + micro_batch_size: int, + decoder_seq_length: int, + config, +@@ -967,12 +973,12 @@ def get_tensor_shapes( + + if model_type == ModelType.encoder_and_decoder: + if parallel_state.is_pipeline_stage_before_split(rank): +- tensor_shapes.append((seq_length, micro_batch_size, config.hidden_size)) ++ tensor_shapes.append((seq_length, micro_batch_size, hidden_size)) + else: +- tensor_shapes.append((decoder_seq_length, micro_batch_size, config.hidden_size)) +- tensor_shapes.append((seq_length, micro_batch_size, config.hidden_size)) ++ tensor_shapes.append((decoder_seq_length, micro_batch_size, hidden_size)) ++ tensor_shapes.append((seq_length, micro_batch_size, hidden_size)) + else: +- tensor_shapes.append((seq_length, micro_batch_size, config.hidden_size)) ++ tensor_shapes.append((seq_length, micro_batch_size, hidden_size)) + return tensor_shapes + + +@@ -1050,8 +1056,10 @@ def forward_backward_pipelining_without_interleaving( + data_iterator: Union[Iterator, List[Iterator]], + model: Union[torch.nn.Module, List[torch.nn.Module]], + num_microbatches: int, +- seq_length: int, +- micro_batch_size: int, ++ seq_length: int = None, ++ hidden_size: int = None, ++ micro_batch_size: int = None, ++ input_shapes: list = None, + decoder_seq_length: int = None, + forward_only: bool = False, + collect_non_loss_data: bool = False, +@@ -1127,22 +1135,34 @@ def forward_backward_pipelining_without_interleaving( + model_type = get_model_type(model) + + rank = parallel_state.get_pipeline_model_parallel_rank() +- recv_tensor_shapes = get_tensor_shapes( +- rank=rank - 1, +- model_type=model_type, +- seq_length=seq_length, +- micro_batch_size=micro_batch_size, +- decoder_seq_length=decoder_seq_length, +- config=config, +- ) +- send_tensor_shapes = get_tensor_shapes( +- rank=rank, +- model_type=model_type, +- seq_length=seq_length, +- micro_batch_size=micro_batch_size, +- decoder_seq_length=decoder_seq_length, +- config=config, +- ) ++ ++ def get_recv_tensor_shapes(microbatch_id): ++ if input_shapes: ++ return [input_shapes[microbatch_id]] ++ recv_tensor_shapes = get_tensor_shapes( ++ rank=rank - 1, ++ model_type=model_type, ++ seq_length=seq_length, ++ hidden_size=hidden_size, ++ micro_batch_size=micro_batch_size, ++ decoder_seq_length=decoder_seq_length, ++ config=config, ++ ) ++ return recv_tensor_shapes ++ ++ def get_send_tensor_shapes(microbatch_id): ++ if input_shapes: ++ return [input_shapes[microbatch_id]] ++ send_tensor_shapes = get_tensor_shapes( ++ rank=rank, ++ model_type=model_type, ++ seq_length=seq_length, ++ hidden_size=hidden_size, ++ micro_batch_size=micro_batch_size, ++ decoder_seq_length=decoder_seq_length, ++ config=config, ++ ) ++ return send_tensor_shapes + + # Input, output tensors only need to be saved when doing backward passes + input_tensors = None +@@ -1163,7 +1183,12 @@ def forward_backward_pipelining_without_interleaving( + else: + checkpoint_activations_microbatch = None + ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {i}: warmup recv_forward begin...') ++ recv_tensor_shapes = get_recv_tensor_shapes(i) # fwd recv shape + input_tensor = recv_forward(recv_tensor_shapes, config) ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {i}: warmup recv_forward end & forward begin...') + output_tensor = forward_step( + forward_step_func, + data_iterator, +@@ -1175,7 +1200,13 @@ def forward_backward_pipelining_without_interleaving( + collect_non_loss_data, + checkpoint_activations_microbatch, + ) ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: output tensor shape = {output_tensor[0].shape}, send_tensor_shapes={send_tensor_shapes}') ++ # print(f'rank {torch.cuda.current_device()}: micro batch {i}: warmup forward end & send_forward begin...') ++ send_tensor_shapes = get_send_tensor_shapes(i) # fwd send shape + send_forward(output_tensor, send_tensor_shapes, config) ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {i}: warmup send_forward end...') + + if not forward_only: + input_tensors.append(input_tensor) +@@ -1186,11 +1217,16 @@ def forward_backward_pipelining_without_interleaving( + # If all microbatches are run in warmup / cooldown phase, then no need to + # receive this tensor here. + if num_microbatches_remaining > 0: +- input_tensor = recv_forward(recv_tensor_shapes, config) ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {num_warmup_microbatches}: 1f1b recv_forward begin...') ++ recv_tensor_shapes = get_recv_tensor_shapes(num_warmup_microbatches) # fwd recv shape ++ input_tensor = recv_forward(recv_tensor_shapes, config) + + # Run 1F1B in steady state. + for i in range(num_microbatches_remaining): + last_iteration = i == (num_microbatches_remaining - 1) ++ next_forward_k = num_warmup_microbatches + i + 1 ++ backward_k = i + + # Decide to checkpoint all layers' activations of the current micro-batch + if max_outstanding_backprops is not None: +@@ -1199,7 +1235,8 @@ def forward_backward_pipelining_without_interleaving( + ) >= config.num_microbatches_with_partial_activation_checkpoints + else: + checkpoint_activations_microbatch = None +- ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {num_warmup_microbatches + i}: 1f1b recv_forward end & forward begin...') + output_tensor = forward_step( + forward_step_func, + data_iterator, +@@ -1213,12 +1250,23 @@ def forward_backward_pipelining_without_interleaving( + ) + + if forward_only: ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {num_warmup_microbatches + i}: 1f1b forward end & send forward begin...') ++ send_tensor_shapes = get_send_tensor_shapes(next_forward_k - 1) # fwd send shape + send_forward(output_tensor, send_tensor_shapes, config) + + if not last_iteration: ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {num_warmup_microbatches + i}: 1f1b send forward end & recv forward begin...') ++ recv_tensor_shapes = get_recv_tensor_shapes(next_forward_k) # fwd recv shape + input_tensor = recv_forward(recv_tensor_shapes, config) ++ else: ++ pass ++ # if torch.cuda.current_device() == 0 or torch.cuda.current_device() == 4: ++ # print(f'rank {torch.cuda.current_device()}: micro batch {num_warmup_microbatches + i}: 1f1b send forward end...') + + else: ++ send_tensor_shapes = get_send_tensor_shapes(backward_k) # bwd recv shape + output_tensor_grad = send_forward_recv_backward( + output_tensor, send_tensor_shapes, config + ) +@@ -1245,8 +1293,10 @@ def forward_backward_pipelining_without_interleaving( + + if last_iteration: + input_tensor = None ++ recv_tensor_shapes = get_recv_tensor_shapes(backward_k) # bwd send shape + send_backward(input_tensor_grad, recv_tensor_shapes, config) + else: ++ recv_tensor_shapes = get_recv_tensor_shapes(next_forward_k) # fwd recv shape + input_tensor = send_backward_recv_forward( + input_tensor_grad, recv_tensor_shapes, config + ) +@@ -1254,7 +1304,7 @@ def forward_backward_pipelining_without_interleaving( + # Run cooldown backward passes. + if not forward_only: + for i in range(num_warmup_microbatches): +- ++ backward_k = num_microbatches_remaining + i + # Enable async grad reduction in the last backward pass + # Note: If grad sync function is provided, only enable + # async grad reduction in first pipeline stage. Other +@@ -1267,12 +1317,14 @@ def forward_backward_pipelining_without_interleaving( + input_tensor = input_tensors.pop(0) + output_tensor = output_tensors.pop(0) + ++ send_tensor_shapes = get_send_tensor_shapes(backward_k) # bwd recv shape + output_tensor_grad = recv_backward(send_tensor_shapes, config) + + input_tensor_grad = backward_step( + input_tensor, output_tensor, output_tensor_grad, model_type, config + ) + ++ recv_tensor_shapes = get_recv_tensor_shapes(backward_k) # bwd send shape + send_backward(input_tensor_grad, recv_tensor_shapes, config) + + # Launch any remaining grad reductions. +diff --git a/megatron/core/utils.py b/megatron/core/utils.py +index d4e042b2..c480d14e 100644 +--- a/megatron/core/utils.py ++++ b/megatron/core/utils.py +@@ -55,8 +55,9 @@ def get_model_type(model): + return get_attr_wrapped_model(model, 'model_type') + + ++# walkaround: get_model_config to get megatron config (ModelParallelConfig) + def get_model_config(model): +- return get_attr_wrapped_model(model, 'config', allow_none=False) ++ return get_attr_wrapped_model(model, 'megatron_config', allow_none=False) + + + class GlobalMemoryBuffer: +diff --git a/megatron/global_vars.py b/megatron/global_vars.py +index b1b4b043..9e23dea5 100644 +--- a/megatron/global_vars.py ++++ b/megatron/global_vars.py +@@ -21,11 +21,48 @@ _GLOBAL_ADLR_AUTORESUME = None + _GLOBAL_TIMERS = None + _GLOBAL_SIGNAL_HANDLER = None + +-def get_args(): ++DEFAULT_NAMESPACE = 'default' ++import contextlib ++ ++@contextlib.contextmanager ++def fork_args_namespace(namespace): ++ """ ++ Usage example: ++ update_args('vit', vit_config) ++ with fork_args_namespace('vit'): ++ do vit stuff here ++ """ ++ # Check if we have added the args namespace ++ if namespace not in _GLOBAL_ARGS: ++ raise Exception('args namespace {} is not added'.format(namespace)) ++ # Store current args namespace. ++ tmp = _GLOBAL_ARGS[DEFAULT_NAMESPACE] ++ # Set args namespace to the desired one ++ _GLOBAL_ARGS[DEFAULT_NAMESPACE] = _GLOBAL_ARGS[namespace] ++ # Do the stuff we wanted to do. ++ try: ++ yield ++ finally: ++ _GLOBAL_ARGS[DEFAULT_NAMESPACE] = tmp ++ ++def get_args(namespace=DEFAULT_NAMESPACE): + """Return arguments.""" + _ensure_var_is_initialized(_GLOBAL_ARGS, 'args') +- return _GLOBAL_ARGS ++ return _GLOBAL_ARGS[namespace] + ++def set_args(args): ++ global _GLOBAL_ARGS ++ if _GLOBAL_ARGS is None: ++ _GLOBAL_ARGS = {} ++ _GLOBAL_ARGS[DEFAULT_NAMESPACE] = args ++ ++def update_args(namespace, args): ++ _ensure_var_is_initialized(_GLOBAL_ARGS, 'args') ++ if namespace not in _GLOBAL_ARGS: ++ import copy ++ _GLOBAL_ARGS[namespace] = copy.deepcopy(_GLOBAL_ARGS[DEFAULT_NAMESPACE]) ++ for k, v in args.items(): ++ setattr(_GLOBAL_ARGS[namespace], k, v) + + def get_retro_args(): + """Return retro arguments.""" +@@ -87,7 +124,7 @@ def _set_signal_handler(): + + + +-def set_global_variables(args, build_tokenizer=True): ++def set_global_variables(args): + """Set args, tokenizer, tensorboard-writer, adlr-autoresume, and timers.""" + + assert args is not None +@@ -96,7 +133,7 @@ def set_global_variables(args, build_tokenizer=True): + set_args(args) + + _build_num_microbatches_calculator(args) +- if build_tokenizer: ++ if args.vocab_file: + _ = _build_tokenizer(args) + _set_tensorboard_writer(args) + _set_wandb_writer(args) +@@ -107,11 +144,6 @@ def set_global_variables(args, build_tokenizer=True): + _set_signal_handler() + + +-def set_args(args): +- global _GLOBAL_ARGS +- _GLOBAL_ARGS = args +- +- + def set_retro_args(retro_args): + global _GLOBAL_RETRO_ARGS + _GLOBAL_RETRO_ARGS = retro_args +diff --git a/megatron/initialize.py b/megatron/initialize.py +index fb7866ab..01999622 100644 +--- a/megatron/initialize.py ++++ b/megatron/initialize.py +@@ -39,7 +39,7 @@ def initialize_megatron( + if not allow_no_cuda: + # Make sure cuda is available. + assert torch.cuda.is_available(), "Megatron requires CUDA." +- ++ print('use open-source megatron initialize...') + # Parse arguments + args = parse_args(extra_args_provider, ignore_unknown_args) + +diff --git a/megatron/model/fused_layer_norm.py b/megatron/model/fused_layer_norm.py +index c91a674e..bcb7bd7e 100644 +--- a/megatron/model/fused_layer_norm.py ++++ b/megatron/model/fused_layer_norm.py +@@ -81,7 +81,7 @@ class MixedFusedLayerNorm(torch.nn.Module): + if self.no_persist_layer_norm: + assert FusedLayerNormAffineFunction is not None, \ + "FusedLayerNormAffineFunction is not available, please install apex from https://github.com/NVIDIA/apex" +- return FusedLayerNormAffineFunction.apply(input, weight, self.bias, self.normalized_shape, self.eps) ++ return FusedLayerNormAffineFunction.apply(input, weight, self.bias, self.normalized_shape, self.eps, False) + else: + output = FastLayerNormFN.apply(input, weight, self.bias, self.eps) + +diff --git a/megatron/optimizer/distrib_optimizer.py b/megatron/optimizer/distrib_optimizer.py +index a04ae478..b64d22a5 100644 +--- a/megatron/optimizer/distrib_optimizer.py ++++ b/megatron/optimizer/distrib_optimizer.py +@@ -366,7 +366,7 @@ class DistributedOptimizer(MixedPrecisionOptimizer): + + def __init__(self, optimizer, clip_grad, log_num_zeros_in_grad, + check_for_nan_in_grad, params_have_main_grad, fp16, +- bf16, params_dtype, grad_scaler, models): ++ bf16, params_dtype, grad_scaler, models, overlap_param_gather=False): + """ + See top of class definition for argument descriptions. + +@@ -382,8 +382,11 @@ class DistributedOptimizer(MixedPrecisionOptimizer): + check_for_nan_in_grad, params_have_main_grad, + fp16, bf16, params_dtype, grad_scaler, models) + +- assert isinstance(optimizer, Adam), \ +- "Only Adam currently supported, due to checkpointing requirements." ++ # assert isinstance(optimizer, Adam), \ ++ # "Only Adam currently supported, due to checkpointing requirements." ++ ++ if not isinstance(optimizer, Adam): ++ print("WARNING: the optimizer type is not Adam, and now Only Adam currently support checkpointing requirements!") + + # Model grad buffer ranges. + self.model_gbuf_ranges = [] +@@ -476,7 +479,7 @@ class DistributedOptimizer(MixedPrecisionOptimizer): + self.param_buffer_copied.append(False) + self.num_all_gather_handles = len(self.all_gather_handle_index_to_bucket_index_map) + +- self.overlap_param_gather = get_args().overlap_param_gather ++ self.overlap_param_gather = overlap_param_gather + if self.overlap_param_gather: + self.remove_pre_hook_handle = torch.nn.modules.module.register_module_forward_pre_hook( + self._make_forward_pre_hook()) +diff --git a/megatron/training.py b/megatron/training.py +index 36f6c52e..73664509 100644 +--- a/megatron/training.py ++++ b/megatron/training.py +@@ -430,6 +430,7 @@ def train_step(forward_step_func, data_iterator, + model=model, + num_microbatches=get_num_microbatches(), + seq_length=args.seq_length, ++ hidden_size=args.hidden_size, + micro_batch_size=args.micro_batch_size, + decoder_seq_length=args.decoder_seq_length, + forward_only=False) +diff --git a/tools/prebuild_kernels.py b/tools/prebuild_kernels.py +new file mode 100644 +index 00000000..6f891b9e +--- /dev/null ++++ b/tools/prebuild_kernels.py +@@ -0,0 +1,13 @@ ++import os ++from megatron.fused_kernels import load ++ ++ ++class FakeArgs: ++ rank = 0 ++ ++ ++# 7.0 for V100 ++# 8.0 for A100/A800 ++os.environ["TORCH_CUDA_ARCH_LIST"] = "7.0+PTX;8.0+PTX" ++ ++load(FakeArgs) +\ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 000000000..823cfda11 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,12 @@ +transformers +hydra-core +tensordict < 0.3.1 +numpy +pytest +deepspeed +pybind11 +codetiming +yapf +wandb +git+https://github.com/NVIDIA/TransformerEngine.git@stable +# vllm==0.5.4 # vllm is installed in image building to avoid ray conflicts \ No newline at end of file diff --git a/setup.py b/setup.py new file mode 100644 index 000000000..b09c6c859 --- /dev/null +++ b/setup.py @@ -0,0 +1,62 @@ +# 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 setuptools import setup, find_packages +import os + +version_folder = os.path.dirname(os.path.join(os.path.abspath(__file__))) + +with open(os.path.join(version_folder, 'verl/version/version')) as f: + __version__ = f.read().strip() + +# TODO: add version info to requirements +install_requires = [ + 'tensordict', + 'transformers<4.45', + 'codetiming', + 'pybind11', + 'hydra-core', + 'numpy', + 'pytest', + 'yapf', + "dill", + "accelerate" +] + +install_optional = [ + 'vllm==0.5.4', + 'liger-kernel' +] + +extras_require = { + 'demo': ['hydra-core', 'transformers', ''], + 'single-controller': ['ray', 'kubernetes'], + 'single-controller-ray': ['ray'], +} + +setup( + name='verl', + version=__version__, + package_dir={'': '.'}, + packages=find_packages(where='.'), + url='https://github.com/volcengine/verl', + license='Apache 2.0', + author='Bytedance - Seed - MLSys', + author_email='zhangchi.usc1992@bytedance.com, gmsheng@connect.hku.hk', + description='veRL: Volcano Engine Reinforcement Learning for LLM', + install_requires=install_requires, + extras_require=extras_require, + package_data={'': ['version/*']}, + include_package_data=True, +) diff --git a/single_controller/__init__.py b/single_controller/__init__.py new file mode 100644 index 000000000..bd850b790 --- /dev/null +++ b/single_controller/__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. + +import os + +version_folder = os.path.dirname(os.path.join(os.path.abspath(__file__))) + +with open(os.path.join(version_folder, 'version/version')) as f: + __version__ = f.read().strip() diff --git a/single_controller/base/__init__.py b/single_controller/base/__init__.py new file mode 100644 index 000000000..75846436c --- /dev/null +++ b/single_controller/base/__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 .worker import Worker +from .worker_group import WorkerGroup, ClassWithInitArgs, ResourcePool diff --git a/single_controller/base/decorator.py b/single_controller/base/decorator.py new file mode 100644 index 000000000..9544ac65a --- /dev/null +++ b/single_controller/base/decorator.py @@ -0,0 +1,410 @@ +# 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 enum import Enum +from functools import wraps +from typing import Dict, List, Tuple +from types import FunctionType +from verl.protocol import DataProtoFuture + +# here we add a magic number of avoid user-defined function already have this attribute +MAGIC_ATTR = 'attrs_3141562937' + + +class Dispatch(Enum): + RANK_ZERO = 0 + ONE_TO_ALL = 1 + ALL_TO_ALL = 2 + MEGATRON_COMPUTE = 3 + MEGATRON_PP_AS_DP = 4 + MEGATRON_PP_ONLY = 5 + MEGATRON_COMPUTE_PROTO = 6 + MEGATRON_PP_AS_DP_PROTO = 7 + DP_COMPUTE = 8 + DP_COMPUTE_PROTO = 9 + DP_COMPUTE_PROTO_WITH_FUNC = 10 + DP_COMPUTE_METRIC = 11 + + +class Execute(Enum): + ALL = 0 + RANK_ZERO = 1 + + +def _split_args_kwargs_data_proto(chunks, *args, **kwargs): + from verl.protocol import DataProto, DataProtoFuture + splitted_args = [] + for arg in args: + assert isinstance(arg, (DataProto, DataProtoFuture)) + splitted_args.append(arg.chunk(chunks=chunks)) + + splitted_kwargs = {} + for key, val in kwargs.items(): + assert isinstance(val, (DataProto, DataProtoFuture)) + splitted_kwargs[key] = val.chunk(chunks=chunks) + + return splitted_args, splitted_kwargs + + +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 + + +def dispatch_all_to_all(worker_group, *args, **kwargs): + return args, kwargs + + +def collect_all_to_all(worker_group, output): + return output + + +def dispatch_megatron_compute(worker_group, *args, **kwargs): + """ + User passes in dp data. The data is dispatched to all tp/pp ranks with the same dp + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, + MegatronWorkerGroup), f'worker_group must be MegatronWorkerGroup, Got {type(worker_group)}' + + all_args = [] + for arg in args: + assert isinstance(arg, (Tuple, List)) and len(arg) == worker_group.dp_size + transformed_args = [] + for i in range(worker_group.world_size): + local_dp_rank = worker_group.get_megatron_rank_info(rank=i).dp_rank + transformed_args.append(arg[local_dp_rank]) + all_args.append(transformed_args) + all_args = tuple(all_args) + + all_kwargs = {} + for k, v in kwargs.items(): + assert isinstance(v, (Tuple, List)) and len(v) == worker_group.dp_size + transformed_v = [] + for i in range(worker_group.world_size): + local_dp_rank = worker_group.get_megatron_rank_info(rank=i).dp_rank + transformed_v.append(v[local_dp_rank]) + all_kwargs[k] = transformed_v + return all_args, all_kwargs + + +def collect_megatron_compute(worker_group, output): + """ + Only collect the data from the tp=0 and pp=last and every dp ranks + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + output_in_dp = [] + pp_size = worker_group.get_megatron_global_info().pp_size + for global_rank in range(worker_group.world_size): + local_rank_info = worker_group.get_megatron_rank_info(rank=global_rank) + if local_rank_info.tp_rank == 0 and local_rank_info.pp_rank == pp_size - 1: + output_in_dp.append(output[global_rank]) + return output_in_dp + + +def dispatch_megatron_compute_data_proto(worker_group, *args, **kwargs): + """ + All the args and kwargs must be DataProto. The batch will be chunked by dp_size and passed to each rank + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + + splitted_args, splitted_kwargs = _split_args_kwargs_data_proto(worker_group.dp_size, *args, **kwargs) + return dispatch_megatron_compute(worker_group, *splitted_args, **splitted_kwargs) + + +def _concat_data_proto_or_future(output: List): + from verl.protocol import DataProto, DataProtoFuture + import ray + + # make sure all the elements in output has the same type + for o in output: + assert type(o) == type(output[0]) + + o = output[0] + + if isinstance(o, DataProto): + return DataProto.concat(output) + elif isinstance(o, ray.ObjectRef): + return DataProtoFuture.concat(output) + else: + raise NotImplementedError + + +def collect_megatron_compute_data_proto(worker_group, output): + """ + Each output must be a DataProto. We concat the dim=0 of output + """ + from verl.protocol import DataProto + import ray + + output = collect_megatron_compute(worker_group, output) + for o in output: + assert isinstance(o, (DataProto, ray.ObjectRef)), f"expecting {o} to be DataProto, but got {type(o)}" + + return _concat_data_proto_or_future(output) + + +def dispatch_megatron_pp_as_dp(worker_group, *args, **kwargs): + """ + treat pp as dp. + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + + pp_size = worker_group.pp_size + dp_size = worker_group.dp_size + + pp_dp_size = pp_size * dp_size + + all_args = [] + for arg in args: + assert isinstance(arg, (List, Tuple)) and len(arg) == pp_dp_size + transformed_args = [] + for i in range(worker_group.world_size): + local_dp_rank = worker_group.get_megatron_rank_info(rank=i).dp_rank + local_pp_rank = worker_group.get_megatron_rank_info(rank=i).pp_rank + # compute the rank in arg. Note that the order is dp then pp + # Also note that the outputs within a pp group will be firstly allgathered, then only the output of pp0 will be collected. + # For pp=2 dp=4, a batch of data "ABCDEFGH" should be dispatched and collected in below order: + # dispatch: pp_allgther: collect: + # dp 0 1 2 3 dp 0 1 2 3 + # pp +---------+ pp +-------------+ + # 0 | A C E G | 0 | AB CD EF GH | ABCDEFGH + # 1 | B D F H | 1 | AB CD EF GH | + # +---------+ +-------------+ + arg_rank = local_dp_rank * worker_group.pp_size + local_pp_rank + + transformed_args.append(arg[arg_rank]) + all_args.append(transformed_args) + all_args = tuple(all_args) + + all_kwargs = {} + for k, v in kwargs.items(): + assert isinstance(v, (List, Tuple)) and len(v) == pp_dp_size, f'expect len(v)=={pp_dp_size}, got {len(v)}' + transformed_v = [] + for i in range(worker_group.world_size): + local_dp_rank = worker_group.get_megatron_rank_info(rank=i).dp_rank + local_pp_rank = worker_group.get_megatron_rank_info(rank=i).pp_rank + # compute the rank in arg. Note that the order is dp then pp + arg_rank = local_dp_rank * worker_group.pp_size + local_pp_rank + transformed_v.append(v[arg_rank]) + all_kwargs[k] = transformed_v + return all_args, all_kwargs + + +def collect_megatron_pp_as_dp(worker_group, output): + """ + treat pp as dp. Only collect data on tp=0 + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + output_in_dp = [] + for global_rank in range(worker_group.world_size): + local_rank_info = worker_group.get_megatron_rank_info(rank=global_rank) + if local_rank_info.tp_rank == 0 and local_rank_info.pp_rank == 0: + output_in_dp.append(output[global_rank]) + return output_in_dp + + +def collect_megatron_pp_only(worker_group, output): + """ + Only collect output of megatron pp. This is useful when examine weight names as they are identical in tp/dp + """ + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + output_in_pp = [] + for global_rank in range(worker_group.world_size): + local_rank_info = worker_group.get_megatron_rank_info(rank=global_rank) + if local_rank_info.tp_rank == 0 and local_rank_info.dp_rank == 0: + output_in_pp.append(output[global_rank]) + return output_in_pp + + +def dispatch_megatron_pp_as_dp_data_proto(worker_group, *args, **kwargs): + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + + pp_dp_size = worker_group.dp_size * worker_group.pp_size + splitted_args, splitted_kwargs = _split_args_kwargs_data_proto(pp_dp_size, *args, **kwargs) + return dispatch_megatron_pp_as_dp(worker_group, *splitted_args, **splitted_kwargs) + + +def collect_megatron_pp_as_dp_data_proto(worker_group, output): + from verl.protocol import DataProto + from single_controller.base.megatron.worker_group import MegatronWorkerGroup + assert isinstance(worker_group, MegatronWorkerGroup) + + output = collect_megatron_pp_as_dp(worker_group, output) + return _concat_data_proto_or_future(output) + + +def dispatch_dp_compute(worker_group, *args, **kwargs): + from single_controller.base.worker_group import WorkerGroup + assert isinstance(worker_group, WorkerGroup) + for arg in args: + assert isinstance(arg, (Tuple, List)) and len(arg) == worker_group.world_size + for k, v in kwargs.items(): + assert isinstance(v, (Tuple, List)) and len(v) == worker_group.world_size + return args, kwargs + + +def collect_dp_compute(worker_group, output): + from single_controller.base.worker_group import WorkerGroup + assert isinstance(worker_group, WorkerGroup) + assert len(output) == worker_group.world_size + return output + + +def dispatch_dp_compute_data_proto(worker_group, *args, **kwargs): + from single_controller.base.worker_group import WorkerGroup + assert isinstance(worker_group, WorkerGroup) + splitted_args, splitted_kwargs = _split_args_kwargs_data_proto(worker_group.world_size, *args, **kwargs) + return splitted_args, splitted_kwargs + + +def dispatch_dp_compute_data_proto_with_func(worker_group, *args, **kwargs): + from single_controller.base.worker_group import WorkerGroup + assert isinstance(worker_group, WorkerGroup) + assert type(args[0]) == FunctionType # NOTE: The first one args is a function! + + splitted_args, splitted_kwargs = _split_args_kwargs_data_proto(worker_group.world_size, *args[1:], **kwargs) + splitted_args_with_func = [[args[0]] * worker_group.world_size] + splitted_args + return splitted_args_with_func, splitted_kwargs + + +def collect_dp_compute_data_proto(worker_group, output): + from verl.protocol import DataProto + import ray + + for o in output: + assert isinstance(o, (DataProto, ray.ObjectRef)), f"expecting {o} to be DataProto, but got {type(o)}" + + output = collect_dp_compute(worker_group, output) + return _concat_data_proto_or_future(output) + + +def get_predefined_dispatch_fn(dispatch_mode): + predefined_dispatch_mode_fn = { + Dispatch.ONE_TO_ALL: { + 'dispatch_fn': dispatch_one_to_all, + 'collect_fn': collect_all_to_all, + }, + Dispatch.ALL_TO_ALL: { + 'dispatch_fn': dispatch_all_to_all, + 'collect_fn': collect_all_to_all, + }, + Dispatch.MEGATRON_COMPUTE: { + 'dispatch_fn': dispatch_megatron_compute, + 'collect_fn': collect_megatron_compute, + }, + Dispatch.MEGATRON_PP_AS_DP: { + 'dispatch_fn': dispatch_megatron_pp_as_dp, + 'collect_fn': collect_megatron_pp_as_dp, + }, + Dispatch.MEGATRON_PP_ONLY: { + 'dispatch_fn': dispatch_one_to_all, + 'collect_fn': collect_megatron_pp_only + }, + Dispatch.MEGATRON_COMPUTE_PROTO: { + 'dispatch_fn': dispatch_megatron_compute_data_proto, + 'collect_fn': collect_megatron_compute_data_proto + }, + Dispatch.MEGATRON_PP_AS_DP_PROTO: { + 'dispatch_fn': dispatch_megatron_pp_as_dp_data_proto, + 'collect_fn': collect_megatron_pp_as_dp_data_proto + }, + Dispatch.DP_COMPUTE: { + 'dispatch_fn': dispatch_dp_compute, + 'collect_fn': collect_dp_compute + }, + Dispatch.DP_COMPUTE_PROTO: { + 'dispatch_fn': dispatch_dp_compute_data_proto, + 'collect_fn': collect_dp_compute_data_proto + }, + Dispatch.DP_COMPUTE_PROTO_WITH_FUNC: { + 'dispatch_fn': dispatch_dp_compute_data_proto_with_func, + 'collect_fn': collect_dp_compute_data_proto + }, + Dispatch.DP_COMPUTE_METRIC: { + 'dispatch_fn': dispatch_dp_compute_data_proto, + 'collect_fn': collect_dp_compute + } + } + return predefined_dispatch_mode_fn[dispatch_mode] + + +def get_predefined_execute_fn(execute_mode): + """ + Note that here we only asks execute_all and execute_rank_zero to be implemented + Leave the choice of how these two functions handle argument 'blocking' to users + """ + predefined_execute_mode_fn = { + Execute.ALL: { + 'execute_fn_name': 'execute_all' + }, + Execute.RANK_ZERO: { + 'execute_fn_name': 'execute_rank_zero' + } + } + return predefined_execute_mode_fn[execute_mode] + + +def _check_dispatch_mode(dispatch_mode): + assert isinstance(dispatch_mode, + (Dispatch, Dict)), f'dispatch_mode must be a Dispatch or a Dict. Got {dispatch_mode}' + if isinstance(dispatch_mode, Dict): + necessary_keys = ['dispatch_fn', 'collect_fn'] + for key in necessary_keys: + assert key in dispatch_mode, f'key {key} should be in dispatch_mode if it is a dictionary' + + +def _check_execute_mode(execute_mode): + assert isinstance(execute_mode, Execute), f'execute_mode must be a Execute. Got {execute_mode}' + + +def _materialize_futures(*args, **kwargs): + new_args = [] + for arg in args: + if isinstance(arg, DataProtoFuture): + arg = arg.get() + # add more type to materialize + new_args.append(arg) + for k, v in kwargs.items(): + if isinstance(v, DataProtoFuture): + kwargs[k] = v.get() + + new_args = tuple(new_args) + return new_args, kwargs + + +def register(dispatch_mode=Dispatch.ALL_TO_ALL, execute_mode=Execute.ALL, blocking=True, materialize_futures=True): + _check_dispatch_mode(dispatch_mode=dispatch_mode) + _check_execute_mode(execute_mode=execute_mode) + + 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 diff --git a/single_controller/base/dp.py b/single_controller/base/dp.py new file mode 100644 index 000000000..5d534e87d --- /dev/null +++ b/single_controller/base/dp.py @@ -0,0 +1,47 @@ +# 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 single_controller.base.worker import Worker + + +class DPEngineWorker(Worker): + + def __init__(self, *args, **kwargs): + # todo: extract _world_size etc. from kwargs and inject in super().__init__() + Worker.__init__(self, *args, **kwargs) + + def init(self): + raise NotImplementedError + + def add_engine(self, model, dp_config): + raise NotImplementedError + + def execute_engine(self, method_name, *args, **kwargs): + print(f"execute_engine called with method={method_name}") + func = getattr(self._engine, method_name) + return func(*args, **kwargs) + + def execute_module(self, method_name, *args, **kwargs): + print(f"execute_module called with method={method_name}") + func = getattr(self._engine.module, method_name) + return func(*args, **kwargs) + + def get_model_size_on_rank_zero(self): + import torch + from verl.utils.model import get_model_size + if torch.distributed.get_rank() == 0: + # print("model print on rank 0: ", self._model) + module_size, module_size_scale = get_model_size(self._model) + return module_size, module_size_scale + return None diff --git a/single_controller/base/megatron/__init__.py b/single_controller/base/megatron/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/single_controller/base/megatron/__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/single_controller/base/megatron/worker.py b/single_controller/base/megatron/worker.py new file mode 100644 index 000000000..46608bbf0 --- /dev/null +++ b/single_controller/base/megatron/worker.py @@ -0,0 +1,39 @@ +# 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 dataclasses import dataclass +from single_controller.base.worker import Worker, DistRankInfo, DistGlobalInfo + + +class MegatronWorker(Worker): + + def __init__(self, cuda_visible_devices=None) -> None: + super().__init__(cuda_visible_devices) + + def get_megatron_global_info(self): + from megatron.core import parallel_state as mpu + tp_size = mpu.get_tensor_model_parallel_world_size() + dp_size = mpu.get_data_parallel_world_size() + pp_size = mpu.get_pipeline_model_parallel_world_size() + info = DistGlobalInfo(tp_size=tp_size, dp_size=dp_size, pp_size=pp_size) + return info + + def get_megatron_rank_info(self): + from megatron.core import parallel_state as mpu + tp_rank = mpu.get_tensor_model_parallel_rank() + dp_rank = mpu.get_data_parallel_rank() + pp_rank = mpu.get_pipeline_model_parallel_rank() + info = DistRankInfo(tp_rank=tp_rank, dp_rank=dp_rank, pp_rank=pp_rank) + return info \ No newline at end of file diff --git a/single_controller/base/megatron/worker_group.py b/single_controller/base/megatron/worker_group.py new file mode 100644 index 000000000..59a78ffbc --- /dev/null +++ b/single_controller/base/megatron/worker_group.py @@ -0,0 +1,51 @@ +# 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 Dict + +from .worker import DistRankInfo, DistGlobalInfo +from single_controller.base import ResourcePool, WorkerGroup + + +class MegatronWorkerGroup(WorkerGroup): + + def __init__(self, resource_pool: ResourcePool, **kwargs): + super().__init__(resource_pool=resource_pool, **kwargs) + self._megatron_rank_info = None + self._megatron_global_info: DistGlobalInfo = None + + def init_megatron(self, default_megatron_kwargs: Dict = None): + raise NotImplementedError(f"MegatronWorkerGroup.init_megatron should be overwritten") + + def get_megatron_rank_info(self, rank: int) -> DistRankInfo: + assert 0 <= rank < self.world_size, f'rank must be from [0, world_size), Got {rank}' + return self._megatron_rank_info[rank] + + @property + def tp_size(self): + assert self._megatron_global_info is not None, "MegatronWorkerGroup._megatron_global_info must be initialized" + return self._megatron_global_info.tp_size + + @property + def dp_size(self): + assert self._megatron_global_info is not None, "MegatronWorkerGroup._megatron_global_info must be initialized" + return self._megatron_global_info.dp_size + + @property + def pp_size(self): + assert self._megatron_global_info is not None, "MegatronWorkerGroup._megatron_global_info must be initialized" + return self._megatron_global_info.pp_size + + def get_megatron_global_info(self): + return self._megatron_global_info diff --git a/single_controller/base/register_center/__init__.py b/single_controller/base/register_center/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/single_controller/base/register_center/__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/single_controller/base/register_center/ray.py b/single_controller/base/register_center/ray.py new file mode 100644 index 000000000..430290cf2 --- /dev/null +++ b/single_controller/base/register_center/ray.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. + +import ray + + +@ray.remote +class WorkerGroupRegisterCenter: + + def __init__(self, rank_zero_info): + self.rank_zero_info = rank_zero_info + + def get_rank_zero_info(self): + return self.rank_zero_info + + +def create_worker_group_register_center(name, info): + return WorkerGroupRegisterCenter.options(name=name).remote(info) diff --git a/single_controller/base/worker.py b/single_controller/base/worker.py new file mode 100644 index 000000000..efd23a8da --- /dev/null +++ b/single_controller/base/worker.py @@ -0,0 +1,181 @@ +# 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 class for Worker +""" +import os +import socket +from dataclasses import dataclass +from single_controller.base.decorator import register, Dispatch + + +@dataclass +class DistRankInfo: + tp_rank: int + dp_rank: int + pp_rank: int + + +@dataclass +class DistGlobalInfo: + tp_size: int + dp_size: int + pp_size: int + + +class WorkerHelper: + + def _get_node_ip(self): + + def get_node_ip_by_sdk(): + if os.getenv("WG_BACKEND", None) == "ray": + import ray + return ray._private.services.get_node_ip_address() + elif os.getenv("WG_BACKEND", None) == "torch_rpc": + from single_controller.torchrpc.k8s_client import get_ip_addr + return get_ip_addr() + return None + + host_ipv4 = os.getenv("MY_HOST_IP", None) + host_ipv6 = os.getenv("MY_HOST_IPV6", None) + host_ip_by_env = host_ipv4 or host_ipv6 + host_ip_by_sdk = get_node_ip_by_sdk() + + host_ip = host_ip_by_env or host_ip_by_sdk + return host_ip + + def _get_free_port(self): + with socket.socket() as sock: + sock.bind(('', 0)) + return sock.getsockname()[1] + + def get_availale_master_addr_port(self): + return self._get_node_ip(), str(self._get_free_port()) + + def _get_pid(self): + return + + +class WorkerMeta: + keys = [ + "WORLD_SIZE", "RANK", "LOCAL_WORLD_SIZE", "LOCAL_RANK", "MASTER_ADDR", "MASTER_PORT", "CUDA_VISIBLE_DEVICES" + ] + + def __init__(self, store) -> None: + self._store = store + + def to_dict(self): + return {f"_{key.lower()}": self._store.get(f"_{key.lower()}", None) for key in WorkerMeta.keys} + + +# we assume that in each WorkerGroup, there is a Master Worker +class Worker(WorkerHelper): + + def __new__(cls, *args, **kwargs): + instance = super().__new__(cls) + + # note that here we use int to distinguish + disable_worker_init = int(os.environ.get('DISABLE_WORKER_INIT', 0)) + if disable_worker_init: + return instance + + rank = os.environ.get("RANK", None) + worker_group_prefix = os.environ.get("WG_PREFIX", None) + + # when decorator @ray.remote applies, __new__ will be called while we don't want to apply _configure_before_init + if None not in [rank, worker_group_prefix] and 'ActorClass(' not in cls.__name__: + instance._configure_before_init(f"{worker_group_prefix}_register_center", int(rank)) + + return instance + + def _configure_before_init(self, register_center_name: str, rank: int): + assert isinstance(rank, int), f"rank must be int, instead of {type(rank)}" + + if rank == 0: + master_addr, master_port = self.get_availale_master_addr_port() + rank_zero_info = { + "MASTER_ADDR": master_addr, + "MASTER_PORT": master_port, + } + + if os.getenv("WG_BACKEND", None) == "ray": + from single_controller.base.register_center.ray import create_worker_group_register_center + self.register_center = create_worker_group_register_center(name=register_center_name, + info=rank_zero_info) + + os.environ.update(rank_zero_info) + + def __init__(self, cuda_visible_devices=None) -> None: + # construct a meta from envrionment variable. Note that the import must be inside the class because it is executed remotely + import os + world_size = int(os.environ['WORLD_SIZE']) + rank = int(os.environ['RANK']) + self._rank = rank + self._world_size = world_size + + master_addr = os.environ["MASTER_ADDR"] + master_port = os.environ["MASTER_PORT"] + + local_world_size = int(os.getenv("LOCAL_WORLD_SIZE", "1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + + store = { + '_world_size': world_size, + '_rank': rank, + '_local_world_size': local_world_size, + '_local_rank': local_rank, + '_master_addr': master_addr, + '_master_port': master_port + } + if cuda_visible_devices is not None: + store['_cuda_visible_devices'] = cuda_visible_devices + + meta = WorkerMeta(store=store) + self._configure_with_meta(meta=meta) + + def _configure_with_meta(self, meta: WorkerMeta): + """ + This function should only be called inside by WorkerGroup + """ + assert isinstance(meta, WorkerMeta) + self.__dict__.update(meta.to_dict()) # this is hacky + # print(f"__dict__: {self.__dict__}") + for key in WorkerMeta.keys: + val = self.__dict__.get(f"_{key.lower()}", None) + if val is not None: + # print(f"set {key} to {val}") + os.environ[key] = str(val) + os.environ["REDIS_STORE_SERVER_HOST"] = str(self._master_addr).replace("[", "").replace( + "]", "") if self._master_addr else "" + + def get_master_addr_port(self): + return self._master_addr, self._master_port + + def get_cuda_visible_devices(self): + import os + cuda_visible_devices = os.environ.get("CUDA_VISIBLE_DEVICES", "not set") + return cuda_visible_devices + + @property + def world_size(self): + return self._world_size + + @property + def rank(self): + return self._rank + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO_WITH_FUNC) + def execute_with_func_generator(self, func, *args, **kwargs): + ret_proto = func(self, *args, **kwargs) + return ret_proto diff --git a/single_controller/base/worker_group.py b/single_controller/base/worker_group.py new file mode 100644 index 000000000..a6bc9279f --- /dev/null +++ b/single_controller/base/worker_group.py @@ -0,0 +1,196 @@ +# 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 class of WorkerGroup +""" +import logging +import threading +import signal +import time +from typing import List, Any, Callable, Dict + +from single_controller.base.decorator import MAGIC_ATTR, Dispatch, get_predefined_dispatch_fn, get_predefined_execute_fn + + +class ResourcePool: + + def __init__(self, process_on_nodes=None, max_collocate_count: int = 10, n_gpus_per_node=8) -> None: + if process_on_nodes is None: + process_on_nodes = [] + self._store = process_on_nodes + self.max_collocate_count = max_collocate_count + self.n_gpus_per_node = n_gpus_per_node # this is left for future huawei GPU that contains 16 GPUs per node + + def add_node(self, process_count): + self._store.append(process_count) + + @property + def world_size(self): + return sum(self._store) + + def __call__(self) -> Any: + return self._store + + @property + def store(self): + return self._store + + def local_world_size_list(self) -> List[int]: + nested_local_world_size_list = [ + [local_world_size for _ in range(local_world_size)] for local_world_size in self._store + ] + return [item for row in nested_local_world_size_list for item in row] + + def local_rank_list(self) -> List[int]: + nested_local_rank_list = [[i for i in range(local_world_size)] for local_world_size in self._store] + return [item for row in nested_local_rank_list for item in row] + + +class ClassWithInitArgs: + """ + This class stores a class constructor and the args/kwargs to construct the class. + It is used to instantiate the remote class. + """ + + def __init__(self, cls, *args, **kwargs) -> None: + self.cls = cls + self.args = args + self.kwargs = kwargs + + # def add_arg(self, arg): + # self.args += (arg,) + + # def add_kwarg(self, key, value): + # self.kwargs[key] = value + + def __call__(self) -> Any: + return self.cls(*self.args, **self.kwargs) + + +def check_workers_alive(workers: List, is_alive: Callable, gap_time: float = 1) -> None: + import time + while True: + for worker in workers: + if not is_alive(worker): + logging.warning(f"worker {worker} is not alive" + " sending signal to main thread") + signal.raise_signal(signal.SIGABRT) + time.sleep(gap_time) + + +class WorkerGroup: + + def __init__(self, resource_pool: ResourcePool, **kwargs) -> None: + self._is_init_with_detached_workers = True if resource_pool is None else False + + if resource_pool is not None: + # handle the case when WorkGroup is attached to an existing one + self._procecss_dispatch_config = resource_pool() + else: + self._procecss_dispatch_config = None + + self._workers = [] + self._worker_names = [] + + self._master_addr = None + self._master_port = None + + self._checker_thread: threading.Thread = None + + def _is_worker_alive(self, worker): + raise NotImplementedError(f"WorkerGroup._is_worker_alive called, should be implemented in derived class.") + + def _block_until_all_workers_alive(self) -> None: + while True: + all_state = [self._is_worker_alive(worker) for worker in self._workers] + if False in all_state: + time.sleep(1) + else: + break + + def start_worker_aliveness_check(self, every_n_seconds=1) -> None: + # before starting checking worker aliveness, make sure all workers are already alive + self._block_until_all_workers_alive() + + self._checker_thread = threading.Thread(target=check_workers_alive, + args=(self._workers, self._is_worker_alive, every_n_seconds)) + self._checker_thread.start() + + @property + def world_size(self): + return len(self._workers) + + # execute_all_async and execute_rank_zero_async should be implemented by RayWorkerGroup, TorchRPCWorkerGroup, + # MegatronWorkerGroup, XperfWorkerGroup should skip + + def _bind_worker_method(self, user_defined_cls, func_generator): + """ + Bind the worker method to the WorkerGroup + """ + + for method_name in dir(user_defined_cls): + + try: + method = getattr(user_defined_cls, method_name) + assert callable(method), f"{method_name} in {user_defined_cls} is not callable" + except Exception as e: + # if it is a property, it will fail because Class doesn't have instance property + continue + + if hasattr(method, MAGIC_ATTR): + # this method is decorated by register + attribute = getattr(method, MAGIC_ATTR) + assert isinstance(attribute, Dict), f'attribute must be a dictionary. Got {type(attribute)}' + assert 'dispatch_mode' in attribute, f'attribute must contain dispatch_mode in its key' + + dispatch_mode = attribute['dispatch_mode'] + execute_mode = attribute['execute_mode'] + blocking = attribute['blocking'] + + # get dispatch fn + if isinstance(dispatch_mode, Dispatch): + # get default dispatch fn + fn = get_predefined_dispatch_fn(dispatch_mode=dispatch_mode) + dispatch_fn = fn['dispatch_fn'] + collect_fn = fn['collect_fn'] + else: + assert isinstance(dispatch_mode, dict) + assert 'dispatch_fn' in dispatch_mode + assert 'collect_fn' in dispatch_mode + dispatch_fn = dispatch_mode['dispatch_fn'] + collect_fn = dispatch_mode['collect_fn'] + + # 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 as e: + print(f'execute_fn {wg_execute_fn_name} is invalid') + raise + + # 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) + except Exception as e: + raise ValueError(f'Fail to set method_name {method_name}') diff --git a/single_controller/ray/__init__.py b/single_controller/ray/__init__.py new file mode 100644 index 000000000..4d5783448 --- /dev/null +++ b/single_controller/ray/__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 .base import RayResourcePool, RayClassWithInitArgs, RayWorkerGroup, create_colocated_worker_cls +from .megatron import (MegatronRayWorkerGroup, DistRankInfo, DistGlobalInfo) \ No newline at end of file diff --git a/single_controller/ray/base.py b/single_controller/ray/base.py new file mode 100644 index 000000000..f87acd23d --- /dev/null +++ b/single_controller/ray/base.py @@ -0,0 +1,455 @@ +# 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 time +from typing import Dict, List, Any, Tuple + +import ray +from ray.util import list_named_actors +from ray.util.placement_group import placement_group, PlacementGroup +from ray.util.scheduling_strategies import PlacementGroupSchedulingStrategy, NodeAffinitySchedulingStrategy +from ray.experimental.state.api import get_actor + +from single_controller.base import WorkerGroup, ResourcePool, ClassWithInitArgs, Worker + +__all__ = ['Worker'] + + +def get_random_string(length: int) -> str: + import random + import string + letters_digits = string.ascii_letters + string.digits + return ''.join(random.choice(letters_digits) for _ in range(length)) + + +def func_generator(self, method_name, dispatch_fn, collect_fn, execute_fn, blocking): + + def func(*args, **kwargs): + args, kwargs = dispatch_fn(self, *args, **kwargs) + output = execute_fn(method_name, *args, **kwargs) + if blocking: + output = ray.get(output) + output = collect_fn(self, output) + return output + + return func + + +class RayResourcePool(ResourcePool): + + def __init__(self, + process_on_nodes: List[int] = None, + use_gpu: bool = True, + name_prefix: str = "", + max_colocate_count: int = 5, + detached=False) -> None: + super().__init__(process_on_nodes, max_colocate_count) + self.use_gpu = use_gpu + # print(f"in RayProcessDispatchConfiguration: name_prefix = {name_prefix}") + self.name_prefix = name_prefix + self.pgs = None + self.detached = detached + + def get_placement_groups(self, strategy="STRICT_PACK", name=None): + if self.pgs is not None: + return self.pgs + + pg_name_prefix = name if name else \ + f"{self.name_prefix}verl_group_{'_'.join([str(count) for count in self._store])}:" + # print(f"pg_name_prefix = {pg_name_prefix}") + pg_scheme = [[{ + "CPU": self.max_collocate_count, + "GPU": 1 + } if self.use_gpu else { + "CPU": self.max_collocate_count + } for _ in range(process_count)] for process_count in self._store] + + lifetime = 'detached' if self.detached else None + + pgs = [ + placement_group(bundles=bundles, strategy=strategy, name=pg_name_prefix + str(idx), lifetime=lifetime) + for idx, bundles in enumerate(pg_scheme) + ] + + ray.get([pg.ready() for pg in pgs]) + + self.pgs = pgs + return pgs + + +def extract_pg_from_exist(resource_pools: Dict[str, RayResourcePool], src_role_names: List[str], + resource_pool: RayResourcePool) -> List: + + src_pgs = [ + pg for role_name, resource_pool in resource_pools.items() for pg in resource_pool.get_placement_groups() + if role_name in src_role_names + ] + + sorted_src_pgs = sorted(src_pgs, key=lambda pg: pg.bundle_count, reverse=True) + sorted_process_on_nodes = sorted([(val, idx) for idx, val in enumerate(resource_pool.store)], reverse=True) + + unsorted_pgs: List[Tuple[int, PlacementGroup]] = [] + searching_idx = 0 + for request_process, original_idx in sorted_process_on_nodes: + assert searching_idx < len(sorted_src_pgs), f"no enough nodes for request: searching {searching_idx} th node" + assert request_process <= sorted_src_pgs[searching_idx].bundle_count, \ + f"requesting {request_process} processes, bundle count cannot satisfy" + unsorted_pgs.append((original_idx, sorted_src_pgs[searching_idx])) + searching_idx += 1 + + return [pg for _, pg in sorted(unsorted_pgs)] + + +def merge_resource_pool(rp1: RayResourcePool, rp2: RayResourcePool) -> RayResourcePool: + assert rp1.use_gpu == rp2.use_gpu, 'Both RayResourcePool must either use_gpu or not' + assert rp1.max_collocate_count == rp2.max_collocate_count, 'Both RayResourcePool must has the same max_collocate_count' + assert rp1.n_gpus_per_node == rp2.n_gpus_per_node, 'Both RayResourcePool must has the same n_gpus_per_node' + assert rp1.detached == rp2.detached, 'Detached ResourcePool cannot be merged with non-detached ResourcePool' + + new_store = rp1.store + rp2.store + + merged = RayResourcePool(new_store, rp1.use_gpu, f"{rp1.name_prefix}_{rp2.name_prefix}") + merged.pgs = rp1.get_placement_groups() + rp2.get_placement_groups() + + return merged + + +class RayClassWithInitArgs(ClassWithInitArgs): + + def __init__(self, cls, *args, **kwargs) -> None: + # self._options = kwargs.pop('options', dict()) + super().__init__(cls, *args, **kwargs) + self._options = {} + self._additional_resource = {} + + def set_additional_resource(self, additional_resource): + self._additional_resource = additional_resource + + def update_options(self, options: Dict): + self._options.update(options) + + def __call__(self, + placement_group, + placement_group_bundle_idx, + use_gpu: bool = True, + num_gpus=1, + sharing_with=None) -> Any: + if sharing_with is not None: + target_node_id = ray.get(sharing_with.get_node_id.remote()) + cuda_visible_devices = ray.get(sharing_with.get_cuda_visible_devices.remote()) + options = {"scheduling_strategy": NodeAffinitySchedulingStrategy(node_id=target_node_id, soft=False)} + return self.cls.options(**options).remote(*self.args, + cuda_visible_devices=cuda_visible_devices, + **self.kwargs) + + options = { + "scheduling_strategy": + PlacementGroupSchedulingStrategy(placement_group=placement_group, + placement_group_bundle_index=placement_group_bundle_idx) + } + options.update(self._options) + + if use_gpu: + options["num_gpus"] = num_gpus + + if len(self._additional_resource) > 1: + for k, v in self._additional_resource.items(): + options[k] = v + + # print("cls:", self.cls) + # print("args: ", self.args) + # print("kwargs: ", self.kwargs) + return self.cls.options(**options).remote(*self.args, **self.kwargs) + + +class RayWorkerGroup(WorkerGroup): + + def __init__(self, + resource_pool: RayResourcePool = None, + ray_cls_with_init: RayClassWithInitArgs = None, + bin_pack: bool = True, + name_prefix: str = None, + detached=False, + worker_names=None, + **kwargs) -> None: + super().__init__(resource_pool=resource_pool, **kwargs) + self.ray_cls_with_init = ray_cls_with_init + self.name_prefix = get_random_string(length=6) if name_prefix is None else name_prefix + + if self._is_init_with_detached_workers: + self._init_with_detached_workers(worker_names=worker_names) + else: + self._init_with_resource_pool(resource_pool=resource_pool, + ray_cls_with_init=ray_cls_with_init, + bin_pack=bin_pack, + detached=detached) + + if ray_cls_with_init is not None: + self._bind_worker_method(self.ray_cls_with_init.cls, func_generator) + + def _is_worker_alive(self, worker: ray.actor.ActorHandle): + worker_state_dict = get_actor(worker._actor_id.hex()) + return worker_state_dict.get("state", "undefined") == "ALIVE" if worker_state_dict is not None else False + + def _init_with_detached_workers(self, worker_names): + workers = [ray.get_actor(name=name) for name in worker_names] + self._workers = workers + self._worker_names = worker_names + self._world_size = len(worker_names) + + def _init_with_resource_pool(self, resource_pool, ray_cls_with_init, bin_pack, detached): + use_gpu = resource_pool.use_gpu + + strategy = "PACK" + if bin_pack: + strategy = "STRICT_PACK" + pgs = resource_pool.get_placement_groups(strategy=strategy) + world_size = resource_pool.world_size + self._world_size = world_size + # cia.add_kwarg("_world_size", world_size) + num_gpus = 1 / resource_pool.max_collocate_count + + rank = -1 + for pg_idx, local_world_size in enumerate(resource_pool.store): + pg = pgs[pg_idx] + assert local_world_size <= pg.bundle_count, \ + f"when generating for {self.name_prefix}, for the " + for local_rank in range(local_world_size): + rank += 1 + + # we pass in environment variable at option so that Worker can use environment variable to set + env_vars = { + 'WORLD_SIZE': str(world_size), + 'RANK': str(rank), + 'WG_PREFIX': self.name_prefix, + 'WG_BACKEND': 'ray', + 'RAY_LOCAL_WORLD_SIZE': str(local_world_size), + 'RAY_LOCAL_RANK': str(local_rank), + } + if rank != 0: + env_vars['MASTER_ADDR'] = self._master_addr + env_vars['MASTER_PORT'] = self._master_port + + import re + cia_name = type(ray_cls_with_init.cls).__name__ + match = re.search(r"ActorClass\(([^)]+)\)", cia_name) # ray.remote(Obj) -> "ActorClass(Obj)" + cia_name = match.group(1) if match else cia_name # "ActorClass(Obj)" -> "Obj" + name = f"{self.name_prefix}{cia_name}_{pg_idx}:{local_rank}" # e.g. Worker_2:5 + + ray_cls_with_init.update_options({'runtime_env': {'env_vars': env_vars}, 'name': name}) + + if detached: + ray_cls_with_init.update_options({'lifetime': 'detached'}) + + # create a worker + worker = ray_cls_with_init(placement_group=pg, + placement_group_bundle_idx=local_rank, + use_gpu=use_gpu, + num_gpus=num_gpus) + self._workers.append(worker) + self._worker_names.append(name) + + if rank == 0: + register_center_actor = None + for _ in range(120): + if f"{self.name_prefix}_register_center" not in list_named_actors(): + time.sleep(1) + else: + register_center_actor = ray.get_actor(f"{self.name_prefix}_register_center") + assert register_center_actor is not None, f"failed to get register_center_actor: {self.name_prefix}_register_center in {list_named_actors(all_namespaces=True)}" + rank_zero_info = ray.get(register_center_actor.get_rank_zero_info.remote()) + self._master_addr, self._master_port = rank_zero_info['MASTER_ADDR'], rank_zero_info['MASTER_PORT'] + # print(f"rank_zero_info: {rank_zero_info}") + # print(f"master_addr: {self._master_addr}, master_port: {self._master_port}") + + @property + def worker_names(self): + return self._worker_names + + @classmethod + def from_detached(cls, worker_names=None, ray_cls_with_init=None): + worker_group = cls(resource_pool=None, + ray_cls_with_init=ray_cls_with_init, + name_prefix=None, + worker_names=worker_names) + return worker_group + + def spawn(self, prefix_set): + """ + spawn to a dictionary of worker groups, each with a subset of method with prefix. + + """ + + def _rebind_actor_methods(worker_group, actor_name): + """ + bind the method with actor_prefix to its original name + """ + prefix: str = actor_name + '_' + for method_name in dir(worker_group): + if method_name.startswith(prefix): + # only valid when Python >= 3.9 + original_method_name = method_name.removeprefix(prefix) + method = getattr(worker_group, method_name) + setattr(worker_group, original_method_name, method) + + new_worker_group_dict = {} + for prefix in prefix_set: + new_worker_group = self.from_detached(worker_names=self._worker_names, + ray_cls_with_init=self.ray_cls_with_init) + + _rebind_actor_methods(new_worker_group, prefix) + new_worker_group_dict[prefix] = new_worker_group + return new_worker_group_dict + + def execute_rank_zero_sync(self, method_name: str, *args, **kwargs): + return ray.get(self.execute_all_async(method_name, **args, **kwargs)) + + def execute_rank_zero_async(self, method_name: str, *args, **kwargs): + remote_call = getattr(self._workers[0], method_name) + return remote_call.remote(*args, **kwargs) + + def execute_rank_zero(self, method_name: str, *args, **kwargs): + return self.execute_rank_zero_async(method_name, *args, **kwargs) + + def execute_all(self, method_name: str, *args, **kwargs): + return self.execute_all_async(method_name, *args, **kwargs) + + def execute_all_sync(self, method_name: str, *args, **kwargs): + return ray.get(self.execute_all_async(method_name, *args, **kwargs)) + + def execute_all_async(self, method_name: str, *args, **kwargs): + # 这里我们假设,如果 args 和 kwargs 里面所有的参数都是 list,且所有的 list 长度都与 len(self._workers) 一致的话,我们会把 + # list 中的每一个分别发到对应的 worker 上去 + # print(f"execute_all_async: method {method_name}({args}, {kwargs})") + length = len(self._workers) + if all(isinstance(arg, list) for arg in args) and all(isinstance(kwarg, list) for kwarg in kwargs.values()): + if all(len(arg) == length for arg in args) and all(len(kwarg) == length for kwarg in kwargs.values()): + # print(f"splitting args and kwargs into {length} shards") + result = [] + for i in range(length): + sliced_args = tuple(arg[i] for arg in args) + sliced_kwargs = {k: v[i] for k, v in kwargs.items()} + remote_call = getattr(self._workers[i], method_name) + result.append(remote_call.remote(*sliced_args, **sliced_kwargs)) + return result + + return [getattr(worker, method_name).remote(*args, **kwargs) for worker in self._workers] + + @property + def master_address(self): + return self._master_addr + + @property + def master_port(self): + return self._master_port + + @property + def workers(self): + return self._workers + + @property + def world_size(self): + return self._world_size + + +""" +Utilities that enables creating workers inside the same ray.Actor, +with code written in separate ray.Actors. +""" + +from unittest.mock import patch +from single_controller.base.decorator import MAGIC_ATTR +import os + + +def _bind_workers_method_to_parent(cls, key, user_defined_cls): + """ + Binds the methods of each worker to the WorkerDict. + Note that we only bind public methods that are decorated by register + """ + for method_name in dir(user_defined_cls): + try: + method = getattr(user_defined_cls, method_name) + assert callable(method), f"{method_name} in {user_defined_cls} is not callable" + except Exception as e: + # if it is a property, it will fail because Class doesn't have instance property + continue + + if hasattr(method, MAGIC_ATTR): + + def generate_function(name): + + def func(self, *args, **kwargs): + # dispatch to the actual worker + return getattr(self.worker_dict[key], name)(*args, **kwargs) + + return func + + func = generate_function(method_name) + # pass MAGIC_ATTR for outer worker group + setattr(func, MAGIC_ATTR, getattr(method, MAGIC_ATTR)) + try: + method_name_with_prefix = key + '_' + method_name + setattr(cls, method_name_with_prefix, func) + # print(f'Binding {method_name_with_prefix}') + except Exception as e: + raise ValueError(f'Fail to set method_name {method_name}') + + +def _unwrap_ray_remote(cls): + if hasattr(cls, '__ray_actor_class__'): + cls = cls.__ray_actor_class__ + return cls + + +def create_colocated_worker_cls(class_dict: dict[str, RayClassWithInitArgs]): + """ + This function should return a class instance that delegates the calls to every + cls in cls_dict + """ + cls_dict = {} + init_args_dict = {} + worker_cls = None + for key, cls in class_dict.items(): + if worker_cls == None: + worker_cls = cls.cls.__ray_actor_class__.__base__ + else: + assert worker_cls == cls.cls.__ray_actor_class__.__base__, \ + 'the worker class should be the same when share the same process' + cls_dict[key] = cls.cls + init_args_dict[key] = {'args': cls.args, 'kwargs': cls.kwargs} + + assert cls_dict.keys() == init_args_dict.keys() + + # TODO: create a class with customizable name + class WorkerDict(worker_cls): + + def __init__(self): + super().__init__() + self.worker_dict = {} + for key, user_defined_cls in cls_dict.items(): + user_defined_cls = _unwrap_ray_remote(user_defined_cls) + # directly instantiate the class without remote + with patch.dict(os.environ, {'DISABLE_WORKER_INIT': '1'}): + self.worker_dict[key] = user_defined_cls(*init_args_dict[key].get('args', ()), + **init_args_dict[key].get('kwargs', {})) + + # now monkey-patch the methods from inner class to WorkerDict + for key, user_defined_cls in cls_dict.items(): + user_defined_cls = _unwrap_ray_remote(user_defined_cls) + _bind_workers_method_to_parent(WorkerDict, key, user_defined_cls) + + remote_cls = ray.remote(WorkerDict) + remote_cls = RayClassWithInitArgs(cls=remote_cls) + return remote_cls diff --git a/single_controller/ray/decorator.py b/single_controller/ray/decorator.py new file mode 100644 index 000000000..006a80cf8 --- /dev/null +++ b/single_controller/ray/decorator.py @@ -0,0 +1,65 @@ +# 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 json +import os + +import ray + +# compatiblity cern +from single_controller.base.decorator import * + + +def maybe_remote(main): + """Schedule main function as ray remote task if VERL_DRIVER_NUM_GPUS or VERL_DRIVER_RESOURCES specified in config. + - VERL_DRIVER_NUM_GPUS: number of GPUs for driver task. + - VERL_DRIVER_RESOURCES: custom resources for driver task, e.g {"verl_driver": 1.0}. + + For job submission to ray cluster, you can specify these two envs in runtime.yaml. + ```yaml + working_dir: "." + env_vars: + VERL_DRIVER_NUM_GPUS: "1" + VERL_DRIVER_RESOURCES: '{"verl_driver": 1.0}' + ``` + + ray job submit --runtime-env=runtime.yaml -- python3 test.py + + Args: + main (Callable): main function to be schedule. + """ + + num_gpus = 0 + resources = {} + env_num_gpus = os.getenv("VERL_DRIVER_NUM_GPUS") + if env_num_gpus: + num_gpus = int(env_num_gpus) + env_resources = os.getenv("VERL_DRIVER_RESOURCES") + if env_resources: + resources = json.loads(env_resources) + print(f"verl driver num_gpus: {num_gpus}, resources={resources}") + assert isinstance(resources, dict), f"resources must be dict, got {type(resources)}" + + @functools.wraps(main) + def _main(*args, **kwargs): + # Run main function locally. + if num_gpus == 0 and len(resources) == 0: + return main(*args, **kwargs) + + # Run main function remotely as ray task. + f = ray.remote(num_gpus=num_gpus, resources=resources)(main) + return ray.get(f.remote(*args, **kwargs)) + + return _main diff --git a/single_controller/ray/dist_data_pass_protocol.py b/single_controller/ray/dist_data_pass_protocol.py new file mode 100644 index 000000000..59202b139 --- /dev/null +++ b/single_controller/ray/dist_data_pass_protocol.py @@ -0,0 +1,23 @@ +# 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 tensordict import TensorDict +import ray + +from verl import DataProto + + +class DistDataProto(DataProto, ray.ObjectRef): + ... + # skip for prototype, assuming dp size kept among all Roles diff --git a/single_controller/ray/dp.py b/single_controller/ray/dp.py new file mode 100644 index 000000000..fab4da9db --- /dev/null +++ b/single_controller/ray/dp.py @@ -0,0 +1,93 @@ +# 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 ray + +from single_controller.ray.base import RayWorkerGroup, RayResourcePool, RayClassWithInitArgs + + +@ray.remote +class RefBasicRayActor: + ... + + +class DPEngineRayWorkerGroup(RayWorkerGroup): + + class DummyModule: + + def __init__(self, core, methods_names) -> None: + self.core = core + + def func_generator(method_name): + + def func(*args, **kwargs): + return self.core.execute_all_async("execute_engine", method_name, *args, **kwargs) + + return func + + for method_name in methods_names: + setattr(self, method_name, func_generator(method_name)) + + def __init__(self, name_prefix, process_dispatch_scheme, use_gpu, engine_type, *args, **kwargs) -> None: + from torch import nn + # print(f"in DataParallelEngineWrapper, name_prefix = {name_prefix}") + if isinstance(process_dispatch_scheme, RayResourcePool): + rpdc = process_dispatch_scheme + else: + rpdc = RayResourcePool(process_on_nodes=process_dispatch_scheme, + use_gpu=use_gpu, + name_prefix=name_prefix, + max_colocate_count=1) + rcia = RayClassWithInitArgs(cls=engine_type, *args, **kwargs) + + self._engine_type = engine_type + + super().__init__(rpdc, rcia) + + nn_module_methods = [ + method_name for method_name in dir(nn.Module) + if callable(getattr(nn.Module, method_name)) and not method_name.startswith("__") + ] + nn_module_methods += ["__call__"] + + def func_generator(method_name): + + def func(*args, **kwargs): + return self.execute_all_async(method_name, *args, **kwargs) + + return func + + print(f"{engine_type} has methods: {dir(engine_type)}") + for method_name in dir(engine_type): + try: + is_callable = callable(getattr(engine_type, method_name)) + except Exception as _: + pass + else: + if is_callable and method_name not in dir(RefBasicRayActor): + print(f"register method: {method_name}") + setattr(self, method_name, func_generator(method_name)) + + self.module = DPEngineRayWorkerGroup.DummyModule(self, nn_module_methods) + + @property + def engine(self): + return self.module + + def get_model_size_on_rank_zero(self): + results = ray.get([worker.get_model_size_on_rank_zero.remote() for worker in self._workers]) + + for result in results: + if result is not None: + return result diff --git a/single_controller/ray/megatron.py b/single_controller/ray/megatron.py new file mode 100644 index 000000000..3aad741c4 --- /dev/null +++ b/single_controller/ray/megatron.py @@ -0,0 +1,62 @@ +# 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 Dict, Optional + +import ray + +from .base import RayWorkerGroup, RayResourcePool, RayClassWithInitArgs +from single_controller.base.megatron.worker import DistRankInfo, DistGlobalInfo +from single_controller.base.megatron.worker_group import MegatronWorkerGroup + + +# NOTE(sgm): for opensource megatron-core +class NVMegatronRayWorkerGroup(RayWorkerGroup, MegatronWorkerGroup): + """ + MegatronWorkerGroup will query each worker of its megatron rank info and store it inside the WorkerGroup + so that the dispatcher can use it to dispatch data. + """ + + def __init__(self, resource_pool: RayResourcePool, ray_cls_with_init: RayClassWithInitArgs, **kwargs): + super().__init__(resource_pool=resource_pool, ray_cls_with_init=ray_cls_with_init, **kwargs) + self._megatron_rank_info: DistRankInfo = self.execute_all_sync(method_name='get_megatron_rank_info') + self._megatron_global_info: DistGlobalInfo = ray.get( + self.execute_rank_zero_async(method_name='get_megatron_global_info')) + + +class MegatronRayWorkerGroup(RayWorkerGroup, MegatronWorkerGroup): + """ + MegatronWorkerGroup will query each worker of its megatron rank info and store it inside the WorkerGroup + so that the dispatcher can use it to dispatch data. + """ + + def __init__(self, + resource_pool: RayResourcePool, + ray_cls_with_init: RayClassWithInitArgs, + default_megatron_kwargs: Dict = None, + **kwargs): + super().__init__(resource_pool=resource_pool, + ray_cls_with_init=ray_cls_with_init, + default_megatron_kwargs=default_megatron_kwargs, + **kwargs) + self.init_megatron(default_megatron_kwargs=default_megatron_kwargs) + self._megatron_rank_info: DistRankInfo = self.execute_all_sync(method_name='get_megatron_rank_info') + self._megatron_global_info: DistGlobalInfo = ray.get( + self.execute_rank_zero_async(method_name='get_megatron_global_info')) + + def init_megatron(self, default_megatron_kwargs: Optional[Dict] = None): + # after super, we will call init of each worker + if not self._is_init_with_detached_workers: + # only init_megatron if the WorkerGroup is created from scratch + self.execute_all_sync(method_name='init_megatron', default_megatron_kwargs=default_megatron_kwargs) diff --git a/single_controller/version/version b/single_controller/version/version new file mode 100644 index 000000000..7bcd0e361 --- /dev/null +++ b/single_controller/version/version @@ -0,0 +1 @@ +0.0.2 \ No newline at end of file diff --git a/verl/__init__.py b/verl/__init__.py new file mode 100644 index 000000000..f06871776 --- /dev/null +++ b/verl/__init__.py @@ -0,0 +1,27 @@ +# 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 + +version_folder = os.path.dirname(os.path.join(os.path.abspath(__file__))) + +with open(os.path.join(version_folder, 'version/version')) as f: + __version__ = f.read().strip() + +from .protocol import DataProto + +from .utils.logging_utils import set_basic_config +import logging + +set_basic_config(level=logging.WARNING) diff --git a/verl/models/README.md b/verl/models/README.md new file mode 100644 index 000000000..677b92f38 --- /dev/null +++ b/verl/models/README.md @@ -0,0 +1,35 @@ +# Models +Common modelzoo such as huggingface/transformers stuggles when using Pytorch native model parallelism. Following the design principle of vLLM, we keep a simple, parallelizable, highly-optimized with packed inputs in verl. +## Adding a New Huggingface Model +### Step 1: Copy the model file from HF to verl +- Add a new file under verl/models/hf +- Copy ONLY the model file from huggingface/transformers/models to verl/models/hf + +### Step 2: Modify the model file to use packed inputs +- Remove all the code related to inference (kv cache) +- Modify the inputs to include only + - input_ids (total_nnz,) + - cu_seqlens (total_nnz + 1,) + - max_seqlen_in_batch: int +- Note that this requires using flash attention with causal mask. + +### Step 2.5: Add tests +- Add a test to compare this version and the huggingface version +- Following the infrastructure and add tests to tests/models/hf + +### Step 3: Add a function to apply tensor parallelism +- Please follow + - https://pytorch.org/docs/stable/distributed.tensor.parallel.html + - https://pytorch.org/tutorials/intermediate/TP_tutorial.html +- General comments + - Tensor Parallelism in native Pytorch is NOT auto-parallelism. The way it works is to specify how model parameters and input/output reshards using configs. These configs are then registered as hooks to perform input/output resharding before/after model forward. + +### Step 4: Add a function to apply data parallelism +- Please use FSDP2 APIs +- See demo here https://github.com/pytorch/torchtitan/blob/main/torchtitan/parallelisms/parallelize_llama.py#L413 + +### Step 5: Add a function to apply pipeline parallelism +- Comes in Pytorch 2.4 +- Currently only in alpha in nightly version +- Check torchtitan for more details + diff --git a/verl/models/__init__.py b/verl/models/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/models/__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/models/llama/megatron/__init__.py b/verl/models/llama/megatron/__init__.py new file mode 100644 index 000000000..b188b3ee6 --- /dev/null +++ b/verl/models/llama/megatron/__init__.py @@ -0,0 +1,24 @@ +# 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 .modeling_llama_megatron import ( + # original model with megatron + ParallelLlamaModel, + ParallelLlamaForCausalLM, + # rmpad with megatron + ParallelLlamaForCausalLMRmPad, + ParallelLlamaForValueRmPad, + # rmpad with megatron and pipeline parallelism + ParallelLlamaForCausalLMRmPadPP, + ParallelLlamaForValueRmPadPP) diff --git a/verl/models/llama/megatron/checkpoint_utils/__init__.py b/verl/models/llama/megatron/checkpoint_utils/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/models/llama/megatron/checkpoint_utils/__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/models/llama/megatron/checkpoint_utils/llama_loader.py b/verl/models/llama/megatron/checkpoint_utils/llama_loader.py new file mode 100644 index 000000000..00fb0a9c6 --- /dev/null +++ b/verl/models/llama/megatron/checkpoint_utils/llama_loader.py @@ -0,0 +1,446 @@ +# 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 +import time +from typing import Dict, Any, Callable, Optional +import torch.distributed as dist + + +def _megatron_calc_layer_map(config): + """Calculate the mapping of global layer_idx to local layer_idx + Returns: + layer_map (Dict: int -> tuple(int, int, int)): + mapping from the global layer index to + a tuple of (pp_rank, virtual_pp_rank, layer_idx inside model) + """ + import megatron + from megatron.core import mpu + + pp_size = mpu.get_pipeline_model_parallel_world_size() + virtual_pp_size = mpu.get_virtual_pipeline_model_parallel_world_size() or 1 + + layer_map = dict() + num_layers_per_model = config.num_hidden_layers // pp_size // virtual_pp_size + assert num_layers_per_model * pp_size * virtual_pp_size == config.num_hidden_layers + + for pp_rank_idx in range(pp_size): + for virtual_pp_rank_idx in range(virtual_pp_size): + layer_offset = (virtual_pp_rank_idx * (config.num_hidden_layers // virtual_pp_size) + + pp_rank_idx * num_layers_per_model) + for layer_idx in range(num_layers_per_model): + layer_map[layer_offset + layer_idx] = ( + pp_rank_idx, + virtual_pp_rank_idx, + layer_idx, + ) + return layer_map + + +def load_state_dict_to_megatron_llama(state_dict, wrapped_models, config, params_dtype, is_value_model=False): + """Load merged state_dict to sharded Megatron module in training. + """ + import megatron + from megatron.core import mpu + from megatron.utils import print_rank_0, unwrap_model + from megatron.core.transformer.module import Float16Module + from megatron.core import DistributedDataParallel as LocalDDP + from torch.nn.parallel import DistributedDataParallel as torchDDP + + start_time = time.time() + + def _get_gpt_model(model): + return model + + def broadcast_params(module): + for param in module.parameters(): + torch.distributed.broadcast(param.data, + src=mpu.get_data_parallel_src_rank(), + group=mpu.get_data_parallel_group()) + + dp_rank = mpu.get_data_parallel_rank() + pp_rank = mpu.get_pipeline_model_parallel_rank() + pp_size = mpu.get_pipeline_model_parallel_world_size() + virtual_pp_size = mpu.get_virtual_pipeline_model_parallel_world_size() or 1 + mp_group = mpu.get_model_parallel_group() + + if torch.distributed.get_rank() == 0: + assert mp_group.rank() == 0, f"mp_rank:[{mp_group.rank}] != 0 on rank #0" + assert pp_rank == 0, f"pp_rank:[{pp_rank}] != 0 on rank #0" + assert dp_rank == 0, f"dp_rank:[{dp_rank}] != 0 on rank #0" + + if not isinstance(wrapped_models, (list, tuple)): + wrapped_models = list(wrapped_models) + + assert len(wrapped_models) == virtual_pp_size + num_layers_per_model = config.num_hidden_layers // pp_size // virtual_pp_size + assert num_layers_per_model * pp_size * virtual_pp_size == config.num_hidden_layers + + models = [None] * len(wrapped_models) + + for i, wrapped_model in enumerate(wrapped_models): + models[i] = unwrap_model(wrapped_model, (torchDDP, LocalDDP, Float16Module)) + gpt_model_module = _get_gpt_model(models[i]) + assert len(gpt_model_module.model.layers) == num_layers_per_model + + def _broadcast_tensor(tensor, name) -> torch.Tensor: + """broadcast tensor from rank0 across mp_group""" + nonlocal state_dict + nonlocal mp_group + if torch.distributed.get_rank() == 0: + if name in state_dict: + weight = state_dict[name] + tensor_shape = weight.shape + else: + tensor_shape = None + else: + weight = None + tensor_shape = None + + obj_list = [tensor_shape] + dist.broadcast_object_list(obj_list, src=0, group=mp_group) + tensor_shape = obj_list[0] + + if tensor_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tensor:[{name}] not in state_dict, skip load") + return + + if tensor is None: + tensor = torch.empty( + tensor_shape, + dtype=params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + if torch.distributed.get_rank() == 0: + tensor.data.copy_(weight) + dist.broadcast(tensor, src=0, group=mp_group) + + def _broadcast_tp_shard_tensor_vocab(tensor, name, chunk_dim=0, mutate_func=None) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + + if torch.distributed.get_rank() == 0: + if name in state_dict: + full_weight = state_dict[name] + + if mutate_func is not None: + full_weight = mutate_func(full_weight) + tensor_chunk = torch.chunk(full_weight, tp_size, dim=chunk_dim) + chunk_shape = tensor_chunk[0].shape + else: + chunk_shape = None + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=0, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{name}] not in state_dict, skip loading") + return + + if tensor is None: + sync_tensor = torch.empty( + chunk_shape, + dtype=params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + else: + assert (tensor.shape == chunk_shape + ), f"rank #{torch.distributed.get_rank()} tensor {name} shape {tensor.shape} != {chunk_shape}" + sync_tensor = torch.empty_like(tensor, device=torch.cuda.current_device(), requires_grad=False) + + for i in range(tp_size): + if torch.distributed.get_rank() == 0: + sync_tensor.data.copy_(tensor_chunk[i]) + dist.broadcast(sync_tensor, src=0, group=mp_group) + if (i == tp_rank) and (tensor is not None): + tensor.data.copy_(sync_tensor) + + def _broadcast_tp_shard_tensor(tensor, name, chunk_dim=0, mutate_func=None) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + + if torch.distributed.get_rank() == 0: + if name in state_dict: + full_weight = state_dict[name] + if mutate_func is not None: + full_weight = mutate_func(full_weight) + tensor_chunk = torch.chunk(full_weight, tp_size, dim=chunk_dim) + chunk_shape = tensor_chunk[0].shape + else: + chunk_shape = None + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=0, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{name}] not in state_dict, skip loading") + return + + if tensor is None: + sync_tensor = torch.empty( + chunk_shape, + dtype=params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + else: + assert (tensor.shape == chunk_shape + ), f"rank #{torch.distributed.get_rank()} tensor {name} shape {tensor.shape} != {chunk_shape}" + sync_tensor = torch.empty_like(tensor, device=torch.cuda.current_device(), requires_grad=False) + + for i in range(tp_size): + if torch.distributed.get_rank() == 0: + sync_tensor.data.copy_(tensor_chunk[i]) + dist.broadcast(sync_tensor, src=0, group=mp_group) + if (i == tp_rank) and (tensor is not None): + tensor.data.copy_(sync_tensor) + + def _broadcast_tp_shard_tensor_gate_up(tensor, gate_name, up_name) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + + if torch.distributed.get_rank() == 0: + gate_weight = state_dict[gate_name] + up_weight = state_dict[up_name] + new_gate_up_weight = torch.empty(config.intermediate_size * 2, + config.hidden_size, + dtype=params_dtype, + device=torch.cuda.current_device()) + for i in range(tp_size): + intermediate_size_tp = config.intermediate_size // tp_size + gate_weight_tp = gate_weight[i * intermediate_size_tp:(i + 1) * intermediate_size_tp] + up_weight_tp = up_weight[i * intermediate_size_tp:(i + 1) * intermediate_size_tp] + new_gate_up_weight[intermediate_size_tp * 2 * i:intermediate_size_tp * 2 * (i + 1)].copy_( + torch.cat([gate_weight_tp, up_weight_tp], dim=0)) + + tensor_chunk = torch.chunk(new_gate_up_weight, tp_size, dim=0) + chunk_shape = tensor_chunk[0].shape + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=0, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{gate_name, up_name}] not in state_dict, skip loading") + return + + if tensor is None: + sync_tensor = torch.empty( + chunk_shape, + dtype=params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + else: + assert ( + tensor.shape == chunk_shape + ), f"rank #{torch.distributed.get_rank() == 0:} tensor {gate_name, up_name} shape {tensor.shape} != {chunk_shape}" + sync_tensor = torch.empty_like(tensor, device=torch.cuda.current_device(), requires_grad=False) + + for i in range(tp_size): + if torch.distributed.get_rank() == 0: + sync_tensor.data.copy_(tensor_chunk[i]) + dist.broadcast(sync_tensor, src=0, group=mp_group) + if (i == tp_rank) and (tensor is not None): + tensor.data.copy_(sync_tensor) + + def _broadcast_tp_shard_tensor_qkv(tensor, q_name, k_name, v_name) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + + if torch.distributed.get_rank() == 0: + assert (q_name in state_dict and k_name in state_dict and v_name in state_dict) + full_weight_q = state_dict[q_name] + full_weight_k = state_dict[k_name] + full_weight_v = state_dict[v_name] + + hidden_size_per_head = config.hidden_size // config.num_attention_heads + + if config.num_key_value_heads >= tp_size: + q_size_tp = config.hidden_size // tp_size + kv_size_tp = hidden_size_per_head * config.num_key_value_heads // tp_size + total_size = q_size_tp + 2 * kv_size_tp + new_weight_qkv = torch.empty(total_size * tp_size, + config.hidden_size, + dtype=params_dtype, + device=torch.cuda.current_device()) + for i in range(tp_size): + q_part = full_weight_q[i * q_size_tp:(i + 1) * q_size_tp] + k_part = full_weight_k[i * kv_size_tp:(i + 1) * kv_size_tp] + v_part = full_weight_v[i * kv_size_tp:(i + 1) * kv_size_tp] + new_weight_qkv[i * total_size:(i + 1) * total_size].copy_(torch.cat([q_part, k_part, v_part], + dim=0)) + + else: + q_size_tp = config.hidden_size // tp_size + kv_size_tp = hidden_size_per_head + total_size = q_size_tp + 2 * kv_size_tp + new_weight_qkv = torch.empty(total_size * tp_size, + config.hidden_size, + dtype=params_dtype, + device=torch.cuda.current_device()) + for i in range(tp_size): + q_part = full_weight_q[i * q_size_tp:(i + 1) * q_size_tp] + start_idx = i * config.num_key_value_heads // tp_size * hidden_size_per_head + end_idx = (i * config.num_key_value_heads // tp_size + 1) * hidden_size_per_head + k_part = full_weight_k[start_idx:end_idx] + v_part = full_weight_v[start_idx:end_idx] + new_weight_qkv[i * total_size:(i + 1) * total_size].copy_(torch.cat([q_part, k_part, v_part], + dim=0)) + + tensor_chunk = torch.chunk(new_weight_qkv, tp_size, dim=0) + chunk_shape = tensor_chunk[0].shape + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=0, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{name}] not in state_dict, skip loading") + return + + if tensor is None: + sync_tensor = torch.empty( + chunk_shape, + dtype=params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + else: + assert (tensor.shape == chunk_shape + ), f"rank #{torch.distributed.get_rank()} tensor {q_name} shape {tensor.shape} != {chunk_shape}" + sync_tensor = torch.empty_like(tensor, device=torch.cuda.current_device(), requires_grad=False) + + for i in range(tp_size): + if torch.distributed.get_rank() == 0: + sync_tensor.data.copy_(tensor_chunk[i]) + dist.broadcast(sync_tensor, src=0, group=mp_group) + if (i == tp_rank) and (tensor is not None): + tensor.data.copy_(sync_tensor) + + if dp_rank == 0: + # Embeddings + # ------------------- + print_rank_0("loading embeddings...") + gpt_model_module = _get_gpt_model(models[0]) + embed_tokens_weight = None + if pp_rank == 0: + embed_tokens_weight = gpt_model_module.model.embed_tokens.weight + _broadcast_tp_shard_tensor_vocab(embed_tokens_weight, "model.embed_tokens.weight") + + # Transformer layers + # ------------------- + layer_map = _megatron_calc_layer_map(config) + + for layer in range(config.num_hidden_layers): + print_rank_0(f"loading layer #{layer}...") + layer_name = f"model.layers.{layer}" + dst_pp_rank, dst_virtual_pp_rank, dst_layer_idx = layer_map[layer] + + gpt_model_module = _get_gpt_model(models[dst_virtual_pp_rank]) + sync_layer = gpt_model_module.model.layers[dst_layer_idx] + + _broadcast_tensor( + sync_layer.input_layernorm.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.input_layernorm.weight", + ) + + _broadcast_tp_shard_tensor_qkv( + sync_layer.self_attn.qkv_proj.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.self_attn.q_proj.weight", + f"{layer_name}.self_attn.k_proj.weight", + f"{layer_name}.self_attn.v_proj.weight", + ) + + _broadcast_tp_shard_tensor( + sync_layer.self_attn.o_proj.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.self_attn.o_proj.weight", + chunk_dim=1, + ) + + _broadcast_tensor( + sync_layer.post_attention_layernorm.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.post_attention_layernorm.weight", + ) + + _broadcast_tp_shard_tensor_gate_up(sync_layer.mlp.gate_up_proj.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.mlp.gate_proj.weight", f"{layer_name}.mlp.up_proj.weight") + + _broadcast_tp_shard_tensor( + sync_layer.mlp.down_proj.weight if dst_pp_rank == pp_rank else None, + f"{layer_name}.mlp.down_proj.weight", + chunk_dim=1, + ) + # Final Layernorm + # ------------------- + print_rank_0("loading final layernorm...") + gpt_model_module = _get_gpt_model(models[-1]) + _broadcast_tensor( + getattr(gpt_model_module.model.norm, "weight", None), + "model.norm.weight", + ) + + print_rank_0("loading lm_head...") + lm_head_weight = None + if pp_rank + 1 == pp_size: + lm_head_weight = gpt_model_module.lm_head.weight + + if is_value_model: + # if torch.distributed.get_rank() == 0: + if 'lm_head.weight' in state_dict and state_dict['lm_head.weight'].shape[0] == 1: + _broadcast_tensor(lm_head_weight, "lm_head.weight") + elif 'reward_head.weight' in state_dict and state_dict['reward_head.weight'].shape[0] == 1: + _broadcast_tensor(lm_head_weight, "reward_head.weight") + print_rank_0('load lm_head from value_head weight') + else: + _broadcast_tensor(None, "lm_head.weight") + print_rank_0('fail to match lm_head in value_model') + # else: + + # _broadcast_tensor(lm_head_weight, "lm_head.weight") + + else: + _broadcast_tp_shard_tensor(lm_head_weight, "lm_head.weight") + dist.barrier() + # Broadcast weights inside data parallel groups + for wrapped_model in wrapped_models: + broadcast_params(wrapped_model) + + torch.cuda.empty_cache() + print_rank_0(f"loading megatron ckpt done, time elapsed {time.time() - start_time}s") diff --git a/verl/models/llama/megatron/checkpoint_utils/llama_saver.py b/verl/models/llama/megatron/checkpoint_utils/llama_saver.py new file mode 100644 index 000000000..0764b6fe5 --- /dev/null +++ b/verl/models/llama/megatron/checkpoint_utils/llama_saver.py @@ -0,0 +1,449 @@ +# 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 megatron +from megatron.core import mpu +from megatron.utils import print_rank_0, unwrap_model +from megatron.model import Float16Module +from megatron.model import DistributedDataParallel as LocalDDP +from torch.nn.parallel import DistributedDataParallel as torchDDP +import torch +import time +from typing import Optional +import torch.distributed as dist +from megatron import get_args + + +def _megatron_calc_global_rank(tp_rank: int = 0, dp_rank: int = 0, pp_rank: int = 0): + """given TP,DP,PP rank to get the global rank.""" + + args = get_args() + tp_size = mpu.get_tensor_model_parallel_world_size() + dp_size = mpu.get_data_parallel_world_size() + pp_size = mpu.get_pipeline_model_parallel_world_size() + assert (tp_size * dp_size * pp_size == torch.distributed.get_world_size() + ), f"{tp_size} x {dp_size} x {pp_size} != {torch.distributed.get_world_size()}" + if args.switch_dp_and_pp_grouping: + # TP-PP-DP grouping + return (dp_rank * pp_size + pp_rank) * tp_size + tp_rank + else: + # TP-DP-PP grouping + return (pp_rank * dp_size + dp_rank) * tp_size + tp_rank + + +def _megatron_calc_layer_map(config): + """Calculate the mapping of global layer_idx to local layer_idx + Returns: + layer_map (Dict: int -> tuple(int, int, int)): + mapping from the global layer index to + a tuple of (pp_rank, virtual_pp_rank, layer_idx inside model) + """ + import megatron + from megatron.core import mpu + + pp_size = mpu.get_pipeline_model_parallel_world_size() + virtual_pp_size = mpu.get_virtual_pipeline_model_parallel_world_size() or 1 + + args = megatron.get_args() + layer_map = dict() + num_layers_per_model = config.num_hidden_layers // pp_size // virtual_pp_size + assert num_layers_per_model * pp_size * virtual_pp_size == config.num_hidden_layers + + for pp_rank_idx in range(pp_size): + for virtual_pp_rank_idx in range(virtual_pp_size): + layer_offset = (virtual_pp_rank_idx * (config.num_hidden_layers // virtual_pp_size) + + pp_rank_idx * num_layers_per_model) + for layer_idx in range(num_layers_per_model): + layer_map[layer_offset + layer_idx] = ( + pp_rank_idx, + virtual_pp_rank_idx, + layer_idx, + ) + return layer_map + + +def merge_megatron_ckpt_llama(wrapped_models, config, is_value_model=False, dtype='bf16'): + """Merge sharded parameters of a Megatron module into a merged checkpoint. + + Args: + wrapped_modelss (list of megatron.model.DistributedDataParallel): + The local DDP wrapped megatron modules. + dtype (str or None): + The data type of state_dict. if None, the data type of the original parameters + is used. + gpt_model_key: key to access model + Returns: + state_dict (dict): + The merged state_dict in rank 0, and an empty dictionary in other ranks. + """ + start_time = time.time() + args = megatron.get_args() + + def _get_gpt_model(model): + return model + + dp_rank = mpu.get_data_parallel_rank() + pp_size = mpu.get_pipeline_model_parallel_world_size() + pp_rank = mpu.get_pipeline_model_parallel_rank() + virtual_pp_size = mpu.get_virtual_pipeline_model_parallel_world_size() or 1 + mp_group = mpu.get_model_parallel_group() + + if dist.get_rank() == 0: + assert mp_group.rank() == 0, f"mp_rank:[{mp_group.rank}] != 0 on rank #0" + assert pp_rank == 0, f"pp_rank:[{pp_rank}] != 0 on rank #0" + assert dp_rank == 0, f"dp_rank:[{dp_rank}] != 0 on rank #0" + + if not isinstance(wrapped_models, (list, tuple)): + wrapped_models = list(wrapped_models) + + assert len(wrapped_models) == virtual_pp_size + num_layers_per_model = config.num_hidden_layers // pp_size // virtual_pp_size + assert num_layers_per_model * pp_size * virtual_pp_size == config.num_hidden_layers + + models = [None] * len(wrapped_models) + + for i, wrapped_model in enumerate(wrapped_models): + models[i] = unwrap_model(wrapped_model, (torchDDP, LocalDDP, Float16Module)) + assert len(models[i].model.layers + ) == num_layers_per_model, 'len model layers {} not equal to num_layers_per_model {}'.format( + len(models[i].model.layers), num_layers_per_model) + + state_dict = dict() + + def _get_cpu_tensor(tensor: torch.Tensor): + if tensor is None: + return None + if tensor.device == torch.device("cpu"): + return tensor.detach().clone() + return tensor.detach().cpu() + + def _broadcast_tensor(tensor, name, src_pp_rank) -> torch.Tensor: + """broadcast tensor across mp_group""" + nonlocal state_dict + nonlocal mp_group + src_rank = _megatron_calc_global_rank(tp_rank=0, dp_rank=0, pp_rank=src_pp_rank) + + if torch.distributed.get_rank() == src_rank: + if tensor is None: + weight = None + tensor_shape = None + else: + weight = tensor + tensor_shape = weight.shape + else: + weight = None + tensor_shape = None + + obj_list = [tensor_shape] + dist.broadcast_object_list(obj_list, src=src_rank, group=mp_group) + tensor_shape = obj_list[0] + + if tensor_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tensor:[{name}] not exist, skip collect") + return + + if weight is None: + weight = torch.empty( + tensor_shape, + dtype=args.params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + + dist.broadcast(weight, src=src_rank, group=mp_group) + + if torch.distributed.get_rank() == 0: + state_dict[name] = _get_cpu_tensor(weight) + + def _broadcast_tp_shard_tensor(tensor, name, src_pp_rank, concat_dim=0, mutate_func=None) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + src_rank = _megatron_calc_global_rank(tp_rank=0, dp_rank=0, pp_rank=src_pp_rank) + + if torch.distributed.get_rank() == src_rank: + chunk_shape = tensor.shape + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=src_rank, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{name}] not exist, skip collecting") + return + + buffer_tensor = torch.empty( + chunk_shape, + dtype=args.params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + + chunk_tensors = [None] * tp_size + + for i in range(tp_size): + cur_src_rank = _megatron_calc_global_rank(tp_rank=i, dp_rank=0, pp_rank=src_pp_rank) + sync_tensor = tensor if torch.distributed.get_rank() == cur_src_rank else buffer_tensor + dist.broadcast(sync_tensor, src=cur_src_rank, group=mp_group) + + if torch.distributed.get_rank() == 0: + chunk_tensors[i] = _get_cpu_tensor(sync_tensor) + + if torch.distributed.get_rank() == 0: + full_tensor = torch.concat(chunk_tensors, dim=concat_dim) + if mutate_func is not None: + full_tensor = mutate_func(full_tensor) + state_dict[name] = full_tensor + + def _broadcast_tp_shard_tensor_gate_up(tensor, gate_name, up_name, src_pp_rank) -> torch.Tensor: + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + src_rank = _megatron_calc_global_rank(tp_rank=0, dp_rank=0, pp_rank=src_pp_rank) + + if torch.distributed.get_rank() == src_rank: + chunk_shape = tensor.shape + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=src_rank, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{gate_name, up_name}] not exist, skip collecting") + return + + buffer_tensor = torch.empty( + chunk_shape, + dtype=args.params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + + chunk_tensors = [None] * tp_size + + for i in range(tp_size): + cur_src_rank = _megatron_calc_global_rank(tp_rank=i, dp_rank=0, pp_rank=src_pp_rank) + sync_tensor = tensor if torch.distributed.get_rank() == cur_src_rank else buffer_tensor + dist.broadcast(sync_tensor, src=cur_src_rank, group=mp_group) + + if torch.distributed.get_rank() == 0: + chunk_tensors[i] = _get_cpu_tensor(sync_tensor) + + if torch.distributed.get_rank() == 0: + full_tensor = torch.concat(chunk_tensors, dim=0) + intermediate_size_tp = config.intermediate_size // tp_size + gate_weight_list = [] + up_weight_list = [] + for i in range(tp_size): + gate_up_weight_tp = full_tensor[intermediate_size_tp * 2 * i:intermediate_size_tp * 2 * (i + 1)] + gate_weight_tp = gate_up_weight_tp[:intermediate_size_tp] + up_weight_tp = gate_up_weight_tp[intermediate_size_tp:] + gate_weight_list.append(gate_weight_tp) + up_weight_list.append(up_weight_tp) + + state_dict[gate_name] = torch.cat(gate_weight_list, dim=0) + state_dict[up_name] = torch.cat(up_weight_list, dim=0) + + def _broadcast_tp_shard_tensor_qkv(tensor, q_name, k_name, v_name, src_pp_rank): + """broadcast tensor in tp shards across mp_group""" + nonlocal state_dict + nonlocal mp_group + tp_rank = mpu.get_tensor_model_parallel_rank() + tp_size = mpu.get_tensor_model_parallel_world_size() + src_rank = _megatron_calc_global_rank(tp_rank=0, dp_rank=0, pp_rank=src_pp_rank) + + if torch.distributed.get_rank() == src_rank: + chunk_shape = tensor.shape + else: + chunk_shape = None + + obj_list = [chunk_shape] + dist.broadcast_object_list(obj_list, src=src_rank, group=mp_group) + chunk_shape = obj_list[0] + if chunk_shape is None: + # all or none ranks in the mp_group should reach here + print_rank_0(f"tp_shard tensor:[{q_name}] not exist, skip collecting") + return + + buffer_tensor = torch.empty( + chunk_shape, + dtype=args.params_dtype, + device=torch.cuda.current_device(), + requires_grad=False, + ) + + chunk_tensors = [None] * tp_size + + for i in range(tp_size): + cur_src_rank = _megatron_calc_global_rank(tp_rank=i, dp_rank=0, pp_rank=src_pp_rank) + sync_tensor = tensor if torch.distributed.get_rank() == cur_src_rank else buffer_tensor + dist.broadcast(sync_tensor, src=cur_src_rank, group=mp_group) + + if torch.distributed.get_rank() == 0: + chunk_tensors[i] = _get_cpu_tensor(sync_tensor) + + if torch.distributed.get_rank() == 0: + full_tensor = torch.concat(chunk_tensors, dim=0) + q_weight_list = [] + k_weight_list = [] + v_weight_list = [] + hidden_size_per_head = config.hidden_size // config.num_attention_heads + + if config.num_key_value_heads >= tp_size: + q_size_tp = config.hidden_size // tp_size + kv_size_tp = hidden_size_per_head * config.num_key_value_heads // tp_size + total_size = q_size_tp + 2 * kv_size_tp + for i in range(tp_size): + qkv_part = full_tensor[i * total_size:(i + 1) * total_size] + q_part = qkv_part[:q_size_tp] + k_part = qkv_part[q_size_tp:q_size_tp + kv_size_tp] + v_part = qkv_part[q_size_tp + kv_size_tp:total_size] + q_weight_list.append(q_part) + k_weight_list.append(k_part) + v_weight_list.append(v_part) + else: + q_size_tp = config.hidden_size // tp_size + kv_size_tp = hidden_size_per_head + total_size = q_size_tp + 2 * kv_size_tp + for i in range(tp_size): + qkv_part = full_tensor[i * total_size:(i + 1) * total_size] + q_part = qkv_part[:q_size_tp] + k_part = qkv_part[q_size_tp:q_size_tp + kv_size_tp] + v_part = qkv_part[q_size_tp + kv_size_tp:total_size] + q_weight_list.append(q_part) + if i * config.num_key_value_heads % tp_size == 0: + k_weight_list.append(k_part) + v_weight_list.append(v_part) + + state_dict[q_name] = torch.cat(q_weight_list, dim=0) + state_dict[k_name] = torch.cat(k_weight_list, dim=0) + state_dict[v_name] = torch.cat(v_weight_list, dim=0) + + # empty cache before collecting weights + torch.cuda.empty_cache() + # Embeddings + # ------------------- + if dp_rank == 0: + # Embeddings + # ------------------- + print_rank_0("collecting embeddings...") + gpt_model_module = _get_gpt_model(models[0]) + _broadcast_tp_shard_tensor( + gpt_model_module.model.embed_tokens.weight if pp_rank == 0 else None, + "model.embed_tokens.weight", + src_pp_rank=0, + ) + + # Transformer layers + # ------------------- + layer_map = _megatron_calc_layer_map(config) + for layer in range(config.num_hidden_layers): + print_rank_0(f"collecting layer #{layer}...") + layer_name = f"model.layers.{layer}" + src_pp_rank, src_virtual_pp_rank, src_layer_idx = layer_map[layer] + + gpt_model_module = _get_gpt_model(models[src_virtual_pp_rank]) + sync_layer = gpt_model_module.model.layers[src_layer_idx] + + _broadcast_tensor( + sync_layer.input_layernorm.weight, + f"{layer_name}.input_layernorm.weight", + src_pp_rank=src_pp_rank, + ) + + _broadcast_tp_shard_tensor_qkv( + sync_layer.self_attn.qkv_proj.weight, + f"{layer_name}.self_attn.q_proj.weight", + f"{layer_name}.self_attn.k_proj.weight", + f"{layer_name}.self_attn.v_proj.weight", + src_pp_rank=src_pp_rank, + ) + + _broadcast_tp_shard_tensor( + sync_layer.self_attn.o_proj.weight, + f"{layer_name}.self_attn.o_proj.weight", + concat_dim=1, + src_pp_rank=src_pp_rank, + ) + + _broadcast_tensor( + sync_layer.post_attention_layernorm.weight, + f"{layer_name}.post_attention_layernorm.weight", + src_pp_rank=src_pp_rank, + ) + + _broadcast_tp_shard_tensor_gate_up(sync_layer.mlp.gate_up_proj.weight, + f"{layer_name}.mlp.gate_proj.weight", + f"{layer_name}.mlp.up_proj.weight", + src_pp_rank=src_pp_rank) + + _broadcast_tp_shard_tensor( + sync_layer.mlp.down_proj.weight, + f"{layer_name}.mlp.down_proj.weight", + concat_dim=1, + src_pp_rank=src_pp_rank, + ) + + # Final Layernorm + # ------------------- + print_rank_0("collecting final layernorm...") + gpt_model_module = _get_gpt_model(models[-1]) + _broadcast_tensor( + getattr(gpt_model_module.model.norm, "weight", None), + "model.norm.weight", + src_pp_rank=pp_size - 1, + ) + + print_rank_0("collecting lm_head...") + + if is_value_model: + _broadcast_tensor(getattr(gpt_model_module.lm_head, "weight", None) if pp_rank == pp_size - 1 else None, + "reward_head.weight", + src_pp_rank=pp_size - 1) + + else: + _broadcast_tp_shard_tensor( + getattr(gpt_model_module.lm_head, "weight", None) if pp_rank == pp_size - 1 else None, + "lm_head.weight", + src_pp_rank=pp_size - 1, + ) + + dist.barrier() + + torch.cuda.empty_cache() + if torch.distributed.get_rank() == 0: + if dtype == "fp16": + dtype = torch.float16 + elif dtype == "bf16": + dtype = torch.bfloat16 + elif dtype is None or dtype == "fp32": + dtype = torch.float32 + else: + print(f'Unknown/unsupported dtype to save: {dtype}"') + exit(1) + for k, v in state_dict.items(): + if dtype != v.dtype: + state_dict[k] = v.to(dtype) + + print_rank_0(f"merge megatron ckpt done, time elapsed {time.time() - start_time}s") + return state_dict diff --git a/verl/models/llama/megatron/layers/__init__.py b/verl/models/llama/megatron/layers/__init__.py new file mode 100644 index 000000000..3761bae7d --- /dev/null +++ b/verl/models/llama/megatron/layers/__init__.py @@ -0,0 +1,18 @@ +# 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 .parallel_attention import ParallelLlamaAttention +from .parallel_decoder import ParallelLlamaDecoderLayer, ParallelLlamaDecoderLayerRmPad +from .parallel_mlp import ParallelLlamaMLP +from .parallel_rmsnorm import ParallelLlamaRMSNorm diff --git a/verl/models/llama/megatron/layers/parallel_attention.py b/verl/models/llama/megatron/layers/parallel_attention.py new file mode 100644 index 000000000..f14653fca --- /dev/null +++ b/verl/models/llama/megatron/layers/parallel_attention.py @@ -0,0 +1,418 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. +# +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX +# and OPT implementations in this library. It has been modified from its +# original forms to accommodate minor architectural differences compared +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. +# +# 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 math +from typing import Optional, Tuple + +import torch +from megatron.core import parallel_state as mpu +from megatron.core import tensor_parallel +from megatron.core import ModelParallelConfig +from torch import nn +from transformers import LlamaConfig +from verl.models.llama.megatron.layers.parallel_linear import QKVParallelLinear + +from verl.utils.megatron import tensor_parallel as tp_utils + + +class LlamaRotaryEmbedding(nn.Module): + + def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None): + super().__init__() + + self.dim = dim + self.max_position_embeddings = max_position_embeddings + self.base = base + inv_freq = 1.0 / (self.base**(torch.arange(0, self.dim, 2).float().to(device) / self.dim)) + self.register_buffer("inv_freq", inv_freq, persistent=False) + + # Build here to make `torch.jit.trace` work. + self._set_cos_sin_cache(seq_len=max_position_embeddings, + device=self.inv_freq.device, + dtype=torch.get_default_dtype()) + + def _set_cos_sin_cache(self, seq_len, device, dtype): + self.max_seq_len_cached = seq_len + t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype) + + freqs = torch.einsum("i,j->ij", t, self.inv_freq) + # Different from paper, but it uses a different permutation in order to obtain the same calculation + emb = torch.cat((freqs, freqs), dim=-1) + self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False) + self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False) + + def forward(self, x, seq_len=None): + # x: [bs, num_attention_heads, seq_len, head_size] + if seq_len > self.max_seq_len_cached: + self._set_cos_sin_cache(seq_len=seq_len, device=x.device, dtype=x.dtype) + + return ( + self.cos_cached[:seq_len].to(dtype=x.dtype), + self.sin_cached[:seq_len].to(dtype=x.dtype), + ) + + +class LlamaLinearScalingRotaryEmbedding(LlamaRotaryEmbedding): + """LlamaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev""" + + def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0): + self.scaling_factor = scaling_factor + super().__init__(dim, max_position_embeddings, base, device) + + def _set_cos_sin_cache(self, seq_len, device, dtype): + self.max_seq_len_cached = seq_len + t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype) + t = t / self.scaling_factor + + freqs = torch.einsum("i,j->ij", t, self.inv_freq) + # Different from paper, but it uses a different permutation in order to obtain the same calculation + emb = torch.cat((freqs, freqs), dim=-1) + self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False) + self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False) + + +class LlamaDynamicNTKScalingRotaryEmbedding(LlamaRotaryEmbedding): + """LlamaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla""" + + def __init__(self, dim, max_position_embeddings=2048, base=10000, device=None, scaling_factor=1.0): + self.scaling_factor = scaling_factor + super().__init__(dim, max_position_embeddings, base, device) + + def _set_cos_sin_cache(self, seq_len, device, dtype): + self.max_seq_len_cached = seq_len + + if seq_len > self.max_position_embeddings: + base = self.base * ((self.scaling_factor * seq_len / self.max_position_embeddings) - + (self.scaling_factor - 1))**(self.dim / (self.dim - 2)) + inv_freq = 1.0 / (base**(torch.arange(0, self.dim, 2).float().to(device) / self.dim)) + self.register_buffer("inv_freq", inv_freq, persistent=False) + + t = torch.arange(self.max_seq_len_cached, device=device, dtype=self.inv_freq.dtype) + + freqs = torch.einsum("i,j->ij", t, self.inv_freq) + # Different from paper, but it uses a different permutation in order to obtain the same calculation + emb = torch.cat((freqs, freqs), dim=-1) + self.register_buffer("cos_cached", emb.cos().to(dtype), persistent=False) + self.register_buffer("sin_cached", emb.sin().to(dtype), persistent=False) + + +def rotate_half(x): + """Rotates half the hidden dims of the input.""" + x1 = x[..., :x.shape[-1] // 2] + x2 = x[..., x.shape[-1] // 2:] + return torch.cat((-x2, x1), dim=-1) + + +def apply_rotary_pos_emb(q, k, cos, sin, position_ids): + cos = cos[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim] + sin = sin[position_ids].unsqueeze(1) # [bs, 1, seq_len, dim] + q_embed = (q * cos) + (rotate_half(q) * sin) + k_embed = (k * cos) + (rotate_half(k) * sin) + return q_embed, k_embed + + +def repeat_kv(hidden_states: torch.Tensor, n_rep: int) -> torch.Tensor: + """ + This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, + num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim) + """ + batch, num_key_value_heads, slen, head_dim = hidden_states.shape + if n_rep == 1: + return hidden_states + hidden_states = hidden_states[:, :, None, :, :].expand(batch, num_key_value_heads, n_rep, slen, head_dim) + return hidden_states.reshape(batch, num_key_value_heads * n_rep, slen, head_dim) + + +class ParallelLlamaAttention(nn.Module): + """Multi-headed attention from 'Attention Is All You Need' paper""" + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.config = config + self.megatron_config = megatron_config + self.hidden_size = config.hidden_size + self.num_heads = config.num_attention_heads + self.head_dim = self.hidden_size // self.num_heads + self.num_key_value_heads = config.num_key_value_heads + self.num_key_value_groups = self.num_heads // self.num_key_value_heads + self.max_position_embeddings = config.max_position_embeddings + self.rope_theta = config.rope_theta + + # assign values after tp + tp_size = mpu.get_tensor_model_parallel_world_size() + assert self.num_heads % tp_size == 0, f'num_head must be divisible by tp_size. Got num_head={self.num_heads}, tp_size={tp_size}' + assert self.num_key_value_heads % tp_size == 0, \ + f'num_key_value_heads must be divisible by tp_size. Got num_key_value_heads={self.num_key_value_heads}, tp_size={tp_size}' + + self.num_heads_per_tp = self.num_heads // tp_size + self.num_key_value_heads_per_tp = self.num_key_value_heads // tp_size + self.hidden_size_per_tp = self.hidden_size // tp_size + + if (self.head_dim * self.num_heads) != self.hidden_size: + raise ValueError(f"hidden_size must be divisible by num_heads (got `hidden_size`: {self.hidden_size}" + f" and `num_heads`: {self.num_heads}).") + + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + row_kwargs = tp_utils.get_default_kwargs_for_row_parallel_linear() + + if megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + assert row_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, megatron_config) + tp_utils.update_kwargs_with_config(row_kwargs, megatron_config) + + # [self.q_size, self.k_size, self.v_size] + self.qkv_proj = QKVParallelLinear(input_size=self.hidden_size, + num_heads=self.num_heads, + num_key_value_heads=self.num_key_value_heads, + head_dim=self.head_dim, + bias=config.attention_bias, + gather_output=False, + skip_bias_add=False, + **column_kwargs) + + self.q_size = self.num_heads_per_tp * self.head_dim + self.k_size = self.num_key_value_heads_per_tp * self.head_dim + self.v_size = self.num_key_value_heads_per_tp * self.head_dim + + self.o_proj = tensor_parallel.RowParallelLinear(input_size=self.num_heads * self.head_dim, + output_size=self.hidden_size, + bias=config.attention_bias, + input_is_parallel=True, + skip_bias_add=False, + **row_kwargs) + + self._init_rope() + + def _init_rope(self): + if self.config.rope_scaling is None: + self.rotary_emb = LlamaRotaryEmbedding( + self.head_dim, + max_position_embeddings=self.max_position_embeddings, + base=self.rope_theta, + ) + else: + scaling_type = self.config.rope_scaling["type"] + scaling_factor = self.config.rope_scaling["factor"] + if scaling_type == "linear": + self.rotary_emb = LlamaLinearScalingRotaryEmbedding( + self.head_dim, + max_position_embeddings=self.max_position_embeddings, + scaling_factor=scaling_factor, + base=self.rope_theta, + ) + elif scaling_type == "dynamic": + self.rotary_emb = LlamaDynamicNTKScalingRotaryEmbedding( + self.head_dim, + max_position_embeddings=self.max_position_embeddings, + scaling_factor=scaling_factor, + base=self.rope_theta, + ) + else: + raise ValueError(f"Unknown RoPE scaling type {scaling_type}") + + def _shape(self, tensor: torch.Tensor, seq_len: int, bsz: int): + return tensor.view(bsz, seq_len, self.num_heads, self.head_dim).transpose(1, 2).contiguous() + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Tuple[torch.Tensor, Optional[torch.Tensor], Optional[Tuple[torch.Tensor]]]: + bsz, q_len, _ = hidden_states.size() + qkv = self.qkv_proj(hidden_states)[0] + query_states, key_states, value_states = qkv.split([self.q_size, self.k_size, self.v_size], dim=-1) + + query_states = query_states.view(bsz, q_len, self.num_heads_per_tp, self.head_dim).transpose(1, 2) + key_states = key_states.view(bsz, q_len, self.num_key_value_heads_per_tp, self.head_dim).transpose(1, 2) + value_states = value_states.view(bsz, q_len, self.num_key_value_heads_per_tp, self.head_dim).transpose(1, 2) + + kv_seq_len = key_states.shape[-2] + cos, sin = self.rotary_emb(value_states, seq_len=kv_seq_len) + query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin, position_ids) + + key_states = repeat_kv(key_states, self.num_key_value_groups) + value_states = repeat_kv(value_states, self.num_key_value_groups) + + attn_weights = torch.matmul(query_states, key_states.transpose(2, 3)) / math.sqrt(self.head_dim) + + if attn_weights.size() != (bsz, self.num_heads_per_tp, q_len, kv_seq_len): + raise ValueError( + f"Attention weights should be of size {(bsz, self.num_heads_per_tp, q_len, kv_seq_len)}, but is" + f" {attn_weights.size()}") + + if attention_mask is not None: + if attention_mask.size() != (bsz, 1, q_len, kv_seq_len): + raise ValueError( + f"Attention mask should be of size {(bsz, 1, q_len, kv_seq_len)}, but is {attention_mask.size()}") + attn_weights = attn_weights + attention_mask + + # upcast attention to fp32 + attn_weights = nn.functional.softmax(attn_weights, dim=-1, dtype=torch.float32).to(query_states.dtype) + attn_output = torch.matmul(attn_weights, value_states) + + if attn_output.size() != (bsz, self.num_heads_per_tp, q_len, self.head_dim): + raise ValueError( + f"`attn_output` should be of size {(bsz, self.num_heads_per_tp, q_len, self.head_dim)}, but is" + f" {attn_output.size()}") + + attn_output = attn_output.transpose(1, 2).contiguous() + attn_output = attn_output.reshape(bsz, q_len, self.hidden_size_per_tp) + attn_output = self.o_proj(attn_output)[0] + return attn_output + + +""" +Remove padding Attention +- Using Flash-attn 2 +- Compatible with sequence parallel +""" + +from transformers.utils import is_flash_attn_2_available +import torch.nn.functional as F + +from einops import rearrange + +if is_flash_attn_2_available(): + from flash_attn import flash_attn_varlen_func + from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa + + +def apply_rotary_pos_emb_rmpad(q, k, cos, sin, position_ids, indices, sequence_length): + batch_size = position_ids.shape[0] + + q = pad_input(q, indices, batch_size, sequence_length) # (batch_size, seqlen, num_head, head_dim) + k = pad_input(k, indices, batch_size, sequence_length) + cos = cos[position_ids].unsqueeze(2) # [bs, seq_len, 1, dim] + sin = sin[position_ids].unsqueeze(2) # [bs, seq_len, 1, dim] + q_embed = (q * cos) + (rotate_half(q) * sin) + k_embed = (k * cos) + (rotate_half(k) * sin) + + q_embed = index_first_axis(rearrange(q_embed, "b s ... -> (b s) ..."), indices) + k_embed = index_first_axis(rearrange(k_embed, "b s ... -> (b s) ..."), indices) + + return q_embed, k_embed + + +from flash_attn.layers.rotary import apply_rotary_emb + + +# use flash-attn rotary embeddings with rmpad +# cos/sin shoudl be: (seq_length, rotary_dim / 2) +def apply_rotary_pos_emb_rmpad_flash(q, k, cos, sin, cu_seqlens, max_seqlen): + q_embed = apply_rotary_emb(q, + cos, + sin, + interleaved=False, + inplace=False, + cu_seqlens=cu_seqlens, + max_seqlen=max_seqlen) + k_embed = apply_rotary_emb(k, + cos, + sin, + interleaved=False, + inplace=False, + cu_seqlens=cu_seqlens, + max_seqlen=max_seqlen) + return q_embed, k_embed + + +class ParallelLlamaAttentionRmPad(ParallelLlamaAttention): + + def forward(self, + hidden_states: torch.Tensor, + position_ids: Optional[torch.LongTensor] = None, + sequence_length: int = None, + indices: torch.Tensor = None, + cu_seqlens: torch.Tensor = None, + max_seqlen_in_batch: int = None): + total_nnz, _, _ = hidden_states.size() # This is the total_nnz padded after sequence parallel + + if self.megatron_config.sequence_parallel: + total_nnz = total_nnz * mpu.get_tensor_model_parallel_world_size() + + qkv = self.qkv_proj(hidden_states)[0] + query_states, key_states, value_states = qkv.split([self.q_size, self.k_size, self.v_size], + dim=-1) # (total_nnz, 1, hidden_size) + + if self.megatron_config.sequence_parallel: + sequence_parallel_pad = total_nnz - cu_seqlens[-1] + total_nnz = cu_seqlens[-1] # total_nnz before sp padding + query_states = query_states[:total_nnz] + key_states = key_states[:total_nnz] + value_states = value_states[:total_nnz] + + # Flash attention requires the input to have the shape + # batch_size x seq_length x head_dime x hidden_dim + # therefore we just need to keep the original shape + query_states = query_states.view(total_nnz, self.num_heads_per_tp, self.head_dim) + key_states = key_states.view(total_nnz, self.num_key_value_heads_per_tp, self.head_dim) + value_states = value_states.view(total_nnz, self.num_key_value_heads_per_tp, self.head_dim) + + cos, sin = self.rotary_emb(value_states, seq_len=sequence_length) + cos, sin = cos[:, :cos.shape[1] // 2], sin[:, :sin.shape[1] // 2] # flash attn only needs half + query_states, key_states = apply_rotary_pos_emb_rmpad_flash(query_states, + key_states, + cos, + sin, + cu_seqlens=cu_seqlens, + max_seqlen=max_seqlen_in_batch) + # query_states, key_states = apply_rotary_pos_emb_rmpad(query_states, key_states, cos, sin, position_ids, indices, + + # TODO: llama does not have dropout in the config?? + # It is recommended to use dropout with FA according to the docs + # when training. + dropout_rate = 0.0 # if not self.training else self.attn_dropout + + # In PEFT, usually we cast the layer norms in float32 for training stability reasons + # therefore the input hidden states gets silently casted in float32. Hence, we need + # cast them back in float16 just to be sure everything works as expected. + # This might slowdown training & inference so it is recommended to not cast the LayerNorms + # in fp32. (LlamaRMSNorm handles it correctly) + input_dtype = query_states.dtype + if input_dtype == torch.float32: + query_states = query_states.to(torch.float16) + key_states = key_states.to(torch.float16) + value_states = value_states.to(torch.float16) + + attn_output_unpad = flash_attn_varlen_func( + query_states, + key_states, + value_states, + cu_seqlens_q=cu_seqlens, + cu_seqlens_k=cu_seqlens, + max_seqlen_q=max_seqlen_in_batch, + max_seqlen_k=max_seqlen_in_batch, + dropout_p=dropout_rate, + softmax_scale=None, + causal=True, + ) + + attn_output_unpad = attn_output_unpad.to(input_dtype) + attn_output_unpad = attn_output_unpad.reshape(total_nnz, 1, self.hidden_size_per_tp).contiguous() + + # sequence parallel reduce_scatter is performed inside RowColumnParallel if enabled + # Here we need to repad + if self.megatron_config.sequence_parallel: + attn_output_unpad = F.pad(attn_output_unpad, pad=(0, 0, 0, 0, 0, sequence_parallel_pad)) + + attn_output_unpad = self.o_proj(attn_output_unpad)[0] + return attn_output_unpad diff --git a/verl/models/llama/megatron/layers/parallel_decoder.py b/verl/models/llama/megatron/layers/parallel_decoder.py new file mode 100644 index 000000000..93050a37f --- /dev/null +++ b/verl/models/llama/megatron/layers/parallel_decoder.py @@ -0,0 +1,146 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. +# +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX +# and OPT implementations in this library. It has been modified from its +# original forms to accommodate minor architectural differences compared +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. +# +# 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 Optional, Tuple + +import torch +from torch import nn +from transformers import LlamaConfig +from megatron.core import ModelParallelConfig + +from .parallel_attention import ParallelLlamaAttention, ParallelLlamaAttentionRmPad +from .parallel_mlp import ParallelLlamaMLP +from .parallel_rmsnorm import ParallelLlamaRMSNorm + + +class ParallelLlamaDecoderLayer(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.hidden_size = config.hidden_size + self.self_attn = ParallelLlamaAttention(config=config, megatron_config=megatron_config) + + self.mlp = ParallelLlamaMLP(config, megatron_config=megatron_config) + self.input_layernorm = ParallelLlamaRMSNorm(config, megatron_config) + self.post_attention_layernorm = ParallelLlamaRMSNorm(config, megatron_config) + + def forward( + self, + hidden_states: torch.Tensor, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + """ + Args: + hidden_states (`torch.FloatTensor`): input to the layer of shape `(batch, seq_len, embed_dim)` + attention_mask (`torch.FloatTensor`, *optional*): attention mask of size + `(batch, 1, tgt_len, src_len)` where padding elements are indicated by very large negative values. + output_attentions (`bool`, *optional*): + Whether or not to return the attentions tensors of all attention layers. See `attentions` under + returned tensors for more detail. + use_cache (`bool`, *optional*): + If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding + (see `past_key_values`). + past_key_value (`Tuple(torch.FloatTensor)`, *optional*): cached past key and value projection states + """ + + residual = hidden_states + + hidden_states = self.input_layernorm(hidden_states) + + # Note: sequence parallel is hidden inside ColumnParallelLinear + # reduce scatter is hidden inside RowParallelLinear + + # Self Attention + hidden_states = self.self_attn( + hidden_states=hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + ) + + # TODO: add sequence parallel operator reduce_scatter here + + hidden_states = residual + hidden_states + + # Fully Connected + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + + # TODO: add sequence parallel operator all_gather here + + hidden_states = self.mlp(hidden_states) + + # TODO: add sequence parallel operator reduce_scatter here + + hidden_states = residual + hidden_states + + outputs = hidden_states + + return outputs + + +class ParallelLlamaDecoderLayerRmPad(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.config = config + self.megatron_config = megatron_config + self.hidden_size = config.hidden_size + self.self_attn = ParallelLlamaAttentionRmPad(config=config, megatron_config=megatron_config) + + self.mlp = ParallelLlamaMLP(config, megatron_config=megatron_config) + self.input_layernorm = ParallelLlamaRMSNorm(config, megatron_config) + self.post_attention_layernorm = ParallelLlamaRMSNorm(config, megatron_config) + + def forward( + self, + hidden_states: torch.Tensor, + position_ids: Optional[torch.LongTensor] = None, + sequence_length: int = None, + indices: torch.Tensor = None, + cu_seqlens: int = None, + max_seqlen_in_batch: int = None + ) -> Tuple[torch.FloatTensor, Optional[Tuple[torch.FloatTensor, torch.FloatTensor]]]: + residual = hidden_states # (total_nnz // sp, 1, hidden_size) + + hidden_states = self.input_layernorm(hidden_states) + + # Self Attention + # (total_nnz // sp, 1, hidden_size) -> all-gather (total_nnz, 1, hidden_size) + # -> col + row -> reduce-scatter -> (total_nnz // sp, 1, hidden_size) + hidden_states = self.self_attn(hidden_states=hidden_states, + position_ids=position_ids, + sequence_length=sequence_length, + indices=indices, + cu_seqlens=cu_seqlens, + max_seqlen_in_batch=max_seqlen_in_batch) + + hidden_states = residual + hidden_states + + # Fully Connected + # shape changes same as attn + residual = hidden_states + hidden_states = self.post_attention_layernorm(hidden_states) + hidden_states = self.mlp(hidden_states) + hidden_states = residual + hidden_states + + outputs = hidden_states + + return outputs diff --git a/verl/models/llama/megatron/layers/parallel_linear.py b/verl/models/llama/megatron/layers/parallel_linear.py new file mode 100644 index 000000000..82f9c2d04 --- /dev/null +++ b/verl/models/llama/megatron/layers/parallel_linear.py @@ -0,0 +1,74 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/model_executor/layers/linear.py + +from typing import Optional, Tuple + +from megatron.core import tensor_parallel + + +class QKVParallelLinear(tensor_parallel.ColumnParallelLinear): + + def __init__(self, + input_size, + num_heads, + num_key_value_heads, + head_dim, + *, + bias=True, + gather_output=True, + skip_bias_add=False, + **kwargs): + # Keep input parameters, and already restrict the head numbers + self.input_size = input_size + self.q_output_size = num_heads * head_dim + self.kv_output_size = num_key_value_heads * head_dim + self.head_dim = head_dim + self.gather_output = gather_output + self.skip_bias_add = skip_bias_add + + input_size = self.input_size + output_size = (num_heads + 2 * num_key_value_heads) * self.head_dim + + super().__init__(input_size=input_size, + output_size=output_size, + bias=bias, + gather_output=gather_output, + skip_bias_add=skip_bias_add, + **kwargs) + + +class MergedColumnParallelLinear(tensor_parallel.ColumnParallelLinear): + + def __init__(self, + input_size, + gate_ouput_size, + up_output_size, + *, + bias=True, + gather_output=True, + skip_bias_add=False, + **kwargs): + # Keep input parameters, and already restrict the head numbers + self.input_size = input_size + self.output_size = gate_ouput_size + up_output_size + self.gather_output = gather_output + self.skip_bias_add = skip_bias_add + + super().__init__(input_size=self.input_size, + output_size=self.output_size, + bias=bias, + gather_output=gather_output, + skip_bias_add=skip_bias_add, + **kwargs) diff --git a/verl/models/llama/megatron/layers/parallel_mlp.py b/verl/models/llama/megatron/layers/parallel_mlp.py new file mode 100644 index 000000000..21ad9b16a --- /dev/null +++ b/verl/models/llama/megatron/layers/parallel_mlp.py @@ -0,0 +1,74 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. +# +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX +# and OPT implementations in this library. It has been modified from its +# original forms to accommodate minor architectural differences compared +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. +# +# 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 megatron.core import parallel_state as mpu +from megatron.core import tensor_parallel +from megatron.core import ModelParallelConfig +from torch import nn +from transformers.activations import ACT2FN +from verl.models.llama.megatron.layers.parallel_linear import MergedColumnParallelLinear + +from verl.utils.megatron import tensor_parallel as tp_utils + + +class ParallelLlamaMLP(nn.Module): + + def __init__(self, config, megatron_config: ModelParallelConfig = None) -> None: + super().__init__() + self.config = config + self.hidden_size = config.hidden_size + self.intermediate_size = config.intermediate_size + # The weight is only [hidden_size, intermediate_size // model_parallel_world_size] + + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + row_kwargs = tp_utils.get_default_kwargs_for_row_parallel_linear() + + if megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + assert row_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(row_kwargs, megatron_config) + tp_utils.update_kwargs_with_config(column_kwargs, megatron_config) + + tp_size = mpu.get_tensor_model_parallel_world_size() + + self.gate_up_proj = MergedColumnParallelLinear( + input_size=self.hidden_size, + gate_ouput_size=self.intermediate_size, + up_output_size=self.intermediate_size, + bias=False, + gather_output=False, + skip_bias_add=False, + **column_kwargs, + ) + self.gate_size = self.intermediate_size // tp_size + + self.down_proj = tensor_parallel.RowParallelLinear(input_size=self.intermediate_size, + output_size=self.hidden_size, + bias=False, + input_is_parallel=True, + skip_bias_add=False, + **row_kwargs) + + self.act_fn = ACT2FN[config.hidden_act] + + def forward(self, x): + gate_up = self.gate_up_proj(x)[0] + gate, up = gate_up.split(self.gate_size, dim=-1) + return self.down_proj(self.act_fn(gate) * up)[0] diff --git a/verl/models/llama/megatron/layers/parallel_rmsnorm.py b/verl/models/llama/megatron/layers/parallel_rmsnorm.py new file mode 100644 index 000000000..7027036bf --- /dev/null +++ b/verl/models/llama/megatron/layers/parallel_rmsnorm.py @@ -0,0 +1,46 @@ +# 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 numbers +import torch +from megatron.core import ModelParallelConfig +from torch import nn +from transformers import LlamaConfig + +from apex.normalization.fused_layer_norm import fused_rms_norm_affine +from verl.utils.megatron import sequence_parallel as sp_utils + + +class ParallelLlamaRMSNorm(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + """ + LlamaRMSNorm is equivalent to T5LayerNorm + """ + super().__init__() + if isinstance(config.hidden_size, numbers.Integral): + normalized_shape = (config.hidden_size,) + self.normalized_shape = torch.Size(normalized_shape) + self.weight = nn.Parameter(torch.ones(self.normalized_shape)) + self.variance_epsilon = config.rms_norm_eps + + if megatron_config.sequence_parallel: + sp_utils.mark_parameter_as_sequence_parallel(self.weight) + + def forward(self, hidden_states): + return fused_rms_norm_affine(input=hidden_states, + weight=self.weight, + normalized_shape=self.normalized_shape, + eps=self.variance_epsilon, + memory_efficient=True) \ No newline at end of file diff --git a/verl/models/llama/megatron/modeling_llama_megatron.py b/verl/models/llama/megatron/modeling_llama_megatron.py new file mode 100644 index 000000000..c3563286f --- /dev/null +++ b/verl/models/llama/megatron/modeling_llama_megatron.py @@ -0,0 +1,663 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved. +# +# This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX +# and OPT implementations in this library. It has been modified from its +# original forms to accommodate minor architectural differences compared +# to GPT-NeoX and OPT used by the Meta AI team that trained the model. +# +# 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. +""" PyTorch LLaMA model.""" + +from typing import Optional, Tuple, Union + +import torch +import torch.utils.checkpoint +from megatron.core import tensor_parallel +from megatron.core import ModelParallelConfig +from torch import nn +from torch.nn import init +from transformers.modeling_outputs import BaseModelOutputWithPast +from transformers.models.llama.configuration_llama import LlamaConfig +from transformers.models.llama.modeling_llama import CausalLMOutputWithPast + +from verl.utils.megatron import sequence_parallel as sp_utils +from verl.utils.megatron import tensor_parallel as tp_utils +from .layers import ParallelLlamaDecoderLayer, ParallelLlamaRMSNorm, ParallelLlamaDecoderLayerRmPad +""" +TODO: +1. Add weight initialization. Here we need to be careful on TP weight init. +2. Add sequence parallel +3. Load checkpoint from meta LLama pretrained checkpoint +""" + + +# 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) + + +class ParallelLlamaModel(nn.Module): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`] + + Args: + config: LlamaConfig + """ + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.padding_idx = config.pad_token_id + self.vocab_size = config.vocab_size + embedding_kwargs = tp_utils.get_default_kwargs_for_parallel_embedding() + if megatron_config is not None: + assert embedding_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(embedding_kwargs, self.megatron_config) + self.embed_tokens = tensor_parallel.VocabParallelEmbedding(num_embeddings=config.vocab_size, + embedding_dim=config.hidden_size, + **embedding_kwargs) + + self.layers = nn.ModuleList( + [ParallelLlamaDecoderLayer(config, megatron_config) for _ in range(config.num_hidden_layers)]) + self.norm = ParallelLlamaRMSNorm(config, megatron_config) + + # Copied from transformers.models.bart.modeling_bart.BartDecoder._prepare_decoder_attention_mask + def _prepare_decoder_attention_mask(self, 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 + + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, BaseModelOutputWithPast]: + """ + + Args: + input_ids: input ids. shape (batch_size, seq_length) + attention_mask: attention_mask. shape (batch_size, seq_length) + position_ids: position ids. shape (batch_size, seq_length) + + Returns: + + """ + batch_size, seq_length = input_ids.shape + inputs_embeds = self.embed_tokens(input_ids) + # embed positions + + attention_mask = self._prepare_decoder_attention_mask(attention_mask, (batch_size, seq_length), inputs_embeds) + + hidden_states = inputs_embeds + + for idx, decoder_layer in enumerate(self.layers): + layer_outputs = decoder_layer( + hidden_states, + attention_mask=attention_mask, + position_ids=position_ids, + ) + + hidden_states = layer_outputs + + hidden_states = self.norm(hidden_states) + + return hidden_states + + +class ParallelLlamaForCausalLM(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.model = ParallelLlamaModel(config, megatron_config=megatron_config) + self.vocab_size = config.vocab_size + + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + if megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, self.megatron_config) + + self.lm_head = tensor_parallel.ColumnParallelLinear(input_size=config.hidden_size, + output_size=config.vocab_size, + bias=False, + gather_output=False, + skip_bias_add=False, + **column_kwargs) + + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, CausalLMOutputWithPast]: + r""" + Args: + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., + config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored + (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. + + Returns: + ```""" + + # decoder outputs consists of (dec_features, layer_state, dec_hidden, dec_attn) + outputs = self.model( + input_ids=input_ids, + attention_mask=attention_mask, + position_ids=position_ids, + ) + + hidden_states = outputs + logits = self.lm_head(hidden_states)[0] + + logits = tensor_parallel.gather_from_tensor_model_parallel_region(logits) + + logits = logits.float() + return CausalLMOutputWithPast( + loss=None, + logits=logits, + past_key_values=None, + hidden_states=None, + attentions=None, + ) + + +from flash_attn.bert_padding import index_first_axis, pad_input, unpad_input # noqa + + +class ParallelLlamaModelRmPad(nn.Module): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`] + + Args: + config: LlamaConfig + """ + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.padding_idx = config.pad_token_id + self.vocab_size = config.vocab_size + embedding_kwargs = tp_utils.get_default_kwargs_for_parallel_embedding() + self.megatron_config = megatron_config + if megatron_config is not None: + assert embedding_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(embedding_kwargs, self.megatron_config) + self.embed_tokens = tensor_parallel.VocabParallelEmbedding(num_embeddings=config.vocab_size, + embedding_dim=config.hidden_size, + **embedding_kwargs) + + self.layers = nn.ModuleList( + [ParallelLlamaDecoderLayerRmPad(config, megatron_config) for _ in range(config.num_hidden_layers)]) + self.norm = ParallelLlamaRMSNorm(config, megatron_config) + + def forward(self, + input_ids: torch.Tensor, + position_ids: Optional[torch.LongTensor] = None, + sequence_length: int = None, + indices: torch.Tensor = None, + cu_seqlens: int = None, + max_seqlen_in_batch: int = None) -> Union[Tuple, BaseModelOutputWithPast]: + """ + + Args: + input_ids: input ids. shape (1, totol_nnz) + position_ids: position ids. shape (batch_size, seq_length) + + Returns: + + """ + inputs_embeds = self.embed_tokens(input_ids) # (1, total_nnz) -> (1, total_nnz, hidden_size) + + # (1, total_nnz, hidden_size) -> (total_nnz, 1, hidden_size) -> (total_nnz // sp, 1, hidden_size) + inputs_embeds = inputs_embeds.transpose(0, 1) + if self.megatron_config.sequence_parallel: + inputs_embeds = tensor_parallel.scatter_to_sequence_parallel_region(inputs_embeds) + + hidden_states = inputs_embeds + for idx, decoder_layer in enumerate(self.layers): + layer_outputs = decoder_layer(hidden_states, + position_ids=position_ids, + sequence_length=sequence_length, + indices=indices, + cu_seqlens=cu_seqlens, + max_seqlen_in_batch=max_seqlen_in_batch) + + hidden_states = layer_outputs + + hidden_states = self.norm(hidden_states) + + return hidden_states + + +class ParallelLlamaForCausalLMRmPad(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig): + super().__init__() + self.config = config + self.megatron_config = megatron_config + self.model = ParallelLlamaModelRmPad(config, megatron_config=megatron_config) + self.vocab_size = config.vocab_size + self._init_head() + + def _init_head(self): + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + if self.megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, self.megatron_config) + self.lm_head = tensor_parallel.ColumnParallelLinear(input_size=self.config.hidden_size, + output_size=self.config.vocab_size, + bias=False, + gather_output=False, + skip_bias_add=False, + **column_kwargs) + + def _forward_head(self, hidden_states): + # all_gather from sequence parallel region is performed inside lm_head + logits = self.lm_head(hidden_states)[0] + logits = logits.float() # (total_nnz_padded, 1, vocab_size // tp) + logits = tensor_parallel.gather_from_tensor_model_parallel_region(logits) # (total_nnz_padded, 1, vocab_size) + return logits + + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, CausalLMOutputWithPast]: + r""" + Args: + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., + config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored + (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. + + Returns: + ```""" + batch_size, sequence_length = input_ids.shape + + # remove padding here + input_ids, indices, cu_seqlens, max_seqlen_in_batch = unpad_input(input_ids.unsqueeze(dim=-1), + attention_mask) # (total_nnz, 1) + + # pad input_ids to multiple of tp for all tp ranks + # TODO: for better performance, the sp padding should be removed at each layer. Not sure the performance gap + if self.megatron_config.sequence_parallel: + input_ids = sp_utils.pad_to_sequence_parallel(input_ids) + + input_ids = input_ids.transpose(0, 1) # (1, total_nnz+pad) + + outputs = self.model(input_ids=input_ids, + position_ids=position_ids, + sequence_length=sequence_length, + indices=indices, + cu_seqlens=cu_seqlens, + max_seqlen_in_batch=max_seqlen_in_batch) + + hidden_states = outputs + + logits = self._forward_head(hidden_states) + + # remove padding from sequence parallel + if self.megatron_config.sequence_parallel: + totol_nnz = cu_seqlens[-1] + logits = logits[:totol_nnz] # (total_nnz_padded) + + logits = torch.squeeze(logits, dim=1) # remove the artificial batch dimension + # add removed padding back + logits = pad_input(logits, indices, batch_size, + seqlen=sequence_length) # (batch_size, sequence_length, vocab_size) + + return CausalLMOutputWithPast( + loss=None, + logits=logits, + past_key_values=None, + hidden_states=None, + attentions=None, + ) + + +class ParallelLlamaForValueRmPad(ParallelLlamaForCausalLMRmPad): + + def _init_head(self): + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + if self.megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, self.megatron_config) + self.lm_head = nn.Linear(in_features=self.config.hidden_size, out_features=1, bias=False) + # lm_head is effectively the same as sequence parallel + sp_utils.mark_parameter_as_sequence_parallel(self.lm_head.weight) + + def _forward_head(self, hidden_states): + logits = self.lm_head(hidden_states) # (total_nnz_padded // tp, 1, 1) + logits = logits.float() + if self.megatron_config.sequence_parallel: + logits = tensor_parallel.gather_from_sequence_parallel_region(logits, tensor_parallel_output_grad=False) + return logits + + def forward( + self, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, CausalLMOutputWithPast]: + output = super().forward(input_ids, attention_mask, position_ids) + output.logits = torch.squeeze(output.logits, dim=-1) + return output + + +""" +Support pipeline parallelism +""" + + +class ParallelLlamaModelRmPadPP(nn.Module): + """ + Transformer decoder consisting of *config.num_hidden_layers* layers. Each layer is a [`LlamaDecoderLayer`] + This model definition supports pipeline parallelism. To support pp and vpp, + - This model only contains layer in this pp stage and vpp chunk + - When calling get_model in Megatron, this rank will instantiate all the vpp chunks in this pp. + Args: + config: LlamaConfig + """ + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig, pre_process, post_process): + super().__init__() + self.padding_idx = config.pad_token_id + self.vocab_size = config.vocab_size + self.pre_process = pre_process + self.post_process = post_process + self.megatron_config = megatron_config + embedding_kwargs = tp_utils.get_default_kwargs_for_parallel_embedding() + if megatron_config is not None: + assert embedding_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(embedding_kwargs, self.megatron_config) + if pre_process: + self.embed_tokens = tensor_parallel.VocabParallelEmbedding(num_embeddings=config.vocab_size, + embedding_dim=config.hidden_size, + **embedding_kwargs) + else: + self.embed_tokens = None + + # pp_rank = megatron_config.pipeline_model_parallel_rank + pp_size = megatron_config.pipeline_model_parallel_size + self.num_layer_per_pp = config.num_hidden_layers // pp_size + vpp_size = megatron_config.virtual_pipeline_model_parallel_size + + if vpp_size is not None: + self.num_layer_vpp_chunk = self.num_layer_per_pp // vpp_size + self.num_layer_this_model = self.num_layer_vpp_chunk + # vpp_rank = megatron_config.virtual_pipeline_model_parallel_rank + # self.offset = vpp_rank * ( + # config.num_hidden_layers // megatron_config.virtual_pipeline_model_parallel_size) + \ + # (megatron_config.pipeline_model_parallel_rank * self.num_layer_vpp_chunk) + else: + self.num_layer_this_model = self.num_layer_per_pp + # self.offset = pp_rank * self.num_layer_per_pp + + layers = [] + for i in range(self.num_layer_this_model): + layer = ParallelLlamaDecoderLayerRmPad(config, megatron_config) + # setattr(layer, 'hidden_layer_index', self.offset + i) + layers.append(layer) + + self.layers = nn.ModuleList(layers) + + if post_process: + self.norm = ParallelLlamaRMSNorm(config, megatron_config) + else: + self.norm = None + + def set_input_tensor(self, input_tensor): + """Set input tensor to be used instead of forward()'s input. + + When doing pipeline parallelism the input from the previous + stage comes from communication, not from the input, so the + model's forward_step_func won't have it. This function is thus + used by internal code to bypass the input provided by the + forward_step_func""" + self.input_tensor = input_tensor + + def forward(self, + input_ids: torch.Tensor, + position_ids: Optional[torch.LongTensor] = None, + sequence_length: int = None, + indices: torch.Tensor = None, + cu_seqlens: int = None, + max_seqlen_in_batch: int = None) -> Union[Tuple, BaseModelOutputWithPast]: + """ + + Args: + input_ids: input ids. shape (1, totol_nnz) + position_ids: position ids. shape (batch_size, seq_length) + + Returns: + + """ + if self.pre_process: + # if torch.cuda.current_device() == 0: + # print(f'rank {torch.cuda.current_device()}: input_ids shape before embedding: {input_ids.shape}') + inputs_embeds = self.embed_tokens(input_ids) # (1, total_nnz) -> (1, total_nnz, hidden_size) + + # vocab parallel embedding will not do sequence parallel reduce-scatter in open source megatron + # so need to deal with it by handle here: + # (1, total_nnz, hidden_size) -> (total_nnz, 1, hidden_size) -> (total_nnz // sp, 1, hidden_size) + inputs_embeds = inputs_embeds.transpose(0, 1) + if self.megatron_config.sequence_parallel: + inputs_embeds = tensor_parallel.scatter_to_sequence_parallel_region(inputs_embeds) + + # if torch.cuda.current_device() == 0: + # print(f'rank {torch.cuda.current_device()}: input_embeds shape after embedding: {inputs_embeds.shape}') + hidden_states = inputs_embeds + else: + # self.hidden_states should be passed by Megatron + hidden_states = self.input_tensor + + for idx, decoder_layer in enumerate(self.layers): + layer_outputs = decoder_layer(hidden_states, + position_ids=position_ids, + sequence_length=sequence_length, + indices=indices, + cu_seqlens=cu_seqlens, + max_seqlen_in_batch=max_seqlen_in_batch) + + hidden_states = layer_outputs + + if self.post_process: + hidden_states = self.norm(hidden_states) + + return hidden_states + + +class ParallelLlamaForCausalLMRmPadPP(nn.Module): + + def __init__(self, config: LlamaConfig, megatron_config: ModelParallelConfig, pre_process, post_process): + super().__init__() + self.config = config + self.megatron_config = megatron_config + self.model = ParallelLlamaModelRmPadPP(config, + megatron_config=megatron_config, + pre_process=pre_process, + post_process=post_process) + self.share_embeddings_and_output_weights = None # workaround, megatron requires this attr + self.vocab_size = config.vocab_size + self.pre_process = pre_process + self.post_process = post_process + if post_process: + self._init_head() + + def set_input_tensor(self, input_tensor): + """Set input tensor to be used instead of forward()'s input. + + When doing pipeline parallelism the input from the previous + stage comes from communication, not from the input, so the + model's forward_step_func won't have it. This function is thus + used by internal code to bypass the input provided by the + forward_step_func""" + assert len(input_tensor) == 1 + self.model.set_input_tensor(input_tensor[0]) + + def _init_head(self): + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + if self.megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, self.megatron_config) + self.lm_head = tensor_parallel.ColumnParallelLinear(input_size=self.config.hidden_size, + output_size=self.config.vocab_size, + bias=False, + gather_output=False, + skip_bias_add=False, + **column_kwargs) + + def _forward_head(self, hidden_states): + # all_gather from sequence parallel region is performed inside lm_head + # print(f'logits shape before forward_head: {hidden_states.shape}, vocab_size = {self.config.vocab_size}') # [4, 32, 4096] + logits = self.lm_head(hidden_states)[0] + # print(f'logits shape after forward_head: {logits.shape}') # [8, 32, 8] + logits = logits.float() # (total_nnz_padded, 1, vocab_size // tp) + return logits + + def forward( + self, + # original input + *, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, CausalLMOutputWithPast]: + r""" + Args: + labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*): + Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., + config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored + (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`. + + Returns: + ```""" + + # Note that input_ids, attention_mask and position_ids should be passed to every pp layer. + # In the first pp, input_ids will be used, in other pp layers hidden_states will be used inside self.model + batch_size, sequence_length = input_ids.shape + # remove padding here + input_ids_rmpad, indices, cu_seqlens, max_seqlen_in_batch = unpad_input(input_ids.unsqueeze(dim=-1), + attention_mask) # (total_nnz, 1) + # print(f'input_ids.shape = {input_ids.shape}, input_ids_rmpad.shape = {input_ids_rmpad.shape}, indices.shape = {indices.shape}, cu_seqlens[-1] = {cu_seqlens[-1]}') + # pad input_ids to multiple of tp for all tp ranks + # TODO: for better performance, the sp padding should be removed at each layer. Not sure the performance gap + if self.megatron_config.sequence_parallel: + input_ids_rmpad = sp_utils.pad_to_sequence_parallel(input_ids_rmpad) + + input_ids_rmpad = input_ids_rmpad.transpose(0, 1) # (1, total_nnz+pad) + + outputs = self.model(input_ids=input_ids_rmpad, + position_ids=position_ids, + sequence_length=sequence_length, + indices=indices, + cu_seqlens=cu_seqlens, + max_seqlen_in_batch=max_seqlen_in_batch) + + if self.post_process: + hidden_states = outputs + # print(f'hidden_states.shape = {hidden_states.shape}') # torch.Size([4, 32, 4096]) + logits = self._forward_head(hidden_states) + # print(f'logits.shape = {logits.shape}') + logits = torch.squeeze(logits, dim=1) # remove the artificial batch dimension # torch.Size([8, 32, 16]) + + # remove padding from sequence parallel + if self.megatron_config.sequence_parallel: + totol_nnz = cu_seqlens[-1] + logits = logits[:totol_nnz] # (total_nnz_padded) + # add removed padding back. If input is already rmpad, we let the caller pad_input + # print(f'logits.shape = {logits.shape}, indices.shape = {indices.shape}, batch_size = {batch_size}, seq_len = {sequence_length}') + logits = pad_input(logits, indices, batch_size, + seqlen=sequence_length) # (batch_size, sequence_length, vocab_size) + + return CausalLMOutputWithPast( + loss=None, + logits=logits, + past_key_values=None, + hidden_states=None, + attentions=None, + ) + else: + return outputs + + +class ParallelLlamaForValueRmPadPP(ParallelLlamaForCausalLMRmPadPP): + + def _init_head(self): + column_kwargs = tp_utils.get_default_kwargs_for_column_parallel_linear() + if self.megatron_config is not None: + assert column_kwargs.get('config', False), 'must have ModelParallelConfig' + tp_utils.update_kwargs_with_config(column_kwargs, self.megatron_config) + self.lm_head = nn.Linear(in_features=self.config.hidden_size, out_features=1, bias=False) + # lm_head is effectively the same as sequence parallel + sp_utils.mark_parameter_as_sequence_parallel(self.lm_head.weight) + + def _forward_head(self, hidden_states): + logits = self.lm_head(hidden_states) # (total_nnz_padded // tp, 1, 1) + logits = logits.float() + if self.megatron_config.sequence_parallel: + logits = tensor_parallel.gather_from_sequence_parallel_region(logits, tensor_parallel_output_grad=False) + return logits + + def forward( + self, + *, + input_ids: torch.LongTensor = None, + attention_mask: Optional[torch.Tensor] = None, + position_ids: Optional[torch.LongTensor] = None, + ) -> Union[Tuple, CausalLMOutputWithPast]: + output = super().forward(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids) + if self.post_process: + output.logits = torch.squeeze(output.logits, dim=-1) + return output + else: + return output diff --git a/verl/models/registry.py b/verl/models/registry.py new file mode 100644 index 000000000..f08f473a5 --- /dev/null +++ b/verl/models/registry.py @@ -0,0 +1,50 @@ +# 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 typing import List, Optional, Type + +import torch.nn as nn + +# Architecture -> (module, class). +_MODELS = { + "LlamaForCausalLM": + ("llama", ("ParallelLlamaForCausalLMRmPadPP", "ParallelLlamaForValueRmPadPP", "ParallelLlamaForCausalLMRmPad")), + "MistralForCausalLM": ("mistral", ("ParallelMistralForCausalLMRmPadPP", "ParallelMistralForValueRmPadPP", + "ParallelMistralForCausalLMRmPad")) +} + + +# return model class +class ModelRegistry: + + @staticmethod + def load_model_cls(model_arch: str, value=False) -> Optional[Type[nn.Module]]: + if model_arch not in _MODELS: + return None + + megatron = "megatron" + + module_name, model_cls_name = _MODELS[model_arch] + if not value: # actor/ref + model_cls_name = model_cls_name[0] + elif value: # critic/rm + model_cls_name = model_cls_name[1] + + module = importlib.import_module(f"verl.models.{module_name}.{megatron}.modeling_{module_name}_megatron") + return getattr(module, model_cls_name, None) + + @staticmethod + def get_supported_archs() -> List[str]: + return list(_MODELS.keys()) diff --git a/verl/models/weight_loader_registry.py b/verl/models/weight_loader_registry.py new file mode 100644 index 000000000..17f0c5cae --- /dev/null +++ b/verl/models/weight_loader_registry.py @@ -0,0 +1,23 @@ +# 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. + + +def get_weight_loader(arch: str): + from verl.models.llama.megatron.checkpoint_utils.llama_loader import load_state_dict_to_megatron_llama + _MODEL_WEIGHT_MEGATRON_LOADER_REGISTRY = {'LlamaForCausalLM': load_state_dict_to_megatron_llama} + + if arch in _MODEL_WEIGHT_MEGATRON_LOADER_REGISTRY: + return _MODEL_WEIGHT_MEGATRON_LOADER_REGISTRY[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {_MODEL_WEIGHT_MEGATRON_LOADER_REGISTRY.keys()}") diff --git a/verl/protocol.py b/verl/protocol.py new file mode 100644 index 000000000..827c44e45 --- /dev/null +++ b/verl/protocol.py @@ -0,0 +1,487 @@ +# 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. +""" +Implement base data transfer protocol between any two functions, modules. +We can subclass Protocol to define more detailed batch info with specific keys +""" + +import numpy as np +import copy +from dataclasses import dataclass, field +from typing import Callable, Dict, List, Union + +import torch +import tensordict +from tensordict import TensorDict +from torch.utils.data import DataLoader, Dataset + +from verl.utils.py_functional import union_two_dict + +__all__ = ['DataProto', 'union_tensor_dict'] + +try: + tensordict.set_lazy_legacy(False).set() +except: + pass + + +def union_tensor_dict(tensor_dict1: TensorDict, tensor_dict2: TensorDict) -> TensorDict: + """Union two tensordicts.""" + 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(): + tensor_dict1[key] = tensor_dict2[key] + else: + assert tensor_dict1[key].equal(tensor_dict2[key]), \ + f'{key} in tensor_dict1 and tensor_dict2 are not the same object' + + return tensor_dict1 + + +def union_numpy_dict(tensor_dict1: dict[np.ndarray], tensor_dict2: dict[np.ndarray]) -> dict[np.ndarray]: + for key, val in tensor_dict2.items(): + if key in tensor_dict1: + assert isinstance(tensor_dict2[key], np.ndarray) + assert isinstance(tensor_dict1[key], np.ndarray) + assert np.all(tensor_dict2[key] == tensor_dict1[key]), \ + f'{key} in tensor_dict1 and tensor_dict2 are not the same object' + tensor_dict1[key] = val + + return tensor_dict1 + + +def list_of_dict_to_dict_of_list(list_of_dict: list[dict]): + if len(list_of_dict) == 0: + return {} + keys = list_of_dict[0].keys() + output = {key: [] for key in keys} + for data in list_of_dict: + for key, item in data.items(): + assert key in output + output[key].append(item) + return output + + +def collate_fn(x: list['DataProtoItem']): + batch = [] + non_tensor_batch = [] + for data in x: + batch.append(data.batch) + non_tensor_batch.append(data.non_tensor_batch) + batch = torch.stack(batch).contiguous() + non_tensor_batch = list_of_dict_to_dict_of_list(non_tensor_batch) + for key, val in non_tensor_batch.items(): + non_tensor_batch[key] = np.array(val, dtype=object) + return DataProto(batch=batch, non_tensor_batch=non_tensor_batch) + + +@dataclass +class DataProtoItem: + # TODO(zhangchi.usc1992) add consistency check + batch: TensorDict = None + non_tensor_batch: Dict = field(default_factory=dict) + meta_info: Dict = field(default_factory=dict) + + +@dataclass +class DataProto: + """ + A DataProto is a data structure that aims to provide a standard protocol for data exchange between functions. + It contains a batch (TensorDict) and a meta_info (Dict). The batch is a TensorDict https://pytorch.org/tensordict/. + TensorDict allows you to manipulate a dictionary of Tensors like a single Tensor. Ideally, the tensors with the + same batch size should be put inside batch. + """ + batch: TensorDict = None + non_tensor_batch: Dict = field(default_factory=dict) + meta_info: Dict = field(default_factory=dict) + + def __post_init__(self): + # perform necessary checking + self.check_consistency() + + def __len__(self): + return self.batch.batch_size[0] + + def __getitem__(self, item): + tensor_data = self.batch[item] + non_tensor_data = {key: val[item] for key, val in self.non_tensor_batch.items()} + return DataProtoItem(batch=tensor_data, non_tensor_batch=non_tensor_data, meta_info=self.meta_info) + + def __getstate__(self): + import io + buffer = io.BytesIO() + if tensordict.__version__ >= '0.5.0' and self.batch is not None: + self.batch = self.batch.contiguous() + self.batch = self.batch.consolidate() + torch.save(self.batch, buffer) + return buffer, self.non_tensor_batch, self.meta_info + + def __setstate__(self, data): + batch_deserialized, non_tensor_batch, meta_info = data + batch_deserialized.seek(0) + batch = torch.load(batch_deserialized, + weights_only=False, + map_location='cpu' if not torch.cuda.is_available() else None) + self.batch = batch + self.non_tensor_batch = non_tensor_batch + self.meta_info = meta_info + + def check_consistency(self): + """Check the consistency of the DataProto. Mainly for batch and non_tensor_batch + We expose this function as a public one so that user can call themselves directly + """ + if self.batch is not None: + assert len(self.batch.batch_size) == 1, 'only support num_batch_dims=1' + + if len(self.non_tensor_batch) != 0: + # TODO: we can actually lift this restriction if needed + assert len(self.batch.batch_size) == 1, 'only support num_batch_dims=1 when non_tensor_batch is not empty.' + + batch_size = self.batch.batch_size[0] + for key, val in self.non_tensor_batch.items(): + assert isinstance( + val, np.ndarray + ) and val.dtype == object, 'data in the non_tensor_batch must be a numpy.array with dtype=object' + assert val.shape[ + 0] == batch_size, f'key {key} length {len(val)} is not equal to batch size {batch_size}' + + @classmethod + def from_single_dict(cls, data: Dict[str, Union[torch.Tensor, np.ndarray]], meta_info=None): + tensors = {} + non_tensors = {} + + for key, val in data.items(): + if isinstance(val, torch.Tensor): + tensors[key] = val + elif isinstance(val, np.ndarray): + non_tensors[key] = val + else: + raise ValueError(f'Unsupported type in data {type(val)}') + + return DataProto.from_dict(tensors=tensors, non_tensors=non_tensors, meta_info=meta_info) + + @classmethod + def from_dict(cls, tensors: Dict[str, torch.Tensor], non_tensors=None, meta_info=None, num_batch_dims=1): + """Create a DataProto from a dict of tensors. This assumes that + 1. All the tensor in tensors have the same dim0 + 2. Only dim0 is the batch dim + """ + assert len(tensors) > 0, 'tensors must not be empty' + assert num_batch_dims > 0, 'num_batch_dims must be greater than zero' + if non_tensors is not None: + assert num_batch_dims == 1, 'only support num_batch_dims=1 when non_tensors is not None.' + + if meta_info is None: + meta_info = {} + if non_tensors is None: + non_tensors = {} + + assert isinstance(non_tensors, dict) + + # get and check batch size + batch_size = None + pivot_key = None + for key, tensor in tensors.items(): + if batch_size is None: + batch_size = tensor.shape[:num_batch_dims] + pivot_key = key + else: + current_batch = tensor.shape[:num_batch_dims] + assert batch_size == current_batch, \ + f'Not all the tensor in tensors have the same batch size with batch_dims={num_batch_dims}. Got {pivot_key} has {batch_size}, {key} has {current_batch}' + + for key, val in non_tensors.items(): + non_tensors[key] = np.array(val, dtype=object) + + tensor_dict = TensorDict(source=tensors, batch_size=batch_size) + return cls(batch=tensor_dict, non_tensor_batch=non_tensors, meta_info=meta_info) + + def to(self, device) -> 'DataProto': + """move the batch to device + + Args: + device (torch.device, str): torch device + + Returns: + DataProto: the current DataProto + + """ + if self.batch is not None: + self.batch = self.batch.to(device) + return self + + def select(self, batch_keys=None, non_tensor_batch_keys=None, meta_info_keys=None, deepcopy=False) -> 'DataProto': + """Select a subset of the DataProto via batch_keys and meta_info_keys + + Args: + batch_keys (list, optional): a list of strings indicating the keys in batch to select + meta_info_keys (list, optional): a list of keys indicating the meta info to select + + Returns: + DataProto: the DataProto with the selected batch_keys and meta_info_keys + """ + # TODO (zhangchi.usc1992) whether to copy + if batch_keys is not None: + batch_keys = tuple(batch_keys) + sub_batch = self.batch.select(*batch_keys) + else: + sub_batch = self.batch + + if non_tensor_batch_keys is not None: + non_tensor_batch = {key: val for key, val in self.non_tensor_batch.items() if key in non_tensor_batch_keys} + else: + non_tensor_batch = self.non_tensor_batch + + if deepcopy: + non_tensor_batch = copy.deepcopy(non_tensor_batch) + + if meta_info_keys is not None: + sub_meta_info = {key: val for key, val in self.meta_info.items() if key in meta_info_keys} + else: + sub_meta_info = self.meta_info + + if deepcopy: + sub_meta_info = copy.deepcopy(sub_meta_info) + + return DataProto(batch=sub_batch, non_tensor_batch=non_tensor_batch, meta_info=sub_meta_info) + + def pop(self, batch_keys=None, non_tensor_batch_keys=None, meta_info_keys=None) -> 'DataProto': + """Pop a subset of the DataProto via `batch_keys` and `meta_info_keys` + + Args: + batch_keys (list, optional): a list of strings indicating the keys in batch to pop + meta_info_keys (list, optional): a list of keys indicating the meta info to pop + + Returns: + DataProto: the DataProto with the poped batch_keys and meta_info_keys + """ + assert batch_keys is not None + if meta_info_keys is None: + meta_info_keys = [] + if non_tensor_batch_keys is None: + non_tensor_batch_keys = [] + + tensors = {} + # tensor batch + for key in batch_keys: + assert key in self.batch.keys() + tensors[key] = self.batch.pop(key) + non_tensors = {} + # non tensor batch + for key in non_tensor_batch_keys: + assert key in self.non_tensor_batch.keys() + non_tensors[key] = self.non_tensor_batch.pop(key) + meta_info = {} + for key in meta_info_keys: + assert key in self.meta_info.keys() + meta_info[key] = self.meta_info.pop(key) + return DataProto.from_dict(tensors=tensors, non_tensors=non_tensors, meta_info=meta_info) + + def rename(self, old_keys=None, new_keys=None) -> 'DataProto': + """ + Note that this function only rename the key in the batch + """ + + def validate_input(keys): + if keys is not None: + if isinstance(keys, str): + keys = [keys] + elif isinstance(keys, list): + pass + else: + raise TypeError(f'keys must be a list or a string, but got {type(keys)}') + return keys + + old_keys = validate_input(old_keys) + new_keys = validate_input(new_keys) + + if len(new_keys) != len(old_keys): + raise ValueError( + f'new_keys and old_keys must have the same length, but got {len(new_keys)} and {len(old_keys)}') + + self.batch.rename_key_(tuple(old_keys), tuple(new_keys)) + + return self + + def union(self, other: 'DataProto') -> 'DataProto': + """Union with another DataProto. Union batch and meta_info separately. + Throw an error if + - there are conflict keys in batch and they are not equal + - the batch size of two data batch is not the same + - there are conflict keys in meta_info and they are not the same. + + Args: + other (DataProto): another DataProto to union + + Returns: + DataProto: the DataProto after union + """ + self.batch = union_tensor_dict(self.batch, other.batch) + self.non_tensor_batch = union_numpy_dict(self.non_tensor_batch, other.non_tensor_batch) + self.meta_info = union_two_dict(self.meta_info, other.meta_info) + return self + + def make_iterator(self, mini_batch_size, epochs, seed=None, dataloader_kwargs=None): + """Make an iterator from the DataProto. This is built upon that TensorDict can be used as a normal Pytorch + dataset. See https://pytorch.org/tensordict/tutorials/data_fashion for more details. + + Args: + mini_batch_size (int): mini-batch size when iterating the dataset. We require that + ``batch.batch_size[0] % mini_batch_size == 0`` + epochs (int): number of epochs when iterating the dataset. + dataloader_kwargs: internally, it returns a DataLoader over the batch. + The dataloader_kwargs is the kwargs passed to the DataLoader + + Returns: + Iterator: an iterator that yields a mini-batch data at a time. The total number of iteration steps is + ``self.batch.batch_size * epochs // mini_batch_size`` + """ + assert self.batch.batch_size[0] % mini_batch_size == 0, f"{self.batch.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) + train_dataloader = DataLoader(dataset=self, + batch_size=mini_batch_size, + collate_fn=collate_fn, + generator=generator, + **dataloader_kwargs) + + def get_data(): + for _ in range(epochs): + for d in train_dataloader: + d.meta_info = self.meta_info + yield d + + return iter(get_data()) + + def chunk(self, chunks: int) -> List['DataProto']: + """Split the batch among dim=0 into chunks. The meta_info is passed to each DataProto after split. + + Args: + chunks (int): the number of chunks to split on dim=0 + + Returns: + List[DataProto]: a list of DataProto after splitting + """ + if self.batch is not None: + batch_lst = self.batch.chunk(chunks=chunks, dim=0) + else: + batch_lst = [None for _ in range(chunks)] + + non_tensor_batch_lst = [{} for _ in range(chunks)] + for key, val in self.non_tensor_batch.items(): + assert isinstance(val, np.ndarray) + non_tensor_lst = np.array_split(val, chunks) + assert len(non_tensor_lst) == chunks + for i in range(chunks): + non_tensor_batch_lst[i][key] = non_tensor_lst[i] + + output = [] + for i in range(chunks): + output.append( + DataProto(batch=batch_lst[i], non_tensor_batch=non_tensor_batch_lst[i], meta_info=self.meta_info)) + + return output + + @staticmethod + def concat(data: List['DataProto']) -> 'DataProto': + """Concat a list of DataProto. The batch is concatenated among dim=0. + The meta_info is assumed to be identical and will use the first one. + + Args: + data (List[DataProto]): list of DataProto + + Returns: + DataProto: concatenated DataProto + """ + batch_lst = [] + for batch in data: + batch_lst.append(batch.batch) + if batch_lst[0] is not None: + new_batch = torch.cat(batch_lst, dim=0) + else: + new_batch = None + + non_tensor_batch = list_of_dict_to_dict_of_list(list_of_dict=[d.non_tensor_batch for d in data]) + for key, val in non_tensor_batch.items(): + non_tensor_batch[key] = np.concatenate(val, axis=0) + + return DataProto(batch=new_batch, non_tensor_batch=non_tensor_batch, meta_info=data[0].meta_info) + + def reorder(self, indices): + """ + Note that this operation is in-place + """ + indices_np = indices.detach().numpy() + self.batch = self.batch[indices] + self.non_tensor_batch = {key: val[indices_np] for key, val in self.non_tensor_batch.items()} + + +import ray + + +@dataclass +class DataProtoFuture: + """ + DataProtoFuture aims to eliminate actual data fetching on driver. By doing so, the driver doesn't have to wait + for data so that asynchronous execution becomes possible. + DataProtoFuture contains a list of futures from another WorkerGroup of size world_size. + - collect_fn is a Callable that reduces the list of futures to a DataProto + - dispatch_fn is a Callable that partitions the DataProto into a list of DataProto of size world_size and then select + + Potential issue: we can optimize dispatch_fn(collect_fn) such that only needed data is fetched on destination + - DataProtoFuture only supports directly passing from the output of a method to another input. You can't perform any + operation on the DataProtoFuture in driver. + """ + collect_fn: Callable + futures: List[ray.ObjectRef] + dispatch_fn: Callable = None + + @staticmethod + def concat(data: List[ray.ObjectRef]) -> 'DataProtoFuture': + output = DataProtoFuture(collect_fn=DataProto.concat, futures=data) + return output + + def chunk(self, chunks: int) -> List['DataProtoFuture']: + from functools import partial + + arg_future_lst = [] + for i in range(chunks): + # note that we can't directly pass i and chunks + def dispatch_fn(x, i, chunks): + return x.chunk(chunks=chunks)[i] + + arg_future = DataProtoFuture(collect_fn=self.collect_fn, + dispatch_fn=partial(dispatch_fn, i=i, chunks=chunks), + futures=self.futures) + arg_future_lst.append(arg_future) + return arg_future_lst + + def get(self): + output = ray.get(self.futures) # dp_size. + for o in output: + assert isinstance(o, DataProto) + output = self.collect_fn(output) # select dp, concat + if self.dispatch_fn is not None: + output = self.dispatch_fn(output) # split in batch dim, select using dp + return output diff --git a/verl/third_party/__init__.py b/verl/third_party/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/third_party/__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/third_party/vllm/__init__.py b/verl/third_party/vllm/__init__.py new file mode 100644 index 000000000..9eee28f7a --- /dev/null +++ b/verl/third_party/vllm/__init__.py @@ -0,0 +1,45 @@ +# 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 importlib.metadata import version, PackageNotFoundError + + +def get_version(pkg): + try: + return version(pkg) + except PackageNotFoundError: + return None + + +package_name = 'vllm' +package_version = get_version(package_name) + +if package_version == '0.3.1': + vllm_version = '0.3.1' + from .vllm_v_0_3_1.llm import LLM + from .vllm_v_0_3_1.llm import LLMEngine + from .vllm_v_0_3_1 import parallel_state +elif package_version == '0.4.2': + vllm_version = '0.4.2' + from .vllm_v_0_4_2.llm import LLM + from .vllm_v_0_4_2.llm import LLMEngine + from .vllm_v_0_4_2 import parallel_state +elif package_version == '0.5.4': + vllm_version = '0.5.4' + from .vllm_v_0_5_4.llm import LLM + from .vllm_v_0_5_4.llm import LLMEngine + from .vllm_v_0_5_4 import parallel_state +else: + raise ValueError( + f'vllm version {package_version} not supported. Currently supported versions are 0.3.1, 0.4.2, and 0.5.4.') diff --git a/verl/third_party/vllm/vllm_v_0_3_1/__init__.py b/verl/third_party/vllm/vllm_v_0_3_1/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/__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/third_party/vllm/vllm_v_0_3_1/arg_utils.py b/verl/third_party/vllm/vllm_v_0_3_1/arg_utils.py new file mode 100644 index 000000000..1ae8f3b8f --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/arg_utils.py @@ -0,0 +1,228 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/arg_utils.py +import argparse +import dataclasses +from dataclasses import dataclass +from typing import Dict, Optional, Tuple + +import torch.nn as nn +from vllm.config import (CacheConfig, DeviceConfig, ModelConfig, ParallelConfig, SchedulerConfig, LoRAConfig) +from transformers import PretrainedConfig +from .config import ModelConfig + + +@dataclass +class EngineArgs: + """Arguments for vLLM engine.""" + model_hf_config: PretrainedConfig = None + dtype: str = 'auto' + kv_cache_dtype: str = 'auto' + seed: int = 0 + max_model_len: Optional[int] = None + worker_use_ray: bool = False + pipeline_parallel_size: int = 1 + tensor_parallel_size: int = 1 + max_parallel_loading_workers: Optional[int] = None + block_size: int = 16 + swap_space: int = 4 # GiB + gpu_memory_utilization: float = 0.90 + max_num_batched_tokens: Optional[int] = None + max_num_seqs: int = 256 + max_paddings: int = 256 + disable_log_stats: bool = False + revision: Optional[str] = None + tokenizer_revision: Optional[str] = None + quantization: Optional[str] = None + load_format: str = 'model' + enforce_eager: bool = False + max_context_len_to_capture: int = 8192 + disable_custom_all_reduce: bool = False + enable_lora: bool = False + max_loras: int = 1 + max_lora_rank: int = 16 + lora_extra_vocab_size: int = 256 + lora_dtype = 'auto' + max_cpu_loras: Optional[int] = None + device: str = 'cuda' + + @staticmethod + def add_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: + """Shared CLI arguments for vLLM engine.""" + # Model arguments + # TODO(shengguangming): delete the unused args + parser.add_argument('--model', + type=str, + default='facebook/opt-125m', + help='name or path of the huggingface model to use') + parser.add_argument('--tokenizer', + type=str, + default=EngineArgs.tokenizer, + help='name or path of the huggingface tokenizer to use') + parser.add_argument('--revision', + type=str, + default=None, + help='the specific model version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-revision', + type=str, + default=None, + help='the specific tokenizer version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-mode', + type=str, + default=EngineArgs.tokenizer_mode, + choices=['auto', 'slow'], + help='tokenizer mode. "auto" will use the fast ' + 'tokenizer if available, and "slow" will ' + 'always use the slow tokenizer.') + parser.add_argument('--trust-remote-code', action='store_true', help='trust remote code from huggingface') + parser.add_argument('--download-dir', + type=str, + default=EngineArgs.download_dir, + help='directory to download and load the weights, ' + 'default to the default cache dir of ' + 'huggingface') + parser.add_argument('--load-format', + type=str, + default=EngineArgs.load_format, + choices=['auto', 'pt', 'safetensors', 'npcache', 'dummy'], + help='The format of the model weights to load. ' + '"auto" will try to load the weights in the safetensors format ' + 'and fall back to the pytorch bin format if safetensors format ' + 'is not available. ' + '"pt" will load the weights in the pytorch bin format. ' + '"safetensors" will load the weights in the safetensors format. ' + '"npcache" will load the weights in pytorch format and store ' + 'a numpy cache to speed up the loading. ' + '"dummy" will initialize the weights with random values, ' + 'which is mainly for profiling.') + parser.add_argument('--dtype', + type=str, + default=EngineArgs.dtype, + choices=['auto', 'half', 'float16', 'bfloat16', 'float', 'float32'], + help='data type for model weights and activations. ' + 'The "auto" option will use FP16 precision ' + 'for FP32 and FP16 models, and BF16 precision ' + 'for BF16 models.') + parser.add_argument('--max-model-len', + type=int, + default=None, + help='model context length. If unspecified, ' + 'will be automatically derived from the model.') + # Parallel arguments + parser.add_argument('--worker-use-ray', + action='store_true', + help='use Ray for distributed serving, will be ' + 'automatically set when using more than 1 GPU') + parser.add_argument('--pipeline-parallel-size', + '-pp', + type=int, + default=EngineArgs.pipeline_parallel_size, + help='number of pipeline stages') + parser.add_argument('--tensor-parallel-size', + '-tp', + type=int, + default=EngineArgs.tensor_parallel_size, + help='number of tensor parallel replicas') + # KV cache arguments + parser.add_argument('--block-size', + type=int, + default=EngineArgs.block_size, + choices=[8, 16, 32], + help='token block size') + # TODO(woosuk): Support fine-grained seeds (e.g., seed per request). + parser.add_argument('--seed', type=int, default=EngineArgs.seed, help='random seed') + parser.add_argument('--swap-space', + type=int, + default=EngineArgs.swap_space, + help='CPU swap space size (GiB) per GPU') + parser.add_argument('--gpu-memory-utilization', + type=float, + default=EngineArgs.gpu_memory_utilization, + help='the percentage of GPU memory to be used for' + 'the model executor') + parser.add_argument('--max-num-batched-tokens', + type=int, + default=EngineArgs.max_num_batched_tokens, + help='maximum number of batched tokens per ' + 'iteration') + parser.add_argument('--max-num-seqs', + type=int, + default=EngineArgs.max_num_seqs, + help='maximum number of sequences per iteration') + parser.add_argument('--disable-log-stats', action='store_true', help='disable logging statistics') + # Quantization settings. + parser.add_argument('--quantization', + '-q', + type=str, + choices=['awq', None], + default=None, + help='Method used to quantize the weights') + return parser + + @classmethod + def from_cli_args(cls, args: argparse.Namespace) -> 'EngineArgs': + # Get the list of attributes of this dataclass. + attrs = [attr.name for attr in dataclasses.fields(cls)] + # Set the attributes from the parsed arguments. + engine_args = cls(**{attr: getattr(args, attr) for attr in attrs}) + return engine_args + + def create_engine_configs( + self, + ) -> Tuple[ModelConfig, CacheConfig, ParallelConfig, SchedulerConfig]: + device_config = DeviceConfig(self.device) + model_config = ModelConfig(self.model_hf_config, self.dtype, self.seed, self.load_format, self.revision, + self.tokenizer_revision, self.max_model_len, self.quantization, self.enforce_eager, + self.max_context_len_to_capture) + cache_config = CacheConfig(self.block_size, self.gpu_memory_utilization, self.swap_space, self.kv_cache_dtype, + model_config.get_sliding_window()) + parallel_config = ParallelConfig(self.pipeline_parallel_size, self.tensor_parallel_size, self.worker_use_ray, + self.max_parallel_loading_workers, self.disable_custom_all_reduce) + scheduler_config = SchedulerConfig(self.max_num_batched_tokens, self.max_num_seqs, model_config.max_model_len, + self.max_paddings) + lora_config = LoRAConfig(max_lora_rank=self.max_lora_rank, + max_loras=self.max_loras, + lora_extra_vocab_size=self.lora_extra_vocab_size, + lora_dtype=self.lora_dtype, + max_cpu_loras=self.max_cpu_loras if self.max_cpu_loras and self.max_cpu_loras > 0 else + None) if self.enable_lora else None + return (model_config, cache_config, parallel_config, scheduler_config, device_config, lora_config) + + +@dataclass +class AsyncEngineArgs(EngineArgs): + """Arguments for asynchronous vLLM engine.""" + engine_use_ray: bool = False + disable_log_requests: bool = False + max_log_len: Optional[int] = None + + @staticmethod + def add_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: + parser = EngineArgs.add_cli_args(parser) + parser.add_argument('--engine-use-ray', + action='store_true', + help='use Ray to start the LLM engine in a ' + 'separate process as the server process.') + parser.add_argument('--disable-log-requests', action='store_true', help='disable logging requests') + parser.add_argument('--max-log-len', + type=int, + default=None, + help='max number of prompt characters or prompt ' + 'ID numbers being printed in log. ' + 'Default: unlimited.') + return parser diff --git a/verl/third_party/vllm/vllm_v_0_3_1/config.py b/verl/third_party/vllm/vllm_v_0_3_1/config.py new file mode 100644 index 000000000..734dfc7a2 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/config.py @@ -0,0 +1,578 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/config.py + + +from typing import Optional, Union, ClassVar +from dataclasses import dataclass +import torch +from transformers import PretrainedConfig +from packaging.version import Version + +from vllm.logger import init_logger +from vllm.transformers_utils.config import get_config +from vllm.utils import get_cpu_memory, is_hip, get_nvcc_cuda_version + +logger = init_logger(__name__) + +_GB = 1 << 30 + + +class ModelConfig: + """Configuration for the model. + + Args: + model: Name or path of the huggingface model to use. + tokenizer: Name or path of the huggingface tokenizer to use. + tokenizer_mode: Tokenizer mode. "auto" will use the fast tokenizer if + available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + download_dir: Directory to download and load the weights, default to the + default cache directory of huggingface. + load_format: The format of the model weights to load: + "auto" will try to load the weights in the safetensors format and + fall back to the pytorch bin format if safetensors format is + not available. + "pt" will load the weights in the pytorch bin format. + "safetensors" will load the weights in the safetensors format. + "npcache" will load the weights in pytorch format and store + a numpy cache to speed up the loading. + "dummy" will initialize the weights with random values, which is + mainly for profiling. + dtype: Data type for model weights and activations. The "auto" option + will use FP16 precision for FP32 and FP16 models, and BF16 precision + for BF16 models. + seed: Random seed for reproducibility. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. If unspecified, will use the default + version. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. If unspecified, will use + the default version. + max_model_len: Maximum length of a sequence (including prompt and + output). If None, will be derived from the model. + quantization: Quantization method that was used to quantize the model + weights. If None, we assume the model weights are not quantized. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode. + """ + + def __init__( + self, + hf_config: PretrainedConfig, + dtype: str, + seed: int, + load_format: str = 'model', + revision: Optional[str] = None, + tokenizer_revision: Optional[str] = None, + max_model_len: Optional[int] = None, + quantization: Optional[str] = None, + trust_remote_code: Optional[bool] = True, + enforce_eager: bool = False, + max_context_len_to_capture: Optional[int] = None, + ) -> None: + self.model = hf_config._name_or_path + self.tokenizer = hf_config._name_or_path + self.load_format = load_format + self.seed = seed + self.revision = revision + self.tokenizer_revision = tokenizer_revision + self.quantization = quantization + self.trust_remote_code = trust_remote_code + self.enforce_eager = enforce_eager + self.max_context_len_to_capture = max_context_len_to_capture + + # self.hf_config = get_config(model, trust_remote_code, revision) + self.hf_config = hf_config + self.dtype = _get_and_verify_dtype(self.hf_config, dtype) + self.max_model_len = _get_and_verify_max_len(self.hf_config, max_model_len) + # self._verify_load_format() + # self._verify_tokenizer_mode() + self._verify_quantization() + self._verify_cuda_graph() + + def _verify_load_format(self) -> None: + load_format = self.load_format.lower() + if load_format not in ["auto", "pt", "safetensors", "npcache", "dummy", "model"]: + raise ValueError(f"Unknown load format: {self.load_format}. Must be one of " + "'auto', 'pt', 'safetensors', 'npcache', 'dummy' or 'model'.") + self.load_format = load_format + + # def _verify_tokenizer_mode(self) -> None: + # tokenizer_mode = self.tokenizer_mode.lower() + # if tokenizer_mode not in ["auto", "slow"]: + # raise ValueError( + # f"Unknown tokenizer mode: {self.tokenizer_mode}. Must be " + # "either 'auto' or 'slow'.") + # self.tokenizer_mode = tokenizer_mode + + def _verify_quantization(self) -> None: + supported_quantization = ["awq", "gptq", "squeezellm"] + rocm_not_supported_quantization = ["awq", "gptq"] + if self.quantization is not None: + self.quantization = self.quantization.lower() + + # Parse quantization method from the HF model config, if available. + hf_quant_config = getattr(self.hf_config, "quantization_config", None) + if hf_quant_config is not None: + hf_quant_method = str(hf_quant_config["quant_method"]).lower() + if self.quantization is None: + self.quantization = hf_quant_method + elif self.quantization != hf_quant_method: + raise ValueError("Quantization method specified in the model config " + f"({hf_quant_method}) does not match the quantization " + f"method specified in the `quantization` argument " + f"({self.quantization}).") + + if self.quantization is not None: + if self.quantization not in supported_quantization: + raise ValueError(f"Unknown quantization method: {self.quantization}. Must " + f"be one of {supported_quantization}.") + if is_hip() and self.quantization in rocm_not_supported_quantization: + raise ValueError(f"{self.quantization} quantization is currently not supported " + f"in ROCm.") + logger.warning(f"{self.quantization} quantization is not fully " + "optimized yet. The speed can be slower than " + "non-quantized models.") + + def _verify_cuda_graph(self) -> None: + if self.max_context_len_to_capture is None: + self.max_context_len_to_capture = self.max_model_len + self.max_context_len_to_capture = min(self.max_context_len_to_capture, self.max_model_len) + if (self.quantization in ["gptq", "squeezellm"] and not self.enforce_eager): + # Related issue: https://github.com/vllm-project/vllm/issues/2147 + logger.warning(f"{self.quantization} does not support CUDA graph " + "yet. Disabling CUDA graph.") + self.enforce_eager = True + + def verify_with_parallel_config( + self, + parallel_config: "ParallelConfig", + ) -> None: + total_num_attention_heads = self.hf_config.num_attention_heads + tensor_parallel_size = parallel_config.tensor_parallel_size + if total_num_attention_heads % tensor_parallel_size != 0: + raise ValueError(f"Total number of attention heads ({total_num_attention_heads})" + " must be divisible by tensor parallel size " + f"({tensor_parallel_size}).") + + total_num_hidden_layers = self.hf_config.num_hidden_layers + pipeline_parallel_size = parallel_config.pipeline_parallel_size + if total_num_hidden_layers % pipeline_parallel_size != 0: + raise ValueError(f"Total number of hidden layers ({total_num_hidden_layers}) " + "must be divisible by pipeline parallel size " + f"({pipeline_parallel_size}).") + + def get_sliding_window(self) -> Optional[int]: + return getattr(self.hf_config, "sliding_window", None) + + def get_vocab_size(self) -> int: + return self.hf_config.vocab_size + + def get_hidden_size(self) -> int: + return self.hf_config.hidden_size + + def get_head_size(self) -> int: + # FIXME(woosuk): This may not be true for all models. + return self.hf_config.hidden_size // self.hf_config.num_attention_heads + + def get_total_num_kv_heads(self) -> int: + """Returns the total number of KV heads.""" + # For GPTBigCode & Falcon: + # NOTE: for falcon, when new_decoder_architecture is True, the + # multi_query flag is ignored and we use n_head_kv for the number of + # KV heads. + falcon_model_types = ["falcon", "RefinedWeb", "RefinedWebModel"] + new_decoder_arch_falcon = (self.hf_config.model_type in falcon_model_types and + getattr(self.hf_config, "new_decoder_architecture", False)) + if not new_decoder_arch_falcon and getattr(self.hf_config, "multi_query", False): + # Multi-query attention, only one KV head. + # Currently, tensor parallelism is not supported in this case. + return 1 + + attributes = [ + # For Falcon: + "n_head_kv", + "num_kv_heads", + # For LLaMA-2: + "num_key_value_heads", + # For ChatGLM: + "multi_query_group_num", + ] + for attr in attributes: + num_kv_heads = getattr(self.hf_config, attr, None) + if num_kv_heads is not None: + return num_kv_heads + + # For non-grouped-query attention models, the number of KV heads is + # equal to the number of attention heads. + return self.hf_config.num_attention_heads + + def get_num_kv_heads(self, parallel_config: "ParallelConfig") -> int: + """Returns the number of KV heads per GPU.""" + total_num_kv_heads = self.get_total_num_kv_heads() + # If tensor parallelism is used, we divide the number of KV heads by + # the tensor parallel size. We will replicate the KV heads in the + # case where the number of KV heads is smaller than the tensor + # parallel size so each GPU has at least one KV head. + return max(1, total_num_kv_heads // parallel_config.tensor_parallel_size) + + def get_num_layers(self, parallel_config: "ParallelConfig") -> int: + total_num_hidden_layers = self.hf_config.num_hidden_layers + return total_num_hidden_layers // parallel_config.pipeline_parallel_size + + +class CacheConfig: + """Configuration for the KV cache. + + Args: + block_size: Size of a cache block in number of tokens. + gpu_memory_utilization: Fraction of GPU memory to use for the + vLLM execution. + swap_space: Size of the CPU swap space per GPU (in GiB). + cache_dtype: Data type for kv cache storage. + """ + + def __init__( + self, + block_size: int, + gpu_memory_utilization: float, + swap_space: int, + cache_dtype: str, + sliding_window: Optional[int] = None, + ) -> None: + self.block_size = block_size + self.gpu_memory_utilization = gpu_memory_utilization + self.swap_space_bytes = swap_space * _GB + self.cache_dtype = cache_dtype + self.sliding_window = sliding_window + self._verify_args() + self._verify_cache_dtype() + + # Will be set after profiling. + self.num_gpu_blocks = None + self.num_cpu_blocks = None + + def _verify_args(self) -> None: + if self.gpu_memory_utilization > 1.0: + raise ValueError("GPU memory utilization must be less than 1.0. Got " + f"{self.gpu_memory_utilization}.") + + def _verify_cache_dtype(self) -> None: + if self.cache_dtype == "auto": + pass + elif self.cache_dtype == "fp8_e5m2": + nvcc_cuda_version = get_nvcc_cuda_version() + if nvcc_cuda_version < Version("11.8"): + raise ValueError("FP8 is not supported when cuda version is lower than 11.8.") + device_name = torch.cuda.get_device_name() + if "AMD" in device_name: + raise NotImplementedError("FP8_E5M2 KV Cache on AMD GPU has not been supported yet.") + logger.info("Using fp8_e5m2 data type to store kv cache. It reduces " + "the GPU memory footprint and boosts the performance. " + "But it may cause slight accuracy drop. " + "Currently we only support fp8 without scaling factors and " + "make e5m2 as a default format.") + else: + raise ValueError(f"Unknown kv cache dtype: {self.cache_dtype}") + + def verify_with_parallel_config( + self, + parallel_config: "ParallelConfig", + ) -> None: + total_cpu_memory = get_cpu_memory() + # FIXME(woosuk): Here, it is assumed that the GPUs in a tensor parallel + # group are in the same node. However, the GPUs may span multiple nodes. + num_gpus_per_node = parallel_config.tensor_parallel_size + cpu_memory_usage = self.swap_space_bytes * num_gpus_per_node + + msg = (f"{cpu_memory_usage / _GB:.2f} GiB out of " + f"the {total_cpu_memory / _GB:.2f} GiB total CPU memory is " + "allocated for the swap space.") + if cpu_memory_usage > 0.7 * total_cpu_memory: + raise ValueError("Too large swap space. " + msg) + elif cpu_memory_usage > 0.4 * total_cpu_memory: + logger.warning("Possibly too large swap space. " + msg) + + +class ParallelConfig: + """Configuration for the distributed execution. + + Args: + pipeline_parallel_size: Number of pipeline parallel groups. + tensor_parallel_size: Number of tensor parallel groups. + worker_use_ray: Whether to use Ray for model workers. Will be set to + True if either pipeline_parallel_size or tensor_parallel_size is + greater than 1. + max_parallel_loading_workers: Maximum number of multiple batches + when load model sequentially. To avoid RAM OOM when using tensor + parallel and large models. + disable_custom_all_reduce: Disable the custom all-reduce kernel and + fall back to NCCL. + """ + + def __init__( + self, + pipeline_parallel_size: int, + tensor_parallel_size: int, + worker_use_ray: bool, + max_parallel_loading_workers: Optional[int] = None, + disable_custom_all_reduce: bool = False, + ) -> None: + self.pipeline_parallel_size = pipeline_parallel_size + self.tensor_parallel_size = tensor_parallel_size + self.worker_use_ray = worker_use_ray + self.max_parallel_loading_workers = max_parallel_loading_workers + self.disable_custom_all_reduce = disable_custom_all_reduce + + self.world_size = pipeline_parallel_size * tensor_parallel_size + if self.world_size > 1: + self.worker_use_ray = True + self._verify_args() + + def _verify_args(self) -> None: + if self.pipeline_parallel_size > 1: + raise NotImplementedError("Pipeline parallelism is not supported yet.") + if not self.disable_custom_all_reduce and self.world_size > 1: + if is_hip(): + self.disable_custom_all_reduce = True + logger.info("Disabled the custom all-reduce kernel because it is not " + "supported on AMD GPUs.") + elif self.pipeline_parallel_size > 1: + self.disable_custom_all_reduce = True + logger.info("Disabled the custom all-reduce kernel because it is not " + "supported with pipeline parallelism.") + + # FIXME(woosuk): Fix the stability issues and re-enable the custom + # all-reduce kernel. + if not self.disable_custom_all_reduce and self.world_size > 1: + self.disable_custom_all_reduce = True + logger.info("Custom all-reduce kernels are temporarily disabled due to " + "stability issues. We will re-enable them once the issues are " + "resolved.") + + +class SchedulerConfig: + """Scheduler configuration. + + Args: + max_num_batched_tokens: Maximum number of tokens to be processed in + a single iteration. + max_num_seqs: Maximum number of sequences to be processed in a single + iteration. + max_model_len: Maximum length of a sequence (including prompt + and generated text). + max_paddings: Maximum number of paddings to be added to a batch. + """ + + def __init__( + self, + max_num_batched_tokens: Optional[int], + max_num_seqs: int, + max_model_len: int, + max_paddings: int, + ) -> None: + if max_num_batched_tokens is not None: + self.max_num_batched_tokens = max_num_batched_tokens + else: + # If max_model_len is too short, use 2048 as the default value for + # higher throughput. + self.max_num_batched_tokens = max(max_model_len, 2048) + self.max_num_seqs = max_num_seqs + self.max_model_len = max_model_len + self.max_paddings = max_paddings + self._verify_args() + + def _verify_args(self) -> None: + if self.max_num_batched_tokens < self.max_model_len: + raise ValueError(f"max_num_batched_tokens ({self.max_num_batched_tokens}) is " + f"smaller than max_model_len ({self.max_model_len}). " + "This effectively limits the maximum sequence length to " + "max_num_batched_tokens and makes vLLM reject longer " + "sequences. Please increase max_num_batched_tokens or " + "decrease max_model_len.") + if self.max_num_batched_tokens < self.max_num_seqs: + raise ValueError(f"max_num_batched_tokens ({self.max_num_batched_tokens}) must " + "be greater than or equal to max_num_seqs " + f"({self.max_num_seqs}).") + + +class DeviceConfig: + + def __init__(self, device: str = "cuda") -> None: + self.device = torch.device(device) + + +@dataclass +class LoRAConfig: + max_lora_rank: int + max_loras: int + max_cpu_loras: Optional[int] = None + lora_dtype: Optional[torch.dtype] = None + lora_extra_vocab_size: int = 256 + # This is a constant. + lora_vocab_padding_size: ClassVar[int] = 256 + + def __post_init__(self): + # Keep this in sync with csrc/punica/bgmv/bgmv_config.h + possible_max_ranks = (8, 16, 32, 64) + possible_lora_extra_vocab_size = (0, 256, 512) + if self.max_lora_rank not in possible_max_ranks: + raise ValueError(f"max_lora_rank ({self.max_lora_rank}) must be one of " + f"{possible_max_ranks}.") + if self.lora_extra_vocab_size not in possible_lora_extra_vocab_size: + raise ValueError(f"lora_extra_vocab_size ({self.lora_extra_vocab_size}) " + f"must be one of {possible_lora_extra_vocab_size}.") + if self.max_loras < 1: + raise ValueError(f"max_loras ({self.max_loras}) must be >= 1.") + if self.max_cpu_loras is None: + self.max_cpu_loras = self.max_loras + elif self.max_cpu_loras < self.max_loras: + raise ValueError(f"max_cpu_loras ({self.max_cpu_loras}) must be >= " + f"max_loras ({self.max_loras})") + + def verify_with_model_config(self, model_config: ModelConfig): + if self.lora_dtype in (None, "auto"): + self.lora_dtype = model_config.dtype + elif isinstance(self.lora_dtype, str): + self.lora_dtype = getattr(torch, self.lora_dtype) + if model_config.quantization is not None: + raise ValueError("LoRA is not supported with quantized models yet.") + + def verify_with_scheduler_config(self, scheduler_config: SchedulerConfig): + if scheduler_config.max_num_batched_tokens > 65528: + raise ValueError("Due to limitations of the custom LoRA CUDA kernel, " + "max_num_batched_tokens must be <= 65528 when " + "LoRA is enabled.") + + +_STR_DTYPE_TO_TORCH_DTYPE = { + "half": torch.float16, + "float16": torch.float16, + "float": torch.float32, + "float32": torch.float32, + "bfloat16": torch.bfloat16, +} + +_ROCM_NOT_SUPPORTED_DTYPE = ["float", "float32"] + + +def _get_and_verify_dtype( + config: PretrainedConfig, + dtype: Union[str, torch.dtype], +) -> torch.dtype: + # NOTE: getattr(config, "torch_dtype", torch.float32) is not correct + # because config.torch_dtype can be None. + config_dtype = getattr(config, "torch_dtype", None) + if config_dtype is None: + config_dtype = torch.float32 + + if isinstance(dtype, str): + dtype = dtype.lower() + if dtype == "auto": + if config_dtype == torch.float32: + # Following the common practice, we use float16 for float32 + # models. + torch_dtype = torch.float16 + else: + torch_dtype = config_dtype + else: + if dtype not in _STR_DTYPE_TO_TORCH_DTYPE: + raise ValueError(f"Unknown dtype: {dtype}") + torch_dtype = _STR_DTYPE_TO_TORCH_DTYPE[dtype] + elif isinstance(dtype, torch.dtype): + torch_dtype = dtype + else: + raise ValueError(f"Unknown dtype: {dtype}") + + if is_hip() and torch_dtype == torch.float32: + rocm_supported_dtypes = [ + k for k, v in _STR_DTYPE_TO_TORCH_DTYPE.items() if (k not in _ROCM_NOT_SUPPORTED_DTYPE) + ] + raise ValueError(f"dtype \'{dtype}\' is not supported in ROCm. " + f"Supported dtypes are {rocm_supported_dtypes}") + + # Verify the dtype. + if torch_dtype != config_dtype: + if torch_dtype == torch.float32: + # Upcasting to float32 is allowed. + pass + elif config_dtype == torch.float32: + # Downcasting from float32 to float16 or bfloat16 is allowed. + pass + else: + # Casting between float16 and bfloat16 is allowed with a warning. + logger.warning(f"Casting {config_dtype} to {torch_dtype}.") + + return torch_dtype + + +def _get_and_verify_max_len( + hf_config: PretrainedConfig, + max_model_len: Optional[int], +) -> int: + """Get and verify the model's maximum length.""" + derived_max_model_len = float("inf") + possible_keys = [ + # OPT + "max_position_embeddings", + # GPT-2 + "n_positions", + # MPT + "max_seq_len", + # ChatGLM2 + "seq_length", + # Others + "max_sequence_length", + "max_seq_length", + "seq_len", + ] + for key in possible_keys: + max_len_key = getattr(hf_config, key, None) + if max_len_key is not None: + derived_max_model_len = min(derived_max_model_len, max_len_key) + if derived_max_model_len == float("inf"): + if max_model_len is not None: + # If max_model_len is specified, we use it. + return max_model_len + + default_max_len = 2048 + logger.warning("The model's config.json does not contain any of the following " + "keys to determine the original maximum length of the model: " + f"{possible_keys}. Assuming the model's maximum length is " + f"{default_max_len}.") + derived_max_model_len = default_max_len + + rope_scaling = getattr(hf_config, "rope_scaling", None) + if rope_scaling is not None: + assert "factor" in rope_scaling + scaling_factor = rope_scaling["factor"] + if rope_scaling["type"] == "yarn": + derived_max_model_len = rope_scaling["original_max_position_embeddings"] + derived_max_model_len *= scaling_factor + + if max_model_len is None: + max_model_len = derived_max_model_len + elif max_model_len > derived_max_model_len: + raise ValueError(f"User-specified max_model_len ({max_model_len}) is greater than " + f"the derived max_model_len ({max_len_key}={derived_max_model_len}" + " in model's config.json). This may lead to incorrect model " + "outputs or CUDA errors. Make sure the value is correct and " + "within the model context size.") + return int(max_model_len) diff --git a/verl/third_party/vllm/vllm_v_0_3_1/llm.py b/verl/third_party/vllm/vllm_v_0_3_1/llm.py new file mode 100644 index 000000000..ab7165491 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/llm.py @@ -0,0 +1,275 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/llm.py + +from typing import Dict, List, Optional, Tuple, Union + +from tqdm import tqdm +from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast +from transformers import PretrainedConfig +import torch.nn as nn +from .arg_utils import EngineArgs +from .llm_engine_sp import LLMEngine +from vllm.lora.request import LoRARequest +from vllm.outputs import RequestOutput +from vllm.sampling_params import SamplingParams +from vllm.utils import Counter +import torch +from torch.nn.utils.rnn import pad_sequence +from verl.trainer.ppo.rollout.tokenizer import HybridEngineBaseTokenizer + + +class LLM: + """An LLM for generating texts from given prompts and sampling parameters. + + This class includes a tokenizer, a language model (possibly distributed + across multiple GPUs), and GPU memory space allocated for intermediate + states (aka KV cache). Given a batch of prompts and sampling parameters, + this class generates texts from the model, using an intelligent batching + mechanism and efficient memory management. + + NOTE: This class is intended to be used for offline inference. For online + serving, use the `AsyncLLMEngine` class instead. + NOTE: For the comprehensive list of arguments, see `EngineArgs`. + + Args: + model: A HuggingFace Transformers model instance. + tokenizer: A HuggingFace Transformers tokenizer instance. + tokenizer_mode: The tokenizer mode. "auto" will use the fast tokenizer + if available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + tensor_parallel_size: The number of GPUs to use for distributed + execution with tensor parallelism. + dtype: The data type for the model weights and activations. Currently, + we support `float32`, `float16`, and `bfloat16`. If `auto`, we use + the `torch_dtype` attribute specified in the model config file. + However, if the `torch_dtype` in the config is `float32`, we will + use `float16` instead. + quantization: The method used to quantize the model weights. Currently, + we support "awq". If None, we assume the model weights are not + quantized and use `dtype` to determine the data type of the weights. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. + seed: The seed to initialize the random number generator for sampling. + gpu_memory_utilization: The ratio (between 0 and 1) of GPU memory to + reserve for the model weights, activations, and KV cache. Higher + values will increase the KV cache size and thus improve the model's + throughput. However, if the value is too high, it may cause out-of- + memory (OOM) errors. + swap_space: The size (GiB) of CPU memory per GPU to use as swap space. + This can be used for temporarily storing the states of the requests + when their `best_of` sampling parameters are larger than 1. If all + requests will have `best_of=1`, you can safely set this to 0. + Otherwise, too small values may cause out-of-memory (OOM) errors. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode. + disable_custom_all_reduce: See ParallelConfig + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer], + model_hf_config: PretrainedConfig, + tokenizer_mode: str = "auto", + trust_remote_code: bool = False, + tensor_parallel_size: int = 1, + dtype: str = "auto", + quantization: Optional[str] = None, + revision: Optional[str] = None, + tokenizer_revision: Optional[str] = None, + seed: int = 0, + gpu_memory_utilization: float = 0.9, + swap_space: int = 4, + enforce_eager: bool = False, + max_context_len_to_capture: int = 8192, + disable_custom_all_reduce: bool = False, + **kwargs, + ) -> None: + if "disable_log_stats" not in kwargs: + kwargs["disable_log_stats"] = True + engine_args = EngineArgs( + model_hf_config=model_hf_config, + tensor_parallel_size=tensor_parallel_size, + dtype=dtype, + quantization=quantization, + revision=revision, + tokenizer_revision=tokenizer_revision, + seed=seed, + gpu_memory_utilization=gpu_memory_utilization, + swap_space=swap_space, + enforce_eager=enforce_eager, + max_context_len_to_capture=max_context_len_to_capture, + disable_custom_all_reduce=disable_custom_all_reduce, + **kwargs, + ) + tokenizer_cls = (PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer) + if not isinstance(tokenizer, tokenizer_cls): + raise ValueError( + f"Unexpected tokenizer type: {type(tokenizer)}. Must be" + "one of the following: PreTrainedTokenizer, PreTrainedTokenizerFast, verl.trainer.ppo.rollout.HybridEngineBaseTokenizer" + ) + self.llm_engine = LLMEngine.from_engine_args(model, tokenizer, engine_args) + self.request_counter = Counter() + + def init_cache_engine(self): + self.llm_engine.init_cache_engine() + + def free_cache_engine(self): + self.llm_engine.free_cache_engine() + + def get_tokenizer(self) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]: + return self.llm_engine.tokenizer + + def set_tokenizer( + self, + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], + ) -> None: + self.llm_engine.tokenizer = tokenizer + + def generate( + self, + prompts: Optional[Union[str, List[str]]] = None, + sampling_params: Optional[SamplingParams] = None, + prompt_token_ids: Optional[List[List[int]]] = None, + prefix_pos: Optional[Union[int, List[int]]] = None, + use_tqdm: bool = True, + lora_request: Optional[LoRARequest] = None, + ) -> List[RequestOutput]: + """Generates the completions for the input prompts. + + NOTE: This class automatically batches the given prompts, considering + the memory constraint. For the best performance, put all of your prompts + into a single list and pass it to this method. + + Args: + prompts: A list of prompts to generate completions for. + sampling_params: The sampling parameters for text generation. If + None, we use the default sampling parameters. + prompt_token_ids: A list of token IDs for the prompts. If None, we + use the tokenizer to convert the prompts to token IDs. + use_tqdm: Whether to use tqdm to display the progress bar. + + Returns: + A list of `RequestOutput` objects containing the generated + completions in the same order as the input prompts. + """ + if prompts is None and prompt_token_ids is None: + raise ValueError("Either prompts or prompt_token_ids must be " + "provided.") + if isinstance(prompts, str): + # Convert a single prompt to a list. + prompts = [prompts] + if prompts is not None and prompt_token_ids is not None: + if len(prompts) != len(prompt_token_ids): + raise ValueError("The lengths of prompts and prompt_token_ids " + "must be the same.") + if sampling_params is None: + # Use default sampling params. + sampling_params = SamplingParams() + + # Add requests to the engine. + num_requests = len(prompts) if prompts is not None else len(prompt_token_ids) + for i in range(num_requests): + prompt = prompts[i] if prompts is not None else None + prefix_pos_i = prefix_pos[i] if prefix_pos is not None else None + token_ids = None if prompt_token_ids is None else prompt_token_ids[i] + if not isinstance(token_ids, list): + # NOTE(shengguangming): convert the rollout input into List[str] + token_ids = self._pre_process_inputs(token_ids) + self._add_request(prompt, sampling_params, token_ids, lora_request=lora_request, prefix_pos=prefix_pos_i) + return self._run_engine(use_tqdm) + + def _add_request( + self, + prompt: Optional[str], + sampling_params: SamplingParams, + prompt_token_ids: Optional[List[int]], + lora_request: Optional[LoRARequest] = None, + prefix_pos: Optional[int] = None, + ) -> None: + request_id = str(next(self.request_counter)) + self.llm_engine.add_request(request_id, + prompt, + sampling_params, + prompt_token_ids, + lora_request=lora_request, + prefix_pos=prefix_pos) + + def _run_engine(self, use_tqdm: bool) -> List[RequestOutput]: + # Initialize tqdm. + if use_tqdm: + num_requests = self.llm_engine.get_num_unfinished_requests() + pbar = tqdm(total=num_requests, desc="Processed prompts") + # Run the engine. + outputs: List[RequestOutput] = [] + while self.llm_engine.has_unfinished_requests(): + step_outputs = self.llm_engine.step() + for output in step_outputs: + if output.finished: + outputs.append(output) + if use_tqdm: + pbar.update(1) + if use_tqdm: + pbar.close() + # Sort the outputs by request ID. + # This is necessary because some requests may be finished earlier than + # its previous requests. + outputs = sorted(outputs, key=lambda x: int(x.request_id)) + # TODO(shengguangming): maybe we can hack the autoregressive logics without only apply post process for better performance + return self._post_process_outputs(outputs) + + # NOTE(shengguangming): add for verl + # TODO(sgm): we can optimize it by making the dataloader yield List[int] without padding. + def _pre_process_inputs(self, prompt_token_ids: torch.Tensor) -> List[int]: + # remove the left padding in the prompt token_id + pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + non_pad_index = torch.nonzero(prompt_token_ids != pad_token_id, as_tuple=False)[0][0] + token_ids = prompt_token_ids[non_pad_index:].tolist() + return token_ids + + # NOTE(shengguangming): add for verl + def _post_process_outputs(self, outputs: List[RequestOutput]) -> Tuple[torch.Tensor, torch.Tensor]: + output_token_ids = [] + logprobs = [] + for output in outputs: # List[RequestOutput] + output = output.outputs + for output in output: # List[CompletionOutput], usually len == 1 + output_token_ids.append(torch.tensor(output.token_ids)) + # TODO(shengguangming): can be optimzied by rewrite the Sampler._get_logprobs() logits + logprobs_dicts = output.logprobs + if logprobs_dicts is not None: + logprob = [] + for logprobs_dict, id in zip(logprobs_dicts, output.token_ids): + logprob.append(logprobs_dict[id]) + logprobs.append(torch.tensor(logprob)) + + pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + output_token_ids = pad_sequence(output_token_ids, batch_first=True, padding_value=pad_token_id) + if len(logprobs) > 0: + logprobs = pad_sequence(logprobs, batch_first=True, padding_value=pad_token_id) + return output_token_ids, logprobs + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor]) -> None: + self.llm_engine.sync_model_weights(actor_weights=actor_weights) + + def offload_model_weights(self) -> None: + self.llm_engine.offload_model_weights() diff --git a/verl/third_party/vllm/vllm_v_0_3_1/llm_engine_sp.py b/verl/third_party/vllm/vllm_v_0_3_1/llm_engine_sp.py new file mode 100644 index 000000000..e264a8585 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/llm_engine_sp.py @@ -0,0 +1,765 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/llm_engine.py + +import os +import socket +import time +import torch +from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Tuple, Union + +from vllm.lora.request import LoRARequest +from vllm.config import (CacheConfig, DeviceConfig, ModelConfig, ParallelConfig, SchedulerConfig, LoRAConfig) +from vllm.core.scheduler import Scheduler, SchedulerOutputs +from vllm.logger import init_logger +from vllm.outputs import RequestOutput +from vllm.sampling_params import SamplingParams +from vllm.sequence import (SamplerOutput, Sequence, SequenceGroup, SequenceGroupMetadata, SequenceGroupOutput, + SequenceOutput, SequenceStatus) +from vllm.transformers_utils.tokenizer import detokenize_incrementally +from vllm.engine.metrics import StatLogger, Stats +from vllm.utils import Counter +import torch.nn as nn +from .arg_utils import EngineArgs +from .tokenizer import TokenizerGroup + +logger = init_logger(__name__) +_LOCAL_LOGGING_INTERVAL_SEC = 5 + + +class LLMEngine: + """An LLM engine that receives requests and generates texts. + + This is the main class for the vLLM engine. It receives requests + from clients and generates texts from the LLM. It includes a tokenizer, a + language model (possibly distributed across multiple GPUs), and GPU memory + space allocated for intermediate states (aka KV cache). This class utilizes + iteration-level scheduling and efficient memory management to maximize the + serving throughput. + + The `LLM` class wraps this class for offline batched inference and the + `AsyncLLMEngine` class wraps this class for online serving. + + NOTE: The config arguments are derived from the `EngineArgs` class. For the + comprehensive list of arguments, see `EngineArgs`. + + Args: + model_config: The configuration related to the LLM model. + cache_config: The configuration related to the KV cache memory + management. + parallel_config: The configuration related to distributed execution. + scheduler_config: The configuration related to the request scheduler. + distributed_init_method: The initialization method for distributed + execution. See `torch.distributed.init_process_group` for details. + placement_group: Ray placement group for distributed execution. + Required for distributed execution. + log_stats: Whether to log statistics. + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: nn.Module, + model_config: ModelConfig, + cache_config: CacheConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + lora_config: Optional[LoRAConfig], + distributed_init_method: str, + placement_group: Optional[None], + log_stats: bool, + ) -> None: + logger.info("Initializing an LLM engine with config: " + f"model={model_config.model!r}, " + f"tokenizer={model_config.tokenizer!r}, " + # f"tokenizer_mode={model_config.tokenizer_mode}, " + f"revision={model_config.revision}, " + f"tokenizer_revision={model_config.tokenizer_revision}, " + # f"trust_remote_code={model_config.trust_remote_code}, " + f"dtype={model_config.dtype}, " + f"max_seq_len={model_config.max_model_len}, " + # f"download_dir={model_config.download_dir!r}, " + # f"load_format={model_config.load_format}, " + f"disable_custom_all_reduce={parallel_config.disable_custom_all_reduce}, " + f"tensor_parallel_size={parallel_config.tensor_parallel_size}, " + f"quantization={model_config.quantization}, " + f"seed={model_config.seed})") + # TODO(woosuk): Print more configs in debug mode. + + self.model_config = model_config # TODO: currently is hfconfig + self.cache_config = cache_config + self.lora_config = lora_config + assert self.cache_config.sliding_window == getattr(self.model_config.hf_config, "sliding_window", None) + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.log_stats = log_stats + self._verify_args() + + # self.model = model # should not store the model, it should be deleted + # TODO(shengguangming): maybe we can choose init here or from arguments + self._init_tokenizer(tokenizer) + + self.seq_counter = Counter() + + # Create the parallel GPU workers. + self._init_workers_sp(model, distributed_init_method) + + # Profile the memory usage and initialize the cache. + self._init_cache_sp() + + # Create the scheduler. + # NOTE(shengguangming): each process will have independent scheduler + self.scheduler = Scheduler(scheduler_config, cache_config, lora_config) + + # Metric Logging. + if self.log_stats: + self.stat_logger = StatLogger(local_interval=_LOCAL_LOGGING_INTERVAL_SEC) + + # Logging. + self.last_logging_time = 0.0 + # List of (timestamp, num_tokens) + self.num_prompt_tokens: List[Tuple[float, int]] = [] + # List of (timestamp, num_tokens) + self.num_generation_tokens: List[Tuple[float, int]] = [] + + def _init_tokenizer(self, tokenizer, **tokenizer_init_kwargs): + init_kwargs = dict(enable_lora=bool(self.lora_config), + max_num_seqs=self.scheduler_config.max_num_seqs, + max_input_length=None) + init_kwargs.update(tokenizer_init_kwargs) + self.tokenizer: TokenizerGroup = TokenizerGroup(tokenizer, **init_kwargs) + + # TODO: check get_lora_tokenizer func + def get_tokenizer_for_seq(self, sequence: Sequence): + return self.tokenizer.get_lora_tokenizer(sequence.lora_request) + + def _init_workers_sp(self, model, distributed_init_method: str): + # Lazy import the Worker to avoid importing torch.cuda/xformers + # before CUDA_VISIBLE_DEVICES is set in the Worker + from .worker import Worker # pylint: disable=import-outside-toplevel + + rank = int(os.getenv("RANK")) + + self.worker = Worker( + model, + self.model_config, + self.parallel_config, + self.scheduler_config, + self.device_config, + rank, + distributed_init_method, + lora_config=self.lora_config, + kv_cache_dtype=self.cache_config.cache_dtype, + ) + + # NOTE(shengguangming): torch.distributed.init_process_group will be called inside the init_model() + self.worker.init_model() + self.worker.load_model() + + def _verify_args(self) -> None: + self.model_config.verify_with_parallel_config(self.parallel_config) + self.cache_config.verify_with_parallel_config(self.parallel_config) + + def _init_cache_sp(self) -> None: + """Profiles the memory usage and initializes the KV cache.""" + # Get the maximum number of blocks that can be allocated on GPU and CPU. + num_blocks = self.worker.profile_num_available_blocks( + block_size=self.cache_config.block_size, + gpu_memory_utilization=self.cache_config.gpu_memory_utilization, + cpu_swap_space=self.cache_config.swap_space_bytes, + cache_dtype=self.cache_config.cache_dtype, + ) + + # NOTE(shengguangming): Now we don't use a shared centralized controler but each process will + # have its own scheduler + num_gpu_blocks = num_blocks[0] + num_cpu_blocks = num_blocks[1] + + # FIXME(woosuk): Change to debug log. + logger.info(f"# GPU blocks: {num_gpu_blocks}, " + f"# CPU blocks: {num_cpu_blocks}") + + if num_gpu_blocks <= 0: + raise ValueError("No available memory for the cache blocks. " + "Try increasing `gpu_memory_utilization` when " + "initializing the engine.") + + max_seq_len = self.cache_config.block_size * num_gpu_blocks + if self.model_config.max_model_len > max_seq_len: + raise ValueError(f"The model's max seq len ({self.model_config.max_model_len}) " + "is larger than the maximum number of tokens that can be " + f"stored in KV cache ({max_seq_len}). Try increasing " + "`gpu_memory_utilization` or decreasing `max_model_len` when " + "initializing the engine.") + + self.cache_config.num_gpu_blocks = num_gpu_blocks + self.cache_config.num_cpu_blocks = num_cpu_blocks + + # Initialize the cache. + self.worker.init_cache_engine(cache_config=self.cache_config) + self.worker.warm_up_model() + + def init_cache_engine(self): + self.worker.init_cache_engine(cache_config=self.cache_config) + + def free_cache_engine(self): + self.worker.free_cache_engine() + + @classmethod + def from_engine_args(cls, model, tokenizer, engine_args: EngineArgs) -> "LLMEngine": + """Creates an LLM engine from the engine arguments.""" + # Create the engine configs. + engine_configs = engine_args.create_engine_configs() + parallel_config = engine_configs[2] + # Initialize the cluster. + distributed_init_method, placement_group = initialize_cluster(parallel_config) + # Create the LLM engine. + engine = cls(model, + tokenizer, + *engine_configs, + distributed_init_method, + placement_group, + log_stats=not engine_args.disable_log_stats) + return engine + + def add_request( + self, + request_id: str, + prompt: Optional[str], + sampling_params: SamplingParams, + prompt_token_ids: Optional[List[int]] = None, + arrival_time: Optional[float] = None, + lora_request: Optional[LoRARequest] = None, + prefix_pos: Optional[int] = None, + ) -> None: + """Add a request to the engine's request pool. + + The request is added to the request pool and will be processed by the + scheduler as `engine.step()` is called. The exact scheduling policy is + determined by the scheduler. + + Args: + request_id: The unique ID of the request. + prompt: The prompt string. Can be None if prompt_token_ids is + provided. + sampling_params: The sampling parameters for text generation. + prompt_token_ids: The token IDs of the prompt. If None, we + use the tokenizer to convert the prompts to token IDs. + arrival_time: The arrival time of the request. If None, we use + the current monotonic time. + prefix_pos: If not None, we use the given position as the prefix + position for each prompt. We will cache the prefix's KV + cache and reuse it for the next request with the same prefix. + This is an experimental feature, and may be replaced with + automatic prefix caching in the future. + + Details: + - Set arrival_time to the current time if it is None. + - Set prompt_token_ids to the encoded prompt if it is None. + - Create `best_of` number of :class:`~vllm.Sequence` objects. + - Create a :class:`~vllm.SequenceGroup` object + from the list of :class:`~vllm.Sequence`. + - Add the :class:`~vllm.SequenceGroup` object to the scheduler. + + Example: + >>> # initialize engine + >>> engine = LLMEngine.from_engine_args(engine_args) + >>> # set request arguments + >>> example_prompt = "Who is the president of the United States?" + >>> sampling_params = SamplingParams(temperature=0.0) + >>> request_id = 0 + >>> + >>> # add the request to the engine + >>> engine.add_request( + >>> str(request_id), + >>> example_prompt, + >>> SamplingParams(temperature=0.0)) + >>> # continue the request processing + >>> ... + """ + if lora_request is not None and not self.lora_config: + raise ValueError(f"Got lora_request {lora_request} but LoRA is " + "not enabled!") + if arrival_time is None: + arrival_time = time.monotonic() + if prompt_token_ids is None: + assert prompt is not None + prompt_token_ids = self.tokenizer.encode(prompt) + + # Create the sequences. + block_size = self.cache_config.block_size + seq_id = next(self.seq_counter) + seq = Sequence(seq_id, prompt, prompt_token_ids, block_size, lora_request) + + # Check whether the input specifies prefix + prefix = self.scheduler.prefix_pool.add_or_get_prefix(prompt_token_ids[:prefix_pos], lora_request.lora_int_id if + lora_request else 0) if prefix_pos is not None else None + + # Create the sequence group. + seq_group = SequenceGroup(request_id, [seq], sampling_params, arrival_time, lora_request, prefix) + + # Add the sequence group to the scheduler. + self.scheduler.add_seq_group(seq_group) + + def abort_request(self, request_id: Union[str, Iterable[str]]) -> None: + """Aborts a request(s) with the given ID. + + Args: + request_id: The ID(s) of the request to abort. + + Details: + - Refer to the + :meth:`~vllm.core.scheduler.Scheduler.abort_seq_group` + from class :class:`~vllm.core.scheduler.Scheduler`. + + Example: + >>> # initialize engine and add a request with request_id + >>> request_id = str(0) + >>> # abort the request + >>> engine.abort_request(request_id) + """ + self.scheduler.abort_seq_group(request_id) + + def get_model_config(self) -> ModelConfig: + """Gets the model configuration.""" + return self.model_config + + def get_num_unfinished_requests(self) -> int: + """Gets the number of unfinished requests.""" + return self.scheduler.get_num_unfinished_seq_groups() + + def has_unfinished_requests(self) -> bool: + """Returns True if there are unfinished requests.""" + return self.scheduler.has_unfinished_seqs() + + def _check_beam_search_early_stopping( + self, + early_stopping: Union[bool, str], + sampling_params: SamplingParams, + best_running_seq: Sequence, + current_worst_seq: Sequence, + ) -> bool: + assert sampling_params.use_beam_search + length_penalty = sampling_params.length_penalty + if early_stopping is True: + return True + + current_worst_score = (current_worst_seq.get_beam_search_score( + length_penalty=length_penalty, eos_token_id=self.get_tokenizer_for_seq(current_worst_seq).eos_token_id)) + if early_stopping is False: + highest_attainable_score = (best_running_seq.get_beam_search_score( + length_penalty=length_penalty, eos_token_id=self.get_tokenizer_for_seq(best_running_seq).eos_token_id)) + else: + assert early_stopping == "never" + if length_penalty > 0.0: + # If length_penalty > 0.0, beam search will prefer longer + # sequences. The highest attainable score calculation is + # based on the longest possible sequence length in this case. + max_possible_length = max(best_running_seq.get_prompt_len() + sampling_params.max_tokens, + self.scheduler_config.max_model_len) + highest_attainable_score = (best_running_seq.get_beam_search_score( + length_penalty=length_penalty, + eos_token_id=self.get_tokenizer_for_seq(best_running_seq).eos_token_id, + seq_len=max_possible_length)) + else: + # Otherwise, beam search will prefer shorter sequences. The + # highest attainable score calculation is based on the current + # sequence length. + highest_attainable_score = (best_running_seq.get_beam_search_score( + length_penalty=length_penalty, + eos_token_id=self.get_tokenizer_for_seq(best_running_seq).eos_token_id)) + + def _process_sequence_group_outputs(self, seq_group: SequenceGroup, outputs: SequenceGroupOutput) -> None: + + # Process prompt logprobs + prompt_logprobs = outputs.prompt_logprobs + if prompt_logprobs is not None: + seq_group.prompt_logprobs = prompt_logprobs + + # Process samples + samples = outputs.samples + parent_seqs = seq_group.get_seqs(status=SequenceStatus.RUNNING) + existing_finished_seqs = seq_group.get_finished_seqs() + parent_child_dict = {parent_seq.seq_id: [] for parent_seq in parent_seqs} + for sample in samples: + parent_child_dict[sample.parent_seq_id].append(sample) + # List of (child, parent) + child_seqs: List[Tuple[Sequence, Sequence]] = [] + + # Process the child samples for each parent sequence + for parent in parent_seqs: + child_samples: List[SequenceOutput] = parent_child_dict[parent.seq_id] + if len(child_samples) == 0: + # This parent sequence has no children samples. Remove + # the parent sequence from the sequence group since it will + # not be used in the future iterations. + parent.status = SequenceStatus.FINISHED_ABORTED + seq_group.remove(parent.seq_id) + self.scheduler.free_seq(parent) + continue + # Fork the parent sequence if there are multiple child samples. + for child_sample in child_samples[:-1]: + new_child_seq_id = next(self.seq_counter) + child = parent.fork(new_child_seq_id) + child.append_token_id(child_sample.output_token, child_sample.logprobs) + child_seqs.append((child, parent)) + # Continue the parent sequence for the last child sample. + # We reuse the parent sequence here to reduce redundant memory + # copies, especially when using non-beam search sampling methods. + last_child_sample = child_samples[-1] + parent.append_token_id(last_child_sample.output_token, last_child_sample.logprobs) + child_seqs.append((parent, parent)) + + for seq, _ in child_seqs: + # self._decode_sequence(seq, seq_group.sampling_params) + self._check_stop(seq, seq_group.sampling_params) + + # Non-beam search case + if not seq_group.sampling_params.use_beam_search: + # For newly created child sequences, add them to the sequence group + # and fork them in block manager if they are not finished. + for seq, parent in child_seqs: + if seq is not parent: + seq_group.add(seq) + if not seq.is_finished(): + self.scheduler.fork_seq(parent, seq) + + # Free the finished and selected parent sequences' memory in block + # manager. Keep them in the sequence group as candidate output. + # NOTE: we need to fork the new sequences before freeing the + # old sequences. + for seq, parent in child_seqs: + if seq is parent and seq.is_finished(): + self.scheduler.free_seq(seq) + return + + # Beam search case + # Select the child sequences to keep in the sequence group. + selected_child_seqs = [] + unselected_child_seqs = [] + beam_width = seq_group.sampling_params.best_of + length_penalty = seq_group.sampling_params.length_penalty + + # Select the newly finished sequences with the highest scores + # to replace existing finished sequences. + # Tuple of (seq, parent, is_new) + existing_finished_seqs = [(seq, None, False) for seq in existing_finished_seqs] + new_finished_seqs = [(seq, parent, True) for seq, parent in child_seqs if seq.is_finished()] + all_finished_seqs = existing_finished_seqs + new_finished_seqs + # Sort the finished sequences by their scores. + all_finished_seqs.sort(key=lambda x: x[0].get_beam_search_score( + length_penalty=length_penalty, eos_token_id=self.get_tokenizer_for_seq(x[0]).eos_token_id), + reverse=True) + for seq, parent, is_new in all_finished_seqs[:beam_width]: + if is_new: + # A newly generated child sequence finishes and has a high + # score, so we will add it into the sequence group. + selected_child_seqs.append((seq, parent)) + for seq, parent, is_new in all_finished_seqs[beam_width:]: + if is_new: + # A newly generated child sequence finishes but has a low + # score, so we will not add it into the sequence group. + # Additionally, if this sequence is a continuation of a + # parent sequence, we will need remove the parent sequence + # from the sequence group. + unselected_child_seqs.append((seq, parent)) + else: + # An existing finished sequence has a low score, so we will + # remove it from the sequence group. + seq_group.remove(seq.seq_id) + + # select the top beam_width sequences from the running + # sequences for the next iteration to continue the beam + # search. + running_child_seqs = [(seq, parent) for seq, parent in child_seqs if not seq.is_finished()] + # Sort the running sequences by their scores. + running_child_seqs.sort(key=lambda x: x[0].get_beam_search_score( + length_penalty=length_penalty, eos_token_id=self.get_tokenizer_for_seq(x[0]).eos_token_id), + reverse=True) + + # Check if we can stop the beam search. + if len(running_child_seqs) == 0: + # No running sequences, stop the beam search. + stop_beam_search = True + elif len(all_finished_seqs) < beam_width: + # Not enough finished sequences, continue the beam search. + stop_beam_search = False + else: + # Check the early stopping criteria + best_running_seq = running_child_seqs[0][0] + current_worst_seq = all_finished_seqs[beam_width - 1][0] + stop_beam_search = self._check_beam_search_early_stopping(seq_group.sampling_params.early_stopping, + seq_group.sampling_params, best_running_seq, + current_worst_seq) + + if stop_beam_search: + # Stop the beam search and remove all the running sequences from + # the sequence group. + unselected_child_seqs.extend(running_child_seqs) + else: + # Continue the beam search and select the top beam_width sequences + # to continue the beam search. + selected_child_seqs.extend(running_child_seqs[:beam_width]) + # The remaining running sequences will not be used in the next + # iteration. Again, if these sequences are continuations of + # parent sequences, we will need to remove the parent sequences + # from the sequence group. + unselected_child_seqs.extend(running_child_seqs[beam_width:]) + + # For newly created child sequences, add them to the sequence group + # and fork them in block manager if they are not finished. + for seq, parent in selected_child_seqs: + if seq is not parent: + seq_group.add(seq) + if not seq.is_finished(): + self.scheduler.fork_seq(parent, seq) + + # Free the finished and selected parent sequences' memory in block + # manager. Keep them in the sequence group as candidate output. + for seq, parent in selected_child_seqs: + if seq is parent and seq.is_finished(): + self.scheduler.free_seq(seq) + + # Remove the unselected parent sequences from the sequence group and + # free their memory in block manager. + for seq, parent in unselected_child_seqs: + if seq is parent: + # Remove the parent sequence if it is not selected for next + # iteration + seq_group.remove(seq.seq_id) + self.scheduler.free_seq(seq) + + def _process_model_outputs(self, output: SamplerOutput, scheduler_outputs: SchedulerOutputs) -> List[RequestOutput]: + # Update the scheduled sequence groups with the model outputs. + scheduled_seq_groups = scheduler_outputs.scheduled_seq_groups + for seq_group, outputs in zip(scheduled_seq_groups, output): + self._process_sequence_group_outputs(seq_group, outputs) + + # Free the finished sequence groups. + self.scheduler.free_finished_seq_groups() + + # Create the outputs. + request_outputs: List[RequestOutput] = [] + for seq_group in scheduled_seq_groups: + request_output = RequestOutput.from_seq_group(seq_group) + request_outputs.append(request_output) + for seq_group in scheduler_outputs.ignored_seq_groups: + request_output = RequestOutput.from_seq_group(seq_group) + request_outputs.append(request_output) + + # Update prefix state, now all the uncomputed prefixes are computed. + for seq_group in scheduled_seq_groups: + if (seq_group.prefix is not None and seq_group.prefix.allocated and not seq_group.prefix.computed): + seq_group.prefix.computed = True + + # Log stats. + if self.log_stats: + self.stat_logger.log(self._get_stats(scheduler_outputs)) + + return request_outputs + + def step(self) -> List[RequestOutput]: + """Performs one decoding iteration and returns newly generated results. + + This function performs one decoding iteration of the engine. It first + schedules the sequences to be executed in the next iteration and the + token blocks to be swapped in/out/copy. Then, it executes the model + and updates the scheduler with the model outputs. Finally, it decodes + the sequences and returns the newly generated results. + """ + seq_group_metadata_list, scheduler_outputs = self.scheduler.schedule() + if not scheduler_outputs.is_empty(): + output = self.worker.execute_model( + seq_group_metadata_list=seq_group_metadata_list, # TODO: check this input + blocks_to_swap_in=scheduler_outputs.blocks_to_swap_in, + blocks_to_swap_out=scheduler_outputs.blocks_to_swap_out, + blocks_to_copy=scheduler_outputs.blocks_to_copy,) + else: + return [RequestOutput.from_seq_group(seq_group) for seq_group in scheduler_outputs.ignored_seq_groups] + + return self._process_model_outputs(output, scheduler_outputs) + + def do_log_stats(self) -> None: + """Forced log when no requests active.""" + if self.log_stats: + self.stat_logger.log(self._get_stats(scheduler_outputs=None)) + + def _get_stats(self, scheduler_outputs: Optional[SchedulerOutputs]) -> Stats: + """Get Stats to be Logged to Prometheus.""" + now = time.monotonic() + + # KV Cache Usage in %. + num_total_gpu = self.cache_config.num_gpu_blocks + num_free_gpu = self.scheduler.block_manager.get_num_free_gpu_blocks() + gpu_cache_usage = 1.0 - (num_free_gpu / num_total_gpu) + + num_total_cpu = self.cache_config.num_cpu_blocks + cpu_cache_usage = 0. + if num_total_cpu > 0: + num_free_cpu = self.scheduler.block_manager.get_num_free_cpu_blocks() + cpu_cache_usage = 1.0 - (num_free_cpu / num_total_cpu) + + # Scheduler State + num_running = len(self.scheduler.running) + num_swapped = len(self.scheduler.swapped) + num_waiting = len(self.scheduler.waiting) + + # Iteration stats if we have scheduler output. + num_prompt_tokens = 0 + num_generation_tokens = 0 + time_to_first_tokens = [] + time_per_output_tokens = [] + time_e2e_requests = [] + if scheduler_outputs is not None: + prompt_run = scheduler_outputs.prompt_run + + # Number of Tokens. + if prompt_run: + num_prompt_tokens = scheduler_outputs.num_batched_tokens + else: + num_generation_tokens = scheduler_outputs.num_batched_tokens + + # Latency Timings. + time_last_iters = [] + for seq_group in scheduler_outputs.scheduled_seq_groups: + # Time since last token. (n.b. updates seq_group.last_token_time) + time_last_iters.append(seq_group.get_last_latency(now)) + # Time since arrival for all finished requests. + if seq_group.is_finished(): + time_e2e_requests.append(now - seq_group.arrival_time) + + time_to_first_tokens = time_last_iters if prompt_run else [] + time_per_output_tokens = [] if prompt_run else time_last_iters + + return Stats( + now=now, + num_running=num_running, + num_swapped=num_swapped, + num_waiting=num_waiting, + gpu_cache_usage=gpu_cache_usage, + cpu_cache_usage=cpu_cache_usage, + num_prompt_tokens=num_prompt_tokens, + num_generation_tokens=num_generation_tokens, + time_to_first_tokens=time_to_first_tokens, + time_per_output_tokens=time_per_output_tokens, + time_e2e_requests=time_e2e_requests, + ) + + # TODO: we may not need to decode + def _decode_sequence(self, seq: Sequence, prms: SamplingParams) -> None: + """Decodes the new token for a sequence.""" + (new_tokens, new_output_text, prefix_offset, read_offset) = detokenize_incrementally( + self.get_tokenizer_for_seq(seq), + all_input_ids=seq.get_token_ids(), + prev_tokens=seq.tokens, + prefix_offset=seq.prefix_offset, + read_offset=seq.read_offset, + skip_special_tokens=prms.skip_special_tokens, + spaces_between_special_tokens=prms.spaces_between_special_tokens, + ) + if seq.tokens is None: + seq.tokens = new_tokens + else: + seq.tokens.extend(new_tokens) + seq.prefix_offset = prefix_offset + seq.read_offset = read_offset + seq.output_text += new_output_text + + def _check_stop(self, seq: Sequence, sampling_params: SamplingParams) -> None: + """Stop the finished sequences.""" + # for stop_str in sampling_params.stop: + # if seq.output_text.endswith(stop_str): + # self._finalize_sequence(seq, sampling_params, stop_str) + # seq.status = SequenceStatus.FINISHED_STOPPED + # return + # if seq.get_last_token_id() in sampling_params.stop_token_ids: + # stop_str = self.get_tokenizer_for_seq(seq).convert_ids_to_tokens(seq.get_last_token_id()) + # self._finalize_sequence(seq, sampling_params, stop_str) + # seq.status = SequenceStatus.FINISHED_STOPPED + # return + + # Check if the sequence has reached max_model_len. + if seq.get_len() > self.scheduler_config.max_model_len: + seq.status = SequenceStatus.FINISHED_LENGTH_CAPPED + return + + # Check if the sequence has reached max_tokens. + if seq.get_output_len() == sampling_params.max_tokens: + seq.status = SequenceStatus.FINISHED_LENGTH_CAPPED + return + + # Check if the sequence has generated the EOS token. + if ((not sampling_params.ignore_eos) and + seq.get_last_token_id() == self.get_tokenizer_for_seq(seq).eos_token_id): + seq.status = SequenceStatus.FINISHED_STOPPED + return + + def _finalize_sequence(self, seq: Sequence, sampling_params: SamplingParams, stop_string: str) -> None: + if not sampling_params.include_stop_str_in_output and stop_string: + # Truncate the output text so that the stop string is + # not included in the output. + seq.output_text = seq.output_text[:-len(stop_string)] + + def add_lora(self, lora_request: LoRARequest) -> bool: + assert lora_request.lora_int_id > 0, "lora_id must be greater than 0." + return self.worker.add_lora(lora_request) + + def remove_lora(self, lora_id: int) -> bool: + assert lora_id > 0, "lora_id must be greater than 0." + return self.worker.remove_lora(lora_id) + + def list_loras(self) -> List[int]: + return self.worker.list_loras() + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor]) -> None: + self.worker.sync_model_weights(actor_weights=actor_weights) + + def offload_model_weights(self) -> None: + self.worker.offload_model_weights() + + +def initialize_cluster( + parallel_config: ParallelConfig, + engine_use_ray: bool = False, + ray_address: Optional[str] = None, +) -> Tuple[str, Optional[None]]: + """Initialize the distributed cluster probably with Ray. + + Args: + parallel_config: The configurations for parallel execution. + engine_use_ray: Whether to use Ray for async engine. + ray_address: The address of the Ray cluster. If None, uses + the default Ray cluster address. + + Returns: + A tuple of (`distributed_init_method`, `placement_group`). The + `distributed_init_method` is the address for initializing the + distributed backend. `placement_group` includes the specification + of the resources for each distributed worker. + """ + + # Initialize cluster locally. + port = get_open_port() + # We need to setup the distributed init method to make sure + # the distributed megatron code (e.g., get world size) works correctly. + distributed_init_method = f"tcp://localhost:{port}" + return distributed_init_method, None + + +def get_open_port(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("", 0)) + return s.getsockname()[1] diff --git a/verl/third_party/vllm/vllm_v_0_3_1/model_loader.py b/verl/third_party/vllm/vllm_v_0_3_1/model_loader.py new file mode 100644 index 000000000..450e2f4b4 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/model_loader.py @@ -0,0 +1,275 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/model_loader +"""Utilities for selecting and loading models.""" +import contextlib +from typing import Dict, Type, Union + +import torch +import torch.nn as nn +from transformers import PretrainedConfig, PreTrainedModel +from megatron.core.tensor_parallel.utils import VocabUtility + +from vllm.model_executor.models import ModelRegistry +from vllm.model_executor.weight_utils import (get_quant_config, initialize_dummy_weights) + +from .config import ModelConfig +from vllm.config import DeviceConfig, LoRAConfig +from .weight_loaders import * +from vllm.model_executor.sampling_metadata import SamplingMetadata, SamplingTensors +from vllm.sequence import SamplerOutput +from typing import Optional +from vllm.model_executor.layers.sampler import Sampler +from vllm.model_executor.layers.sampler import _prune_hidden_states, _apply_logits_processors, _apply_penalties, _apply_top_k_top_p, _apply_min_p, _apply_penalties, _sample, _get_logprobs, _build_sampler_output + + +@contextlib.contextmanager +def _set_default_torch_dtype(dtype: torch.dtype): + """Sets the default torch dtype to the given dtype.""" + old_dtype = torch.get_default_dtype() + torch.set_default_dtype(dtype) + yield + torch.set_default_dtype(old_dtype) + + +def _get_model_architecture(config: PretrainedConfig) -> Type[nn.Module]: + architectures = getattr(config, "architectures", []) + for arch in architectures: + model_cls = ModelRegistry.load_model_cls(arch) + if model_cls is not None: + return model_cls + raise ValueError(f"Model architectures {architectures} are not supported for now. " + f"Supported architectures: {ModelRegistry.get_supported_archs()}") + + +from vllm.model_executor.layers.linear import * +from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding, ParallelLMHead +from vllm.model_executor.layers.activation import ScaledActivation + +__LAYER_WEIGHT_LOADER_REGISTRY__ = { + ColumnParallelLinear: parallel_weight_loader, + MergedColumnParallelLinear: parallel_weight_loader, + QKVParallelLinear: parallel_weight_loader, + RowParallelLinear: parallel_weight_loader, + VocabParallelEmbedding: parallel_weight_loader, + ParallelLMHead: parallel_weight_loader + # "ScaledActivation.weight_loader": ScaledActivation, # TODO(shengguangming): latest commit in vllm fix awq for this function and add load_weights + # "default_weight_loader": default_weight_loader +} + +# NOTE(gmsheng): change the weight_loader function in runtime +for layer_class, weight_loader in __LAYER_WEIGHT_LOADER_REGISTRY__.items(): + layer_class.weight_loader = weight_loader + +__MODEL_WEIGHT_LOADER_REGISTRY__ = { + 'GPT2LMHeadModel': gpt2_weight_loader, + 'LlamaForCausalLM': llama_weight_loader, + 'LLaMAForCausalLM': llama_weight_loader, + 'MistralForCausalLM': mistral_weight_loader, +} + +# FIXME(shengguangming): the vLLM vocab will pad to 64, which may incur out of bounds +# so we need to rewrite the init function of vocab +DEFAULT_VOCAB_PADDING_SIZE = 64 + + +def vocab_init(self, + num_embeddings: int, + embedding_dim: int, + params_dtype: Optional[torch.dtype] = None, + org_num_embeddings: Optional[int] = None, + padding_size: int = DEFAULT_VOCAB_PADDING_SIZE): + super(VocabParallelEmbedding, self).__init__() + + # Keep the input dimensions. + # TODO (pad to be divided by 4) + self.num_embeddings = num_embeddings + self.org_vocab_size = org_num_embeddings or num_embeddings + + # self.num_embeddings_padded = pad_vocab_size(num_embeddings, + # padding_size) + self.embedding_dim = embedding_dim + if params_dtype is None: + params_dtype = torch.get_default_dtype() + self.tp_size = get_tensor_model_parallel_world_size() + # Divide the weight matrix along the vocaburaly dimension. + + self.vocab_start_index, self.vocab_end_index = (VocabUtility.vocab_range_from_global_vocab_size( + self.num_embeddings, get_tensor_model_parallel_rank(), self.tp_size)) + self.num_embeddings_per_partition = (self.vocab_end_index - self.vocab_start_index) + self.weight = Parameter( + torch.empty( + self.num_embeddings_per_partition, + self.embedding_dim, + # device=torch.cuda.current_device(), + dtype=params_dtype)) + set_weight_attrs(self.weight, {"parallel_dim": 0, "weight_loader": self.weight_loader}) + + +VocabParallelEmbedding.__init__ = vocab_init + + +def _get_model_weight_loader(arch: str): + if arch in __MODEL_WEIGHT_LOADER_REGISTRY__: + return __MODEL_WEIGHT_LOADER_REGISTRY__[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {ModelRegistry.get_supported_archs()}") + + +def get_model(actor_model: Union[PreTrainedModel, Dict], + model_config: ModelConfig, + device_config: DeviceConfig, + lora_config: Optional[LoRAConfig] = None) -> nn.Module: + model_class = _get_model_architecture(model_config.hf_config) + + # Get the quantization config. + linear_method = None + quant_config = None + if model_config.quantization is not None: + quant_config = get_quant_config(model_config.quantization, model_config.model, model_config.hf_config, + model_config.download_dir) + capability = torch.cuda.get_device_capability() + capability = capability[0] * 10 + capability[1] + if capability < quant_config.get_min_capability(): + raise ValueError(f"The quantization method {model_config.quantization} is not " + "supported for the current GPU. " + f"Minimum capability: {quant_config.get_min_capability()}. " + f"Current capability: {capability}.") + supported_dtypes = quant_config.get_supported_act_dtypes() + if model_config.dtype not in supported_dtypes: + raise ValueError(f"{model_config.dtype} is not supported for quantization " + f"method {model_config.quantization}. Supported dtypes: " + f"{supported_dtypes}") + linear_method = quant_config.get_linear_method() + + with _set_default_torch_dtype(model_config.dtype): + # Create a model instance. + # The weights will be initialized as empty tensors. + # with torch.device(device_config.device): + # NOTE(sgm): init the model in cpu + model = model_class(model_config.hf_config, linear_method) + + if model_config.load_format == "dummy": + model = model.cuda() + # NOTE(woosuk): For accurate performance evaluation, we assign + # random values to the weights. + initialize_dummy_weights(model) + elif model_config.load_format == 'model' or model_config.load_format == 'auto': + # NOTE(shengguangming) Load the weights from the actor model + if isinstance(actor_model, nn.Module): + load_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), vllm_model=model) + else: + load_weights(actor_weights=actor_model, vllm_model=model) + + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +# the actor model is .state_dict() +def load_weights(actor_weights: Dict, vllm_model: nn.Module): + weight_loader = _get_model_weight_loader(vllm_model.__class__.__name__) + weight_loader(actor_weights, vllm_model) + # NOTE(sgm) to reduce peak memory usage, we offload vllm model to cpu + # after init, and we need this after sync model weights for in first iter. + vllm_model = vllm_model.cuda() + + +# FIXME(sgm): hack the Sampler function in vllm v0.3.1 +# as they use ray, the sampler result will only need to return to the driver node, +# therefore gather is enough. However, we use SPMD instead of a central scheduler, +# all_gather is required (aligned with v0.2.6) +def _get_logits(self, hidden_states: torch.Tensor, embedding: torch.Tensor, + embedding_bias: Optional[torch.Tensor]) -> torch.Tensor: + # Get the logits for the next tokens. + logits = torch.matmul(hidden_states, embedding.t()) + if embedding_bias is not None: + logits += embedding_bias + logits = tensor_model_parallel_all_gather(logits) + # Remove paddings in vocab (if any). + if logits is not None: + logits = logits[:, :self.org_vocab_size] + return logits + + +def forward( + self, + embedding: torch.Tensor, + hidden_states: torch.Tensor, + sampling_metadata: SamplingMetadata, + embedding_bias: Optional[torch.Tensor] = None, +) -> Optional[SamplerOutput]: + # Get the hidden states that we use for sampling. + hidden_states = _prune_hidden_states(hidden_states, sampling_metadata) + + # Get the logits for the next tokens. + logits = self._get_logits(hidden_states, embedding, embedding_bias) + # save origin logprobs for sampler_output + origin_logprobs = torch.log_softmax(logits, dim=-1, dtype=torch.float) + + # Only perform sampling in the driver worker. + # Note: `_get_logits` is still distributed across TP workers because + # the `embedding` weight is distributed across TP workers. + # TODO(zhuohan): Change the get_logits part to a separate stage. + if not sampling_metadata.perform_sampling: + return None + + assert logits is not None + _, vocab_size = logits.shape + + # Apply logits processors (if any). + logits = _apply_logits_processors(logits, sampling_metadata) + + # Prepare sampling tensors with pinned memory to avoid blocking. + (sampling_tensors, do_penalties, do_top_p_top_k, + do_min_p) = SamplingTensors.from_sampling_metadata(sampling_metadata, vocab_size, logits.device, logits.dtype) + + # Apply presence and frequency penalties. + if do_penalties: + logits = _apply_penalties(logits, sampling_tensors.prompt_tokens, sampling_tensors.output_tokens, + sampling_tensors.presence_penalties, sampling_tensors.frequency_penalties, + sampling_tensors.repetition_penalties) + + # Apply temperature scaling. + # Use in-place division to avoid creating a new tensor. + logits.div_(sampling_tensors.temperatures.unsqueeze_(dim=1)) + + if do_top_p_top_k: + logits = _apply_top_k_top_p(logits, sampling_tensors.top_ps, sampling_tensors.top_ks) + + if do_min_p: + logits = _apply_min_p(logits, sampling_tensors.min_ps) + + # We use float32 for probabilities and log probabilities. + # Compute the probabilities. + probs = torch.softmax(logits, dim=-1, dtype=torch.float) + # Compute the log probabilities. + # Use log_softmax to ensure numerical stability. + logprobs = torch.log_softmax(logits, dim=-1, dtype=torch.float) + + # Sample the next tokens. + sample_results = _sample(probs, logprobs, sampling_metadata) + + # Get the logprobs query results. + # prompt_logprobs, sample_logprobs = _get_logprobs( + # logprobs, sampling_metadata, sample_results) + prompt_logprobs, sample_logprobs = _get_logprobs(origin_logprobs, sampling_metadata, sample_results) + + return _build_sampler_output(sample_results, sampling_metadata, prompt_logprobs, sample_logprobs) + + +from vllm.model_executor.layers.sampler import Sampler + +Sampler._get_logits = _get_logits +Sampler.forward = forward diff --git a/verl/third_party/vllm/vllm_v_0_3_1/model_runner.py b/verl/third_party/vllm/vllm_v_0_3_1/model_runner.py new file mode 100644 index 000000000..4acf3422d --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/model_runner.py @@ -0,0 +1,285 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/model_runner.py + +from typing import Dict, List, Optional, Tuple, Set, Union +import contextlib +import time +import numpy as np +import torch +import torch.nn as nn + +from vllm.config import (DeviceConfig, ModelConfig, LoRAConfig, ParallelConfig, SchedulerConfig) +from vllm.logger import init_logger +from vllm.model_executor import InputMetadata, SamplingMetadata +from vllm.sampling_params import SamplingParams, SamplingType +from vllm.sequence import SamplerOutput, SequenceData, SequenceGroupMetadata +from vllm.lora.worker_manager import LRUCacheWorkerLoRAManager +from vllm.lora.layers import LoRAMapping +from vllm.lora.request import LoRARequest +from vllm.utils import in_wsl +from vllm.worker.model_runner import ModelRunner, CUDAGraphRunner, _async_h2d + +from .model_loader import get_model + +logger = init_logger(__name__) + +KVCache = Tuple[torch.Tensor, torch.Tensor] +_PAD_SLOT_ID = -1 +LORA_WARMUP_RANK = 8 +# Capture graphs for batch size 1, 2, 4, 8, 16, 24, 32, 40, ..., 256. +# NOTE: _get_graph_batch_size needs to be updated if this list is changed. +_BATCH_SIZES_TO_CAPTURE = [1, 2, 4] + [8 * i for i in range(1, 33)] + + +class ModelRunner(ModelRunner): + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + lora_config: Optional[LoRAConfig], + kv_cache_dtype: Optional[str] = "auto", + ): + self.model_config = model_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.lora_config = lora_config + + # model_config can be None in tests/samplers/test_sampler.py. + # FIXME(woosuk): This is a hack to make the tests work. Refactor this. + self.sliding_window = (model_config.get_sliding_window() if model_config is not None else None) + + self.device_config = (device_config if device_config is not None else DeviceConfig()) + self.device = self.device_config.device + + self.model = model # this will be replaced by get_model() + self.block_size = None # Set after initial profiling. + self.lora_manager = None + + self.graph_runners: Dict[int, CUDAGraphRunner] = {} + self.graph_memory_pool = None # Set during graph capture. + + self.max_context_len_to_capture = (self.model_config.max_context_len_to_capture + if self.model_config is not None else 0) + # When using CUDA graph, the input block tables must be padded to + # max_context_len_to_capture. However, creating the block table in + # Python can be expensive. To optimize this, we cache the block table + # in numpy and only copy the actual input content at every iteration. + # The shape of the cached block table will be + # (max batch size to capture, max context len to capture / block size). + self.graph_block_tables = None # Set after initial profiling. + # cache in_wsl result + self.in_wsl = in_wsl() + self.kv_cache_dtype = kv_cache_dtype + + def load_model(self) -> None: + self.model = get_model(actor_model=self.model, + model_config=self.model_config, + device_config=self.device_config, + lora_config=self.lora_config) + vocab_size = self.model.config.vocab_size + + if self.lora_config: + assert hasattr( + self.model, + "supported_lora_modules") and self.model.supported_lora_modules, "Model does not support LoRA" + assert hasattr(self.model, "embedding_modules"), "Model does not have embedding_modules" + assert hasattr(self.model, "embedding_padding_modules"), "Model does not have embedding_padding_modules" + self.lora_manager = LRUCacheWorkerLoRAManager( + self.scheduler_config.max_num_seqs, + self.scheduler_config.max_num_batched_tokens + self.scheduler_config.max_paddings, vocab_size, + self.lora_config, self.device, self.model.embedding_modules, self.model.embedding_padding_modules) + self.model = self.lora_manager.create_lora_manager(self.model) + + def _prepare_sample( + self, + seq_group_metadata_list: List[SequenceGroupMetadata], + prompt_lens: List[int], + subquery_lens: Optional[List[int]], + ) -> SamplingMetadata: + seq_groups: List[Tuple[List[int], SamplingParams]] = [] + selected_token_indices: List[int] = [] + selected_token_start_idx = 0 + categorized_sample_indices = {t: [] for t in SamplingType} + categorized_sample_indices_start_idx = 0 + + max_subquery_len = max(subquery_lens) if subquery_lens else 1 + for i, seq_group_metadata in enumerate(seq_group_metadata_list): + seq_ids = list(seq_group_metadata.seq_data.keys()) + sampling_params = seq_group_metadata.sampling_params + seq_groups.append((seq_ids, sampling_params)) + + if seq_group_metadata.is_prompt: + assert len(seq_ids) == 1 + assert subquery_lens is not None + subquery_len = subquery_lens[i] + if sampling_params.prompt_logprobs is not None: + # NOTE: prompt token positions do not need sample, skip + categorized_sample_indices_start_idx += subquery_len - 1 + + categorized_sample_indices[sampling_params.sampling_type].append(categorized_sample_indices_start_idx) + categorized_sample_indices_start_idx += 1 + + if sampling_params.prompt_logprobs is not None: + selected_token_indices.extend( + range(selected_token_start_idx, selected_token_start_idx + subquery_len - 1)) + selected_token_indices.append(selected_token_start_idx + subquery_len - 1) + selected_token_start_idx += max_subquery_len + else: + num_seqs = len(seq_ids) + selected_token_indices.extend(range(selected_token_start_idx, selected_token_start_idx + num_seqs)) + selected_token_start_idx += num_seqs + + categorized_sample_indices[sampling_params.sampling_type].extend( + range(categorized_sample_indices_start_idx, categorized_sample_indices_start_idx + num_seqs)) + categorized_sample_indices_start_idx += num_seqs + + selected_token_indices = _async_h2d(selected_token_indices, + dtype=torch.long, + target_device=self.device, + pin_memory=not self.in_wsl) + categorized_sample_indices = { + t: _async_h2d(seq_ids, dtype=torch.int, target_device=self.device, pin_memory=not self.in_wsl) + for t, seq_ids in categorized_sample_indices.items() + } + + seq_data: Dict[int, SequenceData] = {} + for seq_group_metadata in seq_group_metadata_list: + seq_data.update(seq_group_metadata.seq_data) + + sampling_metadata = SamplingMetadata( + seq_groups=seq_groups, + seq_data=seq_data, + prompt_lens=prompt_lens, + selected_token_indices=selected_token_indices, + categorized_sample_indices=categorized_sample_indices, + ) + return sampling_metadata + + def prepare_input_tensors( + self, + seq_group_metadata_list: Optional[List[SequenceGroupMetadata]], + ) -> Tuple[torch.Tensor, torch.Tensor, InputMetadata, SamplingMetadata, Set[int], LoRAMapping]: + # NOTE: We assume that all sequences in the group are all prompts or + # all decodes. + is_prompt = seq_group_metadata_list[0].is_prompt + # Prepare input tensors. + if is_prompt: + (input_tokens, input_positions, input_metadata, prompt_lens, subquery_lens, lora_index_mapping, + lora_prompt_mapping, lora_requests) = self._prepare_prompt(seq_group_metadata_list) + else: + (input_tokens, input_positions, input_metadata, lora_index_mapping, lora_prompt_mapping, + lora_requests) = self._prepare_decode(seq_group_metadata_list) + prompt_lens = [] + subquery_lens = None + sampling_metadata = self._prepare_sample(seq_group_metadata_list, prompt_lens, subquery_lens) + if self.lora_config: + flat_lora_index_mapping = [item for sublist in lora_index_mapping for item in sublist] + lora_mapping = LoRAMapping( + flat_lora_index_mapping, + lora_prompt_mapping, + ) + else: + lora_mapping = None + + return (input_tokens, input_positions, input_metadata, sampling_metadata, lora_requests, lora_mapping) + + @torch.inference_mode() + def execute_model( + self, + seq_group_metadata_list: Optional[List[SequenceGroupMetadata]], + kv_caches: List[Tuple[torch.Tensor, torch.Tensor]], + ) -> Optional[SamplerOutput]: + (input_tokens, input_positions, input_metadata, sampling_metadata, lora_requests, + lora_mapping) = self.prepare_input_tensors(seq_group_metadata_list) + + if self.lora_config: + self.set_active_loras(lora_requests, lora_mapping) + + # Execute the model. + if input_metadata.use_cuda_graph: + graph_batch_size = input_tokens.shape[0] + model_executable = self.graph_runners[graph_batch_size] + else: + model_executable = self.model + hidden_states = model_executable( + input_ids=input_tokens, + positions=input_positions, + kv_caches=kv_caches, + input_metadata=input_metadata, + ) + + # Sample the next token. + output = self.model.sample( + hidden_states=hidden_states, + sampling_metadata=sampling_metadata, + ) + return output + + @torch.inference_mode() + def profile_run(self) -> None: + # Enable top-k sampling to reflect the accurate memory usage. + vocab_size = self.model_config.get_vocab_size() + # FIXME(sgm): this sampling params will call cumsum(), causing the + # deterministic cumsum throw error + sampling_params = SamplingParams(top_p=0.99, top_k=vocab_size - 1) + max_num_batched_tokens = self.scheduler_config.max_num_batched_tokens + max_num_seqs = self.scheduler_config.max_num_seqs + + # This represents the maximum number of different requests + # that will have unique loras, an therefore the max amount of memory + # consumption create dummy lora request copies from the lora request + # passed in, which contains a lora from the lora warmup path. + dummy_lora_requests = [] + dummy_lora_requests_per_seq = [] + if self.lora_config: + for idx in range(self.lora_config.max_loras): + lora_id = idx + 1 + dummy_lora_request = LoRARequest( + lora_name=f"warmup_{lora_id}", + lora_int_id=lora_id, + lora_local_path="/not/a/real/path", + ) + self.lora_manager.add_dummy_lora(dummy_lora_request, rank=LORA_WARMUP_RANK) + dummy_lora_requests.append(dummy_lora_request) + dummy_lora_requests_per_seq = [ + dummy_lora_requests[idx % len(dummy_lora_requests)] for idx in range(max_num_seqs) + ] + + # Profile memory usage with max_num_sequences sequences and the total + # number of tokens equal to max_num_batched_tokens. + seqs: List[SequenceGroupMetadata] = [] + for group_id in range(max_num_seqs): + seq_len = (max_num_batched_tokens // max_num_seqs + (group_id < max_num_batched_tokens % max_num_seqs)) + seq_data = SequenceData([0] * seq_len) + seq = SequenceGroupMetadata( + request_id=str(group_id), + is_prompt=True, + seq_data={group_id: seq_data}, + sampling_params=sampling_params, + block_tables=None, + lora_request=dummy_lora_requests_per_seq[group_id] if dummy_lora_requests_per_seq else None, + ) + seqs.append(seq) + + # Run the model with the dummy inputs. + num_layers = self.model_config.get_num_layers(self.parallel_config) + kv_caches = [(None, None)] * num_layers + self.execute_model(seqs, kv_caches) + torch.cuda.synchronize() + return diff --git a/verl/third_party/vllm/vllm_v_0_3_1/parallel_state.py b/verl/third_party/vllm/vllm_v_0_3_1/parallel_state.py new file mode 100644 index 000000000..c3b7a45c8 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/parallel_state.py @@ -0,0 +1,147 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM team. +# Adapted from +# https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/parallel_state.py +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +"""Model and data parallel groups.""" + +import torch +import torch.distributed + +import vllm.model_executor.parallel_utils.parallel_state as ps +""" +This version is strongly tied with Megatron to implement HybridEngine and weight sharing between vllm and Megatron. +- We assume the Megatron tp+dp+pp world is already established before calling this function. + +""" + +# Tensor model parallel group that the current rank belongs to. +_TENSOR_MODEL_PARALLEL_GROUP = None + +# Micro Data parallel group. Micro data parallel group is additional dp group that origins from splitting training tp +# into infer_tp and micro_tp. By default, we use order micro_dp - tp +_MICRO_DATA_PARALLEL_GROUP = None + + +def initialize_model_parallel_from_megatron( + tensor_model_parallel_size=None # we set None for backward compatibility to set infer_tp = train_tp +) -> None: + from megatron.core import parallel_state as mpu + from megatron.distributed import new_group + # Get world size and rank. Ensure some consistencies. + assert torch.distributed.is_initialized() + + if tensor_model_parallel_size is None: + tensor_model_parallel_size = mpu.get_tensor_model_parallel_world_size() + else: + assert isinstance(tensor_model_parallel_size, int) + + # Build the tensor model-parallel groups. + assert ps._TENSOR_MODEL_PARALLEL_GROUP is None, ("tensor model parallel group is already initialized") + + assert tensor_model_parallel_size <= mpu.get_tensor_model_parallel_world_size( + ), 'Not implemented for infer_tp > train_tp' + + global _TENSOR_MODEL_PARALLEL_GROUP + global _MICRO_DATA_PARALLEL_GROUP + + assert mpu.get_tensor_model_parallel_world_size() % tensor_model_parallel_size == 0 + + micro_dp_size = mpu.get_tensor_model_parallel_world_size() // tensor_model_parallel_size + + world_size: int = torch.distributed.get_world_size() + + num_micro_dp_groups = world_size // micro_dp_size + + rank = torch.distributed.get_rank() + + # Build the micro dp groups. + assert _MICRO_DATA_PARALLEL_GROUP is None, ("micro data parallel group is already initialized") + for i in range(num_micro_dp_groups): + ranks = range(i * micro_dp_size, (i + 1) * micro_dp_size) + group = new_group(rank=rank, ranks=ranks, group_type='micro_dp') + if rank in ranks: + _MICRO_DATA_PARALLEL_GROUP = group + + if tensor_model_parallel_size == mpu.get_tensor_model_parallel_world_size(): + # using the same tp group as Megatron + ps._TENSOR_MODEL_PARALLEL_GROUP = mpu.get_tensor_model_parallel_group() + + _TENSOR_MODEL_PARALLEL_GROUP = mpu.get_tensor_model_parallel_group() + # no _MICRO_DATA_PARALLEL_GROUP + else: + # initialize a micro_dp group and a tp group + # assume training tp=4, infer tp=2, then, weight is partitioned as + # [1], [2], [3], [4] for training and [1,2], [1,2], [3,4], [3,4] for inference + + # Build the inference tp groups + train_tp = mpu.get_tensor_model_parallel_world_size() + num_tensor_model_parallel_groups_per_train_tp = train_tp // tensor_model_parallel_size + num_tensor_model_parallel_groups = world_size // tensor_model_parallel_size + assert _TENSOR_MODEL_PARALLEL_GROUP is None, ("tensor model parallel group is already initialized") + for i in range(num_tensor_model_parallel_groups // num_tensor_model_parallel_groups_per_train_tp): + start = train_tp * i + end = train_tp * (i + 1) + for j in range(num_tensor_model_parallel_groups_per_train_tp): + ranks = list(range(start, end, num_tensor_model_parallel_groups_per_train_tp)) + for i in range(len(ranks)): + ranks[i] += j + # group = torch.distributed.new_group(ranks) + group = new_group(rank=rank, ranks=ranks, group_type='infer_tp') + if rank in ranks: + _TENSOR_MODEL_PARALLEL_GROUP = group + ps._TENSOR_MODEL_PARALLEL_GROUP = _TENSOR_MODEL_PARALLEL_GROUP + # Build the pipeline model-parallel groups. + # global _PIPELINE_MODEL_PARALLEL_GROUP + # global _PIPELINE_GLOBAL_RANKS + # assert ps._PIPELINE_MODEL_PARALLEL_GROUP is None, ("pipeline model parallel group is already initialized") + + # ps._PIPELINE_MODEL_PARALLEL_GROUP = mpu.get_pipeline_model_parallel_group() + # ps._PIPELINE_GLOBAL_RANKS = mpu.get_pipeline_model_parallel_ranks() + + +""" +Tensor model parallel utilities +""" + + +def get_tensor_model_parallel_group(): + """Get the tensor model parallel group the caller rank belongs to.""" + assert _TENSOR_MODEL_PARALLEL_GROUP is not None, ("tensor model parallel group is not initialized") + return _TENSOR_MODEL_PARALLEL_GROUP + + +def get_tensor_model_parallel_world_size(): + """Return world size for the tensor model parallel group.""" + return torch.distributed.get_world_size(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_rank(): + """Return my rank for the tensor model parallel group.""" + return torch.distributed.get_rank(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_src_rank(): + """Calculate the global rank corresponding to the first local rank + in the tensor model parallel group.""" + global_rank = torch.distributed.get_rank() + local_world_size = get_tensor_model_parallel_world_size() + return (global_rank // local_world_size) * local_world_size + + +""" +Micro Data parallel group +""" + + +def get_micro_data_parallel_group(): + assert _MICRO_DATA_PARALLEL_GROUP is not None + return _MICRO_DATA_PARALLEL_GROUP + + +def get_micro_data_parallel_world_size(): + return torch.distributed.get_world_size(group=get_micro_data_parallel_group()) + + +def get_micro_data_parallel_rank(): + return torch.distributed.get_rank(group=get_micro_data_parallel_group()) diff --git a/verl/third_party/vllm/vllm_v_0_3_1/tokenizer.py b/verl/third_party/vllm/vllm_v_0_3_1/tokenizer.py new file mode 100644 index 000000000..b8de24afb --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/tokenizer.py @@ -0,0 +1,72 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/transformers_utils/tokenizer_group/tokenizer_group.py + +from typing import List, Optional, Tuple, Union + +from transformers import (AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast) + +from vllm.lora.request import LoRARequest +from vllm.utils import make_async, LRUCache +from vllm.transformers_utils.tokenizers import * + + +class TokenizerGroup: + """A group of tokenizers that can be used for LoRA adapters.""" + + def __init__(self, tokenizer: PreTrainedTokenizer, enable_lora: bool, max_num_seqs: int, + max_input_length: Optional[int]): + self.enable_lora = enable_lora + self.max_input_length = max_input_length + self.tokenizer = tokenizer + if enable_lora: + self.lora_tokenizers = LRUCache(capacity=max_num_seqs) + else: + self.lora_tokenizers = None + + def encode(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = self.get_lora_tokenizer(lora_request) + return tokenizer.encode(prompt) + + async def encode_async(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = await self.get_lora_tokenizer_async(lora_request) + return tokenizer.encode(prompt) + + def get_lora_tokenizer(self, lora_request: Optional[LoRARequest]) -> "PreTrainedTokenizer": + if not lora_request or not self.enable_lora: + return self.tokenizer + if lora_request.lora_int_id not in self.lora_tokenizers: + # TODO(sgm): the lora tokenizer is also passed, but may be different + tokenizer = self.tokenizer + # tokenizer = (get_lora_tokenizer( + # lora_request, **self.tokenizer_config) or self.tokenizer) + self.lora_tokenizers.put(lora_request.lora_int_id, tokenizer) + return tokenizer + else: + return self.lora_tokenizers.get(lora_request.lora_int_id) + + # FIXME(sgm): for simplicity, we assign the special token here + @property + def pad_token_id(self): + return self.tokenizer.pad_token_id + + @property + def eos_token_id(self): + return self.tokenizer.eos_token_id diff --git a/verl/third_party/vllm/vllm_v_0_3_1/weight_loaders.py b/verl/third_party/vllm/vllm_v_0_3_1/weight_loaders.py new file mode 100644 index 000000000..72aa26d06 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/weight_loaders.py @@ -0,0 +1,95 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict +import torch +import torch.nn as nn + + +# NOTE(shengguangming): replace the origin weight loader function in the class +def parallel_weight_loader(self, param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Parallel Linear weight loader.""" + assert param.size() == loaded_weight.size( + ), 'the parameter size is not align with the loaded weight size, param size: {}, loaded_weight size: {}'.format( + param.size(), loaded_weight.size()) + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def default_weight_loader(param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Default weight loader.""" + assert param.size() == loaded_weight.size() + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def gpt2_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "lm_head.weight" in name: + # GPT-2 ties the weights of the embedding layer and the final + # linear layer. + continue + if ".attn.bias" in name or ".attn.masked_bias" in name: + # Skip attention mask. + # NOTE: "c_attn.bias" should not be skipped. + continue + if not name.startswith("transformer."): + name = "transformer." + name + param = params_dict[name] + # The HF's GPT-2 implementation uses Conv1D instead of Linear. + # Because of this, we need to transpose the weights. + # Note(zhuohan): the logic below might break quantized models. + for conv1d_weight_name in ["c_attn", "c_proj", "c_fc"]: + if conv1d_weight_name not in name: + continue + if not name.endswith(".weight"): + continue + # TODO: check megatron + loaded_weight = loaded_weight.t() + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # NOTE(shengguangming): the megatron llama may have this prefix + prefix = '0.module.module.' + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if name[:len(prefix)] == prefix: + name = name[len(prefix):] + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def mistral_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # TODO: need to implement a general way to deal with prefix + prefix = '0.module.module.' + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if name[:len(prefix)] == prefix: + name = name[len(prefix):] + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) diff --git a/verl/third_party/vllm/vllm_v_0_3_1/worker.py b/verl/third_party/vllm/vllm_v_0_3_1/worker.py new file mode 100644 index 000000000..50eebd70b --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_3_1/worker.py @@ -0,0 +1,314 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/worker.py +"""A GPU worker class.""" +import os +import gc +from typing import Dict, List, Tuple, Optional, Union, Set + +import torch +import torch.distributed +import torch.nn as nn + +from vllm.config import (CacheConfig, DeviceConfig, ModelConfig, ParallelConfig, SchedulerConfig, LoRAConfig) +from vllm.model_executor import InputMetadata, set_random_seed +from vllm.model_executor.parallel_utils.parallel_state import (initialize_model_parallel) +from vllm.sampling_params import SamplingParams, SamplingType +from vllm.sequence import SamplerOutput, SequenceData, SequenceGroupMetadata +from vllm.worker.cache_engine import CacheEngine +from vllm.model_executor.parallel_utils.custom_all_reduce import init_custom_ar +from vllm.model_executor.parallel_utils.parallel_state import get_tensor_model_parallel_group + +from .model_runner import ModelRunner +from .model_loader import load_weights +from .parallel_state import initialize_model_parallel_from_megatron +from vllm.lora.request import LoRARequest + + +class Worker: + """A worker class that executes (a partition of) the model on a GPU. + + Each worker is associated with a single GPU. The worker is responsible for + maintaining the KV cache and executing the model on the GPU. In case of + distributed inference, each worker is assigned a partition of the model. + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + rank: Optional[int] = None, + distributed_init_method: Optional[str] = None, + lora_config: Optional[LoRAConfig] = None, + kv_cache_dtype: Optional[str] = "auto", + ) -> None: + # self.model = model # will be replaced in the init_model + self.model_config = model_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.rank = rank + self.distributed_init_method = distributed_init_method + self.lora_config = lora_config + + self.model_runner = ModelRunner( + model, + model_config, + parallel_config, + scheduler_config, + device_config, + lora_config=self.lora_config, + kv_cache_dtype=kv_cache_dtype, + ) + + # Uninitialized cache engine. Will be initialized by + # self.init_cache_engine(). + self.cache_config = None + self.block_size = None + self.sliding_window = None + self.cache_engine = None + self.cache_events = None + self.gpu_cache = None + + # For offloading inference engine params + self.cpu_model = None + + def init_model(self, cupy_port: Optional[int] = None): + # torch.distributed.all_reduce does not free the input tensor until + # the synchronization point. This causes the memory usage to grow + # as the number of all_reduce calls increases. This env var disables + # this behavior. + # Related issue: + # https://discuss.pytorch.org/t/cuda-allocation-lifetime-for-inputs-to-distributed-all-reduce/191573 + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + + # Env vars will be set by TORCHRUN. + self.rank = self.rank if self.rank is not None else int(os.getenv("RANK", "-1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + self.device = torch.device(f"cuda:{local_rank}") + if self.rank < 0: + raise ValueError("Invalid or unspecified rank.") + torch.cuda.set_device(self.device) + + _check_if_gpu_supports_dtype(self.model_config.dtype) + + # Initialize the distributed environment. + # TODO: do not use cupy + _init_distributed_environment(self.parallel_config, self.rank, self.distributed_init_method) + if not self.parallel_config.disable_custom_all_reduce: + init_custom_ar() + # Initialize the model. + set_random_seed(self.model_config.seed) + # self.model = get_model(actor_model=self.model, model_config=self.model_config) + + def load_model(self): + self.model_runner.load_model() + + @torch.inference_mode() + def profile_num_available_blocks( + self, + block_size: int, + gpu_memory_utilization: float, + cpu_swap_space: int, + cache_dtype: str, + ) -> Tuple[int, int]: + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + # torch.cuda.reset_peak_memory_stats() + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. + self.model_runner.profile_run() + + # Calculate the number of blocks that can be allocated with the + # profiled peak memory. + torch.cuda.synchronize() + free_gpu_memory, total_gpu_memory = torch.cuda.mem_get_info() + peak_memory = total_gpu_memory - free_gpu_memory + + cache_block_size = CacheEngine.get_cache_block_size(block_size, cache_dtype, self.model_config, + self.parallel_config) + # NOTE(sgm) use the remaining memory + num_gpu_blocks = int((free_gpu_memory * gpu_memory_utilization) // cache_block_size) + # num_gpu_blocks = int((total_gpu_memory * gpu_memory_utilization - peak_memory) // cache_block_size) + num_cpu_blocks = int(cpu_swap_space // cache_block_size) + num_gpu_blocks = max(num_gpu_blocks, 0) + num_cpu_blocks = max(num_cpu_blocks, 0) + if self.model_runner.lora_manager: + self.model_runner.remove_all_loras() + gc.collect() + torch.cuda.empty_cache() + # Synchronize number of blocks with all the rank + num_gpu_blocks = torch.tensor([num_gpu_blocks], device='cuda') + num_cpu_blocks = torch.tensor([num_cpu_blocks], device='cuda') + torch.distributed.all_reduce(num_gpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group()) + torch.distributed.all_reduce(num_cpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group()) + num_gpu_blocks = num_gpu_blocks.item() + num_cpu_blocks = num_cpu_blocks.item() + return num_gpu_blocks, num_cpu_blocks + + def init_cache_engine(self, cache_config: CacheConfig) -> None: + if self.cache_engine is None and self.gpu_cache is None: + self.cache_config = cache_config + self.cache_engine = CacheEngine(self.cache_config, self.model_config, self.parallel_config) + self.cache_events = self.cache_engine.events + self.gpu_cache = self.cache_engine.gpu_cache + self.model_runner.set_block_size(self.cache_engine.block_size) + + def free_cache_engine(self): + # ensure `enforce_eager=True` + self.cache_engine = None + self.gpu_cache = None + + def warm_up_model(self) -> None: + if not self.model_config.enforce_eager: + self.model_runner.capture_model(self.gpu_cache) + # Reset the seed to ensure that the random state is not affected by + # the model initialization and profiling. + set_random_seed(self.model_config.seed) + + def cache_swap( + self, + blocks_to_swap_in: Dict[int, int], + blocks_to_swap_out: Dict[int, int], + blocks_to_copy: Dict[int, List[int]], + ) -> None: + # Issue cache operations. + issued_cache_op = False + if blocks_to_swap_in: + self.cache_engine.swap_in(blocks_to_swap_in) + issued_cache_op = True + if blocks_to_swap_out: + self.cache_engine.swap_out(blocks_to_swap_out) + issued_cache_op = True + if blocks_to_copy: + self.cache_engine.copy(blocks_to_copy) + issued_cache_op = True + + cache_events = self.cache_events if issued_cache_op else None + + # Wait for cache operations to finish. + # TODO(woosuk): Profile swapping overhead and optimize if needed. + if cache_events is not None: + for event in cache_events: + event.wait() + + @torch.inference_mode() + def execute_model( + self, + seq_group_metadata_list: List[SequenceGroupMetadata], + blocks_to_swap_in: Dict[int, int], + blocks_to_swap_out: Dict[int, int], + blocks_to_copy: Dict[int, List[int]], + ) -> SamplerOutput: + num_seq_groups = len(seq_group_metadata_list) + self.cache_swap(blocks_to_swap_in, blocks_to_swap_out, blocks_to_copy) + + # If there is no input, we don't need to execute the model. + if num_seq_groups == 0: + return {} + output = self.model_runner.execute_model(seq_group_metadata_list, self.gpu_cache) + return output + + # # Prepare input tensors. + # # NOTE(shengguangming): currently we pad in our dataloader and unpad it in pre_process_input, j + # # we can just input un-padded sequence for better performance + # input_tokens, input_positions, input_metadata = self._prepare_inputs(seq_group_metadata_list) + + # # Execute the model. + # output = self.model( + # input_ids=input_tokens, + # positions=input_positions, + # kv_caches=self.gpu_cache, + # input_metadata=input_metadata, + # cache_events=cache_events, + # ) + # return output + + # assume the input is .state_dict() + def sync_model_weights(self, actor_weights: Dict): + load_weights(actor_weights, self.model_runner.model) + + def offload_model_weights(self) -> None: + if self.cpu_model == None: + self.cpu_model = {} + for name, params in self.model_runner.model.named_parameters(): + self.cpu_model[name] = torch.empty_like(params, device='cpu') + params.data = self.cpu_model[name] + else: + for name, params in self.model_runner.model.named_parameters(): + params.data = self.cpu_model[name] + + def add_lora(self, lora_request: LoRARequest) -> bool: + return self.model_runner.add_lora(lora_request) + + def remove_lora(self, lora_id: int) -> bool: + return self.model_runner.remove_lora(lora_id) + + def list_loras(self) -> Set[int]: + return self.model_runner.list_loras() + + +def _init_distributed_environment( + parallel_config: ParallelConfig, + rank: int, + distributed_init_method: Optional[str] = None, +) -> None: + """Initialize the distributed environment.""" + if torch.distributed.is_initialized(): + print('The distributed environment has been initialized before vLLM') + elif not distributed_init_method: + raise ValueError("distributed_init_method must be set if torch.distributed " + "is not already initialized") + else: + torch.distributed.init_process_group( + backend="nccl", + world_size=parallel_config.world_size, + rank=rank, + # init_method=distributed_init_method, + ) + + # A small all_reduce for warmup. + torch.distributed.all_reduce(torch.zeros(1).cuda()) + # TODO (shengguangming): maybe we should also flag the megatron is initialized + if torch.distributed.get_world_size() > 1: + initialize_model_parallel_from_megatron(tensor_model_parallel_size=parallel_config.tensor_parallel_size) + else: + initialize_model_parallel() + + +def _pad_to_alignment(x: List[int], multiple_of: int, pad: int) -> List[int]: + return x + [pad] * ((-len(x)) % multiple_of) + + +def _pad_to_max(x: List[int], max_len: int, pad: int) -> List[int]: + return x + [pad] * (max_len - len(x)) + + +def _check_if_gpu_supports_dtype(torch_dtype: torch.dtype): + # Check if the GPU supports the dtype. + if torch_dtype == torch.bfloat16: + compute_capability = torch.cuda.get_device_capability() + if compute_capability[0] < 8: + gpu_name = torch.cuda.get_device_name() + raise ValueError("Bfloat16 is only supported on GPUs with compute capability " + f"of at least 8.0. Your {gpu_name} GPU has compute capability " + f"{compute_capability[0]}.{compute_capability[1]}.") diff --git a/verl/third_party/vllm/vllm_v_0_4_2/__init__.py b/verl/third_party/vllm/vllm_v_0_4_2/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/__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/third_party/vllm/vllm_v_0_4_2/arg_utils.py b/verl/third_party/vllm/vllm_v_0_4_2/arg_utils.py new file mode 100644 index 000000000..089bbd748 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/arg_utils.py @@ -0,0 +1,320 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/arg_utils.py + +import os +import argparse +import dataclasses +from dataclasses import dataclass +from typing import List, Optional, Union + +import torch.nn as nn + +from transformers import PretrainedConfig +from .config import ModelConfig, LoadConfig + +from vllm.config import (CacheConfig, DecodingConfig, DeviceConfig, EngineConfig, LoRAConfig, ParallelConfig, + SchedulerConfig, SpeculativeConfig, TokenizerPoolConfig, VisionLanguageConfig) +from vllm.model_executor.layers.quantization import QUANTIZATION_METHODS +from vllm.utils import str_to_int_tuple + + +def nullable_str(val: str): + if not val or val == "None": + return None + return val + + +@dataclass +class EngineArgs: + """Arguments for vLLM engine.""" + model_hf_config: PretrainedConfig = None + skip_tokenizer_init: bool = False + served_model_name: Optional[Union[str, List[str]]] = None # TODO + download_dir: Optional[str] = None + load_format: str = 'auto' + dtype: str = 'auto' + kv_cache_dtype: str = 'auto' + quantization_param_path: Optional[str] = None + seed: int = 0 + max_model_len: Optional[int] = None + worker_use_ray: bool = False + pipeline_parallel_size: int = 1 + tensor_parallel_size: int = 1 + max_parallel_loading_workers: Optional[int] = None + block_size: int = 16 + enable_prefix_caching: bool = False + use_v2_block_manager: bool = False + swap_space: int = 4 # GiB + gpu_memory_utilization: float = 0.90 + max_num_batched_tokens: Optional[int] = None + max_num_seqs: int = 256 + max_logprobs: int = 5 # OpenAI default value + disable_log_stats: bool = False + revision: Optional[str] = None + code_revision: Optional[str] = None + tokenizer_revision: Optional[str] = None + quantization: Optional[str] = None + enforce_eager: bool = False + max_context_len_to_capture: Optional[int] = None + max_seq_len_to_capture: int = 8192 + disable_custom_all_reduce: bool = False + tokenizer_pool_size: int = 0 + tokenizer_pool_type: str = "ray" + tokenizer_pool_extra_config: Optional[dict] = None + enable_lora: bool = False + max_loras: int = 1 + max_lora_rank: int = 16 + fully_sharded_loras: bool = False + lora_extra_vocab_size: int = 256 + lora_dtype = 'auto' + max_cpu_loras: Optional[int] = None + device: str = 'auto' + ray_workers_use_nsight: bool = False + num_gpu_blocks_override: Optional[int] = None + num_lookahead_slots: int = 0 + model_loader_extra_config: Optional[dict] = None + + # Related to Vision-language models such as llava + image_input_type: Optional[str] = None + image_token_id: Optional[int] = None + image_input_shape: Optional[str] = None + image_feature_size: Optional[int] = None + scheduler_delay_factor: float = 0.0 + enable_chunked_prefill: bool = False + + guided_decoding_backend: str = 'outlines' + # Speculative decoding configuration. + speculative_model: Optional[str] = None + num_speculative_tokens: Optional[int] = None + speculative_max_model_len: Optional[int] = None + ngram_prompt_lookup_max: Optional[int] = None + ngram_prompt_lookup_min: Optional[int] = None + + @staticmethod + def add_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: + """Shared CLI arguments for vLLM engine.""" + # Model arguments + # TODO(shengguangming): delete the unused args + parser.add_argument('--model', + type=str, + default='facebook/opt-125m', + help='name or path of the huggingface model to use') + parser.add_argument('--tokenizer', + type=str, + default=EngineArgs.tokenizer, + help='name or path of the huggingface tokenizer to use') + parser.add_argument('--revision', + type=str, + default=None, + help='the specific model version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-revision', + type=str, + default=None, + help='the specific tokenizer version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-mode', + type=str, + default=EngineArgs.tokenizer_mode, + choices=['auto', 'slow'], + help='tokenizer mode. "auto" will use the fast ' + 'tokenizer if available, and "slow" will ' + 'always use the slow tokenizer.') + parser.add_argument('--trust-remote-code', action='store_true', help='trust remote code from huggingface') + parser.add_argument('--download-dir', + type=str, + default=EngineArgs.download_dir, + help='directory to download and load the weights, ' + 'default to the default cache dir of ' + 'huggingface') + parser.add_argument('--load-format', + type=str, + default=EngineArgs.load_format, + choices=['auto', 'pt', 'safetensors', 'npcache', 'dummy'], + help='The format of the model weights to load. ' + '"auto" will try to load the weights in the safetensors format ' + 'and fall back to the pytorch bin format if safetensors format ' + 'is not available. ' + '"pt" will load the weights in the pytorch bin format. ' + '"safetensors" will load the weights in the safetensors format. ' + '"npcache" will load the weights in pytorch format and store ' + 'a numpy cache to speed up the loading. ' + '"dummy" will initialize the weights with random values, ' + 'which is mainly for profiling.') + parser.add_argument('--dtype', + type=str, + default=EngineArgs.dtype, + choices=['auto', 'half', 'float16', 'bfloat16', 'float', 'float32'], + help='data type for model weights and activations. ' + 'The "auto" option will use FP16 precision ' + 'for FP32 and FP16 models, and BF16 precision ' + 'for BF16 models.') + parser.add_argument('--max-model-len', + type=int, + default=None, + help='model context length. If unspecified, ' + 'will be automatically derived from the model.') + # Parallel arguments + parser.add_argument('--worker-use-ray', + action='store_true', + help='use Ray for distributed serving, will be ' + 'automatically set when using more than 1 GPU') + parser.add_argument('--pipeline-parallel-size', + '-pp', + type=int, + default=EngineArgs.pipeline_parallel_size, + help='number of pipeline stages') + parser.add_argument('--tensor-parallel-size', + '-tp', + type=int, + default=EngineArgs.tensor_parallel_size, + help='number of tensor parallel replicas') + # KV cache arguments + parser.add_argument('--block-size', + type=int, + default=EngineArgs.block_size, + choices=[8, 16, 32], + help='token block size') + # TODO(woosuk): Support fine-grained seeds (e.g., seed per request). + parser.add_argument('--seed', type=int, default=EngineArgs.seed, help='random seed') + parser.add_argument('--swap-space', + type=int, + default=EngineArgs.swap_space, + help='CPU swap space size (GiB) per GPU') + parser.add_argument('--gpu-memory-utilization', + type=float, + default=EngineArgs.gpu_memory_utilization, + help='the percentage of GPU memory to be used for' + 'the model executor') + parser.add_argument('--max-num-batched-tokens', + type=int, + default=EngineArgs.max_num_batched_tokens, + help='maximum number of batched tokens per ' + 'iteration') + parser.add_argument('--max-num-seqs', + type=int, + default=EngineArgs.max_num_seqs, + help='maximum number of sequences per iteration') + parser.add_argument('--disable-log-stats', action='store_true', help='disable logging statistics') + # Quantization settings. + parser.add_argument('--quantization', + '-q', + type=str, + choices=['awq', None], + default=None, + help='Method used to quantize the weights') + return parser + + @classmethod + def from_cli_args(cls, args: argparse.Namespace) -> 'EngineArgs': + # Get the list of attributes of this dataclass. + attrs = [attr.name for attr in dataclasses.fields(cls)] + # Set the attributes from the parsed arguments. + engine_args = cls(**{attr: getattr(args, attr) for attr in attrs}) + return engine_args + + def create_engine_config( + self, + ) -> EngineConfig: + device_config = DeviceConfig(self.device) + # NOTE(sgm): we only modify ModelConfig, other configs are import from vllm + model_config = ModelConfig(self.model_hf_config, self.dtype, self.seed, self.revision, self.code_revision, + self.tokenizer_revision, self.max_model_len, self.quantization, + self.quantization_param_path, self.enforce_eager, self.max_context_len_to_capture, + self.max_seq_len_to_capture, self.max_logprobs, self.skip_tokenizer_init, + self.served_model_name) + cache_config = CacheConfig(self.block_size, self.gpu_memory_utilization, + self.swap_space, self.kv_cache_dtype, self.num_gpu_blocks_override, + model_config.get_sliding_window(), self.enable_prefix_caching) + parallel_config = ParallelConfig( + self.pipeline_parallel_size, self.tensor_parallel_size, self.worker_use_ray, + self.max_parallel_loading_workers, self.disable_custom_all_reduce, + TokenizerPoolConfig.create_config( + self.tokenizer_pool_size, + self.tokenizer_pool_type, + self.tokenizer_pool_extra_config, + ), self.ray_workers_use_nsight) + + # Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + parallel_config.world_size = world_size + + # TODO: spec config + speculative_config = SpeculativeConfig.maybe_create_spec_config( + target_model_config=model_config, + target_parallel_config=parallel_config, + target_dtype=self.dtype, + speculative_model=self.speculative_model, + num_speculative_tokens=self.num_speculative_tokens, + speculative_max_model_len=self.speculative_max_model_len, + enable_chunked_prefill=self.enable_chunked_prefill, + use_v2_block_manager=self.use_v2_block_manager, + ngram_prompt_lookup_max=self.ngram_prompt_lookup_max, + ngram_prompt_lookup_min=self.ngram_prompt_lookup_min, + ) + + scheduler_config = SchedulerConfig( + self.max_num_batched_tokens, + self.max_num_seqs, + model_config.max_model_len, + self.use_v2_block_manager, + num_lookahead_slots=(self.num_lookahead_slots + if speculative_config is None else speculative_config.num_lookahead_slots), + delay_factor=self.scheduler_delay_factor, + enable_chunked_prefill=self.enable_chunked_prefill, + ) + + lora_config = LoRAConfig(max_lora_rank=self.max_lora_rank, + max_loras=self.max_loras, + fully_sharded_loras=self.fully_sharded_loras, + lora_extra_vocab_size=self.lora_extra_vocab_size, + lora_dtype=self.lora_dtype, + max_cpu_loras=self.max_cpu_loras if self.max_cpu_loras and self.max_cpu_loras > 0 else + None) if self.enable_lora else None + + load_config = LoadConfig( + load_format=self.load_format, + download_dir=self.download_dir, + model_loader_extra_config=self.model_loader_extra_config, + ) + + if self.image_input_type: + if (not self.image_token_id or not self.image_input_shape or not self.image_feature_size): + raise ValueError('Specify `image_token_id`, `image_input_shape` and ' + '`image_feature_size` together with `image_input_type`.') + vision_language_config = VisionLanguageConfig( + image_input_type=VisionLanguageConfig.get_image_input_enum_type(self.image_input_type), + image_token_id=self.image_token_id, + image_input_shape=str_to_int_tuple(self.image_input_shape), + image_feature_size=self.image_feature_size, + ) + else: + vision_language_config = None + + decoding_config = DecodingConfig(guided_decoding_backend=self.guided_decoding_backend) + + return EngineConfig(model_config=model_config, + cache_config=cache_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + device_config=device_config, + lora_config=lora_config, + vision_language_config=vision_language_config, + speculative_config=speculative_config, + load_config=load_config, + decoding_config=decoding_config) diff --git a/verl/third_party/vllm/vllm_v_0_4_2/config.py b/verl/third_party/vllm/vllm_v_0_4_2/config.py new file mode 100644 index 000000000..76ccd5912 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/config.py @@ -0,0 +1,201 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/config.py + + +import enum +import json +from typing import List, Optional, Union +from dataclasses import dataclass, field, fields + +from transformers import PretrainedConfig + +from vllm.logger import init_logger +from vllm.model_executor.layers.quantization import get_quantization_config +from vllm.transformers_utils.config import get_hf_text_config +from vllm.utils import is_hip +# Add for verl +from vllm.config import ModelConfig, _get_and_verify_dtype, _get_and_verify_max_len + +GPTQMarlinConfig = get_quantization_config("gptq_marlin") + +logger = init_logger(__name__) + +_GB = 1 << 30 + + +class ModelConfig(ModelConfig): + """Configuration for the model. + + Args: + model: Name or path of the huggingface model to use. + tokenizer: Name or path of the huggingface tokenizer to use. + tokenizer_mode: Tokenizer mode. "auto" will use the fast tokenizer if + available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + download_dir: Directory to download and load the weights, default to the + default cache directory of huggingface. + load_format: The format of the model weights to load: + "auto" will try to load the weights in the safetensors format and + fall back to the pytorch bin format if safetensors format is + not available. + "pt" will load the weights in the pytorch bin format. + "safetensors" will load the weights in the safetensors format. + "npcache" will load the weights in pytorch format and store + a numpy cache to speed up the loading. + "dummy" will initialize the weights with random values, which is + mainly for profiling. + dtype: Data type for model weights and activations. The "auto" option + will use FP16 precision for FP32 and FP16 models, and BF16 precision + for BF16 models. + seed: Random seed for reproducibility. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. If unspecified, will use the default + version. + code_revision: The specific revision to use for the model code on + Hugging Face Hub. It can be a branch name, a tag name, or a + commit id. If unspecified, will use the default version. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. If unspecified, will use + the default version. + max_model_len: Maximum length of a sequence (including prompt and + output). If None, will be derived from the model. + quantization: Quantization method that was used to quantize the model + weights. If None, we assume the model weights are not quantized. + quantization_param_path: Path to JSON file containing scaling factors. + Used to load KV cache scaling factors into the model when KV cache + type is FP8_E4M3 on ROCm (AMD GPU). In the future these will also + be used to load activation and weight scaling factors when the + model dtype is FP8_E4M3 on ROCm. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode (DEPRECATED. Use max_seq_len_to_capture instead). + max_seq_len_to_capture: Maximum sequence len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode + skip_tokenizer_init: If true, skip initialization of tokenizer and + detokenizer. + served_model_name: The model name used in metrics tag `model_name`, + matches the model name exposed via the APIs. If multiple model + names provided, the first name will be used. If not specified, + the model name will be the same as `model`. + """ + + def __init__( + self, + hf_config: PretrainedConfig, + dtype: str, + seed: int, + revision: Optional[str] = None, + code_revision: Optional[str] = None, + tokenizer_revision: Optional[str] = None, + max_model_len: Optional[int] = None, + quantization: Optional[str] = None, + quantization_param_path: Optional[str] = None, + enforce_eager: bool = False, + max_context_len_to_capture: Optional[int] = None, + max_seq_len_to_capture: Optional[int] = None, + max_logprobs: int = 5, + skip_tokenizer_init: bool = False, + served_model_name: Optional[Union[str, List[str]]] = None, + ) -> None: + self.model = hf_config._name_or_path + self.tokenizer = hf_config._name_or_path + self.seed = seed + self.revision = revision + self.code_revision = code_revision + self.tokenizer_revision = tokenizer_revision + self.quantization = quantization + self.quantization_param_path = quantization_param_path + self.enforce_eager = enforce_eager + self.max_context_len_to_capture = max_context_len_to_capture + if self.max_context_len_to_capture is not None: + raise ValueError("`max_context_len_to_capture` is deprecated. " + "Use `max_seq_len_to_capture` instead.") + self.max_seq_len_to_capture = (max_seq_len_to_capture or max_context_len_to_capture) + self.max_logprobs = max_logprobs + self.skip_tokenizer_init = skip_tokenizer_init + + # self.hf_config = get_config(model, trust_remote_code, revision) + self.hf_config = hf_config + self.hf_text_config = get_hf_text_config(hf_config) + # TODO: for multimodal model + self.dtype = _get_and_verify_dtype(self.hf_config, dtype) + self.max_model_len = _get_and_verify_max_len(self.hf_config, max_model_len) + # self.served_model_name = get_served_model_name(model, + # served_model_name) + # self._verify_load_format() + # self._verify_tokenizer_mode() + self._verify_quantization() + self._verify_cuda_graph() + + +class LoadFormat(str, enum.Enum): + AUTO = 'auto' + MEGATRON = "megatron" + HF = "hf" + DTENSOR = 'dtensor' + DUMMY_HF = 'dummy_hf' + DUMMY_MEGATRON = 'dummy_megatron' + DUMMY_DTENSOR = 'dummy_dtensor' + + +@dataclass +class LoadConfig: + """ + download_dir: Directory to download and load the weights, default to the + default cache directory of huggingface. + load_format: The format of the model weights to load: + "auto" will try to load the weights in the safetensors format and + fall back to the pytorch bin format if safetensors format is + not available. + "pt" will load the weights in the pytorch bin format. + "safetensors" will load the weights in the safetensors format. + "npcache" will load the weights in pytorch format and store + a numpy cache to speed up the loading. + "dummy" will initialize the weights with random values, which is + mainly for profiling. + "tensorizer" will use CoreWeave's tensorizer library for + fast weight loading. + """ + + load_format: Union[str, LoadFormat, "BaseModelLoader"] = LoadFormat.AUTO + download_dir: Optional[str] = None + model_loader_extra_config: Optional[Union[str, dict]] = field(default_factory=dict) + + def __post_init__(self): + model_loader_extra_config = self.model_loader_extra_config or {} + if isinstance(model_loader_extra_config, str): + self.model_loader_extra_config = json.loads(model_loader_extra_config) + self._verify_load_format() + + def _verify_load_format(self) -> None: + if not isinstance(self.load_format, str): + return + + load_format = self.load_format.lower() + self.load_format = LoadFormat(load_format) + + rocm_not_supported_load_format: List[str] = [] + if is_hip() and load_format in rocm_not_supported_load_format: + rocm_supported_load_format = [ + f for f in LoadFormat.__members__ if (f not in rocm_not_supported_load_format) + ] + raise ValueError(f"load format '{load_format}' is not supported in ROCm. " + f"Supported load formats are " + f"{rocm_supported_load_format}") diff --git a/verl/third_party/vllm/vllm_v_0_4_2/dtensor_weight_loaders.py b/verl/third_party/vllm/vllm_v_0_4_2/dtensor_weight_loaders.py new file mode 100644 index 000000000..6668b7509 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/dtensor_weight_loaders.py @@ -0,0 +1,269 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict, Iterable, Tuple +import torch +import torch.nn as nn +from torch.distributed._tensor import DTensor, Shard, Replicate + +from vllm.model_executor.layers.linear import * +from vllm.model_executor.models import ModelRegistry +from vllm.model_executor.model_loader.weight_utils import default_weight_loader + + +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(vllm_model.named_parameters()) + 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 + stacked_name = name.replace(shard_name, param_name) + # Skip loading extra bias for GPTQ models. + if stacked_name.endswith(".bias") and stacked_name not in params_dict: + continue + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[stacked_name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + 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 + # GemmaRMSNorm is different from Llama's in that it multiplies + # (1 + weight) to the output, instead of just weight. + if "norm.weight" in name: + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + + norm_weight = local_loaded_weight + 1.0 + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, norm_weight.to(dtype=param.dtype)) + else: + 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, local_loaded_weight.to(dtype=param.dtype)) + + +def gptbigcode_dtensor_load_weights(actor_weights: Dict, vllm_model: nn.Module): + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "lm_head.weight" in name: + continue + if ".attn.bias" in name: + # Skip attention mask. + # NOTE: "c_attn.bias" should not be skipped. + 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, local_loaded_weight.to(dtype=param.dtype)) + + +def starcoder2_dtensor_load_weights(actor_weights: Dict, vllm_model: 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"), + ] + + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_name, param_name) + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[name] + weight_loader = param.weight_loader + weight_loader(param, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + + +def llama_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(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + if ("rotary_emb.cos_cached" in name or "rotary_emb.sin_cached" in name): + # Models trained using ColossalAI may include these tensors in + # the checkpoint. Skip them. + continue + # With tie_word_embeddings, we can skip lm_head.weight + # The weight might appear unnecessarily in the files if the model is + # processed with quantization, LoRA, fine-tuning, etc. + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_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, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + # 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, local_loaded_weight) + + +def qwen2_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(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_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, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + + +def gpt2_dtensor_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + pass + + +def redistribute_dtensor(param_name: str, loaded_weights: DTensor, parallelize_plan: Dict = None): + param_name = _process_parameter_names(name=param_name) + if parallelize_plan is not None: + assert param_name in parallelize_plan.keys(), \ + f"param name: {param_name} not in parallelize_plan :{parallelize_plan.keys()}" + placement = parallelize_plan[param_name] + local_loaded_weights = loaded_weights.redistribute(device_mesh=loaded_weights.device_mesh, + placements=placement).to_local() + else: + local_loaded_weights = loaded_weights.full_tensor() + return local_loaded_weights + + +def _process_parameter_names(name): + # Remove '.weight' if it exists at the end of the string + if name.endswith(".weight"): + name = name[:-7] + + # Remove 'model.layers.x.' or 'model.' prefix + if "model.layers" in name: + parts = name.split('.') + # Reconstruct the string without 'model.layers.x.' + name = '.'.join(parts[3:]) # parts[0] is 'model', parts[1] is 'layers', parts[2] is 'x' + elif name.startswith("model."): + name = name[6:] # Remove 'model.' + + return name + + +__MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__ = { + 'GPT2LMHeadModel': gpt2_dtensor_weight_loader, + 'LlamaForCausalLM': llama_dtensor_weight_loader, + 'LLaMAForCausalLM': llama_dtensor_weight_loader, + 'MistralForCausalLM': llama_dtensor_weight_loader, # mistral is the same as llama in vLLM + 'InternLMForCausalLM': llama_dtensor_weight_loader, + 'AquilaModel': llama_dtensor_weight_loader, + 'AquilaForCausalLM': llama_dtensor_weight_loader, + 'Phi3ForCausalLM': llama_dtensor_weight_loader, + 'GemmaForCausalLM': gemma_dtensor_weight_loader, + 'GPTBigCodeForCausalLM': gptbigcode_dtensor_load_weights, + 'Starcoder2ForCausalLM': starcoder2_dtensor_load_weights, + 'Qwen2ForCausalLM': qwen2_dtensor_weight_loader +} + + +# the actor model is .state_dict() +# Load dtensor weights +def load_dtensor_weights(actor_weights: Dict, vllm_model: nn.Module): + weight_loader = _get_model_weight_loader(vllm_model.__class__.__name__) + weight_loader(actor_weights, vllm_model) + # NOTE(sgm) to reduce peak memory usage, we offload vllm model to cpu + # after init, and we need this after sync model weights for in first iter. + vllm_model = vllm_model.cuda() + + +def _get_model_weight_loader(arch: str): + if arch in __MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__: + return __MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {__MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__.keys()}") + + +# NOTE(sgm): we use per-parameter weight loader in each vllm sub +def update_dtensor_weight_loader(): + pass diff --git a/verl/third_party/vllm/vllm_v_0_4_2/hf_weight_loader.py b/verl/third_party/vllm/vllm_v_0_4_2/hf_weight_loader.py new file mode 100644 index 000000000..0d562e596 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/hf_weight_loader.py @@ -0,0 +1,91 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict, Union, Optional, Iterable, Tuple + +import torch +import torch.nn as nn + +from vllm.model_executor.model_loader.utils import set_default_torch_dtype +from vllm.model_executor.model_loader.weight_utils import default_weight_loader + + +def update_hf_weight_loader(): + from vllm.model_executor.models.gemma import GemmaForCausalLM + GemmaForCausalLM.load_weights = gemma_load_weights + + +def gemma_load_weights(self, weights: Iterable[Tuple[str, torch.Tensor]]): + 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()) + loaded_params = set() + for name, loaded_weight in weights: + 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 + param = params_dict[name] + weight_loader = param.weight_loader + weight_loader(param, loaded_weight, 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 + # GemmaRMSNorm is different from Llama's in that it multiplies + # (1 + weight) to the output, instead of just weight. + if "norm.weight" in name: + norm_weight = loaded_weight + 1.0 # prevent inplace modify actor weights + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, norm_weight) + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + 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}") + + +def load_hf_weights(actor_weights: Dict, vllm_model: nn.Module): + assert isinstance(actor_weights, Dict) + with set_default_torch_dtype(next(vllm_model.parameters()).dtype): # TODO + vllm_model.load_weights(actor_weights.items()) + for _, module in vllm_model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + vllm_model = vllm_model.cuda() diff --git a/verl/third_party/vllm/vllm_v_0_4_2/llm.py b/verl/third_party/vllm/vllm_v_0_4_2/llm.py new file mode 100644 index 000000000..b042525d4 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/llm.py @@ -0,0 +1,306 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/llm.py + +from typing import Dict, List, Optional, Tuple, Union + +from tqdm import tqdm +from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast +from transformers import PretrainedConfig +import torch.nn as nn +from .arg_utils import EngineArgs +from .llm_engine_sp import LLMEngine +from vllm.lora.request import LoRARequest +from vllm.outputs import RequestOutput +from vllm.sampling_params import SamplingParams +from vllm.sequence import MultiModalData +from vllm.usage.usage_lib import UsageContext +from vllm.utils import Counter +import torch +from torch.nn.utils.rnn import pad_sequence +from verl.trainer.ppo.rollout.tokenizer import HybridEngineBaseTokenizer + + +class LLM: + """An LLM for generating texts from given prompts and sampling parameters. + + This class includes a tokenizer, a language model (possibly distributed + across multiple GPUs), and GPU memory space allocated for intermediate + states (aka KV cache). Given a batch of prompts and sampling parameters, + this class generates texts from the model, using an intelligent batching + mechanism and efficient memory management. + + NOTE: This class is intended to be used for offline inference. For online + serving, use the `AsyncLLMEngine` class instead. + NOTE: For the comprehensive list of arguments, see `EngineArgs`. + + Args: + model: A HuggingFace Transformers model instance. + tokenizer: A HuggingFace Transformers tokenizer instance. + tokenizer_mode: The tokenizer mode. "auto" will use the fast tokenizer + if available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + tensor_parallel_size: The number of GPUs to use for distributed + execution with tensor parallelism. + dtype: The data type for the model weights and activations. Currently, + we support `float32`, `float16`, and `bfloat16`. If `auto`, we use + the `torch_dtype` attribute specified in the model config file. + However, if the `torch_dtype` in the config is `float32`, we will + use `float16` instead. + quantization: The method used to quantize the model weights. Currently, + we support "awq". If None, we assume the model weights are not + quantized and use `dtype` to determine the data type of the weights. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. + seed: The seed to initialize the random number generator for sampling. + gpu_memory_utilization: The ratio (between 0 and 1) of GPU memory to + reserve for the model weights, activations, and KV cache. Higher + values will increase the KV cache size and thus improve the model's + throughput. However, if the value is too high, it may cause out-of- + memory (OOM) errors. + swap_space: The size (GiB) of CPU memory per GPU to use as swap space. + This can be used for temporarily storing the states of the requests + when their `best_of` sampling parameters are larger than 1. If all + requests will have `best_of=1`, you can safely set this to 0. + Otherwise, too small values may cause out-of-memory (OOM) errors. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode. + disable_custom_all_reduce: See ParallelConfig + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer], + model_hf_config: PretrainedConfig, + tokenizer_mode: str = "auto", + trust_remote_code: bool = False, + tensor_parallel_size: int = 1, + dtype: str = "auto", + quantization: Optional[str] = None, + revision: Optional[str] = None, + tokenizer_revision: Optional[str] = None, + seed: int = 0, + gpu_memory_utilization: float = 0.9, + swap_space: int = 4, + enforce_eager: bool = False, + max_context_len_to_capture: int = None, + disable_custom_all_reduce: bool = False, + load_format = 'auto', + **kwargs, + ) -> None: + if "disable_log_stats" not in kwargs: + kwargs["disable_log_stats"] = True + engine_args = EngineArgs( + model_hf_config=model_hf_config, + tensor_parallel_size=tensor_parallel_size, + dtype=dtype, + quantization=quantization, + revision=revision, + tokenizer_revision=tokenizer_revision, + seed=seed, + gpu_memory_utilization=gpu_memory_utilization, + swap_space=swap_space, + enforce_eager=enforce_eager, + max_context_len_to_capture=max_context_len_to_capture, + disable_custom_all_reduce=disable_custom_all_reduce, + load_format=load_format, + **kwargs, + ) + tokenizer_cls = (PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer) + if not isinstance(tokenizer, tokenizer_cls): + raise ValueError( + f"Unexpected tokenizer type: {type(tokenizer)}. Must be" + "one of the following: PreTrainedTokenizer, PreTrainedTokenizerFast, verl.trainer.ppo.rollout.HybridEngineBaseTokenizer" + ) + self.llm_engine = LLMEngine.from_engine_args(model, tokenizer, engine_args) + self.request_counter = Counter() + + def init_cache_engine(self): + self.llm_engine.init_cache_engine() + + def free_cache_engine(self): + self.llm_engine.free_cache_engine() + + def get_tokenizer(self) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]: + return self.llm_engine.tokenizer + + def set_tokenizer( + self, + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], + ) -> None: + self.llm_engine.tokenizer = tokenizer + + def generate( + self, + prompts: Optional[Union[str, List[str]]] = None, + sampling_params: Optional[Union[SamplingParams, List[SamplingParams]]] = None, + prompt_token_ids: Optional[List[List[int]]] = None, + use_tqdm: bool = True, + lora_request: Optional[LoRARequest] = None, + multi_modal_data: Optional[MultiModalData] = None, + ) -> List[RequestOutput]: + """Generates the completions for the input prompts. + + NOTE: This class automatically batches the given prompts, considering + the memory constraint. For the best performance, put all of your prompts + into a single list and pass it to this method. + + Args: + prompts: A list of prompts to generate completions for. + sampling_params: The sampling parameters for text generation. If + None, we use the default sampling parameters. + When it is a single value, it is applied to every prompt. + When it is a list, the list must have the same length as the + prompts and it is paired one by one with the prompt. + prompt_token_ids: A list of token IDs for the prompts. If None, we + use the tokenizer to convert the prompts to token IDs. + use_tqdm: Whether to use tqdm to display the progress bar. + lora_request: LoRA request to use for generation, if any. + multi_modal_data: Multi modal data. + + Returns: + A list of `RequestOutput` objects containing the generated + completions in the same order as the input prompts. + """ + if prompts is None and prompt_token_ids is None: + raise ValueError("Either prompts or prompt_token_ids must be " + "provided.") + if self.llm_engine.model_config.skip_tokenizer_init \ + and prompts is not None: + raise ValueError("prompts must be None if skip_tokenizer_init " + "is True") + if isinstance(prompts, str): + # Convert a single prompt to a list. + prompts = [prompts] + if (prompts is not None and prompt_token_ids is not None and len(prompts) != len(prompt_token_ids)): + raise ValueError("The lengths of prompts and prompt_token_ids " + "must be the same.") + + if prompts is not None: + num_requests = len(prompts) + else: + assert prompt_token_ids is not None + num_requests = len(prompt_token_ids) + + if sampling_params is None: + # Use default sampling params. + sampling_params = SamplingParams() + + elif isinstance(sampling_params, list) and len(sampling_params) != num_requests: + raise ValueError("The lengths of prompts and sampling_params " + "must be the same.") + if multi_modal_data: + multi_modal_data.data = multi_modal_data.data.to(torch.float16) + + # Add requests to the engine. + for i in range(num_requests): + prompt = prompts[i] if prompts is not None else None + token_ids = None if prompt_token_ids is None else prompt_token_ids[i] + if not isinstance(token_ids, list): + # NOTE(shengguangming): convert the rollout input into List[str] + token_ids = self._pre_process_inputs(token_ids) + self._add_request( + prompt, + sampling_params[i] if isinstance(sampling_params, list) else sampling_params, + token_ids, + lora_request=lora_request, + # Get ith image while maintaining the batch dim. + multi_modal_data=MultiModalData(type=multi_modal_data.type, data=multi_modal_data.data[i].unsqueeze(0)) + if multi_modal_data else None, + ) + return self._run_engine(use_tqdm) + + def _add_request( + self, + prompt: Optional[str], + sampling_params: SamplingParams, + prompt_token_ids: Optional[List[int]], + lora_request: Optional[LoRARequest] = None, + multi_modal_data: Optional[MultiModalData] = None, + ) -> None: + request_id = str(next(self.request_counter)) + self.llm_engine.add_request(request_id, + prompt, + sampling_params, + prompt_token_ids, + lora_request=lora_request, + multi_modal_data=multi_modal_data) + + def _run_engine(self, use_tqdm: bool) -> List[RequestOutput]: + # Initialize tqdm. + if use_tqdm: + num_requests = self.llm_engine.get_num_unfinished_requests() + pbar = tqdm(total=num_requests, desc="Processed prompts", dynamic_ncols=True) + # Run the engine. + outputs: List[RequestOutput] = [] + while self.llm_engine.has_unfinished_requests(): + step_outputs = self.llm_engine.step() + for output in step_outputs: + if output.finished: + outputs.append(output) + if use_tqdm: + pbar.update(1) + if use_tqdm: + pbar.close() + # Sort the outputs by request ID. + # This is necessary because some requests may be finished earlier than + # its previous requests. + outputs = sorted(outputs, key=lambda x: int(x.request_id)) + # TODO(shengguangming): maybe we can hack the autoregressive logics without only apply post process for better performance + return self._post_process_outputs(outputs) + + # NOTE(shengguangming): add for verl + # TODO(sgm): we can optimize it by making the dataloader yield List[int] without padding. + def _pre_process_inputs(self, prompt_token_ids: torch.Tensor) -> List[int]: + # remove the left padding in the prompt token_id + pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + non_pad_index = torch.nonzero(prompt_token_ids != pad_token_id, as_tuple=False)[0][0] + token_ids = prompt_token_ids[non_pad_index:].tolist() + return token_ids + + # NOTE(shengguangming): add for verl + def _post_process_outputs(self, request_outputs: List[RequestOutput]) -> Tuple[torch.Tensor, torch.Tensor]: + output_token_ids = [] + logprobs = [] + for request_output in request_outputs: # List[RequestOutput] + outputs = request_output.outputs + for output in outputs: # List[CompletionOutput], usually len == 1 + output_token_ids.append(torch.tensor(output.token_ids)) + # TODO(shengguangming): can be optimzied by rewrite the Sampler._get_logprobs() logits + logprobs_dicts = output.logprobs + if logprobs_dicts is not None: + logprob = [] + for logprobs_dict, id in zip(logprobs_dicts, output.token_ids): + logprob.append(logprobs_dict[id].logprob) + logprobs.append(torch.tensor(logprob)) + + pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + output_token_ids = pad_sequence(output_token_ids, batch_first=True, padding_value=pad_token_id) + if len(logprobs) > 0: + logprobs = pad_sequence(logprobs, batch_first=True, padding_value=pad_token_id) + return output_token_ids, logprobs + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.llm_engine.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + def offload_model_weights(self) -> None: + self.llm_engine.offload_model_weights() diff --git a/verl/third_party/vllm/vllm_v_0_4_2/llm_engine_sp.py b/verl/third_party/vllm/vllm_v_0_4_2/llm_engine_sp.py new file mode 100644 index 000000000..c002f49a2 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/llm_engine_sp.py @@ -0,0 +1,285 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/llm_engine.py + +import torch +from typing import Dict, Optional, Union, Type + +import vllm +from vllm.config import (CacheConfig, DecodingConfig, DeviceConfig, LoRAConfig, ParallelConfig, SchedulerConfig, + SpeculativeConfig, VisionLanguageConfig) +from vllm.core.scheduler import Scheduler +from vllm.engine.output_processor.interfaces import (SequenceGroupOutputProcessor) +from vllm.engine.output_processor.stop_checker import StopChecker +from vllm.executor.executor_base import ExecutorBase +from vllm.logger import init_logger +from vllm.transformers_utils.detokenizer import Detokenizer +from vllm.engine.metrics import StatLogger +from vllm.usage.usage_lib import (UsageContext, is_usage_stats_enabled, usage_message) +from vllm.utils import Counter +from vllm.engine.llm_engine import _load_generation_config_dict +from vllm.engine.llm_engine import LLMEngine + +import torch.nn as nn +from .arg_utils import EngineArgs +from .tokenizer import TokenizerGroup +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) +_LOCAL_LOGGING_INTERVAL_SEC = 5 + + +class LLMEngine(LLMEngine): + """An LLM engine that receives requests and generates texts. + + This is the main class for the vLLM engine. It receives requests + from clients and generates texts from the LLM. It includes a tokenizer, a + language model (possibly distributed across multiple GPUs), and GPU memory + space allocated for intermediate states (aka KV cache). This class utilizes + iteration-level scheduling and efficient memory management to maximize the + serving throughput. + + The `LLM` class wraps this class for offline batched inference and the + `AsyncLLMEngine` class wraps this class for online serving. + + NOTE: The config arguments are derived from the `EngineArgs` class. For the + comprehensive list of arguments, see `EngineArgs`. + + Args: + model: the actor model initialize outside vllm (add for verl) + tokenizer: the initialized tokenizer (add for verl) + model_config: The configuration related to the LLM model. + cache_config: The configuration related to the KV cache memory + management. + parallel_config: The configuration related to distributed execution. + scheduler_config: The configuration related to the request scheduler. + distributed_init_method: The initialization method for distributed + execution. See `torch.distributed.init_process_group` for details. + placement_group: Ray placement group for distributed execution. + Required for distributed execution. + log_stats: Whether to log statistics. + """ + + def __init__( + self, + # NOTE(sgm): first two arguments are added for verl + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: nn.Module, + # NOTE(sgm): vllm original arguments + model_config: ModelConfig, + cache_config: CacheConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + vision_language_config: Optional[VisionLanguageConfig], + speculative_config: Optional[SpeculativeConfig], + decoding_config: Optional[DecodingConfig], + executor_class: Type[ExecutorBase], + log_stats: bool, + usage_context: UsageContext = UsageContext.ENGINE_CONTEXT, + ) -> None: + logger.info( + "Initializing an LLM engine (v%s) with config: " + "model=%r, speculative_config=%r, tokenizer=%r, " + "skip_tokenizer_init=%s, tokenizer_mode=%s, revision=%s, " + "tokenizer_revision=%s, trust_remote_code=%s, dtype=%s, " + "max_seq_len=%d, download_dir=%r, load_format=%s, " + "tensor_parallel_size=%d, disable_custom_all_reduce=%s, " + "quantization=%s, enforce_eager=%s, kv_cache_dtype=%s, " + "quantization_param_path=%s, device_config=%s, " + "decoding_config=%r, seed=%d, served_model_name=%s)", + vllm.__version__, + model_config.model, + speculative_config, + model_config.tokenizer, + model_config.skip_tokenizer_init, + # model_config.tokenizer_mode, + model_config.revision, + model_config.tokenizer_revision, + # model_config.trust_remote_code, + model_config.dtype, + model_config.max_model_len, + load_config.download_dir, + load_config.load_format, + parallel_config.tensor_parallel_size, + parallel_config.disable_custom_all_reduce, + model_config.quantization, + model_config.enforce_eager, + cache_config.cache_dtype, + model_config.quantization_param_path, + device_config.device, + decoding_config, + model_config.seed, + # model_config.served_model_name, + ) + # TODO(woosuk): Print more configs in debug mode. + + self.model_config = model_config # TODO: currently is hfconfig + self.cache_config = cache_config + self.lora_config = lora_config + self.vision_language_config = vision_language_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.speculative_config = speculative_config + self.load_config = load_config + self.decoding_config = decoding_config or DecodingConfig() + self.log_stats = log_stats + + # self.model = model # should not store the model, it should be deleted + # TODO(shengguangming): maybe we can choose init here or from arguments + if not self.model_config.skip_tokenizer_init: + # TODO: check tokenizer class + self._init_tokenizer(tokenizer) + self.detokenizer = Detokenizer(self.tokenizer) + else: + self.detokenizer = None + self.tokenizer = None + + self.seq_counter = Counter() + # TODO: don't know what's the usage + self.generation_config_fields = _load_generation_config_dict(model_config) + + self.model_executor = executor_class( + model=model, # add for spmd_gpu_executor + model_config=model_config, + cache_config=cache_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + device_config=device_config, + lora_config=lora_config, + vision_language_config=vision_language_config, + speculative_config=speculative_config, + load_config=load_config, + ) + + # Profile the memory usage and initialize the cache. + self._initialize_kv_caches() + + # If usage stat is enabled, collect relevant info. + if is_usage_stats_enabled(): + from vllm.model_executor.model_loader import (get_architecture_class_name) + usage_message.report_usage( + get_architecture_class_name(model_config), + usage_context, + extra_kvs={ + # Common configuration + "dtype": str(model_config.dtype), + "tensor_parallel_size": parallel_config.tensor_parallel_size, + "block_size": cache_config.block_size, + "gpu_memory_utilization": cache_config.gpu_memory_utilization, + + # Quantization + "quantization": model_config.quantization, + "kv_cache_dtype": cache_config.cache_dtype, + + # Feature flags + "enable_lora": bool(lora_config), + "enable_prefix_caching": cache_config.enable_prefix_caching, + "enforce_eager": model_config.enforce_eager, + "disable_custom_all_reduce": parallel_config.disable_custom_all_reduce, + }) + + if self.tokenizer: + # Ping the tokenizer to ensure liveness if it runs in a + # different process. + self.tokenizer.ping() + + # Create the scheduler. + # NOTE: the cache_config here have been updated with the numbers of + # GPU and CPU blocks, which are profiled in the distributed executor. + # NOTE(shengguangming): each process will have independent scheduler + self.scheduler = Scheduler(scheduler_config, cache_config, lora_config) + + # Metric Logging. + if self.log_stats: + self.stat_logger = StatLogger(local_interval=_LOCAL_LOGGING_INTERVAL_SEC, + labels=dict(model_name=model_config.served_model_name), + max_model_len=self.model_config.max_model_len) + self.stat_logger.info("cache_config", self.cache_config) + + # Create sequence output processor, e.g. for beam search or + # speculative decoding. + self.output_processor = (SequenceGroupOutputProcessor.create_output_processor( + self.scheduler_config, + self.detokenizer, + self.scheduler, + self.seq_counter, + self.get_tokenizer_for_seq, + stop_checker=StopChecker( + self.scheduler_config.max_model_len, + self.get_tokenizer_for_seq, + ), + )) + + # TODO(sgm): add for verl but we may not tokenizer in Rollout + def _init_tokenizer(self, tokenizer, **tokenizer_init_kwargs): + init_kwargs = dict(enable_lora=bool(self.lora_config), + max_num_seqs=self.scheduler_config.max_num_seqs, + max_input_length=None) + init_kwargs.update(tokenizer_init_kwargs) + self.tokenizer: TokenizerGroup = TokenizerGroup(tokenizer, **init_kwargs) + + def init_cache_engine(self): + # TODO: check whether we should rebuild the CUDAGraph every iter when offload/load KVCache + # Re-capture CUDAGraph would be time-consuming + self.model_executor.init_cache_engine() + + def free_cache_engine(self): + self.model_executor.free_cache_engine() + + # NOTE(sgm): currently, we only support GPU executor + # The GPUExecutor remove the Ray dependency + @classmethod + def from_engine_args( + cls, + model, + tokenizer, + engine_args: EngineArgs, + usage_context: UsageContext = UsageContext.ENGINE_CONTEXT, + ) -> "LLMEngine": + """Creates an LLM engine from the engine arguments.""" + # Create the engine configs. + engine_config = engine_args.create_engine_config() + + # Initialize the cluster and specify the executor class. + assert engine_config.device_config.device_type == "cuda", \ + "Currently, the vllm in verl only support running on GPU" + + if engine_config.parallel_config.world_size == 1: + # TODO: may also need to init process group + from vllm.executor.gpu_executor import GPUExecutor + executor_class = GPUExecutor + else: + from .spmd_gpu_executor import SPMDGPUExecutor + executor_class = SPMDGPUExecutor + + # Create the LLM engine. + engine = cls( + model, + tokenizer, + **engine_config.to_dict(), + executor_class=executor_class, + log_stats=not engine_args.disable_log_stats, + usage_context=usage_context, + ) + return engine + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.model_executor.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + def offload_model_weights(self) -> None: + self.model_executor.offload_model_weights() diff --git a/verl/third_party/vllm/vllm_v_0_4_2/megatron_weight_loaders.py b/verl/third_party/vllm/vllm_v_0_4_2/megatron_weight_loaders.py new file mode 100644 index 000000000..1a7c2e2cf --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/megatron_weight_loaders.py @@ -0,0 +1,348 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict +import torch +import torch.nn as nn + +from vllm.model_executor.layers.linear import * +from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding, ParallelLMHead +from vllm.model_executor.layers.activation import ScaledActivation +from vllm.model_executor.models import ModelRegistry + + +# NOTE(shengguangming): replace the origin weight loader function in the class +def parallel_weight_loader(self, param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Parallel Linear weight loader.""" + assert param.size() == loaded_weight.size( + ), 'the parameter size is not align with the loaded weight size, param size: {}, loaded_weight size: {}'.format( + param.size(), loaded_weight.size()) + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def default_weight_loader(param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Default weight loader.""" + assert param.size() == loaded_weight.size() + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def gpt2_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "lm_head.weight" in name: + # GPT-2 ties the weights of the embedding layer and the final + # linear layer. + continue + if ".attn.bias" in name or ".attn.masked_bias" in name: + # Skip attention mask. + # NOTE: "c_attn.bias" should not be skipped. + continue + if not name.startswith("transformer."): + name = "transformer." + name + param = params_dict[name] + # The HF's GPT-2 implementation uses Conv1D instead of Linear. + # Because of this, we need to transpose the weights. + # Note(zhuohan): the logic below might break quantized models. + for conv1d_weight_name in ["c_attn", "c_proj", "c_fc"]: + if conv1d_weight_name not in name: + continue + if not name.endswith(".weight"): + continue + # TODO: check megatron + loaded_weight = loaded_weight.t() + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_te_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv.layer_norm_weight", "input_layernorm.weight"), + ("self_attention.linear_qkv.layer_norm_bias", "input_layernorm.bias"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1.layer_norm_weight', 'post_attention_layernorm.weight'), + ('mlp.linear_fc1.layer_norm_bias', 'post_attention_layernorm.bias'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ( + 'input_layernorm', + 'input_layernorm', + ), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def _replace_name(megatron_name, name_mapping): + for m_name, v_name in name_mapping: + if m_name not in megatron_name: + continue + if 'layers' in megatron_name: # deal with decoder layers + megatron_name = megatron_name.replace('decoder', 'model') + megatron_name_list = megatron_name.split('.') + if 'layer_norm_weight' in megatron_name_list or 'layer_norm_bias' in megatron_name_list: + param_name_list = megatron_name_list[:3] + param_name_list.append(v_name) + param_name = '.'.join(param_name_list) + else: + param_name_list = megatron_name_list[:3] + weight_or_bias = megatron_name_list[-1] + param_name_list.append(v_name) + param_name_list.append(weight_or_bias) + param_name = '.'.join(param_name_list) + return param_name + else: + param_name = megatron_name.replace(m_name, v_name) + return param_name + + +def llama_megatron_core_te_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv.layer_norm_weight", "input_layernorm.weight"), + ("self_attention.linear_qkv.layer_norm_bias", "input_layernorm.bias"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1.layer_norm_weight', 'post_attention_layernorm.weight'), + ('mlp.linear_fc1.layer_norm_bias', 'post_attention_layernorm.bias'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ( + 'input_layernorm', + 'input_layernorm', + ), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def _replace_name(megatron_name, name_mapping): + for m_name, v_name in name_mapping: + if m_name not in megatron_name: + continue + if 'layers' in megatron_name: # deal with decoder layers + megatron_name = megatron_name.replace('decoder', 'model') + megatron_name_list = megatron_name.split('.') + if 'layer_norm_weight' in megatron_name_list or 'layer_norm_bias' in megatron_name_list: + param_name_list = megatron_name_list[:3] + param_name_list.append(v_name) + param_name = '.'.join(param_name_list) + else: + param_name_list = megatron_name_list[:3] + weight_or_bias = megatron_name_list[-1] + param_name_list.append(v_name) + param_name_list.append(weight_or_bias) + param_name = '.'.join(param_name_list) + return param_name + else: + param_name = megatron_name.replace(m_name, v_name) + return param_name + + +def mistral_megatron_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # TODO: need to implement a general way to deal with prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +__LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__ = { + ColumnParallelLinear: parallel_weight_loader, + MergedColumnParallelLinear: parallel_weight_loader, + QKVParallelLinear: parallel_weight_loader, + RowParallelLinear: parallel_weight_loader, + VocabParallelEmbedding: parallel_weight_loader, + ParallelLMHead: parallel_weight_loader + # "ScaledActivation.weight_loader": ScaledActivation, # TODO(shengguangming): latest commit in vllm fix awq for this function and add load_weights + # "default_weight_loader": default_weight_loader +} + +# for layer_class, weight_loader in __LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__.items(): +# # setattr(layer_class, 'megatron_weight_loader', weight_loader) +# layer_class.weight_loader = weight_loader + +__MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__ = { + 'GPT2LMHeadModel': gpt2_weight_loader, + 'LlamaForCausalLM': llama_megatron_core_te_weight_loader, # use te backend for open-source megatron + 'LLaMAForCausalLM': llama_megatron_core_te_weight_loader, + 'MistralForCausalLM': mistral_megatron_weight_loader, +} + + +# the actor model is .state_dict() +# Load megatron weights +def load_megatron_weights(actor_weights: Dict, vllm_model: nn.Module): + weight_loader = _get_model_weight_loader(vllm_model.__class__.__name__) + weight_loader(actor_weights, vllm_model) + # NOTE(sgm) to reduce peak memory usage, we offload vllm model to cpu + # after init, and we need this after sync model weights for in first iter. + vllm_model = vllm_model.cuda() + + +def _get_model_weight_loader(arch: str): + if arch in __MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__: + return __MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {ModelRegistry.get_supported_archs()}") + + +def update_megatron_weight_loader(): + for layer_class, weight_loader in __LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__.items(): + layer_class.weight_loader = weight_loader + VocabParallelEmbedding.__init__ = vocab_init + + +# FIXME(shengguangming): the vLLM vocab will pad to 64, which may incur out of bounds +# so we need to rewrite the init function of vocab +DEFAULT_VOCAB_PADDING_SIZE = 64 + + +def vocab_init(self, + num_embeddings: int, + embedding_dim: int, + params_dtype: Optional[torch.dtype] = None, + org_num_embeddings: Optional[int] = None, + padding_size: int = DEFAULT_VOCAB_PADDING_SIZE): + super(VocabParallelEmbedding, self).__init__() + + # Keep the input dimensions. + # TODO (pad to be divided by 4) + self.num_embeddings = num_embeddings + self.org_vocab_size = org_num_embeddings or num_embeddings + + # self.num_embeddings_padded = pad_vocab_size(num_embeddings, + # padding_size) + self.embedding_dim = embedding_dim + if params_dtype is None: + params_dtype = torch.get_default_dtype() + self.tp_size = get_tensor_model_parallel_world_size() + # Divide the weight matrix along the vocaburaly dimension. + + # TODO: remove dependencies from megatron + from megatron.core.tensor_parallel.utils import VocabUtility + self.vocab_start_index, self.vocab_end_index = (VocabUtility.vocab_range_from_global_vocab_size( + self.num_embeddings, get_tensor_model_parallel_rank(), self.tp_size)) + self.num_embeddings_per_partition = (self.vocab_end_index - self.vocab_start_index) + self.weight = Parameter( + torch.empty( + self.num_embeddings_per_partition, + self.embedding_dim, + # device=torch.cuda.current_device(), + dtype=params_dtype)) + set_weight_attrs(self.weight, {"parallel_dim": 0, "weight_loader": self.weight_loader}) diff --git a/verl/third_party/vllm/vllm_v_0_4_2/model_loader.py b/verl/third_party/vllm/vllm_v_0_4_2/model_loader.py new file mode 100644 index 000000000..5f4013451 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/model_loader.py @@ -0,0 +1,265 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/model_loader +"""Utilities for selecting and loading models.""" +from typing import Dict, Union, Optional, Iterable, Tuple + +import torch +import torch.nn as nn +from transformers import PreTrainedModel + +from vllm.config import (DeviceConfig, LoRAConfig, ParallelConfig, SchedulerConfig, VisionLanguageConfig) +from vllm.model_executor.model_loader import BaseModelLoader +from vllm.model_executor.model_loader.loader import _initialize_model +from vllm.model_executor.model_loader.utils import set_default_torch_dtype +from vllm.distributed.communication_op import tensor_model_parallel_all_gather + +from .config import ModelConfig, LoadFormat, LoadConfig +from .megatron_weight_loaders import load_megatron_weights, update_megatron_weight_loader +from .dtensor_weight_loaders import load_dtensor_weights, update_dtensor_weight_loader +from .hf_weight_loader import update_hf_weight_loader + + +def get_model(actor_model: Union[PreTrainedModel, Dict], model_config: ModelConfig, load_config: LoadConfig, + device_config: DeviceConfig, parallel_config: ParallelConfig, scheduler_config: SchedulerConfig, + lora_config: Optional[LoRAConfig], vision_language_config: Optional[VisionLanguageConfig]) -> nn.Module: + loader = get_model_loader(load_config) + if load_config.load_format.startswith('dummy'): + return loader.load_model(model_config=model_config, + device_config=device_config, + lora_config=lora_config, + vision_language_config=vision_language_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config) + else: + return loader.load_model(actor_model=actor_model, + model_config=model_config, + device_config=device_config, + lora_config=lora_config, + vision_language_config=vision_language_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config) + + +def get_model_loader(load_config: LoadConfig) -> BaseModelLoader: + """Get a model loader based on the load format.""" + + if isinstance(load_config.load_format, type): + return load_config.load_format(load_config) + + if load_config.load_format == LoadFormat.AUTO: + update_megatron_weight_loader() + return MegatronLoader(load_config) + + # NOTE(sgm): change the weight_loader function in runtime + if load_config.load_format == LoadFormat.MEGATRON: + update_megatron_weight_loader() + return MegatronLoader(load_config) + + if load_config.load_format == LoadFormat.HF: + update_hf_weight_loader() + return HFLoader(load_config) + + if load_config.load_format == LoadFormat.DTENSOR: + update_dtensor_weight_loader() + return DTensorLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_HF: + update_hf_weight_loader() + return DummyModelLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_MEGATRON: + update_megatron_weight_loader() + return DummyModelLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_DTENSOR: + update_dtensor_weight_loader() + return DummyModelLoader(load_config) + + raise ValueError('load format not supported in verl: {}, only support {} and {}'.format( + load_config.load_format, LoadFormat.MEGATRON, LoadFormat.HF)) + + +class DummyModelLoader(BaseModelLoader): + """Model loader that will set model weights to random values.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def load_model(self, *, model_config: ModelConfig, device_config: DeviceConfig, lora_config: Optional[LoRAConfig], + vision_language_config: Optional[VisionLanguageConfig], parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, vision_language_config) + # NOTE(woosuk): For accurate performance evaluation, we assign + # random values to the weights. + # initialize_dummy_weights(model) + return model.eval() + + +class MegatronLoader(BaseModelLoader): + """Model loader that can load the model weights from partitioned megatron model.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(actor_model: Union[PreTrainedModel, Dict]): + # NOTE(shengguangming) Load the weights from the actor model + pass + # if isinstance(actor_model, nn.Module): + # load_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), vllm_model=model) + # else: + # load_weights(actor_weights=actor_model, vllm_model=model) + # return actor_model + + def load_model(self, actor_model: Union[PreTrainedModel, + Dict], model_config: ModelConfig, device_config: DeviceConfig, + lora_config: Optional[LoRAConfig], vision_language_config: Optional[VisionLanguageConfig], + parallel_config: ParallelConfig, scheduler_config: SchedulerConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, vision_language_config) + + # TODO(sgm): This is a hack, we need to register the load_weight() func for each model in vllm + if isinstance(actor_model, nn.Module): + load_megatron_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), + vllm_model=model) + else: + load_megatron_weights(actor_weights=actor_model, vllm_model=model) + + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +class HFLoader(BaseModelLoader): + """Model loader that can load the model weights from model's full params.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(self, actor_model: Union[PreTrainedModel, Dict]): + if isinstance(actor_model, Dict): + return actor_model.items() + elif isinstance(actor_model, nn.Module): + return dict(actor_model.named_parameters()).items() + else: + raise ValueError(f'actor model should be Dict or nn.Module, but get {type(actor_model)}') + + def load_model(self, actor_model: Union[PreTrainedModel, + Dict], model_config: ModelConfig, device_config: DeviceConfig, + lora_config: Optional[LoRAConfig], vision_language_config: Optional[VisionLanguageConfig], + parallel_config: ParallelConfig, scheduler_config: SchedulerConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + # with torch.device(device_config.device): + # NOTE(sgm): init the model in cpu + model = _initialize_model(model_config, self.load_config, lora_config, vision_language_config) + model.load_weights(self._get_weights_iterator(actor_model)) + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +class DTensorLoader(BaseModelLoader): + """Model loader that can load the model weights from partitioned megatron model.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(actor_model: Union[PreTrainedModel, Dict]): + # NOTE(shengguangming) Load the weights from the actor model + pass + # if isinstance(actor_model, nn.Module): + # load_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), vllm_model=model) + # else: + # load_weights(actor_weights=actor_model, vllm_model=model) + # return actor_model + + def load_model(self, actor_model: Union[PreTrainedModel, + Dict], model_config: ModelConfig, device_config: DeviceConfig, + lora_config: Optional[LoRAConfig], vision_language_config: Optional[VisionLanguageConfig], + parallel_config: ParallelConfig, scheduler_config: SchedulerConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, vision_language_config) + + # TODO(sgm): This is a hack, we need to register the load_weight() func for each model in vllm + if isinstance(actor_model, nn.Module): + load_dtensor_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), + vllm_model=model) + else: + load_dtensor_weights(actor_weights=actor_model, vllm_model=model) + + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +# FIXME(sgm): hack the _get_logits function in vllm v0.4.2 +# as they use ray, the _get_logits result will only need to return to the driver node, +# therefore gather is enough. However, we use SPMD instead of a central scheduler, +# all_gather is required (aligned with v0.2.6) +def _get_logits(self, hidden_states: torch.Tensor, embedding: torch.Tensor, + embedding_bias: Optional[torch.Tensor]) -> torch.Tensor: + # Get the logits for the next tokens. + logits = torch.matmul(hidden_states, embedding.t()) + if embedding_bias is not None: + logits += embedding_bias + logits = tensor_model_parallel_all_gather(logits) + # Remove paddings in vocab (if any). + if logits is not None: + logits = logits[:, :self.org_vocab_size] + return logits + + +from vllm.model_executor.layers.logits_processor import LogitsProcessor + +LogitsProcessor._get_logits = _get_logits diff --git a/verl/third_party/vllm/vllm_v_0_4_2/model_runner.py b/verl/third_party/vllm/vllm_v_0_4_2/model_runner.py new file mode 100644 index 000000000..1604b0363 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/model_runner.py @@ -0,0 +1,281 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/model_runner.py + +import torch +import torch.nn as nn +from enum import IntEnum +from typing import Dict, List, Optional, Set, Tuple, Union + +from vllm.attention import (AttentionMetadata, get_attn_backend) +from vllm.config import (DeviceConfig, LoRAConfig, ParallelConfig, SchedulerConfig, VisionLanguageConfig) +from vllm.logger import init_logger +from vllm.lora.layers import LoRAMapping +from vllm.lora.request import LoRARequest +from vllm.lora.worker_manager import LRUCacheWorkerLoRAManager +from vllm.model_executor import SamplingMetadata +from vllm.sequence import (MultiModalData, SamplerOutput, SequenceData, SequenceGroupMetadata) +from vllm.utils import (CudaMemoryProfiler, is_hip, is_pin_memory_available) +from vllm.worker.model_runner import ModelRunner, CUDAGraphRunner + +from .model_loader import get_model +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) + + +# How batches are constructed. +class BatchType(IntEnum): + # Every batch is prefill. + PREFILL = 0 + # Every batch is decode. + DECODE = 1 + # Batch is a mixture of prefill and decode. + MIXED = 2 + + +class ModelRunner(ModelRunner): + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + kv_cache_dtype: Optional[str] = "auto", + vision_language_config: Optional[VisionLanguageConfig] = None, + ): + self.model_config = model_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.lora_config = lora_config + self.load_config = load_config + + # model_config can be None in tests/samplers/test_sampler.py. + # FIXME(woosuk): This is a hack to make the tests work. Refactor this. + self.sliding_window = (model_config.get_sliding_window() if model_config is not None else None) + self.device_config = (device_config if device_config is not None else DeviceConfig()) + self.device = self.device_config.device + + # NOTE(sgm): add for verl + self.model = model # this will be replaced by get_model() + + # Set after load_model. + self.lora_manager: LRUCacheWorkerLoRAManager = None + + self.graph_runners: Dict[int, CUDAGraphRunner] = {} + self.graph_memory_pool: Optional[Tuple[int, int]] = None # Set during graph capture. + + self.max_seq_len_to_capture = (self.model_config.max_seq_len_to_capture if self.model_config is not None else 0) + + self.pin_memory = is_pin_memory_available() + self.kv_cache_dtype = kv_cache_dtype + self.vision_language_config = vision_language_config + + self.attn_backend = get_attn_backend(self.model_config.dtype if model_config is not None else None) + + # Lazy initialization + self.block_size: int # Set after initial profiling. + # When using CUDA graph, the input block tables must be padded to + # max_seq_len_to_capture. However, creating the block table in + # Python can be expensive. To optimize this, we cache the block table + # in numpy and only copy the actual input content at every iteration. + # The shape of the cached block table will be + # (max batch size to capture, max context len to capture / block size). + self.graph_block_tables: torch.Tensor # Set after initial profiling. + + # Set if the backend is flashinfer. + self.flashinfer_workspace_buffer: torch.Tensor + + # NOTE(sgm): initialize model using the actor model + def load_model(self) -> None: + with CudaMemoryProfiler() as m: + self.model = get_model(actor_model=self.model, + model_config=self.model_config, + device_config=self.device_config, + lora_config=self.lora_config, + load_config=self.load_config, + parallel_config=self.parallel_config, + scheduler_config=self.scheduler_config, + vision_language_config=self.vision_language_config) + self.model_memory_usage = m.consumed_memory + logger.info("Loading model weights took %.4f GB", self.model_memory_usage / float(2**30)) + + if self.lora_config: + assert hasattr(self.model, "supported_lora_modules") and self.model.supported_lora_modules, ( + "Model does not support LoRA") + assert hasattr(self.model, "embedding_modules"), "Model does not have embedding_modules" + assert hasattr(self.model, "embedding_padding_modules"), "Model does not have embedding_padding_modules" + self.lora_manager = LRUCacheWorkerLoRAManager(self.scheduler_config.max_num_seqs, + self.scheduler_config.max_num_batched_tokens, self.vocab_size, + self.lora_config, self.device, self.model.embedding_modules, + self.model.embedding_padding_modules) + self.model = self.lora_manager.create_lora_manager(self.model) + + if self.kv_cache_dtype == "fp8" and is_hip(): + # Currently scaled KV cache is only enabled on ROCm + if self.model_config.quantization_param_path is not None: + if callable(getattr(self.model, "load_kv_cache_scales", None)): + self.model.load_kv_cache_scales(self.model_config.quantization_param_path) + else: + raise RuntimeError( + "Using FP8 KV cache and scaling factors provided but " + "model %s does not support loading scaling factors.", self.model.__class__) + else: + logger.warning("Using FP8 KV cache but no scaling factors " + "provided. Defaulting to scaling factors of 1.0. " + "This may lead to less accurate results!") + elif self.model_config.quantization_param_path is not None: + logger.warning("KV cache scaling factors provided, " + "but the KV cache data type is not FP8. " + "KV cache scaling factors will not be used.") + + def prepare_input_tensors( + self, + seq_group_metadata_list: List[SequenceGroupMetadata], + ) -> Tuple[torch.Tensor, torch.Tensor, AttentionMetadata, SamplingMetadata, Set[LoRARequest], LoRAMapping, + torch.Tensor]: + # NOTE(sgm): all workers prepare the input in the same way + prefill_reqs = [] + decode_reqs = [] + for seq_group_meta in seq_group_metadata_list: + if seq_group_meta.is_prompt: + prefill_reqs.append(seq_group_meta) + else: + decode_reqs.append(seq_group_meta) + + # Prepare input tensors. + ( + input_tokens, + input_positions, + prefill_attn_metadata, + seq_lens, + query_lens, + lora_index_mapping, + lora_prompt_mapping, + lora_requests, + multi_modal_input, + slot_mapping, + ) = self._prepare_prompt(prefill_reqs) + ( + decode_input_tokens, + decode_input_positions, + decode_attn_metadata, + decode_lora_index_mapping, + decode_lora_prompt_mapping, + decode_lora_requests, + decode_slot_mapping, + ) = self._prepare_decode(decode_reqs) + sampling_metadata = SamplingMetadata.prepare(seq_group_metadata_list, seq_lens, query_lens, self.device, + self.pin_memory) + + if not self.scheduler_config.chunked_prefill_enabled: + assert (len(prefill_reqs) and len(decode_reqs)) == 0 + + num_prefills = len(seq_lens) + num_prefill_tokens = len(input_tokens) + num_decode_tokens = len(decode_input_tokens) + + # Coalesce tensors. Note that attn_metadata is currently not + # coalesced for simplicity. + input_tokens.extend(decode_input_tokens) + input_positions.extend(decode_input_positions) + slot_mapping.extend(decode_slot_mapping) + lora_index_mapping.extend(decode_lora_index_mapping) + lora_prompt_mapping.extend(decode_lora_prompt_mapping) + lora_requests.update(decode_lora_requests) + + input_tokens = torch.tensor(input_tokens, dtype=torch.long, device=self.device) + input_positions = torch.tensor(input_positions, dtype=torch.long, device=self.device) + slot_mapping = torch.tensor(slot_mapping, dtype=torch.long, device=self.device) + + if self.lora_config: + lora_mapping = LoRAMapping( + lora_index_mapping, + lora_prompt_mapping, + ) + else: + lora_mapping = None + + # Broadcast the metadata. + # If batch contains both prefill and decode, it sends 2 broadcasts. + # If it only contains 1 type, it triggers a single broadcast. + if (prefill_attn_metadata is not None and decode_attn_metadata is not None): + batch_type = BatchType.MIXED + elif prefill_attn_metadata is not None: + batch_type = BatchType.PREFILL + else: + batch_type = BatchType.DECODE + + attn_metadata = AttentionMetadata( + num_prefills=num_prefills, + slot_mapping=slot_mapping, + num_prefill_tokens=num_prefill_tokens, + num_decode_tokens=num_decode_tokens, + prefill_metadata=prefill_attn_metadata, + decode_metadata=decode_attn_metadata, + kv_cache_dtype=self.kv_cache_dtype, + ) + + return (input_tokens, input_positions, attn_metadata, sampling_metadata, lora_requests, lora_mapping, + multi_modal_input) + + @torch.inference_mode() + def execute_model( + self, + seq_group_metadata_list: List[SequenceGroupMetadata], + kv_caches: List[torch.Tensor], + ) -> Optional[SamplerOutput]: + (input_tokens, input_positions, attn_metadata, sampling_metadata, lora_requests, lora_mapping, + multi_modal_input) = self.prepare_input_tensors(seq_group_metadata_list) + + if self.lora_config: + self.set_active_loras(lora_requests, lora_mapping) + + # Currently cuda graph is only supported by the decode phase. + prefill_meta = attn_metadata.prefill_metadata + decode_meta = attn_metadata.decode_metadata + if prefill_meta is None and decode_meta.use_cuda_graph: + graph_batch_size = input_tokens.shape[0] + model_executable = self.graph_runners[graph_batch_size] + else: + model_executable = self.model + execute_model_kwargs = { + "input_ids": input_tokens, + "positions": input_positions, + "kv_caches": kv_caches, + "attn_metadata": attn_metadata, + } + if self.vision_language_config: + execute_model_kwargs.update({"image_input": multi_modal_input}) + hidden_states = model_executable(**execute_model_kwargs) + + # Compute the logits. + logits = self.model.compute_logits(hidden_states, sampling_metadata) + + # Only perform sampling in the driver worker. + # if not self.is_driver_worker: + # return None + + # TODO(sgm): perform sampling on rank 0 + # Sample the next token. + output = self.model.sample( + logits=logits, + sampling_metadata=sampling_metadata, + ) + + return output diff --git a/verl/third_party/vllm/vllm_v_0_4_2/parallel_state.py b/verl/third_party/vllm/vllm_v_0_4_2/parallel_state.py new file mode 100644 index 000000000..be7464a2a --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/parallel_state.py @@ -0,0 +1,294 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM team. +# Adapted from +# https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/parallel_state.py +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. +"""Model and data parallel groups.""" +import os +import torch +import torch.distributed +from typing import Optional + +import vllm.distributed.parallel_state as ps + +import vllm.envs as envs +from vllm.logger import init_logger + +from torch.distributed.device_mesh import init_device_mesh + +logger = init_logger(__name__) +""" +This version is strongly tied with Megatron to implement HybridEngine and weight sharing between vllm and Megatron. +- We assume the Megatron tp+dp+pp world is already established before calling this function. + +""" + +# Device mesh for using DTensor +_DEVICE_MESH = None + +# Tensor model parallel group that the current rank belongs to. +_TP_DEVICE_GROUP = None +_TP_CPU_GROUP = None + + +# This method is for initializing the ParallelGroup when using HybridEngine +def initialize_parallel_state( + distributed_init_method: str = "env://", + backend: str = "nccl", + tensor_model_parallel_size: int = 1, + num_tp_per_train_tp: int = 1, + pipeline_model_parallel_size: int = 1, +): + # torch.distributed.all_reduce does not free the input tensor until + # the synchronization point. This causes the memory usage to grow + # as the number of all_reduce calls increases. This env var disables + # this behavior. + # Related issue: + # https://discuss.pytorch.org/t/cuda-allocation-lifetime-for-inputs-to-distributed-all-reduce/191573 + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + + # NOTE(sgm): Modify for verl, Env vars will be set by TORCHRUN. + rank = int(os.getenv("RANK", "-1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + + # Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + ps.init_distributed_environment(world_size, rank, distributed_init_method, local_rank, backend) + if torch.distributed.get_world_size() > 1: + # NOTE: build a sepearate inference group with infer tp & micro dp + initialize_model_parallel_for_vllm(tensor_model_parallel_size=tensor_model_parallel_size, + num_tensor_model_parallel_groups_per_train_tp=num_tp_per_train_tp) + else: + initialize_model_parallel(tensor_model_parallel_size, pipeline_model_parallel_size, backend) + + +def ensure_model_parallel_initialized( + tensor_model_parallel_size: int, + pipeline_model_parallel_size: int = 1, + backend: Optional[str] = None, +) -> None: + """Helper to initialize model parallel groups if they are not initialized, + or ensure tensor-parallel and pipeline-parallel sizes are equal to expected + values if the model parallel groups are initialized. + """ + # get the backend of _DEVICE_WORLD_GROUP + backend = backend or torch.distributed.get_backend() + if not model_parallel_is_initialized(): + initialize_model_parallel(tensor_model_parallel_size, pipeline_model_parallel_size, backend) + return + + assert (get_tensor_model_parallel_world_size() == tensor_model_parallel_size), ( + "tensor parallel group already initialized, but of unexpected size: " + f"{get_tensor_model_parallel_world_size()=} vs. " + f"{tensor_model_parallel_size=}") + # assert (get_pipeline_model_parallel_world_size( + # ) == pipeline_model_parallel_size), ( + # "pipeline parallel group already initialized, but of unexpected size: " + # f"{get_pipeline_model_parallel_world_size()=} vs. " + # f"{pipeline_model_parallel_size=}") + + +def model_parallel_is_initialized(): + """Check if tensor and pipeline parallel groups are initialized.""" + return (ps._TP_DEVICE_GROUP is not None) + # and _PIPELINE_MODEL_PARALLEL_GROUP is not None) + + +def initialize_model_parallel_for_vllm(tensor_model_parallel_size: int, + num_tensor_model_parallel_groups_per_train_tp: int = 1) -> None: + from torch.distributed import new_group + # Get world size and rank. Ensure some consistencies. + assert torch.distributed.is_initialized() + + assert isinstance(tensor_model_parallel_size, int) + + # assert num_tensor_model_parallel_groups_per_train_tp == 1 and not different_tp_group + # assert num_tensor_model_parallel_groups_per_train_tp > 1 and different_tp_group + + # Build the tensor model-parallel groups. + assert ps._TP_DEVICE_GROUP is None, ("tensor model parallel group is already initialized") + + global _TP_DEVICE_GROUP + global _TP_CPU_GROUP + global _DEVICE_MESH + + world_size: int = torch.distributed.get_world_size() + + rank = torch.distributed.get_rank() + + backend = torch.distributed.get_backend() + + num_tensor_model_parallel_groups = world_size // tensor_model_parallel_size + + if num_tensor_model_parallel_groups_per_train_tp == 1: + # if tensor_model_parallel_size == train_tensor_parallel_size: + # using the same tp group as Megatron/vllm + for i in range(num_tensor_model_parallel_groups): + ranks = range(i * tensor_model_parallel_size, (i + 1) * tensor_model_parallel_size) + group = torch.distributed.new_group(ranks, backend=backend) + cpu_group = torch.distributed.new_group(ranks, backend="gloo") + if rank in ranks: + _TP_DEVICE_GROUP = group + _TP_CPU_GROUP = cpu_group + ps._TP_DEVICE_GROUP = group + ps._TP_CPU_GROUP = cpu_group + + # no _MICRO_DATA_PARALLEL_GROUP + else: + # initialize a micro_dp group and a tp group + # assume training tp=4, infer tp=2, then, weight is partitioned as + # [1], [2], [3], [4] for training and [1,2], [1,2], [3,4], [3,4] for inference + + # Build the inference tp groups + # train_tp = train_tensor_parallel_size + train_tp = num_tensor_model_parallel_groups_per_train_tp * tensor_model_parallel_size + # num_tensor_model_parallel_groups_per_train_tp = train_tp // tensor_model_parallel_size + assert _TP_DEVICE_GROUP is None, ("tensor model parallel group is already initialized") + for i in range(num_tensor_model_parallel_groups // num_tensor_model_parallel_groups_per_train_tp): + start = train_tp * i + end = train_tp * (i + 1) + for j in range(num_tensor_model_parallel_groups_per_train_tp): + ranks = list(range(start, end, num_tensor_model_parallel_groups_per_train_tp)) + for i in range(len(ranks)): + ranks[i] += j + group = torch.distributed.new_group(ranks) + cpu_group = torch.distributed.new_group(ranks, backend='gloo') + if rank in ranks: + _TP_DEVICE_GROUP = group + _TP_CPU_GROUP = cpu_group + ps._TP_DEVICE_GROUP = _TP_DEVICE_GROUP + ps._TP_CPU_GROUP = cpu_group + + # Build the pipeline model-parallel groups. + # global _PIPELINE_MODEL_PARALLEL_GROUP + # global _PIPELINE_GLOBAL_RANKS + # assert ps._PIPELINE_MODEL_PARALLEL_GROUP is None, ("pipeline model parallel group is already initialized") + + # ps._PIPELINE_MODEL_PARALLEL_GROUP = mpu.get_pipeline_model_parallel_group() + # ps._PIPELINE_GLOBAL_RANKS = mpu.get_pipeline_model_parallel_ranks() + + +def initialize_model_parallel( + tensor_model_parallel_size: int = 1, + pipeline_model_parallel_size: int = 1, + backend: Optional[str] = None, +) -> None: + """ + NOTE: This method is a hack from the open-sourced version without + asertion of world_size = tp * pp + + Initialize model parallel groups. + + Arguments: + tensor_model_parallel_size: number of GPUs used for tensor model + parallelism. + pipeline_model_parallel_size: number of GPUs used for pipeline model + parallelism. + + Let's say we have a total of 8 GPUs denoted by g0 ... g7 and we + use 2 GPUs to parallelize the model tensor, and 4 GPUs to parallelize + the model pipeline. The present function will + create 4 tensor model-parallel groups and 2 pipeline model-parallel groups: + 4 tensor model-parallel groups: + [g0, g1], [g2, g3], [g4, g5], [g6, g7] + 2 pipeline model-parallel groups: + [g0, g2, g4, g6], [g1, g3, g5, g7] + Note that for efficiency, the caller should make sure adjacent ranks + are on the same DGX box. For example if we are using 2 DGX-1 boxes + with a total of 16 GPUs, rank 0 to 7 belong to the first box and + ranks 8 to 15 belong to the second box. + """ + # Get world size and rank. Ensure some consistencies. + assert torch.distributed.is_initialized() + world_size: int = torch.distributed.get_world_size() + # get the backend of _DEVICE_WORLD_GROUP + backend = backend or torch.distributed.get_backend() + + # NOTE(sgm) we don't assert world_size == tp * pp + # DP is not managed by vllm but by the veRL WorkerGroup + + num_tensor_model_parallel_groups: int = (world_size // tensor_model_parallel_size) + num_pipeline_model_parallel_groups: int = (world_size // pipeline_model_parallel_size) + rank = torch.distributed.get_rank() + + # Build device mesh for TP + if num_tensor_model_parallel_groups > 1: + device_mesh = init_device_mesh("cuda", (num_tensor_model_parallel_groups, tensor_model_parallel_size), + mesh_dim_names=("replicate", "tp_shard")) + else: + device_mesh = init_device_mesh("cuda", (tensor_model_parallel_size,), mesh_dim_names=["tp_shard"]) + shard_group = device_mesh.get_group(mesh_dim="tp_shard") + + # Build the tensor model-parallel groups. + global _TP_DEVICE_GROUP, _TP_CPU_GROUP + global _DEVICE_MESH + assert _TP_DEVICE_GROUP is None, ("tensor model parallel group is already initialized") + assert _DEVICE_MESH is None, ("device mesh in vllm is already initialized") + + _DEVICE_MESH = device_mesh + # for i in range(num_tensor_model_parallel_groups): + # ranks = range(i * tensor_model_parallel_size, (i + 1) * tensor_model_parallel_size) + # group = torch.distributed.new_group(ranks, backend=backend) + # cpu_group = torch.distributed.new_group(ranks, backend="gloo") + # assert torch.distributed.get_process_group_ranks(shard_group) == torch.distributed.get_process_group_ranks(cpu_group) + # ranks = torch.distributed.get_process_group_ranks(shard_group) + # cpu_group = torch.distributed.new_group(ranks, backend="gloo") # TODO: this will hang + # cpu_group = torch.distributed.new_group(, backend="gloo") + # if rank == 0: + # print(f'rank: {rank}') + # print(f'ranks: {ranks}') + # print(f'torch.distributed.get_process_group_ranks(shard_group): {torch.distributed.get_process_group_ranks(shard_group)}') + # if rank in ranks: + _TP_DEVICE_GROUP = shard_group + ps._TP_DEVICE_GROUP = _TP_DEVICE_GROUP + # ps._TP_CPU_GROUP = cpu_group # TODO: will hang when used with device mesh + + # TODO: init using device mesh + # Build the pipeline model-parallel groups. + assert ps._PIPELINE_MODEL_PARALLEL_GROUP is None, ("pipeline model parallel group is already initialized") + for i in range(num_pipeline_model_parallel_groups): + ranks = range(i, world_size, num_pipeline_model_parallel_groups) + group = torch.distributed.new_group(ranks, backend=backend) + if rank in ranks: + ps._PIPELINE_MODEL_PARALLEL_GROUP = group + ps._PIPELINE_GLOBAL_RANKS = ranks + + +""" +Device mesh utilities +""" + + +def get_device_mesh(): + assert _DEVICE_MESH is not None, ("device mesh is not initialized") + return _DEVICE_MESH + + +""" +Tensor model parallel utilities +""" + + +def get_tensor_model_parallel_group(): + """Get the tensor model parallel group the caller rank belongs to.""" + assert _TP_DEVICE_GROUP is not None, ("tensor model parallel group is not initialized") + return _TP_DEVICE_GROUP + + +def get_tensor_model_parallel_world_size(): + """Return world size for the tensor model parallel group.""" + return torch.distributed.get_world_size(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_rank(): + """Return my rank for the tensor model parallel group.""" + return torch.distributed.get_rank(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_src_rank(): + """Calculate the global rank corresponding to the first local rank + in the tensor model parallel group.""" + global_rank = torch.distributed.get_rank() + local_world_size = get_tensor_model_parallel_world_size() + return (global_rank // local_world_size) * local_world_size diff --git a/verl/third_party/vllm/vllm_v_0_4_2/spmd_gpu_executor.py b/verl/third_party/vllm/vllm_v_0_4_2/spmd_gpu_executor.py new file mode 100644 index 000000000..b97bb600a --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/spmd_gpu_executor.py @@ -0,0 +1,218 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/executor/gpu_executor.py +import os +import socket +from typing import Any, Dict, List, Optional, Set, Tuple + +import torch +import vllm.envs as envs +from vllm.executor.executor_base import ExecutorBase, ExecutorAsyncBase +from vllm.logger import init_logger +from vllm.lora.request import LoRARequest +from vllm.sequence import SamplerOutput, ExecuteModelRequest + +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, ParallelConfig, SchedulerConfig, SpeculativeConfig, + VisionLanguageConfig) +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) + + +class SPMDGPUExecutor(ExecutorBase): + """SPMD-based multi-GPU executor implementations.""" + + def __init__( + self, + model, # pytorch model itself or its parameter dict + model_config: ModelConfig, + cache_config: CacheConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + vision_language_config: Optional[VisionLanguageConfig], + speculative_config: Optional[SpeculativeConfig], + ) -> None: + self.model_config = model_config + self.cache_config = cache_config + self.lora_config = lora_config + self.load_config = load_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.vision_language_config = vision_language_config + self.speculative_config = speculative_config + + distributed_init_method = initialize_cluster(parallel_config) + self._init_executor(model, distributed_init_method) + + # TODO(sgm): verl not support speculative decode now + def _init_executor(self, model, distributed_init_method) -> None: + assert (not self.speculative_config), "Speculative decoding not yet supported for multi-GPU backend." + + # Create the parallel worker for each GPU. + self._init_workers_sp(model, distributed_init_method) + + def _init_workers_sp(self, model, distributed_init_method: str): + # Lazy import the Worker to avoid importing torch.cuda/xformers + # before CUDA_VISIBLE_DEVICES is set in the Worker + from .worker import Worker # pylint: disable=import-outside-toplevel + + rank = int(os.getenv("RANK")) + local_rank = int(os.getenv("LOCAL_RANK")) + print(f'local rank {local_rank}') + + self.worker = Worker( + model, + self.model_config, + self.parallel_config, + self.scheduler_config, + self.device_config, + self.cache_config, + self.load_config, + local_rank, + rank, + distributed_init_method, + lora_config=self.lora_config, + vision_language_config=self.vision_language_config, + ) + + # NOTE(shengguangming): torch.distributed.init_process_group will be called inside the init_model() + self.worker.init_device() + self.worker.load_model() + + def determine_num_available_blocks(self) -> Tuple[int, int]: + """Determine the number of available KV blocks. + + This invokes `determine_num_available_blocks` on each worker and takes + the min of the results, guaranteeing that the selected cache sizes are + compatible with all workers. + + Returns: + - tuple[num_gpu_blocks, num_cpu_blocks] + """ + # Get the maximum number of blocks that can be allocated on GPU and CPU. + num_blocks = self.worker.determine_num_available_blocks() + + # NOTE(shengguangming): Now we don't use a shared centralized controler but each process will + # have its own scheduler + num_gpu_blocks = num_blocks[0] + num_cpu_blocks = num_blocks[1] + + return num_gpu_blocks, num_cpu_blocks + + def initialize_cache(self, num_gpu_blocks: int, num_cpu_blocks: int) -> None: + """Initialize the KV cache in all workers. + """ + + # NOTE: We log here to avoid multiple logs when number of workers is + # greater than one. We could log in the engine, but not all executors + # have GPUs. + logger.info("# GPU blocks: %d, # CPU blocks: %d", num_gpu_blocks, num_cpu_blocks) + + self.cache_config.num_gpu_blocks = num_gpu_blocks + self.cache_config.num_cpu_blocks = num_cpu_blocks + + if torch.distributed.get_rank() == 0: + print( + f'before init cache memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB' + ) + self.worker.initialize_cache(num_gpu_blocks=num_gpu_blocks, num_cpu_blocks=num_cpu_blocks) + if torch.distributed.get_rank() == 0: + print( + f'after init cache memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB' + ) + + # NOTE(sgm): This will not profile & capture the model(CUDAGraph) when rebuilding KVCache + def init_cache_engine(self) -> None: + self.worker._init_cache_engine() + + def free_cache_engine(self) -> None: + self.worker.free_cache_engine() + + def execute_model(self, execute_model_req) -> List[SamplerOutput]: + all_outputs = self.worker.execute_model(execute_model_req=execute_model_req) + + # NOTE(sgm): + # Each GPU in vllm under verl has its own spmd_gpu_executor, therefore all GPUs should return the outputs + # In vllm with ray, only the driver worker returns the sampling results. + return all_outputs + + def add_lora(self, lora_request: LoRARequest) -> bool: + assert lora_request.lora_int_id > 0, "lora_id must be greater than 0." + return self.worker.add_lora(lora_request=lora_request) + + def remove_lora(self, lora_id: int) -> bool: + assert lora_id > 0, "lora_id must be greater than 0." + return self.worker.remove_lora(lora_id=lora_id) + + def list_loras(self) -> Set[int]: + return self.worker.list_loras() + + def check_health(self) -> None: + # SPMDExecutor will always be healthy as long as + # it's running. + return + + # NOTE(sgm): add for verl + def offload_model_weights(self) -> None: + self.worker.offload_model_weights() + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.worker.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + +def initialize_cluster( + parallel_config: ParallelConfig, + engine_use_ray: bool = False, + ray_address: Optional[str] = None, +) -> Tuple[str, Optional[None]]: + """Initialize the distributed cluster probably with Ray. + + Args: + parallel_config: The configurations for parallel execution. + + Returns: + The `distributed_init_method` is the address for initializing the + distributed backend. + """ + + # Initialize cluster locally. + port = get_open_port() + # We need to setup the distributed init method to make sure + # the distributed megatron code (e.g., get world size) works correctly. + # distributed_init_method = f"tcp://localhost:{port}" + distributed_init_method = 'env://' + return distributed_init_method + + +def get_open_port(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("", 0)) + return s.getsockname()[1] + + +# TODO(sgm): not implemented async executor yet +class SPMDGPUExecutorAsync(SPMDGPUExecutor, ExecutorAsyncBase): + + async def execute_model_async(self, execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]: + """Executes one model step on the given sequences.""" + raise NotImplementedError + + async def check_health_async(self) -> None: + """Checks if the executor is healthy. If not, it should raise an + exception.""" + self.check_health() diff --git a/verl/third_party/vllm/vllm_v_0_4_2/tokenizer.py b/verl/third_party/vllm/vllm_v_0_4_2/tokenizer.py new file mode 100644 index 000000000..aa625a033 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/tokenizer.py @@ -0,0 +1,77 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/transformers_utils/tokenizer_group/tokenizer_group.py + +from typing import List, Optional, Tuple, Union + +from transformers import (AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast) + +from vllm.lora.request import LoRARequest +from vllm.utils import make_async, LRUCache +from vllm.transformers_utils.tokenizers import * + + +class TokenizerGroup: + """A group of tokenizers that can be used for LoRA adapters.""" + + def __init__(self, tokenizer: PreTrainedTokenizer, enable_lora: bool, max_num_seqs: int, + max_input_length: Optional[int]): + self.enable_lora = enable_lora + self.max_input_length = max_input_length + self.tokenizer = tokenizer + self.lora_tokenizers = LRUCache[PreTrainedTokenizer](capacity=max_num_seqs) if enable_lora else None + + def ping(self) -> bool: + """Check if the tokenizer group is alive.""" + return True + + def get_max_input_len(self, lora_request: Optional[LoRARequest] = None) -> Optional[int]: + """Get the maximum input length for the LoRA request.""" + return self.max_input_length + + def encode(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = self.get_lora_tokenizer(lora_request) + return tokenizer.encode(prompt) + + async def encode_async(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = await self.get_lora_tokenizer_async(lora_request) + return tokenizer.encode(prompt) + + def get_lora_tokenizer(self, lora_request: Optional[LoRARequest]) -> "PreTrainedTokenizer": + if not lora_request or not self.enable_lora: + return self.tokenizer + if lora_request.lora_int_id not in self.lora_tokenizers: + # TODO(sgm): the lora tokenizer is also passed, but may be different + tokenizer = self.tokenizer + # tokenizer = (get_lora_tokenizer( + # lora_request, **self.tokenizer_config) or self.tokenizer) + self.lora_tokenizers.put(lora_request.lora_int_id, tokenizer) + return tokenizer + else: + return self.lora_tokenizers.get(lora_request.lora_int_id) + + # FIXME(sgm): for simplicity, we assign the special token here + @property + def pad_token_id(self): + return self.tokenizer.pad_token_id + + @property + def eos_token_id(self): + return self.tokenizer.eos_token_id diff --git a/verl/third_party/vllm/vllm_v_0_4_2/worker.py b/verl/third_party/vllm/vllm_v_0_4_2/worker.py new file mode 100644 index 000000000..1fab3e41f --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_4_2/worker.py @@ -0,0 +1,292 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/worker.py +"""A GPU worker class.""" +import os +import gc +from typing import Dict, List, Tuple, Optional, Union + +import torch +import torch.distributed +import torch.nn as nn + +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, ParallelConfig, SchedulerConfig, VisionLanguageConfig) +from vllm.model_executor import set_random_seed +from vllm.sequence import SamplerOutput, ExecuteModelRequest +from vllm.worker.cache_engine import CacheEngine +from vllm.distributed.device_communicators import pynccl_utils +from vllm.distributed.device_communicators.custom_all_reduce import (init_custom_ar) +# TODO(sgm): check why vllm has similar file in vllm.model_executor.parallel_utils.parallel_state +from vllm.distributed import get_tensor_model_parallel_cpu_group, init_distributed_environment, get_tensor_model_parallel_group +from vllm.worker.worker import Worker, _check_if_gpu_supports_dtype + +from .model_runner import ModelRunner +from .megatron_weight_loaders import load_megatron_weights +from .hf_weight_loader import load_hf_weights +from .dtensor_weight_loaders import load_dtensor_weights +from .parallel_state import (ensure_model_parallel_initialized) +from .config import ModelConfig, LoadConfig, LoadFormat + + +class Worker(Worker): + """A worker class that executes (a partition of) the model on a GPU. + + Each worker is associated with a single GPU. The worker is responsible for + maintaining the KV cache and executing the model on the GPU. In case of + distributed inference, each worker is assigned a partition of the model. + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + cache_config: CacheConfig, + load_config: LoadConfig, + local_rank: int, + rank: int, + distributed_init_method: str, + lora_config: Optional[LoRAConfig] = None, + vision_language_config: Optional[VisionLanguageConfig] = None, + is_driver_worker: bool = False, + ) -> None: + # self.model = model # will be replaced in the init_model + self.model_config = model_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.cache_config = cache_config + self.local_rank = local_rank + self.rank = rank + self.distributed_init_method = distributed_init_method + self.lora_config = lora_config + self.load_config = load_config + self.is_driver_worker = is_driver_worker + if self.is_driver_worker: + assert self.rank == 0, "The driver worker must have rank 0." + + self.vision_language_config = vision_language_config + if self.vision_language_config: + assert not self.lora_config, ("To be tested: vision language model with LoRA settings.") + + self.model_runner = ModelRunner( + model, + model_config, + parallel_config, + scheduler_config, + device_config, + load_config=load_config, + lora_config=self.lora_config, + kv_cache_dtype=self.cache_config.cache_dtype, + vision_language_config=vision_language_config, + ) + + # Uninitialized cache engine. Will be initialized by + # init_cache_engine. + self.cache_engine: CacheEngine = None + self.gpu_cache: List[torch.Tensor] = None + + # NOTE(sgm): For offloading inference engine params + self.cpu_model = None + + def init_device(self) -> None: + if self.device_config.device.type == "cuda": + # torch.distributed.all_reduce does not free the input tensor until + # the synchronization point. This causes the memory usage to grow + # as the number of all_reduce calls increases. This env var disables + # this behavior. + # Related issue: + # https://discuss.pytorch.org/t/cuda-allocation-lifetime-for-inputs-to-distributed-all-reduce/191573 + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + + # NOTE(sgm): Modify for verl, Env vars will be set by TORCHRUN. + self.rank = self.rank if self.rank is not None else int(os.getenv("RANK", "-1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + self.device = torch.device(f"cuda:{local_rank}") + if self.rank < 0: + raise ValueError("Invalid or unspecified rank.") + torch.cuda.set_device(self.device) + + # Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + self.parallel_config.world_size = world_size + + _check_if_gpu_supports_dtype(self.model_config.dtype) + torch.cuda.empty_cache() + self.init_gpu_memory = torch.cuda.mem_get_info()[0] + else: + raise RuntimeError(f"Not support device type: {self.device_config.device}") + + # Initialize the distributed environment. + init_worker_distributed_environment(self.parallel_config, self.rank, self.distributed_init_method, + self.local_rank) + # Set random seed. + set_random_seed(self.model_config.seed) + # self.model = get_model(actor_model=self.model, model_config=self.model_config) + + @torch.inference_mode() + def determine_num_available_blocks(self) -> Tuple[int, int]: + """Profiles the peak memory usage of the model to determine how many + KV blocks may be allocated without OOMs. + + The engine will first conduct a profiling of the existing memory usage. + Then, it calculate the maximum possible number of GPU and CPU blocks + that can be allocated with the remaining free memory. + + .. tip:: + You may limit the usage of GPU memory + by adjusting the `gpu_memory_utilization` parameter. + """ + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + # torch.cuda.reset_peak_memory_stats() + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. + self.model_runner.profile_run() + + # Calculate the number of blocks that can be allocated with the + # profiled peak memory. + torch.cuda.synchronize() + free_gpu_memory, total_gpu_memory = torch.cuda.mem_get_info() + peak_memory = total_gpu_memory - free_gpu_memory + + assert peak_memory > 0, ("Error in memory profiling. This happens when the GPU memory was " + "not properly cleaned up before initializing the vLLM instance.") + + cache_block_size = self.get_cache_block_size_bytes() + + # NOTE(sgm) use the remaining memory + num_gpu_blocks = int((free_gpu_memory * self.cache_config.gpu_memory_utilization) // cache_block_size) + # num_gpu_blocks = int((total_gpu_memory * self.cache_config.gpu_memory_utilization - peak_memory) // cache_block_size) + + num_cpu_blocks = int(self.cache_config.swap_space_bytes // cache_block_size) + num_gpu_blocks = max(num_gpu_blocks, 0) + num_cpu_blocks = max(num_cpu_blocks, 0) + if self.model_runner.lora_manager: + self.model_runner.remove_all_loras() + + # NOTE(sgm): Add for verl, synchronize number of blocks with all the rank + num_gpu_blocks = torch.tensor([num_gpu_blocks], device='cuda') + num_cpu_blocks = torch.tensor([num_cpu_blocks], device='cuda') + torch.distributed.all_reduce(num_gpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group()) + torch.distributed.all_reduce(num_cpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group()) + num_gpu_blocks = num_gpu_blocks.item() + num_cpu_blocks = num_cpu_blocks.item() + gc.collect() + torch.cuda.empty_cache() + return num_gpu_blocks, num_cpu_blocks + + def _init_cache_engine(self): + if self.cache_engine is None and self.gpu_cache is None: + super()._init_cache_engine() + + def free_cache_engine(self): + # ensure `enforce_eager=True` + self.cache_engine = None + self.gpu_cache = None + + @torch.inference_mode() + def execute_model(self, execute_model_req: Optional[ExecuteModelRequest] = None) -> List[SamplerOutput]: + + if execute_model_req is None: + seq_group_metadata_list = None + else: + seq_group_metadata_list = execute_model_req.seq_group_metadata_list + + # NOTE(sgm): each SPMD rank will have identical input + assert seq_group_metadata_list is not None + assert execute_model_req is not None + num_seq_groups = len(seq_group_metadata_list) + blocks_to_swap_in = execute_model_req.blocks_to_swap_in + blocks_to_swap_out = execute_model_req.blocks_to_swap_out + blocks_to_copy = execute_model_req.blocks_to_copy + + self.cache_swap(blocks_to_swap_in, blocks_to_swap_out, blocks_to_copy) + + # If there is no input, we don't need to execute the model. + if num_seq_groups == 0: + return [] + + output = self.model_runner.execute_model(seq_group_metadata_list, self.gpu_cache) + + # Worker only supports single-step execution. Wrap the output in a list + # to conform to interface. + return [output] + + # assume the input is .state_dict() + def sync_model_weights(self, actor_weights: Dict, load_format: str): + if load_format in [LoadFormat.MEGATRON, LoadFormat.AUTO]: + load_megatron_weights(actor_weights, self.model_runner.model) + elif load_format == LoadFormat.HF: + # full model state dict without no sharding + load_hf_weights(actor_weights, self.model_runner.model) + elif load_format == LoadFormat.DTENSOR: + load_dtensor_weights(actor_weights, self.model_runner.model) + + def offload_model_weights(self) -> None: + if self.cpu_model == None: + self.cpu_model = {} + for name, params in self.model_runner.model.named_parameters(): + self.cpu_model[name] = torch.empty_like(params, device='cpu') + params.data = self.cpu_model[name] + else: + for name, params in self.model_runner.model.named_parameters(): + params.data = self.cpu_model[name] + + +def init_worker_distributed_environment( + parallel_config: ParallelConfig, + rank: int, + distributed_init_method: Optional[str] = "env://", + local_rank: int = -1, +) -> None: + """Initialize the distributed environment.""" + # NOTE(sgm) use tcp://localhost:xxxx will hang in HF setting without megatron + init_distributed_environment(parallel_config.world_size, rank, distributed_init_method, local_rank) + + ensure_model_parallel_initialized(tensor_model_parallel_size=parallel_config.tensor_parallel_size, + pipeline_model_parallel_size=parallel_config.pipeline_parallel_size) + + # TODO(sgm): check whether need this + # if pynccl_utils.is_initialized(): + # pynccl_world_size = pynccl_utils.get_world_size() + # if pynccl_world_size != parallel_config.world_size: + # raise RuntimeError( + # "pynccl is already initialized but the pynccl world " + # "size does not match parallel_config.world_size " + # f"({pynccl_world_size} vs. {parallel_config.world_size}).") + # elif parallel_config.world_size > 1: + # # NOTE(woosuk): We don't initialize pynccl process group when world size + # # is 1. + # # NOTE(kaichao): By default, pynccl is initialized for tp group. + # pynccl_utils.init_process_group( + # group=get_tensor_model_parallel_cpu_group()) + + # # Initialize a custom fast all-reduce implementation. + # if not parallel_config.disable_custom_all_reduce: + # init_custom_ar() + + # A small all_reduce for warmup. + torch.distributed.all_reduce(torch.zeros(1).cuda()) + # if pynccl_utils.is_initialized(): + # pynccl_utils.all_reduce(torch.zeros(1).cuda()) diff --git a/verl/third_party/vllm/vllm_v_0_5_4/__init__.py b/verl/third_party/vllm/vllm_v_0_5_4/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/__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/third_party/vllm/vllm_v_0_5_4/arg_utils.py b/verl/third_party/vllm/vllm_v_0_5_4/arg_utils.py new file mode 100644 index 000000000..6c577277b --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/arg_utils.py @@ -0,0 +1,453 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/arg_utils.py + +import os +import argparse +import dataclasses +import json +from dataclasses import dataclass +from typing import TYPE_CHECKING, List, Optional, Tuple, Type, Union + +import torch.nn as nn + +from transformers import PretrainedConfig +from .config import ModelConfig, LoadConfig + +from vllm.config import (CacheConfig, DecodingConfig, DeviceConfig, EngineConfig, LoRAConfig, MultiModalConfig, + ObservabilityConfig, ParallelConfig, PromptAdapterConfig, SchedulerConfig, SpeculativeConfig, + TokenizerPoolConfig) +from vllm.executor.executor_base import ExecutorBase +from vllm.logger import init_logger +from vllm.utils import FlexibleArgumentParser +from vllm.model_executor.layers.quantization import QUANTIZATION_METHODS +from vllm.utils import str_to_int_tuple + +if TYPE_CHECKING: + from vllm.transformers_utils.tokenizer_group.base_tokenizer_group import (BaseTokenizerGroup) + +logger = init_logger(__name__) + + +def nullable_str(val: str): + if not val or val == "None": + return None + return val + + +@dataclass +class EngineArgs: + """Arguments for vLLM engine.""" + model_hf_config: PretrainedConfig = None # for verl + served_model_name = None # TODO(sgm): check this + # tokenizer: Optional[str] = None # TODO(sgm): check this + skip_tokenizer_init: bool = False + tokenizer_mode: str = 'auto' + trust_remote_code: bool = False + download_dir: Optional[str] = None + load_format: str = 'auto' + dtype: str = 'auto' + kv_cache_dtype: str = 'auto' + quantization_param_path: Optional[str] = None + seed: int = 0 + max_model_len: Optional[int] = None + worker_use_ray: bool = False + # Note: Specifying a custom executor backend by passing a class + # is intended for expert use only. The API may change without + # notice. + distributed_executor_backend: Optional[Union[str, Type[ExecutorBase]]] = None + pipeline_parallel_size: int = 1 + tensor_parallel_size: int = 1 + max_parallel_loading_workers: Optional[int] = None + block_size: int = 16 + enable_prefix_caching: bool = False + disable_sliding_window: bool = False + use_v2_block_manager: bool = False + swap_space: int = 4 # GiB + cpu_offload_gb: int = 0 # GiB + gpu_memory_utilization: float = 0.90 + max_num_batched_tokens: Optional[int] = None + max_num_seqs: int = 256 + max_logprobs: int = 20 # Default value for OpenAI Chat Completions API + disable_log_stats: bool = False + revision: Optional[str] = None + code_revision: Optional[str] = None + rope_scaling: Optional[dict] = None + rope_theta: Optional[float] = None + tokenizer_revision: Optional[str] = None + quantization: Optional[str] = None + enforce_eager: bool = False + max_context_len_to_capture: Optional[int] = None + max_seq_len_to_capture: int = 8192 + disable_custom_all_reduce: bool = False + tokenizer_pool_size: int = 0 + # Note: Specifying a tokenizer pool by passing a class + # is intended for expert use only. The API may change without + # notice. + tokenizer_pool_type: Union[str, Type["BaseTokenizerGroup"]] = "ray" + tokenizer_pool_extra_config: Optional[dict] = None + enable_lora: bool = False + max_loras: int = 1 + max_lora_rank: int = 16 + enable_prompt_adapter: bool = False + max_prompt_adapters: int = 1 + max_prompt_adapter_token: int = 0 + fully_sharded_loras: bool = False + lora_extra_vocab_size: int = 256 + long_lora_scaling_factors: Optional[Tuple[float]] = None + lora_dtype: str = 'auto' + max_cpu_loras: Optional[int] = None + device: str = 'auto' + ray_workers_use_nsight: bool = False + num_gpu_blocks_override: Optional[int] = None + num_lookahead_slots: int = 0 + model_loader_extra_config: Optional[dict] = None + ignore_patterns: Optional[Union[str, List[str]]] = None + preemption_mode: Optional[str] = None + + scheduler_delay_factor: float = 0.0 + enable_chunked_prefill: Optional[bool] = None + + guided_decoding_backend: str = 'outlines' + # Speculative decoding configuration. + speculative_model: Optional[str] = None + speculative_draft_tensor_parallel_size: Optional[int] = None + num_speculative_tokens: Optional[int] = None + speculative_max_model_len: Optional[int] = None + speculative_disable_by_batch_size: Optional[int] = None + ngram_prompt_lookup_max: Optional[int] = None + ngram_prompt_lookup_min: Optional[int] = None + spec_decoding_acceptance_method: str = 'rejection_sampler' + typical_acceptance_sampler_posterior_threshold: Optional[float] = None + typical_acceptance_sampler_posterior_alpha: Optional[float] = None + qlora_adapter_name_or_path: Optional[str] = None + disable_logprobs_during_spec_decoding: Optional[bool] = None + + otlp_traces_endpoint: Optional[str] = None + + @staticmethod + def add_cli_args(parser: argparse.ArgumentParser) -> argparse.ArgumentParser: + """Shared CLI arguments for vLLM engine.""" + # Model arguments + # TODO(shengguangming): delete the unused args + parser.add_argument('--model', + type=str, + default='facebook/opt-125m', + help='name or path of the huggingface model to use') + parser.add_argument('--tokenizer', + type=str, + default=EngineArgs.tokenizer, + help='name or path of the huggingface tokenizer to use') + parser.add_argument('--revision', + type=str, + default=None, + help='the specific model version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-revision', + type=str, + default=None, + help='the specific tokenizer version to use. It can be a branch ' + 'name, a tag name, or a commit id. If unspecified, will use ' + 'the default version.') + parser.add_argument('--tokenizer-mode', + type=str, + default=EngineArgs.tokenizer_mode, + choices=['auto', 'slow'], + help='tokenizer mode. "auto" will use the fast ' + 'tokenizer if available, and "slow" will ' + 'always use the slow tokenizer.') + parser.add_argument('--trust-remote-code', action='store_true', help='trust remote code from huggingface') + parser.add_argument('--download-dir', + type=str, + default=EngineArgs.download_dir, + help='directory to download and load the weights, ' + 'default to the default cache dir of ' + 'huggingface') + parser.add_argument('--load-format', + type=str, + default=EngineArgs.load_format, + choices=['auto', 'pt', 'safetensors', 'npcache', 'dummy'], + help='The format of the model weights to load. ' + '"auto" will try to load the weights in the safetensors format ' + 'and fall back to the pytorch bin format if safetensors format ' + 'is not available. ' + '"pt" will load the weights in the pytorch bin format. ' + '"safetensors" will load the weights in the safetensors format. ' + '"npcache" will load the weights in pytorch format and store ' + 'a numpy cache to speed up the loading. ' + '"dummy" will initialize the weights with random values, ' + 'which is mainly for profiling.') + parser.add_argument('--dtype', + type=str, + default=EngineArgs.dtype, + choices=['auto', 'half', 'float16', 'bfloat16', 'float', 'float32'], + help='data type for model weights and activations. ' + 'The "auto" option will use FP16 precision ' + 'for FP32 and FP16 models, and BF16 precision ' + 'for BF16 models.') + parser.add_argument('--max-model-len', + type=int, + default=None, + help='model context length. If unspecified, ' + 'will be automatically derived from the model.') + # Parallel arguments + parser.add_argument('--worker-use-ray', + action='store_true', + help='use Ray for distributed serving, will be ' + 'automatically set when using more than 1 GPU') + parser.add_argument('--pipeline-parallel-size', + '-pp', + type=int, + default=EngineArgs.pipeline_parallel_size, + help='number of pipeline stages') + parser.add_argument('--tensor-parallel-size', + '-tp', + type=int, + default=EngineArgs.tensor_parallel_size, + help='number of tensor parallel replicas') + # KV cache arguments + parser.add_argument('--block-size', + type=int, + default=EngineArgs.block_size, + choices=[8, 16, 32], + help='token block size') + # TODO(woosuk): Support fine-grained seeds (e.g., seed per request). + parser.add_argument('--seed', type=int, default=EngineArgs.seed, help='random seed') + parser.add_argument('--swap-space', + type=int, + default=EngineArgs.swap_space, + help='CPU swap space size (GiB) per GPU') + parser.add_argument('--gpu-memory-utilization', + type=float, + default=EngineArgs.gpu_memory_utilization, + help='the percentage of GPU memory to be used for' + 'the model executor') + parser.add_argument('--max-num-batched-tokens', + type=int, + default=EngineArgs.max_num_batched_tokens, + help='maximum number of batched tokens per ' + 'iteration') + parser.add_argument('--max-num-seqs', + type=int, + default=EngineArgs.max_num_seqs, + help='maximum number of sequences per iteration') + parser.add_argument('--disable-log-stats', action='store_true', help='disable logging statistics') + # Quantization settings. + parser.add_argument('--quantization', + '-q', + type=str, + choices=['awq', None], + default=None, + help='Method used to quantize the weights') + return parser + + @classmethod + def from_cli_args(cls, args: argparse.Namespace) -> 'EngineArgs': + # Get the list of attributes of this dataclass. + attrs = [attr.name for attr in dataclasses.fields(cls)] + # Set the attributes from the parsed arguments. + engine_args = cls(**{attr: getattr(args, attr) for attr in attrs}) + return engine_args + + def create_engine_config( + self, + ) -> EngineConfig: + # bitsandbytes quantization needs a specific model loader + # so we make sure the quant method and the load format are consistent + if (self.quantization == "bitsandbytes" or + self.qlora_adapter_name_or_path is not None) and \ + self.load_format != "bitsandbytes": + raise ValueError("BitsAndBytes quantization and QLoRA adapter only support " + f"'bitsandbytes' load format, but got {self.load_format}") + + if (self.load_format == "bitsandbytes" or + self.qlora_adapter_name_or_path is not None) and \ + self.quantization != "bitsandbytes": + raise ValueError("BitsAndBytes load format and QLoRA adapter only support " + f"'bitsandbytes' quantization, but got {self.quantization}") + + assert self.cpu_offload_gb >= 0, ("CPU offload space must be non-negative" + f", but got {self.cpu_offload_gb}") + + multimodal_config = MultiModalConfig() + device_config = DeviceConfig(self.device) + # NOTE(sgm): we only modify ModelConfig, other configs are import from vllm + model_config = ModelConfig(hf_config=self.model_hf_config, + tokenizer_mode=self.tokenizer_mode, + trust_remote_code=self.trust_remote_code, + dtype=self.dtype, + seed=self.seed, + revision=self.revision, + code_revision=self.code_revision, + rope_scaling=self.rope_scaling, + rope_theta=self.rope_theta, + tokenizer_revision=self.tokenizer_revision, + max_model_len=self.max_model_len, + quantization=self.quantization, + quantization_param_path=self.quantization_param_path, + enforce_eager=self.enforce_eager, + max_context_len_to_capture=self.max_context_len_to_capture, + max_seq_len_to_capture=self.max_seq_len_to_capture, + max_logprobs=self.max_logprobs, + disable_sliding_window=self.disable_sliding_window, + skip_tokenizer_init=self.skip_tokenizer_init, + served_model_name=self.served_model_name, + multimodal_config=multimodal_config) + cache_config = CacheConfig( + block_size=self.block_size, + gpu_memory_utilization=self.gpu_memory_utilization, + swap_space=self.swap_space, + cache_dtype=self.kv_cache_dtype, + num_gpu_blocks_override=self.num_gpu_blocks_override, + sliding_window=model_config.get_sliding_window(), + enable_prefix_caching=self.enable_prefix_caching, + cpu_offload_gb=self.cpu_offload_gb, + ) + parallel_config = ParallelConfig(pipeline_parallel_size=self.pipeline_parallel_size, + tensor_parallel_size=self.tensor_parallel_size, + worker_use_ray=self.worker_use_ray, + max_parallel_loading_workers=self.max_parallel_loading_workers, + disable_custom_all_reduce=self.disable_custom_all_reduce, + tokenizer_pool_config=TokenizerPoolConfig.create_config( + self.tokenizer_pool_size, + self.tokenizer_pool_type, + self.tokenizer_pool_extra_config, + ), + ray_workers_use_nsight=self.ray_workers_use_nsight, + distributed_executor_backend=self.distributed_executor_backend) + + # NOTE[VERL]: Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + parallel_config.world_size = world_size + + max_model_len = model_config.max_model_len + use_long_context = max_model_len > 32768 + if self.enable_chunked_prefill is None: + # If not explicitly set, enable chunked prefill by default for + # long context (> 32K) models. This is to avoid OOM errors in the + # initial memory profiling phase. + if use_long_context: + is_gpu = device_config.device_type == "cuda" + use_sliding_window = (model_config.get_sliding_window() is not None) + use_spec_decode = self.speculative_model is not None + has_seqlen_agnostic_layers = (model_config.contains_seqlen_agnostic_layers(parallel_config)) + if (is_gpu and not use_sliding_window and not use_spec_decode and not self.enable_lora and + not self.enable_prompt_adapter and not self.enable_prefix_caching and + not has_seqlen_agnostic_layers): + self.enable_chunked_prefill = True + logger.warning("Chunked prefill is enabled by default for models with " + "max_model_len > 32K. Currently, chunked prefill might " + "not work with some features or models. If you " + "encounter any issues, please disable chunked prefill " + "by setting --enable-chunked-prefill=False.") + if self.enable_chunked_prefill is None: + self.enable_chunked_prefill = False + + if not self.enable_chunked_prefill and use_long_context: + logger.warning( + "The model has a long context length (%s). This may cause OOM " + "errors during the initial memory profiling phase, or result " + "in low performance due to small KV cache space. Consider " + "setting --max-model-len to a smaller value.", max_model_len) + + # TODO: spec config + speculative_config = SpeculativeConfig.maybe_create_spec_config( + target_model_config=model_config, + target_parallel_config=parallel_config, + target_dtype=self.dtype, + speculative_model=self.speculative_model, + speculative_draft_tensor_parallel_size = \ + self.speculative_draft_tensor_parallel_size, + num_speculative_tokens=self.num_speculative_tokens, + speculative_disable_by_batch_size=self. + speculative_disable_by_batch_size, + speculative_max_model_len=self.speculative_max_model_len, + enable_chunked_prefill=self.enable_chunked_prefill, + use_v2_block_manager=self.use_v2_block_manager, + disable_log_stats=self.disable_log_stats, + ngram_prompt_lookup_max=self.ngram_prompt_lookup_max, + ngram_prompt_lookup_min=self.ngram_prompt_lookup_min, + draft_token_acceptance_method=\ + self.spec_decoding_acceptance_method, + typical_acceptance_sampler_posterior_threshold=self. + typical_acceptance_sampler_posterior_threshold, + typical_acceptance_sampler_posterior_alpha=self. + typical_acceptance_sampler_posterior_alpha, + disable_logprobs=self.disable_logprobs_during_spec_decoding, + ) + + scheduler_config = SchedulerConfig( + max_num_batched_tokens=self.max_num_batched_tokens, + max_num_seqs=self.max_num_seqs, + max_model_len=model_config.max_model_len, + use_v2_block_manager=self.use_v2_block_manager, + num_lookahead_slots=(self.num_lookahead_slots + if speculative_config is None else speculative_config.num_lookahead_slots), + delay_factor=self.scheduler_delay_factor, + enable_chunked_prefill=self.enable_chunked_prefill, + embedding_mode=model_config.embedding_mode, + preemption_mode=self.preemption_mode, + ) + lora_config = LoRAConfig(max_lora_rank=self.max_lora_rank, + max_loras=self.max_loras, + fully_sharded_loras=self.fully_sharded_loras, + lora_extra_vocab_size=self.lora_extra_vocab_size, + long_lora_scaling_factors=self.long_lora_scaling_factors, + lora_dtype=self.lora_dtype, + max_cpu_loras=self.max_cpu_loras if self.max_cpu_loras and self.max_cpu_loras > 0 else + None) if self.enable_lora else None + + if self.qlora_adapter_name_or_path is not None and \ + self.qlora_adapter_name_or_path != "": + if self.model_loader_extra_config is None: + self.model_loader_extra_config = {} + self.model_loader_extra_config["qlora_adapter_name_or_path"] = self.qlora_adapter_name_or_path + + load_config = LoadConfig( + load_format=self.load_format, + download_dir=self.download_dir, + model_loader_extra_config=self.model_loader_extra_config, + ignore_patterns=self.ignore_patterns, + ) + + prompt_adapter_config = PromptAdapterConfig( + max_prompt_adapters=self.max_prompt_adapters, + max_prompt_adapter_token=self.max_prompt_adapter_token) \ + if self.enable_prompt_adapter else None + + decoding_config = DecodingConfig(guided_decoding_backend=self.guided_decoding_backend) + + observability_config = ObservabilityConfig(otlp_traces_endpoint=self.otlp_traces_endpoint) + + if (model_config.get_sliding_window() is not None and scheduler_config.chunked_prefill_enabled and + not scheduler_config.use_v2_block_manager): + raise ValueError("Chunked prefill is not supported with sliding window. " + "Set --disable-sliding-window to disable sliding window.") + + return EngineConfig( + model_config=model_config, + cache_config=cache_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + device_config=device_config, + lora_config=lora_config, + multimodal_config=multimodal_config, + speculative_config=speculative_config, + load_config=load_config, + decoding_config=decoding_config, + observability_config=observability_config, + prompt_adapter_config=prompt_adapter_config, + ) diff --git a/verl/third_party/vllm/vllm_v_0_5_4/config.py b/verl/third_party/vllm/vllm_v_0_5_4/config.py new file mode 100644 index 000000000..5fc61e6fe --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/config.py @@ -0,0 +1,246 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/config.py + +import enum +import json +from typing import List, Optional, Union +from dataclasses import dataclass, field, fields + +import torch +from transformers import PretrainedConfig + +from vllm.logger import init_logger +from vllm.model_executor.layers.quantization import get_quantization_config +from vllm.transformers_utils.config import get_hf_text_config +from vllm.utils import is_hip, print_warning_once +# Add for verl +from vllm.config import ModelConfig, _get_and_verify_dtype, _get_and_verify_max_len, get_served_model_name + +GPTQMarlinConfig = get_quantization_config("gptq_marlin") + +logger = init_logger(__name__) + +_GB = 1 << 30 + + +class ModelConfig(ModelConfig): + """Configuration for the model. + + Args: + model: Name or path of the huggingface model to use. + tokenizer: Name or path of the huggingface tokenizer to use. + tokenizer_mode: Tokenizer mode. "auto" will use the fast tokenizer if + available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + download_dir: Directory to download and load the weights, default to the + default cache directory of huggingface. + load_format: The format of the model weights to load: + "auto" will try to load the weights in the safetensors format and + fall back to the pytorch bin format if safetensors format is + not available. + "pt" will load the weights in the pytorch bin format. + "safetensors" will load the weights in the safetensors format. + "npcache" will load the weights in pytorch format and store + a numpy cache to speed up the loading. + "dummy" will initialize the weights with random values, which is + mainly for profiling. + dtype: Data type for model weights and activations. The "auto" option + will use FP16 precision for FP32 and FP16 models, and BF16 precision + for BF16 models. + seed: Random seed for reproducibility. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. If unspecified, will use the default + version. + code_revision: The specific revision to use for the model code on + Hugging Face Hub. It can be a branch name, a tag name, or a + commit id. If unspecified, will use the default version. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. If unspecified, will use + the default version. + max_model_len: Maximum length of a sequence (including prompt and + output). If None, will be derived from the model. + quantization: Quantization method that was used to quantize the model + weights. If None, we assume the model weights are not quantized. + quantization_param_path: Path to JSON file containing scaling factors. + Used to load KV cache scaling factors into the model when KV cache + type is FP8_E4M3 on ROCm (AMD GPU). In the future these will also + be used to load activation and weight scaling factors when the + model dtype is FP8_E4M3 on ROCm. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode (DEPRECATED. Use max_seq_len_to_capture instead). + max_seq_len_to_capture: Maximum sequence len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode + skip_tokenizer_init: If true, skip initialization of tokenizer and + detokenizer. + served_model_name: The model name used in metrics tag `model_name`, + matches the model name exposed via the APIs. If multiple model + names provided, the first name will be used. If not specified, + the model name will be the same as `model`. + """ + + def __init__( + self, + hf_config: PretrainedConfig, + tokenizer_mode: str, + trust_remote_code: bool, + dtype: Union[str, torch.dtype], + seed: int, + revision: Optional[str] = None, + code_revision: Optional[str] = None, + rope_scaling: Optional[dict] = None, + rope_theta: Optional[float] = None, + tokenizer_revision: Optional[str] = None, + max_model_len: Optional[int] = None, + quantization: Optional[str] = None, + quantization_param_path: Optional[str] = None, + enforce_eager: bool = False, + max_context_len_to_capture: Optional[int] = None, + max_seq_len_to_capture: Optional[int] = None, + max_logprobs: int = 20, + disable_sliding_window: bool = False, + skip_tokenizer_init: bool = False, + served_model_name: Optional[Union[str, List[str]]] = None, + multimodal_config: Optional["MultiModalConfig"] = None, + ) -> None: + self.model = hf_config._name_or_path + self.tokenizer = hf_config._name_or_path + # NOTE(sgm): same as open-sourced + self.tokenizer_mode = tokenizer_mode + self.trust_remote_code = trust_remote_code + self.seed = seed + self.revision = revision + self.code_revision = code_revision + self.rope_scaling = rope_scaling + self.rope_theta = rope_theta + # The tokenizer version is consistent with the model version by default. + if tokenizer_revision is None: + self.tokenizer_revision = revision + else: + self.tokenizer_revision = tokenizer_revision + self.quantization = quantization + self.quantization_param_path = quantization_param_path + self.enforce_eager = enforce_eager + if max_context_len_to_capture is not None: + raise ValueError("`max_context_len_to_capture` is deprecated. " + "Use `max_seq_len_to_capture` instead.") + self.max_seq_len_to_capture = max_seq_len_to_capture + self.max_logprobs = max_logprobs + self.disable_sliding_window = disable_sliding_window + self.skip_tokenizer_init = skip_tokenizer_init + + # self.hf_config = get_config(model, trust_remote_code, revision) + self.hf_config = hf_config + self.hf_text_config = get_hf_text_config(hf_config) + self.dtype = _get_and_verify_dtype(self.hf_text_config, dtype) + # self.served_model_name = get_served_model_name(model, + # served_model_name) + # self._verify_load_format() + # self._verify_tokenizer_mode() + if (not self.disable_sliding_window and self.hf_text_config.model_type == "gemma2" and + self.hf_text_config.sliding_window is not None): + print_warning_once("Gemma 2 uses sliding window attention for every odd layer, " + "which is currently not supported by vLLM. Disabling sliding " + "window and capping the max length to the sliding window size " + f"({self.hf_text_config.sliding_window}).") + self.disable_sliding_window = True + + self.max_model_len = _get_and_verify_max_len(hf_config=self.hf_text_config, + max_model_len=max_model_len, + disable_sliding_window=self.disable_sliding_window, + sliding_window_len=self.get_hf_config_sliding_window()) + self.served_model_name = get_served_model_name( + self.model, # str + served_model_name) + self.multimodal_config = multimodal_config + + if not self.skip_tokenizer_init: + self._verify_tokenizer_mode() + self._verify_embedding_mode() + self._verify_quantization() + self._verify_cuda_graph() + + +class LoadFormat(str, enum.Enum): + AUTO = 'auto' + MEGATRON = "megatron" + HF = "hf" + DTENSOR = 'dtensor' + DUMMY_HF = 'dummy_hf' + DUMMY_MEGATRON = 'dummy_megatron' + DUMMY_DTENSOR = 'dummy_dtensor' + + +# TODO: check whether this is necessary +@dataclass +class LoadConfig: + """ + download_dir: Directory to download and load the weights, default to the + default cache directory of huggingface. + load_format: The format of the model weights to load: + "auto" will try to load the weights in the safetensors format and + fall back to the pytorch bin format if safetensors format is + not available. + "pt" will load the weights in the pytorch bin format. + "safetensors" will load the weights in the safetensors format. + "npcache" will load the weights in pytorch format and store + a numpy cache to speed up the loading. + "dummy" will initialize the weights with random values, which is + mainly for profiling. + "tensorizer" will use CoreWeave's tensorizer library for + fast weight loading. + "bitsandbytes" will load nf4 type weights. + ignore_patterns: The list of patterns to ignore when loading the model. + Default to "original/**/*" to avoid repeated loading of llama's + checkpoints. + + """ + + load_format: Union[str, LoadFormat, "BaseModelLoader"] = LoadFormat.AUTO + download_dir: Optional[str] = None + model_loader_extra_config: Optional[Union[str, dict]] = field(default_factory=dict) + ignore_patterns: Optional[Union[List[str], str]] = None + + def __post_init__(self): + model_loader_extra_config = self.model_loader_extra_config or {} + if isinstance(model_loader_extra_config, str): + self.model_loader_extra_config = json.loads(model_loader_extra_config) + self._verify_load_format() + + if self.ignore_patterns is not None and len(self.ignore_patterns) > 0: + logger.info("Ignoring the following patterns when downloading weights: %s", self.ignore_patterns) + else: + self.ignore_patterns = ["original/**/*"] + + def _verify_load_format(self) -> None: + if not isinstance(self.load_format, str): + return + + load_format = self.load_format.lower() + self.load_format = LoadFormat(load_format) + + rocm_not_supported_load_format: List[str] = [] + if is_hip() and load_format in rocm_not_supported_load_format: + rocm_supported_load_format = [ + f for f in LoadFormat.__members__ if (f not in rocm_not_supported_load_format) + ] + raise ValueError(f"load format '{load_format}' is not supported in ROCm. " + f"Supported load formats are " + f"{rocm_supported_load_format}") diff --git a/verl/third_party/vllm/vllm_v_0_5_4/dtensor_weight_loaders.py b/verl/third_party/vllm/vllm_v_0_5_4/dtensor_weight_loaders.py new file mode 100644 index 000000000..732b543db --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/dtensor_weight_loaders.py @@ -0,0 +1,340 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict, Iterable, Tuple +import torch +import torch.nn as nn +from torch.distributed._tensor import DTensor, Shard, Replicate + +from vllm.model_executor.layers.linear import * +from vllm.model_executor.models import ModelRegistry +from vllm.model_executor.model_loader.weight_utils import default_weight_loader +from vllm.model_executor.models.utils import is_pp_missing_parameter + + +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(vllm_model.named_parameters()) + 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 + stacked_name = name.replace(shard_name, param_name) + # Skip loading extra bias for GPTQ models. + if stacked_name.endswith(".bias") and stacked_name not in params_dict: + continue + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[stacked_name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + 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, local_loaded_weight.to(dtype=param.dtype)) + + +def gptbigcode_dtensor_load_weights(actor_weights: Dict, vllm_model: nn.Module): + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "lm_head.weight" in name: + continue + if ".attn.bias" in name: + # Skip attention mask. + # NOTE: "c_attn.bias" should not be skipped. + 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, local_loaded_weight.to(dtype=param.dtype)) + + +def starcoder2_dtensor_load_weights(actor_weights: Dict, vllm_model: 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"), + ] + + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_name, param_name) + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + param = params_dict[name] + weight_loader = param.weight_loader + weight_loader(param, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + + +def llama_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(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + if ("rotary_emb.cos_cached" in name or "rotary_emb.sin_cached" in name): + # Models trained using ColossalAI may include these tensors in + # the checkpoint. Skip them. + continue + # With tie_word_embeddings, we can skip lm_head.weight + # The weight might appear unnecessarily in the files if the model is + # processed with quantization, LoRA, fine-tuning, etc. + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_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, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + # 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, local_loaded_weight) + + +def qwen2_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(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + if vllm_model.config.tie_word_embeddings and "lm_head.weight" in name: + continue + for (param_name, weight_name, shard_id) in stacked_params_mapping: + if weight_name not in name: + continue + name = name.replace(weight_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, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + + +from vllm.model_executor.layers.fused_moe import FusedMoE + + +def deepseekv2_dtensor_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + stacked_params_mapping = [ + # (param_name, shard_name, shard_id) + ("gate_up_proj", "gate_proj", 0), + ("gate_up_proj", "up_proj", 1), + ] + + # Params for weights, fp8 weight scales, fp8 activation scales + # (param_name, weight_name, expert_id, shard_id) + expert_params_mapping = FusedMoE.make_expert_params_mapping(ckpt_gate_proj_name="gate_proj", + ckpt_down_proj_name="down_proj", + ckpt_up_proj_name="up_proj", + num_experts=vllm_model.config.n_routed_experts) + + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + for (param_name, weight_name, shard_id) in stacked_params_mapping: + # Skip non-stacked layers and experts (experts handled below). + if weight_name not in name: + continue + # We have mlp.experts[0].gate_proj in the checkpoint. + # Since we handle the experts below in expert_params_mapping, + # we need to skip here BEFORE we update the name, otherwise + # name will be updated to mlp.experts[0].gate_up_proj, which + # will then be updated below in expert_params_mapping + # for mlp.experts[0].gate_gate_up_proj, which breaks load. + if (("mlp.experts." in name) and name not in params_dict): + continue + name = name.replace(weight_name, param_name) + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + + if is_pp_missing_parameter(name, vllm_model): + continue + + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype), shard_id) + break + else: + for mapping in expert_params_mapping: + param_name, weight_name, expert_id, shard_id = mapping + if weight_name not in name: + continue + name = name.replace(weight_name, param_name) + + if is_pp_missing_parameter(name, vllm_model): + continue + + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, + local_loaded_weight.to(dtype=param.dtype), + weight_name, + shard_id=shard_id, + expert_id=expert_id) + break + else: + # Skip loading extra bias for GPTQ models. + if name.endswith(".bias") and name not in params_dict: + continue + + if is_pp_missing_parameter(name, vllm_model): + continue + + param = params_dict[name] + local_loaded_weight = redistribute_dtensor(param_name=name, loaded_weights=loaded_weight) + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, local_loaded_weight.to(dtype=param.dtype)) + + +def gpt2_dtensor_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + pass + + +def redistribute_dtensor(param_name: str, loaded_weights: DTensor, parallelize_plan: Dict = None): + param_name = _process_parameter_names(name=param_name) + if parallelize_plan is not None: + assert param_name in parallelize_plan.keys(), \ + f"param name: {param_name} not in parallelize_plan :{parallelize_plan.keys()}" + placement = parallelize_plan[param_name] + local_loaded_weights = loaded_weights.redistribute(device_mesh=loaded_weights.device_mesh, + placements=placement).to_local() + else: + local_loaded_weights = loaded_weights.full_tensor() + return local_loaded_weights + + +def _process_parameter_names(name): + # Remove '.weight' if it exists at the end of the string + if name.endswith(".weight"): + name = name[:-7] + + # Remove 'model.layers.x.' or 'model.' prefix + if "model.layers" in name: + parts = name.split('.') + # Reconstruct the string without 'model.layers.x.' + name = '.'.join(parts[3:]) # parts[0] is 'model', parts[1] is 'layers', parts[2] is 'x' + elif name.startswith("model."): + name = name[6:] # Remove 'model.' + + return name + + +__MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__ = { + 'GPT2LMHeadModel': gpt2_dtensor_weight_loader, + 'LlamaForCausalLM': llama_dtensor_weight_loader, + 'LLaMAForCausalLM': llama_dtensor_weight_loader, + 'MistralForCausalLM': llama_dtensor_weight_loader, # mistral is the same as llama in vLLM + 'InternLMForCausalLM': llama_dtensor_weight_loader, + 'AquilaModel': llama_dtensor_weight_loader, + 'AquilaForCausalLM': llama_dtensor_weight_loader, + 'Phi3ForCausalLM': llama_dtensor_weight_loader, + 'GemmaForCausalLM': gemma_dtensor_weight_loader, + 'Gemma2ForCausalLM': gemma_dtensor_weight_loader, + 'GPTBigCodeForCausalLM': gptbigcode_dtensor_load_weights, + 'Starcoder2ForCausalLM': starcoder2_dtensor_load_weights, + 'Qwen2ForCausalLM': qwen2_dtensor_weight_loader, + 'DeepseekV2ForCausalLM': deepseekv2_dtensor_weight_loader +} + + +# the actor model is .state_dict() +# Load dtensor weights +def load_dtensor_weights(actor_weights: Dict, vllm_model: nn.Module): + weight_loader = _get_model_weight_loader(vllm_model.__class__.__name__) + weight_loader(actor_weights, vllm_model) + # NOTE(sgm) to reduce peak memory usage, we offload vllm model to cpu + # after init, and we need this after sync model weights for in first iter. + vllm_model = vllm_model.cuda() + + +def _get_model_weight_loader(arch: str): + if arch in __MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__: + return __MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {__MODEL_DTENSOR_WEIGHT_LOADER_REGISTRY__.keys()}") + + +# NOTE(sgm): we use per-parameter weight loader in each vllm sub +def update_dtensor_weight_loader(): + pass diff --git a/verl/third_party/vllm/vllm_v_0_5_4/hf_weight_loader.py b/verl/third_party/vllm/vllm_v_0_5_4/hf_weight_loader.py new file mode 100644 index 000000000..6f1bf0b92 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/hf_weight_loader.py @@ -0,0 +1,42 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict, Union, Optional, Iterable, Tuple + +import torch +import torch.nn as nn + +from vllm.model_executor.model_loader.utils import set_default_torch_dtype +from vllm.model_executor.model_loader.weight_utils import default_weight_loader + + +def update_hf_weight_loader(): + print('no hf weight loader need to be updated') + return + + +def load_hf_weights(actor_weights: Dict, vllm_model: nn.Module): + assert isinstance(actor_weights, Dict) + with set_default_torch_dtype(next(vllm_model.parameters()).dtype): # TODO + vllm_model.load_weights(actor_weights.items()) + for _, module in vllm_model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + vllm_model = vllm_model.cuda() diff --git a/verl/third_party/vllm/vllm_v_0_5_4/llm.py b/verl/third_party/vllm/vllm_v_0_5_4/llm.py new file mode 100644 index 000000000..0979cdc7f --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/llm.py @@ -0,0 +1,239 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/entrypoints/llm.py + +from contextlib import contextmanager +from typing import ClassVar, List, Optional, Sequence, Union, cast, overload, Dict, Tuple + +from tqdm import tqdm +from transformers import PreTrainedTokenizer, PreTrainedTokenizerFast +from transformers import PretrainedConfig +import torch.nn as nn +from .arg_utils import EngineArgs +from .llm_engine_sp import LLMEngine +from vllm import LLM +from vllm.inputs import (PromptInputs, TextPrompt, TokensPrompt, parse_and_batch_prompt) +from vllm.logger import init_logger +from vllm.lora.request import LoRARequest +from vllm.model_executor.guided_decoding import (GuidedDecodingRequest, get_local_guided_decoding_logits_processor) +from vllm.model_executor.guided_decoding.guided_fields import LLMGuidedOptions +from vllm.outputs import EmbeddingRequestOutput, RequestOutput +from vllm.pooling_params import PoolingParams +from vllm.prompt_adapter.request import PromptAdapterRequest +from vllm.sampling_params import SamplingParams +from vllm.transformers_utils.tokenizer import get_cached_tokenizer +from vllm.usage.usage_lib import UsageContext +from vllm.utils import Counter, deprecate_kwargs +import torch +from torch.nn.utils.rnn import pad_sequence +from verl.trainer.ppo.rollout.tokenizer import HybridEngineBaseTokenizer + + +class LLM(LLM): + """An LLM for generating texts from given prompts and sampling parameters. + + This class includes a tokenizer, a language model (possibly distributed + across multiple GPUs), and GPU memory space allocated for intermediate + states (aka KV cache). Given a batch of prompts and sampling parameters, + this class generates texts from the model, using an intelligent batching + mechanism and efficient memory management. + + NOTE: This class is intended to be used for offline inference. For online + serving, use the `AsyncLLMEngine` class instead. + NOTE: For the comprehensive list of arguments, see `EngineArgs`. + + Args: + model: A HuggingFace Transformers model instance. + tokenizer: A HuggingFace Transformers tokenizer instance. + tokenizer_mode: The tokenizer mode. "auto" will use the fast tokenizer + if available, and "slow" will always use the slow tokenizer. + trust_remote_code: Trust remote code (e.g., from HuggingFace) when + downloading the model and tokenizer. + tensor_parallel_size: The number of GPUs to use for distributed + execution with tensor parallelism. + dtype: The data type for the model weights and activations. Currently, + we support `float32`, `float16`, and `bfloat16`. If `auto`, we use + the `torch_dtype` attribute specified in the model config file. + However, if the `torch_dtype` in the config is `float32`, we will + use `float16` instead. + quantization: The method used to quantize the model weights. Currently, + we support "awq". If None, we assume the model weights are not + quantized and use `dtype` to determine the data type of the weights. + revision: The specific model version to use. It can be a branch name, + a tag name, or a commit id. + tokenizer_revision: The specific tokenizer version to use. It can be a + branch name, a tag name, or a commit id. + seed: The seed to initialize the random number generator for sampling. + gpu_memory_utilization: The ratio (between 0 and 1) of GPU memory to + reserve for the model weights, activations, and KV cache. Higher + values will increase the KV cache size and thus improve the model's + throughput. However, if the value is too high, it may cause out-of- + memory (OOM) errors. + swap_space: The size (GiB) of CPU memory per GPU to use as swap space. + This can be used for temporarily storing the states of the requests + when their `best_of` sampling parameters are larger than 1. If all + requests will have `best_of=1`, you can safely set this to 0. + Otherwise, too small values may cause out-of-memory (OOM) errors. + enforce_eager: Whether to enforce eager execution. If True, we will + disable CUDA graph and always execute the model in eager mode. + If False, we will use CUDA graph and eager execution in hybrid. + max_context_len_to_capture: Maximum context len covered by CUDA graphs. + When a sequence has context length larger than this, we fall back + to eager mode. + disable_custom_all_reduce: See ParallelConfig + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer], + model_hf_config: PretrainedConfig, + tokenizer_mode: str = "auto", + trust_remote_code: bool = False, + skip_tokenizer_init: bool = False, + tensor_parallel_size: int = 1, + dtype: str = "auto", + quantization: Optional[str] = None, + revision: Optional[str] = None, + tokenizer_revision: Optional[str] = None, + seed: int = 0, + gpu_memory_utilization: float = 0.9, + swap_space: int = 4, + cpu_offload_gb: float = 0, + enforce_eager: bool = False, + max_context_len_to_capture: Optional[int] = None, + max_seq_len_to_capture: int = 8192, + disable_custom_all_reduce: bool = False, + load_format = 'auto', + **kwargs, + ) -> None: + if "disable_log_stats" not in kwargs: + kwargs["disable_log_stats"] = True + engine_args = EngineArgs( + model_hf_config=model_hf_config, + tensor_parallel_size=tensor_parallel_size, + dtype=dtype, + quantization=quantization, + revision=revision, + tokenizer_revision=tokenizer_revision, + seed=seed, + gpu_memory_utilization=gpu_memory_utilization, + swap_space=swap_space, + cpu_offload_gb=cpu_offload_gb, + enforce_eager=enforce_eager, + max_context_len_to_capture=max_context_len_to_capture, + max_seq_len_to_capture=max_seq_len_to_capture, + disable_custom_all_reduce=disable_custom_all_reduce, + load_format=load_format, + skip_tokenizer_init=skip_tokenizer_init, + **kwargs, + ) + tokenizer_cls = (PreTrainedTokenizer, PreTrainedTokenizerFast, HybridEngineBaseTokenizer) + if not isinstance(tokenizer, tokenizer_cls): + raise ValueError( + f"Unexpected tokenizer type: {type(tokenizer)}. Must be" + "one of the following: PreTrainedTokenizer, PreTrainedTokenizerFast, verl.trainer.ppo.rollout.HybridEngineBaseTokenizer" + ) + self.llm_engine = LLMEngine.from_engine_args(model, tokenizer, engine_args) # TODO: check usagecontext + self.request_counter = Counter() + + def init_cache_engine(self): + self.llm_engine.init_cache_engine() + + def free_cache_engine(self): + self.llm_engine.free_cache_engine() + + def get_tokenizer(self) -> Union[PreTrainedTokenizer, PreTrainedTokenizerFast]: + return self.llm_engine.tokenizer + + def set_tokenizer( + self, + tokenizer: Union[PreTrainedTokenizer, PreTrainedTokenizerFast], + ) -> None: + self.llm_engine.tokenizer = tokenizer + + def _run_engine(self, *, use_tqdm: bool) -> List[Union[RequestOutput, EmbeddingRequestOutput]]: + # Initialize tqdm. + if use_tqdm: + num_requests = self.llm_engine.get_num_unfinished_requests() + pbar = tqdm( + total=num_requests, + desc="Processed prompts", + dynamic_ncols=True, + postfix=(f"est. speed input: {0:.2f} toks/s, " + f"output: {0:.2f} toks/s"), + ) + # Run the engine. + outputs: List[Union[RequestOutput, EmbeddingRequestOutput]] = [] + total_in_toks = 0 + total_out_toks = 0 + while self.llm_engine.has_unfinished_requests(): + step_outputs = self.llm_engine.step() + for output in step_outputs: + if output.finished: + outputs.append(output) + if use_tqdm: + if isinstance(output, RequestOutput): + # Calculate tokens only for RequestOutput + total_in_toks += len(output.prompt_token_ids) + in_spd = total_in_toks / pbar.format_dict["elapsed"] + total_out_toks += sum(len(stp.token_ids) for stp in output.outputs) + out_spd = total_out_toks / pbar.format_dict["elapsed"] + pbar.postfix = (f"est. speed input: {in_spd:.2f} toks/s, " + f"output: {out_spd:.2f} toks/s") + pbar.update(1) + if use_tqdm: + pbar.close() + # Sort the outputs by request ID. + # This is necessary because some requests may be finished earlier than + # its previous requests. + outputs = sorted(outputs, key=lambda x: int(x.request_id)) + return self._post_process_outputs(outputs) + + # # NOTE(shengguangming): add for verl + # # TODO(sgm): we can optimize it by making the dataloader yield List[int] without padding. + # def _pre_process_inputs(self, prompt_token_ids: torch.Tensor) -> List[int]: + # # remove the left padding in the prompt token_id + # pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + # non_pad_index = torch.nonzero(prompt_token_ids != pad_token_id, as_tuple=False)[0][0] + # token_ids = prompt_token_ids[non_pad_index:].tolist() + # return token_ids + + # NOTE(shengguangming): add for verl + def _post_process_outputs(self, request_outputs: List[RequestOutput]) -> Tuple[torch.Tensor, torch.Tensor]: + output_token_ids = [] + logprobs = [] + for request_output in request_outputs: # List[RequestOutput] + outputs = request_output.outputs + for output in outputs: # List[CompletionOutput], usually len == 1 + output_token_ids.append(torch.tensor(output.token_ids)) + # TODO(shengguangming): can be optimzied by rewrite the Sampler._get_logprobs() logits + logprobs_dicts = output.logprobs + if logprobs_dicts is not None: + logprob = [] + for logprobs_dict, id in zip(logprobs_dicts, output.token_ids): + logprob.append(logprobs_dict[id].logprob) + logprobs.append(torch.tensor(logprob)) + + pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + output_token_ids = pad_sequence(output_token_ids, batch_first=True, padding_value=pad_token_id) + if len(logprobs) > 0: + logprobs = pad_sequence(logprobs, batch_first=True, padding_value=pad_token_id) + return output_token_ids, logprobs + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.llm_engine.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + def offload_model_weights(self) -> None: + self.llm_engine.offload_model_weights() diff --git a/verl/third_party/vllm/vllm_v_0_5_4/llm_engine_sp.py b/verl/third_party/vllm/vllm_v_0_5_4/llm_engine_sp.py new file mode 100644 index 000000000..098bffd06 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/llm_engine_sp.py @@ -0,0 +1,348 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/engine/llm_engine.py + +import torch +from typing import Dict, Optional, Union, Type + +import vllm.envs as envs +from vllm.config import (CacheConfig, DecodingConfig, DeviceConfig, EngineConfig, LoRAConfig, MultiModalConfig, + ObservabilityConfig, ParallelConfig, PromptAdapterConfig, SchedulerConfig, SpeculativeConfig) +from vllm.core.scheduler import Scheduler +from vllm.engine.output_processor.interfaces import (SequenceGroupOutputProcessor) +from vllm.engine.output_processor.stop_checker import StopChecker +from vllm.executor.executor_base import ExecutorBase +from vllm.inputs import INPUT_REGISTRY, LLMInputs, PromptInputs +from vllm.logger import init_logger +from vllm.transformers_utils.detokenizer import Detokenizer +from vllm.engine.metrics import (LoggingStatLogger, PrometheusStatLogger, StatLoggerBase, Stats) +from vllm.tracing import (SpanAttributes, SpanKind, extract_trace_context, init_tracer) +from vllm.usage.usage_lib import (UsageContext, is_usage_stats_enabled, usage_message) +from vllm.utils import Counter +from vllm.engine.llm_engine import _load_generation_config_dict +from vllm.engine.llm_engine import LLMEngine +from vllm.version import __version__ as VLLM_VERSION + +import torch.nn as nn +from .arg_utils import EngineArgs +from .tokenizer import TokenizerGroup +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) +_LOCAL_LOGGING_INTERVAL_SEC = 5 + + +class LLMEngine(LLMEngine): + """An LLM engine that receives requests and generates texts. + + This is the main class for the vLLM engine. It receives requests + from clients and generates texts from the LLM. It includes a tokenizer, a + language model (possibly distributed across multiple GPUs), and GPU memory + space allocated for intermediate states (aka KV cache). This class utilizes + iteration-level scheduling and efficient memory management to maximize the + serving throughput. + + The `LLM` class wraps this class for offline batched inference and the + `AsyncLLMEngine` class wraps this class for online serving. + + NOTE: The config arguments are derived from the `EngineArgs` class. For the + comprehensive list of arguments, see `EngineArgs`. + + Args: + model: the actor model initialize outside vllm (add for verl) + tokenizer: the initialized tokenizer (add for verl) + model_config: The configuration related to the LLM model. + cache_config: The configuration related to the KV cache memory + management. + parallel_config: The configuration related to distributed execution. + scheduler_config: The configuration related to the request scheduler. + distributed_init_method: The initialization method for distributed + execution. See `torch.distributed.init_process_group` for details. + placement_group: Ray placement group for distributed execution. + Required for distributed execution. + log_stats: Whether to log statistics. + """ + + def __init__( + self, + # NOTE(sgm): first two arguments are added for verl + model: Union[nn.Module, Dict], # model itself or its parameter dict + tokenizer: nn.Module, + # NOTE(sgm): vllm original arguments + model_config: ModelConfig, + cache_config: CacheConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], + speculative_config: Optional[SpeculativeConfig], + decoding_config: Optional[DecodingConfig], + observability_config: Optional[ObservabilityConfig], + prompt_adapter_config: Optional[PromptAdapterConfig], + executor_class: Type[ExecutorBase], + log_stats: bool, + usage_context: UsageContext = UsageContext.ENGINE_CONTEXT, + stat_loggers: Optional[Dict[str, StatLoggerBase]] = None, + ) -> None: + logger.info( + "Initializing an LLM engine (v%s) with config: " + "model=%r, speculative_config=%r, tokenizer=%r, " + "skip_tokenizer_init=%s, revision=%s, " + "rope_scaling=%r, rope_theta=%r, tokenizer_revision=%s, " + "trust_remote_code=%s, dtype=%s, max_seq_len=%d, " + "download_dir=%r, load_format=%s, tensor_parallel_size=%d, " + "pipeline_parallel_size=%d, " + "disable_custom_all_reduce=%s, quantization=%s, " + "enforce_eager=%s, kv_cache_dtype=%s, " + "quantization_param_path=%s, device_config=%s, " + "decoding_config=%r, observability_config=%r, " + "seed=%d, served_model_name=%s, use_v2_block_manager=%s, " + "enable_prefix_caching=%s)", + VLLM_VERSION, + model_config.model, + speculative_config, + model_config.tokenizer, + model_config.skip_tokenizer_init, + model_config.revision, + model_config.rope_scaling, + model_config.rope_theta, + model_config.tokenizer_revision, + model_config.trust_remote_code, + model_config.dtype, + model_config.max_model_len, + load_config.download_dir, + load_config.load_format, + parallel_config.tensor_parallel_size, + parallel_config.pipeline_parallel_size, + parallel_config.disable_custom_all_reduce, + model_config.quantization, + model_config.enforce_eager, + cache_config.cache_dtype, + model_config.quantization_param_path, + device_config.device, + decoding_config, + observability_config, + model_config.seed, + model_config.served_model_name, + scheduler_config.use_v2_block_manager, + cache_config.enable_prefix_caching, + ) + # TODO(woosuk): Print more configs in debug mode. + + self.model_config = model_config + self.cache_config = cache_config + self.lora_config = lora_config + self.multimodal_config = multimodal_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.speculative_config = speculative_config + self.load_config = load_config + self.decoding_config = decoding_config or DecodingConfig() + self.prompt_adapter_config = prompt_adapter_config + self.observability_config = observability_config or ObservabilityConfig() + self.log_stats = log_stats + + # self.model = model # should not store the model, it should be deleted + # TODO(shengguangming): maybe we can choose init here or from arguments + if not self.model_config.skip_tokenizer_init: + self.tokenizer = self._init_tokenizer(tokenizer) + self.detokenizer = Detokenizer(self.tokenizer) + else: + self.tokenizer = None + self.detokenizer = None + + self.seq_counter = Counter() + self.generation_config_fields = _load_generation_config_dict(model_config) + + self.input_processor = INPUT_REGISTRY.create_input_processor(self.model_config) + + self.model_executor = executor_class( + model=model, # add for spmd_gpu_executor + model_config=model_config, + cache_config=cache_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + device_config=device_config, + lora_config=lora_config, + multimodal_config=multimodal_config, + speculative_config=speculative_config, + load_config=load_config, + prompt_adapter_config=prompt_adapter_config, + ) + + # Profile the memory usage and initialize the cache. + if not self.model_config.embedding_mode: + self._initialize_kv_caches() + + # If usage stat is enabled, collect relevant info. + if is_usage_stats_enabled(): + from vllm.model_executor.model_loader import (get_architecture_class_name) + usage_message.report_usage( + get_architecture_class_name(model_config), + usage_context, + extra_kvs={ + # Common configuration + "dtype": str(model_config.dtype), + "tensor_parallel_size": parallel_config.tensor_parallel_size, + "block_size": cache_config.block_size, + "gpu_memory_utilization": cache_config.gpu_memory_utilization, + + # Quantization + "quantization": model_config.quantization, + "kv_cache_dtype": str(cache_config.cache_dtype), + + # Feature flags + "enable_lora": bool(lora_config), + "enable_prompt_adapter": bool(prompt_adapter_config), + "enable_prefix_caching": cache_config.enable_prefix_caching, + "enforce_eager": model_config.enforce_eager, + "disable_custom_all_reduce": parallel_config.disable_custom_all_reduce, + }) + + if self.tokenizer: + # Ping the tokenizer to ensure liveness if it runs in a + # different process. + self.tokenizer.ping() + + # Create the scheduler. + # NOTE: the cache_config here have been updated with the numbers of + # GPU and CPU blocks, which are profiled in the distributed executor. + self.scheduler = [ + Scheduler(scheduler_config, cache_config, lora_config, parallel_config.pipeline_parallel_size) + for _ in range(parallel_config.pipeline_parallel_size) + ] + + # Metric Logging. + if self.log_stats: + if stat_loggers is not None: + self.stat_loggers = stat_loggers + else: + self.stat_loggers = { + "logging": + LoggingStatLogger(local_interval=_LOCAL_LOGGING_INTERVAL_SEC), + "prometheus": + PrometheusStatLogger(local_interval=_LOCAL_LOGGING_INTERVAL_SEC, + labels=dict(model_name=model_config.served_model_name), + max_model_len=self.model_config.max_model_len), + } + self.stat_loggers["prometheus"].info("cache_config", self.cache_config) + + self.tracer = None + if self.observability_config.otlp_traces_endpoint: + self.tracer = init_tracer("vllm.llm_engine", self.observability_config.otlp_traces_endpoint) + + # Create sequence output processor, e.g. for beam search or + # speculative decoding. + self.output_processor = (SequenceGroupOutputProcessor.create_output_processor( + self.scheduler_config, + self.detokenizer, + self.scheduler, + self.seq_counter, + self.get_tokenizer_for_seq, + stop_checker=StopChecker( + self.scheduler_config.max_model_len, + self.get_tokenizer_for_seq, + ), + )) + + # TODO(sgm): add for verl but we may not tokenizer in Rollout + def _init_tokenizer(self, tokenizer, **tokenizer_init_kwargs): + init_kwargs = dict(enable_lora=bool(self.lora_config), + max_num_seqs=self.scheduler_config.max_num_seqs, + max_input_length=None) + init_kwargs.update(tokenizer_init_kwargs) + return TokenizerGroup(tokenizer, **init_kwargs) + + def init_cache_engine(self): + # TODO: check whether we should rebuild the CUDAGraph every iter when offload/load KVCache + # Re-capture CUDAGraph would be time-consuming + self.model_executor.init_cache_engine() + + def free_cache_engine(self): + self.model_executor.free_cache_engine() + + # NOTE(sgm): currently, we only support GPU executor + # The GPUExecutor remove the Ray dependency + @classmethod + def _get_executor_cls(cls, engine_config: EngineConfig) -> Type[ExecutorBase]: + distributed_executor_backend = (engine_config.parallel_config.distributed_executor_backend) + # Initialize the cluster and specify the executor class.] + # Initialize the cluster and specify the executor class. + assert engine_config.device_config.device_type == "cuda", \ + "Currently, the vllm in verl only support running on GPU" + + if engine_config.parallel_config.world_size == 1: + # TODO: may also need to init process group + from vllm.executor.gpu_executor import GPUExecutor + executor_class = GPUExecutor + else: + from .spmd_gpu_executor import SPMDGPUExecutor + executor_class = SPMDGPUExecutor + # elif distributed_executor_backend == "mp": + # from vllm.executor.multiproc_gpu_executor import ( + # MultiprocessingGPUExecutor) + # assert not envs.VLLM_USE_RAY_SPMD_WORKER, ( + # "multiprocessing distributed executor backend does not " + # "support VLLM_USE_RAY_SPMD_WORKER=1") + # executor_class = MultiprocessingGPUExecutor + # else: + # from vllm.executor.gpu_executor import GPUExecutor + # executor_class = GPUExecutor + return executor_class + + @classmethod + def from_engine_args( + cls, + model, + tokenizer, + engine_args: EngineArgs, + usage_context: UsageContext = UsageContext.ENGINE_CONTEXT, + stat_loggers: Optional[Dict[str, StatLoggerBase]] = None, + ) -> "LLMEngine": + """Creates an LLM engine from the engine arguments.""" + # Create the engine configs. + engine_config = engine_args.create_engine_config() + executor_class = cls._get_executor_cls(engine_config) + # Initialize the cluster and specify the executor class. + assert engine_config.device_config.device_type == "cuda", \ + "Currently, the vllm in verl only support running on GPU" + + if engine_config.parallel_config.world_size == 1: + # TODO: may also need to init process group + from vllm.executor.gpu_executor import GPUExecutor + executor_class = GPUExecutor + else: + from .spmd_gpu_executor import SPMDGPUExecutor + executor_class = SPMDGPUExecutor + + # Create the LLM engine. + engine = cls( + model, + tokenizer, + **engine_config.to_dict(), + executor_class=executor_class, + log_stats=not engine_args.disable_log_stats, + usage_context=usage_context, + stat_loggers=stat_loggers, + ) + return engine + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.model_executor.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + def offload_model_weights(self) -> None: + self.model_executor.offload_model_weights() diff --git a/verl/third_party/vllm/vllm_v_0_5_4/megatron_weight_loaders.py b/verl/third_party/vllm/vllm_v_0_5_4/megatron_weight_loaders.py new file mode 100644 index 000000000..4f2b19a90 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/megatron_weight_loaders.py @@ -0,0 +1,307 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/models + +from typing import Dict +import torch +import torch.nn as nn + +from vllm.model_executor.layers.linear import * +from vllm.model_executor.layers.vocab_parallel_embedding import VocabParallelEmbedding, ParallelLMHead +from vllm.model_executor.layers.activation import ScaledActivation +from vllm.model_executor.models import ModelRegistry + + +# NOTE(shengguangming): replace the origin weight loader function in the class +def parallel_weight_loader(self, param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Parallel Linear weight loader.""" + assert param.size() == loaded_weight.size( + ), 'the parameter size is not align with the loaded weight size, param size: {}, loaded_weight size: {}'.format( + param.size(), loaded_weight.size()) + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def default_weight_loader(param: torch.Tensor, loaded_weight: torch.Tensor) -> None: + """Default weight loader.""" + assert param.size() == loaded_weight.size() + assert param.data.dtype == loaded_weight.data.dtype, "if we want to shared weights, the data type should also be the same" + + param.data = loaded_weight.data + + +def gpt2_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_dict = dict(vllm_model.named_parameters(remove_duplicate=False)) + for name, loaded_weight in actor_weights.items(): + if "lm_head.weight" in name: + # GPT-2 ties the weights of the embedding layer and the final + # linear layer. + continue + if ".attn.bias" in name or ".attn.masked_bias" in name: + # Skip attention mask. + # NOTE: "c_attn.bias" should not be skipped. + continue + if not name.startswith("transformer."): + name = "transformer." + name + param = params_dict[name] + # The HF's GPT-2 implementation uses Conv1D instead of Linear. + # Because of this, we need to transpose the weights. + # Note(zhuohan): the logic below might break quantized models. + for conv1d_weight_name in ["c_attn", "c_proj", "c_fc"]: + if conv1d_weight_name not in name: + continue + if not name.endswith(".weight"): + continue + # TODO: check megatron + loaded_weight = loaded_weight.t() + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_te_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv.layer_norm_weight", "input_layernorm.weight"), + ("self_attention.linear_qkv.layer_norm_bias", "input_layernorm.bias"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1.layer_norm_weight', 'post_attention_layernorm.weight'), + ('mlp.linear_fc1.layer_norm_bias', 'post_attention_layernorm.bias'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ( + 'input_layernorm', + 'input_layernorm', + ), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def _replace_name(megatron_name, name_mapping): + for m_name, v_name in name_mapping: + if m_name not in megatron_name: + continue + if 'layers' in megatron_name: # deal with decoder layers + megatron_name = megatron_name.replace('decoder', 'model') + megatron_name_list = megatron_name.split('.') + if 'layer_norm_weight' in megatron_name_list or 'layer_norm_bias' in megatron_name_list: + param_name_list = megatron_name_list[:3] + param_name_list.append(v_name) + param_name = '.'.join(param_name_list) + else: + param_name_list = megatron_name_list[:3] + weight_or_bias = megatron_name_list[-1] + param_name_list.append(v_name) + param_name_list.append(weight_or_bias) + param_name = '.'.join(param_name_list) + return param_name + else: + param_name = megatron_name.replace(m_name, v_name) + return param_name + + +def llama_megatron_core_te_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv.layer_norm_weight", "input_layernorm.weight"), + ("self_attention.linear_qkv.layer_norm_bias", "input_layernorm.bias"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1.layer_norm_weight', 'post_attention_layernorm.weight'), + ('mlp.linear_fc1.layer_norm_bias', 'post_attention_layernorm.bias'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def llama_megatron_core_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + params_mapping = [ + # (megatron core gpt model name, vllm model name) + ("embedding.word_embeddings", "model.embed_tokens"), + ("self_attention.linear_qkv", "self_attn.qkv_proj"), + ("self_attention.linear_proj", 'self_attn.o_proj'), + ( + 'input_layernorm', + 'input_layernorm', + ), + ('pre_mlp_layernorm', 'post_attention_layernorm'), + ('mlp.linear_fc1', 'mlp.gate_up_proj'), + ('mlp.linear_fc2', 'mlp.down_proj'), + ('decoder.final_layernorm', 'model.norm'), + ('output_layer', 'lm_head'), + ] + # NOTE(shengguangming): the megatron llama may have this prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + name = _replace_name(name, params_mapping) + if name.endswith('.bias') and name not in params_dict: + continue + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +def _replace_name(megatron_name, name_mapping): + for m_name, v_name in name_mapping: + if m_name not in megatron_name: + continue + if 'layers' in megatron_name: # deal with decoder layers + megatron_name = megatron_name.replace('decoder', 'model') + megatron_name_list = megatron_name.split('.') + if 'layer_norm_weight' in megatron_name_list or 'layer_norm_bias' in megatron_name_list: + param_name_list = megatron_name_list[:3] + param_name_list.append(v_name) + param_name = '.'.join(param_name_list) + else: + param_name_list = megatron_name_list[:3] + weight_or_bias = megatron_name_list[-1] + param_name_list.append(v_name) + param_name_list.append(weight_or_bias) + param_name = '.'.join(param_name_list) + return param_name + else: + param_name = megatron_name.replace(m_name, v_name) + return param_name + + +def mistral_megatron_weight_loader(actor_weights: Dict, vllm_model: nn.Module) -> nn.Module: + # TODO: need to implement a general way to deal with prefix + params_dict = dict(vllm_model.named_parameters()) + for name, loaded_weight in actor_weights.items(): + if "rotary_emb.inv_freq" in name: + continue + else: + param = params_dict[name] + weight_loader = getattr(param, "weight_loader", default_weight_loader) + weight_loader(param, loaded_weight) + + +__LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__ = { + ColumnParallelLinear: parallel_weight_loader, + MergedColumnParallelLinear: parallel_weight_loader, + QKVParallelLinear: parallel_weight_loader, + RowParallelLinear: parallel_weight_loader, + VocabParallelEmbedding: parallel_weight_loader, + ParallelLMHead: parallel_weight_loader + # "ScaledActivation.weight_loader": ScaledActivation, # TODO(shengguangming): latest commit in vllm fix awq for this function and add load_weights + # "default_weight_loader": default_weight_loader +} + +# for layer_class, weight_loader in __LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__.items(): +# # setattr(layer_class, 'megatron_weight_loader', weight_loader) +# layer_class.weight_loader = weight_loader + +__MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__ = { + 'GPT2LMHeadModel': gpt2_weight_loader, + 'LlamaForCausalLM': llama_megatron_weight_loader, # use te backend for open-source megatron + 'LLaMAForCausalLM': llama_megatron_weight_loader, + 'MistralForCausalLM': mistral_megatron_weight_loader, +} + + +# the actor model is .state_dict() +# Load megatron weights +def load_megatron_weights(actor_weights: Dict, vllm_model: nn.Module): + weight_loader = _get_model_weight_loader(vllm_model.__class__.__name__) + weight_loader(actor_weights, vllm_model) + # NOTE(sgm) to reduce peak memory usage, we offload vllm model to cpu + # after init, and we need this after sync model weights for in first iter. + vllm_model = vllm_model.cuda() + + +def _get_model_weight_loader(arch: str): + if arch in __MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__: + return __MODEL_MEGATRON_WEIGHT_LOADER_REGISTRY__[arch] + raise ValueError(f"Model architectures {arch} are not supported for now. " + f"Supported architectures: {ModelRegistry.get_supported_archs()}") + + +def update_megatron_weight_loader(): + for layer_class, weight_loader in __LAYER_WEIGHT_MEGATRON_LOADER_REGISTRY__.items(): + layer_class.weight_loader = weight_loader diff --git a/verl/third_party/vllm/vllm_v_0_5_4/model_loader.py b/verl/third_party/vllm/vllm_v_0_5_4/model_loader.py new file mode 100644 index 000000000..1b675bb79 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/model_loader.py @@ -0,0 +1,302 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/tree/main/vllm/model_executor/model_loader + +from typing import Dict, Union, Optional, Iterable, Tuple + +import torch +import torch.nn as nn +from transformers import PreTrainedModel + +from vllm.config import (CacheConfig, DeviceConfig, LoadConfig, LoRAConfig, ModelConfig, MultiModalConfig, + ParallelConfig, SchedulerConfig) +from vllm.model_executor.model_loader import BaseModelLoader +from vllm.model_executor.model_loader.loader import _initialize_model +from vllm.model_executor.model_loader.utils import set_default_torch_dtype +from vllm.distributed.communication_op import tensor_model_parallel_all_gather + +from .config import ModelConfig, LoadFormat, LoadConfig +from .megatron_weight_loaders import load_megatron_weights, update_megatron_weight_loader +from .dtensor_weight_loaders import load_dtensor_weights, update_dtensor_weight_loader +from .hf_weight_loader import update_hf_weight_loader + + +def get_model(actor_model: Union[PreTrainedModel, Dict], + model_config: ModelConfig, + load_config: LoadConfig, + device_config: DeviceConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], + cache_config: CacheConfig = None) -> nn.Module: + loader = get_model_loader(load_config) + if load_config.load_format.startswith('dummy'): + return loader.load_model(model_config=model_config, + device_config=device_config, + lora_config=lora_config, + multimodal_config=multimodal_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + cache_config=cache_config) + else: + return loader.load_model(actor_model=actor_model, + model_config=model_config, + device_config=device_config, + lora_config=lora_config, + multimodal_config=multimodal_config, + parallel_config=parallel_config, + scheduler_config=scheduler_config, + cache_config=cache_config) + + +def get_model_loader(load_config: LoadConfig) -> BaseModelLoader: + """Get a model loader based on the load format.""" + + if isinstance(load_config.load_format, type): + return load_config.load_format(load_config) + + if load_config.load_format == LoadFormat.AUTO: + update_megatron_weight_loader() + return MegatronLoader(load_config) + + # NOTE(sgm): change the weight_loader function in runtime + if load_config.load_format == LoadFormat.MEGATRON: + update_megatron_weight_loader() + return MegatronLoader(load_config) + + if load_config.load_format == LoadFormat.HF: + update_hf_weight_loader() + return HFLoader(load_config) + + if load_config.load_format == LoadFormat.DTENSOR: + update_dtensor_weight_loader() + return DTensorLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_HF: + update_hf_weight_loader() + return DummyModelLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_MEGATRON: + update_megatron_weight_loader() + return DummyModelLoader(load_config) + + if load_config.load_format == LoadFormat.DUMMY_DTENSOR: + update_dtensor_weight_loader() + return DummyModelLoader(load_config) + + raise ValueError('load format not supported in verl: {}, only support {} and {}'.format( + load_config.load_format, LoadFormat.MEGATRON, LoadFormat.HF)) + + +class DummyModelLoader(BaseModelLoader): + """Model loader that will set model weights to random values.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def load_model(self, *, model_config: ModelConfig, device_config: DeviceConfig, lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, cache_config: CacheConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, multimodal_config, cache_config, + scheduler_config) + # NOTE(woosuk): For accurate performance evaluation, we assign + # random values to the weights. + # initialize_dummy_weights(model) + return model.eval() + + +class MegatronLoader(BaseModelLoader): + """Model loader that can load the model weights from partitioned megatron model.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(actor_model: Union[PreTrainedModel, Dict]): + # NOTE(shengguangming) Load the weights from the actor model + pass + # if isinstance(actor_model, nn.Module): + # load_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), vllm_model=model) + # else: + # load_weights(actor_weights=actor_model, vllm_model=model) + # return actor_model + + def load_model(self, actor_model: Union[PreTrainedModel, Dict], model_config: ModelConfig, + device_config: DeviceConfig, lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, cache_config: CacheConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, multimodal_config, cache_config, + scheduler_config) + + # TODO(sgm): This is a hack, we need to register the load_weight() func for each model in vllm + if isinstance(actor_model, nn.Module): + load_megatron_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), + vllm_model=model) + else: + load_megatron_weights(actor_weights=actor_model, vllm_model=model) + + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +class HFLoader(BaseModelLoader): + """Model loader that can load the model weights from model's full params.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(self, actor_model: Union[PreTrainedModel, Dict]): + if isinstance(actor_model, Dict): + return actor_model.items() + elif isinstance(actor_model, nn.Module): + return dict(actor_model.named_parameters()).items() + else: + raise ValueError(f'actor model should be Dict or nn.Module, but get {type(actor_model)}') + + def load_model(self, actor_model: Union[PreTrainedModel, Dict], model_config: ModelConfig, + device_config: DeviceConfig, lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, cache_config: CacheConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + # with torch.device(device_config.device): + # NOTE(sgm): init the model in cpu + model = _initialize_model(model_config, self.load_config, lora_config, multimodal_config, cache_config, + scheduler_config) + model.load_weights(self._get_weights_iterator(actor_model)) + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +class DTensorLoader(BaseModelLoader): + """Model loader that can load the model weights from partitioned megatron model.""" + + def __init__(self, load_config: LoadConfig): + super().__init__(load_config) + if load_config.model_loader_extra_config: + raise ValueError(f"Model loader extra config is not supported for " + f"load format {load_config.load_format}") + + def _get_weights_iterator(actor_model: Union[PreTrainedModel, Dict]): + # NOTE(shengguangming) Load the weights from the actor model + pass + # if isinstance(actor_model, nn.Module): + # load_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), vllm_model=model) + # else: + # load_weights(actor_weights=actor_model, vllm_model=model) + # return actor_model + + def load_model(self, actor_model: Union[PreTrainedModel, Dict], model_config: ModelConfig, + device_config: DeviceConfig, lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, cache_config: CacheConfig) -> nn.Module: + with set_default_torch_dtype(model_config.dtype): + with torch.device(device_config.device): + model = _initialize_model(model_config, self.load_config, lora_config, multimodal_config, cache_config, + scheduler_config) + + # TODO(sgm): This is a hack, we need to register the load_weight() func for each model in vllm + if isinstance(actor_model, nn.Module): + load_dtensor_weights(actor_weights=dict(actor_model.named_parameters(remove_duplicate=False)), + vllm_model=model) + else: + load_dtensor_weights(actor_weights=actor_model, vllm_model=model) + + for _, module in model.named_modules(): + quant_method = getattr(module, "quant_method", None) + if quant_method is not None: + quant_method.process_weights_after_loading(module) + # FIXME: Remove this after Mixtral is updated + # to use quant_method. + if hasattr(module, "process_weights_after_loading"): + module.process_weights_after_loading() + # NOTE(sgm) Some weights are point to gpu, but still need this. + model = model.cuda() # NOTE (zhangchi.usc1992) We need this for vllm to profile memory usage + return model.eval() + + +# FIXME(sgm): hack the _get_logits function in vllm v0.4.2 +# as they use ray, the _get_logits result will only need to return to the driver node, +# therefore gather is enough. However, we use SPMD instead of a central scheduler, +# all_gather is required (aligned with v0.2.6) +def _get_logits(self, hidden_states: torch.Tensor, embedding: torch.Tensor, + embedding_bias: Optional[torch.Tensor]) -> torch.Tensor: + # Get the logits for the next tokens. + logits = torch.matmul(hidden_states, embedding.t()) + if embedding_bias is not None: + logits += embedding_bias + logits = tensor_model_parallel_all_gather(logits) + # Remove paddings in vocab (if any). + if logits is not None: + logits = logits[:, :self.org_vocab_size] + return logits + + +from vllm.model_executor.layers.logits_processor import LogitsProcessor + + +def logitsprocessor_init(self, + vocab_size: int, + org_vocab_size: Optional[int] = None, + scale: float = 1.0, + logits_as_input: bool = False, + soft_cap: Optional[float] = None) -> None: + """ + Args: + scale: A scaling factor to apply to the logits. + """ + super(LogitsProcessor, self).__init__() + self.scale = scale + self.vocab_size = vocab_size + # Whether the input is logits (default is hidden states). + self.logits_as_input = logits_as_input + # original vocabulary size (without LoRA). + self.org_vocab_size = org_vocab_size or vocab_size + # Soft cap the logits. Used in Gemma 2. + self.soft_cap = soft_cap + # Whether to use gather or all-gather to gather the logits. + self.use_gather = False + + +LogitsProcessor.__init__ = logitsprocessor_init # use all_gather diff --git a/verl/third_party/vllm/vllm_v_0_5_4/model_runner.py b/verl/third_party/vllm/vllm_v_0_5_4/model_runner.py new file mode 100644 index 000000000..d6ab23255 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/model_runner.py @@ -0,0 +1,150 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/model_runner.py + +import torch +import torch.nn as nn +from enum import IntEnum +from typing import Dict, List, Optional, Set, Tuple, Union +import warnings + +import vllm.envs as envs +from vllm.attention import (AttentionMetadata, get_attn_backend) +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, MultiModalConfig, ParallelConfig, PromptAdapterConfig, + SchedulerConfig) +from vllm.logger import init_logger +from vllm.lora.layers import LoRAMapping +from vllm.lora.request import LoRARequest +from vllm.lora.worker_manager import LRUCacheWorkerLoRAManager +from vllm.model_executor import SamplingMetadata +from vllm.model_executor.models.interfaces import (supports_lora, supports_vision) +from vllm.utils import (CudaMemoryProfiler, is_hip, is_pin_memory_available) +from vllm.worker.model_runner import ModelRunner, CUDAGraphRunner +from vllm.prompt_adapter.worker_manager import (LRUCacheWorkerPromptAdapterManager) + +from .model_loader import get_model +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) + + +# How batches are constructed. +class BatchType(IntEnum): + # Every batch is prefill. + PREFILL = 0 + # Every batch is decode. + DECODE = 1 + # Batch is a mixture of prefill and decode. + MIXED = 2 + + +class ModelRunner(ModelRunner): + + def __init__( + self, + model: Union[nn.Module, Dict], # [verl] model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + cache_config: CacheConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + kv_cache_dtype: Optional[str] = "auto", + is_driver_worker: bool = False, + prompt_adapter_config: Optional[PromptAdapterConfig] = None, + multimodal_config: Optional[MultiModalConfig] = None, + return_hidden_states: bool = False, + ): + + super().__init__( + model_config, + parallel_config, + scheduler_config, + device_config, + cache_config, + load_config, + lora_config, + kv_cache_dtype, + is_driver_worker=True, # a hack + prompt_adapter_config=prompt_adapter_config, + multimodal_config=multimodal_config, + return_hidden_states=return_hidden_states) + + # NOTE(sgm): add for verl + self.model = model # this will be replaced by get_model() + + # NOTE(sgm): initialize model using the actor model + def load_model(self) -> None: + logger.info("Starting to load model %s...", self.model_config.model) + with CudaMemoryProfiler() as m: + self.model = get_model(actor_model=self.model, + model_config=self.model_config, + device_config=self.device_config, + lora_config=self.lora_config, + load_config=self.load_config, + parallel_config=self.parallel_config, + scheduler_config=self.scheduler_config, + multimodal_config=self.multimodal_config, + cache_config=self.cache_config) + self.model_memory_usage = m.consumed_memory + logger.info("Loading model weights took %.4f GB", self.model_memory_usage / float(2**30)) + + if self.lora_config: + assert supports_lora(self.model), "Model does not support LoRA" + assert not supports_vision(self.model), "To be tested: vision language model with LoRA settings." + + self.lora_manager = LRUCacheWorkerLoRAManager( + self.scheduler_config.max_num_seqs, + self.scheduler_config.max_num_batched_tokens, + self.vocab_size, + self.lora_config, + self.device, + self.model.embedding_modules, + self.model.embedding_padding_modules, + max_position_embeddings=self.model.config.max_position_embeddings, + ) + self.model = self.lora_manager.create_lora_manager(self.model) + + if self.prompt_adapter_config: + self.prompt_adapter_manager = LRUCacheWorkerPromptAdapterManager( + self.scheduler_config.max_num_seqs, self.scheduler_config.max_num_batched_tokens, self.device, + self.prompt_adapter_config) + self.model = (self.prompt_adapter_manager.create_prompt_adapter_manager(self.model)) + + if self.kv_cache_dtype == "fp8" and is_hip(): + # Currently only ROCm accepts kv-cache scaling factors + # via quantization_param_path and this will be deprecated + # in the future. + if self.model_config.quantization_param_path is not None: + if callable(getattr(self.model, "load_kv_cache_scales", None)): + warnings.warn( + "Loading kv cache scaling factor from JSON is " + "deprecated and will be removed. Please include " + "kv cache scaling factors in the model checkpoint.", + FutureWarning, + stacklevel=2) + self.model.load_kv_cache_scales(self.model_config.quantization_param_path) + logger.info("Loaded KV cache scaling factors from %s", self.model_config.quantization_param_path) + else: + raise RuntimeError( + "Using FP8 KV cache and scaling factors provided but " + "model %s does not support loading scaling factors.", self.model.__class__) + else: + logger.warning("Using FP8 KV cache but no scaling factors " + "provided. Defaulting to scaling factors of 1.0. " + "This may lead to less accurate results!") + + if envs.VLLM_TEST_DYNAMO_GRAPH_CAPTURE: + self.model = torch.compile(self.model, fullgraph=True, backend="eager") diff --git a/verl/third_party/vllm/vllm_v_0_5_4/parallel_state.py b/verl/third_party/vllm/vllm_v_0_5_4/parallel_state.py new file mode 100644 index 000000000..7efb85690 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/parallel_state.py @@ -0,0 +1,304 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM team. +# Adapted from +# https://github.com/NVIDIA/Megatron-LM/blob/main/megatron/core/parallel_state.py +# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved. + +"""Model and data parallel groups.""" +import os +import torch +import torch.distributed +from typing import Optional + +import vllm.distributed.parallel_state as ps +from vllm.distributed.parallel_state import get_pp_group, get_world_group, init_distributed_environment, init_model_parallel_group + +import vllm.envs as envs +from vllm.logger import init_logger + +from torch.distributed.device_mesh import init_device_mesh + +logger = init_logger(__name__) +""" +This version is strongly tied with Megatron to implement HybridEngine and weight sharing between vllm and Megatron. +- We assume the Megatron tp+dp+pp world is already established before calling this function. + +""" + +# Device mesh for using DTensor +_DEVICE_MESH = None + +# Tensor model parallel group that the current rank belongs to. +_TP = None +# Pipeline model parallel group that the current rank belongs to. +_PP = None + + +# This method is for initializing the ParallelGroup when using HybridEngine +def initialize_parallel_state( + distributed_init_method: str = "env://", + backend: str = "nccl", + tensor_model_parallel_size: int = 1, + num_tp_per_train_tp: int = 1, + pipeline_model_parallel_size: int = 1, +): + # torch.distributed.all_reduce does not free the input tensor until + # the synchronization point. This causes the memory usage to grow + # as the number of all_reduce calls increases. This env var disables + # this behavior. + # Related issue: + # https://discuss.pytorch.org/t/cuda-allocation-lifetime-for-inputs-to-distributed-all-reduce/191573 + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + + # NOTE(sgm): Modify for verl, Env vars will be set by TORCHRUN. + rank = int(os.getenv("RANK", "-1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + + # Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + init_distributed_environment(world_size, rank, distributed_init_method, local_rank, backend) + if torch.distributed.get_world_size() > 1: + # NOTE: build a sepearate inference group with infer tp & micro dp + initialize_model_parallel_for_vllm(tensor_model_parallel_size=tensor_model_parallel_size, + num_tensor_model_parallel_groups_per_train_tp=num_tp_per_train_tp) + else: + initialize_model_parallel(tensor_model_parallel_size, pipeline_model_parallel_size, backend) + + +def ensure_model_parallel_initialized( + tensor_model_parallel_size: int, + pipeline_model_parallel_size: int = 1, + backend: Optional[str] = None, +) -> None: + """Helper to initialize model parallel groups if they are not initialized, + or ensure tensor-parallel and pipeline-parallel sizes are equal to expected + values if the model parallel groups are initialized. + """ + # get the backend of _DEVICE_WORLD_GROUP + backend = backend or torch.distributed.get_backend(get_world_group().device_group) + if not model_parallel_is_initialized(): + initialize_model_parallel(tensor_model_parallel_size, pipeline_model_parallel_size, backend) + return + + assert (get_tensor_model_parallel_world_size() == tensor_model_parallel_size), ( + "tensor parallel group already initialized, but of unexpected size: " + f"{get_tensor_model_parallel_world_size()=} vs. " + f"{tensor_model_parallel_size=}") + pp_world_size = get_pp_group().world_size + assert (pp_world_size == pipeline_model_parallel_size), ( + "pipeline parallel group already initialized, but of unexpected size: " + f"{pp_world_size=} vs. " + f"{pipeline_model_parallel_size=}") + + +# TODO(sgm): deviate from the v0.5.4, not pp now +def model_parallel_is_initialized(): + """Check if tensor and pipeline parallel groups are initialized.""" + return (ps._TP is not None) + # and _PIPELINE_MODEL_PARALLEL_GROUP is not None) + + +def initialize_model_parallel_for_vllm(tensor_model_parallel_size: int, + num_tensor_model_parallel_groups_per_train_tp: int = 1, + pipeline_model_parallel_size: int = 1) -> None: + from torch.distributed import new_group + # Get world size and rank. Ensure some consistencies. + assert torch.distributed.is_initialized() + + assert isinstance(tensor_model_parallel_size, int) + + # assert num_tensor_model_parallel_groups_per_train_tp == 1 and not different_tp_group + # assert num_tensor_model_parallel_groups_per_train_tp > 1 and different_tp_group + + # Build the tensor model-parallel groups. + assert ps._TP is None, ("tensor model parallel group is already initialized") + + global _TP + + world_size: int = torch.distributed.get_world_size() + + rank = torch.distributed.get_rank() + + backend = torch.distributed.get_backend() + + num_tensor_model_parallel_groups = world_size // tensor_model_parallel_size + + if num_tensor_model_parallel_groups_per_train_tp == 1: + # if tensor_model_parallel_size == train_tensor_parallel_size: + # using the same tp group as Megatron/vllm + assert _TP is None, ("tensor model parallel group is already initialized") + group_ranks = [] + for i in range(num_tensor_model_parallel_groups): + ranks = range(i * tensor_model_parallel_size, (i + 1) * tensor_model_parallel_size) + group_ranks.append(ranks) + _TP = init_model_parallel_group( + group_ranks=group_ranks, + local_rank=get_world_group().local_rank, + backend=backend, + use_custom_allreduce=False, # TODO: check why True is not work in Ray trainer + use_message_queue_broadcaster=True) + ps._TP = _TP + # _MICRO_DATA_PARALLEL_GROUP is move to hybrid engine + else: + # initialize a micro_dp group and a tp group + # assume training tp=4, infer tp=2, then, weight is partitioned as + # [1], [2], [3], [4] for training and [1,2], [1,2], [3,4], [3,4] for inference + + # Build the inference tp groups + # train_tp = train_tensor_parallel_size + train_tp = num_tensor_model_parallel_groups_per_train_tp * tensor_model_parallel_size + # num_tensor_model_parallel_groups_per_train_tp = train_tp // tensor_model_parallel_size + assert _TP is None, ("tensor model parallel group is already initialized") + group_ranks = [] + for i in range(num_tensor_model_parallel_groups // num_tensor_model_parallel_groups_per_train_tp): + start = train_tp * i + end = train_tp * (i + 1) + for j in range(num_tensor_model_parallel_groups_per_train_tp): + ranks = list(range(start, end, num_tensor_model_parallel_groups_per_train_tp)) + for i in range(len(ranks)): + ranks[i] += j + group_ranks.append(ranks) + _TP = init_model_parallel_group( + group_ranks=group_ranks, + local_rank=get_world_group().local_rank, + backend=backend, + use_custom_allreduce=False, # TODO: check why True is not work in Ray trainer + use_message_queue_broadcaster=True) + ps._TP = _TP + + # Build the pipeline model-parallel groups. + # global _PIPELINE_MODEL_PARALLEL_GROUP + # global _PIPELINE_GLOBAL_RANKS + # assert ps._PIPELINE_MODEL_PARALLEL_GROUP is None, ("pipeline model parallel group is already initialized") + + # ps._PIPELINE_MODEL_PARALLEL_GROUP = mpu.get_pipeline_model_parallel_group() + # ps._PIPELINE_GLOBAL_RANKS = mpu.get_pipeline_model_parallel_ranks() + + # TODO: init using device mesh (not support hybrid engine now) + # Build the pipeline model-parallel groups. + num_pipeline_model_parallel_groups: int = (world_size // pipeline_model_parallel_size) + global _PP + assert _PP is None, ("pipeline model parallel group is already initialized") + group_ranks = [] + for i in range(num_pipeline_model_parallel_groups): + ranks = list(range(i, world_size, num_pipeline_model_parallel_groups)) + group_ranks.append(ranks) + # pipeline parallel does not need custom allreduce + _PP = init_model_parallel_group(group_ranks, get_world_group().local_rank, backend, use_custom_allreduce=False) + ps._PP = _PP # for verl + + +def initialize_model_parallel( + tensor_model_parallel_size: int = 1, + pipeline_model_parallel_size: int = 1, + backend: Optional[str] = None, +) -> None: + """ + NOTE: This method is a hack from the open-sourced version without + asertion of world_size = tp * pp + + Initialize model parallel groups. + + Arguments: + tensor_model_parallel_size: number of GPUs used for tensor model + parallelism. + pipeline_model_parallel_size: number of GPUs used for pipeline model + parallelism. + + Let's say we have a total of 8 GPUs denoted by g0 ... g7 and we + use 2 GPUs to parallelize the model tensor, and 4 GPUs to parallelize + the model pipeline. The present function will + create 4 tensor model-parallel groups and 2 pipeline model-parallel groups: + 4 tensor model-parallel groups: + [g0, g1], [g2, g3], [g4, g5], [g6, g7] + 2 pipeline model-parallel groups: + [g0, g2, g4, g6], [g1, g3, g5, g7] + Note that for efficiency, the caller should make sure adjacent ranks + are on the same DGX box. For example if we are using 2 DGX-1 boxes + with a total of 16 GPUs, rank 0 to 7 belong to the first box and + ranks 8 to 15 belong to the second box. + """ + # Get world size and rank. Ensure some consistencies. + assert torch.distributed.is_initialized() + world_size: int = torch.distributed.get_world_size() + backend = backend or torch.distributed.get_backend(ps.get_world_group().device_group) + + # NOTE(sgm) we don't assert world_size == tp * pp + # DP is not managed by vllm but by the veRL WorkerGroup + # 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})") + + num_tensor_model_parallel_groups: int = (world_size // tensor_model_parallel_size) + rank = torch.distributed.get_rank() + global _TP + assert _TP is None, ("tensor model parallel group is already initialized") + group_ranks = [] + for i in range(num_tensor_model_parallel_groups): + ranks = list(range(i * tensor_model_parallel_size, (i + 1) * tensor_model_parallel_size)) + group_ranks.append(ranks) + + # message queue broadcaster is only used in tensor model parallel group + _TP = init_model_parallel_group( + group_ranks, + get_world_group().local_rank, + backend, + use_custom_allreduce=False, # TODO: check why True is not work in Ray trainer + use_message_queue_broadcaster=True) + ps._TP = _TP + + # TODO: init using device mesh (not support hybrid engine now) + # Build the pipeline model-parallel groups. + num_pipeline_model_parallel_groups: int = (world_size // pipeline_model_parallel_size) + global _PP + assert _PP is None, ("pipeline model parallel group is already initialized") + group_ranks = [] + for i in range(num_pipeline_model_parallel_groups): + ranks = list(range(i, world_size, num_pipeline_model_parallel_groups)) + group_ranks.append(ranks) + # pipeline parallel does not need custom allreduce + _PP = init_model_parallel_group(group_ranks, get_world_group().local_rank, backend, use_custom_allreduce=False) + ps._PP = _PP # for verl + + +""" +Device mesh utilities +""" + + +def get_device_mesh(): + assert _DEVICE_MESH is not None, ("device mesh is not initialized") + return _DEVICE_MESH + + +""" +Tensor model parallel utilities +""" + + +def get_tensor_model_parallel_group(): + """Get the tensor model parallel group the caller rank belongs to.""" + assert _TP is not None, ("tensor model parallel group is not initialized") + return _TP.device_group + + +def get_tensor_model_parallel_world_size(): + """Return world size for the tensor model parallel group.""" + return torch.distributed.get_world_size(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_rank(): + """Return my rank for the tensor model parallel group.""" + return torch.distributed.get_rank(group=get_tensor_model_parallel_group()) + + +def get_tensor_model_parallel_src_rank(): + """Calculate the global rank corresponding to the first local rank + in the tensor model parallel group.""" + global_rank = torch.distributed.get_rank() + local_world_size = get_tensor_model_parallel_world_size() + return (global_rank // local_world_size) * local_world_size diff --git a/verl/third_party/vllm/vllm_v_0_5_4/spmd_gpu_executor.py b/verl/third_party/vllm/vllm_v_0_5_4/spmd_gpu_executor.py new file mode 100644 index 000000000..e9040d52b --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/spmd_gpu_executor.py @@ -0,0 +1,253 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/executor/gpu_executor.py + +import os +import socket +from typing import Any, Dict, List, Optional, Set, Tuple + +import torch +import vllm.envs as envs +from vllm.executor.executor_base import ExecutorBase, ExecutorAsyncBase +from vllm.logger import init_logger +from vllm.lora.request import LoRARequest +from vllm.sequence import SamplerOutput, ExecuteModelRequest + +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, MultiModalConfig, ParallelConfig, PromptAdapterConfig, + SchedulerConfig, SpeculativeConfig) +from .config import ModelConfig, LoadConfig + +logger = init_logger(__name__) + + +class SPMDGPUExecutor(ExecutorBase): + """SPMD-based multi-GPU executor implementations.""" + + def __init__( + self, + model, # pytorch model itself or its parameter dict + model_config: ModelConfig, + cache_config: CacheConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + load_config: LoadConfig, + lora_config: Optional[LoRAConfig], + multimodal_config: Optional[MultiModalConfig], + speculative_config: Optional[SpeculativeConfig], + prompt_adapter_config: Optional[PromptAdapterConfig], + ) -> None: + self.model_config = model_config + self.cache_config = cache_config + self.lora_config = lora_config + self.load_config = load_config + self.parallel_config = parallel_config + self.scheduler_config = scheduler_config + self.device_config = device_config + self.multimodal_config = multimodal_config + self.speculative_config = speculative_config + self.prompt_adapter_config = prompt_adapter_config + + distributed_init_method = initialize_cluster(parallel_config) + self._init_executor(model, distributed_init_method) + + # TODO(sgm): verl not support speculative decode now + def _init_executor(self, model, distributed_init_method) -> None: + assert (not self.speculative_config), "Speculative decoding not yet supported for multi-GPU backend." + + # Create the parallel worker for each GPU. + self._init_workers_sp(model, distributed_init_method) + + def _init_workers_sp(self, model, distributed_init_method: str): + # Lazy import the Worker to avoid importing torch.cuda/xformers + # before CUDA_VISIBLE_DEVICES is set in the Worker + from .worker import Worker # pylint: disable=import-outside-toplevel + + rank = int(os.getenv("RANK")) + local_rank = int(os.getenv("LOCAL_RANK")) + print(f'local rank {local_rank}') + + # see https://github.com/NVIDIA/nccl/issues/1234 + os.environ['NCCL_CUMEM_ENABLE'] = '0' + + self.worker = Worker( + model, + self.model_config, + self.parallel_config, + self.scheduler_config, + self.device_config, + self.cache_config, + self.load_config, + local_rank, + rank, + distributed_init_method, + lora_config=self.lora_config, + multimodal_config=self.multimodal_config, + speculative_config=None, + prompt_adapter_config=self.speculative_config, + is_driver_worker=True, + model_runner_cls=None, # use the default one + ) + + # NOTE(shengguangming): torch.distributed.init_process_group will be called inside the init_model() + self.worker.init_device() + self.worker.load_model() + + def determine_num_available_blocks(self) -> Tuple[int, int]: + """Determine the number of available KV blocks. + + This invokes `determine_num_available_blocks` on each worker and takes + the min of the results, guaranteeing that the selected cache sizes are + compatible with all workers. + + Returns: + - tuple[num_gpu_blocks, num_cpu_blocks] + """ + # Get the maximum number of blocks that can be allocated on GPU and CPU. + num_blocks = self.worker.determine_num_available_blocks() + + # NOTE(shengguangming): Now we don't use a shared centralized controler but each process will + # have its own scheduler + num_gpu_blocks = num_blocks[0] + num_cpu_blocks = num_blocks[1] + + return num_gpu_blocks, num_cpu_blocks + + def initialize_cache(self, num_gpu_blocks: int, num_cpu_blocks: int) -> None: + """Initialize the KV cache in all workers. + """ + + # NOTE: We log here to avoid multiple logs when number of workers is + # greater than one. We could log in the engine, but not all executors + # have GPUs. + logger.info("# GPU blocks: %d, # CPU blocks: %d", num_gpu_blocks, num_cpu_blocks) + + self.cache_config.num_gpu_blocks = num_gpu_blocks + self.cache_config.num_cpu_blocks = num_cpu_blocks + + if torch.distributed.get_rank() == 0: + print( + f'before init cache memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB' + ) + self.worker.initialize_cache(num_gpu_blocks=num_gpu_blocks, num_cpu_blocks=num_cpu_blocks) + if torch.distributed.get_rank() == 0: + print( + f'after init cache memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB' + ) + + # NOTE(sgm): This will not profile & capture the model(CUDAGraph) when rebuilding KVCache + def init_cache_engine(self) -> None: + self.worker._init_cache_engine() + + def free_cache_engine(self) -> None: + self.worker.free_cache_engine() + + def execute_model(self, execute_model_req) -> List[SamplerOutput]: + all_outputs = self.worker.execute_model(execute_model_req=execute_model_req) + + # NOTE(sgm): + # Each GPU in vllm under verl has its own spmd_gpu_executor, therefore all GPUs should return the outputs + # In vllm with ray, only the driver worker returns the sampling results. + return all_outputs + + def add_lora(self, lora_request: LoRARequest) -> bool: + assert lora_request.lora_int_id > 0, "lora_id must be greater than 0." + return self.worker.add_lora(lora_request=lora_request) + + def remove_lora(self, lora_id: int) -> bool: + assert lora_id > 0, "lora_id must be greater than 0." + return self.worker.remove_lora(lora_id=lora_id) + + def list_loras(self) -> Set[int]: + return self.worker.list_loras() + + def check_health(self) -> None: + # SPMDExecutor will always be healthy as long as + # it's running. + return + + # NOTE(sgm) add for verl to pass the abstract class test, not used + from vllm.prompt_adapter.request import PromptAdapterRequest + + def add_prompt_adapter(self, prompt_adapter_request: PromptAdapterRequest) -> bool: + assert prompt_adapter_request.prompt_adapter_id > 0, \ + "prompt_adapter_id must be greater than 0." + return self.worker.add_prompt_adapter(prompt_adapter_request) + + def list_prompt_adapters(self) -> Set[int]: + return self.worker.list_prompt_adapters() + + def pin_lora(self, lora_id: int) -> bool: + assert lora_id > 0, "lora_id must be greater than 0." + return self.worker.pin_lora(lora_id) + + def pin_prompt_adapter(self, prompt_adapter_id: int) -> bool: + assert prompt_adapter_id > 0, \ + "prompt_adapter_id must be greater than 0." + return self.worker.pin_prompt_adapter(prompt_adapter_id) + + def remove_prompt_adapter(self, prompt_adapter_id: int) -> bool: + assert prompt_adapter_id > 0, \ + "prompt_adapter_id must be greater than 0." + return self.worker.remove_prompt_adapter(prompt_adapter_id) + + # NOTE(sgm): add for verl + def offload_model_weights(self) -> None: + self.worker.offload_model_weights() + + def sync_model_weights(self, actor_weights: Dict[str, torch.Tensor], load_format: str) -> None: + self.worker.sync_model_weights(actor_weights=actor_weights, load_format=load_format) + + +def initialize_cluster( + parallel_config: ParallelConfig, + engine_use_ray: bool = False, + ray_address: Optional[str] = None, +) -> Tuple[str, Optional[None]]: + """Initialize the distributed cluster probably with Ray. + + Args: + parallel_config: The configurations for parallel execution. + + Returns: + The `distributed_init_method` is the address for initializing the + distributed backend. + """ + + # Initialize cluster locally. + port = get_open_port() + # We need to setup the distributed init method to make sure + # the distributed megatron code (e.g., get world size) works correctly. + # distributed_init_method = f"tcp://localhost:{port}" + distributed_init_method = 'env://' + return distributed_init_method + + +def get_open_port(): + with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: + s.bind(("", 0)) + return s.getsockname()[1] + + +# TODO(sgm): not implemented async executor yet +class SPMDGPUExecutorAsync(SPMDGPUExecutor, ExecutorAsyncBase): + + async def execute_model_async(self, execute_model_req: ExecuteModelRequest) -> List[SamplerOutput]: + """Executes one model step on the given sequences.""" + raise NotImplementedError + + async def check_health_async(self) -> None: + """Checks if the executor is healthy. If not, it should raise an + exception.""" + self.check_health() diff --git a/verl/third_party/vllm/vllm_v_0_5_4/tokenizer.py b/verl/third_party/vllm/vllm_v_0_5_4/tokenizer.py new file mode 100644 index 000000000..aa625a033 --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/tokenizer.py @@ -0,0 +1,77 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/transformers_utils/tokenizer_group/tokenizer_group.py + +from typing import List, Optional, Tuple, Union + +from transformers import (AutoTokenizer, PreTrainedTokenizer, PreTrainedTokenizerFast) + +from vllm.lora.request import LoRARequest +from vllm.utils import make_async, LRUCache +from vllm.transformers_utils.tokenizers import * + + +class TokenizerGroup: + """A group of tokenizers that can be used for LoRA adapters.""" + + def __init__(self, tokenizer: PreTrainedTokenizer, enable_lora: bool, max_num_seqs: int, + max_input_length: Optional[int]): + self.enable_lora = enable_lora + self.max_input_length = max_input_length + self.tokenizer = tokenizer + self.lora_tokenizers = LRUCache[PreTrainedTokenizer](capacity=max_num_seqs) if enable_lora else None + + def ping(self) -> bool: + """Check if the tokenizer group is alive.""" + return True + + def get_max_input_len(self, lora_request: Optional[LoRARequest] = None) -> Optional[int]: + """Get the maximum input length for the LoRA request.""" + return self.max_input_length + + def encode(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = self.get_lora_tokenizer(lora_request) + return tokenizer.encode(prompt) + + async def encode_async(self, + prompt: str, + request_id: Optional[str] = None, + lora_request: Optional[LoRARequest] = None) -> List[int]: + tokenizer = await self.get_lora_tokenizer_async(lora_request) + return tokenizer.encode(prompt) + + def get_lora_tokenizer(self, lora_request: Optional[LoRARequest]) -> "PreTrainedTokenizer": + if not lora_request or not self.enable_lora: + return self.tokenizer + if lora_request.lora_int_id not in self.lora_tokenizers: + # TODO(sgm): the lora tokenizer is also passed, but may be different + tokenizer = self.tokenizer + # tokenizer = (get_lora_tokenizer( + # lora_request, **self.tokenizer_config) or self.tokenizer) + self.lora_tokenizers.put(lora_request.lora_int_id, tokenizer) + return tokenizer + else: + return self.lora_tokenizers.get(lora_request.lora_int_id) + + # FIXME(sgm): for simplicity, we assign the special token here + @property + def pad_token_id(self): + return self.tokenizer.pad_token_id + + @property + def eos_token_id(self): + return self.tokenizer.eos_token_id diff --git a/verl/third_party/vllm/vllm_v_0_5_4/worker.py b/verl/third_party/vllm/vllm_v_0_5_4/worker.py new file mode 100644 index 000000000..a5deb675a --- /dev/null +++ b/verl/third_party/vllm/vllm_v_0_5_4/worker.py @@ -0,0 +1,323 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2023 The vLLM 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. +# Adapted from https://github.com/vllm-project/vllm/blob/main/vllm/worker/worker.py +"""A GPU worker class.""" +import os +import gc +from typing import Dict, List, Tuple, Optional, Union, Type + +import torch +import torch.distributed +import torch.nn as nn + +from vllm.config import (CacheConfig, DeviceConfig, LoRAConfig, MultiModalConfig, ParallelConfig, PromptAdapterConfig, + SchedulerConfig, SpeculativeConfig) +from vllm.model_executor import set_random_seed +from vllm.sequence import (ExecuteModelRequest, IntermediateTensors, SamplerOutput) +from vllm.worker.cache_engine import CacheEngine +# TODO(sgm): check why vllm has similar file in vllm.model_executor.parallel_utils.parallel_state +from vllm.distributed import (init_distributed_environment, set_custom_all_reduce, get_tensor_model_parallel_group) +from vllm.worker.worker_base import WorkerInput +from vllm.worker.worker import Worker, _check_if_gpu_supports_dtype +from vllm.worker.model_runner_base import ModelRunnerBase, ModelRunnerInputBase +from vllm.worker.embedding_model_runner import EmbeddingModelRunner +from vllm.worker.model_runner import GPUModelRunnerBase +from .model_runner import ModelRunner +from .megatron_weight_loaders import load_megatron_weights +from .hf_weight_loader import load_hf_weights +from .dtensor_weight_loaders import load_dtensor_weights +from .parallel_state import (ensure_model_parallel_initialized) +from .config import ModelConfig, LoadConfig, LoadFormat + + +class Worker(Worker): + """A worker class that executes (a partition of) the model on a GPU. + + Each worker is associated with a single GPU. The worker is responsible for + maintaining the KV cache and executing the model on the GPU. In case of + distributed inference, each worker is assigned a partition of the model. + """ + + def __init__( + self, + model: Union[nn.Module, Dict], # model itself or its parameter dict + model_config: ModelConfig, + parallel_config: ParallelConfig, + scheduler_config: SchedulerConfig, + device_config: DeviceConfig, + cache_config: CacheConfig, + load_config: LoadConfig, + local_rank: int, + rank: int, + distributed_init_method: str, + lora_config: Optional[LoRAConfig] = None, + multimodal_config: Optional[MultiModalConfig] = None, + speculative_config: Optional[SpeculativeConfig] = None, + prompt_adapter_config: Optional[PromptAdapterConfig] = None, + is_driver_worker: bool = False, + model_runner_cls: Optional[Type[GPUModelRunnerBase]] = None, + ) -> None: + # self.model = model # will be replaced in the init_model + self.model_config = model_config + self.parallel_config = parallel_config + self.parallel_config.rank = rank + self.scheduler_config = scheduler_config + self.device_config = device_config + self.cache_config = cache_config + self.local_rank = local_rank + self.rank = rank + self.distributed_init_method = distributed_init_method + self.lora_config = lora_config + self.load_config = load_config + self.prompt_adapter_config = prompt_adapter_config + self.is_driver_worker = is_driver_worker # TODO: we don't need driver + # if parallel_config and is_driver_worker: + # assert rank % parallel_config.tensor_parallel_size == 0, \ + # "Driver worker should be rank 0 of tensor parallel group." + if self.model_config.trust_remote_code: + # note: lazy import to avoid importing torch before initializing + from vllm.utils import init_cached_hf_modules + init_cached_hf_modules() + self.multimodal_config = multimodal_config + + # Return hidden states from target model if the draft model is an + # mlp_speculator + speculative_args = {} if speculative_config is None \ + or (speculative_config.draft_model_config.model == + model_config.model) \ + or (speculative_config.draft_model_config.hf_config.model_type + not in ["medusa", "mlp_speculator"]) \ + else {"return_hidden_states": True} + + # TODO(sgm): set correct model runner class + ModelRunnerClass: Type[GPUModelRunnerBase] = ModelRunner + if model_runner_cls is not None: + ModelRunnerClass = model_runner_cls + elif self.model_config.embedding_mode: + ModelRunnerClass = EmbeddingModelRunner + self.model_runner: GPUModelRunnerBase = ModelRunnerClass( + model, # [VERL]: add for verl + model_config, + parallel_config, + scheduler_config, + device_config, + cache_config, + load_config=load_config, + lora_config=self.lora_config, + kv_cache_dtype=self.cache_config.cache_dtype, + is_driver_worker=is_driver_worker, + prompt_adapter_config=prompt_adapter_config, + multimodal_config=multimodal_config, + **speculative_args, + ) + + # Uninitialized cache engine. Will be initialized by + # initialize_cache. + self.cache_engine: List[CacheEngine] = None + # Initialize gpu_cache as embedding models don't initialize kv_caches + self.gpu_cache: Optional[List[List[torch.Tensor]]] = None + + # NOTE(sgm): [VERL] For offloading inference engine params + self.cpu_model = None + + def init_device(self) -> None: + if self.device_config.device.type == "cuda": + # torch.distributed.all_reduce does not free the input tensor until + # the synchronization point. This causes the memory usage to grow + # as the number of all_reduce calls increases. This env var disables + # this behavior. + # Related issue: + # https://discuss.pytorch.org/t/cuda-allocation-lifetime-for-inputs-to-distributed-all-reduce/191573 + os.environ["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1" + + # NOTE(sgm): Modify for verl, Env vars will be set by TORCHRUN. + self.rank = self.rank if self.rank is not None else int(os.getenv("RANK", "-1")) + local_rank = int(os.getenv("LOCAL_RANK", "0")) + self.device = torch.device(f"cuda:{local_rank}") + if self.rank < 0: + raise ValueError("Invalid or unspecified rank.") + torch.cuda.set_device(self.device) + + # Use the world_size set by TORCHRUN + world_size = int(os.getenv("WORLD_SIZE", "-1")) + assert world_size != -1, "The world_size is set to -1, not initialized by TORCHRUN" + self.parallel_config.world_size = world_size + + _check_if_gpu_supports_dtype(self.model_config.dtype) + torch.cuda.empty_cache() + self.init_gpu_memory = torch.cuda.mem_get_info()[0] + else: + raise RuntimeError(f"Not support device type: {self.device_config.device}") + + # Initialize the distributed environment. + init_worker_distributed_environment(self.parallel_config, self.rank, self.distributed_init_method, + self.local_rank) + # Set random seed. + set_random_seed(self.model_config.seed) + # self.model = get_model(actor_model=self.model, model_config=self.model_config) + + @torch.inference_mode() + def determine_num_available_blocks(self) -> Tuple[int, int]: + """Profiles the peak memory usage of the model to determine how many + KV blocks may be allocated without OOMs. + + The engine will first conduct a profiling of the existing memory usage. + Then, it calculate the maximum possible number of GPU and CPU blocks + that can be allocated with the remaining free memory. + + .. tip:: + You may limit the usage of GPU memory + by adjusting the `gpu_memory_utilization` parameter. + """ + # Profile the memory usage of the model and get the maximum number of + # cache blocks that can be allocated with the remaining free memory. + torch.cuda.empty_cache() + # torch.cuda.reset_peak_memory_stats() + + # Execute a forward pass with dummy inputs to profile the memory usage + # of the model. + self.model_runner.profile_run() + + # Calculate the number of blocks that can be allocated with the + # profiled peak memory. + torch.cuda.synchronize() + free_gpu_memory, total_gpu_memory = torch.cuda.mem_get_info() + peak_memory = total_gpu_memory - free_gpu_memory + + assert peak_memory > 0, ("Error in memory profiling. This happens when the GPU memory was " + "not properly cleaned up before initializing the vLLM instance.") + + cache_block_size = self.get_cache_block_size_bytes() + + # NOTE(sgm) [VERL] use the remaining memory + num_gpu_blocks = int((free_gpu_memory * self.cache_config.gpu_memory_utilization) // cache_block_size) + # num_gpu_blocks = int((total_gpu_memory * self.cache_config.gpu_memory_utilization - peak_memory) // cache_block_size) + + num_cpu_blocks = int(self.cache_config.swap_space_bytes // cache_block_size) + num_gpu_blocks = max(num_gpu_blocks, 0) + num_cpu_blocks = max(num_cpu_blocks, 0) + if self.model_runner.lora_manager: + self.model_runner.remove_all_loras() + + # NOTE(sgm): Add for [VERL], synchronize number of blocks with all the rank + num_gpu_blocks = torch.tensor([num_gpu_blocks], device='cuda') + num_cpu_blocks = torch.tensor([num_cpu_blocks], device='cuda') + + torch.distributed.all_reduce(num_gpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group().device_group) + torch.distributed.all_reduce(num_cpu_blocks, + op=torch.distributed.ReduceOp.MIN, + group=get_tensor_model_parallel_group().device_group) + num_gpu_blocks = num_gpu_blocks.item() + num_cpu_blocks = num_cpu_blocks.item() + gc.collect() + torch.cuda.empty_cache() + return num_gpu_blocks, num_cpu_blocks + + def _init_cache_engine(self): + if self.cache_engine is None and self.gpu_cache is None: + super()._init_cache_engine() + + def free_cache_engine(self): + # ensure `enforce_eager=True` + self.cache_engine = None + self.gpu_cache = None + + # NOTE(sgm): [VERL]: adapt from _execute_model_spmd() + def execute_model(self, + execute_model_req: ExecuteModelRequest, + intermediate_tensors: Optional[IntermediateTensors] = None) -> Optional[List[SamplerOutput]]: + """ + Execute model in Single Program Multiple Data (SPMD) fashion. + All workers take the same request, prepare the input and + execute the model. + """ + assert execute_model_req is not None, ("_execute_model_spmd() requires each worker to take in an " + "ExecuteModelRequest") + worker_input: WorkerInput = self.prepare_worker_input(execute_model_req=execute_model_req) + model_input: ModelRunnerInputBase = (self.model_runner.prepare_model_input( + execute_model_req.seq_group_metadata_list)) + + # verl.worker.workerbase.WorkerBase + # swap cache + super().execute_worker(worker_input) + + # If there is no input, we don't need to execute the model. + if worker_input.num_seq_groups == 0: + return [] + + return self.model_runner.execute_model( + model_input, self.kv_cache[worker_input.virtual_engine] if self.kv_cache is not None else None, + intermediate_tensors) + + # assume the input is .state_dict() + def sync_model_weights(self, actor_weights: Dict, load_format: str): + if load_format in [LoadFormat.MEGATRON, LoadFormat.AUTO]: + load_megatron_weights(actor_weights, self.model_runner.model) + elif load_format == LoadFormat.HF: + # full model state dict without no sharding + load_hf_weights(actor_weights, self.model_runner.model) + elif load_format == LoadFormat.DTENSOR: + load_dtensor_weights(actor_weights, self.model_runner.model) + + def offload_model_weights(self) -> None: + if self.cpu_model == None: + self.cpu_model = {} + for name, params in self.model_runner.model.named_parameters(): + self.cpu_model[name] = torch.empty_like(params, device='cpu') + params.data = self.cpu_model[name] + else: + for name, params in self.model_runner.model.named_parameters(): + params.data = self.cpu_model[name] + + +def init_worker_distributed_environment( + parallel_config: ParallelConfig, + rank: int, + distributed_init_method: Optional[str] = "env://", + local_rank: int = -1, +) -> None: + """Initialize the distributed environment.""" + set_custom_all_reduce(not parallel_config.disable_custom_all_reduce) + + # NOTE(sgm) use tcp://localhost:xxxx will hang in HF setting without megatron + init_distributed_environment(parallel_config.world_size, rank, distributed_init_method, local_rank) + + ensure_model_parallel_initialized(tensor_model_parallel_size=parallel_config.tensor_parallel_size, + pipeline_model_parallel_size=parallel_config.pipeline_parallel_size) + + # TODO(sgm): check whether need this + # if pynccl_utils.is_initialized(): + # pynccl_world_size = pynccl_utils.get_world_size() + # if pynccl_world_size != parallel_config.world_size: + # raise RuntimeError( + # "pynccl is already initialized but the pynccl world " + # "size does not match parallel_config.world_size " + # f"({pynccl_world_size} vs. {parallel_config.world_size}).") + # elif parallel_config.world_size > 1: + # # NOTE(woosuk): We don't initialize pynccl process group when world size + # # is 1. + # # NOTE(kaichao): By default, pynccl is initialized for tp group. + # pynccl_utils.init_process_group( + # group=get_tensor_model_parallel_cpu_group()) + + # # Initialize a custom fast all-reduce implementation. + # if not parallel_config.disable_custom_all_reduce: + # init_custom_ar() + + # A small all_reduce for warmup. + torch.distributed.all_reduce(torch.zeros(1).cuda()) + # if pynccl_utils.is_initialized(): + # pynccl_utils.all_reduce(torch.zeros(1).cuda()) diff --git a/verl/trainer/__init__.py b/verl/trainer/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/trainer/__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/trainer/config/evaluation.yaml b/verl/trainer/config/evaluation.yaml new file mode 100644 index 000000000..0d8ccff88 --- /dev/null +++ b/verl/trainer/config/evaluation.yaml @@ -0,0 +1,6 @@ +data: + path: /tmp/math_Qwen2-7B-Instruct.parquet + prompt_key: prompt + response_key: responses + data_source_key: data_source + reward_model_key: reward_model \ No newline at end of file diff --git a/verl/trainer/config/generation.yaml b/verl/trainer/config/generation.yaml new file mode 100644 index 000000000..ed805a8c0 --- /dev/null +++ b/verl/trainer/config/generation.yaml @@ -0,0 +1,35 @@ +trainer: + nnodes: 1 + n_gpus_per_node: 8 + +data: + path: ~/data/rlhf/math/test.parquet + prompt_key: prompt + n_samples: 5 + output_path: /opt/tiger/math_Qwen2-7B-Instruct.parquet + batch_size: 128 + +model: + path: ~/models/Qwen2-7B-Instruct + external_lib: null +rollout: + name: vllm + temperature: 1.0 + top_k: 50 # 0 for hf rollout, -1 for vllm rollout + top_p: 0.7 + prompt_length: 1536 + response_length: 512 + # for vllm rollout + dtype: bfloat16 # should align with FSDP + gpu_memory_utilization: 0.5 + ignore_eos: False + micro_batch_size: 256 + enforce_eager: True + free_cache_engine: True + load_format: dummy_dtensor + tensor_model_parallel_size: 1 + max_num_batched_tokens: 8192 + max_num_seqs: 1024 + log_prob_micro_batch_size: 8 + # for hf rollout + do_sample: True \ No newline at end of file diff --git a/verl/trainer/config/ppo_megatron_trainer.yaml b/verl/trainer/config/ppo_megatron_trainer.yaml new file mode 100644 index 000000000..204849008 --- /dev/null +++ b/verl/trainer/config/ppo_megatron_trainer.yaml @@ -0,0 +1,145 @@ +data: + tokenizer: null + train_files: ~/data/rlhf/gsm8k/train.parquet + val_files: ~/data/rlhf/gsm8k/test.parquet + prompt_key: prompt + max_prompt_length: 512 + max_response_length: 512 + train_batch_size: 1024 + val_batch_size: 1312 + return_raw_input_ids: False # This should be set to true when the tokenizer between policy and rm differs + return_raw_chat: False + +actor_rollout_ref: + hybrid_engine: True + model: + path: ~/models/deepseek-llm-7b-chat + external_lib: null + override_config: {} + enable_gradient_checkpointing: False + actor: + strategy: megatron # This is for backward-compatibility + ppo_mini_batch_size: 256 + ppo_micro_batch_size: 64 + clip_ratio: 0.2 + entropy_coeff: 0.001 + ppo_epochs: 1 + shuffle: True + optim: + lr: 1e-6 + clip_grad: 1.0 + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: null # only useful for warmup with cosine + warmup_style: constant # select from constant/cosine + total_training_steps: -1 # must be override by program + megatron: + tensor_model_parallel_size: 4 + pipeline_model_parallel_size: 1 + num_layers_per_virtual_pipeline_stage: null # vpp will hang. need debug. + sequence_parallel: True + seed: 1 + load_weight: True + ref: + megatron: + tensor_model_parallel_size: 4 + pipeline_model_parallel_size: 1 + num_layers_per_virtual_pipeline_stage: null # vpp will hang. need debug. + sequence_parallel: True + seed: 1 + load_weight: True + param_offload: False + log_prob_micro_batch_size: 32 + 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} # for xperf_gpt + 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_megatron + tensor_model_parallel_size: 2 + max_num_batched_tokens: 8192 + max_num_seqs: 1024 + log_prob_micro_batch_size: 2 + # for hf rollout + do_sample: True + layer_name_map: + qkv_layer_name: qkv + gate_proj_layer_name: gate_up + +critic: + strategy: megatron + optim: + lr: 1e-5 + clip_grad: 1.0 + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: null # only useful for warmup with cosine + warmup_style: constant # select from constant/cosine + total_training_steps: -1 # must be override by program + model: + path: ~/models/deepseek-llm-7b-chat + tokenizer_path: ${actor_rollout_ref.model.path} + override_config: {} + external_lib: ${actor_rollout_ref.model.external_lib} + enable_gradient_checkpointing: False + megatron: + tensor_model_parallel_size: 4 + pipeline_model_parallel_size: 1 + num_layers_per_virtual_pipeline_stage: null # vpp will hang. need debug. + sequence_parallel: True + seed: 1 + load_weight: True + ppo_mini_batch_size: ${actor_rollout_ref.actor.ppo_mini_batch_size} + ppo_micro_batch_size: 2 + ppo_epochs: ${actor_rollout_ref.actor.ppo_epochs} + shuffle: ${actor_rollout_ref.actor.shuffle} + cliprange_value: 0.5 + kl_ctrl: + type: fixed + kl_coef: 0.001 + +reward_model: + enable: False + strategy: megatron + megatron: + tensor_model_parallel_size: 4 + pipeline_model_parallel_size: 1 + num_layers_per_virtual_pipeline_stage: null # vpp will hang. need debug. + sequence_parallel: True + seed: 1 + model: + input_tokenizer: ${actor_rollout_ref.model.path} # set this to null if the chat template is identical + path: ~/models/FsfairX-LLaMA3-RM-v0.1 + external_lib: ${actor_rollout_ref.model.external_lib} + load_weight: True + param_offload: False + micro_batch_size: 64 + max_length: null + +algorithm: + gamma: 1.0 + lam: 1.0 + adv_estimator: gae + kl_penalty: kl # how to estimate kl divergence + kl_ctrl: + type: fixed + kl_coef: 0.001 + +trainer: + total_epochs: 30 + project_name: verl_examples + experiment_name: gsm8k + logger: ['console', 'tracking'] + nnodes: 1 + n_gpus_per_node: 8 + save_freq: -1 + test_freq: 2 + critic_warmup: 0 + default_hdfs_dir: ~/experiments/gsm8k/ppo/${trainer.experiment_name} + default_local_dir: checkpoints/${trainer.project_name}/${trainer.experiment_name} diff --git a/verl/trainer/config/ppo_trainer.yaml b/verl/trainer/config/ppo_trainer.yaml new file mode 100644 index 000000000..bd6bcf221 --- /dev/null +++ b/verl/trainer/config/ppo_trainer.yaml @@ -0,0 +1,131 @@ +data: + tokenizer: null + train_files: ~/data/rlhf/gsm8k/train.parquet + val_files: ~/data/rlhf/gsm8k/test.parquet + prompt_key: prompt + max_prompt_length: 512 + max_response_length: 512 + train_batch_size: 1024 + val_batch_size: 1312 + return_raw_input_ids: False # This should be set to true when the tokenizer between policy and rm differs + return_raw_chat: False + +actor_rollout_ref: + hybrid_engine: True + model: + path: ~/models/deepseek-llm-7b-chat + external_lib: null + override_config: {} + enable_gradient_checkpointing: False + actor: + strategy: fsdp # This is for backward-compatibility + ppo_mini_batch_size: 256 + ppo_micro_batch_size: 64 + grad_clip: 1.0 + clip_ratio: 0.2 + entropy_coeff: 0.001 + ppo_epochs: 1 + shuffle: True + optim: + lr: 1e-6 + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: null # only useful for warmup with cosine + warmup_style: 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 + grad_offload: False + optimizer_offload: False + ref: + fsdp_config: + param_offload: False + wrap_policy: + # transformer_layer_cls_to_wrap: None + min_num_params: 0 + log_prob_micro_batch_size: 128 + 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: 128 + # for hf rollout + do_sample: True + +critic: + strategy: fsdp + optim: + lr: 1e-5 + lr_warmup_steps_ratio: 0. # the total steps will be injected during runtime + min_lr_ratio: null # only useful for warmup with cosine + warmup_style: constant # select from constant/cosine + total_training_steps: -1 # must be override by program + model: + path: ~/models/deepseek-llm-7b-chat + tokenizer_path: ${actor_rollout_ref.model.path} + override_config: {} + external_lib: ${actor_rollout_ref.model.external_lib} + enable_gradient_checkpointing: False + fsdp_config: + param_offload: False + grad_offload: False + optimizer_offload: False + wrap_policy: + # transformer_layer_cls_to_wrap: None + min_num_params: 0 + ppo_mini_batch_size: ${actor_rollout_ref.actor.ppo_mini_batch_size} + ppo_micro_batch_size: 64 + ppo_epochs: ${actor_rollout_ref.actor.ppo_epochs} + shuffle: ${actor_rollout_ref.actor.shuffle} + grad_clip: 1.0 + cliprange_value: 0.5 + +reward_model: + enable: False + strategy: fsdp + model: + input_tokenizer: ${actor_rollout_ref.model.path} # set this to null if the chat template is identical + path: ~/models/FsfairX-LLaMA3-RM-v0.1 + external_lib: ${actor_rollout_ref.model.external_lib} + fsdp_config: + min_num_params: 0 + param_offload: False + micro_batch_size: 64 + max_length: null + +algorithm: + gamma: 1.0 + lam: 1.0 + adv_estimator: gae + kl_penalty: kl # how to estimate kl divergence + kl_ctrl: + type: fixed + kl_coef: 0.001 + +trainer: + total_epochs: 30 + project_name: verl_examples + experiment_name: gsm8k + logger: ['console', 'tracking'] + nnodes: 1 + n_gpus_per_node: 8 + save_freq: -1 + test_freq: 2 + critic_warmup: 0 + default_hdfs_dir: ~/experiments/gsm8k/ppo/${trainer.experiment_name} + default_local_dir: checkpoints/${trainer.project_name}/${trainer.experiment_name} diff --git a/verl/trainer/config/sft_trainer.yaml b/verl/trainer/config/sft_trainer.yaml new file mode 100644 index 000000000..1bf7b6ec0 --- /dev/null +++ b/verl/trainer/config/sft_trainer.yaml @@ -0,0 +1,38 @@ +data: + train_batch_size: 256 + micro_batch_size: 16 # this is also val batch size + train_files: ~/data/gsm8k/train.parquet + val_files: ~/data/gsm8k/test.parquet + prompt_key: question + response_key: answer + max_length: 1024 + truncation: error + balance_dp_token: False + chat_template: null +model: + partial_pretrain: ~/models/gemma-1.1-7b-it + fsdp_config: + wrap_policy: + min_num_params: 0 + cpu_offload: False + offload_params: False + external_lib: null + enable_gradient_checkpointing: False + trust_remote_code: False +optim: + lr: 1e-5 + betas: [0.9, 0.95] + weight_decay: 0.01 + warmup_steps_ratio: 0.1 + clip_grad: 1.0 + +trainer: + default_local_dir: /tmp/sft_model + default_hdfs_dir: hdfs://tmp/experiments/gsm8k/gemma-1.1-7b-it/ # change the hdfs path here + resume_path: null + project_name: gsm8k-sft + experiment_name: test + total_epochs: 4 + logger: ['console'] + seed: 1 + diff --git a/verl/trainer/fsdp_sft_trainer.py b/verl/trainer/fsdp_sft_trainer.py new file mode 100644 index 000000000..43400dd28 --- /dev/null +++ b/verl/trainer/fsdp_sft_trainer.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. +""" +A lightweight one-file FSDP SFT Trainer +TODO(zhangchi.usc1992) +- Add calculation of mfu +- Add validation +""" + +import os + +os.environ['NCCL_DEBUG'] = 'WARN' +os.environ['TOKENIZERS_PARALLELISM'] = 'true' + +import logging +import functools +import re +import torch +import torch.distributed +import wandb +from torch import nn, optim +from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, MixedPrecision, ShardingStrategy, CPUOffload +from transformers import AutoTokenizer, AutoModelForCausalLM, PreTrainedModel, AutoConfig +from verl.utils.torch_functional import get_cosine_schedule_with_warmup +from tensordict import TensorDict +from torch.utils.data import DataLoader, DistributedSampler + +from verl.utils.fsdp_utils import get_fsdp_wrap_policy, init_fn, get_init_weight_context_manager +from verl.utils.dataset import SFTDataset +from verl.utils.fs import copy_local_path_from_hdfs +from verl.utils.tracking import Tracking + +from torch.distributed.device_mesh import DeviceMesh + +import verl.utils.hdfs_io as hdfs_io +from verl.utils.debug import log_gpu_memory_usage + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv('VERL_SFT_LOGGING_LEVEL', 'WARN')) + + +def extract_step(path): + match = re.search(r'global_step_(\d+)', path) + if match: + return int(match.group(1)) + return None + + +class FSDPSFTTrainer(object): + + def __init__(self, config, device_mesh: DeviceMesh): + self.config = config + self.device_mesh = device_mesh + # build tokenizer first + local_model_path = copy_local_path_from_hdfs(src=self.config.model.partial_pretrain, verbose=True) + self.tokenizer = AutoTokenizer.from_pretrained(local_model_path, + trust_remote_code=self.config.model.trust_remote_code) + + if self.config.data.chat_template is not None: + raise ValueError('Apply Chat template from config is not supported yet.') + + # normalize dp size + self._normalize_config_bsz() + + self._build_dataloader() + # build model + self._build_model_optimizer() + + # TODO: add checkpoint manager + + def _normalize_config_bsz(self): + dp_size = self.device_mesh.size() + if self.device_mesh.get_rank() == 0: + print(f'Normalize batch size by dp {dp_size}') + + assert self.config.data.train_batch_size % dp_size == 0 + assert self.config.data.micro_batch_size % dp_size == 0 + + self.config.data.train_batch_size //= dp_size + self.config.data.micro_batch_size //= dp_size + + def _build_dataloader(self): + config = self.config + # build dataset + self.train_dataset = SFTDataset(parquet_files=config.data.train_files, + tokenizer=self.tokenizer, + prompt_key=config.data.prompt_key, + response_key=config.data.response_key, + max_length=config.data.max_length, + truncation=config.data.truncation) + self.val_dataset = SFTDataset(parquet_files=config.data.val_files, + tokenizer=self.tokenizer, + prompt_key=config.data.prompt_key, + response_key=config.data.response_key, + max_length=config.data.max_length, + truncation=config.data.truncation) + + # build dataloader + rank = self.device_mesh.get_rank() + world_size = self.device_mesh.size() + self.train_sampler = DistributedSampler(self.train_dataset, + shuffle=True, + num_replicas=world_size, + rank=rank, + drop_last=True) + self.train_dataloader = DataLoader(dataset=self.train_dataset, + batch_size=config.data.train_batch_size, + sampler=self.train_sampler, + drop_last=True) + + self.val_sampler = DistributedSampler(self.val_dataset, + shuffle=True, + num_replicas=world_size, + rank=rank, + drop_last=True) + self.val_dataloader = DataLoader(dataset=self.val_dataset, + batch_size=config.data.micro_batch_size, + sampler=self.val_sampler, + drop_last=True) + + def _build_model_optimizer(self): + # TODO (zhangchi.usc1992): + # 1. support pretrain from random weights + # 2. support init directly from sharded weights + local_model_path = copy_local_path_from_hdfs(src=self.config.model.partial_pretrain, verbose=True) + + if self.config.model.get('external_lib', None) is not None: + # This is used to import external_lib into the huggingface systems + import importlib + importlib.import_module(self.config.model.external_lib) + + log_gpu_memory_usage('Before model allocation', logger=logger) + + trust_remote_code = self.config.model.trust_remote_code + # load config first + config = AutoConfig.from_pretrained(local_model_path, trust_remote_code=trust_remote_code) + + # This may be very large + init_context = get_init_weight_context_manager(use_meta_tensor=not config.tie_word_embeddings) + + with init_context(): + self.model: PreTrainedModel = AutoModelForCausalLM.from_pretrained(local_model_path, + config=config, + torch_dtype=torch.float32, + attn_implementation='flash_attention_2', + trust_remote_code=trust_remote_code) + + if self.config.model.enable_gradient_checkpointing: + self.model.gradient_checkpointing_enable(gradient_checkpointing_kwargs={'use_reentrant': False}) + + log_gpu_memory_usage('After model allocation', logger=logger) + + mixed_precision = MixedPrecision(param_dtype=torch.bfloat16, + reduce_dtype=torch.float32, + buffer_dtype=torch.float32) + + auto_wrap_policy = get_fsdp_wrap_policy(self.model, config=self.config.model.fsdp_config.wrap_policy) + if self.device_mesh.get_rank() == 0: + print(auto_wrap_policy) + + if not self.config.model.fsdp_config.cpu_offload: + cpu_offload = None + else: + cpu_offload = CPUOffload(offload_params=self.config.model.fsdp_config.offload_params) + + self.fsdp_model = FSDP(module=self.model, + auto_wrap_policy=auto_wrap_policy, + param_init_fn=init_fn, + sharding_strategy=ShardingStrategy.FULL_SHARD, + mixed_precision=mixed_precision, + device_mesh=self.device_mesh, + sync_module_states=True, + device_id=torch.cuda.current_device(), + cpu_offload=cpu_offload, + use_orig_params=False) + + log_gpu_memory_usage('After FSDP wrapping', logger=logger) + + self.optimizer = optim.AdamW(self.fsdp_model.parameters(), + lr=self.config.optim.lr, + betas=self.config.optim.betas, + weight_decay=self.config.optim.weight_decay) + + log_gpu_memory_usage('After initialize optimizer', logger=logger) + + steps_per_epoch = len(self.train_dataloader) + total_steps = steps_per_epoch * self.config.trainer.total_epochs + + if self.device_mesh.get_rank() == 0: + print( + f'Number of steps/epoch {steps_per_epoch}, number of epochs {self.config.trainer.total_epochs}, total number of steps {total_steps}' + ) + + num_warmup_steps = int(total_steps * self.config.optim.warmup_steps_ratio) + + self.lr_scheduler = get_cosine_schedule_with_warmup(optimizer=self.optimizer, + num_warmup_steps=num_warmup_steps, + num_training_steps=total_steps) + + def _compute_loss(self, batch): + loss_mask = batch.pop('loss_mask')[:, :-1].reshape(-1).cuda() + labels = batch['input_ids'][:, 1:].cuda() + + with torch.autocast(device_type='cuda', dtype=torch.bfloat16): + output = self.fsdp_model(input_ids=batch['input_ids'], + attention_mask=batch['attention_mask'], + position_ids=batch['position_ids'], + use_cache=False) # prevent model thinks it it generating + + logits = output.logits + + shift_logits = logits[..., :-1, :].contiguous() + shift_labels = labels.contiguous() + # Flatten the tokens + loss_fct = nn.CrossEntropyLoss(reduction='none') + shift_logits = shift_logits.view(-1, self.model.config.vocab_size) + shift_labels = shift_labels.view(-1) + # Enable model parallelism + shift_labels = shift_labels.to(shift_logits.device) + loss = loss_fct(shift_logits, shift_labels) + loss = loss * loss_mask + + valid_token_this_rank = torch.sum(loss_mask) + + if self.config.data.balance_dp_token: + torch.distributed.all_reduce(valid_token_this_rank) # becomes total valid tokens in all ranks + dp_size = torch.distributed.get_world_size() + else: + dp_size = 1 + + loss = torch.sum(loss) / valid_token_this_rank * dp_size # possible bugs here for dp + return loss + + def training_step(self, batch: TensorDict): + self.fsdp_model.train() + + log_gpu_memory_usage('Before optimizer zero_grad', logger=logger) + + self.optimizer.zero_grad() + + log_gpu_memory_usage('After optimizer zero_grad', logger=logger) + + micro_batches = batch.split(self.config.data.micro_batch_size) + n_micro_batches = len(micro_batches) + for micro_batch in micro_batches: + loss = self._compute_loss(batch=micro_batch) / n_micro_batches + loss.backward() + + self.fsdp_model.clip_grad_norm_(max_norm=self.config.optim.clip_grad) + + log_gpu_memory_usage('Before optimizer step', logger=logger) + + self.optimizer.step() + + log_gpu_memory_usage('After optimizer step', logger=logger) + + self.lr_scheduler.step() + + # reduce loss across dp ranks + lr = self.lr_scheduler.get_last_lr()[0] + + log_gpu_memory_usage('After offload weights', logger=logger) + + # TODO: all reduce to get accurate loss + return {'train/loss': loss.detach().item(), 'train/lr(1e-3)': lr * 1e3} + + def validation_step(self, batch: TensorDict): + self.fsdp_model.eval() + with torch.no_grad(): + loss = self._compute_loss(batch) + torch.distributed.all_reduce(loss, op=torch.distributed.ReduceOp.AVG) + return loss + + def save_checkpoint(self, step): + # save checkpoint + from torch.distributed.fsdp import FullStateDictConfig, StateDictType + cfg = FullStateDictConfig(offload_to_cpu=True, rank0_only=True) + with FSDP.state_dict_type(self.fsdp_model, StateDictType.FULL_STATE_DICT, cfg): + state_dict = self.fsdp_model.state_dict() + + path = os.path.join(self.config.trainer.default_local_dir, f'global_step_{step}') + # save huggingface model + if self.device_mesh.get_rank() == 0: + os.makedirs(path, exist_ok=True) + hdfs_io.makedirs(self.config.trainer.default_hdfs_dir) + self.model.save_pretrained(path, state_dict=state_dict) + self.tokenizer.save_pretrained(path) + hdfs_io.copy(src=path, dst=self.config.trainer.default_hdfs_dir) + torch.distributed.barrier() + + def fit(self): + rank = self.device_mesh.get_rank() + + # TODO: add a unified tracking + if rank == 0: + tracking = Tracking(project_name=self.config.trainer.project_name, + experiment_name=self.config.trainer.experiment_name, + default_backend=self.config.trainer.logger) + + global_step = 0 + + # TODO (zhangchi.usc1992) add back checkpoint manager. Currently, it blocks when uploading to hdfs. So very slow. + + for epoch in range(self.config.trainer.total_epochs): + self.train_sampler.set_epoch(epoch=epoch) + for data in self.train_dataloader: + data = TensorDict(data, batch_size=self.config.data.train_batch_size).cuda() + metric = self.training_step(data) + if rank == 0: + tracking.log(data=metric, step=global_step) + global_step += 1 + + # validation + val_losses = [] + for data in self.val_dataloader: + data = TensorDict(data, batch_size=self.config.data.micro_batch_size).cuda() + val_loss = self.validation_step(data) + val_losses.append(val_loss) + if rank == 0: + val_loss = torch.mean(torch.stack(val_losses)) + metric = {'val/loss': val_loss.detach().item()} + tracking.log(data=metric, step=global_step) + torch.distributed.barrier() + + # save checkpoint + self.save_checkpoint(step=global_step) + + +from verl.trainer.fsdp_sft_trainer import FSDPSFTTrainer +import hydra + +from torch.distributed.device_mesh import init_device_mesh + +from verl.utils.distributed import initialize_global_process_group + + +@hydra.main(config_path='config', config_name='sft_trainer', version_base=None) +def main(config): + local_rank, rank, world_size = initialize_global_process_group() + + device_mesh = init_device_mesh(device_type='cuda', mesh_shape=(world_size,), mesh_dim_names=('dp',)) + + trainer = FSDPSFTTrainer(config=config, device_mesh=device_mesh) + trainer.fit() + + +if __name__ == '__main__': + main() diff --git a/verl/trainer/main_eval.py b/verl/trainer/main_eval.py new file mode 100644 index 000000000..018bdd8fd --- /dev/null +++ b/verl/trainer/main_eval.py @@ -0,0 +1,69 @@ +# 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. +""" +Offline evaluate the performance of a generated file using reward model and ground truth verifier. +The input is a parquet file that contains N generated sequences and (optional) the ground truth. + +""" + +import hydra +from verl.utils.fs import copy_local_path_from_hdfs +from verl.utils.reward_score import math, gsm8k +import pandas as pd +import numpy as np + + +def select_reward_fn(data_source): + if data_source == 'lighteval/MATH': + return math.compute_score + else: + raise NotImplementedError + + +@hydra.main(config_path='config', config_name='evaluation', version_base=None) +def main(config): + local_path = copy_local_path_from_hdfs(config.data.path) + dataset = pd.read_parquet(local_path) + prompts = dataset[config.data.prompt_key] + responses = dataset[config.data.response_key] + data_sources = dataset[config.data.data_source_key] + reward_model_data = dataset[config.data.reward_model_key] + + passes = 0 + + total = len(dataset) + + for i in range(total): + response_lst = responses[i] + data_source = data_sources[i] + # select reward score based on data_source + prompt = prompts[i] + reward_data = reward_model_data[i] + reward_fn = select_reward_fn(data_source) + ground_truth = reward_data['ground_truth'] + score_lst = [] + for r in response_lst: + score = reward_fn(r, ground_truth) + score_lst.append(score) + + max_score = np.max(score_lst) + + if max_score == 1: + passes += 1 + + print(f'pass@5: {passes / total}') + + +if __name__ == '__main__': + main() diff --git a/verl/trainer/main_generation.py b/verl/trainer/main_generation.py new file mode 100644 index 000000000..b13e8902c --- /dev/null +++ b/verl/trainer/main_generation.py @@ -0,0 +1,136 @@ +# 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. +""" +Generate responses given a dataset of prompts +""" + +import numpy as np +import hydra +import os + +os.environ['NCCL_DEBUG'] = 'WARN' +os.environ['TOKENIZERS_PARALLELISM'] = 'true' +# os.environ['TORCH_COMPILE_DISABLE'] = '1' + +from verl.utils.model import compute_position_id_with_mask + +import torch +import pandas as pd + +from transformers import AutoTokenizer + +from verl import DataProto +from verl.utils.fs import copy_local_path_from_hdfs +from verl.trainer.ppo.workers.fsdp_workers import ActorRolloutRefWorker +from verl.utils.hdfs_io import makedirs +from single_controller.ray import RayClassWithInitArgs, RayResourcePool, RayWorkerGroup + + +@hydra.main(config_path='config', config_name='generation', version_base=None) +def main(config): + from pprint import pprint + from omegaconf import OmegaConf + pprint(OmegaConf.to_container(config, resolve=True)) # resolve=True will eval symbol values + OmegaConf.resolve(config) + local_path = copy_local_path_from_hdfs(config.model.path) + tokenizer = AutoTokenizer.from_pretrained(local_path) + if config.rollout.temperature == 0.: + assert config.data.n_samples == 1, 'When temperature=0, n_samples must be 1.' + + # read dataset. Note that the dataset should directly contain chat template format (e.g., a list of dictionary) + dataset = pd.read_parquet(config.data.path) + chat_lst = dataset[config.data.prompt_key].tolist() + + chat_lst = [chat.tolist() for chat in chat_lst] + + tokenizer.padding_side = 'left' + if tokenizer.pad_token is None: + tokenizer.pad_token = tokenizer.eos_token + + ray_cls_with_init = RayClassWithInitArgs(cls=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) + wg.init_model() + + total_samples = len(dataset) + # real_batch_size = data.batch['input_ids'].shape[0] + config_batch_size = config.data.batch_size + dp_size = wg.world_size // config.rollout.tensor_model_parallel_size + num_batch = (total_samples // config_batch_size) + 1 + output_lst = [[] for _ in range(config.data.n_samples)] + + for batch_idx in range(num_batch): + print(f'[{batch_idx+1}/{num_batch}] Start to process.') + batch_chat_lst = chat_lst[batch_idx * config_batch_size:(batch_idx + 1) * config_batch_size] + inputs = tokenizer.apply_chat_template(batch_chat_lst, + add_generation_prompt=True, + padding=True, + truncation=True, + max_length=config.rollout.prompt_length, + return_tensors='pt', + return_dict=True, + tokenize=True) + input_ids = inputs['input_ids'] + attention_mask = inputs['attention_mask'] + position_ids = compute_position_id_with_mask(attention_mask) + + batch_dict = {'input_ids': input_ids, 'attention_mask': attention_mask, 'position_ids': position_ids} + + data = DataProto.from_dict(batch_dict) + real_batch_size = data.batch['input_ids'].shape[0] + if real_batch_size % dp_size != 0: + dummy_data_size = dp_size - real_batch_size % dp_size + dummy_data = data[:dummy_data_size] + data = DataProto.concat([data, dummy_data]) + print( + f'dp_size {dp_size} is not divisible by real_batch_size {real_batch_size}, add {dummy_data_size} dummy data' + ) + + batch_size = data.batch['input_ids'].shape[0] + assert batch_size % dp_size == 0, f'batch_size {batch_size} is not divisible by dp_size {dp_size}' + + print(f'[{batch_idx+1}/{num_batch}] Start to generate.') + # START TO GENERATE FOR n_samples TIMES + for i in range(config.data.n_samples): + output = wg.generate_sequences(data) + # remove dummy data + output = output[:real_batch_size] + output_text = tokenizer.batch_decode(output.batch['input_ids'][:, -config.rollout.response_length:], + skip_special_tokens=False) + + # remove the padding + pad_token = tokenizer.pad_token + output_text_unpad = [] + for text in output_text: + output_text_unpad.append(text.replace(pad_token, '')) + + output_lst[i].extend(output_text_unpad) + + # convert output_lst from (n_samples, n_data) to (n_data, n_sampels) + output_lst = np.array(output_lst, dtype=object) + output_lst = np.transpose(output_lst, axes=(1, 0)).tolist() + + # add to the data frame + dataset[f'responses'] = output_lst + + # write to a new parquet + output_dir = os.path.dirname(config.data.output_path) + makedirs(output_dir, exist_ok=True) + dataset.to_parquet(config.data.output_path) + + return output_text + + +if __name__ == '__main__': + main() diff --git a/verl/trainer/main_ppo.py b/verl/trainer/main_ppo.py new file mode 100644 index 000000000..c1cdb2362 --- /dev/null +++ b/verl/trainer/main_ppo.py @@ -0,0 +1,186 @@ +# 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. +""" +Note that we don't combine the main with ray_trainer as ray_trainer is used by other main. +""" + +from verl import DataProto +import torch +from verl.utils.reward_score import gsm8k, math +from verl.trainer.ppo.ray_trainer import RayPPOTrainer + + +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 + + +class RewardManager(): + + def __init__(self, tokenizer, num_examine) -> None: + self.tokenizer = tokenizer + self.num_examine = num_examine # the number of batches of decoded responses to print to the console + + def __call__(self, data: DataProto): + """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 + if 'rm_scores' in data.batch.keys(): + return data.batch['rm_scores'] + + reward_tensor = torch.zeros_like(data.batch['responses'], dtype=torch.float32) + + 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 + sequences = torch.cat((valid_prompt_ids, valid_response_ids)) + sequences_str = self.tokenizer.decode(sequences) + + ground_truth = data_item.non_tensor_batch['reward_model']['ground_truth'] + + # select rm_score + data_source = data_item.non_tensor_batch['data_source'] + compute_score_fn = _select_rm_score_fn(data_source) + + score = compute_score_fn(solution_str=sequences_str, ground_truth=ground_truth) + reward_tensor[i, valid_response_length - 1] = score + + 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) + + return reward_tensor + + +import ray +import hydra + + +@hydra.main(config_path='config', config_name='ppo_trainer', version_base=None) +def main(config): + if not ray.is_initialized(): + # this is for local ray cluster + ray.init(runtime_env={'env_vars': {'TOKENIZERS_PARALLELISM': 'true', 'NCCL_DEBUG': 'WARN'}}) + + ray.get(main_task.remote(config)) + + +@ray.remote +def main_task(config): + from verl.utils.fs import copy_local_path_from_hdfs + from transformers import AutoTokenizer + + # print initial config + from pprint import pprint + from omegaconf import OmegaConf + pprint(OmegaConf.to_container(config, resolve=True)) # resolve=True will eval symbol values + OmegaConf.resolve(config) + + # download the checkpoint from hdfs + local_path = copy_local_path_from_hdfs(config.actor_rollout_ref.model.path) + + # instantiate tokenizer + tokenizer = AutoTokenizer.from_pretrained(local_path) + + # define worker classes + if config.actor_rollout_ref.actor.strategy == 'fsdp': + assert config.actor_rollout_ref.actor.strategy == config.critic.strategy + from verl.trainer.ppo.workers.fsdp_workers import ActorRolloutRefWorker, CriticWorker + from single_controller.ray import RayWorkerGroup + ray_worker_group_cls = RayWorkerGroup + + elif config.actor_rollout_ref.actor.strategy == 'megatron': + assert config.actor_rollout_ref.actor.strategy == config.critic.strategy + from verl.trainer.ppo.workers.megatron_workers import ActorRolloutRefWorker, CriticWorker + from single_controller.ray.megatron import NVMegatronRayWorkerGroup + ray_worker_group_cls = NVMegatronRayWorkerGroup + + else: + raise NotImplementedError + + from verl.trainer.ppo.ray_trainer import ResourcePoolManager, Role + + role_worker_mapping = { + Role.ActorRollout: ActorRolloutRefWorker, + Role.Critic: CriticWorker, + 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, + } + + # 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: + if config.reward_model.strategy == 'fsdp': + from verl.trainer.ppo.workers.fsdp_workers import RewardModelWorker + elif config.reward_model.strategy == 'megatron': + from verl.trainer.ppo.workers.megatron_workers import RewardModelWorker + else: + raise NotImplementedError + role_worker_mapping[Role.RewardModel] = RewardModelWorker + 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) + + 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() + + +if __name__ == '__main__': + main() diff --git a/verl/trainer/ppo/__init__.py b/verl/trainer/ppo/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/trainer/ppo/__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/trainer/ppo/actor/__init__.py b/verl/trainer/ppo/actor/__init__.py new file mode 100644 index 000000000..7a1404e17 --- /dev/null +++ b/verl/trainer/ppo/actor/__init__.py @@ -0,0 +1,18 @@ +# 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 BasePPOActor +from .dp_actor import DataParallelPPOActor + +__all__ = ["BasePPOActor", "DataParallelPPOActor"] diff --git a/verl/trainer/ppo/actor/base.py b/verl/trainer/ppo/actor/base.py new file mode 100644 index 000000000..144f0b90e --- /dev/null +++ b/verl/trainer/ppo/actor/base.py @@ -0,0 +1,66 @@ +# 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 class for Actor +""" +from abc import ABC, abstractmethod +from typing import Iterable, Dict + +from verl import DataProto +import torch + +__all__ = ['BasePPOActor'] + + +class BasePPOActor(ABC): + + def __init__(self, config): + """The base class for PPO actor + + Args: + config (DictConfig): a config passed to the PPOActor. We expect the type to be + DictConfig (https://omegaconf.readthedocs.io/), but it can be any namedtuple in general. + """ + super().__init__() + self.config = config + + @abstractmethod + def compute_log_prob(self, data: DataProto) -> torch.Tensor: + """Compute logits given a batch of data. + + Args: + data (DataProto): a batch of data represented by DataProto. It must contain key ```input_ids```, + ```attention_mask``` and ```position_ids```. + + Returns: + DataProto: a DataProto containing the key ```log_probs``` + + + """ + pass + + @abstractmethod + def update_policy(self, data: DataProto) -> Dict: + """Update the policy with an iterator of DataProto + + Args: + data (DataProto): an iterator over the DataProto that returns by + ```make_minibatch_iterator``` + + Returns: + Dict: a dictionary contains anything. Typically, it contains the statistics during updating the model + such as ```loss```, ```grad_norm```, etc,. + + """ + pass diff --git a/verl/trainer/ppo/actor/dp_actor.py b/verl/trainer/ppo/actor/dp_actor.py new file mode 100644 index 000000000..02c3328e8 --- /dev/null +++ b/verl/trainer/ppo/actor/dp_actor.py @@ -0,0 +1,167 @@ +# 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. +""" +Single Process Actor +""" +from typing import Iterable + +import torch +from torch import nn +from torch.distributed.fsdp import FullyShardedDataParallel as FSDP + +from verl import DataProto +from verl.trainer.ppo import core_algos +from verl.trainer.ppo.actor import BasePPOActor +from verl.utils.py_functional import append_to_dict +from verl.utils.torch_functional import logprobs_from_logits + +__all__ = ['DataParallelPPOActor'] + + +class DataParallelPPOActor(BasePPOActor): + + def __init__( + self, + config, + actor_module: nn.Module, + actor_optimizer: torch.optim.Optimizer = None, + ): + """When optimizer is None, it is Reference Policy""" + super().__init__(config) + self.actor_module = actor_module + self.actor_optimizer = actor_optimizer + + def _forward_micro_batch(self, micro_batch, temperature): + response_length = micro_batch['responses'].size(-1) + with torch.autocast(device_type='cuda', dtype=torch.bfloat16): + output = self.actor_module(input_ids=micro_batch['input_ids'], + attention_mask=micro_batch['attention_mask'], + position_ids=micro_batch['position_ids'], + use_cache=False) # prevent model thinks we are generating + logits = output.logits / temperature + logits = logits[:, -response_length - 1:-1] + log_probs = logprobs_from_logits(logits, micro_batch['responses']) + return logits, log_probs + + def _make_minibatch_iterator(self, data: DataProto) -> Iterable[DataProto]: + """Make minibatch iterator for updating the actor + See PPO paper for details. https://arxiv.org/abs/1707.06347 + """ + select_keys = ['responses', 'input_ids', 'attention_mask', 'position_ids', 'old_log_probs', 'advantages'] + data = data.select(batch_keys=select_keys) + return data.make_iterator(mini_batch_size=self.config.ppo_mini_batch_size, + epochs=self.config.ppo_epochs, + dataloader_kwargs={'shuffle': self.config.shuffle}) + + def _optimizer_step(self): + assert self.config.grad_clip is not None + + if isinstance(self.actor_module, FSDP): + grad_norm = self.actor_module.clip_grad_norm_(max_norm=self.config.grad_clip) + else: + grad_norm = torch.nn.utils.clip_grad_norm_(self.actor_module.parameters(), max_norm=self.config.grad_clip) + self.actor_optimizer.step() + return grad_norm + + def compute_log_prob(self, data: DataProto) -> torch.Tensor: + """Compute the log probability of the responses given input_ids, attention_mask and position_ids + + Args: + data (DataProto): a DataProto containing keys + + ``input_ids``: tensor of shape [batch_size, sequence_length]. torch.int64. Note that input_ids is the + concatenation of prompt and response. Note that ``sequence_length = prompt_length + response_length``. + + ``attention_mask``: tensor of shape [batch_size, sequence_length]. torch.int64. + + ``position_ids``: tensor of shape [batch_size, sequence_length]. torch.int64. + + ``responses``: tensor of shape [batch_size, response_length]. torch.int64. + + Returns: + torch.Tensor: the log_prob tensor + """ + # set to eval + self.actor_module.eval() + + micro_batch_size = data.meta_info['micro_batch_size'] + temperature = data.meta_info['temperature'] # temperature must be in the data.meta_info to avoid slient error + + select_keys = ['responses', 'input_ids', 'attention_mask', 'position_ids'] + batch = data.select(batch_keys=select_keys).batch + micro_batches = batch.split(micro_batch_size) + log_probs_lst = [] + for micro_batch in micro_batches: + with torch.no_grad(): + _, log_probs = self._forward_micro_batch(micro_batch, temperature=temperature) + log_probs_lst.append(log_probs) + log_probs = torch.concat(log_probs_lst, dim=0) + return log_probs + + def update_policy(self, data: DataProto): + # make sure we are in training mode + self.actor_module.train() + + assert self.config.ppo_mini_batch_size % self.config.ppo_micro_batch_size == 0 + self.gradient_accumulation = self.config.ppo_mini_batch_size // self.config.ppo_micro_batch_size + temperature = data.meta_info['temperature'] # temperature must be in the data.meta_info to avoid slient error + + dataloader = self._make_minibatch_iterator(data=data) + + metrics = {} + for batch_idx, data in enumerate(dataloader): + # split batch into micro_batches + micro_batches = data.batch.split(self.config.ppo_micro_batch_size) + + self.actor_optimizer.zero_grad() + + for data in micro_batches: + data = data.cuda() # actor device is cpu when using offload + responses = data['responses'] + response_length = responses.size(1) + attention_mask = data['attention_mask'] + response_mask = attention_mask[:, -response_length:] + old_log_prob = data['old_log_probs'] + advantages = data['advantages'] + + clip_ratio = self.config.clip_ratio + entropy_coeff = self.config.entropy_coeff + + logits, log_prob = self._forward_micro_batch(micro_batch=data, temperature=temperature) + + pg_loss, pg_clipfrac, ppo_kl = core_algos.compute_policy_loss(old_log_prob=old_log_prob, + log_prob=log_prob, + advantages=advantages, + eos_mask=response_mask, + cliprange=clip_ratio) + + entropy_loss = core_algos.compute_entropy_loss(logits, response_mask) + policy_loss = pg_loss - entropy_loss * entropy_coeff + + loss = policy_loss / self.gradient_accumulation + loss.backward() + + data = { + 'actor/entropy_loss': entropy_loss.detach().item(), + 'actor/pg_loss': pg_loss.detach().item(), + 'actor/pg_clipfrac': pg_clipfrac.detach().item(), + 'actor/ppo_kl': ppo_kl.detach().item(), + } + append_to_dict(metrics, data) + + grad_norm = self._optimizer_step() + data = {'actor/grad_norm': grad_norm.detach().item()} + append_to_dict(metrics, data) + self.actor_optimizer.zero_grad() + return metrics diff --git a/verl/trainer/ppo/actor/megatron_actor.py b/verl/trainer/ppo/actor/megatron_actor.py new file mode 100644 index 000000000..8bb31b17d --- /dev/null +++ b/verl/trainer/ppo/actor/megatron_actor.py @@ -0,0 +1,368 @@ +# 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. +""" +Megatron Actor. +In megatron actor, the differences are: +1. We only make minibatch + +Note that our model doesn't have to be `MegatronModule` because we don't share embedding in the last layer +""" + +from functools import partial +from typing import Iterable, Dict + +import torch +from torch import nn +import torch.distributed +# from megatron import get_args +from megatron.optimizer import DistributedOptimizer +from verl.utils.megatron.optimizer_config import OptimizerConfig +from megatron.core import parallel_state as mpu +from megatron.core import ModelParallelConfig +from megatron.core.pipeline_parallel import get_forward_backward_func +# from megatron.core.optimizer import DistributedOptimizer + +from omegaconf import OmegaConf +from verl.utils.megatron.tensor_parallel import vocab_parallel_compute_entropy_loss, vocab_parallel_log_probs_from_logits +from verl.utils.megatron.pipeline_parallel import (compute_transformers_input_shapes, make_batch_generator) +from verl import DataProto +from verl.trainer.ppo import core_algos +from verl.trainer.ppo.actor import BasePPOActor +from verl.utils.py_functional import append_to_dict +from verl.utils.torch_functional import logprobs_from_logits, broadcast_dict_tensor, split_dict_tensor_into_batches + +__all__ = ['MegatronPPOActor'] + + +class MegatronPPOActor(BasePPOActor): + + def __init__(self, config, model_config, megatron_config: ModelParallelConfig, actor_module: nn.ModuleList, + actor_optimizer: DistributedOptimizer, actor_optimizer_config: OptimizerConfig): + """MeagtronPPOActor class. This class implements the simple PPO logics when the model is built with Megatron. + + Args: + config (OmegaConf): the basic config that contains the hyper-parameters of PPO Actor. It must contain + + ``ppo_micro_batch_size``: minibatch size when updating ppo. + + ``ppo_mini_batch_size``: minibatch size when updating ppo using the batch data. + + ``ppo_epochs``: number of epochs to update the actor using the batch data. + + ``shuffle``: whether to shuffle the data after each ppo epoch. + + ``clip_ratio``: clip ratio of the ppo algorithm. See https://arxiv.org/abs/1707.06347. + + ``entropy_coeff``: entropy coefficient of the PPO loss. See https://arxiv.org/abs/1707.06347. + model_config (OmegaConf): model configuration. It must contains ``model_config.vocab_size`` and + ``model_config.hidden_size`` + megatron_config (OmegaConf): megatron configuration. It must contains + + ``sequence_parallel_enabled``: whether the sequence parallel is enabled. + + ``param_dtype``: the dtype of the parameters. + + ``virtual_pipeline_model_parallel_size``: virtual pipeline model parallel size. a.k.a number of chunks in each pp stage. + actor_module (nn.ModuleList): actor module is a ModuleList that contains a list of nn.Module in this pp stage. + each nn.Module in this rank holds a vpp module chunk. See https://arxiv.org/pdf/2104.04473.pdf for more details. + The actor module has some constraints to follow in order to use the updating logics implemented here + + 1. It must implement unpad_input before any computation and pad_input after all the computation. Remove padding is an + optimization that removes the padding tokens. See unpad_input and pad_input function in flash-attn + (https://github.com/Dao-AILab/flash-attention/blob/main/flash_attn/bert_padding.py). + + 2. Each pp stage must return the hidden state with the same shape [total_nnz, 1, hidden_size], + where total_nnz is the number of valid tokens in this batch. If sequence parallel is enabled, the size + of the hidden state is [total_nnz // tp, 1, hidden_size]. + actor_optimizer (DistributedOptimizer): currently, we only support DistributedOptimizer in Megatron. It implements + zero1 optimizer that shards the optimizer state across dp ranks. + + >>> def megatron_actor_model_provider(pre_process, post_process): + >>> vpp_rank = mpu.get_virtual_pipeline_model_parallel_rank() + >>> parallel_model = ParallelMistralForCausalLMRmPadPP(config=actor_model_config, + >>> megatron_config=megatron_config, + >>> pre_process=pre_process, + >>> post_process=post_process).cuda() + >>> return parallel_model + >>> from megatron.training import get_model + >>> from megatron.optimizer import get_megatron_optimizer + >>> actor_module = get_model(megatron_actor_model_provider, wrap_with_ddp=True) + >>> actor_module = nn.ModuleList(actor_module) + >>> actor_optimizer = get_megatron_optimizer(actor_module) + >>> actor = MegatronPPOActor(config=config, + >>> model_config=actor_model_config, + >>> megatron_config=megatron_config, + >>> actor_module=actor_module, + >>> actor_optimizer=actor_optimizer) + """ + super().__init__(config) + self.model_config = model_config + self.megatron_config = megatron_config + # self.megatron_args = get_args() + self.actor_module = actor_module + self.actor_optimizer: DistributedOptimizer = actor_optimizer + self.actor_optimizer_config = actor_optimizer_config + + self.optimizer_step_args = OmegaConf.create({ + 'skip_grad': None, + 'overlap_dp_param_comm': False, + 'overlap_dp_grad_comm': False, + 'gradient_accumulation_steps': 1, + 'sequence_parallel': self.megatron_config.sequence_parallel, + 'DDP_impl': 'local', + 'layernorm_allreduce_bucket_threshold': 0, + 'pipeline_model_parallel_split_rank': None, + 'reduce_grads_use_alltoall': False + }) + + def compute_log_prob(self, data: DataProto) -> torch.Tensor: + """Compute the log probability of the responses given input_ids, attention_mask and position_ids + + Args: + data (DataProto): a DataProto containing keys + + ``input_ids``: tensor of shape [batch_size, sequence_length]. torch.int64. Note that input_ids is the + concatenation of prompt and response. Note that ``sequence_length = prompt_length + response_length``. + + ``attention_mask``: tensor of shape [batch_size, sequence_length]. torch.int64. + + ``position_ids``: tensor of shape [batch_size, sequence_length]. torch.int64. + + ``responses``: tensor of shape [batch_size, response_length]. torch.int64. + + Returns: + DataProto: torch.Tensor: the log_prob tensor + """ + data.batch = data.batch.contiguous() + + def compute_logprobs_fn(output, data): + response = data['responses'] + response_length = response.size(1) + logits = output['logits'] + logits = logits[:, -response_length - 1:-1] + log_probs = vocab_parallel_log_probs_from_logits(logits, response) + return {'log_probs': log_probs} + + # We make recompute_old_log_prob by default here. + # TODO (zhangchi.usc1992): actually, this function should only return log_prob and this logic should be handled by user outside + recompute_old_log_prob = self.config.get('recompute_old_log_prob', True) + + if recompute_old_log_prob or 'old_log_probs' not in data.batch.keys(): + select_keys = ['responses', 'input_ids', 'attention_mask', 'position_ids'] + batch = data.select(batch_keys=select_keys).batch + input_ids = batch['input_ids'] + batch_size = input_ids.size(0) + response = batch['responses'] + response_length = response.size(1) + with torch.no_grad(): + output = self.forward_backward_batch(data, forward_only=True, post_process_fn=compute_logprobs_fn) + if mpu.is_pipeline_last_stage(ignore_virtual=True): + # only on last rank. It should be on every tp rank + log_probs = torch.cat([o['log_probs'] for o in output], dim=0) # (bs, seq_size) + log_probs = log_probs.to(torch.float32) + else: + log_probs = torch.empty(size=(batch_size, response_length), + dtype=torch.float32, + device=input_ids.device) + + # broadcast across pp ranks + torch.distributed.broadcast(tensor=log_probs, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group(), + async_op=False) + + # add empty cache after each compute + torch.cuda.empty_cache() + + return log_probs + + def make_minibatch_iterator(self, data: DataProto) -> Iterable[DataProto]: + """Make minibatch iterator for updating the actor + + Args: + data (DataProto): a DataProto containing keys + + ``input_ids``: tensor of shape [batch_size, sequence_length]. torch.int64, where ``sequence_length = prompt_length + response_length`` + + ``attention_mask``: tensor of shape [batch_size, sequence_length]. torch.int64 + + ``position_ids``: tensor of shape [batch_size, sequence_length]. torch.int64 + + ``responses``: tensor of shape [batch_size, response_length]. torch.int64. Note that responses = input_ids[:, -response_length:] + + ``old_log_probs``: tensor of shape [batch_size, response_length]. torch.float32. The log probability of responses. + + ``advantages``: tensor of shape [batch_size, response_length]. torch.float32. The advantages of responses. + See PPO paper for details. https://arxiv.org/abs/1707.06347 + + Returns: + + """ + select_keys = ['responses', 'input_ids', 'attention_mask', 'position_ids', 'old_log_probs', 'advantages'] + data = data.select(batch_keys=select_keys) + return data.make_iterator(mini_batch_size=self.config.ppo_mini_batch_size, + epochs=self.config.ppo_epochs, + dataloader_kwargs={'shuffle': self.config.shuffle}) + + def forward_backward_batch(self, data: DataProto, forward_only=False, post_process_fn=None): + """ + We assume: + - The model takes input: (input_ids, attention_mask, position_ids). No rmpad for the input + - The communication shape is (total_nnz_pad_to_sp // tp_size, 1, hidden_size) if sequence parallel is enabled + """ + # broadcast from last pp rank to all other pp ranks + # TODO: actually, we just need to control the sampling order. + broadcast_dict_tensor(data.batch, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group()) + # split into micro-batches + data.batch['attention_mask'] = data.batch['attention_mask'].to(bool) + + if data.meta_info.get('micro_batch_size', None) is not None: + batch_size = data.meta_info['micro_batch_size'] + else: + batch_size = self.config.ppo_micro_batch_size + batches = split_dict_tensor_into_batches(data.batch, batch_size=batch_size) + # compute input shapes for pp stages + input_shapes = compute_transformers_input_shapes( + batches, + meta_info={ + 'sequence_parallel': self.megatron_config.sequence_parallel, + 'hidden_size': self.model_config.hidden_size + }) + n_micro_batch = len(batches) + seq_len = batches[0]['input_ids'].shape[1] + + forward_backward_func = get_forward_backward_func() + + def loss_func(output, data, meta_info): + if forward_only: + if post_process_fn is None: + return 1.0, {'logits': output.logits} + else: + return 1.0, post_process_fn(output, data) + + responses = data['responses'] + response_length = responses.size(1) + attention_mask = data['attention_mask'] + response_mask = attention_mask[:, -response_length:] + old_log_prob = data['old_log_probs'] + advantages = data['advantages'] + + clip_ratio = meta_info['clip_ratio'] + entropy_coeff = meta_info['entropy_coeff'] + + # compute policy loss + logits = output.logits + logits = logits[:, -response_length - 1:-1] + log_prob = vocab_parallel_log_probs_from_logits(logits, responses) + pg_loss, pg_clipfrac, ppo_kl = core_algos.compute_policy_loss(old_log_prob=old_log_prob, + log_prob=log_prob, + advantages=advantages, + eos_mask=response_mask, + cliprange=clip_ratio) + entropy_loss = vocab_parallel_compute_entropy_loss(logits, eos_mask=response_mask) + policy_loss = pg_loss - entropy_loss * entropy_coeff + # return loss and stats + stats = { + 'actor/entropy_loss': entropy_loss.detach().item(), + 'actor/pg_loss': pg_loss.detach().item(), + 'actor/pg_clipfrac': pg_clipfrac.detach().item(), + 'actor/ppo_kl': ppo_kl.detach().item() + } + return policy_loss, stats + + def forward_step(batch_iter, model): + batch = next(batch_iter) + input_ids = batch['input_ids'] + attention_mask = batch['attention_mask'] + position_ids = batch['position_ids'] + output = model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids) + if forward_only: + meta_info = None + else: + meta_info = {'clip_ratio': self.config.clip_ratio, 'entropy_coeff': self.config.entropy_coeff} + return output, partial(loss_func, data=batch, meta_info=meta_info) + + # batch should be a list of batches inside micro-batches + batch_generator = make_batch_generator(batches, vpp_size=len(self.actor_module)) + + # TODO: we may use the new schedule instead + # for flash-attn: (seq_len, batch_size, hidden_size) = (mbs*seq_len, 1, hidden_size) + if mpu.get_pipeline_model_parallel_world_size() > 1: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.actor_module, + num_microbatches=n_micro_batch, + input_shapes=input_shapes, # must set for flash-attn sequence packing + seq_length=batch_size * seq_len, # no use when input_shapes was set + hidden_size=self.model_config.hidden_size, # no use when input_shapes was set + micro_batch_size=1, # no use when input_shapes was set + forward_only=forward_only, + ) + else: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.actor_module, + num_microbatches=n_micro_batch, + seq_length=batch_size * seq_len, # in use for pp = 1 + hidden_size=self.model_config.hidden_size, # in use for pp = 1 + micro_batch_size=1, # in use for pp = 1 + forward_only=forward_only, + ) + # loss_reduces contains the stats returned from loss_func + return losses_reduced + + def update_policy(self, dataloader: Iterable[DataProto]) -> Dict: + """Update the policy with an iterator of DataProto + + Args: + dataloader (Iterable[DataProto]): an iterator over the DataProto that returns by ``make_minibatch_iterator`` + The keys of each data batch is described in the make_minibatch_iterator. + + Returns: + Dict: a dictionary containing the statistics. Note that the statistics are only valid in the last pp stage + and users have to combine the output in each dp rank manually. + + """ + metrics = {} + for data in dataloader: + # data = data.batch.to(self.actor_module.device) + self.actor_optimizer.zero_grad() + # use use_contiguous_buffers_in_local_ddp and no overlap_dp_param_comm + for chunk in self.actor_module: + # if use distributed optimizer, zero grad buffer will be handled by optimizer + chunk.zero_grad_buffer(zero_buffer=(not self.actor_optimizer_config.use_distributed_optimizer)) + + metric_micro_batch = self.forward_backward_batch(data) + for metric in metric_micro_batch: + append_to_dict(metrics, metric) # append the metric from this micro-batch to global metrics. + + update_successful, grad_norm, num_zeros_in_grad = self.actor_optimizer.step( + self.megatron_config, self.megatron_config.timers) + if update_successful: + # allgather already execute in optimizer.step in new megatron + pass + else: + raise NotImplementedError + + for metric in metric_micro_batch: + append_to_dict(metrics, metric) # append the metric from this micro-batch to global metrics. + + # add empty cache after each compute + torch.cuda.empty_cache() + + return metrics diff --git a/verl/trainer/ppo/core_algos.py b/verl/trainer/ppo/core_algos.py new file mode 100644 index 000000000..d8594399c --- /dev/null +++ b/verl/trainer/ppo/core_algos.py @@ -0,0 +1,216 @@ +# Copyright 2022 The HuggingFace Team. 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. +""" +Core functions to implement PPO algorithms. +The function implemented in this file should be used by trainer with different distributed strategies to +implement PPO +""" + +import numpy as np +import torch + +import verl.utils.torch_functional as verl_F + + +class AdaptiveKLController: + """ + Adaptive KL controller described in the paper: + https://arxiv.org/pdf/1909.08593.pdf + """ + + def __init__(self, init_kl_coef, target_kl, horizon): + self.value = init_kl_coef + self.target = target_kl + self.horizon = horizon + + def update(self, current_kl, n_steps): + target = self.target + proportional_error = np.clip(current_kl / target - 1, -0.2, 0.2) + mult = 1 + proportional_error * n_steps / self.horizon + self.value *= mult + + +class FixedKLController: + """Fixed KL controller.""" + + def __init__(self, kl_coef): + self.value = kl_coef + + def update(self, current_kl, n_steps): + pass + + +def get_kl_controller(config): + if config.critic.kl_ctrl.type == 'fixed': + kl_ctrl = FixedKLController(kl_coef=config.critic.kl_ctrl.kl_coef) + elif config.critic.kl_ctrl.type == 'adaptive': + assert config.kl_ctrl.horizon > 0, f'horizon must be larger than 0. Got {config.critic.kl_ctrl.horizon}' + kl_ctrl = AdaptiveKLController(init_kl_coef=config.critic.kl_ctrl.kl_coef, + target_kl=config.critic.kl_ctrl.target_kl, + horizon=config.critic.kl_ctrl.horizon) + else: + raise ValueError('Unknown kl_ctrl type') + + return kl_ctrl + + +def compute_gae_advantage_return(token_level_rewards: torch.Tensor, values: torch.Tensor, eos_mask: torch.Tensor, + gamma: torch.Tensor, lam: torch.Tensor): + """Adapted from https://github.com/huggingface/trl/blob/main/trl/trainer/ppo_trainer.py + + Args: + token_level_rewards: `(torch.Tensor)` + shape: (bs, response_length) + values: `(torch.Tensor)` + shape: (bs, response_length) + eos_mask: `(torch.Tensor)` + shape: (bs, response_length). [EOS] mask. The token after [EOS] have mask zero. + gamma: `(float)` + discounted factor used in RL + lam: `(float)` + lambda value when computing Generalized Advantage Estimation (https://arxiv.org/abs/1506.02438) + + Returns: + advantages: `(torch.Tensor)` + shape: (bs, response_length) + Returns: `(torch.Tensor)` + shape: (bs, response_length) + + """ + with torch.no_grad(): + lastgaelam = 0 + advantages_reversed = [] + gen_len = token_level_rewards.shape[-1] + + for t in reversed(range(gen_len)): + nextvalues = values[:, t + 1] if t < gen_len - 1 else 0.0 + delta = token_level_rewards[:, t] + gamma * nextvalues - values[:, t] + lastgaelam = delta + gamma * lam * lastgaelam + advantages_reversed.append(lastgaelam) + advantages = torch.stack(advantages_reversed[::-1], dim=1) + + returns = advantages + values + advantages = verl_F.masked_whiten(advantages, eos_mask) + return advantages, returns + + +def compute_rewards(token_level_scores, old_log_prob, ref_log_prob, kl_ratio): + kl = old_log_prob - ref_log_prob + return token_level_scores - kl * kl_ratio + + +def compute_policy_loss(old_log_prob, log_prob, advantages, eos_mask, cliprange): + """Adapted from https://github.com/huggingface/trl/blob/main/trl/trainer/ppo_trainer.py#L1122 + + Args: + old_log_prob: `(torch.Tensor)` + shape: (bs, response_length) + log_prob: `(torch.Tensor)` + shape: (bs, response_length) + advantages: `(torch.Tensor)` + shape: (bs, response_length) + eos_mask: `(torch.Tensor)` + shape: (bs, response_length) + cliprange: (float) + The clip range used in PPO. See https://arxiv.org/abs/1707.06347 + + Returns: + pg_loss: `a scalar torch.Tensor` + policy gradient loss computed via PPO + pg_clipfrac: (float) + a float number indicating the fraction of policy gradient loss being clipped + + """ + negative_approx_kl = log_prob - old_log_prob + ratio = torch.exp(negative_approx_kl) + ppo_kl = verl_F.masked_mean(-negative_approx_kl, eos_mask) + + pg_losses = -advantages * ratio + pg_losses2 = -advantages * torch.clamp(ratio, 1.0 - cliprange, 1.0 + cliprange) + + pg_loss = verl_F.masked_mean(torch.max(pg_losses, pg_losses2), eos_mask) + pg_clipfrac = verl_F.masked_mean(torch.gt(pg_losses2, pg_losses).float(), eos_mask) + return pg_loss, pg_clipfrac, ppo_kl + + +def compute_entropy_loss(logits, eos_mask): + """Compute Categorical entropy loss + + Args: + logits: `(torch.Tensor)` + shape: (bs, response_length, vocab_size) + eos_mask: `(torch.Tensor)` + shape: (bs, response_length) + + Returns: + entropy: a scalar torch.Tensor + + """ + # compute entropy + entropy = verl_F.entropy_from_logits(logits) # (bs, response_len) + entropy_loss = verl_F.masked_mean(entropy, mask=eos_mask) + return entropy_loss + + +def compute_value_loss(vpreds, returns, values, eos_mask, cliprange_value): + """Compute the value loss. Copied from https://github.com/huggingface/trl/blob/main/trl/trainer/ppo_trainer.py#L1151 + + Args: + vpreds (`torch.FloatTensor`): + Predicted values of the value head, shape (`batch_size`, `response_length`) + values (`torch.FloatTensor`): + Old values of value head, shape (`batch_size`, `response_length`) + returns: (`torch.FloatTensor`): + Ground truth returns, shape (`batch_size`, `response_length`) + + Returns: + vf_loss: a scalar (`torch.FloatTensor`): + value function loss + vf_clipfrac: a float + The ratio of vf being clipped + + """ + vpredclipped = verl_F.clip_by_value(vpreds, values - cliprange_value, values + cliprange_value) + vf_losses1 = (vpreds - returns)**2 + vf_losses2 = (vpredclipped - returns)**2 + vf_loss = 0.5 * verl_F.masked_mean(torch.max(vf_losses1, vf_losses2), eos_mask) + vf_clipfrac = verl_F.masked_mean(torch.gt(vf_losses2, vf_losses1).float(), eos_mask) + return vf_loss, vf_clipfrac + + +def kl_penalty(logprob: torch.FloatTensor, ref_logprob: torch.FloatTensor, kl_penalty) -> torch.FloatTensor: + """Compute KL divergence given logprob and ref_logprob. + Copied from https://github.com/huggingface/trl/blob/main/trl/trainer/ppo_trainer.py#L1104 + + Args: + logprob: + ref_logprob: + + Returns: + + """ + if kl_penalty == "kl": + return logprob - ref_logprob + + if kl_penalty == "abs": + return (logprob - ref_logprob).abs() + + if kl_penalty == "mse": + return 0.5 * (logprob - ref_logprob).square() + + if kl_penalty == "full": + # so, here logprob and ref_logprob should contain the logits for every token in vocabulary + raise NotImplementedError + + raise NotImplementedError diff --git a/verl/trainer/ppo/critic/__init__.py b/verl/trainer/ppo/critic/__init__.py new file mode 100644 index 000000000..80808f106 --- /dev/null +++ b/verl/trainer/ppo/critic/__init__.py @@ -0,0 +1,18 @@ +# 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 BasePPOCritic +from .dp_critic import DataParallelPPOCritic + +__all__ = ["BasePPOCritic", "DataParallelPPOCritic"] diff --git a/verl/trainer/ppo/critic/base.py b/verl/trainer/ppo/critic/base.py new file mode 100644 index 000000000..9d1055df4 --- /dev/null +++ b/verl/trainer/ppo/critic/base.py @@ -0,0 +1,40 @@ +# 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. +""" +Base class for a critic +""" +from abc import ABC, abstractmethod + +import torch + +from verl import DataProto + +__all__ = ['BasePPOCritic'] + + +class BasePPOCritic(ABC): + + def __init__(self, config): + super().__init__() + self.config = config + + @abstractmethod + def compute_values(self, data: DataProto) -> torch.Tensor: + """Compute values""" + pass + + @abstractmethod + def update_critic(self, data: DataProto): + """Update the critic""" + pass diff --git a/verl/trainer/ppo/critic/dp_critic.py b/verl/trainer/ppo/critic/dp_critic.py new file mode 100644 index 000000000..7c7788a00 --- /dev/null +++ b/verl/trainer/ppo/critic/dp_critic.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. +""" +Implement a multiprocess PPOCritic +""" + +from typing import Iterable + +import torch +import torch.distributed +from torch import nn, optim + +from torch.distributed.fsdp import FullyShardedDataParallel as FSDP + +from verl import DataProto +from verl.trainer.ppo import core_algos +from verl.trainer.ppo.critic import BasePPOCritic +from verl.utils.py_functional import append_to_dict +from verl.utils.torch_functional import masked_mean + +from flash_attn.bert_padding import pad_input, unpad_input + +__all__ = ['DataParallelPPOCritic'] + + +class DataParallelPPOCritic(BasePPOCritic): + + def __init__(self, config, critic_module: nn.Module, critic_optimizer: optim.Optimizer): + super().__init__(config=config) + self.critic_module = critic_module + self.critic_optimizer = critic_optimizer + + assert self.config.ppo_mini_batch_size % self.config.ppo_micro_batch_size == 0 + self.gradient_accumulation = self.config.ppo_mini_batch_size // self.config.ppo_micro_batch_size + + def _forward_micro_batch(self, micro_batch): + response_length = micro_batch['responses'].size(-1) + with torch.autocast(device_type='cuda', dtype=torch.bfloat16): + output = self.critic_module(input_ids=micro_batch['input_ids'], + attention_mask=micro_batch['attention_mask'], + position_ids=micro_batch['position_ids'], + use_cache=False) # prevent model thinks we are generating + values = output.logits + values = values[:, -response_length - 1:-1] + return values + + def _make_minibatch_iterator(self, data: DataProto) -> Iterable[DataProto]: + select_keys = ['input_ids', 'responses', 'attention_mask', 'position_ids', 'values', 'returns'] + data = data.select(batch_keys=select_keys) + return data.make_iterator(mini_batch_size=self.config.ppo_mini_batch_size, + epochs=self.config.ppo_epochs, + dataloader_kwargs={'shuffle': self.config.shuffle}) + + def _optimizer_step(self): + assert self.config.grad_clip is not None + + if isinstance(self.critic_module, FSDP): + grad_norm = self.critic_module.clip_grad_norm_(self.config.grad_clip) + else: + grad_norm = torch.nn.utils.clip_grad_norm_(self.critic_module.parameters(), max_norm=self.config.grad_clip) + self.critic_optimizer.step() + return grad_norm + + def compute_values(self, data: DataProto) -> torch.Tensor: + micro_batch_size = data.meta_info['micro_batch_size'] + select_keys = ['responses', 'input_ids', 'attention_mask', 'position_ids'] + batch = data.select(batch_keys=select_keys).batch + micro_batches = batch.split(micro_batch_size) + values_lst = [] + for micro_batch in micro_batches: + with torch.no_grad(): + values = self._forward_micro_batch(micro_batch) + values_lst.append(values) + values = torch.concat(values_lst, dim=0) + return values + + def update_critic(self, data: DataProto): + metrics = {} + + dataloader = self._make_minibatch_iterator(data) + + for batch_idx, data in enumerate(dataloader): + # split batch into micro_batches + micro_batches = data.batch.split(self.config.ppo_micro_batch_size) + self.critic_optimizer.zero_grad() + + for data in micro_batches: + data = data.cuda() # critic device is cpu when using offload + input_ids = data['input_ids'] + responses = data['responses'] + attention_mask = data['attention_mask'] + position_ids = data['position_ids'] + values = data['values'] + returns = data['returns'] + response_length = responses.size(1) + + eos_mask = attention_mask[:, -response_length - 1:-1] + + vpreds = self._forward_micro_batch(data) + + # assert not torch.any(torch.isnan(vpreds)).item() + + vf_loss, vf_clipfrac = core_algos.compute_value_loss(vpreds=vpreds, + values=values, + returns=returns, + eos_mask=eos_mask, + cliprange_value=self.config.cliprange_value) + loss = vf_loss / self.gradient_accumulation + loss.backward() + + data = { + 'critic/vf_loss': vf_loss.detach().item(), + 'critic/vf_clipfrac': vf_clipfrac.detach().item(), + 'critic/vpred_mean': masked_mean(vpreds, eos_mask).detach().item(), + } + + append_to_dict(metrics, data) + + grad_norm = self._optimizer_step() + data = {'critic/grad_norm': grad_norm.detach().item()} + append_to_dict(metrics, data) + self.critic_optimizer.zero_grad() + return metrics diff --git a/verl/trainer/ppo/critic/megatron_critic.py b/verl/trainer/ppo/critic/megatron_critic.py new file mode 100644 index 000000000..17c3e18cc --- /dev/null +++ b/verl/trainer/ppo/critic/megatron_critic.py @@ -0,0 +1,297 @@ +# 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. +""" +Implement a multiprocess PPOCritic +""" + +from functools import partial +from typing import Iterable + +import torch +import torch.distributed +from omegaconf import OmegaConf +from torch import nn + +from verl import DataProto +from verl.trainer.ppo import core_algos +from verl.trainer.ppo.critic import BasePPOCritic +from verl.utils.megatron.pipeline_parallel import (compute_transformers_input_shapes, make_batch_generator) +from verl.utils.py_functional import append_to_dict +from verl.utils.torch_dtypes import PrecisionType +from verl.utils.torch_functional import masked_mean, broadcast_dict_tensor, split_dict_tensor_into_batches +from verl.utils.megatron import sequence_parallel as sp_utils +from verl.utils.megatron.optimizer_config import OptimizerConfig + +from megatron.optimizer import DistributedOptimizer +from megatron.core import parallel_state as mpu +from megatron.core.pipeline_parallel import get_forward_backward_func + + +class MegatronPPOCritic(BasePPOCritic): + + def __init__(self, config, model_config, megatron_config, critic_module: nn.ModuleList, + critic_optimizer: DistributedOptimizer, critic_optimizer_config: OptimizerConfig): + super().__init__(config=config) + + self.model_config = model_config + self.megatron_config = megatron_config + + self.critic_module = critic_module + self.critic_optimizer = critic_optimizer + self.critic_optimizer_config = critic_optimizer_config + + # we create a separate nametuple for optimizer step so that global args won't affect it. + self.optimizer_step_args = OmegaConf.create({ + 'skip_grad': None, + 'overlap_dp_param_comm': False, + 'overlap_dp_grad_comm': False, + 'gradient_accumulation_steps': 1, + 'sequence_parallel': self.megatron_config.sequence_parallel, + 'DDP_impl': 'local', + 'layernorm_allreduce_bucket_threshold': 0, + 'pipeline_model_parallel_split_rank': None, + 'reduce_grads_use_alltoall': False + }) + + if self.config.kl_ctrl.type == 'fixed': + self.kl_ctrl = core_algos.FixedKLController(kl_coef=self.config.kl_ctrl.kl_coef) + elif self.config.kl_ctrl.type == 'adaptive': + assert self.config.kl_ctrl.horizon > 0, f'horizon must be larger than 0. Got {self.config.kl_ctrl.horizon}' + self.kl_ctrl = core_algos.AdaptiveKLController(init_kl_coef=self.config.kl_ctrl.kl_coef, + target_kl=self.config.kl_ctrl.target_kl, + horizon=self.config.kl_ctrl.horizon) + else: + raise NotImplementedError + + def compute_values(self, data: DataProto) -> DataProto: + # data.batch = data.batch.to(self.critic_module.module.device) + responses = data.batch['responses'] + attention_mask = data.batch['attention_mask'] + response_length = responses.size(1) + with torch.no_grad(): + output = self.forward_backward_batch(data=data, forward_only=True) + if mpu.is_pipeline_last_stage(ignore_virtual=True): + # only on last rank. It should be on every tp rank + values = torch.cat([o['vpreds'] for o in output], dim=0) # (bs, seq_size, vocal_size) + values = values.to(torch.float32) + else: + values = torch.empty_like(attention_mask, dtype=torch.float32) + + # each tp ranks should contain the same value + values = values * attention_mask + values = values[:, -response_length - 1:-1] + values = values.contiguous() + + # sync among pp ranks + torch.distributed.broadcast(tensor=values, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group()) + + # add empty cache after each compute + torch.cuda.empty_cache() + + return values + + def compute_advantages(self, data: DataProto) -> DataProto: + # data.batch = data.batch.to(self.critic_module.device) + # TODO: in general, we should compute reward of ref_log_prob here + responses = data.batch['responses'] + response_length = responses.size(1) + token_level_rewards = data.batch['token_level_rewards'] + batch_size = data.batch.batch_size[0] + dp_size = mpu.get_data_parallel_world_size() + attention_mask = data.batch['attention_mask'] + eos_mask = attention_mask[:, -response_length:] + + # compute kl between ref_policy and current policy + if 'ref_log_prob' in data.batch.keys(): + kld = core_algos.kl_penalty(data.batch['old_log_probs'], + data.batch['ref_log_prob'], + kl_penalty=self.config.kl_ctrl.kl_penalty_type) # (batch_size, response_length) + kld = kld * eos_mask + beta = self.kl_ctrl.value + rewards = token_level_rewards - beta * kld + + # per token kld + current_kl = masked_mean(kld, mask=eos_mask).item() + # according to https://github.com/huggingface/trl/blob/951ca1841f29114b969b57b26c7d3e80a39f75a0/trl/trainer/ppo_trainer.py#L837 + self.kl_ctrl.update(current_kl=current_kl, n_steps=batch_size * dp_size) + else: + beta = 0 + current_kl = 0 + rewards = token_level_rewards + + values = data.batch['values'] + + gamma = self.config.gamma + lam = self.config.lam + + advantages, returns = core_algos.compute_gae_advantage_return(token_level_rewards=rewards, + values=values, + eos_mask=eos_mask, + gamma=gamma, + lam=lam) + data.batch['advantages'] = advantages + data.batch['returns'] = returns + + sequence_reward = torch.sum(token_level_rewards, dim=-1) + + metrics = { + 'critic/rewards/mean': torch.mean(sequence_reward).detach().item(), + 'critic/rewards/max': torch.max(sequence_reward[eos_mask]).detach().item(), + 'critic/rewards/min': torch.min(sequence_reward[eos_mask]).detach().item(), + 'critic/advantages/mean': masked_mean(advantages, eos_mask).detach().item(), + 'critic/advantages/max': torch.max(advantages[eos_mask]).detach().item(), + 'critic/advantages/min': torch.min(advantages[eos_mask]).detach().item(), + 'critic/returns/mean': masked_mean(returns, eos_mask).detach().item(), + 'critic/returns/max': torch.max(returns[eos_mask]).detach().item(), + 'critic/returns/min': torch.min(returns[eos_mask]).detach().item(), + 'critic/values/mean': masked_mean(values, eos_mask).detach().item(), + 'critic/values/max': torch.max(values[eos_mask]).detach().item(), + 'critic/values/min': torch.min(values[eos_mask]).detach().item(), + 'critic/kld': current_kl, + 'critic/kl_coef': beta, + } + + data.meta_info['metrics'] = metrics + + # add empty cache after each compute + torch.cuda.empty_cache() + + return data + + def make_minibatch_iterator(self, data: DataProto) -> Iterable[DataProto]: + select_keys = ['input_ids', 'responses', 'attention_mask', 'position_ids', 'values', 'returns'] + data = data.select(batch_keys=select_keys) + return data.make_iterator(mini_batch_size=self.config.ppo_mini_batch_size, + epochs=self.config.ppo_epochs, + dataloader_kwargs={'shuffle': self.config.shuffle}) + + def forward_backward_batch(self, data: DataProto, forward_only=False): + # broadcast from last pp rank to all other pp ranks + data.batch = data.batch.contiguous() + broadcast_dict_tensor(data.batch, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group()) + # split into micro-batches + data.batch['attention_mask'] = data.batch['attention_mask'].to(bool) + batches = split_dict_tensor_into_batches(data.batch, batch_size=self.config.ppo_micro_batch_size) + n_micro_batch = len(batches) + seq_len = batches[0]['input_ids'].shape[1] + + # compute input shapes for pp stages + input_shapes = compute_transformers_input_shapes( + batches, + meta_info={ + 'sequence_parallel': self.megatron_config.sequence_parallel, + 'hidden_size': self.model_config.hidden_size + }) + + forward_backward_func = get_forward_backward_func() + + def loss_func(output, data, meta_info): + if forward_only: + return 1.0, {'vpreds': output.logits} + + responses = data['responses'] + attention_mask = data['attention_mask'] + values = data['values'] + returns = data['returns'] + response_length = responses.size(1) + + eos_mask = attention_mask[:, -response_length:] + + cliprange_value = self.config.cliprange_value + + vpreds = output.logits # (bs, sequence_length) + vpreds = vpreds[:, -response_length - 1:-1] + + vf_loss, vf_clipfrac = core_algos.compute_value_loss(vpreds=vpreds, + values=values, + returns=returns, + eos_mask=eos_mask, + cliprange_value=cliprange_value) + stats = { + 'critic/vf_loss': vf_loss.detach().item(), + 'critic/vf_clipfrac': vf_clipfrac.detach().item(), + 'critic/vpred_mean': masked_mean(vpreds, eos_mask).detach().item(), + } + + return vf_loss, stats + + def forward_step(batch_iter, model): + batch = next(batch_iter) + input_ids = batch['input_ids'] + attention_mask = batch['attention_mask'] + position_ids = batch['position_ids'] + output = model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids) + return output, partial(loss_func, data=batch, meta_info={}) + + # batch should be a list of batches inside micro-batches + batch_generator = make_batch_generator(batches, vpp_size=len(self.critic_module)) + + # TODO: we may use the new schedule instead + # for flash-attn: (seq_len, batch_size, hidden_size) = (mbs*seq_len, 1, hidden_size) + if mpu.get_pipeline_model_parallel_world_size() > 1: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.critic_module, + num_microbatches=n_micro_batch, + input_shapes=input_shapes, # must set for flash-attn sequence packing + seq_length=self.config.ppo_micro_batch_size * seq_len, # no use when input_shapes was set + hidden_size=self.model_config.hidden_size, # no use when input_shapes was set + micro_batch_size=1, # no use when input_shapes was set + forward_only=forward_only, + ) + else: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.critic_module, + num_microbatches=n_micro_batch, + seq_length=self.config.ppo_micro_batch_size * seq_len, # in use for pp = 1 + hidden_size=self.model_config.hidden_size, # in use for pp = 1 + micro_batch_size=1, # in use for pp = 1 + forward_only=forward_only, + ) + # loss_reduces contains the stats returned from loss_func + return losses_reduced + + def update_critic(self, dataloader: Iterable[DataProto]): + metrics = {} + + for data in dataloader: + # data = data.batch.to(self.critic_module.device) + self.critic_optimizer.zero_grad() + # use use_contiguous_buffers_in_local_ddp and no overlap_dp_param_comm + for chunk in self.critic_module: + chunk.zero_grad_buffer(zero_buffer=(not self.critic_optimizer_config.use_distributed_optimizer)) + + metric_micro_batch = self.forward_backward_batch(data) + + update_successful, grad_norm, num_zeros_in_grad = self.critic_optimizer.step( + self.megatron_config, self.megatron_config.timers) + if update_successful: + # allgather already execute in optimizer.step in new megatron + pass + else: + raise NotImplementedError + + for metric in metric_micro_batch: + append_to_dict(metrics, metric) # append the metric from this micro-batch to global metrics. + + # add empty cache after each compute + torch.cuda.empty_cache() + return metrics diff --git a/verl/trainer/ppo/hybrid_engine/__init__.py b/verl/trainer/ppo/hybrid_engine/__init__.py new file mode 100644 index 000000000..786380977 --- /dev/null +++ b/verl/trainer/ppo/hybrid_engine/__init__.py @@ -0,0 +1,32 @@ +# 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.import_utils import is_vllm_available, is_megatron_core_available + +from .base import BaseShardingManager + +if is_megatron_core_available() and is_vllm_available(): + from .megatron_vllm import AllGatherPPModel, MegatronVLLMShardingManager +elif AllGatherPPModel is not None: + pass +else: + AllGatherPPModel = None + MegatronVLLMShardingManager = None + +if is_vllm_available(): + from .fsdp_vllm import FSDPVLLMShardingManager +else: + FSDPVLLMShardingManager = None diff --git a/verl/trainer/ppo/hybrid_engine/base.py b/verl/trainer/ppo/hybrid_engine/base.py new file mode 100644 index 000000000..d8717890f --- /dev/null +++ b/verl/trainer/ppo/hybrid_engine/base.py @@ -0,0 +1,33 @@ +# 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. +""" +Sharding manager to implement HybridEngine +""" + +from verl import DataProto + + +class BaseShardingManager: + + 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 diff --git a/verl/trainer/ppo/hybrid_engine/fsdp_vllm.py b/verl/trainer/ppo/hybrid_engine/fsdp_vllm.py new file mode 100644 index 000000000..5c65b7fdf --- /dev/null +++ b/verl/trainer/ppo/hybrid_engine/fsdp_vllm.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 os +import logging +import torch +from torch.distributed.fsdp.fully_sharded_data_parallel import FullyShardedDataParallel as FSDP +from torch.distributed.fsdp.api import ShardingStrategy, ShardedStateDictConfig, StateDictType + +from verl.third_party.vllm import LLM +from verl.third_party.vllm import parallel_state as vllm_ps +from verl import DataProto +from verl.utils.torch_functional import (broadcast_dict_tensor, allgather_dict_tensors) +from verl.utils.debug import log_gpu_memory_usage + +from .base import BaseShardingManager + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv('VERL_PPO_LOGGING_LEVEL', 'WARN')) + + +class FSDPVLLMShardingManager(BaseShardingManager): + + def __init__(self, module: FSDP, inference_engine: LLM, model_config, full_params: bool = False): + self.module = module + self.inference_engine = inference_engine + self.model_config = model_config + + # Full params + self.full_params = full_params + if full_params: + FSDP.set_state_dict_type(self.module, + state_dict_type=StateDictType.FULL_STATE_DICT, + state_dict_config=ShardedStateDictConfig()) + else: + FSDP.set_state_dict_type(self.module, + state_dict_type=StateDictType.SHARDED_STATE_DICT, + state_dict_config=ShardedStateDictConfig()) + + def __enter__(self): + log_gpu_memory_usage('Before state_dict() in sharding manager memory', logger=logger) + params = self.module.state_dict() + log_gpu_memory_usage('After state_dict() in sharding manager memory', logger=logger) + # Copy, not share memory + load_format = 'hf' if self.full_params else 'dtensor' + self.inference_engine.sync_model_weights(params, load_format=load_format) + log_gpu_memory_usage('After sync model weights in sharding manager', logger=logger) + + del params + torch.cuda.empty_cache() + log_gpu_memory_usage('After del state_dict and empty_cache in sharding manager', logger=logger) + + # TODO: offload FSDP model weights + # self.module.cpu() + # torch.cuda.empty_cache() + # if torch.distributed.get_rank() == 0: + # print(f'after model to cpu in sharding manager memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB') + + def __exit__(self, exc_type, exc_value, traceback): + log_gpu_memory_usage('Before vllm offload in sharding manager', logger=logger) + self.inference_engine.offload_model_weights() + log_gpu_memory_usage('After vllm offload in sharding manager', logger=logger) + + # self.module.to('cuda') + # if torch.distributed.get_rank() == 0: + # print(f'after actor module to cuda in sharding manager memory allocated: {torch.cuda.memory_allocated() / 1e9}GB, reserved: {torch.cuda.memory_reserved() / 1e9}GB') + + self.module.train() + + # add empty cache after each compute + torch.cuda.empty_cache() + + def preprocess_data(self, data: DataProto) -> DataProto: + # TODO: Current impl doesn't consider FSDP with torch micro-dp + data.batch = allgather_dict_tensors(data.batch.contiguous(), + size=vllm_ps.get_tensor_model_parallel_world_size(), + group=vllm_ps.get_tensor_model_parallel_group(), + dim=0) + + return data + + def postprocess_data(self, data: DataProto) -> DataProto: + # TODO: Current impl doesn't consider FSDP with torch micro-dp + broadcast_dict_tensor(data.batch, + src=vllm_ps.get_tensor_model_parallel_src_rank(), + group=vllm_ps.get_tensor_model_parallel_group()) + dp_rank = torch.distributed.get_rank() + dp_size = torch.distributed.get_world_size() # not consider torch micro-dp + tp_size = vllm_ps.get_tensor_model_parallel_world_size() + if tp_size > 1: + # TODO: shall we build a micro_dp group for vllm when integrating with vLLM? + local_prompts = data.chunk(chunks=tp_size) + data = local_prompts[dp_rank % tp_size] + return data diff --git a/verl/trainer/ppo/hybrid_engine/megatron_vllm.py b/verl/trainer/ppo/hybrid_engine/megatron_vllm.py new file mode 100644 index 000000000..bc07a5a65 --- /dev/null +++ b/verl/trainer/ppo/hybrid_engine/megatron_vllm.py @@ -0,0 +1,428 @@ +# 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. +""" +This file contains a Megatron style Hybrid Engine that shares the weights of the actor with the inference engine. +""" + +import torch +import torch.distributed as dist + +from torch import nn + +from megatron.core import parallel_state as mpu +from megatron.core import DistributedDataParallel as LocalDDP +from megatron.core.transformer.module import Float16Module +from torch.nn.parallel.distributed import DistributedDataParallel as torchDDP +from verl.utils.megatron_utils import get_model, unwrap_model +from verl.utils.memory_buffer import ( + build_memory_buffer, + build_memory_reference_from_module, + get_weight_buffer_meta_from_module, +) + + +class AllGatherPPModel: + + def __init__(self, model_provider) -> None: + + self._pp_group = mpu.get_pipeline_model_parallel_group() + self._pp_rank = mpu.get_pipeline_model_parallel_rank() + self._pp_size = mpu.get_pipeline_model_parallel_world_size() + self._vpp_size = mpu.get_virtual_pipeline_model_parallel_world_size() + self._model_chunk_size = self._vpp_size or 1 + + # each one holds a list of model_chunks in this pp stage + self._pp_models = [None] * self.pp_size + + rank_list = list(range(self.pp_size)) + # make current rank the last one to initialize + rank_list[self.pp_rank], rank_list[-1] = rank_list[-1], rank_list[self.pp_rank] + self._this_rank_models = None + + # store the parameter of each pp stage + self.memory_buffers = [None] * self.pp_size + for cur_pp_rank in rank_list: + print( + f'create pp model', f'torch allocated {torch.cuda.memory_allocated() / 1e9:.4f} GB, ' + f'reserved {torch.cuda.memory_reserved() / 1e9:.4f} GB') + # since the last initialized rank is the current pp rank, after init, the pp rank is still correct + mpu.set_pipeline_model_parallel_rank(cur_pp_rank) + if cur_pp_rank != self.pp_rank: + models = get_model(model_provider, wrap_with_ddp=False) + models = nn.ModuleList(models) + assert len(models) == self._model_chunk_size, f"{len(models)} != {self._model_chunk_size}" + self.pp_models[cur_pp_rank] = models + else: + # for regular model, we wrapped it with DDP + models = get_model(model_provider) + assert len(models) == self._model_chunk_size, f"{len(models)} != {self._model_chunk_size}" + self._this_rank_models = nn.ModuleList(models) + self.pp_models[cur_pp_rank] = nn.ModuleList(unwrap_model(models, (torchDDP, LocalDDP))) + + self._build_param_buffer(cur_pp_rank) + self._build_param_references(cur_pp_rank, maintain_weight=cur_pp_rank == self.pp_rank) + + # TODO: after binding to the memory buffer, we can load the checkpoint here + if cur_pp_rank != self.pp_rank: + for model in self.pp_models[cur_pp_rank]: + model.eval() + self._offload_params_to_cpu(cur_pp_rank) + + def _build_param_buffer(self, pp_rank): + """Build the parameter buffer in each pp rank""" + model = self.pp_models[pp_rank] + weight_buffer_meta = get_weight_buffer_meta_from_module(model) + self.memory_buffers[pp_rank] = build_memory_buffer(weight_buffer_meta) + + def _build_param_references(self, pp_rank, maintain_weight=False): + model = self.pp_models[pp_rank] + build_memory_reference_from_module(model, self.memory_buffers[pp_rank], maintain_weight=maintain_weight) + + def _load_params_to_cuda(self, pp_rank, to_empty=False): + assert pp_rank != self.pp_rank, f"unexpected to load current pp rank [{pp_rank}] back to cuda" + for buffer in self.memory_buffers[pp_rank].values(): + if not to_empty: + buffer.data = buffer.data.to(torch.cuda.current_device(), non_blocking=True) + else: + buffer.data = torch.empty_like(buffer.data, device='cuda') + # rebuild reference after loading to CUDA + self._build_param_references(pp_rank) + + def _offload_params_to_cpu(self, pp_rank, to_empty=False): + assert pp_rank != self.pp_rank, f"unexpected to offload current pp rank [{pp_rank}] to cpu" + for buffer in self.memory_buffers[pp_rank].values(): + if not to_empty: + # offload the whole memory buffer to CPU + buffer.data = buffer.data.to('cpu', non_blocking=True) + else: + buffer.data = torch.empty_like(buffer.data, device='cpu') + self._build_param_references(pp_rank) + + def load_params_to_cuda(self, to_empty=False): + """load all model params to cuda""" + for cur_pp_rank in range(self.pp_size): + if cur_pp_rank != self.pp_rank: + self._load_params_to_cuda(cur_pp_rank, to_empty=to_empty) + + def allgather_params(self): + """allgather params of all pp ranks. Return a list of handles""" + for cur_pp_rank in range(self.pp_size): + global_src = dist.get_global_rank(group=self.pp_group, group_rank=cur_pp_rank) + + # NOTE(sgm): the async op may cause memory leakage of the memory_buffer/pp_models + for memory_buffer in self.memory_buffers[cur_pp_rank].values(): + dist.broadcast(tensor=memory_buffer.data, src=global_src, group=self.pp_group, async_op=False) + + def forward(self, *inputs, **kwargs): + try: + prev_output = None + for cur_chunk_rank in range(self._model_chunk_size): + if self._vpp_size: + mpu.set_virtual_pipeline_model_parallel_rank(cur_chunk_rank) + + for cur_pp_rank in range(self.pp_size): + mpu.set_pipeline_model_parallel_rank(cur_pp_rank) + self.pp_models[cur_pp_rank][cur_chunk_rank].set_input_tensor(prev_output) + ret = self.pp_models[cur_pp_rank][cur_chunk_rank](*inputs, **kwargs) + self.pp_models[cur_pp_rank][cur_chunk_rank].set_input_tensor(None) + prev_output = ret + finally: + if self._vpp_size: + mpu.set_virtual_pipeline_model_parallel_rank(0) + mpu.set_pipeline_model_parallel_rank(self.pp_rank) + return ret + + def __call__(self, *inputs, **kwargs): + return self.forward(*inputs, **kwargs) + + def eval(self): + for model in self.pp_models[self.pp_rank]: + model.eval() + + def train(self): + for model in self.pp_models[self.pp_rank]: + model.train() + + def offload_params_to_cpu(self, to_empty=False): + """offload params of models that are not of current pp rank to cpu""" + for cur_pp_rank in range(self.pp_size): + if cur_pp_rank != self.pp_rank: + self._offload_params_to_cpu(cur_pp_rank, to_empty=to_empty) + + def get_all_params(self): + """Get all the parameters of the models in all pp ranks + + Returns: + params: List[List[Dict[str, Tensor]]]: a list of parameters in all pp, where each is a list of dict + tensors of each model chunk + + """ + params = [] + for pp_rank in range(self.pp_size): + params.append([]) + for model_chunk_idx in range(len(self.pp_models[pp_rank])): + params[pp_rank].append({}) + pp_model = self.pp_models[pp_rank][model_chunk_idx] + pp_model = unwrap_model(pp_model, ((torchDDP, LocalDDP, Float16Module))) # not use Float16Module + for name, param in pp_model.named_parameters(): + # NOTE(gh) workaround: should not get lora params for inference + if 'lora' in name: + continue + params[pp_rank][model_chunk_idx][name] = param + + return params + + def update_this_rank_models(self, new_models): + self._this_rank_models = new_models + self._pp_models[self.pp_rank] = unwrap_model(new_models, (torchDDP, LocalDDP)) + + @property + def this_rank_models(self): + return self._this_rank_models + + @property + def pp_size(self): + return self._pp_size + + @property + def pp_rank(self): + return self._pp_rank + + @property + def pp_group(self): + return self._pp_group + + @property + def pp_models(self): + return self._pp_models + + +""" +Megatron Hybrid Engine: +- 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. +""" + +from .base import BaseShardingManager + +import torch +from torch import nn +import torch.distributed +from torch.distributed import new_group + +from verl import DataProto +from verl.utils.torch_functional import (broadcast_dict_tensor, allgather_dict_tensors) +import verl.utils.megatron.tensor_parallel as tp_utils +from verl.third_party.vllm import parallel_state as vllm_ps +from verl.third_party.vllm import LLM +from verl.utils.model import normalize_pp_vpp_params +# Micro Data parallel group. Micro data parallel group is additional dp group that origins from splitting training tp +# into infer_tp and micro_tp. By default, we use order micro_dp - tp +_MICRO_DATA_PARALLEL_GROUP = None + + +class MegatronVLLMShardingManager(BaseShardingManager): + + def __init__(self, module: AllGatherPPModel, inference_engine: LLM, model_config, layer_name_mapping): + self.module = module + self.inference_engine = inference_engine + self.model_config = model_config + self.layer_name_mapping = layer_name_mapping + + # initialize micro_dp group for vllm inference + global _MICRO_DATA_PARALLEL_GROUP + world_size = torch.distributed.get_world_size() + rank = torch.distributed.get_rank() + train_tensor_parallel_size = mpu.get_tensor_model_parallel_world_size() + infer_tensor_parallel_size = vllm_ps.get_tensor_model_parallel_world_size() + + # TODO(sgm): this may not be true for FSDP -> vLLM + assert infer_tensor_parallel_size <= train_tensor_parallel_size, \ + 'Not implemented for infer_tp > train_tp' + assert train_tensor_parallel_size % infer_tensor_parallel_size == 0 + + micro_dp_size = train_tensor_parallel_size // infer_tensor_parallel_size + num_micro_dp_groups = world_size // micro_dp_size + assert _MICRO_DATA_PARALLEL_GROUP is None, ("micro data parallel group is already initialized") + for i in range(num_micro_dp_groups): + ranks = range(i * micro_dp_size, (i + 1) * micro_dp_size) + group = new_group(ranks=ranks) + if rank in ranks: + _MICRO_DATA_PARALLEL_GROUP = group + + def default_tp_concat_fn(self, name, param, infer_params, model_config): + """ + name: name of the parameter + param: training parameters + infer_params (List[torch.Tensor]): a list of parameters all-gathered from micro_dp_group + model_config: huggingface model_config + TODO(zhangchi.usc1992): currently, the implementation is adhoc. We can move this function to the model + definition so that it is model-agnostic. If the model doesn't implement this function, + we can throw an error to force user disable TP HybridEngine. + """ + + if self.layer_name_mapping.get("qkv_layer_name") in name: + # if the tensor is qkv, for each param on tp, split into q, k, v + # concat q, k, v separately. + q_lst = [] + k_lst = [] + v_lst = [] + assert model_config.num_attention_heads % model_config.num_key_value_heads == 0 + num_q_per_kv = model_config.num_attention_heads // model_config.num_key_value_heads + assert infer_params[0].shape[0] % (num_q_per_kv + 2) == 0 + kv_size_per_tp = infer_params[0].shape[0] // (num_q_per_kv + 2) + split_size = [kv_size_per_tp * num_q_per_kv, kv_size_per_tp, kv_size_per_tp] + for infer_param in infer_params: + q, k, v = infer_param.split(split_size) + q_lst.append(q) + k_lst.append(k) + v_lst.append(v) + q = torch.cat(q_lst, dim=0) + k = torch.cat(k_lst, dim=0) + v = torch.cat(v_lst, dim=0) + + infer_params = torch.cat((q, k, v), dim=0) + + elif self.layer_name_mapping.get("gate_proj_layer_name") in name: + # if the tensor is gate and proj + gate_lst = [] + up_lst = [] + for infer_param in infer_params: + gate, up = infer_param.chunk(2) + gate_lst.append(gate) + up_lst.append(up) + gate = torch.cat(gate_lst, dim=0) + up = torch.cat(up_lst, dim=0) + infer_params = torch.cat((gate, up), dim=0) + + else: + # concat tensor + infer_params = torch.cat(infer_params, dim=tp_utils.get_tensor_parallel_partition_dim(param)) + + return infer_params + + def _post_process_params(self, params): + """ + For each param, if it is a tp-splited param, we all-gather from micro_dp group. + """ + # here the params are in train tp format. we iterate params and all-gather + # TODO(zhangchi.usc1992) We can consider copy non-tp weight to another infer buffer. + # In this way, all the params in the original memory_buffers and can be offload. + micro_dp_size = get_micro_data_parallel_world_size() + micro_dp_group = get_micro_data_parallel_group() + + if micro_dp_size <= 1: + return + + origin_params = {} + for name in params.keys(): + param = params[name] + if tp_utils.is_tensor_parallel_param(param): + # allocate a new tensor with proper size + infer_params = [torch.empty_like(param) for _ in range(micro_dp_size)] + torch.distributed.all_gather(infer_params, param, group=micro_dp_group) + infer_params = self.default_tp_concat_fn(name, param, infer_params, self.model_config) + # replace with original param + params[name] = infer_params + origin_params[name] = param + + return origin_params + + def __enter__(self): + # create a new cuda space for parameters not in this pp rank + self.module.load_params_to_cuda() + # broadcast the parameters from pp rank to other ranks + self.module.allgather_params() + # obtain name to parameters in pp/vpp + params = self.module.get_all_params() + + # bind the params to inference engine + self.params = normalize_pp_vpp_params(params=params, + num_hidden_layers=self.model_config.num_hidden_layers, + layer_name='layers') + self.origin_params = self._post_process_params(self.params) + self.inference_engine.sync_model_weights(self.params, load_format='megatron') + + def __exit__(self, exc_type, exc_value, traceback): + # offload parameters doesn't belong to this pp rank + self.module.offload_params_to_cpu() + + # FIXME(sgm): the best practice is to delete the cuda tensor + # rebind the model weights, can be any cpu tensor + if get_micro_data_parallel_world_size() > 1: + for name in self.params.keys(): + self.params[name] = self.origin_params[name] + + # self.inference_engine.sync_model_weights(params) + self.inference_engine.offload_model_weights() + + self.module.train() + + # add empty cache after each compute + torch.cuda.empty_cache() + + def preprocess_data(self, data: DataProto) -> DataProto: + # prompts are identical for each training tp. We select for each inference tp + micro_dp_size = get_micro_data_parallel_world_size() + micro_dp_rank = get_micro_data_parallel_rank() + + # broadcast from tp=0 to other tp ranks + broadcast_dict_tensor(data.batch, + src=mpu.get_tensor_model_parallel_src_rank(), + group=mpu.get_tensor_model_parallel_group()) + + if micro_dp_size > 1: + local_prompts = data.chunk(chunks=micro_dp_size) + data = local_prompts[micro_dp_rank] + + return data + + def postprocess_data(self, data: DataProto) -> DataProto: + meta_info = data.meta_info + # all gather batch among micro-dp groups + micro_dp_size = get_micro_data_parallel_world_size() + if micro_dp_size > 1: + data.batch = allgather_dict_tensors(data.batch.contiguous(), + size=get_micro_data_parallel_world_size(), + group=get_micro_data_parallel_group(), + dim=0) + + # all gather batch among pp group + if meta_info.get('allgather_pp_output', True): + data.batch = allgather_dict_tensors(data.batch.contiguous(), + size=mpu.get_pipeline_model_parallel_world_size(), + group=mpu.get_pipeline_model_parallel_group(), + dim=0) + return data + + +""" +Micro Data parallel group +""" + + +def get_micro_data_parallel_group(): + assert _MICRO_DATA_PARALLEL_GROUP is not None + return _MICRO_DATA_PARALLEL_GROUP + + +def get_micro_data_parallel_world_size(): + return torch.distributed.get_world_size(group=get_micro_data_parallel_group()) + + +def get_micro_data_parallel_rank(): + return torch.distributed.get_rank(group=get_micro_data_parallel_group()) diff --git a/verl/trainer/ppo/ray_trainer.py b/verl/trainer/ppo/ray_trainer.py new file mode 100644 index 000000000..bd726499c --- /dev/null +++ b/verl/trainer/ppo/ray_trainer.py @@ -0,0 +1,526 @@ +# 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. +""" +FSDP PPO Trainer with Ray-based single controller. +This trainer supports model-agonistic model initialization with huggingface +""" + +import os +from dataclasses import dataclass, field +from enum import Enum +from pprint import pprint +from typing import Callable, Type, Tuple, Union + +from omegaconf import OmegaConf, open_dict +import numpy as np +from codetiming import Timer + +from single_controller.base import Worker +from single_controller.ray import RayResourcePool, RayWorkerGroup, RayClassWithInitArgs +from single_controller.ray.base import create_colocated_worker_cls +from verl import DataProto +from verl.trainer.ppo import core_algos + +WorkerType = Type[Worker] + + +class Role(Enum): + """ + To create more roles dynamically, you can subclass Role and add new members + """ + Actor = 0 + Rollout = 1 + ActorRollout = 2 + Critic = 3 + RefPolicy = 4 + RewardModel = 5 + ActorRolloutRef = 6 + + +@dataclass +class ResourcePoolManager: + """ + Define a resource pool specification. Resource pool will be initialized first. + Mapping + """ + resource_pool_spec: dict[str, list[int]] + mapping: dict[Role, str] + resource_pool_dict: dict[str, RayResourcePool] = field(default_factory=dict) + + def create_resource_pool(self): + for resource_pool_name, process_on_nodes in self.resource_pool_spec.items(): + # Due to the Ray issue, we can only support max_colocate_count=1 for now. + # This means that each GPU can only have one process. + # We can support max_colocate > 1 when applying this pull request: https://github.com/ray-project/ray/pull/44385 + resource_pool = RayResourcePool(process_on_nodes=process_on_nodes, use_gpu=True, max_colocate_count=1) + self.resource_pool_dict[resource_pool_name] = resource_pool + + def get_resource_pool(self, role: Role) -> RayResourcePool: + """Get the resource pool of the worker_cls""" + return self.resource_pool_dict[self.mapping[role]] + + +import torch +from verl.utils.torch_functional import masked_mean + + +def apply_kl_penalty(data: DataProto, kl_ctrl: core_algos.AdaptiveKLController, kl_penalty='kl'): + responses = data.batch['responses'] + response_length = responses.size(1) + token_level_scores = data.batch['token_level_scores'] + batch_size = data.batch.batch_size[0] + attention_mask = data.batch['attention_mask'] + response_mask = attention_mask[:, -response_length:] + + # compute kl between ref_policy and current policy + if 'ref_log_prob' in data.batch.keys(): + kld = core_algos.kl_penalty(data.batch['old_log_probs'], data.batch['ref_log_prob'], + kl_penalty=kl_penalty) # (batch_size, response_length) + kld = kld * response_mask + beta = kl_ctrl.value + else: + beta = 0 + kld = torch.zeros_like(response_mask, dtype=torch.float32) + + token_level_rewards = token_level_scores - beta * kld + + current_kl = masked_mean(kld, mask=response_mask, axis=-1) # average over sequence + current_kl = torch.mean(current_kl, dim=0).item() + + # according to https://github.com/huggingface/trl/blob/951ca1841f29114b969b57b26c7d3e80a39f75a0/trl/trainer/ppo_trainer.py#L837 + kl_ctrl.update(current_kl=current_kl, n_steps=batch_size) + data.batch['token_level_rewards'] = token_level_rewards + + metrics = {'critic/kl': current_kl, 'critic/kl_coeff': beta} + + return data, metrics + + +def compute_advantage(data: DataProto, gamma, lam, adv_estimator): + values = data.batch['values'] + responses = data.batch['responses'] + response_length = responses.size(1) + attention_mask = data.batch['attention_mask'] + response_mask = attention_mask[:, -response_length:] + token_level_rewards = data.batch['token_level_rewards'] + + # TODO: add other ways to estimate advantages + if adv_estimator == 'gae': + advantages, returns = core_algos.compute_gae_advantage_return(token_level_rewards=token_level_rewards, + values=values, + eos_mask=response_mask, + gamma=gamma, + lam=lam) + data.batch['advantages'] = advantages + data.batch['returns'] = returns + else: + raise NotImplementedError + return data + + +def reduce_metrics(metrics: dict): + for key, val in metrics.items(): + metrics[key] = np.mean(val) + return metrics + + +def compute_data_metrics(batch): + # TODO: add response length + sequence_score = batch.batch['token_level_scores'].sum(-1) + sequence_reward = batch.batch['token_level_rewards'].sum(-1) + + response_length = batch.batch['responses'].shape[-1] + + advantages = batch.batch['advantages'] + prompt_mask = batch.batch['attention_mask'][:, :-response_length] + response_mask = batch.batch['attention_mask'][:, -response_length:] + + prompt_length = prompt_mask.sum(-1).float() + response_length = response_mask.sum(-1).float() # (batch_size,) + + returns = batch.batch['returns'] + values = batch.batch['values'] + + metrics = { + # score + 'critic/score/mean': torch.mean(sequence_score).detach().item(), + 'critic/score/max': torch.max(sequence_score).detach().item(), + 'critic/score/min': torch.min(sequence_score).detach().item(), + # reward + 'critic/rewards/mean': torch.mean(sequence_reward).detach().item(), + 'critic/rewards/max': torch.max(sequence_reward).detach().item(), + 'critic/rewards/min': torch.min(sequence_reward).detach().item(), + # adv + 'critic/advantages/mean': masked_mean(advantages, response_mask).detach().item(), + 'critic/advantages/max': torch.max(advantages[response_mask]).detach().item(), + 'critic/advantages/min': torch.min(advantages[response_mask]).detach().item(), + # returns + 'critic/returns/mean': masked_mean(returns, response_mask).detach().item(), + 'critic/returns/max': torch.max(returns[response_mask]).detach().item(), + 'critic/returns/min': torch.min(returns[response_mask]).detach().item(), + # values + 'critic/values/mean': masked_mean(values, response_mask).detach().item(), + 'critic/values/max': torch.max(values[response_mask]).detach().item(), + 'critic/values/min': torch.min(values[response_mask]).detach().item(), + # response length + 'response_length/mean': torch.mean(response_length).detach().item(), + 'response_length/max': torch.max(response_length).detach().item(), + 'response_length/min': torch.min(response_length).detach().item(), + # prompt length + 'prompt_length/mean': torch.mean(prompt_length).detach().item(), + 'prompt_length/max': torch.max(prompt_length).detach().item(), + 'prompt_length/min': torch.min(prompt_length).detach().item(), + } + return metrics + + +class RayPPOTrainer(object): + """ + Note that this trainer runs on the driver process on a single CPU/GPU node. + """ + + # TODO: support each role have individual ray_worker_group_cls, + # i.e., support different backend of different role + def __init__(self, + config, + tokenizer, + role_worker_mapping: dict[Role, WorkerType], + resource_pool_manager: ResourcePoolManager, + ray_worker_group_cls: RayWorkerGroup = RayWorkerGroup, + reward_fn=None, + val_reward_fn=None): + + # assert torch.cuda.is_available(), 'cuda must be available on driver' + + self.tokenizer = tokenizer + self.config = config + self.reward_fn = reward_fn + self.val_reward_fn = val_reward_fn + + self.hybrid_engine = config.actor_rollout_ref.hybrid_engine + assert self.hybrid_engine, 'Currently, only support hybrid engine' + + if self.hybrid_engine: + assert Role.ActorRollout in role_worker_mapping, f'{role_worker_mapping.keys()=}' + + self.role_worker_mapping = role_worker_mapping + self.resource_pool_manager = resource_pool_manager + self.use_reference_policy = Role.RefPolicy in role_worker_mapping + self.use_rm = Role.RewardModel in role_worker_mapping + self.ray_worker_group_cls = ray_worker_group_cls + + # define KL control + if self.use_reference_policy: + if config.algorithm.kl_ctrl.type == 'fixed': + self.kl_ctrl = core_algos.FixedKLController(kl_coef=config.algorithm.kl_ctrl.kl_coef) + elif config.algorithm.kl_ctrl.type == 'adaptive': + assert config.algorithm.kl_ctrl.horizon > 0, f'horizon must be larger than 0. Got {config.critic.kl_ctrl.horizon}' + self.kl_ctrl = core_algos.AdaptiveKLController(init_kl_coef=config.algorithm.kl_ctrl.kl_coef, + target_kl=config.algorithm.kl_ctrl.target_kl, + horizon=config.algorithm.kl_ctrl.horizon) + else: + raise NotImplementedError + else: + self.kl_ctrl = core_algos.FixedKLController(kl_coef=0.) + + self._create_dataloader() + + def _create_dataloader(self): + from torch.utils.data import DataLoader + # TODO: we have to make sure the batch size is divisible by the dp size + from verl.utils.dataset.rl_dataset import RLHFDataset, collate_fn + self.train_dataset = RLHFDataset(parquet_files=self.config.data.train_files, + tokenizer=self.tokenizer, + prompt_key=self.config.data.prompt_key, + max_prompt_length=self.config.data.max_prompt_length, + filter_prompts=True, + return_raw_chat=self.config.data.get('return_raw_chat', False), + truncation='error') + self.train_dataloader = DataLoader(dataset=self.train_dataset, + batch_size=self.config.data.train_batch_size, + shuffle=True, + drop_last=True, + collate_fn=collate_fn) + + self.val_dataset = RLHFDataset(parquet_files=self.config.data.val_files, + tokenizer=self.tokenizer, + prompt_key=self.config.data.prompt_key, + max_prompt_length=self.config.data.max_prompt_length, + filter_prompts=True, + return_raw_chat=self.config.data.get('return_raw_chat', False), + truncation='error') + self.val_dataloader = DataLoader(dataset=self.val_dataset, + batch_size=self.config.data.val_batch_size, + shuffle=True, + drop_last=True, + collate_fn=collate_fn) + + assert len(self.train_dataloader) >= 1 + assert len(self.val_dataloader) >= 1 + + print(f'Size of train dataloader: {len(self.train_dataloader)}') + print(f'Size of val dataloader: {len(self.val_dataloader)}') + + # inject total_training_steps to actor/critic optim_config. This is hacky. + total_training_steps = len(self.train_dataloader) * self.config.trainer.total_epochs + + OmegaConf.set_struct(self.config, True) + with open_dict(self.config): + self.config.actor_rollout_ref.actor.optim.total_training_steps = total_training_steps + self.config.critic.optim.total_training_steps = total_training_steps + + def _validate(self): + reward_tensor_lst = [] + data_source_lst = [] + for test_data in self.val_dataloader: + test_batch = DataProto.from_single_dict(test_data) + # test_batch = test_batch.to('cuda') + + # we only do validation on rule-based rm + if test_batch[0].non_tensor_batch['reward_model']['style'] == 'model': + return {} + + test_gen_batch = test_batch.pop(['input_ids', 'attention_mask', 'position_ids']) + test_gen_batch.meta_info = { + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + 'recompute_log_prob': False, + 'do_sample': False, + 'validate': True, + } + + test_output_gen_batch = self.actor_rollout_wg.generate_sequences(test_gen_batch) + print('validation generation end') + + test_batch = test_batch.union(test_output_gen_batch) + + # evaluate using reward_function + # for certain reward function (e.g. sandbox), the generation can overlap with reward + reward_tensor = self.val_reward_fn(test_batch) + + reward_tensor_lst.append(reward_tensor) + data_source_lst.append(test_batch.non_tensor_batch.get('data_source', ['unknown'] * reward_tensor.shape[0])) + + reward_tensor = torch.cat(reward_tensor_lst, dim=0).sum(-1).cpu() # (batch_size,) + data_sources = np.concatenate(data_source_lst, axis=0) + # evaluate test_score based on data source + data_source_reward = {} + for i in range(reward_tensor.shape[0]): + data_source = data_sources[i] + if data_source not in data_source_reward: + data_source_reward[data_source] = [] + data_source_reward[data_source].append(reward_tensor[i].item()) + + metric_dict = {} + for data_source, rewards in data_source_reward.items(): + metric_dict[f'test_score/{data_source}'] = np.mean(rewards) + + return metric_dict + + def init_workers(self): + """Init resource pool and worker group""" + self.resource_pool_manager.create_resource_pool() + + self.resource_pool_to_cls = {pool: {} for pool in self.resource_pool_manager.resource_pool_dict.values()} + + # create actor and rollout + if self.hybrid_engine: + resource_pool = self.resource_pool_manager.get_resource_pool(Role.ActorRollout) + actor_rollout_cls = RayClassWithInitArgs(cls=self.role_worker_mapping[Role.ActorRollout], + config=self.config.actor_rollout_ref, + role='actor_rollout') + self.resource_pool_to_cls[resource_pool]['actor_rollout'] = actor_rollout_cls + else: + raise NotImplementedError + + # create critic + if self.config.algorithm.adv_estimator == 'gae': + resource_pool = self.resource_pool_manager.get_resource_pool(Role.Critic) + critic_cls = RayClassWithInitArgs(cls=self.role_worker_mapping[Role.Critic], config=self.config.critic) + self.resource_pool_to_cls[resource_pool]['critic'] = critic_cls + self.use_critic = True + else: + # support GRPO and ReMax + raise NotImplementedError + + # create reference policy if needed + if self.use_reference_policy: + resource_pool = self.resource_pool_manager.get_resource_pool(Role.RefPolicy) + ref_policy_cls = RayClassWithInitArgs(self.role_worker_mapping[Role.RefPolicy], + config=self.config.actor_rollout_ref, + role='ref') + self.resource_pool_to_cls[resource_pool]['ref'] = ref_policy_cls + + # create a reward model if reward_fn is None + if self.use_rm: + # we create a RM here + resource_pool = self.resource_pool_manager.get_resource_pool(Role.RewardModel) + rm_cls = RayClassWithInitArgs(self.role_worker_mapping[Role.RewardModel], config=self.config.reward_model) + self.resource_pool_to_cls[resource_pool]['rm'] = rm_cls + + # 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 https://github.com/volcengine/verl/blob/master/examples/ray/tutorial.ipynb 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() + + 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, + 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/trainer/ppo/reward_model/__init__.py b/verl/trainer/ppo/reward_model/__init__.py new file mode 100644 index 000000000..a0b48a750 --- /dev/null +++ b/verl/trainer/ppo/reward_model/__init__.py @@ -0,0 +1,15 @@ +# 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 BasePPORewardModel diff --git a/verl/trainer/ppo/reward_model/base.py b/verl/trainer/ppo/reward_model/base.py new file mode 100644 index 000000000..c02487db3 --- /dev/null +++ b/verl/trainer/ppo/reward_model/base.py @@ -0,0 +1,45 @@ +# 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 class for reward model +""" + +from abc import ABC, abstractmethod + +from verl import DataProto + + +class BasePPORewardModel(ABC): + + def __init__(self, config): + self.config = config + + @abstractmethod + def compute_reward(self, data: DataProto) -> DataProto: + """Computing reward given input_ids. The transformers should output a tensor with shape + [batch_size, sequence_length], and the value at [EOS] mask should be gathered. + + Args: + data: must contain keys "input_ids", "attention_mask" and "position_ids". + - input_ids: [batch_size, sequence_length] + - attention_mask: [batch_size, sequence_length] + - position_ids: [batch_size, sequence_length] + + Returns: a data pass protocol containing "reward". Only the [EOS] position contains the reward. + Other position should have zero reward. Note that this may change in the future if we use + dense reward. So, we leave the interface for general case. + - reward: [batch_size, sequence_length]. + + """ + pass diff --git a/verl/trainer/ppo/reward_model/megatron/__init__.py b/verl/trainer/ppo/reward_model/megatron/__init__.py new file mode 100644 index 000000000..b0956b4cc --- /dev/null +++ b/verl/trainer/ppo/reward_model/megatron/__init__.py @@ -0,0 +1,15 @@ +# 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 .reward_model import MegatronRewardModel diff --git a/verl/trainer/ppo/reward_model/megatron/reward_model.py b/verl/trainer/ppo/reward_model/megatron/reward_model.py new file mode 100644 index 000000000..7ea56299e --- /dev/null +++ b/verl/trainer/ppo/reward_model/megatron/reward_model.py @@ -0,0 +1,275 @@ +# 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. +""" +Megatron Reward Model. +""" + +from tensordict import TensorDict +from functools import partial +from verl import DataProto +from verl.utils.torch_functional import logprobs_from_logits +import torch +import torch +import torch.distributed + +from verl.utils.torch_functional import get_eos_mask, pad_sequence_to_length +from verl.utils.megatron.pipeline_parallel import (compute_transformers_input_shapes, make_batch_generator) +from verl import DataProto +from verl.utils.torch_functional import logprobs_from_logits, broadcast_dict_tensor, split_dict_tensor_into_batches +from verl.utils.torch_dtypes import PrecisionType +from verl.trainer.ppo.reward_model.base import BasePPORewardModel +from verl.utils.megatron import sequence_parallel as sp_utils +from megatron.core import parallel_state as mpu +from megatron.core.pipeline_parallel import get_forward_backward_func + + +class MegatronRewardModel(BasePPORewardModel): + + def __init__(self, + config, + model_config, + reward_model_module: torch.nn.ModuleList, + megatron_config, + sft_tokenizer=None, + rm_tokenizer=None): + self.config = config + self.reward_model_module = reward_model_module + self.megatron_config = megatron_config + self.model_config = model_config + self.device = 'cuda' + self.sft_tokenizer = sft_tokenizer + self.rm_tokenizer = rm_tokenizer + self.use_different_tokenizer = rm_tokenizer is not None + + if self.config.param_offload: + self.offload_params_to_cpu() + + def re_encode_by_rm_tokenizer(self, data: DataProto) -> DataProto: + assert self.use_different_tokenizer, 're-encode need rm tokenizer not be None!' + # need to use rm tokenizer to re-generate input_ids, attention_mask and position_ids + # 1. remove pad for each sequence + # 2. decode by sft_tokenizer, remove sft system prompts + # 3. encode by rm_tokenizer with rm system prompts, get rm_input_ids + # 4. generate attention_mask and position_ids + input_ids = data.batch['input_ids'] # (bs, seq_len) + attention_mask = data.batch['attention_mask'] + position_ids = data.batch['position_ids'] + ori_values = {'input_ids': input_ids, 'attention_mask': attention_mask, 'position_ids': position_ids} + ori_bs, ori_seqlen = input_ids.size(0), input_ids.size(1) + input_ids_for_rm = [] + attention_mask_for_rm = [] + position_ids_for_rm = [] + print_decode = True + ori_seqlen = ori_seqlen + 128 + for id, mask in zip(input_ids, attention_mask): + # 1. remove pad for each sequence + non_zero_indices = torch.nonzero(mask).view(-1) + begin_pos, end_pos = non_zero_indices[0].item(), non_zero_indices[-1].item() + valid_id = id[begin_pos:end_pos + 1] + # 2. decode by sft_tokenizer, remove sft system prompts + decode_result = self.sft_tokenizer.decode(valid_id) + # workaround + decode_with_rm_chat = decode_result.replace("<|user|>\n", "[INST] ").replace( + "\n<|assistant|>\n", " [/INST]").replace(" \n<|assistant|>\n", " [/INST]") + "" + if print_decode and torch.distributed.get_rank() == 0: + # only print first decode result + print(f'device {torch.cuda.current_device()}: sft decode result:\n{decode_result}\n \ + \ndevice {torch.cuda.current_device()}: sft decode result with rm chat template:\n{decode_with_rm_chat}\n\n' + ) + print_decode = False + # 3. encode by rm_tokenizer + rm_input_ids = self.rm_tokenizer(decode_with_rm_chat, + return_tensors='pt')['input_ids'][0].to(input_ids.device) + # 4. generate attention_mask and position_ids + rm_attention_mask = torch.ones_like(rm_input_ids, device=input_ids.device) + cur_seqlen = rm_input_ids.shape[-1] + # NOTE(gh): the later reward compute will process the shape (bs, seqlen_pad_128) + if cur_seqlen > ori_seqlen: + print(f'warninig: rm encode seqlen {cur_seqlen} > sft encode seqlen {ori_seqlen}') + rm_input_ids = rm_input_ids[:ori_seqlen] + rm_attention_mask = rm_attention_mask[:ori_seqlen] + else: + # right padding + rm_input_ids = pad_sequence_to_length(rm_input_ids, ori_seqlen, self.rm_tokenizer.pad_token_id) + rm_attention_mask = pad_sequence_to_length(rm_attention_mask, ori_seqlen, 0) + rm_position_ids = torch.arange(0, ori_seqlen, device=input_ids.device) + input_ids_for_rm.append(torch.unsqueeze(rm_input_ids, dim=0)) + attention_mask_for_rm.append(torch.unsqueeze(rm_attention_mask, dim=0)) + position_ids_for_rm.append(torch.unsqueeze(rm_position_ids, dim=0)) + input_ids_for_rm = torch.cat(input_ids_for_rm, dim=0) + attention_mask_for_rm = torch.cat(attention_mask_for_rm, dim=0) + position_ids_for_rm = torch.cat(position_ids_for_rm, dim=0) + + # (bs, seqlen) will not change, but input_ids, attention_mask and position_ids will change + # NOTE(gh): need to replace into origin values after compute reward! + data.batch['input_ids'] = input_ids_for_rm + data.batch['attention_mask'] = attention_mask_for_rm + data.batch['position_ids'] = position_ids_for_rm + + return data, ori_values + + @torch.no_grad() + def compute_reward(self, data: DataProto) -> DataProto: + if self.config.param_offload: + self.load_params_to_cuda() + + if self.use_different_tokenizer: + data, ori_values = self.re_encode_by_rm_tokenizer(data) + + input_ids = data.batch['input_ids'] # (bs, seq_len') + attention_mask = data.batch['attention_mask'] + position_ids = data.batch['position_ids'] + + responses = data.batch['responses'] + batch_size = responses.size(0) + response_length = responses.size(1) + + with torch.no_grad(): + output = self.forward_batch(data) + if mpu.is_pipeline_last_stage(ignore_virtual=True): + logits = torch.cat([o['logits'] for o in output], dim=0) + else: + logits = torch.empty( + (input_ids.shape[0], input_ids.shape[1]), + dtype=torch.bfloat16, # TODO(sgm): check why is bfloat16 + device=input_ids.device) + # broadcast across pp ranks + torch.distributed.broadcast(tensor=logits, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group(), + async_op=False) + + # (bs, seqlen', hidden_size) -> (bs, seqlen', 1) -> (bs, seqlen') + token_level_rewards = logits + # find the last token reward + ends = attention_mask.cumsum(dim=-1).argmax(dim=-1).view(-1, 1) # (bs, 1) + rewards = torch.gather(token_level_rewards, dim=1, index=ends) # (bs, 1) + + if self.use_different_tokenizer: + data.batch.update(ori_values) + input_ids = ori_values['input_ids'] + attention_mask = ori_values['attention_mask'] + position_ids = ori_values['position_ids'] + + token_level_rewards = rewards.expand(attention_mask.shape[0], attention_mask.shape[1]) # (bs, ori_seqlen) + + # assign last valid token reward to ori position + eos_mask_idx = torch.argmax(position_ids * attention_mask, dim=-1) # (bs,) + eos_mask = torch.zeros_like(attention_mask) + eos_mask[torch.arange(batch_size), eos_mask_idx] = 1. + + token_level_rewards = token_level_rewards * eos_mask + token_level_rewards = token_level_rewards[:, -response_length:] + + if self.config.param_offload: + self.offload_params_to_cpu() + else: + # add empty cache after each compute + torch.cuda.empty_cache() + + batch = TensorDict({'rm_scores': token_level_rewards}, batch_size=input_ids.shape[0]) + + return DataProto(batch=batch) + + def forward_batch(self, data: DataProto): + """ + We assume: + - The model takes input: (input_ids, attention_mask, position_ids). No rmpad for the input + - The communication shape is (total_nnz_pad_to_sp // tp_size, 1, hidden_size) if sequence parallel is enabled + """ + # broadcast from last pp rank to all other pp ranks + # TODO: actually, we just need to control the sampling order. + data.batch = data.batch.contiguous() + broadcast_dict_tensor(data.batch, + src=mpu.get_pipeline_model_parallel_last_rank(), + group=mpu.get_pipeline_model_parallel_group()) + + # split into micro-batches + if self.config is not None and 'ppo_micro_batch_size' in self.config: + infer_batch_size = self.config.ppo_micro_batch_size + else: + infer_batch_size = data.batch.batch_size[0] + + data.batch['attention_mask'] = data.batch['attention_mask'].to(bool) + batches = split_dict_tensor_into_batches(data.batch, batch_size=infer_batch_size) + n_micro_batch = len(batches) + seq_len = batches[0]['input_ids'].shape[1] + + # compute input shapes for pp stages + input_shapes = compute_transformers_input_shapes( + batches, + meta_info={ + 'sequence_parallel': self.megatron_config.sequence_parallel, + 'hidden_size': self.model_config.hidden_size + }) + # compute input shapes for pp stages + forward_backward_func = get_forward_backward_func() + + def loss_func(output): + return 1., {'logits': output.logits} + + def forward_step(batch_iter, model): + batch = next(batch_iter) + input_ids = batch['input_ids'] + attention_mask = batch['attention_mask'] + position_ids = batch['position_ids'] + output = model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids) + return output, loss_func + + # batch should be a list of batches inside micro-batches + batch_generator = make_batch_generator(batches, vpp_size=len(self.reward_model_module)) + + # TODO: we may use the new schedule instead + # for flash-attn: (seq_len, batch_size, hidden_size) = (mbs*seq_len, 1, hidden_size) + if mpu.get_pipeline_model_parallel_world_size() > 1: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.reward_model_module, + num_microbatches=n_micro_batch, + input_shapes=input_shapes, # must set for flash-attn sequence packing + seq_length=infer_batch_size * seq_len, # no use when input_shapes was set + hidden_size=self.model_config.hidden_size, # no use when input_shapes was set + micro_batch_size=1, # no use when input_shapes was set + forward_only=True, + ) + else: + losses_reduced = forward_backward_func( + forward_step_func=forward_step, + data_iterator=batch_generator, + model=self.reward_model_module, + num_microbatches=n_micro_batch, + seq_length=infer_batch_size * seq_len, # in use for pp = 1 + hidden_size=self.model_config.hidden_size, # in use for pp = 1 + micro_batch_size=1, # in use for pp = 1 + forward_only=True, + ) + # loss_reduces contains the stats returned from loss_func + + return losses_reduced + + def offload_params_to_cpu(self): + if self.device == 'cuda': + for reward_model_module in self.reward_model_module: + for name, param in reward_model_module.named_parameters(): + param.data = param.data.to('cpu', non_blocking=True) + self.device = 'cpu' + torch.cuda.empty_cache() + + def load_params_to_cuda(self): + if self.device == 'cpu': + for reward_model_module in self.reward_model_module: + for name, param in reward_model_module.named_parameters(): + param.data = param.data.to(torch.cuda.current_device(), non_blocking=True) + self.device = 'cuda' diff --git a/verl/trainer/ppo/rollout/__init__.py b/verl/trainer/ppo/rollout/__init__.py new file mode 100644 index 000000000..083848c77 --- /dev/null +++ b/verl/trainer/ppo/rollout/__init__.py @@ -0,0 +1,19 @@ +# 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 +from .naive import NaiveRollout +from .hf_rollout import HFRollout + +__all__ = ["BaseRollout", "NaiveRollout", "HFRollout"] diff --git a/verl/trainer/ppo/rollout/base.py b/verl/trainer/ppo/rollout/base.py new file mode 100644 index 000000000..8c2733325 --- /dev/null +++ b/verl/trainer/ppo/rollout/base.py @@ -0,0 +1,37 @@ +# 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 abc import ABC, abstractmethod +from typing import Iterable, Union + +from verl import DataProto + +__all__ = ['BaseRollout'] + + +class BaseRollout(ABC): + + def __init__(self): + """ + + Args: + dataloader: an Iterable of TensorDict that consistently generates prompts. Note that the dataloader + should handle when the training stops. + """ + super().__init__() + + @abstractmethod + def generate_sequences(self, prompts: DataProto) -> DataProto: + """Generate sequences""" + pass diff --git a/verl/trainer/ppo/rollout/hf_rollout.py b/verl/trainer/ppo/rollout/hf_rollout.py new file mode 100644 index 000000000..1d929e5dd --- /dev/null +++ b/verl/trainer/ppo/rollout/hf_rollout.py @@ -0,0 +1,140 @@ +# 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 verl import DataProto +from verl.utils.torch_functional import get_eos_mask +from .base import BaseRollout + +from transformers import GenerationConfig + +__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: + 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'] + pad_token_id = prompts.meta_info['pad_token_id'] + + batch_size = idx.size(0) + prompt_length = idx.size(1) + + self.module.eval() + param_ctx = contextlib.nullcontext() + + # make sampling args can be overriden by inputs + do_sample = prompts.meta_info.get('do_sample', self.config.do_sample) + 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 = prompts.meta_info.get('top_k', self.config.get('top_k', 0)) + + if top_k is None: + top_k = 0 + top_k = max(0, top_k) # to be compatible with vllm + + temperature = prompts.meta_info.get('temperature', self.config.temperature) + + generation_config = GenerationConfig(temperature=temperature, top_p=top_p, top_k=top_k) + + 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: + with torch.autocast(device_type='cuda', dtype=torch.bfloat16): + output = self.module.generate( + input_ids=idx, + attention_mask=attention_mask, + do_sample=do_sample, + max_new_tokens=response_length, + # max_length=max_length, + eos_token_id=eos_token_id, + pad_token_id=pad_token_id, + generation_config=generation_config, + # renormalize_logits=True, + 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 + + # 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=(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 + + prompt = seq[:, :prompt_length] # (bs, prompt_length) + response = seq[:, prompt_length:] # (bs, 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(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_eos_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=batch_size) + + # empty cache before compute old_log_prob + torch.cuda.empty_cache() + + self.module.train() + return DataProto(batch=batch) diff --git a/verl/trainer/ppo/rollout/megatron/__init__.py b/verl/trainer/ppo/rollout/megatron/__init__.py new file mode 100644 index 000000000..d1b842bae --- /dev/null +++ b/verl/trainer/ppo/rollout/megatron/__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 .allgather_pp_model import AllGatherPPModel +from .hybrid_engine_naive_rollout import MegatronHybridEngineNaiveRollout +from .naive_rollout import MegatronNaiveRollout diff --git a/verl/trainer/ppo/rollout/naive/__init__.py b/verl/trainer/ppo/rollout/naive/__init__.py new file mode 100644 index 000000000..df81c8603 --- /dev/null +++ b/verl/trainer/ppo/rollout/naive/__init__.py @@ -0,0 +1,15 @@ +# 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 diff --git a/verl/trainer/ppo/rollout/naive/naive_rollout.py b/verl/trainer/ppo/rollout/naive/naive_rollout.py new file mode 100644 index 000000000..6f2e8d59b --- /dev/null +++ b/verl/trainer/ppo/rollout/naive/naive_rollout.py @@ -0,0 +1,119 @@ +# 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 +""" +from typing import Iterable, Union + +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__ = ['NativeRollout'] + + +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) + + prev_attention_mask = torch.logical_and(idx_next != eos_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/trainer/ppo/rollout/tokenizer.py b/verl/trainer/ppo/rollout/tokenizer.py new file mode 100644 index 000000000..c0dfa3a53 --- /dev/null +++ b/verl/trainer/ppo/rollout/tokenizer.py @@ -0,0 +1,162 @@ +# 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 +from typing import Dict, List, Union + +__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: Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.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, tf.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: Union[int, List[int]], + skip_special_tokens: bool = False) -> Union[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/trainer/ppo/rollout/vllm_rollout/__init__.py b/verl/trainer/ppo/rollout/vllm_rollout/__init__.py new file mode 100644 index 000000000..4f06d209f --- /dev/null +++ b/verl/trainer/ppo/rollout/vllm_rollout/__init__.py @@ -0,0 +1,15 @@ +# 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 .vllm_rollout import vLLMRollout \ No newline at end of file diff --git a/verl/trainer/ppo/rollout/vllm_rollout/vllm_rollout.py b/verl/trainer/ppo/rollout/vllm_rollout/vllm_rollout.py new file mode 100644 index 000000000..638e043ae --- /dev/null +++ b/verl/trainer/ppo/rollout/vllm_rollout/vllm_rollout.py @@ -0,0 +1,216 @@ +# 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. +""" +from typing import List +from contextlib import contextmanager +from omegaconf import DictConfig +import torch +import torch.distributed +from tensordict import TensorDict +from torch import nn + +from verl import DataProto +from verl.utils.torch_functional import get_eos_mask, pad_sequence_to_length +from verl.trainer.ppo.rollout.base import BaseRollout +from verl.third_party.vllm import LLM, vllm_version +from verl.third_party.vllm import parallel_state as vllm_ps +from vllm import SamplingParams + +# TODO +# 1. support pp in vllm +# 2. passing tokenizer is not necessary? no encoding/decoding is happending here +# 3. simplify init logics + + +# NOTE(sgm): add for verl. We can optimize it by making the dataloader yield List[int] without padding. +def _pre_process_inputs(pad_token_id, prompt_token_ids: torch.Tensor) -> List[int]: + # remove the left padding in the prompt token_id + # pad_token_id = self.llm_engine.tokenizer.pad_token_id if self.llm_engine.tokenizer.pad_token_id is not None else self.llm_engine.tokenizer.eos_token_id + non_pad_index = torch.nonzero(prompt_token_ids != pad_token_id, as_tuple=False)[0][0] + token_ids = prompt_token_ids[non_pad_index:].tolist() + return token_ids + + +class vLLMRollout(BaseRollout): + + def __init__(self, actor_module: nn.Module, config: DictConfig, tokenizer, model_hf_config, **kwargs): + """A vLLM rollout. It requires the module is supported by the vllm. + + Args: + module: module here follows huggingface APIs + config: DictConfig + tokenizer: the task/model tokenizer + model_hf_config: the huggingface config to initiallize the generating model in vllm + **kwargs: train_tp, for Megatron Backend to initialize hybrid engine (zero redundancy) process group + """ + super().__init__() + self.config = config + assert not (not config.enforce_eager and config.free_cache_engine), \ + "disable CUDA graph (enforce_eager = False) if free cache engine" + + tensor_parallel_size = self.config.get('tensor_model_parallel_size', 1) + + if kwargs.get('train_tp', None) is not None: + # deployed with megatron + import os + os.environ['CUDA_TIMER_STREAM_KAFKA_ENABLE'] = '0' + os.environ['MEGATRON_IMPORT_TIMERS'] = '0' + train_tp = kwargs.get('train_tp', None) + num_tp_per_train_tp = train_tp // tensor_parallel_size + if vllm_version == '0.4.2' or vllm_version == '0.5.4': + vllm_ps.initialize_parallel_state(tensor_model_parallel_size=tensor_parallel_size, + num_tp_per_train_tp=num_tp_per_train_tp) + + assert model_hf_config.max_position_embeddings >= config.prompt_length + config.response_length, \ + "model context length should be greater than total sequence length" + self.inference_engine = LLM(actor_module, + tokenizer=tokenizer, + model_hf_config=model_hf_config, + tensor_parallel_size=tensor_parallel_size, + dtype=config.dtype, + enforce_eager=config.enforce_eager, + gpu_memory_utilization=config.gpu_memory_utilization, + skip_tokenizer_init=False, + max_model_len=config.prompt_length + config.response_length, + load_format=config.load_format) + + # Offload vllm model to reduce peak memory usage + self.inference_engine.offload_model_weights() + + kwargs = dict( + n=1, + logprobs=1, # can be set to 0 and let actor to recompute + max_tokens=config.response_length, + ) + + # we may detokenize the result all together later + if vllm_version == '0.4.2' or vllm_version == '0.5.4': + kwargs['detokenize'] = False + + # supporting adding any sampling params from the config file + for k in config.keys(): + if hasattr(SamplingParams(), str(k)): + kwargs[k] = config.get(k) + + print(f"kwargs: {kwargs}") + self.sampling_params = SamplingParams(**kwargs) + + self.pad_token_id = tokenizer.pad_token_id + + @contextmanager + def update_sampling_params(self, **kwargs): + # update sampling params + old_sampling_params_args = {} + if kwargs: + for key, value in kwargs.items(): + if hasattr(self.sampling_params, key): + old_value = getattr(self.sampling_params, key) + old_sampling_params_args[key] = old_value + setattr(self.sampling_params, key, value) + yield + # roll back to previous sampling params + # if len(old_sampling_params_args): + for key, value in old_sampling_params_args.items(): + setattr(self.sampling_params, key, value) + + @torch.no_grad() + def generate_sequences(self, prompts: DataProto, **kwargs) -> DataProto: + # rebuild vllm cache engine + if self.config.free_cache_engine: + self.inference_engine.init_cache_engine() + + idx = prompts.batch['input_ids'] # (bs, prompt_length) + # left-padded attention_mask + attention_mask = prompts.batch['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) + + idx_list = [] + # parse idx from torch.Tensor to List[List[str]] + for i in range(batch_size): + idx_list.append(_pre_process_inputs(self.pad_token_id, idx[i])) + + do_sample = prompts.meta_info.get('do_sample', True) + if not do_sample: + kwargs = { + 'best_of': 1, + 'top_p': 1.0, + 'top_k': -1, + 'min_p': 0.0, + 'temperature': 0, + } + + # users can customize different sampling_params at different run + with self.update_sampling_params(**kwargs): + output = self.inference_engine.generate( + prompts=None, # because we have already convert it to prompt token id + sampling_params=self.sampling_params, + prompt_token_ids=idx_list, + use_tqdm=False) + + response = output[0].to(idx.device) # (bs, response_length) + log_probs = output[1].to(idx.device) # (bs, response_length) + + if response.shape[1] < self.config.response_length: + response = pad_sequence_to_length(response, self.config.response_length, self.pad_token_id) + log_probs = pad_sequence_to_length(log_probs, self.config.response_length, self.pad_token_id) + + seq = torch.cat([idx, response], dim=-1) + + 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(batch_size, 1) + + # TODO(sgm): fix position_ids on right_pad + # prompt: left pad + response: right pad + # attention_mask: [0,0,0,0,1,1,1,1, | 1,1,1,0,0,0,0,0] + # position_ids: [0,0,0,0,0,1,2,3, | 4,5,6,7,8,9,10,11] + response_position_ids = position_ids[:, -1:] + delta_position_id + position_ids = torch.cat([position_ids, response_position_ids], dim=-1) + response_attention_mask = get_eos_mask(response_id=response, eos_token=eos_token_id, dtype=attention_mask.dtype) + attention_mask = torch.cat((attention_mask, response_attention_mask), dim=-1) + + # all the tp ranks should contain the same data here. data in all ranks are valid + batch = TensorDict( + { + 'prompts': idx, + 'responses': response, + 'input_ids': seq, # here input_ids become the whole sentences + # 'old_log_probs': log_probs, # we will recompute old log prob with actor + 'attention_mask': attention_mask, + 'position_ids': position_ids + }, + batch_size=batch_size) + + # free vllm cache engine + if self.config.free_cache_engine: + self.inference_engine.free_cache_engine() + + return DataProto(batch=batch) diff --git a/verl/trainer/ppo/workers/__init__.py b/verl/trainer/ppo/workers/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/trainer/ppo/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/trainer/ppo/workers/fsdp_workers.py b/verl/trainer/ppo/workers/fsdp_workers.py new file mode 100644 index 000000000..aaf275ddf --- /dev/null +++ b/verl/trainer/ppo/workers/fsdp_workers.py @@ -0,0 +1,809 @@ +# 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 main entry point to run the PPO algorithm +""" + +import os +import logging +import warnings +import ray +import torch +import torch.distributed +from omegaconf import DictConfig, open_dict + +from single_controller.base import Worker +from single_controller.base.decorator import register, Dispatch +import verl.utils.torch_functional as verl_F +from verl import DataProto +from verl.trainer.ppo.actor import DataParallelPPOActor +from verl.utils.model import compute_position_id_with_mask +from verl.utils.fs import copy_local_path_from_hdfs +from verl.utils.fsdp_utils import get_fsdp_wrap_policy, load_fsdp_grad, offload_fsdp_grad, init_fn, get_init_weight_context_manager +from verl.utils.fsdp_utils import offload_fsdp_optimizer, offload_fsdp_param_and_grad, load_fsdp_optimizer, load_fsdp_param_and_grad +from verl.utils.import_utils import import_external_libs +from verl.utils.debug import log_gpu_memory_usage +import verl.utils.hdfs_io as hdfs_io + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv('VERL_PPO_LOGGING_LEVEL', 'WARN')) + + +@ray.remote +class ActorRolloutRefWorker(Worker): + """ + This worker can be instantiated as a standalone actor or a standalone rollout or a standalone reference policy + or a hybrid engine based on the config.rollout + """ + + def __init__(self, config: DictConfig, role: str): + super().__init__() + self.config = config + import torch.distributed + if not torch.distributed.is_initialized(): + torch.distributed.init_process_group(backend="nccl") + + # build device mesh + world_size = torch.distributed.get_world_size() + from torch.distributed.device_mesh import init_device_mesh + # TODO(sgm): support FSDP hybrid shard for larger model + self.device_mesh = init_device_mesh('cuda', mesh_shape=(world_size,), mesh_dim_names=['fsdp']) + + self.role = role + 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'] + + self._is_offload_param = False + self._is_offload_grad = False + self._is_offload_optimizer = False + if self._is_actor: + self._is_offload_param = self.config.actor.fsdp_config.get('param_offload', False) + self._is_offload_grad = self.config.actor.fsdp_config.get('grad_offload', False) + self._is_offload_optimizer = self.config.actor.fsdp_config.get('optimizer_offload', False) + elif self._is_ref: + # TODO: it seems that manual offload is slowly than FSDP offload + self._is_offload_param = self.config.ref.fsdp_config.get('param_offload', False) + + # normalize config + if self._is_actor: + self.config.actor.ppo_mini_batch_size //= self.device_mesh.shape[0] + self.config.actor.ppo_micro_batch_size //= self.device_mesh.shape[0] + if self._is_rollout: + self.config.rollout.log_prob_micro_batch_size //= self.device_mesh.shape[0] + if self._is_ref: + self.config.ref.log_prob_micro_batch_size //= self.device_mesh.shape[0] + + def _build_model_optimizer(self, + model_path, + fsdp_config, + optim_config, + override_model_config, + enable_gradient_checkpointing=False, + trust_remote_code=False): + from verl.utils.model import print_model_size, update_model_config + from verl.utils.torch_dtypes import PrecisionType + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig + from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, ShardingStrategy, MixedPrecision, \ + CPUOffload + from torch import optim + + log_gpu_memory_usage('Before init from HF AutoModel', logger=logger) + local_path = copy_local_path_from_hdfs(model_path) + + # note that we have to create model in fp32. Otherwise, the optimizer is in bf16, which is incorrect + # TODO(zhangchi.usc1992): 1. support create from random initialized model. 2. Support init with FSDP directly + self.tokenizer = AutoTokenizer.from_pretrained(local_path, trust_remote_code=trust_remote_code) + torch_dtype = fsdp_config.get('model_dtype', None) + if torch_dtype is None: + torch_dtype = torch.float32 if self._is_actor else torch.bfloat16 + else: + torch_dtype = PrecisionType.to_dtype(torch_dtype) + + # override model kwargs + actor_model_config = AutoConfig.from_pretrained(local_path, trust_remote_code=trust_remote_code) + + override_config_kwargs = { + 'bos_token_id': self.tokenizer.bos_token_id, + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + } + override_config_kwargs.update(override_model_config) + update_model_config(actor_model_config, override_config_kwargs=override_config_kwargs) + if self.rank == 0: + print(f'Model config after override: {actor_model_config}') + + # NOTE(fix me): tie_word_embedding causes meta_tensor init to hang + init_context = get_init_weight_context_manager(use_meta_tensor=not actor_model_config.tie_word_embeddings) + + with init_context(), warnings.catch_warnings(): + warnings.simplefilter("ignore") + actor_module = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=local_path, + torch_dtype=torch_dtype, + config=actor_model_config, + attn_implementation='flash_attention_2', + trust_remote_code=trust_remote_code) + # some parameters may not in torch_dtype. TODO(zhangchi.usc1992) remove this after we switch to fsdp2 + actor_module.to(torch_dtype) + + if enable_gradient_checkpointing: + actor_module.gradient_checkpointing_enable() + torch.distributed.barrier() + + if self.rank == 0: + print_model_size(actor_module) + + log_gpu_memory_usage('After init from HF AutoModel', logger=logger) + + # We wrap FSDP for rollout as well + mixed_precision_config = fsdp_config.get('mixed_precision', None) + 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) + + if self._is_ref: + mixed_precision = None + + auto_wrap_policy = get_fsdp_wrap_policy(module=actor_module, config=fsdp_config.get('wrap_policy', None)) + + if self._is_rollout and self.config.rollout.name == 'hf': + # TODO(zhangchi.usc1992, shengguangming) fix me. Current, auto_wrap_policy causes HFRollout to hang in Gemma + auto_wrap_policy = None + + print(f'wrap_policy: {auto_wrap_policy}') + + # TODO(sgm): support hybrid + if auto_wrap_policy is None: + sharding_strategy = ShardingStrategy.SHARD_GRAD_OP + else: + sharding_strategy = ShardingStrategy.FULL_SHARD + + # TODO: add transformer policy + actor_module_fsdp = FSDP( + actor_module, + param_init_fn=init_fn, + use_orig_params=False, + auto_wrap_policy=auto_wrap_policy, + device_id=torch.cuda.current_device(), + sharding_strategy=sharding_strategy, # zero3 + mixed_precision=mixed_precision, + sync_module_states=True, + device_mesh=self.device_mesh) + + log_gpu_memory_usage('After Actor FSDP init', logger=logger) + + # TODO: add more optimizer args into config + if self._is_actor: + from verl.utils.torch_functional import get_constant_schedule_with_warmup + actor_optimizer = optim.AdamW(actor_module_fsdp.parameters(), + lr=optim_config.lr, + betas=optim_config.get('betas', (0.9, 0.999)), + weight_decay=optim_config.get('weight_decay', 1e-2)) + + total_steps = optim_config.get('total_training_steps', 0) + num_warmup_steps_ratio = optim_config.get('lr_warmup_steps_ratio', 0.) + num_warmup_steps = int(num_warmup_steps_ratio * total_steps) + + print(f'Total steps: {total_steps}, num_warmup_steps: {num_warmup_steps}') + + actor_lr_scheduler = get_constant_schedule_with_warmup(optimizer=actor_optimizer, + num_warmup_steps=num_warmup_steps) + else: + actor_optimizer = None + actor_lr_scheduler = None + + log_gpu_memory_usage('After actor optimizer init', logger=logger) + + return actor_module_fsdp, actor_optimizer, actor_lr_scheduler, actor_model_config + + def _build_rollout(self): + if self.config.rollout.name == 'hf': + from verl.trainer.ppo.rollout import HFRollout + from verl.trainer.ppo.hybrid_engine import BaseShardingManager + rollout = HFRollout(module=self.actor_module_fsdp, config=self.config.rollout) + sharding_manager = BaseShardingManager() + # TODO: a sharding manager that do nothing? + elif self.config.rollout.name == 'vllm': + from verl.trainer.ppo.rollout.vllm_rollout import vLLMRollout + from verl.trainer.ppo.hybrid_engine import FSDPVLLMShardingManager + log_gpu_memory_usage('Before building vllm rollout', logger=None) + rollout = vLLMRollout(actor_module=self.actor_module_fsdp, + config=self.config.rollout, + tokenizer=self.tokenizer, + model_hf_config=self.actor_model_config) + log_gpu_memory_usage('After building vllm rollout', logger=None) + sharding_manager = FSDPVLLMShardingManager(module=self.actor_module_fsdp, + inference_engine=rollout.inference_engine, + model_config=self.actor_model_config, + full_params='hf' in self.config.rollout.load_format) + log_gpu_memory_usage('After building sharding manager', logger=None) + + return rollout, sharding_manager + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + # This is used to import external_lib into the huggingface systems + import_external_libs(self.config.model.get('external_lib', None)) + + from omegaconf import OmegaConf + override_model_config = OmegaConf.to_container(self.config.model.get('override_config', OmegaConf.create())) + + if self._is_actor or self._is_rollout: + # we need the model for actor and rollout + if self._is_actor: + optim_config = self.config.actor.optim + fsdp_config = self.config.actor.fsdp_config + else: + optim_config = None + fsdp_config = OmegaConf.create() + self.actor_module_fsdp, self.actor_optimizer, self.actor_lr_scheduler, self.actor_model_config = self._build_model_optimizer( + model_path=self.config.model.path, + fsdp_config=fsdp_config, + optim_config=optim_config, + override_model_config=override_model_config, + enable_gradient_checkpointing=self.config.model.get('enable_gradient_checkpointing', False), + trust_remote_code=self.config.model.get('trust_remote_code', False)) + + # get the original unwrapped module + self.actor_module = self.actor_module_fsdp._fsdp_wrapped_module + + if self._is_offload_param: + # param is require during state_dict in sharding manager + offload_fsdp_grad(module=self.actor_module_fsdp) + log_gpu_memory_usage('After offload actor grad during init', logger=logger) + if self._is_offload_optimizer: + offload_fsdp_optimizer(optimizer=self.actor_optimizer) + log_gpu_memory_usage('After offload actor optimizer during init', logger=logger) + # load from checkpoint + if self._is_actor: + OmegaConf.set_struct(self.config.actor, True) + self.actor = DataParallelPPOActor(config=self.config.actor, + actor_module=self.actor_module_fsdp, + actor_optimizer=self.actor_optimizer) + + if self._is_rollout: + self.rollout, self.sharding_manager = self._build_rollout() + + if self._is_ref: + self.ref_module_fsdp = self._build_model_optimizer(model_path=self.config.model.path, + fsdp_config=self.config.ref.fsdp_config, + optim_config=None, + override_model_config=override_model_config, + trust_remote_code=self.config.model.get( + 'trust_remote_code', False))[0] + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.ref_module_fsdp, offload_grad=self._is_offload_grad) + + OmegaConf.set_struct(self.config.ref, True) + self.ref_policy = DataParallelPPOActor(config=self.config.ref, actor_module=self.ref_module_fsdp) + + torch.cuda.empty_cache() + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def update_actor(self, data: DataProto): + data = data.to('cuda') + + assert self._is_actor + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.actor_module_fsdp, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + if self._is_offload_optimizer: + load_fsdp_optimizer(optimizer=self.actor_optimizer, device_id=torch.cuda.current_device()) + + data.batch = data.batch.cuda() + + log_gpu_memory_usage('Before update policy', logger=logger) + + metrics = self.actor.update_policy(data=data) + + self.actor_lr_scheduler.step() + lr = self.actor_lr_scheduler.get_last_lr()[0] + metrics['actor/lr(1e-4)'] = lr * 1e4 + + log_gpu_memory_usage('After update policy', logger=logger) + + # TODO: here, we should return all metrics + output = DataProto(meta_info={'metrics': metrics}) + output = output.to('cpu') + + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.actor_module_fsdp, offload_grad=self._is_offload_grad) + if self._is_offload_optimizer: + offload_fsdp_optimizer(optimizer=self.actor_optimizer) + torch.cuda.empty_cache() + return output + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def generate_sequences(self, prompts: DataProto): + prompts = prompts.to('cuda') + # set to False if it is validation + recompute_log_prob = prompts.meta_info.get('recompute_log_prob', True) + + assert self._is_rollout + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.actor_module_fsdp, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + + prompts.batch = prompts.batch.cuda() + meta_info = {'eos_token_id': self.tokenizer.eos_token_id, 'pad_token_id': self.tokenizer.pad_token_id} + prompts.meta_info.update(meta_info) + with self.sharding_manager: + log_gpu_memory_usage('After entering sharding manager', logger=logger) + + prompts = self.sharding_manager.preprocess_data(prompts) + output = self.rollout.generate_sequences(prompts=prompts) + + log_gpu_memory_usage('After rollout generation', logger=logger) + + output = self.sharding_manager.postprocess_data(output) + + if self._is_actor and recompute_log_prob: + # we should always recompute old_log_probs when it is HybridEngine + output.meta_info['micro_batch_size'] = self.config.rollout.log_prob_micro_batch_size + output.meta_info['temperature'] = self.config.rollout.temperature + old_log_probs = self.actor.compute_log_prob(data=output) + output.batch['old_log_probs'] = old_log_probs + + output = output.to('cpu') + + if self._is_offload_param: + # NOTE(sgm): the grad is already in CPU, only offload param here + offload_fsdp_param_and_grad(module=self.actor_module_fsdp, offload_grad=self._is_offload_grad) + # clear kv cache + torch.cuda.empty_cache() + log_gpu_memory_usage('After recompute log prob', logger=logger) + return output + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_ref_log_prob(self, data: DataProto): + assert self._is_ref + + data = data.to('cuda') + + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.ref_module_fsdp, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + + micro_batch_size = self.config.ref.log_prob_micro_batch_size + data.meta_info['micro_batch_size'] = micro_batch_size + data.meta_info['temperature'] = self.config.rollout.temperature + output = self.ref_policy.compute_log_prob(data=data) + output = DataProto.from_dict(tensors={'ref_log_prob': output}) + + output = output.to('cpu') + + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.ref_module_fsdp, offload_grad=self._is_offload_grad) + torch.cuda.empty_cache() + return output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, local_path, hdfs_path=None): + assert self._is_actor + import torch + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.actor_module_fsdp, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + + # TODO: support DCP and save sharded checkpoints + import torch.distributed + from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, StateDictType, FullStateDictConfig + cfg = FullStateDictConfig(offload_to_cpu=True, rank0_only=True) + with FSDP.state_dict_type(self.actor.actor_module, StateDictType.FULL_STATE_DICT, cfg): + state_dict = self.actor.actor_module.state_dict() + if self.rank == 0: + print(f'Saving actor checkpoint to {local_path}') + os.makedirs(local_path, exist_ok=True) + self.actor_module.save_pretrained(local_path, state_dict=state_dict) + self.tokenizer.save_pretrained(local_path) + if hdfs_path is not None: + print(f'Uploading actor checkpoint to {hdfs_path}') + hdfs_io.makedirs(hdfs_path, exist_ok=True) + hdfs_io.copy(src=local_path, dst=hdfs_path) + + torch.distributed.barrier() + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.actor_module_fsdp, offload_grad=self._is_offload_grad) + + +@ray.remote +class CriticWorker(Worker): + + def __init__(self, config): + super().__init__() + import torch.distributed + if not torch.distributed.is_initialized(): + torch.distributed.init_process_group(backend="nccl") + self.config = config + self._is_offload_param = self.config.model.fsdp_config.param_offload + self._is_offload_grad = self.config.model.fsdp_config.grad_offload + self._is_offload_optimizer = self.config.model.fsdp_config.optimizer_offload + + # normalize config + self.config.ppo_mini_batch_size //= torch.distributed.get_world_size() + self.config.ppo_micro_batch_size //= torch.distributed.get_world_size() + + def _build_critic_model_optimizer(self, config): + # the following line is necessary + from verl.utils.model import LambdaLayer, print_model_size, squeeze + from verl.utils.torch_dtypes import PrecisionType + from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, ShardingStrategy, MixedPrecision, \ + CPUOffload + from torch import optim + + local_path = copy_local_path_from_hdfs(config.model.path) + # note that the tokenizer between actor and critic may be different. So override tokenizer info with actor info + # using random initialized model from any architecture. May not be the same as Actor. + # TODO: support loading critic weights from RM. Support using AutoModelForTokenClassification + from transformers import AutoTokenizer + + tokenizer_path = copy_local_path_from_hdfs(config.model.tokenizer_path) + self.tokenizer = AutoTokenizer.from_pretrained(tokenizer_path, + trust_remote_code=config.model.get('trust_remote_code', False)) + + from omegaconf import OmegaConf + override_config = OmegaConf.to_container(self.config.model.get('override_config', OmegaConf.create())) + override_config_kwargs = { + 'bos_token_id': self.tokenizer.bos_token_id, + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + } + override_config_kwargs.update(override_config) + if self.rank == 0: + print(f'Critic overriding config {override_config_kwargs}') + + torch_dtype = self.config.model.fsdp_config.get('model_dtype', 'fp32') + torch_dtype = PrecisionType.to_dtype(torch_dtype) + + from transformers import AutoConfig, AutoModelForCausalLM + from torch import nn + + trust_remote_code = False + critic_model_config = AutoConfig.from_pretrained(local_path, trust_remote_code=trust_remote_code) + + init_context = get_init_weight_context_manager() + with init_context(), warnings.catch_warnings(): + warnings.simplefilter("ignore") + critic_module = AutoModelForCausalLM.from_pretrained(pretrained_model_name_or_path=local_path, + torch_dtype=torch_dtype, + config=critic_model_config, + attn_implementation='flash_attention_2', + trust_remote_code=trust_remote_code) + critic_module.lm_head = nn.Sequential(nn.Linear(critic_model_config.hidden_size, 1, dtype=torch_dtype), + LambdaLayer(fn=squeeze)) + + # some parameters may not in torch_dtype + critic_module.to(torch_dtype) + + if config.model.get('enable_gradient_checkpointing', False): + critic_module.gradient_checkpointing_enable() + if self.rank == 0: + print_model_size(critic_module) + + fsdp_config = self.config.model.fsdp_config + mixed_precision_config = fsdp_config.get('mixed_precision', None) + 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) + + auto_wrap_policy = get_fsdp_wrap_policy(module=critic_module, config=self.config.model.fsdp_config.wrap_policy) + + log_gpu_memory_usage('Before critic FSDP', logger=None) + + critic_module = FSDP(critic_module, + param_init_fn=init_fn, + use_orig_params=False, + auto_wrap_policy=auto_wrap_policy, + device_id=torch.cuda.current_device(), + sharding_strategy=ShardingStrategy.FULL_SHARD, + mixed_precision=mixed_precision, + sync_module_states=True) + + log_gpu_memory_usage('After critic FSDP', logger=None) + + critic_optimizer = optim.AdamW(critic_module.parameters(), + lr=config.optim.lr, + betas=config.optim.get('betas', (0.9, 0.999)), + weight_decay=config.optim.get('weight_decay', 1e-2)) + + total_steps = config.optim.get('total_training_steps', 0) + num_warmup_steps_ratio = config.optim.get('lr_warmup_steps_ratio', 0.) + num_warmup_steps = int(num_warmup_steps_ratio * total_steps) + + print(f'Total steps: {total_steps}, num_warmup_steps: {num_warmup_steps}') + + from verl.utils.torch_functional import get_constant_schedule_with_warmup + critic_lr_scheduler = get_constant_schedule_with_warmup(optimizer=critic_optimizer, + num_warmup_steps=num_warmup_steps) + + return critic_module, critic_optimizer, critic_lr_scheduler + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + # This is used to import external_lib into the huggingface systems + import_external_libs(self.config.model.get('external_lib', None)) + + from verl.trainer.ppo.critic import DataParallelPPOCritic + self.critic_module, self.critic_optimizer, self.critic_lr_scheduler = self._build_critic_model_optimizer( + self.config) + + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.critic_module, offload_grad=self._is_offload_grad) + if self._is_offload_optimizer: + offload_fsdp_optimizer(optimizer=self.critic_optimizer) + + self.critic = DataParallelPPOCritic(config=self.config, + critic_module=self.critic_module, + critic_optimizer=self.critic_optimizer) + torch.cuda.empty_cache() + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_values(self, data: DataProto): + data = data.to('cuda') + + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.critic_module, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + micro_batch_size = self.config.ppo_micro_batch_size + data.meta_info['micro_batch_size'] = micro_batch_size + values = self.critic.compute_values(data=data) + output = DataProto.from_dict(tensors={'values': values}) + output = output.to('cpu') + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.critic_module, offload_grad=self._is_offload_grad) + torch.cuda.empty_cache() + return output + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def update_critic(self, data: DataProto): + data = data.to('cuda') + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.critic_module, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + if self._is_offload_optimizer: + load_fsdp_optimizer(optimizer=self.critic_optimizer, device_id=torch.cuda.current_device()) + metrics = self.critic.update_critic(data=data) + + self.critic_lr_scheduler.step() + lr = self.critic_lr_scheduler.get_last_lr()[0] + metrics['critic/lr(1e-4)'] = lr * 1e4 + + output = DataProto(batch=None, meta_info={'metrics': metrics}) + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.critic_module, offload_grad=self._is_offload_grad) + if self._is_offload_optimizer: + offload_fsdp_optimizer(optimizer=self.critic_optimizer) + torch.cuda.empty_cache() + output = output.to('cpu') + return output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, local_path, hdfs_path=None): + import torch + if self._is_offload_param: + load_fsdp_param_and_grad(module=self.critic_module, + device_id=torch.cuda.current_device(), + load_grad=self._is_offload_grad) + + # TODO: support DCP and save sharded checkpoints + import torch.distributed + from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, StateDictType, FullStateDictConfig + cfg = FullStateDictConfig(offload_to_cpu=True, rank0_only=True) + with FSDP.state_dict_type(self.critic_module, StateDictType.FULL_STATE_DICT, cfg): + state_dict = self.critic_module.state_dict() + if self.rank == 0: + print(f'Saving critic checkpoint to {local_path}') + os.makedirs(local_path, exist_ok=True) + self.critic_module._fsdp_wrapped_module.save_pretrained(local_path, state_dict=state_dict) + self.tokenizer.save_pretrained(local_path) + if hdfs_path is not None: + print(f'Uploading critic checkpoint to {hdfs_path}') + hdfs_io.makedirs(hdfs_path, exist_ok=True) + hdfs_io.copy(src=local_path, dst=hdfs_path) + + torch.distributed.barrier() + if self._is_offload_param: + offload_fsdp_param_and_grad(module=self.critic_module, offload_grad=self._is_offload_grad) + + +@ray.remote +class RewardModelWorker(Worker): + """ + Note that we only implement the reward model that is subclass of AutoModelForSequenceClassification. + """ + + def __init__(self, config): + super().__init__() + import torch.distributed + if not torch.distributed.is_initialized(): + torch.distributed.init_process_group(backend="nccl") + self.config = config + + self.config.micro_batch_size //= torch.distributed.get_world_size() + + def _build_model(self, config): + # the following line is necessary + from transformers import AutoModelForSequenceClassification, AutoTokenizer, AutoConfig + from torch.distributed.fsdp import FullyShardedDataParallel as FSDP, ShardingStrategy, CPUOffload + + # download the checkpoint from hdfs + local_path = copy_local_path_from_hdfs(config.model.path) + + if self.config.model.input_tokenizer is None: + self._do_switch_chat_template = False + else: + self._do_switch_chat_template = True + input_tokenizer_local_path = copy_local_path_from_hdfs(config.model.input_tokenizer) + self.input_tokenizer = AutoTokenizer.from_pretrained(input_tokenizer_local_path, + trust_remote_code=config.model.get( + 'trust_remote_code', False)) + self.tokenizer = AutoTokenizer.from_pretrained(local_path, + trust_remote_code=config.model.get( + 'trust_remote_code', False)) + + trust_remote_code = config.model.get('trust_remote_code', False) + model_config = AutoConfig.from_pretrained(local_path, trust_remote_code=trust_remote_code) + # note that we have to create model in fp32. Otherwise, the optimizer is in bf16, which is incorrect + init_context = get_init_weight_context_manager(use_meta_tensor=not model_config.tie_word_embeddings) + + with init_context(), warnings.catch_warnings(): + warnings.simplefilter("ignore") + reward_module = AutoModelForSequenceClassification.from_pretrained(pretrained_model_name_or_path=local_path, + torch_dtype=torch.bfloat16, + attn_implementation='flash_attention_2', + trust_remote_code=trust_remote_code) + reward_module.to(torch.bfloat16) + auto_wrap_policy = get_fsdp_wrap_policy(module=reward_module, config=self.config.model.fsdp_config) + + reward_module = FSDP( + reward_module, + param_init_fn=init_fn, + use_orig_params=False, + auto_wrap_policy=auto_wrap_policy, + device_id=torch.cuda.current_device(), + sharding_strategy=ShardingStrategy.FULL_SHARD, # zero3 + sync_module_states=True, + cpu_offload=CPUOffload(offload_params=self.config.model.fsdp_config.param_offload)) + + return reward_module + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + # This is used to import external_lib into the huggingface systems + import_external_libs(self.config.model.get('external_lib', None)) + self.reward_module = self._build_model(config=self.config) + torch.cuda.empty_cache() + + def _forward_micro_batch(self, micro_batch): + with torch.no_grad(), torch.autocast(device_type='cuda', dtype=torch.bfloat16): + output = self.reward_module(input_ids=micro_batch['input_ids'], + attention_mask=micro_batch['attention_mask'], + position_ids=micro_batch['position_ids']) + rm_score = output.logits # (batch_size,) + rm_score = rm_score.squeeze(-1) + return rm_score + + def _expand_to_token_level(self, data: DataProto, scores: torch.Tensor): + batch_size = data.batch.batch_size[0] + # expand as token_level_reward + attention_mask = data.batch['attention_mask'] + position_ids = data.batch['position_ids'] + response_length = data.batch['responses'].shape[-1] + eos_mask_idx = torch.argmax(position_ids * attention_mask, dim=-1) # (bsz,) + token_level_scores = torch.zeros_like(attention_mask, dtype=scores.dtype) # (bsz, seqlen) + token_level_scores[torch.arange(batch_size), eos_mask_idx] = scores + + # select the response part + token_level_scores = token_level_scores[:, -response_length:] + + return token_level_scores + + def _switch_chat_template(self, data: DataProto): + src_max_length = data.batch['attention_mask'].shape[-1] + + src_tokenizer = self.input_tokenizer + target_tokenizer = self.tokenizer + + rm_input_ids = [] + rm_attention_mask = [] + + for i in range(data.batch.batch_size[0]): + # extract raw prompt + chat: list = data.non_tensor_batch['raw_prompt'][i].tolist() + + # extract response + response_ids = data.batch['responses'][i] + response_length = response_ids.shape[-1] + valid_response_length = data.batch['attention_mask'][i][-response_length:].sum() + valid_response_ids = response_ids[:valid_response_length] + + # decode + response = src_tokenizer.decode(valid_response_ids) + # remove bos and eos + response = response.replace(src_tokenizer.eos_token, '') + + chat.append({'role': 'assistant', 'content': response}) + + prompt_with_chat_template = target_tokenizer.apply_chat_template(chat, + add_generation_prompt=False, + tokenize=False) + if self.rank == 0 and i == 0: + # for debugging purpose + print(f'Switch template. chat: {prompt_with_chat_template}') + + # the maximum length is actually determined by the reward model itself + max_length = self.config.get('max_length', src_max_length) + if max_length is None: + max_length = src_max_length + input_ids, attention_mask = verl_F.tokenize_and_postprocess_data( + prompt=prompt_with_chat_template, + tokenizer=target_tokenizer, + max_length=max_length, + pad_token_id=target_tokenizer.pad_token_id, + left_pad=False, # right padding + truncation=self.config.get('truncation', 'right')) # truncate from the right + + rm_input_ids.append(input_ids) + rm_attention_mask.append(attention_mask) + + rm_input_ids = torch.cat(rm_input_ids, dim=0) + rm_attention_mask = torch.cat(rm_attention_mask, dim=0) + + rm_position_ids = compute_position_id_with_mask(rm_attention_mask) + + rm_inputs = {'input_ids': rm_input_ids, 'attention_mask': rm_attention_mask, 'position_ids': rm_position_ids} + + return DataProto.from_dict(rm_inputs) + + @register(dispatch_mode=Dispatch.DP_COMPUTE_PROTO) + def compute_rm_score(self, data: DataProto): + data = data.to('cuda') + if self._do_switch_chat_template: + rm_data = self._switch_chat_template(data) + + rm_data.batch = rm_data.batch.cuda() + micro_batches = rm_data.batch.split(self.config.micro_batch_size) + output = [] + for micro_batch in micro_batches: + rm_score = self._forward_micro_batch(micro_batch) + output.append(rm_score) + scores = torch.cat(output, dim=0) # (batch_size) + token_level_scores = self._expand_to_token_level(data, scores) + # Note that this is only the scores, may not be the final rewards used to train RL + output = DataProto.from_dict(tensors={'rm_scores': token_level_scores}) + output = output.to('cpu') + torch.cuda.empty_cache() + return output diff --git a/verl/trainer/ppo/workers/megatron_workers.py b/verl/trainer/ppo/workers/megatron_workers.py new file mode 100644 index 000000000..65b11a82c --- /dev/null +++ b/verl/trainer/ppo/workers/megatron_workers.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. +""" +The main entry point to run the PPO algorithm +""" + +import os +import logging +import ray +import torch +import torch.distributed +import torch.nn as nn +from omegaconf import DictConfig +from single_controller.base.megatron.worker import MegatronWorker +from verl.trainer.ppo.actor.megatron_actor import MegatronPPOActor +from verl.trainer.ppo.critic.megatron_critic import MegatronPPOCritic +from verl.trainer.ppo.hybrid_engine import AllGatherPPModel +from verl.trainer.ppo.reward_model.megatron.reward_model import MegatronRewardModel + +from single_controller.base.decorator import register, Dispatch +from verl import DataProto +from verl.utils.fs import copy_local_path_from_hdfs +from verl.utils.debug import log_gpu_memory_usage +from verl.utils.model import load_megatron_model_weights +from verl.utils.megatron_utils import init_model_parallel_config +from verl.utils.megatron_utils import offload_megatron_param_and_grad, load_megatron_param_and_grad + +from megatron.core import parallel_state as mpu +from megatron.core import ModelParallelConfig + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv('VERL_PPO_LOGGING_LEVEL', 'WARN')) + + +def set_random_seed(seed): + import torch + import numpy as np + import random + torch.manual_seed(seed) + np.random.seed(seed) + random.seed(seed) + if torch.cuda.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' + + +@ray.remote +class ActorRolloutRefWorker(MegatronWorker): + """ + This worker can be instantiated as a standalone actor or a standalone rollout or a standalone reference policy + or a hybrid engine based on the config.rollout + """ + + def __init__(self, config: DictConfig, role: str): + super().__init__() + self.config = config + + # NOTE(sgm): We utilize colocate WorkerGroup by default. + # As a result, Workers for different model share the same process. + # Therefore, we only require one distribute initialization. + # To utilize different parallel startegy in different models: + # 1, users should disable WorkerDict; 2.assign different ResourcePool to different models, + # 3. and apply the following patch in ray==2.10, https://github.com/ray-project/ray/pull/44385 + if not torch.distributed.is_initialized(): + rank = int(os.environ['LOCAL_RANK']) + torch.distributed.init_process_group(backend="nccl") + torch.cuda.set_device(rank) + + if self.config.actor.megatron.sequence_parallel: + os.environ['CUDA_DEVICE_MAX_CONNECTIONS'] = '1' + mpu.initialize_model_parallel( + tensor_model_parallel_size=self.config.actor.megatron.tensor_model_parallel_size, + pipeline_model_parallel_size=self.config.actor.megatron.pipeline_model_parallel_size, + virtual_pipeline_model_parallel_size=None, + pipeline_model_parallel_split_rank=None, + use_sharp=False, + context_parallel_size=1, + expert_model_parallel_size=1, + nccl_communicator_config_path=None, + ) + + set_random_seed(seed=self.config.actor.megatron.seed) + + self.role = role + 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'] + + # TODO(sgm): Currently, we only support reference model param offload + # will support other offload later + self._is_offload_param = False + self._is_offload_grad = False + self._is_offload_optimizer = False + + # normalize config + if self._is_actor and self._is_rollout: + self.config.actor.ppo_mini_batch_size //= mpu.get_data_parallel_world_size() + self.config.actor.ppo_micro_batch_size //= mpu.get_data_parallel_world_size() + self.config.rollout.log_prob_micro_batch_size //= mpu.get_data_parallel_world_size() + self._is_offload_param = self.config.actor.get('param_offload', False) + self._is_offload_grad = self.config.actor.get('grad_offload', False) + self._is_offload_optimizer = self.config.actor.get('optimizer_offload', False) + elif self._is_ref: + self.config.ref.log_prob_micro_batch_size //= mpu.get_data_parallel_world_size() + self._is_offload_param = self.config.ref.get('param_offload', False) + + def _build_model_optimizer(self, + model_path, + megatron_config: ModelParallelConfig, + optim_config, + override_model_config, + enable_gradient_checkpointing=False): + from verl.utils.megatron.optimizer import get_megatron_optimizer + from megatron.core.models.gpt.gpt_model import ModelType + from verl.utils.model import print_model_size, update_model_config + from verl.utils.megatron_utils import get_model, init_megatron_optim_config + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig + + # Step 1: initialize the tokenizer + local_path = copy_local_path_from_hdfs(model_path) + self.tokenizer = AutoTokenizer.from_pretrained(local_path) + + # Step 2: get the actor_model_config + actor_model_config = AutoConfig.from_pretrained(local_path) + + override_config_kwargs = { + 'bos_token_id': self.tokenizer.bos_token_id, + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + } + override_config_kwargs.update(override_model_config) + update_model_config(actor_model_config, override_config_kwargs=override_config_kwargs) + + if self.rank == 0: + print(f'Model config after override: {actor_model_config}') + + def megatron_actor_model_provider(pre_process, post_process): + from verl.utils.model import get_parallel_model_from_config + # vpp is not supported yet because it will hang for some reason. Need debugging + vpp_rank = mpu.get_virtual_pipeline_model_parallel_rank() # this will be set inside get_model + # this_megatron_config = copy.deepcopy(megatron_config) + # this_megatron_config.virtual_pipeline_model_parallel_rank = vpp_rank + parallel_model = get_parallel_model_from_config(config=actor_model_config, + megatron_config=megatron_config, + pre_process=pre_process, + post_process=post_process, + value=False) + parallel_model.cuda() + return parallel_model + + # Step 3: initialize the megatron model + if self._is_actor and self._is_rollout: + # Initialize the 3D HybridEngine + hybrid_engine = AllGatherPPModel(model_provider=megatron_actor_model_provider) + # Fetch the model at current rank + actor_module = hybrid_engine.this_rank_models + if isinstance(actor_module, nn.ModuleList): + actor_module = [actor_module[0]] + if self.config.actor.load_weight: + load_megatron_model_weights(self.config, + actor_model_config, + actor_module, + params_dtype=megatron_config.params_dtype, + is_value_model=False) + + if self.rank == 0: + print_model_size(actor_module[0]) + log_gpu_memory_usage('After AllGatherPPModel init', logger=logger) + elif self._is_ref: + print(f'self.config.ref.load_weight: {self.config.ref.load_weight}') + ref_module = get_model(model_provider_func=megatron_actor_model_provider, + model_type=ModelType.encoder_or_decoder, + wrap_with_ddp=False) + # ref_module = nn.ModuleList(ref_module) + + if self.config.ref.load_weight: # should align with the actor: + assert self.config.actor.load_weight == self.config.ref.load_weight + print(f'load ref weight start') + load_megatron_model_weights(self.config, + actor_model_config, + ref_module, + params_dtype=megatron_config.params_dtype, + is_value_model=False) + log_gpu_memory_usage('After ref module init', logger=logger) + return ref_module, actor_model_config + + # TODO: add more optimizer args into config + if self._is_actor: + optim_config = init_megatron_optim_config(optim_config) + actor_optimizer = get_megatron_optimizer(model=actor_module, config=optim_config) + else: + optim_config = None + actor_optimizer = None + + log_gpu_memory_usage('After actor optimizer init', logger=logger) + + return actor_module, hybrid_engine, actor_optimizer, actor_model_config, optim_config + + def _build_rollout(self): + if self.config.rollout.name == 'vllm': + from verl.trainer.ppo.rollout.vllm_rollout import vLLMRollout + from verl.trainer.ppo.hybrid_engine import MegatronVLLMShardingManager + from verl.utils.model import normalize_pp_vpp_params + + # NOTE(sgm): If the QKV and gate_up projection layer are concate together in actor, + # we will reorganize their weight format when resharding from actor to rollout. + layer_name_mapping = { + "qkv_layer_name": + self.config.rollout.layer_name_map.get("qkv_layer_name", "qkv"), + "gate_proj_layer_name": + self.config.rollout.layer_name_map.get("gate_proj_layer_name", "linear_fc1.weight"), + } + + # reshard the weight partition from actor to rollout to initialize the rollout class + # create a new cuda space for parameters not in this pp rank + self.hybrid_engine.load_params_to_cuda() + # broadcast the parameters from pp rank to other ranks + self.hybrid_engine.allgather_params() + # obtain name to parameters in pp/vpp + params = self.hybrid_engine.get_all_params() + # update the param name for the + params = normalize_pp_vpp_params(params=params, + num_hidden_layers=self.actor_model_config.num_hidden_layers, + layer_name='layers') + rollout = vLLMRollout(actor_module=params, + config=self.config.rollout, + tokenizer=self.tokenizer, + model_hf_config=self.actor_model_config, + train_tp=mpu.get_tensor_model_parallel_world_size()) + log_gpu_memory_usage('After building vllm rollout', logger=logger) + + # perform weight resharding between actor and rollout + sharding_manager = MegatronVLLMShardingManager(module=self.hybrid_engine, + inference_engine=rollout.inference_engine, + model_config=self.actor_model_config, + layer_name_mapping=layer_name_mapping) + log_gpu_memory_usage('After building sharding manager', logger=logger) + else: + NotImplementedError('Only vllmRollout is supported with Megatron now') + + return rollout, sharding_manager + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + if self.config.model.get('external_lib', None) is not None: + # This is used to import external_lib into the huggingface systems + import importlib + importlib.import_module(self.config.model.external_lib) + + from omegaconf import OmegaConf + from verl.utils.torch_dtypes import PrecisionType + override_model_config = OmegaConf.to_container(self.config.model.get('override_config', OmegaConf.create())) + torch_dtype = torch.bfloat16 + + megatron_config = OmegaConf.create({ + 'sequence_parallel': self.config.actor.megatron.get('sequence_parallel', True), + 'param_dtype': PrecisionType.to_str(torch_dtype), + 'tensor_model_parallel_size': mpu.get_tensor_model_parallel_world_size(), + 'pipeline_model_parallel_rank': mpu.get_pipeline_model_parallel_rank(), + 'pipeline_model_parallel_size': mpu.get_pipeline_model_parallel_world_size(), + 'virtual_pipeline_model_parallel_rank': mpu.get_virtual_pipeline_model_parallel_rank(), + 'virtual_pipeline_model_parallel_size': mpu.get_virtual_pipeline_model_parallel_world_size() + }) + + megatron_config = init_model_parallel_config(megatron_config) + + if self._is_actor or self._is_rollout: + # we need the model for actor and rollout + if self._is_actor: + optim_config = self.config.actor.optim + else: + optim_config = None + self.actor_module, self.hybrid_engine, self.actor_optimizer, \ + self.actor_model_config, self.actor_optim_config = self._build_model_optimizer( + model_path=self.config.model.path, + megatron_config=megatron_config, + optim_config=optim_config, + override_model_config=override_model_config, + ) + + if self._is_actor: + self.actor = MegatronPPOActor(config=self.config.actor, + model_config=self.actor_model_config, + megatron_config=megatron_config, + actor_module=self.actor_module, + actor_optimizer=self.actor_optimizer, + actor_optimizer_config=self.actor_optim_config) + + if self._is_rollout: + self.rollout, self.sharding_manager = self._build_rollout() + + if self._is_ref: + self.ref_module, self.ref_model_config = self._build_model_optimizer( + model_path=self.config.model.path, + megatron_config=megatron_config, + optim_config=None, + override_model_config=override_model_config, + ) + self.ref_policy = MegatronPPOActor(config=self.config.ref, + model_config=self.ref_model_config, + megatron_config=megatron_config, + actor_module=self.ref_module, + actor_optimizer=None, + actor_optimizer_config=None) + + torch.cuda.empty_cache() + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def update_actor(self, data: DataProto): + assert self._is_actor + + data.batch = data.batch.cuda() + + log_gpu_memory_usage('Before update policy', logger=logger) + + dataloader = self.actor.make_minibatch_iterator(data=data) + metrics = self.actor.update_policy(dataloader=dataloader) + + log_gpu_memory_usage('After update policy', logger=logger) + + # TODO: here, we should return all metrics + output = DataProto(meta_info={'metrics': metrics}) + output = output.to('cpu') + torch.cuda.empty_cache() + return output + + @register(dispatch_mode=Dispatch.MEGATRON_PP_AS_DP_PROTO) + def generate_sequences(self, prompts: DataProto): + assert self._is_rollout + + prompts.batch = prompts.batch.cuda() + meta_info = {'eos_token_id': self.tokenizer.eos_token_id, 'pad_token_id': self.tokenizer.pad_token_id} + prompts.meta_info.update(meta_info) + with self.sharding_manager: + log_gpu_memory_usage('After entering sharding manager', logger=logger) + + prompts = self.sharding_manager.preprocess_data(prompts) + output = self.rollout.generate_sequences(prompts=prompts) + + log_gpu_memory_usage('After rollout generation', logger=logger) + + output = self.sharding_manager.postprocess_data(output) + + validate = prompts.meta_info.get('validate', False) + if self._is_actor and not validate: + # we should always recompute old_log_probs when it is HybridEngine + output.meta_info['micro_batch_size'] = self.config.rollout.log_prob_micro_batch_size + output.meta_info['temperature'] = self.config.rollout.temperature + old_log_probs = self.actor.compute_log_prob(data=output) + output.batch['old_log_probs'] = old_log_probs + + output = output.to('cpu') + # clear kv cache + torch.cuda.empty_cache() + log_gpu_memory_usage('After recompute log prob', logger=logger) + return output + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_ref_log_prob(self, data: DataProto): + data = data.to('cuda') + + assert self._is_ref + if self._is_offload_param: + load_megatron_param_and_grad(self.ref_module, torch.cuda.current_device(), self._is_offload_grad) + + micro_batch_size = self.config.rollout.log_prob_micro_batch_size + data.meta_info['micro_batch_size'] = micro_batch_size + data.meta_info['temperature'] = self.config.rollout.temperature + output = self.ref_policy.compute_log_prob(data=data) + output = DataProto.from_dict(tensors={'ref_log_prob': output}) + output = output.to('cpu') + if self._is_offload_param: + offload_megatron_param_and_grad(self.ref_module, self._is_offload_grad) + torch.cuda.empty_cache() + return output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def load_checkpoint(self, checkpoint_path): + pass + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def load_pretrained_model(self, checkpoint_path): + pass + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, checkpoint_path): + assert self._is_actor + pass + + +@ray.remote +class CriticWorker(MegatronWorker): + + def __init__(self, config): + super().__init__() + self.config = config + + # NOTE(sgm): We utilize colocate WorkerGroup by default. + # As a result, Workers for different model share the same process. + # Therefore, we only require one distribute initialization. + # To utilize different parallel startegy in different models: + # 1, users should disable WorkerDict; 2.assign different ResourcePool to different models, + # 3. and apply the following patch in ray==2.10, https://github.com/ray-project/ray/pull/44385 + if not torch.distributed.is_initialized(): + rank = int(os.environ['LOCAL_RANK']) + torch.distributed.init_process_group(backend="nccl") + torch.cuda.set_device(rank) + + if self.config.megatron.sequence_parallel: + os.environ['CUDA_DEVICE_MAX_CONNECTIONS'] = '1' + mpu.initialize_model_parallel( + tensor_model_parallel_size=self.config.megatron.tensor_model_parallel_size, + pipeline_model_parallel_size=self.config.megatron.pipeline_model_parallel_size, + virtual_pipeline_model_parallel_size=None, + pipeline_model_parallel_split_rank=None, + use_sharp=False, + context_parallel_size=1, + expert_model_parallel_size=1, + nccl_communicator_config_path=None, + ) + + set_random_seed(seed=self.config.megatron.seed) + + # normalize config + self.config.ppo_mini_batch_size //= mpu.get_data_parallel_world_size() + self.config.ppo_micro_batch_size //= mpu.get_data_parallel_world_size() + + # TODO(sgm): support critic model offload + + def _build_critic_model_optimizer(self, + model_path, + megatron_config: ModelParallelConfig, + optim_config, + override_model_config, + enable_gradient_checkpointing=False): + from megatron.core.models.gpt.gpt_model import ModelType + from verl.utils.model import print_model_size, update_model_config + from verl.utils.megatron.optimizer import get_megatron_optimizer + from verl.utils.megatron_utils import get_model, init_megatron_optim_config, init_model_parallel_config + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig + + # Step 1: initialize the tokenizer + local_path = copy_local_path_from_hdfs(model_path) + self.tokenizer = AutoTokenizer.from_pretrained(local_path) + + # Step 2: get the actor_model_config + critic_model_config = AutoConfig.from_pretrained(local_path) + + override_config_kwargs = { + 'bos_token_id': self.tokenizer.bos_token_id, + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + } + override_config_kwargs.update(override_model_config) + update_model_config(critic_model_config, override_config_kwargs=override_config_kwargs) + + if self.rank == 0: + print(f'Model config after override: {critic_model_config}') + + def megatron_critic_model_provider(pre_process, post_process): + from verl.utils.model import get_parallel_model_from_config + # TODO: support vpp here + # vpp_rank = mpu.get_virtual_pipeline_model_parallel_rank() # this will be set inside get_model + # this_megatron_config = copy.deepcopy(megatron_config) + # this_megatron_config.virtual_pipeline_model_parallel_rank = vpp_rank + parallel_model = get_parallel_model_from_config(config=critic_model_config, + megatron_config=megatron_config, + pre_process=pre_process, + post_process=post_process, + value=True) + parallel_model.cuda() + return parallel_model + + # Step 3: initialize the megatron model + critic_module = get_model(model_provider_func=megatron_critic_model_provider, + model_type=ModelType.encoder_or_decoder, + wrap_with_ddp=True) + # note that here critic_module will be a list to be compatible with the construction of interleaved pp (vpp). + # but here, we do not use pp (vpp) yet. For simplicity, we remove the list + # critic_module = nn.ModuleList(critic_module) + + if self.config.load_weight: + load_megatron_model_weights(self.config, + critic_model_config, + critic_module, + params_dtype=megatron_config.params_dtype, + is_value_model=True) + if self.rank == 0: + print_model_size(critic_module[0]) + + # TODO: add more optimizer args into config + optim_config = init_megatron_optim_config(optim_config) + critic_optimizer = get_megatron_optimizer(model=critic_module, config=optim_config) + torch.cuda.empty_cache() + return critic_module, critic_optimizer, critic_model_config, optim_config + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + # create critic + from omegaconf import OmegaConf + from verl.utils.torch_dtypes import PrecisionType + + if self.config.model.get('external_lib', None) is not None: + # This is used to import external_lib into the huggingface systems + import importlib + importlib.import_module(self.config.model.external_lib) + override_model_config = OmegaConf.to_container(self.config.model.get('override_config', OmegaConf.create())) + torch_dtype = torch.bfloat16 + + megatron_config = OmegaConf.create({ + 'sequence_parallel': self.config.megatron.get('sequence_parallel', True), + 'param_dtype': PrecisionType.to_str(torch_dtype), + 'tensor_model_parallel_size': mpu.get_tensor_model_parallel_world_size(), + 'pipeline_model_parallel_rank': mpu.get_pipeline_model_parallel_rank(), + 'pipeline_model_parallel_size': mpu.get_pipeline_model_parallel_world_size(), + 'virtual_pipeline_model_parallel_rank': mpu.get_virtual_pipeline_model_parallel_rank(), + 'virtual_pipeline_model_parallel_size': mpu.get_virtual_pipeline_model_parallel_world_size() + }) + + megatron_config = init_model_parallel_config(megatron_config) + + critic_module, critic_optimizer, critic_model_config, critic_optimizer_config = self._build_critic_model_optimizer( + model_path=self.config.model.path, + megatron_config=megatron_config, + optim_config=self.config.optim, + override_model_config=override_model_config) + self.critic = MegatronPPOCritic(config=self.config, + model_config=critic_model_config, + megatron_config=megatron_config, + critic_module=critic_module, + critic_optimizer=critic_optimizer, + critic_optimizer_config=critic_optimizer_config) + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_values(self, data: DataProto): + data = data.to('cuda') + values = self.critic.compute_values(data=data) + output = DataProto.from_dict(tensors={'values': values}) + output = output.to('cpu') + return output + + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def update_critic(self, data: DataProto): + data = data.to('cuda') + dataloader = self.critic.make_minibatch_iterator(data) + metrics = self.critic.update_critic(dataloader=dataloader) + output = DataProto(batch=None, meta_info={'metrics': metrics}) + output = output.to('cpu') + return output + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def load_checkpoint(self, checkpoint_path): + pass + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def save_checkpoint(self, checkpoint_path): + pass + + +@ray.remote +class RewardModelWorker(MegatronWorker): + """ + Note that we only implement the reward model that is subclass of AutoModelForSequenceClassification. + """ + + def __init__(self, config): + super().__init__() + self.config = config + + # NOTE(sgm): We utilize colocate WorkerGroup by default. + # As a result, Workers for different model share the same process. + # Therefore, we only require one distribute initialization. + # To utilize different parallel startegy in different models: + # 1, users should disable WorkerDict; 2.assign different ResourcePool to different models, + # 3. and apply the following patch in ray==2.10, https://github.com/ray-project/ray/pull/44385 + if not torch.distributed.is_initialized(): + rank = int(os.environ['LOCAL_RANK']) + torch.distributed.init_process_group(backend="nccl") + torch.cuda.set_device(rank) + + if self.config.megatron.sequence_parallel: + os.environ['CUDA_DEVICE_MAX_CONNECTIONS'] = '1' + mpu.initialize_model_parallel( + tensor_model_parallel_size=self.config.megatron.tensor_model_parallel_size, + pipeline_model_parallel_size=self.config.megatron.pipeline_model_parallel_size, + virtual_pipeline_model_parallel_size=None, + pipeline_model_parallel_split_rank=None, + use_sharp=False, + context_parallel_size=1, + expert_model_parallel_size=1, + nccl_communicator_config_path=None, + ) + + set_random_seed(seed=self.config.megatron.seed) + + # normalize config + self.config.micro_batch_size //= mpu.get_data_parallel_world_size() + + def _build_rm_model(self, model_path, megatron_config: ModelParallelConfig, override_model_config): + from megatron.core.models.gpt.gpt_model import ModelType + from verl.utils.model import print_model_size, update_model_config + from verl.utils.megatron_utils import get_model + from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig + + # Step 1: initialize the tokenizer + local_path = copy_local_path_from_hdfs(model_path) + self.tokenizer = AutoTokenizer.from_pretrained(local_path) + + # Step 2: get the actor_model_config + rm_model_config = AutoConfig.from_pretrained(local_path) + + override_config_kwargs = { + 'bos_token_id': self.tokenizer.bos_token_id, + 'eos_token_id': self.tokenizer.eos_token_id, + 'pad_token_id': self.tokenizer.pad_token_id, + } + override_config_kwargs.update(override_model_config) + update_model_config(rm_model_config, override_config_kwargs=override_config_kwargs) + + if self.rank == 0: + print(f'Model config after override: {rm_model_config}') + + def megatron_rm_model_provider(pre_process, post_process): + from verl.utils.model import get_parallel_model_from_config + # vpp is not supported yet because it will hang for some reason. Need debugging + vpp_rank = mpu.get_virtual_pipeline_model_parallel_rank() # this will be set inside get_model + # this_megatron_config = copy.deepcopy(megatron_config) + # this_megatron_config.virtual_pipeline_model_parallel_rank = vpp_rank + parallel_model = get_parallel_model_from_config(config=rm_model_config, + megatron_config=megatron_config, + pre_process=pre_process, + post_process=post_process, + value=True) + parallel_model.cuda() + return parallel_model + + # Step 3: initialize the megatron model + reward_model = get_model(model_provider_func=megatron_rm_model_provider, + model_type=ModelType.encoder_or_decoder, + wrap_with_ddp=False) + # note that here critic_module will be a list to be compatible with the construction of interleaved pp (vpp). + # but here, we do not use pp (vpp) yet. For simplicity, we remove the list + # reward_model = nn.ModuleList(reward_model) + + if self.config.load_weight: + load_megatron_model_weights(self.config, + rm_model_config, + reward_model, + params_dtype=megatron_config.params_dtype, + is_value_model=True) + + # TODO: add more optimizer args into config + torch.cuda.empty_cache() + return reward_model, rm_model_config + + @register(dispatch_mode=Dispatch.ONE_TO_ALL) + def init_model(self): + # create critic + from omegaconf import OmegaConf + from verl.utils.torch_dtypes import PrecisionType + from transformers import AutoTokenizer + + if self.config.model.get('external_lib', None) is not None: + # This is used to import external_lib into the huggingface systems + import importlib + importlib.import_module(self.config.model.external_lib) + override_model_config = OmegaConf.to_container(self.config.model.get('override_config', OmegaConf.create())) + + sft_tokenizer_local_path = copy_local_path_from_hdfs(self.config.model.input_tokenizer) + sft_tokenizer = AutoTokenizer.from_pretrained(sft_tokenizer_local_path) + rm_tokenizer_path = self.config.model.get('rm_tokenizer', None) + rm_tokenizer = None + if rm_tokenizer_path is not None: + rm_tokenizer_local_path = copy_local_path_from_hdfs(rm_tokenizer_path) + rm_tokenizer = AutoTokenizer.from_pretrained(rm_tokenizer_local_path) + torch_dtype = torch.bfloat16 + + megatron_config = OmegaConf.create({ + 'sequence_parallel': self.config.megatron.get('sequence_parallel', True), + 'param_dtype': PrecisionType.to_str(torch_dtype), + 'tensor_model_parallel_size': mpu.get_tensor_model_parallel_world_size(), + 'pipeline_model_parallel_rank': mpu.get_pipeline_model_parallel_rank(), + 'pipeline_model_parallel_size': mpu.get_pipeline_model_parallel_world_size(), + 'virtual_pipeline_model_parallel_rank': mpu.get_virtual_pipeline_model_parallel_rank(), + 'virtual_pipeline_model_parallel_size': mpu.get_virtual_pipeline_model_parallel_world_size() + }) + + megatron_config = init_model_parallel_config(megatron_config) + + reward_model_module, reward_model_config = self._build_rm_model( + model_path=self.config.model.path, + megatron_config=megatron_config, + override_model_config=override_model_config, + ) + # FIXME(sgm): reward model param offload is implemented in MegatronRewardModel + # should be implemented in workers + self.rm = MegatronRewardModel(config=self.config, + reward_model_module=reward_model_module, + model_config=reward_model_config, + megatron_config=megatron_config, + sft_tokenizer=sft_tokenizer, + rm_tokenizer=rm_tokenizer) + + # TODO: reward model use itself tokenizer instead of sft tokenizer + # the input_ids, responses, attention_mask and position_ids may be different! + @register(dispatch_mode=Dispatch.MEGATRON_COMPUTE_PROTO) + def compute_rm_score(self, data: DataProto): + data.batch = data.batch.cuda() + output = self.rm.compute_reward(data) + output = output.to('cpu') + return output diff --git a/verl/trainer/runtime_env.yaml b/verl/trainer/runtime_env.yaml new file mode 100644 index 000000000..87bd05a9a --- /dev/null +++ b/verl/trainer/runtime_env.yaml @@ -0,0 +1,5 @@ +working_dir: ./ +excludes: ["/.git/"] +env_vars: + TORCH_NCCL_AVOID_RECORD_STREAMS: "1" + VLLM_ATTENTION_BACKEND: "XFORMERS" \ No newline at end of file diff --git a/verl/utils/__init__.py b/verl/utils/__init__.py new file mode 100644 index 000000000..7a7aadbc9 --- /dev/null +++ b/verl/utils/__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. \ No newline at end of file diff --git a/verl/utils/config.py b/verl/utils/config.py new file mode 100644 index 000000000..5c9298c42 --- /dev/null +++ b/verl/utils/config.py @@ -0,0 +1,23 @@ +# 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 Dict + +from omegaconf import DictConfig + + +def update_dict_with_config(dictionary: Dict, config: DictConfig): + for key in dictionary: + if hasattr(config, key): + dictionary[key] = getattr(config, key) diff --git a/verl/utils/dataset/README.md b/verl/utils/dataset/README.md new file mode 100644 index 000000000..f886a70aa --- /dev/null +++ b/verl/utils/dataset/README.md @@ -0,0 +1,16 @@ +# Dataset Format +## RLHF dataset +We combine all the data sources into a single parquet files. We directly organize the prompt into the chat format so that multi-turn chats can be easily incorporated. In the prompt, we may add instruction following texts to guide the model output the answers in a particular format so that we can extract the answers. + +Math problems +```json +{ + "data_source": "openai/gsm8k", + "prompt": [{"role": "user", "content": "Natalia sold clips to 48 of her friends in April, and then she sold half as many clips in May. How many clips did Natalia sell altogether in April and May? Let's think step by step and output the final answer after \"####\""}], + "ability": "math", + "reward_model": { + "style": "rule", + "ground_truth": ["72"] + }, +} +``` diff --git a/verl/utils/dataset/__init__.py b/verl/utils/dataset/__init__.py new file mode 100644 index 000000000..b833e441e --- /dev/null +++ b/verl/utils/dataset/__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 .rl_dataset import RLHFDataset +from .rm_dataset import RMDataset +from .sft_dataset import SFTDataset diff --git a/verl/utils/dataset/rl_dataset.py b/verl/utils/dataset/rl_dataset.py new file mode 100644 index 000000000..d64f6b53e --- /dev/null +++ b/verl/utils/dataset/rl_dataset.py @@ -0,0 +1,180 @@ +# 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 omegaconf import ListConfig +import os +from typing import List, Union + +import pandas as pd + +import torch +import numpy as np +from torch.utils.data import Dataset, DataLoader +from transformers import AutoTokenizer, PreTrainedTokenizer +from verl.utils.fs import copy_local_path_from_hdfs + +from verl.utils.model import compute_position_id_with_mask +import verl.utils.torch_functional as verl_F + + +def collate_fn(data_list: list[dict]) -> dict: + tensors = {} + non_tensors = {} + + for data in data_list: + for key, val in data.items(): + if isinstance(val, torch.Tensor): + if key not in tensors: + tensors[key] = [] + tensors[key].append(val) + else: + if key not in non_tensors: + non_tensors[key] = [] + non_tensors[key].append(val) + + for key, val in tensors.items(): + tensors[key] = torch.stack(val, dim=0) + + for key, val in non_tensors.items(): + non_tensors[key] = np.array(val, dtype=object) + + output = {} + output.update(tensors) + output.update(non_tensors) + return output + + +class RLHFDataset(Dataset): + """ + We assume the dataset contains a column that contains prompts and other information + """ + + def __init__(self, + parquet_files: Union[str, List[str]], + tokenizer: PreTrainedTokenizer, + prompt_key='prompt', + max_prompt_length=1024, + filter_prompts=True, + cache_dir='~/.cache/verl/rlhf', + chat_template_func=None, + return_raw_chat=False, + truncation='error'): + if not isinstance(parquet_files, (List, ListConfig)): + parquet_files = [parquet_files] + + self.parquet_files = parquet_files + self.cache_dir = os.path.expanduser(cache_dir) + self.tokenizer = tokenizer + + self.prompt_key = prompt_key + self.max_prompt_length = max_prompt_length + self.filter_prompts = filter_prompts + + self.return_raw_chat = return_raw_chat + self.chat_template_func = chat_template_func + self.truncation = truncation + + self._download() + self._read_files_and_tokenize() + + def _download(self): + from verl.utils.fs import copy_local_path_from_hdfs + for i, parquet_file in enumerate(self.parquet_files): + self.parquet_files[i] = copy_local_path_from_hdfs(src=parquet_file, cache_dir=self.cache_dir) + + def _read_files_and_tokenize(self): + dataframes = [] + for parquet_file in self.parquet_files: + # read parquet files and cache + dataframe = pd.read_parquet(parquet_file) + dataframes.append(dataframe) + self.dataframe = pd.concat(dataframes) + + print(f'original dataset len: {len(self.dataframe)}') + + # filter out too long prompts + tokenizer = self.tokenizer + prompt_key = self.prompt_key + self.dataframe = self.dataframe[self.dataframe.apply(lambda doc: len( + tokenizer.apply_chat_template(doc[prompt_key], add_generation_prompt=True)) <= self.max_prompt_length, + axis=1)] + + print(f'filter dataset len: {len(self.dataframe)}') + + def __len__(self): + return len(self.dataframe) + + def __getitem__(self, item): + """ + Note that we also return the raw_input_ids so that it can be combined with other chat template + """ + row_dict = self.dataframe.iloc[item].to_dict() + + chat = row_dict.pop(self.prompt_key) + + prompt_with_chat_template = self.tokenizer.apply_chat_template(chat, add_generation_prompt=True, tokenize=False) + + input_ids, attention_mask = verl_F.tokenize_and_postprocess_data(prompt=prompt_with_chat_template, + tokenizer=self.tokenizer, + max_length=self.max_prompt_length, + pad_token_id=self.tokenizer.pad_token_id, + left_pad=True, + truncation=self.truncation) + + position_ids = compute_position_id_with_mask(attention_mask) + + row_dict['input_ids'] = input_ids[0] + row_dict['attention_mask'] = attention_mask[0] + row_dict['position_ids'] = position_ids[0] + + # encode prompts without chat template + if self.return_raw_chat: + row_dict['raw_prompt'] = chat.tolist() + + return row_dict + + +if __name__ == '__main__': + from transformers import AutoTokenizer + + from torch.utils.data import DataLoader + + local_path = copy_local_path_from_hdfs('~/models/gemma-1.1-7b-it') + tokenizer = AutoTokenizer.from_pretrained(local_path) + + dataset = RLHFDataset(parquet_files='~/data/rlhf/gsm8k/train.parquet', + tokenizer=tokenizer, + prompt_key='prompt', + max_prompt_length=256) + + dataloader = DataLoader(dataset=dataset, batch_size=16, shuffle=True, drop_last=True, collate_fn=collate_fn) + + a = next(iter(dataloader)) + + from verl import DataProto + + tensors = {} + non_tensors = {} + + for key, val in a.items(): + if isinstance(val, torch.Tensor): + tensors[key] = val + else: + non_tensors[key] = val + + data_proto = DataProto.from_dict(tensors=tensors, non_tensors=non_tensors) + + data = dataset[0]['input_ids'] + output = tokenizer.batch_decode([data])[0] + print(f'\n\noutput: {output}') diff --git a/verl/utils/dataset/rm_dataset.py b/verl/utils/dataset/rm_dataset.py new file mode 100644 index 000000000..cc0c6b280 --- /dev/null +++ b/verl/utils/dataset/rm_dataset.py @@ -0,0 +1,151 @@ +# 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 typing import List, Union + +import pandas as pd + +import torch +from torch.utils.data import Dataset, DataLoader +from transformers import AutoTokenizer + + +def download_files_distributed(download_fn): + import torch.distributed + if torch.distributed.is_initialized(): + if torch.distributed.get_rank() == 0: + # download files + download_fn() + + torch.distributed.barrier() + else: + # download anyway + download_fn() + + +class RMDataset(Dataset): + + def __init__(self, + parquet_files: Union[str, List[str]], + tokenizer, + prompt_key='prompt', + chosen_key='chosen', + rejected_key='rejected', + max_length=1024, + add_eos=True, + cache_dir='~/.cache/verl/rm'): + if not isinstance(parquet_files, List): + parquet_files = [parquet_files] + + self.parquet_files = parquet_files + self.cache_dir = os.path.expanduser(cache_dir) + if isinstance(tokenizer, str): + tokenizer = AutoTokenizer.from_pretrained(tokenizer) + self.tokenizer = tokenizer + + self.prompt_key = prompt_key + self.chosen_key = chosen_key + self.rejected_key = rejected_key + + self.add_eos = add_eos + self.max_length = max_length + + self._download() + self._read_files_and_tokenize() + + def _download(self): + + def _download_files(): + from verl.utils.fs import copy, _is_non_local + os.makedirs(self.cache_dir, exist_ok=True) + assert os.path.exists(self.cache_dir) + for i, parquet_file in enumerate(self.parquet_files): + if _is_non_local(parquet_file): + dst = os.path.join(self.cache_dir, os.path.basename(parquet_file)) + if not os.path.exists(dst): + copy(src=parquet_file, dst=dst) + self.parquet_files[i] = dst + + download_files_distributed(_download_files) + + def _read_files_and_tokenize(self): + dataframes = [] + for parquet_file in self.parquet_files: + # read parquet files and cache + dataframe = pd.read_parquet(parquet_file) + dataframes.append(dataframe) + self.dataframe = pd.concat(dataframes) + self.prompts = self.dataframe[self.prompt_key].tolist() + self.chosen_responses = self.dataframe[self.chosen_key].tolist() + self.rejected_responses = self.dataframe[self.rejected_key].tolist() + + def __len__(self): + return len(self.prompts) + + def _pad_to_length(self, input_ids, attention_mask): + curr_length = input_ids.shape[-1] + + if curr_length < self.max_length: + input_ids = torch.cat( + (input_ids, torch.zeros(size=(self.max_length - curr_length,), dtype=input_ids.dtype)), dim=-1) + attention_mask = torch.cat( + (attention_mask, torch.zeros(size=(self.max_length - curr_length,), dtype=attention_mask.dtype)), + dim=-1) + elif curr_length > self.max_length: + input_ids = input_ids[:self.max_length] + attention_mask = attention_mask[:self.max_length] + + return input_ids, attention_mask + + def __getitem__(self, item): + prompt = self.prompts[item] + chosen_response = self.chosen_responses[item] + rejected_response = self.rejected_responses[item] + + prompt_ids = self.tokenizer(prompt, return_tensors='pt')['input_ids'][0] + chosen_response_ids = self.tokenizer(chosen_response, return_tensors='pt')['input_ids'][0] + rejected_response_ids = self.tokenizer(rejected_response, return_tensors='pt')['input_ids'][0] + + if self.add_eos: + chosen_response_ids = torch.cat((chosen_response_ids, torch.tensor([self.tokenizer.eos_token_id])), dim=-1) + rejected_response_ids = torch.cat((rejected_response_ids, torch.tensor([self.tokenizer.eos_token_id])), + dim=-1) + + chosen_input_ids = torch.cat((prompt_ids, chosen_response_ids), dim=-1) + chosen_attention_mask = torch.ones_like(chosen_input_ids) + + rejected_input_ids = torch.cat((prompt_ids, rejected_response_ids), dim=-1) + rejected_attention_mask = torch.ones_like(rejected_input_ids) + + chosen_input_ids, chosen_attention_mask = self._pad_to_length(chosen_input_ids, chosen_attention_mask) + rejected_input_ids, rejected_attention_mask = self._pad_to_length(rejected_input_ids, rejected_attention_mask) + + input_ids = torch.stack((chosen_input_ids, rejected_input_ids), dim=0) + attention_mask = torch.stack((rejected_input_ids, rejected_attention_mask), dim=0) + + return { + 'input_ids': input_ids, + 'attention_mask': attention_mask, + } + + +if __name__ == '__main__': + tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-v0.1", add_bos_token=False) + + dataset = RMDataset(parquet_files='~/data/full_hh_rlhf/rm/train.parquet', tokenizer=tokenizer, max_length=512) + data = dataset[0]['input_ids'] + output = tokenizer.batch_decode(data) + print(output[0]) + print(output[1]) diff --git a/verl/utils/dataset/sft_dataset.py b/verl/utils/dataset/sft_dataset.py new file mode 100644 index 000000000..fa64e2bef --- /dev/null +++ b/verl/utils/dataset/sft_dataset.py @@ -0,0 +1,160 @@ +# 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. +""" +SFT dataset +- We assume user pass a single parquet file. +- We load all the data into the memory. +Each parquet file contains +""" + +from typing import List, Union + +import pandas as pd + +import torch +from torch.utils.data import Dataset +from transformers import AutoTokenizer, PreTrainedTokenizer + +from verl.utils.fs import copy_local_path_from_hdfs +from verl.utils.model import compute_position_id_with_mask + + +class SFTDataset(Dataset): + """ + This is an in-memory SFTDataset + """ + + def __init__(self, + parquet_files: Union[str, List[str]], + tokenizer, + prompt_key='prompt', + response_key='response', + max_length=1024, + truncation='error'): + assert truncation in ['error', 'left', 'right'] + self.truncation = truncation + + if not isinstance(parquet_files, List): + parquet_files = [parquet_files] + + self.parquet_files = parquet_files + if isinstance(tokenizer, str): + tokenizer = AutoTokenizer.from_pretrained(tokenizer) + self.tokenizer: PreTrainedTokenizer = tokenizer + + self.prompt_key = prompt_key + self.response_key = response_key + + self.max_length = max_length + + self._download() + self._read_files_and_tokenize() + + def _download(self): + for i, parquet_file in enumerate(self.parquet_files): + self.parquet_files[i] = copy_local_path_from_hdfs(parquet_file, verbose=True) + + def _read_files_and_tokenize(self): + dataframes = [] + for parquet_file in self.parquet_files: + # read parquet files and cache + dataframe = pd.read_parquet(parquet_file) + dataframes.append(dataframe) + self.dataframe = pd.concat(dataframes) + self.prompts = self.dataframe[self.prompt_key].tolist() + self.responses = self.dataframe[self.response_key].tolist() + + def __len__(self): + return len(self.prompts) + + def __getitem__(self, item): + tokenizer = self.tokenizer + + prompt = self.prompts[item] + response = self.responses[item] + + # apply chat template + prompt_chat = [{'role': 'user', 'content': prompt}] + + # string + prompt_chat_str = tokenizer.apply_chat_template(prompt_chat, add_generation_prompt=True, tokenize=False) + response_chat_str = response + tokenizer.eos_token + + # tokenize + prompt_ids_output = tokenizer(prompt_chat_str, return_tensors='pt', add_special_tokens=False) + prompt_ids = prompt_ids_output['input_ids'][0] + prompt_attention_mask = prompt_ids_output['attention_mask'][0] + + response_ids_output = tokenizer(response_chat_str, return_tensors='pt', add_special_tokens=False) + response_ids = response_ids_output['input_ids'][0] + response_attention_mask = response_ids_output['attention_mask'][0] + + prompt_length = prompt_ids.shape[0] + response_length = response_ids.shape[0] + + input_ids = torch.cat((prompt_ids, response_ids), dim=-1) + attention_mask = torch.cat((prompt_attention_mask, response_attention_mask), dim=-1) + + # padding to max length + sequence_length = input_ids.shape[0] + if sequence_length < self.max_length: + padded_input_ids = torch.ones(size=(self.max_length - sequence_length,), + dtype=input_ids.dtype) * self.tokenizer.pad_token_id + padded_attention_mask = torch.zeros(size=(self.max_length - sequence_length,), dtype=attention_mask.dtype) + + input_ids = torch.cat((input_ids, padded_input_ids)) + attention_mask = torch.cat((attention_mask, padded_attention_mask)) + elif sequence_length > self.max_length: + if self.truncation == 'left': + # actually, left truncation may not be reasonable + input_ids = input_ids[-self.max_length:] + attention_mask = attention_mask[-self.max_length:] + elif self.truncation == 'right': + input_ids = input_ids[:self.max_length] + attention_mask = attention_mask[:self.max_length] + elif self.truncation == 'error': + raise NotImplementedError(f'{sequence_length=} is larger than {self.max_length=}') + else: + raise NotImplementedError(f'Unknown truncation method {self.truncation}') + + position_ids = compute_position_id_with_mask(attention_mask) + + loss_mask = attention_mask.clone() + if prompt_length > 1: + # mask out prompt for SFT. + loss_mask[:min(prompt_length, loss_mask.size(0)) - 1] = 0 + # mask out the last token in response + loss_mask[min(prompt_length + response_length, loss_mask.size(0)) - 1] = 0 + + return { + 'input_ids': input_ids, + 'attention_mask': attention_mask, + 'position_ids': position_ids, + 'loss_mask': loss_mask + } + + +if __name__ == '__main__': + local_model_path = copy_local_path_from_hdfs('~/models/gemma-2b-it') + tokenizer = AutoTokenizer.from_pretrained(local_model_path) + + dataset = SFTDataset(parquet_files='~/data/gsm8k/train.parquet', + tokenizer=tokenizer, + prompt_key='question', + response_key='answer', + max_length=512) + + data = dataset[0]['input_ids'] + output = tokenizer.batch_decode([data])[0] + print(output) diff --git a/verl/utils/debug/__init__.py b/verl/utils/debug/__init__.py new file mode 100644 index 000000000..0d0b3432e --- /dev/null +++ b/verl/utils/debug/__init__.py @@ -0,0 +1,15 @@ +# 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 .performance import log_gpu_memory_usage \ No newline at end of file diff --git a/verl/utils/debug/performance.py b/verl/utils/debug/performance.py new file mode 100644 index 000000000..615475a66 --- /dev/null +++ b/verl/utils/debug/performance.py @@ -0,0 +1,30 @@ +# 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 +import torch.distributed as dist +import logging + + +def log_gpu_memory_usage(head: str, logger: logging.Logger = None, level=logging.DEBUG, rank: int = 0): + if (not dist.is_initialized()) or (rank is None) or (dist.get_rank() == rank): + memory_allocated = torch.cuda.memory_allocated() / 1024**3 + memory_reserved = torch.cuda.memory_reserved() / 1024**3 + + message = f'{head}, memory allocated (GB): {memory_allocated}, memory reserved (GB): {memory_reserved}' + + if logger is None: + print(message) + else: + logger.log(msg=message, level=level) diff --git a/verl/utils/debug/trajectory_tracker.py b/verl/utils/debug/trajectory_tracker.py new file mode 100644 index 000000000..33b254685 --- /dev/null +++ b/verl/utils/debug/trajectory_tracker.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. +""" +Trajectory tracker can be inserted into code to save the intermediate results. +The results will be dump to hdfs for offline comparison. +Each process will have a client that first move all the tensors to CPU +""" + +from verl.utils.hdfs_io import makedirs, copy +import torch +import os +import ray +import io +import tempfile + +from collections import deque + +remote_copy = ray.remote(copy) + + +@ray.remote +def save_to_hdfs(data: io.BytesIO, name, hdfs_dir, verbose): + filename = name + '.pth' + with tempfile.TemporaryDirectory() as tmpdirname: + local_filepath = os.path.join(tmpdirname, filename) + with open(local_filepath, 'wb') as f: + f.write(data.getbuffer()) + # upload to hdfs + + if verbose: + print(f'Saving {local_filepath} to {hdfs_dir}') + try: + copy(local_filepath, hdfs_dir) + except Exception as e: + print(e) + + +@ray.remote +class TrajectoryTracker(): + + def __init__(self, hdfs_dir, verbose) -> None: + self.hdfs_dir = hdfs_dir + makedirs(hdfs_dir) + self.verbose = verbose + + self.handle = deque() + + def dump(self, data: io.BytesIO, name): + # get a temp file and write to it + self.handle.append(save_to_hdfs.remote(data, name, self.hdfs_dir, self.verbose)) + + def wait_for_hdfs(self): + while len(self.handle) != 0: + future = self.handle.popleft() + ray.get(future) + + +def dump_data(data, name): + enable = os.getenv('VERL_ENABLE_TRACKER', '0') == '1' + if not enable: + return + buffer = io.BytesIO() + torch.save(data, buffer) + tracker = get_trajectory_tracker() + ray.get(tracker.dump.remote(buffer, name)) + + +def get_trajectory_tracker(): + hdfs_dir = os.getenv('VERL_TRACKER_HDFS_DIR', default=None) + verbose = os.getenv('VERL_TRACKER_VERBOSE', default='0') == '1' + assert hdfs_dir is not None + tracker = TrajectoryTracker.options(name="global_tracker", get_if_exists=True, + lifetime="detached").remote(hdfs_dir, verbose) + return tracker + + +if __name__ == '__main__': + # testing + os.environ['VERL_ENABLE_TRACKER'] = '1' + os.environ['VERL_TRACKER_HDFS_DIR'] = '~/debug/test' + + @ray.remote + def process(iter): + data = {'obs': torch.randn(10, 20)} + dump_data(data, f'process_{iter}_obs') + + ray.init() + + output_lst = [] + + for i in range(10): + output_lst.append(process.remote(i)) + + out = ray.get(output_lst) + + tracker = get_trajectory_tracker() + ray.get(tracker.wait_for_hdfs.remote()) diff --git a/verl/utils/distributed.py b/verl/utils/distributed.py new file mode 100644 index 000000000..6fea5a29c --- /dev/null +++ b/verl/utils/distributed.py @@ -0,0 +1,28 @@ +# 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 distributed training.""" +import os + + +def initialize_global_process_group(timeout_second=36000): + import torch.distributed + from datetime import timedelta + torch.distributed.init_process_group('nccl', timeout=timedelta(seconds=timeout_second)) + local_rank = int(os.environ["LOCAL_RANK"]) + rank = int(os.environ["RANK"]) + world_size = int(os.environ["WORLD_SIZE"]) + + if torch.distributed.is_initialized(): + torch.cuda.set_device(local_rank) + return local_rank, rank, world_size diff --git a/verl/utils/fs.py b/verl/utils/fs.py new file mode 100644 index 000000000..80c1889be --- /dev/null +++ b/verl/utils/fs.py @@ -0,0 +1,88 @@ +#!/usr/bin/env python +# 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. + +# -*- coding: utf-8 -*- +"""File-system agnostic IO APIs""" +import os +import tempfile +import hashlib + +from .hdfs_io import copy, makedirs, exists + +__all__ = ["copy", "exists", "makedirs"] + +_HDFS_PREFIX = "hdfs://" + + +def _is_non_local(path): + return path.startswith(_HDFS_PREFIX) + + +def md5_encode(path: str) -> str: + return hashlib.md5(path.encode()).hexdigest() + + +def get_local_temp_path(hdfs_path: str, cache_dir: str) -> str: + """Return a local temp path that joins cache_dir and basename of hdfs_path + + Args: + hdfs_path: + cache_dir: + + Returns: + + """ + # make a base64 encoding of hdfs_path to avoid directory conflict + encoded_hdfs_path = md5_encode(hdfs_path) + temp_dir = os.path.join(cache_dir, encoded_hdfs_path) + os.makedirs(temp_dir, exist_ok=True) + dst = os.path.join(temp_dir, os.path.basename(hdfs_path)) + return dst + + +def copy_local_path_from_hdfs(src: str, cache_dir=None, filelock='.file.lock', verbose=False) -> str: + """Copy src from hdfs to local if src is on hdfs or directly return src. + If cache_dir is None, we will use the default cache dir of the system. Note that this may cause conflicts if + the src name is the same between calls + + Args: + src (str): a HDFS path of a local path + + Returns: + a local path of the copied file + """ + from filelock import FileLock + + assert src[-1] != '/', f'Make sure the last char in src is not / because it will cause error. Got {src}' + + if _is_non_local(src): + # download from hdfs to local + if cache_dir is None: + # get a temp folder + cache_dir = tempfile.gettempdir() + os.makedirs(cache_dir, exist_ok=True) + assert os.path.exists(cache_dir) + local_path = get_local_temp_path(src, cache_dir) + # get a specific lock + filelock = md5_encode(src) + '.lock' + lock_file = os.path.join(cache_dir, filelock) + with FileLock(lock_file=lock_file): + if not os.path.exists(local_path): + if verbose: + print(f'Copy from {src} to {local_path}') + copy(src, local_path) + return local_path + else: + return src diff --git a/verl/utils/fsdp_utils.py b/verl/utils/fsdp_utils.py new file mode 100644 index 000000000..a4a8b8a2c --- /dev/null +++ b/verl/utils/fsdp_utils.py @@ -0,0 +1,122 @@ +# 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 +from torch.distributed.fsdp.wrap import size_based_auto_wrap_policy, transformer_auto_wrap_policy +from transformers.trainer_pt_utils import get_module_class_from_name +import torch + + +def init_fn(x: torch.nn.Module): + if not torch.distributed.get_rank() == 0: + x = x.to_empty(device=torch.cuda.current_device(), recurse=False) + torch.cuda.empty_cache() + return x + + +def get_init_weight_context_manager(use_meta_tensor=True): + from accelerate import init_empty_weights + cpu_init_weights = lambda: torch.device('cpu') + if use_meta_tensor: + init_context = init_empty_weights if torch.distributed.get_rank() != 0 else cpu_init_weights + else: + init_context = cpu_init_weights + return init_context + + +# Copyright 2020-present the HuggingFace Inc. team. +# Adapted from https://github.com/huggingface/transformers/src/transformers/trainer.py +def get_fsdp_wrap_policy(module, config=None): + if config is None: + config = {} + + if config.get('disable', False): + return None + + default_transformer_cls_names_to_wrap = getattr(module, "_no_split_modules", None) + fsdp_transformer_layer_cls_to_wrap = config.get("transformer_layer_cls_to_wrap", + default_transformer_cls_names_to_wrap) + min_num_params = config.get('min_num_params', 0) + auto_wrap_policy = None + if min_num_params > 0: + auto_wrap_policy = functools.partial(size_based_auto_wrap_policy, min_num_params=min_num_params) + elif fsdp_transformer_layer_cls_to_wrap is not None: + transformer_cls_to_wrap = set() + for layer_class in fsdp_transformer_layer_cls_to_wrap: + transformer_cls = get_module_class_from_name(module, layer_class) + if transformer_cls is None: + raise Exception("Could not find the transformer layer class to wrap in the model.") + else: + transformer_cls_to_wrap.add(transformer_cls) + + auto_wrap_policy = functools.partial( + transformer_auto_wrap_policy, + # Transformer layer class to wrap + transformer_layer_cls=transformer_cls_to_wrap, + ) + return auto_wrap_policy + + +def offload_fsdp_grad(module): + for _, param in module.named_parameters(): + if param.grad is not None: + param.grad = param.grad.to("cpu", non_blocking=True) + torch.cuda.empty_cache() + + +def load_fsdp_grad(module, device_id): + for _, param in module.named_parameters(): + if param.grad is not None: + param.grad = param.grad.to(device_id, non_blocking=True) + torch.cuda.empty_cache() + + +def offload_fsdp_param_and_grad(module, offload_grad=False): + for _, param in module.named_parameters(): + if hasattr(param, "_local_shard"): + param._local_shard = param._local_shard.to("cpu", non_blocking=True) + param.data = param.data.to('cpu', non_blocking=True) + if offload_grad and param.grad is not None: + param.grad = param.grad.to("cpu", non_blocking=True) + torch.cuda.empty_cache() + + +def load_fsdp_param_and_grad(module, device_id, load_grad=False): + for _, param in module.named_parameters(): + if hasattr(param, "_local_shard"): + param._local_shard = param._local_shard.to(device_id, non_blocking=True) + param.data = param.data.to(device_id, non_blocking=True) + if load_grad and param.grad is not None: + param.grad = param.grad.to(device_id, non_blocking=True) + torch.cuda.empty_cache() + + +def offload_fsdp_optimizer(optimizer): + 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.cuda.empty_cache() + + +def load_fsdp_optimizer(optimizer, device_id): + 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) + torch.cuda.empty_cache() diff --git a/verl/utils/hdfs_io.py b/verl/utils/hdfs_io.py new file mode 100644 index 000000000..f23b4063d --- /dev/null +++ b/verl/utils/hdfs_io.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. + +import os +import shutil +import subprocess +import logging + +logger = logging.getLogger(__file__) +logger.setLevel(os.getenv('VERL_SFT_LOGGING_LEVEL', 'WARN')) + +_HDFS_PREFIX = "hdfs://" + +_HDFS_BIN_PATH = shutil.which('hdfs') + + +def exists(path: str, **kwargs) -> bool: + r"""Works like os.path.exists() but supports hdfs. + + Test whether a path exists. Returns False for broken symbolic links. + + Args: + path (str): path to test + + Returns: + bool: True if the path exists, False otherwise + """ + if _is_non_local(path): + return _exists(path, **kwargs) + return os.path.exists(path) + + +def _exists(file_path: str): + """ hdfs capable to check whether a file_path is exists """ + if file_path.startswith("hdfs"): + return _run_cmd(_hdfs_cmd(f"-test -e {file_path}")) == 0 + return os.path.exists(file_path) + + +def makedirs(name, mode=0o777, exist_ok=False, **kwargs) -> None: + r"""Works like os.makedirs() but supports hdfs. + + Super-mkdir; create a leaf directory and all intermediate ones. Works like + mkdir, except that any intermediate path segment (not just the rightmost) + will be created if it does not exist. If the target directory already + exists, raise an OSError if exist_ok is False. Otherwise no exception is + raised. This is recursive. + + Args: + name (str): directory to create + mode (int): file mode bits + exist_ok (bool): if True, do not raise an exception if the directory already exists + kwargs: keyword arguments for hdfs + + """ + if _is_non_local(name): + # TODO(haibin.lin): + # - handle OSError for hdfs(?) + # - support exist_ok for hdfs(?) + _mkdir(name, **kwargs) + else: + os.makedirs(name, mode=mode, exist_ok=exist_ok) + + +def _mkdir(file_path: str) -> bool: + """hdfs mkdir""" + if file_path.startswith("hdfs"): + _run_cmd(_hdfs_cmd(f"-mkdir -p {file_path}")) + else: + os.makedirs(file_path, exist_ok=True) + return True + + +def copy(src: str, dst: str, **kwargs) -> bool: + r"""Works like shutil.copy() but supports hdfs. + + Copy data and mode bits ("cp src dst"). Return the file's destination. + The destination may be a directory. + If source and destination are the same file, a SameFileError will be + raised. + + Arg: + src (str): source file path + dst (str): destination file path + kwargs: keyword arguments for hdfs copy + + Returns: + str: destination file path + + """ + if _is_non_local(src) or _is_non_local(dst): + # TODO(haibin.lin): + # - handle SameFileError for hdfs files(?) + # - return file destination for hdfs files + return _copy(src, dst) + else: + return shutil.copy(src, dst) + + +def _copy(from_path: str, to_path: str, timeout: int = None) -> bool: + if to_path.startswith("hdfs"): + if from_path.startswith("hdfs"): + returncode = _run_cmd(_hdfs_cmd(f"-cp -f {from_path} {to_path}"), timeout=timeout) + else: + returncode = _run_cmd(_hdfs_cmd(f"-put -f {from_path} {to_path}"), timeout=timeout) + else: + if from_path.startswith("hdfs"): + returncode = _run_cmd(_hdfs_cmd(f"-get \ + {from_path} {to_path}"), timeout=timeout) + else: + try: + shutil.copy(from_path, to_path) + returncode = 0 + except shutil.SameFileError: + returncode = 0 + except Exception as e: + logger.warning(f"copy {from_path} {to_path} failed: {e}") + returncode = -1 + return returncode == 0 + + +def _run_cmd(cmd: str, timeout=None): + return os.system(cmd) + + +def _hdfs_cmd(cmd: str) -> str: + return f"{_HDFS_BIN_PATH} dfs {cmd}" + + +def _is_non_local(path: str): + return path.startswith(_HDFS_PREFIX) diff --git a/verl/utils/import_utils.py b/verl/utils/import_utils.py new file mode 100644 index 000000000..e5690512d --- /dev/null +++ b/verl/utils/import_utils.py @@ -0,0 +1,48 @@ +# 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 to check if packages are available. +We assume package availability won't change during runtime. +""" + +from functools import cache +from typing import List + + +@cache +def is_megatron_core_available(): + try: + from megatron.core import parallel_state as mpu + return True + except ImportError: + return False + + +@cache +def is_vllm_available(): + try: + import vllm + return True + except ImportError: + return False + + +def import_external_libs(external_libs=None): + if external_libs is None: + return + if not isinstance(external_libs, List): + external_libs = [external_libs] + import importlib + for external_lib in external_libs: + importlib.import_module(external_lib) diff --git a/verl/utils/logger/__init__.py b/verl/utils/logger/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/utils/logger/__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/utils/logger/aggregate_logger.py b/verl/utils/logger/aggregate_logger.py new file mode 100644 index 000000000..453f4145c --- /dev/null +++ b/verl/utils/logger/aggregate_logger.py @@ -0,0 +1,43 @@ +# 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 Ray logger will receive logging info from different processes. +""" + +import numbers +from typing import Dict + + +def concat_dict_to_str(dict: Dict, step): + output = [f'step:{step}'] + for k, v in dict.items(): + if isinstance(v, numbers.Number): + output.append(f'{k}:{v:.3f}') + output_str = ' - '.join(output) + return output_str + + +class LocalLogger: + + def __init__(self, remote_logger=None, enable_wandb=False, print_to_console=False): + self.print_to_console = print_to_console + if print_to_console: + print('Using LocalLogger is deprecated. The constructor API will change ') + + def flush(self): + pass + + def log(self, data, step): + if self.print_to_console: + print(concat_dict_to_str(data, step=step)) diff --git a/verl/utils/logging_utils.py b/verl/utils/logging_utils.py new file mode 100644 index 000000000..1bf6e1f0f --- /dev/null +++ b/verl/utils/logging_utils.py @@ -0,0 +1,22 @@ +# 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 + + +def set_basic_config(level): + """ + This function sets the global logging format and level. It will be called when import verl + """ + logging.basicConfig(format='%(levelname)s:%(asctime)s:%(message)s', level=level) diff --git a/verl/utils/megatron/__init__.py b/verl/utils/megatron/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/utils/megatron/__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/utils/megatron/memory.py b/verl/utils/megatron/memory.py new file mode 100644 index 000000000..5e8570ed4 --- /dev/null +++ b/verl/utils/megatron/memory.py @@ -0,0 +1,41 @@ +# 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 + + +class MemoryBuffer: + + def __init__(self, numel, numel_padded, dtype): + self.numel = numel + self.numel_padded = numel_padded + self.dtype = dtype + self.data = torch.zeros(self.numel_padded, + dtype=self.dtype, + device=torch.cuda.current_device(), + requires_grad=False) + + def zero(self): + """Reset the buffer to zero.""" + self.data.zero_() + + def get(self, shape, start_index): + """Return a tensor with the input `shape` as a view into the + 1-D data starting at `start_index`.""" + end_index = start_index + shape.numel() + assert end_index <= self.numel, \ + 'requested tensor is out of the buffer range.' + buffer_tensor = self.data[start_index:end_index] + buffer_tensor = buffer_tensor.view(shape) + return buffer_tensor diff --git a/verl/utils/megatron/optimizer.py b/verl/utils/megatron/optimizer.py new file mode 100644 index 000000000..9ae70b087 --- /dev/null +++ b/verl/utils/megatron/optimizer.py @@ -0,0 +1,92 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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 apex.optimizers import FusedAdam as Adam +from apex.optimizers import FusedSGD as SGD +from megatron.optimizer.distrib_optimizer import DistributedOptimizer +from megatron.optimizer.grad_scaler import ConstantGradScaler, DynamicGradScaler +from megatron.optimizer import Float16OptimizerWithFloat16Params, FP32Optimizer +from megatron.optimizer import get_param_groups + +from verl.utils.megatron.optimizer_config import OptimizerConfig + + +def get_megatron_optimizer( + model, + config: OptimizerConfig, + no_weight_decay_cond=None, + scale_lr_cond=None, + lr_mult=1.0, + check_for_nan_in_loss_and_grad=False, + overlap_param_gather=False # add for verl +): + # Base optimizer. + param_groups = get_param_groups(model, no_weight_decay_cond, scale_lr_cond, lr_mult) + + if config.optimizer == 'adam': + optimizer = Adam(param_groups, + lr=config.lr, + weight_decay=config.weight_decay, + betas=(config.adam_beta1, config.adam_beta2), + eps=config.adam_eps) + elif config.optimizer == 'sgd': + optimizer = SGD(param_groups, lr=config.lr, weight_decay=config.weight_decay, momentum=config.sgd_momentum) + else: + raise Exception('{} optimizer is not supported.'.format(config.optimizer)) + + # Determine whether the params have main-grad field. + params_have_main_grad = True + + # Mixed precision optimizer. + # - Note: both the Float16Optimizer and the DistributedOptimizer inherit + # from the MixedPrecisionOptimizer, which manages any optimizer where + # the model params and main params are distinct. + if config.fp16 or config.bf16 or config.use_distributed_optimizer: + + # Grad scaler: + # if loss-scale is provided, instantiate the constant scaler. + # if we are using fp16 and loss-scale is not present, use a + # dynamic scaler. + # otherwise we are running in bf16 with no loss-scale so + # leave it as None. + grad_scaler = None + + # Constant loss scale. + if config.loss_scale: + grad_scaler = ConstantGradScaler(config.loss_scale) + + # Dynamic loss scale. + else: + if config.fp16: + grad_scaler = DynamicGradScaler(initial_scale=config.initial_loss_scale, + min_scale=config.min_loss_scale, + growth_factor=2.0, + backoff_factor=0.5, + growth_interval=config.loss_scale_window, + hysteresis=config.hysteresis) + + # Megatron optimizer. + if config.use_distributed_optimizer: + return DistributedOptimizer(optimizer, config.clip_grad, config.log_num_zeros_in_grad, + check_for_nan_in_loss_and_grad, params_have_main_grad, config.fp16, config.bf16, + config.params_dtype, grad_scaler, model, overlap_param_gather) + else: + return Float16OptimizerWithFloat16Params(optimizer, config.clip_grad, config.log_num_zeros_in_grad, + check_for_nan_in_loss_and_grad, params_have_main_grad, config.fp16, + config.bf16, config.params_dtype, grad_scaler, model) + + # FP32. + return FP32Optimizer(optimizer, config.clip_grad, config.log_num_zeros_in_grad, check_for_nan_in_loss_and_grad, + params_have_main_grad, model) diff --git a/verl/utils/megatron/optimizer_config.py b/verl/utils/megatron/optimizer_config.py new file mode 100644 index 000000000..3401de416 --- /dev/null +++ b/verl/utils/megatron/optimizer_config.py @@ -0,0 +1,129 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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 dataclasses import dataclass +from typing import Callable, Optional + +import torch + + +@dataclass +class OptimizerConfig: + """Configuration for optimizer.""" + + ############## + # General + ############## + optimizer: str = 'adam' + """Optimizer to use (one of Adam or SGD).""" + + lr: Optional[float] = None + """Initial learning rate. Depending on decay style and initial warmup, the learning rate at each + iteration would be different. + """ + + min_lr: Optional[float] = None + """Minumum value for learning rate. The scheduler clip values below this threshold.""" + + decoupled_lr: Optional[float] = None + """Separate learning rate for the input and output layer.""" + + decoupled_min_lr: Optional[float] = None + """Minimum value for learning rate for the input and output layer. The scheduler clip values + below this threshold. + """ + + weight_decay: float = 0.01 + """Weight decay coefficient for L2 regularization.""" + + ############## + # Precision + ############## + fp16: bool = False + """If true, train with fp16 mixed precision training. Defaults to False.""" + + bf16: bool = False + """If true, train with bf16 mixed precision training. Defaults to False.""" + + params_dtype: torch.dtype = torch.float32 + """dtype used when intializing the weights. Defaults to torch.float32.""" + + ############### + # Loss scaling + ############### + loss_scale: Optional[float] = None + """Static loss scaling, positive power of 2 values can improve fp16 convergence. If None, + dynamic loss scaling is used. + """ + + initial_loss_scale: float = 2**32 + """Initial loss-scale for dynamic loss scaling.""" + + min_loss_scale: float = 1.0 + """Minimum loss scale for dynamic loss scaling.""" + + loss_scale_window: float = 1000 + """Window over which to raise/lower dynamic scale.""" + + hysteresis: int = 2 + """Hysteresis for dynamic loss scaling.""" + + ############## + # Optimizer + ############## + # Adam + adam_beta1: float = 0.9 + """First coefficient for computing running averages of gradient and its square in Adam + optimizer. + """ + + adam_beta2: float = 0.999 + """Second coefficient for computing running averages of gradient and its square in Adam + optimizer. + """ + + adam_eps: float = 1e-08 + """Term added to the denominator to improve numerical stability in Adam optimizer.""" + + # SGD. + sgd_momentum: float = 0.9 + """Momentum factor for SGD optimizer.""" + + ####################### + # Distributed optimizer + ####################### + use_distributed_optimizer: bool = False + """Distribute optimizer state over data-parallel replicas.""" + + overlap_grad_reduce: bool = False + """If true, overlap grad reduce-scatter with backward compute in distributed optimizer.""" + + overlap_param_gather: bool = False + """If true, overlap param all-gather with forward compute in distributed optimizer.""" + + ################ + # Miscellaneous + ################ + clip_grad: float = 1.0 + """Gradient clipping based on global L2 norm.""" + + log_num_zeros_in_grad: bool = False + """If true, calculate and log the number of zeros in gradient.""" + + barrier_with_L1_time: bool = False + """If true, use barrier with level 1 time measurements.""" + + timers: Callable = None + """Function to get timers.""" diff --git a/verl/utils/megatron/pipeline_parallel.py b/verl/utils/megatron/pipeline_parallel.py new file mode 100644 index 000000000..3a3790bb1 --- /dev/null +++ b/verl/utils/megatron/pipeline_parallel.py @@ -0,0 +1,51 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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 torch +from megatron.core import parallel_state as mpu + +from .sequence_parallel import pad_to_sequence_parallel + + +def compute_transformers_input_shapes(batches, meta_info): + from flash_attn.bert_padding import unpad_input # flash 2 is a must for Megatron + # pre-compute input shapes for each micro-batch at each pp stage + input_shapes = [] + for model_inputs in batches: + input_ids = model_inputs['input_ids'] + attention_mask = model_inputs['attention_mask'] + input_ids_rmpad = unpad_input(input_ids.unsqueeze(dim=-1), attention_mask)[0] # (total_nnz, 1) + if meta_info['sequence_parallel']: + input_ids_rmpad = pad_to_sequence_parallel(input_ids_rmpad) + # compute shapes for model_inputs + input_shapes.append( + torch.Size([ + input_ids_rmpad.shape[0] // mpu.get_tensor_model_parallel_world_size(), 1, meta_info['hidden_size'] + ])) + else: + # compute shapes for model_inputs + input_shapes.append(torch.Size([input_ids_rmpad.shape[0], 1, meta_info['hidden_size']])) + return input_shapes + + +def make_batch_generator(batches, vpp_size): + if vpp_size > 1: + # has vpp + batch_generator = [batches] * vpp_size # number of vpp chunks + batch_generator = [iter(b) for b in batch_generator] + else: + # no vpp + batch_generator = iter(batches) + return batch_generator diff --git a/verl/utils/megatron/sequence_parallel.py b/verl/utils/megatron/sequence_parallel.py new file mode 100644 index 000000000..4b76cb295 --- /dev/null +++ b/verl/utils/megatron/sequence_parallel.py @@ -0,0 +1,54 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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 torch +import torch.nn.functional as F +from megatron.core import parallel_state as mpu + + +def mark_parameter_as_sequence_parallel(parameter): + setattr(parameter, 'sequence_parallel', True) + + +def is_sequence_parallel_param(param): + return hasattr(param, 'sequence_parallel') and param.sequence_parallel + + +def pad_to_sequence_parallel(unpad_tokens: torch.Tensor): + """pad the tokens such that the total length is a multiple of sp world size + + Args: + unpad_tokens: (total_nnz, ...). Tokens after removing padding + + Returns: + + """ + total_nnz = unpad_tokens.shape[0] + sp_world_size = mpu.get_tensor_model_parallel_world_size() + + if total_nnz % sp_world_size == 0: + pad_size = 0 + else: + pad_size = sp_world_size - total_nnz % sp_world_size + + if pad_size > 0: + if unpad_tokens.ndim == 1: + unpad_tokens = F.pad(unpad_tokens, (0, pad_size)) + elif unpad_tokens.ndim == 2: + unpad_tokens = F.pad(unpad_tokens, (0, 0, 0, pad_size)) + else: + raise NotImplementedError(f'Padding dim {unpad_tokens.ndim()} is not supported') + + return unpad_tokens diff --git a/verl/utils/megatron/tensor_parallel.py b/verl/utils/megatron/tensor_parallel.py new file mode 100644 index 000000000..073da4f24 --- /dev/null +++ b/verl/utils/megatron/tensor_parallel.py @@ -0,0 +1,185 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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. +""" +Utilities for using tensor_parallel in megatron +""" +from typing import Dict +import torch +from torch.nn import init +import torch.distributed as dist +from megatron.core import ModelParallelConfig +from megatron.core import parallel_state as mpu, tensor_parallel +import verl.utils.torch_functional as verl_F + + +def update_kwargs_with_config(dictionary: Dict, config: ModelParallelConfig): + dictionary['config'] = config + return dictionary + + +def get_default_kwargs_for_model_parallel_config(): + model_parallel_config_kwargs = { + 'params_dtype': torch.float32, + 'use_cpu_initialization': False, + 'perform_initialization': True, + 'gradient_accumulation_fusion': False, + 'sequence_parallel': False, + } + return model_parallel_config_kwargs + + +def get_default_model_parallel_config(): + return ModelParallelConfig(**get_default_kwargs_for_model_parallel_config()) + + +def get_common_default_kwargs_for_parallel_linear(): + default_model_parallel_config = get_default_model_parallel_config() + common_default_kwargs = { + 'init_method': init.xavier_normal_, + 'stride': 1, + 'keep_master_weight_for_test': False, + 'config': default_model_parallel_config, + } + return common_default_kwargs + + +def get_default_kwargs_for_column_parallel_linear(): + model_parallel_config_kwargs = get_default_kwargs_for_model_parallel_config() + column_parallel_config_kwargs = { + 'async_tensor_model_parallel_allreduce': False, + } + model_parallel_config_kwargs.update(column_parallel_config_kwargs) + column_default_kwargs = { + 'config': ModelParallelConfig(**model_parallel_config_kwargs), + } + common_default_kwargs = get_common_default_kwargs_for_parallel_linear() + common_default_kwargs.update(column_default_kwargs) + return common_default_kwargs + + +def get_default_kwargs_for_row_parallel_linear(): + common_default_kwargs = get_common_default_kwargs_for_parallel_linear() + return common_default_kwargs + + +def get_default_kwargs_for_parallel_embedding(): + model_parallel_config_kwargs = get_default_kwargs_for_model_parallel_config() + embedding_default_kwargs = { + 'init_method': init.xavier_normal_, + 'config': ModelParallelConfig(**model_parallel_config_kwargs), + } + return embedding_default_kwargs + + +def is_tensor_parallel_param(param): + return (hasattr(param, 'tensor_model_parallel') and param.tensor_model_parallel) + + +def get_tensor_parallel_partition_dim(param): + assert is_tensor_parallel_param(param) + return param.partition_dim + + +def get_tensor_parallel_partition_stride(param): + assert is_tensor_parallel_param(param) + return param.partition_stride + + +class _VocabParallelEntropy(torch.autograd.Function): + + @staticmethod + def forward(ctx, vocab_parallel_logits: torch.Tensor) -> torch.Tensor: + logits_max = vocab_parallel_logits.max(dim=-1, keepdim=True).values + dist.all_reduce(logits_max, op=dist.ReduceOp.MAX, group=mpu.get_tensor_model_parallel_group()) + normalized_vocab_parallel_logits = vocab_parallel_logits - logits_max + normalized_exp_logits = normalized_vocab_parallel_logits.exp() + normalized_sum_exp_logits = normalized_exp_logits.sum(dim=-1, keepdim=True) + dist.all_reduce(normalized_sum_exp_logits, group=mpu.get_tensor_model_parallel_group()) + softmax_logits = normalized_exp_logits / normalized_sum_exp_logits + sum_softmax_times_logits = (softmax_logits * vocab_parallel_logits).sum(dim=-1, keepdim=True) + dist.all_reduce(sum_softmax_times_logits, group=mpu.get_tensor_model_parallel_group()) + entropy = logits_max + normalized_sum_exp_logits.log() - sum_softmax_times_logits + ctx.save_for_backward(vocab_parallel_logits, softmax_logits, sum_softmax_times_logits) + return entropy.squeeze(dim=-1) + + @staticmethod + def backward(ctx, grad_output: torch.Tensor) -> torch.Tensor: + vocab_parallel_logits, softmax_logits, sum_softmax_times_logits = ctx.saved_tensors + grad_input = grad_output.unsqueeze(dim=-1) * softmax_logits * (sum_softmax_times_logits - vocab_parallel_logits) + return grad_input + + +def vocab_parallel_entropy(vocab_parallel_logits: torch.Tensor) -> torch.Tensor: + """Compute entropy when the logits are sharded in tp ranks + + Args: + vocab_parallel_logits: (total_nnz, vocab_size // tp_size) + + Returns: (total_nnz,) + + """ + return _VocabParallelEntropy.apply(vocab_parallel_logits) + + +def vocab_parallel_log_probs_from_logits(logits, labels): + """TODO(zhangchi.usc1992): We may change the implementation later""" + return -tensor_parallel.vocab_parallel_cross_entropy(vocab_parallel_logits=logits, target=labels) + + +def vocab_parallel_log_probs_from_logits_response_rmpad(input_ids, attention_mask, logits_rmpad, response_length): + """Similar to log_probs_from_logits_response_rmpad, but the logits_rmpad is now spliited across tensor parallel region. + This will further reduce the peak memory usage during training + + Args: + input_ids: [batch_size, seqlen] + attention_mask: [batch_size, seqlen] + logits_rmpad: [total_nnz, vocab_size // tp_size] + response_length: int + + """ + from flash_attn.bert_padding import pad_input, unpad_input + + batch_size, seqlen = input_ids.shape + input_ids_rmpad, indices, cu_seqlens, max_seqlen_in_batch = 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 = vocab_parallel_log_probs_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 vocab_parallel_compute_entropy_loss(logits, eos_mask): + """Compute Categorical entropy loss + + Args: + logits: `(torch.Tensor)` + shape: (bs, response_length, vocab_size) + eos_mask: `(torch.Tensor)` + shape: (bs, response_length) + + Returns: + entropy: a scalar torch.Tensor + + """ + # compute entropy + entropy = vocab_parallel_entropy(logits) + entropy_loss = verl_F.masked_mean(entropy, mask=eos_mask) + return entropy_loss diff --git a/verl/utils/megatron_utils.py b/verl/utils/megatron_utils.py new file mode 100644 index 000000000..2f08027d0 --- /dev/null +++ b/verl/utils/megatron_utils.py @@ -0,0 +1,254 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright (c) 2024, 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. + +"""Pretrain utilities.""" +from typing import Any, Dict +import time +from omegaconf import DictConfig +from verl.utils.torch_dtypes import PrecisionType +from verl.utils.memory_buffer import build_memory_reference_from_module +import torch +import torch.nn as nn +import torch.nn.functional as F + +from megatron.core import mpu, tensor_parallel +from megatron.core.utils import get_model_config +from megatron.core.transformer import TransformerConfig +from megatron.core.transformer.module import Float16Module +# from megatron.core.distributed import DistributedDataParallelConfig +from megatron.core.distributed import DistributedDataParallel as DDP +from megatron.core.enums import ModelType + + +def get_model(model_provider_func, model_type=ModelType.encoder_or_decoder, wrap_with_ddp=True): + """Build the model.""" + # Build model. + if mpu.get_pipeline_model_parallel_world_size() > 1 and \ + mpu.get_virtual_pipeline_model_parallel_world_size() is not None: + assert model_type != ModelType.encoder_and_decoder, \ + "Interleaved schedule not supported for model with both encoder and decoder" + model = [] + for i in range(mpu.get_virtual_pipeline_model_parallel_world_size()): + mpu.set_virtual_pipeline_model_parallel_rank(i) + # Set pre_process and post_process only after virtual rank is set. + pre_process = mpu.is_pipeline_first_stage() + post_process = mpu.is_pipeline_last_stage() + this_model = model_provider_func(pre_process=pre_process, post_process=post_process) + this_model.model_type = model_type + model.append(this_model) + else: + pre_process = mpu.is_pipeline_first_stage() + post_process = mpu.is_pipeline_last_stage() + add_encoder = True + add_decoder = True + if model_type == ModelType.encoder_and_decoder: + if mpu.get_pipeline_model_parallel_world_size() > 1: + assert mpu.get_pipeline_model_parallel_split_rank() is not None, \ + "Split rank needs to be specified for model with both encoder and decoder" + rank = mpu.get_pipeline_model_parallel_rank() + split_rank = mpu.get_pipeline_model_parallel_split_rank() + world_size = mpu.get_pipeline_model_parallel_world_size() + pre_process = rank == 0 or rank == split_rank + post_process = (rank == (split_rank - 1)) or (rank == (world_size - 1)) + add_encoder = mpu.is_pipeline_stage_before_split() + add_decoder = mpu.is_pipeline_stage_after_split() + model = model_provider_func(pre_process=pre_process, + post_process=post_process, + add_encoder=add_encoder, + add_decoder=add_decoder) + else: + model = model_provider_func(pre_process=pre_process, post_process=post_process) + model.model_type = model_type + + if not isinstance(model, list): + model = [model] + + # Set tensor model parallel attributes if not set. + # Only parameters that are already tensor model parallel have these + # attributes set for them. We should make sure the default attributes + # are set for all params so the optimizer can use them. + for model_module in model: + for param in model_module.parameters(): + tensor_parallel.set_defaults_if_not_set_tensor_model_parallel_attributes(param) + + # Print number of parameters. + if mpu.get_data_parallel_rank() == 0: + print(' > number of parameters on (tensor, pipeline) ' + 'model parallel rank ({}, {}): {}'.format( + mpu.get_tensor_model_parallel_rank(), mpu.get_pipeline_model_parallel_rank(), + sum([sum([p.nelement() for p in model_module.parameters()]) for model_module in model])), + flush=True) + + # GPU allocation. + for model_module in model: + model_module.cuda(torch.cuda.current_device()) + + # Fp16 conversion. + config = get_model_config(model[0]) + if config.fp16 or config.bf16: # the ModelParallelConfig in GPTModel + model = [Float16Module(config, model_module) for model_module in model] + + if wrap_with_ddp: + model = [ + DDP(config=config, + module=model_chunk, + data_parallel_group=mpu.get_data_parallel_group(with_context_parallel=True), + accumulate_allreduce_grads_in_fp32=True, + overlap_grad_reduce=False, + use_distributed_optimizer=True, + disable_bucketing=(model_chunk_idx > 0)) for (model_chunk_idx, model_chunk) in enumerate(model) + ] + # # Broadcast params from data parallel src rank to other data parallel ranks. + # if args.data_parallel_random_init: + for model_module in model: + model_module.broadcast_params() + return model + + +ALL_MODULE_WRAPPER_CLASSNAMES = (DDP, Float16Module) + + +def unwrap_model(model, module_instances=ALL_MODULE_WRAPPER_CLASSNAMES): + return_list = True + if not isinstance(model, list): + model = [model] + return_list = False + unwrapped_model = [] + for model_module in model: + while isinstance(model_module, module_instances): + model_module = model_module.module + unwrapped_model.append(model_module) + if not return_list: + return unwrapped_model[0] + return unwrapped_model + + +from transformers import PretrainedConfig + + +def convert_config(hf_config: PretrainedConfig, megatron_config) -> TransformerConfig: + print(f'megatron config {megatron_config}') + dt = PrecisionType.to_dtype(megatron_config['param_dtype']) + print(f'pipeline_dtype=megatron_config {dt}') + transformer_config = TransformerConfig( + 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, + # max_position_embeddings=hf_config.max_position_embeddings, + activation_func=F.silu, + normalization='RMSNorm', + # rotary_percent=False, # default, + gated_linear_unit=True, # for llama + use_cpu_initialization=True, + apply_residual_connection_post_layernorm=False, # check what's this mean + add_bias_linear=False, + tensor_model_parallel_size=mpu.get_tensor_model_parallel_world_size(), + pipeline_model_parallel_size=mpu.get_pipeline_model_parallel_world_size(), + virtual_pipeline_model_parallel_size=mpu.get_virtual_pipeline_model_parallel_world_size(), + pipeline_dtype=PrecisionType.to_dtype(megatron_config['param_dtype']), + params_dtype=PrecisionType.to_dtype(megatron_config['param_dtype']), + sequence_parallel=megatron_config['sequence_parallel_enabled'], + variable_seq_lengths=True, + masked_softmax_fusion=True, + bf16=PrecisionType.to_dtype(megatron_config['param_dtype']) is torch.bfloat16) + if torch.distributed.get_rank() == 0: + print(f'tensor_parallel_size={transformer_config.tensor_model_parallel_size} \n \ + pipeline_model_parallel_size={transformer_config.pipeline_model_parallel_size} \n \ + virtual_pipeline_model_parallel_size={transformer_config.virtual_pipeline_model_parallel_size} \n \ + pipeline_dtype={transformer_config.pipeline_dtype} \n \ + params_dtype={transformer_config.params_dtype} \n \ + sequence_parallel={transformer_config.sequence_parallel} \n \ + variable_seq_lengths={transformer_config.variable_seq_lengths} \n \ + masked_softmax_fusion={transformer_config.masked_softmax_fusion} \n ') + + return transformer_config + + +# from megatron.core.optimizer import OptimizerConfig + +from verl.utils.megatron.optimizer_config import OptimizerConfig + + +def init_megatron_optim_config(optim_config: Dict) -> OptimizerConfig: + config = OptimizerConfig( + optimizer='adam', + lr=optim_config.get('lr'), + clip_grad=optim_config.get('clip_grad'), + weight_decay=1e-2, + bf16=True, + params_dtype=torch.bfloat16, + use_distributed_optimizer=True, + ) + return config + + +from megatron.core import ModelParallelConfig + + +def init_model_parallel_config(config: DictConfig) -> ModelParallelConfig: + # TODO(sgm): check how to disable megatron timers + timers = FakeTimers() + return ModelParallelConfig(tensor_model_parallel_size=config.get('tensor_model_parallel_size'), + pipeline_model_parallel_size=config.get('pipeline_model_parallel_size'), + virtual_pipeline_model_parallel_size=config.get('virtual_pipeline_model_parallel_size'), + sequence_parallel=config.get('sequence_parallel'), + params_dtype=PrecisionType.to_dtype(config.get('param_dtype')), + pipeline_dtype=PrecisionType.to_dtype(config.get('param_dtype')), + bf16=True, + fp16=False, + timers=timers) + + +class FakeTimers: + """Disable All Megatron Timing with FakeTimers""" + + def __init__(self): + from megatron.timers import DummyTimer + self.dummy_timer = DummyTimer() + + def __call__(self, *args: Any, **kwds: Any) -> Any: + return self.dummy_timer + + +def offload_megatron_param_and_grad(module_list: nn.ModuleList, offload_grad=False, hybrid_engine=None): + if hybrid_engine is not None: + pp_rank = mpu.get_pipeline_model_parallel_rank() + for buffer in hybrid_engine.memory_buffers[pp_rank].values(): + buffer.data = buffer.data.to('cpu', non_blocking=True) + build_memory_reference_from_module(module_list, hybrid_engine.memory_buffers[pp_rank], maintain_weight=True) + else: + for module in module_list: + for _, param in module.named_parameters(): + param.data = param.data.to('cpu', non_blocking=True) + if offload_grad and param.grad is not None: + param.grad = param.grad.to("cpu", non_blocking=True) + torch.cuda.empty_cache() + + +def load_megatron_param_and_grad(module_list: nn.ModuleList, device_id, load_grad=False, hybrid_engine=None): + if hybrid_engine is not None: + pp_rank = mpu.get_pipeline_model_parallel_rank() + for buffer in hybrid_engine.memory_buffers[pp_rank].values(): + buffer.data = buffer.data.to(device_id, non_blocking=True) + build_memory_reference_from_module(module_list, hybrid_engine.memory_buffers[pp_rank], maintain_weight=True) + else: + for module in module_list: + for _, param in module.named_parameters(): + param.data = param.data.to(device_id, non_blocking=True) + if load_grad and param.grad is not None: + param.grad = param.grad.to(device_id, non_blocking=True) + torch.cuda.empty_cache() diff --git a/verl/utils/memory_buffer.py b/verl/utils/memory_buffer.py new file mode 100644 index 000000000..2e07e42f7 --- /dev/null +++ b/verl/utils/memory_buffer.py @@ -0,0 +1,214 @@ +# 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. +""" +This file contains utilities to manipulate torch memory buffers +""" + +from typing import Dict, List + +import torch +from torch import nn + + +class MemoryBuffer: + """ + A memory buffer is a contiguous torch tensor that may combine multiple tensors sharing with the underlying + memory. It must have a unique type to support this behavior. + """ + + def __init__(self, numel: int, numel_padded: int, dtype: torch.dtype): + self.numel = numel + self.numel_padded = numel_padded + self.dtype = dtype + self.data = torch.zeros(self.numel_padded, dtype=self.dtype, device='cuda', requires_grad=False) + + def zero(self): + """Reset the buffer to zero.""" + self.data.zero_() + + def get(self, shape, start_index): + """Return a tensor with the input `shape` as a view into the + 1-D data starting at `start_index`.""" + end_index = start_index + shape.numel() + assert end_index <= self.numel, \ + 'requested tensor is out of the buffer range.' + buffer_tensor = self.data[start_index:end_index] + buffer_tensor = buffer_tensor.view(shape) + return buffer_tensor + + +def calc_padded_numel(shape: torch.Size, dtype: torch.dtype): + """for cuda memory alignment, make sure alignment by 128-bits""" + align_numel = 128 // torch.finfo(dtype).bits + numel = shape.numel() + return (numel + align_numel - 1) // align_numel * align_numel + + +def get_weight_buffer_meta_from_module(module: nn.Module) -> Dict[str, Dict]: + """ + Return a dictionary containing name to a shape and dtype. + """ + weight_buffer_meta = {} + for name, param in sorted(module.named_parameters()): + weight_buffer_meta[name] = {'shape': param.shape, 'dtype': param.dtype} + return weight_buffer_meta + + +def build_memory_buffer(weight_buffer_meta: Dict[str, Dict]) -> Dict[torch.dtype, MemoryBuffer]: + """Build the memory buffer given weight_buffer_meta + + Args: + weight_buffer_meta: contains mapping from name to a dictionary containing shape and dtype of the tensors + + Returns: a large memory buffer for each dtype that can hold all the tensors + + """ + memory_buffers = {} + total_numel_map = {} # map from dtype to the total numel + for name, meta_info in sorted(weight_buffer_meta.items()): + shape = meta_info['shape'] + dtype = meta_info['dtype'] + + assert isinstance(shape, torch.Size) + assert isinstance(dtype, torch.dtype) + + if dtype not in total_numel_map: + total_numel_map[dtype] = 0 + + total_numel_map[dtype] += calc_padded_numel(shape, dtype) + + for dtype, total_numel in total_numel_map.items(): + memory_buffers[dtype] = MemoryBuffer(total_numel, total_numel, dtype) + + return memory_buffers + + +def build_memory_reference_from_module(module: torch.nn.Module, + memory_buffers: Dict[torch.dtype, MemoryBuffer], + maintain_weight=True): + start_index = {} + for dtype in memory_buffers.keys(): + start_index[dtype] = 0 + for name, param in sorted(module.named_parameters()): + memory_buffer = memory_buffers[param.dtype] + buffer = memory_buffer.get(shape=param.shape, start_index=start_index[param.dtype]) + # need to increment start_index + start_index[param.dtype] += calc_padded_numel(param.shape, dtype) + if maintain_weight: + buffer.copy_(param.data) + param.data = buffer + + +def build_memory_reference(weight_buffer_meta: Dict[str, Dict], memory_buffers: Dict[torch.dtype, MemoryBuffer]): + """Build the memory references. The memory buffers are built using the build_memory_buffer API. + This API will allocate a weight buffer pointer to the memory buffer according to the weight_buffer_meta. + + Args: + weight_buffer_meta: + memory_buffers: + + Returns: + + """ + start_idx = {} + weight_buffers = {} + for dtype in memory_buffers.keys(): + start_idx[dtype] = 0 + + for name, meta_info in sorted(weight_buffer_meta.items()): + shape = meta_info['shape'] + dtype = meta_info['dtype'] + + buffer = memory_buffers[dtype].get(shape, start_index=start_idx[dtype]) + start_idx[dtype] += calc_padded_numel(shape, dtype) + weight_buffers[name] = buffer + + return weight_buffers + + +class MemoryBufferModuleWrapper: + """ + Note that we do not design MemoryBufferModuleWrapper as an nn.Module due to + - It will change the checkpoint name + """ + + def __init__(self, module: nn.Module): + super().__init__() + self.module = module + self.weight_buffer_meta = get_weight_buffer_meta_from_module(self.module) + self.memory_buffers = build_memory_buffer(self.weight_buffer_meta) + build_memory_reference_from_module(self.module, self.memory_buffers) + + def get_memory_buffers(self): + return self.memory_buffers + + def get_weight_buffer_meta(self): + return self.weight_buffer_meta + + +class MegatronMemoryBufferForRollout(object): + """ + We assume that + - inference engine has tp + dp + - actor has tp + pp + dp + - the tp between inference engine and actor should be the same + - memory_buffers: contains a list of memory_buffers, each is a dict from dtype to MemoryBuffer + - weight_buffers: contains a list of weight_buffers, each is a dict from name to param + - named_parameters: a dict from name to parameter that normalizes the names from pp and vpp. Note that + the named_parameters may not be directly compatible with inference engine. User has to take care of + this part such as the layout mismatches. (e.g. qkv transpose) + - Note that weight_buffer, named_parameters and memory_buffers share the same underlying GPU memory. + - When doing weight sync, the data is transfer via memory buffers + """ + + def __init__(self, transform_memory_param_fn): + self._memory_buffers = [] + self._weight_buffers = [] + self._named_parameters = {} + self.transform_memory_param_fn = transform_memory_param_fn + + def initialize_weight_buffer(self, weight_buffer_meta_pp: List[Dict[str, Dict]]): + """ + Initialize the weight buffer. The weight buffer is obtained according to the actor. We will construct + a large buffer for each dtype in the weight_buffer. + + Args: + weight_buffer_meta: contains pp models, each pp models contains a dictionary of mapping from + + Returns: None + + """ + self.weight_buffer_meta_pp = weight_buffer_meta_pp + + for weight_buffer_meta in self.weight_buffer_meta_pp: + memory_buffer = build_memory_buffer(weight_buffer_meta) + self._memory_buffers.append(memory_buffer) + self._weight_buffers.append(None) + + def build_memory_reference(self): + for i, weight_buffer_meta in enumerate(self.weight_buffer_meta_pp): + self._weight_buffers[i] = build_memory_reference(weight_buffer_meta, self._memory_buffers[i]) + self._named_parameters = self.transform_memory_param_fn(self._weight_buffers) + + @property + def named_parameters(self): + return self._named_parameters + + @property + def weight_buffers(self): + return self._weight_buffers + + @property + def memory_buffers(self): + return self._memory_buffers diff --git a/verl/utils/model.py b/verl/utils/model.py new file mode 100644 index 000000000..7c35395a9 --- /dev/null +++ b/verl/utils/model.py @@ -0,0 +1,332 @@ +# 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 to create common models from huggingface +""" +import os +import warnings +from typing import Dict, Type + +import numpy as np +import torch +from torch import nn +from transformers import AutoConfig, AutoModelForCausalLM, PretrainedConfig, MistralForSequenceClassification +from verl.models.registry import ModelRegistry + + +class LambdaLayer(nn.Module): + + def __init__(self, fn): + super().__init__() + self.fn = fn + + def forward(self, *args, **kwargs): + return self.fn(*args, **kwargs) + + +def squeeze(x): + return torch.squeeze(x, dim=-1) + + +def update_model_config(module_config, override_config_kwargs): + for key, val in override_config_kwargs.items(): + setattr(module_config, key, val) + + +def get_huggingface_actor_config(model_name: str, override_config_kwargs=None, trust_remote_code=False) -> Dict: + if override_config_kwargs is None: + override_config_kwargs = {} + assert isinstance(override_config_kwargs, Dict), \ + f'override_config_kwargs must be a dict, got {type(override_config_kwargs)}' + module_config = AutoConfig.from_pretrained(model_name, trust_remote_code=trust_remote_code) + update_model_config(module_config, override_config_kwargs) + + return module_config + + +def create_huggingface_actor(model_name: str, override_config_kwargs=None, automodel_kwargs=None) -> nn.Module: + """ + + Args: + model_name: + actor_override_config_kwargs: + + Returns: + + """ + if override_config_kwargs is None: + override_config_kwargs = {} + if automodel_kwargs is None: + automodel_kwargs = {} + assert isinstance(override_config_kwargs, Dict), \ + f'override_config_kwargs must be a dict, got {type(override_config_kwargs)}' + module_config = get_huggingface_actor_config(model_name, + override_config_kwargs, + trust_remote_code=automodel_kwargs.get('trust_remote_code', False)) + module: nn.Module = AutoModelForCausalLM.from_config(module_config, **automodel_kwargs) + return module + + +def create_huggingface_critic(model_name: str, override_config_kwargs=None, automodel_kwargs=None) -> nn.Module: + """ + + Args: + model_name: + override_config_kwargs: + + Returns: + + """ + critic_module: nn.Module = create_huggingface_actor(model_name, + override_config_kwargs=override_config_kwargs, + automodel_kwargs=automodel_kwargs) + if automodel_kwargs is None: + automodel_kwargs = {} + torch_dtype = automodel_kwargs.get('torch_dtype', torch.float32) + critic_module.lm_head = nn.Sequential(nn.Linear(critic_module.config.hidden_size, 1, dtype=torch_dtype), + LambdaLayer(fn=squeeze)) + return critic_module + + +def get_model_size(model: nn.Module, scale='auto'): + n_params = sum(p.numel() for p in model.parameters()) + + if scale == 'auto': + if n_params > 1e9: + scale = 'B' + elif n_params > 1e6: + scale = 'M' + elif n_params > 1e3: + scale = 'K' + else: + scale = '' + + if scale == 'B': + n_params = n_params / 1e9 + elif scale == 'M': + n_params = n_params / 1e6 + elif scale == 'K': + n_params = n_params / 1e3 + elif scale == '': + pass + else: + raise NotImplemented(f'Unknown scale {scale}') + + return n_params, scale + + +def print_model_size(model: nn.Module, name: str = None): + n_params, scale = get_model_size(model, scale='auto') + if name is None: + name = model.__class__.__name__ + print(f'{name} contains {n_params:.2f}{scale} parameters') + + +def create_random_mask(input_ids: torch.Tensor, + max_ratio_of_valid_token: float, + max_ratio_of_left_padding: float, + min_ratio_of_valid_token: float = 0): + """Create a random mask given input_ids. Support left padding and right padding. + Process: + - Sample valid token length + - Sample left_padding length + - Generate padding + + Args: + input_ids: + shape (batch_size, seq_len) + + Returns: + + """ + assert max_ratio_of_valid_token > 0 and max_ratio_of_valid_token < 1. + assert max_ratio_of_left_padding > 0 and max_ratio_of_left_padding < 1. + assert min_ratio_of_valid_token <= max_ratio_of_valid_token + + batch_size, sequence_length = input_ids.shape + max_num_valid_tokens = int(sequence_length * max_ratio_of_valid_token) + min_num_valid_tokens = max(1, int(sequence_length * min_ratio_of_valid_token)) + max_left_padding = int(sequence_length * max_ratio_of_left_padding) + assert max_num_valid_tokens + max_left_padding <= sequence_length + assert max_num_valid_tokens > 0 and max_ratio_of_valid_token <= sequence_length + masks = torch.ones_like(input_ids, dtype=torch.int64) + # TODO: we can make this faster + for i in range(batch_size): + num_left_padding = np.random.randint(low=0, high=max_left_padding + 1, dtype=np.int64) + num_valid = np.random.randint(low=min_num_valid_tokens, high=max_num_valid_tokens + 1, dtype=np.int64) + + for index in range(num_left_padding): + masks[i, index] = 0 + + for index in range(num_left_padding + num_valid, sequence_length): + masks[i, index] = 0 + return masks + + +def compute_position_id_with_mask(mask): + return torch.clip(torch.cumsum(mask, dim=-1) - 1, min=0, max=None) + + +def normalize_pp_vpp_params(params, num_hidden_layers, layer_name='layers'): + """ + Normalize the pp vpp params into a complete named parameters. + This is useful when gather parameters from pp ranks and passed to a model without pp + + params: List[List[Dict[str, param]]] + params contains a list of pp, with a list of vpp named_parameters in each vpp chunk. + output: Dict[str, param] + + """ + + def normalize_model_name(name, pp_rank, vpp_rank, pp_size, vpp_size, num_layers): + """ + Transform the model name in each model_chunk in each pp stage into the name in inference engine + """ + if vpp_size > 1: + # print(f'try to bind vpp params to inference engine...') + layers_per_pp = num_layers // pp_size + layers_per_vpp = layers_per_pp // vpp_size + pp_offset = layers_per_vpp * pp_rank + vpp_offset = (layers_per_vpp * pp_size) * vpp_rank + layer_offset = pp_offset + vpp_offset + else: + layers_per_pp = num_layers // pp_size + layer_offset = layers_per_pp * pp_rank + + if layer_name in name: # belong to an intermediate layer + split_name = name.split('.') + # find the num next to split_name + for i, name in enumerate(split_name): + if name == layer_name: + break + layer_num_idx = i + 1 + # check the name + assert len(split_name) >= layer_num_idx + 1, f'split_name = {split_name}' + assert split_name[layer_num_idx].isdigit(), f'split_name = {split_name}' + # increment layer_num_idx by layer_offset + split_name[layer_num_idx] = str(int(split_name[layer_num_idx]) + layer_offset) + name = '.'.join(split_name) # weight name in inference_tp_model + return name + + pp_size = len(params) + normalized_name_to_param = {} + for pp_rank in range(len(params)): + vpp_size = len(params[pp_rank]) + for vpp_rank in range(vpp_size): + for name, param in params[pp_rank][vpp_rank].items(): + normalized_name = normalize_model_name(name, pp_rank, vpp_rank, pp_size, vpp_size, num_hidden_layers) + normalized_name_to_param[normalized_name] = param + + return normalized_name_to_param + + +def get_parallel_model_from_config(config, megatron_config, pre_process=None, post_process=None, value=False): + from megatron.core import ModelParallelConfig + assert isinstance(megatron_config, ModelParallelConfig) + model_class = _get_parallel_model_architecture_from_config(config, value) + + model = model_class(config, megatron_config, pre_process=pre_process, post_process=post_process) + return model + + +def _get_parallel_model_architecture_from_config(config: PretrainedConfig, value=False) -> Type[nn.Module]: + architectures = getattr(config, "architectures", []) + for arch in architectures: + model_cls = ModelRegistry.load_model_cls(arch, value) + if model_cls is not None: + return model_cls + raise ValueError(f"Model architectures {architectures} are not supported for now. " + f"Supported architectures: {ModelRegistry.get_supported_archs()}") + + +def load_megatron_model_weights(config, + model_config, + parallel_model, + params_dtype, + is_value_model=False, + local_cache_path='~/.cache/verl/rlhf'): + assert hasattr(model_config, "architectures"), "architectures cannot be empty when load weight!" + architectures = getattr(model_config, "architectures", []) + local_cache_path = os.path.expanduser(local_cache_path) + + if config.model.path.startswith("hdfs:"): + from verl.utils.fs import copy_local_path_from_hdfs + print(f'start download from {config.model.path}') + local_model_path = copy_local_path_from_hdfs(src=config.model.path, cache_dir=local_cache_path) + print('finish download') + else: + print(f"load from local dir {config.model.path}") + local_model_path = config.model.path + + # TODO: to find a better way to load mistral7b-rm lm_head + if 'mistral7b-rm' in config.model.path: + model = MistralForSequenceClassification.from_pretrained(local_model_path) # use score head instead of lm_head + state_dict = model.state_dict() + state_dict['lm_head.weight'] = state_dict['score.weight'] + state_dict['model.embed_tokens.weight'] = state_dict[ + 'model.embed_tokens.weight'][:32000] # workaround, 32001 -> 32000 + is_value_model = True + else: + with warnings.catch_warnings(): + warnings.simplefilter("ignore") + model = AutoModelForCausalLM.from_pretrained(local_model_path) + state_dict = model.state_dict() + + from verl.models.weight_loader_registry import get_weight_loader + print(f'before weight loader: architectures = {architectures}...') + for arch in architectures: + print(f'call weight loader arch = {arch}, model config = {model.config}') + weight_loader = get_weight_loader(arch) + weight_loader(state_dict=state_dict, + wrapped_models=parallel_model, + config=model.config, + params_dtype=params_dtype, + is_value_model=is_value_model) + + +# pad input_ids_rmpad, cu_seqlens and max_seqlen_in_batch to be divisible by tp +def pad_packed_inputs(unpad_tokens: torch.Tensor, cu_seqlens, max_seqlen_in_batch, size): + """pad the tokens such that the total length is a multiple of size. + This function is useful when applying sequence parallel and context parallel + + Args: + unpad_tokens: (total_nnz, ...). Tokens after removing padding + cu_seqlens: (total_nnz + 1,) + max_seqlen_in_batch: int + + Returns: + + """ + F = nn.functional + + total_nnz = unpad_tokens.shape[0] + + if total_nnz % size == 0: + pad_size = 0 + else: + pad_size = size - total_nnz % size + + # we assume adding a new data in the batch with seqlen pad_size + if pad_size > 0: + if unpad_tokens.ndim == 1: + unpad_tokens = F.pad(unpad_tokens, (0, pad_size)) + elif unpad_tokens.ndim == 2: + unpad_tokens = F.pad(unpad_tokens, (0, 0, 0, pad_size)) + else: + raise NotImplementedError(f'Padding dim {unpad_tokens.ndim()} is not supported') + + cu_seqlens = F.pad(cu_seqlens, (0, 1), value=pad_size + cu_seqlens[-1]) + max_seqlen_in_batch = max(max_seqlen_in_batch, pad_size) + + return unpad_tokens, cu_seqlens, max_seqlen_in_batch diff --git a/verl/utils/py_functional.py b/verl/utils/py_functional.py new file mode 100644 index 000000000..8f5a0e176 --- /dev/null +++ b/verl/utils/py_functional.py @@ -0,0 +1,56 @@ +# 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 python utility functions +""" + +from typing import Dict +from types import SimpleNamespace + + +def union_two_dict(dict1: Dict, dict2: Dict): + """Union two dict. Will throw an error if there is an item not the same object with the same key. + + Args: + dict1: + dict2: + + Returns: + + """ + for key, val in dict2.items(): + if key in dict1: + assert dict2[key] == dict1[key], \ + f'{key} in meta_dict1 and meta_dict2 are not the same object' + dict1[key] = val + + return dict1 + + +def append_to_dict(data: Dict, new_data: Dict): + for key, val in new_data.items(): + if key not in data: + data[key] = [] + data[key].append(val) + + +class NestedNamespace(SimpleNamespace): + + def __init__(self, dictionary, **kwargs): + super().__init__(**kwargs) + for key, value in dictionary.items(): + if isinstance(value, dict): + self.__setattr__(key, NestedNamespace(value)) + else: + self.__setattr__(key, value) diff --git a/verl/utils/ray_utils.py b/verl/utils/ray_utils.py new file mode 100644 index 000000000..9a75df6c3 --- /dev/null +++ b/verl/utils/ray_utils.py @@ -0,0 +1,43 @@ +# 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. +""" +Contains commonly used utilities for ray +""" + +import ray + +import concurrent.futures + + +def parallel_put(data_list, max_workers=None): + + def put_data(index, data): + return index, ray.put(data) + + if max_workers is None: + max_workers = min(len(data_list), 16) + + with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor: + data_list_f = [executor.submit(put_data, i, data) for i, data in enumerate(data_list)] + res_lst = [] + for future in concurrent.futures.as_completed(data_list_f): + res_lst.append(future.result()) + + # reorder based on index + output = [None for _ in range(len(data_list))] + for res in res_lst: + index, data_ref = res + output[index] = data_ref + + return output diff --git a/verl/utils/rendezvous/__init__.py b/verl/utils/rendezvous/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/utils/rendezvous/__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/utils/rendezvous/ray_backend.py b/verl/utils/rendezvous/ray_backend.py new file mode 100644 index 000000000..c0d2bd906 --- /dev/null +++ b/verl/utils/rendezvous/ray_backend.py @@ -0,0 +1,77 @@ +# 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 time + +from cupy.cuda.nccl import NcclCommunicator, get_unique_id + +import ray +from ray.util import list_named_actors + + +@ray.remote +class NCCLIDStore: + + def __init__(self, nccl_id): + self._nccl_id = nccl_id + + def get(self): + return self._nccl_id + + +def get_nccl_id_store_by_name(name): + all_actors = list_named_actors(all_namespaces=True) + matched_actors = [actor for actor in all_actors if actor.get("name", None) == name] + if len(matched_actors) == 1: + actor = matched_actors[0] + return ray.get_actor(**actor) + elif len(matched_actors) > 1: + logging.warning(f"multiple actors with same name found: {matched_actors}") + elif len(matched_actors) == 0: + logging.info(f"failed to get any actor named {name}") + return None + + +def create_nccl_communicator_in_ray(rank: int, + world_size: int, + group_name: str, + max_retries: int = 100, + interval_s: int = 5): + if rank == 0: + nccl_id = get_unique_id() + nccl_id_store = NCCLIDStore.options(name=group_name).remote(nccl_id) + + assert ray.get(nccl_id_store.get.remote()) == nccl_id + communicator = NcclCommunicator( + ndev=world_size, + commId=nccl_id, + rank=0, + ) + return communicator + else: + for i in range(max_retries): + nccl_id_store = get_nccl_id_store_by_name(group_name) + if nccl_id_store is not None: + logging.info(f"nccl_id_store {group_name} got") + nccl_id = ray.get(nccl_id_store.get.remote()) + logging.info(f"nccl id for {group_name} got: {nccl_id}") + communicator = NcclCommunicator( + ndev=world_size, + commId=nccl_id, + rank=rank, + ) + return communicator + logging.info(f"failed to get nccl_id for {i+1} time, sleep for {interval_s} seconds") + time.sleep(interval_s) diff --git a/verl/utils/reward_score/__init__.py b/verl/utils/reward_score/__init__.py new file mode 100644 index 000000000..1ce90c5eb --- /dev/null +++ b/verl/utils/reward_score/__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/utils/reward_score/gsm8k.py b/verl/utils/reward_score/gsm8k.py new file mode 100644 index 000000000..680620b65 --- /dev/null +++ b/verl/utils/reward_score/gsm8k.py @@ -0,0 +1,52 @@ +# 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 re + + +def extract_solution(solution_str, method='strict'): + assert method in ['strict', 'flexible'] + + if method == 'strict': + # this also tests the formatting of the model + solution = re.search("#### (\\-?[0-9\\.\\,]+)", solution_str) + if solution is None: + final_answer = None + else: + final_answer = solution.group(0) + final_answer = final_answer.split('#### ')[1].replace(',', '').replace('$', '') + elif method == 'flexible': + answer = re.findall("(\\-?[0-9\\.\\,]+)", solution_str) + final_answer = None + if len(answer) == 0: + # no reward is there is no answer + pass + else: + invalid_str = ['', '.'] + # find the last number that is not '.' + for final_answer in reversed(answer): + if final_answer not in invalid_str: + break + return final_answer + + +def compute_score(solution_str, ground_truth, method='strict', format_score=0., score=1.): + answer = extract_solution(solution_str=solution_str, method=method) + if answer is None: + return 0 + else: + if answer == ground_truth: + return score + else: + return format_score diff --git a/verl/utils/reward_score/math.py b/verl/utils/reward_score/math.py new file mode 100644 index 000000000..50792aa6e --- /dev/null +++ b/verl/utils/reward_score/math.py @@ -0,0 +1,227 @@ +# Copyright 2024 Bytedance Ltd. and/or its affiliates +# Copyright 2022 EleutherAI and the HuggingFace Inc. team. 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. +# Adapted from https://github.com/EleutherAI/lm-evaluation-harness/blob/main/lm_eval/tasks/hendrycks_math/utils.py + + +def compute_score(solution_str, ground_truth) -> float: + retval = 0. + try: + string_in_last_boxed = last_boxed_only_string(solution_str) + if string_in_last_boxed is not None: + answer = remove_boxed(string_in_last_boxed) + if is_equiv(answer, ground_truth): + retval = 1. + except Exception as e: + print(e) + + return retval + + +# string normalization from https://github.com/EleutherAI/lm-evaluation-harness/blob/master/lm_eval/tasks/hendrycks_math.py +def is_equiv(str1, str2, verbose=False): + if str1 is None and str2 is None: + print("WARNING: Both None") + return True + if str1 is None or str2 is None: + return False + + try: + ss1 = strip_string(str1) + ss2 = strip_string(str2) + if verbose: + print(ss1, ss2) + return ss1 == ss2 + except Exception: + return str1 == str2 + + +def remove_boxed(s): + if "\\boxed " in s: + left = "\\boxed " + assert s[:len(left)] == left + return s[len(left):] + + left = "\\boxed{" + + assert s[:len(left)] == left + assert s[-1] == "}" + + return s[len(left):-1] + + +def last_boxed_only_string(string): + idx = string.rfind("\\boxed") + if "\\boxed " in string: + return "\\boxed " + string.split("\\boxed ")[-1].split("$")[0] + if idx < 0: + idx = string.rfind("\\fbox") + if idx < 0: + return None + + i = idx + right_brace_idx = None + num_left_braces_open = 0 + while i < len(string): + if string[i] == "{": + num_left_braces_open += 1 + if string[i] == "}": + num_left_braces_open -= 1 + if num_left_braces_open == 0: + right_brace_idx = i + break + i += 1 + + if right_brace_idx is None: + retval = None + else: + retval = string[idx:right_brace_idx + 1] + + return retval + + +def fix_fracs(string): + substrs = string.split("\\frac") + new_str = substrs[0] + if len(substrs) > 1: + substrs = substrs[1:] + for substr in substrs: + new_str += "\\frac" + if substr[0] == "{": + new_str += substr + else: + try: + assert len(substr) >= 2 + except AssertionError: + return string + a = substr[0] + b = substr[1] + if b != "{": + if len(substr) > 2: + post_substr = substr[2:] + new_str += "{" + a + "}{" + b + "}" + post_substr + else: + new_str += "{" + a + "}{" + b + "}" + else: + if len(substr) > 2: + post_substr = substr[2:] + new_str += "{" + a + "}" + b + post_substr + else: + new_str += "{" + a + "}" + b + string = new_str + return string + + +def fix_a_slash_b(string): + if len(string.split("/")) != 2: + return string + a = string.split("/")[0] + b = string.split("/")[1] + try: + a = int(a) + b = int(b) + assert string == "{}/{}".format(a, b) + new_string = "\\frac{" + str(a) + "}{" + str(b) + "}" + return new_string + except AssertionError: + return string + + +def remove_right_units(string): + # "\\text{ " only ever occurs (at least in the val set) when describing units + if "\\text{ " in string: + splits = string.split("\\text{ ") + assert len(splits) == 2 + return splits[0] + else: + return string + + +def fix_sqrt(string): + if "\\sqrt" not in string: + return string + splits = string.split("\\sqrt") + new_string = splits[0] + for split in splits[1:]: + if split[0] != "{": + a = split[0] + new_substr = "\\sqrt{" + a + "}" + split[1:] + else: + new_substr = "\\sqrt" + split + new_string += new_substr + return new_string + + +def strip_string(string): + # linebreaks + string = string.replace("\n", "") + + # remove inverse spaces + string = string.replace("\\!", "") + + # replace \\ with \ + string = string.replace("\\\\", "\\") + + # replace tfrac and dfrac with frac + string = string.replace("tfrac", "frac") + string = string.replace("dfrac", "frac") + + # remove \left and \right + string = string.replace("\\left", "") + string = string.replace("\\right", "") + + # Remove circ (degrees) + string = string.replace("^{\\circ}", "") + string = string.replace("^\\circ", "") + + # remove dollar signs + string = string.replace("\\$", "") + + # remove units (on the right) + string = remove_right_units(string) + + # remove percentage + string = string.replace("\\%", "") + string = string.replace("\%", "") # noqa: W605 + + # " 0." equivalent to " ." and "{0." equivalent to "{." Alternatively, add "0" if "." is the start of the string + string = string.replace(" .", " 0.") + string = string.replace("{.", "{0.") + # if empty, return empty string + if len(string) == 0: + return string + if string[0] == ".": + string = "0" + string + + # to consider: get rid of e.g. "k = " or "q = " at beginning + if len(string.split("=")) == 2: + if len(string.split("=")[0]) <= 2: + string = string.split("=")[1] + + # fix sqrt3 --> sqrt{3} + string = fix_sqrt(string) + + # remove spaces + string = string.replace(" ", "") + + # \frac1b or \frac12 --> \frac{1}{b} and \frac{1}{2}, etc. Even works with \frac1{72} (but not \frac{72}1). Also does a/b --> \\frac{a}{b} + string = fix_fracs(string) + + # manually change 0.5 --> \frac{1}{2} + if string == "0.5": + string = "\\frac{1}{2}" + + # NOTE: X/Y changed to \frac{X}{Y} in dataset, but in simple cases fix in case the model output is X/Y + string = fix_a_slash_b(string) + + return string diff --git a/verl/utils/torch_dtypes.py b/verl/utils/torch_dtypes.py new file mode 100644 index 000000000..bb63df13b --- /dev/null +++ b/verl/utils/torch_dtypes.py @@ -0,0 +1,82 @@ +# 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 + +from typing import Union + +HALF_LIST = [16, "16", "fp16", "float16"] +FLOAT_LIST = [32, "32", "fp32", "float32"] +BFLOAT_LIST = ["bf16", "bfloat16"] + + +class PrecisionType(object): + """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: Union[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/utils/torch_functional.py b/verl/utils/torch_functional.py new file mode 100644 index 000000000..b6c129761 --- /dev/null +++ b/verl/utils/torch_functional.py @@ -0,0 +1,465 @@ +# 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 +""" + +from typing import Dict, Union, List, Optional + +import os +import torch +import torch.distributed +import torch.nn.functional as F +from tensordict import TensorDict +from torch import nn + +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 + + +def gather_from_labels(data, label): + """Gather the label from data. The value in label should be [0, vocab_size) + + Args: + data: (..., vocab_size) + label (torch.IntTensor) : (...,) + + Returns: + + """ + + output = torch.gather(data, -1, label.unsqueeze(-1)).squeeze(-1) + return output + + +def logprobs_from_logits(logits, labels): + """ + See: https://github.com/pytorch/pytorch/issues/563#issuecomment-330103591 + """ + 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) + output = output.view(*batch_dim) + else: + output = logprobs_from_logits_naive(logits, labels) + return output + + +def logprobs_from_logits_flash_attn(logits, labels): + output = -cross_entropy_loss(logits, labels)[0] + return output + + +def logprobs_from_logits_naive(logits, labels): + logp = F.log_softmax(logits, dim=-1) + logpy = gather_from_labels(logp, labels) + return logpy + + +def logprobs_of_labels_v2(logits: torch.FloatTensor, labels): + """ + A memory efficient implementation of logprobs_from_logits + """ + assert logits.dtype == torch.float32, 'Using bf16 logits with logprobs_of_labels_v2 may lead to divergence' + logprobs_labels = torch.gather(logits, dim=-1, index=labels.unsqueeze(-1)) + logprobs_labels = logprobs_labels - torch.logsumexp(logits, dim=-1, keepdim=True) + return logprobs_labels.squeeze(-1) + + +def clip_by_value(x, tensor_min, tensor_max): + """ + Tensor extenstion to torch.clamp + 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): + """Calculate entropy from logits.""" + pd = torch.nn.functional.softmax(logits, dim=-1) + entropy = torch.logsumexp(logits, dim=-1) - torch.sum(pd * logits, dim=-1) + return entropy + + +def masked_sum(values, mask, axis=None): + """Compute mean of tensor with a masked values.""" + return (values * mask).sum(axis=axis) + + +def masked_mean(values, mask, axis=None): + """Compute mean of tensor with a masked values.""" + return (values * mask).sum(axis=axis) / mask.sum(axis=axis) + + +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 with masked 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_eos_mask(response_id: torch.Tensor, eos_token: int = 2, dtype=torch.int64): + ''' + e.g. end of sentence token=1 + response_id: [0, 0, 2, 42, 3, 5, 1, 0, 0] + eos_mask: [1, 1, 1, 1, 1, 1, 1, 0, 0] + ''' + eos_mask = response_id.eq(eos_token).long() + eos_mask = (torch.cumsum(eos_mask, dim=1) - eos_mask).bool() + eos_mask = torch.logical_not(eos_mask).to(dtype) + return eos_mask + + +def compute_grad_norm(model: nn.Module): + total_grad_square = 0 + total_params = 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: Union[Dict[str, torch.Tensor], TensorDict], src, group): + """ + TODO: optimize this. Technically, we only need one broadcast + """ + + for key in tensors.sorted_keys: + torch.distributed.broadcast(tensors[key], src=src, group=group, async_op=False) + + +def allgather_dict_tensors(tensors: Union[Dict[str, torch.Tensor], TensorDict], size, group, dim=0): + """ + TODO: optimize this. + - We can use async ops + - We can use only one allgather + Args: + tensors: + size: + group: + + Returns: + + """ + 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 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_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] + (0, max_seq_len - tensors.shape[-1]) means right pad to max_seq_length and no left pad + """ + if tensors.shape[-1] >= max_seq_len: + return tensors + 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) + + +from transformers import PreTrainedTokenizer + + +def tokenize_and_postprocess_data(prompt: str, + tokenizer: PreTrainedTokenizer, + max_length: int, + pad_token_id: int, + left_pad=True, + truncation='error'): + """ + input_data is the output from tokenizer. + """ + assert truncation in ['left', 'right', 'error'] + + input_data = tokenizer(prompt, return_tensors='pt', add_special_tokens=False) + + input_ids = input_data['input_ids'] + attention_mask = input_data['attention_mask'] + + 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 == '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 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): + 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 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: [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, cu_seqlens, max_seqlen_in_batch = 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 + + +from transformers.generation.logits_process import (TemperatureLogitsWarper, TopKLogitsWarper, TopPLogitsWarper) + + +def post_process_logits(input_ids, logits, temperature, top_k, top_p): + if temperature != 1.: + 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 + + +""" +Optimizer related +""" + +from torch.optim import Optimizer +from torch.optim.lr_scheduler import LambdaLR +import math + + +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, +): + """ + 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. + Return: + :obj:`torch.optim.lr_scheduler.LambdaLR` with the appropriate schedule. + """ + assert min_lr_ratio >= 0 and min_lr_ratio <= 1. + coef = (1 - min_lr_ratio) * 0.5 + intercept = (1 + min_lr_ratio) * 0.5 + + def lr_lambda(current_step): + if current_step < num_warmup_steps: + return 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(0.0, 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, +): + + def lr_lambda(current_step): + return min(1, float(current_step) / float(max(1, num_warmup_steps))) + + 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, + ) diff --git a/verl/utils/tracking.py b/verl/utils/tracking.py new file mode 100644 index 000000000..1c8284e26 --- /dev/null +++ b/verl/utils/tracking.py @@ -0,0 +1,46 @@ +# 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 wandb + +from typing import List, Union + + +class Tracking(object): + supported_backend = ['tracking', 'console'] + + def __init__(self, project_name, experiment_name, default_backend: Union[str, List[str]] = 'console', config=None): + if isinstance(default_backend, str): + default_backend = [default_backend] + for backend in default_backend: + assert backend in self.supported_backend, f'{backend} is not supported' + + self.logger = {} + + if 'tracking' in default_backend: + wandb.init(project=project_name, name=experiment_name, config=config) + self.logger['tracking'] = wandb + + if 'console' in default_backend: + from verl.utils.logger.aggregate_logger import LocalLogger + self.console_logger = LocalLogger(print_to_console=True) + self.logger['console'] = self.console_logger + + 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) diff --git a/verl/version/version b/verl/version/version new file mode 100644 index 000000000..7bcd0e361 --- /dev/null +++ b/verl/version/version @@ -0,0 +1 @@ +0.0.2 \ No newline at end of file