Backend

Trait Backend 

Source
pub trait Backend:
    Debug
    + Send
    + Sync {
Show 23 methods // Required methods fn zeros(&self, shape: &Shape) -> Result<Storage>; fn ones(&self, shape: &Shape) -> Result<Storage>; fn from_slice(&self, data: &[f32], shape: &Shape) -> Result<Storage>; fn add(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>; fn sub(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>; fn mul(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>; fn div(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>; fn sum( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>; fn mean( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>; fn transpose(&self, storage: &Storage, shape: &Shape) -> Result<Storage>; fn to_vec_f32(&self, storage: &Storage) -> Result<Vec<f32>>; fn matmul( &self, lhs: &Storage, rhs: &Storage, lhs_shape: &Shape, rhs_shape: &Shape, ) -> Result<Storage>; fn bmm( &self, lhs: &Storage, rhs: &Storage, lhs_shape: &Shape, rhs_shape: &Shape, ) -> Result<Storage>; fn exp(&self, storage: &Storage) -> Result<Storage>; fn log(&self, storage: &Storage) -> Result<Storage>; fn sqrt(&self, storage: &Storage) -> Result<Storage>; fn pow(&self, storage: &Storage, power: f32) -> Result<Storage>; fn sin(&self, storage: &Storage) -> Result<Storage>; fn cos(&self, storage: &Storage) -> Result<Storage>; fn relu(&self, storage: &Storage) -> Result<Storage>; fn sigmoid(&self, storage: &Storage) -> Result<Storage>; fn tanh(&self, storage: &Storage) -> Result<Storage>; // Provided method fn is_available(&self) -> bool { ... }
}
Expand description

The backend trait for tensor operations The main backend trait that all compute backends must implement.

This trait defines the interface for tensor operations across different hardware backends. Each backend is responsible for:

  • Creating tensors (zeros, ones, from data)
  • Performing arithmetic operations
  • Performing reductions and transformations
  • Converting between storage formats

§Implementation Notes

Backends should be thread-safe (Send + Sync) and implement Debug for diagnostic purposes. The is_available method allows backends to report whether they can be used on the current system.

Required Methods§

Source

fn zeros(&self, shape: &Shape) -> Result<Storage>

Creates a tensor filled with zeros.

Source

fn ones(&self, shape: &Shape) -> Result<Storage>

Creates a tensor filled with ones.

Source

fn from_slice(&self, data: &[f32], shape: &Shape) -> Result<Storage>

Creates a tensor from a slice of f32 values.

Source

fn add(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>

Performs element-wise addition.

Source

fn sub(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>

Performs element-wise subtraction.

Source

fn mul(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>

Performs element-wise multiplication.

Source

fn div(&self, lhs: &Storage, rhs: &Storage) -> Result<Storage>

Performs element-wise division.

Source

fn sum( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>

Computes the sum of elements along an optional axis.

Source

fn mean( &self, storage: &Storage, shape: &Shape, axis: Option<usize>, ) -> Result<Storage>

Computes the mean of elements along an optional axis.

Source

fn transpose(&self, storage: &Storage, shape: &Shape) -> Result<Storage>

Transposes a 2D tensor.

Source

fn to_vec_f32(&self, storage: &Storage) -> Result<Vec<f32>>

Converts the storage to a vector of f32 values.

Source

fn matmul( &self, lhs: &Storage, rhs: &Storage, lhs_shape: &Shape, rhs_shape: &Shape, ) -> Result<Storage>

Matrix multiplication for 2D tensors.

Source

fn bmm( &self, lhs: &Storage, rhs: &Storage, lhs_shape: &Shape, rhs_shape: &Shape, ) -> Result<Storage>

Batched matrix multiplication for 3D tensors.

Source

fn exp(&self, storage: &Storage) -> Result<Storage>

Element-wise exponential function.

Source

fn log(&self, storage: &Storage) -> Result<Storage>

Element-wise natural logarithm.

Source

fn sqrt(&self, storage: &Storage) -> Result<Storage>

Element-wise square root.

Source

fn pow(&self, storage: &Storage, power: f32) -> Result<Storage>

Element-wise power function.

Source

fn sin(&self, storage: &Storage) -> Result<Storage>

Element-wise sine function.

Source

fn cos(&self, storage: &Storage) -> Result<Storage>

Element-wise cosine function.

Source

fn relu(&self, storage: &Storage) -> Result<Storage>

Element-wise ReLU activation function.

Source

fn sigmoid(&self, storage: &Storage) -> Result<Storage>

Element-wise sigmoid activation function.

Source

fn tanh(&self, storage: &Storage) -> Result<Storage>

Element-wise tanh activation function.

Provided Methods§

Source

fn is_available(&self) -> bool

Checks if this backend is available on the current system.

Default implementation returns true. Backends should override this to perform actual availability checks (e.g., CUDA device presence).

Implementors§