run.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #!/bin/bash
  2. # Copyright 2024 Alibaba Inc. All Rights Reserved.
  3. . ./path.sh || exit 1;
  4. stage=-1
  5. stop_stage=3
  6. data_url=www.openslr.org/resources/60
  7. data_dir=/mnt/lyuxiang.lx/data/tts/openslr/libritts
  8. pretrained_model_dir=../../../pretrained_models/Fun-CosyVoice3-0.5B
  9. if [ ${stage} -le -1 ] && [ ${stop_stage} -ge -1 ]; then
  10. echo "Data Download"
  11. for part in dev-clean test-clean dev-other test-other train-clean-100 train-clean-360 train-other-500; do
  12. local/download_and_untar.sh ${data_dir} ${data_url} ${part}
  13. done
  14. fi
  15. if [ ${stage} -le 0 ] && [ ${stop_stage} -ge 0 ]; then
  16. echo "Data preparation, prepare wav.scp/text/utt2spk/spk2utt"
  17. for x in train-clean-100 train-clean-360 train-other-500 dev-clean dev-other test-clean test-other; do
  18. mkdir -p data/$x
  19. # NOTE in CosyVoice3, we add instruct in sequence
  20. python local/prepare_data.py --src_dir $data_dir/LibriTTS/$x --des_dir data/$x --instruct "You are a helpful assistant.<|endofprompt|>"
  21. done
  22. fi
  23. if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
  24. echo "Extract campplus speaker embedding, you will get spk2embedding.pt and utt2embedding.pt in data/$x dir"
  25. for x in train-clean-100 train-clean-360 train-other-500 dev-clean dev-other test-clean test-other; do
  26. tools/extract_embedding.py --dir data/$x \
  27. --onnx_path $pretrained_model_dir/campplus.onnx
  28. done
  29. fi
  30. if [ ${stage} -le 2 ] && [ ${stop_stage} -ge 2 ]; then
  31. echo "Extract discrete speech token, you will get utt2speech_token.pt in data/$x dir"
  32. for x in train-clean-100 train-clean-360 train-other-500 dev-clean dev-other test-clean test-other; do
  33. tools/extract_speech_token.py --dir data/$x \
  34. --onnx_path $pretrained_model_dir/speech_tokenizer_v3.onnx
  35. done
  36. fi
  37. if [ ${stage} -le 3 ] && [ ${stop_stage} -ge 3 ]; then
  38. echo "Prepare required parquet format data, you should have prepared wav.scp/text/utt2spk/spk2utt/utt2embedding.pt/spk2embedding.pt/utt2speech_token.pt"
  39. for x in train-clean-100 train-clean-360 train-other-500 dev-clean dev-other test-clean test-other; do
  40. mkdir -p data/$x/parquet
  41. tools/make_parquet_list.py --num_utts_per_parquet 1000 \
  42. --num_processes 10 \
  43. --instruct \
  44. --src_dir data/$x \
  45. --des_dir data/$x/parquet
  46. done
  47. fi
  48. # train llm
  49. export CUDA_VISIBLE_DEVICES="0,1,2,3"
  50. num_gpus=$(echo $CUDA_VISIBLE_DEVICES | awk -F "," '{print NF}')
  51. job_id=1986
  52. dist_backend="nccl"
  53. num_workers=2
  54. prefetch=100
  55. train_engine=torch_ddp
  56. if [ ${stage} -le 5 ] && [ ${stop_stage} -ge 5 ]; then
  57. echo "Run train. We only support llm traning for now"
  58. if [ $train_engine == 'deepspeed' ]; then
  59. echo "Notice deepspeed has its own optimizer config. Modify conf/ds_stage2.json if necessary"
  60. fi
  61. cat data/{train-clean-100,train-clean-360,train-other-500}/parquet/data.list > data/train.data.list
  62. cat data/{dev-clean,dev-other}/parquet/data.list > data/dev.data.list
  63. # NOTE will update llm/hift training later
  64. for model in llm flow hifigan; do
  65. torchrun --nnodes=1 --nproc_per_node=$num_gpus \
  66. --rdzv_id=$job_id --rdzv_backend="c10d" --rdzv_endpoint="localhost:1234" \
  67. cosyvoice/bin/train.py \
  68. --train_engine $train_engine \
  69. --config conf/cosyvoice3.yaml \
  70. --train_data data/train.data.list \
  71. --cv_data data/dev.data.list \
  72. --qwen_pretrain_path $pretrained_model_dir/CosyVoice-BlankEN \
  73. --model $model \
  74. --checkpoint $pretrained_model_dir/$model.pt \
  75. --model_dir `pwd`/exp/cosyvoice3/$model/$train_engine \
  76. --tensorboard_dir `pwd`/tensorboard/cosyvoice3/$model/$train_engine \
  77. --ddp.dist_backend $dist_backend \
  78. --num_workers ${num_workers} \
  79. --prefetch ${prefetch} \
  80. --pin_memory \
  81. --use_amp \
  82. --deepspeed_config ./conf/ds_stage2.json \
  83. --deepspeed.save_states model+optimizer
  84. done
  85. fi
  86. # average model
  87. average_num=5
  88. if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
  89. for model in llm flow hifigan; do
  90. decode_checkpoint=`pwd`/exp/cosyvoice/$model/$train_engine/${model}.pt
  91. echo "do model average and final checkpoint is $decode_checkpoint"
  92. python cosyvoice/bin/average_model.py \
  93. --dst_model $decode_checkpoint \
  94. --src_path `pwd`/exp/cosyvoice/$model/$train_engine \
  95. --num ${average_num} \
  96. --val_best
  97. done
  98. fi
  99. if [ ${stage} -le 7 ] && [ ${stop_stage} -ge 7 ]; then
  100. echo "Export your model for inference speedup. Remember copy your llm or flow model to model_dir"
  101. python cosyvoice/bin/export_jit.py --model_dir $pretrained_model_dir
  102. python cosyvoice/bin/export_onnx.py --model_dir $pretrained_model_dir
  103. fi