pub trait ComputeBackend: Send + Sync {
type Tensor: TensorOps;
type Device: DeviceOps;
Show 18 methods
// Required methods
fn device(&self) -> &Self::Device;
fn allocate(&self, shape: &[usize], dtype: DType) -> Result<Self::Tensor>;
fn from_slice(&self, data: &[f32], shape: &[usize]) -> Result<Self::Tensor>;
fn matmul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>;
fn batch_matmul(
&self,
a: &Self::Tensor,
b: &Self::Tensor,
) -> Result<Self::Tensor>;
fn attention(
&self,
q: &Self::Tensor,
k: &Self::Tensor,
v: &Self::Tensor,
mask: Option<&Self::Tensor>,
scale: Option<f32>,
) -> Result<Self::Tensor>;
fn rms_norm(
&self,
x: &Self::Tensor,
weight: &Self::Tensor,
eps: f32,
) -> Result<Self::Tensor>;
fn layer_norm(
&self,
x: &Self::Tensor,
weight: &Self::Tensor,
bias: Option<&Self::Tensor>,
eps: f32,
) -> Result<Self::Tensor>;
fn silu(&self, x: &Self::Tensor) -> Result<Self::Tensor>;
fn gelu(&self, x: &Self::Tensor) -> Result<Self::Tensor>;
fn relu(&self, x: &Self::Tensor) -> Result<Self::Tensor>;
fn softmax(&self, x: &Self::Tensor, dim: i32) -> Result<Self::Tensor>;
fn add(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>;
fn mul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>;
fn transpose(&self, x: &Self::Tensor) -> Result<Self::Tensor>;
fn reshape(&self, x: &Self::Tensor, shape: &[usize]) -> Result<Self::Tensor>;
fn to_device(&self, tensor: &Self::Tensor) -> Result<Self::Tensor>;
fn to_cpu(&self, tensor: &Self::Tensor) -> Result<Vec<f32>>;
}Expand description
Trait defining a compute backend.
Required Associated Types§
Required Methods§
Sourcefn allocate(&self, shape: &[usize], dtype: DType) -> Result<Self::Tensor>
fn allocate(&self, shape: &[usize], dtype: DType) -> Result<Self::Tensor>
Allocates a new tensor filled with zeros.
Sourcefn from_slice(&self, data: &[f32], shape: &[usize]) -> Result<Self::Tensor>
fn from_slice(&self, data: &[f32], shape: &[usize]) -> Result<Self::Tensor>
Creates a tensor from raw data.
Sourcefn matmul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
fn matmul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
Performs matrix multiplication: C = A @ B.
Sourcefn batch_matmul(
&self,
a: &Self::Tensor,
b: &Self::Tensor,
) -> Result<Self::Tensor>
fn batch_matmul( &self, a: &Self::Tensor, b: &Self::Tensor, ) -> Result<Self::Tensor>
Performs batched matrix multiplication.
Sourcefn attention(
&self,
q: &Self::Tensor,
k: &Self::Tensor,
v: &Self::Tensor,
mask: Option<&Self::Tensor>,
scale: Option<f32>,
) -> Result<Self::Tensor>
fn attention( &self, q: &Self::Tensor, k: &Self::Tensor, v: &Self::Tensor, mask: Option<&Self::Tensor>, scale: Option<f32>, ) -> Result<Self::Tensor>
Performs scaled dot-product attention. Returns softmax(Q @ K^T / sqrt(d_k)) @ V with optional causal masking.
Sourcefn rms_norm(
&self,
x: &Self::Tensor,
weight: &Self::Tensor,
eps: f32,
) -> Result<Self::Tensor>
fn rms_norm( &self, x: &Self::Tensor, weight: &Self::Tensor, eps: f32, ) -> Result<Self::Tensor>
Applies RMS normalization: x * weight / sqrt(mean(x^2) + eps).
Sourcefn layer_norm(
&self,
x: &Self::Tensor,
weight: &Self::Tensor,
bias: Option<&Self::Tensor>,
eps: f32,
) -> Result<Self::Tensor>
fn layer_norm( &self, x: &Self::Tensor, weight: &Self::Tensor, bias: Option<&Self::Tensor>, eps: f32, ) -> Result<Self::Tensor>
Applies Layer normalization.
Sourcefn silu(&self, x: &Self::Tensor) -> Result<Self::Tensor>
fn silu(&self, x: &Self::Tensor) -> Result<Self::Tensor>
Applies SiLU (Swish) activation: x * sigmoid(x).
Sourcefn softmax(&self, x: &Self::Tensor, dim: i32) -> Result<Self::Tensor>
fn softmax(&self, x: &Self::Tensor, dim: i32) -> Result<Self::Tensor>
Applies softmax along the specified dimension.
Sourcefn add(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
fn add(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
Element-wise addition.
Sourcefn mul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
fn mul(&self, a: &Self::Tensor, b: &Self::Tensor) -> Result<Self::Tensor>
Element-wise multiplication.
Sourcefn transpose(&self, x: &Self::Tensor) -> Result<Self::Tensor>
fn transpose(&self, x: &Self::Tensor) -> Result<Self::Tensor>
Transposes the last two dimensions.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".