pub struct Tensor<T: WithDType, B: Backend> { /* private fields */ }Implementations§
Source§impl<T: WithDType, B: Backend> Tensor<T, B>
impl<T: WithDType, B: Backend> Tensor<T, B>
pub fn binary_(&self, lhs: &Self, rhs: &Self, op: BinaryOp) -> Result<()>
pub fn broadcast_binary_( &self, lhs: &Self, rhs: &Self, op: BinaryOp, ) -> Result<()>
pub fn inplace_add(&self, other: &Self) -> Result<()>
pub fn add_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_add_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn inplace_sub(&self, other: &Self) -> Result<()>
pub fn sub_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_sub_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn inplace_mul(&self, other: &Self) -> Result<()>
pub fn mul_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_mul_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn inplace_div(&self, other: &Self) -> Result<()>
pub fn div_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_div_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn inplace_maximum(&self, other: &Self) -> Result<()>
pub fn maximum_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_maximum_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn inplace_minimum(&self, other: &Self) -> Result<()>
pub fn minimum_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn broadcast_minimum_(&self, lhs: &Self, rhs: &Self) -> Result<()>
pub fn to_dtype_<U: WithDType>(&self, src: &Tensor<U, B>) -> Result<()>
pub fn transpose_(&self, src: &Self, dim1: usize, dim2: usize) -> Result<()>
pub fn copy_(&self, src: &Self) -> Result<()>
pub fn fill_(&self, value: T) -> Result<()>
pub fn scale_(&self, src: &Self, m: T) -> Result<()>
pub fn add_scalar_(&self, src: &Self, a: T) -> Result<()>
pub fn scale_add_(&self, src: &Self, scale: T, add: T) -> Result<()>
Source§impl<T: WithDTypeF, B: Backend> Tensor<T, B>
impl<T: WithDTypeF, B: Backend> Tensor<T, B>
pub fn unary_(&self, src: &Self, op: UnaryOp) -> Result<()>
pub fn cos_(&self, src: &Self) -> Result<()>
pub fn sin_(&self, src: &Self) -> Result<()>
pub fn exp_(&self, src: &Self) -> Result<()>
pub fn log_(&self, src: &Self) -> Result<()>
pub fn neg_(&self, src: &Self) -> Result<()>
pub fn gelu_erf_(&self, src: &Self) -> Result<()>
pub fn elu_(&self, src: &Self, alpha: f32) -> Result<()>
pub fn abs_(&self, src: &Self) -> Result<()>
pub fn sqr_(&self, src: &Self) -> Result<()>
pub fn sqrt_(&self, src: &Self) -> Result<()>
pub fn rsqrt_(&self, src: &Self) -> Result<()>
pub fn relu_(&self, src: &Self) -> Result<()>
pub fn tanh_(&self, src: &Self) -> Result<()>
pub fn sigmoid_(&self, src: &Self) -> Result<()>
pub fn silu_(&self, src: &Self) -> Result<()>
pub fn softmax_(&self, src: &Self) -> Result<()>
Sourcepub fn apply_causality_mask_(&self, offset: usize) -> Result<()>
pub fn apply_causality_mask_(&self, offset: usize) -> Result<()>
Apply causality mask in-place. Shape: (batch * heads, seq_q, seq_kv) or (batch, heads, seq_q, seq_kv) Masks positions where key position > query position + offset (sets to -inf). offset: starting position of the first query token (for KV cache generation).
pub fn rms_norm_(&self, src: &Self, alpha: &Self, eps: f32) -> Result<()>
pub fn layer_norm_( &self, src: &Self, weight: &Self, bias: &Self, eps: f32, remove_mean: bool, ) -> Result<()>
pub fn matmul_<L: TensorOrView<T, B>, R: TensorOrView<T, B>>( &self, lhs: &L, rhs: &R, rhs_t: bool, ) -> Result<()>
pub fn rope_( &self, src: &Self, cos: &Self, sin: &Self, pos: usize, ) -> Result<()>
pub fn rope_i_( &self, src: &Self, cos: &Self, sin: &Self, pos: usize, ) -> Result<()>
pub fn reduce_max_(&self, src: &Self, dim: usize) -> Result<()>
pub fn reduce_min_(&self, src: &Self, dim: usize) -> Result<()>
pub fn reduce_argmin_<U: WithDTypeF>( dst: &Tensor<i64, B>, src: &Tensor<U, B>, dim: usize, ) -> Result<()>
pub fn reduce_argmax_<U: WithDTypeF>( dst: &Tensor<i64, B>, src: &Tensor<U, B>, dim: usize, ) -> Result<()>
pub fn reduce_sum_(&self, src: &Self, dim: usize) -> Result<()>
pub fn conv1d_( &self, src: &Self, kernel: &Self, stride: usize, padding: usize, dilation: usize, groups: usize, ) -> Result<()>
pub fn conv_transpose1d_( &self, src: &Self, kernel: &Self, stride: usize, padding: usize, output_padding: usize, groups: usize, ) -> Result<()>
Source§impl<B: Backend> Tensor<f32, B>
impl<B: Backend> Tensor<f32, B>
pub fn randn_like(&self, mean: f32, std: f32) -> Result<Self>
pub fn rand_uniform_like(&self, lo: f32, up: f32) -> Result<Self>
pub fn randn( &self, shape: impl Into<Shape>, mean: f32, std: f32, ) -> Result<Self>
pub fn rand_uniform( &self, shape: impl Into<Shape>, lo: f32, up: f32, ) -> Result<Self>
Source§impl<T: WithDType, B: Backend> Tensor<T, B>
impl<T: WithDType, B: Backend> Tensor<T, B>
pub fn binary(&self, other: &Self, op: BinaryOp) -> Result<Self>
pub fn broadcast_binary(&self, other: &Self, op: BinaryOp) -> Result<Self>
pub fn add(&self, other: &Self) -> Result<Self>
pub fn broadcast_add(&self, other: &Self) -> Result<Self>
pub fn sub(&self, other: &Self) -> Result<Self>
pub fn broadcast_sub(&self, other: &Self) -> Result<Self>
pub fn mul(&self, other: &Self) -> Result<Self>
pub fn broadcast_mul(&self, other: &Self) -> Result<Self>
pub fn div(&self, other: &Self) -> Result<Self>
pub fn broadcast_div(&self, other: &Self) -> Result<Self>
pub fn minimum(&self, other: &Self) -> Result<Self>
pub fn broadcast_minimum(&self, other: &Self) -> Result<Self>
pub fn maximum(&self, other: &Self) -> Result<Self>
pub fn broadcast_maximum(&self, other: &Self) -> Result<Self>
Sourcepub fn transpose<D1: Dim, D2: Dim>(
&self,
dim1: D1,
dim2: D2,
) -> Result<TensorView<T, B>>
pub fn transpose<D1: Dim, D2: Dim>( &self, dim1: D1, dim2: D2, ) -> Result<TensorView<T, B>>
Transpose two dimensions.
Returns a TensorView (zero-copy). Call .contiguous()? on the result
if you need a contiguous Tensor.
pub fn copy(&self) -> Result<Self>
pub fn full_like(&self, value: T) -> Result<Self>
pub fn scale(&self, m: T) -> Result<Self>
pub fn add_scalar(&self, a: T) -> Result<Self>
pub fn scale_add(&self, scale: T, add: T) -> Result<Self>
Sourcepub fn flatten_all(&self) -> Result<Self>
pub fn flatten_all(&self) -> Result<Self>
Flatten all dimensions into a single dimension.
Sourcepub fn flatten<D: Dim>(&self, start_dim: D, end_dim: D) -> Result<Self>
pub fn flatten<D: Dim>(&self, start_dim: D, end_dim: D) -> Result<Self>
Flatten dimensions from start to end (inclusive) into a single dimension.
Sourcepub fn zeros_like(&self) -> Result<Self>
pub fn zeros_like(&self) -> Result<Self>
Create a tensor of zeros with the same shape.
Sourcepub fn t(&self) -> Result<TensorView<T, B>>
pub fn t(&self) -> Result<TensorView<T, B>>
Transpose (swap last two dimensions).
Returns a TensorView (zero-copy). Call .contiguous()? on the result
if you need a contiguous Tensor.
Source§impl<T: WithDTypeF, B: Backend> Tensor<T, B>
impl<T: WithDTypeF, B: Backend> Tensor<T, B>
pub fn cos(&self) -> Result<Self>
pub fn sin(&self) -> Result<Self>
pub fn silu(&self) -> Result<Self>
pub fn softmax(&self) -> Result<Self>
Sourcepub fn apply_causality_mask(&self, offset: usize) -> Result<Self>
pub fn apply_causality_mask(&self, offset: usize) -> Result<Self>
Apply causality mask and return a new tensor. Shape: (batch * heads, seq_q, seq_kv) or (batch, heads, seq_q, seq_kv) Masks positions where key position > query position + offset (sets to -inf). offset: starting position of the first query token (for KV cache generation).
pub fn rms_norm(&self, alpha: &Self, eps: f32) -> Result<Self>
pub fn layer_norm(&self, weight: &Self, bias: &Self, eps: f32) -> Result<Self>
pub fn layer_norm_rm( &self, weight: &Self, bias: &Self, eps: f32, remove_mean: bool, ) -> Result<Self>
pub fn rope(&self, cos: &Self, sin: &Self, pos: usize) -> Result<Self>
Sourcepub fn sdpa_decode(
&self,
k: &TensorView<T, B>,
v: &TensorView<T, B>,
mask: Option<&Self>,
scale: f32,
) -> Result<Self>
pub fn sdpa_decode( &self, k: &TensorView<T, B>, v: &TensorView<T, B>, mask: Option<&Self>, scale: f32, ) -> Result<Self>
Scaled-dot-product attention for a single query position (autoregressive decode).
self is the query, shape (b, 1, h, d). k and v are the accumulated keys and
values, shape (b, kv, h, d) — the layout an attention cache is written in, so callers
need no transposes. Returns (b, 1, h * d), ready for the output projection.
mask, when given, holds additive terms per key position (0 to keep it, -inf to
drop it), applied to every head. It is either kv values, shared by the whole batch,
or one row of kv values per batch entry, shaped (b, kv) or (b, 1, 1, kv) — which
is how a batch of sequences padded to a common length hides each one’s padding. The
per-row form spells kv out even when it is 1, so (b, 1) rather than (b,). A mask
per head is not supported on either path, and neither is a rank-3 per-row mask: against
the (b, h, 1, kv) scores a rank-3 mask’s first dimension is the head, so (b, 1, kv)
is the same shape as the per-head (h, 1, kv) whenever b == h.
Backends advertising Backend::FUSED_SDPA_DECODE run this as one pass; otherwise it
is composed from transpose/matmul/softmax, which is what the caller would have written
by hand.
pub fn rope_i(&self, cos: &Self, sin: &Self, pos: usize) -> Result<Self>
Sourcepub fn conv1d(
&self,
kernel: &Self,
bias: Option<&Self>,
stride: usize,
padding: usize,
dilation: usize,
groups: usize,
) -> Result<Self>
pub fn conv1d( &self, kernel: &Self, bias: Option<&Self>, stride: usize, padding: usize, dilation: usize, groups: usize, ) -> Result<Self>
1D convolution. Input: (batch, in_channels, length) Kernel: (out_channels, in_channels/groups, kernel_size) Output: (batch, out_channels, out_length)
The kernel is treated as a model parameter: on the CPU, the gemm keeps it packed between calls. Do not write it in place once it has been used.
Sourcepub fn conv_transpose1d(
&self,
kernel: &Self,
bias: Option<&Self>,
stride: usize,
padding: usize,
output_padding: usize,
groups: usize,
) -> Result<Self>
pub fn conv_transpose1d( &self, kernel: &Self, bias: Option<&Self>, stride: usize, padding: usize, output_padding: usize, groups: usize, ) -> Result<Self>
1D transposed convolution. Input: (batch, in_channels, length) Kernel: (in_channels, out_channels/groups, kernel_size) Output: (batch, out_channels, out_length)
The kernel is treated as a model parameter: on the CPU, the gemm keeps it packed between calls. Do not write it in place once it has been used.
Sourcepub fn sum_keepdim(&self, dims: impl Into<Vec<usize>>) -> Result<Self>
pub fn sum_keepdim(&self, dims: impl Into<Vec<usize>>) -> Result<Self>
Sum along dimensions, keeping the dimensions (with size 1).
Sourcepub fn argmin<D: Dim>(&self, dim: D) -> Result<Tensor<i64, B>>
pub fn argmin<D: Dim>(&self, dim: D) -> Result<Tensor<i64, B>>
Argmin along dimension. Returns i64 indices.
Sourcepub fn argmax<D: Dim>(&self, dim: D) -> Result<Tensor<i64, B>>
pub fn argmax<D: Dim>(&self, dim: D) -> Result<Tensor<i64, B>>
Argmax along dimension. Returns i64 indices.
Sourcepub fn expand(&self, shape: impl Into<Shape>) -> Result<TensorView<T, B>>
pub fn expand(&self, shape: impl Into<Shape>) -> Result<TensorView<T, B>>
Expand tensor to a new shape (broadcasting).
Sourcepub fn pad_with_zeros<D: Dim>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Self>
pub fn pad_with_zeros<D: Dim>( &self, dim: D, left: usize, right: usize, ) -> Result<Self>
Pad with zeros along a dimension.
Sourcepub fn pad_with_same<D: Dim>(
&self,
dim: D,
left: usize,
right: usize,
) -> Result<Self>
pub fn pad_with_same<D: Dim>( &self, dim: D, left: usize, right: usize, ) -> Result<Self>
Pad by replicating boundary values.
pub fn matmul_t<R: TensorOrView<T, B>>(&self, rhs: &R) -> Result<Self>
pub fn matmul<R: TensorOrView<T, B>>(&self, rhs: &R) -> Result<Self>
Source§impl<T: WithDType, B: Backend> Tensor<T, B>
impl<T: WithDType, B: Backend> Tensor<T, B>
pub fn dtype(&self) -> DType
pub fn shape(&self) -> &Shape
pub fn elem_count(&self) -> usize
pub fn rank(&self) -> usize
pub fn dims(&self) -> &[usize]
pub fn dim(&self, index: impl Dim) -> Result<usize>
pub fn device(&self) -> &B
Sourcepub fn storage(&self) -> Result<RwLockReadGuard<'_, B::Storage<T>>>
pub fn storage(&self) -> Result<RwLockReadGuard<'_, B::Storage<T>>>
Borrow the underlying storage immutably. Returns an error if the storage is currently mutably borrowed.
Sourcepub fn storage_mut(&self) -> Result<RwLockWriteGuard<'_, B::Storage<T>>>
pub fn storage_mut(&self) -> Result<RwLockWriteGuard<'_, B::Storage<T>>>
Borrow the underlying storage mutably. Returns an error if the storage is currently borrowed (mutably or immutably).
pub fn zeros(shape: impl Into<Shape>, device: &B) -> Result<Self>
pub fn to_vec(&self) -> Result<Vec<T>>
pub fn to_scalar(&self) -> Result<T>
pub fn to_vec1(&self) -> Result<Vec<T>>
pub fn to_vec2(&self) -> Result<Vec<Vec<T>>>
pub fn to_vec3(&self) -> Result<Vec<Vec<Vec<T>>>>
pub fn full(value: T, shape: impl Into<Shape>, device: &B) -> Result<Self>
pub fn broadcast_as<S: Into<Shape>>(&self, shape: S) -> Result<TensorView<T, B>>
Sourcepub fn reshape(&self, shape: impl ShapeWithOneHole) -> Result<Self>
pub fn reshape(&self, shape: impl ShapeWithOneHole) -> Result<Self>
Reshape the tensor to a new shape with the same number of elements. This operation shares the underlying data (no copy).
pub fn squeeze(&self, dim: impl Dim) -> Result<Self>
Sourcepub fn narrow(
&self,
dim: impl Dim,
range: impl RangeBounds<usize>,
) -> Result<TensorView<T, B>>
pub fn narrow( &self, dim: impl Dim, range: impl RangeBounds<usize>, ) -> Result<TensorView<T, B>>
Extract a slice of the tensor along a given dimension.
Returns a TensorView (zero-copy). Call .contiguous()? on the result
if you need a contiguous Tensor.
Sourcepub unsafe fn alloc_uninit(shape: impl Into<Shape>, dev: &B) -> Result<Self>
pub unsafe fn alloc_uninit(shape: impl Into<Shape>, dev: &B) -> Result<Self>
§Safety
The returned tensor’s data is uninitialized.
pub fn index_select( &self, indices: &Tensor<i64, B>, dim: impl Dim, ) -> Result<Self>
pub fn from_vec<S: ShapeWithOneHole>( data: Vec<T>, shape: S, dev: &B, ) -> Result<Self>
Sourcepub fn cat(tensors: &[&Self], dim: impl Dim) -> Result<Self>
pub fn cat(tensors: &[&Self], dim: impl Dim) -> Result<Self>
Concatenate tensors along a given dimension.
Sourcepub fn stack(tensors: &[&Self], dim: impl Dim) -> Result<Self>
pub fn stack(tensors: &[&Self], dim: impl Dim) -> Result<Self>
Stack tensors along a new dimension.
All tensors must have the same shape.
The new dimension is inserted at position dim.
pub fn downcast(&self) -> Result<TypedTensor<B>>
Sourcepub fn slice_set(&self, src: &Self, dim: impl Dim, offset: usize) -> Result<()>
pub fn slice_set(&self, src: &Self, dim: impl Dim, offset: usize) -> Result<()>
Set the values on self using values from src. The copy starts at the specified
offset for the target dimension dim on self.
self and src must have the same shape except on dimension dim where the self size
has to be greater than or equal to offset plus the src size.
Note that this modifies self in place.
pub fn slice_assign<D: Dim>( &self, src: &Self, dim: D, offset: usize, ) -> Result<()>
pub fn scatter<D: Dim>( &self, indexes: &Tensor<i64, B>, source: &Self, dim: D, ) -> Result<Self>
pub fn scatter_set<D: Dim>( &self, indexes: &Tensor<i64, B>, source: &Self, dim: D, ) -> Result<()>
pub fn apply<M: Module>(&self, m: &M) -> Result<Self>
Trait Implementations§
Source§impl<T: WithDTypeF, B: Backend> From<Tensor<T, B>> for StreamTensor<T, B>
impl<T: WithDTypeF, B: Backend> From<Tensor<T, B>> for StreamTensor<T, B>
Source§impl<T: WithDTypeF, B: Backend> From<Tensor<T, B>> for StreamTensor<T, B>
impl<T: WithDTypeF, B: Backend> From<Tensor<T, B>> for StreamTensor<T, B>
Auto Trait Implementations§
impl<T, B> Forget for Tensor<T, B>
impl<T, B> Freeze for Tensor<T, B>
impl<T, B> Move for Tensor<T, B>
impl<T, B> RefUnwindSafe for Tensor<T, B>where
Arc<RwLock<<B as Backend>::Storage<T>>>: RefUnwindSafe,
B: RefUnwindSafe,
PhantomData<T>: RefUnwindSafe,
impl<T, B> Send for Tensor<T, B>
impl<T, B> Sync for Tensor<T, B>
impl<T, B> Unpin for Tensor<T, B>
impl<T, B> UnsafeUnpin for Tensor<T, B>where
Arc<RwLock<<B as Backend>::Storage<T>>>: UnsafeUnpin,
B: UnsafeUnpin,
PhantomData<T>: UnsafeUnpin,
impl<T, B> UnwindSafe for Tensor<T, B>where
Arc<RwLock<<B as Backend>::Storage<T>>>: UnwindSafe,
B: UnwindSafe,
PhantomData<T>: UnwindSafe,
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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