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§
Sourcefn is_available(&self) -> bool
fn is_available(&self) -> bool
Check if backend is available and initialized
Sourcefn alloc_tensor(
&mut self,
shape: &[usize],
dtype: DType,
) -> Result<TensorHandle>
fn alloc_tensor( &mut self, shape: &[usize], dtype: DType, ) -> Result<TensorHandle>
Allocate a new tensor
Sourcefn free_tensor(&mut self, handle: TensorHandle) -> Result<()>
fn free_tensor(&mut self, handle: TensorHandle) -> Result<()>
Free a tensor
Sourcefn tensor_meta(&self, handle: TensorHandle) -> Result<TensorMeta>
fn tensor_meta(&self, handle: TensorHandle) -> Result<TensorMeta>
Get tensor metadata
Sourcefn write_tensor(&mut self, handle: TensorHandle, data: &[u8]) -> Result<()>
fn write_tensor(&mut self, handle: TensorHandle, data: &[u8]) -> Result<()>
Write data to tensor
Sourcefn read_tensor(&self, handle: TensorHandle) -> Result<Vec<u8>>
fn read_tensor(&self, handle: TensorHandle) -> Result<Vec<u8>>
Read data from tensor
Sourcefn scalar_sqrt(&mut self, a: &Value) -> Result<Value>
fn scalar_sqrt(&mut self, a: &Value) -> Result<Value>
Square root
Sourcefn scalar_sin(&mut self, a: &Value) -> Result<Value>
fn scalar_sin(&mut self, a: &Value) -> Result<Value>
Sine
Sourcefn scalar_cos(&mut self, a: &Value) -> Result<Value>
fn scalar_cos(&mut self, a: &Value) -> Result<Value>
Cosine
Sourcefn scalar_tan(&mut self, a: &Value) -> Result<Value>
fn scalar_tan(&mut self, a: &Value) -> Result<Value>
Tangent
Sourcefn scalar_log(&mut self, a: &Value) -> Result<Value>
fn scalar_log(&mut self, a: &Value) -> Result<Value>
Natural logarithm
Sourcefn scalar_exp(&mut self, a: &Value) -> Result<Value>
fn scalar_exp(&mut self, a: &Value) -> Result<Value>
Exponential
Sourcefn scalar_floor(&mut self, a: &Value) -> Result<Value>
fn scalar_floor(&mut self, a: &Value) -> Result<Value>
Floor
Sourcefn scalar_ceil(&mut self, a: &Value) -> Result<Value>
fn scalar_ceil(&mut self, a: &Value) -> Result<Value>
Ceiling
Sourcefn scalar_round(&mut self, a: &Value) -> Result<Value>
fn scalar_round(&mut self, a: &Value) -> Result<Value>
Round
Sourcefn scalar_abs(&mut self, a: &Value) -> Result<Value>
fn scalar_abs(&mut self, a: &Value) -> Result<Value>
Absolute 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>
Sourcefn pointwise_add(
&mut self,
a: TensorHandle,
b: TensorHandle,
out: TensorHandle,
) -> Result<()>
fn pointwise_add( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>
Pointwise addition: out = a + b
Sourcefn matmul(
&mut self,
a: TensorHandle,
b: TensorHandle,
out: TensorHandle,
) -> Result<()>
fn matmul( &mut self, a: TensorHandle, b: TensorHandle, out: TensorHandle, ) -> Result<()>
Matrix multiplication: C = A @ B
Sourcefn matmul_bias(
&mut self,
a: TensorHandle,
b: TensorHandle,
bias: TensorHandle,
out: TensorHandle,
) -> Result<()>
fn matmul_bias( &mut self, a: TensorHandle, b: TensorHandle, bias: TensorHandle, out: TensorHandle, ) -> Result<()>
Matrix multiplication with bias: C = A @ B + bias
Sourcefn layer_norm(
&mut self,
input: TensorHandle,
gamma: TensorHandle,
beta: TensorHandle,
out: TensorHandle,
eps: f64,
) -> Result<()>
fn layer_norm( &mut self, input: TensorHandle, gamma: TensorHandle, beta: TensorHandle, out: TensorHandle, eps: f64, ) -> Result<()>
Layer normalization
Sourcefn softmax(
&mut self,
input: TensorHandle,
out: TensorHandle,
dim: i32,
) -> Result<()>
fn softmax( &mut self, input: TensorHandle, out: TensorHandle, dim: i32, ) -> Result<()>
Softmax activation
Sourcefn gelu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
fn gelu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
GELU activation
Sourcefn relu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
fn relu(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
ReLU activation
Sourcefn attention(
&mut self,
q: TensorHandle,
k: TensorHandle,
v: TensorHandle,
out: TensorHandle,
mask: Option<TensorHandle>,
scale: f64,
) -> Result<()>
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
Sourcefn cross_entropy(
&mut self,
logits: TensorHandle,
targets: TensorHandle,
loss_out: TensorHandle,
probs_out: TensorHandle,
) -> Result<()>
fn cross_entropy( &mut self, logits: TensorHandle, targets: TensorHandle, loss_out: TensorHandle, probs_out: TensorHandle, ) -> Result<()>
Cross-entropy loss
Sourcefn reduce_sum(
&mut self,
input: TensorHandle,
out: TensorHandle,
dim: Option<i32>,
) -> Result<()>
fn reduce_sum( &mut self, input: TensorHandle, out: TensorHandle, dim: Option<i32>, ) -> Result<()>
Sum reduction
Sourcefn embedding(
&mut self,
indices: TensorHandle,
weight: TensorHandle,
out: TensorHandle,
) -> Result<()>
fn embedding( &mut self, indices: TensorHandle, weight: TensorHandle, out: TensorHandle, ) -> Result<()>
Embedding lookup
Sourcefn adam_update(
&mut self,
param: TensorHandle,
grad: TensorHandle,
m: TensorHandle,
v: TensorHandle,
lr: f64,
beta1: f64,
beta2: f64,
eps: f64,
step: u64,
) -> 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<()>
Adam optimizer update
Sourcefn gaussian_blur(
&mut self,
input: TensorHandle,
out: TensorHandle,
sigma: &Value,
) -> Result<()>
fn gaussian_blur( &mut self, input: TensorHandle, out: TensorHandle, sigma: &Value, ) -> Result<()>
Gaussian blur filter
Sourcefn sobel_edges(
&mut self,
input: TensorHandle,
out: TensorHandle,
threshold: &Value,
) -> Result<()>
fn sobel_edges( &mut self, input: TensorHandle, out: TensorHandle, threshold: &Value, ) -> Result<()>
Sobel edge detection
Sourcefn grayscale(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
fn grayscale(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
Convert to grayscale
Sourcefn threshold(
&mut self,
input: TensorHandle,
out: TensorHandle,
value: &Value,
) -> Result<()>
fn threshold( &mut self, input: TensorHandle, out: TensorHandle, value: &Value, ) -> Result<()>
Binary threshold
Sourcefn brightness(
&mut self,
input: TensorHandle,
out: TensorHandle,
factor: &Value,
) -> Result<()>
fn brightness( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>
Adjust brightness
Sourcefn contrast(
&mut self,
input: TensorHandle,
out: TensorHandle,
factor: &Value,
) -> Result<()>
fn contrast( &mut self, input: TensorHandle, out: TensorHandle, factor: &Value, ) -> Result<()>
Adjust contrast
Sourcefn invert_colors(
&mut self,
input: TensorHandle,
out: TensorHandle,
) -> Result<()>
fn invert_colors( &mut self, input: TensorHandle, out: TensorHandle, ) -> Result<()>
Invert colors
Sourcefn sharpen(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
fn sharpen(&mut self, input: TensorHandle, out: TensorHandle) -> Result<()>
Sharpen filter
Provided Methods§
Sourcefn abi_fingerprint(&self) -> u64
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.
Sourcefn dispatch_compute(
&mut self,
shader_bytes: &[u8],
bindings: &[TensorHandle],
push_constants: &[u8],
workgroup_count: [u32; 3],
) -> Result<()>
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 binarybindings: List of tensors to bind as storage buffers (binding = index)push_constants: Raw bytes for push constantsworkgroup_count: [x, y, z] dispatch dimensions