Skip to main content

TensorViewMut

Enum TensorViewMut 

Source
pub enum TensorViewMut<'a, D: Representation = Dynamic> {
    F32(TypedTensorViewMut<'a, f32, DynRank, D>),
    F64(TypedTensorViewMut<'a, f64, DynRank, D>),
    I32(TypedTensorViewMut<'a, i32, DynRank, D>),
    I64(TypedTensorViewMut<'a, i64, DynRank, D>),
    Bool(TypedTensorViewMut<'a, bool, DynRank, D>),
    C32(TypedTensorViewMut<'a, Complex<f32>, DynRank, D>),
    C64(TypedTensorViewMut<'a, Complex<f64>, DynRank, D>),
}
Expand description

Dynamic mutable borrowed tensor view.

TensorViewMut is the mutable counterpart to TensorView. It keeps the dtype erased while preserving the typed mutable view’s shape, strides, and offset metadata.

§Examples

use tenferro_tensor::{DType, TensorViewMut, TypedTensorViewMut};

let mut data = [1.0_f64, 2.0];
let view = TensorViewMut::F64(TypedTensorViewMut::from_slice([2], [1], 0, &mut data)?);
assert_eq!(view.dtype(), DType::F64);

D marks the representation the view was proven to borrow, as on TypedTensorViewMut. The default Dynamic view may borrow host or backend storage; a TensorViewMut<'a, Host> comes only from TensorViewMut::try_mut_host or TensorWrite::try_mut_host, so every arm borrows host storage and a consumer writes it without a further placement check.

Variants§

Implementations§

Source§

impl<'a> TensorViewMut<'a>

Source

pub fn try_mut_host(self, op: &'static str) -> Result<TensorViewMut<'a, Host>>

Checked narrowing of a dtype-erased mutable view to the host representation.

See TypedTensorViewMut::try_mut_host: the narrowing succeeds exactly when the view borrows host elements, and a consumer of the TensorViewMut<'a, Host> it returns needs no further placement check.

§Examples
use tenferro_tensor::{DType, TensorViewMut};

let mut data = [1.0_f64, 2.0];
let host = TensorViewMut::f64(&[2], &mut data)?.try_mut_host("example")?;
assert_eq!(host.dtype(), DType::F64);
assert_eq!(host.shape(), &[2]);
§Errors

Returns crate::Error::RuntimeState attributed to op when the view borrows backend (device) storage; download the tensor to host first.

Source§

impl<'a> TensorViewMut<'a, Host>

Source

pub fn into_dynamic(self) -> TensorViewMut<'a>

Widen a host-marked mutable view to the runtime union without copying.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [3_i32, 4];
let host = TensorViewMut::i32(&[2], &mut data)?.try_mut_host("example")?;
let dynamic = host.into_dynamic();
assert_eq!(dynamic.as_read_only().as_slice::<i32>()?, &[3, 4]);
Source

pub fn dtype(&self) -> DType

The element dtype of this host view.

§Examples
use tenferro_tensor::{DType, TensorViewMut};

let mut data = [7_i64];
let host = TensorViewMut::i64(&[1], &mut data)?.try_mut_host("example")?;
assert_eq!(host.dtype(), DType::I64);
Source

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

The logical shape of this host view.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [1.0_f32, 2.0];
let host = TensorViewMut::f32(&[2], &mut data)?.try_mut_host("example")?;
assert_eq!(host.shape(), &[2]);
Source

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

The strides of this host view in element units.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [0.0_f64; 6];
let host = TensorViewMut::f64(&[2, 3], &mut data)?.try_mut_host("example")?;
assert_eq!(host.strides(), &[1, 2]);
Source§

impl<'a> TensorViewMut<'a>

Source

pub fn f32(shape: &'a [usize], data: &'a mut [f32]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn f64(shape: &'a [usize], data: &'a mut [f64]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn i32(shape: &'a [usize], data: &'a mut [i32]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn i64(shape: &'a [usize], data: &'a mut [i64]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn bool(shape: &'a [usize], data: &'a mut [bool]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn c32(shape: &'a [usize], data: &'a mut [Complex32]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn c64(shape: &'a [usize], data: &'a mut [Complex64]) -> Result<Self>

Create a dynamic mutable view over compact column-major host data.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compact layout arithmetic overflows, or tenferro_tensor_core::ValidationError::InvalidArgument when the requested shape exceeds data.

Source

pub fn dtype(&self) -> DType

Source

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

Source

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

Return strides in element units.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [0.0_f64; 6];
let view = TensorViewMut::f64(&[2, 3], &mut data)?;
assert_eq!(view.strides(), &[1, 2]);
Source

pub fn offset(&self) -> isize

Source

pub fn is_host_memory(&self) -> bool

Return whether this mutable view writes host-addressed memory.

See TypedTensorViewMut::is_host_memory; it is the same answer Self::try_mut_host acts on.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [1.0_f64, 2.0];
let view = TensorViewMut::f64(&[2], &mut data)?;
assert!(view.is_host_memory());
Source

pub fn layout_linear_offset(&self, indices: &[usize]) -> Result<usize>

Compute the physical element offset for a logical index.

§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.

Source

pub fn is_col_major_contiguous(&self) -> Result<bool>

Return whether this view is compact column-major.

§Errors

Returns crate::Error::Validation with tenferro_tensor_core::ValidationError::IntegerOverflow when compactness arithmetic overflows.

Source

pub fn layout_summary(&self) -> String

Source

pub fn assert_col_major_contiguous(&self) -> Result<()>

Assert this view is compact column-major.

§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.

Source

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

Explicitly duplicate the compact host data visible through this mutable view into a new tensor owner.

§Examples
use tenferro_tensor::{TensorViewMut, TypedTensorViewMut};

let mut data = [1_i32, 2];
let view = TensorViewMut::I32(TypedTensorViewMut::from_slice(
    vec![2], vec![1], 0, &mut data,
)?);
let copy = view.duplicate()?;
assert_eq!(copy.shape(), &[2]);
§Errors

Returns crate::Error::HostAccess for backend-owned views, ValidationError::NonContiguousViewAsSlice for non-contiguous views, or ValidationError::InvalidArgument for invalid layout metadata.

Source

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

Borrow this mutable view as a read-only dtype-erased view.

§Examples
use tenferro_tensor::TensorViewMut;

let mut data = [1.0_f64, 2.0];
let view = TensorViewMut::f64(&[2], &mut data)?;
assert_eq!(view.as_read_only().as_slice::<f64>()?, &[1.0, 2.0]);

Trait Implementations§

Source§

impl<'a, D: Debug + Representation> Debug for TensorViewMut<'a, D>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> From<TensorViewMut<'a>> for TensorWrite<'a>

Source§

fn from(view: TensorViewMut<'a>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a, D = Dynamic> !RefUnwindSafe for TensorViewMut<'a, D>

§

impl<'a, D = Dynamic> !Sync for TensorViewMut<'a, D>

§

impl<'a, D = Dynamic> !UnwindSafe for TensorViewMut<'a, D>

§

impl<'a, D> Freeze for TensorViewMut<'a, D>

§

impl<'a, D> Send for TensorViewMut<'a, D>
where D: Send,

§

impl<'a, D> Unpin for TensorViewMut<'a, D>
where D: Unpin,

§

impl<'a, D> UnsafeUnpin for TensorViewMut<'a, D>

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.