Struct FlexTensor
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§
§impl FlexTensor
impl FlexTensor
pub fn new(data: Bytes, layout: Layout, dtype: DType) -> FlexTensor
pub fn new(data: Bytes, layout: Layout, dtype: DType) -> FlexTensor
Create a new tensor from bytes, layout, and dtype.
pub fn from_data(data: TensorData) -> FlexTensor
pub fn from_data(data: TensorData) -> FlexTensor
Create a tensor from TensorData.
pub fn into_data(self) -> TensorData
pub fn into_data(self) -> TensorData
Convert tensor to TensorData.
If non-contiguous or shared, this will copy data.
pub 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.
pub fn with_layout(self, layout: Layout) -> FlexTensor
pub fn with_layout(self, layout: Layout) -> FlexTensor
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.
pub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if tensor is contiguous.
pub 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).
pub fn from_arc(data: Arc<Bytes>, layout: Layout, dtype: DType) -> FlexTensor
pub fn from_arc(data: Arc<Bytes>, layout: Layout, dtype: DType) -> FlexTensor
Create a tensor from shared data, layout, and dtype.
Use this for zero-copy view operations.
pub fn storage<E>(&self) -> &[E]where
E: Element + Pod,
pub fn storage<E>(&self) -> &[E]where
E: Element + Pod,
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.
pub fn storage_mut<E>(&mut self) -> &mut [E]where
E: Element + Pod,
pub fn storage_mut<E>(&mut self) -> &mut [E]where
E: Element + Pod,
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.
pub fn try_storage_mut<E>(&mut self) -> Option<&mut [E]>where
E: Element + Pod,
pub fn try_storage_mut<E>(&mut self) -> Option<&mut [E]>where
E: Element + Pod,
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.
pub fn as_slice<E>(&self) -> Option<&[E]>where
E: Element + Pod,
pub fn as_slice<E>(&self) -> Option<&[E]>where
E: Element + Pod,
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.
pub fn empty(shape: Shape, dtype: DType) -> FlexTensor
pub fn empty(shape: Shape, dtype: DType) -> FlexTensor
Create an empty tensor with given shape and dtype.
pub fn zeros(shape: Shape, dtype: DType) -> FlexTensor
pub fn zeros(shape: Shape, dtype: DType) -> FlexTensor
Create a tensor filled with zeros.
pub fn filled_typed<E>(shape: Shape, dtype: DType, value: E) -> FlexTensor
pub fn filled_typed<E>(shape: Shape, dtype: DType, value: E) -> FlexTensor
Create a tensor filled with n copies of a typed value.
pub fn to_contiguous(&self) -> FlexTensor
pub fn to_contiguous(&self) -> FlexTensor
Copy to contiguous layout if needed.
pub fn reshape(&self, new_shape: Shape) -> FlexTensor
pub fn reshape(&self, new_shape: Shape) -> FlexTensor
Reshape tensor. Zero-copy if contiguous.
pub fn transpose(&self, dim1: usize, dim2: usize) -> FlexTensor
pub fn transpose(&self, dim1: usize, dim2: usize) -> FlexTensor
Transpose two dimensions. Zero-copy (metadata only).
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> FlexTensor
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> FlexTensor
Narrow/slice along a dimension. Zero-copy (metadata only).
pub fn permute(&self, axes: &[usize]) -> FlexTensor
pub fn permute(&self, axes: &[usize]) -> FlexTensor
Permute dimensions according to axes. Zero-copy (metadata only).
Trait Implementations§
§impl Clone for FlexTensor
impl Clone for FlexTensor
§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 more§impl Debug for FlexTensor
impl Debug for FlexTensor
§impl TensorMetadata for FlexTensor
impl TensorMetadata for FlexTensor
§type Device = FlexDevice
type Device = FlexDevice
§fn device(&self) -> <FlexTensor as TensorMetadata>::Device
fn device(&self) -> <FlexTensor as TensorMetadata>::Device
§fn can_mut(&self) -> bool
fn can_mut(&self) -> bool
slice_assign, an inplace kernel)
writes the existing allocation instead of copying it first. Read more§fn scheme(&self) -> QuantScheme
fn scheme(&self) -> QuantScheme
Auto Trait Implementations§
impl !RefUnwindSafe for FlexTensor
impl !UnwindSafe for FlexTensor
impl Freeze for FlexTensor
impl Send for FlexTensor
impl Sync for FlexTensor
impl Unpin for FlexTensor
impl UnsafeUnpin 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
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,
§impl<K, Q> Comparable<Q> for K
impl<K, Q> Comparable<Q> for K
§impl<K, Q> Equivalent<Q> for K
impl<K, Q> Equivalent<Q> for K
§fn equivalent(&self, key: &Q) -> bool
fn equivalent(&self, key: &Q) -> bool
key and return true if they are equal.impl<T> ErasedDestructor for Twhere
T: 'static,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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