Streaming: LM and flow decoder on separate processes are ~1.4x faster
In streaming mode the LM spends its time waiting for the Python interpreter rather than for the GPU.
CosyVoice2Model.tts runs llm_job on a background thread while the main thread calls token2wav. Both are Python threads in one process, and Python only lets one thread run Python code at a time, so the two stages take turns instead of overlapping.
I wrapped model.llm.inference and model.token2wav to time them separately. A10G, fp16 off, load_jit=False, load_trt=False, cross-lingual zero shot, warmed the pipeline first, about 10.4s of output speech:
| Configuration | ms/token | Total Time | First Audio |
|---|---|---|---|
| Non-streaming | 16.3 | 5346 ms | 5329 ms |
| Non-streaming, decoder on 2nd GPU | 15.6 | 4949 ms | 4933 ms |
| Streaming | 22.4 | 7110 ms | 1714 ms |
| Streaming, decoder on 2nd GPU | 30.2 | 8796 ms | 2299 ms |
The LM runs at 16.3 ms per token with nothing else in the process and 22.4 when streaming.
Row four is the one that identifies the cause. I gave the LM and the decoder a separate GPU each expecting it to help and it got worse, 22.4 to 30.2. If they were competing for the GPU, a second GPU would have fixed it. They're competing for the interpreter.
An opt-in streaming path that runs the LM and the flow decoder in two processes instead of two threads. Process A runs the LM and sends token chunks over; process B runs flow and hift and sends audio back. Separate interpreters, so there's no lock to share.
I built this to check the diagnosis. The chunk schedule is copied exactly so both sides do the same decoder work:
| Configuration | First Audio | Total Time | ms/token |
|---|---|---|---|
| As shipped (current CosyVoice2 streaming) | 1701 ms | 7270 ms | 23.6 |
| Two processes, one GPU | 1292 ms | 6058 ms | 19.8 |
| Two processes, two GPUs | 1173 ms | 5093 ms | 16.1 |
It holds on longer text. On a paragraph of roughly 30s, two processes on two GPUs came out 1.36x faster to first audio and 1.35x on the total.
Moving the decoder to a second GPU inside the current threaded design. That's row four above, and it made things worse.
Since, I have already tried out and measured the improved performance in TTFA and ms/token with two processes config, I would like to send a PR with a new opt-in path for this (and not touching current streaming path).
Environment: CosyVoice2-0.5B, A10G on Modal, Python 3.10, torch 2.3.1 (repo's requirement.txt), commit 074ca6dc
Source: QwenAudio/CosyVoice