The main array object. Its type is determined by a data type and a shape.
Usage
nv_array(
data,
dtype = NULL,
device = NULL,
shape = NULL,
ambiguous = NULL,
backend = NULL,
byrow = FALSE,
check = FALSE
)
nv_scalar(
data,
dtype = NULL,
device = NULL,
ambiguous = NULL,
backend = NULL,
check = FALSE
)
nv_matrix(
data,
nrow = NULL,
ncol = NULL,
dtype = NULL,
device = NULL,
ambiguous = NULL,
backend = NULL,
byrow = FALSE
)
nv_empty(dtype, shape, device = NULL, ambiguous = FALSE, backend = NULL)
nv_array_like(
like,
data,
dtype = NULL,
device = NULL,
shape = NULL,
ambiguous = NULL,
backend = NULL
)
nv_scalar_like(
like,
data,
dtype = NULL,
device = NULL,
ambiguous = NULL,
backend = NULL
)
nv_empty_like(
like,
dtype = NULL,
shape = NULL,
device = NULL,
ambiguous = NULL
)Arguments
- data
(any)
integer(),double(), orlogical()scalar, vector, or array.- dtype
(
NULL|character(1)|DataType)
One of bool, i8, i16, i32, i64, ui8, ui16, ui32, ui64, f32, f64 or atengen::DataType. The default (NULL) uses the current backend's default dtype:f32for numeric data on"pjrt",f64for numeric data on"quickr",i32for integer data, andboolfor logical data.- device
(
NULL|character(1)| device)
The device the data lives on, given either as:a device string naming the platform (e.g.
"cpu","cuda","cuda:<n>"), which is resolved against the backend in use, ora device object as returned by
nv_device(): aPJRTDevicefor the"pjrt"backend or aquickr_devicefor the"quickr"backend. Because a device object is backend-specific, it also determines the backend.
The default (
NULL) usesdefault_device(): the CPU, or the platform named by thePJRT_PLATFORMenvironment variable on the"pjrt"backend.- shape
(
NULL|integer())
The output shape of the array. The default (NULL) is to infer it from the data if possible. Note thatnv_arrayinterprets length 1 vectors as having shape(1). To create a "scalar" with no axes (shape()), usenv_scalaror explicitly specifyshape = c().- ambiguous
(
NULL|logical(1))
Whether the dtype should be marked as ambiguous. Defaults toFALSEfor new arrays.- backend
(
NULL|character(1))
Backend the array belongs to ("pjrt"or"quickr"). The default (NULL) is inferred fromdevicewhendeviceis a backend-specific device object, and otherwise falls back todefault_backend(). Must not be specified insidejit().- byrow
(
logical(1))
When constructing from an R object and the result has at least two axes, fill the array in row-major order rather than the default column-major order, mirroringbase::matrix()'sbyrow. Only allowed whendatais an R object — passing an existingAnvlArraytogether withbyrow = TRUEis an error.- check
(
logical(1))
IfTRUE, error whendatacontains anyNAvalues. XLA has no representation for missing values, so they are otherwise silently coerced to the closest available value of the target dtype (e.g.NaNfor floats, the bit pattern-2147483648fori32,TRUEforbool). Defaults toFALSE. See the "Gotchas" vignette.- nrow
(
NULL|integer(1))
Number of rows. Inferred fromncoland the data length ifNULL. Defaults to1whendatais a scalar.- ncol
(
NULL|integer(1))
Number of columns. Inferred fromnrowand the data length ifNULL. Defaults to1whendatais a scalar.- like
(
AnvlArray)
An existing array. Any ofdtype,device,shape,ambiguous, andbackendthat areNULL(the default) are taken fromlike.
Terminology
An array's axes are the indices that identify its directions, numbered
1, 2, 3, ... The size of an axis (its axis size) is the extent
along that axis, and the shape is the vector of all axis sizes. For
example, nv_array(1:6, shape = c(2, 3)) has two axes; the size of axis 1
is 2 and the size of axis 2 is 3, so its shape is c(2, 3). Use
naxes() for the number of axes and
shape() for the axis sizes. We speak of the size of an
axis rather than an array's "dimensions", as the latter is generally
overloaded as it is used to refer to both the axis and it's size.
Extractors
The following generic functions can be used to extract information from an AnvlArray:
dtype(): Get the data type of the array.shape(): Get the shape (axis sizes) of the array.naxes(): Get the number of axes.device(): Get the device of the array.platform(): Get the platform (e.g."cpu","cuda").ambiguous(): Get whether the dtype is ambiguous.
Serialization
Arrays can be serialized to and from the safetensors format:
nv_serialize()/nv_unserialize(): Serialize/deserialize arrays to/from raw vectors.
Backend
An AnvlArray is backend-dependent: it belongs to exactly one backend
("pjrt" or the experimental "quickr") and lives on a device of that backend.
The supported data types and devices differ between backends.
Examples
# A 1-d array (vector) with shape (4). Default type for integers is `i32`
nv_array(1:4)
#> AnvlArray
#> 1
#> 2
#> 3
#> 4
#> [ CPUi32{4} ]
# Specify a dtype
nv_array(c(1.5, 2.5, 3.5), dtype = "f64")
#> AnvlArray
#> 1.5000
#> 2.5000
#> 3.5000
#> [ CPUf64{3} ]
# A 2x3 matrix
nv_array(1:6, shape = c(2L, 3L))
#> AnvlArray
#> 1 3 5
#> 2 4 6
#> [ CPUi32{2,3} ]
# A 2x3 matrix filled by row, like `matrix(1:6, 2, 3, byrow = TRUE)`.
nv_array(1:6, shape = c(2L, 3L), byrow = TRUE)
#> AnvlArray
#> 1 2 3
#> 4 5 6
#> [ CPUi32{2,3} ]
# A scalar array.
nv_scalar(3.14)
#> AnvlArray
#> 3.1400
#> [ CPUf32{} ]
# An uninitialized 2x3 array (contents are unspecified)
nv_empty("f32", shape = c(2L, 3L))
#> AnvlArray
#> 2.1019e-44 2.2421e-44 2.6625e-44
#> 2.9427e-44 3.0829e-44 3.0852e-41
#> [ CPUf32{2,3} ]
# --- Extractors ---
x <- nv_array(1:6, shape = c(2L, 3L))
dtype(x)
#> <i32>
shape(x)
#> [1] 2 3
naxes(x)
#> [1] 2
device(x)
#> <CpuDevice(id=0)>
platform(x)
#> [1] "cpu"
ambiguous(x)
#> [1] FALSE
# --- Transforming arrays with jit ---
add_one <- jit(function(x) x + 1)
add_one(nv_array(1:4))
#> AnvlArray
#> 2
#> 3
#> 4
#> 5
#> [ CPUf32?{4} ]
# --- Eager mode (calling operations directly) ---
nv_add(nv_array(1:3), nv_array(4:6))
#> AnvlArray
#> 5
#> 7
#> 9
#> [ CPUi32{3} ]