File size: 1,965 Bytes
f730521
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""How long one 80 ms controller step takes (student encoder on both audio streams plus the
controller), frame by frame as the server runs it. A session needs 12.5 steps per second.

  python -m tools.bench_controller --encoder runs/encoder/final.pt [--controller runs/controller/final.pt] --device cuda:0
"""
import argparse
import time

import numpy as np
import torch

from controller.encoder.student import load_student
from controller.model.model import Controller, load_controller
from controller.runtime import DuplexController
from duplex.spec import FRAME


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--encoder", required=True)
    ap.add_argument("--controller", default=None, help="omit to time a randomly initialised controller of the default size")
    ap.add_argument("--device", default="cuda:0")
    ap.add_argument("--frames", type=int, default=600)
    args = ap.parse_args()

    rt = DuplexController.__new__(DuplexController)
    rt.device = torch.device(args.device)
    rt.encoder = load_student(args.encoder, rt.device)
    rt.ctl = load_controller(args.controller, rt.device) if args.controller else Controller(rt.encoder.cfg["out_dim"]).to(rt.device).eval()
    sess = rt.new_session()
    rng = np.random.default_rng(0)
    times = []
    for _ in range(args.frames):
        a, b = (rng.standard_normal(FRAME).astype(np.float32) * 0.05 for _ in range(2))
        t0 = time.perf_counter()
        rt.step(sess, a, b)
        times.append((time.perf_counter() - t0) * 1000)
    steady = np.array(times[50:])
    print(f"controller step over {len(steady)} frames: mean {steady.mean():.2f} ms, p50 {np.percentile(steady, 50):.2f} ms, "
          f"p99 {np.percentile(steady, 99):.2f} ms; first 400 frames mean {np.mean(times[50:400]):.2f} ms, last 100 mean {np.mean(times[-100:]):.2f} ms")
    print(f"one thread could carry about {1000 / steady.mean() / 12.5:.1f} sessions")


if __name__ == "__main__":
    main()