Skip to content

chuchichaestli.utils.memory

Measuring the memory a device has allocated.

Functions:

Name Description
peak_memory_allocated

Return a device's peak allocated bytes since its reset, or None if unmeasured.

reset_peak_memory

Start a device's peak-memory counter afresh, if it keeps one.

peak_memory_allocated

peak_memory_allocated(
    device: torch.device | str,
) -> int | None

Return a device's peak allocated bytes since its reset, or None if unmeasured.

Parameters:

Name Type Description Default
device torch.device | str

The device to measure.

required
Source code in src/chuchichaestli/utils/memory.py
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def peak_memory_allocated(device: torch.device | str) -> int | None:
    """Return a device's peak allocated bytes since its reset, or `None` if unmeasured.

    Args:
        device: The device to measure.
    """
    device = torch.device(device)
    counters = _counters(device)
    if counters is None:
        return None
    try:
        return int(counters.max_memory_allocated(device))
    except Exception:  # noqa: BLE001 - a device refusing has no peak to report
        return None

reset_peak_memory

reset_peak_memory(device: torch.device | str) -> bool

Start a device's peak-memory counter afresh, if it keeps one.

Parameters:

Name Type Description Default
device torch.device | str

The device to measure.

required
Source code in src/chuchichaestli/utils/memory.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
def reset_peak_memory(device: torch.device | str) -> bool:
    """Start a device's peak-memory counter afresh, if it keeps one.

    Args:
        device: The device to measure.
    """
    device = torch.device(device)
    counters = _counters(device)
    if counters is None:
        return False
    try:
        counters.reset_peak_memory_stats(device)
    except Exception:  # noqa: BLE001 - a device refusing has no counter to reset
        return False
    return True