Skip to main content

TensorValue

Struct TensorValue 

Source
pub struct TensorValue { /* private fields */ }
Expand description

Owned tensor value with one move-only physical owner and metadata-only layout.

TensorValue is intentionally not cloneable. View transformations consume the value and move its existing owner; TensorValue::duplicate is the explicit boundary for creating another physical allocation.

§Examples

use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let view = value.transpose_view([1, 0])?;
assert_eq!(view.strides(), &[2, 1]);
assert!(view.is_view());

Implementations§

Source§

impl TensorValue

Source

pub fn duplicate(&self) -> Result<Self>

Explicitly duplicate the physical owner represented by this value.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2], vec![3., 4.])?);
let copy = value.duplicate()?.into_tensor()?;
assert_eq!(copy.as_slice::<f64>()?, &[3., 4.]);
assert_eq!(value.shape(), &[2]);
§Errors

Returns crate::Error::RuntimeState or crate::Error::Unsupported when the backend/storage owner cannot be duplicated.

Source

pub fn from_tensor(tensor: Tensor) -> Self

Retain a compact tensor as a move-only value.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2], vec![3., 4.])?);
assert!(!value.is_view());
assert_eq!(value.into_tensor()?.as_slice::<f64>()?, &[3., 4.]);
Source

pub fn from_parts( tensor: Tensor, shape: Vec<usize>, strides: Vec<isize>, offset: isize, ) -> Result<Self>

§Errors

Returns ValidationError::InvalidArgument or ValidationError::IntegerOverflow when the supplied layout is invalid for the tensor’s physical buffer.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let tensor = Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?;
let view = TensorValue::from_parts(tensor, vec![2], vec![1], 2)?;
assert_eq!(view.shape(), &[2]);
assert_eq!(view.offset(), 2);
Source

pub fn into_tensor(self) -> Result<Tensor>

Consume a value with its owner’s original layout and return that owner.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2], vec![3., 4.])?);
assert_eq!(value.into_tensor()?.as_slice::<f64>()?, &[3., 4.]);
§Errors

Returns crate::Error::Unsupported when the value’s metadata-only view differs from its physical owner’s layout.

Source

pub fn as_tensor(&self) -> Option<&Tensor>

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert_eq!(value.as_tensor().unwrap().as_slice::<f64>()?, &[1., 2., 3., 4.]);
assert!(value.transpose_view([1, 0])?.as_tensor().is_none());
Source

pub fn is_view(&self) -> bool

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert!(!value.is_view());
assert!(value.transpose_view([1, 0])?.is_view());
Source

pub fn dtype(&self) -> DType

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert_eq!(value.dtype(), tenferro_tensor::DType::F64);
Source

pub fn shape(&self) -> &[usize]

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert_eq!(value.reshape_view([4])?.shape(), &[4]);
Source

pub fn strides(&self) -> &[isize]

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert_eq!(value.transpose_view([1, 0])?.strides(), &[2, 1]);
Source

pub fn offset(&self) -> isize

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
assert_eq!(value.offset(), 0);
Source

pub fn tensor_view(&self) -> TensorView<'_>

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let view = value.tensor_view();
assert_eq!(view.shape(), &[2, 2]);
assert_eq!(view.dtype(), tenferro_tensor::DType::F64);
Source

pub fn tensor_read(&self) -> TensorRead<'_>

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let read = value.tensor_read();
assert_eq!(read.shape(), &[2, 2]);
assert_eq!(read.dtype(), tenferro_tensor::DType::F64);
Source

pub fn transpose_view(self, axes: impl AsRef<[usize]>) -> Result<Self>

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::InvalidPermutationLength, tenferro_tensor_core::ValidationError::AxisOutOfBounds, or tenferro_tensor_core::ValidationError::DuplicateAxis when axes is not a valid permutation of the value rank.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let view = value.transpose_view([1, 0])?;
assert_eq!(view.strides(), &[2, 1]);
assert!(view.is_view());
Source

pub fn try_reshape_view( self, shape: impl IntoShapeVec, ) -> Result<Self, TensorValueViewError>

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::NonContiguousViewAsSlice when the source is not compact column-major, tenferro_tensor_core::ValidationError::ShapeMismatch (whose tenferro_tensor_core::ShapeMismatch::ReshapeElementCount source records the counts) when element counts differ, tenferro_tensor_core::ValidationError::IntegerOverflow for shape arithmetic overflow, or tenferro_tensor_core::ValidationError::ViewOutOfBounds when the reshaped view exceeds the backing buffer.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let (recovered, error) = value.try_reshape_view([3]).unwrap_err().into_parts();
assert_eq!(recovered.into_tensor()?.as_slice::<f64>()?, &[1., 2., 3., 4.]);
assert!(matches!(error, tenferro_tensor::Error::Validation { .. }));
Source

pub fn reshape_view(self, shape: impl IntoShapeVec) -> Result<Self>

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::NonContiguousViewAsSlice when the source is not compact, tenferro_tensor_core::ValidationError::ShapeMismatch when element counts differ, or tenferro_tensor_core::ValidationError::IntegerOverflow / tenferro_tensor_core::ValidationError::ViewOutOfBounds for invalid target-shape arithmetic or bounds.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let view = value.reshape_view([4])?;
assert_eq!(view.shape(), &[4]);
assert_eq!(view.strides(), &[1]);
Source

pub fn slice_view(self, config: &SliceConfig) -> Result<Self>

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::RankMismatch when a slice vector does not match the value rank, tenferro_tensor_core::ValidationError::InvalidArgument when a bound or stride cannot be represented or is invalid, tenferro_tensor_core::ValidationError::InvalidSliceStep or tenferro_tensor_core::ValidationError::InvalidSliceBounds for slice parameters, tenferro_tensor_core::ValidationError::IntegerOverflow for slice arithmetic overflow, or tenferro_tensor_core::ValidationError::ViewOutOfBounds when the result exceeds the backing buffer.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2, 2], vec![1., 2., 3., 4.])?);
let view = value.slice_view(&tenferro_tensor::SliceConfig {
   starts: vec![0, 1], limits: vec![2, 2], strides: vec![1, 1],
})?;
assert_eq!(view.shape(), &[2, 1]);
assert_eq!(view.offset(), 2);
Source

pub fn broadcast_in_dim_view( self, shape: impl IntoShapeVec, dims: impl AsRef<[usize]>, ) -> Result<Self>

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::RankMismatch, tenferro_tensor_core::ValidationError::AxisOutOfBounds, or tenferro_tensor_core::ValidationError::DuplicateAxis for invalid dimension mappings, tenferro_tensor_core::ValidationError::ShapeDataLengthMismatch for incompatible extents, tenferro_tensor_core::ValidationError::ViewOutOfBounds when the result exceeds the backing buffer, or tenferro_tensor_core::ValidationError::IntegerOverflow for layout arithmetic overflow.

§Examples
use tenferro_tensor::{Tensor, TensorValue};
let value = TensorValue::from_tensor(Tensor::from_vec_col_major([2], vec![3., 4.])?);
let view = value.broadcast_in_dim_view([2, 3], [0])?;
assert_eq!(view.shape(), &[2, 3]);
assert_eq!(view.strides(), &[1, 0]);

Trait Implementations§

Source§

impl Debug for TensorValue

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.