Skip to content

ONNX Optimization / Quantization ​

The original Style-Bert-VITS2 was PyTorch-based, so CPU-only inference was slow and used a lot of memory.

HayaKoe exports BERT as Q8 quantized ONNX and the Synthesizer as FP32 ONNX, running them on ONNX Runtime to achieve 1.3x to 3.2x CPU inference speedup depending on text length.

Simultaneously, peak RAM for single-speaker inference was reduced from 7,737 MB to 1,385 MB (−82%).

Thanks to the same runtime path, the same code works on aarch64 (Raspberry Pi, etc.) as well as x86_64.

The Problem ​

The original SBV2 (CPU, PyTorch FP32) had two shortcomings.

  • Speed — Inference time grows more steeply than audio length as text gets longer. For short text (1.5s audio), it runs at 1.54x speed, but for extra-long text (~43s audio), it drops to 32.5 seconds inference at 1.34x.
  • Memory — Peak memory of about 7.7 GB for single-speaker inference is a substantial burden.

Analysis ​

Looking at the model parameter distribution, approximately 84% is concentrated in BERT (DeBERTa-v2-Large-Japanese, ~329M), while the Synthesizer (VITS) is 63M, about 16%.

Since BERT dominates the model, we expected quantizing BERT would significantly reduce memory. We verified this by applying Q8 Dynamic Quantization (torch.quantization.quantize_dynamic) to BERT alone in PyTorch.

ConfigurationAvg Inference TimeRAM
PyTorch BERT FP324.796 s+1,698 MB
PyTorch BERT Q84.536 s+368 MB (−78%)

BERT quantization did not improve speed but confirmed it can significantly reduce memory usage.

We then proceeded to additionally switch to ONNX Runtime to also capture speed improvements.

ONNX Runtime automatically applies graph-level optimizations when loading a model.

  • Kernel fusion — Merges consecutive operations into one. For example, fusing Conv -> BatchNorm -> Activation into a single operation eliminates intermediate memory writes and reads, reducing memory access for faster execution.
  • Constant folding — Pre-computes operations that always produce the same value regardless of input at load time, using the pre-computed values during inference for speed.
  • Dead node elimination — Finds and removes unused, redundant, or no-op nodes.

In summary, it reconstructs mathematically equivalent operations optimized for inference, enabling faster execution.

The Synthesizer was excluded from quantization because its parameter count of 63M is small enough that quantization offers limited memory benefit, at FP16 the flow layer inside the SDP (rational_quadratic_spline) is numerically unstable, and at INT8 dynamic quantization barely applies because the architecture is Conv1d-centric. Instead, it was exported to ONNX only to capture graph optimization benefits.

BERT Optimization ​

To check whether quantization affects audio quality, we compared outputs from FP32, Q8, and Q4 BERT configurations using the same text and speaker (Synthesizer fixed at FP32 for all).

旅の途中で不思議な街に辿り着きました。少し寄り道していきましょう。

(I arrived at a mysterious town during my journey. Let us take a short detour.)

つくよみちゃんBERT dtype = FP32Original baseline
0:00 / 0:00

FP32 and Q8 were consistently indistinguishable by ear.

Q4 sounds similar to FP32 and Q8 throughout most of the audio, but a subtle difference is audible near the end.

ConfigurationBERT SizeRAM (1 speaker)
FP321,157 MB1,599 MB
Q8497 MB1,079 MB (−33%)
Q4394 MB958 MB (−40%)

We determined that the additional memory savings from Q4 did not outweigh the quality trade-off, so we chose Q8 as the default.

Synthesizer Optimization ​

Since BERT accounts for 84% of parameters, it seems like making BERT faster would speed up everything.

However, when measuring BERT and Synthesizer inference times separately, the CPU time is mostly consumed by the Synthesizer.

Actual measurements on PyTorch FP32 CPU (5-run average).

TextBERTSynthesizerBERT ShareSynth Share
short (1.7 s)0.489 s0.885 s36%64%
medium (5.3 s)0.602 s2.504 s19%81%
long (7.8 s)0.690 s3.714 s16%84%
xlong (30 s)1.074 s11.410 s9%91%

The Synthesizer's share grows with longer text because BERT is relatively insensitive to text length while the Synthesizer's time grows proportionally with the audio length to generate.

In practice, quantizing only BERT reduced total inference time by only about 5%.

In other words, improving speed requires optimizing the Synthesizer portion.

The Synthesizer used ONNX conversion only instead of quantization.

  • FP16: The flow layer inside the SDP (rational_quadratic_spline) causes floating-point assertion errors.
  • INT8: Dynamic quantization barely applies because the architecture is Conv1d-centric (even when exported as Q8, the file size is almost the same as FP32).
  • The parameter count of 63M is small enough that quantization offers limited memory benefit.

Instead, because it is converted to ONNX and run on ONNX Runtime, the same graph-level optimizations (kernel fusion, constant folding, dead node elimination) apply to the Synthesizer as well.

ONNX Runtime + CPUExecutionProvider ​

Both BERT quantization and Synthesizer graph optimization run on ONNX Runtime.

Additionally, intra-op parallelism distributes a single operation across multiple CPU cores, utilizing the full CPU even with just one request.

Improvement Results ​

CPU Performance Comparison (Speed Factor, Same Hardware) ​

Speed factor = audio length / inference time (higher is faster).

Configurationshort (1.5 s)medium (8 s)long (15 s)xlong (43 s)
SBV2 PyTorch FP321.54x2.36x1.87x1.34x
HayaKoe (Q8 BERT + FP32 ONNX)2.26x3.15x3.99x4.26x

Speed improvement over PyTorch FP32 is 1.3x to 3.2x depending on text length.

The measurement script is docs/benchmarks/compare/run.py, and the original was run from PyPI's style-bert-vits2 2.5.0 with the same speaker (jvnv-F1-jp) and the same texts (Ryzen 9 3950X · RTX 3080, median of 5 runs).

Memory (Single Speaker Inference Peak) ​

ConfigurationRAM
SBV2 PyTorch FP327,737 MB
HayaKoe (Q8 BERT + FP32 ONNX)1,385 MB (−82%)