"""The exported graph must reproduce SAVA's published reference outputs. Run ``export.py`` and ``quantize.py`` first; ``fixtures/`` holds the recording and expected logits from the SAVA repository (tests/fixtures). """ from pathlib import Path import numpy as np import pytest import soundfile as sf import sava_onnx BUILD = Path(__file__).parent / "build" FIXTURES = Path(__file__).parent / "fixtures" FINE_TUNED = Path(__file__).parent / "runs" / "phone-1" / "best.safetensors" def _logits(model: Path, line=None) -> tuple[np.ndarray, np.ndarray, np.ndarray]: if not model.exists(): pytest.skip(f"{model.name} has not been built") audio, _ = sf.read(FIXTURES / "speech.wav", dtype="float32") audio = audio if line is None else line(audio) session = sava_onnx.Session(str(model)) frames = [session.step(audio[i : i + 640]) for i in range(0, audio.size - 639, 640)] return tuple(np.array(column) for column in zip(*frames)) def test_full_precision_export_matches_the_reference_logits(): expected = np.load(FIXTURES / "speech_expected.npz") speech, lookahead, turn = _logits(BUILD / "sava.onnx") n = speech.size # 2e-4 is the tolerance the SAVA repository holds its own streaming path to. assert np.abs(speech - expected["speech"][:n]).max() < 2e-4 assert np.abs(turn - expected["turn"][:n]).max() < 2e-4 lag = sava_onnx.LAG_FRAMES assert np.abs(lookahead[lag:] - expected["lookahead"][lag:n]).max() < 2e-4 assert np.all(lookahead[:lag] == -30.0) # masked: they would judge frames before the stream def test_int8_version_agrees_with_the_reference_on_nearly_every_frame(): expected = np.load(FIXTURES / "speech_expected.npz") speech, lookahead, turn = _logits(BUILD / "sava.int8.onnx") n, lag = speech.size, sava_onnx.LAG_FRAMES speech_flips = ((lookahead[lag:] > 0) != (expected["lookahead"][lag:n] > 0)).mean() turn_flips = ((turn[:, 0] > 0) != (expected["turn"][:n, 0] > 0)).mean() assert speech_flips < 0.01 and turn_flips < 0.01 def test_session_streams_any_chunk_size_to_the_same_answer(): if not (BUILD / "sava.onnx").exists(): pytest.skip("sava.onnx has not been built") audio, _ = sf.read(FIXTURES / "speech.wav", dtype="float32") audio = audio[: 16000 * 4] whole = sava_onnx.Session(str(BUILD / "sava.onnx")) a = np.concatenate([whole.push(audio).speech, whole.flush().speech]) pieces = sava_onnx.Session(str(BUILD / "sava.onnx")) parts = [pieces.push(audio[i : i + 1000]).speech for i in range(0, audio.size, 1000)] b = np.concatenate([*parts, pieces.flush().speech]) assert a.shape == b.shape and np.array_equal(a, b) with pytest.raises(RuntimeError): pieces.push(audio) @pytest.mark.parametrize("line", ["clean", "phone", "gsm"]) def test_fine_tuned_export_matches_the_fine_tuned_pytorch_model(line): """``export.py --model runs/phone-1/best.safetensors --out build/sava-telephony.onnx``.""" if not FINE_TUNED.exists(): pytest.skip("no fine-tuned checkpoint") import torch from inworld import sava import channels audio, _ = sf.read(FIXTURES / "speech.wav", dtype="float32") heard = channels.TEST_CONDITIONS[line](audio) with torch.inference_mode(): expected = sava.load(str(FINE_TUNED))(torch.from_numpy(heard[: heard.size // 640 * 640])[None]) speech, lookahead, turn = _logits(BUILD / "sava-telephony.onnx", channels.TEST_CONDITIONS[line]) lag = sava_onnx.LAG_FRAMES assert np.abs(speech - expected.speech[0].numpy()).max() < 2e-4 assert np.abs(turn - expected.turn[0].numpy()).max() < 2e-4 assert np.abs(lookahead[lag:] - expected.lookahead[0, lag:].numpy()).max() < 2e-4 def test_fine_tuned_int8_only_changes_decisions_that_were_close(): """8 bits move a logit by up to about 0.7, as for the original weights (0.5 to 0.7 there). On this recording the fine-tuned model sits near 0.5 more often than the original, so a few more frames change side (1.05% of speech verdicts, 0.35% of turn verdicts) than the original's 0.2%. ``bench.py`` measures what that costs in accuracy. """ full = _logits(BUILD / "sava-telephony.onnx") small = _logits(BUILD / "sava-telephony.int8.onnx") lag = sava_onnx.LAG_FRAMES for a, b in ((small[1][lag:], full[1][lag:]), (small[2][:, 0], full[2][:, 0])): changed = (a > 0) != (b > 0) assert np.abs(a - b).max() < 1.0 assert changed.mean() < 0.02 assert np.abs(b[changed]).max(initial=0.0) < 1.0