run.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env bash
  2. set -eou pipefail
  3. stage=-1
  4. stop_stage=5
  5. log() {
  6. # This function is from espnet
  7. local fname=${BASH_SOURCE[1]##*/}
  8. echo -e "$(date '+%Y-%m-%d %H:%M:%S') (${fname}:${BASH_LINENO[0]}:${FUNCNAME[1]}) $*"
  9. }
  10. export PYTHONPATH=/workspace/CosyVoice
  11. model_scope_model_path=./CosyVoice2-0.5B
  12. sft_model_path=./transformers_cosyvoice2_llm
  13. if [ $stage -le -1 ] && [ $stop_stage -ge -1 ]; then
  14. log "stage -1: download official CosyVoice2-0.5B LLM model and convert to huggingface compatible checkpoint"
  15. modelscope download --model iic/CosyVoice2-0.5B --local_dir $model_scope_model_path
  16. python3 pretrained_to_huggingface.py \
  17. --pretrained-cosyvoice2-path $model_scope_model_path \
  18. --save-path $sft_model_path
  19. # Or, you could use the following command to download the huggingface compatible checkpoint
  20. # huggingface-cli download --local-dir $sft_model_path yuekai/cosyvoice2_llm
  21. fi
  22. data_dir=data/parquet_aishell3
  23. if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
  24. log "stage 0: prepare data into verl format"
  25. mkdir -p $data_dir
  26. wget https://huggingface.co/datasets/SparkAudio/voxbox/resolve/main/metadata/aishell-3.jsonl -O data/aishell-3.jsonl
  27. # total 88035 samples
  28. head -n 80000 data/aishell-3.jsonl > data/train.jsonl
  29. tail -n 100 data/aishell-3.jsonl > data/test.jsonl
  30. python prepare_data.py \
  31. --train_file data/train.jsonl \
  32. --test_file data/test.jsonl \
  33. --local_dir $data_dir
  34. fi
  35. if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
  36. log "stage 1: start token2wav asr server for reward function"
  37. python3 token2wav_asr_server.py --number-of-devices 8
  38. fi
  39. exp_name=official_llm_aishell3_grpo
  40. if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  41. log "stage 2: grpo train"
  42. export CUDA_VISIBLE_DEVICES="0,1,2,3,4,5,6,7"
  43. export MKL_SERVICE_FORCE_INTEL=TRUE
  44. n_gpus_per_node=8
  45. micro_batch_size=4
  46. train_batch_size=32
  47. python3 -m verl.trainer.main_ppo \
  48. algorithm.adv_estimator=grpo \
  49. data.train_files=$data_dir/train.parquet \
  50. data.val_files=$data_dir/test.parquet \
  51. data.train_batch_size=$train_batch_size \
  52. data.max_prompt_length=1024 \
  53. data.max_response_length=512 \
  54. data.truncation='error' \
  55. actor_rollout_ref.model.use_remove_padding=False \
  56. actor_rollout_ref.model.path=$sft_model_path \
  57. actor_rollout_ref.actor.optim.lr=1e-6 \
  58. actor_rollout_ref.actor.ppo_mini_batch_size=32 \
  59. actor_rollout_ref.actor.ppo_micro_batch_size_per_gpu=$micro_batch_size \
  60. actor_rollout_ref.actor.use_kl_loss=False \
  61. actor_rollout_ref.model.enable_gradient_checkpointing=True \
  62. actor_rollout_ref.actor.fsdp_config.param_offload=False \
  63. actor_rollout_ref.actor.fsdp_config.optimizer_offload=False \
  64. actor_rollout_ref.rollout.log_prob_micro_batch_size_per_gpu=$micro_batch_size \
  65. actor_rollout_ref.rollout.tensor_model_parallel_size=1 \
  66. actor_rollout_ref.rollout.name=vllm \
  67. actor_rollout_ref.rollout.gpu_memory_utilization=0.6 \
  68. actor_rollout_ref.rollout.do_sample=true \
  69. actor_rollout_ref.rollout.temperature=0.8 \
  70. actor_rollout_ref.rollout.top_p=0.95 \
  71. actor_rollout_ref.rollout.top_k=25 \
  72. actor_rollout_ref.rollout.n=4 \
  73. actor_rollout_ref.rollout.val_kwargs.do_sample=true \
  74. actor_rollout_ref.rollout.val_kwargs.temperature=0.8 \
  75. actor_rollout_ref.rollout.val_kwargs.top_p=0.95 \
  76. actor_rollout_ref.rollout.val_kwargs.top_k=25 \
  77. reward_model.reward_manager=prime \
  78. custom_reward_function.path=reward_tts.py \
  79. custom_reward_function.name=compute_score \
  80. trainer.project_name='cosyvoice2_grpo' \
  81. trainer.experiment_name=$exp_name \
  82. trainer.logger=['console','wandb'] \
  83. trainer.n_gpus_per_node=$n_gpus_per_node \
  84. trainer.nnodes=1 \
  85. trainer.save_freq=100 \
  86. trainer.test_freq=100 \
  87. trainer.resume_mode='auto' \
  88. trainer.total_epochs=1 \
  89. trainer.val_before_train=False
  90. fi
  91. step=400
  92. llm_path=./checkpoints/cosyvoice2_grpo/$exp_name/global_step_${step}
  93. if [ $stage -le 3 ] && [ $stop_stage -ge 3 ]; then
  94. log "stage 3: merge the model"
  95. python -m verl.model_merger merge \
  96. --backend fsdp \
  97. --local_dir $llm_path/actor \
  98. --target_dir $llm_path/merged_hf_model || exit 1
  99. fi
  100. if [ $stage -le 4 ] && [ $stop_stage -ge 4 ]; then
  101. log "stage 4: Test the model"
  102. dataset=zero_shot_zh
  103. # dataset=test_zh
  104. output_dir=./outputs_${exp_name}_${step}_${dataset}
  105. token2wav_path=/workspace/CosyVoice2-0.5B
  106. model_path=$llm_path/merged_hf_model
  107. CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 \
  108. torchrun --nproc_per_node=8 \
  109. infer_dataset.py \
  110. --output-dir $output_dir \
  111. --llm-model-name-or-path $model_path \
  112. --token2wav-path $token2wav_path \
  113. --split-name ${dataset} || exit 1
  114. bash scripts/compute_wer.sh $output_dir ${dataset}
  115. fi
  116. if [ $stage -le 5 ] && [ $stop_stage -ge 5 ]; then
  117. log "stage 5: Convert the RL trained model to CosyVoice repo format"
  118. python3 huggingface_to_pretrained.py \
  119. --hf-cosyvoice2-llm-path $llm_path/merged_hf_model \
  120. --pretrained-cosyvoice2-path /workspace/CosyVoice2-0.5B \
  121. --output-path /workspace/CosyVoice2-0.5B/llm-new.pt
  122. # You need to manually move the llm-new.pt to overwrite /workspace/CosyVoice2-0.5B/llm.pt
  123. fi