Backend

Trait Backend 

Source
pub trait Backend: Send + Sync {
Show 52 methods // Required methods fn name(&self) -> &'static str; fn is_available(&self) -> bool; fn alloc_tensor( &mut self, shape: &[usize], dtype: DType, ) -> Result<TensorHandle>; fn free_tensor(&mut self, handle: TensorHandle) -> Result<()>; fn tensor_meta(&self, handle: TensorHandle) -> Result<TensorMeta>; fn write_tensor(&mut self, handle: TensorHandle, data: &[u8]) -> Result<()>; fn read_tensor(&self, handle: TensorHandle) -> Result<Vec<u8>>; fn scalar_add(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_sub(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_mul(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_div(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_mod(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_sqrt(&mut self, a: &Value) -> Result<Value>; fn scalar_pow(&mut self, base: &Value, exp: &Value) -> Result<Value>; fn scalar_sin(&mut self, a: &Value) -> Result<Value>; fn scalar_cos(&mut self, a: &Value) -> Result<Value>; fn scalar_tan(&mut self, a: &Value) -> Result<Value>; fn scalar_log(&mut self, a: &Value) -> Result<Value>; fn scalar_exp(&mut self, a: &Value) -> Result<Value>; fn scalar_floor(&mut self, a: &Value) -> Result<Value>; fn scalar_ceil(&mut self, a: &Value) -> Result<Value>; fn scalar_round(&mut self, a: &Value) -> Result<Value>; fn scalar_abs(&mut self, a: &Value) -> Result<Value>; fn scalar_eq(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_ne(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_lt(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_le(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_gt(&mut self, a: &Value, b: &Value) -> Result<Value>; fn scalar_ge(&mut self, a: &Value, b: &Value) -> Result<Value>; fn pointwise_add( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>; fn matmul( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>; fn matmul_bias( &mut self, a: TensorHandle, b: TensorHandle, bias: TensorHandle, out: TensorHandle, ) -> Result<()>; fn layer_norm( &mut self, input: TensorHandle, gamma: TensorHandle, beta: TensorHandle, out: TensorHandle, eps: f64, ) -> Result<()>; fn softmax( &mut self, input: TensorHandle, out: TensorHandle, dim: i32, ) -> Result<()>; fn gelu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>; fn relu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>; fn attention( &mut self, q: TensorHandle, k: TensorHandle, v: TensorHandle, out: TensorHandle, mask: Option<TensorHandle>, scale: f64, ) -> Result<()>; fn cross_entropy( &mut self, logits: TensorHandle, targets: TensorHandle, loss_out: TensorHandle, probs_out: TensorHandle, ) -> Result<()>; fn reduce_sum( &mut self, input: TensorHandle, out: TensorHandle, dim: Option<i32>, ) -> Result<()>; fn embedding( &mut self, indices: TensorHandle, weight: TensorHandle, out: TensorHandle, ) -> Result<()>; fn adam_update( &mut self, param: TensorHandle, grad: TensorHandle, m: TensorHandle, v: TensorHandle, lr: f64, beta1: f64, beta2: f64, eps: f64, step: u64, ) -> Result<()>; fn gaussian_blur( &mut self, input: TensorHandle, out: TensorHandle, sigma: &Value, ) -> Result<()>; fn sobel_edges( &mut self, input: TensorHandle, out: TensorHandle, threshold: &Value, ) -> Result<()>; fn grayscale( &mut self, input: TensorHandle, out: TensorHandle, ) -> Result<()>; fn threshold( &mut self, input: TensorHandle, out: TensorHandle, value: &Value, ) -> Result<()>; fn brightness( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>; fn contrast( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>; fn invert_colors( &mut self, input: TensorHandle, out: TensorHandle, ) -> Result<()>; fn sharpen(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>; fn sync(&mut self) -> Result<()>; // Provided methods fn abi_fingerprint(&self) -> u64 { ... } fn dispatch_compute( &mut self, shader_bytes: &[u8], bindings: &[TensorHandle], push_constants: &[u8], workgroup_count: [u32; 3], ) -> Result<()> { ... }
}
Expand description

Backend trait for compute operations

Required Methods§

Source

fn name(&self) -> &'static str

Get backend name

Source

fn is_available(&self) -> bool

Check if backend is available and initialized

Source

fn alloc_tensor( &mut self, shape: &[usize], dtype: DType, ) -> Result<TensorHandle>

Allocate a new tensor

Source

fn free_tensor(&mut self, handle: TensorHandle) -> Result<()>

Free a tensor

Source

fn tensor_meta(&self, handle: TensorHandle) -> Result<TensorMeta>

Get tensor metadata

Source

fn write_tensor(&mut self, handle: TensorHandle, data: &[u8]) -> Result<()>

Write data to tensor

Source

fn read_tensor(&self, handle: TensorHandle) -> Result<Vec<u8>>

Read data from tensor

Source

fn scalar_add(&mut self, a: &Value, b: &Value) -> Result<Value>

Add two scalars

Source

fn scalar_sub(&mut self, a: &Value, b: &Value) -> Result<Value>

Subtract two scalars

Source

fn scalar_mul(&mut self, a: &Value, b: &Value) -> Result<Value>

Multiply two scalars

Source

fn scalar_div(&mut self, a: &Value, b: &Value) -> Result<Value>

Divide two scalars

Source

fn scalar_mod(&mut self, a: &Value, b: &Value) -> Result<Value>

Modulo two scalars

Source

fn scalar_sqrt(&mut self, a: &Value) -> Result<Value>

Square root

Source

fn scalar_pow(&mut self, base: &Value, exp: &Value) -> Result<Value>

Power

Source

fn scalar_sin(&mut self, a: &Value) -> Result<Value>

Sine

Source

fn scalar_cos(&mut self, a: &Value) -> Result<Value>

Cosine

Source

fn scalar_tan(&mut self, a: &Value) -> Result<Value>

Tangent

Source

fn scalar_log(&mut self, a: &Value) -> Result<Value>

Natural logarithm

Source

fn scalar_exp(&mut self, a: &Value) -> Result<Value>

Exponential

Source

fn scalar_floor(&mut self, a: &Value) -> Result<Value>

Floor

Source

fn scalar_ceil(&mut self, a: &Value) -> Result<Value>

Ceiling

Source

fn scalar_round(&mut self, a: &Value) -> Result<Value>

Round

Source

fn scalar_abs(&mut self, a: &Value) -> Result<Value>

Absolute value

Source

fn scalar_eq(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn scalar_ne(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn scalar_lt(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn scalar_le(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn scalar_gt(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn scalar_ge(&mut self, a: &Value, b: &Value) -> Result<Value>

Source

fn pointwise_add( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>

Pointwise addition: out = a + b

Source

fn matmul( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>

Matrix multiplication: C = A @ B

Source

fn matmul_bias( &mut self, a: TensorHandle, b: TensorHandle, bias: TensorHandle, out: TensorHandle, ) -> Result<()>

Matrix multiplication with bias: C = A @ B + bias

Source

fn layer_norm( &mut self, input: TensorHandle, gamma: TensorHandle, beta: TensorHandle, out: TensorHandle, eps: f64, ) -> Result<()>

Layer normalization

Source

fn softmax( &mut self, input: TensorHandle, out: TensorHandle, dim: i32, ) -> Result<()>

Softmax activation

Source

fn gelu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>

GELU activation

Source

fn relu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>

ReLU activation

Source

fn attention( &mut self, q: TensorHandle, k: TensorHandle, v: TensorHandle, out: TensorHandle, mask: Option<TensorHandle>, scale: f64, ) -> Result<()>

Attention: softmax(Q @ K^T / sqrt(d)) @ V

Source

fn cross_entropy( &mut self, logits: TensorHandle, targets: TensorHandle, loss_out: TensorHandle, probs_out: TensorHandle, ) -> Result<()>

Cross-entropy loss

Source

fn reduce_sum( &mut self, input: TensorHandle, out: TensorHandle, dim: Option<i32>, ) -> Result<()>

Sum reduction

Source

fn embedding( &mut self, indices: TensorHandle, weight: TensorHandle, out: TensorHandle, ) -> Result<()>

Embedding lookup

Source

fn adam_update( &mut self, param: TensorHandle, grad: TensorHandle, m: TensorHandle, v: TensorHandle, lr: f64, beta1: f64, beta2: f64, eps: f64, step: u64, ) -> Result<()>

Adam optimizer update

Source

fn gaussian_blur( &mut self, input: TensorHandle, out: TensorHandle, sigma: &Value, ) -> Result<()>

Gaussian blur filter

Source

fn sobel_edges( &mut self, input: TensorHandle, out: TensorHandle, threshold: &Value, ) -> Result<()>

Sobel edge detection

Source

fn grayscale(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>

Convert to grayscale

Source

fn threshold( &mut self, input: TensorHandle, out: TensorHandle, value: &Value, ) -> Result<()>

Binary threshold

Source

fn brightness( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>

Adjust brightness

Source

fn contrast( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>

Adjust contrast

Source

fn invert_colors( &mut self, input: TensorHandle, out: TensorHandle, ) -> Result<()>

Invert colors

Source

fn sharpen(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>

Sharpen filter

Source

fn sync(&mut self) -> Result<()>

Synchronize all pending operations

Provided Methods§

Source

fn abi_fingerprint(&self) -> u64

Get ABI fingerprint for this backend implementation

This MUST return BACKEND_ABI_FINGERPRINT. Runtime will verify this matches to prevent vtable corruption.

Source

fn dispatch_compute( &mut self, shader_bytes: &[u8], bindings: &[TensorHandle], push_constants: &[u8], workgroup_count: [u32; 3], ) -> Result<()>

Dispatch a generic compute shader

  • shader_bytes: Raw SPIR-V binary
  • bindings: List of tensors to bind as storage buffers (binding = index)
  • push_constants: Raw bytes for push constants
  • workgroup_count: [x, y, z] dispatch dimensions

Implementors§