| Title: | Accelerated Array Computing and Automatic Differentiation |
|---|---|
| Description: | Accelerated array computing and code transformations for R. Numerical programs operating on multi-dimensional arrays can be just-in-time compiled to optimized executables via 'XLA' -- the same compiler that powers 'JAX' and 'TensorFlow' -- and run on CPU or NVIDIA GPU from the same source. Also provides reverse-mode automatic differentiation, returning the gradient of a function as another R function. |
| Authors: | Sebastian Fischer [cre, aut] (ORCID: <https://orcid.org/0000-0002-9609-3197>), Daniel Falbel [aut] (ORCID: <https://orcid.org/0009-0006-0143-2392>), Tomasz Kalinowski [aut], Nikolai German [aut] (ORCID: <https://orcid.org/0009-0001-7394-8367>) |
| Maintainer: | Sebastian Fischer <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-06-03 13:25:53 UTC |
| Source: | https://github.com/r-xla/anvl |
Code transformation framework for R.
The anvl package itself is MIT-licensed. The CUDA backend dynamically
loads NVIDIA software which is not bundled with anvl, but downloaded
from NVIDIA's official redistributable channels by the CUDA toolkit R
package (e.g. cuda12.8) at install time. Its use is governed by the
NVIDIA CUDA Toolkit EULA, with the
exception of cuDNN, which is covered by the
NVIDIA cuDNN SLA,
and NCCL, which is covered by its own license.
By installing or using the CUDA backend you accept those terms.
Maintainer: Sebastian Fischer [email protected] (ORCID)
Authors:
Daniel Falbel [email protected] (ORCID)
Tomasz Kalinowski [email protected]
Nikolai German [email protected] (ORCID)
Useful links:
Report bugs at https://github.com/r-xla/anvl/issues
Get the current graph being built (via local_descriptor).
.current_descriptor(silent = FALSE).current_descriptor(silent = FALSE)
silent |
( |
A GraphDescriptor object.
Extracts a subset from an array. You can also use the [ operator.
Supports R-style indexing including scalar indices (which drop dimensions),
ranges (a:b), and array(c(...)) for selecting multiple elements along a
dimension.
## S3 method for class 'AnvlArray' x[...] nv_subset(operand, ...)## S3 method for class 'AnvlArray' x[...] nv_subset(operand, ...)
x |
( |
... |
Subset specifications, one per dimension. Omitted trailing
dimensions select all elements. See |
operand |
( |
nv_subset_assign() for updating subsets, vignette("subsetting")
for a comprehensive guide.
x <- nv_matrix(1:12, nrow = 3) x # Select row 2 x[2, ] # Select rows 1 to 2, all columns x[1:2, ]x <- nv_matrix(1:12, nrow = 3) x # Select row 2 x[2, ] # Select rows 1 to 2, all columns x[1:2, ]
Updates elements of an array at specified positions, returning a new array.
You can also use the [<- operator.
## S3 replacement method for class 'AnvlArray' x[...] <- value nv_subset_assign(operand, ..., value)## S3 replacement method for class 'AnvlArray' x[...] <- value nv_subset_assign(operand, ..., value)
x |
( |
... |
Subset specifications, one per dimension. See
|
value |
( |
operand |
( |
arrayish
A new array with the same shape as operand and the subset replaced.
nv_subset(), vignette("subsetting") for a comprehensive guide.
x <- nv_matrix(1:12, nrow = 3) # Set row 1 to zeros x[1, ] <- nv_scalar(0L) xx <- nv_matrix(1:12, nrow = 3) # Set row 1 to zeros x[1, ] <- nv_scalar(0L) x
Calls the extractor after converting the input to an AbstractArray.
shape_abstract(x) ndims_abstract(x) dtype_abstract(x) ambiguous_abstract(x)shape_abstract(x) ndims_abstract(x) dtype_abstract(x) ambiguous_abstract(x)
x |
( |
Returns whether the array's dtype is ambiguous.
ambiguous(x, ...)ambiguous(x, ...)
x |
An array object |
... |
Additional arguments (unused) |
logical(1) - TRUE if the dtype is ambiguous, FALSE otherwise
The main array object. Its type is determined by a data type and a shape.
nv_array( data, dtype = NULL, device = NULL, shape = NULL, ambiguous = NULL, backend = NULL, byrow = FALSE ) nv_scalar(data, dtype = NULL, device = NULL, ambiguous = NULL, backend = NULL) 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) 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 )nv_array( data, dtype = NULL, device = NULL, shape = NULL, ambiguous = NULL, backend = NULL, byrow = FALSE ) nv_scalar(data, dtype = NULL, device = NULL, ambiguous = NULL, backend = NULL) 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) 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 )
data |
(any) |
dtype |
( |
device |
( |
shape |
( |
ambiguous |
( |
backend |
( |
byrow |
( |
nrow |
( |
ncol |
( |
like |
( |
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 (dimensions) of the array.
ndims(): Get the number of dimensions.
device(): Get the device of the array.
platform(): Get the platform (e.g. "cpu", "cuda").
ambiguous(): Get whether the dtype is ambiguous.
Arrays can be serialized to and from the safetensors format:
nv_serialize() / nv_unserialize():
Serialize/deserialize arrays to/from raw vectors.
nv_fill, nv_iota, nv_seq, as_array, nv_serialize
# A 1-d array (vector) with shape (4). Default type for integers is `i32` nv_array(1:4) # Specify a dtype nv_array(c(1.5, 2.5, 3.5), dtype = "f64") # A 2x3 matrix nv_array(1:6, shape = c(2L, 3L)) # A 2x3 matrix filled by row, like `matrix(1:6, 2, 3, byrow = TRUE)`. nv_array(1:6, shape = c(2L, 3L), byrow = TRUE) # A scalar array. nv_scalar(3.14) # A 0x3 array nv_empty("f32", shape = c(0L, 3L)) # --- Extractors --- x <- nv_array(1:6, shape = c(2L, 3L)) dtype(x) shape(x) ndims(x) device(x) platform(x) ambiguous(x) # --- Transforming arrays with jit --- add_one <- jit(function(x) x + 1) add_one(nv_array(1:4)) # --- Eager mode (calling operations directly) --- nv_add(nv_array(1:3), nv_array(4:6))# A 1-d array (vector) with shape (4). Default type for integers is `i32` nv_array(1:4) # Specify a dtype nv_array(c(1.5, 2.5, 3.5), dtype = "f64") # A 2x3 matrix nv_array(1:6, shape = c(2L, 3L)) # A 2x3 matrix filled by row, like `matrix(1:6, 2, 3, byrow = TRUE)`. nv_array(1:6, shape = c(2L, 3L), byrow = TRUE) # A scalar array. nv_scalar(3.14) # A 0x3 array nv_empty("f32", shape = c(0L, 3L)) # --- Extractors --- x <- nv_array(1:6, shape = c(2L, 3L)) dtype(x) shape(x) ndims(x) device(x) platform(x) ambiguous(x) # --- Transforming arrays with jit --- add_one <- jit(function(x) x + 1) add_one(nv_array(1:4)) # --- Eager mode (calling operations directly) --- nv_add(nv_array(1:3), nv_array(4:6))
Constructs the quickr backend, which stores array data as plain R arrays and compiles jitted functions to R code via the quickr package.
AnvlBackendQuickr()AnvlBackendQuickr()
To use it, the "quickr" package needs to be installed.
Registered automatically under the name "quickr" when the package is
loaded; call local_backend("quickr") or
with_backend("quickr", ...) to use it. Requires the
quickr package to be installed.
An AnvlBackend object with subclass "AnvlBackendQuickr".
An AnvlArray with backend = "quickr" is, under the hood, a plain R
vector or array (numeric, integer, or logical) stored in the $data
field. as_array() returns the underlying vector/array directly without
copying, and nv_array() simply wraps an R vector/array. As a
consequence, there is no separate notion of a device: data always lives in
R's memory and computation always runs on the CPU.
This backend is experimental and has a number of limitations:
Compilation (tracing + quickr lowering) is somewhat slow, so it is best suited to long-running or repeatedly-called functions where the one-time compilation cost is amortized.
Only a subset of the primitives that the XLA backend supports are currently
lowered to quickr code. See vignette("primitives") for an overview.
Only the data types f64, i32, and bool are supported.
Only CPU execution is supported.
unwrap (logical(1), default FALSE): if TRUE, the compiled function
returns plain R arrays instead of AnvlArrays. Useful when the jitted
function's output is consumed by non-anvl R code and the extra wrapping
would only get stripped again.
AnvlBackend(), AnvlBackendXla(), local_backend(), jit().
Constructs the XLA backend, which stores array data in PJRT buffers (via
pjrt::pjrt_buffer()) and compiles jitted functions to XLA executables
via stablehlo() and pjrt::pjrt_compile(). This is the default
backend.
AnvlBackendXla()AnvlBackendXla()
An AnvlBackend object with subclass "AnvlBackendXla".
An AnvlArray with backend = "xla" wraps a pjrt::pjrt_buffer()
stored in the $data field. The buffer owns the memory holding the tensor
values and may live on any device supported by PJRT (CPU, CUDA, Metal,
...). Calling as_array() transfers the buffer contents back to an R
array; calling nv_array() on an R object uploads it to the requested
device.
Each AnvlArray therefore has an associated device, queryable via
device(). A device is a pjrt::as_pjrt_device() object (e.g. the
platform "cpu" or "cuda", optionally with an index such as "cuda:1").
When device is NULL in nv_array() or the jit() wrapper, the
device defaults to the PJRT_PLATFORM environment variable (falling back
to "cpu"), or is inferred from the existing inputs of a jitted call.
Operations require all inputs to live on the same device.
donate (character(), default character()): names of arguments whose
underlying buffers may be donated to (i.e., reused/consumed by) the
compiled XLA executable. Donated buffers must not be used again by the
caller after the call; this can reduce memory usage and copies for large
inputs. Must not overlap with static.
AnvlBackend(), AnvlBackendQuickr(), local_backend(), jit().
Computational graph consisting exclusively of primitive calls. This is a mutable class.
AnvlGraph( calls = list(), in_tree = NULL, out_tree = NULL, inputs = list(), outputs = list(), constants = list(), is_static_flat = NULL, static_args_flat = NULL )AnvlGraph( calls = list(), in_tree = NULL, out_tree = NULL, inputs = list(), outputs = list(), constants = list(), is_static_flat = NULL, static_args_flat = NULL )
calls |
( |
in_tree |
( |
out_tree |
( |
inputs |
( |
outputs |
( |
constants |
( |
is_static_flat |
( |
static_args_flat |
( |
(AnvlGraph)
Primitive interpretation rule.
Note that [[ and [[<- access the interpretation rules.
To access other fields, use $ and $<-.
A primitive is considered higher-order if it contains subgraphs.
AnvlPrimitive(name, subgraphs = character())AnvlPrimitive(name, subgraphs = character())
name |
( |
subgraphs |
( |
(AnvlPrimitive)
Create an R array without having to wrap data in c()
arr(..., shape = NULL)arr(..., shape = NULL)
... |
(any) |
shape |
( |
arr(1, 2, 3) arr(1, 2, 3, 4, shape = c(2, 2))arr(1, 2, 3) arr(1, 2, 3, 4, shape = c(2, 2))
A arrayish value is any object that can be input to a primitive such as prim_add.
During runtime of a JIT-compiled function, these are AnvlArray objects.
The following types are arrayish (during tracing):
AnvlArray: a concrete array holding data on a device.
GraphBox: a boxed abstract array representing a value in a graph.
Length-1 vectors: numeric(1) and logical(1)
R arrays of types: numeric and logical.
Use is_arrayish() to check whether a value is arrayish.
is_arrayish(x, convert_ok = TRUE)is_arrayish(x, convert_ok = TRUE)
x |
( |
convert_ok |
( |
logical(1)
# AnvlArrays are arrayish is_arrayish(nv_array(1:4)) # Scalar R literals are arrayish by default is_arrayish(1.5) # R arrays are arrayish by default is_arrayish(array(1.5)) # R arrays is_arrayish(array(1:4), convert_ok = TRUE) is_arrayish(array(1:4), convert_ok = FALSE) # Length 1 vectors is_arrayish(1.5, convert_ok = FALSE) is_arrayish(1.5, convert_ok = TRUE)# AnvlArrays are arrayish is_arrayish(nv_array(1:4)) # Scalar R literals are arrayish by default is_arrayish(1.5) # R arrays are arrayish by default is_arrayish(array(1.5)) # R arrays is_arrayish(array(1:4), convert_ok = TRUE) is_arrayish(array(1:4), convert_ok = FALSE) # Length 1 vectors is_arrayish(1.5, convert_ok = FALSE) is_arrayish(1.5, convert_ok = TRUE)
Use this to canonicalize inputs at the start of a function so it works
both with eager executing and in combination with jit().
Use as_anvl_array() for a single input and as_anvl_arrays() for multiple inputs.
The latter will also ensure all arrays are from the same backend and live on the same device.
as_anvl_array(x, device = NULL) as_anvl_arrays(...)as_anvl_array(x, device = NULL) as_anvl_arrays(...)
x |
( |
device |
( |
... |
( |
During tracing, boxes are returned as is.
During eager mode, R literals and arrays are converted to AnvlArrays on the specified device.
For AnvlArray inputs, we check that they live on provided device (if specified).
(AnvlArray for as_anvl_array(), list of AnvlArrays
for as_anvl_arrays()).
as_anvl_array(1L) as_anvl_arrays(nv_array(1:3), 1L)as_anvl_array(1L) as_anvl_arrays(nv_array(1:3), 1L)
Transfers array data to R and returns it as an R array.
Only in the case of scalars is the result a vector of length 1, as R arrays cannot have 0 dimensions.
as_array(x, ...)as_array(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
This is implemented via the generic tengen::as_array().
An R array or vector of length 1.
x <- nv_array(1:4, dtype = "f32") as_array(x) y <- nv_scalar(1L) # R arrays can't have 0 dimensions: as_array(y)x <- nv_array(1:4, dtype = "f32") as_array(x) y <- nv_scalar(1L) # R arrays can't have 0 dimensions: as_array(y)
Coerces a value to a DataType. Accepts data type strings
(e.g. "f32", "i64", "bool") or existing DataType objects (they are returned unchanged).
as_dtype(x)as_dtype(x)
x |
A character string or |
This is implemented via the generic tengen::as_dtype().
A DataType object.
is_dtype(), tengen::as_dtype(), tengen::DataType
as_dtype("f32") as_dtype("i32")as_dtype("f32") as_dtype("i32")
Returns the underlying bytes of an array as a raw vector.
as_raw(x, ...)as_raw(x, ...)
x |
( |
... |
Additional arguments passed to method:
|
This is implemented via the generic tengen::as_raw().
A raw vector.
x <- nv_array(1:4, shape = c(2, 2), dtype = "f32") as_raw(x, row_major = TRUE) as_raw(x, row_major = FALSE)x <- nv_array(1:4, shape = c(2, 2), dtype = "f32") as_raw(x, row_major = TRUE) as_raw(x, row_major = FALSE)
Convert an AnvlArray to a bare R vector.
The array's shape is discarded; the result is always a flat vector.
Each method requires a compatible dtype:
as.double() / as.numeric(): float or (signed/unsigned) integer dtypes.
as.integer(): signed or unsigned integer dtypes.
as.logical(): bool.
as.vector(): any dtype; the R type is chosen by the dtype, or
forced via mode (e.g. "integer", "double", "logical", "list").
Use as_array() to obtain an R array that preserves the shape, or
nv_convert() to change the dtype of an AnvlArray before coercing.
## S3 method for class 'AnvlArray' as.double(x, ...) ## S3 method for class 'AnvlArray' as.integer(x, ...) ## S3 method for class 'AnvlArray' as.logical(x, ...) ## S3 method for class 'AnvlArray' as.vector(x, mode = "any")## S3 method for class 'AnvlArray' as.double(x, ...) ## S3 method for class 'AnvlArray' as.integer(x, ...) ## S3 method for class 'AnvlArray' as.logical(x, ...) ## S3 method for class 'AnvlArray' as.vector(x, mode = "any")
x |
( |
... |
Unused. |
mode |
( |
An R vector of the corresponding type (double, integer, or logical).
x <- nv_array(c(1.5, 2.5, 3.5, 4.5), shape = c(2L, 2L)) as.numeric(x) as.integer(nv_array(1:6, shape = c(2L, 3L))) as.logical(nv_array(c(TRUE, FALSE), dtype = "bool")) as.vector(x)x <- nv_array(c(1.5, 2.5, 3.5, 4.5), shape = c(2L, 2L)) as.numeric(x) as.integer(nv_array(1:6, shape = c(2L, 3L))) as.logical(nv_array(c(TRUE, FALSE), dtype = "bool")) as.vector(x)
Convert an AbstractArray to a ValueType.
at2vt(x)at2vt(x)
x |
( |
(ValueType)
Block until the array's underlying computation has finished, and return the object invisibly. Useful for benchmarking, where the dispatch of an asynchronous operation should not be confused with its execution.
await(x, ...)await(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
Implemented via the generic pjrt::await(). For backends without
asynchronous execution (e.g. "quickr"), this is a no-op.
x, invisibly.
pjrt::await(), map_tree() (to await a tree of outputs)
x <- nv_array(1:4, dtype = "f32") await(x) # Await all leaves of a (possibly nested) list of arrays. map_tree(list(x, list(y = x)), await)x <- nv_array(1:4, dtype = "f32") await(x) # Await all leaves of a (possibly nested) list of arrays. map_tree(list(x, list(y = x)), await)
Get Backend of an Array
backend(x, ...)backend(x, ...)
x |
An array object |
... |
Additional arguments (unused) |
character(1) - the backend name
Captures the nesting structure of an object as a tree of Nodes. Each leaf
in the input becomes a LeafNode with an integer index corresponding to its
position in the flat list produced by flatten(). Lists become ListNodes
that record child nodes and names. The resulting tree can be passed to
unflatten() to reconstruct the original structure from a flat list.
build_tree(x, counter = NULL)build_tree(x, counter = NULL)
x |
(any) |
counter |
(NULL | environment) |
A Node (LeafNode for scalars, ListNode for lists).
flatten(), unflatten(), tree_size(), reindex_tree()
x <- list(a = 1, b = list(c = 2, d = 3)) tree <- build_tree(x) tree_size(tree) flat <- flatten(x) unflatten(tree, flat)x <- list(a = 1, b = list(c = 2, d = 3)) tree <- build_tree(x) tree_size(tree) flat <- flatten(x) unflatten(tree, flat)
Computes the common dtype for a set of abstract types, respecting whether a type is ambiguous or not.
A type is ambiguous if it comes from a literal (like 1 or 1.0) or was promoted
to an ambiguous type.
Promoting to an ambiguous type can happen in scenarios like x + 1.2, where x is a bool or an int.
common_dtype( lhs_dtype, rhs_dtype, lhs_ambiguous = FALSE, rhs_ambiguous = FALSE )common_dtype( lhs_dtype, rhs_dtype, lhs_ambiguous = FALSE, rhs_ambiguous = FALSE )
lhs_dtype |
( |
rhs_dtype |
( |
lhs_ambiguous |
( |
rhs_ambiguous |
( |
(list(dtype = [tengen::DataType], ambiguous = logical(1)')
An AbstractArray that also holds a reference to the actual array data.
Usually represents a closed-over constant in a program.
Inherits from AbstractArray.
ConcreteArray(data)ConcreteArray(data)
data |
( |
When lowering to XLA, these become inputs to the executable instead of embedding them into programs as constants. This is to avoid increasing compilation time and bloating the size of the executable.
y <- nv_array(c(0.5, 0.6)) x <- ConcreteArray(y) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing graph <- trace_fn(function() y, list()) graph graph$outputs[[1]]$avaly <- nv_array(c(0.5, 0.6)) x <- ConcreteArray(y) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing graph <- trace_fn(function() y, list()) graph graph$outputs[[1]]$aval
Returns the target platform currently set by an enclosing stablehlo()
call (e.g. "cpu", "cuda"). Platform-aware lowering rules call this to
branch on the target — e.g. SVD switches to a layout-flip variant when
targeting CUDA with m < n because cuSOLVER's gesvd requires m >= n.
Returns NULL outside of a lowering call.
local_platform() sets the current platform for the duration of the
calling scope, restoring the previous value via withr::defer() when the
scope exits. Useful in tests and for manually exercising platform-aware
lowering rules outside of a stablehlo() call.
current_platform() local_platform(platform, envir = parent.frame())current_platform() local_platform(platform, envir = parent.frame())
platform |
( |
envir |
( |
current_platform() returns NULL or character(1).
local_platform() invisibly returns the previous platform.
Returns the current default backend from getOption("anvl.default_backend", "xla").
default_backend()default_backend()
character(1) — the backend name (e.g. "xla", "quickr").
Returns a device object for the default backend and platform.
For the "xla" backend, the platform is determined by the PJRT_PLATFORM
environment variable (defaulting to "cpu"). Other backends (e.g. "quickr")
only support CPU. The backend defaults to default_backend().
default_device(backend = NULL)default_device(backend = NULL)
backend |
( |
A backend-specific device object.
nv_device(), default_backend()
Returns the device on which an array is allocated.
device(x, ...)device(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
This is implemented via the generic tengen::device().
x <- nv_array(1:4, dtype = "f32") device(x)x <- nv_array(1:4, dtype = "f32") device(x)
Pass the result to jit()'s device argument to indicate that the
device should be read from a formal argument of the function being
compiled. At call time, the value of that argument is used to derive the
backend via backend() dispatch and is forwarded to the backend-specific
JIT as the compilation device.
This is intended for functions that have no dynamic array inputs from which
the backend could otherwise be detected (e.g. array constructors like
prim_fill() or prim_iota()).
device_arg(argname)device_arg(argname)
argname |
( |
(AnvlDeviceArg)
An object recognized by jit().
f <- function(x) nv_scalar(1, device = x) g <- jit(f, backend = "auto", device = device_arg("x")) g(nv_device("cpu", "xla"))f <- function(x) nv_scalar(1, device = x) g <- jit(f, backend = "auto", device = device_arg("x")) g(nv_device("cpu", "xla"))
Returns the data type of an array (e.g. f32, i64).
dtype(x, ...)dtype(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
This is implemented via the generic tengen::dtype().
A DataType.
x <- nv_array(1:4, dtype = "f32") dtype(x)x <- nv_array(1:4, dtype = "f32") dtype(x)
Compare two abstract arrays for type equality.
eq_type(e1, e2, ambiguity) neq_type(e1, e2, ambiguity)eq_type(e1, e2, ambiguity) neq_type(e1, e2, ambiguity)
e1 |
( |
e2 |
( |
ambiguity |
( |
logical(1) - TRUE if the arrays are equal, FALSE otherwise.
a <- nv_aval("f32", c(2L, 3L)) b <- nv_aval("f32", c(2L, 3L)) # Same dtype and shape eq_type(a, b, ambiguity = FALSE) # Different dtype eq_type(a, nv_aval("i32", c(2L, 3L)), ambiguity = FALSE) # Different shape eq_type(a, nv_aval("f32", c(3L, 2L)), ambiguity = FALSE) # ambiguity parameter controls whether ambiguous field is compared c <- nv_aval("f32", c(2L, 3L), ambiguous = TRUE) eq_type(a, c, ambiguity = FALSE) eq_type(a, c, ambiguity = TRUE) # neq_type is the negation of eq_type neq_type(a, b, ambiguity = FALSE)a <- nv_aval("f32", c(2L, 3L)) b <- nv_aval("f32", c(2L, 3L)) # Same dtype and shape eq_type(a, b, ambiguity = FALSE) # Different dtype eq_type(a, nv_aval("i32", c(2L, 3L)), ambiguity = FALSE) # Different shape eq_type(a, nv_aval("f32", c(3L, 2L)), ambiguity = FALSE) # ambiguity parameter controls whether ambiguous field is compared c <- nv_aval("f32", c(2L, 3L), ambiguous = TRUE) eq_type(a, c, ambiguity = FALSE) eq_type(a, c, ambiguity = TRUE) # neq_type is the negation of eq_type neq_type(a, b, ambiguity = FALSE)
Subsets a ListNode to keep only the children whose names match names,
then reindexes the leaf nodes so they map to contiguous positions in a flat
list. If all names are kept the original tree is returned unchanged.
filter_list_node(tree, names)filter_list_node(tree, names)
tree |
( |
names |
(character) |
A ListNode containing only the selected children with reindexed
leaves.
build_tree(), reindex_tree(), unflatten()
x <- list(a = 1, b = 2, c = 3) tree <- build_tree(x) sub <- filter_list_node(tree, c("a", "c")) tree_size(sub) unflatten(sub, x[c("a", "c")])x <- list(a = 1, b = 2, c = 3) tree <- build_tree(x) sub <- filter_list_node(tree, c("a", "c")) tree_size(sub) unflatten(sub, x[c("a", "c")])
Recursively flattens a nested list into a single flat list containing only the leaf values, preserving left-to-right order.
Currently only lists are flattened and all other objects are treated as leaves.
Use build_tree() to capture the nesting structure so it can be restored with
unflatten().
flatten(x)flatten(x)
x |
(any) |
list() containing the flattened values.
build_tree(), unflatten(), tree_size()
x <- list(a = 1, b = list(c = 2, d = 3)) flatten(x) flatten(list(1:3, "hello"))x <- list(a = 1, b = list(c = 2, d = 3)) flatten(x) flatten(list(1:3, "hello"))
Returns a new function that computes the gradient of f via reverse-mode automatic
differentiation. f must return a single float scalar. The returned function has the
same signature as f and returns the gradients in the same structure as the inputs
(or the subset selected by wrt).
gradient(f, wrt = NULL)gradient(f, wrt = NULL)
f |
( |
wrt |
( |
function
value_and_gradient() to get both the output and gradients,
transform_gradient() for the low-level graph transformation.
f <- function(x, y) sum(x * y) g <- jit(gradient(f)) g(nv_array(c(1, 2), dtype = "f32"), nv_array(c(3, 4), dtype = "f32")) # Differentiate with respect to a single argument g_x <- jit(gradient(f, wrt = "x")) g_x(nv_array(c(1, 2), dtype = "f32"), nv_array(c(3, 4), dtype = "f32")) # Static (non-array) arguments are passed through but cannot be in wrt f2 <- function(x, power) sum(x^power) g2 <- jit(gradient(f2, wrt = "x"), static = "power") g2(nv_array(c(1, 2, 3), dtype = "f32"), power = 2L)f <- function(x, y) sum(x * y) g <- jit(gradient(f)) g(nv_array(c(1, 2), dtype = "f32"), nv_array(c(3, 4), dtype = "f32")) # Differentiate with respect to a single argument g_x <- jit(gradient(f, wrt = "x")) g_x(nv_array(c(1, 2), dtype = "f32"), nv_array(c(3, 4), dtype = "f32")) # Static (non-array) arguments are passed through but cannot be in wrt f2 <- function(x, power) sum(x^power) g2 <- jit(gradient(f2, wrt = "x"), static = "power") g2(nv_array(c(1, 2, 3), dtype = "f32"), power = 2L)
Add a primitive call to a graph descriptor. Inside a primitive body created
with new_primitive(), pass the lexically-bound self as the primitive
argument.
graph_desc_add(primitive, args, params = list(), infer_fn, desc = NULL)graph_desc_add(primitive, args, params = list(), infer_fn, desc = NULL)
primitive |
( |
args |
( |
params |
( |
infer_fn |
( |
desc |
( |
(list of GraphBox)
Lowers a supported subset of AnvlGraph objects to a plain R function (no
compilation) suitable for quickr::quick(). The returned function expects
plain R scalars/vectors/arrays and returns plain R values/arrays.
graph_to_quickr_r_function(graph)graph_to_quickr_r_function(graph)
graph |
( |
Most users will prefer jit() with backend = "quickr".
This function is the lower-level graph API.
(function)
jit() with options(anvl.backend = "quickr") for tracing and compiling a
regular R function in one step.
An AnvlBox subclass that wraps a GraphNode during graph construction (tracing).
When a function is traced via trace_fn(), each intermediate array
value is represented as a GraphBox.
It also contains an associated GraphDescriptor in which the node "lives".
GraphBox(gnode, desc)GraphBox(gnode, desc)
gnode |
( |
desc |
( |
(GraphBox)
Descriptor of an AnvlGraph. This is a mutable class.
GraphDescriptor( calls = list(), tensor_to_gval = NULL, gval_to_box = NULL, constants = list(), in_tree = NULL, out_tree = NULL, inputs = list(), outputs = list(), is_static_flat = NULL, static_args_flat = NULL, devices = character() )GraphDescriptor( calls = list(), tensor_to_gval = NULL, gval_to_box = NULL, constants = list(), in_tree = NULL, out_tree = NULL, inputs = list(), outputs = list(), is_static_flat = NULL, static_args_flat = NULL, devices = character() )
calls |
( |
tensor_to_gval |
( |
gval_to_box |
( |
constants |
( |
in_tree |
( |
out_tree |
( |
inputs |
( |
outputs |
( |
is_static_flat |
( |
static_args_flat |
( |
devices |
( |
(GraphDescriptor)
Literal in an AnvlGraph. This is a mutable class.
GraphLiteral(aval)GraphLiteral(aval)
aval |
( |
(GraphLiteral)
Virtual base class for nodes in an AnvlGraph.
Is either a GraphValue or a GraphLiteral.
Cannot be instantiated directly - use GraphValue() or GraphLiteral() instead.
Value in an AnvlGraph. This is a mutable class.
GraphValue(aval)GraphValue(aval)
aval |
( |
(GraphValue)
An AbstractArray representing an integer sequence.
Usually created by nv_iota() / nv_seq(), which both call prim_iota() internally.
Inherits from AbstractArray.
IotaArray(shape, dtype, dimension, start = 1L, ambiguous = FALSE)IotaArray(shape, dtype, dimension, start = 1L, ambiguous = FALSE)
shape |
( |
dtype |
( |
dimension |
( |
start |
( |
ambiguous |
( |
When lowering to stableHLO, these become iota operations that generate the integer sequence
so they do not need to actually hold the data in the executable, similar to ALTREPs in R.
It lowers to stablehlo::hlo_iota(), optionally shifting the starting value via
stablehlo::hlo_add().
x <- IotaArray(shape = 4L, dtype = "i32", dimension = 1L) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing: graph <- trace_fn(function() nv_iota(dim = 1L, dtype = "i32", shape = 4L), list()) graph graph$outputs[[1]]$avalx <- IotaArray(shape = 4L, dtype = "i32", dimension = 1L) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing: graph <- trace_fn(function() nv_iota(dim = 1L, dtype = "i32", shape = 4L), list()) graph graph$outputs[[1]]$aval
Test whether an object is a device
is_device(x)is_device(x)
x |
An object to test. |
logical(1)
Tests whether x is a DataType object.
is_dtype(x)is_dtype(x)
x |
An object to test. |
TRUE or FALSE.
as_dtype(), tengen::is_dtype()
is_dtype("f32") is_dtype(as_dtype("f32"))is_dtype("f32") is_dtype(as_dtype("f32"))
Wraps a function so that it is traced and compiled on first call. Subsequent
calls with the same input structure, shapes, and dtypes hit an LRU cache and
skip recompilation. Unlike xla(), the compiled executable is not created
eagerly but lazily on the first invocation.
jit( f, static = character(), cache_size = 100L, backend = NULL, device = NULL, ... )jit( f, static = character(), cache_size = 100L, backend = NULL, device = NULL, ... )
f |
( |
static |
( |
cache_size |
( |
backend |
( |
device |
( The default ( In order to use dynamic device selection with the |
... |
Backend-specific options. Passing an option that is not supported by the selected backend raises an error. See the XLA JIT arguments and Quickr JIT arguments sections below for the options accepted by each backend. |
A JitFunction (a function with the same formals as f).
The returned wrapper expects AnvlArray inputs and returns
AnvlArray values.
There are various ways to specify which device and which backend to use.
Concrete backend:
In the case where we fix a concrete backend (backend is not "auto"), the device can be
inferred or set explicitly.
Setting the device explicitly allows you to enforce that the function always uses the specified
device, e.g. "cuda:0".
If the device argument is set, all encountered arrays are copied to it.
If the device is not specified (NULL; default) the device will be inferred from the input
arrays and the constants within the program. If conflicting devices are found, an error
is thrown. If no array with a device is found, we fall back to the default device.
Auto backend:
When setting backend = "auto", the backend will be inferred from the array inputs and
otherwise fall back to the default backend.
If you want to jit() a function without array inputs but make it work with different devices,
set device = device_arg("<argname>") where <argname> is the name of the argument specifying
the device. Note that this is only necessary with the "auto" backend.
When using a concrete backend, you can just specify the device via a static argument.
donate (character(), default character()): names of arguments whose
underlying buffers may be donated to (i.e., reused/consumed by) the
compiled XLA executable. Donated buffers must not be used again by the
caller after the call; this can reduce memory usage and copies for large
inputs. Must not overlap with static.
unwrap (logical(1), default FALSE): if TRUE, the compiled function
returns plain R arrays instead of AnvlArrays. Useful when the jitted
function's output is consumed by non-anvl R code and the extra wrapping
would only get stripped again.
xla() for ahead-of-time compilation, jit_eval() for evaluating an expression once.
f <- jit(function(x, y) x + y) f(nv_array(1), nv_array(2)) # Static arguments enable data-dependent control flow g <- jit(function(x, flag) { if (flag) x + 1 else x * 2 }, static = "flag") g(nv_array(3), TRUE) g(nv_array(3), FALSE) with_backend("quickr", { h <- jit(function(x, y) x + y) h(nv_array(1), nv_array(2)) })f <- jit(function(x, y) x + y) f(nv_array(1), nv_array(2)) # Static arguments enable data-dependent control flow g <- jit(function(x, flag) { if (flag) x + 1 else x * 2 }, static = "flag") g(nv_array(3), TRUE) g(nv_array(3), FALSE) with_backend("quickr", { h <- jit(function(x, y) x + y) h(nv_array(1), nv_array(2)) })
Convenience wrapper that JIT-compiles and immediately evaluates a single expression.
Equivalent to wrapping expr in an anonymous function, calling jit() on it, and
invoking the result.
Useful if you want to evaluate an expression once.
jit_eval(expr, ...)jit_eval(expr, ...)
expr |
(NSE) |
... |
Backend-specific options forwarded to |
(any)
Result of the compiled and evaluated expression.
x <- nv_array(c(1, 2, 3), dtype = "f32") jit_eval(x + x)x <- nv_array(c(1, 2, 3), dtype = "f32") jit_eval(x + x)
An AbstractArray where all elements have the same constant value.
This either arises when using literals in traced code (e.g. x + 1) or when using
nv_fill() to create a constant.
LiteralArray(data, shape, dtype = default_dtype(data), ambiguous)LiteralArray(data, shape, dtype = default_dtype(data), ambiguous)
data |
( |
shape |
( |
dtype |
( |
ambiguous |
( |
When arising from R literals, the resulting LiteralArray is ambiguous because no type
information was available. See the vignette("type-promotion") for more details.
LiteralArrays become constants inlined into the stableHLO program.
I.e., they lower to stablehlo::hlo_tensor().
x <- LiteralArray(1L, shape = integer(), ambiguous = TRUE) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing: # 1. via R literals graph <- trace_fn(function() 1, list()) graph graph$outputs[[1]]$aval # 2. via nv_fill() graph <- trace_fn(function() nv_fill(2L, shape = c(2, 2)), list()) graph graph$outputs[[1]]$avalx <- LiteralArray(1L, shape = integer(), ambiguous = TRUE) x ambiguous(x) shape(x) ndims(x) dtype(x) # How it appears during tracing: # 1. via R literals graph <- trace_fn(function() 1, list()) graph graph$outputs[[1]]$aval # 2. via nv_fill() graph <- trace_fn(function() nv_fill(2L, shape = c(2, 2)), list()) graph graph$outputs[[1]]$aval
Sets the anvl.default_backend option for the duration of the
calling scope. This affects nv_array(), nv_scalar(), and jit().
local_backend(backend, envir = parent.frame())local_backend(backend, envir = parent.frame())
backend |
( |
envir |
The environment to scope the change to. |
The previous value of the option (invisibly).
Creates a new GraphDescriptor which is afterwards accessible via .current_descriptor().
The graph is automatically removed when exiting the current scope.
After the graph is either cleaned up automatically (by exiting the scope)
or finalized, the previously built graph is restored,
i.e., accessible via .current_descriptor().
local_descriptor(..., envir = parent.frame())local_descriptor(..., envir = parent.frame())
... |
( |
envir |
( |
A GraphDescriptor object.
Apply a function to each leaf of a (possibly nested) list, preserving the
tree structure. Equivalent to flattening .x with flatten(), applying
.f to each leaf, and reassembling with unflatten().
map_tree(.x, .f, ...)map_tree(.x, .f, ...)
.x |
(any) |
.f |
( |
... |
Additional arguments passed to |
An object with the same nesting structure as .x, where each leaf
is the result of .f(leaf, ...).
flatten(), build_tree(), unflatten(), await()
map_tree(list(a = 1, b = list(c = 2, d = 3)), \(x) x + 1)map_tree(list(a = 1, b = list(c = 2, d = 3)), \(x) x + 1)
Returns the number of dimensions (sometimes also refered to as rank) of an array.
Equivalent to length(shape(x)).
ndims(x)ndims(x)
x |
( |
integer(1)
x <- nv_array(1:4, dtype = "f32") ndims(x)x <- nv_array(1:4, dtype = "f32") ndims(x)
Builds an AnvlPrimitive metadata object, wraps fn with jit(),
attaches the metadata via attr(., "primitive"), prepends class
"JitPrimitive", and (by default) registers the result under name in
the primitive registry.
The backend is always "auto" and cannot be configured.
new_primitive( name, fn, subgraphs = character(), static = character(), device = NULL, register = TRUE )new_primitive( name, fn, subgraphs = character(), static = character(), device = NULL, register = TRUE )
name |
( |
fn |
( |
subgraphs |
( |
static |
( |
device |
( |
register |
( |
A callable of class c("JitPrimitive", "JitFunction").
Element-wise absolute value. You can also use abs().
nv_abs(operand)nv_abs(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_abs() for the underlying primitive.
x <- nv_array(c(-1, 2, -3)) abs(x)x <- nv_array(c(-1, 2, -3)) abs(x)
Element-wise inverse cosine. You can also use acos().
nv_acos(operand)nv_acos(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_acos() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) acos(x)x <- nv_array(c(-1, 0, 1)) acos(x)
Element-wise inverse hyperbolic cosine. You can also use acosh().
nv_acosh(operand)nv_acosh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_acosh() for the underlying primitive.
x <- nv_array(c(1, 2, 10)) acosh(x)x <- nv_array(c(1, 2, 10)) acosh(x)
Adds two arrays element-wise. You can also use the + operator.
nv_add(lhs, rhs)nv_add(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_add() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) x + yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) x + y
Element-wise logical AND. You can also use the & operator.
nv_and(lhs, rhs)nv_and(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_and() for the underlying primitive.
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) x & yx <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) x & y
Returns the index of the maximum value along a dimension. Ties are broken by returning the smallest index.
nv_argmax(operand, dim = NULL, drop = TRUE, nan_rm = FALSE)nv_argmax(operand, dim = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dim |
( |
drop |
( |
nan_rm |
( |
arrayish of dtype i32
Same shape as operand with dim removed (or set to 1 if drop = FALSE).
With nan_rm = FALSE (default), if any entry along the reduced axis is
NaN, the returned index points at the first such NaN. With
nan_rm = TRUE, NaN entries are skipped.
nv_argmax(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_argmax(nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE), dim = 2L ) nv_argmax(nv_array(c(1, NaN, 3))) nv_argmax(nv_array(c(1, NaN, 3)), nan_rm = TRUE)nv_argmax(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_argmax(nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE), dim = 2L ) nv_argmax(nv_array(c(1, NaN, 3))) nv_argmax(nv_array(c(1, NaN, 3)), nan_rm = TRUE)
Returns the index of the minimum value along a dimension. Ties are broken by returning the smallest index.
nv_argmin(operand, dim = NULL, drop = TRUE, nan_rm = FALSE)nv_argmin(operand, dim = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dim |
( |
drop |
( |
nan_rm |
( |
arrayish of dtype i32
Same shape as operand with dim removed (or set to 1 if drop = FALSE).
With nan_rm = FALSE (default), if any entry along the reduced axis is
NaN, the returned index points at the first such NaN. With
nan_rm = TRUE, NaN entries are skipped.
nv_argmin(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_argmin(nv_array(c(2, NaN, 1, 3))) nv_argmin(nv_array(c(2, NaN, 1, 3)), nan_rm = TRUE)nv_argmin(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_argmin(nv_array(c(2, NaN, 1, 3))) nv_argmin(nv_array(c(2, NaN, 1, 3)), nan_rm = TRUE)
Returns the indices that would sort the array along a dimension.
nv_argsort(operand, dim = NULL, decreasing = FALSE, stable = FALSE)nv_argsort(operand, dim = NULL, decreasing = FALSE, stable = FALSE)
operand |
( |
dim |
( |
decreasing |
( |
stable |
( |
arrayish of dtype i32
Same shape as operand. For a size-0 axis, the output is an empty i32
array of the same shape (a valid empty permutation).
as_array(operand)[as_array(nv_argsort(operand))] reproduces the sorted
array (for 1-D inputs).
NaN values sort to the end (ascending) or beginning
(descending), regardless of sign. +0 and -0 compare equal.
x <- nv_array(c(3, 1, 4, 1, 5)) nv_argsort(x)x <- nv_array(c(3, 1, 4, 1, 5)) nv_argsort(x)
Element-wise inverse sine. You can also use asin().
nv_asin(operand)nv_asin(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_asin() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) asin(x)x <- nv_array(c(-1, 0, 1)) asin(x)
Element-wise inverse hyperbolic sine. You can also use asinh().
nv_asinh(operand)nv_asinh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_asinh() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) asinh(x)x <- nv_array(c(-1, 0, 1)) asinh(x)
Element-wise inverse tangent. You can also use atan().
nv_atan(operand)nv_atan(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_atan() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) atan(x)x <- nv_array(c(-1, 0, 1)) atan(x)
Element-wise two-argument arctangent, i.e. the angle (in radians) between the positive
x-axis and the point (rhs, lhs).
nv_atan2(lhs, rhs)nv_atan2(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_atan2() for the underlying primitive.
y <- nv_array(c(1, 0, -1)) x <- nv_array(c(0, 1, 0)) nv_atan2(y, x)y <- nv_array(c(1, 0, -1)) x <- nv_array(c(0, 1, 0)) nv_atan2(y, x)
Element-wise inverse hyperbolic tangent. You can also use atanh().
nv_atanh(operand)nv_atanh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_atanh() for the underlying primitive.
x <- nv_array(c(-0.5, 0, 0.5)) atanh(x)x <- nv_array(c(-0.5, 0, 0.5)) atanh(x)
Representation of an abstract array type.
During tracing, it is wrapped in a GraphNode held by a GraphBox.
In the lowered AnvlGraph it is also part of GraphNodes representing the values in the program.
The base class represents an unknown value, but child classes exist for:
closed-over constants: ConcreteArray
scalar arrays arising from R literals: LiteralArray
sequence patterns: IotaArray
To convert a arrayish value to an abstract array, use to_abstract().
nv_aval(dtype, shape, ambiguous = FALSE) AbstractArray(dtype, shape, ambiguous = FALSE)nv_aval(dtype, shape, ambiguous = FALSE) AbstractArray(dtype, shape, ambiguous = FALSE)
dtype |
( |
shape |
( |
ambiguous |
( |
The following extractors are available on AbstractArray objects:
dtype(): Get the data type of the array.
shape(): Get the shape (dimensions) of the array.
ambiguous(): Get whether the dtype is ambiguous.
ndims(): Get the number of dimensions.
LiteralArray, ConcreteArray, IotaArray, GraphValue, to_abstract(), GraphBox
# -- Creating abstract arrays -- a <- AbstractArray("f32", c(2L, 3L)) a dtype(a) shape(a) ambiguous(a) # Shorthand nv_aval("f32", c(2L, 3L)) # How AbstractArrays appear in an AnvlGraph graph <- trace_fn(function(x) x + 1, list(x = nv_aval("i32", 4L))) graph graph$inputs[[1]]$aval# -- Creating abstract arrays -- a <- AbstractArray("f32", c(2L, 3L)) a dtype(a) shape(a) ambiguous(a) # Shorthand nv_aval("f32", c(2L, 3L)) # How AbstractArrays appear in an AnvlGraph graph <- trace_fn(function(x) x + 1, list(x = nv_aval("i32", 4L))) graph graph$inputs[[1]]$aval
Combine arrays along the row (nv_rbind) or column (nv_cbind) dimension.
Arguments are first promoted to a common data type
(see nv_promote_to_common()).
Each input is then handled according to its rank:
0-D: broadcast to match the non-stacked dimensions of the other inputs.
1-D: treated as a single row/column.
Other: used as-is.
nv_rbind(...) nv_cbind(...) ## S3 method for class 'AnvlArray' rbind(..., deparse.level = 1) ## S3 method for class 'AnvlArray' cbind(..., deparse.level = 1)nv_rbind(...) nv_cbind(...) ## S3 method for class 'AnvlArray' rbind(..., deparse.level = 1) ## S3 method for class 'AnvlArray' cbind(..., deparse.level = 1)
... |
( |
deparse.level |
Ignored. Kept for compatibility with |
base::rbind() and base::cbind() applied to an array() of rank > 2
flatten the trailing dimensions into the column axis (so a c(2, 3, 4)
array becomes a 2 x 12 matrix). nv_rbind and nv_cbind instead
preserve all non-stacked dimensions: combining two c(2, 3, 4) arrays
with nv_rbind produces a c(4, 3, 4) array, and with nv_cbind a
c(2, 6, 4) array.
# Vectors as rows / columns nv_rbind(nv_array(1:3), nv_array(4:6)) nv_cbind(nv_array(1:3), nv_array(4:6)) # Scalar broadcasting nv_rbind(nv_matrix(1:6, nrow = 2), nv_scalar(0)) # Rank-3 arrays preserve trailing dimensions a <- nv_array(1:24, shape = c(2, 3, 4)) shape(nv_rbind(a, a)) # c(4, 3, 4)# Vectors as rows / columns nv_rbind(nv_array(1:3), nv_array(4:6)) nv_cbind(nv_array(1:3), nv_array(4:6)) # Scalar broadcasting nv_rbind(nv_matrix(1:6, nrow = 2), nv_scalar(0)) # Rank-3 arrays preserve trailing dimensions a <- nv_array(1:24, shape = c(2, 3, 4)) shape(nv_rbind(a, a)) # c(4, 3, 4)
Reinterprets the bits of an array as a different data type without modifying the underlying data. If the target type is narrower, an extra trailing dimension is added; if wider, the last dimension is consumed.
nv_bitcast_convert(operand, dtype)nv_bitcast_convert(operand, dtype)
operand |
( |
dtype |
( |
arrayish
Has the given dtype.
prim_bitcast_convert() for the underlying primitive, nv_convert()
for value-preserving type conversion.
x <- nv_array(1L) prim_bitcast_convert(x, dtype = "i8")x <- nv_array(1L) prim_bitcast_convert(x, dtype = "i8")
Broadcasts arrays to a common shape using NumPy-style broadcasting rules.
nv_broadcast_arrays(...)nv_broadcast_arrays(...)
... |
( |
(list() of arrayish)
List of arrays, all with the same shape.
If the arrays have different numbers of dimensions, prepend size-1 dimensions to the shorter shape.
For each dimension: if the sizes match, keep them; if one is 1, expand it to the other's size; otherwise raise an error.
nv_broadcast_scalars(), nv_broadcast_to()
x <- nv_matrix(1:6, nrow = 2) y <- nv_array(c(10, 20, 30)) nv_broadcast_arrays(x, y)x <- nv_matrix(1:6, nrow = 2) y <- nv_array(c(10, 20, 30)) nv_broadcast_arrays(x, y)
Broadcast scalar arrays to match the shape of non-scalar arrays. All non-scalar arrays must have the same shape.
nv_broadcast_scalars(...)nv_broadcast_scalars(...)
... |
( |
(list() of arrayish)
List of broadcasted arrays.
x <- nv_array(c(1, 2, 3)) # scalar 1 is broadcast to shape [3] nv_broadcast_scalars(x, nv_scalar(1))x <- nv_array(c(1, 2, 3)) # scalar 1 is broadcast to shape [3] nv_broadcast_scalars(x, nv_scalar(1))
Broadcasts an array to a target shape using NumPy-style broadcasting rules.
nv_broadcast_to(operand, shape)nv_broadcast_to(operand, shape)
operand |
( |
shape |
( |
arrayish
Has the given shape and the same data type as operand.
nv_broadcast_arrays(), nv_broadcast_scalars(),
prim_broadcast_in_dim() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) nv_broadcast_to(x, shape = c(2, 3))x <- nv_array(c(1, 2, 3)) nv_broadcast_to(x, shape = c(2, 3))
Element-wise cube root.
nv_cbrt(operand)nv_cbrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_cbrt() for the underlying primitive.
x <- nv_array(c(1, 8, 27)) nv_cbrt(x)x <- nv_array(c(1, 8, 27)) nv_cbrt(x)
Element-wise ceiling (round toward positive infinity). You can also use ceiling().
nv_ceiling(operand)nv_ceiling(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_ceil() for the underlying primitive.
x <- nv_array(c(1.2, 2.7, -1.5)) ceiling(x)x <- nv_array(c(1.2, 2.7, -1.5)) ceiling(x)
Computes the Cholesky decomposition of a symmetric positive-definite matrix. Supports batched inputs: dimensions before the last two are batch dimensions.
nv_chol(operand, lower = FALSE) ## S3 method for class 'AnvlArray' chol(x, ..., lower = FALSE)nv_chol(operand, lower = FALSE) ## S3 method for class 'AnvlArray' chol(x, ..., lower = FALSE)
operand |
( |
lower |
( |
x |
( |
... |
No additional arguments. |
arrayish
Triangular matrix with the same shape and data type as the input.
a <- nv_matrix(c(4, 2, 2, 3), nrow = 2, dtype = "f32") nv_chol(a)a <- nv_matrix(c(4, 2, 2, 3), nrow = 2, dtype = "f32") nv_chol(a)
Element-wise clamp: max(min_val, min(operand, max_val)).
Converts min_val and max_val to the data type of operand.
nv_clamp(min_val, operand, max_val)nv_clamp(min_val, operand, max_val)
min_val, max_val
|
( |
operand |
( |
The underlying stableHLO function already broadcasts scalars, so no need to broadcast manually.
arrayish
Has the same shape and data type as the input.
prim_clamp() for the underlying primitive.
x <- nv_array(c(-1, 0.5, 2)) nv_clamp(nv_scalar(0), x, nv_scalar(1))x <- nv_array(c(-1, 0.5, 2)) nv_clamp(nv_scalar(0), x, nv_scalar(1))
Concatenates arrays along a dimension. Operands are promoted to a common data type and scalars are broadcast before concatenation.
nv_concatenate(..., dimension = NULL)nv_concatenate(..., dimension = NULL)
... |
( |
dimension |
( |
arrayish
Has the common data type and a shape matching the inputs in all
dimensions except dimension, which is the sum of input sizes.
prim_concatenate() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) nv_concatenate(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) nv_concatenate(x, y)
Converts the elements of an array to a different data type. Returns the input unchanged if it already has the target type.
nv_convert(operand, dtype)nv_convert(operand, dtype)
operand |
( |
dtype |
( |
arrayish
Has the given dtype and the same shape as operand.
prim_convert() for the underlying primitive.
x <- nv_array(c(1L, 2L, 3L)) nv_convert(x, dtype = "f32")x <- nv_array(c(1L, 2L, 3L)) nv_convert(x, dtype = "f32")
Element-wise cosine. You can also use cos().
nv_cos(operand)nv_cos(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_cos() for the underlying primitive.
x <- nv_array(c(0, pi / 2, pi)) cos(x)x <- nv_array(c(0, pi / 2, pi)) cos(x)
Element-wise hyperbolic cosine. You can also use cosh().
nv_cosh(operand)nv_cosh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_cosh() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) cosh(x)x <- nv_array(c(-1, 0, 1)) cosh(x)
Computes t(lhs) %*% rhs. If rhs is missing, computes t(lhs) %*% lhs.
nv_crossprod(lhs, rhs = NULL) ## S3 method for class 'AnvlArray' crossprod(x, y = NULL, ...)nv_crossprod(lhs, rhs = NULL) ## S3 method for class 'AnvlArray' crossprod(x, y = NULL, ...)
lhs |
( |
rhs |
( |
x, y
|
Same as |
... |
No additional arguments. |
x <- nv_matrix(1:6, nrow = 3, dtype = "f32") nv_crossprod(x)x <- nv_matrix(1:6, nrow = 3, dtype = "f32") nv_crossprod(x)
Running maximum, optionally along a single dimension.
nv_cummax(operand, dim = NULL, with_indices = FALSE, nan_rm = FALSE)nv_cummax(operand, dim = NULL, with_indices = FALSE, nan_rm = FALSE)
operand |
( |
dim |
( |
with_indices |
( |
nan_rm |
( |
arrayish (when with_indices = FALSE) or named list of two
arrays (when with_indices = TRUE).
Both nv_cummax() (with dim = NULL) and base::cummax()
flatten a multi-dimensional input to 1-D before accumulating, but the
flatten order differs: anvl arrays are row-major (C order), so the
flattened sequence iterates the last dim fastest, whereas base R uses
column-major (Fortran) order. The two agree on 1-D inputs.
prim_cummax() for the underlying primitive.
x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) nv_cummax(x) nv_cummax(x, dim = 1L) nv_cummax(x, dim = 1L, with_indices = TRUE) nv_cummax(nv_array(c(1, NaN, 3))) # NaN propagates nv_cummax(nv_array(c(1, NaN, 3)), nan_rm = TRUE) # NaN skippedx <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) nv_cummax(x) nv_cummax(x, dim = 1L) nv_cummax(x, dim = 1L, with_indices = TRUE) nv_cummax(nv_array(c(1, NaN, 3))) # NaN propagates nv_cummax(nv_array(c(1, NaN, 3)), nan_rm = TRUE) # NaN skipped
Running minimum, optionally along a single dimension.
nv_cummin(operand, dim = NULL, with_indices = FALSE, nan_rm = FALSE)nv_cummin(operand, dim = NULL, with_indices = FALSE, nan_rm = FALSE)
operand |
( |
dim |
( |
with_indices |
( |
nan_rm |
( |
arrayish (when with_indices = FALSE) or named list of two
arrays (when with_indices = TRUE).
Both nv_cummin() (with dim = NULL) and base::cummin()
flatten a multi-dimensional input to 1-D before accumulating, but the
flatten order differs: anvl arrays are row-major (C order), so the
flattened sequence iterates the last dim fastest, whereas base R uses
column-major (Fortran) order. The two agree on 1-D inputs.
prim_cummin() for the underlying primitive.
x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) nv_cummin(x) nv_cummin(x, dim = 1L) nv_cummin(x, dim = 1L, with_indices = TRUE) nv_cummin(nv_array(c(3, NaN, 1))) # NaN propagates nv_cummin(nv_array(c(3, NaN, 1)), nan_rm = TRUE) # NaN skippedx <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) nv_cummin(x) nv_cummin(x, dim = 1L) nv_cummin(x, dim = 1L, with_indices = TRUE) nv_cummin(nv_array(c(3, NaN, 1))) # NaN propagates nv_cummin(nv_array(c(3, NaN, 1)), nan_rm = TRUE) # NaN skipped
Cumulative product, optionally along a single dimension.
nv_cumprod(operand, dim = NULL, nan_rm = FALSE)nv_cumprod(operand, dim = NULL, nan_rm = FALSE)
operand |
( |
dim |
( |
nan_rm |
( |
arrayish
Has the same shape and data type as the input.
Both nv_cumprod() (with dim = NULL) and base::cumprod()
flatten a multi-dimensional input to 1-D before accumulating, but the
flatten order differs: anvl arrays are row-major (C order), so the
flattened sequence iterates the last dim fastest, whereas base R uses
column-major (Fortran) order. The two agree on 1-D inputs.
prim_cumprod() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_cumprod(x) # row-major flatten, then accumulate nv_cumprod(x, dim = 1L) # accumulate along rows nv_cumprod(nv_array(c(2, NaN, 3))) # NaN propagates nv_cumprod(nv_array(c(2, NaN, 3)), nan_rm = TRUE) # NaN treated as 1x <- nv_matrix(1:6, nrow = 2) nv_cumprod(x) # row-major flatten, then accumulate nv_cumprod(x, dim = 1L) # accumulate along rows nv_cumprod(nv_array(c(2, NaN, 3))) # NaN propagates nv_cumprod(nv_array(c(2, NaN, 3)), nan_rm = TRUE) # NaN treated as 1
Cumulative sum, optionally along a single dimension.
nv_cumsum(operand, dim = NULL, nan_rm = FALSE)nv_cumsum(operand, dim = NULL, nan_rm = FALSE)
operand |
( |
dim |
( |
nan_rm |
( |
arrayish
Has the same shape and data type as the input.
Both nv_cumsum() (with dim = NULL) and base::cumsum()
flatten a multi-dimensional input to 1-D before accumulating, but the
flatten order differs: anvl arrays are row-major (C order), so the
flattened sequence iterates the last dim fastest, whereas base R uses
column-major (Fortran) order. The two agree on 1-D inputs.
prim_cumsum() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_cumsum(x) # row-major flatten, then accumulate nv_cumsum(x, dim = 1L) # accumulate along rows nv_cumsum(nv_array(c(1, NaN, 3))) # NaN propagates nv_cumsum(nv_array(c(1, NaN, 3)), nan_rm = TRUE) # NaN treated as 0x <- nv_matrix(1:6, nrow = 2) nv_cumsum(x) # row-major flatten, then accumulate nv_cumsum(x, dim = 1L) # accumulate along rows nv_cumsum(nv_array(c(1, NaN, 3))) # NaN propagates nv_cumsum(nv_array(c(1, NaN, 3)), nan_rm = TRUE) # NaN treated as 0
Computes the determinant of a square matrix via nv_determinant().
nv_det(operand)nv_det(operand)
operand |
( |
Scalar arrayish with the same dtype as operand.
nv_determinant(), nv_solve(), prim_lu()
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_det(a)a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_det(a)
Computes the determinant of a square matrix in the modulus / sign
decomposition matching base R's base::determinant(). For the plain
scalar determinant, use nv_det().
nv_determinant(operand, logarithm = TRUE) ## S3 method for class 'AnvlArray' determinant(x, logarithm = TRUE, ...)nv_determinant(operand, logarithm = TRUE) ## S3 method for class 'AnvlArray' determinant(x, logarithm = TRUE, ...)
operand |
( |
logarithm |
( |
x |
( |
... |
No additional arguments. |
For computing the determinant, we use:
Matching base R's det_ge_real, the magnitude is computed in log
space when logarithm = TRUE () and as a
direct product when logarithm = FALSE ().
Named list with elements modulus and sign, both scalar
arrayish with the same dtype as operand. The full determinant
is sign * exp(modulus) (with logarithm = TRUE) or
sign * modulus (with logarithm = FALSE).
nv_det(), nv_solve(), prim_lu()
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_determinant(a) nv_determinant(a, logarithm = FALSE)a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_determinant(a) nv_determinant(a, logarithm = FALSE)
Constructs a backend-specific device object.
A device identifies a compute resources, such as CPU, or a specific GPU.
It is relevant for data allocation (e.g. via nv_array()) but also compilation (jit).
nv_device(x, backend = NULL)nv_device(x, backend = NULL)
x |
( |
backend |
( |
A backend-specific device object (e.g. PJRTDevice for "xla",
quickr_device for "quickr").
# Create CPU device for xla backend: nv_device("cpu", "xla") # Create CPU device for quickr backend: nv_device("cpu", "quickr") # Pass through an existing device: dev <- nv_device("cpu") identical(nv_device(dev), dev)# Create CPU device for xla backend: nv_device("cpu", "xla") # Create CPU device for quickr backend: nv_device("cpu", "quickr") # Pass through an existing device: dev <- nv_device("cpu") identical(nv_device(dev), dev)
Creates a diagonal matrix from a 1-D array.
nv_diag(operand)nv_diag(operand)
operand |
( |
arrayish
An n x n matrix with x on the diagonal and zeros elsewhere.
nv_diag(nv_array(c(1, 2, 3)))nv_diag(nv_array(c(1, 2, 3)))
Element-wise digamma function (logarithmic derivative of the gamma
function). You can also use digamma().
nv_digamma(operand)nv_digamma(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_digamma() for the underlying primitive.
x <- nv_array(c(0.5, 1, 2, 5)) digamma(x)x <- nv_array(c(0.5, 1, 2, 5)) digamma(x)
Divides two arrays element-wise. You can also use the / operator.
nv_div(lhs, rhs)nv_div(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_div() for the underlying primitive.
x <- nv_array(c(10, 20, 30)) y <- nv_array(c(2, 5, 10)) x / yx <- nv_array(c(10, 20, 30)) y <- nv_array(c(2, 5, 10)) x / y
Computes the eigendecomposition of a symmetric matrix operand of
shape (n, n):
Only the lower triangle of operand is read. The columns of vectors
are the (orthonormal) eigenvectors and values is the length-n
vector of (real) eigenvalues in ascending order. Output names and
order match base::eigen().
nv_eigh(operand)nv_eigh(operand)
operand |
( |
Named list with elements values (length n) and vectors
(shape (n, n)). Both have the same dtype as the input.
x <- nv_matrix(c(2, 1, 1, 2), nrow = 2, dtype = "f64") nv_eigh(x)x <- nv_matrix(c(2, 1, 1, 2), nrow = 2, dtype = "f64") nv_eigh(x)
Element-wise equality comparison. You can also use the == operator.
nv_eq(lhs, rhs)nv_eq(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_eq() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) x == yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) x == y
Element-wise error function erf(x) = (2 / sqrt(pi)) * integral_0^x exp(-t^2) dt.
nv_erf(operand)nv_erf(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_erf() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) nv_erf(x)x <- nv_array(c(-1, 0, 1)) nv_erf(x)
Element-wise inverse error function (the inverse of erf on (-1, 1)).
nv_erf_inv(operand)nv_erf_inv(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_erf_inv() for the underlying primitive.
x <- nv_array(c(-0.5, 0, 0.5)) nv_erf_inv(x)x <- nv_array(c(-0.5, 0, 0.5)) nv_erf_inv(x)
Element-wise complementary error function erfc(x) = 1 - erf(x).
nv_erfc(operand)nv_erfc(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_erfc() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) nv_erfc(x)x <- nv_array(c(-1, 0, 1)) nv_erfc(x)
Element-wise exponential. You can also use exp().
nv_exp(operand)nv_exp(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_exp() for the underlying primitive.
x <- nv_array(c(0, 1, 2)) exp(x)x <- nv_array(c(0, 1, 2)) exp(x)
Element-wise exp(x) - 1, more accurate for small x.
nv_expm1(operand)nv_expm1(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_expm1() for the underlying primitive.
x <- nv_array(c(0, 0.001, 1)) nv_expm1(x)x <- nv_array(c(0, 0.001, 1)) nv_expm1(x)
Extracts the diagonal elements from a 2-D array.
nv_extract_diag(operand)nv_extract_diag(operand)
operand |
( |
arrayish
A 1-D array of length min(nrow, ncol) containing the diagonal elements.
nv_diag() for creating a diagonal matrix, nv_trace()
x <- nv_array(1:9, shape = c(3, 3)) nv_extract_diag(x)x <- nv_array(1:9, shape = c(3, 3)) nv_extract_diag(x)
Creates an n x n identity matrix.
nv_eye_like() is a variant where dtype and device default to those of
like.
nv_eye(n, dtype = "f32", device = NULL) nv_eye_like(like, n, dtype = NULL, device = NULL)nv_eye(n, dtype = "f32", device = NULL) nv_eye_like(like, n, dtype = NULL, device = NULL)
n |
( |
dtype |
( |
device |
( |
like |
( |
arrayish
An n x n identity matrix.
nv_diag() for general diagonal matrices.
nv_eye(3L) x <- nv_fill(0, shape = c(3, 3), dtype = "f64") nv_eye_like(x, 3L)nv_eye(3L) x <- nv_fill(0, shape = c(3, 3), dtype = "f64") nv_eye_like(x, 3L)
Creates an array filled with a scalar value. More memory-efficient than
nv_array(value, shape = shape) for large arrays.
nv_fill_like() is a variant where dtype, shape, ambiguous, and
device default to those of like.
nv_fill(value, shape, dtype = NULL, ambiguous = FALSE, device = NULL) nv_fill_like( like, value, shape = NULL, dtype = NULL, ambiguous = NULL, device = NULL )nv_fill(value, shape, dtype = NULL, ambiguous = FALSE, device = NULL) nv_fill_like( like, value, shape = NULL, dtype = NULL, ambiguous = NULL, device = NULL )
value |
( |
shape |
( |
dtype |
( |
ambiguous |
( |
device |
( |
like |
( |
arrayish
Has the given shape and dtype.
prim_fill() for the underlying primitive.
nv_fill(0, shape = c(2, 3)) x <- nv_matrix(1:6, nrow = 2) nv_fill_like(x, 0)nv_fill(0, shape = c(2, 3)) x <- nv_matrix(1:6, nrow = 2) nv_fill_like(x, 0)
Element-wise floor (round toward negative infinity). You can also use floor().
nv_floor(operand)nv_floor(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_floor() for the underlying primitive.
x <- nv_array(c(1.2, 2.7, -1.5)) floor(x)x <- nv_array(c(1.2, 2.7, -1.5)) floor(x)
Element-wise greater than or equal comparison. You can also use the >= operator.
nv_ge(lhs, rhs)nv_ge(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_ge() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x >= yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x >= y
Element-wise greater than comparison. You can also use the > operator.
nv_gt(lhs, rhs)nv_gt(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_gt() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x > yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x > y
Conditional execution of two branches.
Unlike nv_ifelse(), which selects elements, this executes only one
of the two branches depending on a scalar predicate.
nv_if(pred, true, false)nv_if(pred, true, false)
pred |
( |
true |
( |
false |
( |
Result of the executed branch.
prim_if() for the underlying primitive, nv_ifelse() for
element-wise selection.
nv_if(nv_scalar(TRUE), \() nv_scalar(1), \() nv_scalar(2))nv_if(nv_scalar(TRUE), \() nv_scalar(1), \() nv_scalar(2))
Selects elements from true_value or false_value based on pred,
analogous to R's ifelse().
nv_ifelse(pred, true_value, false_value)nv_ifelse(pred, true_value, false_value)
pred |
( |
true_value, false_value
|
( |
arrayish
Has the common data type of true_value and false_value and the
shape of the non-scalar arguments.
prim_ifelse() for the underlying primitive.
pred <- nv_array(c(TRUE, FALSE, TRUE)) nv_ifelse(pred, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6))) # scalar branches are broadcast and promoted to a common dtype nv_ifelse(pred, nv_scalar(1L), nv_scalar(0.5))pred <- nv_array(c(TRUE, FALSE, TRUE)) nv_ifelse(pred, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6))) # scalar branches are broadcast and promoted to a common dtype nv_ifelse(pred, nv_scalar(1L), nv_scalar(0.5))
Computes operand^-1, the inverse of a square non-singular matrix, by
solving operand %*% x = I.
For most use cases prefer nv_solve() directly: forming the explicit
inverse is both slower and less numerically stable than solving against
a right-hand side.
nv_inv(operand)nv_inv(operand)
operand |
( |
arrayish
The inverse, same shape and dtype as operand.
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_inv(a)a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_inv(a)
Creates an array with values increasing along the specified dimension,
starting from start.
nv_iota_like() is a variant where dtype, shape, ambiguous, and
device default to those of like.
nv_iota(dim, dtype, shape, start = 1L, ambiguous = FALSE, device = NULL) nv_iota_like( like, dim, shape = NULL, start = 1L, dtype = NULL, ambiguous = NULL, device = NULL )nv_iota(dim, dtype, shape, start = 1L, ambiguous = FALSE, device = NULL) nv_iota_like( like, dim, shape = NULL, start = 1L, dtype = NULL, ambiguous = NULL, device = NULL )
dim |
( |
dtype |
( |
shape |
( |
start |
( |
ambiguous |
( |
device |
( |
like |
( |
arrayish
Has the given dtype and shape.
nv_seq() for a simpler 1-D sequence, prim_iota() for the underlying primitive.
nv_iota(dim = 1L, dtype = "i32", shape = 5L) x <- nv_fill(0L, shape = c(2, 3)) nv_iota_like(x, dim = 1L)nv_iota(dim = 1L, dtype = "i32", shape = 5L) x <- nv_fill(0L, shape = c(2, 3)) nv_iota_like(x, dim = 1L)
Element-wise check if values are finite (not Inf, -Inf, or NaN).
nv_is_finite(operand) ## S3 method for class 'AnvlArray' is.finite(x)nv_is_finite(operand) ## S3 method for class 'AnvlArray' is.finite(x)
operand |
( |
x |
( |
arrayish
Has the same shape as the input and boolean data type.
prim_is_finite() for the underlying primitive.
x <- nv_array(c(1, Inf, NaN, -Inf, 0)) nv_is_finite(x)x <- nv_array(c(1, Inf, NaN, -Inf, 0)) nv_is_finite(x)
Element-wise check if values are infinite (Inf or -Inf).
You can also use is.infinite().
nv_is_infinite(operand) ## S3 method for class 'AnvlArray' is.infinite(x)nv_is_infinite(operand) ## S3 method for class 'AnvlArray' is.infinite(x)
operand |
( |
x |
( |
arrayish
Has the same shape as the input and boolean data type.
x <- nv_array(c(1, NaN, Inf, -Inf, 0)) nv_is_infinite(x)x <- nv_array(c(1, NaN, Inf, -Inf, 0)) nv_is_infinite(x)
Element-wise check if values are NaN. You can also use is.nan().
nv_is_nan(operand) ## S3 method for class 'AnvlArray' is.nan(x)nv_is_nan(operand) ## S3 method for class 'AnvlArray' is.nan(x)
operand |
( |
x |
( |
arrayish
Has the same shape as the input and boolean data type.
nv_is_finite(), nv_is_infinite()
x <- nv_array(c(1, NaN, Inf, -Inf, 0)) nv_is_nan(x)x <- nv_array(c(1, NaN, Inf, -Inf, 0)) nv_is_nan(x)
Element-wise less than or equal comparison. You can also use the <= operator.
nv_le(lhs, rhs)nv_le(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_le() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x <= yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x <= y
Element-wise natural logarithm of the absolute value of the gamma
function. You can also use lgamma().
nv_lgamma(operand)nv_lgamma(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_lgamma() for the underlying primitive.
x <- nv_array(c(0.5, 1, 2, 5)) lgamma(x)x <- nv_array(c(0.5, 1, 2, 5)) lgamma(x)
Element-wise natural logarithm. You can also use log().
nv_log(operand)nv_log(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_log() for the underlying primitive.
x <- nv_array(c(1, 2.718, 7.389)) log(x)x <- nv_array(c(1, 2.718, 7.389)) log(x)
Element-wise base-10 logarithm. You can also use log10().
nv_log10(operand)nv_log10(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
x <- nv_array(c(1, 10, 100, 1000)) nv_log10(x)x <- nv_array(c(1, 10, 100, 1000)) nv_log10(x)
Element-wise log(1 + x), more accurate for small x.
nv_log1p(operand)nv_log1p(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_log1p() for the underlying primitive.
x <- nv_array(c(0, 0.001, 1)) nv_log1p(x)x <- nv_array(c(0, 0.001, 1)) nv_log1p(x)
Element-wise base-2 logarithm. You can also use log2().
nv_log2(operand)nv_log2(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
x <- nv_array(c(1, 2, 4, 8)) nv_log2(x)x <- nv_array(c(1, 2, 4, 8)) nv_log2(x)
Element-wise logistic sigmoid: 1 / (1 + exp(-x)).
nv_logistic(operand)nv_logistic(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_logistic() for the underlying primitive.
x <- nv_array(c(-2, 0, 2)) nv_logistic(x)x <- nv_array(c(-2, 0, 2)) nv_logistic(x)
Element-wise less than comparison. You can also use the < operator.
nv_lt(lhs, rhs)nv_lt(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_lt() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x < yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) x < y
Computes the partial-pivoted LU decomposition of a matrix operand:
where is a permutation matrix, is unit lower
triangular, and is upper triangular.
This function returns L and U as separate matrices.
Use prim_lu() to get them in packed LU form.
nv_lu(operand)nv_lu(operand)
operand |
( |
Named list:
L – unit lower-triangular factor of shape (m, k), where
(m, n) = shape(operand) and k = min(m, n).
U – upper-triangular factor of shape (k, n).
pivots – length k, dtype i32. LAPACK-style sequential
1-based row swaps as returned by getrf.
permutation – length m, dtype i32. A 1-based permutation
vector representing .
x <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_lu(x)x <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") nv_lu(x)
Matrix multiplication of two arrays. You can also use the %*% operator.
Supports batched matrix multiplication when inputs have more than 2 dimensions.
nv_matmul(lhs, rhs)nv_matmul(lhs, rhs)
lhs, rhs
|
( |
lhs: (b1, ..., bk, m, n)
rhs: (b1, ..., bk, n, p)
output: (b1, ..., bk, m, p)
prim_dot_general() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) y <- nv_matrix(1:6, nrow = 3) x %*% yx <- nv_matrix(1:6, nrow = 2) y <- nv_matrix(1:6, nrow = 3) x %*% y
Element-wise maximum of two arrays.
nv_max(lhs, rhs)nv_max(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_max() for the underlying primitive.
x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) nv_max(x, y)x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) nv_max(x, y)
Computes the arithmetic mean along the specified dimensions. You can also
use mean().
nv_mean(operand, dims = NULL, drop = TRUE, nan_rm = FALSE) ## S3 method for class 'AnvlArray' mean(x, trim = 0, na.rm = FALSE, ..., dims = NULL, drop = TRUE)nv_mean(operand, dims = NULL, drop = TRUE, nan_rm = FALSE) ## S3 method for class 'AnvlArray' mean(x, trim = 0, na.rm = FALSE, ..., dims = NULL, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
nan_rm |
( |
x |
( |
trim |
Currently not supported. |
na.rm |
Forwarded to |
... |
No additional arguments. |
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
x <- nv_matrix(1:6, nrow = 2) nv_mean(x) # all dims -> scalar nv_mean(x, dims = 1L) nv_mean(nv_array(c(1, NaN, 3))) nv_mean(nv_array(c(1, NaN, 3)), nan_rm = TRUE)x <- nv_matrix(1:6, nrow = 2) nv_mean(x) # all dims -> scalar nv_mean(x, dims = 1L) nv_mean(nv_array(c(1, NaN, 3))) nv_mean(nv_array(c(1, NaN, 3)), nan_rm = TRUE)
Computes the median along a dimension. Equivalent to
nv_quantile(operand, 0.5, dim, interpolation); for an even-length axis
with the default "linear" interpolation, the average of the two middle
values is returned, matching base R's median().
You can also use median() directly on an AnvlArray or AnvlBox;
extra arguments (e.g. interpolation) are forwarded via ....
nv_median(operand, dim = NULL, interpolation = "linear", nan_rm = FALSE) ## S3 method for class 'AnvlArray' median(x, na.rm = FALSE, ..., dim = NULL, interpolation = "linear")nv_median(operand, dim = NULL, interpolation = "linear", nan_rm = FALSE) ## S3 method for class 'AnvlArray' median(x, na.rm = FALSE, ..., dim = NULL, interpolation = "linear")
operand |
( |
dim |
( |
interpolation |
( |
nan_rm |
( |
x |
( |
na.rm |
Forwarded to |
... |
No additional arguments. |
arrayish
Same shape as operand with dim removed.
nv_quantile(), nv_sort(), prim_sort().
nv_median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_median(nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE), dim = 2L ) # forwards through the S3 generic via `...` median(nv_array(c(1, 2, 3, 4)), interpolation = "lower") nv_median(nv_array(c(1, NaN, 3, 5))) nv_median(nv_array(c(1, NaN, 3, 5)), nan_rm = TRUE)nv_median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) median(nv_array(c(3, 1, 4, 1, 5, 9, 2, 6))) nv_median(nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE), dim = 2L ) # forwards through the S3 generic via `...` median(nv_array(c(1, 2, 3, 4)), interpolation = "lower") nv_median(nv_array(c(1, NaN, 3, 5))) nv_median(nv_array(c(1, NaN, 3, 5)), nan_rm = TRUE)
Element-wise minimum of two arrays.
nv_min(lhs, rhs)nv_min(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_min() for the underlying primitive.
x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) nv_min(x, y)x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) nv_min(x, y)
Element-wise flooring remainder of division. The sign of the result equals
the sign of rhs, matching base R's %% operator.
nv_mod(lhs, rhs)nv_mod(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
nv_remainder() for truncating remainder, prim_remainder() for
the underlying primitive.
x <- nv_array(c(1L, -1L)) y <- nv_array(c(-3L, 3L)) nv_mod(x, y) as.vector(x) %% as.vector(y)x <- nv_array(c(1L, -1L)) y <- nv_array(c(-3L, 3L)) nv_mod(x, y) as.vector(x) %% as.vector(y)
Multiplies two arrays element-wise. You can also use the * operator.
nv_mul(lhs, rhs)nv_mul(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_mul() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) x * yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) x * y
Element-wise inequality comparison. You can also use the != operator.
nv_ne(lhs, rhs)nv_ne(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
prim_ne() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) x != yx <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) x != y
Negates an array element-wise. You can also use the unary - operator.
nv_negate(operand)nv_negate(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_negate() for the underlying primitive.
x <- nv_array(c(1, -2, 3)) -xx <- nv_array(c(1, -2, 3)) -x
Element-wise logical NOT. You can also use the ! operator.
nv_not(operand)nv_not(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_not() for the underlying primitive.
x <- nv_array(c(TRUE, FALSE, TRUE)) !xx <- nv_array(c(TRUE, FALSE, TRUE)) !x
Element-wise logical OR. You can also use the | operator.
nv_or(lhs, rhs)nv_or(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_or() for the underlying primitive.
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) x | yx <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) x | y
Computes the outer product of two 1-D arrays.
nv_outer(lhs, rhs)nv_outer(lhs, rhs)
lhs, rhs
|
( |
arrayish
A 2-D array of shape (length(lhs), length(rhs)).
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5)) nv_outer(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5)) nv_outer(x, y)
Pads an array with a given value at the edges and optionally between elements.
nv_pad( operand, padding_value, edge_padding_low, edge_padding_high, interior_padding = NULL )nv_pad( operand, padding_value, edge_padding_low, edge_padding_high, interior_padding = NULL )
operand |
( |
padding_value |
( |
edge_padding_low |
( |
edge_padding_high |
( |
interior_padding |
( |
arrayish
Has the same data type as operand.
prim_pad() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) nv_pad(x, nv_scalar(0), edge_padding_low = 2L, edge_padding_high = 1L)x <- nv_array(c(1, 2, 3)) nv_pad(x, nv_scalar(0), edge_padding_low = 2L, edge_padding_high = 1L)
Element-wise polygamma function: the (n+1)-th derivative of the
log-gamma function. The order n is broadcast against x (so
nv_polygamma(1, x) works for any x). For n = 0 this is the
digamma function; for n = 1, trigamma() dispatches here.
Inputs are promoted to a common floating data type and scalar arguments are broadcast to the shape of the non-scalar arguments.
nv_polygamma(n, x)nv_polygamma(n, x)
n, x
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_polygamma() for the underlying primitive.
x <- nv_array(c(0.5, 1, 2, 5)) nv_polygamma(1, x) # trigammax <- nv_array(c(0.5, 1, 2, 5)) nv_polygamma(1, x) # trigamma
Element-wise population count (number of set bits).
nv_popcnt(operand)nv_popcnt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_popcnt() for the underlying primitive.
x <- nv_array(c(7L, 3L, 15L)) nv_popcnt(x)x <- nv_array(c(7L, 3L, 15L)) nv_popcnt(x)
Raises lhs to the power of rhs element-wise. You can also use the ^ operator.
nv_pow(lhs, rhs)nv_pow(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_pow() for the underlying primitive.
x <- nv_array(c(2, 3, 4)) y <- nv_array(c(3, 2, 1)) x ^ yx <- nv_array(c(2, 3, 4)) y <- nv_array(c(3, 2, 1)) x ^ y
Prints an array value to the console during JIT execution and returns the input unchanged. Useful for debugging.
nv_print(operand)nv_print(operand)
operand |
( |
arrayish
Returns operand unchanged.
prim_print() for the underlying primitive.
x <- nv_array(c(1, 2, 3)) nv_print(x)x <- nv_array(c(1, 2, 3)) nv_print(x)
Promote arrays to a common data type, see common_dtype for more details.
nv_promote_to_common(...)nv_promote_to_common(...)
... |
( |
(list() of arrayish)
x <- nv_array(1L) y <- nv_array(1.5) # integer is promoted to float nv_promote_to_common(x, y)x <- nv_array(1L) y <- nv_array(1.5) # integer is promoted to float nv_promote_to_common(x, y)
Computes the reduced QR decomposition of a matrix operand:
where has orthonormal columns () and
is upper triangular.
For an input with , has
shape and has shape .
nv_qr(operand) ## S3 method for class 'AnvlArray' qr(x, ...)nv_qr(operand) ## S3 method for class 'AnvlArray' qr(x, ...)
operand |
( |
x |
( |
... |
No additional arguments. |
Named list with elements Q (shape (m, k)) and R
(shape (k, n)), where (m, n) = shape(operand) and
k = min(m, n). Both have the same data type as operand.
x <- nv_matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, dtype = "f32") nv_qr(x)x <- nv_matrix(c(1, 2, 3, 4, 5, 6), nrow = 3, dtype = "f32") nv_qr(x)
Computes the probs quantile(s) of an array along a dimension.
probs follows the same scalar-vs-array convention as nv_select()'s
index:
a length-1 numeric (e.g. 0.5) treats probs as scalar — the output
has dim removed, like a reduction;
a 1-D R array (e.g. array(c(0.25, 0.5, 0.75))) prepends a leading
dimension of size length(probs).
Plain length-K (K > 1) vectors are rejected; wrap with array() to
make the array intent explicit.
nv_quantile( operand, probs, dim = NULL, interpolation = "linear", nan_rm = FALSE )nv_quantile( operand, probs, dim = NULL, interpolation = "linear", nan_rm = FALSE )
operand |
( |
probs |
( |
dim |
( |
interpolation |
( |
nan_rm |
( |
arrayish
For scalar probs: same shape as x with dim removed. For
array probs: a leading dimension of size length(probs) is
prepended.
Let h = (n - 1) * q be the 0-based fractional index for an axis of
length n and probability q, with lo = floor(h), hi = ceil(h),
frac = h - lo. Then:
"linear" (default): (1 - frac) * sorted[lo] + frac * sorted[hi].
"lower": sorted[lo] — the lower bracket of linear.
"higher": sorted[hi] — the upper bracket of linear.
"nearest": sorted[lo] if frac < 0.5 else sorted[hi].
"midpoint": (sorted[lo] + sorted[hi]) / 2.
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_quantile(x, 0.5) # = nv_median(x) nv_quantile(x, array(c(0.25, 0.5, 0.75))) nv_quantile(x, 0.5, interpolation = "lower") nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5) nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5, nan_rm = TRUE)x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_quantile(x, 0.5) # = nv_median(x) nv_quantile(x, array(c(0.25, 0.5, 0.75))) nv_quantile(x, 0.5, interpolation = "lower") nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5) nv_quantile(nv_array(c(1, NaN, 3, 5)), 0.5, nan_rm = TRUE)
Samples from a binomial distribution with trials and success probability .
When n = 1 (the default), this is a Bernoulli distribution.
nv_rbinom(shape, initial_state, n = 1L, prob = 0.5, dtype = "i32")nv_rbinom(shape, initial_state, n = 1L, prob = 0.5, dtype = "i32")
shape |
( |
initial_state |
( |
n |
( |
prob |
( |
dtype |
( |
(list() of arrayish)
List of two elements: the updated RNG state and the sampled values.
Other rng:
nv_rdunif(),
nv_rng_state(),
nv_rnorm(),
nv_runif()
state <- nv_rng_state(42L) # Bernoulli samples result <- nv_rbinom(c(2, 3), state) result[[2]]state <- nv_rng_state(42L) # Bernoulli samples result <- nv_rbinom(c(2, 3), state) result[[2]]
Samples integers from 1 to n with equal probability (with replacement),
analogous to R's sample.int(n, size, replace = TRUE).
nv_rdunif(shape, initial_state, n, dtype = "i32")nv_rdunif(shape, initial_state, n, dtype = "i32")
shape |
( |
initial_state |
( |
n |
( |
dtype |
( |
(list() of arrayish)
List of two elements: the updated RNG state and the sampled integers.
Other rng:
nv_rbinom(),
nv_rng_state(),
nv_rnorm(),
nv_runif()
state <- nv_rng_state(42L) # Roll 6 dice result <- nv_rdunif(6, state, n = 6L) result[[2]]state <- nv_rng_state(42L) # Roll 6 dice result <- nv_rdunif(6, state, n = 6L) result[[2]]
Loads arrays from a file in the safetensors format.
nv_read(path, device = NULL, backend = default_backend())nv_read(path, device = NULL, backend = default_backend())
path |
( |
device |
( |
backend |
( |
This is a convenience wrapper around nv_unserialize() that opens and
closes a file connection.
Named list of AnvlArray objects.
nv_save(), nv_serialize(), nv_unserialize()
x <- nv_matrix(1:6, nrow = 2) x path <- tempfile(fileext = ".safetensors") nv_save(list(x = x), path) nv_read(path)x <- nv_matrix(1:6, nrow = 2) x path <- tempfile(fileext = ".safetensors") nv_save(list(x = x), path) nv_read(path)
Performs logical AND along the specified dimensions.
Returns TRUE only if all elements are TRUE.
nv_reduce_all(operand, dims = NULL, drop = TRUE)nv_reduce_all(operand, dims = NULL, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Boolean array.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_all() for the underlying primitive.
x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) nv_reduce_all(x) # all dims -> scalar nv_reduce_all(x, dims = 1L)x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) nv_reduce_all(x) # all dims -> scalar nv_reduce_all(x, dims = 1L)
Performs logical OR along the specified dimensions.
Returns TRUE if any element is TRUE.
nv_reduce_any(operand, dims = NULL, drop = TRUE)nv_reduce_any(operand, dims = NULL, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Boolean array.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_any() for the underlying primitive.
x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) nv_reduce_any(x) # all dims -> scalar nv_reduce_any(x, dims = 1L)x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) nv_reduce_any(x) # all dims -> scalar nv_reduce_any(x, dims = 1L)
Finds the maximum of array elements along the specified dimensions.
nv_reduce_max(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)nv_reduce_max(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
nan_rm |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_max() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_reduce_max(x) # all dims -> scalar nv_reduce_max(x, dims = 1L) nv_reduce_max(nv_array(c(1, NaN, 3))) nv_reduce_max(nv_array(c(1, NaN, 3)), nan_rm = TRUE)x <- nv_matrix(1:6, nrow = 2) nv_reduce_max(x) # all dims -> scalar nv_reduce_max(x, dims = 1L) nv_reduce_max(nv_array(c(1, NaN, 3))) nv_reduce_max(nv_array(c(1, NaN, 3)), nan_rm = TRUE)
Finds the minimum of array elements along the specified dimensions.
nv_reduce_min(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)nv_reduce_min(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
nan_rm |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_min() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_reduce_min(x) # all dims -> scalar nv_reduce_min(x, dims = 1L) nv_reduce_min(nv_array(c(1, NaN, 3))) nv_reduce_min(nv_array(c(1, NaN, 3)), nan_rm = TRUE)x <- nv_matrix(1:6, nrow = 2) nv_reduce_min(x) # all dims -> scalar nv_reduce_min(x, dims = 1L) nv_reduce_min(nv_array(c(1, NaN, 3))) nv_reduce_min(nv_array(c(1, NaN, 3)), nan_rm = TRUE)
Multiplies array elements along the specified dimensions.
nv_reduce_prod(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)nv_reduce_prod(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
nan_rm |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_prod() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_reduce_prod(x) # all dims -> scalar nv_reduce_prod(x, dims = 1L) nv_reduce_prod(nv_array(c(2, NaN, 3))) nv_reduce_prod(nv_array(c(2, NaN, 3)), nan_rm = TRUE)x <- nv_matrix(1:6, nrow = 2) nv_reduce_prod(x) # all dims -> scalar nv_reduce_prod(x, dims = 1L) nv_reduce_prod(nv_array(c(2, NaN, 3))) nv_reduce_prod(nv_array(c(2, NaN, 3)), nan_rm = TRUE)
Sums array elements along the specified dimensions.
nv_reduce_sum(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)nv_reduce_sum(operand, dims = NULL, drop = TRUE, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
nan_rm |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
prim_reduce_sum() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) nv_reduce_sum(x) # all dims -> scalar nv_reduce_sum(x, dims = 1L) nv_reduce_sum(nv_array(c(1, NaN, 3))) nv_reduce_sum(nv_array(c(1, NaN, 3)), nan_rm = TRUE)x <- nv_matrix(1:6, nrow = 2) nv_reduce_sum(x) # all dims -> scalar nv_reduce_sum(x, dims = 1L) nv_reduce_sum(nv_array(c(1, NaN, 3))) nv_reduce_sum(nv_array(c(1, NaN, 3)), nan_rm = TRUE)
Element-wise remainder.
This differs from base R's %%, use nv_mod()/%% instead.
nv_remainder(lhs, rhs)nv_remainder(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
nv_mod() for the flooring remainder, prim_remainder() for the
underlying primitive.
x <- nv_array(c(7, 8, 9)) y <- nv_array(c(3, 3, 4)) nv_remainder(x, y)x <- nv_array(c(7, 8, 9)) y <- nv_array(c(3, 3, 4)) nv_remainder(x, y)
Reshapes an array to a new shape without changing the underlying data. Returns the input unchanged if it already has the target shape.
nv_reshape(operand, shape)nv_reshape(operand, shape)
operand |
( |
shape |
( |
Note that row-major order is used, which differs from R's column-major order.
arrayish
Has the given shape and the same data type as operand.
prim_reshape() for the underlying primitive.
x <- nv_array(1:6) nv_reshape(x, c(2, 3))x <- nv_array(1:6) nv_reshape(x, c(2, 3))
Reverses the order of elements along specified dimensions.
nv_reverse(operand, dims)nv_reverse(operand, dims)
operand |
( |
dims |
( |
arrayish
Has the same shape and data type as operand.
prim_reverse() for the underlying primitive.
x <- nv_array(c(1, 2, 3, 4, 5)) nv_reverse(x, dims = 1L)x <- nv_array(c(1, 2, 3, 4, 5)) nv_reverse(x, dims = 1L)
Creates an initial RNG state from a seed. This state is required by all random sampling functions and is updated after each call.
nv_rng_state(seed, device = default_device())nv_rng_state(seed, device = default_device())
seed |
( |
device |
( |
nv_array of dtype ui64 and shape (2).
Other rng:
nv_rbinom(),
nv_rdunif(),
nv_rnorm(),
nv_runif()
state <- nv_rng_state(42L) statestate <- nv_rng_state(42L) state
Samples from a normal distribution with mean and standard deviation
using the Box-Muller transform.
nv_rnorm(shape, initial_state, dtype = "f32", mu = 0, sigma = 1)nv_rnorm(shape, initial_state, dtype = "f32", mu = 0, sigma = 1)
shape |
( |
initial_state |
( |
dtype |
( |
mu |
( |
sigma |
( |
(list() of arrayish)
List of two elements: the updated RNG state and the sampled values.
To implement a covariance structure use Cholesky decomposition.
Other rng:
nv_rbinom(),
nv_rdunif(),
nv_rng_state(),
nv_runif()
state <- nv_rng_state(42L) result <- nv_rnorm(c(2, 3), state) result[[2]]state <- nv_rng_state(42L) result <- nv_rnorm(c(2, 3), state) result[[2]]
Element-wise rounding. You can also use the round() generic.
nv_round(operand, method = "nearest_even")nv_round(operand, method = "nearest_even")
operand |
( |
method |
( |
arrayish
Has the same shape and data type as the input.
prim_round() for the underlying primitive.
x <- nv_array(c(1.4, 2.5, 3.6)) round(x)x <- nv_array(c(1.4, 2.5, 3.6)) round(x)
Element-wise reciprocal square root, i.e. 1 / sqrt(x).
nv_rsqrt(operand)nv_rsqrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_rsqrt() for the underlying primitive.
x <- nv_array(c(1, 4, 9)) nv_rsqrt(x)x <- nv_array(c(1, 4, 9)) nv_rsqrt(x)
Samples from a uniform distribution in the open interval (lower, upper).
nv_runif(shape, initial_state, dtype = "f32", lower = 0, upper = 1)nv_runif(shape, initial_state, dtype = "f32", lower = 0, upper = 1)
shape |
( |
initial_state |
( |
dtype |
( |
lower, upper
|
( |
(list() of arrayish)
List of two elements: the updated RNG state and the sampled values.
Other rng:
nv_rbinom(),
nv_rdunif(),
nv_rng_state(),
nv_rnorm()
state <- nv_rng_state(42L) result <- nv_runif(c(2, 3), state) result[[2]]state <- nv_rng_state(42L) result <- nv_runif(c(2, 3), state) result[[2]]
Saves a named list of arrays to a file in the safetensors format.
nv_save(arrays, path)nv_save(arrays, path)
arrays |
(named |
path |
( |
This is a convenience wrapper around nv_serialize() that opens and closes
a file connection.
NULL (invisibly).
nv_read(), nv_serialize(), nv_unserialize()
x <- nv_matrix(1:6, nrow = 2) x path <- tempfile(fileext = ".safetensors") nv_save(list(x = x), path) nv_read(path)x <- nv_matrix(1:6, nrow = 2) x path <- tempfile(fileext = ".safetensors") nv_save(list(x = x), path) nv_read(path)
Computes the standard deviation along the specified dimensions.
nv_sd(operand, dims, drop = TRUE, correction = 1L, nan_rm = FALSE)nv_sd(operand, dims, drop = TRUE, correction = 1L, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
correction |
( |
nan_rm |
( |
Uses Bessel's correction by default (correction = 1), matching R's sd().
Set correction = 0 for population standard deviation.
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
x <- nv_array(c(1, 2, 3, 4, 5)) nv_sd(x, dims = 1L)x <- nv_array(c(1, 2, 3, 4, 5)) nv_sd(x, dims = 1L)
Picks one or more elements along dimension dim of operand.
Use this instead of [ or nv_subset when the index to select is provided
programmatically.
nv_select(operand, dim, index)nv_select(operand, dim, index)
operand |
( |
dim |
( |
index |
( |
arrayish
Same data type as operand. dim is dropped if index was scalar.
nv_subset() for general subsetting, prim_static_slice().
m <- nv_matrix(1:6, nrow = 2) nv_select(m, dim = 2L, index = 2L) nv_select(m, dim = 1L, index = 1L) nv_select(m, dim = 2L, index = array(c(1L, 3L)))m <- nv_matrix(1:6, nrow = 2) nv_select(m, dim = 2L, index = 2L) nv_select(m, dim = 1L, index = 1L) nv_select(m, dim = 2L, index = array(c(1L, 3L)))
Creates a 1-D array with values from start to end (inclusive).
Without steps, behaves like R's seq(start, end) producing integer values.
With steps, produces steps evenly spaced values (like seq(start, end, length.out = steps)).
nv_seq_like() is a variant where dtype, ambiguous, and device
default to those of like.
nv_seq( start, end, steps = NULL, dtype = NULL, ambiguous = FALSE, device = NULL ) nv_seq_like( like, start, end, steps = NULL, dtype = NULL, ambiguous = NULL, device = NULL )nv_seq( start, end, steps = NULL, dtype = NULL, ambiguous = FALSE, device = NULL ) nv_seq_like( like, start, end, steps = NULL, dtype = NULL, ambiguous = NULL, device = NULL )
start, end
|
( |
steps |
( |
dtype |
( |
ambiguous |
( |
device |
( |
like |
( |
arrayish
1-D array of length end - start + 1.
nv_seq(3, 7) x <- nv_array(c(1, 2, 3), dtype = "f64") nv_seq_like(x, 1, 5)nv_seq(3, 7) x <- nv_array(c(1, 2, 3), dtype = "f64") nv_seq_like(x, 1, 5)
Serializes a named list of arrays into the safetensors format.
nv_serialize(arrays, con = NULL)nv_serialize(arrays, con = NULL)
arrays |
(named |
con |
( |
The ambiguity of the arrays is stored in the metadata and preserved in write-read roundtrips.
A raw vector if con is NULL, otherwise NULL (invisibly).
nv_unserialize(), nv_save(), nv_read()
x <- nv_matrix(1:6, nrow = 2) x raw_data <- nv_serialize(list(x = x)) raw_data nv_unserialize(raw_data)x <- nv_matrix(1:6, nrow = 2) x raw_data <- nv_serialize(list(x = x)) raw_data nv_unserialize(raw_data)
Element-wise left bit shift.
nv_shift_left(lhs, rhs)nv_shift_left(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_shift_left() for the underlying primitive.
x <- nv_array(c(1L, 2L, 4L)) y <- nv_array(c(1L, 2L, 1L)) nv_shift_left(x, y)x <- nv_array(c(1L, 2L, 4L)) y <- nv_array(c(1L, 2L, 1L)) nv_shift_left(x, y)
Element-wise arithmetic right bit shift.
nv_shift_right_arithmetic(lhs, rhs)nv_shift_right_arithmetic(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_shift_right_arithmetic() for the underlying primitive.
x <- nv_array(c(8L, -16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) nv_shift_right_arithmetic(x, y)x <- nv_array(c(8L, -16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) nv_shift_right_arithmetic(x, y)
Element-wise logical right bit shift.
nv_shift_right_logical(lhs, rhs)nv_shift_right_logical(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_shift_right_logical() for the underlying primitive.
x <- nv_array(c(8L, 16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) nv_shift_right_logical(x, y)x <- nv_array(c(8L, 16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) nv_shift_right_logical(x, y)
Element-wise sign function. You can also use sign().
nv_sign(operand)nv_sign(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_sign() for the underlying primitive.
x <- nv_array(c(-3, 0, 5)) sign(x)x <- nv_array(c(-3, 0, 5)) sign(x)
Element-wise sine. You can also use sin().
nv_sin(operand)nv_sin(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_sin() for the underlying primitive.
x <- nv_array(c(0, pi / 2, pi)) sin(x)x <- nv_array(c(0, pi / 2, pi)) sin(x)
Element-wise hyperbolic sine. You can also use sinh().
nv_sinh(operand)nv_sinh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_sinh() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) sinh(x)x <- nv_array(c(-1, 0, 1)) sinh(x)
Solves the linear system a %*% x = b for x. Uses LU decomposition
with partial pivoting internally, so a need only be square and
non-singular.
nv_solve(a, b) ## S3 method for class 'AnvlArray' solve(a, b, ...)nv_solve(a, b) ## S3 method for class 'AnvlArray' solve(a, b, ...)
a |
( |
b |
( |
... |
No additional arguments. |
arrayish
The solution x such that a %*% x = b.
a: (n, n)
b: (n,) or (n, k)
output: same shape as b
nv_chol(), nv_triangular_solve(), prim_lu()
a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") b <- nv_matrix(c(1, 2), nrow = 2, dtype = "f64") nv_solve(a, b)a <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") b <- nv_matrix(c(1, 2), nrow = 2, dtype = "f64") nv_solve(a, b)
Sorts an array along a dimension.
You can also use sort() directly.
nv_sort(operand, dim = NULL, decreasing = FALSE, stable = FALSE) ## S3 method for class 'AnvlArray' sort(x, decreasing = FALSE, ..., dim = NULL)nv_sort(operand, dim = NULL, decreasing = FALSE, stable = FALSE) ## S3 method for class 'AnvlArray' sort(x, decreasing = FALSE, ..., dim = NULL)
operand |
( |
dim |
( |
decreasing |
( |
stable |
( |
x |
( |
... |
No additional arguments. |
arrayish
Same shape and data type as operand.
NaN values sort to the end (ascending) or beginning
(descending), regardless of sign. +0 and -0 compare equal.
prim_sort() for the underlying primitive,
nv_argsort(), nv_top_k(), nv_median(),
nv_argmax(), nv_argmin().
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_sort(x) sort(x) # via the S3 generic nv_sort(x, decreasing = TRUE) m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE) nv_sort(m, dim = 2L)x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_sort(x) sort(x) # via the S3 generic nv_sort(x, decreasing = TRUE) m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE) nv_sort(m, dim = 2L)
Element-wise square root. You can also use sqrt().
nv_sqrt(operand)nv_sqrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_sqrt() for the underlying primitive.
x <- nv_array(c(1, 4, 9)) sqrt(x)x <- nv_array(c(1, 4, 9)) sqrt(x)
Removes dimensions of size 1 from an array.
nv_squeeze(operand, dims = NULL)nv_squeeze(operand, dims = NULL)
operand |
( |
dims |
( |
arrayish
Has the same data type as operand with the specified dimensions removed.
x <- nv_array(1:6, shape = c(1, 6, 1)) nv_squeeze(x)x <- nv_array(1:6, shape = c(1, 6, 1)) nv_squeeze(x)
Extracts a slice from an array using static (compile-time) indices.
For dynamic indexing, use nv_subset() instead.
nv_static_slice(operand, start_indices, limit_indices, strides)nv_static_slice(operand, start_indices, limit_indices, strides)
operand |
( |
start_indices |
( |
limit_indices |
( |
strides |
( |
arrayish
Has the same data type as operand.
nv_subset(), prim_static_slice() for the underlying primitive.
x <- nv_array(1:10) nv_static_slice(x, start_indices = 2L, limit_indices = 5L, strides = 1L)x <- nv_array(1:10) nv_static_slice(x, start_indices = 2L, limit_indices = 5L, strides = 1L)
Subtracts two arrays element-wise. You can also use the - operator.
nv_sub(lhs, rhs)nv_sub(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_sub() for the underlying primitive.
x <- nv_array(c(4, 5, 6)) y <- nv_array(c(1, 2, 3)) x - yx <- nv_array(c(4, 5, 6)) y <- nv_array(c(1, 2, 3)) x - y
Computes the reduced ("economy") singular value decomposition of a
matrix operand of shape (m, n):
where u has orthonormal columns, vt has orthonormal rows, and d
is the length-k (k = min(m, n)) vector of non-negative singular
values in descending order.
Note: unlike base::svd(), which returns the right singular vectors
as v of shape (n, k) (so that a = u %*% diag(d) %*% t(v)), this
primitive returns them already transposed as vt of shape (k, n)
(matching the underlying LAPACK / cuSOLVER output and avoiding an
extra transpose).
Supports any matrix shape on both the host (LAPACK gesdd) and CUDA
(cuSOLVER gesvd) backends. cuSOLVER's m >= n requirement is handled
transparently via a layout flip for wide matrices.
nv_svd(operand)nv_svd(operand)
operand |
( |
Named list with elements d (length k), u (shape
(m, k)), and vt (shape (k, n)). All have the same dtype as
the input.
x <- nv_matrix(c(1, 0, 0, 1, 0, 1), nrow = 3, dtype = "f64") nv_svd(x)x <- nv_matrix(c(1, 0, 0, 1, 0, 1), nrow = 3, dtype = "f64") nv_svd(x)
Element-wise tangent. You can also use tan().
nv_tan(operand)nv_tan(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_tan() for the underlying primitive.
x <- nv_array(c(0, 0.5, 1)) tan(x)x <- nv_array(c(0, 0.5, 1)) tan(x)
Element-wise hyperbolic tangent. You can also use tanh().
nv_tanh(operand)nv_tanh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
prim_tanh() for the underlying primitive.
x <- nv_array(c(-1, 0, 1)) tanh(x)x <- nv_array(c(-1, 0, 1)) tanh(x)
Computes lhs %*% t(rhs). If rhs is missing, computes lhs %*% t(lhs).
nv_tcrossprod(lhs, rhs = NULL) ## S3 method for class 'AnvlArray' tcrossprod(x, y = NULL, ...)nv_tcrossprod(lhs, rhs = NULL) ## S3 method for class 'AnvlArray' tcrossprod(x, y = NULL, ...)
lhs |
( |
rhs |
( |
x, y
|
Same as |
... |
No additional arguments. |
x <- nv_matrix(1:6, nrow = 2, dtype = "f32") nv_tcrossprod(x)x <- nv_matrix(1:6, nrow = 2, dtype = "f32") nv_tcrossprod(x)
Returns the k largest values along a dimension, sorted in decreasing order.
nv_top_k(operand, k, dim = NULL, with_indices = FALSE)nv_top_k(operand, k, dim = NULL, with_indices = FALSE)
operand |
( |
k |
( |
dim |
( |
with_indices |
( |
arrayish (when with_indices = FALSE) or named list of two
arrays (when with_indices = TRUE). Output shape matches operand with
dim resized to k; values are sorted decreasing along dim.
NaN ranks larger than any finite value (so it appears first in the
top-k output); -NaN ranks smaller. Unlike nv_sort(), the sign
bit is not canonicalized.
prim_top_k() for the underlying primitive, nv_sort().
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_top_k(x, k = 3L) nv_top_k(x, k = 3L, with_indices = TRUE) m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE) nv_top_k(m, k = 2L, dim = 2L)x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) nv_top_k(x, k = 3L) nv_top_k(x, k = 3L, with_indices = TRUE) m <- nv_matrix(c(3, 1, 5, 2, 4, 0), nrow = 2, byrow = TRUE) nv_top_k(m, k = 2L, dim = 2L)
Computes the trace (sum of diagonal elements) of a 2-D array.
nv_trace(operand)nv_trace(operand)
operand |
( |
arrayish
A scalar with the same data type as operand.
x <- nv_array(c(1, 0, 0, 0, 2, 0, 0, 0, 3), shape = c(3, 3)) nv_trace(x)x <- nv_array(c(1, 0, 0, 0, 2, 0, 0, 0, 3), shape = c(3, 3)) nv_trace(x)
Permutes the dimensions of an array. You can also use t() for matrices.
nv_transpose(operand, permutation = NULL) ## S3 method for class 'AnvlArray' t(x)nv_transpose(operand, permutation = NULL) ## S3 method for class 'AnvlArray' t(x)
operand |
( |
permutation |
( |
x |
( |
arrayish
Has the same data type as operand and shape nv_shape(operand)[permutation].
prim_transpose() for the underlying primitive.
x <- nv_matrix(1:6, nrow = 2) t(x)x <- nv_matrix(1:6, nrow = 2) t(x)
Solves a triangular system of linear equations. When left_side = TRUE,
returns x such that op(a) %*% x = b. When left_side = FALSE,
returns x such that x %*% op(a) = b. Here op is a or t(a)
depending on transpose_a.
nv_triangular_solve( a, b, left_side = TRUE, lower = TRUE, unit_diagonal = FALSE, transpose_a = FALSE )nv_triangular_solve( a, b, left_side = TRUE, lower = TRUE, unit_diagonal = FALSE, transpose_a = FALSE )
a |
( |
b |
(
|
left_side |
( |
lower |
( |
unit_diagonal |
( |
transpose_a |
( |
As a convenience, b may have one fewer dimension than a (a single
right-hand side per batch, shape (B..., n) for a of shape
(B..., n, n)). It is reshaped internally to a column (left_side = TRUE) or row (left_side = FALSE) and reshaped back on the way out.
Because we don't broadcast, this is not ambiguous (as it would be for NumPy).
arrayish
The solution x, with the same shape and dtype as b.
nv_solve(), nv_chol(), prim_triangular_solve()
L <- nv_matrix(c(2, 1, 0, 3), nrow = 2, dtype = "f32") b <- nv_matrix(c(4, 3), nrow = 2, dtype = "f32") nv_triangular_solve(L, b)L <- nv_matrix(c(2, 1, 0, 3), nrow = 2, dtype = "f32") b <- nv_matrix(c(4, 3), nrow = 2, dtype = "f32") nv_triangular_solve(L, b)
Returns the lower triangular part of a 2-D array, setting elements above the specified diagonal to zero.
nv_tril(operand, diagonal = 0L)nv_tril(operand, diagonal = 0L)
operand |
( |
diagonal |
( |
arrayish
Has the same shape and data type as operand.
x <- nv_fill(1, c(3, 3)) nv_tril(x)x <- nv_fill(1, c(3, 3)) nv_tril(x)
Returns the upper triangular part of a 2-D array, setting elements below the specified diagonal to zero.
nv_triu(operand, diagonal = 0L)nv_triu(operand, diagonal = 0L)
operand |
( |
diagonal |
( |
arrayish
Has the same shape and data type as operand.
x <- nv_fill(1, c(3, 3)) nv_triu(x)x <- nv_fill(1, c(3, 3)) nv_triu(x)
Element-wise truncation (round toward zero). You can also use trunc().
nv_trunc(operand)nv_trunc(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
nv_floor(), nv_ceiling(), nv_round().
x <- nv_array(c(1.2, 2.7, -1.5)) trunc(x)x <- nv_array(c(1.2, 2.7, -1.5)) trunc(x)
Deserializes arrays from the safetensors format.
nv_unserialize(con, device = NULL, backend = default_backend())nv_unserialize(con, device = NULL, backend = default_backend())
con |
(connection | |
device |
( |
backend |
( |
The data type, shape, and ambiguity of each array are restored from the serialized data.
Named list of AnvlArray objects.
nv_serialize(), nv_save(), nv_read()
x <- nv_matrix(1:6, nrow = 2) x raw_data <- nv_serialize(list(x = x)) raw_data nv_unserialize(raw_data)x <- nv_matrix(1:6, nrow = 2) x raw_data <- nv_serialize(list(x = x)) raw_data nv_unserialize(raw_data)
Inserts a dimension of size 1 at the specified position.
nv_unsqueeze(operand, dim)nv_unsqueeze(operand, dim)
operand |
( |
dim |
( |
arrayish
Has the same data type as operand with an extra dimension of size 1.
x <- nv_array(c(1, 2, 3)) nv_unsqueeze(x, dim = 1L)x <- nv_array(c(1, 2, 3)) nv_unsqueeze(x, dim = 1L)
Computes the variance along the specified dimensions.
nv_var(operand, dims, drop = TRUE, correction = 1L, nan_rm = FALSE)nv_var(operand, dims, drop = TRUE, correction = 1L, nan_rm = FALSE)
operand |
( |
dims |
( |
drop |
( |
correction |
( |
nan_rm |
( |
Uses Bessel's correction by default (correction = 1), matching R's var().
Set correction = 0 for population variance.
arrayish
Has the same data type as the input.
When drop = TRUE, the reduced dimensions are removed.
When drop = FALSE, the reduced dimensions are set to 1.
x <- nv_array(c(1, 2, 3, 4, 5)) nv_var(x, dims = 1L) nv_var(nv_array(c(1, NaN, 3, 5)), dims = 1L, nan_rm = TRUE)x <- nv_array(c(1, 2, 3, 4, 5)) nv_var(x, dims = 1L) nv_var(nv_array(c(1, NaN, 3, 5)), dims = 1L, nan_rm = TRUE)
Executes a functional while loop.
nv_while(init, cond, body)nv_while(init, cond, body)
init |
( |
cond |
( |
body |
( |
Final state after the loop terminates (same structure as init).
prim_while() for the underlying primitive.
nv_while( init = list(i = nv_scalar(0L), total = nv_scalar(0L)), cond = function(i, total) i < 5L, body = function(i, total) list( i = i + 1L, total = total + i ) )nv_while( init = list(i = nv_scalar(0L), total = nv_scalar(0L)), cond = function(i, total) i < 5L, body = function(i, total) list( i = i + 1L, total = total + i ) )
Element-wise logical XOR.
nv_xor(lhs, rhs)nv_xor(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and the promoted common data type of the inputs.
prim_xor() for the underlying primitive.
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) nv_xor(x, y)x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) nv_xor(x, y)
Returns the platform name (e.g. "cpu", "cuda") identifying
the compute backend.
## S3 method for class 'AnvlArray' platform(x, ...) platform(x, ...)## S3 method for class 'AnvlArray' platform(x, ...) platform(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
Implemented via the generic pjrt::platform().
character(1)
x <- nv_array(1:4, dtype = "f32") platform(x)x <- nv_array(1:4, dtype = "f32") platform(x)
Apply a function leaf-wise over several trees with the same structure.
All trees in .l must have identical structure.
pmap_tree(.l, .f, ...)pmap_tree(.l, .f, ...)
.l |
( |
.f |
( |
... |
Additional arguments passed to |
A tree with the same structure as .l[[1]], where each leaf is
.f(leaf_1, leaf_2, ..., leaf_n ...).
map_tree(), flatten(), unflatten()
pmap_tree(list(list(a = 1, b = 2), list(a = 10, b = 20)), `+`)pmap_tree(list(list(a = 1, b = 2), list(a = 10, b = 20)), `+`)
Element-wise absolute value.
prim_abs(operand)prim_abs(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_abs().
x <- nv_array(c(-1, 2, -3)) prim_abs(x)x <- nv_array(c(-1, 2, -3)) prim_abs(x)
Element-wise inverse cosine.
prim_acos(operand)prim_acos(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_acos().
x <- nv_array(c(-1, 0, 1)) prim_acos(x)x <- nv_array(c(-1, 0, 1)) prim_acos(x)
Element-wise inverse hyperbolic cosine.
prim_acosh(operand)prim_acosh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_acosh().
x <- nv_array(c(1, 2, 10)) prim_acosh(x)x <- nv_array(c(1, 2, 10)) prim_acosh(x)
Adds two arrays element-wise.
prim_add(lhs, rhs)prim_add(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_add().
nv_add(), +
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_add(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_add(x, y)
Element-wise logical AND.
prim_and(lhs, rhs)prim_and(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_and().
nv_and(), &
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_and(x, y)x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_and(x, y)
Returns the index of the maximum value along a single dimension. Ties are broken by returning the smallest index.
prim_argmax(operand, dim, drop = TRUE)prim_argmax(operand, dim, drop = TRUE)
operand |
( |
dim |
( |
drop |
( |
arrayish of dtype i32
Same shape as operand with dim removed (or set to 1 if
drop = FALSE).
stablehlo
reverse
Lowers to a variadic stablehlo::hlo_reduce() over (values, indices)
with a (value > value | (value == value & idx < idx)) selector.
prim_argmax(nv_array(c(3, 1, 4, 1, 5)), dim = 1L)prim_argmax(nv_array(c(3, 1, 4, 1, 5)), dim = 1L)
Returns the index of the minimum value along a single dimension. Ties are broken by returning the smallest index.
prim_argmin(operand, dim, drop = TRUE)prim_argmin(operand, dim, drop = TRUE)
operand |
( |
dim |
( |
drop |
( |
arrayish of dtype i32
Same shape as operand with dim removed (or set to 1 if
drop = FALSE).
stablehlo
reverse
Lowers to a variadic stablehlo::hlo_reduce() over (values, indices)
with a (value < value | (value == value & idx < idx)) selector.
prim_argmin(nv_array(c(3, 1, 4, 1, 5)), dim = 1L)prim_argmin(nv_array(c(3, 1, 4, 1, 5)), dim = 1L)
Element-wise inverse sine.
prim_asin(operand)prim_asin(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_asin().
x <- nv_array(c(-1, 0, 1)) prim_asin(x)x <- nv_array(c(-1, 0, 1)) prim_asin(x)
Element-wise inverse hyperbolic sine.
prim_asinh(operand)prim_asinh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_asinh().
x <- nv_array(c(-1, 0, 1)) prim_asinh(x)x <- nv_array(c(-1, 0, 1)) prim_asinh(x)
Element-wise inverse tangent.
prim_atan(operand)prim_atan(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_atan().
x <- nv_array(c(-1, 0, 1)) prim_atan(x)x <- nv_array(c(-1, 0, 1)) prim_atan(x)
Element-wise atan2 operation.
prim_atan2(lhs, rhs)prim_atan2(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_atan2().
y <- nv_array(c(1, 0, -1)) x <- nv_array(c(0, 1, 0)) prim_atan2(y, x)y <- nv_array(c(1, 0, -1)) x <- nv_array(c(0, 1, 0)) prim_atan2(y, x)
Element-wise inverse hyperbolic tangent.
prim_atanh(operand)prim_atanh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_atanh().
x <- nv_array(c(-0.5, 0, 0.5)) prim_atanh(x)x <- nv_array(c(-0.5, 0, 0.5)) prim_atanh(x)
Reinterprets the bits of an array as a different data type without modifying the underlying data.
prim_bitcast_convert(operand, dtype)prim_bitcast_convert(operand, dtype)
operand |
( |
dtype |
( |
arrayish
Has the given dtype.
stablehlo
reverse
Lowers to stablehlo::hlo_bitcast_convert().
x <- nv_array(1L) prim_bitcast_convert(x, dtype = "i8") x <- nv_array(rep(1L, 4), dtype = "i8") prim_bitcast_convert(x, dtype = "i32")x <- nv_array(1L) prim_bitcast_convert(x, dtype = "i8") x <- nv_array(rep(1L, 4), dtype = "i8") prim_bitcast_convert(x, dtype = "i32")
Broadcasts an array to a new shape by replicating the data along new or size-1 dimensions.
prim_broadcast_in_dim(operand, shape, broadcast_dimensions)prim_broadcast_in_dim(operand, shape, broadcast_dimensions)
operand |
( |
shape |
( |
broadcast_dimensions |
( |
arrayish
Has the same data type as the input and the given shape.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_broadcast_in_dim().
x <- nv_array(c(1, 2, 3)) prim_broadcast_in_dim(x, shape = c(2, 3), broadcast_dimensions = 2L)x <- nv_array(c(1, 2, 3)) prim_broadcast_in_dim(x, shape = c(2, 3), broadcast_dimensions = 2L)
Element-wise cube root.
prim_cbrt(operand)prim_cbrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_cbrt().
x <- nv_array(c(1, 8, 27)) prim_cbrt(x)x <- nv_array(c(1, 8, 27)) prim_cbrt(x)
Element-wise ceiling.
prim_ceil(operand)prim_ceil(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_ceil().
x <- nv_array(c(1.2, 2.7, -1.5)) prim_ceil(x)x <- nv_array(c(1.2, 2.7, -1.5)) prim_ceil(x)
Computes the Cholesky decomposition of a symmetric positive-definite matrix. Dimensions before the last two are batch dimensions.
prim_chol(operand, lower = FALSE)prim_chol(operand, lower = FALSE)
operand |
( |
lower |
( |
arrayish
Has the same shape and data type as the input.
The values in the triangle not specified by lower are implementation-defined.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_cholesky().
Murray, Iain (2016). “Differentiation of the Cholesky decomposition.” arXiv preprint arXiv:1602.07527.
Walter, Sebastian (2012). Structured higher-order algorithmic differentiation in the forward and reverse mode with application in optimum experimental design. Ph.D. thesis, Mathematisch-Naturwissenschaftliche Fakult"at II.
# Create a positive-definite matrix x <- nv_matrix(c(4, 2, 2, 3), nrow = 2, dtype = "f32") prim_chol(x, lower = TRUE)# Create a positive-definite matrix x <- nv_matrix(c(4, 2, 2, 3), nrow = 2, dtype = "f32") prim_chol(x, lower = TRUE)
Clamps every element of operand to the range [min_val, max_val],
i.e. max(min_val, min(operand, max_val)).
prim_clamp(min_val, operand, max_val)prim_clamp(min_val, operand, max_val)
min_val |
( |
operand |
( |
max_val |
( |
arrayish
Has the same data type and shape as operand.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_clamp().
x <- nv_array(c(-1, 0.5, 2)) prim_clamp(nv_scalar(0), x, nv_scalar(1))x <- nv_array(c(-1, 0.5, 2)) prim_clamp(nv_scalar(0), x, nv_scalar(1))
Concatenates arrays along a dimension.
prim_concatenate(..., dimension)prim_concatenate(..., dimension)
... |
( |
dimension |
( |
arrayish
Has the same data type as the inputs.
The output shape matches the inputs in all dimensions except dimension,
which is the sum of the input sizes along that dimension.
It is ambiguous if all inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_concatenate().
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_concatenate(x, y, dimension = 1L)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_concatenate(x, y, dimension = 1L)
Converts the elements of an array to a different data type.
prim_convert(operand, dtype, ambiguous = FALSE)prim_convert(operand, dtype, ambiguous = FALSE)
operand |
( |
dtype |
( |
ambiguous |
( |
arrayish
Has the given dtype and the same shape as operand.
Ambiguity is controlled by the ambiguous parameter.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_convert().
x <- nv_array(c(1L, 2L, 3L)) prim_convert(x, dtype = "f32")x <- nv_array(c(1L, 2L, 3L)) prim_convert(x, dtype = "f32")
Element-wise cosine.
prim_cos(operand)prim_cos(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_cosine().
x <- nv_array(c(0, pi / 2, pi)) prim_cos(x)x <- nv_array(c(0, pi / 2, pi)) prim_cos(x)
Element-wise hyperbolic cosine.
prim_cosh(operand)prim_cosh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_cosh().
x <- nv_array(c(-1, 0, 1)) prim_cosh(x)x <- nv_array(c(-1, 0, 1)) prim_cosh(x)
Running maximum of array elements along a single dimension along with
the index of the last occurrence of the running maximum.
At output position j, the values output is max(input[1:j]) and the
indices output is the largest i in 1:j with
input[i] == values[j] (last-occurrence tiebreak).
prim_cummax(operand, dim)prim_cummax(operand, dim)
operand |
( |
dim |
( |
list of two arrayish values:
The running maximum (same dtype as operand) and the
running argmax (dtype i32, 1-based). Both have
the same shape as operand.
stablehlo
reverse
Lowers to a variadic stablehlo::hlo_reduce_window() over (values, iota).
x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) prim_cummax(x, dim = 1L)x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) prim_cummax(x, dim = 1L)
Running minimum of array elements along a single dimension along with
the index of the last occurrence of the running minimum.
At output position j, the values output is min(input[1:j]) and the
indices output is the largest i in 1:j with
input[i] == values[j] (last-occurrence tiebreak).
prim_cummin(operand, dim)prim_cummin(operand, dim)
operand |
( |
dim |
( |
list of two arrayish values:
The running minimum (same dtype as operand) and the
running argmin (dtype i32, 1-based). Both have
the same shape as operand.
stablehlo
reverse
Lowers to a variadic stablehlo::hlo_reduce_window() over (values, iota).
x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) prim_cummin(x, dim = 1L)x <- nv_matrix(c(3, 1, 4, 1, 5, 9), nrow = 2) prim_cummin(x, dim = 1L)
Cumulative product of array elements along a single dimension.
Output position j along dim equals the product of input positions 1:j.
prim_cumprod(operand, dim)prim_cumprod(operand, dim)
operand |
( |
dim |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
Lowers to stablehlo::hlo_reduce_window() with stablehlo::hlo_multiply() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_cumprod(x, dim = 1L)x <- nv_matrix(1:6, nrow = 2) prim_cumprod(x, dim = 1L)
Cumulative sum of array elements along a single dimension.
Output position j along dim equals the sum of input positions 1:j.
prim_cumsum(operand, dim)prim_cumsum(operand, dim)
operand |
( |
dim |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_reduce_window() with stablehlo::hlo_add() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_cumsum(x, dim = 1L)x <- nv_matrix(1:6, nrow = 2) prim_cumsum(x, dim = 1L)
Element-wise digamma function (logarithmic derivative of the gamma function).
prim_digamma(operand)prim_digamma(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_digamma().
x <- nv_array(c(0.5, 1, 2, 5)) prim_digamma(x)x <- nv_array(c(0.5, 1, 2, 5)) prim_digamma(x)
Divides two arrays element-wise.
prim_div(lhs, rhs)prim_div(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_divide().
nv_div(), /
x <- nv_array(c(10, 20, 30)) y <- nv_array(c(2, 5, 10)) prim_div(x, y)x <- nv_array(c(10, 20, 30)) y <- nv_array(c(2, 5, 10)) prim_div(x, y)
General dot product of two arrays, supporting contraction over arbitrary dimensions and batching.
prim_dot_general(lhs, rhs, contracting_dims, batching_dims)prim_dot_general(lhs, rhs, contracting_dims, batching_dims)
lhs, rhs
|
( |
contracting_dims |
( |
batching_dims |
( |
arrayish
The output shape is the batch dimensions followed by the remaining
(non-contracted, non-batched) dimensions of lhs, then rhs.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_dot_general().
nv_matmul(), %*%
x <- nv_matrix(1:6, nrow = 2) y <- nv_matrix(1:6, nrow = 3) prim_dot_general(x, y, contracting_dims = list(2L, 1L), batching_dims = list(integer(0), integer(0)) )x <- nv_matrix(1:6, nrow = 2) y <- nv_matrix(1:6, nrow = 3) prim_dot_general(x, y, contracting_dims = list(2L, 1L), batching_dims = list(integer(0), integer(0)) )
Extracts a slice from an array whose start position is determined at
runtime via array-valued indices. The slice shape (slice_sizes) is
a fixed R integer vector.
Use prim_static_slice() instead when all indices are known at compile
time and you need stride support.
prim_dynamic_slice(operand, ..., slice_sizes)prim_dynamic_slice(operand, ..., slice_sizes)
operand |
( |
... |
( |
slice_sizes |
( |
arrayish
Has the same data type as the input and shape slice_sizes.
It is ambiguous if the input is ambiguous.
Start indices are clamped before the slice is extracted:
adjusted_start_indices = clamp(1, start_indices, nv_shape(operand) - slice_sizes + 1).
This means that out-of-bounds indices will not cause an error, but
the effective start position may differ from the requested one.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_dynamic_slice().
prim_static_slice(), prim_dynamic_update_slice(), prim_scatter(), prim_gather(), nv_subset(), [
# 1-D: extract 3 elements starting at position 3 x <- nv_array(1:10) start <- nv_scalar(3L) prim_dynamic_slice(x, start, slice_sizes = 3L) # 2-D: extract a 2x2 block from a matrix x <- nv_matrix(1:12, nrow = 3, ncol = 4) row_start <- nv_scalar(2L) col_start <- nv_scalar(1L) prim_dynamic_slice(x, row_start, col_start, slice_sizes = c(2L, 2L))# 1-D: extract 3 elements starting at position 3 x <- nv_array(1:10) start <- nv_scalar(3L) prim_dynamic_slice(x, start, slice_sizes = 3L) # 2-D: extract a 2x2 block from a matrix x <- nv_matrix(1:12, nrow = 3, ncol = 4) row_start <- nv_scalar(2L) col_start <- nv_scalar(1L) prim_dynamic_slice(x, row_start, col_start, slice_sizes = c(2L, 2L))
Returns a copy of operand with a slice replaced by update at a
runtime-determined position. This is the write counterpart of
prim_dynamic_slice(): dynamic slice reads a block from an array,
while dynamic update slice writes a block into an array.
prim_dynamic_update_slice(operand, update, ...)prim_dynamic_update_slice(operand, update, ...)
operand |
( |
update |
( |
... |
( |
arrayish
Has the same data type and shape as operand.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_dynamic_update_slice().
Start indices are clamped before the slice is extracted:
adjusted_start_indices = clamp(1, start_indices, nv_shape(operand) - slice_sizes + 1).
This means that out-of-bounds indices will not cause an error, but
the effective start position may differ from the requested one.
prim_dynamic_slice(), prim_scatter(), prim_gather(), nv_subset_assign(), [<-
# 1-D: overwrite two elements starting at position 2 x <- nv_array(1:5) update <- nv_array(c(10L, 20L)) start <- nv_scalar(2L) prim_dynamic_update_slice(x, update, start) # 2-D: write a 2x2 block into a 3x4 matrix x <- nv_fill(0L, shape = c(3, 4)) update <- nv_matrix(c(1L, 2L, 3L, 4L), nrow = 2, ncol = 2) row_start <- nv_scalar(2L) col_start <- nv_scalar(3L) prim_dynamic_update_slice(x, update, row_start, col_start)# 1-D: overwrite two elements starting at position 2 x <- nv_array(1:5) update <- nv_array(c(10L, 20L)) start <- nv_scalar(2L) prim_dynamic_update_slice(x, update, start) # 2-D: write a 2x2 block into a 3x4 matrix x <- nv_fill(0L, shape = c(3, 4)) update <- nv_matrix(c(1L, 2L, 3L, 4L), nrow = 2, ncol = 2) row_start <- nv_scalar(2L) col_start <- nv_scalar(3L) prim_dynamic_update_slice(x, update, row_start, col_start)
Computes the eigendecomposition of a symmetric matrix operand of
shape (n, n):
Only the lower triangle of operand is read. The columns of vectors
are the (orthonormal) eigenvectors and values is the length-n
vector of (real) eigenvalues in ascending order. Output names and
order match base::eigen().
prim_eigh(operand)prim_eigh(operand)
operand |
( |
Named list with elements values (length n) and vectors
(shape (n, n)). Both have the same dtype as the input.
stablehlo
Lowers to stablehlo::hlo_custom_call() with target "eigh".
x <- nv_array(c(2, 1, 1, 2), shape = c(2, 2), dtype = "f64") prim_eigh(x)x <- nv_array(c(2, 1, 1, 2), shape = c(2, 2), dtype = "f64") prim_eigh(x)
Element-wise equality comparison.
prim_eq(lhs, rhs)prim_eq(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "EQ".
nv_eq(), ==
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) prim_eq(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) prim_eq(x, y)
Element-wise error function erf(x) = (2 / sqrt(pi)) * integral_0^x exp(-t^2) dt.
prim_erf(operand)prim_erf(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_erf().
x <- nv_array(c(-1, 0, 1)) prim_erf(x)x <- nv_array(c(-1, 0, 1)) prim_erf(x)
Element-wise inverse error function (the inverse of erf on (-1, 1)).
prim_erf_inv(operand)prim_erf_inv(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_erf_inv().
x <- nv_array(c(-0.5, 0, 0.5)) prim_erf_inv(x)x <- nv_array(c(-0.5, 0, 0.5)) prim_erf_inv(x)
Element-wise complementary error function erfc(x) = 1 - erf(x).
prim_erfc(operand)prim_erfc(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_erfc().
x <- nv_array(c(-1, 0, 1)) prim_erfc(x)x <- nv_array(c(-1, 0, 1)) prim_erfc(x)
Element-wise exponential.
prim_exp(operand)prim_exp(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_exponential().
x <- nv_array(c(0, 1, 2)) prim_exp(x)x <- nv_array(c(0, 1, 2)) prim_exp(x)
Element-wise exp(x) - 1, more accurate for small x.
prim_expm1(operand)prim_expm1(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_exponential_minus_one().
x <- nv_array(c(0, 0.001, 1)) prim_expm1(x)x <- nv_array(c(0, 0.001, 1)) prim_expm1(x)
Creates an array of a given shape and data type, filled with a scalar value.
The advantage of using this function instead of e.g. doing
nv_array(1, shape = c(100, 100)) is that lowering of prim_fill() is
efficiently represented in the compiled program, while the latter uses
100 * 100 * 4 bytes of memory.
prim_fill(value, shape, dtype, ambiguous = FALSE, device = NULL)prim_fill(value, shape, dtype, ambiguous = FALSE, device = NULL)
value |
( |
shape |
( |
dtype |
( |
ambiguous |
( |
device |
( |
arrayish
Has the given shape and dtype.
stablehlo
quickr
Lowers to stablehlo::hlo_tensor().
prim_fill(3.14, shape = c(2, 3), dtype = "f32")prim_fill(3.14, shape = c(2, 3), dtype = "f32")
Element-wise floor.
prim_floor(operand)prim_floor(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_floor().
x <- nv_array(c(1.2, 2.7, -1.5)) prim_floor(x)x <- nv_array(c(1.2, 2.7, -1.5)) prim_floor(x)
Gathers slices from the operand array at positions specified by
start_indices. Each index vector in start_indices identifies a
starting position in operand, and a slice of size slice_sizes is
extracted from that position. The gathered slices are assembled into
the output array.
This is the inverse of prim_scatter(): gather reads slices from a
array at given indices, while scatter writes slices into an array at
given indices.
prim_gather( operand, start_indices, slice_sizes, offset_dims, collapsed_slice_dims, operand_batching_dims, start_indices_batching_dims, start_index_map, index_vector_dim, indices_are_sorted = FALSE, unique_indices = FALSE )prim_gather( operand, start_indices, slice_sizes, offset_dims, collapsed_slice_dims, operand_batching_dims, start_indices_batching_dims, start_index_map, index_vector_dim, indices_are_sorted = FALSE, unique_indices = FALSE )
operand |
( |
start_indices |
( |
slice_sizes |
( |
offset_dims |
( |
collapsed_slice_dims |
( |
operand_batching_dims |
( |
start_indices_batching_dims |
( |
start_index_map |
( |
index_vector_dim |
( |
indices_are_sorted |
( |
unique_indices |
( |
arrayish
Has the same data type as operand. The output shape is composed
of the offset dimensions (from the slice) and the remaining
dimensions from start_indices. See the underluing stableHLO function
for more details.
Start indices are clamped before the slice is extracted:
clamp(1, start_index, nv_shape(operand) - slice_sizes + 1).
This means that out-of-bounds indices will not cause an error, but
the effective start position may differ from the requested one.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_gather().
prim_scatter(), nv_subset(), nv_subset_assign(), [, [<-
# Gather rows 1 and 3 from a 3x3 matrix operand <- nv_matrix(1:9, nrow = 3) indices <- nv_matrix(c(1L, 3L), ncol = 1) prim_gather( operand, indices, slice_sizes = c(1L, 3L), offset_dims = 2L, collapsed_slice_dims = 1L, operand_batching_dims = integer(0), start_indices_batching_dims = integer(0), start_index_map = 1L, index_vector_dim = 2L )# Gather rows 1 and 3 from a 3x3 matrix operand <- nv_matrix(1:9, nrow = 3) indices <- nv_matrix(c(1L, 3L), ncol = 1) prim_gather( operand, indices, slice_sizes = c(1L, 3L), offset_dims = 2L, collapsed_slice_dims = 1L, operand_batching_dims = integer(0), start_indices_batching_dims = integer(0), start_index_map = 1L, index_vector_dim = 2L )
Element-wise greater than or equal comparison.
prim_ge(lhs, rhs)prim_ge(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "GE".
nv_ge(), >=
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_ge(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_ge(x, y)
Element-wise greater than comparison.
prim_gt(lhs, rhs)prim_gt(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "GT".
nv_gt(), >
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_gt(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_gt(x, y)
Conditional execution of one of two branches based on a scalar boolean
predicate. Unlike prim_ifelse() which operates element-wise, this
evaluates only the selected branch.
prim_if(pred, true, false)prim_if(pred, true, false)
pred |
( |
true, false
|
( |
Result of the executed branch.
An output is ambiguous if it is ambiguous in both branches.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_if().
prim_if(nv_scalar(TRUE), \() nv_scalar(1), \() nv_scalar(2))prim_if(nv_scalar(TRUE), \() nv_scalar(1), \() nv_scalar(2))
Element-wise selection based on a boolean predicate, like R's ifelse().
For each element, returns the corresponding element from true_value where
pred is TRUE and from false_value where pred is FALSE.
prim_ifelse(pred, true_value, false_value)prim_ifelse(pred, true_value, false_value)
pred |
( |
true_value, false_value
|
( |
arrayish
Has the same dtype and shape as true_value.
It is ambiguous if both true_value and false_value are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_select().
pred <- nv_array(c(TRUE, FALSE, TRUE)) prim_ifelse(pred, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6)))pred <- nv_array(c(TRUE, FALSE, TRUE)) prim_ifelse(pred, nv_array(c(1, 2, 3)), nv_array(c(4, 5, 6)))
Creates an array with values increasing along the specified dimension.
prim_iota(dim, dtype, shape, start = 1L, ambiguous = FALSE, device = NULL)prim_iota(dim, dtype, shape, start = 1L, ambiguous = FALSE, device = NULL)
dim |
( |
dtype |
( |
shape |
( |
start |
( |
ambiguous |
( |
device |
( |
arrayish
Has the given dtype and shape.
stablehlo
quickr
Lowers to stablehlo::hlo_iota().
prim_iota(dim = 1L, dtype = "i32", shape = 5L)prim_iota(dim = 1L, dtype = "i32", shape = 5L)
Element-wise check if values are finite (not Inf, -Inf, or NaN).
prim_is_finite(operand)prim_is_finite(operand)
operand |
( |
arrayish
Has the same shape as the input and boolean data type.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_is_finite().
x <- nv_array(c(1, Inf, NaN, -Inf, 0)) prim_is_finite(x)x <- nv_array(c(1, Inf, NaN, -Inf, 0)) prim_is_finite(x)
Element-wise less than or equal comparison.
prim_le(lhs, rhs)prim_le(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "LE".
nv_le(), <=
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_le(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_le(x, y)
Element-wise natural logarithm of the absolute value of the gamma function.
prim_lgamma(operand)prim_lgamma(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_lgamma().
x <- nv_array(c(0.5, 1, 2, 5)) prim_lgamma(x)x <- nv_array(c(0.5, 1, 2, 5)) prim_lgamma(x)
Element-wise natural logarithm.
prim_log(operand)prim_log(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_log().
x <- nv_array(c(1, 2.718, 7.389)) prim_log(x)x <- nv_array(c(1, 2.718, 7.389)) prim_log(x)
Element-wise log(1 + x), more accurate for small x.
prim_log1p(operand)prim_log1p(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_log_plus_one().
x <- nv_array(c(0, 0.001, 1)) prim_log1p(x)x <- nv_array(c(0, 0.001, 1)) prim_log1p(x)
Element-wise logistic sigmoid: 1 / (1 + exp(-x)).
prim_logistic(operand)prim_logistic(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_logistic().
x <- nv_array(c(-2, 0, 2)) prim_logistic(x)x <- nv_array(c(-2, 0, 2)) prim_logistic(x)
Element-wise less than comparison.
prim_lt(lhs, rhs)prim_lt(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "LT".
nv_lt(), <
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_lt(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(3, 2, 1)) prim_lt(x, y)
Computes the partial-pivoted LU decomposition of a matrix operand:
where is a permutation matrix, is unit lower triangular,
and is upper triangular. L (with implicit unit diagonal) and
U are packed into a single LU output matching LAPACK's getrf
layout. is returned in two equivalent forms: pivots (LAPACK's
sequential row-swap encoding) and permutation (an explicit
permutation vector).
prim_lu(operand)prim_lu(operand)
operand |
( |
list of three arrayish values: LU (m, n) with the same
dtype as the input; pivots (k,) of dtype i32 with
k = min(m, n) (1-based row swaps such that row i was exchanged
with row pivots[i] during elimination step i); and permutation
(m,) of dtype i32, a 1-based permutation vector for such
that (P %*% A)[i, ] equals A[permutation[i], ].
stablehlo
Lowers to a "lu" stablehlo::hlo_custom_call() (backed by LAPACK on
CPU and cuSOLVER on CUDA) for LU and pivots, followed by a
stablehlo::hlo_while() loop that converts pivots to permutation
in-graph.
x <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") prim_lu(x)x <- nv_matrix(c(4, 3, 6, 3), nrow = 2, dtype = "f64") prim_lu(x)
Element-wise maximum of two arrays.
prim_max(lhs, rhs)prim_max(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_maximum().
x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) prim_max(x, y)x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) prim_max(x, y)
Element-wise minimum of two arrays.
prim_min(lhs, rhs)prim_min(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_minimum().
x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) prim_min(x, y)x <- nv_array(c(1, 5, 3)) y <- nv_array(c(4, 2, 6)) prim_min(x, y)
Multiplies two arrays element-wise.
prim_mul(lhs, rhs)prim_mul(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_multiply().
nv_mul(), *
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_mul(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_mul(x, y)
Element-wise inequality comparison.
prim_ne(lhs, rhs)prim_ne(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape as the inputs and boolean data type.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_compare() with comparison_direction = "NE".
nv_ne(), !=
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) prim_ne(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(1, 3, 2)) prim_ne(x, y)
Negates an array element-wise.
prim_negate(operand)prim_negate(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_negate().
nv_negate(), unary -
x <- nv_array(c(1, -2, 3)) prim_negate(x)x <- nv_array(c(1, -2, 3)) prim_negate(x)
Element-wise logical NOT.
prim_not(operand)prim_not(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_not().
x <- nv_array(c(TRUE, FALSE, TRUE)) prim_not(x)x <- nv_array(c(TRUE, FALSE, TRUE)) prim_not(x)
Element-wise logical OR.
prim_or(lhs, rhs)prim_or(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_or().
nv_or(), |
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_or(x, y)x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_or(x, y)
Pads an array with a given padding value.
prim_pad( operand, padding_value, edge_padding_low, edge_padding_high, interior_padding )prim_pad( operand, padding_value, edge_padding_low, edge_padding_high, interior_padding )
operand |
( |
padding_value |
( |
edge_padding_low |
( |
edge_padding_high |
( |
interior_padding |
( |
arrayish
Has the same data type as operand.
For the output shape see the underlying stablehlo documentation (stablehlo::hlo_pad()).
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_pad().
x <- nv_array(c(1, 2, 3)) prim_pad(x, nv_scalar(0), edge_padding_low = 2L, edge_padding_high = 1L, interior_padding = 0L )x <- nv_array(c(1, 2, 3)) prim_pad(x, nv_scalar(0), edge_padding_low = 2L, edge_padding_high = 1L, interior_padding = 0L )
Element-wise polygamma function: the (n+1)-th derivative of the
log-gamma function. Both n and x must have the same shape; n
typically holds non-negative integer values.
prim_polygamma(n, x)prim_polygamma(n, x)
n, x
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_polygamma().
n <- nv_array(c(1, 1, 2)) x <- nv_array(c(0.5, 1, 2)) prim_polygamma(n, x)n <- nv_array(c(1, 1, 2)) x <- nv_array(c(0.5, 1, 2)) prim_polygamma(n, x)
Element-wise population count (number of set bits).
prim_popcnt(operand)prim_popcnt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_popcnt().
x <- nv_array(c(7L, 3L, 15L)) prim_popcnt(x)x <- nv_array(c(7L, 3L, 15L)) prim_popcnt(x)
Raises lhs to the power of rhs element-wise.
prim_pow(lhs, rhs)prim_pow(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_power().
nv_pow(), ^
x <- nv_array(c(2, 3, 4)) y <- nv_array(c(3, 2, 1)) prim_pow(x, y)x <- nv_array(c(2, 3, 4)) y <- nv_array(c(3, 2, 1)) prim_pow(x, y)
Prints an array value to the console during execution and returns the input unchanged. This is useful for debugging JIT-compiled code.
prim_print(operand)prim_print(operand)
operand |
( |
arrayish
Returns operand as-is.
stablehlo
Lowers to stablehlo::hlo_custom_call().
x <- nv_array(c(1, 2, 3)) prim_print(x)x <- nv_array(c(1, 2, 3)) prim_print(x)
Computes the reduced QR decomposition of a matrix operand:
where has orthonormal columns () and
is upper triangular.
For an input with , has
shape and has shape .
prim_qr(operand)prim_qr(operand)
operand |
( |
Named list with elements Q (shape (m, k)) and R
(shape (k, n)), where (m, n) = shape(operand) and
k = min(m, n). Both have the same data type as operand.
stablehlo
Lowers to a "geqrf" + "orgqr" stablehlo::hlo_custom_call() pair
(backed by LAPACK on CPU and cuSOLVER on CUDA) + postprocessing.
x <- nv_array(1:6, shape = c(3, 2), dtype = "f32") prim_qr(x)x <- nv_array(1:6, shape = c(3, 2), dtype = "f32") prim_qr(x)
Reduces an array along the specified dimensions using a user-supplied associative reducer.
prim_reduce(operand, init, dims, drop = TRUE, reductor)prim_reduce(operand, init, dims, drop = TRUE, reductor)
operand |
( |
init |
( |
dims |
( |
drop |
( |
reductor |
( |
arrayish
Same data type as operand. Shape is operand with dims removed
(or set to 1 if drop = FALSE).
The order in which reductor is applied across the reduction window is
implementation-defined. If the reductor is not associative, the result
is ill-defined.
Furthermore, init must be the neutral element for this reductor.
Because floating point math is non-associative, the output of
the reduction can differ between backends (GPU, CPU), even if the underlying mathematical
function (like +) is associative.
stablehlo
Lowers to stablehlo::hlo_reduce() with reductor as the body.
prim_reduce_sum(), prim_reduce_max()
x <- nv_array(c(1, 2, 3, 4)) prim_reduce(x, init = nv_scalar(0), dims = 1L, reductor = prim_add) prim_reduce(x, init = nv_scalar(1), dims = 1L, reductor = prim_mul)x <- nv_array(c(1, 2, 3, 4)) prim_reduce(x, init = nv_scalar(0), dims = 1L, reductor = prim_add) prim_reduce(x, init = nv_scalar(1), dims = 1L, reductor = prim_mul)
Performs logical AND along the specified dimensions.
prim_reduce_all(operand, dims, drop = TRUE)prim_reduce_all(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Boolean array. Never ambiguous.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_and() as the reducer.
x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) prim_reduce_all(x, dims = 1L)x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) prim_reduce_all(x, dims = 1L)
Performs logical OR along the specified dimensions.
prim_reduce_any(operand, dims, drop = TRUE)prim_reduce_any(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Boolean array. Never ambiguous.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_or() as the reducer.
x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) prim_reduce_any(x, dims = 1L)x <- nv_matrix(c(TRUE, FALSE, TRUE, TRUE), nrow = 2) prim_reduce_any(x, dims = 1L)
Finds the maximum of array elements along the specified dimensions.
prim_reduce_max(operand, dims, drop = TRUE)prim_reduce_max(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_maximum() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_reduce_max(x, dims = 1L)x <- nv_matrix(1:6, nrow = 2) prim_reduce_max(x, dims = 1L)
Finds the minimum of array elements along the specified dimensions.
prim_reduce_min(operand, dims, drop = TRUE)prim_reduce_min(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_minimum() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_reduce_min(x, dims = 1L)x <- nv_matrix(1:6, nrow = 2) prim_reduce_min(x, dims = 1L)
Multiplies array elements along the specified dimensions.
prim_reduce_prod(operand, dims, drop = TRUE)prim_reduce_prod(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_multiply() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_reduce_prod(x, dims = 1L)x <- nv_matrix(1:6, nrow = 2) prim_reduce_prod(x, dims = 1L)
Sums array elements along the specified dimensions.
prim_reduce_sum(operand, dims, drop = TRUE)prim_reduce_sum(operand, dims, drop = TRUE)
operand |
( |
dims |
( |
drop |
( |
arrayish
Has the same data type as the input.
When drop = TRUE, the shape is that of operand with dims removed.
When drop = FALSE, the shape is that of operand with dims set to 1.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reduce() with stablehlo::hlo_add() as the reducer.
x <- nv_matrix(1:6, nrow = 2) prim_reduce_sum(x, dims = 1L)x <- nv_matrix(1:6, nrow = 2) prim_reduce_sum(x, dims = 1L)
Element-wise remainder.
Result has sign of the divident, which differs from base R's %%, which is available
via nv_mod() and has sign of divisor.
prim_remainder(lhs, rhs)prim_remainder(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_remainder().
prim_remainder(1, -3) 1 %% -3prim_remainder(1, -3) 1 %% -3
Reshapes an array to a new shape without changing the underlying data. Note that row-major order is used, which differs from R's column-major order.
prim_reshape(operand, shape)prim_reshape(operand, shape)
operand |
( |
shape |
( |
arrayish
Has the same data type as the input and the given shape.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reshape().
x <- nv_array(1:6) prim_reshape(x, shape = c(2, 3))x <- nv_array(1:6) prim_reshape(x, shape = c(2, 3))
Reverses the order of elements along specified dimensions.
prim_reverse(operand, dims)prim_reverse(operand, dims)
operand |
( |
dims |
( |
arrayish
Has the same data type and shape as operand.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_reverse().
x <- nv_array(c(1, 2, 3, 4, 5)) prim_reverse(x, dims = 1L)x <- nv_array(c(1, 2, 3, 4, 5)) prim_reverse(x, dims = 1L)
Generates pseudo-random numbers using the specified algorithm and returns the updated RNG state together with the generated values.
prim_rng_bit_generator( initial_state, rng_algorithm = "THREE_FRY", dtype, shape )prim_rng_bit_generator( initial_state, rng_algorithm = "THREE_FRY", dtype, shape )
initial_state |
( |
rng_algorithm |
( |
dtype |
( |
shape |
( |
list of two arrayish values:
The first element is the updated RNG state with the same dtype and shape
as initial_state. The second element is an array of random values with
the given dtype and shape.
stablehlo
Lowers to stablehlo::hlo_rng_bit_generator().
state <- nv_array(c(0L, 0L), dtype = "ui64") prim_rng_bit_generator(state, dtype = "f32", shape = c(3, 2))state <- nv_array(c(0L, 0L), dtype = "ui64") prim_rng_bit_generator(state, dtype = "f32", shape = c(3, 2))
Rounds the elements of an array to the nearest integer.
prim_round(operand, method = "nearest_even")prim_round(operand, method = "nearest_even")
operand |
( |
method |
( |
arrayish
Has the same dtype and shape as operand.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_round_nearest_even() or
stablehlo::hlo_round_nearest_afz() depending on the method parameter.
x <- nv_array(c(1.4, 2.5, 3.6)) prim_round(x)x <- nv_array(c(1.4, 2.5, 3.6)) prim_round(x)
Element-wise reciprocal square root.
prim_rsqrt(operand)prim_rsqrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_rsqrt().
x <- nv_array(c(1, 4, 9)) prim_rsqrt(x)x <- nv_array(c(1, 4, 9)) prim_rsqrt(x)
Produces a result array identical to input except that slices at
positions specified by scatter_indices are updated with values from
the update array. When multiple indices point to the same location,
the update_computation function determines how to combine the values
(by default the new value replaces the old one).
This is the inverse of prim_gather(): gather reads slices from an array
at given indices, while scatter writes slices into an array at given
indices.
prim_scatter( input, scatter_indices, update, update_window_dims, inserted_window_dims, input_batching_dims, scatter_indices_batching_dims, scatter_dims_to_operand_dims, index_vector_dim, indices_are_sorted = FALSE, unique_indices = FALSE, update_computation = NULL )prim_scatter( input, scatter_indices, update, update_window_dims, inserted_window_dims, input_batching_dims, scatter_indices_batching_dims, scatter_dims_to_operand_dims, index_vector_dim, indices_are_sorted = FALSE, unique_indices = FALSE, update_computation = NULL )
input |
( |
scatter_indices |
( |
update |
( |
update_window_dims |
( |
inserted_window_dims |
( |
input_batching_dims |
( |
scatter_indices_batching_dims |
( |
scatter_dims_to_operand_dims |
( |
index_vector_dim |
( |
indices_are_sorted |
( |
unique_indices |
( |
update_computation |
( |
arrayish
Has the same data type and shape as input.
It is ambiguous if input is ambiguous.
If a computed result index falls outside the bounds of input, the
update for that index is silently ignored.
When multiple indices in scatter_indices map to the same element
of input, the order in which update_computation is applied is
implementation-defined and may vary between plugins ("cpu", "cuda").
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_scatter().
prim_gather(), nv_subset(), nv_subset_assign(), [, [<-
# Scatter values 10 and 30 into positions 1 and 3 of a zero vector input <- nv_array(c(0, 0, 0, 0, 0)) indices <- nv_matrix(c(1L, 3L), ncol = 1) updates <- nv_array(c(10, 30)) prim_scatter( input, indices, updates, update_window_dims = integer(0), inserted_window_dims = 1L, input_batching_dims = integer(0), scatter_indices_batching_dims = integer(0), scatter_dims_to_operand_dims = 1L, index_vector_dim = 2L )# Scatter values 10 and 30 into positions 1 and 3 of a zero vector input <- nv_array(c(0, 0, 0, 0, 0)) indices <- nv_matrix(c(1L, 3L), ncol = 1) updates <- nv_array(c(10, 30)) prim_scatter( input, indices, updates, update_window_dims = integer(0), inserted_window_dims = 1L, input_batching_dims = integer(0), scatter_indices_batching_dims = integer(0), scatter_dims_to_operand_dims = 1L, index_vector_dim = 2L )
Element-wise left bit shift.
prim_shift_left(lhs, rhs)prim_shift_left(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_shift_left().
x <- nv_array(c(1L, 2L, 4L)) y <- nv_array(c(1L, 2L, 1L)) prim_shift_left(x, y)x <- nv_array(c(1L, 2L, 4L)) y <- nv_array(c(1L, 2L, 1L)) prim_shift_left(x, y)
Element-wise arithmetic right bit shift.
prim_shift_right_arithmetic(lhs, rhs)prim_shift_right_arithmetic(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_shift_right_arithmetic().
x <- nv_array(c(8L, -16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) prim_shift_right_arithmetic(x, y)x <- nv_array(c(8L, -16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) prim_shift_right_arithmetic(x, y)
Element-wise logical right bit shift.
prim_shift_right_logical(lhs, rhs)prim_shift_right_logical(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_shift_right_logical().
x <- nv_array(c(8L, 16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) prim_shift_right_logical(x, y)x <- nv_array(c(8L, 16L, 32L)) y <- nv_array(c(1L, 2L, 3L)) prim_shift_right_logical(x, y)
Element-wise sign.
prim_sign(operand)prim_sign(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_sign().
x <- nv_array(c(-3, 0, 5)) prim_sign(x)x <- nv_array(c(-3, 0, 5)) prim_sign(x)
Element-wise sine.
prim_sin(operand)prim_sin(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_sine().
x <- nv_array(c(0, pi / 2, pi)) prim_sin(x)x <- nv_array(c(0, pi / 2, pi)) prim_sin(x)
Element-wise hyperbolic sine.
prim_sinh(operand)prim_sinh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_sinh().
x <- nv_array(c(-1, 0, 1)) prim_sinh(x)x <- nv_array(c(-1, 0, 1)) prim_sinh(x)
Sorts arrays along the given dimension.
Sorting is determined by the first operand only: it is the sort key,
and any additional operands are reordered with the same permutation
that sorts the first. This enables idioms like argsort (sort x
paired with an iota and read off the second output) and key-value
sorts (sort keys paired with values).
All operands must have the same shape; their dtypes may differ.
1-dimensional slices along dim are sorted independently; other
dimensions are preserved.
prim_sort(operands, dim = 1L, descending = FALSE, is_stable = FALSE)prim_sort(operands, dim = 1L, descending = FALSE, is_stable = FALSE)
operands |
( |
dim |
( |
descending |
( |
is_stable |
( |
list of arrayish
One sorted output per element of operands, in the same order. Each
output has the same shape, data type, and ambiguity as the
corresponding input.
stablehlo
reverse
Lowers to stablehlo::hlo_sort() with a comparator that uses
stablehlo::hlo_compare() (LT for ascending, GT for descending) on
the first operand. For float keys the comparator uses
compare_type = "TOTALORDER" and canonicalizes -0/+0 and
-NaN/+NaN to their positive form before comparing, so all NaN
values land at one end of the result regardless of sign. Integer keys
use SIGNED / UNSIGNED as appropriate.
nv_sort(), nv_argsort(), nv_top_k(), nv_median()
x <- nv_array(c(3, 1, 4, 1, 5)) prim_sort(list(x), dim = 1L)[[1L]] # Sort indices by the values (argsort): pair x with iota and read off # the second result. idx <- nv_iota(dim = 1L, dtype = "i64", shape = 5L) out <- prim_sort(list(x, idx), dim = 1L) out[[1L]] # sorted x out[[2L]] # permutation indicesx <- nv_array(c(3, 1, 4, 1, 5)) prim_sort(list(x), dim = 1L)[[1L]] # Sort indices by the values (argsort): pair x with iota and read off # the second result. idx <- nv_iota(dim = 1L, dtype = "i64", shape = 5L) out <- prim_sort(list(x, idx), dim = 1L) out[[1L]] # sorted x out[[2L]] # permutation indices
Element-wise square root.
prim_sqrt(operand)prim_sqrt(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_sqrt().
x <- nv_array(c(1, 4, 9)) prim_sqrt(x)x <- nv_array(c(1, 4, 9)) prim_sqrt(x)
Extracts a slice from an array using static (compile-time) indices. All indices, limits, and strides are fixed R integers.
Use prim_dynamic_slice() instead when the start position must be
computed at runtime (e.g. depends on array values).
prim_static_slice(operand, start_indices, limit_indices, strides)prim_static_slice(operand, start_indices, limit_indices, strides)
operand |
( |
start_indices |
( |
limit_indices |
( |
strides |
( |
arrayish
Has the same data type as the input and shape
ceiling((limit_indices - start_indices + 1) / strides).
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_slice().
prim_dynamic_slice(), prim_scatter(), prim_gather(), nv_subset(), [
# 1-D: extract elements 2 through 4 (limit is exclusive) x <- nv_array(1:10) prim_static_slice(x, start_indices = 2L, limit_indices = 5L, strides = 1L) # 1-D: every other element using strides x <- nv_array(1:10) prim_static_slice(x, start_indices = 1L, limit_indices = 10L, strides = 2L) # 2-D: extract a submatrix (rows 1-2, columns 2-3) x <- nv_matrix(1:12, nrow = 3, ncol = 4) prim_static_slice(x, start_indices = c(1L, 2L), limit_indices = c(3L, 4L), strides = c(1L, 1L) )# 1-D: extract elements 2 through 4 (limit is exclusive) x <- nv_array(1:10) prim_static_slice(x, start_indices = 2L, limit_indices = 5L, strides = 1L) # 1-D: every other element using strides x <- nv_array(1:10) prim_static_slice(x, start_indices = 1L, limit_indices = 10L, strides = 2L) # 2-D: extract a submatrix (rows 1-2, columns 2-3) x <- nv_matrix(1:12, nrow = 3, ncol = 4) prim_static_slice(x, start_indices = c(1L, 2L), limit_indices = c(3L, 4L), strides = c(1L, 1L) )
Subtracts two arrays element-wise.
prim_sub(lhs, rhs)prim_sub(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_subtract().
nv_sub(), -
x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_sub(x, y)x <- nv_array(c(1, 2, 3)) y <- nv_array(c(4, 5, 6)) prim_sub(x, y)
Computes the reduced ("economy") singular value decomposition of a
matrix operand of shape (m, n):
where u has orthonormal columns, vt has orthonormal rows, and d
is the length-k (k = min(m, n)) vector of non-negative singular
values in descending order.
Note: unlike base::svd(), which returns the right singular vectors
as v of shape (n, k) (so that a = u %*% diag(d) %*% t(v)), this
primitive returns them already transposed as vt of shape (k, n)
(matching the underlying LAPACK / cuSOLVER output and avoiding an
extra transpose).
Supports any matrix shape on both the host (LAPACK gesdd) and CUDA
(cuSOLVER gesvd) backends. cuSOLVER's m >= n requirement is handled
transparently via a layout flip for wide matrices.
prim_svd(operand)prim_svd(operand)
operand |
( |
Named list with elements d (length k), u (shape
(m, k)), and vt (shape (k, n)). All have the same dtype as
the input.
stablehlo
Lowers to stablehlo::hlo_custom_call() with target "svd".
x <- nv_array(c(1, 0, 0, 1, 0, 1), shape = c(3, 2)) prim_svd(x)x <- nv_array(c(1, 0, 0, 1, 0, 1), shape = c(3, 2)) prim_svd(x)
Element-wise tangent.
prim_tan(operand)prim_tan(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_tan().
x <- nv_array(c(0, 0.5, 1)) prim_tan(x)x <- nv_array(c(0, 0.5, 1)) prim_tan(x)
Element-wise hyperbolic tangent.
prim_tanh(operand)prim_tanh(operand)
operand |
( |
arrayish
Has the same shape and data type as the input.
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_tanh().
x <- nv_array(c(-1, 0, 1)) prim_tanh(x)x <- nv_array(c(-1, 0, 1)) prim_tanh(x)
Returns the k largest values along the last dimension, sorted in
descending order, together with their indices into that dimension.
For other dimensions, transpose so the target dimension is last, call
prim_top_k(), then transpose back. nv_top_k() does this.
prim_top_k(operand, k)prim_top_k(operand, k)
operand |
( |
k |
( |
list of two arrayish values:
The top-k values (same dtype as operand) and their indices along
the last dimension (dtype i32, matching JAX). Both have the same
shape as operand with the last dimension replaced by k. Ties are
broken by lower index first.
stablehlo
reverse
Lowers to stablehlo::hlo_top_k().
x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) prim_top_k(x, k = 3L)x <- nv_array(c(3, 1, 4, 1, 5, 9, 2, 6)) prim_top_k(x, k = 3L)
Permutes the dimensions of an array.
prim_transpose(operand, permutation)prim_transpose(operand, permutation)
operand |
( |
permutation |
( |
arrayish
Has the same data type as the input and shape nv_shape(operand)[permutation].
It is ambiguous if the input is ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_transpose().
x <- nv_matrix(1:6, nrow = 2) prim_transpose(x, permutation = c(2L, 1L))x <- nv_matrix(1:6, nrow = 2) prim_transpose(x, permutation = c(2L, 1L))
Solves a system of linear equations with a triangular coefficient matrix.
When left_side is TRUE, solves op(a) %*% x = b for x.
When left_side is FALSE, solves x %*% op(a) = b for x.
Dimensions before the last two are batch dimensions and must match
between a and b (no broadcasting).
Here op is A or A^T depending on transpose_a.
prim_triangular_solve(a, b, left_side, lower, unit_diagonal, transpose_a)prim_triangular_solve(a, b, left_side, lower, unit_diagonal, transpose_a)
a |
( |
b |
( |
left_side |
( |
lower |
( |
unit_diagonal |
( |
transpose_a |
( |
arrayish
Has the same shape and data type as b.
It is ambiguous if both a and b are ambiguous.
stablehlo
reverse
Lowers to stablehlo::hlo_triangular_solve().
Giles, Mike (2008). “An extended collection of matrix derivative results for forward and reverse mode automatic differentiation.” Oxford University Computing Laboratory.
# Solve L %*% x = b where L is lower triangular L <- nv_matrix(c(2, 0, 1, 3), nrow = 2, dtype = "f32") b <- nv_matrix(c(4, 3), nrow = 2, dtype = "f32") prim_triangular_solve(L, b, left_side = TRUE, lower = TRUE, unit_diagonal = FALSE, transpose_a = FALSE )# Solve L %*% x = b where L is lower triangular L <- nv_matrix(c(2, 0, 1, 3), nrow = 2, dtype = "f32") b <- nv_matrix(c(4, 3), nrow = 2, dtype = "f32") prim_triangular_solve(L, b, left_side = TRUE, lower = TRUE, unit_diagonal = FALSE, transpose_a = FALSE )
Repeatedly executes body while cond returns TRUE, like R's
while loop. The loop state is initialized with init and
passed through each iteration.
Otherwise, no state is maintained between iterations.
prim_while(init, cond, body)prim_while(init, cond, body)
init |
( |
cond |
( |
body |
( |
Named list with the same structure as init containing the
final state after the loop terminates.
stablehlo
quickr
Lowers to stablehlo::hlo_while().
prim_while( init = list(i = nv_scalar(0L), total = nv_scalar(0L)), cond = function(i, total) i <= 5L, body = function(i, total) list( i = i + 1L, total = total + i ) )prim_while( init = list(i = nv_scalar(0L), total = nv_scalar(0L)), cond = function(i, total) i <= 5L, body = function(i, total) list( i = i + 1L, total = total + i ) )
Element-wise logical XOR.
prim_xor(lhs, rhs)prim_xor(lhs, rhs)
lhs, rhs
|
( |
arrayish
Has the same shape and data type as the inputs.
It is ambiguous if both inputs are ambiguous.
stablehlo
quickr
reverse
Lowers to stablehlo::hlo_xor().
x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_xor(x, y)x <- nv_array(c(TRUE, FALSE, TRUE)) y <- nv_array(c(TRUE, TRUE, FALSE)) prim_xor(x, y)
Call of a primitive in an AnvlGraph.
PrimitiveCall(primitive, inputs, params, outputs)PrimitiveCall(primitive, inputs, params, outputs)
primitive |
( |
inputs |
( |
params |
( |
outputs |
( |
(PrimitiveCall)
Device descriptor for the quickr backend. The only supported type is
"cpu".
quickr_device(x = "cpu")quickr_device(x = "cpu")
x |
( |
A QuickrDevice object.
nv_device(), AnvlBackendQuickr().
Reassigns leaf indices so they form a contiguous sequence starting from the
current counter value. This is used internally after filtering nodes from a tree (e.g.
via filter_list_node()) to ensure leaf indices still map correctly to
positions in a flat list.
Not intended for direct use.
reindex_tree(x, counter)reindex_tree(x, counter)
x |
( |
counter |
(environment) |
A new Node with updated leaf indices.
Construct a reverse-mode autodiff rule for a primitive.
The backward argument should be provided if the forward call for
the primitive should run un-modified.
This covers most use-cases.
The backward argument should have this signature:
function(inputs, outputs, grads, params, required) -> list(input_grads).
In some scenarios, it can be beneficial to perform a slightly different forward pass
to enable a more efficient backward pass.
In this case, pass the forward argument.
It should return a list containing the results from the forward pass, as well as
closure that has the same signature as the one above.
It can make use of intermediate values computed in the forward pass via lexical scoping.
rule_reverse(backward = NULL, forward = NULL)rule_reverse(backward = NULL, forward = NULL)
backward |
( |
forward |
( |
An anvl_rule_reverse object.
Returns the shape of an array as an integer() vector.
shape(x, ...)shape(x, ...)
x |
( |
... |
Additional arguments passed to methods (unused). |
This is implemented via the generic tengen::shape().
integer()
x <- nv_array(1:4, dtype = "f32") shape(x)x <- nv_array(1:4, dtype = "f32") shape(x)
Constructs a Shape representing array dimensions.
Shape(dims = integer())Shape(dims = integer())
dims |
An |
A Shape object.
Shape(c(2L, 3L))Shape(c(2L, 3L))
Converts a traced AnvlGraph into the StableHLO intermediate representation (IR).
Each graph operation is translated to its corresponding StableHLO op. The result can
be serialized to MLIR text via stablehlo::repr() and subsequently compiled to an
XLA executable with pjrt::pjrt_compile().
The rules for translating to stablehlo are stored in $rules[["stablehlo"]] of the primitives.
This is a low-level function; most users should use jit() or xla() instead.
stablehlo( graph, constants_as_inputs = TRUE, env = NULL, donate = character(), platform = NULL )stablehlo( graph, constants_as_inputs = TRUE, env = NULL, donate = character(), platform = NULL )
graph |
( |
constants_as_inputs |
( |
env |
( |
donate |
( |
platform |
( |
A list of length 2:
the stablehlo::Func
The list of GraphValues holding ConcreteArrays.
trace_fn(), jit(), xla(), current_platform()
x <- nv_array(c(1, 2)) graph <- trace_fn(function(y) y + x, list(y = nv_aval("f32", shape = c()))) graph stablehlo(graph)x <- nv_array(c(1, 2)) graph <- trace_fn(function(y) y + x, list(y = nv_aval("f32", shape = c()))) graph stablehlo(graph)
Extracts subgraphs from the parameters of a higher-order primitive call.
subgraphs(call)subgraphs(call)
call |
( |
(list(AnvlGraph))
List of subgraphs found in the parameters.
Convert an object to its abstract array representation (AbstractArray).
to_abstract(x, pure = FALSE)to_abstract(x, pure = FALSE)
x |
( |
pure |
( |
# R literals become LiteralArrays (ambiguous by default, except logicals) to_abstract(1.5) to_abstract(1L) to_abstract(TRUE) # AnvlArrays become ConcreteArrays to_abstract(nv_array(1:4)) # Use pure = TRUE to strip subclass info to_abstract(nv_array(1:4), pure = TRUE)# R literals become LiteralArrays (ambiguous by default, except logicals) to_abstract(1.5) to_abstract(1L) to_abstract(TRUE) # AnvlArrays become ConcreteArrays to_abstract(nv_array(1:4)) # Use pure = TRUE to strip subclass info to_abstract(nv_array(1:4), pure = TRUE)
Executes f with abstract array arguments and records every primitive operation into
an AnvlGraph.
The resulting graph can be lowered to StableHLO (via stablehlo()) or transformed
(e.g. via transform_gradient()).
trace_fn( f, args = NULL, desc = NULL, mode = NULL, args_flat = NULL, in_tree = NULL )trace_fn( f, args = NULL, desc = NULL, mode = NULL, args_flat = NULL, in_tree = NULL )
f |
( |
args |
( |
desc |
( |
mode |
(
|
args_flat |
( |
in_tree |
( |
An AnvlGraph containing the traced operations.
stablehlo() to lower the graph, jit() / xla() for end-to-end
compilation.
graph <- trace_fn(function(x, y) x + y, args = list(x = nv_array(1, dtype = "f32"), y = nv_array(2, dtype = "f32"))) graphgraph <- trace_fn(function(x, y) x + y, args = list(x = nv_array(1, dtype = "f32"), y = nv_array(2, dtype = "f32"))) graph
Low-level graph transformation that transforms a graph into its gradient.
The function f represented by graph must return a single
float scalar. The resulting graph computes the gradients of that scalar with respect
to the inputs specified by wrt.
transform_gradient(graph, wrt)transform_gradient(graph, wrt)
graph |
( |
wrt |
( |
To support alternative forward passes for more efficient backward passes, we replay and possibly rewrite the graph into a new descriptor. Afterwards, we traverse it backwards and call the gradient rules where necessary.
See rule_reverse() for more information.
This is the building block used by gradient() and value_and_gradient(); prefer
those higher-level wrappers unless you need to operate on graphs directly.
An AnvlGraph whose outputs are the requested gradients.
gradient(), value_and_gradient(), rule_reverse()
graph <- trace_fn(prim_mul, list(nv_aval("f32", c()), nv_aval("f32", c()))) graph transform_gradient(graph, "lhs")graph <- trace_fn(prim_mul, list(nv_aval("f32", c()), nv_aval("f32", c()))) graph transform_gradient(graph, "lhs")
Returns the human-readable path for a single leaf node identified by its flat index. Only descends into the branch containing the target leaf, making it efficient for error reporting.
tree_path(node, i, prefix = "")tree_path(node, i, prefix = "")
node |
( |
i |
( |
prefix |
( |
A scalar character string.
Counts the number of leaf nodes in a tree. This equals the length of the
flat list produced by flatten() on the original structure.
tree_size(x)tree_size(x)
x |
( |
A scalar integer.
tree <- build_tree(list(a = 1, b = list(c = 2, d = 3))) tree_size(tree) tree_size(build_tree(list(1)))tree <- build_tree(list(a = 1, b = list(c = 2, d = 3))) tree_size(tree) tree_size(build_tree(list(1)))
Reconstructs a nested structure from a flat list by using a tree previously
created with build_tree(). Each LeafNode in the tree selects the
corresponding element from x by index, and ListNodes restore the
original nesting and names.
unflatten(node, x)unflatten(node, x)
node |
( |
x |
(list) |
The reconstructed nested structure (list or single value).
x <- list(a = 1, b = list(c = 2, d = 3)) tree <- build_tree(x) flat <- flatten(x) unflatten(tree, flat) unflatten(tree, list(10, 20, 30))x <- list(a = 1, b = list(c = 2, d = 3)) tree <- build_tree(x) flat <- flatten(x) unflatten(tree, flat) unflatten(tree, list(10, 20, 30))
Returns a new function that computes both the output of f and its gradient in a
single forward+reverse pass. The result is a named list with elements value (the
original return value of f) and grad (the gradients, structured like the inputs or
the wrt subset).
value_and_gradient(f, wrt = NULL)value_and_gradient(f, wrt = NULL)
f |
( |
wrt |
( |
A function with the same formals as f that returns
list(value = ..., grad = ...).
loss_fn <- function(x) sum(x^2L) vg <- jit(value_and_gradient(loss_fn)) result <- vg(nv_array(c(3, 4), dtype = "f32")) result$value result$gradloss_fn <- function(x) sum(x^2L) vg <- jit(value_and_gradient(loss_fn)) result <- vg(nv_array(c(3, 4), dtype = "f32")) result$value result$grad
Shorthand for building a tensor stablehlo::ValueType from a dtype
and shape — convenient inside stablehlo lowering rules that need to
declare custom-call output types or similar.
vt(dtype, shape)vt(dtype, shape)
dtype |
A dtype (string or |
shape |
An integer vector or |
(ValueType)
Convert a ValueType to an AbstractArray.
vt2at(x)vt2at(x)
x |
( |
(AbstractArray)
Sets the anvl.default_backend option for the duration of the
expression. This affects jit() and data construction (e.g. via nv_array).
with_backend(backend, code)with_backend(backend, code)
backend |
( |
code |
An expression to evaluate with the given backend. |
The result of evaluating code.
Compiles a function to an XLA executable via tracing.
Returns a callable R function that executes the compiled binary.
Unlike jit(), compilation happens eagerly at
definition time rather than on first call, so the input shapes and dtypes must be
specified upfront via abstract arrays (see nv_aval()).
xla(f, args, donate = character(), device = NULL)xla(f, args, donate = character(), device = NULL)
f |
( |
args |
( |
donate |
( |
device |
( |
Traces f with the given abstract args (via trace_fn()), lowers the resulting graph
via stablehlo() and then compiles it to an XLA executable via pjrt::pjrt_compile().
(function)
A function that accepts AnvlArray arguments (matching the flat inputs)
and returns the result as AnvlArrays.
jit() for lazy compilation, compile_xla() for the lower-level API.
f_compiled <- xla(function(x, y) x + y, args = list(x = nv_aval("f32", c(2, 2)), y = nv_aval("f32", c(2, 2))) ) a <- nv_matrix(1:4, nrow = 2, dtype = "f32") b <- nv_matrix(5:8, nrow = 2, dtype = "f32") f_compiled(a, b)f_compiled <- xla(function(x, y) x + y, args = list(x = nv_aval("f32", c(2, 2)), y = nv_aval("f32", c(2, 2))) ) a <- nv_matrix(1:4, nrow = 2, dtype = "f32") b <- nv_matrix(5:8, nrow = 2, dtype = "f32") f_compiled(a, b)