Skip to main content

ComputeEngine

Struct ComputeEngine 

Source
pub struct ComputeEngine { /* private fields */ }
Expand description

Compiled WGSL kernels and the methods that dispatch them.

Every method records and submits its own command buffer and returns without waiting; reading a result (e.g. with UnifiedTensor::to_vec) waits for the GPU. Tensors are dense, row-major f32 unless stated otherwise.

Implementations§

Source§

impl ComputeEngine

Source

pub fn rope( &self, ctx: &WgpuContext, tensor: &UnifiedTensor, head_dim: usize, pos: usize, )

Rotate-half RoPE (theta 10000) applied in place.

tensor is [batch, seq, ...], [seq, hidden] or a single token [hidden]; every head vector of token s gets position pos + s.

Source

pub fn rope_theta( &self, ctx: &WgpuContext, tensor: &UnifiedTensor, head_dim: usize, pos: usize, theta: f32, )

rope with base frequency theta (the rope_theta of a Hugging Face config, e.g. 100000 for SmolLM2).

Source

pub fn flash_attention( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, causal: bool, )

Single-head attention: q, k, v, out are [seq_len, head_dim].

Runs the v2 kernel on [1, seq_len, 1, head_dim] views of the inputs.

Source

pub fn flash_attention_v2( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, causal: bool, dropout_prob: f32, seed: u64, mask: Option<&UnifiedTensor>, )

Fused attention forward pass with grouped-query attention, causal masking, an optional additive mask and attention dropout.

  • q, out - [batch, seq_q, num_heads, head_dim]
  • k, v - [batch, seq_k, num_kv_heads, head_dim]; num_heads must be a multiple of num_kv_heads
  • causal - key j is visible to query i only when j <= i
  • dropout_prob, seed - attention dropout; pass 0.0 to disable
  • mask - additive mask [batch, heads, seq_q, seq_k], where size-1 dimensions broadcast

head_dim must be at most 128. See flash_attention_v2_with for KV-cache offsets and sliding windows.

Source

pub fn flash_attention_v2_with( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, opts: &AttentionOptions, mask: Option<&UnifiedTensor>, )

flash_attention_v2 with every option.

Source

pub fn flash_attention_backward( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, grad_out: &UnifiedTensor, grad_q: &UnifiedTensor, grad_k: &UnifiedTensor, grad_v: &UnifiedTensor, causal: bool, )

Single-head attention backward pass for [seq_len, head_dim] tensors. Writes (does not accumulate into) grad_q, grad_k and grad_v.

Source

pub fn flash_attention_v2_backward( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, grad_out: &UnifiedTensor, grad_q: &UnifiedTensor, grad_k: &UnifiedTensor, grad_v: &UnifiedTensor, causal: bool, )

Backward pass of flash_attention_v2 without mask or dropout. See flash_attention_v2_backward_with.

Source

pub fn flash_attention_v2_backward_with( &self, ctx: &WgpuContext, q: &UnifiedTensor, k: &UnifiedTensor, v: &UnifiedTensor, out: &UnifiedTensor, grad_out: &UnifiedTensor, grad_q: &UnifiedTensor, grad_k: &UnifiedTensor, grad_v: &UnifiedTensor, opts: &AttentionOptions, mask: Option<&UnifiedTensor>, )

Backward pass of flash_attention_v2.

out is the forward output for the same inputs; opts and mask must match the forward call (dropout decisions are regenerated from opts.seed). Writes grad_q, grad_k and grad_v (same shapes as q, k, v); no gradient is produced for the mask.

Source§

impl ComputeEngine

Source

pub fn add( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out = a + b, element-wise; a, b and out have the same length.

Source

pub fn mul( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out = a * b, element-wise.

Source

pub fn sub( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out = a - b, element-wise.

Source

pub fn add_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, grad_a: &UnifiedTensor, grad_b: &UnifiedTensor, )

Gradients of a + b: writes grad_out to both grad_a and grad_b.

Source

pub fn sub_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, grad_a: &UnifiedTensor, grad_b: &UnifiedTensor, )

Gradients of a - b: grad_a = grad_out, grad_b = -grad_out.

Source

pub fn mul_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, a: &UnifiedTensor, b: &UnifiedTensor, grad_a: &UnifiedTensor, grad_b: &UnifiedTensor, )

Gradients of a * b: grad_a = grad_out * b, grad_b = grad_out * a.

Source

pub fn silu( &self, ctx: &WgpuContext, input: &UnifiedTensor, out: &UnifiedTensor, )

out = x * sigmoid(x), element-wise.

Source

pub fn silu_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, input: &UnifiedTensor, grad_in: &UnifiedTensor, )

Gradient of SiLU: grad_in = grad_out * s * (1 + x * (1 - s)) with s = sigmoid(input).

Source

pub fn relu( &self, ctx: &WgpuContext, input: &UnifiedTensor, out: &UnifiedTensor, )

out = max(x, 0), element-wise.

Source

pub fn relu_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, input: &UnifiedTensor, grad_in: &UnifiedTensor, )

Gradient of ReLU: grad_out where input > 0, else 0.

Source

pub fn silu_gate_fused( &self, ctx: &WgpuContext, up: &UnifiedTensor, gate: &UnifiedTensor, out: &UnifiedTensor, )

Fused SiLU-Gate: out = SiLU(up) * gate Reduces memory bandwidth by 2x compared to separate ops

Source

pub fn silu_gate_backward( &self, ctx: &WgpuContext, up: &UnifiedTensor, gate: &UnifiedTensor, grad_out: &UnifiedTensor, grad_up: &UnifiedTensor, grad_gate: &UnifiedTensor, )

Fused SiLU-Gate backward: computes gradients for up and gate

Source§

impl ComputeEngine

Source

pub fn matmul( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out[M, N] = a[M, K] @ b[K, N].

Source

pub fn matmul_bt( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out[M, N] = a[M, K] @ b[N, K]^T (e.g. the input gradient of a linear layer).

Source

pub fn matmul_at( &self, ctx: &WgpuContext, a: &UnifiedTensor, b: &UnifiedTensor, out: &UnifiedTensor, )

out[M, N] = a[K, M]^T @ b[K, N] (e.g. the weight gradient of a linear layer).

Source§

impl ComputeEngine

Source

pub fn top_k( &self, ctx: &WgpuContext, input: &UnifiedTensor, out_idx: &UnifiedTensor, k: u32, )

Writes the indices of the k largest values of input, largest first, as u32s into out_idx. Runs on a single GPU thread; meant for sampling from one logits vector.

Source

pub fn conv1d( &self, ctx: &WgpuContext, input: &UnifiedTensor, weight: &UnifiedTensor, bias: &UnifiedTensor, out: &UnifiedTensor, )

Depthwise 1-D convolution with kernel size 3 and padding 1: out[d, s] = bias[d] + sum_k weight[d, k] * input[d, s + k - 1], with input/out [dim, seq_len], weight [dim, 3] and bias [dim].

Source§

impl ComputeEngine

Source

pub fn rms_norm( &self, ctx: &WgpuContext, input: &UnifiedTensor, weight: &UnifiedTensor, out: &UnifiedTensor, )

RMSNorm over the last dimension with eps = 1e-5: out = x / sqrt(mean(x^2) + eps) * weight.

Source

pub fn rms_norm_eps( &self, ctx: &WgpuContext, input: &UnifiedTensor, weight: &UnifiedTensor, out: &UnifiedTensor, eps: f32, )

RMSNorm over the last dimension with a caller-chosen eps.

Source

pub fn rms_norm_residual( &self, ctx: &WgpuContext, x: &UnifiedTensor, residual: &UnifiedTensor, weight: &UnifiedTensor, out: &UnifiedTensor, )

Fused RMSNorm + Residual: out = RMSNorm(x + residual) * weight

Source

pub fn softmax( &self, ctx: &WgpuContext, input: &UnifiedTensor, out: &UnifiedTensor, )

Softmax over the last dimension of input.

Source

pub fn softmax_backward( &self, ctx: &WgpuContext, grad_out: &UnifiedTensor, final_out: &UnifiedTensor, grad_in: &UnifiedTensor, )

Gradient of softmax over the last dimension, given the forward output final_out: grad_in = s * (grad_out - sum(grad_out * s)).

Source§

impl ComputeEngine

Source

pub fn dequantize( &self, ctx: &WgpuContext, input: &UnifiedTensor, out: &UnifiedTensor, )

Unpacks bytes to floats: element i of out is byte i of input (read as packed u32s) converted to f32, without scaling.

Source

pub fn affine_dequantize( &self, ctx: &WgpuContext, weight: &Buffer, scales: &Buffer, biases: &Buffer, out: &UnifiedTensor, )

8-bit affine dequantisation with groups of 64 (MLX layout): out[i] = byte(weight, i) * scales[i / 64] + biases[i / 64].

Source§

impl ComputeEngine

Source

pub fn sgd_update( &self, ctx: &WgpuContext, weight: &UnifiedTensor, grad: &UnifiedTensor, lr: f32, )

In-place SGD step: weight -= lr * grad.

Source

pub fn adamw_update( &self, ctx: &WgpuContext, weight: &UnifiedTensor, grad: &UnifiedTensor, m: &UnifiedTensor, v: &UnifiedTensor, params: &AdamParams, )

In-place AdamW step, updating weight and the moment buffers m and v.

Source

pub fn mse_loss( &self, ctx: &WgpuContext, pred: &UnifiedTensor, target: &UnifiedTensor, out: &UnifiedTensor, )

Per-element squared error out = 0.5 * (pred - target)^2 (not reduced).

Source

pub fn mse_loss_backward( &self, ctx: &WgpuContext, pred: &UnifiedTensor, target: &UnifiedTensor, grad_in: &UnifiedTensor, )

Gradient of mse_loss: grad_in = pred - target.

Source

pub fn cross_entropy_loss( &self, ctx: &WgpuContext, logits: &UnifiedTensor, target: u32, loss_out: &UnifiedTensor, )

Cross-entropy loss of one logits vector: loss_out[0] = -log(softmax(logits)[target]).

Source

pub fn cross_entropy_backward( &self, ctx: &WgpuContext, logits: &UnifiedTensor, target: u32, grad_out: &UnifiedTensor, )

Cross-entropy loss backward: gradient = softmax(logits) - one_hot(target)

Source

pub fn gradient_norm_squared( &self, ctx: &WgpuContext, input: &UnifiedTensor, out: &UnifiedTensor, )

Compute squared elements (for gradient norm computation)

Source

pub fn clip_grad_by_norm( &self, ctx: &WgpuContext, grad: &UnifiedTensor, scale: f32, )

Clip gradients by scaling: grad *= min(1.0, max_norm / actual_norm)

Source

pub fn gradient_accumulate( &self, ctx: &WgpuContext, accum_grad: &UnifiedTensor, new_grad: &UnifiedTensor, )

Gradient accumulation: accum_grad += new_grad Essential for micro-batching on memory-constrained devices

Source

pub fn gradient_scale( &self, ctx: &WgpuContext, grad: &UnifiedTensor, scale: f32, )

Gradient scaling: grad *= scale_factor Used to average gradients after accumulation

Source§

impl ComputeEngine

Source

pub fn new(ctx: &WgpuContext) -> Self

Compiles every kernel for ctx’s device.

Source

pub fn copy_tensor( &self, ctx: &WgpuContext, src: &UnifiedTensor, dst: &UnifiedTensor, dst_offset: usize, )

Copies all of src into dst, starting at byte offset dst_offset.

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<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, 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, !>

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,