sava-onnx / scripts /drift.py
matthiastl's picture
SAVA exported to ONNX: full precision and 8-bit, with model card, runner, benchmarks and scripts
cadfcff
Raw History Blame Contribute Delete
2.38 kB
"""How far each smaller version's outputs move from the full-precision export, frame by frame.
For every recording and version: mean and largest change in P(speech) and P(end of turn), and the
share of frames whose yes/no answer at 0.5 flips. This measures faithfulness to the original
model, not accuracy against labels.
"""
from __future__ import annotations
import argparse
import time
from pathlib import Path
import numpy as np
import soundfile as sf
import sava_onnx
def curves(model: Path, audio: np.ndarray) -> tuple[np.ndarray, np.ndarray, float]:
session = sava_onnx.Session(str(model))
start = time.perf_counter()
speech, turn = session.run(audio)
return speech, turn, 1000 * (time.perf_counter() - start) / session.frames
def main() -> None:
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0])
parser.add_argument("--reference", type=Path, default=Path("build/sava.onnx"))
parser.add_argument("--versions", type=Path, nargs="+", required=True)
parser.add_argument("--audio", type=Path, nargs="+", required=True)
args = parser.parse_args()
print("| Recording | Version | MB | ms/frame | Speech: mean / max change | flips | Turn: mean / max change | flips |")
print("|---|---|---|---|---|---|---|---|")
for path in args.audio:
audio, sample_rate = sf.read(path, dtype="float32")
assert sample_rate == sava_onnx.SAMPLE_RATE and audio.ndim == 1, path
ref_speech, ref_turn, ref_ms = curves(args.reference, audio)
print(
f"| {path.stem} | {args.reference.name} | {args.reference.stat().st_size / 1e6:.0f} | {ref_ms:.2f} | "
f"speech in {100 * (ref_speech > 0.5).mean():.0f}% of {ref_speech.size} frames | | "
f"turn ended in {100 * (ref_turn > 0.5).mean():.0f}% | |"
)
for version in args.versions:
speech, turn, ms = curves(version, audio)
cells = []
for got, ref in ((speech, ref_speech), (turn, ref_turn)):
change = np.abs(got - ref)
flips = ((got > 0.5) != (ref > 0.5)).mean()
cells += [f"{change.mean():.4f} / {change.max():.3f}", f"{100 * flips:.2f}%"]
print(f"| {path.stem} | {version.name} | {version.stat().st_size / 1e6:.0f} | {ms:.2f} | " + " | ".join(cells) + " |")
if __name__ == "__main__":
main()