Struct TensorValue
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§
§impl TensorValue
impl TensorValue
pub fn duplicate(&self) -> Result<TensorValue, Error>
pub fn duplicate(&self) -> Result<TensorValue, Error>
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.
pub fn from_tensor(tensor: Tensor) -> TensorValue
pub fn from_tensor(tensor: Tensor) -> TensorValue
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.]);pub fn from_parts(
tensor: Tensor,
shape: Vec<usize>,
strides: Vec<isize>,
offset: isize,
) -> Result<TensorValue, Error>
pub fn from_parts( tensor: Tensor, shape: Vec<usize>, strides: Vec<isize>, offset: isize, ) -> Result<TensorValue, Error>
§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);pub fn into_tensor(self) -> Result<Tensor, Error>
pub fn into_tensor(self) -> Result<Tensor, Error>
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.
pub fn as_tensor(&self) -> Option<&Tensor>
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());pub fn is_view(&self) -> bool
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());pub fn dtype(&self) -> DType
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);pub fn shape(&self) -> &[usize]
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]);pub fn strides(&self) -> &[isize]
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]);pub fn offset(&self) -> isize
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);pub fn tensor_view(&self) -> TensorView<'_>
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);pub fn tensor_read(&self) -> TensorRead<'_>
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);pub fn transpose_view(
self,
axes: impl AsRef<[usize]>,
) -> Result<TensorValue, Error>
pub fn transpose_view( self, axes: impl AsRef<[usize]>, ) -> Result<TensorValue, Error>
§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());pub fn try_reshape_view(
self,
shape: impl IntoShapeVec,
) -> Result<TensorValue, TensorValueViewError>
pub fn try_reshape_view( self, shape: impl IntoShapeVec, ) -> Result<TensorValue, 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 { .. }));pub fn reshape_view(
self,
shape: impl IntoShapeVec,
) -> Result<TensorValue, Error>
pub fn reshape_view( self, shape: impl IntoShapeVec, ) -> Result<TensorValue, Error>
§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]);pub fn slice_view(self, config: &SliceConfig) -> Result<TensorValue, Error>
pub fn slice_view(self, config: &SliceConfig) -> Result<TensorValue, Error>
§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);pub fn broadcast_in_dim_view(
self,
shape: impl IntoShapeVec,
dims: impl AsRef<[usize]>,
) -> Result<TensorValue, Error>
pub fn broadcast_in_dim_view( self, shape: impl IntoShapeVec, dims: impl AsRef<[usize]>, ) -> Result<TensorValue, Error>
§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]);