# sweep.py -- batch-size throughput sweep (48000-sample inputs = the model's fixed 3 s window) from __future__ import annotations import time from statistics import median import numpy as np import torch import rawnetlite as R _CUT = 48000 def time_batch_sizes(): print("device", torch.cuda.get_device_name(), "| other GPU processes during the sweep make it unreliable (check nvidia-smi)") m = R.RawNetLite(); m.load() results = {} for bs in [1, 8, 16, 32, 64, 96, 128, 192, 256]: audios = [np.random.randn(_CUT).astype(np.float32) * 0.1 for _ in range(bs)] srs = [16000] * bs try: m.score_batch(audios, srs); torch.cuda.synchronize() reps = [] for _ in range(5): torch.cuda.synchronize(); t0 = time.perf_counter() m.score_batch(audios, srs) torch.cuda.synchronize(); reps.append(time.perf_counter() - t0) med = median(reps); results[bs] = bs / med print(f"bs={bs:3d} median={med*1000:8.3f} ms utt/s={bs/med:8.2f} peak_mem={torch.cuda.max_memory_allocated()/2**30:.2f} GiB") except RuntimeError as e: # TorchScript wraps CUDA OOM in a plain RuntimeError if "out of memory" not in str(e): raise print(f"bs={bs:3d} OOM"); torch.cuda.empty_cache() break m.unload() best = max(results, key=results.get) print(f"best batch_size = {best} (utt/s={results[best]:.2f})") if __name__ == "__main__": time_batch_sizes()