ag_load_model: Load an autograd module from a saved state

View source: R/ag_save.R

ag_load_modelR Documentation

Load an autograd module from a saved state

Description

Reconstructs an ag_* module saved with ag_save_model. The architecture is rebuilt by calling model_fn (either the one passed here, or the one stored inside the container at save time), and the saved parameter and buffer values are copied back by name.

Usage

ag_load_model(path, model_fn = NULL, device = NULL, dtype = NULL)

Arguments

path

File path written by ag_save_model.

model_fn

Optional zero-argument rebuild function. Required if no model_fn was stored at save time. If both are present, this argument takes precedence.

device

Optional device for the rebuilt module ("cpu" or "gpu"). If NULL (default), the current ag_device() is used by the rebuild.

dtype

Optional GPU upload precision for the restored parameters ("f32", "f16", or "bf16"). If NULL (default), the dtype recorded in the file is restored, so a module saved under ag_dtype("f16") keeps computing in f16 regardless of the loading session's default. Pass an explicit value to override the file. Files written before dtypes were recorded fall back to whatever model_fn builds.

Note that this is an upload/compute precision, not a storage precision: parameter values always live in $data as full-precision R numeric matrices (that is what backward needs), and dtype only controls the precision they are uploaded to the GPU with. Saving therefore never rounds the weights, whatever dtype is in effect.

Value

The reconstructed module with restored weights, in eval mode.

See Also

ag_save_model

Examples


build <- function() ag_sequential(ag_linear(4L, 8L), ag_linear(8L, 3L))
f <- tempfile(fileext = ".rds")
ag_save_model(build(), f, model_fn = build)
model <- ag_load_model(f)


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