"""RULE-2 1: the streamed ONNX decoder against NeMo's whole-sentence decode, on 10 sentences of the reference's frames; and its speed.""" import sys, time; from pathlib import Path sys.path.insert(0, str(Path(__file__).parent)) import numpy as np, onnxruntime as ort ROOT = Path(__file__).resolve().parents[1]; HALF = np.array([4, 3, 3, 3] * 8, np.float32) def make(th): o = ort.SessionOptions(); o.intra_op_num_threads = th; o.inter_op_num_threads = 1; return ort.InferenceSession(str(ROOT / "export/codec_stream.onnx"), o, providers=["CPUExecutionProvider"]) whole = ort.InferenceSession(str(ROOT / "export/codec_decoder.onnx"), providers=["CPUExecutionProvider"]) s = make(4); N = s.get_inputs()[1].shape[0]; worst = mean = 0.0 for i in range(10): c = np.load(ROOT / f"runs/ref/{i + 20:02d}.npz")["codes"]; z = ((c.astype(np.float32) - HALF) / HALF).T[None]; ref = whole.run(None, {"z": z})[0].reshape(-1) mem = np.zeros(N, np.float32); out = [] for a in range(0, z.shape[2], 12): y, mem = s.run(None, {"z": z[:, :, a:a + 12], "memory": mem}); out.append(y.reshape(-1)) out = np.concatenate(out); worst = max(worst, float(np.abs(out - ref).max())); mean = max(mean, float(np.abs(out - ref).mean())) print(f"streamed ONNX vs whole-sentence ONNX, 10 sentences: worst sample {worst:.2e}, worst sentence mean {mean:.2e}") for th in (1, 4): s = make(th); z = np.zeros((1, 32, 12), np.float32); mem = np.zeros(N, np.float32); s.run(None, {"z": z, "memory": mem}); t = [] for _ in range(25): a = time.perf_counter(); _, mem = s.run(None, {"z": z, "memory": mem}); t.append(time.perf_counter() - a) m = np.median(t); print(f"stream codec, {th} thread(s): {m * 1e3:.0f} ms per 12 frames (557 ms of sound) = {m / 0.5573:.2f} of real time")