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>
impl<'a, T: Scalar> TensorView<'a, T>
Sourcepub fn logical_memory_space(&self) -> LogicalMemorySpace
pub fn logical_memory_space(&self) -> LogicalMemorySpace
Returns the logical memory space where the source tensor’s data resides.
Sourcepub fn preferred_compute_device(&self) -> Option<ComputeDevice>
pub fn preferred_compute_device(&self) -> Option<ComputeDevice>
Returns the preferred compute device override, if set.
Sourcepub fn buffer(&self) -> &DataBuffer<T>
pub fn buffer(&self) -> &DataBuffer<T>
Returns a reference to the underlying data buffer.
Sourcepub fn permute(&self, _perm: &[usize]) -> Result<TensorView<'a, T>>
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().
Sourcepub fn broadcast(&self, _target_dims: &[usize]) -> Result<TensorView<'a, T>>
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.
Sourcepub fn diagonal(&self, _axes: &[(usize, usize)]) -> Result<TensorView<'a, T>>
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.
Sourcepub fn select(&self, _dim: usize, _index: usize) -> Result<TensorView<'a, T>>
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]);Sourcepub fn narrow(
&self,
_dim: usize,
_start: usize,
_length: usize,
) -> Result<TensorView<'a, T>>
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]);Sourcepub fn to_tensor(&self, _order: MemoryOrder) -> Tensor<T>
pub fn to_tensor(&self, _order: MemoryOrder) -> Tensor<T>
Copy this view into an owned Tensor.
Sourcepub fn contiguous(&self, _order: MemoryOrder) -> Tensor<T>
pub fn contiguous(&self, _order: MemoryOrder) -> Tensor<T>
Return a contiguous copy of this view’s data.