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.
sava-TensorRT / fp16 /pipeline_trt.py
jennyxue's picture
fp16 build of SAVA (voice activity + end of turn)
d45acc0 verified
Raw History Blame Contribute Delete
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])