Skip to main content

A simple package to time CPU/GPU/Multi-GPU ops

Project description

Torch Simple Timing

A simple yet versatile package to time CPU/GPU/Multi-GPU ops.

  1. "I want to time operations once"
    1. That's what a Clock is for
  2. "I want to time the same operations multiple times"
    1. That's what a Timer is for

In simple terms:

  • A Clock is an object (and context-manager) that will compute the ellapsed time between its start() (or __enter__) and stop() (or __exit__)
  • A Timer will 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.mark("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.mark("forward", ignore=epoch>0):
            p = model(x)
        loss = torch.nn.functional.cross_entropy(p, y)
        with timer.mark("backward", ignore=epoch>0):
            loss.backward()
        optimizer.step()
    timer.mark("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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torch_simple_timing-0.1.1.tar.gz (9.9 kB view details)

Uploaded Source

Built Distribution

torch_simple_timing-0.1.1-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file torch_simple_timing-0.1.1.tar.gz.

File metadata

  • Download URL: torch_simple_timing-0.1.1.tar.gz
  • Upload date:
  • Size: 9.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.9.2 Darwin/21.2.0

File hashes

Hashes for torch_simple_timing-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8fb34988a7247221e2e75823b6499f305145d0729129806f9f7f5d77e60e2b0a
MD5 023156f42835cd288cfdfddf6a810088
BLAKE2b-256 fd55012e6db9a596aee2367cff301bd84e93d1c2e6b7719cb6b14fd7430cacb4

See more details on using hashes here.

File details

Details for the file torch_simple_timing-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for torch_simple_timing-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f1d2bc0785fdc665df72a0cc2e949d9b93209f13f73b7732de5aa20259e94c0d
MD5 6b953c52fa505cf6f1254a49312cd4b0
BLAKE2b-256 6e8b583233fa48c991aca4f6fe0de70468bfc695034c8df78f8c661280996ad0

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page