"""Checks that need no model weights and no GPU. Run first on the training machine: python -m tests.test_streaming 1. Frame-by-frame serving is the same function as whole-crop training, for the shared transformer, the student encoder and the controller. 2. The timeline builder's labels obey their definition on a synthetic dialogue. """ import numpy as np import torch from controller.encoder.student import StudentEncoder from controller.model.model import Controller from data.build_timeline import lay_out from duplex.spec import AGENT, CALLER, FRAME, SR, TEACHER_DIM from duplex.streaming import CausalTransformer def close(a, b, what, tol=1e-4): err = (a - b).abs().max().item() assert err < tol, f"{what}: streaming and offline differ by {err}" print(f"ok {what}: max difference {err:.2e}") @torch.no_grad() def test_transformer(): torch.manual_seed(0) tf = CausalTransformer(dim=64, layers=3, heads=4, window=16).eval() x = torch.randn(2, 61, 64) state, parts = tf.init_state(), [] for i in range(0, 61, 2): # longer than the window, odd length, 2 tokens at a time y, state = tf.step(x[:, i: i + 2], state) parts.append(y) close(torch.cat(parts, 1), tf(x), "transformer") @torch.no_grad() def test_student(): torch.manual_seed(0) fb = torch.rand(128, 201) * 0.01 # any filterbank: the check is about streaming, not about mel values enc = StudentEncoder(dim=64, layers=2, heads=4, window=20, out_dim=32, fb=fb).eval() wav = torch.randn(2, 40 * FRAME) * 0.1 state, parts = enc.init_state(2, wav.device), [] for i in range(40): y, state = enc.step(wav[:, i * FRAME: (i + 1) * FRAME], state) parts.append(y) close(torch.cat(parts, 1), enc(wav), "student encoder") @torch.no_grad() def test_controller(): torch.manual_seed(0) ctl = Controller(feat_dim=32, dim=64, layers=2, heads=4, window=12).eval() caller, own = torch.randn(1, 60, 32), torch.randn(1, 60, 32) state, parts = ctl.init_state(), [] for i in range(0, 60, 2): y, state = ctl.step(caller[:, i: i + 2], own[:, i: i + 2], state) parts.append(y) close(torch.cat(parts, 1), ctl(caller, own), "controller") assert Controller().cfg["feat_dim"] == TEACHER_DIM def test_labels(): rng = np.random.default_rng(0) speech = lambda s: (rng.standard_normal(int(s * SR)) * 8000).astype(np.int16) for trial in range(200): d = {"turns": [ {"spk": "agent", "text": "", "interrupts": False}, {"spk": "caller", "text": "", "interrupts": False}, {"spk": "agent", "text": "", "interrupts": False}, {"spk": "caller", "text": "", "interrupts": True}, {"spk": "agent", "text": "", "interrupts": False}, {"spk": "caller", "text": "", "interrupts": False}, ]} clips = {"t0_s0": speech(2), "t1_s0": speech(1.5), "t1_s1": speech(1), "t2_s0": speech(6), "t3_s0": speech(2), "t4_s0": speech(7), "t5_s0": speech(1)} place, events, end = lay_out(d, clips, [speech(0.4).astype(np.float32) / 32768], np.random.default_rng(trial)) audio = np.zeros((-(-end // FRAME) * FRAME, 2), dtype=np.float32) for ch, start, x in place: audio[start: start + len(x), ch] += x loud = lambda ch, f0, f1: bool(np.abs(audio[f0 * FRAME: f1 * FRAME, ch]).max() > 0) if f1 > f0 else False kinds = [e["type"] for e in events] # two caller turns are answered (the last caller turn ends the call), one has a pause, one cuts in assert kinds.count("pause") == 1 and kinds.count("interrupt") == 1 and kinds.count("start") == 2, kinds for e in events: if e["type"] == "start": # nobody speaks during START; the agent speaks right after it assert not loud(CALLER, e["f0"], e["f1"]) and not loud(AGENT, e["f0"], e["f1"]) and loud(AGENT, e["f1"], e["f1"] + 1) elif e["type"] == "pause": # the caller is silent in a pause and nothing was labelled there assert not loud(CALLER, e["f0"], e["f1"]) elif e["type"] == "interrupt": # both speak during STOP; the agent is silent from the cut on assert loud(CALLER, e["f0"], e["f1"]) and loud(AGENT, e["f0"], e["f1"]) and not loud(AGENT, e["f1"], e["f1"] + 3) elif e["type"] == "backchannel": # the caller's "mm-hm" lies inside the agent's speech assert loud(CALLER, e["f0"], e["f1"]) and loud(AGENT, e["f0"], e["f1"]) print("ok labels: 200 synthetic dialogues obey the label definitions") if __name__ == "__main__": test_transformer() test_student() test_controller() test_labels()