torch_simple_timing#

  1. I want to time operations once

    • That’s what a Clock is for

  2. I want to time the same operations multiple times

    • That’s what a Timer is for

In simple terms:

  • A Clock is an object (and context-manager) that

    will compute the elapsed 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

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.

Example of using timeit() to time a function (pseudo-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_global_timer()

Get the global Timer instance.

reset_global_timer()

Sets the global Timer instance to a new one.

set_global_timer(timer)

Set the global Timer instance

timeit([name, gpu, timer])

Decorator to time a function call.

Attributes#

TIMER

The global Timer instance

___version__

The package version string.

torch_simple_timing.TIMER[source]#

The global Timer instance used by timeit() to time functions.

torch_simple_timing.___version__[source]#

The package version string.

torch_simple_timing.get_global_timer()[source]#

Get the global Timer instance.

Returns:

The global timer instance.

Return type:

Timer

torch_simple_timing.reset_global_timer()[source]#

Sets the global Timer instance to a new one.

Return type:

None

torch_simple_timing.set_global_timer(timer)[source]#

Set the global Timer instance 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 to None, in which case the global timer instance is used.