BERT GPU Retention & Batch Inference
On the GPU path, two key optimization points stand out.
- Removing the unnecessary CPU transfer from the original SBV2 to keep BERT output as a GPU tensor
- Batching multiple sentences to reduce the number of BERT and Synthesizer calls, which reduces kernel launch overhead (the fixed cost incurred each time an operation is dispatched to the GPU)
Why It Matters
The original SBV2 splits text only at line breaks for synthesis (line_split=True is the default), so text without line breaks is synthesized in one pass.
In this case BERT is also called only once, so batching was not needed.
HayaKoe introduced punctuation-based sentence splitting for prosody stability, which created the new problem of BERT and the Synthesizer being called once per sentence.
Unnecessary CPU Transfer of BERT Output
The original SBV2's BERT feature extraction code contains this:
# Original SBV2 (style_bert_vits2/nlp/japanese/bert_feature.py)
res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu()After running BERT forward on GPU, it calls .cpu() on the output tensor, transferring it to CPU every time.
This output is then passed to the Synthesizer, which runs on GPU, requiring another transfer back to GPU.
The result is a GPU -> CPU -> GPU round-trip per sentence, and this unnecessary round-trip itself becomes a bottleneck.
Per-sentence Individual Calls
When calling BERT and the Synthesizer separately for each sentence after splitting, GPU kernel launches repeat once per sentence.
A kernel launch is the fixed cost incurred each time an operation is dispatched to the GPU.
For short sentences, the overhead proportion exceeds the actual computation time, accumulating inefficiency proportional to sentence count.
Implementation
Removing .cpu() — Keeping GPU Tensors
The original .cpu() call was removed so BERT output passes to the Synthesizer as a GPU tensor directly.
# Original SBV2
res = torch.cat(res["hidden_states"][-3:-2], -1)[0].cpu() # GPU -> CPU
# HayaKoe
res = model(**inputs).last_hidden_state[0].float() # stays on GPUThe BERT model itself is loaded to GPU at prepare() time and stays there through inference.
The original loads all 24 layers plus the MLM head, then uses only the 22nd layer's output (hidden_states[-3]). HayaKoe loads only up to layer 22 from the start and gets the same value via last_hidden_state, saving VRAM (about 100 MB) and compute for the 2 unused layers and the MLM head.
The BERT model is managed as a global singleton, so loading multiple speakers still loads BERT only once, shared by all.
Multi-sentence BERT Batching
The BERT (DeBERTa) used by HayaKoe is a HuggingFace Transformer model that natively supports batch input.
Leveraging this, instead of calling BERT individually for each sentence in multi-sentence synthesis, all sentences are grouped into a single batch for one call.
Multiple sentences are fed to the tokenizer at once to create a padded batch input, and BERT is called only once.
The same batch logic is implemented on the ONNX path as well.
Multi-sentence Synthesizer Batching
On the GPU path, the Synthesizer also synthesizes up to 4 sentences at once, padded into a single batch.
- Only the valid frames for each sentence are sliced from the batch output, so padding never leaks into the audio.
- Pauses between sentences and phoneme timings are computed the same way as in per-sentence synthesis.
- Batch VRAM is proportional to sentence count × longest sentence length, so a cap on sentence count keeps VRAM from growing unbounded even for long texts.
- Padded matrix multiplications round slightly differently under TF32 than per-sentence synthesis, so even with the same input, some sentence lengths may differ by ±1 frame (about 12 ms).
The CPU (ONNX) path keeps per-sentence synthesis for the same reason as the BERT experiment below.
Improvement Results
GPU Batch Inference Speed
Sequential vs batched comparison on the same hardware (5-run average).
| Sentences | Sequential | Batched | Speedup |
|---|---|---|---|
| 2 | 0.447 s | 0.364 s | 1.23x |
| 4 | 0.812 s | 0.566 s | 1.43x |
| 8 | 1.598 s | 1.121 s | 1.43x |
| 16 | 2.972 s | 2.264 s | 1.31x |
With kernel launch overhead consolidated into a single call, speedups of +23% to +43% are observed.
GPU Memory
We verified that batching does not consume additional memory.
| Sentences | Sequential peak | Batched peak | Difference |
|---|---|---|---|
| 2 | 1,662.2 MB | 1,661.9 MB | −0.3 MB |
| 4 | 1,661.8 MB | 1,662.2 MB | +0.4 MB |
| 8 | 1,697.7 MB | 1,699.0 MB | +1.3 MB |
| 16 | 1,934.3 MB | 1,934.3 MB | 0 MB |
The difference between sequential and batched is within 1.3 MB, essentially identical.
GPU Synthesizer Batch Speed
Synthesizer batching was compared before and after using docs/benchmarks/compare/run.py (RTX 3080, median of 5 runs).
| Text | Sentences | Per-sentence synthesis | Batched synthesis | Speedup |
|---|---|---|---|---|
| Medium (~8 s) | 2 | 0.443 s | 0.292 s | 1.52x |
| Long (~15 s) | 3 | 0.523 s | 0.359 s | 1.46x |
| Very long (~43 s) | 9 | 1.421 s | 0.835 s | 1.70x |
The batch size was set to 4 considering both speed and VRAM (based on the 9-sentence text).
| Batch size | Synthesis time | VRAM peak |
|---|---|---|
| 1 (per-sentence) | 1,381 ms | 1,679 MB |
| 2 | 952 ms | 1,810 MB |
| 4 | 746 ms | 2,163 MB |
| 8 | 682 ms | 2,823 MB |
Going from 4 to 8 is about 9% faster but uses 660 MB more VRAM.
No Effect on CPU
Repeating the BERT batching experiment on CPU (ONNX) shows virtually no batching benefit.
| Sentences | Sequential | Batched | Speed Difference |
|---|---|---|---|
| 2 | 2.566 s | 2.564 s | 1.00x |
| 4 | 5.464 s | 4.855 s | 1.13x |
| 8 | 10.647 s | 11.783 s | 0.90x |
| 16 | 24.559 s | 24.195 s | 1.01x |
ONNX Runtime's graph optimization is already strong enough that Python-level dispatch overhead is not the bottleneck, and padding overhead in batching offsets the gains.
Batching is maintained on GPU, and since neither gains nor losses are significant on CPU, the same path is kept for code consistency across backends.
