Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Git LFS file not shown
29 changes: 29 additions & 0 deletions tests/test_vad.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
from concurrent.futures import Future, ThreadPoolExecutor
from typing import Any

import numpy as np
import pytest

from livekit.agents import vad
from livekit.agents.inference import VAD as InferenceVAD
from livekit.local_inference import VAD as NativeVAD, VAD_WINDOW_SAMPLES
from livekit.plugins.silero import onnx_model

from . import utils

Expand Down Expand Up @@ -174,3 +177,29 @@ async def test_file_vad(sample_rate):

assert start_of_speech_i > 0, "no start of speech detected"
assert start_of_speech_i == end_of_speech_i, "start and end of speech mismatch"


async def test_plugin_checkpoint_matches_inference_vad() -> None:
"""The silero plugin and inference.VAD must run the same underlying checkpoint.

They ship the model separately -- an onnx file in the plugin, baked-in weights in
livekit-local-inference -- so nothing but this test keeps the two from drifting apart.
"""
frames, *_ = await utils.make_test_speech(sample_rate=16000)
pcm = frames[0].data

plugin_model = onnx_model.OnnxModel(
onnx_session=onnx_model.new_inference_session(force_cpu=True), sample_rate=16000
)
native_model = NativeVAD()

windows = 0
for i in range(0, len(pcm) - VAD_WINDOW_SAMPLES + 1, VAD_WINDOW_SAMPLES):
window = np.ascontiguousarray(pcm[i : i + VAD_WINDOW_SAMPLES], dtype=np.int16)
plugin_p = plugin_model(np.divide(window, np.iinfo(np.int16).max, dtype=np.float32))
assert plugin_p == pytest.approx(native_model.predict(window), abs=1e-3), (
f"checkpoints diverge at window {i // VAD_WINDOW_SAMPLES}"
)
windows += 1

assert windows > 100, "test audio too short to be meaningful"