aboutsummaryrefslogtreecommitdiff
path: root/tools/baseline.py
blob: 08ae72a9922dad854e6d930ba338b0a69819a4bd (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
"""PyTorch baseline measurement and golden-vector capture.

Usage:
    python tools/baseline.py bench [--batch N]
    python tools/baseline.py golden <audio.wav> <out.npz> [--chunks N]

The benchmark protocol is fixed here and mirrored by bench/bench.c so the two
numbers are comparable: 3 warm-up forwards, then 20 timed forwards on the same
input, reporting mean, p50 and p99 in milliseconds plus the realtime factor.
"""

import argparse
import resource
import statistics
import sys
import time

import numpy as np

from model import NUM_FRAMES, NUM_SAMPLES, SAMPLE_RATE, checkpoint_path


def load_torch_model():
    import torch
    from pyannote.audio import Model

    torch.set_num_threads(1)
    model = Model.from_pretrained(checkpoint_path())
    model.eval()
    return model


def read_wav(path: str) -> np.ndarray:
    import soundfile as sf

    data, rate = sf.read(path, dtype="float32", always_2d=True)
    if rate != SAMPLE_RATE:
        raise SystemExit(f"{path}: expected {SAMPLE_RATE} Hz, got {rate}")
    return np.ascontiguousarray(data[:, 0])


def bench(args) -> int:
    import torch

    t0 = time.perf_counter()
    model = load_torch_model()
    init_ms = (time.perf_counter() - t0) * 1e3

    rng = np.random.default_rng(1)
    wav = rng.standard_normal((args.batch, 1, NUM_SAMPLES)).astype(np.float32) * 0.1
    x = torch.from_numpy(wav)

    with torch.no_grad():
        for _ in range(3):
            model(x)
        times = []
        for _ in range(20):
            t = time.perf_counter()
            model(x)
            times.append((time.perf_counter() - t) * 1e3)

    times.sort()
    mean = statistics.fmean(times)
    p50 = times[len(times) // 2]
    p99 = times[min(len(times) - 1, int(0.99 * len(times)))]
    audio_s = 10.0 * args.batch
    rss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1e6

    print(f"torch batch={args.batch} threads=1")
    print(f"  init            {init_ms:8.1f} ms")
    print(f"  mean/p50/p99    {mean:8.2f} / {p50:.2f} / {p99:.2f} ms")
    print(f"  per chunk       {mean / args.batch:8.2f} ms")
    print(f"  realtime factor {audio_s / (mean / 1e3):8.1f}x")
    print(f"  max rss         {rss:8.1f} MB")
    return 0


def golden(args) -> int:
    import torch

    from reference import Reference
    from model import load_state_dict

    wav = read_wav(args.audio)
    step = int(1.0 * SAMPLE_RATE)
    starts = list(range(0, len(wav) - NUM_SAMPLES + 1, step))[: args.chunks]
    if not starts:
        raise SystemExit("audio shorter than one 10 s window")

    model = load_torch_model()
    inputs = np.stack([wav[s : s + NUM_SAMPLES] for s in starts])

    outs = []
    with torch.no_grad():
        for i in range(0, len(inputs), 8):
            batch = torch.from_numpy(inputs[i : i + 8])[:, None, :]
            outs.append(model(batch).numpy())
    outputs = np.concatenate(outs)
    assert outputs.shape == (len(starts), NUM_FRAMES, 7)

    # A numpy-reference pass over the same inputs pins the tolerance the C
    # engine is later judged against.
    ref = Reference(load_state_dict())
    ref_out = np.stack([ref.forward(x) for x in inputs[: min(8, len(inputs))]])
    diff = np.abs(ref_out - outputs[: len(ref_out)]).max()
    print(f"numpy reference vs torch on {len(ref_out)} chunks: max diff {diff:.3e}")

    np.savez_compressed(
        args.out,
        inputs=inputs.astype(np.float32),
        outputs=outputs.astype(np.float32),
        starts=np.array(starts, dtype=np.int64),
    )
    print(f"wrote {args.out}: {len(starts)} chunks from {args.audio}")
    return 0


def main() -> int:
    ap = argparse.ArgumentParser()
    sub = ap.add_subparsers(dest="cmd", required=True)

    b = sub.add_parser("bench")
    b.add_argument("--batch", type=int, default=1)
    b.set_defaults(func=bench)

    g = sub.add_parser("golden")
    g.add_argument("audio")
    g.add_argument("out")
    g.add_argument("--chunks", type=int, default=32)
    g.set_defaults(func=golden)

    args = ap.parse_args()
    return args.func(args)


if __name__ == "__main__":
    sys.exit(main())