torch_simple_timing#
“I want to time operations once”
That’s what a
Clockis for
“I want to time the same operations multiple times”
That’s what a
Timeris for
In simple terms:
- A
Clockis an object (and context-manager) that will compute the elapsed time between its
start()(or__enter__()) andstop()(or__exit__())
- A
- A
Timerwill internally manage clocks so that you can focus on readability and not data structures
- A
You can also decorate your functions with timeit() to time
their execution with little code overhead. Then use
get_global_timer() to access the timer it uses and access
its stats() or use it manually to time code.
from torch_simple_timing import timeit, get_global_timer
@timeit(gpu=True)
def train(*args, **kwargs):
# do stuff
@timeit(gpu=True)
def test(*args, **kwargs):
# some other stuff
def main():
for _ in range(epochs):
train(epochs, model, optimizer, loss_fn)
restults = test(model, loss_fn)
timer = get_global_timer()
with timer.clock("logging"):
logger.log(results)
logger.log(timer.stats())
Submodules#
Package Contents#
Functions#
Get the global |
|
Sets the global |
|
|
Set the global |
|
Decorator to time a function call. |
Attributes#
The global |
|
The package version string. |
- torch_simple_timing.get_global_timer()[source]#
Get the global
Timerinstance.- Returns:
The global timer instance.
- Return type:
- torch_simple_timing.reset_global_timer()[source]#
Sets the global
Timerinstance to a new one.- Return type:
None
- torch_simple_timing.set_global_timer(timer)[source]#
Set the global
Timerinstance to a user-provided new one.- Parameters:
timer (
Timer) – The new timer instance to use globally.- Return type:
None
- torch_simple_timing.timeit(name=None, gpu=False, timer=None)[source]#
Decorator to time a function call. Example:
from torch_simple_timing import timeit, get_global_timer, reset_global_timer import torch device = torch.device("cuda" if torch.cuda.is_available() else "cpu") gpu = device.type == "cuda" # Use the function name as the timer name @timeit(gpu=gpu) def train(): x = torch.rand(1000, 1000, device=) 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() stats = timer.stats() print(timer.display(stats=stats)) reset_global_timer()
- Parameters:
name (str, optional) – The name of the timer to use. Defaults to the decorated function’s name.
gpu (bool, optional) – Whether to use GPU timing. Defaults to False.
timer (
Timer, optional) – The timer instance to use. Defaults toNone, in which case the global timer instance is used.