Create a PJRT Buffer from an R object.
Any numeric PJRT buffer is an array and 0-dimensional arrays are used as scalars.
pjrt_buffer will create a array with dimensions (1) for a vector of length 1, while
pjrt_scalar will create a 0-dimensional array for an R vector of length 1.
To create an empty buffer (at least one dimension must be 0), use pjrt_empty.
Important: No checks are performed when creating the buffer, so you need to ensure that the data fits the selected element type (e.g., to prevent buffer overflow) and that no NA values are present.
Usage
pjrt_buffer(data, dtype = NULL, device = NULL, shape = NULL, ...)
pjrt_scalar(data, dtype = NULL, device = NULL, ...)
pjrt_empty(dtype, shape, device = NULL)Arguments
- data
(any)
Data to convert to aPJRTBuffer.- dtype
(
NULL|character(1))
The type of the buffer. Currently supported types are:"pred": predicate (i.e. a boolean)"{s,u}{8,16,32,64}": Signed and unsigned integer (forintegerdata)."f{32,64}": Floating point (fordoubleorintegerdata). The default (NULL) depends on the method:logical->"pred"integer->"i32"double->"f32"raw-> must be supplied
- device
(
NULL|PJRTDevice|character(1))
APJRTDeviceobject or the name of the platform to use ("cpu", "cuda", ...), in which case the first device for that platform is used. The default is to use the CPU platform, but this can be configured via thePJRT_PLATFORMenvironment variable.- shape
(
NULL|integer())
The dimensions of the buffer. The default (NULL) is to infer them from the data if possible. The default (NULL) depends on the method.- ...
(any)
Additional arguments. Forrawtypes, this includes:row_major: Whether to read the data in row-major format or column-major format. R uses column-major format.
Extractors
platform()->character(1): for the platform name of the buffer ("cpu","cuda", ...).device()->PJRTDevice: for the device of the buffer (also includes device number)elt_type()->PJRTElementType: for the element type of the buffer.
Converters
as_array()->array|vector: for converting back to R (vectoris only used for shapeinteger()).as_raw()->rawfor a raw vector.
Reading and Writing
safetensors::safe_save_filefor writing to a safetensors file.safetensors::safe_load_filefor reading from a safetensors file.
Scalars
When calling this function on a vector of length 1, the resulting shape is 1L.
To create a 0-dimensional buffer, use pjrt_scalar where the resulting shape is integer().