Skip to main content

etensor_core/backends/
traits.rs

1//! Standardized trait bounds for hardware execution routing.
2//! 
3//! By enforcing these traits, the `Dispatcher` remains entirely decoupled from 
4//! the underlying physical hardware (CPU, CUDA, MPS, etc.).
5
6use crate::tensor::Tensor;
7use crate::errors::EtensorResult;
8
9/// The foundational contract that all physical hardware backends must fulfill.
10pub trait Backend {
11    /// Executes an element-wise addition (a + b) and returns a dynamically allocated output tensor.
12    fn add(a: &Tensor, b: &Tensor) -> EtensorResult<Tensor>;
13    
14    /// Executes an element-wise multiplication (a * b) and returns a dynamically allocated output tensor.
15    fn mul(a: &Tensor, b: &Tensor) -> EtensorResult<Tensor>;
16    
17    /// Executes matrix multiplication (a @ b) and returns a dynamically allocated output tensor.
18    fn matmul(a: &Tensor, b: &Tensor) -> EtensorResult<Tensor>;
19
20    /// Executes a global sum reduction, collapsing the tensor into a single scalar.
21    fn sum_all(a: &Tensor) -> EtensorResult<Tensor>;
22
23    /// Executes a global mean reduction, collapsing the tensor into a single scalar.
24    fn mean_all(a: &Tensor) -> EtensorResult<Tensor>;
25
26    /// Executes a global max reduction, collapsing the tensor into a single scalar.
27    fn max_all(a: &Tensor) -> EtensorResult<Tensor>;
28
29    /// Executes the Rectified Linear Unit (ReLU) activation function.
30    fn relu(a: &Tensor) -> EtensorResult<Tensor>;
31
32    /// Executes the Sigmoid activation function.
33    fn sigmoid(a: &Tensor) -> EtensorResult<Tensor>;
34
35    /// Executes Fused Addition and ReLU: f(A, B) = max(0, A + B)
36    fn add_relu(a: &Tensor, b: &Tensor) -> EtensorResult<Tensor>;
37
38    /// Executes Fused Linear Layer: y = X @ W + b
39    fn linear(x: &Tensor, w: &Tensor, b: &Tensor) -> EtensorResult<Tensor>;
40    
41}