ag_tape_memory: Report what the gradient tape is holding

View source: R/ag_tape_memory.R

ag_tape_memoryR Documentation

Report what the gradient tape is holding

Description

Walks the current tape and reports how many nodes it holds and how much memory those nodes keep alive, split into three categories: the operand tensors, which outlive the tape; the activations captured by the backward closures, which clearing the tape would release; and any extras recorded for the graph backward path beyond those.

Usage

ag_tape_memory(top = 10L, quiet = FALSE)

Arguments

top

Number of operations to list in the per-op breakdown, most expensive first. Set to 0 for totals only.

quiet

Suppress the printed report and return the figures only. For callers that consume the numbers – tests, scripts that format their own table – rather than read them.

Details

Storage shared between categories is counted once, and charged to the longest-lived one. That matters for reading the result: a weight's matrix and the a_snap an ag_matmul closure captured are the same object, so it is reported as an operand. The activation figure is therefore what clearing the tape would actually free, not the sum of everything the closures reference.

The tape lives from the start of with_grad_tape until zero_grad() clears it, which happens after the optimizer step. Call this between backward and zero_grad() to see the peak.

Value

Invisibly, a list with nodes, bytes_total, bytes_snapshots, bytes_fields, bytes_inputs and a data frame by_op. Printed as a report as a side effect.

Examples


w <- ag_param(matrix(runif(64), 8, 8))
x <- ag_tensor(matrix(runif(64), 8, 8))
with_grad_tape({
  h    <- ag_relu(ag_matmul(w, x))
  loss <- ag_mse_loss(h, matrix(0, 8, 8))
})
backward(loss)
ag_tape_memory()


ggmlR documentation built on Oct. 23, 2026, 1:07 a.m.