Benchmark torch.matmul at all sizes and dtypes. Returns results dict.
()
| 274 | # --------------------------------------------------------------------------- |
| 275 | |
| 276 | def benchmark_baselines() -> dict: |
| 277 | """Benchmark torch.matmul at all sizes and dtypes. Returns results dict.""" |
| 278 | |
| 279 | print("Benchmarking PyTorch baselines...") |
| 280 | results = {} |
| 281 | |
| 282 | for size_name, dims in MATMUL_SIZES: |
| 283 | M, N, K = dims["M"], dims["N"], dims["K"] |
| 284 | flops = _matmul_flops(M, N, K) |
| 285 | |
| 286 | for dtype in TEST_DTYPES: |
| 287 | tag = _dtype_tag(dtype) |
| 288 | |
| 289 | # Load cached test data if available, else generate on the fly |
| 290 | save_path = os.path.join(TEST_DATA_DIR, "matmul", size_name, f"{tag}.pt") |
| 291 | if os.path.exists(save_path): |
| 292 | data = torch.load(save_path, weights_only=True) |
| 293 | A = data["A"].cuda() |
| 294 | B = data["B"].cuda() |
| 295 | else: |
| 296 | gen = torch.Generator(device="cpu") |
| 297 | gen.manual_seed(_SEED) |
| 298 | A = torch.randn(M, K, generator=gen, dtype=dtype).cuda() |
| 299 | B = torch.randn(K, N, generator=gen, dtype=dtype).cuda() |
| 300 | |
| 301 | latency_us = _benchmark_fn(torch.matmul, A, B) |
| 302 | tflops = flops / (latency_us * 1e-6) / 1e12 |
| 303 | |
| 304 | key = f"matmul_{size_name}_{tag}" |
| 305 | results[key] = { |
| 306 | "kernel_type": "matmul", |
| 307 | "size": size_name, |
| 308 | "dtype": tag, |
| 309 | "M": M, "N": N, "K": K, |
| 310 | "latency_us": round(latency_us, 2), |
| 311 | "throughput_tflops": round(tflops, 3), |
| 312 | } |
| 313 | |
| 314 | print(f" matmul {size_name} {tag}: {tflops:.1f} TFLOPS ({latency_us:.2f} us)") |
| 315 | |
| 316 | # Free GPU memory |
| 317 | del A, B |
| 318 | torch.cuda.empty_cache() |
| 319 | |
| 320 | print() |
| 321 | return results |
| 322 | |
| 323 | |
| 324 | # --------------------------------------------------------------------------- |
no test coverage detected