aboutsummaryrefslogtreecommitdiff
path: root/examples/torch_gpu_benchmark.py
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-28 07:17:33 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-28 07:17:33 +0200
commit9807b449c66b06b708766753a3d38ea9022537d8 (patch)
tree21488bfb28e9adf151c6acb861dde0f4e37d73b2 /examples/torch_gpu_benchmark.py
parentdb5c2134ff03c459f226aeb17b00b659c90e0543 (diff)
downloadcolabctl-9807b449c66b06b708766753a3d38ea9022537d8.tar.gz
colabctl-9807b449c66b06b708766753a3d38ea9022537d8.zip
Add examples
Diffstat (limited to 'examples/torch_gpu_benchmark.py')
-rw-r--r--examples/torch_gpu_benchmark.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/examples/torch_gpu_benchmark.py b/examples/torch_gpu_benchmark.py
new file mode 100644
index 0000000..777ea3b
--- /dev/null
+++ b/examples/torch_gpu_benchmark.py
@@ -0,0 +1,31 @@
+import torch
+
+assert torch.cuda.is_available(), "no CUDA device"
+dev = torch.device("cuda")
+print("Device:", torch.cuda.get_device_name(0))
+print("Torch :", torch.__version__)
+
+# Measured matrix-multiply throughput in TFLOP/s.
+n = 4096
+a = torch.randn(n, n, device=dev, dtype=torch.float32)
+b = torch.randn(n, n, device=dev, dtype=torch.float32)
+
+# Warm up so the timed loop excludes one-time CUDA setup.
+for _ in range(3):
+ a @ b
+torch.cuda.synchronize()
+
+start = torch.cuda.Event(enable_timing=True)
+end = torch.cuda.Event(enable_timing=True)
+
+iters = 20
+start.record()
+for _ in range(iters):
+ c = a @ b
+end.record()
+torch.cuda.synchronize()
+
+ms = start.elapsed_time(end) / iters
+flops = 2 * n ** 3 # one multiply and one add per output element
+tflops = flops / (ms * 1e-3) / 1e12
+print(f"{n}x{n} matmul: {ms:.2f} ms/iter, {tflops:.1f} TFLOP/s (fp32)")