pub struct Tensor { /* private fields */ }Expand description
Tensor is a concrete tensor in tract.
Implementations§
Source§impl Tensor
impl Tensor
pub fn storage_as<T: TensorStorage>(&self) -> Option<&T>
pub fn try_storage_as<T: TensorStorage>(&self) -> TractResult<&T>
pub fn from_storage( dt: DatumType, shape: &[usize], storage: impl TensorStorage + 'static, ) -> Tensor
Sourcepub fn as_plain_ram(&self) -> Option<PlainView<'_>>
pub fn as_plain_ram(&self) -> Option<PlainView<'_>>
Returns an immutable PlainView if this tensor has plain storage.
Sourcepub fn try_as_plain_ram(&self) -> TractResult<PlainView<'_>>
pub fn try_as_plain_ram(&self) -> TractResult<PlainView<'_>>
Returns an immutable PlainView, or an error if storage is not plain.
Unlike as_plain_ram, this materializes storage that holds its bytes
elsewhere, so it is the way to read a tensor whose storage may be
lazily host-backed.
Sourcepub fn as_plain_ram_storage(&self) -> Option<&PlainStorage>
pub fn as_plain_ram_storage(&self) -> Option<&PlainStorage>
Plain storage this tensor already holds in ram, if any. Never materializes: the two axes at once, and the accessor to reach for before a plain read.
Sourcepub fn as_plain_ram_storage_mut(&mut self) -> Option<&mut PlainStorage>
pub fn as_plain_ram_storage_mut(&mut self) -> Option<&mut PlainStorage>
Mutable plain storage this tensor already holds in ram, if any. Never materializes.
Sourcepub fn is_plain(&self) -> bool
pub fn is_plain(&self) -> bool
Returns true if datum type and shape describe this tensor’s layout on
their own.
The layout axis, orthogonal to placement: a dense tensor is plain
wherever its bytes sit, block-quant weights are exotic wherever theirs
sit. Matches TypedFact::is_plain.
Sourcepub fn is_exotic(&self) -> bool
pub fn is_exotic(&self) -> bool
Returns true if datum type and shape do not describe this tensor’s
layout on their own, so a fact over it carries an ExoticFact.
Sourcepub fn is_plain_ram(&self) -> bool
pub fn is_plain_ram(&self) -> bool
Returns true if this tensor’s bytes can be read as plain host memory
right now: both axes at once, and what a caller gating an eager read or
an evaluation on cost is really asking.
Sourcepub fn in_ram(&self) -> bool
pub fn in_ram(&self) -> bool
Returns true if this tensor’s bytes are in host memory, readable
without a transfer.
The placement axis, and a transient one: storage that leaves its bytes
on a device answers false until something materializes them, true
afterwards. It answers for the bytes in whatever layout the storage
keeps them, so it takes both axes – as_plain_ram_storage – for a
plain read to be sure to work.
Sourcepub fn exotic_fact(&self) -> TractResult<Option<Box<dyn ExoticFact>>>
pub fn exotic_fact(&self) -> TractResult<Option<Box<dyn ExoticFact>>>
Build the ExoticFact matching this tensor’s storage, or None for plain tensors.
Sourcepub fn as_plain_ram_mut(&mut self) -> Option<PlainViewMut<'_>>
pub fn as_plain_ram_mut(&mut self) -> Option<PlainViewMut<'_>>
Returns a mutable PlainViewMut if this tensor has plain storage.
Sourcepub fn try_as_plain_ram_mut(&mut self) -> TractResult<PlainViewMut<'_>>
pub fn try_as_plain_ram_mut(&mut self) -> TractResult<PlainViewMut<'_>>
Returns a mutable PlainViewMut, or an error if storage is not plain.
Sourcepub unsafe fn uninitialized<T: Datum>(shape: &[usize]) -> TractResult<Tensor>
pub unsafe fn uninitialized<T: Datum>(shape: &[usize]) -> TractResult<Tensor>
Create an uninitialized tensor (dt as type paramater).
Sourcepub unsafe fn uninitialized_dt(
dt: DatumType,
shape: &[usize],
) -> TractResult<Tensor>
pub unsafe fn uninitialized_dt( dt: DatumType, shape: &[usize], ) -> TractResult<Tensor>
Create an uninitialized tensor (dt as regular parameter).
Sourcepub unsafe fn uninitialized_aligned<T: Datum>(
shape: &[usize],
alignment: usize,
) -> TractResult<Tensor>
pub unsafe fn uninitialized_aligned<T: Datum>( shape: &[usize], alignment: usize, ) -> TractResult<Tensor>
Create an uninitialized tensor with a given alignment (in bytes).
Sourcepub unsafe fn uninitialized_aligned_dt(
dt: DatumType,
shape: &[usize],
alignment: usize,
) -> TractResult<Tensor>
pub unsafe fn uninitialized_aligned_dt( dt: DatumType, shape: &[usize], alignment: usize, ) -> TractResult<Tensor>
Create an uninitialized tensor with a given alignment (in bytes).
pub fn stack_tensors( axis: usize, tensors: &[impl Borrow<Tensor>], ) -> TractResult<Tensor>
pub fn clear<T: Datum + Zero + Clone>(&mut self) -> TractResult<()>
pub fn zero<T: Datum + Zero>(shape: &[usize]) -> TractResult<Tensor>
pub fn zero_scalar<T: Datum + Zero>() -> TractResult<Tensor>
pub fn zero_scalar_dt(dt: DatumType) -> TractResult<Tensor>
pub fn zero_dt(dt: DatumType, shape: &[usize]) -> TractResult<Tensor>
pub fn fill_t<T: Datum + Clone>(&mut self, value: T) -> TractResult<()>
pub fn zero_aligned_dt( dt: DatumType, shape: &[usize], alignment: usize, ) -> TractResult<Tensor>
pub fn zero_aligned<T: Datum + Zero>( shape: &[usize], alignment: usize, ) -> TractResult<Tensor>
Sourcepub fn from_shape<T: Datum + Copy>(
shape: &[usize],
data: &[T],
) -> TractResult<Tensor>
pub fn from_shape<T: Datum + Copy>( shape: &[usize], data: &[T], ) -> TractResult<Tensor>
Create a tensor with a given shape and a slice of elements. The data is copied and aligned to size of T.
Sourcepub fn from_shape_align<T: Datum + Copy>(
shape: &[usize],
data: &[T],
align: usize,
) -> TractResult<Tensor>
pub fn from_shape_align<T: Datum + Copy>( shape: &[usize], data: &[T], align: usize, ) -> TractResult<Tensor>
Create a tensor with a given shape and a slice of elements. The data is copied and aligned to given alignment.
Sourcepub unsafe fn from_raw<T: Datum>(
shape: &[usize],
content: &[u8],
) -> TractResult<Tensor>
pub unsafe fn from_raw<T: Datum>( shape: &[usize], content: &[u8], ) -> TractResult<Tensor>
Create a tensor from raw data.
It copies the data, aligning it to the size of T.
pub unsafe fn from_raw_aligned<T: Datum>( shape: &[usize], content: &[u8], align: usize, ) -> TractResult<Tensor>
pub unsafe fn from_raw_dt( dt: DatumType, shape: &[usize], content: &[u8], ) -> TractResult<Tensor>
pub unsafe fn from_raw_dt_align( dt: DatumType, shape: &[usize], content: &[u8], align: usize, ) -> TractResult<Tensor>
pub unsafe fn from_slice_align<T: Datum>( content: &[T], align: usize, ) -> TractResult<Tensor>
Sourcepub unsafe fn set_shape_unchecked(&mut self, shape: &[usize])
pub unsafe fn set_shape_unchecked(&mut self, shape: &[usize])
Force the tensor shape, no consistency check.
Sourcepub unsafe fn set_geometry_unchecked(
&mut self,
shape: &[usize],
strides: &[isize],
)
pub unsafe fn set_geometry_unchecked( &mut self, shape: &[usize], strides: &[isize], )
Force the tensor shape and strides, no consistency check.
Sourcepub fn set_shape(&mut self, shape: &[usize]) -> TractResult<()>
pub fn set_shape(&mut self, shape: &[usize]) -> TractResult<()>
Force the tensor shape.
pub fn permute_axes(self, axes: &[usize]) -> TractResult<Tensor>
pub fn move_axis(self, from: usize, to: usize) -> TractResult<Tensor>
pub fn collapse_axis_with_next(self, axis: usize) -> Tensor
pub fn split_axis(self, axis: usize, outer_dim: usize) -> TractResult<Tensor>
Sourcepub fn into_shape(self, shape: &[usize]) -> TractResult<Tensor>
pub fn into_shape(self, shape: &[usize]) -> TractResult<Tensor>
Reshape the tensor to shape.
pub fn insert_axis(&mut self, axis: usize) -> TractResult<()>
pub fn remove_axis(&mut self, axis: usize) -> TractResult<()>
pub fn broadcast_into_rank(self, rank: usize) -> TractResult<Tensor>
pub fn broadcast_to_rank(&mut self, rank: usize) -> TractResult<()>
pub fn broadcast_scalar_to_shape(&self, shape: &[usize]) -> TractResult<Tensor>
pub fn broadcast_to_shape(&self, shape: &[usize]) -> TractResult<Tensor>
pub fn broadcast_vector_to_shape( &self, shape: &[usize], axis: usize, ) -> TractResult<Tensor>
pub fn assign_slice( &mut self, range: impl RangeBounds<usize>, src: &Tensor, src_range: impl RangeBounds<usize>, axis: usize, ) -> TractResult<()>
Sourcepub fn assign_slice_at_prefix(
&mut self,
prefix: &[usize],
range: impl RangeBounds<usize>,
src: &Tensor,
src_prefix: &[usize],
src_range: impl RangeBounds<usize>,
axis: usize,
) -> TractResult<()>
pub fn assign_slice_at_prefix( &mut self, prefix: &[usize], range: impl RangeBounds<usize>, src: &Tensor, src_prefix: &[usize], src_range: impl RangeBounds<usize>, axis: usize, ) -> TractResult<()>
Assign src’s src_range along axis into range along axis, each
taken in the sub-tensor at its prefix. A prefix indexes the leading axes
as Tensor::view_at_prefix does, and both prefixes cover the same
axes, whose extents may then differ. axis stays an axis of the whole
tensors, so it has to sit past the prefixes.
pub unsafe fn assign_slice_unchecked( &mut self, range: impl RangeBounds<usize>, src: &Tensor, src_range: impl RangeBounds<usize>, axis: usize, )
Sourcepub fn fill_slice(
&mut self,
range: impl RangeBounds<usize>,
value: &Tensor,
axis: usize,
) -> TractResult<()>
pub fn fill_slice( &mut self, range: impl RangeBounds<usize>, value: &Tensor, axis: usize, ) -> TractResult<()>
Fill range along axis with value, a one-element tensor of this
tensor’s datum type.
Sourcepub fn fill_slice_at_prefix(
&mut self,
prefix: &[usize],
range: impl RangeBounds<usize>,
value: &Tensor,
axis: usize,
) -> TractResult<()>
pub fn fill_slice_at_prefix( &mut self, prefix: &[usize], range: impl RangeBounds<usize>, value: &Tensor, axis: usize, ) -> TractResult<()>
Fill range along axis of the sub-tensor at prefix, which indexes
the leading axes as Tensor::view_at_prefix does, with value, a
one-element tensor of this tensor’s datum type. axis stays an axis of
the whole tensor, so it has to sit past prefix.
Sourcepub fn datum_type(&self) -> DatumType
pub fn datum_type(&self) -> DatumType
Get the datum type of the tensor.
Sourcepub unsafe fn set_datum_type(&mut self, dt: DatumType)
pub unsafe fn set_datum_type(&mut self, dt: DatumType)
Set the datum type of the tensor.
Sourcepub fn dump(&self, force_full: bool) -> TractResult<String>
pub fn dump(&self, force_full: bool) -> TractResult<String>
Dump the tensor in a human readable form.
force_full will force the tensor to be dump in full even if it is big.
Sourcepub fn close_enough(
&self,
other: &Self,
approx: impl Into<Approximation> + Debug,
) -> TractResult<()>
pub fn close_enough( &self, other: &Self, approx: impl Into<Approximation> + Debug, ) -> TractResult<()>
Compare two tensors, allowing for rounding errors.
Sourcepub fn ulp_comparison_dt(&self) -> DatumType
pub fn ulp_comparison_dt(&self) -> DatumType
The float type ULP distances against this tensor are measured in.
Float tensors are compared in their own type, so an f16 comparison stays an
f16 comparison. Anything else falls back to f32, matching what
close_enough does for its tolerance check.
Sourcepub fn max_ulp_distance(
&self,
other: &Self,
) -> TractResult<(u64, Option<usize>)>
pub fn max_ulp_distance( &self, other: &Self, ) -> TractResult<(u64, Option<usize>)>
Largest integer ULP distance between self and other, and the flat index
where it occurs.
Comparison happens in Self::ulp_comparison_dt. See crate::ulp for
the exact convention around signed zeros, infinities and NaN.
Sourcepub fn into_plain_array<D: Datum>(self) -> TractResult<ArrayD<D>>
pub fn into_plain_array<D: Datum>(self) -> TractResult<ArrayD<D>>
Transform the tensor into a ndarray::Array.
Sourcepub unsafe fn into_array_unchecked<D: Datum>(self) -> ArrayD<D>
pub unsafe fn into_array_unchecked<D: Datum>(self) -> ArrayD<D>
Transform the tensor into a ndarray::Array.
Sourcepub fn to_plain_array_view<D: Datum>(&self) -> TractResult<ArrayViewD<'_, D>>
pub fn to_plain_array_view<D: Datum>(&self) -> TractResult<ArrayViewD<'_, D>>
Returns a plain array view of the tensor.
Errors if the storage is not plain or the datum type does not match D.
Sourcepub fn to_plain_array_view_mut<D: Datum>(
&mut self,
) -> TractResult<ArrayViewMutD<'_, D>>
pub fn to_plain_array_view_mut<D: Datum>( &mut self, ) -> TractResult<ArrayViewMutD<'_, D>>
Returns a mutable plain array view of the tensor.
Errors if the storage is not plain or the datum type does not match D.
Sourcepub unsafe fn to_array_view_unchecked<D: Datum>(&self) -> ArrayViewD<'_, D>
pub unsafe fn to_array_view_unchecked<D: Datum>(&self) -> ArrayViewD<'_, D>
Transform the data as a ndarray::Array.
Sourcepub unsafe fn to_array_view_mut_unchecked<D: Datum>(
&mut self,
) -> ArrayViewMutD<'_, D>
pub unsafe fn to_array_view_mut_unchecked<D: Datum>( &mut self, ) -> ArrayViewMutD<'_, D>
Transform the data as a mutable ndarray::Array.
Sourcepub fn as_ptr<D: Datum>(&self) -> TractResult<*const D>
pub fn as_ptr<D: Datum>(&self) -> TractResult<*const D>
Access the data as a pointer.
Sourcepub unsafe fn as_ptr_unchecked<D: Datum>(&self) -> *const D
pub unsafe fn as_ptr_unchecked<D: Datum>(&self) -> *const D
Access the data as a pointer.
Sourcepub unsafe fn as_ptr_mut_unchecked<D: Datum>(&mut self) -> *mut D
pub unsafe fn as_ptr_mut_unchecked<D: Datum>(&mut self) -> *mut D
Access the data as a pointer.
Sourcepub fn as_ptr_mut<D: Datum>(&mut self) -> TractResult<*mut D>
pub fn as_ptr_mut<D: Datum>(&mut self) -> TractResult<*mut D>
Access the data as a mutable pointer.
Sourcepub unsafe fn as_slice_unchecked<D: Datum>(&self) -> &[D]
pub unsafe fn as_slice_unchecked<D: Datum>(&self) -> &[D]
Access the data as a slice.
Sourcepub unsafe fn as_slice_mut_unchecked<D: Datum>(&mut self) -> &mut [D]
pub unsafe fn as_slice_mut_unchecked<D: Datum>(&mut self) -> &mut [D]
Access the data as a mutable slice.
Sourcepub fn to_scalar_tensor(&self) -> TractResult<Tensor>
pub fn to_scalar_tensor(&self) -> TractResult<Tensor>
Make the tensor a scalar tensor (assumes it contains a single value).
Sourcepub unsafe fn to_scalar_unchecked<D: Datum>(&self) -> &D
pub unsafe fn to_scalar_unchecked<D: Datum>(&self) -> &D
Access the data as a scalar.
Sourcepub fn to_scalar_mut<D: Datum>(&mut self) -> TractResult<&mut D>
pub fn to_scalar_mut<D: Datum>(&mut self) -> TractResult<&mut D>
Mutable access the data as a scalar.
Sourcepub unsafe fn to_scalar_mut_unchecked<D: Datum>(&mut self) -> &mut D
pub unsafe fn to_scalar_mut_unchecked<D: Datum>(&mut self) -> &mut D
Mutable access the data as a scalar.
pub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes_mut(&mut self) -> &mut [u8] ⓘ
pub fn is_uniform(&self) -> bool
pub fn as_uniform(&self) -> Option<Tensor>
pub fn is_all_zero(&self) -> TractResult<bool>
pub fn is_zero(&self) -> TractResult<bool>
Sourcepub fn cast_to<D: Datum>(&self) -> TractResult<Cow<'_, Tensor>>
pub fn cast_to<D: Datum>(&self) -> TractResult<Cow<'_, Tensor>>
Optionnaly convert data to a tensor for a new DatumType.
Sourcepub fn cast_to_dt(&self, dst_dt: DatumType) -> TractResult<Cow<'_, Tensor>>
pub fn cast_to_dt(&self, dst_dt: DatumType) -> TractResult<Cow<'_, Tensor>>
Optionnaly convert data to a tensor for a new DatumType.
Sourcepub fn cast_to_scalar<D: Datum + Copy>(&self) -> TractResult<D>
pub fn cast_to_scalar<D: Datum + Copy>(&self) -> TractResult<D>
Access the data as a scalar, after a cast.
Sourcepub fn nth(&self, nth: usize) -> TractResult<Tensor>
pub fn nth(&self, nth: usize) -> TractResult<Tensor>
Access the nth element of the tensor, returned as a 0-rank Tensor
pub fn deep_clone(&self) -> Tensor
pub fn slice( &self, axis: usize, start: usize, end: usize, ) -> TractResult<Tensor>
pub fn view(&self) -> TensorView<'_>
pub fn view_at_prefix(&self, prefix: &[usize]) -> TractResult<TensorView<'_>>
pub fn view_offsetting(&self, coords: &[usize]) -> TractResult<TensorView<'_>>
pub unsafe fn view_offsetting_unchecked( &self, coords: &[usize], ) -> TensorView<'_>
pub fn view_mut(&mut self) -> TensorView<'_>
pub fn view_at_prefix_mut( &mut self, prefix: &[usize], ) -> TractResult<TensorView<'_>>
pub fn view_offsetting_mut( &mut self, coords: &[usize], ) -> TractResult<TensorView<'_>>
Sourcepub fn offset_u8_as_i8(self: &Arc<Self>) -> Arc<Self> ⓘ
pub fn offset_u8_as_i8(self: &Arc<Self>) -> Arc<Self> ⓘ
Offsets the tensor as an i8 type if it’s an u8 type, otherwise passes it unchanged.
Sourcepub fn offset_i8_as_u8(self: &Arc<Self>) -> Arc<Self> ⓘ
pub fn offset_i8_as_u8(self: &Arc<Self>) -> Arc<Self> ⓘ
Offsets the tensor as an u8 type if it’s an i8 type, otherwise passes it unchanged.
pub fn to_aligned_default(&self) -> TractResult<Self>
pub fn natural_strides(shape: &[usize]) -> TVec<isize>
Sourcepub fn has_plain_ram_storage(&self) -> bool
pub fn has_plain_ram_storage(&self) -> bool
Returns true if this tensor owns plain ram storage outright, rather
than storage that produces or holds some – a device readback that has
come back still keeps its device tensor.
Sourcepub fn into_plain_ram(self) -> TractResult<Tensor>
pub fn into_plain_ram(self) -> TractResult<Tensor>
This tensor backed by plain ram storage of its own: bytes left on a device come back here, and exotic storage, which has no plain form, errors.
pub fn into_blob(self) -> TractResult<Blob>
Trait Implementations§
impl Eq for Tensor
Source§impl IntoArcTensor for Tensor
impl IntoArcTensor for Tensor
Source§impl IntoTensor for Tensor
impl IntoTensor for Tensor
Source§fn into_tensor(self) -> Tensor
fn into_tensor(self) -> Tensor
impl Send for Tensor
impl Sync for Tensor
Auto Trait Implementations§
impl !RefUnwindSafe for Tensor
impl !UnwindSafe for Tensor
impl Freeze for Tensor
impl Unpin for Tensor
impl UnsafeUnpin for Tensor
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> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>, which can then be
downcast into Box<dyn ConcreteType> where ConcreteType implements Trait.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>, which can then be further
downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> DowncastSend for T
impl<T> DowncastSend for T
Source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
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