Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor<D: Device = Cpu, K: DTypeKind<D> = Float>(/* private fields */);
Expand description

The reference-counted tensor handle. Cheap to clone (bumps an Arc).

Two generic parameters:

  • D: Device — where the data lives (e.g. Cpu); selects the storage type.
  • K: DTypeKind<D> — the kind (Float/Int/Bool), a compile-time marker. The concrete precision within a kind (f32 vs f64, …) is a runtime [DType] carried inside the tensor.

Float is the default kind so Tensor<Cpu> means a float tensor.

Implementations§

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn backward_into(&self, grads: &mut GradStore<D>) -> Result<()>

Accumulate gradients of self w.r.t. all leaf tensors that require grad into an existing GradStore.

Unlike Tensor::backward, this does not create a fresh store; leaf gradients are added onto whatever is already present. Calling it multiple times with different loss tensors (e.g. one per micro-batch) and then stepping the optimizer once implements gradient accumulation.

Source

pub fn backward(&self) -> Result<GradStore<D>>

Compute gradients of self w.r.t. all leaf tensors that require grad.

Source

pub fn sorted_nodes(&self) -> Vec<&Tensor<D, Float>>

Reverse-topological order of nodes reachable from grad-requiring leaves.

Source§

impl<D: Device> Tensor<D, Float>

Convenience accessors on a Float tensor for its autograd state.

Source

pub fn detach(&self) -> Self

Source

pub fn requires_grad(&self) -> bool

Source

pub fn set_requires_grad(&self, mode: bool)

Source

pub fn is_leaf(&self) -> bool

Source

pub fn op(&self) -> Option<&Op<D>>

Source§

impl<D: Device> Tensor<D, Bool>

Source

pub fn and(&self, rhs: &Self) -> Result<Self>

Source

pub fn or(&self, rhs: &Self) -> Result<Self>

Source

pub fn xor(&self, rhs: &Self) -> Result<Self>

Source

pub fn not(&self) -> Result<Self>

Source

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

Source

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

Source

pub fn true_count(&self) -> Result<usize>

Source§

impl<D: Device> Tensor<D, Bool>

Source

pub fn pick<K: PickDTypeKind<D>>( &self, on_true: &Tensor<D, K>, on_false: &Tensor<D, K>, ) -> Result<Tensor<D, K>>

Source

pub fn pick_true<K: PickDTypeKind<D>>( &self, value: K::Scalar, on_false: &Tensor<D, K>, ) -> Result<Tensor<D, K>>

mask ? value : on_false with a scalar true-value.

Source

pub fn pick_false<K: PickDTypeKind<D>>( &self, on_true: &Tensor<D, K>, value: K::Scalar, ) -> Result<Tensor<D, K>>

mask ? on_true : value with a scalar false-value.

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn cast<T: Cast<D, K>>(&self, dtype: T) -> Result<Tensor<D, T::Output>>

Source§

impl<D: Device, K: CastDTypeKind<D>> Tensor<D, K>

Source

pub fn cast_float(&self, to: FloatDType) -> Result<Tensor<D, Float>>

Source

pub fn cast_int(&self, to: IntDType) -> Result<Tensor<D, Int>>

Source

pub fn cast_bool(&self, to: BoolDType) -> Result<Tensor<D, Bool>>

Source§

impl<D: Device> Tensor<D, Bool>

Source

pub fn new(data: impl IntoTensor<D, Bool>, device: &D) -> Result<Self>

Source

pub fn from_vec_bool<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [bool]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn falses<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn trues<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn from_slice<S: Into<Shape>>( data: &[bool], shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn to_vec(&self) -> Result<Vec<bool>>

Source

pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn diag( diag: &[bool], options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source

pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn new(data: impl IntoTensor<D, Float>, device: &D) -> Result<Self>

Source

pub fn from_vec_f64<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [f64]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_vec_f32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [f32]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_slice<S: Into<Shape>>( data: &[f64], shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn to_vec(&self) -> Result<Vec<f64>>

Source

pub fn randn<S: Into<Shape>>( mean: f64, std: f64, shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn rand<S: Into<Shape>>( lo: f64, hi: f64, shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn rand_like(&self, lo: f64, hi: f64) -> Result<Self>

Source

pub fn randn_like(&self, mean: f64, std: f64) -> Result<Self>

Source

pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn diag( diag: &[f64], options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source

pub fn linspace( start: f64, stop: f64, n: usize, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>

Source§

impl<D: Device> Tensor<D, Int>

Source

pub fn new(data: impl IntoTensor<D, Int>, device: &D) -> Result<Self>

Source

pub fn from_vec_i64<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [i64]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_vec_i32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [i32]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_vec_u32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [u32]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_vec_u8<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [u8]>>, shape: S, device: &D, ) -> Result<Self>

Source

pub fn from_slice<S: Into<Shape>>( data: &[i64], shape: S, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source

pub fn to_vec(&self) -> Result<Vec<i64>>

Source

pub fn arange( start: i64, end: i64, step: i64, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source

pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source

pub fn diag( diag: &[i64], options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source

pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source

pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn phantom<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>

Source§

impl<D: Device, K: ConstructDTypeKind<D>> Tensor<D, K>

Source

pub fn zeros<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>

Source

pub fn ones<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>

Source

pub fn full<S: Into<Shape>>( shape: S, value: K::Scalar, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>

Source

pub fn zeros_like(&self) -> Result<Self>

Source

pub fn ones_like(&self) -> Result<Self>

Source§

impl<D: Device, K: BytesDTypeKind<D>> Tensor<D, K>

Source

pub fn from_bytes<'a>( bytes: impl Into<Cow<'a, [u8]>>, shape: impl Into<Shape>, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>

Create a tensor from raw little-endian bytes.

The byte slice length must equal shape.element_count() * dtype.size_in_bytes().

Source

pub fn to_bytes(&self) -> Result<Vec<u8>>

Read raw little-endian bytes in logical (layout) order.

Always returns owned bytes (the internal RwLock prevents zero-copy borrowing at this level; the device trait internally uses Cow for contiguous-optimised paths).

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn to_scalar(&self) -> Result<f64>

Read the single element of a scalar (0-d or 1-element) tensor.

Source§

impl<D: Device> Tensor<D, Int>

Source

pub fn to_scalar(&self) -> Result<i64>

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn allclose(&self, other: &Self, rtol: f64, atol: f64) -> Result<bool>

Source§

impl<D: Device> Tensor<D, Int>

Source

pub fn allclose(&self, other: &Self) -> Result<bool>

Source§

impl<D: Device> Tensor<D, Bool>

Source

pub fn allclose(&self, other: &Self) -> Result<bool>

Source§

impl<D: Device> Tensor<D, Bool>

Source

pub fn false_count(&self) -> Result<usize>

Source§

impl<D: Device, K: IndexingDTypeKind<D>> Tensor<D, K>

Source

pub fn index_select<Dm: Dim>( &self, indices: &Tensor<D, Int>, dim: Dm, ) -> Result<Self>

Select slices along dim at the given 1-D indices.

Source

pub fn gather<Dm: Dim>(&self, indices: &Tensor<D, Int>, dim: Dm) -> Result<Self>

Gather along dim using an index tensor of the same rank.

Source

pub fn index_add<Dm: Dim>( &self, indices: &Tensor<D, Int>, src: &Tensor<D, K>, dim: Dm, ) -> Result<Self>

out = self; out[.., idx[i], ..] += src[.., i, ..].

Source

pub fn scatter_add<Dm: Dim>( &self, indices: &Tensor<D, Int>, src: &Tensor<D, K>, dim: Dm, ) -> Result<Self>

out = self; out[.., idx[i,j,k], k] += src[i,j,k].

Source§

impl<D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn indexes(&self, indexers: &[Indexer<D>]) -> Result<Self>

Apply a sequence of Indexers, one per dimension, in order.

Source§

impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn get(&self, i: usize) -> Result<Self>

Returns the sub-tensor fixing the index at i on the first dimension.

let t = Tensor::<Cpu>::new(&[[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).unwrap();
let row1 = t.get(1).unwrap(); // [3.0, 4.0]
Source§

impl<D: Device, K: MatmulDTypeKind<D>> Tensor<D, K>

Source

pub fn matmul(&self, rhs: &Self) -> Result<Self>

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn softmax<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Softmax over dim. Records Op::Softmax.

Source

pub fn rms_norm(&self, weight: &Self, eps: f64) -> Result<Self>

RMSNorm over the last dim: x / sqrt(mean(x^2)+eps) * weight. Records Op::RmsNorm.

Source§

impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn add(&self, rhs: &Self) -> Result<Self>

Source

pub fn add_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn add_(&self, rhs: &Self) -> Result<()>

Source

pub fn add_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_add(&self, rhs: &Self) -> Result<Self>

Source

pub fn sub(&self, rhs: &Self) -> Result<Self>

Source

pub fn sub_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn sub_(&self, rhs: &Self) -> Result<()>

Source

pub fn sub_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_sub(&self, rhs: &Self) -> Result<Self>

Source

pub fn mul(&self, rhs: &Self) -> Result<Self>

Source

pub fn mul_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn mul_(&self, rhs: &Self) -> Result<()>

Source

pub fn mul_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_mul(&self, rhs: &Self) -> Result<Self>

Source

pub fn div(&self, rhs: &Self) -> Result<Self>

Source

pub fn div_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn div_(&self, rhs: &Self) -> Result<()>

Source

pub fn div_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_div(&self, rhs: &Self) -> Result<Self>

Source

pub fn maximum(&self, rhs: &Self) -> Result<Self>

Source

pub fn maximum_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn maximum_(&self, rhs: &Self) -> Result<()>

Source

pub fn maximum_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_maximum(&self, rhs: &Self) -> Result<Self>

Source

pub fn minimum(&self, rhs: &Self) -> Result<Self>

Source

pub fn minimum_scalar(&self, rhs: K::Scalar) -> Result<Self>

Source

pub fn minimum_(&self, rhs: &Self) -> Result<()>

Source

pub fn minimum_scalar_(&self, rhs: K::Scalar) -> Result<()>

Source

pub fn broadcast_minimum(&self, rhs: &Self) -> Result<Self>

Source§

impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn eq(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn eq_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_eq(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn ne(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn ne_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_ne(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn lt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn lt_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_lt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn le(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn le_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_le(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn gt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn gt_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_gt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn ge(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source

pub fn ge_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>

Source

pub fn broadcast_ge(&self, rhs: &Self) -> Result<Tensor<D, Bool>>

Source§

impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn neg(&self) -> Result<Self>

Source

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

Source

pub fn abs(&self) -> Result<Self>

Source

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

Source

pub fn sign(&self) -> Result<Self>

Source

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

Source

pub fn affine(&self, mul: K::Scalar, add: K::Scalar) -> Result<Self>

Source

pub fn affine_(&self, mul: K::Scalar, add: K::Scalar) -> Result<()>

Source

pub fn pow(&self, exp: K::Scalar) -> Result<Self>

Source

pub fn pow_(&self, exp: K::Scalar) -> Result<()>

Source

pub fn clamp( &self, min: Option<K::Scalar>, max: Option<K::Scalar>, ) -> Result<Self>

Source

pub fn clamp_( &self, min: Option<K::Scalar>, max: Option<K::Scalar>, ) -> Result<()>

Source

pub fn sub_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>

scalar - self (scalar on the left).

Source

pub fn div_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>

scalar / self (scalar on the left).

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn exp(&self) -> Result<Self>

Source

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

Source

pub fn ln(&self) -> Result<Self>

Source

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

Source

pub fn sin(&self) -> Result<Self>

Source

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

Source

pub fn cos(&self) -> Result<Self>

Source

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

Source

pub fn tanh(&self) -> Result<Self>

Source

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

Source

pub fn sqr(&self) -> Result<Self>

Source

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

Source

pub fn sqrt(&self) -> Result<Self>

Source

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

Source

pub fn recip(&self) -> Result<Self>

Source

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

Source

pub fn gelu(&self) -> Result<Self>

Source

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

Source

pub fn gelu_erf(&self) -> Result<Self>

Source

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

Source

pub fn erf(&self) -> Result<Self>

Source

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

Source

pub fn relu(&self) -> Result<Self>

Source

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

Source

pub fn silu(&self) -> Result<Self>

Source

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

Source

pub fn sigmoid(&self) -> Result<Self>

Source

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

Source

pub fn floor(&self) -> Result<Self>

Source

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

Source

pub fn ceil(&self) -> Result<Self>

Source

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

Source

pub fn round(&self) -> Result<Self>

Source

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

Source

pub fn leaky_relu(&self, negative_slope: f64) -> Result<Self>

Source

pub fn leaky_relu_(&self, negative_slope: f64) -> Result<()>

Source§

impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>

Source

pub fn argmin<Dm: Dim>(&self, dim: Dm) -> Result<Tensor<D, Int>>

Source

pub fn argmin_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Tensor<D, Int>>

Source

pub fn argmax<Dm: Dim>(&self, dim: Dm) -> Result<Tensor<D, Int>>

Source

pub fn argmax_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Tensor<D, Int>>

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn mean<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn mean_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn mean_all(&self) -> Result<Self>

Source

pub fn var<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn var_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn var_all(&self) -> Result<Self>

Source

pub fn var_unbiased<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn var_unbiased_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn var_unbiased_all(&self) -> Result<Self>

Source§

impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>

Source

pub fn sum<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn sum_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn sum_all(&self) -> Result<Self>

Source

pub fn max<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn max_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn max_all(&self) -> Result<Self>

Source

pub fn min<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn min_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn min_all(&self) -> Result<Self>

Source

pub fn prod<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn prod_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn prod_all(&self) -> Result<Self>

Source

pub fn sum_dims<Ds: Dims>(&self, dims: Ds, keepdim: bool) -> Result<Self>

Source§

impl<D: Device> Tensor<D, Float>

Source

pub fn std<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Standard deviation along dim.

Source

pub fn std_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn std_all(&self) -> Result<Self>

Source

pub fn logsumexp<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Log-sum-exp along dim, numerically stable.

Source

pub fn logsumexp_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn logsumexp_all(&self) -> Result<Self>

Source§

impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>

Source

pub fn copy(&self) -> Result<Self>

Deep-copy the tensor data to independent storage (records Op::Copy for Float).

Source

pub fn copy_(&mut self, src: &Self) -> Result<()>

Copy the data from src into self in-place, preserving [TensorId].

Source

pub fn reshape<S: Into<Shape>>(&self, shape: S) -> Result<Self>

Source

pub fn transpose<D1: Dim, D2: Dim>(&self, dim1: D1, dim2: D2) -> Result<Self>

Source

pub fn transpose_last(&self) -> Result<Self>

Source

pub fn permute<Ds: Dims>(&self, dims: Ds) -> Result<Self>

Source

pub fn narrow<Dm: Dim>(&self, dim: Dm, start: usize, len: usize) -> Result<Self>

Source

pub fn slice<Dm: Dim>( &self, dim: Dm, start: usize, end: usize, step: usize, ) -> Result<Self>

Slice along dim with start, end, and step.

Returns a view (no copy) when possible. For step == 1 this is equivalent to narrow; for step > 1 the backward pass is not yet supported on autograd tensors.

Source

pub fn broadcast_as<S: Into<Shape>>(&self, shape: S) -> Result<Self>

Source

pub fn squeeze<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn unsqueeze<Dm: Dim>(&self, dim: Dm) -> Result<Self>

Source

pub fn contiguous(&self) -> Result<Self>

Materialize a contiguous copy (records Op::Copy for Float).

Source

pub fn cat<A: AsRef<Self>, Dm: Dim>(arrs: &[A], dim: Dm) -> Result<Self>

Concatenate tensors along dim (materializes new storage).

Source

pub fn flatten<D1: Dim, D2: Dim>( &self, start_dim: D1, end_dim: D2, ) -> Result<Self>

Flatten dims start_dim..=end_dim into one.

Source

pub fn flatten_from<Dm: Dim>(&self, start_dim: Dm) -> Result<Self>

Source

pub fn flatten_to<Dm: Dim>(&self, end_dim: Dm) -> Result<Self>

Source

pub fn flatten_all(&self) -> Result<Self>

Source

pub fn stack<A: AsRef<Self>, Dm: Dim>(args: &[A], dim: Dm) -> Result<Self>

Stack tensors along a new axis at dim (unsqueeze each + cat).

Source

pub fn split<Dm: Dim>(&self, dim: Dm) -> Result<Vec<Self>>

Split into individual slices along dim (one per index).

Source

pub fn chunk<Dm: Dim>(&self, chunks: usize, dim: Dm) -> Result<Vec<Self>>

Split into chunks roughly equal pieces along dim.

Source

pub fn repeat_dim<Dm: Dim>(&self, dim: Dm, times: usize) -> Result<Self>

Tile self by times along dim.

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn to_device<D2: Device>(&self, device: &D2) -> Result<Tensor<D2, K>>
where K: TransferDTypeKind<D, D2>,

Move the tensor to another device.

  • Same device: returns this handle unchanged (shared Arc, O(1)) — generic code can call this unconditionally.
  • Cross device: deep-copies the data (to_bytes/from_bytes), dtype unchanged, result contiguous. For Float tensors the autograd graph is severed (the result is a leaf) but requires_grad is preserved.
  • Meta tensors (no storage) error with Error::MetaTensor.
Source

pub fn cpu(&self) -> Result<Tensor<Cpu, K>>
where K: TransferDTypeKind<D, Cpu>,

Convenience for to_device(&Cpu).

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

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

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn dims1(&self) -> Result<usize>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn dims2(&self) -> Result<(usize, usize)>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn dims3(&self) -> Result<(usize, usize, usize)>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn dims4(&self) -> Result<(usize, usize, usize, usize)>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn dims5(&self) -> Result<(usize, usize, usize, usize, usize)>

Source§

impl<D: Device, K: DTypeKind<D>> Tensor<D, K>

Source

pub fn id(&self) -> TensorId

Source

pub fn dtype(&self) -> K::DType

Source

pub fn device(&self) -> &D

Source

pub fn layout(&self) -> &Layout

Source

pub fn shape(&self) -> &Shape

Source

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

Source

pub fn rank(&self) -> usize

Source

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

Source

pub fn dim<Di: Dim>(&self, dim: Di) -> Result<usize>

Source

pub fn element_count(&self) -> usize

Source

pub fn is_contiguous(&self) -> bool

Source

pub fn is_meta(&self) -> bool

A meta tensor carries shape but no storage.

Source

pub fn storage(&self) -> Result<&Arc<RwLock<K::Storage>>>

Access the underlying storage, erroring on a meta tensor.

Trait Implementations§

Source§

impl<D: Device> Add<&Tensor<D, Int>> for i64

Source§

type Output = Tensor<D, Int>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Tensor<D, Int>) -> Self::Output

Performs the + operation. Read more
Source§

impl<D: Device> Add<&Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Tensor<D, Float>) -> Self::Output

Performs the + operation. Read more
Source§

impl<D, K, R> Add<R> for &Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: R) -> Self::Output

Performs the + operation. Read more
Source§

impl<D, K, R> Add<R> for Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: R) -> Self::Output

Performs the + operation. Read more
Source§

impl<D: Device> Add<Tensor<D, Int>> for i64

Source§

type Output = Tensor<D, Int>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Tensor<D, Int>) -> Self::Output

Performs the + operation. Read more
Source§

impl<D: Device> Add<Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Tensor<D, Float>) -> Self::Output

Performs the + operation. Read more
Source§

impl<D: Device> AddAssign<&Tensor<D, Int>> for Tensor<D, Int>

Source§

fn add_assign(&mut self, rhs: &Tensor<D, Int>)

Performs the += operation. Read more
Source§

impl<D: Device> AddAssign<&Tensor<D>> for Tensor<D, Float>

Source§

fn add_assign(&mut self, rhs: &Tensor<D, Float>)

Performs the += operation. Read more
Source§

impl<D: Device, K: DTypeKind<D>> AsRef<Tensor<D, K>> for Tensor<D, K>

Source§

fn as_ref(&self) -> &Tensor<D, K>

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<D: Device, K: DTypeKind<D>> Clone for Tensor<D, K>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<D: Device> Display for Tensor<D, Float>

Source§

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

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

impl<D: Device> Display for Tensor<D, Int>

Source§

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

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

impl<D: Device> Display for Tensor<D, Bool>

Source§

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

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

impl<D: Device> Div<&Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Tensor<D, Float>) -> Self::Output

Performs the / operation. Read more
Source§

impl<D, K, R> Div<R> for &Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: R) -> Self::Output

Performs the / operation. Read more
Source§

impl<D, K, R> Div<R> for Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: R) -> Self::Output

Performs the / operation. Read more
Source§

impl<D: Device> Div<Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Tensor<D, Float>) -> Self::Output

Performs the / operation. Read more
Source§

impl<D: Device> DivAssign<&Tensor<D, Int>> for Tensor<D, Int>

Source§

fn div_assign(&mut self, rhs: &Tensor<D, Int>)

Performs the /= operation. Read more
Source§

impl<D: Device> DivAssign<&Tensor<D>> for Tensor<D, Float>

Source§

fn div_assign(&mut self, rhs: &Tensor<D, Float>)

Performs the /= operation. Read more
Source§

impl<D: Device, K: DTypeKind<D>> Eq for Tensor<D, K>

Source§

impl<D: Device> From<&Tensor<D, Bool>> for Indexer<D>

Source§

fn from(value: &Tensor<D, Bool>) -> Self

Converts to this type from the input type.
Source§

impl<D: Device> From<Tensor<D, Bool>> for DynTensor<D>

Source§

fn from(t: Tensor<D, Bool>) -> Self

Converts to this type from the input type.
Source§

impl<D: Device> From<Tensor<D, Bool>> for Indexer<D>

Source§

fn from(value: Tensor<D, Bool>) -> Self

Converts to this type from the input type.
Source§

impl<D: Device> From<Tensor<D, Int>> for DynTensor<D>

Source§

fn from(t: Tensor<D, Int>) -> Self

Converts to this type from the input type.
Source§

impl<D: Device> From<Tensor<D>> for DynTensor<D>

Source§

fn from(t: Tensor<D, Float>) -> Self

Converts to this type from the input type.
Source§

impl<D: Device, K: DTypeKind<D>> Hash for Tensor<D, K>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<D: Device> Index<&Tensor<D>> for GradStore<D>

Source§

type Output = Tensor<D>

The returned type after indexing.
Source§

fn index(&self, index: &Tensor<D, Float>) -> &Self::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl<I1, I2, D, K> IndexOp<(I1, I2), D, K> for Tensor<D, K>
where I1: Into<Indexer<D>>, I2: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, (I1, I2): (I1, I2)) -> Result<Tensor<D, K>>

Source§

impl<I1, I2, I3, D, K> IndexOp<(I1, I2, I3), D, K> for Tensor<D, K>
where I1: Into<Indexer<D>>, I2: Into<Indexer<D>>, I3: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, (I1, I2, I3): (I1, I2, I3)) -> Result<Tensor<D, K>>

Source§

impl<I1, I2, I3, I4, D, K> IndexOp<(I1, I2, I3, I4), D, K> for Tensor<D, K>
where I1: Into<Indexer<D>>, I2: Into<Indexer<D>>, I3: Into<Indexer<D>>, I4: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, (I1, I2, I3, I4): (I1, I2, I3, I4)) -> Result<Tensor<D, K>>

Source§

impl<I1, I2, I3, I4, I5, D, K> IndexOp<(I1, I2, I3, I4, I5), D, K> for Tensor<D, K>
where I1: Into<Indexer<D>>, I2: Into<Indexer<D>>, I3: Into<Indexer<D>>, I4: Into<Indexer<D>>, I5: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, (I1, I2, I3, I4, I5): (I1, I2, I3, I4, I5)) -> Result<Tensor<D, K>>

Source§

impl<I1, D, K> IndexOp<(I1,), D, K> for Tensor<D, K>
where I1: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, (I1): (I1,)) -> Result<Tensor<D, K>>

Source§

impl<I, D, K> IndexOp<I, D, K> for Tensor<D, K>
where I: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, index: I) -> Result<Tensor<D, K>>

Source§

impl<I, D, K> IndexOp<Vec<I>, D, K> for Tensor<D, K>
where I: Into<Indexer<D>>, D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>,

Source§

fn i(&self, index: Vec<I>) -> Result<Tensor<D, K>>

Source§

impl<D: Device> Mul<&Tensor<D, Int>> for i64

Source§

type Output = Tensor<D, Int>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Tensor<D, Int>) -> Self::Output

Performs the * operation. Read more
Source§

impl<D: Device> Mul<&Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Tensor<D, Float>) -> Self::Output

Performs the * operation. Read more
Source§

impl<D, K, R> Mul<R> for &Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: R) -> Self::Output

Performs the * operation. Read more
Source§

impl<D, K, R> Mul<R> for Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: R) -> Self::Output

Performs the * operation. Read more
Source§

impl<D: Device> Mul<Tensor<D, Int>> for i64

Source§

type Output = Tensor<D, Int>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Tensor<D, Int>) -> Self::Output

Performs the * operation. Read more
Source§

impl<D: Device> Mul<Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Tensor<D, Float>) -> Self::Output

Performs the * operation. Read more
Source§

impl<D: Device> MulAssign<&Tensor<D, Int>> for Tensor<D, Int>

Source§

fn mul_assign(&mut self, rhs: &Tensor<D, Int>)

Performs the *= operation. Read more
Source§

impl<D: Device> MulAssign<&Tensor<D>> for Tensor<D, Float>

Source§

fn mul_assign(&mut self, rhs: &Tensor<D, Float>)

Performs the *= operation. Read more
Source§

impl<D: Device> Neg for &Tensor<D, Float>

Source§

type Output = Tensor<D>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<D: Device> Neg for Tensor<D, Float>

Source§

type Output = Tensor<D>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<D: Device> Neg for &Tensor<D, Int>

Source§

type Output = Tensor<D, Int>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<D: Device> Neg for Tensor<D, Int>

Source§

type Output = Tensor<D, Int>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<D: Device, K: DTypeKind<D>> PartialEq for Tensor<D, K>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<D: Device> Sub<&Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Tensor<D, Float>) -> Self::Output

Performs the - operation. Read more
Source§

impl<D, K, R> Sub<R> for &Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: R) -> Self::Output

Performs the - operation. Read more
Source§

impl<D, K, R> Sub<R> for Tensor<D, K>
where D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>, R: Into<TensorOrScalar<D, K>>,

Source§

type Output = Tensor<D, K>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: R) -> Self::Output

Performs the - operation. Read more
Source§

impl<D: Device> Sub<Tensor<D>> for f64

Source§

type Output = Tensor<D>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Tensor<D, Float>) -> Self::Output

Performs the - operation. Read more
Source§

impl<D: Device> SubAssign<&Tensor<D, Int>> for Tensor<D, Int>

Source§

fn sub_assign(&mut self, rhs: &Tensor<D, Int>)

Performs the -= operation. Read more
Source§

impl<D: Device> SubAssign<&Tensor<D>> for Tensor<D, Float>

Source§

fn sub_assign(&mut self, rhs: &Tensor<D, Float>)

Performs the -= operation. Read more

Auto Trait Implementations§

§

impl<D, K> Freeze for Tensor<D, K>
where Arc<TensorImpl<D, K>>: Freeze,

§

impl<D, K> RefUnwindSafe for Tensor<D, K>

§

impl<D, K> Send for Tensor<D, K>
where Arc<TensorImpl<D, K>>: Send,

§

impl<D, K> Sync for Tensor<D, K>
where Arc<TensorImpl<D, K>>: Sync,

§

impl<D, K> Unpin for Tensor<D, K>
where Arc<TensorImpl<D, K>>: Unpin,

§

impl<D, K> UnsafeUnpin for Tensor<D, K>
where Arc<TensorImpl<D, K>>: UnsafeUnpin,

§

impl<D, K> UnwindSafe for Tensor<D, K>
where Arc<TensorImpl<D, K>>: UnwindSafe,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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, <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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V