pub struct HostTensor { /* private fields */ }Expand description
CPU tensor primitive for the Flex backend.
Uses type-erased byte storage with runtime dtype and Arc-based sharing. Clone is O(1) (refcount increment). Copy-on-write for mutations.
Implementations§
Source§impl HostTensor
impl HostTensor
Sourcepub fn new(data: Bytes, layout: Layout, dtype: DType) -> HostTensor
pub fn new(data: Bytes, layout: Layout, dtype: DType) -> HostTensor
Create a new tensor from bytes, layout, and dtype.
Sourcepub fn from_data(data: TensorData) -> HostTensor
pub fn from_data(data: TensorData) -> HostTensor
Create a tensor from TensorData.
Sourcepub fn into_data(self) -> TensorData
pub fn into_data(self) -> TensorData
Convert tensor to TensorData.
If non-contiguous or shared, this will copy data.
Sourcepub fn is_unique(&self) -> bool
pub fn is_unique(&self) -> bool
Check if this tensor has exclusive ownership of its data.
When true, in-place mutations are safe without copying.
Sourcepub fn with_layout(self, layout: Layout) -> HostTensor
pub fn with_layout(self, layout: Layout) -> HostTensor
Create a new tensor with a different layout but sharing the same data.
This is a zero-copy operation used for operations like flip, transpose, etc.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if tensor is contiguous.
Sourcepub fn data_arc(&self) -> Arc<Bytes> ⓘ
pub fn data_arc(&self) -> Arc<Bytes> ⓘ
Get a clone of the Arc for sharing data with a new layout.
Use this for zero-copy view operations (reshape, transpose, slice).
Sourcepub fn from_arc(data: Arc<Bytes>, layout: Layout, dtype: DType) -> HostTensor
pub fn from_arc(data: Arc<Bytes>, layout: Layout, dtype: DType) -> HostTensor
Create a tensor from shared data, layout, and dtype.
Use this for zero-copy view operations.
Sourcepub fn storage<E>(&self) -> &[E]
pub fn storage<E>(&self) -> &[E]
Zero-copy typed view of the full storage buffer.
Use with StridedIter for non-contiguous access, or with
layout().contiguous_offsets() for the contiguous fast path.
§Panics
Panics if E::dtype() doesn’t match the tensor’s dtype.
Note: Bool tensors are stored as u8, so both Bool(Native) and Bool(U8)
dtypes accept u8 access.
Sourcepub fn storage_mut<E>(&mut self) -> &mut [E]
pub fn storage_mut<E>(&mut self) -> &mut [E]
Mutable typed view with copy-on-write semantics.
If the tensor is shared (refcount > 1), this will copy the data first.
For in-place operations, prefer try_storage_mut() which returns None
if shared, allowing you to choose an alternative strategy.
§Panics
Panics if E::dtype() doesn’t match the tensor’s dtype.
Note: Bool tensors are stored as u8, so both Bool(Native) and Bool(U8)
dtypes accept u8 access.
Sourcepub fn try_storage_mut<E>(&mut self) -> Option<&mut [E]>
pub fn try_storage_mut<E>(&mut self) -> Option<&mut [E]>
Try to get mutable storage without copying.
Returns Some if tensor is uniquely owned, None if shared.
Use this when you want to avoid the implicit copy in storage_mut().
Note: Bool tensors are stored as u8, so both Bool(Native) and Bool(U8)
dtypes accept u8 access.
Sourcepub fn as_slice<E>(&self) -> Option<&[E]>
pub fn as_slice<E>(&self) -> Option<&[E]>
Get typed slice view (zero-cost if contiguous and offset is 0).
Returns None if dtype doesn’t match E or tensor is non-contiguous.
Sourcepub fn empty(shape: Shape, dtype: DType) -> HostTensor
pub fn empty(shape: Shape, dtype: DType) -> HostTensor
Create an empty tensor with given shape and dtype.
Sourcepub fn zeros(shape: Shape, dtype: DType) -> HostTensor
pub fn zeros(shape: Shape, dtype: DType) -> HostTensor
Create a tensor filled with zeros.
Sourcepub fn filled_typed<E>(shape: Shape, dtype: DType, value: E) -> HostTensor
pub fn filled_typed<E>(shape: Shape, dtype: DType, value: E) -> HostTensor
Create a tensor filled with n copies of a typed value.
Sourcepub fn to_contiguous(&self) -> HostTensor
pub fn to_contiguous(&self) -> HostTensor
Copy to contiguous layout if needed.
Sourcepub fn reshape(&self, new_shape: Shape) -> HostTensor
pub fn reshape(&self, new_shape: Shape) -> HostTensor
Reshape tensor. Zero-copy if contiguous.
Sourcepub fn transpose(&self, dim1: usize, dim2: usize) -> HostTensor
pub fn transpose(&self, dim1: usize, dim2: usize) -> HostTensor
Transpose two dimensions. Zero-copy (metadata only).
Sourcepub fn narrow(&self, dim: usize, start: usize, len: usize) -> HostTensor
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> HostTensor
Narrow/slice along a dimension. Zero-copy (metadata only).
Sourcepub fn permute(&self, axes: &[usize]) -> HostTensor
pub fn permute(&self, axes: &[usize]) -> HostTensor
Permute dimensions according to axes. Zero-copy (metadata only).
Trait Implementations§
Source§impl Clone for HostTensor
impl Clone for HostTensor
Source§fn clone(&self) -> HostTensor
fn clone(&self) -> HostTensor
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for HostTensor
impl Debug for HostTensor
Source§impl TensorMetadata for HostTensor
impl TensorMetadata for HostTensor
Auto Trait Implementations§
impl !RefUnwindSafe for HostTensor
impl !UnwindSafe for HostTensor
impl Freeze for HostTensor
impl Send for HostTensor
impl Sync for HostTensor
impl Unpin for HostTensor
impl UnsafeUnpin for HostTensor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more