run.sh 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/bin/bash
  2. # Copyright (c) 2025 NVIDIA (authors: Yuekai Zhang)
  3. export CUDA_VISIBLE_DEVICES=0
  4. cosyvoice_path=/workspace/CosyVoice
  5. export PYTHONPATH=${cosyvoice_path}:$PYTHONPATH
  6. export PYTHONPATH=${cosyvoice_path}/third_party/Matcha-TTS:$PYTHONPATH
  7. stage=$1
  8. stop_stage=$2
  9. huggingface_model_local_dir=./cosyvoice2_llm
  10. model_scope_model_local_dir=./CosyVoice2-0.5B
  11. trt_dtype=bfloat16
  12. trt_weights_dir=./trt_weights_${trt_dtype}
  13. trt_engines_dir=./trt_engines_${trt_dtype}
  14. model_repo=./model_repo_cosyvoice2
  15. if [ $stage -le -1 ] && [ $stop_stage -ge -1 ]; then
  16. echo "Cloning CosyVoice"
  17. git clone --recursive https://github.com/FunAudioLLM/CosyVoice.git $cosyvoice_path
  18. cd $cosyvoice_path
  19. git submodule update --init --recursive
  20. cd runtime/triton_trtllm
  21. fi
  22. if [ $stage -le 0 ] && [ $stop_stage -ge 0 ]; then
  23. echo "Downloading CosyVoice2-0.5B"
  24. huggingface-cli download --local-dir $huggingface_model_local_dir yuekai/cosyvoice2_llm
  25. modelscope download --model iic/CosyVoice2-0.5B --local_dir $model_scope_model_local_dir
  26. fi
  27. if [ $stage -le 1 ] && [ $stop_stage -ge 1 ]; then
  28. echo "Converting checkpoint to TensorRT weights"
  29. python3 scripts/convert_checkpoint.py --model_dir $huggingface_model_local_dir \
  30. --output_dir $trt_weights_dir \
  31. --dtype $trt_dtype || exit 1
  32. echo "Building TensorRT engines"
  33. trtllm-build --checkpoint_dir $trt_weights_dir \
  34. --output_dir $trt_engines_dir \
  35. --max_batch_size 16 \
  36. --max_num_tokens 32768 \
  37. --gemm_plugin $trt_dtype || exit 1
  38. echo "Testing TensorRT engines"
  39. python3 ./scripts/test_llm.py --input_text "你好,请问你叫什么?" \
  40. --tokenizer_dir $huggingface_model_local_dir \
  41. --top_k 50 --top_p 0.95 --temperature 0.8 \
  42. --engine_dir=$trt_engines_dir || exit 1
  43. fi
  44. if [ $stage -le 2 ] && [ $stop_stage -ge 2 ]; then
  45. echo "Creating model repository"
  46. rm -rf $model_repo
  47. mkdir -p $model_repo
  48. cosyvoice2_dir="cosyvoice2"
  49. cp -r ./model_repo/${cosyvoice2_dir} $model_repo
  50. cp -r ./model_repo/audio_tokenizer $model_repo
  51. cp -r ./model_repo/tensorrt_llm $model_repo
  52. cp -r ./model_repo/token2wav $model_repo
  53. cp -r ./model_repo/speaker_embedding $model_repo
  54. ENGINE_PATH=$trt_engines_dir
  55. MAX_QUEUE_DELAY_MICROSECONDS=0
  56. MODEL_DIR=$model_scope_model_local_dir
  57. LLM_TOKENIZER_DIR=$huggingface_model_local_dir
  58. BLS_INSTANCE_NUM=4
  59. TRITON_MAX_BATCH_SIZE=16
  60. DECOUPLED_MODE=True # True for streaming, False for offline
  61. python3 scripts/fill_template.py -i ${model_repo}/token2wav/config.pbtxt model_dir:${MODEL_DIR},triton_max_batch_size:${TRITON_MAX_BATCH_SIZE},max_queue_delay_microseconds:${MAX_QUEUE_DELAY_MICROSECONDS}
  62. 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}
  63. 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}
  64. 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}
  65. 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
  66. fi
  67. if [ $stage -le 3 ] && [ $stop_stage -ge 3 ]; then
  68. echo "Starting Triton server"
  69. tritonserver --model-repository $model_repo
  70. fi
  71. if [ $stage -le 4 ] && [ $stop_stage -ge 4 ]; then
  72. echo "Single request test http, only work for offline TTS mode"
  73. python3 client_http.py \
  74. --reference-audio ./assets/prompt_audio.wav \
  75. --reference-text "吃燕窝就选燕之屋,本节目由26年专注高品质燕窝的燕之屋冠名播出。豆奶牛奶换着喝,营养更均衡,本节目由豆本豆豆奶特约播出。" \
  76. --target-text "身临其境,换新体验。塑造开源语音合成新范式,让智能语音更自然。" \
  77. --model-name cosyvoice2
  78. fi
  79. if [ $stage -le 5 ] && [ $stop_stage -ge 5 ]; then
  80. echo "Running benchmark client grpc"
  81. num_task=1
  82. mode=streaming
  83. BLS_INSTANCE_NUM=4
  84. python3 client_grpc.py \
  85. --server-addr localhost \
  86. --model-name cosyvoice2 \
  87. --num-tasks $num_task \
  88. --mode $mode \
  89. --huggingface-dataset yuekai/seed_tts_cosy2 \
  90. --log-dir ./log_concurrent_tasks_${num_task}_${mode}_bls_${BLS_INSTANCE_NUM}
  91. fi