ag_backward_resident: Keep backward gradients on the device

View source: R/ag_backward_graph.R

ag_backward_residentR Documentation

Keep backward gradients on the device

Description

Component 3 of the resident contract. When on, the graph backward leaves each leaf gradient in its backend buffer and puts a device handle in $grad instead of an R matrix; the numbers come back when something actually reads them. That removes the two largest stages of a backward pass – the gradient download and the leaf install, together 43-64 (inst/scripts/measure_ag_residency_on_backward.R).

Usage

ag_backward_resident(on = TRUE)

Arguments

on

'TRUE' to keep gradients resident, 'FALSE' to download them as before, 'NA' to only query the current state.

Details

On by default; GGMLR_AG_RESIDENT_GRADS=0 or FALSE here restores host-side gradients. It changes what $grad contains: code reading it through .ag_data/.ag_as_matrix is unaffected, while code doing arithmetic on $grad directly gets an ag_handle, which has no arithmetic methods and so fails loudly rather than computing something wrong.

Value

The previous state, invisibly.


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