ReDimNet2-ONNX
ONNX graphs of ReDimNet2, a family of speaker-verification models, for ONNX Runtime on CPU or GPU without PyTorch. Each graph takes a 16 kHz waveform and returns a 192-dimensional speaker embedding, with the mel-spectrogram frontend inside the graph. Two clips are compared by the cosine similarity of their embeddings.
This is an unofficial conversion of all 20 checkpoints the authors released (v1.0.0), each in two precisions:
fp32/<checkpoint>.onnxreproduces the original PyTorch model to float32 rounding (relative difference ~1e-6), on CPU or GPU.fp16/<checkpoint>.onnxruns the network in FP16 for GPUs. It stays within a cosine distance of 6e-6 of the original on clips of 1 s or longer, and is 21-42% smaller.
The FP32 graphs run 1.8-2.6x faster than PyTorch on CPU and about as fast as PyTorch on a GPU. The FP16 graphs are up to 1.5x faster than FP32 on single clips, and about 2x faster on long audio or batches (see Benchmark).
Files
| Checkpoint | Size | Training data | Stage | Params | fp32/ |
fp16/ |
|---|---|---|---|---|---|---|
b0-vox2-ptn |
B0 | VoxCeleb2 | ptn |
1.1M | 5.5 MiB | 4.3 MiB |
b0-vox2-lm |
B0 | VoxCeleb2 | lm |
1.1M | 5.5 MiB | 4.3 MiB |
b1-vox2-ptn |
B1 | VoxCeleb2 | ptn |
2.2M | 9.4 MiB | 7.3 MiB |
b1-vox2-lm |
B1 | VoxCeleb2 | lm |
2.2M | 9.4 MiB | 7.3 MiB |
b2-vox2-ptn |
B2 | VoxCeleb2 | ptn |
3.7M | 15.2 MiB | 10.7 MiB |
b2-vox2-lm |
B2 | VoxCeleb2 | lm |
3.7M | 15.2 MiB | 10.7 MiB |
b3-vox2-ptn |
B3 | VoxCeleb2 | ptn |
4.2M | 17.2 MiB | 12.2 MiB |
b3-vox2-lm |
B3 | VoxCeleb2 | lm |
4.2M | 17.2 MiB | 12.2 MiB |
b4-vox2-ptn |
B4 | VoxCeleb2 | ptn |
6.7M | 26.6 MiB | 17.9 MiB |
b4-vox2-lm |
B4 | VoxCeleb2 | lm |
6.7M | 26.6 MiB | 17.9 MiB |
b5-vox2-ptn |
B5 | VoxCeleb2 | ptn |
9.0M | 35.4 MiB | 22.3 MiB |
b5-vox2-lm |
B5 | VoxCeleb2 | lm |
9.0M | 35.4 MiB | 22.3 MiB |
b6-vox2-ptn |
B6 | VoxCeleb2 | ptn |
12.4M | 48.5 MiB | 28.3 MiB |
b6-vox2-lm |
B6 | VoxCeleb2 | lm |
12.4M | 48.5 MiB | 28.3 MiB |
b6-vb2+vox2_v0-ptn |
B6 | VoxBlink2 + VoxCeleb2 | ptn |
12.5M | 48.8 MiB | 28.5 MiB |
b6-vb2+vox2_v0-lm |
B6 | VoxBlink2 + VoxCeleb2 | lm |
12.5M | 48.8 MiB | 28.5 MiB |
b3-vb2+vox2+cnc2_v0-ptn |
B3 | VoxBlink2 + VoxCeleb2 + CN-Celeb2 | ptn |
4.5M | 18.4 MiB | 13.3 MiB |
b3-vb2+vox2+cnc2_v0-lm |
B3 | VoxBlink2 + VoxCeleb2 + CN-Celeb2 | lm |
4.5M | 18.4 MiB | 13.3 MiB |
b6-vb2+vox2+cnc2_v0-ptn |
B6 | VoxBlink2 + VoxCeleb2 + CN-Celeb2 | ptn |
12.5M | 48.8 MiB | 28.5 MiB |
b6-vb2+vox2+cnc2_v0-lm |
B6 | VoxBlink2 + VoxCeleb2 + CN-Celeb2 | lm |
12.5M | 48.8 MiB | 28.5 MiB |
ptn= pretraining,lm= large-margin fine-tuning. The authors recommendlm.example.py: a runnable demo that scores two audio files.
All graphs use ONNX opset 20 (ONNX Runtime 1.17 or newer; tested with 1.31.1). The batch and length axes are dynamic. Each graph's ONNX metadata records its source, revision, checkpoint, license and the changes made in conversion.
Usage
pip install onnxruntime numpy soundfile soxr huggingface_hub
import numpy as np
import onnxruntime as ort
import soundfile as sf
import soxr
from huggingface_hub import hf_hub_download
path = hf_hub_download("lzrhahahahaha/ReDimNet2-ONNX", "fp32/b6-vox2-lm.onnx")
options = ort.SessionOptions()
options.add_session_config_entry("session.set_denormal_as_zero", "1") # see "Runtime settings"
model = ort.InferenceSession(path, options, providers=["CPUExecutionProvider"])
def embed(file):
audio, rate = sf.read(file, dtype="float32", always_2d=True)
audio = soxr.resample(audio.mean(axis=1), rate, 16000) # mono, 16 kHz
return model.run(None, {"input_values": audio[None]})[0][0] # float32 [192]
a, b = embed("first.wav"), embed("second.wav")
score = a @ b / (np.linalg.norm(a) * np.linalg.norm(b)) # higher = more likely the same speaker
Choose a decision threshold on your own data; it depends on the checkpoint and the recording
conditions. example.py does the same from the command line:
python example.py first.wav second.wav # fp32/b6-vox2-lm.onnx on CPU
python example.py first.wav second.wav --cuda --fp16 # fp16/b6-vox2-lm.onnx on a GPU
python example.py first.wav second.wav --model b3-vb2+vox2+cnc2_v0-lm
Input and output
| Input | Output |
|---|---|
input_values float32 [batch, num_samples], 16 kHz mono, num_samples >= 720 |
embedding float32 [batch, 192] |
- Feed raw samples at any scale. The graph normalises each clip itself.
- 720 samples (45 ms) is the model's minimum. Clips of a few seconds give much more reliable embeddings.
- Clips in one batch must have the same length, because the model has no padding mask. Each row is processed independently.
- The FP16 and FP32 graphs have the same inputs and outputs, so they are interchangeable.
GPU
pip install "onnxruntime-gpu[cuda,cudnn]" numpy soundfile soxr huggingface_hub
ort.preload_dlls() # load the CUDA and cuDNN libraries installed by pip
providers = [("CUDAExecutionProvider", {"use_tf32": "0"}), "CPUExecutionProvider"]
model = ort.InferenceSession(hf_hub_download("lzrhahahahaha/ReDimNet2-ONNX", "fp16/b6-vox2-lm.onnx"),
providers=providers)
Runtime settings
- CPU: flush denormals. Add
session.set_denormal_as_zero = 1as in the example above. The fourcnc2checkpoints produce denormal numbers (below 1.2e-38) during inference, which x86 CPUs process very slowly. With the option, thecnc2B3 takes 106 instead of 161 ms per clip and thecnc2B6 366 instead of 708 ms (FP32 graphs, 7-9 s clips), with bit-identical embeddings. Other checkpoints run at the same speed either way. - CUDA: TF32 or not. ONNX Runtime's CUDA provider uses TF32 for FP32 convolutions and matrix
products by default. That saves up to 19% of the time, but moves embeddings by about 1e-3
relative and scores by up to 8e-5.
use_tf32=0keeps full FP32 accuracy. - FP16 on CUDA: leave
prefer_nhwcoff. With it, B5 takes 20.3 instead of 18.8 ms per clip and 400 instead of 128 ms for a 60 s clip. - Long audio on CUDA. ONNX Runtime's memory arena grows to fit the largest input it has seen
and keeps that memory. One B5 session holds about 0.5 GB after a 7 s clip and about
3.0 GB after a 60 s clip. Set
gpu_mem_limit, or split long recordings.
Benchmark
Measured on an Intel Core i5-14400 and an NVIDIA GeForce RTX 4070 SUPER, with onnxruntime-gpu 1.31.1 and, for the reference, torch 2.15.0+cu134. The test set is seven speech clips of one speaker, 7-9 s each.
The reference is the original PyTorch model (the authors' code) in FP32 on CPU, with denormal weights set to zero as in the graphs. Every row is compared with it on every clip of every checkpoint:
- Cosine distance (1 - cosine similarity) and relative L2 (|a - r| / |r|) between the embeddings of each clip.
- Score difference: the largest change in the cosine score of any pair of clips.
FP32 graphs must stay within 1e-6, 1e-4 and 1e-5, and FP16 graphs within 1e-4, 1e-2 and 1e-3.
All 20 checkpoints pass on CPU and CUDA. On CUDA, FP32 means TF32 off (use_tf32=0) unless a
row says TF32.
Accuracy
Worst case over all 20 checkpoints and 7 clips:
| Implementation | Device | Cosine distance | Relative L2 | Score difference |
|---|---|---|---|---|
| ONNX FP32 (this repo) | CPU | 1e-12 | 1e-06 | 1e-07 |
| ONNX FP32 (this repo) | CUDA | 3e-12 | 2e-06 | 1e-07 |
| ONNX FP32 (this repo), TF32 | CUDA | 5e-07 | 1e-03 | 8e-05 |
| ONNX FP16 (this repo) | CUDA | 5e-06 | 3e-03 | 2e-04 |
| PyTorch FP32 | CUDA | 1e-12 | 1e-06 | 2e-07 |
Between the seven clips, all of one speaker, the reference scores range from 0.895 to 0.974 across checkpoints. The FP16 graphs change any of these scores by at most 2e-4.
Speed
Milliseconds per clip, batch 1: the median over the seven clips of each clip's median over 20
runs, after 3 warmups. Times include host-device copies and exclude model loading. The CPU
rows use ONNX Runtime's default settings; see "Runtime settings" for the cnc2 checkpoints.
| Checkpoint | PyTorch FP32, CPU | ONNX FP32, CPU | PyTorch FP32, CUDA | ONNX FP32, CUDA | ONNX FP32, TF32, CUDA | ONNX FP16, CUDA |
|---|---|---|---|---|---|---|
b0-vox2-ptn |
66.3 | 27.1 | 8.0 | 7.5 | 6.8 | 6.3 |
b0-vox2-lm |
70.0 | 26.7 | 8.1 | 7.7 | 7.1 | 7.6 |
b1-vox2-ptn |
92.0 | 35.2 | 8.1 | 8.0 | 8.1 | 7.3 |
b1-vox2-lm |
87.0 | 34.9 | 8.3 | 8.9 | 8.2 | 7.5 |
b2-vox2-ptn |
87.6 | 46.7 | 8.4 | 7.1 | 6.7 | 10.9 |
b2-vox2-lm |
91.9 | 51.2 | 8.3 | 7.2 | 6.7 | 11.1 |
b3-vox2-ptn |
225.1 | 95.7 | 12.1 | 10.2 | 9.0 | 13.6 |
b3-vox2-lm |
207.2 | 95.6 | 11.9 | 10.2 | 9.1 | 13.5 |
b4-vox2-ptn |
275.5 | 150.0 | 15.3 | 13.5 | 11.6 | 15.0 |
b4-vox2-lm |
281.1 | 142.4 | 15.1 | 13.7 | 11.8 | 15.0 |
b5-vox2-ptn |
538.0 | 259.6 | 21.2 | 21.6 | 17.7 | 19.6 |
b5-vox2-lm |
541.3 | 270.2 | 21.3 | 21.8 | 17.8 | 19.6 |
b6-vox2-ptn |
708.1 | 359.9 | 31.8 | 31.4 | 26.7 | 21.0 |
b6-vox2-lm |
762.5 | 361.1 | 32.0 | 31.6 | 26.8 | 21.1 |
b6-vb2+vox2_v0-ptn |
769.1 | 363.8 | 32.3 | 32.0 | 27.4 | 21.7 |
b6-vb2+vox2_v0-lm |
798.2 | 363.8 | 32.2 | 31.6 | 27.4 | 21.3 |
b3-vb2+vox2+cnc2_v0-ptn |
322.8 | 164.4 | 12.4 | 10.2 | 9.2 | 14.0 |
b3-vb2+vox2+cnc2_v0-lm |
322.3 | 159.1 | 12.4 | 10.2 | 9.3 | 13.6 |
b6-vb2+vox2+cnc2_v0-ptn |
1158.4 | 632.2 | 32.4 | 32.0 | 27.5 | 21.5 |
b6-vb2+vox2+cnc2_v0-lm |
1168.0 | 629.1 | 32.3 | 31.9 | 27.5 | 21.5 |
- On CPU, ONNX Runtime is 1.8-2.6x faster than PyTorch.
- On CUDA, the FP32 graphs run at PyTorch's speed: up to 18% faster (B2-B4) and at most 8% slower.
- FP16 pays off when each call has enough work. For B6 a clip runs 1.5x faster than in FP32, and for B5 1.1x. For B2-B4, single clips run slower in FP16 than in FP32.
With longer inputs FP16 pays off clearly, measured on B5 and B6:
| Graph | Device | One clip (7-9 s) | One 60 s clip | Batch of 8 x 7 s |
|---|---|---|---|---|
fp32/b5-vox2-lm.onnx |
CUDA | 21.5 ms | 243.1 ms | 187.3 ms |
fp16/b5-vox2-lm.onnx |
CUDA | 18.8 ms | 127.7 ms | 112.4 ms |
fp32/b6-vox2-lm.onnx |
CUDA | 31.5 ms | 321.1 ms | 239.4 ms |
fp16/b6-vox2-lm.onnx |
CUDA | 20.4 ms | 160.7 ms | 141.4 ms |
On a 60 s clip FP16 is 1.9-2.0x faster than FP32, and on a batch of eight 1.7x.
Changes from the original model
The original PyTorch checkpoints and the authors' code (revision c5bbe0b) were exported with
torch.export. The graphs compute the same function as the original, with these rewrites:
- The mel frontend is included as is. It already computes the DFT as a strided convolution.
- The cut of the spectrogram to a multiple of 4 frames runs as a gather, so the input length stays dynamic.
- 1x1 convolutions run as matrix products. For some of these shapes, ONNX Runtime's CUDA
provider picks a cuDNN engine that rounds like TF32 even with
use_tf32=0. - Denormal weights (below 1.2e-38) are set to zero. The two
cnc2B6 checkpoints hold about 200 000 of them. - The softmax-normalised weights that mix the stages are precomputed.
- GELU runs as the
Geluoperator, channels-first layer normalisation asLayerNormalization, and attention on[batch, heads, time, dim]tensors. - Reshape targets are constants, so no shape arithmetic runs at inference.
In the FP16 graphs, everything between the mel spectrogram and the pooling layer runs in FP16: convolutions, matrix products, normalisation and attention. These stay in FP32:
- the mel frontend, whose 1e-8 floor and 1e8 clip are outside FP16's range;
- the attentive statistics pooling, whose variance is a difference of large numbers;
- the final BatchNorm and linear layer;
- the input and the output.