Skip to content
class

MemoryTracker

MemoryTracker

Atomic per-device allocation counter bank.

All counters live in static storage, one bank per Device. Updates use std::atomic so track_alloc / track_free can be invoked from any thread without external locking. Peak tracking uses a compare-exchange loop, avoiding a mutex while still guaranteeing a monotonically non-decreasing peak.

Notes

All public methods are static — the class is effectively a namespace over the two global counter banks. There is no instance state.

Thread Safety

Counter updates are atomic. Snapshots returned by get_stats are eventually consistent — a get_stats call during concurrent allocation may observe current_bytes slightly behind the true total or a peak that lags the true maximum by one CAS cycle.

Static methods

8
static

counters_for

→Counters&
Counters & counters_for(Device device)

Returns the live Counters bank for device.

Implementation detail — the two banks live in static storage inside the .cpp file's anonymous namespace.

Parameters

deviceDevice
Device whose counter bank should be returned.

Returns

Counters&

Reference to the static bank for the requested device.

static

get_stats

→MemoryStats
MemoryStats get_stats(Device device)

Returns a coherent-looking snapshot of one device's counters.

Each field is read separately with memory_order_relaxed, so the four values may not correspond to the same instant under concurrent activity. Sufficient for diagnostics; not safe to base correctness invariants on.

Parameters

deviceDevice
Device whose counters should be sampled.

Returns

MemoryStats

Value-typed snapshot disconnected from the live atomics.

static

host_sync_bytes

int host_sync_bytes()

Lifetime bytes read by the host, per track_host_sync.

static

host_sync_count

int host_sync_count()

Lifetime count of host reads recorded by track_host_sync.

static

reset_peak

void reset_peak(Device device)

Resets peak_bytes to the current current_bytes value.

Useful at the start of a profiling window — after this call, peak_bytes reports the maximum live byte total reached since the reset rather than since process start.

Parameters

deviceDevice
Device whose peak counter should be cleared.

Notes

Mirrors the reference framework's reset_peak_memory_stats for API parity.

static

track_alloc

void track_alloc(int nbytes, Device device)

Records an allocation of nbytes on device.

Adds nbytes to current_bytes, increments alloc_count, and — if the new current_bytes exceeds the previous peak — advances peak_bytes via a relaxed compare-exchange loop.

Parameters

nbytesstd::size_t
Size of the allocation just performed.
deviceDevice
Device whose counter bank should be updated.

Notes

Called from the allocator's deleter; not for direct application use.

static

track_free

void track_free(int nbytes, Device device)

Records a deallocation of nbytes on device.

Subtracts nbytes from current_bytes and increments free_count. Does not touch peak_bytes — peaks are historical, by definition.

Parameters

nbytesstd::size_t
Size of the block being freed. Must match the value passed to the corresponding track_alloc.
deviceDevice
Device whose counter bank should be updated.
static

track_host_sync

void track_host_sync(int nbytes)

Record one read of an evaluated engine array by the host.

Every such read waits for the lazy graph behind the array to finish and copies its bytes out — a pipeline stall on Metal, and the cost a training step's allocation counts cannot see: a loop that reads a loss with .item() every step allocates nothing extra and still serialises the GPU. Called from each place the engine hands device memory to the host (download_gpu_to_cpu, the CPU-side fallbacks for data-dependent and linear-algebra ops, item).

Parameters

nbytesstd::size_t
Bytes read.