AsView

Trait AsView 

Source
pub trait AsView: Layout {
    type Elem;
    type Layout: Clone + for<'a> Layout<Index<'a> = Self::Index<'a>>;

Show 46 methods // Required methods fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>; fn layout(&self) -> &Self::Layout; fn copy_into_slice<'a>( &self, dest: &'a mut [MaybeUninit<Self::Elem>], ) -> &'a [Self::Elem] where Self::Elem: Copy; fn data(&self) -> Option<&[Self::Elem]>; fn insert_axis(&mut self, index: usize) where Self::Layout: ResizeLayout; fn remove_axis(&mut self, index: usize) where Self::Layout: ResizeLayout; fn iter(&self) -> Iter<'_, Self::Elem> ; fn merge_axes(&mut self) where Self::Layout: ResizeLayout; fn move_axis(&mut self, from: usize, to: usize) where Self::Layout: MutLayout; fn permute(&mut self, order: Self::Index<'_>) where Self::Layout: MutLayout; fn transpose(&mut self) where Self::Layout: MutLayout; fn to_vec(&self) -> Vec<Self::Elem> where Self::Elem: Clone; fn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem> where Self::Elem: Clone; fn to_shape<S: IntoLayout>( &self, shape: S, ) -> TensorBase<Vec<Self::Elem>, S::Layout> where Self::Elem: Clone, Self::Layout: MutLayout; // Provided methods fn as_cow(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where [Self::Elem]: ToOwned { ... } fn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout> { ... } fn axis_chunks( &self, dim: usize, chunk_size: usize, ) -> AxisChunks<'_, Self::Elem, Self::Layout> where Self::Layout: MutLayout { ... } fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout> where Self::Layout: MutLayout + RemoveDim { ... } fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout> where Self::Layout: BroadcastLayout<S::Layout> { ... } fn try_broadcast<S: IntoLayout>( &self, shape: S, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, S::Layout>, ExpandError> where Self::Layout: BroadcastLayout<S::Layout> { ... } fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem> where Self::Layout: TrustedLayout { ... } unsafe fn get_unchecked<I: AsIndex<Self::Layout>>( &self, index: I, ) -> &Self::Elem { ... } fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output> where Self::Layout: MutLayout + RemoveDim { ... } fn inner_iter<const N: usize>( &self, ) -> InnerIter<'_, Self::Elem, NdLayout<N>> { ... } fn inner_iter_dyn(&self, n: usize) -> InnerIter<'_, Self::Elem, DynLayout> { ... } fn item(&self) -> Option<&Self::Elem> { ... } fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem> where Self::Layout: RemoveDim { ... } fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout> where F: Fn(&Self::Elem) -> U, Self::Layout: MutLayout { ... } fn map_in<A: Alloc, F, U>( &self, alloc: A, f: F, ) -> TensorBase<Vec<U>, Self::Layout> where F: Fn(&Self::Elem) -> U, Self::Layout: MutLayout { ... } fn nd_view<const N: usize>( &self, ) -> TensorBase<ViewData<'_, Self::Elem>, NdLayout<N>> { ... } fn permuted( &self, order: Self::Index<'_>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout> where Self::Layout: MutLayout { ... } fn reshaped<S: Copy + IntoLayout>( &self, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn reshaped_in<A: Alloc, S: Copy + IntoLayout>( &self, alloc: A, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout> where Self::Layout: MutLayout { ... } fn slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout> where Self::Layout: SliceWith<R, R::Count> { ... } fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout> where Self::Layout: MutLayout { ... } fn try_slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>, SliceError> where Self::Layout: SliceWith<R, R::Count> { ... } fn slice_copy<R: Clone + IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout> where Self::Elem: Clone, Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>> { ... } fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems + IndexCount>( &self, pool: A, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout> where Self::Elem: Clone, Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>> { ... } fn squeezed(&self) -> TensorView<'_, Self::Elem> where Self::Layout: MutLayout { ... } fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn to_slice(&self) -> Cow<'_, [Self::Elem]> where Self::Elem: Clone { ... } fn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn to_tensor_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<Vec<Self::Elem>, Self::Layout> where Self::Elem: Clone, Self::Layout: MutLayout { ... } fn weakly_checked_view( &self, ) -> WeaklyCheckedView<ViewData<'_, Self::Elem>, Self::Layout> { ... }
}
Expand description

Trait implemented by all variants of TensorBase, which provides a view method to get an immutable view of the tensor, plus methods which forward to such a view.

The purpose of this trait is to allow methods to be specialized for immutable views by preserving the lifetime of the underlying data in return types (eg. iter returns &[T] in the trait, but &'a [T] in the view). This allows for chaining operations on views together (eg. tensor.slice(...).transpose()) without needing to separate each step into separate statements.

This trait is conceptually similar to the way std::ops::Deref in the Rust standard library allows a Vec<T> to have all the methods of an &[T].

If stable Rust gains support for specialization or a Deref trait that can return non-references (see https://github.com/rust-lang/rfcs/issues/997) this will become unnecessary.

Required Associated Types§

Source

type Elem

Type of element stored in this tensor.

Source

type Layout: Clone + for<'a> Layout<Index<'a> = Self::Index<'a>>

The underlying layout of this tensor. It must have the same index type (eg. [usize; N] or &[usize]) as this view.

Required Methods§

Source

fn view(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>

Return a borrowed view of this tensor.

Source

fn layout(&self) -> &Self::Layout

Return the layout of this tensor.

Source

fn copy_into_slice<'a>( &self, dest: &'a mut [MaybeUninit<Self::Elem>], ) -> &'a [Self::Elem]
where Self::Elem: Copy,

Copy elements from this tensor into dest in logical order.

Returns the initialized slice. Panics if the length of dest does not match the number of elements in self.

Source

fn data(&self) -> Option<&[Self::Elem]>

Return the layout of this tensor as a slice, if it is contiguous.

Source

fn insert_axis(&mut self, index: usize)
where Self::Layout: ResizeLayout,

Insert a size-1 axis at the given index.

Source

fn remove_axis(&mut self, index: usize)
where Self::Layout: ResizeLayout,

Remove a size-1 axis at the given index.

This will panic if the index is out of bounds or the size of the index is not 1.

Source

fn iter(&self) -> Iter<'_, Self::Elem>

Return an iterator over elements in this tensor in their logical order.

Source

fn merge_axes(&mut self)
where Self::Layout: ResizeLayout,

Merge consecutive dimensions to the extent possible without copying data or changing the iteration order.

If the tensor is contiguous, this has the effect of flattening the tensor into a vector.

Source

fn move_axis(&mut self, from: usize, to: usize)
where Self::Layout: MutLayout,

Re-order the axes of this tensor to move the axis at index from to to.

Panics if from or to is >= self.ndim().

Source

fn permute(&mut self, order: Self::Index<'_>)
where Self::Layout: MutLayout,

Permute the dimensions of this tensor.

Source

fn transpose(&mut self)
where Self::Layout: MutLayout,

Reverse the order of dimensions in this tensor.

Source

fn to_vec(&self) -> Vec<Self::Elem>
where Self::Elem: Clone,

Return a vector containing the elements of this tensor in their logical order, ie. as if the tensor were flattened into one dimension.

Source

fn to_vec_in<A: Alloc>(&self, alloc: A) -> Vec<Self::Elem>
where Self::Elem: Clone,

Variant of to_vec which takes an allocator.

Source

fn to_shape<S: IntoLayout>( &self, shape: S, ) -> TensorBase<Vec<Self::Elem>, S::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Return a copy of this tensor with a given shape.

Provided Methods§

Source

fn as_cow(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where [Self::Elem]: ToOwned,

Return a view of this tensor using a borrowed CowData for storage.

Together with into_cow, this is useful where code needs to conditionally copy or create a new tensor, and get either the borrowed or owned tensor into the same type.

Source

fn as_dyn(&self) -> TensorBase<ViewData<'_, Self::Elem>, DynLayout>

Return a view of this tensor with a dynamic rank.

Source

fn axis_chunks( &self, dim: usize, chunk_size: usize, ) -> AxisChunks<'_, Self::Elem, Self::Layout>
where Self::Layout: MutLayout,

Return an iterator over slices of this tensor along a given axis.

Source

fn axis_iter(&self, dim: usize) -> AxisIter<'_, Self::Elem, Self::Layout>
where Self::Layout: MutLayout + RemoveDim,

Return an iterator over slices of this tensor along a given axis.

Source

fn broadcast<S: IntoLayout>( &self, shape: S, ) -> TensorBase<ViewData<'_, Self::Elem>, S::Layout>
where Self::Layout: BroadcastLayout<S::Layout>,

Broadcast this view to another shape.

If shape is an array ([usize; N]), the result will have a static-rank layout with N dims. If shape is a slice, the result will have a dynamic-rank layout.

Source

fn try_broadcast<S: IntoLayout>( &self, shape: S, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, S::Layout>, ExpandError>
where Self::Layout: BroadcastLayout<S::Layout>,

Fallible variant of broadcast.

Source

fn get<I: AsIndex<Self::Layout>>(&self, index: I) -> Option<&Self::Elem>
where Self::Layout: TrustedLayout,

Return a reference to the element at a given index, or None if the index is invalid.

Source

unsafe fn get_unchecked<I: AsIndex<Self::Layout>>( &self, index: I, ) -> &Self::Elem

Return a reference to the element at a given index, without performing bounds checks.

§Safety

The caller must ensure that the index is valid for the tensor’s shape.

Source

fn index_axis( &self, axis: usize, index: usize, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as RemoveDim>::Output>
where Self::Layout: MutLayout + RemoveDim,

Index the tensor along a given axis.

Returns a view with one dimension removed.

Panics if axis >= self.ndim() or index >= self.size(axis).

Source

fn inner_iter<const N: usize>(&self) -> InnerIter<'_, Self::Elem, NdLayout<N>>

Return an iterator over the innermost N dimensions.

Source

fn inner_iter_dyn(&self, n: usize) -> InnerIter<'_, Self::Elem, DynLayout>

Return an iterator over the innermost n dimensions.

Prefer inner_iter if N is known at compile time.

Source

fn item(&self) -> Option<&Self::Elem>

Return the scalar value in this tensor if it has 0 dimensions.

Source

fn lanes(&self, dim: usize) -> Lanes<'_, Self::Elem>
where Self::Layout: RemoveDim,

Return an iterator over 1D slices of this tensor along a given axis.

Source

fn map<F, U>(&self, f: F) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U, Self::Layout: MutLayout,

Return a new tensor with the same shape, formed by applying f to each element in this tensor.

Source

fn map_in<A: Alloc, F, U>( &self, alloc: A, f: F, ) -> TensorBase<Vec<U>, Self::Layout>
where F: Fn(&Self::Elem) -> U, Self::Layout: MutLayout,

Variant of map which takes an allocator.

Source

fn nd_view<const N: usize>( &self, ) -> TensorBase<ViewData<'_, Self::Elem>, NdLayout<N>>

Convert this tensor to one with the same shape but a static dimension count.

Panics if self.ndim() != N.

Source

fn permuted( &self, order: Self::Index<'_>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
where Self::Layout: MutLayout,

Return a view with dimensions permuted in the order given by dims.

Source

fn reshaped<S: Copy + IntoLayout>( &self, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Return either a view or a copy of self with the given shape.

The new shape must have the same number of elments as the current shape. The result will have a static rank if shape is an array or a dynamic rank if it is a slice.

If self is contiguous this will return a view, as changing the shape can be done without moving data. Otherwise it will copy elements into a new tensor.

§Panics

Panics if the number of elements in the new shape does not match the current shape.

Source

fn reshaped_in<A: Alloc, S: Copy + IntoLayout>( &self, alloc: A, shape: S, ) -> TensorBase<CowData<'_, Self::Elem>, S::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

A variant of reshaped that allows specifying the allocator to use if a copy is needed.

Source

fn transposed(&self) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
where Self::Layout: MutLayout,

Return a view with the order of dimensions reversed.

Source

fn slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Layout: SliceWith<R, R::Count>,

Slice this tensor and return a view.

If both this tensor’s layout and the range have a statically-known number of index terms, the result will have a static rank. Otherwise it will have a dynamic rank.

use rten_tensor::prelude::*;
use rten_tensor::NdTensor;

let x = NdTensor::from([[1, 2], [3, 4]]);
let col = x.slice((.., 1)); // `col` is an `NdTensorView<i32, 1>`
assert_eq!(col.shape(), [2usize]);
assert_eq!(col.to_vec(), [2, 4]);
Source

fn slice_axis( &self, axis: usize, range: Range<usize>, ) -> TensorBase<ViewData<'_, Self::Elem>, Self::Layout>
where Self::Layout: MutLayout,

Slice this tensor along a given axis.

Source

fn try_slice<R: IntoSliceItems + IndexCount>( &self, range: R, ) -> Result<TensorBase<ViewData<'_, Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>, SliceError>
where Self::Layout: SliceWith<R, R::Count>,

A variant of slice that returns a result instead of panicking.

Source

fn slice_copy<R: Clone + IntoSliceItems + IndexCount>( &self, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Elem: Clone, Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>>,

Return a slice of this tensor as an owned tensor.

This is more expensive than slice as it copies the data, but is more flexible as it supports ranges with negative steps.

Source

fn slice_copy_in<A: Alloc, R: Clone + IntoSliceItems + IndexCount>( &self, pool: A, range: R, ) -> TensorBase<Vec<Self::Elem>, <Self::Layout as SliceWith<R, R::Count>>::Layout>
where Self::Elem: Clone, Self::Layout: SliceWith<R, R::Count, Layout: for<'a> Layout<Index<'a>: TryFrom<&'a [usize], Error: Debug>>>,

Variant of slice_copy which takes an allocator.

Source

fn squeezed(&self) -> TensorView<'_, Self::Elem>
where Self::Layout: MutLayout,

Return a view of this tensor with all dimensions of size 1 removed.

Source

fn to_contiguous(&self) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Return a tensor with the same shape as this tensor/view but with the data contiguous in memory and arranged in the same order as the logical/iteration order (used by iter).

This will return a view if the data is already contiguous or copy data into a new buffer otherwise.

Certain operations require or are faster with contiguous tensors.

Source

fn to_contiguous_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<CowData<'_, Self::Elem>, Self::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Variant of to_contiguous which takes an allocator.

Source

fn to_slice(&self) -> Cow<'_, [Self::Elem]>
where Self::Elem: Clone,

Return a slice containing the elements of this tensor in their logical order, ie. as if the tensor were flattened into one dimension.

Unlike data this will copy the elements if they are not contiguous. Unlike to_vec this will not copy the elements if the tensor is already contiguous.

Source

fn to_tensor(&self) -> TensorBase<Vec<Self::Elem>, Self::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Return a copy of this tensor/view which uniquely owns its elements.

Source

fn to_tensor_in<A: Alloc>( &self, alloc: A, ) -> TensorBase<Vec<Self::Elem>, Self::Layout>
where Self::Elem: Clone, Self::Layout: MutLayout,

Variant of to_tensor which takes an allocator.

Source

fn weakly_checked_view( &self, ) -> WeaklyCheckedView<ViewData<'_, Self::Elem>, Self::Layout>

Return a view which performs “weak” checking when indexing via view[<index>]. See WeaklyCheckedView for an explanation.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<T, S: Storage<Elem = T>, L: Layout + Clone> AsView for TensorBase<S, L>

Source§

type Elem = T

Source§

type Layout = L