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 (f32vsf64, …) 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>
impl<D: Device> Tensor<D, Float>
Sourcepub fn backward_into(&self, grads: &mut GradStore<D>) -> Result<()>
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.
Sourcepub fn backward(&self) -> Result<GradStore<D>>
pub fn backward(&self) -> Result<GradStore<D>>
Compute gradients of self w.r.t. all leaf tensors that require grad.
Sourcepub fn sorted_nodes(&self) -> Vec<&Tensor<D, Float>>
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.
impl<D: Device> Tensor<D, Float>
Convenience accessors on a Float tensor for its autograd state.
Source§impl<D: Device> Tensor<D, Bool>
impl<D: Device> Tensor<D, Bool>
pub fn and(&self, rhs: &Self) -> Result<Self>
pub fn or(&self, rhs: &Self) -> Result<Self>
pub fn xor(&self, rhs: &Self) -> Result<Self>
pub fn not(&self) -> Result<Self>
pub fn all_all(&self) -> Result<bool>
pub fn any_all(&self) -> Result<bool>
pub fn true_count(&self) -> Result<usize>
Source§impl<D: Device> Tensor<D, Bool>
impl<D: Device> Tensor<D, Bool>
Source§impl<D: Device> Tensor<D, Bool>
impl<D: Device> Tensor<D, Bool>
pub fn new(data: impl IntoTensor<D, Bool>, device: &D) -> Result<Self>
pub fn from_vec_bool<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [bool]>>, shape: S, device: &D, ) -> Result<Self>
pub fn falses<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn trues<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn from_slice<S: Into<Shape>>( data: &[bool], shape: S, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn to_vec(&self) -> Result<Vec<bool>>
pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn diag( diag: &[bool], options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Bool>>, ) -> Result<Self>
Source§impl<D: Device> Tensor<D, Float>
impl<D: Device> Tensor<D, Float>
pub fn new(data: impl IntoTensor<D, Float>, device: &D) -> Result<Self>
pub fn from_vec_f64<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [f64]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_vec_f32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [f32]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_slice<S: Into<Shape>>( data: &[f64], shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn to_vec(&self) -> Result<Vec<f64>>
pub fn randn<S: Into<Shape>>( mean: f64, std: f64, shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn rand<S: Into<Shape>>( lo: f64, hi: f64, shape: S, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn rand_like(&self, lo: f64, hi: f64) -> Result<Self>
pub fn randn_like(&self, mean: f64, std: f64) -> Result<Self>
pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn diag( diag: &[f64], options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
pub fn linspace( start: f64, stop: f64, n: usize, options: impl Into<TensorCreationOptions<D, Float>>, ) -> Result<Self>
Source§impl<D: Device> Tensor<D, Int>
impl<D: Device> Tensor<D, Int>
pub fn new(data: impl IntoTensor<D, Int>, device: &D) -> Result<Self>
pub fn from_vec_i64<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [i64]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_vec_i32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [i32]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_vec_u32<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [u32]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_vec_u8<'a, S: Into<Shape>>( data: impl Into<Cow<'a, [u8]>>, shape: S, device: &D, ) -> Result<Self>
pub fn from_slice<S: Into<Shape>>( data: &[i64], shape: S, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
pub fn to_vec(&self) -> Result<Vec<i64>>
pub fn arange( start: i64, end: i64, step: i64, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
pub fn eye( n: usize, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
pub fn diag( diag: &[i64], options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
pub fn tril( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
pub fn triu( n: usize, diagonal: bool, options: impl Into<TensorCreationOptions<D, Int>>, ) -> Result<Self>
Source§impl<D: Device, K: ConstructDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: ConstructDTypeKind<D>> Tensor<D, K>
pub fn zeros<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>
pub fn ones<S: Into<Shape>>( shape: S, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>
pub fn full<S: Into<Shape>>( shape: S, value: K::Scalar, options: impl Into<TensorCreationOptions<D, K>>, ) -> Result<Self>
pub fn zeros_like(&self) -> Result<Self>
pub fn ones_like(&self) -> Result<Self>
Source§impl<D: Device, K: BytesDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: BytesDTypeKind<D>> Tensor<D, K>
Sourcepub fn from_bytes<'a>(
bytes: impl Into<Cow<'a, [u8]>>,
shape: impl Into<Shape>,
options: impl Into<TensorCreationOptions<D, K>>,
) -> Result<Self>
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§impl<D: Device, K: IndexingDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: IndexingDTypeKind<D>> Tensor<D, K>
Sourcepub fn index_select<Dm: Dim>(
&self,
indices: &Tensor<D, Int>,
dim: Dm,
) -> Result<Self>
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.
Sourcepub fn gather<Dm: Dim>(&self, indices: &Tensor<D, Int>, dim: Dm) -> Result<Self>
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§impl<D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: IndexingDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
Sourcepub fn indexes(&self, indexers: &[Indexer<D>]) -> Result<Self>
pub fn indexes(&self, indexers: &[Indexer<D>]) -> Result<Self>
Apply a sequence of Indexers, one per dimension, in order.
Indexer::Select/Indexer::SelectD: narrow + squeeze (removes dim).Indexer::Slice: narrow (step=1) or slice (step>1) — keeps dim.Indexer::Boolean: boolean mask →index_select— keeps dim.
Source§impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>
Source§impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
pub fn add(&self, rhs: &Self) -> Result<Self>
pub fn add_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn add_(&self, rhs: &Self) -> Result<()>
pub fn add_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_add(&self, rhs: &Self) -> Result<Self>
pub fn sub(&self, rhs: &Self) -> Result<Self>
pub fn sub_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn sub_(&self, rhs: &Self) -> Result<()>
pub fn sub_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_sub(&self, rhs: &Self) -> Result<Self>
pub fn mul(&self, rhs: &Self) -> Result<Self>
pub fn mul_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn mul_(&self, rhs: &Self) -> Result<()>
pub fn mul_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_mul(&self, rhs: &Self) -> Result<Self>
pub fn div(&self, rhs: &Self) -> Result<Self>
pub fn div_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn div_(&self, rhs: &Self) -> Result<()>
pub fn div_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_div(&self, rhs: &Self) -> Result<Self>
pub fn maximum(&self, rhs: &Self) -> Result<Self>
pub fn maximum_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn maximum_(&self, rhs: &Self) -> Result<()>
pub fn maximum_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_maximum(&self, rhs: &Self) -> Result<Self>
pub fn minimum(&self, rhs: &Self) -> Result<Self>
pub fn minimum_scalar(&self, rhs: K::Scalar) -> Result<Self>
pub fn minimum_(&self, rhs: &Self) -> Result<()>
pub fn minimum_scalar_(&self, rhs: K::Scalar) -> Result<()>
pub fn broadcast_minimum(&self, rhs: &Self) -> Result<Self>
Source§impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
pub fn eq(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn eq_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_eq(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn ne(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn ne_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_ne(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn lt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn lt_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_lt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn le(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn le_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_le(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn gt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn gt_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_gt(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn ge(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
pub fn ge_scalar(&self, rhs: K::Scalar) -> Result<Tensor<D, Bool>>
pub fn broadcast_ge(&self, rhs: &Self) -> Result<Tensor<D, Bool>>
Source§impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: NumericDTypeKind<D> + ShapeDTypeKind<D>> Tensor<D, K>
pub fn neg(&self) -> Result<Self>
pub fn neg_(&self) -> Result<()>
pub fn abs(&self) -> Result<Self>
pub fn abs_(&self) -> Result<()>
pub fn sign(&self) -> Result<Self>
pub fn sign_(&self) -> Result<()>
pub fn affine(&self, mul: K::Scalar, add: K::Scalar) -> Result<Self>
pub fn affine_(&self, mul: K::Scalar, add: K::Scalar) -> Result<()>
pub fn pow(&self, exp: K::Scalar) -> Result<Self>
pub fn pow_(&self, exp: K::Scalar) -> Result<()>
pub fn clamp( &self, min: Option<K::Scalar>, max: Option<K::Scalar>, ) -> Result<Self>
pub fn clamp_( &self, min: Option<K::Scalar>, max: Option<K::Scalar>, ) -> Result<()>
Sourcepub fn sub_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>
pub fn sub_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>
scalar - self (scalar on the left).
Sourcepub fn div_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>
pub fn div_scalar_lhs(&self, lhs: K::Scalar) -> Result<Self>
scalar / self (scalar on the left).
Source§impl<D: Device> Tensor<D, Float>
impl<D: Device> Tensor<D, Float>
pub fn exp(&self) -> Result<Self>
pub fn exp_(&self) -> Result<()>
pub fn ln(&self) -> Result<Self>
pub fn ln_(&self) -> Result<()>
pub fn sin(&self) -> Result<Self>
pub fn sin_(&self) -> Result<()>
pub fn cos(&self) -> Result<Self>
pub fn cos_(&self) -> Result<()>
pub fn tanh(&self) -> Result<Self>
pub fn tanh_(&self) -> Result<()>
pub fn sqr(&self) -> Result<Self>
pub fn sqr_(&self) -> Result<()>
pub fn sqrt(&self) -> Result<Self>
pub fn sqrt_(&self) -> Result<()>
pub fn recip(&self) -> Result<Self>
pub fn recip_(&self) -> Result<()>
pub fn gelu(&self) -> Result<Self>
pub fn gelu_(&self) -> Result<()>
pub fn gelu_erf(&self) -> Result<Self>
pub fn gelu_erf_(&self) -> Result<()>
pub fn erf(&self) -> Result<Self>
pub fn erf_(&self) -> Result<()>
pub fn relu(&self) -> Result<Self>
pub fn relu_(&self) -> Result<()>
pub fn silu(&self) -> Result<Self>
pub fn silu_(&self) -> Result<()>
pub fn sigmoid(&self) -> Result<Self>
pub fn sigmoid_(&self) -> Result<()>
pub fn floor(&self) -> Result<Self>
pub fn floor_(&self) -> Result<()>
pub fn ceil(&self) -> Result<Self>
pub fn ceil_(&self) -> Result<()>
pub fn round(&self) -> Result<Self>
pub fn round_(&self) -> Result<()>
pub fn leaky_relu(&self, negative_slope: f64) -> Result<Self>
pub fn leaky_relu_(&self, negative_slope: f64) -> Result<()>
Source§impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>
Source§impl<D: Device> Tensor<D, Float>
impl<D: Device> Tensor<D, Float>
pub fn mean<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn mean_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn mean_all(&self) -> Result<Self>
pub fn var<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn var_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn var_all(&self) -> Result<Self>
pub fn var_unbiased<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn var_unbiased_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn var_unbiased_all(&self) -> Result<Self>
Source§impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: ReduceDTypeKind<D>> Tensor<D, K>
pub fn sum<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn sum_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn sum_all(&self) -> Result<Self>
pub fn max<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn max_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn max_all(&self) -> Result<Self>
pub fn min<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn min_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn min_all(&self) -> Result<Self>
pub fn prod<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn prod_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn prod_all(&self) -> Result<Self>
pub fn sum_dims<Ds: Dims>(&self, dims: Ds, keepdim: bool) -> Result<Self>
Source§impl<D: Device> Tensor<D, Float>
impl<D: Device> Tensor<D, Float>
pub fn std_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn std_all(&self) -> Result<Self>
Sourcepub fn logsumexp<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn logsumexp<Dm: Dim>(&self, dim: Dm) -> Result<Self>
Log-sum-exp along dim, numerically stable.
pub fn logsumexp_keepdim<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn logsumexp_all(&self) -> Result<Self>
Source§impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>
impl<D: Device, K: ShapeDTypeKind<D>> Tensor<D, K>
Sourcepub fn copy(&self) -> Result<Self>
pub fn copy(&self) -> Result<Self>
Deep-copy the tensor data to independent storage (records Op::Copy for Float).
Sourcepub fn copy_(&mut self, src: &Self) -> Result<()>
pub fn copy_(&mut self, src: &Self) -> Result<()>
Copy the data from src into self in-place, preserving [TensorId].
pub fn reshape<S: Into<Shape>>(&self, shape: S) -> Result<Self>
pub fn transpose<D1: Dim, D2: Dim>(&self, dim1: D1, dim2: D2) -> Result<Self>
pub fn transpose_last(&self) -> Result<Self>
pub fn permute<Ds: Dims>(&self, dims: Ds) -> Result<Self>
pub fn narrow<Dm: Dim>(&self, dim: Dm, start: usize, len: usize) -> Result<Self>
Sourcepub fn slice<Dm: Dim>(
&self,
dim: Dm,
start: usize,
end: usize,
step: usize,
) -> Result<Self>
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.
pub fn broadcast_as<S: Into<Shape>>(&self, shape: S) -> Result<Self>
pub fn squeeze<Dm: Dim>(&self, dim: Dm) -> Result<Self>
pub fn unsqueeze<Dm: Dim>(&self, dim: Dm) -> Result<Self>
Sourcepub fn contiguous(&self) -> Result<Self>
pub fn contiguous(&self) -> Result<Self>
Materialize a contiguous copy (records Op::Copy for Float).
Sourcepub fn cat<A: AsRef<Self>, Dm: Dim>(arrs: &[A], dim: Dm) -> Result<Self>
pub fn cat<A: AsRef<Self>, Dm: Dim>(arrs: &[A], dim: Dm) -> Result<Self>
Concatenate tensors along dim (materializes new storage).
Sourcepub fn flatten<D1: Dim, D2: Dim>(
&self,
start_dim: D1,
end_dim: D2,
) -> Result<Self>
pub fn flatten<D1: Dim, D2: Dim>( &self, start_dim: D1, end_dim: D2, ) -> Result<Self>
Flatten dims start_dim..=end_dim into one.
pub fn flatten_from<Dm: Dim>(&self, start_dim: Dm) -> Result<Self>
pub fn flatten_to<Dm: Dim>(&self, end_dim: Dm) -> Result<Self>
pub fn flatten_all(&self) -> Result<Self>
Sourcepub fn stack<A: AsRef<Self>, Dm: Dim>(args: &[A], dim: Dm) -> Result<Self>
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).
Sourcepub fn split<Dm: Dim>(&self, dim: Dm) -> Result<Vec<Self>>
pub fn split<Dm: Dim>(&self, dim: Dm) -> Result<Vec<Self>>
Split into individual slices along dim (one per index).
Sourcepub fn chunk<Dm: Dim>(&self, chunks: usize, dim: Dm) -> Result<Vec<Self>>
pub fn chunk<Dm: Dim>(&self, chunks: usize, dim: Dm) -> Result<Vec<Self>>
Split into chunks roughly equal pieces along dim.
Sourcepub fn repeat_dim<Dm: Dim>(&self, dim: Dm, times: usize) -> Result<Self>
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>
impl<D: Device, K: DTypeKind<D>> Tensor<D, K>
Sourcepub fn to_device<D2: Device>(&self, device: &D2) -> Result<Tensor<D2, K>>where
K: TransferDTypeKind<D, D2>,
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. ForFloattensors the autograd graph is severed (the result is a leaf) butrequires_gradis preserved. - Meta tensors (no storage) error with
Error::MetaTensor.
Source§impl<D: Device, K: DTypeKind<D>> Tensor<D, K>
impl<D: Device, K: DTypeKind<D>> Tensor<D, K>
pub fn id(&self) -> TensorId
pub fn dtype(&self) -> K::DType
pub fn device(&self) -> &D
pub fn layout(&self) -> &Layout
pub fn shape(&self) -> &Shape
pub fn stride(&self) -> &[usize]
pub fn rank(&self) -> usize
pub fn dims(&self) -> &[usize]
pub fn dim<Di: Dim>(&self, dim: Di) -> Result<usize>
pub fn element_count(&self) -> usize
pub fn is_contiguous(&self) -> bool
Trait Implementations§
impl<D: Device, K: DTypeKind<D>> Eq for Tensor<D, K>
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>,
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§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>,
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§impl<I1, I2, I3, I4, D, K> IndexOp<(I1, I2, I3, I4), D, K> for Tensor<D, K>
impl<I1, I2, I3, I4, D, K> IndexOp<(I1, I2, I3, I4), D, K> for Tensor<D, K>
Source§impl<I1, I2, I3, I4, I5, D, K> IndexOp<(I1, I2, I3, I4, I5), D, K> for Tensor<D, K>
impl<I1, I2, I3, I4, I5, D, K> IndexOp<(I1, I2, I3, I4, I5), D, K> for Tensor<D, K>
Auto Trait Implementations§
impl<D, K> Freeze for Tensor<D, K>
impl<D, K> RefUnwindSafe for Tensor<D, K>
impl<D, K> Send for Tensor<D, K>
impl<D, K> Sync for Tensor<D, K>
impl<D, K> Unpin for Tensor<D, K>
impl<D, K> UnsafeUnpin for Tensor<D, K>
impl<D, K> UnwindSafe for Tensor<D, K>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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