Skip to content

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:

python
# 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.

python
# 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 GPU

The 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).

SentencesSequentialBatchedSpeedup
20.447 s0.364 s1.23x
40.812 s0.566 s1.43x
81.598 s1.121 s1.43x
162.972 s2.264 s1.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.

SentencesSequential peakBatched peakDifference
21,662.2 MB1,661.9 MB−0.3 MB
41,661.8 MB1,662.2 MB+0.4 MB
81,697.7 MB1,699.0 MB+1.3 MB
161,934.3 MB1,934.3 MB0 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).

TextSentencesPer-sentence synthesisBatched synthesisSpeedup
Medium (~8 s)20.443 s0.292 s1.52x
Long (~15 s)30.523 s0.359 s1.46x
Very long (~43 s)91.421 s0.835 s1.70x

The batch size was set to 4 considering both speed and VRAM (based on the 9-sentence text).

Batch sizeSynthesis timeVRAM peak
1 (per-sentence)1,381 ms1,679 MB
2952 ms1,810 MB
4746 ms2,163 MB
8682 ms2,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.

SentencesSequentialBatchedSpeed Difference
22.566 s2.564 s1.00x
45.464 s4.855 s1.13x
810.647 s11.783 s0.90x
1624.559 s24.195 s1.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.