pub enum TensorWrite<'a> {
Tensor(&'a mut Tensor),
View(TensorViewMut<'a>),
}Expand description
Mutable tensor output accepted by synchronous eager kernels.
TensorWrite mirrors TensorRead for output dispatch: it can target an
owned compact Tensor or a borrowed mutable TensorViewMut. The target
is never resized.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![1], vec![0.0_f64])?;
let write = TensorWrite::from_tensor(&mut tensor);
assert_eq!(write.shape(), &[1]);Variants§
Tensor(&'a mut Tensor)
View(TensorViewMut<'a>)
Implementations§
Source§impl<'a> TensorWrite<'a>
impl<'a> TensorWrite<'a>
Sourcepub fn from_tensor(tensor: &'a mut Tensor) -> Self
pub fn from_tensor(tensor: &'a mut Tensor) -> Self
Borrow an owned tensor as a writable target without copying it.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let write = TensorWrite::from_tensor(&mut tensor);
assert_eq!(write.shape(), &[2]);Sourcepub fn from_view(view: TensorViewMut<'a>) -> Self
pub fn from_view(view: TensorViewMut<'a>) -> Self
Wrap a borrowed mutable dtype-erased view as a writable target.
§Examples
use tenferro_tensor::{DType, TensorViewMut, TensorWrite};
let mut data = [1.0_f64, 2.0];
let write = TensorWrite::from_view(TensorViewMut::f64(&[2], &mut data)?);
assert_eq!(write.dtype(), DType::F64);
assert_eq!(write.as_read().as_slice::<f64>()?, &[1.0, 2.0]);Sourcepub fn as_read(&self) -> TensorRead<'_>
pub fn as_read(&self) -> TensorRead<'_>
Borrow this writable target as a read-only tensor input.
This is useful for explicit read-modify-write kernels such as
accumulation updates. The returned view borrows through &self, so it
cannot outlive the current read-only borrow of the writable target.
§Examples
use tenferro_tensor::{DType, Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![1], vec![2.0_f64])?;
let write = TensorWrite::from_tensor(&mut tensor);
let read = write.as_read();
assert_eq!(read.dtype(), DType::F64);Sourcepub fn dtype(&self) -> DType
pub fn dtype(&self) -> DType
Return the element dtype of this writable target.
§Examples
use tenferro_tensor::{DType, Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![1], vec![1_i32])?;
assert_eq!(TensorWrite::from_tensor(&mut tensor).dtype(), DType::I32);Sourcepub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Return the logical shape of this writable target.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
assert_eq!(TensorWrite::from_tensor(&mut tensor).shape(), &[2, 1]);Sourcepub fn strides(&self) -> Result<Vec<isize>>
pub fn strides(&self) -> Result<Vec<isize>>
Return strides in element units; owned tensors report compact column-major strides.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![2, 3], vec![0.0_f64; 6])?;
assert_eq!(TensorWrite::from_tensor(&mut tensor).strides()?, vec![1, 2]);§Errors
Returns crate::Error::Validation with
tenferro_tensor_core::ValidationError::IntegerOverflow when
column-major stride arithmetic overflows.
Sourcepub fn offset(&self) -> isize
pub fn offset(&self) -> isize
Return the physical element offset; owned tensors always start at 0.
§Examples
use tenferro_tensor::{TensorViewMut, TensorWrite, TypedTensorViewMut};
let mut data = [1.0_f64, 2.0, 3.0];
let view = TensorViewMut::F64(TypedTensorViewMut::from_slice(vec![2], vec![1], 1, &mut data)?);
assert_eq!(TensorWrite::from_view(view).offset(), 1);Sourcepub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize>
pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize>
Compute the physical element offset for a logical index.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![2, 3], vec![0.0_f64; 6])?;
let write = TensorWrite::from_tensor(&mut tensor);
assert_eq!(write.layout_linear_offset(&[1, 2])?, 5);
assert!(write.layout_linear_offset(&[0]).is_err());§Errors
Returns crate::Error::Validation with
tenferro_tensor_core::ValidationError::RankMismatch when indices
has the wrong rank, tenferro_tensor_core::ValidationError::InvalidArgument
when an index is outside its axis extent, or
tenferro_tensor_core::ValidationError::IntegerOverflow when offset
arithmetic overflows.
Sourcepub fn is_col_major_contiguous(&self) -> Result<bool>
pub fn is_col_major_contiguous(&self) -> Result<bool>
Return whether this writable target is compact column-major.
§Examples
use tenferro_tensor::{Tensor, TensorViewMut, TensorWrite, TypedTensorViewMut};
let mut tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert!(TensorWrite::from_tensor(&mut tensor).is_col_major_contiguous()?);
let mut data = [1.0_f64, 2.0, 3.0];
let strided = TensorViewMut::F64(TypedTensorViewMut::from_slice(vec![2], vec![2], 0, &mut data)?);
assert!(!TensorWrite::from_view(strided).is_col_major_contiguous()?);§Errors
Returns crate::Error::Validation with
tenferro_tensor_core::ValidationError::IntegerOverflow when
compactness arithmetic overflows.
Sourcepub fn layout_summary(&self) -> String
pub fn layout_summary(&self) -> String
Return a compact string summary of this writable target’s layout metadata.
§Examples
use tenferro_tensor::{Tensor, TensorWrite};
let mut tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(
TensorWrite::from_tensor(&mut tensor).layout_summary(),
"shape=[2] strides=[1] offset=0"
);Sourcepub fn assert_col_major_contiguous(&self) -> Result<()>
pub fn assert_col_major_contiguous(&self) -> Result<()>
Assert this writable target is compact column-major.
§Examples
use tenferro_tensor::{Tensor, TensorViewMut, TensorWrite, TypedTensorViewMut};
let mut tensor = Tensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
TensorWrite::from_tensor(&mut tensor).assert_col_major_contiguous()?;
let mut data = [1.0_f64, 2.0, 3.0];
let strided = TensorViewMut::F64(TypedTensorViewMut::from_slice(vec![2], vec![2], 0, &mut data)?);
assert!(TensorWrite::from_view(strided).assert_col_major_contiguous().is_err());§Errors
Returns crate::Error::Validation with
tenferro_tensor_core::ValidationError::IntegerOverflow when
compactness arithmetic overflows, or
tenferro_tensor_core::ValidationError::InvalidArgument when the
view is not compact column-major.