aurora-wake-model / code /plans /s1 /delay_device.py
thucdangvan020999's picture
Initial upload: releases, checkpoints, code, docs
a1fe2e2 verified
Raw History Blame Contribute Delete
2.43 kB
#!/usr/bin/env python3
"""Wake delay of the cascade when stage-2 catch-up takes real compute time (device estimate).
On a stage-1 trigger at time x, stage 2 must process frames from j-lb-146 (warm-up) onward; it runs at C ms per frame
(all stage-2 members), so frame k's decision is ready at max((k+1)*0.01, x + (k - k0 + 1) * C/1000) while the backlog lasts.
python delay_device.py S2SPEC T2 C_MS_PER_FRAME [C ...]"""
import json, sys
import numpy as np
sys.path.insert(0, "/home/user/Downloads/vita-kws/plans_local/fa2"); sys.path.insert(0, "/home/user/Downloads/vita-kws/plans_local/s1")
import cascade as C # noqa
S2, T2 = sys.argv[1], float(sys.argv[2]); T1 = float(sys.argv[3]); CS = [float(c) for c in sys.argv[4:]]
TAGS = [t.split("*")[0] for t in S2.split("+")]; C.WTS = [float(t.split("*")[1]) for t in S2.split("+")] if "*" in S2 else None
z1 = np.load(C.E1 / "delay_s2_s1bc3.npz", allow_pickle=True); z2 = C.s2_load(lambda t: C.E1 / f"delay_s2_{t}.npz", TAGS)
meta = {m["name"]: m for m in json.load(open("/tmp/claude-1000/-home-user-Downloads-vita-wakeword-handoff/76566f21-879a-47b4-93d7-fe8a82af41ba/scratchpad/twbench/f32/meta.json"))}
for c in [0.0] + CS:
D = []; n = 0
for nm, d1, d in zip(z1["names"], z1["pos"], z2["pos"]):
e = meta[nm]["end"]
if e is None: continue
n += 1; t2 = C.s2_track(d, 120); nf = len(t2)
s1 = C.s2_track(np.asarray(d1, np.float64), 60); s1 = s1[:len(s1) // 2 * 2].reshape(-1, 2).max(1)
ready = np.full(nf, np.inf); act_end = -1; busy_until = 0.0
for i in np.flatnonzero(s1 >= T1):
x = (2 * i + 2) * 0.01; j = 2 * i + 1
if j >= nf: break
k0 = max(j - 50 - 146, 0) if j > act_end else act_end + 1 # new activation: warm-up + lookback; else extend
k1 = min(nf, j + 60)
t = max(x, busy_until)
for k in range(k0, k1):
t = max(t + c / 1000, (k + 1) * 0.01); ready[k] = min(ready[k], t) if ready[k] < np.inf else t
busy_until = t; act_end = max(act_end, k1 - 1)
hit = [k for k in np.flatnonzero(t2 >= T2) if ready[k] < np.inf]
w = [ready[k] for k in hit if ready[k] >= 0.5]
if w: D.append((min(w) - e) * 1000)
q = np.percentile(D, [10, 50, 90]); print(f"{S2} @{T2} t1 {T1} stage-2 {c:.2f} ms/frame: fires {100 * len(D) / n:.1f}% delay p10/p50/p90 {q[0]:+.0f}/{q[1]:+.0f}/{q[2]:+.0f} ms", flush=True)