ytm.utils.Profiler#

class ytm.utils.Profiler(poll_rate: int = 100, cuda_id: int | None = None)#

Bases: object

Context manager for profiling CPU and GPU memory usage over time.

Samples RAM (and optionally VRAM) at a fixed poll rate while the block executes. Call get_profile() or summary() after the block to inspect results. Requires pynvml for GPU metrics.

RAM is the process RSS. VRAM is the per-process memory reported by NVML, which includes the CUDA context and any memory pool, not only arrays in use.

Parameters:
poll_rateint, default=100

Sampling frequency in milliseconds.

cuda_idint, optional

Index of the CUDA device to monitor. If None, auto-detects the device used by the current process.

Examples

>>> with Profiler() as p:
...     run_experiment()
>>> p.summary()
property elapsed: float#

Elapsed time in seconds. Must be accessed after the context exits.

Returns:
float

Wall-clock seconds between __enter__ and __exit__.

Raises:
RuntimeError

If accessed before the context has started or exited.

get_profile() → Profile#

Return a Profile snapshot after the context exits.

Returns:
Profile

Frozen dataclass with elapsed time and RAM/VRAM statistics.

Raises:
RuntimeError

If called before the context has exited.

summary() → None#

Print a formatted summary table of the profile to stdout.