Struct TensorView

Source
pub struct TensorView<'a> { /* private fields */ }
Expand description

A view into the original Tensor. A view borrows part of (or even the entirety of) the original Tensor data. It can NOT modify the data itself. It is normally produced by indexing or slicing a Tensor.

It can be useful for creating a new Tensor from part of another one:

§Examples

use tensor_compute::{Tensor, TensorView, s};
let tensor = Tensor::from_data_and_shape(vec![1., 2., 3., 4.], vec![2, 2]);
let first_row: TensorView = tensor.slice(s![1]);
assert_eq!(first_row.shape(), &[1, 2]);
assert_eq!(first_row.to_cpu().as_contiguous_vec(), &[3., 4.]);
let new_tensor = first_row.make_contiguous(); // creates a new owned tensor from the view
assert_eq!(new_tensor.shape(), &[1, 2]);
assert_eq!(new_tensor.to_cpu().as_contiguous_vec(), &[3., 4.]);

Implementations§

Source§

impl<'a> TensorView<'a>

Source

pub async fn make_contiguous_async(&self) -> Tensor

Same as TensorView::make_contiguous, but async.

Source

pub fn make_contiguous(&self) -> Tensor

Copies the TensorView into a standalone contiguous Tensor

§Examples
use tensor_compute::{Tensor, TensorView, s};
let tensor = Tensor::from_data_and_shape(vec![1., 2., 3., 4., 5., 6., 7., 8.], vec![2, 2, 2]);
let first_row: TensorView = tensor.slice(s![1]);
assert_eq!(first_row.shape(), &[1, 2, 2]);
assert_eq!(first_row.to_cpu().as_contiguous_vec(), &[5., 6., 7., 8.]);
let new_tensor = first_row.make_contiguous(); // creates a new owned tensor from the view
assert_eq!(new_tensor.shape(), &[1, 2, 2]);
assert_eq!(new_tensor.to_cpu().as_contiguous_vec(), &[5., 6., 7., 8.]);
assert!(new_tensor.slice(s![..]).compare(&first_row));
Source

pub async fn compare_async(&self, other: &Self) -> bool

Source

pub fn compare(&self, other: &Self) -> bool

Source

pub fn shape(&self) -> &VecDeque<usize>

Same as Tensor::shape

Source

pub fn strides(&self) -> &VecDeque<usize>

Source

pub async fn to_cpu_async(&self) -> CpuTensor

Source

pub fn to_cpu(&self) -> CpuTensor

Trait Implementations§

Source§

impl<'a> Debug for TensorView<'a>

Beware, printing the TensorView forces a copy from GPU memory to CPU memory.

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for TensorView<'a>

§

impl<'a> !RefUnwindSafe for TensorView<'a>

§

impl<'a> Send for TensorView<'a>

§

impl<'a> Sync for TensorView<'a>

§

impl<'a> Unpin for TensorView<'a>

§

impl<'a> !UnwindSafe for TensorView<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V