Last active
September 20, 2024 06:08
-
-
Save cli99/ceaacf96c811f9189fc88aa258abd2a5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import torch | |
| a = torch.randn(1000, 1000, device="cuda") | |
| torch.softmax(a, dim=1) | |
| times = [] | |
| for i in range(1000): | |
| t0 = time.perf_counter() | |
| torch.softmax(a, dim=1) | |
| torch.cuda.synchronize() | |
| t1 = time.perf_counter() | |
| times.append(t1 - t0) | |
| print(f"perf_counter Time: {1000*sum(times):.4f} us") | |
| torch.cuda.synchronize() | |
| times = [] | |
| for i in range(1000): | |
| t0 = time.perf_counter_ns() | |
| torch.softmax(a, dim=1) | |
| torch.cuda.synchronize() | |
| t1 = time.perf_counter_ns() | |
| times.append(t1 - t0) | |
| print(f"perf_counter_ns Time: {sum(times)/1000/1000:.4f} us") | |
| torch.softmax(a, dim=1) | |
| times = [] | |
| start = torch.cuda.Event(enable_timing=True) | |
| end = torch.cuda.Event(enable_timing=True) | |
| for i in range(1000): | |
| start.record() | |
| torch.softmax(a, dim=1) | |
| end.record() | |
| torch.cuda.synchronize() | |
| times.append(start.elapsed_time(end)) | |
| print(f"cuda.Event Time: {sum(times):.4f} us") | |
| torch.softmax(a, dim=1) | |
| starts = [torch.cuda.Event(enable_timing=True) for _ in range(1000)] | |
| ends = [torch.cuda.Event(enable_timing=True) for _ in range(1000)] | |
| for i in range(1000): | |
| starts[i].record() | |
| torch.softmax(a, dim=1) | |
| ends[i].record() | |
| torch.cuda.synchronize() | |
| times = [starts[i].elapsed_time(ends[i]) for i in range(1000)] | |
| print(f"cuda.Event list Time: {sum(times):.4f} us") | |
| # perf_counter Time: 11.4154 us | |
| # perf_counter_ns Time: 11.2727 us | |
| # cuda.Event list Time: 13.3122 us | |
| # cuda.Event Time: 12.3625 us |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment