tprofiler.time.wrapper

Timer decorators and context managers for distributed PyTorch applications.

This module provides convenient decorators and context managers for timing function execution and code blocks. It integrates with the TimeManager system to enable seamless timing across distributed environments.

The main components include:

  • timer_wrap: Decorator for automatically timing function execution

  • timer: Context manager for timing arbitrary code blocks

timer_wrap

tprofiler.time.wrapper.timer_wrap(name: str | None = None)[source]

Decorator that automatically times function execution using the active timer stack.

This decorator wraps a function to measure its execution time and record it using all active TimeManager instances in the timer stack. The timing name defaults to the function’s name if not specified.

Parameters:

name (Optional[str]) – Custom name for the timing measurement. If None, uses function name.

Returns:

Decorator function that wraps the target function with timing.

Return type:

callable

Example:

>>> @timer_wrap('my_function')
... def slow_function():
...     import time
...     time.sleep(0.1)
...     return "done"
>>> 
>>> # Or with default name
>>> @timer_wrap()
... def another_function():
...     pass

timer

tprofiler.time.wrapper.timer(name: str)[source]

Context manager for timing code execution using the active timer stack.

This context manager measures the execution time of the code block within it and records the timing using all active TimeManager instances in the current timer stack. This provides a convenient way to time arbitrary code sections without needing direct access to a TimeManager instance.

Parameters:

name (str) – Name to associate with this timing measurement.

Yields:

None

Example:

>>> with timer('data_loading'):
...     # Load and process data
...     data = load_large_dataset()
...     processed_data = preprocess(data)
>>> 
>>> with timer('model_forward'):
...     output = model(input_tensor)