Skip to contents

nv_serialize() serializes a named list of arrays into the safetensors format, and nv_unserialize() deserializes them. The data type and shape of each array are restored.

Usage

nv_serialize(arrays, con = NULL)

nv_unserialize(con, device = NULL)

Arguments

arrays

(named list of AnvlArray)
Named list of arrays to serialize. Names must be unique.

con

(NULL | connection | raw)
For nv_serialize(), an optional connection to write to; if NULL (default), a raw vector is returned. For nv_unserialize(), a connection or raw vector to read from.

device

(NULL | character(1) | PJRTDevice)
The device on which to place the loaded arrays ("cpu", "cuda", ...) when the active backend is "pjrt", defaulting to default_device().

Value

nv_serialize(): (raw | NULL)
A raw vector if con is NULL, otherwise NULL invisibly.

nv_unserialize(): (named list of AnvlArray)

See also

Examples

x <- nv_matrix(1:6, nrow = 2)
x
#> AnvlArray
#>  1 3 5
#>  2 4 6
#> [ CPUi32{2,3} ] 
raw_data <- nv_serialize(list(x = x))
raw_data
#>  [1] 39 00 00 00 00 00 00 00 7b 22 78 22 3a 7b 22 73 68 61 70 65 22 3a 5b 32 2c
#> [26] 33 5d 2c 22 64 74 79 70 65 22 3a 22 49 33 32 22 2c 22 64 61 74 61 5f 6f 66
#> [51] 66 73 65 74 73 22 3a 5b 30 2c 32 34 5d 7d 7d 01 00 00 00 03 00 00 00 05 00
#> [76] 00 00 02 00 00 00 04 00 00 00 06 00 00 00
nv_unserialize(raw_data)
#> $x
#> AnvlArray
#>  1 3 5
#>  2 4 6
#> [ CPUi32{2,3} ] 
#>