File size: 1,684 Bytes
5182c89
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# sweep.py
from __future__ import annotations
import time
from statistics import median
import numpy as np
import torch
import deepfense_codecfake_w2v2_mlp

_CUT = 64000

def time_batch_sizes():
    print("device", torch.cuda.get_device_name())
    m = deepfense_codecfake_w2v2_mlp.DeepFenseCodecFakeW2V2MLP()
    m.load()
    import os
    if os.environ.get("SWEEP_TF32") is not None:
        torch.backends.cudnn.allow_tf32 = bool(int(os.environ["SWEEP_TF32"]))
    print("cudnn.allow_tf32", torch.backends.cudnn.allow_tf32)
    cuda = getattr(m, "device", "cpu") == "cuda"
    results = {}
    print("=== batch-size sweep ===")
    for bs in [1, 4, 8, 16, 24, 32, 48, 64]:
        audios = [np.random.randn(_CUT).astype(np.float32) for _ in range(bs)]
        srs = [16000] * bs
        try:
            m.score_batch(audios, srs)            # warm-up
            if cuda: torch.cuda.synchronize()
            reps = []
            for _ in range(5):
                if cuda: torch.cuda.synchronize()
                t0 = time.perf_counter()
                m.score_batch(audios, srs)
                if cuda: torch.cuda.synchronize()
                reps.append(time.perf_counter() - t0)
            med = median(reps); ups = bs / med
            results[bs] = ups
            print(f"bs={bs:3d}  median={med*1000:8.3f} ms  utt/s={ups:8.2f}")
        except torch.cuda.OutOfMemoryError:
            print(f"bs={bs:3d}  OOM (skipped)")
            if cuda: torch.cuda.empty_cache()
    m.unload()
    best = max(results, key=results.get)
    print(f"best batch_size = {best} (utt/s={results[best]:.2f})")
    return best, results

if __name__ == "__main__":
    time_batch_sizes()