"""Measure achievable XPU memory bandwidth and bf16 GEMM throughput (roofline anchors).""" import time, torch d = "xpu" def t(fn, n=20): fn(); torch.xpu.synchronize(); t0 = time.perf_counter() for _ in range(n): fn() torch.xpu.synchronize(); return (time.perf_counter() - t0) / n x = torch.empty(512 * 2**20, dtype=torch.uint8, device=d); y = torch.empty_like(x) s = t(lambda: y.copy_(x)); print(f"copy bandwidth (read+write): {2 * x.numel() / s / 1e9:.0f} GB/s") w = torch.randn(7680, 2048, device=d, dtype=torch.bfloat16); v = torch.randn(1, 2048, device=d, dtype=torch.bfloat16) s = t(lambda: v @ w.T, 200); print(f"GEMV 7680x2048 bf16 (decode-like): {w.numel() * 2 / s / 1e9:.0f} GB/s") for m in (4096, 8192): a = torch.randn(m, 2048, device=d, dtype=torch.bfloat16); b = torch.randn(7680, 2048, device=d, dtype=torch.bfloat16) s = t(lambda: a @ b.T); print(f"GEMM {m}x2048x7680 bf16: {2 * m * 2048 * 7680 / s / 1e12:.1f} TFLOPS") print(torch.xpu.get_device_name(), f"{torch.xpu.get_device_properties().total_memory / 2**30:.0f} GiB")