Frequently used generics and helper functions used in 'anvl' and its companion packages 'stablehlo' and 'pjrt'. Comes with S3 generics to query arrays (shape, data type, device) and to convert them to R arrays and raw vectors, as well as a class for data types.
Provides S3 generics and data types for tensors, shared across the r-xla packages.
pak::pak("r-xla/xlamisc")
shape(): Different from dim() by also supporting 0-dimensional
tensors.dtype(): Returns the data type of the tensor.as_array(): Converts the tensor to an R array.as_raw(): Converts the tensor to a raw() vector.device(): Returns the device of the tensor.Other functions:
naxes(): Returns the number of axes of the tensor. Is implemented as
length(shape(x)).nelts(): Returns the number of elements of the tensor. Is
implemented as prod(shape(x)).xlamisc provides DataType, an enum-style S3 class representing a
tensor element type. There is one singleton object per supported dtype,
wrapping its canonical string. The members are:
bool (aliases i1, pred)i8, i16, i32, i64ui8, ui16, ui32, ui64f16, bf16, f32, f64c64, c128Construct and inspect data types with:
as_dtype(x): Convert a string (e.g. "f32") or DataType to a
DataType object.is_dtype(x): Check whether an object is a DataType.assert_dtype(x): Assert that an object is a DataType.dtype_width(x): The width of one element in bits.dtype_category(x): The category, one of "bool", "int", "uint",
"float", or "complex".is_dtype_float(x), is_dtype_int(x), is_dtype_uint(x),
is_dtype_bool(x), is_dtype_complex(x): Per-category predicates.Data types support equality comparison with == and !=.