pub struct Tensor { /* private fields */ }Expand description
A tensor: shared storage viewed through a layout.
Cloning a tensor is cheap — it clones the layout and bumps the storage
refcount, never the data. View operations (reshape, permute,
narrow, …) produce new tensors over the same storage whenever the
layout arithmetic allows it.
Implementations§
Source§impl Tensor
impl Tensor
Sourcepub fn from_storage(storage: Arc<Storage>, layout: Layout) -> Result<Self>
pub fn from_storage(storage: Arc<Storage>, layout: Layout) -> Result<Self>
A tensor over existing storage with an explicit layout.
Errors when the layout addresses elements outside the storage.
Sourcepub fn from_vec_f32(data: Vec<f32>, shape: Shape) -> Result<Self>
pub fn from_vec_f32(data: Vec<f32>, shape: Shape) -> Result<Self>
A contiguous CPU tensor holding data with shape shape.
Errors when data.len() does not equal shape.numel().
Sourcepub fn reshape(&self, shape: Shape) -> Result<Self>
pub fn reshape(&self, shape: Shape) -> Result<Self>
A view with the same elements in a new shape.
Succeeds without copying only when the current layout permits it;
errors on element-count mismatch. Never copies — contiguous is the
explicit spelling for that.
Sourcepub fn permute(&self, perm: &[usize]) -> Result<Self>
pub fn permute(&self, perm: &[usize]) -> Result<Self>
A view with dimensions reordered by perm (a permutation of
0..ndim).
Sourcepub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Self>
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Self>
A view of len elements of dimension dim starting at start.
Sourcepub fn contiguous(&self) -> Result<Self>
pub fn contiguous(&self) -> Result<Self>
This tensor’s elements, in logical order, in fresh contiguous storage on the same device. A no-op clone when already contiguous.