run_stepaudio2_dit_token2wav.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #!/bin/bash
  2. # Copyright (c) 2025 NVIDIA (authors: Yuekai Zhang)
  3. export CUDA_VISIBLE_DEVICES=1
  4. cosyvoice_path=/workspace/CosyVoice
  5. cosyvoice_path=/workspace_yuekai/tts/CosyVoice
  6. stepaudio2_path=/workspace_yuekai/tts/Step-Audio2
  7. export PYTHONPATH=${stepaudio2_path}:$PYTHONPATH
  8. export PYTHONPATH=${cosyvoice_path}:$PYTHONPATH
  9. export PYTHONPATH=${cosyvoice_path}/third_party/Matcha-TTS:$PYTHONPATH
  10. stage=$1
  11. stop_stage=$2
  12. huggingface_model_local_dir=./cosyvoice2_llm
  13. model_scope_model_local_dir=./CosyVoice2-0.5B
  14. trt_dtype=bfloat16
  15. trt_weights_dir=./trt_weights_${trt_dtype}
  16. trt_engines_dir=./trt_engines_${trt_dtype}
  17. model_repo=./model_repo_cosyvoice2_dit
  18. use_spk2info_cache=False
  19. if [ $stage -le -1 ] && [ $stop_stage -ge -1 ]; then
  20. echo "Cloning CosyVoice"
  21. git clone --recursive https://github.com/FunAudioLLM/CosyVoice.git $cosyvoice_path
  22. cd $cosyvoice_path
  23. git submodule update --init --recursive
  24. cd runtime/triton_trtllm
  25. fi
  26. if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
  27. echo "Downloading CosyVoice2-0.5B"
  28. # see https://github.com/nvidia-china-sae/mair-hub/blob/main/rl-tutorial/cosyvoice_llm/pretrained_to_huggingface.py
  29. huggingface-cli download --local-dir $huggingface_model_local_dir yuekai/cosyvoice2_llm
  30. modelscope download --model iic/CosyVoice2-0.5B --local_dir $model_scope_model_local_dir
  31. # download spk2info.pt to directly use cached speech tokens, speech feats, and embeddings
  32. wget https://raw.githubusercontent.com/qi-hua/async_cosyvoice/main/CosyVoice2-0.5B/spk2info.pt -O $model_scope_model_local_dir/spk2info.pt
  33. fi
  34. if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
  35. echo "Converting checkpoint to TensorRT weights"
  36. python3 scripts/convert_checkpoint.py --model_dir $huggingface_model_local_dir \
  37. --output_dir $trt_weights_dir \
  38. --dtype $trt_dtype || exit 1
  39. echo "Building TensorRT engines"
  40. trtllm-build --checkpoint_dir $trt_weights_dir \
  41. --output_dir $trt_engines_dir \
  42. --max_batch_size 16 \
  43. --max_num_tokens 32768 \
  44. --gemm_plugin $trt_dtype || exit 1
  45. echo "Testing TensorRT engines"
  46. python3 ./scripts/test_llm.py --input_text "你好,请问你叫什么?" \
  47. --tokenizer_dir $huggingface_model_local_dir \
  48. --top_k 50 --top_p 0.95 --temperature 0.8 \
  49. --engine_dir=$trt_engines_dir || exit 1
  50. fi
  51. # if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  52. # echo "Creating model repository"
  53. # rm -rf $model_repo
  54. # mkdir -p $model_repo
  55. # cosyvoice2_dir="cosyvoice2_dit"
  56. # token2wav_dir="token2wav_dit"
  57. # cp -r ./model_repo/${cosyvoice2_dir} $model_repo
  58. # cp -r ./model_repo/tensorrt_llm $model_repo
  59. # cp -r ./model_repo/${token2wav_dir} $model_repo
  60. # #if [ $use_spk2info_cache == "False" ]; then
  61. # cp -r ./model_repo/audio_tokenizer $model_repo
  62. # cp -r ./model_repo/speaker_embedding $model_repo
  63. # #fi
  64. # ENGINE_PATH=$trt_engines_dir
  65. # MAX_QUEUE_DELAY_MICROSECONDS=0
  66. # MODEL_DIR=$model_scope_model_local_dir
  67. # LLM_TOKENIZER_DIR=$huggingface_model_local_dir
  68. # BLS_INSTANCE_NUM=1
  69. # TRITON_MAX_BATCH_SIZE=16
  70. # DECOUPLED_MODE=True # True for streaming, False for offline
  71. # STEP_AUDIO_MODEL_DIR=/workspace_yuekai/tts/CosyVoice/runtime/triton_trtllm/Step-Audio-2-mini/token2wav
  72. # python3 scripts/fill_template.py -i ${model_repo}/${token2wav_dir}/config.pbtxt model_dir:${STEP_AUDIO_MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  73. # python3 scripts/fill_template.py -i ${model_repo}/${cosyvoice2_dir}/config.pbtxt model_dir:${MODEL_DIR},bls_instance_num:${BLS_INSTANCE_NUM},llm_tokenizer_dir:${LLM_TOKENIZER_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  74. # python3 scripts/fill_template.py -i ${model_repo}/tensorrt_llm/config.pbtxt triton_backend:tensorrtllm,triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},max_beam_width:1,engine_dir:${ENGINE_PATH},max_tokens_in_paged_kv_cache:2560,max_attention_window_size:2560,kv_cache_free_gpu_mem_fraction:0.5,exclude_input_in_output:True,enable_kv_cache_reuse:False,batching_strategy:inflight_fused_batching,max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS},encoder_input_features_data_type:TYPE_FP16,logits_datatype:TYPE_FP32
  75. # #if [ $use_spk2info_cache == "False" ]; then
  76. # python3 scripts/fill_template.py -i ${model_repo}/audio_tokenizer/config.pbtxt model_dir:${MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  77. # python3 scripts/fill_template.py -i ${model_repo}/speaker_embedding/config.pbtxt model_dir:${MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  78. # #fi
  79. # fi
  80. if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  81. echo "Creating model repository async mode"
  82. rm -rf $model_repo
  83. mkdir -p $model_repo
  84. cosyvoice2_dir="cosyvoice2_dit"
  85. token2wav_dir="token2wav_dit"
  86. cp -r ./model_repo/${cosyvoice2_dir} $model_repo
  87. cp -r ./model_repo/tensorrt_llm $model_repo
  88. cp -r ./model_repo/${token2wav_dir} $model_repo
  89. #if [ $use_spk2info_cache == "False" ]; then
  90. cp -r ./model_repo/audio_tokenizer $model_repo
  91. cp -r ./model_repo/speaker_embedding $model_repo
  92. #fi
  93. ENGINE_PATH=$trt_engines_dir
  94. MAX_QUEUE_DELAY_MICROSECONDS=0
  95. MODEL_DIR=$model_scope_model_local_dir
  96. LLM_TOKENIZER_DIR=$huggingface_model_local_dir
  97. BLS_INSTANCE_NUM=4
  98. TRITON_MAX_BATCH_SIZE=32
  99. DECOUPLED_MODE=True # True for streaming, False for offline
  100. STEP_AUDIO_MODEL_DIR=/workspace_yuekai/tts/CosyVoice/runtime/triton_trtllm/Step-Audio-2-mini/token2wav
  101. python3 scripts/fill_template.py -i ${model_repo}/${token2wav_dir}/config.pbtxt model_dir:${STEP_AUDIO_MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  102. python3 scripts/fill_template.py -i ${model_repo}/${cosyvoice2_dir}/config.pbtxt model_dir:${MODEL_DIR},bls_instance_num:${BLS_INSTANCE_NUM},llm_tokenizer_dir:${LLM_TOKENIZER_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  103. python3 scripts/fill_template.py -i ${model_repo}/tensorrt_llm/config.pbtxt triton_backend:tensorrtllm,triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},decoupled_mode:${DECOUPLED_MODE},max_beam_width:1,engine_dir:${ENGINE_PATH},max_tokens_in_paged_kv_cache:2560,max_attention_window_size:2560,kv_cache_free_gpu_mem_fraction:0.5,exclude_input_in_output:True,enable_kv_cache_reuse:False,batching_strategy:inflight_fused_batching,max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS},encoder_input_features_data_type:TYPE_FP16,logits_datatype:TYPE_FP32
  104. #if [ $use_spk2info_cache == "False" ]; then
  105. python3 scripts/fill_template.py -i ${model_repo}/audio_tokenizer/config.pbtxt model_dir:${MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  106. python3 scripts/fill_template.py -i ${model_repo}/speaker_embedding/config.pbtxt model_dir:${MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  107. #fi
  108. rm -rf $model_repo/tensorrt_llm
  109. # mv $model_repo/cosyvoice2_dit/1 $model_repo/cosyvoice2_dit/4
  110. fi
  111. if [ $stage -le 3 ] && [ $stop_stage -ge 3 ]; then
  112. echo "Starting Triton server"
  113. tritonserver --model-repository $model_repo --http-port 18000
  114. fi
  115. if [ $stage -le 4 ] && [ $stop_stage -ge 4 ]; then
  116. echo "Single request test http, only work for offline TTS mode"
  117. python3 client_http.py \
  118. --reference-audio ./assets/prompt_audio.wav \
  119. --reference-text "吃燕窝就选燕之屋,本节目由26年专注高品质燕窝的燕之屋冠名播出。豆奶牛奶换着喝,营养更均衡,本节目由豆本豆豆奶特约播出。" \
  120. --target-text "身临其境,换新体验。塑造开源语音合成新范式,让智能语音更自然。" \
  121. --model-name cosyvoice2
  122. fi
  123. if [ $stage -le 5 ] && [ $stop_stage -ge 5 ]; then
  124. echo "Running benchmark client grpc"
  125. num_task=4
  126. mode=streaming
  127. BLS_INSTANCE_NUM=4
  128. python3 client_grpc.py \
  129. --server-addr localhost \
  130. --model-name cosyvoice2_dit \
  131. --num-tasks $num_task \
  132. --mode $mode \
  133. --huggingface-dataset yuekai/seed_tts_cosy2 \
  134. --log-dir ./log_concurrent_tasks_${num_task}_${mode}_bls_${BLS_INSTANCE_NUM}_no_att_cnn_cache_new
  135. fi
  136. if [ $stage -le 6 ] && [ $stop_stage -ge 6 ]; then
  137. echo "stage 6: Offline inference benchmark"
  138. n_gpus=1
  139. datasets=(wenetspeech4tts) # wenetspeech4tts, test_zh, zero_shot_zh
  140. backend=trtllm-serve # hf, trtllm, vllm
  141. batch_sizes=(16 8 4 2 1)
  142. batch_sizes=(16 8 4 2)
  143. token2wav_batch_size=1
  144. for batch_size in ${batch_sizes[@]}; do
  145. for dataset in ${datasets[@]}; do
  146. output_dir=./${dataset}_${backend}_llm_batch_size_${batch_size}_token2wav_batch_size_${token2wav_batch_size}
  147. CUDA_VISIBLE_DEVICES=1 \
  148. python3 offline_inference.py \
  149. --output-dir $output_dir \
  150. --llm-model-name-or-path $huggingface_model_local_dir \
  151. --token2wav-path $model_scope_model_local_dir \
  152. --backend $backend \
  153. --batch-size $batch_size --token2wav-batch-size $token2wav_batch_size \
  154. --engine-dir $trt_engines_dir \
  155. --split-name ${dataset} || exit 1
  156. done
  157. done
  158. fi
  159. if [ $stage -le 7 ] && [ $stop_stage -ge 7 ]; then
  160. python3 streaming_inference.py
  161. fi
  162. if [ $stage -le 8 ] && [ $stop_stage -ge 8 ]; then
  163. mpirun -np 1 --allow-run-as-root --oversubscribe trtllm-serve serve --tokenizer $huggingface_model_local_dir $trt_engines_dir --max_batch_size 16
  164. fi
  165. if [ $stage -le 9 ] && [ $stop_stage -ge 9 ]; then
  166. #! /usr/bin/env bash
  167. curl http://localhost:8000/v1/chat/completions \
  168. -H "Content-Type: application/json" \
  169. -d '{
  170. "model": "trt_engines_bfloat16",
  171. "messages":[{"role": "user", "content": "Where is New York?"},
  172. {"role": "assistant", "content": "<|s_1708|><|s_2050|><|s_2159|>"}],
  173. "max_tokens": 512,
  174. "temperature": 0.8,
  175. "top_p": 0.95,
  176. "top_k": 50,
  177. "stop": ["<|eos1|>"],
  178. "repetition_penalty": 1.2,
  179. "stream": false
  180. }'
  181. fi