SAVA exported to ONNX: full precision and 8-bit, with model card, runner, benchmarks and scripts
cadfcff Download scripts/speed.py from matthiastl/sava-onnx: direct link, hf CLI and curl.
- Browser
- Download file 2.94 kB
-
https://huggingface.co/matthiastl/sava-onnx/resolve/main/scripts/speed.py
- Command line
-
hf download hf://matthiastl/sava-onnx/scripts/speed.py
-
curl -L -o speed.py https://huggingface.co/matthiastl/sava-onnx/resolve/main/scripts/speed.py
2.94 kB
| """Time one 40 ms frame on one CPU thread: each ONNX file, and the PyTorch model's streaming step. | |
| The recording is run three times, frame by frame from a fresh stream; the first two passes warm | |
| up and the third is reported, as in the iOS app and the web page. | |
| python -I speed.py --audio fixtures/speech.wav --versions build/sava.onnx build/sava.int8.onnx | |
| """ | |
| from __future__ import annotations | |
| import argparse | |
| import json | |
| import platform | |
| import sys | |
| import time | |
| from pathlib import Path | |
| import numpy as np | |
| sys.path.insert(0, str(Path(__file__).parent)) # for sava_onnx when run with python -I | |
| FRAME = 640 | |
| PASSES = 3 | |
| def per_frame_ms(step, frames: list[np.ndarray], reset) -> float: | |
| for _ in range(PASSES): | |
| reset() | |
| started = time.perf_counter() | |
| for frame in frames: | |
| step(frame) | |
| elapsed = time.perf_counter() - started | |
| return 1000.0 * elapsed / len(frames) | |
| def onnx_timing(path: Path, frames: list[np.ndarray]) -> float: | |
| import onnxruntime as ort | |
| options = ort.SessionOptions() | |
| options.intra_op_num_threads = 1 | |
| options.inter_op_num_threads = 1 | |
| session = ort.InferenceSession(str(path), options, providers=["CPUExecutionProvider"]) | |
| size = session.get_inputs()[1].shape[1] | |
| state = {"value": np.zeros((1, size), dtype=np.float32)} | |
| def reset(): | |
| state["value"] = np.zeros((1, size), dtype=np.float32) | |
| def step(frame): | |
| state["value"] = session.run(None, {"pcm": frame[None], "state": state["value"]})[3] | |
| return per_frame_ms(step, frames, reset) | |
| def pytorch_timing(frames: list[np.ndarray]) -> float: | |
| import torch | |
| from inworld import sava | |
| torch.set_num_threads(1) | |
| model = sava.load("inworld/sava") | |
| holder = {} | |
| def reset(): | |
| holder["session"] = sava.Session(model) | |
| def step(frame): | |
| holder["session"].push(frame) | |
| with torch.inference_mode(): | |
| return per_frame_ms(step, frames, reset) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | |
| parser.add_argument("--audio", type=Path, required=True, help="a 16 kHz mono recording") | |
| parser.add_argument("--versions", type=Path, nargs="*", default=[], help="ONNX files") | |
| parser.add_argument("--no-pytorch", action="store_true") | |
| args = parser.parse_args() | |
| from inworld.sava import audio | |
| samples = audio.load(str(args.audio)) | |
| frames = [samples[i : i + FRAME].astype(np.float32) for i in range(0, samples.size - FRAME + 1, FRAME)] | |
| result = {"machine": platform.machine(), "processor": platform.processor(), "frames": len(frames), "ms_per_frame": {}} | |
| for path in args.versions: | |
| result["ms_per_frame"][path.name] = round(onnx_timing(path, frames), 2) | |
| if not args.no_pytorch: | |
| result["ms_per_frame"]["pytorch"] = round(pytorch_timing(frames), 2) | |
| print(json.dumps(result, indent=1)) | |
| if __name__ == "__main__": | |
| main() | |