Download scripts/port2.py from matthiastl/gepard-onnx: direct link, hf CLI and curl.
- Browser
- Download file 2.34 kB
-
https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/port2.py
- Command line
-
hf download hf://matthiastl/gepard-onnx/scripts/port2.py
-
curl -L -o port2.py https://huggingface.co/matthiastl/gepard-onnx/resolve/main/scripts/port2.py
2.34 kB
| """Port with the streaming sound decoder: 12-frame chunks, each frame decoded once.""" | |
| import math, time | |
| import numpy as np | |
| import port as P | |
| CHUNK = 12 | |
| class Port2(P.Port): | |
| def __init__(self, decoder="decoder.int8.onnx", threads=4, folder=None): | |
| super().__init__(folder=folder or P.ROOT / "export", decoder=decoder, threads=threads) | |
| self.stream = P.session((folder or P.ROOT / "export") / "codec_stream.onnx", threads) | |
| self.n = self.stream.get_inputs()[1].shape[0] | |
| def speak(self, ids, seed, temperature=0.3, max_frames=600, prefix=None, timing=None): | |
| """-> (frames [T, 32], audio). `timing`, a dict, gets seconds: per frame (decoder + its share of the sound), first sound.""" | |
| t0 = time.perf_counter() | |
| rng = np.random.default_rng(seed) | |
| h, k, v = self.prefill(ids, prefix) | |
| frames, audio, mem, pend, per, first = [], [], np.zeros(self.n, np.float32), 0, [], None | |
| for i in range(max_frames): | |
| a = time.perf_counter() | |
| logits, stop = self.out(h) | |
| if i > 0 and 1 / (1 + math.exp(-stop)) > 0.5: | |
| break | |
| codes = P.sample(logits, rng, temperature) | |
| frames.append(codes) | |
| h, k, v = self.step(codes, k, v) | |
| per.append(time.perf_counter() - a) | |
| pend += 1 | |
| if pend == CHUNK: | |
| b = time.perf_counter() | |
| mem = self._sound(frames[-CHUNK:], mem, audio) | |
| e = (time.perf_counter() - b) / CHUNK | |
| per[-CHUNK:] = [x + e for x in per[-CHUNK:]] | |
| pend = 0 | |
| first = first or time.perf_counter() - t0 | |
| if pend: | |
| b = time.perf_counter() | |
| mem = self._sound(frames[-pend:], mem, audio) | |
| e = (time.perf_counter() - b) / pend | |
| per[-pend:] = [x + e for x in per[-pend:]] | |
| first = first or time.perf_counter() - t0 | |
| if timing is not None: | |
| timing.update(total=time.perf_counter() - t0, per=per, first=first) | |
| return np.stack(frames), np.concatenate(audio) | |
| def _sound(self, frames, mem, audio): | |
| z = ((np.stack(frames).astype(np.float32) - P.HALF) / P.HALF).T[None] | |
| a, mem = self.stream.run(None, {"z": z, "memory": mem}) | |
| audio.append(a.reshape(-1)) | |
| return mem | |