TensorView

Struct TensorView 

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

Borrowed tensor view, lifetime-tied to the source Tensor.

TensorView is the borrowed counterpart to Tensor, following the String/&str pattern. It references the source tensor’s data buffer without copying.

§Public vs. internal views

Public API methods (Tensor::tensor_view, etc.) call Tensor::wait before constructing a view, so the returned TensorView always has event = None — data is ready to read.

The crate-internal as_operand_view() skips the wait and propagates the pending event, allowing accelerator operations to chain without CPU synchronization.

Implementations§

Source§

impl<'a, T: Scalar> TensorView<'a, T>

Source

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

Returns the shape (size of each dimension).

Source

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

Returns the strides (in units of T).

Source

pub fn ndim(&self) -> usize

Returns the number of dimensions (rank).

Source

pub fn logical_memory_space(&self) -> LogicalMemorySpace

Returns the logical memory space where the source tensor’s data resides.

Source

pub fn preferred_compute_device(&self) -> Option<ComputeDevice>

Returns the preferred compute device override, if set.

Source

pub fn buffer(&self) -> &DataBuffer<T>

Returns a reference to the underlying data buffer.

Source

pub fn offset(&self) -> isize

Returns the element offset into the data buffer.

Source

pub fn permute(&self, _perm: &[usize]) -> Result<TensorView<'a, T>>

Permute (reorder) the dimensions of this view.

Returns a new TensorView with reordered dims and strides (zero-copy).

§Errors

Returns an error if perm is not a valid permutation of 0..ndim().

Source

pub fn broadcast(&self, _target_dims: &[usize]) -> Result<TensorView<'a, T>>

Broadcast this view to a larger shape.

Dimensions of size 1 are expanded to the target size (zero-copy via stride 0).

§Errors

Returns an error if target_dims is incompatible with the current shape.

Source

pub fn diagonal(&self, _axes: &[(usize, usize)]) -> Result<TensorView<'a, T>>

Extract a diagonal view by merging pairs of axes.

§Errors

Returns an error if any axis is out of range or paired dimensions have different sizes.

Source

pub fn select(&self, _dim: usize, _index: usize) -> Result<TensorView<'a, T>>

Select a single index along a dimension, removing that dimension.

Returns a view with ndim() - 1 dimensions. Zero-copy: adjusts offset and removes the selected dimension from dims/strides.

§Errors

Returns an error if dim >= ndim() or index >= dims()[dim].

§Examples
use tenferro_tensor::{Tensor, MemoryOrder};
use tenferro_device::LogicalMemorySpace;

let a = Tensor::<f64>::zeros(&[3, 4, 10],
    LogicalMemorySpace::MainMemory, MemoryOrder::ColumnMajor);
let tv = a.tensor_view();
// Select batch index 5 → view of shape [3, 4]
let mat = tv.select(2, 5).unwrap();
assert_eq!(mat.dims(), &[3, 4]);
Source

pub fn narrow( &self, _dim: usize, _start: usize, _length: usize, ) -> Result<TensorView<'a, T>>

Narrow (slice) a dimension to a sub-range.

Returns a view with the same number of dimensions, but dims()[dim] reduced to length. Zero-copy: only offset and dim size change.

§Errors

Returns an error if dim >= ndim() or start + length > dims()[dim].

§Examples
use tenferro_tensor::{Tensor, MemoryOrder};
use tenferro_device::LogicalMemorySpace;

let a = Tensor::<f64>::zeros(&[3, 10],
    LogicalMemorySpace::MainMemory, MemoryOrder::ColumnMajor);
let tv = a.tensor_view();
// Take columns 2..5 → view of shape [3, 3]
let sub = tv.narrow(1, 2, 3).unwrap();
assert_eq!(sub.dims(), &[3, 3]);
Source

pub fn to_tensor(&self, _order: MemoryOrder) -> Tensor<T>

Copy this view into an owned Tensor.

Source

pub fn contiguous(&self, _order: MemoryOrder) -> Tensor<T>

Return a contiguous copy of this view’s data.

Source

pub fn conj(&self) -> Tensor<T>
where T: Conjugate,

Return a tensor with complex-conjugated elements from this view.

For real types, returns a copy unchanged.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

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.