rlmesh::Tensor
An immutable n-dimensional array with shared storage.
struct TensorThe constructor copies its buffer; Clone, buffer
and a contiguous reshape share storage, and
copy makes a fresh one. Strides are in bytes.
PartialEq compares dtype, shape and element bytes.
Associated functions
Methods
- to_dlpack()Export a DLPack 1.0 tensor, sharing read-only storage by default.
- dlpack_device()The DLPack device
(kDLCPU, 0). - shape()Dimension sizes, C order.
- dtype()Element dtype.
- ndim()Number of dimensions.
- size()Number of elements.
- nbytes()Size of the elements in bytes (
size * itemsize). - strides()Strides in bytes, one per dimension.
- device()The device the storage lives on.
- buffer()The bytes from the first element, read-only and shared:
nbyteslong when contiguous, else through the last byte the strides address. - is_contiguous()Whether the elements are laid out C-contiguously.
- reshape()The same elements in a new shape; one dimension may be
-1. - copy()A deep, contiguous copy.
- tobytes()The elements as C-ordered bytes (a copy).
from_dlpack
associated function[source]unsafe fn from_dlpack(
obj: NonNull<DLManagedTensorVersioned>,
) -> Result<Tensor>Copy a versioned CPU DLPack tensor into fresh contiguous storage.
On success, calls the producer’s deleter; on error, ownership stays
with the caller, who may recover it with DlpackTensor::from_raw.
Safety
obj must point to a live, exclusively owned managed tensor. Its
shape/stride arrays must be readable for ndim entries and all
described element addresses must be readable, initialized CPU bytes
for the duration of the call. The producer must not mutate them
concurrently. The deleter must be valid to call once on success.
new
associated function[source]fn new(buffer: &[u8], shape: &[usize], dtype: DType) -> Result<Tensor>Copy buffer into a C-contiguous tensor of shape and dtype, as
Tensor(buffer, shape, dtype). buffer must hold exactly
size * itemsize bytes.
to_dlpack
method[source]fn to_dlpack(&self, options: ToDlpackOptions) -> Result<DlpackTensor>Export a DLPack 1.0 tensor, sharing read-only storage by default. A copying export owns fresh writable storage. The returned owner keeps storage and layout alive even after this tensor is destroyed.
dlpack_device
method[source]fn dlpack_device(&self) -> (i32, i32)The DLPack device (kDLCPU, 0).
shape
method[source]fn shape(&self) -> &[usize]Dimension sizes, C order.
dtype
method[source]fn dtype(&self) -> DTypeElement dtype.
ndim
method[source]fn ndim(&self) -> usizeNumber of dimensions.
size
method[source]fn size(&self) -> usizeNumber of elements.
nbytes
method[source]fn nbytes(&self) -> usizeSize of the elements in bytes (size * itemsize).
strides
method[source]fn strides(&self) -> Vec<usize>Strides in bytes, one per dimension.
device
method[source]fn device(&self) -> DeviceThe device the storage lives on.
buffer
method[source]fn buffer(&self) -> &[u8]The bytes from the first element, read-only and shared: nbytes long
when contiguous, else through the last byte the strides address.
is_contiguous
method[source]fn is_contiguous(&self) -> boolWhether the elements are laid out C-contiguously.
reshape
method[source]fn reshape(&self, shape: &[i64]) -> Result<Tensor>The same elements in a new shape; one dimension may be -1. Shares
storage when contiguous.
copy
method[source]fn copy(&self) -> TensorA deep, contiguous copy.
tobytes
method[source]fn tobytes(&self) -> Vec<u8>The elements as C-ordered bytes (a copy).
Trait implementations
- impl TensorExt for Tensor
- impl Clone for Tensor
- impl Display for Tensor
- impl Debug for Tensor
- impl PartialEq for Tensor
- impl From<Tensor> for Value