pub struct FlexTensor { /* 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 FlexTensor
impl FlexTensor
Sourcepub fn new(data: Bytes, layout: Layout, dtype: DType) -> Self
pub fn new(data: Bytes, layout: Layout, dtype: DType) -> Self
Create a new tensor from bytes, layout, and dtype.
Sourcepub fn from_data(data: TensorData) -> Self
pub fn from_data(data: TensorData) -> Self
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) -> Self
pub fn with_layout(self, layout: Layout) -> Self
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) -> Self
pub fn from_arc(data: Arc<Bytes>, layout: Layout, dtype: DType) -> Self
Create a tensor from shared data, layout, and dtype.
Use this for zero-copy view operations.
Sourcepub fn storage<E: Element + Pod>(&self) -> &[E]
pub fn storage<E: Element + Pod>(&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: Element + Pod>(&mut self) -> &mut [E]
pub fn storage_mut<E: Element + Pod>(&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: Element + Pod>(&mut self) -> Option<&mut [E]>
pub fn try_storage_mut<E: Element + Pod>(&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: Element + Pod>(&self) -> Option<&[E]>
pub fn as_slice<E: Element + Pod>(&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) -> Self
pub fn empty(shape: Shape, dtype: DType) -> Self
Create an empty tensor with given shape and dtype.
Sourcepub fn filled_typed<E: Pod + Send + Sync>(
shape: Shape,
dtype: DType,
value: E,
) -> Self
pub fn filled_typed<E: Pod + Send + Sync>( shape: Shape, dtype: DType, value: E, ) -> Self
Create a tensor filled with n copies of a typed value.
Sourcepub fn to_contiguous(&self) -> Self
pub fn to_contiguous(&self) -> Self
Copy to contiguous layout if needed.
Sourcepub fn transpose(&self, dim1: usize, dim2: usize) -> Self
pub fn transpose(&self, dim1: usize, dim2: usize) -> Self
Transpose two dimensions. Zero-copy (metadata only).
Trait Implementations§
Source§impl Clone for FlexTensor
impl Clone for FlexTensor
Source§fn clone(&self) -> FlexTensor
fn clone(&self) -> FlexTensor
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 FlexTensor
impl Debug for FlexTensor
Auto Trait Implementations§
impl Freeze for FlexTensor
impl !RefUnwindSafe for FlexTensor
impl Send for FlexTensor
impl Sync for FlexTensor
impl Unpin for FlexTensor
impl UnsafeUnpin for FlexTensor
impl !UnwindSafe for FlexTensor
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
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