CosyVoice
repository·main·Indexed 12 days ago
https://github.com/funaudiollm/cosyvoiceAn advanced text-to-speech (TTS) system based on large language models supporting zero-shot multilingual speech synthesis, voice cloning, and instruction-based control. It includes support for CosyVoice2 and CosyVoice3, with capabilities for GRPO reinforcement learning fine-tuning, vLLM integration, and acceleration via NVIDIA Triton and TensorRT-LLM.
What's inside CosyVoice
- This repository provides acceleration solutions for CosyVoice using NVIDIA Triton Inference Server for serving and TensorRT-LLM for LLM acceleration. It offers three distinct deployment paths depending on the model version and Token2Wav architecture required.
Optimize Performance with Disaggregated Server Setup
mainTo prevent the LLM and Token2Wav components from competing for GPU resources, use a Disaggregated Server configuration. In this setup:
- The LLM is deployed on a dedicated GPU (e.g., GPU 0) to leverage in-flight batching.
- The Token2Wav module is deployed on separate, dedicated GPUs (e.g., GPUs 1-3).
This configuration can be launched using Stage 7 of the orchestration script, and benchmarked using Stage 8.
Choose a CosyVoice acceleration solution
mainSelect one of the following three solutions based on your model requirements:
- CosyVoice3: Designed for
Fun-CosyVoice3-0.5B-2512. The pipeline includesaudio_tokenizer,speaker_embedding,token2wav, andvocodermodules managed by Triton, with the LLM served viatrtllm-serve. - CosyVoice2 + UNet Token2Wav: The baseline acceleration solution for CosyVoice2, utilizing the original UNet-based flow-matching Token2Wav module.
- CosyVoice2 + DiT Token2Wav: Replaces the UNet Token2Wav with a DiT-based module (from Step-Audio2). This solution supports disaggregated deployment, allowing the LLM and Token2Wav to run on separate GPUs to optimize resource utilization during high concurrency.
- CosyVoice3: Designed for
Configure Streaming vs. Offline TTS Modes
mainCosyVoice supports two primary modes of operation. The configuration is handled during Stage 2 of the
run.shworkflow via theDecoupledparameter:- Streaming TTS: Set
Decoupled=True. This is optimized for low-latency chunked delivery. - Offline TTS: Set
Decoupled=False. This is used for full-sentence generation.
Important: If you switch between these modes, you must rerun
run.shstarting from Stage 2 to reconfigure the Triton model repository.- Streaming TTS: Set
Test and Benchmark the Triton Server
mainOnce the server is running (Stage 3), you can verify functionality and performance using the following methods:
1. Single-Utterance HTTP Test (Offline Mode only):
bash run.sh 4 42. gRPC Client-Server Benchmark: Run Stage 5 and specify
streamingorofflineas the third argument.bash run.sh 5 5 streaming bash run.sh 5 5 offlineTo customize the benchmark (e.g., number of tasks or dataset), use the
client_grpc.pyscript directly:python3 client_grpc.py --num-tasks 2 --huggingface-dataset yuekai/seed_tts_cosy2 --split-name test_zh --mode [streaming|offline]3. Offline Inference Benchmark: Requires
FlashCosyVoicefor batching. Run Stage 6:bash run.sh 6 6Note: You can switch to the HuggingFace backend by setting
backend=hf.# Benchmark streaming mode bash run.sh 5 5 streaming # Benchmark offline mode bash run.sh 5 5 offlineOrchestrate CosyVoice3 workflow with run_cosyvoice3.sh
mainThe
run_cosyvoice3.shscript manages the lifecycle of the CosyVoice3 deployment through numbered stages. You can execute specific ranges of stages using the syntax:bash run_cosyvoice3.sh <start_stage> <stop_stage>.# Example: Run stages 0 through 3 to prepare models and launch the server bash run_cosyvoice3.sh 0 3Set up the CosyVoice2 GRPO Reinforcement Learning environment
mainTo fine-tune CosyVoice2 using GRPO, it is recommended to use the pre-built Docker image containing the necessary
veRLandvLLMdependencies. If Docker is unavailable, you can install dependencies locally by following the instructions inrun.shstage-2.docker pull soar97/verl:app-verl0.4-vllm0.8.5-mcore0.12.2-te2.2Evaluate and Export the trained CosyVoice2 model
mainAfter training, follow these steps to evaluate and use your model:
Merge and Export Checkpoint (Stage 3): Collect sharded FSDP weights and merge them into a Hugging Face-style checkpoint.
bash run.sh 3 3 # Merges weights into $llm_path/merged_hf_modelEvaluate (Stage 4): Run distributed inference via
infer_dataset.pyto compute Word Error Rate (WER). You can evaluate on the CosyVoice3 zero-shot Chinese set or the Seed-TTS test set (by settingdataset=test_zh).bash run.sh 4 4Convert for CosyVoice Repository (Stage 5): Convert the Hugging Face checkpoint back into the format expected by the official CosyVoice repository.
bash run.sh 5 5Note: A slight accuracy drop may occur when using the converted model compared to the direct Hugging Face format.
bash run.sh 3 3 bash run.sh 4 4 bash run.sh 5 5Quick Start CosyVoice acceleration via Docker Compose
mainEach acceleration solution can be launched using a specific Docker Compose configuration file. Run the command corresponding to your chosen solution to start the environment.
# CosyVoice3 docker compose -f docker-compose.cosyvoice3.yml up # CosyVoice2 + UNet Token2Wav docker compose -f docker-compose.cosyvoice2.unet.yml up # CosyVoice2 + DiT Token2Wav docker compose -f docker-compose.cosyvoice2.dit.yml upTrain CosyVoice2 using GRPO
mainRun the GRPO training process using stage
2of therun.shscript. This invokesverl.trainer.main_ppowith specific configurations.Key Arguments:
algorithm.adv_estimator=grpo: Switches the algorithm from PPO to GRPO.data.train_files: Path to the training parquet file.data.val_files: Path to the validation parquet file.custom_reward_function.path: Path to the reward script (e.g.,reward_tts.py).
Note: The
lm_headbias is disabled during training to ensure compatibility with VLLM and Transformers' Qwen models.bash run.sh 2 2Configure the Reward Function and ASR Server
mainThe GRPO training requires a reward server to calculate scores based on speech synthesis quality. The server performs the following:
- Converts generated speech tokens to a 16 kHz waveform using the CosyVoice2 pretrained U-Net.
- Transcribes the waveform using SenseVoice ASR.
- Calculates the pinyin-level error rate against the ground-truth text (score 0-1).
Start the server in a dedicated terminal or on a separate GPU using stage
1. The Triton server will listen on ports8000,8001, and8002. The custom reward implementation inreward_tts.pycommunicates with this server.bash run.sh 1 1Accelerate CosyVoice2 with Nvidia TensorRT-LLM
mainUsing TensorRT-LLM can provide up to 4x acceleration for CosyVoice2 LLM inference compared to the HuggingFace Transformers implementation. This is managed via Docker Compose in the
runtime/triton_trtllmdirectory.cd runtime/triton_trtllm docker compose up -d