Voice Activity Detection
TensorRT
moonbox
inworld
sava
vad
end-of-turn
turn-detection
streaming
realtime
Run this model with HF Jobs
Run moonbox-dev/sava-TensorRT on Hugging Face's managed compute, monitor its progress, and retrieve the results when it's done.
Paste one of these prompts into your coding agent:
Fine-tune on my dataset
Use HF Jobs to fine-tune https://huggingface.co/moonbox-dev/sava-TensorRT. Ask me which dataset to train on, then push the fine-tuned model to a new model repo. Run `hf jobs --help` first to learn the CLI.
Run batch inference
Use HF Jobs to run batch inference with https://huggingface.co/moonbox-dev/sava-TensorRT on the inputs in a mounted HF Bucket. Ask me which Bucket to use, then write the outputs back to it and keep each input ID. Run `hf jobs --help` first to learn the CLI.
Instructions to use moonbox-dev/sava-TensorRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use moonbox-dev/sava-TensorRT with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
Download fp16/pipeline_trt.py from moonbox-dev/sava-TensorRT: direct link, hf CLI and curl.
- Browser
- Download file 3.29 kB
-
https://huggingface.co/moonbox-dev/sava-TensorRT/resolve/main/fp16/pipeline_trt.py
- Command line
-
hf download hf://moonbox-dev/sava-TensorRT/fp16/pipeline_trt.py
-
curl -L -o pipeline_trt.py https://huggingface.co/moonbox-dev/sava-TensorRT/resolve/main/fp16/pipeline_trt.py
3.29 kB
| """SAVA (Inworld: streaming voice activity and end-of-turn detection) with a TensorRT engine: the same pipeline as | |
| pipeline.py (the package's streaming Session: 40 ms frames, the lookahead, the turn heads), with | |
| - step.engine: one 640-sample frame (16 kHz) and the stream state (mel front-end tail, the trunk's conv tails and SSM | |
| states, the attention caches) -> lookahead / speech / turn logits and the next state. All shapes are fixed. | |
| Built from the fp16 ONNX build, whose weights are stored in fp16 and read as fp32: the engine's math is fp32. The | |
| stream state stays on the GPU (two sets of buffers per stream, read from one and written to the other each frame): | |
| only the frame goes in and the three logits come out. Needs numpy, tensorrt-cu12 / tensorrt-lean-cu12 10.x and | |
| polygraphy; soundfile to read files. | |
| python fp16/pipeline_trt.py speech_16k.wav | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import numpy as np | |
| HERE = Path(__file__).resolve().parent | |
| sys.path.insert(0, str(HERE)) | |
| from device_chain import Chain, device_array # noqa: E402 | |
| from pipeline import _DT, Sava, Session, run # noqa: E402 | |
| HEADS = ["lookahead", "speech", "turn"] | |
| class _State: | |
| """One stream's state on the GPU: set a is read by the next step, set b written by it.""" | |
| def __init__(self, spec, stream): | |
| self.names = [n for n, _, _ in spec] | |
| self.a, self.b = {}, {} | |
| for n, shape, dt in spec: | |
| for side in (self.a, self.b): | |
| buf = device_array(tuple(shape) or (1,), dt) | |
| buf.resize(tuple(shape)) | |
| side[n] = buf | |
| z = np.zeros(shape, dt) | |
| self.a[n].copy_from(z, stream) | |
| def swap(self): | |
| self.a, self.b = self.b, self.a | |
| def free(self): | |
| for buf in list(self.a.values()) + list(self.b.values()): | |
| buf.free() | |
| self.a = self.b = {} | |
| class SavaTrt(Sava): | |
| def __init__(self, folder=HERE): | |
| folder = Path(folder) | |
| self.cfg = json.loads((folder / "config.json").read_text()) | |
| self.frame = self.cfg["frame_samples"] | |
| self.lag = self.cfg["lookahead_frames"] | |
| self.chain = Chain(folder, [{"engine": "step.engine"}]).load() | |
| e = self.chain.engines[0] | |
| want = {n: tuple(e.engine.get_tensor_shape(n)) for n in e.inputs} | |
| # config.json lists the scalar offsets as [1]; the engine takes [] | |
| self.spec = [(n, want[n], _DT[t]) for n, _, t in self.cfg["state"]] | |
| self.next = {n: "next." + n[len("state."):] for n, _, _ in self.spec} | |
| def init_state(self): | |
| st = _State(self.spec, self.chain.stream) | |
| self.chain.stream.synchronize() | |
| return st | |
| def step(self, frame, state): | |
| out = self.chain({"pcm": np.ascontiguousarray(frame.reshape(1, -1), np.float32), **state.a}, | |
| outputs={self.next[n]: state.b[n] for n in state.names}, fetch=HEADS) | |
| state.swap() | |
| return out["lookahead"].reshape(-1)[0], out["speech"].reshape(-1)[0], out["turn"].reshape(-1), state | |
| if __name__ == "__main__": | |
| import soundfile as sf | |
| x, sr = sf.read(sys.argv[1], dtype="float32", always_2d=True) | |
| assert sr == 16000, "16 kHz audio" | |
| print(json.dumps(run(SavaTrt(), x.mean(1)))[:2000]) | |