Skip to main content

Tensor

Struct Tensor 

Source
pub struct Tensor {
    pub shape: Vec<usize>,
    pub strides: Vec<usize>,
    /* private fields */
}
Expand description

A general N-D f32 tensor: an Arc-shared device buffer viewed through (shape, strides, offset).

Fields§

§shape: Vec<usize>§strides: Vec<usize>

Implementations§

Source§

impl Tensor

Source

pub fn to_half(&self, dtype: DType) -> Half

Pack this f32 tensor down to half precision (round-to-nearest-even), on-device.

Source§

impl Tensor

Source

pub async fn quantize_i8(&self) -> QTensor

Symmetric per-tensor int8 quantization (scale = max|x|/127). Async: the scalar scale is read back so the quantized matmul can fold both scales into one small buffer (WebGPU allows only 4 storage buffers per shader — scalars ride in the info buffer instead of their own bindings).

Source§

impl Tensor

Source

pub fn quantize_rowwise(&self, bits: u32) -> QRow

Per-row symmetric quantization of a 2D matrix at 4 or 8 bits (scale = max|row|/(2^(bits-1)−1)).

Source§

impl Tensor

Source

pub fn matmul_qweight(&self, w: &QRow) -> Tensor

Weight-only quantized matmul (the efficient-inference path): x [rows, in] · Wᵀ where W is a per-row-quantized [out, in] matrix that stays packed in memory — dequantized on the fly in the kernel. Returns [rows, out]. This is W4A16/W8A16-style: activations f32, weights int4/int8.

Source§

impl Tensor

Source

pub fn quantize_ternary(&self) -> Ternary

Quantize a 2D [out,in] weight to ternary {−1,0,+1} with per-row absmean scale (BitNet-style).

Source

pub fn matmul_ternary(&self, w: &Ternary) -> Tensor

Multiply-free ternary matmul: x [rows,in] · Wᵀ where W is ternary [out,in]. Returns [rows,out].

Source§

impl Tensor

Source

pub fn numel(&self) -> usize

Source

pub fn rank(&self) -> usize

Source

pub fn is_contiguous(&self) -> bool

Source

pub fn from_vec(ctx: &Arc<Context>, data: &[f32], shape: &[usize]) -> Tensor

Source

pub fn zeros(ctx: &Arc<Context>, shape: &[usize]) -> Tensor

Source

pub async fn to_vec(&self) -> Vec<f32>

Materialize (contiguous) and read back to host in logical row-major order.

Source

pub fn reshape(&self, shape: &[usize]) -> Tensor

Source

pub fn permute(&self, perm: &[usize]) -> Tensor

Source

pub fn transpose(&self, a: usize, b: usize) -> Tensor

Source

pub fn broadcast_to(&self, shape: &[usize]) -> Tensor

Broadcast to a larger shape (right-aligned); broadcast dims get stride 0.

Source

pub fn contiguous(&self) -> Tensor

Materialize a (possibly strided/broadcast) view into a fresh contiguous buffer.

Source

pub fn add(&self, o: &Tensor) -> Tensor

Source

pub fn sub(&self, o: &Tensor) -> Tensor

Source

pub fn mul(&self, o: &Tensor) -> Tensor

Source

pub fn div(&self, o: &Tensor) -> Tensor

Source

pub fn maximum(&self, o: &Tensor) -> Tensor

Source

pub fn exp(&self) -> Tensor

Source

pub fn neg(&self) -> Tensor

Source

pub fn relu(&self) -> Tensor

Source

pub fn sqrt(&self) -> Tensor

Source

pub fn relu_mask(&self) -> Tensor

Source

pub fn abs(&self) -> Tensor

Source

pub fn sigmoid(&self) -> Tensor

Source

pub fn silu(&self) -> Tensor

Source

pub fn gelu(&self) -> Tensor

Source

pub fn log(&self) -> Tensor

Source

pub fn relu2(&self) -> Tensor

Source

pub fn scalar(&self, s: f32) -> Tensor

Source

pub fn softmax(&self, axis: usize) -> Tensor

Softmax over axis (fused: per-row max/exp/sum/div in one kernel).

Source

pub fn rmsnorm(&self, weight: &Tensor, eps: f32) -> Tensor

RMSNorm over the last dim: x/sqrt(mean(x²)+eps)·weight (fused).

Source

pub fn rope( &self, n_heads: usize, head_dim: usize, base: f32, offset: usize, ) -> Tensor

Rotary position embedding (NeoX rotate-half) on a [T, n_heads·head_dim] tensor.

Source

pub fn rope_3d( &self, n_heads: usize, head_dim: usize, base: f32, gt: usize, gh: usize, gw: usize, ) -> Tensor

3D rotary position embedding (V-JEPA 2): head_dim split into 3 groups (temporal/height/width), each rotated by the token’s coordinate along that axis. self is [T, n_heads·head_dim], T=gt·gh·gw.

Source

pub fn depthwise_conv1d_causal(&self, weight: &Tensor, l: usize) -> Tensor

Causal depthwise conv1d — the LFM2 / Liquid AI short-conv mixer. self is [T, C] (sequence × channels), weight is [C, L] (per-channel kernel of length L). Causal: out[t] sees only t-L+1..t.

Source

pub fn matmul_bt(&self, w: &Tensor) -> Tensor

y = x·Wᵀ where x is [rows,in] and W is stored [out,in] (HF linear convention) — computed directly, without materializing Wᵀ. Essential for big tied LM heads (avoids a huge transpose).

Source

pub fn matmul_bt_act(&self, w: &Tensor, act: u32) -> Tensor

y = act(x·Wᵀ) — a linear projection with the activation fused into the matmul epilogue (one kernel, no intermediate). act: 0 identity, 1 relu, 2 silu, 3 gelu, 4 sigmoid. Every gated FFN (silu(x·Wgateᵀ)) and every relu/gelu MLP hidden layer collapses to a single dispatch.

Source

pub fn gather_rows(&self, idx: &[u32]) -> Tensor

Row gather (embedding lookup): self is a [vocab, d] table; returns [idx.len(), d].

Source

pub fn sum(&self, axes: &[usize], keepdim: bool) -> Tensor

Source

pub fn max(&self, axes: &[usize], keepdim: bool) -> Tensor

Source

pub fn mean(&self, axes: &[usize], keepdim: bool) -> Tensor

Source

pub fn matmul(&self, other: &Tensor) -> Tensor

Source

pub async fn autotune_matmul(&self, other: &Tensor) -> &'static str

Autotune the GEMM kernel for this shape on this device: time naive vs tiled and cache the winner (keyed by shape bucket). Subsequent matmuls of the same bucket use it. Returns the choice. This is how GEMM stays fast portably — the winner differs across GPUs.

Source

pub fn matmul_tiled(&self, other: &Tensor) -> Tensor

Force the register-blocked tiled 2D GEMM (for benchmarking / large matmuls).

Source

pub fn matmul_naive(&self, other: &Tensor) -> Tensor

The naive (non-tiled) matmul, kept for benchmarking the tiled fast-path against.

Trait Implementations§

Source§

impl Clone for Tensor

Source§

fn clone(&self) -> Tensor

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

Auto Trait Implementations§

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> Downcast<T> for T

Source§

fn downcast(&self) -> &T

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,