Torch Simple Timing
A simple yet versatile package to time CPU/GPU/Multi-GPU ops.
- "I want to time operations once"
- That's what a
Clockis for
- That's what a
- "I want to time the same operations multiple times"
- That's what a
Timeris for
- That's what a
In simple terms:
- A
Clockis an object (and context-manager) that will compute the ellapsed time between itsstart()(or__enter__) andstop()(or__exit__) - A
Timerwill internally manage clocks so that you can focus on readability and not data structures
Installation
pip install torch_simple_timing
How to use
A Clock
from torch_simple_parsing import Clock
import torch
t = torch.rand(2000, 2000)
gpu = torch.cuda.is_available()
with Clock(gpu=gpu) as context_clock:
torch.inverse(t @ t.T)
clock = Clock(gpu=gpu).start()
torch.inverse(t @ t.T)
clock.stop()
print(context_clock.duration) # 0.29688501358032227
print(clock.duration) # 0.292896032333374
More examples, including bout how to easily share data structures using a store can be found in the documentation.
A Timer
from torch_simple_timing import Timer
import torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
X = torch.rand(5000, 5000, device=device)
Y = torch.rand(5000, 100, device=device)
model = torch.nn.Linear(5000, 100).to(device)
optimizer = torch.optim.Adam(model.parameters())
gpu = device.type == "cuda"
timer = Timer(gpu=gpu)
for epoch in range(10):
timer.clock("epoch").start()
for b in range(50):
x = X[b*100: (b+1)*100]
y = Y[b*100: (b+1)*100]
optimizer.zero_grad()
with timer.clock("forward", ignore=epoch>0):
p = model(x)
loss = torch.nn.functional.cross_entropy(p, y)
with timer.clock("backward", ignore=epoch>0):
loss.backward()
optimizer.step()
timer.clock("epoch").stop()
stats = timer.stats()
# use stats for display and/or logging
# wandb.summary.update(stats)
print(timer.display(stats=stats, precision=5))
epoch : 0.25064 ± 0.02728 (n=10)
forward : 0.00226 ± 0.00526 (n=50)
backward : 0.00209 ± 0.00387 (n=50)
A decorator
You can also use a decorator to time functions without much overhead in your code:
from torch_simple_timing import timeit, get_global_timer, reset_global_timer
import torch
# Use the function name as the timer name
@timeit(gpu=True)
def train():
x = torch.rand(1000, 1000, device="cuda" if torch.cuda.is_available() else "cpu")
return torch.inverse(x @ x)
# Use a custom name
@timeit("test")
def test_cpu():
return torch.inverse(torch.rand(1000, 1000) @ torch.rand(1000, 1000))
if __name__ == "__main__":
for _ in range((epochs := 10)):
train()
test_cpu()
timer = get_global_timer()
print(timer.display())
reset_global_timer()
Prints:
train : 0.045 ± 0.007 (n=10)
test : 0.046 (n= 1)
By default the @timeit decodrator takes at least a name, will use gpu=False and use the global timer (torch_simple_timing.TIMER). You can pass your own timer with @timeit(name, timer=timer).
See in the docs.
Metadata
Release files for torch-simple-timing 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| torch_simple_timing-0.1.5.tar.gz | 12.7 kB | Without provenance | Details for torch_simple_timing-0.1.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| torch_simple_timing-0.1.5-py3-none-any.whl | Python 3 | none | any | Details for torch_simple_timing-0.1.5-py3-none-any.whl |
Total release size: 25.5 kB
Release files / torch_simple_timing-0.1.5.tar.gz
| Download URL | torch_simple_timing-0.1.5.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
faa1522ca3cd907f9b3af076c8d9b9edbd81401bcf6003f0c13b3d3068b0c81f
|
|
BLAKE2b-256 checksum How to use checksums |
786744299519cc181b1d294374a5791ea1819617b534932b82523573e2358d71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.2 Darwin/22.4.0
|
Release files / torch_simple_timing-0.1.5-py3-none-any.whl
| Download URL | torch_simple_timing-0.1.5-py3-none-any.whl |
|---|---|
| Size | 12.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f0849c533288a7b3978632df7d5da945013896db5a50c780992d156a5998062
|
|
BLAKE2b-256 checksum How to use checksums |
848a7ad167cd3eba15b39c0505f91b50e1fd809a8b3d170e4e52ae2a57340b66
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.9.2 Darwin/22.4.0
|
Security for torch-simple-timing 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.